1 /*
2  * 1394-Based Digital Camera Control Library
3  *
4  * Generic camera control functions
5  *
6  * Written by Damien Douxchamps <ddouxchamps@users.sf.net>
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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 
28 #include "internal.h"
29 #include "offsets.h"
30 
31 dc1394error_t
dc1394_camera_set_broadcast(dc1394camera_t * camera,dc1394bool_t pwr)32 dc1394_camera_set_broadcast(dc1394camera_t *camera, dc1394bool_t pwr)
33 {
34     dc1394camera_priv_t * priv = DC1394_CAMERA_PRIV (camera);
35     const platform_dispatch_t * d = priv->platform->dispatch;
36     if (!d->set_broadcast)
37         return DC1394_FUNCTION_NOT_SUPPORTED;
38     return d->set_broadcast (priv->pcam, pwr);
39 }
40 
41 dc1394error_t
dc1394_camera_get_broadcast(dc1394camera_t * camera,dc1394bool_t * pwr)42 dc1394_camera_get_broadcast(dc1394camera_t *camera, dc1394bool_t *pwr)
43 {
44     dc1394camera_priv_t * priv = DC1394_CAMERA_PRIV (camera);
45     const platform_dispatch_t * d = priv->platform->dispatch;
46     if (!d->get_broadcast)
47         return DC1394_FUNCTION_NOT_SUPPORTED;
48     return d->get_broadcast (priv->pcam, pwr);
49 }
50 
51 dc1394error_t
dc1394_reset_bus(dc1394camera_t * camera)52 dc1394_reset_bus (dc1394camera_t * camera)
53 {
54     dc1394camera_priv_t * priv = DC1394_CAMERA_PRIV (camera);
55     const platform_dispatch_t * d = priv->platform->dispatch;
56     if (!d->reset_bus)
57         return DC1394_FUNCTION_NOT_SUPPORTED;
58     return d->reset_bus (priv->pcam);
59 }
60 
61 dc1394error_t
dc1394_read_cycle_timer(dc1394camera_t * camera,uint32_t * cycle_timer,uint64_t * local_time)62 dc1394_read_cycle_timer (dc1394camera_t * camera,
63                          uint32_t * cycle_timer, uint64_t * local_time)
64 {
65     dc1394camera_priv_t * priv = DC1394_CAMERA_PRIV (camera);
66     const platform_dispatch_t * d = priv->platform->dispatch;
67     if (!d->read_cycle_timer)
68         return DC1394_FUNCTION_NOT_SUPPORTED;
69     return d->read_cycle_timer (priv->pcam, cycle_timer, local_time);
70 }
71 
72 dc1394error_t
dc1394_camera_get_node(dc1394camera_t * camera,uint32_t * node,uint32_t * generation)73 dc1394_camera_get_node (dc1394camera_t *camera, uint32_t *node,
74         uint32_t * generation)
75 {
76     dc1394camera_priv_t * priv = DC1394_CAMERA_PRIV (camera);
77     const platform_dispatch_t * d = priv->platform->dispatch;
78     if (!d->camera_get_node)
79         return DC1394_FUNCTION_NOT_SUPPORTED;
80     return d->camera_get_node (priv->pcam, node, generation);
81 }
82 
83 static dc1394error_t
update_camera_info(dc1394camera_t * camera)84 update_camera_info (dc1394camera_t *camera)
85 {
86     uint32_t value=0, quadval = 0; // set to zero to avoid valgrind errors
87 
88     dc1394_get_control_register(camera, REG_CAMERA_BASIC_FUNC_INQ, &value);
89 
90     int adv_features_capable = value >> 31;
91     camera->has_vmode_error_status = (value >> 30) & 1;
92     camera->has_feature_error_status = (value >> 29) & 1;
93     int opt_function_capable = (value >> 28) & 1;
94     camera->bmode_capable = (value >> 23) & 1;
95     camera->can_switch_on_off = (value >> 15) & 1;
96     camera->one_shot_capable = (value >> 12) & 1;
97     camera->multi_shot_capable = (value >> 11) & 1;
98     camera->max_mem_channel    = value & 0xf;
99 
100     if (adv_features_capable)
101         if (dc1394_get_control_register (camera, REG_CAMERA_ADV_FEATURE_INQ,
102                                          &quadval) == DC1394_SUCCESS)
103             camera->advanced_features_csr = (uint64_t) quadval * 4;
104 
105     if (opt_function_capable) {
106         value = 0;
107         dc1394_get_control_register (camera, REG_CAMERA_OPT_FUNC_INQ, &value);
108         if ((value >> 30) & 1)
109             if (dc1394_get_control_register (camera, REG_CAMERA_PIO_CONTROL_CSR_INQ, &quadval) == DC1394_SUCCESS)
110                 camera->PIO_control_csr = (uint64_t) quadval * 4;
111         if ((value >> 29) & 1)
112             if (dc1394_get_control_register (camera, REG_CAMERA_SIO_CONTROL_CSR_INQ, &quadval) == DC1394_SUCCESS)
113                 camera->SIO_control_csr = (uint64_t) quadval * 4;
114         if ((value >> 28) & 1)
115             if (dc1394_get_control_register (camera, REG_CAMERA_STROBE_CONTROL_CSR_INQ, &quadval) == DC1394_SUCCESS)
116                 camera->strobe_control_csr = (uint64_t) quadval * 4;
117     }
118 
119     // verify that the iso speed, the video mode and the framerates are OK
120     // at boot time
121 
122     /* get the current ISO speed, and verify it*/
123     dc1394error_t err;
124     dc1394speed_t iso_speed;
125     err=dc1394_video_get_iso_speed(camera, &iso_speed);
126     if (err==DC1394_INVALID_ISO_SPEED) {
127         // default to the most probable speed: 400 Mbps
128         dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
129     }
130 
131     /* get the current video mode, and verify it*/
132     dc1394video_modes_t modes;
133     dc1394video_mode_t video_mode;
134     err=dc1394_video_get_mode(camera, &video_mode);
135     if (err==DC1394_INVALID_VIDEO_FORMAT) {
136         // a proper video mode may not be present. Try to set a default video mode
137         dc1394_video_get_supported_modes(camera,&modes);
138 
139         dc1394_video_set_mode(camera,modes.modes[0]);
140     }
141 
142     /* get the current framerate, and verify it*/
143     dc1394framerate_t framerate;
144     dc1394framerates_t framerates;
145     err=dc1394_video_get_framerate(camera, &framerate);
146     if (err==DC1394_INVALID_FRAMERATE) {
147         // a proper framerate may not be present. Try to set a default framerate
148         dc1394_video_get_supported_framerates(camera,video_mode,&framerates);
149 
150         dc1394_video_set_framerate(camera,framerates.framerates[0]);
151     }
152 
153     return DC1394_SUCCESS;
154 }
155 
156 dc1394error_t
dc1394_camera_print_info(dc1394camera_t * camera,FILE * fd)157 dc1394_camera_print_info(dc1394camera_t *camera, FILE* fd)
158 {
159     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
160     uint32_t value[2];
161 
162     value[0]= camera->guid & 0xffffffff;
163     value[1]= (camera->guid >>32) & 0xffffffff;
164     fprintf(fd,"------ Camera information ------\n");
165     fprintf(fd,"Vendor                            :     %s\n", camera->vendor);
166     fprintf(fd,"Model                             :     %s\n", camera->model);
167     fprintf(fd,"Unit                              :     %d\n", camera->unit);
168     fprintf(fd,"Specifications ID                 :     0x%x\n", camera->unit_spec_ID);
169     fprintf(fd,"Software revision                 :     0x%x\n", camera->unit_sw_version);
170     fprintf(fd,"IIDC version code                 :     %d\n", camera->iidc_version);
171     fprintf(fd,"Unit directory offset             :     0x%x\n", camera->unit_directory);
172     fprintf(fd,"Unit dependent directory offset   :     0x%x\n", camera->unit_dependent_directory);
173     fprintf(fd,"Commands registers base           :     0x%x\n", camera->command_registers_base);
174     fprintf(fd,"Unique ID                         :     0x%08x%08x\n", value[1], value[0]);
175     fprintf(fd,"Vendor ID                         :     0x%x\n", camera->vendor_id);
176     fprintf(fd,"Model ID                          :     0x%x\n", camera->model_id);
177     if (camera->advanced_features_csr>0)
178         fprintf(fd,"Advanced features found at offset :     0x%"PRIx64"\n", camera->advanced_features_csr);
179     fprintf(fd,"1394b mode capable (>=800Mbit/s)  :     ");
180     if (camera->bmode_capable==DC1394_TRUE)
181         fprintf(fd,"Yes\n");
182     else
183         fprintf(fd,"No\n");
184 
185     fprintf(fd,"Platform backend                  :     %s\n",
186             cpriv->platform->name);
187     const platform_dispatch_t * d = cpriv->platform->dispatch;
188     if (d->camera_print_info)
189         d->camera_print_info (cpriv->pcam, fd);
190 
191     return DC1394_SUCCESS;
192 }
193 
194 dc1394error_t
dc1394_camera_get_platform_string(dc1394camera_t * camera,const char ** platform)195 dc1394_camera_get_platform_string(dc1394camera_t *camera, const char **platform)
196 {
197     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
198 	*platform=cpriv->platform->name;
199 
200 	return DC1394_SUCCESS;
201 }
202 
203 /*****************************************************
204  dc1394_get_camera_feature_set
205 
206  Collects the available features for the camera
207  described by node and stores them in features.
208 *****************************************************/
209 dc1394error_t
dc1394_feature_get_all(dc1394camera_t * camera,dc1394featureset_t * features)210 dc1394_feature_get_all(dc1394camera_t *camera, dc1394featureset_t *features)
211 {
212     uint32_t i, j;
213     dc1394error_t err=DC1394_SUCCESS;
214 
215     for (i= DC1394_FEATURE_MIN, j= 0; i <= DC1394_FEATURE_MAX; i++, j++)  {
216         features->feature[j].id= i;
217         err=dc1394_feature_get(camera, &features->feature[j]);
218         DC1394_ERR_RTN(err, "Could not get camera feature");
219     }
220 
221     return err;
222 }
223 
224 /*****************************************************
225  dc1394_get_camera_feature
226 
227  Stores the bounds and options associated with the
228  feature described by feature->id
229 *****************************************************/
230 dc1394error_t
dc1394_feature_get(dc1394camera_t * camera,dc1394feature_info_t * feature)231 dc1394_feature_get(dc1394camera_t *camera, dc1394feature_info_t *feature)
232 {
233     uint64_t offset;
234     uint32_t value;
235     dc1394error_t err;
236 
237     if ( (feature->id < DC1394_FEATURE_MIN) || (feature->id > DC1394_FEATURE_MAX) ) {
238         return DC1394_INVALID_FEATURE;
239     }
240 
241     // check presence
242     err=dc1394_feature_is_present(camera, feature->id, &(feature->available));
243     DC1394_ERR_RTN(err, "Could not check feature presence");
244 
245     if (feature->available == DC1394_FALSE) {
246         return DC1394_SUCCESS;
247     }
248 
249     // get capabilities
250     FEATURE_TO_INQUIRY_OFFSET(feature->id, offset);
251     err=dc1394_get_control_register(camera, offset, &value);
252     DC1394_ERR_RTN(err, "Could not check feature characteristics");
253 
254     dc1394_feature_get_modes(camera, feature->id, &feature->modes);
255     dc1394_feature_get_mode(camera, feature->id, &feature->current_mode);
256 
257     switch (feature->id) {
258     case DC1394_FEATURE_TRIGGER:
259         feature->polarity_capable= (value & 0x02000000UL) ? DC1394_TRUE : DC1394_FALSE;
260         int i, j;
261         uint32_t value_tmp;
262 
263         feature->trigger_modes.num=0;
264         value_tmp= (value & (0xFFFF));
265 
266         for (i=DC1394_TRIGGER_MODE_MIN;i<=DC1394_TRIGGER_MODE_MAX;i++) {
267             j = i - DC1394_TRIGGER_MODE_MIN;
268             if (value_tmp & (0x1 << (15-j-(j>5)*8))) { // (i>5)*8 to take the mode gap into account
269                 feature->trigger_modes.modes[feature->trigger_modes.num]=i;
270                 feature->trigger_modes.num++;
271             }
272         }
273 
274         err=dc1394_external_trigger_get_supported_sources(camera,&feature->trigger_sources);
275         DC1394_ERR_RTN(err, "Could not get supported trigger sources");
276 
277         break;
278     default:
279         feature->polarity_capable = 0;
280         feature->trigger_mode     = 0;
281 
282         feature->min= (value & 0xFFF000UL) >> 12;
283         feature->max= (value & 0xFFFUL);
284         break;
285     }
286 
287     feature->absolute_capable = (value & 0x40000000UL) ? DC1394_TRUE : DC1394_FALSE;
288     feature->readout_capable  = (value & 0x08000000UL) ? DC1394_TRUE : DC1394_FALSE;
289     feature->on_off_capable   = (value & 0x04000000UL) ? DC1394_TRUE : DC1394_FALSE;
290 
291     // get current values
292     FEATURE_TO_VALUE_OFFSET(feature->id, offset);
293 
294     err=dc1394_get_control_register(camera, offset, &value);
295     DC1394_ERR_RTN(err, "Could not get feature register");
296 
297     switch (feature->id) {
298     case DC1394_FEATURE_TRIGGER:
299         feature->trigger_polarity= (value & 0x01000000UL) ? DC1394_TRUE : DC1394_FALSE;
300         feature->trigger_mode= (uint32_t)((value >> 16) & 0xF);
301         if (feature->trigger_mode >= 14)
302             feature->trigger_mode += DC1394_TRIGGER_MODE_MIN - 8;
303         else
304             feature->trigger_mode += DC1394_TRIGGER_MODE_MIN;
305         feature->trigger_source = (uint32_t)((value >> 21) & 0x7UL);
306         if (feature->trigger_source > 3)
307             feature->trigger_source -= 3;
308         feature->trigger_source += DC1394_TRIGGER_SOURCE_MIN;
309         break;
310     default:
311         break;
312     }
313 
314     feature->is_on= (value & 0x02000000UL) ? DC1394_TRUE : DC1394_FALSE;
315 
316     switch (feature->id) {
317     case DC1394_FEATURE_WHITE_BALANCE:
318         feature->RV_value= value & 0xFFFUL;
319         feature->BU_value= (value & 0xFFF000UL) >> 12;
320         break;
321     case DC1394_FEATURE_WHITE_SHADING:
322         feature->R_value=value & 0xFFUL;
323         feature->G_value=(value & 0xFF00UL)>>8;
324         feature->B_value=(value & 0xFF0000UL)>>16;
325         break;
326     case DC1394_FEATURE_TEMPERATURE:
327         feature->value= value & 0xFFFUL;
328         feature->target_value= value & 0xFFF000UL;
329         break;
330     default:
331         feature->value= value & 0xFFFUL;
332         break;
333     }
334 
335     if (feature->absolute_capable>0) {
336         err=dc1394_feature_get_absolute_boundaries(camera, feature->id, &feature->abs_min, &feature->abs_max);
337         DC1394_ERR_RTN(err, "Could not get feature absolute min/max");
338         err=dc1394_feature_get_absolute_value(camera, feature->id, &feature->abs_value);
339         DC1394_ERR_RTN(err, "Could not get feature absolute value");
340         err=dc1394_feature_get_absolute_control(camera, feature->id, &feature->abs_control);
341         DC1394_ERR_RTN(err, "Could not get feature absolute control");
342     }
343 
344     return err;
345 }
346 
347 /*****************************************************
348  dc1394_print_feature
349 
350  Displays the bounds and options of the given feature
351 *****************************************************/
352 dc1394error_t
dc1394_feature_print(dc1394feature_info_t * f,FILE * fd)353 dc1394_feature_print(dc1394feature_info_t *f, FILE *fd)
354 {
355     int fid= f->id;
356 
357     if ( (fid < DC1394_FEATURE_MIN) || (fid > DC1394_FEATURE_MAX) ) {
358         return DC1394_INVALID_FEATURE;
359     }
360     const char *feature_string = dc1394_feature_get_string (fid);
361     fprintf(fd,"%s:\n\t", feature_string);
362 
363     if (!f->available) {
364         fprintf(fd,"NOT AVAILABLE\n");
365         return DC1394_SUCCESS;
366     }
367 
368     if (f->readout_capable)
369         fprintf(fd,"RC  ");
370     if (f->on_off_capable)
371         fprintf(fd,"O/OC  ");
372     int i;
373     for (i=0;i<f->modes.num;i++) {
374         switch (f->modes.modes[i]) {
375         case DC1394_FEATURE_MODE_MANUAL:
376             fprintf(fd,"MC  ");
377             break;
378         case DC1394_FEATURE_MODE_AUTO:
379             fprintf(fd,"AC  ");
380             break;
381         case DC1394_FEATURE_MODE_ONE_PUSH_AUTO:
382             fprintf(fd,"OP  ");
383             break;
384         }
385         fprintf(fd,"(active is: ");
386         switch (f->current_mode) {
387         case DC1394_FEATURE_MODE_MANUAL:
388             fprintf(fd,"MAN)  ");
389             break;
390         case DC1394_FEATURE_MODE_AUTO:
391             fprintf(fd,"AUTO)  ");
392             break;
393         case DC1394_FEATURE_MODE_ONE_PUSH_AUTO:
394             fprintf(fd,"ONE PUSH)  ");
395             break;
396         }
397 
398     }
399     if (f->absolute_capable)
400         fprintf(fd,"ABS  ");
401     fprintf(fd,"\n");
402 
403     if (f->on_off_capable) {
404         if (f->is_on)
405             fprintf(fd,"\tFeature: ON  ");
406         else
407             fprintf(fd,"\tFeature: OFF  ");
408     }
409     else
410         fprintf(fd,"\t");
411 
412     if (fid != DC1394_FEATURE_TRIGGER)
413         fprintf(fd,"min: %d max %d\n", f->min, f->max);
414 
415     switch(fid) {
416     case DC1394_FEATURE_TRIGGER:
417         fprintf(fd,"\n\tAvailableTriggerModes: ");
418         if (f->trigger_modes.num==0) {
419             fprintf(fd,"none");
420         }
421         else {
422             int i;
423             for (i=0;i<f->trigger_modes.num;i++) {
424                 fprintf(fd,"%d ",f->trigger_modes.modes[i]);
425             }
426         }
427         fprintf(fd,"\n\tAvailableTriggerSources: ");
428         if (f->trigger_sources.num==0) {
429             fprintf(fd,"none");
430         }
431         else {
432             int i;
433             for (i=0;i<f->trigger_sources.num;i++) {
434                 fprintf(fd,"%d ",f->trigger_sources.sources[i]);
435             }
436         }
437         fprintf(fd,"\n\tPolarity Change Capable: ");
438 
439         if (f->polarity_capable)
440             fprintf(fd,"True");
441         else
442             fprintf(fd,"False");
443 
444         fprintf(fd,"\n\tCurrent Polarity: ");
445 
446         if (f->trigger_polarity)
447             fprintf(fd,"POS");
448         else
449             fprintf(fd,"NEG");
450 
451         fprintf(fd,"\n\tcurrent mode: %d\n", f->trigger_mode);
452         if (f->trigger_sources.num>0) {
453             fprintf(fd,"\n\tcurrent source: %d\n", f->trigger_source);
454         }
455         break;
456     case DC1394_FEATURE_WHITE_BALANCE:
457         fprintf(fd,"\tB/U value: %d R/V value: %d\n", f->BU_value, f->RV_value);
458         break;
459     case DC1394_FEATURE_TEMPERATURE:
460         fprintf(fd,"\tTarget temp: %d Current Temp: %d\n", f->target_value, f->value);
461         break;
462     case DC1394_FEATURE_WHITE_SHADING:
463         fprintf(fd,"\tR value: %d G value: %d B value: %d\n", f->R_value,
464                 f->G_value, f->B_value);
465         break;
466     default:
467         fprintf(fd,"\tcurrent value is: %d\n",f->value);
468         break;
469     }
470     if (f->absolute_capable)
471         fprintf(fd,"\tabsolute settings:\n\t value: %f\n\t min: %f\n\t max: %f\n", f->abs_value,f->abs_min,f->abs_max);
472 
473     return DC1394_SUCCESS;
474 }
475 
476 /*****************************************************
477  dc1394_print_feature_set
478 
479  Displays the entire feature set stored in features
480 *****************************************************/
481 dc1394error_t
dc1394_feature_print_all(dc1394featureset_t * features,FILE * fd)482 dc1394_feature_print_all(dc1394featureset_t *features, FILE *fd)
483 {
484     uint32_t i, j;
485     dc1394error_t err=DC1394_SUCCESS;
486 
487     fprintf(fd,"------ Features report ------\n");
488     fprintf(fd,"OP   - one push capable\n");
489     fprintf(fd,"RC   - readout capable\n");
490     fprintf(fd,"O/OC - on/off capable\n");
491     fprintf(fd,"AC   - auto capable\n");
492     fprintf(fd,"MC   - manual capable\n");
493     fprintf(fd,"ABS  - absolute capable\n");
494     fprintf(fd,"-----------------------------\n");
495 
496     for (i= DC1394_FEATURE_MIN, j= 0; i <= DC1394_FEATURE_MAX; i++, j++)  {
497         err=dc1394_feature_print(&features->feature[j], fd);
498         DC1394_ERR_RTN(err, "Could not print feature");
499     }
500 
501     return err;
502 }
503 
504 dc1394error_t
dc1394_camera_reset(dc1394camera_t * camera)505 dc1394_camera_reset(dc1394camera_t *camera)
506 {
507     dc1394error_t err;
508     err=dc1394_set_control_register(camera, REG_CAMERA_INITIALIZE, DC1394_FEATURE_ON);
509     DC1394_ERR_RTN(err, "Could not reset the camera");
510     return err;
511 }
512 
513 dc1394error_t
dc1394_video_get_supported_modes(dc1394camera_t * camera,dc1394video_modes_t * modes)514 dc1394_video_get_supported_modes(dc1394camera_t *camera, dc1394video_modes_t *modes)
515 {
516     dc1394error_t err;
517     uint32_t value, sup_formats;
518     dc1394video_mode_t mode;
519 
520     // get supported formats
521     err=dc1394_get_control_register(camera, REG_CAMERA_V_FORMAT_INQ, &sup_formats);
522     DC1394_ERR_RTN(err, "Could not get supported formats");
523 
524     // for each format check supported modes and add them as we find them.
525 
526     modes->num=0;
527     // Format_0
528     if ((sup_formats & (0x1 << (31-(DC1394_FORMAT0-DC1394_FORMAT_MIN)))) > 0) {
529         err=dc1394_get_control_register(camera, REG_CAMERA_V_MODE_INQ_BASE + ((DC1394_FORMAT0-DC1394_FORMAT_MIN) * 0x04U), &value);
530         DC1394_ERR_RTN(err, "Could not get supported modes for Format_0");
531 
532         for (mode=DC1394_VIDEO_MODE_FORMAT0_MIN;mode<=DC1394_VIDEO_MODE_FORMAT0_MAX;mode++) {
533             if ((value & (0x1<<(31-(mode-DC1394_VIDEO_MODE_FORMAT0_MIN)))) > 0) {
534                 modes->modes[modes->num]=mode;
535                 modes->num++;
536             }
537         }
538     }
539     // Format_1
540     if ((sup_formats & (0x1 << (31-(DC1394_FORMAT1-DC1394_FORMAT_MIN)))) > 0) {
541         err=dc1394_get_control_register(camera, REG_CAMERA_V_MODE_INQ_BASE + ((DC1394_FORMAT1-DC1394_FORMAT_MIN) * 0x04U), &value);
542         DC1394_ERR_RTN(err, "Could not get supported modes for Format_1");
543 
544         for (mode=DC1394_VIDEO_MODE_FORMAT1_MIN;mode<=DC1394_VIDEO_MODE_FORMAT1_MAX;mode++) {
545             if ((value & (0x1<<(31-(mode-DC1394_VIDEO_MODE_FORMAT1_MIN)))) > 0) {
546                 modes->modes[modes->num]=mode;
547                 modes->num++;
548             }
549         }
550     }
551     // Format_2
552     if ((sup_formats & (0x1 << (31-(DC1394_FORMAT2-DC1394_FORMAT_MIN)))) > 0) {
553         err=dc1394_get_control_register(camera, REG_CAMERA_V_MODE_INQ_BASE + ((DC1394_FORMAT2-DC1394_FORMAT_MIN) * 0x04U), &value);
554         DC1394_ERR_RTN(err, "Could not get supported modes for Format_2");
555 
556         for (mode=DC1394_VIDEO_MODE_FORMAT2_MIN;mode<=DC1394_VIDEO_MODE_FORMAT2_MAX;mode++) {
557             if ((value & (0x1<<(31-(mode-DC1394_VIDEO_MODE_FORMAT2_MIN)))) > 0) {
558                 modes->modes[modes->num]=mode;
559                 modes->num++;
560             }
561         }
562     }
563     // Format_6
564     if ((sup_formats & (0x1 << (31-(DC1394_FORMAT6-DC1394_FORMAT_MIN)))) > 0) {
565         err=dc1394_get_control_register(camera, REG_CAMERA_V_MODE_INQ_BASE + ((DC1394_FORMAT6-DC1394_FORMAT_MIN) * 0x04U), &value);
566         DC1394_ERR_RTN(err, "Could not get supported modes for Format_3");
567 
568         for (mode=DC1394_VIDEO_MODE_FORMAT6_MIN;mode<=DC1394_VIDEO_MODE_FORMAT6_MAX;mode++) {
569             if ((value & (0x1<<(31-(mode-DC1394_VIDEO_MODE_FORMAT6_MIN))))>0) {
570                 modes->modes[modes->num]=mode;
571                 modes->num++;
572             }
573         }
574     }
575     // Format_7
576     if ((sup_formats & (0x1 << (31-(DC1394_FORMAT7-DC1394_FORMAT_MIN)))) > 0) {
577         err=dc1394_get_control_register(camera, REG_CAMERA_V_MODE_INQ_BASE + ((DC1394_FORMAT7-DC1394_FORMAT_MIN) * 0x04U), &value);
578         DC1394_ERR_RTN(err, "Could not get supported modes for Format_4");
579 
580         for (mode=DC1394_VIDEO_MODE_FORMAT7_MIN;mode<=DC1394_VIDEO_MODE_FORMAT7_MAX;mode++) {
581             if ((value & (0x1<<(31-(mode-DC1394_VIDEO_MODE_FORMAT7_MIN))))>0) {
582                 modes->modes[modes->num]=mode;
583                 modes->num++;
584             }
585         }
586     }
587 
588     return err;
589 }
590 
591 dc1394error_t
dc1394_video_get_supported_framerates(dc1394camera_t * camera,dc1394video_mode_t video_mode,dc1394framerates_t * framerates)592 dc1394_video_get_supported_framerates(dc1394camera_t *camera, dc1394video_mode_t video_mode, dc1394framerates_t *framerates)
593 {
594     dc1394framerate_t framerate;
595     dc1394error_t err;
596     uint32_t format;
597     uint32_t value;
598 
599     err=get_format_from_mode(video_mode, &format);
600     DC1394_ERR_RTN(err, "Invalid mode code");
601 
602     if ((format==DC1394_FORMAT6)||(format==DC1394_FORMAT7)) {
603         err=DC1394_INVALID_VIDEO_FORMAT;
604         DC1394_ERR_RTN(err, "Modes corresponding for format6 and format7 do not have framerates!");
605     }
606 
607     switch (format) {
608     case DC1394_FORMAT0:
609         video_mode-=DC1394_VIDEO_MODE_FORMAT0_MIN;
610         break;
611     case DC1394_FORMAT1:
612         video_mode-=DC1394_VIDEO_MODE_FORMAT1_MIN;
613         break;
614     case DC1394_FORMAT2:
615         video_mode-=DC1394_VIDEO_MODE_FORMAT2_MIN;
616         break;
617     }
618     format-=DC1394_FORMAT_MIN;
619 
620 
621     err=dc1394_get_control_register(camera,REG_CAMERA_V_RATE_INQ_BASE + (format * 0x20U) + (video_mode * 0x04U), &value);
622     DC1394_ERR_RTN(err, "Could not get supported framerates");
623 
624     framerates->num=0;
625     for (framerate=DC1394_FRAMERATE_MIN;framerate<=DC1394_FRAMERATE_MAX;framerate++) {
626         if ((value & (0x1<<(31-(framerate-DC1394_FRAMERATE_MIN))))>0) {
627             framerates->framerates[framerates->num]=framerate;
628             framerates->num++;
629         }
630     }
631 
632     return err;
633 }
634 
635 
636 dc1394error_t
dc1394_video_get_framerate(dc1394camera_t * camera,dc1394framerate_t * framerate)637 dc1394_video_get_framerate(dc1394camera_t *camera, dc1394framerate_t *framerate)
638 {
639     uint32_t value;
640     dc1394error_t err;
641 
642     err=dc1394_get_control_register(camera, REG_CAMERA_FRAME_RATE, &value);
643     DC1394_ERR_RTN(err, "Could not get video framerate");
644 
645     *framerate= (uint32_t)((value >> 29) & 0x7UL) + DC1394_FRAMERATE_MIN;
646 
647     return err;
648 }
649 
650 dc1394error_t
dc1394_video_set_framerate(dc1394camera_t * camera,dc1394framerate_t framerate)651 dc1394_video_set_framerate(dc1394camera_t *camera, dc1394framerate_t framerate)
652 {
653     dc1394error_t err;
654     if ( (framerate < DC1394_FRAMERATE_MIN) || (framerate > DC1394_FRAMERATE_MAX) ) {
655         return DC1394_INVALID_FRAMERATE;
656     }
657 
658     err=dc1394_set_control_register(camera, REG_CAMERA_FRAME_RATE, (uint32_t)(((framerate - DC1394_FRAMERATE_MIN) & 0x7UL) << 29));
659     DC1394_ERR_RTN(err, "Could not set video framerate");
660 
661     return err;
662 }
663 
664 dc1394error_t
dc1394_video_get_mode(dc1394camera_t * camera,dc1394video_mode_t * mode)665 dc1394_video_get_mode(dc1394camera_t *camera, dc1394video_mode_t *mode)
666 {
667     dc1394error_t err;
668     uint32_t value = 0; // set to zero to avoid valgrind errors
669     uint32_t format = 0; // set to zero to avoid valgrind errors
670 
671     err= dc1394_get_control_register(camera, REG_CAMERA_VIDEO_FORMAT, &value);
672     DC1394_ERR_RTN(err, "Could not get video format");
673 
674     format= (uint32_t)((value >> 29) & 0x7UL) + DC1394_FORMAT_MIN;
675 
676     err= dc1394_get_control_register(camera, REG_CAMERA_VIDEO_MODE, &value);
677     DC1394_ERR_RTN(err, "Could not get video mode");
678 
679     switch(format) {
680     case DC1394_FORMAT0:
681         *mode= (uint32_t)((value >> 29) & 0x7UL) + DC1394_VIDEO_MODE_FORMAT0_MIN;
682         break;
683     case DC1394_FORMAT1:
684         *mode= (uint32_t)((value >> 29) & 0x7UL) + DC1394_VIDEO_MODE_FORMAT1_MIN;
685         break;
686     case DC1394_FORMAT2:
687         *mode= (uint32_t)((value >> 29) & 0x7UL) + DC1394_VIDEO_MODE_FORMAT2_MIN;
688         break;
689     case DC1394_FORMAT6:
690         *mode= (uint32_t)((value >> 29) & 0x7UL) + DC1394_VIDEO_MODE_FORMAT6_MIN;
691         break;
692     case DC1394_FORMAT7:
693         *mode= (uint32_t)((value >> 29) & 0x7UL) + DC1394_VIDEO_MODE_FORMAT7_MIN;
694         break;
695     default:
696         return DC1394_INVALID_VIDEO_FORMAT;
697         break;
698     }
699 
700     return err;
701 }
702 
703 dc1394error_t
dc1394_video_set_mode(dc1394camera_t * camera,dc1394video_mode_t mode)704 dc1394_video_set_mode(dc1394camera_t *camera, dc1394video_mode_t  mode)
705 {
706     uint32_t format, min;
707     dc1394error_t err;
708 
709     if ( (mode<DC1394_VIDEO_MODE_MIN) || (mode>DC1394_VIDEO_MODE_MAX) )
710         return DC1394_INVALID_VIDEO_MODE;
711 
712     err=get_format_from_mode(mode, &format);
713     DC1394_ERR_RTN(err, "Invalid video mode code");
714 
715     switch(format) {
716     case DC1394_FORMAT0:
717         min= DC1394_VIDEO_MODE_FORMAT0_MIN;
718         break;
719     case DC1394_FORMAT1:
720         min= DC1394_VIDEO_MODE_FORMAT1_MIN;
721         break;
722     case DC1394_FORMAT2:
723         min= DC1394_VIDEO_MODE_FORMAT2_MIN;
724         break;
725     case DC1394_FORMAT6:
726         min= DC1394_VIDEO_MODE_FORMAT6_MIN;
727         break;
728     case DC1394_FORMAT7:
729         min= DC1394_VIDEO_MODE_FORMAT7_MIN;
730         break;
731     default:
732         return DC1394_INVALID_VIDEO_MODE;
733         break;
734     }
735 
736     //if (format>FORMAT2)
737     //  format+=DC1394_FORMAT_GAP;
738 
739     err=dc1394_set_control_register(camera, REG_CAMERA_VIDEO_FORMAT, (uint32_t)(((format - DC1394_FORMAT_MIN) & 0x7UL) << 29));
740     DC1394_ERR_RTN(err, "Could not set video format");
741 
742     err=dc1394_set_control_register(camera, REG_CAMERA_VIDEO_MODE, (uint32_t)(((mode - min) & 0x7UL) << 29));
743     DC1394_ERR_RTN(err, "Could not set video mode");
744 
745     return err;
746 
747 }
748 
749 dc1394error_t
dc1394_video_get_iso_speed(dc1394camera_t * camera,dc1394speed_t * speed)750 dc1394_video_get_iso_speed(dc1394camera_t *camera, dc1394speed_t *speed)
751 {
752     dc1394error_t err;
753     uint32_t value;
754 
755     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
756     DC1394_ERR_RTN(err, "Could not get ISO data");
757 
758     if (camera->bmode_capable) { // check if 1394b is available
759         if (value & 0x00008000) { //check if we are now using 1394b
760             *speed= (uint32_t)(value& 0x7UL);
761             if ((*speed<DC1394_ISO_SPEED_MIN)||(*speed>DC1394_ISO_SPEED_MAX)) // abort if speed not within valid range
762                 return DC1394_INVALID_ISO_SPEED;
763         }
764         else { // fallback to legacy
765             *speed= (uint32_t)((value >> 24) & 0x3UL);
766             if ((*speed<DC1394_ISO_SPEED_MIN)||(*speed>DC1394_ISO_SPEED_400)) // abort if speed not within valid range
767                 return DC1394_INVALID_ISO_SPEED;
768         }
769     }
770     else { // legacy
771         *speed= (uint32_t)((value >> 24) & 0x3UL);
772         if ((*speed<DC1394_ISO_SPEED_MIN)||(*speed>DC1394_ISO_SPEED_400)) // abort if speed not within valid range
773             return DC1394_INVALID_ISO_SPEED;
774     }
775 
776     return err;
777 }
778 
779 dc1394error_t
dc1394_video_set_iso_speed(dc1394camera_t * camera,dc1394speed_t speed)780 dc1394_video_set_iso_speed(dc1394camera_t *camera, dc1394speed_t speed)
781 {
782     dc1394error_t err;
783     uint32_t value=0;
784     int channel;
785 
786     if ((speed>DC1394_ISO_SPEED_MAX) || (speed<DC1394_ISO_SPEED_MIN))
787         return DC1394_INVALID_ISO_SPEED;
788 
789     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
790     DC1394_ERR_RTN(err, "Could not get ISO data");
791 
792     // check if 1394b is available and if we are now using 1394b
793     if ((camera->bmode_capable)&&(value & 0x00008000)) {
794         err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
795         DC1394_ERR_RTN(err, "oops");
796         channel=(value >> 8) & 0x3FUL;
797         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA,
798                                         (uint32_t) ( ((channel & 0x3FUL) << 8) | (speed & 0x7UL) | (0x1 << 15) ));
799         DC1394_ERR_RTN(err, "oops");
800     }
801     else { // fallback to legacy
802         if (speed>DC1394_ISO_SPEED_400-DC1394_ISO_SPEED_MIN) {
803             dc1394_log_error("An ISO speed >400Mbps was requested while the camera is in LEGACY mode. Please set the operation mode to OPERATION_MODE_1394B before asking for 1394b ISO speeds");
804             return DC1394_INVALID_ISO_SPEED;
805         }
806         err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
807         DC1394_ERR_RTN(err, "oops");
808         channel=(value >> 28) & 0xFUL;
809         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA,
810                                         (uint32_t) (((channel & 0xFUL) << 28) |
811                                                     ((speed & 0x3UL) << 24) ));
812         DC1394_ERR_RTN(err, "Could not set ISO data register");
813     }
814 
815     return err;;
816 }
817 
818 dc1394error_t
dc1394_video_get_iso_channel(dc1394camera_t * camera,uint32_t * channel)819 dc1394_video_get_iso_channel(dc1394camera_t *camera, uint32_t * channel)
820 {
821     dc1394error_t err;
822     uint32_t value_inq, value;
823 
824     err=dc1394_get_control_register(camera, REG_CAMERA_BASIC_FUNC_INQ, &value_inq);
825     DC1394_ERR_RTN(err, "Could not get basic function register");
826 
827     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
828     DC1394_ERR_RTN(err, "Could not get ISO data");
829 
830     // check if 1394b is available and if we are now using 1394b
831     if ((value_inq & 0x00800000)&&(value & 0x00008000))
832         *channel = (value >> 8) & 0x3FUL;
833     else
834         *channel = (value >> 28) & 0xFUL;
835 
836     return DC1394_SUCCESS;
837 }
838 
839 dc1394error_t
dc1394_video_set_iso_channel(dc1394camera_t * camera,uint32_t channel)840 dc1394_video_set_iso_channel(dc1394camera_t *camera, uint32_t channel)
841 {
842     dc1394error_t err;
843     uint32_t value_inq, value=0;
844     int speed;
845 
846     err=dc1394_get_control_register(camera, REG_CAMERA_BASIC_FUNC_INQ, &value_inq);
847     DC1394_ERR_RTN(err, "Could not get basic function register");
848 
849     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
850     DC1394_ERR_RTN(err, "Could not get ISO data");
851 
852     // check if 1394b is available and if we are now using 1394b
853     if ((value_inq & 0x00800000)&&(value & 0x00008000)) {
854         err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
855         DC1394_ERR_RTN(err, "oops");
856         speed=value & 0x7UL;
857         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA,
858                                         (uint32_t) ( ((channel & 0x3FUL) << 8) | (speed & 0x7UL) | (0x1 << 15) ));
859         DC1394_ERR_RTN(err, "oops");
860     }
861     else { // fallback to legacy
862         err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
863         DC1394_ERR_RTN(err, "oops");
864         speed=(value >> 24) & 0x3UL;
865         if (speed>DC1394_ISO_SPEED_400-DC1394_ISO_SPEED_MIN) {
866             dc1394_log_error("an ISO speed >400Mbps was requested while the camera is in LEGACY mode              Please set the operation mode to OPERATION_MODE_1394B before asking for\n              1394b ISO speeds");
867             return DC1394_FAILURE;
868         }
869         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA,
870                                         (uint32_t) (((channel & 0xFUL) << 28) | ((speed & 0x3UL) << 24) ));
871         DC1394_ERR_RTN(err, "Could not set ISO data register");
872     }
873 
874     return err;
875 }
876 
877 dc1394error_t
dc1394_video_get_operation_mode(dc1394camera_t * camera,dc1394operation_mode_t * mode)878 dc1394_video_get_operation_mode(dc1394camera_t *camera, dc1394operation_mode_t  *mode)
879 {
880     dc1394error_t err;
881     uint32_t value;
882 
883     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
884     DC1394_ERR_RTN(err, "Could not get ISO data");
885 
886     if (camera->bmode_capable==DC1394_TRUE) {
887         if ((value & 0x00008000) >0)
888             *mode=DC1394_OPERATION_MODE_1394B;
889         else
890             *mode=DC1394_OPERATION_MODE_LEGACY;
891     }
892     else {
893         *mode=DC1394_OPERATION_MODE_LEGACY;
894     }
895 
896     return err;
897 }
898 
899 
900 dc1394error_t
dc1394_video_set_operation_mode(dc1394camera_t * camera,dc1394operation_mode_t mode)901 dc1394_video_set_operation_mode(dc1394camera_t *camera, dc1394operation_mode_t  mode)
902 {
903     dc1394error_t err;
904     uint32_t value;
905 
906     if ( (mode<DC1394_OPERATION_MODE_MIN) || (mode>DC1394_OPERATION_MODE_MAX) )
907         return DC1394_INVALID_OPERATION_MODE;
908 
909     err=dc1394_get_control_register(camera, REG_CAMERA_ISO_DATA, &value);
910     DC1394_ERR_RTN(err, "Could not get ISO data");
911 
912     if (mode==DC1394_OPERATION_MODE_LEGACY) {
913         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA, (uint32_t) (value & 0xFFFF7FFF));
914         DC1394_ERR_RTN(err, "Could not set ISO data");
915     }
916     else { // 1394b
917         if (camera->bmode_capable) { // if 1394b available
918             err=dc1394_set_control_register(camera, REG_CAMERA_ISO_DATA, (uint32_t) (value | 0x00008000));
919             DC1394_ERR_RTN(err, "Could not set ISO data");
920         }
921         else { // 1394b asked, but it is not available
922             return DC1394_FUNCTION_NOT_SUPPORTED;
923         }
924     }
925 
926     return DC1394_SUCCESS;
927 
928 }
929 
930 dc1394error_t
dc1394_camera_set_power(dc1394camera_t * camera,dc1394switch_t pwr)931 dc1394_camera_set_power(dc1394camera_t *camera, dc1394switch_t pwr)
932 {
933     dc1394error_t err;
934     switch (pwr) {
935     case DC1394_ON:
936         err=dc1394_set_control_register(camera, REG_CAMERA_POWER, DC1394_FEATURE_ON);
937         DC1394_ERR_RTN(err, "Could not switch camera ON");
938         break;
939     case DC1394_OFF:
940         err=dc1394_set_control_register(camera, REG_CAMERA_POWER, DC1394_FEATURE_OFF);
941         DC1394_ERR_RTN(err, "Could not switch camera OFF");
942         break;
943     default:
944         err=DC1394_INVALID_ARGUMENT_VALUE;
945         DC1394_ERR_RTN(err, "Invalid switch value");
946     }
947     return err;
948 }
949 
950 dc1394error_t
dc1394_video_set_transmission(dc1394camera_t * camera,dc1394switch_t pwr)951 dc1394_video_set_transmission(dc1394camera_t *camera, dc1394switch_t pwr)
952 {
953     dc1394error_t err;
954 
955     if (pwr==DC1394_ON) {
956         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_EN, DC1394_FEATURE_ON);
957         DC1394_ERR_RTN(err, "Could not start ISO transmission");
958     }
959     else {
960         // first we stop ISO
961         err=dc1394_set_control_register(camera, REG_CAMERA_ISO_EN, DC1394_FEATURE_OFF);
962         DC1394_ERR_RTN(err, "Could not stop ISO transmission");
963     }
964 
965     return err;
966 }
967 
968 dc1394error_t
dc1394_video_get_transmission(dc1394camera_t * camera,dc1394switch_t * is_on)969 dc1394_video_get_transmission(dc1394camera_t *camera, dc1394switch_t *is_on)
970 {
971     dc1394error_t err;
972     uint32_t value;
973     err= dc1394_get_control_register(camera, REG_CAMERA_ISO_EN, &value);
974     DC1394_ERR_RTN(err, "Could not get ISO status");
975 
976     *is_on= (value & DC1394_FEATURE_ON)>>31;
977     return err;
978 }
979 
980 dc1394error_t
dc1394_video_set_one_shot(dc1394camera_t * camera,dc1394switch_t pwr)981 dc1394_video_set_one_shot(dc1394camera_t *camera, dc1394switch_t pwr)
982 {
983     dc1394error_t err;
984     switch (pwr) {
985     case DC1394_ON:
986         err=dc1394_set_control_register(camera, REG_CAMERA_ONE_SHOT, DC1394_FEATURE_ON);
987         DC1394_ERR_RTN(err, "Could not set one-shot");
988         break;
989     case DC1394_OFF:
990         err=dc1394_set_control_register(camera, REG_CAMERA_ONE_SHOT, DC1394_FEATURE_OFF);
991         DC1394_ERR_RTN(err, "Could not unset one-shot");
992         break;
993     default:
994         err=DC1394_INVALID_ARGUMENT_VALUE;
995         DC1394_ERR_RTN(err, "Invalid switch value");
996     }
997     return err;
998 }
999 
1000 dc1394error_t
dc1394_video_get_one_shot(dc1394camera_t * camera,dc1394bool_t * is_on)1001 dc1394_video_get_one_shot(dc1394camera_t *camera, dc1394bool_t *is_on)
1002 {
1003     uint32_t value;
1004     dc1394error_t err = dc1394_get_control_register(camera, REG_CAMERA_ONE_SHOT, &value);
1005     DC1394_ERR_RTN(err, "Could not get one-shot status");
1006     *is_on = ( value & DC1394_FEATURE_ON) ? DC1394_TRUE : DC1394_FALSE;
1007     return err;
1008 }
1009 
1010 dc1394error_t
dc1394_video_get_multi_shot(dc1394camera_t * camera,dc1394bool_t * is_on,uint32_t * numFrames)1011 dc1394_video_get_multi_shot(dc1394camera_t *camera, dc1394bool_t *is_on, uint32_t *numFrames)
1012 {
1013     uint32_t value;
1014     dc1394error_t err = dc1394_get_control_register(camera, REG_CAMERA_ONE_SHOT, &value);
1015     DC1394_ERR_RTN(err, "Could not get multishot status");
1016     *is_on = (value & (DC1394_FEATURE_ON>>1)) >> 30;
1017     *numFrames= value & 0xFFFFUL;
1018 
1019     return err;
1020 }
1021 
1022 dc1394error_t
dc1394_video_set_multi_shot(dc1394camera_t * camera,uint32_t numFrames,dc1394switch_t pwr)1023 dc1394_video_set_multi_shot(dc1394camera_t *camera, uint32_t numFrames, dc1394switch_t pwr)
1024 {
1025     dc1394error_t err;
1026     switch (pwr) {
1027     case DC1394_ON:
1028         err=dc1394_set_control_register(camera, REG_CAMERA_ONE_SHOT, (0x40000000UL | (numFrames & 0xFFFFUL)));
1029         DC1394_ERR_RTN(err, "Could not set multishot");
1030         break;
1031     case DC1394_OFF:
1032         err=dc1394_video_set_one_shot(camera,pwr);
1033         DC1394_ERR_RTN(err, "Could not unset multishot");
1034         break;
1035     default:
1036         err=DC1394_INVALID_ARGUMENT_VALUE;
1037         DC1394_ERR_RTN(err, "Invalid switch value");
1038     }
1039     return err;
1040 }
1041 
1042 dc1394error_t
dc1394_feature_whitebalance_get_value(dc1394camera_t * camera,uint32_t * u_b_value,uint32_t * v_r_value)1043 dc1394_feature_whitebalance_get_value(dc1394camera_t *camera, uint32_t *u_b_value, uint32_t *v_r_value)
1044 {
1045     uint32_t value;
1046     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_WHITE_BALANCE, &value);
1047     DC1394_ERR_RTN(err, "Could not get white balance");
1048 
1049     *u_b_value= (uint32_t)((value & 0xFFF000UL) >> 12);
1050     *v_r_value= (uint32_t)(value & 0xFFFUL);
1051     return err;
1052 }
1053 
1054 dc1394error_t
dc1394_feature_whitebalance_set_value(dc1394camera_t * camera,uint32_t u_b_value,uint32_t v_r_value)1055 dc1394_feature_whitebalance_set_value(dc1394camera_t *camera, uint32_t u_b_value, uint32_t v_r_value)
1056 {
1057     uint32_t curval;
1058     dc1394error_t err;
1059     err=dc1394_get_control_register(camera, REG_CAMERA_WHITE_BALANCE, &curval);
1060     DC1394_ERR_RTN(err, "Could not get white balance");
1061 
1062     curval= (curval & 0xFF000000UL) | ( ((u_b_value & 0xFFFUL) << 12) | (v_r_value & 0xFFFUL) );
1063     err=dc1394_set_control_register(camera, REG_CAMERA_WHITE_BALANCE, curval);
1064     DC1394_ERR_RTN(err, "Could not set white balance");
1065     return err;
1066 }
1067 
1068 dc1394error_t
dc1394_feature_temperature_get_value(dc1394camera_t * camera,uint32_t * target_temperature,uint32_t * temperature)1069 dc1394_feature_temperature_get_value(dc1394camera_t *camera, uint32_t *target_temperature, uint32_t *temperature)
1070 {
1071     uint32_t value;
1072     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_TEMPERATURE, &value);
1073     DC1394_ERR_RTN(err, "Could not get temperature");
1074     *target_temperature= (uint32_t)((value >> 12) & 0xFFF);
1075     *temperature= (uint32_t)(value & 0xFFFUL);
1076     return err;
1077 }
1078 
1079 dc1394error_t
dc1394_feature_temperature_set_value(dc1394camera_t * camera,uint32_t target_temperature)1080 dc1394_feature_temperature_set_value(dc1394camera_t *camera, uint32_t target_temperature)
1081 {
1082     dc1394error_t err;
1083     uint32_t curval;
1084 
1085     err=dc1394_get_control_register(camera, REG_CAMERA_TEMPERATURE, &curval);
1086     DC1394_ERR_RTN(err, "Could not get temperature");
1087 
1088     curval= (curval & 0xFF000FFFUL) | ((target_temperature & 0xFFFUL) << 12);
1089     err= dc1394_set_control_register(camera, REG_CAMERA_TEMPERATURE, curval);
1090     DC1394_ERR_RTN(err, "Could not set temperature");
1091 
1092     return err;
1093 }
1094 
1095 dc1394error_t
dc1394_feature_whiteshading_get_value(dc1394camera_t * camera,uint32_t * r_value,uint32_t * g_value,uint32_t * b_value)1096 dc1394_feature_whiteshading_get_value(dc1394camera_t *camera, uint32_t *r_value, uint32_t *g_value, uint32_t *b_value)
1097 {
1098     uint32_t value;
1099     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_WHITE_SHADING, &value);
1100     DC1394_ERR_RTN(err, "Could not get white shading");
1101 
1102     *r_value= (uint32_t)((value & 0xFF0000UL) >> 16);
1103     *g_value= (uint32_t)((value & 0xFF00UL) >> 8);
1104     *b_value= (uint32_t)(value & 0xFFUL);
1105 
1106     return err;
1107 }
1108 
1109 dc1394error_t
dc1394_feature_whiteshading_set_value(dc1394camera_t * camera,uint32_t r_value,uint32_t g_value,uint32_t b_value)1110 dc1394_feature_whiteshading_set_value(dc1394camera_t *camera, uint32_t r_value, uint32_t g_value, uint32_t b_value)
1111 {
1112     uint32_t curval;
1113 
1114     dc1394error_t err=dc1394_get_control_register(camera, REG_CAMERA_WHITE_SHADING, &curval);
1115     DC1394_ERR_RTN(err, "Could not get white shading");
1116 
1117     curval= (curval & 0xFF000000UL) | ( ((r_value & 0xFFUL) << 16) |
1118                                         ((g_value & 0xFFUL) << 8) |
1119                                          (b_value & 0xFFUL) );
1120     err=dc1394_set_control_register(camera, REG_CAMERA_WHITE_SHADING, curval);
1121     DC1394_ERR_RTN(err, "Could not set white shading");
1122 
1123     return err;
1124 }
1125 
1126 dc1394error_t
dc1394_external_trigger_get_mode(dc1394camera_t * camera,dc1394trigger_mode_t * mode)1127 dc1394_external_trigger_get_mode(dc1394camera_t *camera, dc1394trigger_mode_t *mode)
1128 {
1129     uint32_t value;
1130     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &value);
1131     DC1394_ERR_RTN(err, "Could not get trigger mode");
1132 
1133     *mode= (uint32_t)( ((value >> 16) & 0xFUL) );
1134     if ((*mode)>5)
1135         (*mode)-=8;
1136     (*mode)+= DC1394_TRIGGER_MODE_MIN;
1137 
1138     return err;
1139 }
1140 
1141 dc1394error_t
dc1394_external_trigger_set_mode(dc1394camera_t * camera,dc1394trigger_mode_t mode)1142 dc1394_external_trigger_set_mode(dc1394camera_t *camera, dc1394trigger_mode_t mode)
1143 {
1144     dc1394error_t err;
1145     uint32_t curval;
1146 
1147     if ( (mode < DC1394_TRIGGER_MODE_MIN) || (mode > DC1394_TRIGGER_MODE_MAX) ) {
1148         return DC1394_INVALID_TRIGGER_MODE;
1149     }
1150 
1151     err=dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &curval);
1152     DC1394_ERR_RTN(err, "Could not get trigger mode");
1153 
1154     mode-= DC1394_TRIGGER_MODE_MIN;
1155     if (mode>5)
1156         mode+=8;
1157     curval= (curval & 0xFFF0FFFFUL) | ((mode & 0xFUL) << 16);
1158     err=dc1394_set_control_register(camera, REG_CAMERA_TRIGGER_MODE, curval);
1159     DC1394_ERR_RTN(err, "Could not set trigger mode");
1160     return err;
1161 }
1162 
1163 
1164 dc1394error_t
dc1394_external_trigger_get_supported_sources(dc1394camera_t * camera,dc1394trigger_sources_t * sources)1165 dc1394_external_trigger_get_supported_sources(dc1394camera_t *camera, dc1394trigger_sources_t *sources)
1166 {
1167     uint32_t value;
1168     dc1394error_t err;
1169     uint64_t offset;
1170     int i;
1171 
1172     FEATURE_TO_INQUIRY_OFFSET(DC1394_FEATURE_TRIGGER, offset);
1173     err=dc1394_get_control_register(camera, offset, &value);
1174     DC1394_ERR_RTN(err,"Could not query supported trigger sources");
1175 
1176     sources->num=0;
1177     for (i = 0; i < DC1394_TRIGGER_SOURCE_NUM; i++) {
1178         if (value & (0x1 << (23-i-(i>3)*3))){
1179             sources->sources[sources->num]=i+DC1394_TRIGGER_SOURCE_MIN;
1180             sources->num++;
1181         }
1182     }
1183 
1184     return err;
1185 }
1186 
1187 
1188 dc1394error_t
dc1394_external_trigger_get_source(dc1394camera_t * camera,dc1394trigger_source_t * source)1189 dc1394_external_trigger_get_source(dc1394camera_t *camera, dc1394trigger_source_t *source)
1190 {
1191     uint32_t value;
1192     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &value);
1193     DC1394_ERR_RTN(err, "Could not get trigger source");
1194 
1195     *source= (uint32_t)( ((value >> 21) & 0x7UL) );
1196     if (*source > 3)
1197         *source -= 3;
1198     (*source)+= DC1394_TRIGGER_SOURCE_MIN;
1199 
1200     return err;
1201 }
1202 
1203 dc1394error_t
dc1394_external_trigger_set_source(dc1394camera_t * camera,dc1394trigger_source_t source)1204 dc1394_external_trigger_set_source(dc1394camera_t *camera, dc1394trigger_source_t source)
1205 {
1206     dc1394error_t err;
1207     uint32_t curval;
1208 
1209     if ( (source < DC1394_TRIGGER_SOURCE_MIN) || (source > DC1394_TRIGGER_SOURCE_MAX) ) {
1210         return DC1394_INVALID_TRIGGER_SOURCE;
1211     }
1212 
1213     err=dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &curval);
1214     DC1394_ERR_RTN(err, "Could not get trigger source");
1215 
1216     source-= DC1394_TRIGGER_SOURCE_MIN;
1217     if (source > 3)
1218         source += 3;
1219     curval= (curval & 0xFF1FFFFFUL) | ((source & 0x7UL) << 21);
1220     err=dc1394_set_control_register(camera, REG_CAMERA_TRIGGER_MODE, curval);
1221     DC1394_ERR_RTN(err, "Could not set trigger source");
1222     return err;
1223 }
1224 
1225 dc1394error_t
dc1394_feature_get_value(dc1394camera_t * camera,dc1394feature_t feature,uint32_t * value)1226 dc1394_feature_get_value(dc1394camera_t *camera, dc1394feature_t feature, uint32_t *value)
1227 {
1228     uint32_t quadval;
1229     uint64_t offset;
1230     dc1394error_t err;
1231 
1232     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1233         return DC1394_INVALID_FEATURE;
1234 
1235     if ((feature==DC1394_FEATURE_WHITE_BALANCE)||
1236         (feature==DC1394_FEATURE_WHITE_SHADING)||
1237         (feature==DC1394_FEATURE_TEMPERATURE)) {
1238         err=DC1394_INVALID_FEATURE;
1239         DC1394_ERR_RTN(err, "You should use the specific functions to read from multiple-value features");
1240     }
1241 
1242     FEATURE_TO_VALUE_OFFSET(feature, offset);
1243 
1244     err=dc1394_get_control_register(camera, offset, &quadval);
1245     DC1394_ERR_RTN(err, "Could not get feature value");
1246     *value= (uint32_t)(quadval & 0xFFFUL);
1247 
1248     return err;
1249 }
1250 
1251 dc1394error_t
dc1394_feature_set_value(dc1394camera_t * camera,dc1394feature_t feature,uint32_t value)1252 dc1394_feature_set_value(dc1394camera_t *camera, dc1394feature_t feature, uint32_t value)
1253 {
1254     uint32_t quadval;
1255     uint64_t offset;
1256     dc1394error_t err;
1257 
1258     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1259         return DC1394_INVALID_FEATURE;
1260 
1261     if ((feature==DC1394_FEATURE_WHITE_BALANCE)||
1262         (feature==DC1394_FEATURE_WHITE_SHADING)||
1263         (feature==DC1394_FEATURE_TEMPERATURE)) {
1264         err=DC1394_INVALID_FEATURE;
1265         DC1394_ERR_RTN(err, "You should use the specific functions to write from multiple-value features");
1266     }
1267 
1268     FEATURE_TO_VALUE_OFFSET(feature, offset);
1269 
1270     err=dc1394_get_control_register(camera, offset, &quadval);
1271     DC1394_ERR_RTN(err, "Could not get feature value");
1272 
1273     err=dc1394_set_control_register(camera, offset, (quadval & 0xFFFFF000UL) | (value & 0xFFFUL));
1274     DC1394_ERR_RTN(err, "Could not set feature value");
1275     return err;
1276 }
1277 
1278 dc1394error_t
dc1394_feature_is_present(dc1394camera_t * camera,dc1394feature_t feature,dc1394bool_t * value)1279 dc1394_feature_is_present(dc1394camera_t *camera, dc1394feature_t feature, dc1394bool_t *value)
1280 {
1281 /*
1282 
1283   NOTE ON FEATURE PRESENCE DETECTION:
1284 
1285   The IIDC specs have 3 locations where the feature presence is notified, at offsets 0x40X,
1286   0x5XX and 0x8XX. The specs do not give any difference between the different locations,
1287   leading to different interpretations by different manufacturers, or even from model to
1288   model. Firmware revisions may also reflect a change in interpretation by a company. This
1289   problem is acknowledged by the IIDC working group and will be resolved in IIDC 1.32.
1290 
1291   In the meantime, the policy of libdc1394 is to make an AND of the three locations to
1292   determine if a feature is available or not. No other verifications is performed.
1293 
1294   Some manufacturer may choose to indicate feature presence by other means, such as setting
1295   a feature OFF and simultaneously disabling the capability to turn the feature ON. Another
1296   technique is to disable all control means (on/off, manual, auto, absolute, etc.),
1297   effectively resulting in a feature that can't be used.
1298 
1299   This kind of interpretation could be implemented in libdc1394. However, the feature may
1300   still be writable even if it is not possible to use it. To allow this off-state writing,
1301   the decision on whether a feature is available or not is not taking this into account.
1302 
1303   Damien
1304 
1305   */
1306 
1307     dc1394error_t err;
1308     uint64_t offset;
1309     uint32_t quadval;
1310 
1311     *value=DC1394_FALSE;
1312 
1313     if ( (feature > DC1394_FEATURE_MAX) || (feature < DC1394_FEATURE_MIN) ) {
1314         return DC1394_INVALID_FEATURE;
1315     }
1316 
1317     if (feature < DC1394_FEATURE_ZOOM) {
1318         offset= REG_CAMERA_FEATURE_HI_INQ;
1319     }
1320     else {
1321         offset= REG_CAMERA_FEATURE_LO_INQ;
1322     }
1323 
1324     // check feature presence in 0x40x
1325     err=dc1394_get_control_register(camera, offset, &quadval);
1326     DC1394_ERR_RTN(err, "Could not get register for feature");
1327 
1328     if (is_feature_bit_set(quadval, feature)!=DC1394_TRUE) {
1329         *value=DC1394_FALSE;
1330         return DC1394_SUCCESS;
1331     }
1332 
1333     // if feature is present in 0x40x, check for availability in 0x5xx
1334     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1335 
1336     err=dc1394_get_control_register(camera, offset, &quadval);
1337     DC1394_ERR_RTN(err, "Could not get register for feature");
1338 
1339     if (quadval & 0x80000000UL) {
1340         *value= DC1394_TRUE;
1341     }
1342     else {
1343         *value= DC1394_FALSE;
1344         return DC1394_SUCCESS;
1345     }
1346 
1347     // if feature is present in 0x5xx, check for availability in 0x8xx
1348     FEATURE_TO_VALUE_OFFSET(feature, offset);
1349 
1350     err=dc1394_get_control_register(camera, offset, &quadval);
1351     DC1394_ERR_RTN(err, "Could not get register for feature");
1352 
1353     if (quadval & 0x80000000UL) {
1354         *value= DC1394_TRUE;
1355     }
1356     else {
1357         *value= DC1394_FALSE;
1358         return DC1394_SUCCESS;
1359     }
1360 
1361     return err;
1362 }
1363 
1364 dc1394error_t
dc1394_feature_is_readable(dc1394camera_t * camera,dc1394feature_t feature,dc1394bool_t * value)1365 dc1394_feature_is_readable(dc1394camera_t *camera, dc1394feature_t feature, dc1394bool_t *value)
1366 {
1367     dc1394error_t err;
1368     uint64_t offset;
1369     uint32_t quadval;
1370 
1371     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1372         return DC1394_INVALID_FEATURE;
1373 
1374     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1375 
1376     err=dc1394_get_control_register(camera, offset, &quadval);
1377     DC1394_ERR_RTN(err, "Could not get read-out capability for feature");
1378 
1379     *value = (quadval & 0x08000000UL) ? DC1394_TRUE: DC1394_FALSE;
1380 
1381     return err;
1382 }
1383 
1384 dc1394error_t
dc1394_feature_is_switchable(dc1394camera_t * camera,dc1394feature_t feature,dc1394bool_t * value)1385 dc1394_feature_is_switchable(dc1394camera_t *camera, dc1394feature_t feature, dc1394bool_t *value)
1386 {
1387     dc1394error_t err;
1388     uint64_t offset;
1389     uint32_t quadval;
1390 
1391     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1392         return DC1394_INVALID_FEATURE;
1393 
1394     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1395 
1396     err=dc1394_get_control_register(camera, offset, &quadval);
1397     DC1394_ERR_RTN(err, "Could not get power capability for feature");
1398 
1399     *value = (quadval & 0x04000000UL) ? DC1394_TRUE: DC1394_FALSE;
1400 
1401     return err;
1402 }
1403 
1404 dc1394error_t
dc1394_feature_get_power(dc1394camera_t * camera,dc1394feature_t feature,dc1394switch_t * value)1405 dc1394_feature_get_power(dc1394camera_t *camera, dc1394feature_t feature, dc1394switch_t *value)
1406 {
1407     dc1394error_t err;
1408     uint64_t offset;
1409     uint32_t quadval;
1410 
1411     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1412         return DC1394_INVALID_FEATURE;
1413 
1414     FEATURE_TO_VALUE_OFFSET(feature, offset);
1415 
1416     err=dc1394_get_control_register(camera, offset, &quadval);
1417     DC1394_ERR_RTN(err, "Could not get feature status");
1418 
1419     *value = (quadval & 0x02000000UL) ? DC1394_TRUE: DC1394_FALSE;
1420 
1421     return err;
1422 }
1423 
1424 dc1394error_t
dc1394_feature_set_power(dc1394camera_t * camera,dc1394feature_t feature,dc1394switch_t value)1425 dc1394_feature_set_power(dc1394camera_t *camera, dc1394feature_t feature, dc1394switch_t value)
1426 {
1427     dc1394error_t err;
1428     uint64_t offset;
1429     uint32_t curval;
1430 
1431     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1432         return DC1394_INVALID_FEATURE;
1433 
1434     FEATURE_TO_VALUE_OFFSET(feature, offset);
1435 
1436     err=dc1394_get_control_register(camera, offset, &curval);
1437     DC1394_ERR_RTN(err, "Could not get feature register");
1438 
1439     if (value && !(curval & 0x02000000UL)) {
1440         curval|= 0x02000000UL;
1441         err=dc1394_set_control_register(camera, offset, curval);
1442         DC1394_ERR_RTN(err, "Could not set feature power");
1443     }
1444     else if (!value && (curval & 0x02000000UL)) {
1445         curval&= 0xFDFFFFFFUL;
1446         err=dc1394_set_control_register(camera, offset, curval);
1447         DC1394_ERR_RTN(err, "Could not set feature power");
1448     }
1449 
1450     return err;
1451 }
1452 
1453 
1454 dc1394error_t
dc1394_feature_get_modes(dc1394camera_t * camera,dc1394feature_t feature,dc1394feature_modes_t * modes)1455 dc1394_feature_get_modes(dc1394camera_t *camera, dc1394feature_t feature, dc1394feature_modes_t *modes)
1456 {
1457     dc1394error_t err;
1458     uint64_t offset;
1459     uint32_t quadval;
1460 
1461     modes->num=0;
1462 
1463     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1464         return DC1394_INVALID_FEATURE;
1465 
1466     if (feature == DC1394_FEATURE_TRIGGER) {
1467         return DC1394_SUCCESS; // success, but no mode is available.
1468     }
1469 
1470     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1471 
1472     err=dc1394_get_control_register(camera, offset, &quadval);
1473     DC1394_ERR_RTN(err, "Could not get mode availability for feature");
1474 
1475     if (quadval & 0x01000000UL) {
1476         modes->modes[modes->num]=DC1394_FEATURE_MODE_MANUAL;
1477         modes->num++;
1478     }
1479     if (quadval & 0x02000000UL) {
1480         modes->modes[modes->num]=DC1394_FEATURE_MODE_AUTO;
1481         modes->num++;
1482     }
1483     if (quadval & 0x10000000UL) {
1484         modes->modes[modes->num]=DC1394_FEATURE_MODE_ONE_PUSH_AUTO;
1485         modes->num++;
1486     }
1487 
1488     return err;
1489 }
1490 
1491 
1492 dc1394error_t
dc1394_feature_get_mode(dc1394camera_t * camera,dc1394feature_t feature,dc1394feature_mode_t * mode)1493 dc1394_feature_get_mode(dc1394camera_t *camera, dc1394feature_t feature, dc1394feature_mode_t *mode)
1494 {
1495     dc1394error_t err;
1496     uint64_t offset;
1497     uint32_t quadval;
1498 
1499     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1500         return DC1394_INVALID_FEATURE;
1501 
1502     if ((feature == DC1394_FEATURE_TRIGGER)||
1503         (feature == DC1394_FEATURE_TRIGGER_DELAY)) {
1504         *mode=DC1394_FEATURE_MODE_MANUAL;
1505     }
1506 
1507     FEATURE_TO_VALUE_OFFSET(feature, offset);
1508 
1509     err=dc1394_get_control_register(camera, offset, &quadval);
1510     DC1394_ERR_RTN(err, "Could not get feature auto status");
1511 
1512     if (quadval & 0x04000000UL) {
1513         *mode= DC1394_FEATURE_MODE_ONE_PUSH_AUTO;
1514     }
1515     else if (quadval & 0x01000000UL) {
1516         *mode= DC1394_FEATURE_MODE_AUTO;
1517     }
1518     else {
1519         *mode= DC1394_FEATURE_MODE_MANUAL;
1520     }
1521 
1522     return err;
1523 }
1524 
1525 dc1394error_t
dc1394_feature_set_mode(dc1394camera_t * camera,dc1394feature_t feature,dc1394feature_mode_t mode)1526 dc1394_feature_set_mode(dc1394camera_t *camera, dc1394feature_t feature, dc1394feature_mode_t mode)
1527 {
1528     dc1394error_t err;
1529     uint64_t offset;
1530     uint32_t curval;
1531 
1532     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1533         return DC1394_INVALID_FEATURE;
1534 
1535     if ( (mode<DC1394_FEATURE_MODE_MIN) || (mode>DC1394_FEATURE_MODE_MAX) )
1536         return DC1394_INVALID_FEATURE_MODE;
1537 
1538     if (feature == DC1394_FEATURE_TRIGGER) {
1539         return DC1394_INVALID_FEATURE;
1540     }
1541 
1542     FEATURE_TO_VALUE_OFFSET(feature, offset);
1543 
1544     err=dc1394_get_control_register(camera, offset, &curval);
1545     DC1394_ERR_RTN(err, "Could not get feature register");
1546 
1547     if ((mode==DC1394_FEATURE_MODE_AUTO) && !(curval & 0x01000000UL)) {
1548         curval|= 0x01000000UL;
1549         err=dc1394_set_control_register(camera, offset, curval);
1550         DC1394_ERR_RTN(err, "Could not set auto mode for feature");
1551     }
1552     else if ((mode==DC1394_FEATURE_MODE_MANUAL) && (curval & 0x01000000UL)) {
1553         curval&= 0xFEFFFFFFUL;
1554         err=dc1394_set_control_register(camera, offset, curval);
1555         DC1394_ERR_RTN(err, "Could not set auto mode for feature");
1556     }
1557     else if ((mode==DC1394_FEATURE_MODE_ONE_PUSH_AUTO)&& !(curval & 0x04000000UL)) {
1558         curval|= 0x04000000UL;
1559         err=dc1394_set_control_register(camera, offset, curval);
1560         DC1394_ERR_RTN(err, "Could not sart one-push capability for feature");
1561     }
1562 
1563     return err;
1564 }
1565 
1566 dc1394error_t
dc1394_feature_get_boundaries(dc1394camera_t * camera,dc1394feature_t feature,uint32_t * min,uint32_t * max)1567 dc1394_feature_get_boundaries(dc1394camera_t *camera, dc1394feature_t feature, uint32_t *min, uint32_t *max)
1568 {
1569     dc1394error_t err;
1570     uint64_t offset;
1571     uint32_t quadval;
1572 
1573     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1574         return DC1394_INVALID_FEATURE;
1575 
1576     if (feature == DC1394_FEATURE_TRIGGER) {
1577         return DC1394_INVALID_FEATURE;
1578     }
1579 
1580     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1581 
1582     err=dc1394_get_control_register(camera, offset, &quadval);
1583     DC1394_ERR_RTN(err, "Could not get feature min value");
1584 
1585     *min= (uint32_t)((quadval & 0xFFF000UL) >> 12);
1586     *max= (uint32_t)(quadval & 0xFFFUL);
1587     return err;
1588 }
1589 
1590 /*
1591  * Memory load/save functions
1592  */
1593 
1594 dc1394error_t
dc1394_memory_busy(dc1394camera_t * camera,dc1394bool_t * value)1595 dc1394_memory_busy(dc1394camera_t *camera, dc1394bool_t *value)
1596 {
1597     uint32_t quadlet;
1598     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_MEMORY_SAVE, &quadlet);
1599     DC1394_ERR_RTN(err, "Could not get memory busy status");
1600     *value = (quadlet & DC1394_FEATURE_ON) >> 31;
1601     return err;
1602 }
1603 
1604 dc1394error_t
dc1394_memory_save(dc1394camera_t * camera,uint32_t channel)1605 dc1394_memory_save(dc1394camera_t *camera, uint32_t channel)
1606 {
1607     dc1394error_t err=dc1394_set_control_register(camera, REG_CAMERA_MEM_SAVE_CH, (uint32_t)((channel & 0xFUL) << 28));
1608     DC1394_ERR_RTN(err, "Could not save memory channel");
1609 
1610     err=dc1394_set_control_register(camera, REG_CAMERA_MEMORY_SAVE, DC1394_FEATURE_ON);
1611     DC1394_ERR_RTN(err, "Could not save to memory");
1612     return err;
1613 }
1614 
1615 dc1394error_t
dc1394_memory_load(dc1394camera_t * camera,uint32_t channel)1616 dc1394_memory_load(dc1394camera_t *camera, uint32_t channel)
1617 {
1618     dc1394error_t err=dc1394_set_control_register(camera, REG_CAMERA_CUR_MEM_CH, (uint32_t)((channel & 0xFUL) << 28));
1619     DC1394_ERR_RTN(err, "Could not load from memory");
1620     return err;
1621 }
1622 
1623 /*
1624  * Trigger functions
1625  */
1626 
1627 dc1394error_t
dc1394_external_trigger_set_polarity(dc1394camera_t * camera,dc1394trigger_polarity_t polarity)1628 dc1394_external_trigger_set_polarity(dc1394camera_t *camera, dc1394trigger_polarity_t polarity)
1629 {
1630     dc1394error_t err;
1631     uint32_t curval;
1632 
1633     if ( (polarity<DC1394_TRIGGER_ACTIVE_MIN) || (polarity>DC1394_TRIGGER_ACTIVE_MAX) )
1634         return DC1394_INVALID_TRIGGER_POLARITY;
1635 
1636     err=dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &curval);
1637     DC1394_ERR_RTN(err, "Could not get trigger register");
1638 
1639     curval= (curval & 0xFEFFFFFFUL) | ((polarity & 0x1UL) << 24);
1640     err=dc1394_set_control_register(camera, REG_CAMERA_TRIGGER_MODE, curval);
1641     DC1394_ERR_RTN(err, "Could not set set trigger polarity");
1642     return err;
1643 }
1644 
1645 dc1394error_t
dc1394_external_trigger_get_polarity(dc1394camera_t * camera,dc1394trigger_polarity_t * polarity)1646 dc1394_external_trigger_get_polarity(dc1394camera_t *camera, dc1394trigger_polarity_t *polarity)
1647 {
1648     uint32_t value;
1649     dc1394error_t err= dc1394_get_control_register(camera, REG_CAMERA_TRIGGER_MODE, &value);
1650     DC1394_ERR_RTN(err, "Could not get trigger polarity");
1651 
1652     *polarity= (uint32_t)( ((value >> 24) & 0x1UL) )+DC1394_TRIGGER_ACTIVE_MIN;
1653     return err;
1654 }
1655 
1656 dc1394error_t
dc1394_external_trigger_has_polarity(dc1394camera_t * camera,dc1394bool_t * polarity)1657 dc1394_external_trigger_has_polarity(dc1394camera_t *camera, dc1394bool_t *polarity)
1658 {
1659     dc1394error_t err;
1660     uint64_t offset;
1661     uint32_t quadval;
1662 
1663     offset= REG_CAMERA_FEATURE_HI_BASE_INQ;
1664 
1665     err=dc1394_get_control_register(camera, offset + ((DC1394_FEATURE_TRIGGER - DC1394_FEATURE_MIN) * 0x04U), &quadval);
1666     DC1394_ERR_RTN(err, "Could not get trigger polarity capability");
1667 
1668     *polarity = (quadval & 0x02000000UL) ? DC1394_TRUE: DC1394_FALSE;
1669 
1670     return err;
1671 }
1672 
1673 dc1394error_t
dc1394_external_trigger_set_power(dc1394camera_t * camera,dc1394switch_t pwr)1674 dc1394_external_trigger_set_power(dc1394camera_t *camera, dc1394switch_t pwr)
1675 {
1676     dc1394error_t err=dc1394_feature_set_power(camera, DC1394_FEATURE_TRIGGER, pwr);
1677     DC1394_ERR_RTN(err, "Could not set external trigger");
1678     return err;
1679 }
1680 
1681 dc1394error_t
dc1394_external_trigger_get_power(dc1394camera_t * camera,dc1394switch_t * pwr)1682 dc1394_external_trigger_get_power(dc1394camera_t *camera, dc1394switch_t *pwr)
1683 {
1684     dc1394error_t err=dc1394_feature_get_power(camera, DC1394_FEATURE_TRIGGER, pwr);
1685     DC1394_ERR_RTN(err, "Could not set external trigger");
1686     return err;
1687 }
1688 
1689 dc1394error_t
dc1394_software_trigger_set_power(dc1394camera_t * camera,dc1394switch_t pwr)1690 dc1394_software_trigger_set_power(dc1394camera_t *camera, dc1394switch_t pwr)
1691 {
1692     dc1394error_t err;
1693 
1694     if (pwr==DC1394_ON) {
1695         err=dc1394_set_control_register(camera, REG_CAMERA_SOFT_TRIGGER, DC1394_FEATURE_ON);
1696     }
1697     else {
1698         err=dc1394_set_control_register(camera, REG_CAMERA_SOFT_TRIGGER, DC1394_FEATURE_OFF);
1699     }
1700     DC1394_ERR_RTN(err, "Could not set software trigger");
1701     return err;
1702 }
1703 
1704 dc1394error_t
dc1394_software_trigger_get_power(dc1394camera_t * camera,dc1394switch_t * pwr)1705 dc1394_software_trigger_get_power(dc1394camera_t *camera, dc1394switch_t *pwr)
1706 {
1707     uint32_t value;
1708     dc1394error_t err = dc1394_get_control_register(camera, REG_CAMERA_SOFT_TRIGGER, &value);
1709     DC1394_ERR_RTN(err, "Could not get software trigger status");
1710 
1711     *pwr = (value & DC1394_FEATURE_ON)? DC1394_ON : DC1394_OFF;
1712 
1713     return err;
1714 }
1715 
1716 dc1394error_t
dc1394_video_get_data_depth(dc1394camera_t * camera,uint32_t * depth)1717 dc1394_video_get_data_depth(dc1394camera_t *camera, uint32_t *depth)
1718 {
1719     dc1394error_t err;
1720     uint32_t value;
1721     dc1394video_mode_t mode;
1722     dc1394color_coding_t coding;
1723 
1724     *depth = 0;
1725     if (camera->iidc_version >= DC1394_IIDC_VERSION_1_31) {
1726         err= dc1394_get_control_register(camera, REG_CAMERA_DATA_DEPTH, &value);
1727         if (err==DC1394_SUCCESS)
1728             *depth = value >> 24;
1729     }
1730 
1731     /* For cameras that do not have the DATA_DEPTH register, perform a
1732        sane default. */
1733     if (*depth == 0) {
1734         err = dc1394_video_get_mode(camera, &mode);
1735         DC1394_ERR_RTN(err, "Could not get video mode");
1736 
1737         if (dc1394_is_video_mode_scalable (mode))
1738             return dc1394_format7_get_data_depth (camera, mode, depth);
1739 
1740         err = dc1394_get_color_coding_from_video_mode (camera, mode, &coding);
1741         DC1394_ERR_RTN(err, "Could not get color coding");
1742 
1743         err = dc1394_get_color_coding_data_depth (coding, depth);
1744         DC1394_ERR_RTN(err, "Could not get data depth from color coding");
1745 
1746         return err;
1747     }
1748 
1749     return DC1394_SUCCESS;
1750 }
1751 
1752 dc1394error_t
dc1394_feature_get_absolute_control(dc1394camera_t * camera,dc1394feature_t feature,dc1394switch_t * pwr)1753 dc1394_feature_get_absolute_control(dc1394camera_t *camera, dc1394feature_t feature, dc1394switch_t *pwr)
1754 {
1755     dc1394error_t err;
1756     uint64_t offset;
1757     uint32_t quadval;
1758 
1759     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1760         return DC1394_INVALID_FEATURE;
1761 
1762     FEATURE_TO_VALUE_OFFSET(feature, offset);
1763 
1764     err=dc1394_get_control_register(camera, offset, &quadval);
1765     DC1394_ERR_RTN(err, "Could not get get abs control for feature");
1766 
1767     *pwr = (quadval & 0x40000000UL) ? DC1394_TRUE: DC1394_FALSE;
1768 
1769     return err;
1770 }
1771 
1772 dc1394error_t
dc1394_feature_set_absolute_control(dc1394camera_t * camera,dc1394feature_t feature,dc1394switch_t pwr)1773 dc1394_feature_set_absolute_control(dc1394camera_t *camera, dc1394feature_t feature, dc1394switch_t pwr)
1774 {
1775     dc1394error_t err;
1776     uint64_t offset;
1777     uint32_t curval;
1778 
1779     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1780         return DC1394_INVALID_FEATURE;
1781 
1782     FEATURE_TO_VALUE_OFFSET(feature, offset);
1783 
1784     err=dc1394_get_control_register(camera, offset, &curval);
1785     DC1394_ERR_RTN(err, "Could not get abs setting status for feature");
1786 
1787     if (pwr && !(curval & 0x40000000UL)) {
1788         curval|= 0x40000000UL;
1789         err=dc1394_set_control_register(camera, offset, curval);
1790         DC1394_ERR_RTN(err, "Could not set absolute control for feature");
1791     }
1792     else if (!pwr && (curval & 0x40000000UL)) {
1793         curval&= 0xBFFFFFFFUL;
1794         err=dc1394_set_control_register(camera, offset, curval);
1795         DC1394_ERR_RTN(err, "Could not set absolute control for feature");
1796     }
1797 
1798     return err;
1799 }
1800 
1801 
1802 dc1394error_t
dc1394_feature_has_absolute_control(dc1394camera_t * camera,dc1394feature_t feature,dc1394bool_t * value)1803 dc1394_feature_has_absolute_control(dc1394camera_t *camera, dc1394feature_t feature, dc1394bool_t *value)
1804 {
1805     dc1394error_t err;
1806     uint64_t offset;
1807     uint32_t quadval;
1808 
1809     if ( (feature<DC1394_FEATURE_MIN) || (feature>DC1394_FEATURE_MAX) )
1810         return DC1394_INVALID_FEATURE;
1811 
1812     FEATURE_TO_INQUIRY_OFFSET(feature, offset);
1813 
1814     err=dc1394_get_control_register(camera, offset, &quadval);
1815     DC1394_ERR_RTN(err, "Could not get absolute control register for feature");
1816 
1817     *value = (quadval & 0x40000000UL) ? DC1394_TRUE: DC1394_FALSE;
1818 
1819     return err;
1820 }
1821 
1822 
1823 dc1394error_t
dc1394_video_get_bandwidth_usage(dc1394camera_t * camera,uint32_t * bandwidth)1824 dc1394_video_get_bandwidth_usage(dc1394camera_t *camera, uint32_t *bandwidth)
1825 {
1826     uint32_t format, qpp;
1827     dc1394video_mode_t video_mode;
1828     dc1394speed_t speed;
1829     dc1394framerate_t framerate=0;
1830     dc1394error_t err;
1831 
1832     // get format and mode
1833     err=dc1394_video_get_mode(camera, &video_mode);
1834     DC1394_ERR_RTN(err, "Could not get video mode");
1835 
1836     err=get_format_from_mode(video_mode, &format);
1837     DC1394_ERR_RTN(err, "Invalid mode ID");
1838 
1839     if (format==DC1394_FORMAT7) {
1840         // use the bytes per packet value:
1841         err=dc1394_format7_get_packet_size(camera, video_mode, &qpp);
1842         DC1394_ERR_RTN(err, "Could not get BPP");
1843         qpp=qpp/4;
1844     }
1845     else {
1846         // get the framerate:
1847         err=dc1394_video_get_framerate(camera, &framerate);
1848         DC1394_ERR_RTN(err, "Could not get framerate");
1849         err=get_quadlets_per_packet(video_mode, framerate, &qpp);
1850     }
1851     // add the ISO header and footer:
1852     qpp+=3;
1853 
1854     // get camera ISO speed:
1855     err=dc1394_video_get_iso_speed(camera, &speed);
1856     DC1394_ERR_RTN(err, "Could not get ISO speed");
1857 
1858     // mutiply by 4 anyway because the best speed is SPEED_400 only
1859     if (speed>=DC1394_ISO_SPEED_1600)
1860         *bandwidth = qpp >> (speed-DC1394_ISO_SPEED_1600);
1861     else
1862         *bandwidth = qpp << (DC1394_ISO_SPEED_1600-speed);
1863 
1864     return err;
1865 }
1866 
1867 dc1394error_t
dc1394_feature_get_absolute_boundaries(dc1394camera_t * camera,dc1394feature_t feature,float * min,float * max)1868 dc1394_feature_get_absolute_boundaries(dc1394camera_t *camera, dc1394feature_t feature, float *min, float *max)
1869 {
1870     dc1394error_t err=DC1394_SUCCESS;
1871 
1872     if ( (feature > DC1394_FEATURE_MAX) || (feature < DC1394_FEATURE_MIN) ) {
1873         return DC1394_INVALID_FEATURE;
1874     }
1875 
1876     err=dc1394_get_absolute_register(camera, feature, REG_CAMERA_ABS_MAX, (uint32_t*)max);
1877     DC1394_ERR_RTN(err,"Could not get maximal absolute value");
1878     err=dc1394_get_absolute_register(camera, feature, REG_CAMERA_ABS_MIN, (uint32_t*)min);
1879     DC1394_ERR_RTN(err,"Could not get minimal absolute value");
1880 
1881     return err;
1882 }
1883 
1884 
1885 dc1394error_t
dc1394_feature_get_absolute_value(dc1394camera_t * camera,dc1394feature_t feature,float * value)1886 dc1394_feature_get_absolute_value(dc1394camera_t *camera, dc1394feature_t feature, float *value)
1887 {
1888     dc1394error_t err=DC1394_SUCCESS;
1889 
1890     if ( (feature > DC1394_FEATURE_MAX) || (feature < DC1394_FEATURE_MIN) ) {
1891         return DC1394_INVALID_FEATURE;
1892     }
1893     err=dc1394_get_absolute_register(camera, feature, REG_CAMERA_ABS_VALUE, (uint32_t*)value);
1894     DC1394_ERR_RTN(err,"Could not get current absolute value");
1895 
1896     return err;
1897 }
1898 
1899 
1900 dc1394error_t
dc1394_feature_set_absolute_value(dc1394camera_t * camera,dc1394feature_t feature,float value)1901 dc1394_feature_set_absolute_value(dc1394camera_t *camera, dc1394feature_t feature, float value)
1902 {
1903     dc1394error_t err=DC1394_SUCCESS;
1904 
1905     uint32_t tempq;
1906     memcpy(&tempq,&value,4);
1907 
1908     if ( (feature > DC1394_FEATURE_MAX) || (feature < DC1394_FEATURE_MIN) ) {
1909         return DC1394_INVALID_FEATURE;
1910     }
1911 
1912     dc1394_set_absolute_register(camera, feature, REG_CAMERA_ABS_VALUE, tempq);
1913     DC1394_ERR_RTN(err,"Could not get current absolute value");
1914 
1915     return err;
1916 }
1917 
1918 
1919 dc1394error_t
dc1394_pio_set(dc1394camera_t * camera,uint32_t value)1920 dc1394_pio_set(dc1394camera_t *camera, uint32_t value)
1921 {
1922     dc1394error_t err=DC1394_SUCCESS;
1923 
1924     err=dc1394_set_PIO_register(camera, REG_CAMERA_PIO_OUT, value);
1925     DC1394_ERR_RTN(err,"Could not set PIO value");
1926 
1927     return err;
1928 }
1929 
1930 
1931 dc1394error_t
dc1394_pio_get(dc1394camera_t * camera,uint32_t * value)1932 dc1394_pio_get(dc1394camera_t *camera, uint32_t *value)
1933 {
1934     dc1394error_t err=DC1394_SUCCESS;
1935 
1936     err=dc1394_get_PIO_register(camera, REG_CAMERA_PIO_IN, value);
1937     DC1394_ERR_RTN(err,"Could not get PIO value");
1938 
1939     return err;
1940 }
1941 
1942 /*******************************************************************************
1943    New API for camera detection. For now we only have wrappers around existing
1944    functions, which is of course far from optimal.
1945  *******************************************************************************/
1946 
1947 /*
1948   Create a new dc1394 struct, which also initialises the library
1949 */
1950 dc1394_t *
dc1394_new(void)1951 dc1394_new (void)
1952 {
1953     dc1394_t * d = calloc (1, sizeof (dc1394_t));
1954 #ifdef HAVE_LINUX
1955 #ifdef HAVE_LIBRAW1394
1956     linux_init (d);
1957 #endif
1958     juju_init (d);
1959 #endif
1960 #ifdef HAVE_MACOSX
1961     macosx_init (d);
1962 #endif
1963 #ifdef HAVE_WINDOWS
1964     windows_init (d);
1965 #endif
1966 #ifdef HAVE_LIBUSB
1967     dc1394_usb_init (d);
1968 #endif
1969 
1970     int i;
1971     int initializations = 0;
1972     for (i = 0; i < d->num_platforms; i++) {
1973         dc1394_log_debug ("Initializing platform %d: %s",
1974                 i, d->platforms[i].name);
1975         d->platforms[i].p = d->platforms[i].dispatch->platform_new ();
1976         if (d->platforms[i].p) {
1977             initializations++;
1978             dc1394_log_debug ("Initialized platform %d", i);
1979         }
1980         else {
1981             dc1394_log_debug ("Failed to initialize platform %d", i);
1982         }
1983     }
1984 
1985     if (initializations == 0) {
1986         dc1394_free (d);
1987         dc1394_log_error ("Failed to initialize libdc1394");
1988         return NULL;
1989     }
1990     return d;
1991 }
1992 
1993 /*
1994   Free a dc1394 struct, which also terminates the use of the library
1995 */
1996 void
dc1394_free(dc1394_t * d)1997 dc1394_free (dc1394_t * d)
1998 {
1999     free_enumeration (d);
2000     int i;
2001     for (i = 0; i < d->num_platforms; i++) {
2002         if (d->platforms[i].p)
2003             d->platforms[i].dispatch->platform_free (d->platforms[i].p);
2004         d->platforms[i].p = NULL;
2005     }
2006     free (d->platforms);
2007     d->platforms = NULL;
2008     free (d);
2009 }
2010 
register_platform(dc1394_t * d,const platform_dispatch_t * dispatch,const char * name)2011 void register_platform (dc1394_t * d, const platform_dispatch_t * dispatch,
2012         const char * name)
2013 {
2014     if (!dispatch->platform_new || !dispatch->platform_free ||
2015             !dispatch->get_device_list || !dispatch->free_device_list ||
2016             !dispatch->device_get_config_rom ||
2017             !dispatch->camera_new || !dispatch->camera_free ||
2018             !dispatch->camera_set_parent || !dispatch->camera_read ||
2019             !dispatch->camera_write) {
2020         dc1394_log_error ("Platform %s is missing required functions", name);
2021         return;
2022     }
2023     int n = d->num_platforms;
2024     d->platforms = realloc(d->platforms, (n+1)*sizeof(platform_info_t));
2025     d->platforms[n].dispatch = dispatch;
2026     d->platforms[n].name = name;
2027     d->platforms[n].device_list = NULL;
2028     d->platforms[n].p = NULL;
2029     d->num_platforms++;
2030 }
2031 
2032 char *
get_leaf_string(platform_camera_t * pcam,const platform_dispatch_t * disp,uint32_t offset)2033 get_leaf_string (platform_camera_t * pcam, const platform_dispatch_t * disp,
2034         uint32_t offset)
2035 {
2036     uint32_t quad;
2037     int len, i;
2038     char * str;
2039 
2040     if (disp->camera_read (pcam, offset, &quad, 1) < 0)
2041         return NULL;
2042 
2043     len = quad >> 16;
2044     str = malloc (4 * (len - 2) + 1);
2045     for (i = 0; i < len - 2; i++) {
2046         if (disp->camera_read (pcam, offset + 12 + 4 * i, &quad, 1) < 0) {
2047             free (str);
2048             return NULL;
2049         }
2050         str[4*i+0] = quad >> 24;
2051         str[4*i+1] = (quad >> 16) & 0xff;
2052         str[4*i+2] = (quad >> 8) & 0xff;
2053         str[4*i+3] = quad & 0xff;
2054     }
2055     str[4*i] = '\0';
2056     return str;
2057 }
2058 
2059 dc1394camera_t *
dc1394_camera_new_unit(dc1394_t * d,uint64_t guid,int unit)2060 dc1394_camera_new_unit (dc1394_t * d, uint64_t guid, int unit)
2061 {
2062     int i;
2063     camera_info_t * info = NULL;
2064     platform_camera_t * pcam;
2065     const platform_dispatch_t * disp;
2066     uint32_t command_regs_base = 0;
2067     uint32_t vendor_name_offset = 0;
2068     uint32_t model_name_offset = 0;
2069     uint32_t unit_sub_sw_version = 0;
2070     uint32_t ghigh, glow, quad;
2071     uint32_t offset, num_entries;
2072     dc1394camera_t * camera;
2073     dc1394camera_priv_t * cpriv;
2074 
2075     if (!d->num_cameras)
2076         refresh_enumeration (d);
2077 
2078     for (i = 0; i < d->num_cameras; i++) {
2079         if (d->cameras[i].guid == guid &&
2080             (unit < 0 || d->cameras[i].unit == unit)) {
2081             info = d->cameras + i;
2082             break;
2083         }
2084     }
2085     if (!info)
2086         return NULL;
2087 
2088     disp = info->platform->dispatch;
2089     pcam = disp->camera_new (info->platform->p, info->device,
2090             info->unit_dependent_directory);
2091     if (!pcam)
2092         return NULL;
2093 
2094     /* Check to make sure the GUID still matches. */
2095     if (disp->camera_read (pcam, 0x40C, &ghigh, 1) < 0 ||
2096         disp->camera_read (pcam, 0x410, &glow, 1) < 0)
2097         goto fail;
2098 
2099     if (ghigh != (info->guid >> 32) || glow != (info->guid & 0xffffffff))
2100         goto fail;
2101 
2102     if (disp->camera_read (pcam, info->unit_dependent_directory,
2103                 &quad, 1) < 0)
2104         goto fail;
2105 
2106     num_entries = quad >> 16;
2107     offset = info->unit_dependent_directory + 4;
2108     for (i = 0; i < num_entries; i++) {
2109         if (disp->camera_read (pcam, offset + 4 * i, &quad, 1) < 0)
2110             goto fail;
2111         if ((quad >> 24) == 0x40)
2112             command_regs_base = quad & 0xffffff;
2113         else if ((quad >> 24) == 0x81) {
2114             /*
2115                The iSight version 1.0.3 has two 0x81 (vendor) leaves instead
2116                of a 0x81 and a 0x82 (model leaf). To go around this problem,
2117                we save the second vendor leaf as the model leaf. This is safe
2118                because if there is two 0x81 AND a 0x82, the real model leaf
2119                will overwrite the spurious second vendor string.
2120             */
2121             if (vendor_name_offset==0)
2122                 vendor_name_offset = offset + 4 * ((quad & 0xffffff) + i);
2123             else
2124                 model_name_offset = offset + 4 * ((quad & 0xffffff) + i);
2125         }
2126         else if ((quad >> 24) == 0x82)
2127             model_name_offset = offset + 4 * ((quad & 0xffffff) + i);
2128         else if ((quad >> 24) == 0x38)
2129             unit_sub_sw_version = quad & 0xffffff;
2130     }
2131 
2132     if (!command_regs_base)
2133         goto fail;
2134 
2135     camera = calloc (1, sizeof (dc1394camera_priv_t));
2136     cpriv = DC1394_CAMERA_PRIV (camera);
2137 
2138     cpriv->pcam = pcam;
2139     cpriv->platform = info->platform;
2140     camera->guid = info->guid;
2141     camera->unit = info->unit;
2142     camera->unit_spec_ID = info->unit_spec_ID;
2143     camera->unit_sw_version = info->unit_sw_version;
2144     camera->unit_sub_sw_version = unit_sub_sw_version;
2145     camera->unit_directory = info->unit_directory;
2146     camera->unit_dependent_directory = info->unit_dependent_directory;
2147     camera->command_registers_base = command_regs_base * 4;
2148     camera->vendor_id = info->vendor_id;
2149     camera->model_id = info->model_id;
2150 
2151     camera->vendor = get_leaf_string (pcam, disp, vendor_name_offset);
2152     camera->model = get_leaf_string (pcam, disp, model_name_offset);
2153 
2154     if (camera->unit_spec_ID == 0xA02D) {
2155         if (info->unit_sw_version == 0x100)
2156             camera->iidc_version = DC1394_IIDC_VERSION_1_04;
2157         else if (info->unit_sw_version == 0x101)
2158             camera->iidc_version = DC1394_IIDC_VERSION_1_20;
2159         else if (info->unit_sw_version == 0x102) {
2160             camera->iidc_version = DC1394_IIDC_VERSION_1_30;
2161             // only add sub_sw_version if it is valid. Otherwise
2162             // consider that it's IIDC 1.30 (hence add nothing)
2163             if ((unit_sub_sw_version >> 4)<=9)
2164                 camera->iidc_version += unit_sub_sw_version >> 4;
2165         }
2166     }
2167     else
2168         camera->iidc_version = DC1394_IIDC_VERSION_PTGREY;
2169 
2170     disp->camera_set_parent (cpriv->pcam, camera);
2171     update_camera_info (camera);
2172 
2173     return camera;
2174 
2175  fail:
2176     disp->camera_free (pcam);
2177     return NULL;
2178 }
2179 
2180 dc1394camera_t *
dc1394_camera_new(dc1394_t * d,uint64_t guid)2181 dc1394_camera_new (dc1394_t * d, uint64_t guid)
2182 {
2183     return dc1394_camera_new_unit (d, guid, -1);
2184 }
2185 
2186 /*
2187   Free a camera structure as well as resources used by that camera (bandwidth,
2188   ISO channels, etc...)
2189 */
2190 void
dc1394_camera_free(dc1394camera_t * camera)2191 dc1394_camera_free(dc1394camera_t *camera)
2192 {
2193     dc1394camera_priv_t * cpriv = DC1394_CAMERA_PRIV (camera);
2194 
2195     if (cpriv->iso_persist!=1)
2196         dc1394_iso_release_all(camera);
2197 
2198     cpriv->platform->dispatch->camera_free (cpriv->pcam);
2199     free (camera->vendor);
2200     free (camera->model);
2201     free (camera);
2202 }
2203