stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / libuvc / src / device.c
1 /*********************************************************************
2  *********************************************************************/
3 /*********************************************************************
4  * add and modified some functions to support non-rooted Android devices
5  * and modified some function for optimaization with gcc
6  * Copyright (C) 2014-2016 saki@serenegiant All rights reserved.
7  *********************************************************************/
8 /*********************************************************************
9  * Software License Agreement (BSD License)
10  *
11  *  Copyright (C) 2010-2012 Ken Tossell
12  *  All rights reserved.
13  *
14  *  Redistribution and use in source and binary forms, with or without
15  *  modification, are permitted provided that the following conditions
16  *  are met:
17  *
18  *   * Redistributions of source code must retain the above copyright
19  *     notice, this list of conditions and the following disclaimer.
20  *   * Redistributions in binary form must reproduce the above
21  *     copyright notice, this list of conditions and the following
22  *     disclaimer in the documentation and/or other materials provided
23  *     with the distribution.
24  *   * Neither the name of the author nor other contributors may be
25  *     used to endorse or promote products derived from this software
26  *     without specific prior written permission.
27  *
28  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  *  POSSIBILITY OF SUCH DAMAGE.
40  *********************************************************************/
41 /**
42  * @defgroup device Device handling and enumeration
43  * @brief Support for finding, inspecting and opening UVC devices
44  */
45
46 #define LOCAL_DEBUG 0
47
48 #define LOG_TAG "libuvc/device"
49 #if 1   // デバッグ情報を出さない時1
50         #ifndef LOG_NDEBUG
51                 #define LOG_NDEBUG              // LOGV/LOGD/MARKを出力しない時
52                 #endif
53         #undef USE_LOGALL                       // 指定したLOGxだけを出力
54 #else
55         #define USE_LOGALL
56         #undef LOG_NDEBUG
57         #undef NDEBUG
58         #define GET_RAW_DESCRIPTOR
59 #endif
60
61 #include "libuvc/libuvc.h"
62 #include "libuvc/libuvc_internal.h"
63
64 #define UVC_DETACH_ATTACH 0     // set this 1 attach/detach kernel driver by libuvc, set this 0 automatically attach/detach by libusb
65
66 int uvc_already_open(uvc_context_t *ctx, struct libusb_device *usb_dev);
67 void uvc_free_devh(uvc_device_handle_t *devh);
68
69 uvc_error_t uvc_get_device_info(uvc_device_t *dev, uvc_device_info_t **info);
70 void uvc_free_device_info(uvc_device_info_t *info);
71
72 uvc_error_t uvc_scan_control(uvc_device_t *dev, uvc_device_info_t *info);
73 uvc_error_t uvc_parse_vc(uvc_device_t *dev, uvc_device_info_t *info,
74                 const unsigned char *block, size_t block_size);
75 uvc_error_t uvc_parse_vc_extension_unit(uvc_device_t *dev,
76                 uvc_device_info_t *info, const unsigned char *block, size_t block_size);
77 uvc_error_t uvc_parse_vc_header(uvc_device_t *dev, uvc_device_info_t *info,
78                 const unsigned char *block, size_t block_size);
79 uvc_error_t uvc_parse_vc_input_terminal(uvc_device_t *dev,
80                 uvc_device_info_t *info, const unsigned char *block, size_t block_size);
81 uvc_error_t uvc_parse_vc_processing_unit(uvc_device_t *dev,
82                 uvc_device_info_t *info, const unsigned char *block, size_t block_size);
83
84 uvc_error_t uvc_scan_streaming(uvc_device_t *dev, uvc_device_info_t *info,
85                 int interface_idx);
86 uvc_error_t uvc_parse_vs(uvc_device_t *dev, uvc_device_info_t *info,
87                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
88                 size_t block_size);
89 uvc_error_t uvc_parse_vs_format_uncompressed(
90                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
91                 size_t block_size);
92 uvc_error_t uvc_parse_vs_format_mjpeg(uvc_streaming_interface_t *stream_if,
93                 const unsigned char *block, size_t block_size);
94 uvc_error_t uvc_parse_vs_frame_uncompressed(
95                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
96                 size_t block_size);
97 uvc_error_t uvc_parse_vs_frame_format(uvc_streaming_interface_t *stream_if,
98             const unsigned char *block,
99             size_t block_size);
100 uvc_error_t uvc_parse_vs_frame_frame(uvc_streaming_interface_t *stream_if,
101             const unsigned char *block,
102             size_t block_size);
103 uvc_error_t uvc_parse_vs_input_header(uvc_streaming_interface_t *stream_if,
104                 const unsigned char *block, size_t block_size);
105
106 void _uvc_status_callback(struct libusb_transfer *transfer);
107
108 /** @internal
109  * @brief Test whether the specified USB device has been opened as a UVC device
110  * @ingroup device
111  *
112  * @param ctx Context in which to search for the UVC device
113  * @param usb_dev USB device to find
114  * @return true if the device is open in this context
115  */
116 int uvc_already_open(uvc_context_t *ctx, struct libusb_device *usb_dev) {
117         uvc_device_handle_t *devh;
118
119         DL_FOREACH(ctx->open_devices, devh)
120         {
121                 if (usb_dev == devh->dev->usb_dev)
122                         return 1;
123         }
124
125         return 0;
126 }
127
128 /** @brief Finds a camera identified by vendor, product and/or serial number
129  * @ingroup device
130  *
131  * @param[in] ctx UVC context in which to search for the camera
132  * @param[out] dev Reference to the camera, or NULL if not found
133  * @param[in] vid Vendor ID number, optional
134  * @param[in] pid Product ID number, optional
135  * @param[in] sn Serial number or NULL
136  * @return Error finding device or UVC_SUCCESS
137  */
138 uvc_error_t uvc_find_device(uvc_context_t *ctx, uvc_device_t **dev, int vid,
139                 int pid, const char *sn) {
140         uvc_error_t ret = UVC_SUCCESS;
141
142         uvc_device_t **list;
143         uvc_device_t *test_dev;
144         int dev_idx;
145         int found_dev;
146
147         UVC_ENTER();
148
149         ret = uvc_get_device_list(ctx, &list);
150
151         if (UNLIKELY(ret != UVC_SUCCESS)) {
152                 UVC_EXIT(ret);
153                 return ret;
154         }
155
156         dev_idx = 0;
157         found_dev = 0;
158
159         while (!found_dev && (test_dev = list[dev_idx++]) != NULL ) {
160                 uvc_device_descriptor_t *desc;
161
162                 if (uvc_get_device_descriptor(test_dev, &desc) != UVC_SUCCESS)
163                         continue;
164
165                 if ((!vid || desc->idVendor == vid) && (!pid || desc->idProduct == pid)
166                                 && (!sn || (desc->serialNumber && !strcmp(desc->serialNumber, sn))))
167                         found_dev = 1;
168
169                 uvc_free_device_descriptor(desc);
170         }
171
172         if (found_dev)
173                 uvc_ref_device(test_dev);
174
175         uvc_free_device_list(list, 1);
176
177         if (found_dev) {
178                 *dev = test_dev;
179                 UVC_EXIT(UVC_SUCCESS);
180                 return UVC_SUCCESS;
181         } else {
182                 UVC_EXIT(UVC_ERROR_NO_DEVICE);
183                 return UVC_ERROR_NO_DEVICE;
184         }
185 }
186
187 /**
188  * XXX add for non-rooted Android devices
189  * get uvc_device_t from specified vid/pid/serial
190  * return UVC_ERROR_NO_DEVICE when device not found
191  * return UVC_SUCCESS and set uvc_device_t when specific device found
192  */
193 uvc_error_t uvc_find_device2(uvc_context_t *ctx, uvc_device_t **device, int vid,
194                 int pid, const char *sn, int fd) {
195
196         UVC_ENTER();
197
198         LOGD("call libusb_find_device");
199         struct libusb_device *usb_dev = libusb_find_device(ctx->usb_ctx, vid, pid, sn, fd);
200
201         if (usb_dev) {
202                 *device = malloc(sizeof(uvc_device_t/* *device */));
203                 (*device)->ctx = ctx;
204                 (*device)->ref = 0;
205                 (*device)->usb_dev = usb_dev;
206                 libusb_set_device_fd(usb_dev, fd);      // assign fd to libusb_device for non-rooted Android devices
207                 uvc_ref_device(*device);
208                 UVC_EXIT(UVC_SUCCESS);
209                 return UVC_SUCCESS;
210         } else {
211                 LOGE("could not find specific device");
212                 *device = NULL;
213         }
214         UVC_EXIT(UVC_ERROR_NO_DEVICE);
215         return UVC_ERROR_NO_DEVICE;
216 }
217
218 /**
219  * XXX add for non-rooted Android device, >= Android7
220  * generate fake libusb_device according to specific params
221  * and set it to uvc_device_t to access UVC device on Android7 or later
222  */
223 uvc_error_t uvc_get_device_with_fd(uvc_context_t *ctx, uvc_device_t **device,
224                 int vid, int pid, const char *serial, int fd, int busnum, int devaddr) {
225
226         ENTER();
227
228         LOGD("call libusb_get_device_with_fd");
229         struct libusb_device *usb_dev = libusb_get_device_with_fd(ctx->usb_ctx, vid, pid, serial, fd, busnum, devaddr);
230
231         if (LIKELY(usb_dev)) {
232                 *device = malloc(sizeof(uvc_device_t/* *device */));
233                 (*device)->ctx = ctx;
234                 (*device)->ref = 0;
235                 (*device)->usb_dev = usb_dev;
236 //              libusb_set_device_fd(usb_dev, fd);      // assign fd to libusb_device for non-rooted Android devices
237                 uvc_ref_device(*device);
238                 UVC_EXIT(UVC_SUCCESS);
239                 RETURN(UVC_SUCCESS, int);
240         } else {
241                 LOGE("could not find specific device");
242                 *device = NULL;
243                 RETURN(UVC_ERROR_NO_DEVICE, int);
244         }
245
246 }
247
248 /** @brief Get the number of the bus to which the device is attached
249  * @ingroup device
250  */
251 uint8_t uvc_get_bus_number(uvc_device_t *dev) {
252         return libusb_get_bus_number(dev->usb_dev);
253 }
254
255 /** @brief Get the number assigned to the device within its bus
256  * @ingroup device
257  */
258 uint8_t uvc_get_device_address(uvc_device_t *dev) {
259         return libusb_get_device_address(dev->usb_dev);
260 }
261
262 /** @brief Open a UVC device
263  * @ingroup device
264  *
265  * @param dev Device to open
266  * @param[out] devh Handle on opened device
267  * @return Error opening device or SUCCESS
268  */
269 uvc_error_t uvc_open(uvc_device_t *dev, uvc_device_handle_t **devh) {
270         uvc_error_t ret;
271         struct libusb_device_handle *usb_devh;
272         uvc_device_handle_t *internal_devh;
273         struct libusb_device_descriptor desc;
274
275         UVC_ENTER();
276
277         ret = libusb_open(dev->usb_dev, &usb_devh);
278         UVC_DEBUG("libusb_open() = %d", ret);
279
280         if (UNLIKELY(ret != UVC_SUCCESS)) {
281                 UVC_EXIT(ret);
282                 return ret;
283         }
284
285         uvc_ref_device(dev);
286
287         internal_devh = calloc(1, sizeof(*internal_devh));
288         internal_devh->dev = dev;
289         internal_devh->usb_devh = usb_devh;
290         internal_devh->reset_on_release_if = 0; // XXX
291         ret = uvc_get_device_info(dev, &(internal_devh->info));
292         pthread_mutex_init(&internal_devh->status_mutex, NULL); // XXX saki
293
294         if (UNLIKELY(ret != UVC_SUCCESS))
295                 goto fail2;     // uvc_claim_if was not called yet and we don't need to call uvc_release_if
296 #if !UVC_DETACH_ATTACH
297         /* enable automatic attach/detach kernel driver on supported platforms in libusb */
298         libusb_set_auto_detach_kernel_driver(usb_devh, 1);
299 #endif
300         UVC_DEBUG("claiming control interface %d",
301                         internal_devh->info->ctrl_if.bInterfaceNumber);
302         ret = uvc_claim_if(internal_devh,
303                         internal_devh->info->ctrl_if.bInterfaceNumber);
304         if (UNLIKELY(ret != UVC_SUCCESS))
305                 goto fail;
306
307         libusb_get_device_descriptor(dev->usb_dev, &desc);
308         internal_devh->is_isight = (desc.idVendor == 0x05ac && desc.idProduct == 0x8501);
309
310         if (internal_devh->info->ctrl_if.bEndpointAddress) {
311                 UVC_DEBUG("status check transfer:bEndpointAddress=0x%02x", internal_devh->info->ctrl_if.bEndpointAddress);
312                 internal_devh->status_xfer = libusb_alloc_transfer(0);
313                 if (UNLIKELY(!internal_devh->status_xfer)) {
314                         ret = UVC_ERROR_NO_MEM;
315                         goto fail;
316                 }
317
318                 libusb_fill_interrupt_transfer(internal_devh->status_xfer, usb_devh,
319                                 internal_devh->info->ctrl_if.bEndpointAddress,
320                                 internal_devh->status_buf, sizeof(internal_devh->status_buf),
321                                 _uvc_status_callback, internal_devh, 0);
322                 ret = libusb_submit_transfer(internal_devh->status_xfer);
323                 UVC_DEBUG("libusb_submit_transfer() = %d", ret);
324
325                 if (UNLIKELY(ret)) {
326                         LOGE("device has a status interrupt endpoint, but unable to read from it");
327                         goto fail;
328                 }
329         } else {
330                 LOGE("internal_devh->info->ctrl_if.bEndpointAddress is null");
331         }
332
333         if (dev->ctx->own_usb_ctx && dev->ctx->open_devices == NULL) {
334                 /* Since this is our first device, we need to spawn the event handler thread */
335                 uvc_start_handler_thread(dev->ctx);
336         }
337
338         DL_APPEND(dev->ctx->open_devices, internal_devh);
339         *devh = internal_devh;
340
341         UVC_EXIT(ret);
342
343         return ret;
344
345 fail:
346         uvc_release_if(internal_devh, internal_devh->info->ctrl_if.bInterfaceNumber);   // XXX crash, assume when uvc_get_device_info failed.
347 fail2:
348 #if !UVC_DETACH_ATTACH
349         /* disable automatic attach/detach kernel driver on supported platforms in libusb */
350         libusb_set_auto_detach_kernel_driver(usb_devh, 0);
351 #endif
352         libusb_close(usb_devh);
353         uvc_unref_device(dev);
354         uvc_free_devh(internal_devh);
355
356         UVC_EXIT(ret);
357
358         return ret;
359 }
360
361 /**
362  * @internal
363  * @brief Parses the complete device descriptor for a device
364  * @ingroup device
365  * @note Free *info with uvc_free_device_info when you're done
366  *
367  * @param dev Device to parse descriptor for
368  * @param info Where to store a pointer to the new info struct
369  */
370 uvc_error_t uvc_get_device_info(uvc_device_t *dev, uvc_device_info_t **info) {
371         uvc_error_t ret;
372         uvc_device_info_t *internal_info;
373
374         UVC_ENTER();
375
376         internal_info = calloc(1, sizeof(*internal_info));
377         if (!internal_info) {
378                 UVC_EXIT(UVC_ERROR_NO_MEM);
379                 return UVC_ERROR_NO_MEM;
380         }
381         if (libusb_get_config_descriptor(dev->usb_dev, 0, &(internal_info->config)) != 0) {
382 //      if (libusb_get_active_config_descriptor(dev->usb_dev, &(internal_info->config)) != 0) {
383                 // XXX assume libusb_get_active_config_descriptor is better
384                 // but some buggy device will return error when get active config.
385                 // so we will use libusb_get_config_descriptor...
386                 free(internal_info);
387                 UVC_EXIT(UVC_ERROR_IO);
388                 return UVC_ERROR_IO;
389         }
390
391         ret = uvc_scan_control(dev, internal_info);
392         if (UNLIKELY(ret)) {
393                 uvc_free_device_info(internal_info);
394                 UVC_EXIT(ret);
395                 return ret;
396         }
397
398         *info = internal_info;
399
400         UVC_EXIT(ret);
401         return ret;
402 }
403
404 /**
405  * @internal
406  * @brief Frees the device descriptor for a device
407  * @ingroup device
408  *
409  * @param info Which device info block to free
410  */
411 void uvc_free_device_info(uvc_device_info_t *info) {
412         uvc_input_terminal_t *input_term, *input_term_tmp;
413         uvc_output_terminal_t *output_term, *output_term_tmp;
414         uvc_processing_unit_t *proc_unit, *proc_unit_tmp;
415         uvc_extension_unit_t *ext_unit, *ext_unit_tmp;
416
417         uvc_streaming_interface_t *stream_if, *stream_if_tmp;
418         uvc_format_desc_t *format, *format_tmp;
419         uvc_frame_desc_t *frame, *frame_tmp;
420
421         UVC_ENTER();
422
423         DL_FOREACH_SAFE(info->ctrl_if.input_term_descs, input_term, input_term_tmp)
424         {
425                 DL_DELETE(info->ctrl_if.input_term_descs, input_term);
426                 free(input_term);
427         }
428
429         DL_FOREACH_SAFE(info->ctrl_if.output_term_descs, output_term, output_term_tmp)
430         {
431                 DL_DELETE(info->ctrl_if.output_term_descs, output_term);
432                 free(output_term);
433         }
434
435         DL_FOREACH_SAFE(info->ctrl_if.processing_unit_descs, proc_unit, proc_unit_tmp)
436         {
437                 DL_DELETE(info->ctrl_if.processing_unit_descs, proc_unit);
438                 free(proc_unit);
439         }
440
441         DL_FOREACH_SAFE(info->ctrl_if.extension_unit_descs, ext_unit, ext_unit_tmp)
442         {
443                 DL_DELETE(info->ctrl_if.extension_unit_descs, ext_unit);
444                 free(ext_unit);
445         }
446
447         DL_FOREACH_SAFE(info->stream_ifs, stream_if, stream_if_tmp)
448         {
449                 DL_FOREACH_SAFE(stream_if->format_descs, format, format_tmp)
450                 {
451                         DL_FOREACH_SAFE(format->frame_descs, frame, frame_tmp)
452                         {
453                                 if (frame->intervals)
454                                         free(frame->intervals);
455
456                                 DL_DELETE(format->frame_descs, frame);
457                                 free(frame);
458                         }
459
460                         DL_DELETE(stream_if->format_descs, format);
461                         free(format);
462                 }
463
464                 DL_DELETE(info->stream_ifs, stream_if);
465                 free(stream_if->bmaControls);   // XXX
466                 free(stream_if);
467         }
468
469         if (info->config)
470                 libusb_free_config_descriptor(info->config);
471
472         free(info);
473
474         UVC_EXIT_VOID();
475 }
476
477 /**
478  * @brief Get a descriptor that contains the general information about
479  * a device
480  * @ingroup device
481  *
482  * Free *desc with uvc_free_device_descriptor when you're done.
483  *
484  * @param dev Device to fetch information about
485  * @param[out] desc Descriptor structure
486  * @return Error if unable to fetch information, else SUCCESS
487  */
488 uvc_error_t uvc_get_device_descriptor(uvc_device_t *dev,
489                 uvc_device_descriptor_t **desc) {
490
491         uvc_device_descriptor_t *desc_internal;
492         struct libusb_device_descriptor usb_desc;
493         struct libusb_device_handle *usb_devh;
494         uvc_error_t ret;
495
496         UVC_ENTER();
497
498         ret = libusb_get_device_descriptor(dev->usb_dev, &usb_desc);
499
500         if (UNLIKELY(ret)) {
501                 UVC_EXIT(ret);
502                 return ret;
503         }
504
505         desc_internal = calloc(1, sizeof(*desc_internal));
506         desc_internal->idVendor = usb_desc.idVendor;
507         desc_internal->idProduct = usb_desc.idProduct;
508
509         if (libusb_open(dev->usb_dev, &usb_devh) == 0) {
510                 unsigned char str_buf[255];     // XXX 64 => 255
511                 // get serialNumber
512                 int str_bytes = libusb_get_string_descriptor_ascii(usb_devh,
513                         usb_desc.iSerialNumber, str_buf, sizeof(str_buf));
514
515                 if (str_bytes > 0)
516                         desc_internal->serialNumber = strdup((const char*) str_buf);
517
518                 // get manufacturer
519                 str_bytes = libusb_get_string_descriptor_ascii(usb_devh,
520                         usb_desc.iManufacturer, str_buf, sizeof(str_buf));
521
522                 if (str_bytes > 0)
523                         desc_internal->manufacturer = strdup((const char*) str_buf);
524
525                 // get product name
526                 str_bytes = libusb_get_string_descriptor_ascii(usb_devh,
527                         usb_desc.iProduct, str_buf, sizeof(str_buf));
528
529                 if (str_bytes > 0)
530                         desc_internal->product = strdup((const char*) str_buf);
531
532                 libusb_close(usb_devh);
533         } else {
534                 UVC_DEBUG("can't open device %04x:%04x, not fetching serial",
535                                 usb_desc.idVendor, usb_desc.idProduct);
536         }
537
538         *desc = desc_internal;
539
540         UVC_EXIT(ret);
541         return ret;
542 }
543
544 /**
545  * @brief Frees a device descriptor created with uvc_get_device_descriptor
546  * @ingroup device
547  *
548  * @param desc Descriptor to free
549  */
550 void uvc_free_device_descriptor(uvc_device_descriptor_t *desc) {
551         UVC_ENTER();
552
553         if (desc->serialNumber)
554                 free((void*) desc->serialNumber);
555
556         if (desc->manufacturer)
557                 free((void*) desc->manufacturer);
558
559         if (desc->product)
560                 free((void*) desc->product);
561
562         free(desc);
563
564         UVC_EXIT_VOID();
565 }
566
567 /**
568  * @brief Get a list of the UVC devices attached to the system
569  * @ingroup device
570  *
571  * @note Free the list with uvc_free_device_list when you're done.
572  *
573  * @param ctx UVC context in which to list devices
574  * @param list List of uvc_device structures
575  * @return Error if unable to list devices, else SUCCESS
576  */
577 uvc_error_t uvc_get_device_list(uvc_context_t *ctx, uvc_device_t ***list) {
578         uvc_error_t ret;
579         struct libusb_device **usb_dev_list;
580         struct libusb_device *usb_dev;
581         int num_usb_devices;
582
583         uvc_device_t **list_internal;
584         int num_uvc_devices;
585
586         /* per device */
587         int dev_idx;
588         struct libusb_device_handle *usb_devh;
589         struct libusb_config_descriptor *config;
590         struct libusb_device_descriptor desc;
591         uint8_t got_interface;
592
593         /* per interface */
594         int interface_idx;
595         const struct libusb_interface *interface;
596
597         /* per altsetting */
598         int altsetting_idx;
599         const struct libusb_interface_descriptor *if_desc;
600
601         UVC_ENTER();
602
603         num_usb_devices = libusb_get_device_list(ctx->usb_ctx, &usb_dev_list);
604
605         if (UNLIKELY(num_usb_devices < 0)) {
606                 UVC_EXIT(UVC_ERROR_IO);
607                 return UVC_ERROR_IO;
608         }
609
610         list_internal = malloc(sizeof(*list_internal));
611         *list_internal = NULL;
612
613         num_uvc_devices = 0;
614         dev_idx = -1;
615
616         while ((usb_dev = usb_dev_list[++dev_idx]) != NULL ) {
617                 usb_devh = NULL;
618                 got_interface = 0;
619
620                 if (libusb_get_config_descriptor(usb_dev, 0, &config) != 0)
621                         continue;
622
623                 if (libusb_get_device_descriptor (usb_dev, &desc) != LIBUSB_SUCCESS)
624                         continue;
625
626                 // Special case for Imaging Source cameras
627                 if ((0x199e == desc.idVendor) && (0x8101 == desc.idProduct)) {
628                         got_interface = 1;
629                 } else {
630                         for (interface_idx = 0;
631                                         !got_interface && interface_idx < config->bNumInterfaces;
632                                         ++interface_idx) {
633                                 interface = &config->interface[interface_idx];
634
635                                 for (altsetting_idx = 0;
636                                                 !got_interface && altsetting_idx < interface->num_altsetting;
637                                                 ++altsetting_idx) {
638                                         if_desc = &interface->altsetting[altsetting_idx];
639
640                                         /* Video, Streaming */
641                                         if (if_desc->bInterfaceClass == 14
642                                                         && if_desc->bInterfaceSubClass == 2) {
643                                                 got_interface = 1;
644                                         }
645                                 }
646                         }
647                 }
648
649                 libusb_free_config_descriptor(config);
650
651                 if (got_interface) {
652                         uvc_device_t *uvc_dev = malloc(sizeof(*uvc_dev));
653                         uvc_dev->ctx = ctx;
654                         uvc_dev->ref = 0;
655                         uvc_dev->usb_dev = usb_dev;
656                         uvc_ref_device(uvc_dev);
657
658                         num_uvc_devices++;
659                         list_internal = realloc(list_internal,
660                                         (num_uvc_devices + 1) * sizeof(*list_internal));
661
662                         list_internal[num_uvc_devices - 1] = uvc_dev;
663                         list_internal[num_uvc_devices] = NULL;
664
665                         UVC_DEBUG("    UVC: %d", dev_idx);
666                 } else {
667                         UVC_DEBUG("non-UVC: %d", dev_idx);
668                 }
669         }
670
671         libusb_free_device_list(usb_dev_list, 1);
672
673         *list = list_internal;
674
675         UVC_EXIT(UVC_SUCCESS);
676         return UVC_SUCCESS;
677 }
678
679 /**
680  * @brief Frees a list of device structures created with uvc_get_device_list.
681  * @ingroup device
682  *
683  * @param list Device list to free
684  * @param unref_devices Decrement the reference counter for each device
685  * in the list, and destroy any entries that end up with zero references
686  */
687 void uvc_free_device_list(uvc_device_t **list, uint8_t unref_devices) {
688         uvc_device_t *dev;
689         int dev_idx = 0;
690
691         UVC_ENTER();
692
693         if (unref_devices) {
694                 while ((dev = list[dev_idx++]) != NULL ) {
695                         uvc_unref_device(dev);
696                 }
697         }
698
699         free(list);
700
701         UVC_EXIT_VOID();
702 }
703
704 /**
705  * @brief Get the uvc_device_t corresponding to an open device
706  * @ingroup device
707  *
708  * @note Unref the uvc_device_t when you're done with it
709  *
710  * @param devh Device handle to an open UVC device
711  */
712 uvc_device_t *uvc_get_device(uvc_device_handle_t *devh) {
713         uvc_ref_device(devh->dev);
714         return devh->dev;
715 }
716
717 /**
718  * @brief Get the underlying libusb device handle for an open device
719  * @ingroup device
720  *
721  * This can be used to access other interfaces on the same device, e.g.
722  * a webcam microphone.
723  *
724  * @note The libusb device handle is only valid while the UVC device is open;
725  * it will be invalidated upon calling uvc_close.
726  *
727  * @param devh UVC device handle to an open device
728  */
729 libusb_device_handle *uvc_get_libusb_handle(uvc_device_handle_t *devh) {
730         return devh->usb_devh;
731 }
732
733 /**
734  * @brief Get input terminal descriptors for the open device.
735  *
736  * @note Do not modify the returned structure.
737  * @note The returned structure is part of a linked list. Iterate through
738  *       it by using the 'next' pointers.
739  *
740  * @param devh Device handle to an open UVC device
741  */
742 const uvc_input_terminal_t *uvc_get_input_terminals(uvc_device_handle_t *devh) {
743         return devh->info->ctrl_if.input_term_descs;
744 }
745
746 /**
747  * @brief Get output terminal descriptors for the open device.
748  *
749  * @note Do not modify the returned structure.
750  * @note The returned structure is part of a linked list. Iterate through
751  *       it by using the 'next' pointers.
752  *
753  * @param devh Device handle to an open UVC device
754  */
755 const uvc_output_terminal_t *uvc_get_output_terminals(uvc_device_handle_t *devh) {
756         return devh->info->ctrl_if.output_term_descs ;
757 }
758
759 /**
760  * @brief Get processing unit descriptors for the open device.
761  *
762  * @note Do not modify the returned structure.
763  * @note The returned structure is part of a linked list. Iterate through
764  *       it by using the 'next' pointers.
765  *
766  * @param devh Device handle to an open UVC device
767  */
768 const uvc_processing_unit_t *uvc_get_processing_units(uvc_device_handle_t *devh) {
769         return devh->info->ctrl_if.processing_unit_descs;
770 }
771
772 /**
773  * @brief Get extension unit descriptors for the open device.
774  *
775  * @note Do not modify the returned structure.
776  * @note The returned structure is part of a linked list. Iterate through
777  *       it by using the 'next' pointers.
778  *
779  * @param devh Device handle to an open UVC device
780  */
781 const uvc_extension_unit_t *uvc_get_extension_units(uvc_device_handle_t *devh) {
782         return devh->info->ctrl_if.extension_unit_descs;
783 }
784
785 /**
786  * @brief Increment the reference count for a device
787  * @ingroup device
788  *
789  * @param dev Device to reference
790  */
791 void uvc_ref_device(uvc_device_t *dev) {
792         UVC_ENTER();
793
794         dev->ref++;     // これ排他制御要るんちゃうかなぁ(。・_・。)
795 //      LOGI("ref=%d", dev->ref);
796         libusb_ref_device(dev->usb_dev);
797
798         UVC_EXIT_VOID();
799 }
800
801 /**
802  * @brief Decrement the reference count for a device
803  * @ingropu device
804  * @note If the count reaches zero, the device will be discarded
805  *
806  * @param dev Device to unreference
807  */
808 void uvc_unref_device(uvc_device_t *dev) {
809         UVC_ENTER();
810
811         libusb_unref_device(dev->usb_dev);
812         dev->ref--;     // これ排他制御要るんちゃうかなぁ(。・_・。)
813
814 //      LOGI("ref=%d", dev->ref);
815         if (dev->ref == 0) {
816                 free(dev);
817                 dev = NULL;
818         }
819
820         UVC_EXIT_VOID();
821 }
822
823 /** @internal
824  * Claim a UVC interface, detaching the kernel driver if necessary.
825  * @ingroup device
826  *
827  * @param devh UVC device handle
828  * @param idx UVC interface index
829  */
830 uvc_error_t uvc_claim_if(uvc_device_handle_t *devh, int idx) {
831         int ret;
832
833         UVC_ENTER();
834 #if !UVC_DETACH_ATTACH
835         // libusb automatically attach/detach kernel driver on supported platforms
836         UVC_DEBUG("claiming interface %d", idx);
837         ret = libusb_claim_interface(devh->usb_devh, idx);
838 #else
839         /* Tell libusb to detach any active kernel drivers. libusb will keep track of whether
840          * it found a kernel driver for this interface. */
841         ret = libusb_detach_kernel_driver(devh->usb_devh, idx);
842         
843         if LIKELY(!ret || ret == LIBUSB_ERROR_NOT_FOUND || ret == LIBUSB_ERROR_NOT_SUPPORTED) {
844                 UVC_DEBUG("claiming interface %d", idx);
845                 ret = libusb_claim_interface(devh->usb_devh, idx);
846         } else {
847             UVC_DEBUG("not claiming interface %d: unable to detach kernel driver (%s)",
848                         idx, uvc_strerror(ret));
849         }
850 #endif
851         UVC_EXIT(ret);
852         return ret;
853 }
854
855 /** @internal
856  * Release a UVC interface.
857  * @ingroup device
858  *
859  * @param devh UVC device handle
860  * @param idx UVC interface index
861  */
862 uvc_error_t uvc_release_if(uvc_device_handle_t *devh, int idx) {
863         int ret;
864
865         UVC_ENTER();
866         UVC_DEBUG("releasing interface %d", idx);
867         /* libusb_release_interface *should* reset the alternate setting to the first available,
868          but sometimes (e.g. on Darwin) it doesn't. Thus, we do it explicitly here.
869          This is needed to de-initialize certain cameras. */
870         // XXX but resetting the alt setting here manytimes leads trouble
871         // on GT-N7100(international Galaxy Note2 at lease with Android4.4.2)
872         // so we add flag to avoid the issue
873         if (devh->reset_on_release_if)
874                 libusb_set_interface_alt_setting(devh->usb_devh, idx, 0);
875
876         ret = libusb_release_interface(devh->usb_devh, idx);
877
878 #if !UVC_DETACH_ATTACH
879         // libusb automatically attach/detach kernel driver on supported platforms
880         // and nothing to do here
881 #else
882         if (UVC_SUCCESS == ret) {
883                 /* Reattach any kernel drivers that were disabled when we claimed this interface */
884                 ret = libusb_attach_kernel_driver(devh->usb_devh, idx);
885
886                 if LIKELY(!ret) {
887                         UVC_DEBUG("reattached kernel driver to interface %d", idx);
888                 } else if (ret == LIBUSB_ERROR_NOT_FOUND || ret == LIBUSB_ERROR_NOT_SUPPORTED) {
889                         ret = UVC_SUCCESS;  /* NOT_FOUND and NOT_SUPPORTED are OK: nothing to do */
890                 } else {
891                         UVC_DEBUG("error reattaching kernel driver to interface %d: %s",
892                 idx, uvc_strerror(ret));
893                 }
894         }
895 #endif
896         UVC_EXIT(ret);
897         return ret;
898 }
899
900 /** @internal
901  * Find a device's VideoControl interface and process its descriptor
902  * @ingroup device
903  */
904 uvc_error_t uvc_scan_control(uvc_device_t *dev, uvc_device_info_t *info) {
905         const struct libusb_interface_descriptor *if_desc;
906         uvc_error_t parse_ret, ret;
907         int interface_idx;
908         const unsigned char *buffer;
909         size_t buffer_left, block_size;
910
911         UVC_ENTER();
912
913         ret = UVC_SUCCESS;
914         if_desc = NULL;
915
916         if (LIKELY(info && info->config)) {     // XXX add to avoid crash
917                 MARK("bNumInterfaces=%d", info->config->bNumInterfaces);
918                 for (interface_idx = 0; interface_idx < info->config->bNumInterfaces; ++interface_idx) {
919                         if_desc = &info->config->interface[interface_idx].altsetting[0];
920                         MARK("interface_idx=%d:bInterfaceClass=%02x,bInterfaceSubClass=%02x", interface_idx, if_desc->bInterfaceClass, if_desc->bInterfaceSubClass);
921                         // select first found Video control
922                         if (if_desc->bInterfaceClass == LIBUSB_CLASS_VIDEO/*14*/ && if_desc->bInterfaceSubClass == 1) // Video, Control
923                                 break;
924
925                         // Another TIS camera hack.
926                         if (if_desc->bInterfaceClass == 255 && if_desc->bInterfaceSubClass == 1) {
927                                 uvc_device_descriptor_t* dev_desc;
928                                 int haveTISCamera = 0;
929                                 uvc_get_device_descriptor (dev, &dev_desc);
930                                 if (dev_desc->idVendor == 0x199e && dev_desc->idProduct == 0x8101) {
931                                         haveTISCamera = 1;
932                                 }
933                                 uvc_free_device_descriptor (dev_desc);
934                                 if (haveTISCamera) {
935                                         break;
936                                 }
937                         }
938                         if_desc = NULL;
939                 }
940         }
941
942         if (UNLIKELY(!if_desc)) {
943                 UVC_EXIT(UVC_ERROR_INVALID_DEVICE);
944                 LOGE("UVC_ERROR_INVALID_DEVICE");
945                 return UVC_ERROR_INVALID_DEVICE;
946         }
947
948         info->ctrl_if.bInterfaceNumber = interface_idx;
949         if (if_desc->bNumEndpoints != 0) {
950                 info->ctrl_if.bEndpointAddress = if_desc->endpoint[0].bEndpointAddress;
951         }
952
953         buffer = if_desc->extra;
954         buffer_left = if_desc->extra_length;
955
956         while (buffer_left >= 3) { // parseX needs to see buf[0,2] = length,type
957                 block_size = buffer[0];
958                 parse_ret = uvc_parse_vc(dev, info, buffer, block_size);
959
960                 if (parse_ret != UVC_SUCCESS) {
961                         ret = parse_ret;
962                         break;
963                 }
964
965                 buffer_left -= block_size;
966                 buffer += block_size;
967         }
968
969         UVC_EXIT(ret);
970         return ret;
971 }
972
973 /** @internal
974  * @brief Parse a VideoControl header.
975  * @ingroup device
976  */
977 uvc_error_t uvc_parse_vc_header(uvc_device_t *dev, uvc_device_info_t *info,
978                 const unsigned char *block, size_t block_size) {
979         size_t i;
980         uvc_error_t scan_ret, ret = UVC_SUCCESS;
981
982         UVC_ENTER();
983
984         /*
985          int uvc_version;
986          uvc_version = (block[4] >> 4) * 1000 + (block[4] & 0x0f) * 100
987          + (block[3] >> 4) * 10 + (block[3] & 0x0f);
988          */
989
990         info->ctrl_if.bcdUVC = SW_TO_SHORT(&block[3]);
991
992         switch (info->ctrl_if.bcdUVC) {
993         case 0x0100:
994         case 0x010a:
995         case 0x0110:
996         case 0x0150:    // XXX add to support UVC 1.5
997                 break;
998         default:
999                 UVC_EXIT(UVC_ERROR_NOT_SUPPORTED);
1000                 return UVC_ERROR_NOT_SUPPORTED;
1001         }
1002
1003         for (i = 12; i < block_size; ++i) {
1004                 scan_ret = uvc_scan_streaming(dev, info, block[i]);
1005                 if (scan_ret != UVC_SUCCESS) {
1006                         ret = scan_ret;
1007                         break;
1008                 }
1009         }
1010
1011         UVC_EXIT(ret);
1012         return ret;
1013 }
1014
1015 /** @internal
1016  * @brief Parse a VideoControl input terminal.
1017  * @ingroup device
1018  */
1019 uvc_error_t uvc_parse_vc_input_terminal(uvc_device_t *dev,
1020                 uvc_device_info_t *info, const unsigned char *block, size_t block_size) {
1021         uvc_input_terminal_t *term;
1022         size_t i;
1023
1024         UVC_ENTER();
1025
1026         /* only supporting camera-type input terminals */
1027         if (SW_TO_SHORT(&block[4]) != UVC_ITT_CAMERA) {
1028                 UVC_EXIT(UVC_SUCCESS);
1029                 return UVC_SUCCESS;
1030         }
1031
1032         term = calloc(1, sizeof(*term));
1033
1034         term->bTerminalID = block[3];
1035         term->wTerminalType = SW_TO_SHORT(&block[4]);
1036         term->wObjectiveFocalLengthMin = SW_TO_SHORT(&block[8]);
1037         term->wObjectiveFocalLengthMax = SW_TO_SHORT(&block[10]);
1038         term->wOcularFocalLength = SW_TO_SHORT(&block[12]);
1039         term->request = (term->bTerminalID << 8) | info->ctrl_if.bInterfaceNumber;
1040         term->bmControls = 0;   // XXX
1041         for (i = 14 + block[14]; i >= 15; i--)
1042                 term->bmControls = block[i] + (term->bmControls << 8);
1043
1044         DL_APPEND(info->ctrl_if.input_term_descs, term);
1045
1046         UVC_EXIT(UVC_SUCCESS);
1047         return UVC_SUCCESS;
1048 }
1049
1050 /** @internal
1051  * @brief Parse a output terminal.
1052  * @ingroup device
1053  */
1054 uvc_error_t uvc_parse_vc_output_terminal(uvc_device_t *dev,
1055                 uvc_device_info_t *info, const unsigned char *block, size_t block_size) {
1056         uvc_output_terminal_t *term;
1057         size_t i;
1058
1059         UVC_ENTER();
1060
1061         /* only supporting display-type input terminals */
1062         if (SW_TO_SHORT(&block[4]) != UVC_OTT_DISPLAY) {
1063                 UVC_EXIT(UVC_SUCCESS);
1064                 return UVC_SUCCESS;
1065         }
1066
1067         term = calloc(1, sizeof(*term));
1068
1069         term->bTerminalID = block[3];
1070         term->wTerminalType = SW_TO_SHORT(&block[4]);
1071         term->bAssocTerminal = block[6];
1072         term->bSourceID = block[7];
1073         term->iTerminal = block[8];
1074         term->request = (term->bTerminalID << 8) | info->ctrl_if.bInterfaceNumber;      // XXX
1075         // TODO depending on the wTerminalType
1076
1077         DL_APPEND(info->ctrl_if.output_term_descs, term);
1078
1079         UVC_EXIT(UVC_SUCCESS);
1080         return UVC_SUCCESS;
1081 }
1082
1083 /** @internal
1084  * @brief Parse a VideoControl processing unit.
1085  * @ingroup device
1086  */
1087 uvc_error_t uvc_parse_vc_processing_unit(uvc_device_t *dev,
1088                 uvc_device_info_t *info, const unsigned char *block, size_t block_size) {
1089         uvc_processing_unit_t *unit;
1090         size_t i;
1091
1092         UVC_ENTER();
1093
1094         unit = calloc(1, sizeof(*unit));
1095         unit->bUnitID = block[3];
1096         unit->bSourceID = block[4];
1097         unit->request = (unit->bUnitID << 8) | info->ctrl_if.bInterfaceNumber;  // XXX
1098
1099         unit->bmControls = 0;   // XXX
1100         for (i = 7 + block[7]; i >= 8; i--)
1101                 unit->bmControls = block[i] + (unit->bmControls << 8);
1102
1103         DL_APPEND(info->ctrl_if.processing_unit_descs, unit);
1104
1105         UVC_EXIT(UVC_SUCCESS);
1106         return UVC_SUCCESS;
1107 }
1108
1109 /** @internal
1110  * @brief Parse a VideoControl extension unit.
1111  * @ingroup device
1112  */
1113 uvc_error_t uvc_parse_vc_extension_unit(uvc_device_t *dev,
1114                 uvc_device_info_t *info, const unsigned char *block, size_t block_size) {
1115         uvc_extension_unit_t *unit = calloc(1, sizeof(*unit));
1116         const uint8_t *start_of_controls;
1117         int size_of_controls, num_in_pins;
1118         int i;
1119
1120         UVC_ENTER();
1121
1122         unit->bUnitID = block[3];
1123         memcpy(unit->guidExtensionCode, &block[4], 16);
1124
1125         num_in_pins = block[21];
1126         size_of_controls = block[22 + num_in_pins];
1127         start_of_controls = &block[23 + num_in_pins];
1128         unit->request = (unit->bUnitID << 8) | info->ctrl_if.bInterfaceNumber;
1129
1130         unit->bmControls = 0;   // XXX
1131         for (i = size_of_controls - 1; i >= 0; i--)
1132                 unit->bmControls = start_of_controls[i] + (unit->bmControls << 8);
1133
1134         DL_APPEND(info->ctrl_if.extension_unit_descs, unit);
1135
1136         UVC_EXIT(UVC_SUCCESS);
1137         return UVC_SUCCESS;
1138 }
1139
1140 /** @internal
1141  * Process a single VideoControl descriptor block
1142  * @ingroup device
1143  */
1144 uvc_error_t uvc_parse_vc(uvc_device_t *dev, uvc_device_info_t *info,
1145                 const unsigned char *block, size_t block_size) {
1146         int descriptor_subtype;
1147         uvc_error_t ret = UVC_SUCCESS;
1148
1149         UVC_ENTER();
1150
1151         if (block[1] != LIBUSB_DT_CS_INTERFACE/*36*/) { // not a CS_INTERFACE descriptor??
1152                 UVC_EXIT(UVC_SUCCESS);
1153                 return UVC_SUCCESS; // UVC_ERROR_INVALID_DEVICE;
1154         }
1155
1156         descriptor_subtype = block[2];
1157
1158         switch (descriptor_subtype) {
1159         case UVC_VC_HEADER:
1160                 ret = uvc_parse_vc_header(dev, info, block, block_size);
1161                 break;
1162         case UVC_VC_INPUT_TERMINAL:
1163                 ret = uvc_parse_vc_input_terminal(dev, info, block, block_size);
1164                 break;
1165         case UVC_VC_OUTPUT_TERMINAL:
1166                 break;
1167         case UVC_VC_SELECTOR_UNIT:
1168                 break;
1169         case UVC_VC_PROCESSING_UNIT:
1170                 ret = uvc_parse_vc_processing_unit(dev, info, block, block_size);
1171                 break;
1172         case UVC_VC_EXTENSION_UNIT:
1173                 ret = uvc_parse_vc_extension_unit(dev, info, block, block_size);
1174                 break;
1175         default:
1176                 LOGW("UVC_ERROR_INVALID_DEVICE:descriptor_subtype=0x%02x", descriptor_subtype);
1177                 ret = UVC_ERROR_INVALID_DEVICE;
1178         }
1179
1180         UVC_EXIT(ret);
1181         return ret;
1182 }
1183
1184 /** @internal
1185  * Process a VideoStreaming interface
1186  * @ingroup device
1187  */
1188 uvc_error_t uvc_scan_streaming(uvc_device_t *dev, uvc_device_info_t *info,
1189                 int interface_idx) {
1190         const struct libusb_interface_descriptor *if_desc;
1191         const unsigned char *buffer;
1192         size_t buffer_left, block_size;
1193         uvc_error_t ret, parse_ret;
1194         uvc_streaming_interface_t *stream_if;
1195
1196         UVC_ENTER();
1197
1198         ret = UVC_SUCCESS;
1199
1200         if_desc = &(info->config->interface[interface_idx].altsetting[0]);
1201         buffer = if_desc->extra;
1202         buffer_left = if_desc->extra_length;
1203         // XXX some device have it's format descriptions after the endpoint descriptor
1204         if (UNLIKELY(!buffer || !buffer_left)) {
1205                 if (if_desc->bNumEndpoints && if_desc->endpoint) {
1206                         // try to use extra data in endpoint[0]
1207                         buffer = if_desc->endpoint[0].extra;
1208                         buffer_left = if_desc->endpoint[0].extra_length;
1209                 }
1210         }
1211         stream_if = calloc(1, sizeof(*stream_if));
1212         stream_if->parent = info;
1213         stream_if->bInterfaceNumber = if_desc->bInterfaceNumber;
1214         DL_APPEND(info->stream_ifs, stream_if);
1215
1216         if (LIKELY(buffer_left >= 3)) {
1217                 while (buffer_left >= 3) {
1218                         block_size = buffer[0];
1219 //                      MARK("bDescriptorType=0x%02x", buffer[1]);
1220                         parse_ret = uvc_parse_vs(dev, info, stream_if, buffer, block_size);
1221
1222                         if (parse_ret != UVC_SUCCESS) {
1223                                 ret = parse_ret;
1224                                 break;
1225                         }
1226
1227                         buffer_left -= block_size;
1228                         buffer += block_size;
1229                 }
1230         } else {
1231                 LOGW("This VideoStreaming interface has no extra data");
1232         }
1233
1234         UVC_EXIT(ret);
1235         return ret;
1236 }
1237
1238 /** @internal
1239  * @brief Parse a VideoStreaming header block.
1240  * @ingroup device
1241  */
1242 uvc_error_t uvc_parse_vs_input_header(uvc_streaming_interface_t *stream_if,
1243                 const unsigned char *block, size_t block_size) {
1244         UVC_ENTER();
1245
1246         stream_if->bEndpointAddress = block[6] & 0x8f;
1247         stream_if->bTerminalLink = block[8];
1248         stream_if->bmInfo = block[7];   // XXX
1249         stream_if->bStillCaptureMethod = block[9];      // XXX
1250         stream_if->bTriggerSupport = block[10]; // XXX
1251         stream_if->bTriggerUsage = block[11];   // XXX
1252         stream_if->bmaControls = NULL;
1253         const uint8_t n = block[12];
1254         if (LIKELY(n)) {
1255                 const uint8_t p = (block_size - 13) / n;
1256                 if (LIKELY(p)) {
1257                         uint64_t *bmaControls = (uint64_t *)calloc(p, sizeof(uint64_t));
1258                         stream_if->bmaControls = bmaControls;
1259                         const uint8_t *bma;
1260                         int pp, nn;
1261                         for (pp = 1; pp <= p; pp++) {
1262                                 bma = &block[12 + pp * n];
1263                                 for (nn = n - 1; nn >= 0; --nn) {
1264                                         *bmaControls = *bma-- + (*bmaControls << 8);
1265                                 }
1266                                 bmaControls++;
1267                         }
1268                 }
1269         }
1270         UVC_EXIT(UVC_SUCCESS);
1271         return UVC_SUCCESS;
1272 }
1273
1274 /** @internal
1275  * @brief Parse a VideoStreaming uncompressed format block.
1276  * @ingroup device
1277  */
1278 uvc_error_t uvc_parse_vs_format_uncompressed(
1279                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
1280                 size_t block_size) {
1281         UVC_ENTER();
1282
1283         uvc_format_desc_t *format = calloc(1, sizeof(*format));
1284
1285         format->parent = stream_if;
1286         format->bDescriptorSubtype = block[2];
1287         format->bFormatIndex = block[3];
1288         //format->bmCapabilities = block[4];
1289         //format->bmFlags = block[5];
1290         memcpy(format->guidFormat, &block[5], 16);
1291         format->bBitsPerPixel = block[21];
1292         format->bDefaultFrameIndex = block[22];
1293         format->bAspectRatioX = block[23];
1294         format->bAspectRatioY = block[24];
1295         format->bmInterlaceFlags = block[25];
1296         format->bCopyProtect = block[26];
1297
1298         DL_APPEND(stream_if->format_descs, format);
1299
1300         UVC_EXIT(UVC_SUCCESS);
1301         return UVC_SUCCESS;
1302 }
1303
1304 /** @internal
1305  * @brief Parse a VideoStreaming frame format block.
1306  * @ingroup device
1307  */
1308 uvc_error_t uvc_parse_vs_frame_format(uvc_streaming_interface_t *stream_if,
1309         const unsigned char *block, size_t block_size) {
1310         UVC_ENTER();
1311
1312         uvc_format_desc_t *format = calloc(1, sizeof(*format));
1313
1314         format->parent = stream_if;
1315         format->bDescriptorSubtype = block[2];
1316         format->bFormatIndex = block[3];
1317         format->bNumFrameDescriptors = block[4];
1318         memcpy(format->guidFormat, &block[5], 16);
1319         format->bBitsPerPixel = block[21];
1320         format->bDefaultFrameIndex = block[22];
1321         format->bAspectRatioX = block[23];
1322         format->bAspectRatioY = block[24];
1323         format->bmInterlaceFlags = block[25];
1324         format->bCopyProtect = block[26];
1325         format->bVariableSize = block[27];
1326
1327         DL_APPEND(stream_if->format_descs, format);
1328
1329         UVC_EXIT(UVC_SUCCESS);
1330         return UVC_SUCCESS;
1331 }
1332
1333 /** @internal
1334  * @brief Parse a VideoStreaming MJPEG format block.
1335  * @ingroup device
1336  */
1337 uvc_error_t uvc_parse_vs_format_mjpeg(uvc_streaming_interface_t *stream_if,
1338                 const unsigned char *block, size_t block_size) {
1339         UVC_ENTER();
1340
1341         uvc_format_desc_t *format = calloc(1, sizeof(*format));
1342
1343         format->parent = stream_if;
1344         format->bDescriptorSubtype = block[2];
1345         format->bFormatIndex = block[3];
1346         memcpy(format->fourccFormat, "MJPG", 4);
1347         format->bmFlags = block[5];
1348         format->bBitsPerPixel = 0;
1349         format->bDefaultFrameIndex = block[6];
1350         format->bAspectRatioX = block[7];
1351         format->bAspectRatioY = block[8];
1352         format->bmInterlaceFlags = block[9];
1353         format->bCopyProtect = block[10];
1354
1355         DL_APPEND(stream_if->format_descs, format);
1356
1357         UVC_EXIT(UVC_SUCCESS);
1358         return UVC_SUCCESS;
1359 }
1360
1361 /** @internal
1362  * @brief Parse a VideoStreaming uncompressed frame block.
1363  * @ingroup device
1364  */
1365 uvc_error_t uvc_parse_vs_frame_frame(uvc_streaming_interface_t *stream_if,
1366                                             const unsigned char *block,
1367                                             size_t block_size) {
1368   uvc_format_desc_t *format;
1369   uvc_frame_desc_t *frame;
1370
1371   const unsigned char *p;
1372   int i;
1373
1374   UVC_ENTER();
1375
1376   format = stream_if->format_descs->prev;
1377   frame = calloc(1, sizeof(*frame));
1378
1379   frame->parent = format;
1380
1381   frame->bDescriptorSubtype = block[2];
1382   frame->bFrameIndex = block[3];
1383   frame->bmCapabilities = block[4];
1384   frame->wWidth = block[5] + (block[6] << 8);
1385   frame->wHeight = block[7] + (block[8] << 8);
1386   frame->dwMinBitRate = DW_TO_INT(&block[9]);
1387   frame->dwMaxBitRate = DW_TO_INT(&block[13]);
1388   frame->dwDefaultFrameInterval = DW_TO_INT(&block[17]);
1389   frame->bFrameIntervalType = block[21];
1390   frame->dwBytesPerLine = DW_TO_INT(&block[22]);
1391
1392   if (block[21] == 0) {
1393     frame->dwMinFrameInterval = DW_TO_INT(&block[26]);
1394     frame->dwMaxFrameInterval = DW_TO_INT(&block[30]);
1395     frame->dwFrameIntervalStep = DW_TO_INT(&block[34]);
1396   } else {
1397     frame->intervals = calloc(block[21] + 1, sizeof(frame->intervals[0]));
1398     p = &block[26];
1399
1400     for (i = 0; i < block[21]; ++i) {
1401       frame->intervals[i] = DW_TO_INT(p);
1402       p += 4;
1403     }
1404     frame->intervals[block[21]] = 0;
1405   }
1406
1407   DL_APPEND(format->frame_descs, frame);
1408
1409   UVC_EXIT(UVC_SUCCESS);
1410   return UVC_SUCCESS;
1411 }
1412
1413 /** @internal
1414  * @brief Parse a VideoStreaming uncompressed frame block.
1415  * @ingroup device
1416  */
1417 uvc_error_t uvc_parse_vs_frame_uncompressed(
1418                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
1419                 size_t block_size) {
1420         uvc_format_desc_t *format;
1421         uvc_frame_desc_t *frame;
1422         uint8_t frame_type;
1423         uint8_t n;
1424         uint32_t interval;
1425
1426         const unsigned char *p;
1427         int i;
1428
1429         UVC_ENTER();
1430
1431         format = stream_if->format_descs->prev;
1432         frame = calloc(1, sizeof(*frame));
1433
1434         frame->parent = format;
1435
1436         frame_type = frame->bDescriptorSubtype = block[2];
1437         frame->bFrameIndex = block[3];
1438         frame->bmCapabilities = block[4];
1439         frame->wWidth = block[5] + (block[6] << 8);
1440         frame->wHeight = block[7] + (block[8] << 8);
1441         frame->dwMinBitRate = DW_TO_INT(&block[9]);
1442         frame->dwMaxBitRate = DW_TO_INT(&block[13]);
1443         frame->dwMaxVideoFrameBufferSize = DW_TO_INT(&block[17]);
1444         frame->dwDefaultFrameInterval = DW_TO_INT(&block[21]);
1445         n = frame->bFrameIntervalType = block[25];
1446
1447         if (!n) {
1448                 frame->dwMinFrameInterval = DW_TO_INT(&block[26]);
1449                 frame->dwMaxFrameInterval = DW_TO_INT(&block[30]);
1450                 frame->dwFrameIntervalStep = DW_TO_INT(&block[34]);
1451         } else {
1452                 frame->intervals = calloc(n + 1, sizeof(frame->intervals[0]));
1453                 p = &block[26];
1454
1455                 for (i = 0; i < n; ++i) {
1456                         interval = DW_TO_INT(p);
1457                         frame->intervals[i] = interval ? interval : 1;
1458                         p += 4;
1459                 }
1460                 frame->intervals[n] = 0;
1461                 
1462                 frame->dwDefaultFrameInterval
1463                         = MIN(frame->intervals[n-1],
1464                                 MAX(frame->intervals[0], frame->dwDefaultFrameInterval));
1465         }
1466         
1467         if (frame_type == UVC_VS_FRAME_UNCOMPRESSED) {
1468                 frame->dwMaxVideoFrameBufferSize
1469                         = format->bBitsPerPixel * frame->wWidth * frame->wHeight / 8;
1470         }
1471         
1472         
1473         DL_APPEND(format->frame_descs, frame);
1474
1475         UVC_EXIT(UVC_SUCCESS);
1476         return UVC_SUCCESS;
1477 }
1478
1479 /** @internal
1480  * Process a single VideoStreaming descriptor block
1481  * @ingroup device
1482  */
1483 uvc_error_t uvc_parse_vs(uvc_device_t *dev, uvc_device_info_t *info,
1484                 uvc_streaming_interface_t *stream_if, const unsigned char *block,
1485                 size_t block_size) {
1486         uvc_error_t ret;
1487         int descriptor_subtype;
1488
1489         UVC_ENTER();
1490
1491         ret = UVC_SUCCESS;
1492         descriptor_subtype = block[2];
1493 //      MARK("descriptor_subtype=0x%02x", descriptor_subtype);
1494         switch (descriptor_subtype) {
1495         case UVC_VS_INPUT_HEADER:
1496                 ret = uvc_parse_vs_input_header(stream_if, block, block_size);
1497                 break;
1498 //      case UVC_VS_STILL_IMAGE_FRAME:  // FIXME unsupported now
1499 //              break;
1500         case UVC_VS_FORMAT_UNCOMPRESSED:
1501                 ret = uvc_parse_vs_format_uncompressed(stream_if, block, block_size);
1502                 break;
1503         case UVC_VS_FORMAT_MJPEG:
1504                 ret = uvc_parse_vs_format_mjpeg(stream_if, block, block_size);
1505                 break;
1506         case UVC_VS_FRAME_UNCOMPRESSED:
1507         case UVC_VS_FRAME_MJPEG:
1508                 ret = uvc_parse_vs_frame_uncompressed(stream_if, block, block_size);
1509                 break;
1510         case UVC_VS_FORMAT_FRAME_BASED:
1511                 ret = uvc_parse_vs_frame_format(stream_if, block, block_size );
1512                 break;
1513         case UVC_VS_FRAME_FRAME_BASED:
1514                 ret = uvc_parse_vs_frame_frame(stream_if, block, block_size );
1515                 break;
1516 //      case UVC_VS_COLORFORMAT:        // FIXME unsupported now
1517 //              break;
1518         default:
1519                 /** @todo handle JPEG and maybe still frames or even DV... */
1520                 LOGV("unsupported descriptor_subtype(0x%02x)", descriptor_subtype);
1521                 break;
1522         }
1523
1524         UVC_EXIT(ret);
1525         return ret;
1526 }
1527
1528 /** @internal
1529  * @brief Free memory associated with a UVC device
1530  * @pre Streaming must be stopped, and threads must have died
1531  */
1532 void uvc_free_devh(uvc_device_handle_t *devh) {
1533         UVC_ENTER();
1534
1535         pthread_mutex_destroy(&devh->status_mutex);     // XXX saki
1536         if (devh->info)
1537                 uvc_free_device_info(devh->info);
1538
1539         if (devh->status_xfer)
1540                 libusb_free_transfer(devh->status_xfer);
1541
1542         free(devh);
1543
1544         UVC_EXIT_VOID();
1545 }
1546
1547 /** @brief Close a device
1548  *
1549  * @ingroup device
1550  *
1551  * Ends any stream that's in progress.
1552  *
1553  * The device handle and frame structures will be invalidated.
1554  */
1555 void uvc_close(uvc_device_handle_t *devh) {
1556
1557         UVC_ENTER();
1558
1559         uvc_context_t *ctx = devh->dev->ctx;
1560
1561         if (devh->streams)
1562                 uvc_stop_streaming(devh);
1563
1564         uvc_release_if(devh, devh->info->ctrl_if.bInterfaceNumber);
1565
1566 #if !UVC_DETACH_ATTACH
1567         /* disable automatic attach/detach kernel driver on supported platforms in libusb */
1568         libusb_set_auto_detach_kernel_driver(devh->usb_devh, 0);
1569 #endif
1570         /* If we are managing the libusb context and this is the last open device,
1571          * then we need to cancel the handler thread. When we call libusb_close,
1572          * it'll cause a return from the thread's libusb_handle_events call, after
1573          * which the handler thread will check the flag we set and then exit. */
1574         if (ctx->own_usb_ctx && ctx->open_devices == devh && devh->next == NULL) {
1575                 ctx->kill_handler_thread = 1;
1576                 libusb_close(devh->usb_devh);
1577                 pthread_join(ctx->handler_thread, NULL);
1578         } else {
1579                 libusb_close(devh->usb_devh);
1580         }
1581
1582         DL_DELETE(ctx->open_devices, devh);
1583
1584         uvc_unref_device(devh->dev);
1585
1586         uvc_free_devh(devh);
1587
1588         UVC_EXIT_VOID();
1589 }
1590
1591 uvc_error_t uvc_set_reset_altsetting(uvc_device_handle_t *devh, uint8_t reset_on_release_if) {
1592         if (UNLIKELY(!devh))
1593                 RETURN(UVC_ERROR_INVALID_PARAM, uvc_error_t);
1594         devh->reset_on_release_if = reset_on_release_if;
1595         RETURN(UVC_SUCCESS, uvc_error_t);
1596 }
1597
1598 /** @internal
1599  * @brief Get number of open devices
1600  */
1601 size_t uvc_num_devices(uvc_context_t *ctx) {
1602         size_t count = 0;
1603
1604         uvc_device_handle_t *devh;
1605
1606         UVC_ENTER();
1607
1608         DL_FOREACH(ctx->open_devices, devh)
1609         {
1610                 count++;
1611         }
1612
1613         UVC_EXIT((int) count);
1614         return count;
1615 }
1616
1617 void uvc_process_control_status(uvc_device_handle_t *devh, unsigned char *data, int len) {
1618         enum uvc_status_class status_class;
1619         uint8_t originator = 0, selector = 0, event = 0;
1620         enum uvc_status_attribute attribute = UVC_STATUS_ATTRIBUTE_UNKNOWN;
1621         void *content = NULL;
1622         size_t content_len = 0;
1623         int found_entity = 0;
1624         struct uvc_input_terminal *input_terminal;
1625         struct uvc_processing_unit *processing_unit;
1626
1627         UVC_ENTER();
1628
1629         if (len < 5) {
1630                 UVC_DEBUG("Short read of VideoControl status update (%d bytes)", len);
1631                 UVC_EXIT_VOID();
1632                 return;
1633         }
1634
1635         originator = data[1];
1636         event = data[2];
1637         selector = data[3];
1638
1639         if (originator == 0) {
1640                 UVC_DEBUG("Unhandled update from VC interface");
1641                 UVC_EXIT_VOID();
1642                 return;  /* @todo VideoControl virtual entity interface updates */
1643         }
1644
1645         if (event != 0) {
1646                 UVC_DEBUG("Unhandled VC event %d", (int) event);
1647                 UVC_EXIT_VOID();
1648                 return;
1649         }
1650
1651         /* printf("bSelector: %d\n", selector); */
1652
1653         DL_FOREACH(devh->info->ctrl_if.input_term_descs, input_terminal) {
1654                 if (input_terminal->bTerminalID == originator) {
1655                         status_class = UVC_STATUS_CLASS_CONTROL_CAMERA;
1656                         found_entity = 1;
1657                         break;
1658                 }
1659         }
1660
1661         if (!found_entity) {
1662                 DL_FOREACH(devh->info->ctrl_if.processing_unit_descs, processing_unit) {
1663                         if (processing_unit->bUnitID == originator) {
1664                                 status_class = UVC_STATUS_CLASS_CONTROL_PROCESSING;
1665                                 found_entity = 1;
1666                                 break;
1667                         }
1668                 }
1669         }
1670
1671         if (!found_entity) {
1672                 UVC_DEBUG("Got status update for unknown VideoControl entity %d",
1673                                   (int) originator);
1674                 UVC_EXIT_VOID();
1675                 return;
1676         }
1677
1678         attribute = data[4];
1679         content = data + 5;
1680         content_len = len - 5;
1681
1682         UVC_DEBUG("Event: class=%d, event=%d, selector=%d, attribute=%d, content_len=%zd",
1683                           status_class, event, selector, attribute, content_len);
1684
1685         pthread_mutex_lock(&devh->status_mutex);        // XXX saki
1686         {
1687         if(devh->status_cb) {
1688                 UVC_DEBUG("Running user-supplied status callback");
1689                 devh->status_cb(status_class,
1690                                                 event,
1691                                                 selector,
1692                                                 attribute,
1693                                                 content, content_len,
1694                                                 devh->status_user_ptr);
1695         }
1696         }
1697         pthread_mutex_unlock(&devh->status_mutex);      // XXX saki
1698
1699         UVC_EXIT_VOID();
1700 }
1701
1702 void uvc_process_streaming_status(uvc_device_handle_t *devh, unsigned char *data, int len) {
1703
1704         UVC_ENTER();
1705
1706         if (len < 3) {
1707                 UVC_DEBUG("Invalid streaming status event received.\n");
1708                 UVC_EXIT_VOID();
1709                 return;
1710         }
1711
1712         if (data[2] == 0) {
1713                 if (len < 4) {
1714                         UVC_DEBUG("Short read of status update (%d bytes)", len);
1715                         UVC_EXIT_VOID();
1716                         return;
1717                 }
1718                 UVC_DEBUG("Button (intf %u) %s len %d\n", data[1], data[3] ? "pressed" : "released", len);
1719
1720                 pthread_mutex_lock(&devh->status_mutex);        // XXX saki
1721                 {
1722                         if(devh->button_cb) {
1723                                 UVC_DEBUG("Running user-supplied button callback");
1724                                 devh->button_cb(data[1],
1725                                                                 data[3],
1726                                                                 devh->button_user_ptr);
1727                 }
1728                 }
1729                 pthread_mutex_unlock(&devh->status_mutex);      // XXX saki
1730         } else {
1731                 UVC_DEBUG("Stream %u error event %02x %02x len %d.\n", data[1], data[2], data[3], len);
1732         }
1733
1734         UVC_EXIT_VOID();
1735 }
1736
1737 void uvc_process_status_xfer(uvc_device_handle_t *devh, struct libusb_transfer *transfer) {
1738
1739         UVC_ENTER();
1740
1741         /* printf("Got transfer of aLen = %d\n", transfer->actual_length); */
1742
1743         if (transfer->actual_length > 0) {
1744                 switch (transfer->buffer[0] & 0x0f) {
1745                         case 1: /* VideoControl interface */
1746                                 uvc_process_control_status(devh, transfer->buffer, transfer->actual_length);
1747                                 break;
1748                         case 2:  /* VideoStreaming interface */
1749                                 uvc_process_streaming_status(devh, transfer->buffer, transfer->actual_length);
1750                                 break;
1751                 }
1752         }
1753
1754         UVC_EXIT_VOID();
1755 }
1756
1757 /** @internal
1758  * @brief Process asynchronous status updates from the device.
1759  */
1760 void _uvc_status_callback(struct libusb_transfer *transfer) {
1761         UVC_ENTER();
1762
1763         uvc_device_handle_t *devh = (uvc_device_handle_t *) transfer->user_data;
1764
1765         switch (transfer->status) {
1766         case LIBUSB_TRANSFER_ERROR:
1767         case LIBUSB_TRANSFER_CANCELLED:
1768         case LIBUSB_TRANSFER_NO_DEVICE:
1769                 UVC_DEBUG("not processing/resubmitting, status = %d", transfer->status);
1770                 UVC_EXIT_VOID();
1771                 return;
1772         case LIBUSB_TRANSFER_COMPLETED:
1773                 uvc_process_status_xfer(devh, transfer);
1774                 break;
1775         case LIBUSB_TRANSFER_TIMED_OUT:
1776         case LIBUSB_TRANSFER_STALL:
1777         case LIBUSB_TRANSFER_OVERFLOW:
1778                 UVC_DEBUG("retrying transfer, status = %d", transfer->status);
1779                 break;
1780         }
1781
1782         uvc_error_t ret = libusb_submit_transfer(transfer);
1783         UVC_DEBUG("libusb_submit_transfer() = %d", ret);
1784
1785         UVC_EXIT_VOID();
1786 }
1787
1788 /** @brief Set a callback function to receive status updates
1789  *
1790  * @ingroup device
1791  */
1792 void uvc_set_status_callback(uvc_device_handle_t *devh,
1793                 uvc_status_callback_t cb, void *user_ptr) {
1794         UVC_ENTER();
1795
1796         pthread_mutex_lock(&devh->status_mutex);        // XXX saki
1797         {
1798                 devh->status_cb = cb;
1799                 devh->status_user_ptr = user_ptr;
1800         }
1801         pthread_mutex_unlock(&devh->status_mutex);      // XXX saki
1802
1803         UVC_EXIT_VOID();
1804 }
1805
1806 /** @brief Set a callback function to receive button events
1807  *
1808  * @ingroup device
1809  */
1810 void uvc_set_button_callback(uvc_device_handle_t *devh,
1811                 uvc_button_callback_t cb, void *user_ptr) {
1812         UVC_ENTER();
1813
1814         pthread_mutex_lock(&devh->status_mutex);        // XXX saki
1815         {
1816                 devh->button_cb = cb;
1817                 devh->button_user_ptr = user_ptr;
1818         }
1819         pthread_mutex_unlock(&devh->status_mutex);      // XXX saki
1820
1821         UVC_EXIT_VOID();
1822 }
1823
1824 /**
1825  * @brief Get format descriptions for the open device.
1826  *
1827  * @note Do not modify the returned structure.
1828  *
1829  * @param devh Device handle to an open UVC device
1830  */
1831 const uvc_format_desc_t *uvc_get_format_descs(uvc_device_handle_t *devh) {
1832   return devh->info->stream_ifs->format_descs;
1833 }