1 /*
2  * 1394-Based Digital Camera Control Library
3  *
4  * Functions for the manual allocations of ISO ressources.
5  *
6  * Written by David Moore <dcm@acm.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 
23 #include "iso.h"
24 #include "platform.h"
25 #include "internal.h"
26 
27 dc1394error_t
dc1394_iso_set_persist(dc1394camera_t * camera)28 dc1394_iso_set_persist (dc1394camera_t * camera)
29 {
30     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
31     const platform_dispatch_t * d = cpriv->platform->dispatch;
32     if (!d->iso_set_persist)
33         return DC1394_FUNCTION_NOT_SUPPORTED;
34     dc1394error_t err;
35     if ((err = d->iso_set_persist (cpriv->pcam)) != DC1394_SUCCESS)
36         return err;
37 
38     cpriv->iso_persist = 1;
39     return DC1394_SUCCESS;
40 }
41 
42 dc1394error_t
dc1394_iso_allocate_channel(dc1394camera_t * camera,uint64_t channels_allowed,int * channel)43 dc1394_iso_allocate_channel (dc1394camera_t * camera,
44         uint64_t channels_allowed, int * channel)
45 {
46     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
47     dc1394error_t err;
48     const platform_dispatch_t * d = cpriv->platform->dispatch;
49     if (!d->iso_allocate_channel)
50         return DC1394_FUNCTION_NOT_SUPPORTED;
51 
52     if (channels_allowed == 0) {
53         if (camera->bmode_capable)
54             channels_allowed = ~((uint64_t)0);
55         else
56             channels_allowed = 0xffff;
57     }
58 
59     if ((err = d->iso_allocate_channel (cpriv->pcam, channels_allowed,
60             channel)) != DC1394_SUCCESS)
61         return err;
62 
63     cpriv->allocated_channels |= ((uint64_t)1 << *channel);
64     return DC1394_SUCCESS;
65 }
66 
67 dc1394error_t
dc1394_iso_release_channel(dc1394camera_t * camera,int channel)68 dc1394_iso_release_channel (dc1394camera_t * camera, int channel)
69 {
70     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
71     const platform_dispatch_t * d = cpriv->platform->dispatch;
72     if (!d->iso_release_channel)
73         return DC1394_FUNCTION_NOT_SUPPORTED;
74     dc1394error_t err;
75     if ((err = d->iso_release_channel (cpriv->pcam, channel))
76             != DC1394_SUCCESS)
77         return err;
78 
79     cpriv->allocated_channels &= ~((uint64_t)1 << channel);
80     return DC1394_SUCCESS;
81 }
82 
83 dc1394error_t
dc1394_iso_allocate_bandwidth(dc1394camera_t * camera,int bandwidth_units)84 dc1394_iso_allocate_bandwidth (dc1394camera_t * camera, int bandwidth_units)
85 {
86     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
87     const platform_dispatch_t * d = cpriv->platform->dispatch;
88     if (!d->iso_allocate_bandwidth)
89         return DC1394_FUNCTION_NOT_SUPPORTED;
90     dc1394error_t err;
91     if ((err = d->iso_allocate_bandwidth (cpriv->pcam, bandwidth_units))
92             != DC1394_SUCCESS)
93         return err;
94 
95     cpriv->allocated_bandwidth += bandwidth_units;
96     return DC1394_SUCCESS;
97 }
98 
99 dc1394error_t
dc1394_iso_release_bandwidth(dc1394camera_t * camera,int bandwidth_units)100 dc1394_iso_release_bandwidth (dc1394camera_t * camera, int bandwidth_units)
101 {
102     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
103     const platform_dispatch_t * d = cpriv->platform->dispatch;
104     if (!d->iso_release_bandwidth)
105         return DC1394_FUNCTION_NOT_SUPPORTED;
106     dc1394error_t err;
107     if ((err = d->iso_release_bandwidth (cpriv->pcam, bandwidth_units))
108             != DC1394_SUCCESS)
109         return err;
110 
111     cpriv->allocated_bandwidth -= bandwidth_units;
112     if (cpriv->allocated_bandwidth < 0)
113         cpriv->allocated_bandwidth = 0;
114     return DC1394_SUCCESS;
115 }
116 
117 dc1394error_t
dc1394_iso_release_all(dc1394camera_t * camera)118 dc1394_iso_release_all (dc1394camera_t * camera)
119 {
120     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
121     int i;
122     for (i = 0; i < 64; i++)
123         if (cpriv->allocated_channels & ((uint64_t)1 << i))
124             dc1394_iso_release_channel (camera, i);
125     if (cpriv->allocated_bandwidth)
126         dc1394_iso_release_bandwidth (camera, cpriv->allocated_bandwidth);
127 
128     if (cpriv->allocated_bandwidth || cpriv->allocated_channels)
129         return DC1394_FAILURE;
130 
131     return DC1394_SUCCESS;
132 }
133