-
Notifications
You must be signed in to change notification settings - Fork 494
/
device.c
1870 lines (1530 loc) · 49.6 KB
/
device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (C) 2010-2012 Ken Tossell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the author nor other contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/**
* @defgroup device Device handling and enumeration
* @brief Support for finding, inspecting and opening UVC devices
*/
#include "libuvc/libuvc.h"
#include "libuvc/libuvc_internal.h"
int uvc_already_open(uvc_context_t *ctx, struct libusb_device *usb_dev);
void uvc_free_devh(uvc_device_handle_t *devh);
uvc_error_t uvc_get_device_info(uvc_device_t *dev, uvc_device_info_t **info);
void uvc_free_device_info(uvc_device_info_t *info);
uvc_error_t uvc_scan_control(uvc_device_t *dev, uvc_device_info_t *info);
uvc_error_t uvc_parse_vc(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block, size_t block_size);
uvc_error_t uvc_parse_vc_selector_unit(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block, size_t block_size);
uvc_error_t uvc_parse_vc_extension_unit(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vc_header(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block, size_t block_size);
uvc_error_t uvc_parse_vc_input_terminal(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vc_processing_unit(uvc_device_t *dev,
uvc_device_info_t *info,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_scan_streaming(uvc_device_t *dev,
uvc_device_info_t *info,
int interface_idx);
uvc_error_t uvc_parse_vs(uvc_device_t *dev,
uvc_device_info_t *info,
uvc_streaming_interface_t *stream_if,
const unsigned char *block, size_t block_size);
uvc_error_t uvc_parse_vs_format_uncompressed(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vs_format_mjpeg(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vs_frame_uncompressed(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vs_frame_format(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vs_frame_frame(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
uvc_error_t uvc_parse_vs_input_header(uvc_streaming_interface_t *stream_if,
const unsigned char *block,
size_t block_size);
void LIBUSB_CALL _uvc_status_callback(struct libusb_transfer *transfer);
/** @internal
* @brief Test whether the specified USB device has been opened as a UVC device
* @ingroup device
*
* @param ctx Context in which to search for the UVC device
* @param usb_dev USB device to find
* @return true if the device is open in this context
*/
int uvc_already_open(uvc_context_t *ctx, struct libusb_device *usb_dev) {
uvc_device_handle_t *devh;
DL_FOREACH(ctx->open_devices, devh) {
if (usb_dev == devh->dev->usb_dev)
return 1;
}
return 0;
}
/** @brief Finds a camera identified by vendor, product and/or serial number
* @ingroup device
*
* @param[in] ctx UVC context in which to search for the camera
* @param[out] dev Reference to the camera, or NULL if not found
* @param[in] vid Vendor ID number, optional
* @param[in] pid Product ID number, optional
* @param[in] sn Serial number or NULL
* @return Error finding device or UVC_SUCCESS
*/
uvc_error_t uvc_find_device(
uvc_context_t *ctx, uvc_device_t **dev,
int vid, int pid, const char *sn) {
uvc_error_t ret = UVC_SUCCESS;
uvc_device_t **list;
uvc_device_t *test_dev;
int dev_idx;
int found_dev;
UVC_ENTER();
ret = uvc_get_device_list(ctx, &list);
if (ret != UVC_SUCCESS) {
UVC_EXIT(ret);
return ret;
}
dev_idx = 0;
found_dev = 0;
while (!found_dev && (test_dev = list[dev_idx++]) != NULL) {
uvc_device_descriptor_t *desc;
if (uvc_get_device_descriptor(test_dev, &desc) != UVC_SUCCESS)
continue;
if ((!vid || desc->idVendor == vid)
&& (!pid || desc->idProduct == pid)
&& (!sn || (desc->serialNumber && !strcmp(desc->serialNumber, sn))))
found_dev = 1;
uvc_free_device_descriptor(desc);
}
if (found_dev)
uvc_ref_device(test_dev);
uvc_free_device_list(list, 1);
if (found_dev) {
*dev = test_dev;
UVC_EXIT(UVC_SUCCESS);
return UVC_SUCCESS;
} else {
UVC_EXIT(UVC_ERROR_NO_DEVICE);
return UVC_ERROR_NO_DEVICE;
}
}
/** @brief Finds all cameras identified by vendor, product and/or serial number
* @ingroup device
*
* @param[in] ctx UVC context in which to search for the camera
* @param[out] devs List of matching cameras
* @param[in] vid Vendor ID number, optional
* @param[in] pid Product ID number, optional
* @param[in] sn Serial number or NULL
* @return Error finding device or UVC_SUCCESS
*/
uvc_error_t uvc_find_devices(
uvc_context_t *ctx, uvc_device_t ***devs,
int vid, int pid, const char *sn) {
uvc_error_t ret = UVC_SUCCESS;
uvc_device_t **list;
uvc_device_t *test_dev;
int dev_idx;
int found_dev;
uvc_device_t **list_internal;
int num_uvc_devices;
UVC_ENTER();
ret = uvc_get_device_list(ctx, &list);
if (ret != UVC_SUCCESS) {
UVC_EXIT(ret);
return ret;
}
num_uvc_devices = 0;
dev_idx = 0;
found_dev = 0;
list_internal = malloc(sizeof(*list_internal));
*list_internal = NULL;
while ((test_dev = list[dev_idx++]) != NULL) {
uvc_device_descriptor_t *desc;
if (uvc_get_device_descriptor(test_dev, &desc) != UVC_SUCCESS)
continue;
if ((!vid || desc->idVendor == vid)
&& (!pid || desc->idProduct == pid)
&& (!sn || (desc->serialNumber && !strcmp(desc->serialNumber, sn)))) {
found_dev = 1;
uvc_ref_device(test_dev);
num_uvc_devices++;
list_internal = realloc(list_internal, (num_uvc_devices + 1) * sizeof(*list_internal));
list_internal[num_uvc_devices - 1] = test_dev;
list_internal[num_uvc_devices] = NULL;
}
uvc_free_device_descriptor(desc);
}
uvc_free_device_list(list, 1);
if (found_dev) {
*devs = list_internal;
UVC_EXIT(UVC_SUCCESS);
return UVC_SUCCESS;
} else {
UVC_EXIT(UVC_ERROR_NO_DEVICE);
return UVC_ERROR_NO_DEVICE;
}
}
/** @brief Get the number of the bus to which the device is attached
* @ingroup device
*/
uint8_t uvc_get_bus_number(uvc_device_t *dev) {
return libusb_get_bus_number(dev->usb_dev);
}
/** @brief Get the number assigned to the device within its bus
* @ingroup device
*/
uint8_t uvc_get_device_address(uvc_device_t *dev) {
return libusb_get_device_address(dev->usb_dev);
}
/** @brief Open a UVC device
* @ingroup device
*
* @param dev Device to open
* @param[out] devh Handle on opened device
* @return Error opening device or SUCCESS
*/
uvc_error_t uvc_open(
uvc_device_t *dev,
uvc_device_handle_t **devh) {
uvc_error_t ret;
struct libusb_device_handle *usb_devh;
uvc_device_handle_t *internal_devh;
struct libusb_device_descriptor desc;
UVC_ENTER();
ret = libusb_open(dev->usb_dev, &usb_devh);
UVC_DEBUG("libusb_open() = %d", ret);
if (ret != UVC_SUCCESS) {
UVC_EXIT(ret);
return ret;
}
uvc_ref_device(dev);
internal_devh = calloc(1, sizeof(*internal_devh));
internal_devh->dev = dev;
internal_devh->usb_devh = usb_devh;
ret = uvc_get_device_info(dev, &(internal_devh->info));
if (ret != UVC_SUCCESS)
goto fail;
UVC_DEBUG("claiming control interface %d", internal_devh->info->ctrl_if.bInterfaceNumber);
ret = uvc_claim_if(internal_devh, internal_devh->info->ctrl_if.bInterfaceNumber);
if (ret != UVC_SUCCESS)
goto fail;
libusb_get_device_descriptor(dev->usb_dev, &desc);
internal_devh->is_isight = (desc.idVendor == 0x05ac && desc.idProduct == 0x8501);
if (internal_devh->info->ctrl_if.bEndpointAddress) {
internal_devh->status_xfer = libusb_alloc_transfer(0);
if (!internal_devh->status_xfer) {
ret = UVC_ERROR_NO_MEM;
goto fail;
}
libusb_fill_interrupt_transfer(internal_devh->status_xfer,
usb_devh,
internal_devh->info->ctrl_if.bEndpointAddress,
internal_devh->status_buf,
sizeof(internal_devh->status_buf),
_uvc_status_callback,
internal_devh,
0);
ret = libusb_submit_transfer(internal_devh->status_xfer);
UVC_DEBUG("libusb_submit_transfer() = %d", ret);
if (ret) {
fprintf(stderr,
"uvc: device has a status interrupt endpoint, but unable to read from it\n");
goto fail;
}
}
if (dev->ctx->own_usb_ctx && dev->ctx->open_devices == NULL) {
/* Since this is our first device, we need to spawn the event handler thread */
uvc_start_handler_thread(dev->ctx);
}
DL_APPEND(dev->ctx->open_devices, internal_devh);
*devh = internal_devh;
UVC_EXIT(ret);
return ret;
fail:
if ( internal_devh->info ) {
uvc_release_if(internal_devh, internal_devh->info->ctrl_if.bInterfaceNumber);
}
libusb_close(usb_devh);
uvc_unref_device(dev);
uvc_free_devh(internal_devh);
UVC_EXIT(ret);
return ret;
}
/**
* @internal
* @brief Parses the complete device descriptor for a device
* @ingroup device
* @note Free *info with uvc_free_device_info when you're done
*
* @param dev Device to parse descriptor for
* @param info Where to store a pointer to the new info struct
*/
uvc_error_t uvc_get_device_info(uvc_device_t *dev,
uvc_device_info_t **info) {
uvc_error_t ret;
uvc_device_info_t *internal_info;
UVC_ENTER();
internal_info = calloc(1, sizeof(*internal_info));
if (!internal_info) {
UVC_EXIT(UVC_ERROR_NO_MEM);
return UVC_ERROR_NO_MEM;
}
if (libusb_get_config_descriptor(dev->usb_dev,
0,
&(internal_info->config)) != 0) {
free(internal_info);
UVC_EXIT(UVC_ERROR_IO);
return UVC_ERROR_IO;
}
ret = uvc_scan_control(dev, internal_info);
if (ret != UVC_SUCCESS) {
uvc_free_device_info(internal_info);
UVC_EXIT(ret);
return ret;
}
*info = internal_info;
UVC_EXIT(ret);
return ret;
}
/**
* @internal
* @brief Frees the device descriptor for a device
* @ingroup device
*
* @param info Which device info block to free
*/
void uvc_free_device_info(uvc_device_info_t *info) {
uvc_input_terminal_t *input_term, *input_term_tmp;
uvc_processing_unit_t *proc_unit, *proc_unit_tmp;
uvc_extension_unit_t *ext_unit, *ext_unit_tmp;
uvc_streaming_interface_t *stream_if, *stream_if_tmp;
uvc_format_desc_t *format, *format_tmp;
uvc_frame_desc_t *frame, *frame_tmp;
uvc_still_frame_desc_t *still_frame, *still_frame_tmp;
uvc_still_frame_res_t *still_res, *still_res_tmp;
UVC_ENTER();
DL_FOREACH_SAFE(info->ctrl_if.input_term_descs, input_term, input_term_tmp) {
DL_DELETE(info->ctrl_if.input_term_descs, input_term);
free(input_term);
}
DL_FOREACH_SAFE(info->ctrl_if.processing_unit_descs, proc_unit, proc_unit_tmp) {
DL_DELETE(info->ctrl_if.processing_unit_descs, proc_unit);
free(proc_unit);
}
DL_FOREACH_SAFE(info->ctrl_if.extension_unit_descs, ext_unit, ext_unit_tmp) {
DL_DELETE(info->ctrl_if.extension_unit_descs, ext_unit);
free(ext_unit);
}
DL_FOREACH_SAFE(info->stream_ifs, stream_if, stream_if_tmp) {
DL_FOREACH_SAFE(stream_if->format_descs, format, format_tmp) {
DL_FOREACH_SAFE(format->frame_descs, frame, frame_tmp) {
if (frame->intervals)
free(frame->intervals);
DL_DELETE(format->frame_descs, frame);
free(frame);
}
if(format->still_frame_desc) {
DL_FOREACH_SAFE(format->still_frame_desc, still_frame, still_frame_tmp) {
DL_FOREACH_SAFE(still_frame->imageSizePatterns, still_res, still_res_tmp) {
free(still_res);
}
if(still_frame->bCompression) {
free(still_frame->bCompression);
}
free(still_frame);
}
}
DL_DELETE(stream_if->format_descs, format);
free(format);
}
DL_DELETE(info->stream_ifs, stream_if);
free(stream_if);
}
if (info->config)
libusb_free_config_descriptor(info->config);
free(info);
UVC_EXIT_VOID();
}
/**
* @brief Get a descriptor that contains the general information about
* a device
* @ingroup device
*
* Free *desc with uvc_free_device_descriptor when you're done.
*
* @param dev Device to fetch information about
* @param[out] desc Descriptor structure
* @return Error if unable to fetch information, else SUCCESS
*/
uvc_error_t uvc_get_device_descriptor(
uvc_device_t *dev,
uvc_device_descriptor_t **desc) {
uvc_device_descriptor_t *desc_internal;
struct libusb_device_descriptor usb_desc;
struct libusb_device_handle *usb_devh;
uvc_error_t ret;
UVC_ENTER();
ret = libusb_get_device_descriptor(dev->usb_dev, &usb_desc);
if (ret != UVC_SUCCESS) {
UVC_EXIT(ret);
return ret;
}
desc_internal = calloc(1, sizeof(*desc_internal));
desc_internal->idVendor = usb_desc.idVendor;
desc_internal->idProduct = usb_desc.idProduct;
if (libusb_open(dev->usb_dev, &usb_devh) == 0) {
unsigned char buf[64];
int bytes = libusb_get_string_descriptor_ascii(
usb_devh, usb_desc.iSerialNumber, buf, sizeof(buf));
if (bytes > 0)
desc_internal->serialNumber = strdup((const char*) buf);
bytes = libusb_get_string_descriptor_ascii(
usb_devh, usb_desc.iManufacturer, buf, sizeof(buf));
if (bytes > 0)
desc_internal->manufacturer = strdup((const char*) buf);
bytes = libusb_get_string_descriptor_ascii(
usb_devh, usb_desc.iProduct, buf, sizeof(buf));
if (bytes > 0)
desc_internal->product = strdup((const char*) buf);
libusb_close(usb_devh);
} else {
UVC_DEBUG("can't open device %04x:%04x, not fetching serial etc.",
usb_desc.idVendor, usb_desc.idProduct);
}
*desc = desc_internal;
UVC_EXIT(ret);
return ret;
}
/**
* @brief Frees a device descriptor created with uvc_get_device_descriptor
* @ingroup device
*
* @param desc Descriptor to free
*/
void uvc_free_device_descriptor(
uvc_device_descriptor_t *desc) {
UVC_ENTER();
if (desc->serialNumber)
free((void*) desc->serialNumber);
if (desc->manufacturer)
free((void*) desc->manufacturer);
if (desc->product)
free((void*) desc->product);
free(desc);
UVC_EXIT_VOID();
}
/**
* @brief Get a list of the UVC devices attached to the system
* @ingroup device
*
* @note Free the list with uvc_free_device_list when you're done.
*
* @param ctx UVC context in which to list devices
* @param list List of uvc_device structures
* @return Error if unable to list devices, else SUCCESS
*/
uvc_error_t uvc_get_device_list(
uvc_context_t *ctx,
uvc_device_t ***list) {
struct libusb_device **usb_dev_list;
struct libusb_device *usb_dev;
int num_usb_devices;
uvc_device_t **list_internal;
int num_uvc_devices;
/* per device */
int dev_idx;
struct libusb_config_descriptor *config;
struct libusb_device_descriptor desc;
uint8_t got_interface;
/* per interface */
int interface_idx;
const struct libusb_interface *interface;
/* per altsetting */
int altsetting_idx;
const struct libusb_interface_descriptor *if_desc;
UVC_ENTER();
num_usb_devices = libusb_get_device_list(ctx->usb_ctx, &usb_dev_list);
if (num_usb_devices < 0) {
UVC_EXIT(UVC_ERROR_IO);
return UVC_ERROR_IO;
}
list_internal = malloc(sizeof(*list_internal));
*list_internal = NULL;
num_uvc_devices = 0;
dev_idx = -1;
while ((usb_dev = usb_dev_list[++dev_idx]) != NULL) {
got_interface = 0;
if (libusb_get_config_descriptor(usb_dev, 0, &config) != 0)
continue;
if ( libusb_get_device_descriptor ( usb_dev, &desc ) != LIBUSB_SUCCESS )
continue;
for (interface_idx = 0;
!got_interface && interface_idx < config->bNumInterfaces;
++interface_idx) {
interface = &config->interface[interface_idx];
for (altsetting_idx = 0;
!got_interface && altsetting_idx < interface->num_altsetting;
++altsetting_idx) {
if_desc = &interface->altsetting[altsetting_idx];
// Skip TIS cameras that definitely aren't UVC even though they might
// look that way
if ( 0x199e == desc.idVendor && desc.idProduct >= 0x8201 &&
desc.idProduct <= 0x8208 ) {
continue;
}
// Special case for Imaging Source cameras
/* Video, Streaming */
if ( 0x199e == desc.idVendor && ( 0x8101 == desc.idProduct ||
0x8102 == desc.idProduct ) &&
if_desc->bInterfaceClass == 255 &&
if_desc->bInterfaceSubClass == 2 ) {
got_interface = 1;
}
/* Video, Streaming */
if (if_desc->bInterfaceClass == 14 && if_desc->bInterfaceSubClass == 2) {
got_interface = 1;
}
}
}
libusb_free_config_descriptor(config);
if (got_interface) {
uvc_device_t *uvc_dev = malloc(sizeof(*uvc_dev));
uvc_dev->ctx = ctx;
uvc_dev->ref = 0;
uvc_dev->usb_dev = usb_dev;
uvc_ref_device(uvc_dev);
num_uvc_devices++;
list_internal = realloc(list_internal, (num_uvc_devices + 1) * sizeof(*list_internal));
list_internal[num_uvc_devices - 1] = uvc_dev;
list_internal[num_uvc_devices] = NULL;
UVC_DEBUG(" UVC: %d", dev_idx);
} else {
UVC_DEBUG("non-UVC: %d", dev_idx);
}
}
libusb_free_device_list(usb_dev_list, 1);
*list = list_internal;
UVC_EXIT(UVC_SUCCESS);
return UVC_SUCCESS;
}
/**
* @brief Frees a list of device structures created with uvc_get_device_list.
* @ingroup device
*
* @param list Device list to free
* @param unref_devices Decrement the reference counter for each device
* in the list, and destroy any entries that end up with zero references
*/
void uvc_free_device_list(uvc_device_t **list, uint8_t unref_devices) {
uvc_device_t *dev;
int dev_idx = 0;
UVC_ENTER();
if (unref_devices) {
while ((dev = list[dev_idx++]) != NULL) {
uvc_unref_device(dev);
}
}
free(list);
UVC_EXIT_VOID();
}
/**
* @brief Get the uvc_device_t corresponding to an open device
* @ingroup device
*
* @note Unref the uvc_device_t when you're done with it
*
* @param devh Device handle to an open UVC device
*/
uvc_device_t *uvc_get_device(uvc_device_handle_t *devh) {
uvc_ref_device(devh->dev);
return devh->dev;
}
/**
* @brief Get the underlying libusb device handle for an open device
* @ingroup device
*
* This can be used to access other interfaces on the same device, e.g.
* a webcam microphone.
*
* @note The libusb device handle is only valid while the UVC device is open;
* it will be invalidated upon calling uvc_close.
*
* @param devh UVC device handle to an open device
*/
libusb_device_handle *uvc_get_libusb_handle(uvc_device_handle_t *devh) {
return devh->usb_devh;
}
/**
* @brief Get camera terminal descriptor for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list, but iterating through
* it will make it no longer the camera terminal
*
* @param devh Device handle to an open UVC device
*/
const uvc_input_terminal_t *uvc_get_camera_terminal(uvc_device_handle_t *devh) {
const uvc_input_terminal_t *term = uvc_get_input_terminals(devh);
while(term != NULL) {
if (term->wTerminalType == UVC_ITT_CAMERA) {
break;
}
else {
term = term->next;
}
}
return term;
}
/**
* @brief Get input terminal descriptors for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list. Iterate through
* it by using the 'next' pointers.
*
* @param devh Device handle to an open UVC device
*/
const uvc_input_terminal_t *uvc_get_input_terminals(uvc_device_handle_t *devh) {
return devh->info->ctrl_if.input_term_descs;
}
/**
* @brief Get output terminal descriptors for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list. Iterate through
* it by using the 'next' pointers.
*
* @param devh Device handle to an open UVC device
*/
const uvc_output_terminal_t *uvc_get_output_terminals(uvc_device_handle_t *devh) {
return NULL; /* @todo */
}
/**
* @brief Get selector unit descriptors for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list. Iterate through
* it by using the 'next' pointers.
*
* @param devh Device handle to an open UVC device
*/
const uvc_selector_unit_t *uvc_get_selector_units(uvc_device_handle_t *devh) {
return devh->info->ctrl_if.selector_unit_descs;
}
/**
* @brief Get processing unit descriptors for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list. Iterate through
* it by using the 'next' pointers.
*
* @param devh Device handle to an open UVC device
*/
const uvc_processing_unit_t *uvc_get_processing_units(uvc_device_handle_t *devh) {
return devh->info->ctrl_if.processing_unit_descs;
}
/**
* @brief Get extension unit descriptors for the open device.
*
* @note Do not modify the returned structure.
* @note The returned structure is part of a linked list. Iterate through
* it by using the 'next' pointers.
*
* @param devh Device handle to an open UVC device
*/
const uvc_extension_unit_t *uvc_get_extension_units(uvc_device_handle_t *devh) {
return devh->info->ctrl_if.extension_unit_descs;
}
/**
* @brief Increment the reference count for a device
* @ingroup device
*
* @param dev Device to reference
*/
void uvc_ref_device(uvc_device_t *dev) {
UVC_ENTER();
dev->ref++;
libusb_ref_device(dev->usb_dev);
UVC_EXIT_VOID();
}
/**
* @brief Decrement the reference count for a device
* @ingropu device
* @note If the count reaches zero, the device will be discarded
*
* @param dev Device to unreference
*/
void uvc_unref_device(uvc_device_t *dev) {
UVC_ENTER();
libusb_unref_device(dev->usb_dev);
dev->ref--;
if (dev->ref == 0)
free(dev);
UVC_EXIT_VOID();
}
/** @internal
* Claim a UVC interface, detaching the kernel driver if necessary.
* @ingroup device
*
* @param devh UVC device handle
* @param idx UVC interface index
*/
uvc_error_t uvc_claim_if(uvc_device_handle_t *devh, int idx) {
int ret = UVC_SUCCESS;
UVC_ENTER();
if ( devh->claimed & ( 1 << idx )) {
UVC_DEBUG("attempt to claim already-claimed interface %d\n", idx );
UVC_EXIT(ret);
return ret;
}
/* Tell libusb to detach any active kernel drivers. libusb will keep track of whether
* it found a kernel driver for this interface. */
ret = libusb_detach_kernel_driver(devh->usb_devh, idx);
if (ret == UVC_SUCCESS || ret == LIBUSB_ERROR_NOT_FOUND || ret == LIBUSB_ERROR_NOT_SUPPORTED) {
UVC_DEBUG("claiming interface %d", idx);
if (!( ret = libusb_claim_interface(devh->usb_devh, idx))) {
devh->claimed |= ( 1 << idx );
}
} else {
UVC_DEBUG("not claiming interface %d: unable to detach kernel driver (%s)",
idx, uvc_strerror(ret));
}
UVC_EXIT(ret);
return ret;
}
/** @internal
* Release a UVC interface.
* @ingroup device
*
* @param devh UVC device handle
* @param idx UVC interface index
*/
uvc_error_t uvc_release_if(uvc_device_handle_t *devh, int idx) {
int ret = UVC_SUCCESS;
UVC_ENTER();
UVC_DEBUG("releasing interface %d", idx);
if (!( devh->claimed & ( 1 << idx ))) {
UVC_DEBUG("attempt to release unclaimed interface %d\n", idx );
UVC_EXIT(ret);
return ret;
}
/* libusb_release_interface *should* reset the alternate setting to the first available,
but sometimes (e.g. on Darwin) it doesn't. Thus, we do it explicitly here.
This is needed to de-initialize certain cameras. */
libusb_set_interface_alt_setting(devh->usb_devh, idx, 0);
ret = libusb_release_interface(devh->usb_devh, idx);
if (UVC_SUCCESS == ret) {
devh->claimed &= ~( 1 << idx );
/* Reattach any kernel drivers that were disabled when we claimed this interface */
ret = libusb_attach_kernel_driver(devh->usb_devh, idx);
if (ret == UVC_SUCCESS) {
UVC_DEBUG("reattached kernel driver to interface %d", idx);
} else if (ret == LIBUSB_ERROR_NOT_FOUND || ret == LIBUSB_ERROR_NOT_SUPPORTED) {
ret = UVC_SUCCESS; /* NOT_FOUND and NOT_SUPPORTED are OK: nothing to do */
} else {
UVC_DEBUG("error reattaching kernel driver to interface %d: %s",
idx, uvc_strerror(ret));
}
}
UVC_EXIT(ret);
return ret;
}
/** @internal
* Find a device's VideoControl interface and process its descriptor
* @ingroup device
*/
uvc_error_t uvc_scan_control(uvc_device_t *dev, uvc_device_info_t *info) {
const struct libusb_interface_descriptor *if_desc;
uvc_error_t parse_ret, ret;
int interface_idx;
const unsigned char *buffer;
size_t buffer_left, block_size;
UVC_ENTER();
ret = UVC_SUCCESS;
if_desc = NULL;
uvc_device_descriptor_t* dev_desc;
int haveTISCamera = 0;
uvc_get_device_descriptor ( dev, &dev_desc );
if ( 0x199e == dev_desc->idVendor && ( 0x8101 == dev_desc->idProduct ||
0x8102 == dev_desc->idProduct )) {
haveTISCamera = 1;
}
uvc_free_device_descriptor ( dev_desc );
for (interface_idx = 0; interface_idx < info->config->bNumInterfaces; ++interface_idx) {
if_desc = &info->config->interface[interface_idx].altsetting[0];
if ( haveTISCamera && if_desc->bInterfaceClass == 255 && if_desc->bInterfaceSubClass == 1) // Video, Control
break;
if (if_desc->bInterfaceClass == 14 && if_desc->bInterfaceSubClass == 1) // Video, Control
break;
if_desc = NULL;
}
if (if_desc == NULL) {
UVC_EXIT(UVC_ERROR_INVALID_DEVICE);
return UVC_ERROR_INVALID_DEVICE;
}
info->ctrl_if.bInterfaceNumber = interface_idx;
if (if_desc->bNumEndpoints != 0) {
info->ctrl_if.bEndpointAddress = if_desc->endpoint[0].bEndpointAddress;
}
buffer = if_desc->extra;
buffer_left = if_desc->extra_length;
while (buffer_left >= 3) { // parseX needs to see buf[0,2] = length,type
block_size = buffer[0];
parse_ret = uvc_parse_vc(dev, info, buffer, block_size);
if (parse_ret != UVC_SUCCESS) {
ret = parse_ret;
break;
}