2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2008
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
15 * This driver aims to support video input devices compliant with the 'USB
16 * Video Class' specification.
18 * The driver doesn't support the deprecated v4l1 interface. It implements the
19 * mmap capture method only, and doesn't do any image format conversion in
20 * software. If your user-space application doesn't support YUYV or MJPEG, fix
21 * it :-). Please note that the MJPEG data have been stripped from their
22 * Huffman tables (DHT marker), you will need to add it back if your JPEG
23 * codec can't handle MJPEG data.
26 #include <linux/kernel.h>
27 #include <linux/version.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
36 #include <media/v4l2-common.h>
40 #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
41 #define DRIVER_DESC "USB Video Class driver"
42 #ifndef DRIVER_VERSION
43 #define DRIVER_VERSION "v0.1.0"
46 static unsigned int uvc_quirks_param;
47 unsigned int uvc_trace_param;
49 /* ------------------------------------------------------------------------
50 * Control, formats, ...
53 static struct uvc_format_desc uvc_fmts[] = {
55 .name = "YUV 4:2:2 (YUYV)",
56 .guid = UVC_GUID_FORMAT_YUY2,
57 .fcc = V4L2_PIX_FMT_YUYV,
60 .name = "YUV 4:2:0 (NV12)",
61 .guid = UVC_GUID_FORMAT_NV12,
62 .fcc = V4L2_PIX_FMT_NV12,
66 .guid = UVC_GUID_FORMAT_MJPEG,
67 .fcc = V4L2_PIX_FMT_MJPEG,
70 .name = "YVU 4:2:0 (YV12)",
71 .guid = UVC_GUID_FORMAT_YV12,
72 .fcc = V4L2_PIX_FMT_YVU420,
75 .name = "YUV 4:2:0 (I420)",
76 .guid = UVC_GUID_FORMAT_I420,
77 .fcc = V4L2_PIX_FMT_YUV420,
80 .name = "YUV 4:2:2 (UYVY)",
81 .guid = UVC_GUID_FORMAT_UYVY,
82 .fcc = V4L2_PIX_FMT_UYVY,
86 .guid = UVC_GUID_FORMAT_Y800,
87 .fcc = V4L2_PIX_FMT_GREY,
91 .guid = UVC_GUID_FORMAT_BY8,
92 .fcc = V4L2_PIX_FMT_SBGGR8,
96 /* ------------------------------------------------------------------------
100 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
103 struct usb_host_endpoint *ep;
106 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
107 ep = &alts->endpoint[i];
108 if (ep->desc.bEndpointAddress == epaddr)
115 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
117 unsigned int len = ARRAY_SIZE(uvc_fmts);
120 for (i = 0; i < len; ++i) {
121 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
128 static __u32 uvc_colorspace(const __u8 primaries)
130 static const __u8 colorprimaries[] = {
132 V4L2_COLORSPACE_SRGB,
133 V4L2_COLORSPACE_470_SYSTEM_M,
134 V4L2_COLORSPACE_470_SYSTEM_BG,
135 V4L2_COLORSPACE_SMPTE170M,
136 V4L2_COLORSPACE_SMPTE240M,
139 if (primaries < ARRAY_SIZE(colorprimaries))
140 return colorprimaries[primaries];
145 /* Simplify a fraction using a simple continued fraction decomposition. The
146 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
147 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
148 * arbitrary parameters to remove non-significative terms from the simple
149 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
150 * respectively seems to give nice results.
152 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
153 unsigned int n_terms, unsigned int threshold)
159 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
163 /* Convert the fraction to a simple continued fraction. See
164 * http://mathforum.org/dr.math/faq/faq.fractions.html
165 * Stop if the current term is bigger than or equal to the given
171 for (n = 0; n < n_terms && y != 0; ++n) {
173 if (an[n] >= threshold) {
184 /* Expand the simple continued fraction back to an integer fraction. */
188 for (i = n; i > 0; --i) {
199 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
200 * to compute numerator / denominator * 10000000 using 32 bit fixed point
203 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
207 /* Saturate the result if the operation would overflow. */
208 if (denominator == 0 ||
209 numerator/denominator >= ((uint32_t)-1)/10000000)
212 /* Divide both the denominator and the multiplier by two until
213 * numerator * multiplier doesn't overflow. If anyone knows a better
214 * algorithm please let me know.
216 multiplier = 10000000;
217 while (numerator > ((uint32_t)-1)/multiplier) {
222 return denominator ? numerator * multiplier / denominator : 0;
225 /* ------------------------------------------------------------------------
226 * Terminal and unit management
229 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
231 struct uvc_entity *entity;
233 list_for_each_entry(entity, &dev->entities, list) {
234 if (entity->id == id)
241 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
242 int id, struct uvc_entity *entity)
247 entity = list_entry(&dev->entities, struct uvc_entity, list);
249 list_for_each_entry_continue(entity, &dev->entities, list) {
250 switch (UVC_ENTITY_TYPE(entity)) {
252 if (entity->output.bSourceID == id)
256 case VC_PROCESSING_UNIT:
257 if (entity->processing.bSourceID == id)
261 case VC_SELECTOR_UNIT:
262 for (i = 0; i < entity->selector.bNrInPins; ++i)
263 if (entity->selector.baSourceID[i] == id)
267 case VC_EXTENSION_UNIT:
268 for (i = 0; i < entity->extension.bNrInPins; ++i)
269 if (entity->extension.baSourceID[i] == id)
278 /* ------------------------------------------------------------------------
279 * Descriptors handling
282 static int uvc_parse_format(struct uvc_device *dev,
283 struct uvc_streaming *streaming, struct uvc_format *format,
284 __u32 **intervals, unsigned char *buffer, int buflen)
286 struct usb_interface *intf = streaming->intf;
287 struct usb_host_interface *alts = intf->cur_altsetting;
288 struct uvc_format_desc *fmtdesc;
289 struct uvc_frame *frame;
290 const unsigned char *start = buffer;
291 unsigned int interval;
295 format->type = buffer[2];
296 format->index = buffer[3];
299 case VS_FORMAT_UNCOMPRESSED:
300 case VS_FORMAT_FRAME_BASED:
301 n = buffer[2] == VS_FORMAT_UNCOMPRESSED ? 27 : 28;
303 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
304 "interface %d FORMAT error\n",
306 alts->desc.bInterfaceNumber);
310 /* Find the format descriptor from its GUID. */
311 fmtdesc = uvc_format_by_guid(&buffer[5]);
313 if (fmtdesc != NULL) {
314 strncpy(format->name, fmtdesc->name,
315 sizeof format->name);
316 format->fcc = fmtdesc->fcc;
318 uvc_printk(KERN_INFO, "Unknown video format "
319 UVC_GUID_FORMAT "\n",
320 UVC_GUID_ARGS(&buffer[5]));
321 snprintf(format->name, sizeof format->name,
322 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
326 format->bpp = buffer[21];
327 if (buffer[2] == VS_FORMAT_UNCOMPRESSED) {
328 ftype = VS_FRAME_UNCOMPRESSED;
330 ftype = VS_FRAME_FRAME_BASED;
332 format->flags = UVC_FMT_FLAG_COMPRESSED;
336 case VS_FORMAT_MJPEG:
338 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
339 "interface %d FORMAT error\n",
341 alts->desc.bInterfaceNumber);
345 strncpy(format->name, "MJPEG", sizeof format->name);
346 format->fcc = V4L2_PIX_FMT_MJPEG;
347 format->flags = UVC_FMT_FLAG_COMPRESSED;
349 ftype = VS_FRAME_MJPEG;
354 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
355 "interface %d FORMAT error\n",
357 alts->desc.bInterfaceNumber);
361 switch (buffer[8] & 0x7f) {
363 strncpy(format->name, "SD-DV", sizeof format->name);
366 strncpy(format->name, "SDL-DV", sizeof format->name);
369 strncpy(format->name, "HD-DV", sizeof format->name);
372 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
373 "interface %d: unknown DV format %u\n",
375 alts->desc.bInterfaceNumber, buffer[8]);
379 strncat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
380 sizeof format->name);
382 format->fcc = V4L2_PIX_FMT_DV;
383 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
387 /* Create a dummy frame descriptor. */
388 frame = &format->frame[0];
389 memset(&format->frame[0], 0, sizeof format->frame[0]);
390 frame->bFrameIntervalType = 1;
391 frame->dwDefaultFrameInterval = 1;
392 frame->dwFrameInterval = *intervals;
397 case VS_FORMAT_MPEG2TS:
398 case VS_FORMAT_STREAM_BASED:
399 /* Not supported yet. */
401 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
402 "interface %d unsupported format %u\n",
403 dev->udev->devnum, alts->desc.bInterfaceNumber,
408 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
413 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
414 * based formats have frame descriptors.
416 while (buflen > 2 && buffer[2] == ftype) {
417 frame = &format->frame[format->nframes];
419 if (ftype != VS_FRAME_FRAME_BASED)
420 n = buflen > 25 ? buffer[25] : 0;
422 n = buflen > 21 ? buffer[21] : 0;
426 if (buflen < 26 + 4*n) {
427 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
428 "interface %d FRAME error\n", dev->udev->devnum,
429 alts->desc.bInterfaceNumber);
433 frame->bFrameIndex = buffer[3];
434 frame->bmCapabilities = buffer[4];
435 frame->wWidth = le16_to_cpup((__le16 *)&buffer[5]);
436 frame->wHeight = le16_to_cpup((__le16 *)&buffer[7]);
437 frame->dwMinBitRate = le32_to_cpup((__le32 *)&buffer[9]);
438 frame->dwMaxBitRate = le32_to_cpup((__le32 *)&buffer[13]);
439 if (ftype != VS_FRAME_FRAME_BASED) {
440 frame->dwMaxVideoFrameBufferSize =
441 le32_to_cpup((__le32 *)&buffer[17]);
442 frame->dwDefaultFrameInterval =
443 le32_to_cpup((__le32 *)&buffer[21]);
444 frame->bFrameIntervalType = buffer[25];
446 frame->dwMaxVideoFrameBufferSize = 0;
447 frame->dwDefaultFrameInterval =
448 le32_to_cpup((__le32 *)&buffer[17]);
449 frame->bFrameIntervalType = buffer[21];
451 frame->dwFrameInterval = *intervals;
453 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454 * completely. Observed behaviours range from setting the
455 * value to 1.1x the actual frame size of hardwiring the
456 * 16 low bits to 0. This results in a higher than necessary
457 * memory usage as well as a wrong image size information. For
458 * uncompressed formats this can be fixed by computing the
459 * value from the frame size.
461 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
462 frame->dwMaxVideoFrameBufferSize = format->bpp
463 * frame->wWidth * frame->wHeight / 8;
465 /* Some bogus devices report dwMinFrameInterval equal to
466 * dwMaxFrameInterval and have dwFrameIntervalStep set to
467 * zero. Setting all null intervals to 1 fixes the problem and
468 * some other divisions by zero which could happen.
470 for (i = 0; i < n; ++i) {
471 interval = le32_to_cpup((__le32 *)&buffer[26+4*i]);
472 *(*intervals)++ = interval ? interval : 1;
475 /* Make sure that the default frame interval stays between
478 n -= frame->bFrameIntervalType ? 1 : 2;
479 frame->dwDefaultFrameInterval =
480 min(frame->dwFrameInterval[n],
481 max(frame->dwFrameInterval[0],
482 frame->dwDefaultFrameInterval));
484 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
485 frame->wWidth, frame->wHeight,
486 10000000/frame->dwDefaultFrameInterval,
487 (100000000/frame->dwDefaultFrameInterval)%10);
494 if (buflen > 2 && buffer[2] == VS_STILL_IMAGE_FRAME) {
499 if (buflen > 2 && buffer[2] == VS_COLORFORMAT) {
501 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming"
502 "interface %d COLORFORMAT error\n",
504 alts->desc.bInterfaceNumber);
508 format->colorspace = uvc_colorspace(buffer[3]);
514 return buffer - start;
517 static int uvc_parse_streaming(struct uvc_device *dev,
518 struct usb_interface *intf)
520 struct uvc_streaming *streaming = NULL;
521 struct uvc_format *format;
522 struct uvc_frame *frame;
523 struct usb_host_interface *alts = &intf->altsetting[0];
524 unsigned char *_buffer, *buffer = alts->extra;
525 int _buflen, buflen = alts->extralen;
526 unsigned int nformats = 0, nframes = 0, nintervals = 0;
527 unsigned int size, i, n, p;
532 if (intf->cur_altsetting->desc.bInterfaceSubClass
533 != SC_VIDEOSTREAMING) {
534 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
535 "video streaming interface\n", dev->udev->devnum,
536 intf->altsetting[0].desc.bInterfaceNumber);
540 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
541 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
542 "claimed\n", dev->udev->devnum,
543 intf->altsetting[0].desc.bInterfaceNumber);
547 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
548 if (streaming == NULL) {
549 usb_driver_release_interface(&uvc_driver.driver, intf);
553 mutex_init(&streaming->mutex);
554 streaming->intf = usb_get_intf(intf);
555 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
557 /* The Pico iMage webcam has its class-specific interface descriptors
558 * after the endpoint descriptors.
561 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
562 struct usb_host_endpoint *ep = &alts->endpoint[i];
564 if (ep->extralen == 0)
567 if (ep->extralen > 2 &&
568 ep->extra[1] == USB_DT_CS_INTERFACE) {
569 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
570 "from endpoint %u.\n", i);
571 buffer = alts->endpoint[i].extra;
572 buflen = alts->endpoint[i].extralen;
578 /* Skip the standard interface descriptors. */
579 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
585 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
586 "interface descriptors found.\n");
590 /* Parse the header descriptor. */
591 if (buffer[2] == VS_OUTPUT_HEADER) {
592 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
593 "%d OUTPUT HEADER descriptor is not supported.\n",
594 dev->udev->devnum, alts->desc.bInterfaceNumber);
596 } else if (buffer[2] == VS_INPUT_HEADER) {
597 p = buflen >= 5 ? buffer[3] : 0;
598 n = buflen >= 12 ? buffer[12] : 0;
600 if (buflen < 13 + p*n || buffer[2] != VS_INPUT_HEADER) {
601 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
602 "interface %d INPUT HEADER descriptor is "
603 "invalid.\n", dev->udev->devnum,
604 alts->desc.bInterfaceNumber);
608 streaming->header.bNumFormats = p;
609 streaming->header.bEndpointAddress = buffer[6];
610 streaming->header.bmInfo = buffer[7];
611 streaming->header.bTerminalLink = buffer[8];
612 streaming->header.bStillCaptureMethod = buffer[9];
613 streaming->header.bTriggerSupport = buffer[10];
614 streaming->header.bTriggerUsage = buffer[11];
615 streaming->header.bControlSize = n;
617 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
618 if (streaming->header.bmaControls == NULL) {
623 memcpy(streaming->header.bmaControls, &buffer[13], p*n);
625 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
626 "%d HEADER descriptor not found.\n", dev->udev->devnum,
627 alts->desc.bInterfaceNumber);
637 /* Count the format and frame descriptors. */
638 while (_buflen > 2) {
639 switch (_buffer[2]) {
640 case VS_FORMAT_UNCOMPRESSED:
641 case VS_FORMAT_MJPEG:
642 case VS_FORMAT_FRAME_BASED:
647 /* DV format has no frame descriptor. We will create a
648 * dummy frame descriptor with a dummy frame interval.
655 case VS_FORMAT_MPEG2TS:
656 case VS_FORMAT_STREAM_BASED:
657 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
658 "interface %d FORMAT %u is not supported.\n",
660 alts->desc.bInterfaceNumber, _buffer[2]);
663 case VS_FRAME_UNCOMPRESSED:
667 nintervals += _buffer[25] ? _buffer[25] : 3;
670 case VS_FRAME_FRAME_BASED:
673 nintervals += _buffer[21] ? _buffer[21] : 3;
677 _buflen -= _buffer[0];
678 _buffer += _buffer[0];
682 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
683 "%d has no supported formats defined.\n",
684 dev->udev->devnum, alts->desc.bInterfaceNumber);
688 size = nformats * sizeof *format + nframes * sizeof *frame
689 + nintervals * sizeof *interval;
690 format = kzalloc(size, GFP_KERNEL);
691 if (format == NULL) {
696 frame = (struct uvc_frame *)&format[nformats];
697 interval = (__u32 *)&frame[nframes];
699 streaming->format = format;
700 streaming->nformats = nformats;
702 /* Parse the format descriptors. */
705 case VS_FORMAT_UNCOMPRESSED:
706 case VS_FORMAT_MJPEG:
708 case VS_FORMAT_FRAME_BASED:
709 format->frame = frame;
710 ret = uvc_parse_format(dev, streaming, format,
711 &interval, buffer, buflen);
715 frame += format->nframes;
730 /* Parse the alternate settings to find the maximum bandwidth. */
731 for (i = 0; i < intf->num_altsetting; ++i) {
732 struct usb_host_endpoint *ep;
733 alts = &intf->altsetting[i];
734 ep = uvc_find_endpoint(alts,
735 streaming->header.bEndpointAddress);
739 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
740 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
741 if (psize > streaming->maxpsize)
742 streaming->maxpsize = psize;
745 list_add_tail(&streaming->list, &dev->streaming);
749 usb_driver_release_interface(&uvc_driver.driver, intf);
751 kfree(streaming->format);
752 kfree(streaming->header.bmaControls);
757 /* Parse vendor-specific extensions. */
758 static int uvc_parse_vendor_control(struct uvc_device *dev,
759 const unsigned char *buffer, int buflen)
761 struct usb_device *udev = dev->udev;
762 struct usb_host_interface *alts = dev->intf->cur_altsetting;
763 struct uvc_entity *unit;
767 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
768 case 0x046d: /* Logitech */
769 if (buffer[1] != 0x41 || buffer[2] != 0x01)
772 /* Logitech implements several vendor specific functions
773 * through vendor specific extension units (LXU).
775 * The LXU descriptors are similar to XU descriptors
776 * (see "USB Device Video Class for Video Devices", section
777 * 3.7.2.6 "Extension Unit Descriptor") with the following
780 * ----------------------------------------------------------
782 * Size of this descriptor, in bytes: 24+p+n*2
783 * ----------------------------------------------------------
784 * 23+p+n bmControlsType N Bitmap
785 * Individual bits in the set are defined:
789 * This bitset is mapped exactly the same as bmControls.
790 * ----------------------------------------------------------
791 * 23+p+n*2 bReserved 1 Boolean
792 * ----------------------------------------------------------
793 * 24+p+n*2 iExtension 1 Index
794 * Index of a string descriptor that describes this
796 * ----------------------------------------------------------
798 p = buflen >= 22 ? buffer[21] : 0;
799 n = buflen >= 25 + p ? buffer[22+p] : 0;
801 if (buflen < 25 + p + 2*n) {
802 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
803 "interface %d EXTENSION_UNIT error\n",
804 udev->devnum, alts->desc.bInterfaceNumber);
808 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
812 unit->id = buffer[3];
813 unit->type = VC_EXTENSION_UNIT;
814 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
815 unit->extension.bNumControls = buffer[20];
816 unit->extension.bNrInPins =
817 le16_to_cpup((__le16 *)&buffer[21]);
818 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
819 memcpy(unit->extension.baSourceID, &buffer[22], p);
820 unit->extension.bControlSize = buffer[22+p];
821 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
822 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
824 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
826 if (buffer[24+p+2*n] != 0)
827 usb_string(udev, buffer[24+p+2*n], unit->name,
830 sprintf(unit->name, "Extension %u", buffer[3]);
832 list_add_tail(&unit->list, &dev->entities);
840 static int uvc_parse_standard_control(struct uvc_device *dev,
841 const unsigned char *buffer, int buflen)
843 struct usb_device *udev = dev->udev;
844 struct uvc_entity *unit, *term;
845 struct usb_interface *intf;
846 struct usb_host_interface *alts = dev->intf->cur_altsetting;
847 unsigned int i, n, p, len;
852 n = buflen >= 12 ? buffer[11] : 0;
854 if (buflen < 12 || buflen < 12 + n) {
855 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
856 "interface %d HEADER error\n", udev->devnum,
857 alts->desc.bInterfaceNumber);
861 dev->uvc_version = le16_to_cpup((__le16 *)&buffer[3]);
862 dev->clock_frequency = le32_to_cpup((__le32 *)&buffer[7]);
864 /* Parse all USB Video Streaming interfaces. */
865 for (i = 0; i < n; ++i) {
866 intf = usb_ifnum_to_if(udev, buffer[12+i]);
868 uvc_trace(UVC_TRACE_DESCR, "device %d "
869 "interface %d doesn't exists\n",
874 uvc_parse_streaming(dev, intf);
878 case VC_INPUT_TERMINAL:
880 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
881 "interface %d INPUT_TERMINAL error\n",
882 udev->devnum, alts->desc.bInterfaceNumber);
886 /* Make sure the terminal type MSB is not null, otherwise it
887 * could be confused with a unit.
889 type = le16_to_cpup((__le16 *)&buffer[4]);
890 if ((type & 0xff00) == 0) {
891 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
892 "interface %d INPUT_TERMINAL %d has invalid "
893 "type 0x%04x, skipping\n", udev->devnum,
894 alts->desc.bInterfaceNumber,
903 if (type == ITT_CAMERA) {
904 n = buflen >= 15 ? buffer[14] : 0;
907 } else if (type == ITT_MEDIA_TRANSPORT_INPUT) {
908 n = buflen >= 9 ? buffer[8] : 0;
909 p = buflen >= 10 + n ? buffer[9+n] : 0;
913 if (buflen < len + n + p) {
914 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
915 "interface %d INPUT_TERMINAL error\n",
916 udev->devnum, alts->desc.bInterfaceNumber);
920 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
924 term->id = buffer[3];
925 term->type = type | UVC_TERM_INPUT;
927 if (UVC_ENTITY_TYPE(term) == ITT_CAMERA) {
928 term->camera.bControlSize = n;
929 term->camera.bmControls = (__u8 *)term + sizeof *term;
930 term->camera.wObjectiveFocalLengthMin =
931 le16_to_cpup((__le16 *)&buffer[8]);
932 term->camera.wObjectiveFocalLengthMax =
933 le16_to_cpup((__le16 *)&buffer[10]);
934 term->camera.wOcularFocalLength =
935 le16_to_cpup((__le16 *)&buffer[12]);
936 memcpy(term->camera.bmControls, &buffer[15], n);
937 } else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT) {
938 term->media.bControlSize = n;
939 term->media.bmControls = (__u8 *)term + sizeof *term;
940 term->media.bTransportModeSize = p;
941 term->media.bmTransportModes = (__u8 *)term
943 memcpy(term->media.bmControls, &buffer[9], n);
944 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
948 usb_string(udev, buffer[7], term->name,
950 else if (UVC_ENTITY_TYPE(term) == ITT_CAMERA)
951 sprintf(term->name, "Camera %u", buffer[3]);
952 else if (UVC_ENTITY_TYPE(term) == ITT_MEDIA_TRANSPORT_INPUT)
953 sprintf(term->name, "Media %u", buffer[3]);
955 sprintf(term->name, "Input %u", buffer[3]);
957 list_add_tail(&term->list, &dev->entities);
960 case VC_OUTPUT_TERMINAL:
962 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
963 "interface %d OUTPUT_TERMINAL error\n",
964 udev->devnum, alts->desc.bInterfaceNumber);
968 /* Make sure the terminal type MSB is not null, otherwise it
969 * could be confused with a unit.
971 type = le16_to_cpup((__le16 *)&buffer[4]);
972 if ((type & 0xff00) == 0) {
973 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
974 "interface %d OUTPUT_TERMINAL %d has invalid "
975 "type 0x%04x, skipping\n", udev->devnum,
976 alts->desc.bInterfaceNumber, buffer[3], type);
980 term = kzalloc(sizeof *term, GFP_KERNEL);
984 term->id = buffer[3];
985 term->type = type | UVC_TERM_OUTPUT;
986 term->output.bSourceID = buffer[7];
989 usb_string(udev, buffer[8], term->name,
992 sprintf(term->name, "Output %u", buffer[3]);
994 list_add_tail(&term->list, &dev->entities);
997 case VC_SELECTOR_UNIT:
998 p = buflen >= 5 ? buffer[4] : 0;
1000 if (buflen < 5 || buflen < 6 + p) {
1001 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1002 "interface %d SELECTOR_UNIT error\n",
1003 udev->devnum, alts->desc.bInterfaceNumber);
1007 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1011 unit->id = buffer[3];
1012 unit->type = buffer[2];
1013 unit->selector.bNrInPins = buffer[4];
1014 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1015 memcpy(unit->selector.baSourceID, &buffer[5], p);
1017 if (buffer[5+p] != 0)
1018 usb_string(udev, buffer[5+p], unit->name,
1021 sprintf(unit->name, "Selector %u", buffer[3]);
1023 list_add_tail(&unit->list, &dev->entities);
1026 case VC_PROCESSING_UNIT:
1027 n = buflen >= 8 ? buffer[7] : 0;
1028 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1030 if (buflen < p + n) {
1031 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1032 "interface %d PROCESSING_UNIT error\n",
1033 udev->devnum, alts->desc.bInterfaceNumber);
1037 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1041 unit->id = buffer[3];
1042 unit->type = buffer[2];
1043 unit->processing.bSourceID = buffer[4];
1044 unit->processing.wMaxMultiplier =
1045 le16_to_cpup((__le16 *)&buffer[5]);
1046 unit->processing.bControlSize = buffer[7];
1047 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1048 memcpy(unit->processing.bmControls, &buffer[8], n);
1049 if (dev->uvc_version >= 0x0110)
1050 unit->processing.bmVideoStandards = buffer[9+n];
1052 if (buffer[8+n] != 0)
1053 usb_string(udev, buffer[8+n], unit->name,
1056 sprintf(unit->name, "Processing %u", buffer[3]);
1058 list_add_tail(&unit->list, &dev->entities);
1061 case VC_EXTENSION_UNIT:
1062 p = buflen >= 22 ? buffer[21] : 0;
1063 n = buflen >= 24 + p ? buffer[22+p] : 0;
1065 if (buflen < 24 + p + n) {
1066 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1067 "interface %d EXTENSION_UNIT error\n",
1068 udev->devnum, alts->desc.bInterfaceNumber);
1072 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1076 unit->id = buffer[3];
1077 unit->type = buffer[2];
1078 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1079 unit->extension.bNumControls = buffer[20];
1080 unit->extension.bNrInPins =
1081 le16_to_cpup((__le16 *)&buffer[21]);
1082 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1083 memcpy(unit->extension.baSourceID, &buffer[22], p);
1084 unit->extension.bControlSize = buffer[22+p];
1085 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1086 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1088 if (buffer[23+p+n] != 0)
1089 usb_string(udev, buffer[23+p+n], unit->name,
1092 sprintf(unit->name, "Extension %u", buffer[3]);
1094 list_add_tail(&unit->list, &dev->entities);
1098 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1099 "descriptor (%u)\n", buffer[2]);
1106 static int uvc_parse_control(struct uvc_device *dev)
1108 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1109 unsigned char *buffer = alts->extra;
1110 int buflen = alts->extralen;
1113 /* Parse the default alternate setting only, as the UVC specification
1114 * defines a single alternate setting, the default alternate setting
1118 while (buflen > 2) {
1119 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1120 buffer[1] != USB_DT_CS_INTERFACE)
1121 goto next_descriptor;
1123 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1127 buflen -= buffer[0];
1128 buffer += buffer[0];
1131 /* Check if the optional status endpoint is present. */
1132 if (alts->desc.bNumEndpoints == 1) {
1133 struct usb_host_endpoint *ep = &alts->endpoint[0];
1134 struct usb_endpoint_descriptor *desc = &ep->desc;
1136 if (usb_endpoint_is_int_in(desc) &&
1137 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1138 desc->bInterval != 0) {
1139 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1140 "(addr %02x).\n", desc->bEndpointAddress);
1148 /* ------------------------------------------------------------------------
1149 * USB probe and disconnect
1153 * Unregister the video devices.
1155 static void uvc_unregister_video(struct uvc_device *dev)
1157 if (dev->video.vdev) {
1158 if (dev->video.vdev->minor == -1)
1159 video_device_release(dev->video.vdev);
1161 video_unregister_device(dev->video.vdev);
1162 dev->video.vdev = NULL;
1167 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1168 * and containing the following units:
1170 * - a USB Streaming Output Terminal
1171 * - zero or one Processing Unit
1172 * - zero, one or mode single-input Selector Units
1173 * - zero or one multiple-input Selector Units, provided all inputs are
1174 * connected to input terminals
1175 * - zero, one or mode single-input Extension Units
1176 * - one Camera Input Terminal, or one or more External terminals.
1178 * A side forward scan is made on each detected entity to check for additional
1181 static int uvc_scan_chain_entity(struct uvc_video_device *video,
1182 struct uvc_entity *entity)
1184 switch (UVC_ENTITY_TYPE(entity)) {
1185 case VC_EXTENSION_UNIT:
1186 if (uvc_trace_param & UVC_TRACE_PROBE)
1187 printk(" <- XU %d", entity->id);
1189 if (entity->extension.bNrInPins != 1) {
1190 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1191 "than 1 input pin.\n", entity->id);
1195 list_add_tail(&entity->chain, &video->extensions);
1198 case VC_PROCESSING_UNIT:
1199 if (uvc_trace_param & UVC_TRACE_PROBE)
1200 printk(" <- PU %d", entity->id);
1202 if (video->processing != NULL) {
1203 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1204 "Processing Units in chain.\n");
1208 video->processing = entity;
1211 case VC_SELECTOR_UNIT:
1212 if (uvc_trace_param & UVC_TRACE_PROBE)
1213 printk(" <- SU %d", entity->id);
1215 /* Single-input selector units are ignored. */
1216 if (entity->selector.bNrInPins == 1)
1219 if (video->selector != NULL) {
1220 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1221 "Units in chain.\n");
1225 video->selector = entity;
1228 case ITT_VENDOR_SPECIFIC:
1230 case ITT_MEDIA_TRANSPORT_INPUT:
1231 if (uvc_trace_param & UVC_TRACE_PROBE)
1232 printk(" <- IT %d\n", entity->id);
1234 list_add_tail(&entity->chain, &video->iterms);
1238 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1239 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1246 static int uvc_scan_chain_forward(struct uvc_video_device *video,
1247 struct uvc_entity *entity, struct uvc_entity *prev)
1249 struct uvc_entity *forward;
1257 forward = uvc_entity_by_reference(video->dev, entity->id,
1259 if (forward == NULL)
1262 if (UVC_ENTITY_TYPE(forward) != VC_EXTENSION_UNIT ||
1266 if (forward->extension.bNrInPins != 1) {
1267 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has"
1268 "more than 1 input pin.\n", entity->id);
1272 list_add_tail(&forward->chain, &video->extensions);
1273 if (uvc_trace_param & UVC_TRACE_PROBE) {
1277 printk(" %d", forward->id);
1287 static int uvc_scan_chain_backward(struct uvc_video_device *video,
1288 struct uvc_entity *entity)
1290 struct uvc_entity *term;
1293 switch (UVC_ENTITY_TYPE(entity)) {
1294 case VC_EXTENSION_UNIT:
1295 id = entity->extension.baSourceID[0];
1298 case VC_PROCESSING_UNIT:
1299 id = entity->processing.bSourceID;
1302 case VC_SELECTOR_UNIT:
1303 /* Single-input selector units are ignored. */
1304 if (entity->selector.bNrInPins == 1) {
1305 id = entity->selector.baSourceID[0];
1309 if (uvc_trace_param & UVC_TRACE_PROBE)
1312 video->selector = entity;
1313 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1314 id = entity->selector.baSourceID[i];
1315 term = uvc_entity_by_id(video->dev, id);
1316 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1317 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1318 "input %d isn't connected to an "
1319 "input terminal\n", entity->id, i);
1323 if (uvc_trace_param & UVC_TRACE_PROBE)
1324 printk(" %d", term->id);
1326 list_add_tail(&term->chain, &video->iterms);
1327 uvc_scan_chain_forward(video, term, entity);
1330 if (uvc_trace_param & UVC_TRACE_PROBE)
1340 static int uvc_scan_chain(struct uvc_video_device *video)
1342 struct uvc_entity *entity, *prev;
1345 entity = video->oterm;
1346 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1347 id = entity->output.bSourceID;
1350 entity = uvc_entity_by_id(video->dev, id);
1351 if (entity == NULL) {
1352 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1353 "unknown entity %d.\n", id);
1357 /* Process entity */
1358 if (uvc_scan_chain_entity(video, entity) < 0)
1362 if (uvc_scan_chain_forward(video, entity, prev) < 0)
1365 /* Stop when a terminal is found. */
1366 if (!UVC_ENTITY_IS_UNIT(entity))
1370 id = uvc_scan_chain_backward(video, entity);
1375 /* Initialize the video buffers queue. */
1376 uvc_queue_init(&video->queue);
1382 * Register the video devices.
1384 * The driver currently supports a single video device per control interface
1385 * only. The terminal and units must match the following structure:
1387 * ITT_CAMERA -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1389 * The Extension Units, if present, must have a single input pin. The
1390 * Processing Unit and Extension Units can be in any order. Additional
1391 * Extension Units connected to the main chain as single-unit branches are
1394 static int uvc_register_video(struct uvc_device *dev)
1396 struct video_device *vdev;
1397 struct uvc_entity *term;
1400 /* Check if the control interface matches the structure we expect. */
1401 list_for_each_entry(term, &dev->entities, list) {
1402 struct uvc_streaming *streaming;
1404 if (UVC_ENTITY_TYPE(term) != TT_STREAMING)
1407 memset(&dev->video, 0, sizeof dev->video);
1408 mutex_init(&dev->video.ctrl_mutex);
1409 INIT_LIST_HEAD(&dev->video.iterms);
1410 INIT_LIST_HEAD(&dev->video.extensions);
1411 dev->video.oterm = term;
1412 dev->video.dev = dev;
1413 if (uvc_scan_chain(&dev->video) < 0)
1416 list_for_each_entry(streaming, &dev->streaming, list) {
1417 if (streaming->header.bTerminalLink == term->id) {
1418 dev->video.streaming = streaming;
1429 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1433 if (uvc_trace_param & UVC_TRACE_PROBE) {
1434 uvc_printk(KERN_INFO, "Found a valid video chain (");
1435 list_for_each_entry(term, &dev->video.iterms, chain) {
1436 printk("%d", term->id);
1437 if (term->chain.next != &dev->video.iterms)
1440 printk(" -> %d).\n", dev->video.oterm->id);
1443 /* Initialize the streaming interface with default streaming
1446 if ((ret = uvc_video_init(&dev->video)) < 0) {
1447 uvc_printk(KERN_ERR, "Failed to initialize the device "
1452 /* Register the device with V4L. */
1453 vdev = video_device_alloc();
1457 /* We already hold a reference to dev->udev. The video device will be
1458 * unregistered before the reference is released, so we don't need to
1461 vdev->parent = &dev->intf->dev;
1463 vdev->fops = &uvc_fops;
1464 vdev->release = video_device_release;
1465 strncpy(vdev->name, dev->name, sizeof vdev->name);
1467 /* Set the driver data before calling video_register_device, otherwise
1468 * uvc_v4l2_open might race us.
1470 * FIXME: usb_set_intfdata hasn't been called so far. Is that a
1471 * problem ? Does any function which could be called here get
1472 * a pointer to the usb_interface ?
1474 dev->video.vdev = vdev;
1475 video_set_drvdata(vdev, &dev->video);
1477 if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) {
1478 dev->video.vdev = NULL;
1479 video_device_release(vdev);
1487 * Delete the UVC device.
1489 * Called by the kernel when the last reference to the uvc_device structure
1492 * Unregistering the video devices is done here because every opened instance
1493 * must be closed before the device can be unregistered. An alternative would
1494 * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1495 * unregister the video devices on disconnect when that reference count drops
1498 * As this function is called after or during disconnect(), all URBs have
1499 * already been canceled by the USB core. There is no need to kill the
1500 * interrupt URB manually.
1502 void uvc_delete(struct kref *kref)
1504 struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1505 struct list_head *p, *n;
1507 /* Unregister the video device */
1508 uvc_unregister_video(dev);
1509 usb_put_intf(dev->intf);
1510 usb_put_dev(dev->udev);
1512 uvc_status_cleanup(dev);
1513 uvc_ctrl_cleanup_device(dev);
1515 list_for_each_safe(p, n, &dev->entities) {
1516 struct uvc_entity *entity;
1517 entity = list_entry(p, struct uvc_entity, list);
1521 list_for_each_safe(p, n, &dev->streaming) {
1522 struct uvc_streaming *streaming;
1523 streaming = list_entry(p, struct uvc_streaming, list);
1524 usb_driver_release_interface(&uvc_driver.driver,
1526 usb_put_intf(streaming->intf);
1527 kfree(streaming->format);
1528 kfree(streaming->header.bmaControls);
1535 static int uvc_probe(struct usb_interface *intf,
1536 const struct usb_device_id *id)
1538 struct usb_device *udev = interface_to_usbdev(intf);
1539 struct uvc_device *dev;
1542 if (id->idVendor && id->idProduct)
1543 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1544 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1547 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1550 /* Allocate memory for the device and initialize it */
1551 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1554 INIT_LIST_HEAD(&dev->entities);
1555 INIT_LIST_HEAD(&dev->streaming);
1556 kref_init(&dev->kref);
1558 dev->udev = usb_get_dev(udev);
1559 dev->intf = usb_get_intf(intf);
1560 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1561 dev->quirks = id->driver_info | uvc_quirks_param;
1563 if (udev->product != NULL)
1564 strncpy(dev->name, udev->product, sizeof dev->name);
1566 snprintf(dev->name, sizeof dev->name,
1567 "UVC Camera (%04x:%04x)",
1568 le16_to_cpu(udev->descriptor.idVendor),
1569 le16_to_cpu(udev->descriptor.idProduct));
1571 /* Parse the Video Class control descriptor */
1572 if (uvc_parse_control(dev) < 0) {
1573 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1578 uvc_printk(KERN_INFO, "Found UVC %u.%02u device %s (%04x:%04x)\n",
1579 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1580 udev->product ? udev->product : "<unnamed>",
1581 le16_to_cpu(udev->descriptor.idVendor),
1582 le16_to_cpu(udev->descriptor.idProduct));
1584 if (uvc_quirks_param != 0) {
1585 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1586 "parameter for testing purpose.\n", uvc_quirks_param);
1587 uvc_printk(KERN_INFO, "Please report required quirks to the "
1588 "linux-uvc-devel mailing list.\n");
1591 /* Initialize controls */
1592 if (uvc_ctrl_init_device(dev) < 0)
1595 /* Register the video devices */
1596 if (uvc_register_video(dev) < 0)
1599 /* Save our data pointer in the interface data */
1600 usb_set_intfdata(intf, dev);
1602 /* Initialize the interrupt URB */
1603 if ((ret = uvc_status_init(dev)) < 0) {
1604 uvc_printk(KERN_INFO, "Unable to initialize the status "
1605 "endpoint (%d), status interrupt will not be "
1606 "supported.\n", ret);
1609 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1613 kref_put(&dev->kref, uvc_delete);
1617 static void uvc_disconnect(struct usb_interface *intf)
1619 struct uvc_device *dev = usb_get_intfdata(intf);
1621 /* Set the USB interface data to NULL. This can be done outside the
1622 * lock, as there's no other reader.
1624 usb_set_intfdata(intf, NULL);
1626 if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOSTREAMING)
1629 /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1630 * lock is needed to prevent uvc_disconnect from releasing its
1631 * reference to the uvc_device instance after uvc_v4l2_open() received
1632 * the pointer to the device (video_devdata) but before it got the
1633 * chance to increase the reference count (kref_get).
1635 * Note that the reference can't be released with the lock held,
1636 * otherwise a AB-BA deadlock can occur with videodev_lock that
1637 * videodev acquires in videodev_open() and video_unregister_device().
1639 mutex_lock(&uvc_driver.open_mutex);
1640 dev->state |= UVC_DEV_DISCONNECTED;
1641 mutex_unlock(&uvc_driver.open_mutex);
1643 kref_put(&dev->kref, uvc_delete);
1646 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1648 struct uvc_device *dev = usb_get_intfdata(intf);
1650 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1651 intf->cur_altsetting->desc.bInterfaceNumber);
1653 /* Controls are cached on the fly so they don't need to be saved. */
1654 if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL)
1655 return uvc_status_suspend(dev);
1657 if (dev->video.streaming->intf != intf) {
1658 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB "
1659 "interface mismatch.\n");
1663 return uvc_video_suspend(&dev->video);
1666 static int uvc_resume(struct usb_interface *intf)
1668 struct uvc_device *dev = usb_get_intfdata(intf);
1671 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1672 intf->cur_altsetting->desc.bInterfaceNumber);
1674 if (intf->cur_altsetting->desc.bInterfaceSubClass == SC_VIDEOCONTROL) {
1675 if ((ret = uvc_ctrl_resume_device(dev)) < 0)
1678 return uvc_status_resume(dev);
1681 if (dev->video.streaming->intf != intf) {
1682 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB "
1683 "interface mismatch.\n");
1687 return uvc_video_resume(&dev->video);
1690 /* ------------------------------------------------------------------------
1691 * Driver initialization and cleanup
1695 * The Logitech cameras listed below have their interface class set to
1696 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1697 * though they are compliant.
1699 static struct usb_device_id uvc_ids[] = {
1700 /* ALi M5606 (Clevo M540SR) */
1701 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1702 | USB_DEVICE_ID_MATCH_INT_INFO,
1704 .idProduct = 0x5606,
1705 .bInterfaceClass = USB_CLASS_VIDEO,
1706 .bInterfaceSubClass = 1,
1707 .bInterfaceProtocol = 0,
1708 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1709 /* Creative Live! Optia */
1710 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1711 | USB_DEVICE_ID_MATCH_INT_INFO,
1713 .idProduct = 0x4057,
1714 .bInterfaceClass = USB_CLASS_VIDEO,
1715 .bInterfaceSubClass = 1,
1716 .bInterfaceProtocol = 0,
1717 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1718 /* Microsoft Lifecam NX-6000 */
1719 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1720 | USB_DEVICE_ID_MATCH_INT_INFO,
1722 .idProduct = 0x00f8,
1723 .bInterfaceClass = USB_CLASS_VIDEO,
1724 .bInterfaceSubClass = 1,
1725 .bInterfaceProtocol = 0,
1726 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1727 /* Microsoft Lifecam VX-7000 */
1728 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1729 | USB_DEVICE_ID_MATCH_INT_INFO,
1731 .idProduct = 0x0723,
1732 .bInterfaceClass = USB_CLASS_VIDEO,
1733 .bInterfaceSubClass = 1,
1734 .bInterfaceProtocol = 0,
1735 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1736 /* Logitech Quickcam Fusion */
1737 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1738 | USB_DEVICE_ID_MATCH_INT_INFO,
1740 .idProduct = 0x08c1,
1741 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1742 .bInterfaceSubClass = 1,
1743 .bInterfaceProtocol = 0 },
1744 /* Logitech Quickcam Orbit MP */
1745 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1746 | USB_DEVICE_ID_MATCH_INT_INFO,
1748 .idProduct = 0x08c2,
1749 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1750 .bInterfaceSubClass = 1,
1751 .bInterfaceProtocol = 0 },
1752 /* Logitech Quickcam Pro for Notebook */
1753 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1754 | USB_DEVICE_ID_MATCH_INT_INFO,
1756 .idProduct = 0x08c3,
1757 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1758 .bInterfaceSubClass = 1,
1759 .bInterfaceProtocol = 0 },
1760 /* Logitech Quickcam Pro 5000 */
1761 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1762 | USB_DEVICE_ID_MATCH_INT_INFO,
1764 .idProduct = 0x08c5,
1765 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1766 .bInterfaceSubClass = 1,
1767 .bInterfaceProtocol = 0 },
1768 /* Logitech Quickcam OEM Dell Notebook */
1769 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1770 | USB_DEVICE_ID_MATCH_INT_INFO,
1772 .idProduct = 0x08c6,
1773 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1774 .bInterfaceSubClass = 1,
1775 .bInterfaceProtocol = 0 },
1776 /* Logitech Quickcam OEM Cisco VT Camera II */
1777 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1778 | USB_DEVICE_ID_MATCH_INT_INFO,
1780 .idProduct = 0x08c7,
1781 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1782 .bInterfaceSubClass = 1,
1783 .bInterfaceProtocol = 0 },
1784 /* Apple Built-In iSight */
1785 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1786 | USB_DEVICE_ID_MATCH_INT_INFO,
1788 .idProduct = 0x8501,
1789 .bInterfaceClass = USB_CLASS_VIDEO,
1790 .bInterfaceSubClass = 1,
1791 .bInterfaceProtocol = 0,
1792 .driver_info = UVC_QUIRK_PROBE_MINMAX
1793 | UVC_QUIRK_BUILTIN_ISIGHT },
1794 /* Genesys Logic USB 2.0 PC Camera */
1795 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1796 | USB_DEVICE_ID_MATCH_INT_INFO,
1798 .idProduct = 0x0505,
1799 .bInterfaceClass = USB_CLASS_VIDEO,
1800 .bInterfaceSubClass = 1,
1801 .bInterfaceProtocol = 0,
1802 .driver_info = UVC_QUIRK_STREAM_NO_FID },
1803 /* Silicon Motion SM371 */
1804 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1805 | USB_DEVICE_ID_MATCH_INT_INFO,
1807 .idProduct = 0xb371,
1808 .bInterfaceClass = USB_CLASS_VIDEO,
1809 .bInterfaceSubClass = 1,
1810 .bInterfaceProtocol = 0,
1811 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1813 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1814 | USB_DEVICE_ID_MATCH_INT_INFO,
1816 .idProduct = 0x0004,
1817 .bInterfaceClass = USB_CLASS_VIDEO,
1818 .bInterfaceSubClass = 1,
1819 .bInterfaceProtocol = 0,
1820 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1821 /* Syntek (HP Spartan) */
1822 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1823 | USB_DEVICE_ID_MATCH_INT_INFO,
1825 .idProduct = 0x5212,
1826 .bInterfaceClass = USB_CLASS_VIDEO,
1827 .bInterfaceSubClass = 1,
1828 .bInterfaceProtocol = 0,
1829 .driver_info = UVC_QUIRK_STREAM_NO_FID },
1831 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1832 | USB_DEVICE_ID_MATCH_INT_INFO,
1834 .idProduct = 0x8a31,
1835 .bInterfaceClass = USB_CLASS_VIDEO,
1836 .bInterfaceSubClass = 1,
1837 .bInterfaceProtocol = 0,
1838 .driver_info = UVC_QUIRK_STREAM_NO_FID },
1839 /* Syntek (Asus U3S) */
1840 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1841 | USB_DEVICE_ID_MATCH_INT_INFO,
1843 .idProduct = 0x8a33,
1844 .bInterfaceClass = USB_CLASS_VIDEO,
1845 .bInterfaceSubClass = 1,
1846 .bInterfaceProtocol = 0,
1847 .driver_info = UVC_QUIRK_STREAM_NO_FID },
1848 /* Ecamm Pico iMage */
1849 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1850 | USB_DEVICE_ID_MATCH_INT_INFO,
1852 .idProduct = 0xcafe,
1853 .bInterfaceClass = USB_CLASS_VIDEO,
1854 .bInterfaceSubClass = 1,
1855 .bInterfaceProtocol = 0,
1856 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
1857 /* Bodelin ProScopeHR */
1858 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1859 | USB_DEVICE_ID_MATCH_DEV_HI
1860 | USB_DEVICE_ID_MATCH_INT_INFO,
1862 .idProduct = 0x1000,
1863 .bcdDevice_hi = 0x0126,
1864 .bInterfaceClass = USB_CLASS_VIDEO,
1865 .bInterfaceSubClass = 1,
1866 .bInterfaceProtocol = 0,
1867 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
1868 /* SiGma Micro USB Web Camera */
1869 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1870 | USB_DEVICE_ID_MATCH_INT_INFO,
1872 .idProduct = 0x3000,
1873 .bInterfaceClass = USB_CLASS_VIDEO,
1874 .bInterfaceSubClass = 1,
1875 .bInterfaceProtocol = 0,
1876 .driver_info = UVC_QUIRK_PROBE_MINMAX
1877 | UVC_QUIRK_IGNORE_SELECTOR_UNIT},
1878 /* Acer OEM Webcam - Unknown vendor */
1879 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1880 | USB_DEVICE_ID_MATCH_INT_INFO,
1882 .idProduct = 0x0100,
1883 .bInterfaceClass = USB_CLASS_VIDEO,
1884 .bInterfaceSubClass = 1,
1885 .bInterfaceProtocol = 0,
1886 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1887 /* Packard Bell OEM Webcam - Bison Electronics */
1888 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1889 | USB_DEVICE_ID_MATCH_INT_INFO,
1891 .idProduct = 0x0101,
1892 .bInterfaceClass = USB_CLASS_VIDEO,
1893 .bInterfaceSubClass = 1,
1894 .bInterfaceProtocol = 0,
1895 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1896 /* Acer Crystal Eye webcam - Bison Electronics */
1897 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1898 | USB_DEVICE_ID_MATCH_INT_INFO,
1900 .idProduct = 0x0102,
1901 .bInterfaceClass = USB_CLASS_VIDEO,
1902 .bInterfaceSubClass = 1,
1903 .bInterfaceProtocol = 0,
1904 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1905 /* Medion Akoya Mini E1210 - Bison Electronics */
1906 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1907 | USB_DEVICE_ID_MATCH_INT_INFO,
1909 .idProduct = 0x0141,
1910 .bInterfaceClass = USB_CLASS_VIDEO,
1911 .bInterfaceSubClass = 1,
1912 .bInterfaceProtocol = 0,
1913 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1914 /* Acer OrbiCam - Bison Electronics */
1915 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1916 | USB_DEVICE_ID_MATCH_INT_INFO,
1918 .idProduct = 0x0200,
1919 .bInterfaceClass = USB_CLASS_VIDEO,
1920 .bInterfaceSubClass = 1,
1921 .bInterfaceProtocol = 0,
1922 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1923 /* Bison Electronics */
1924 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1925 | USB_DEVICE_ID_MATCH_INT_INFO,
1927 .idProduct = 0x0300,
1928 .bInterfaceClass = USB_CLASS_VIDEO,
1929 .bInterfaceSubClass = 1,
1930 .bInterfaceProtocol = 0,
1931 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1932 /* Clevo M570TU - Bison Electronics */
1933 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1934 | USB_DEVICE_ID_MATCH_INT_INFO,
1936 .idProduct = 0x0303,
1937 .bInterfaceClass = USB_CLASS_VIDEO,
1938 .bInterfaceSubClass = 1,
1939 .bInterfaceProtocol = 0,
1940 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1941 /* Generic USB Video Class */
1942 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
1946 MODULE_DEVICE_TABLE(usb, uvc_ids);
1948 struct uvc_driver uvc_driver = {
1952 .disconnect = uvc_disconnect,
1953 .suspend = uvc_suspend,
1954 .resume = uvc_resume,
1955 .id_table = uvc_ids,
1956 .supports_autosuspend = 1,
1960 static int __init uvc_init(void)
1964 INIT_LIST_HEAD(&uvc_driver.devices);
1965 INIT_LIST_HEAD(&uvc_driver.controls);
1966 mutex_init(&uvc_driver.open_mutex);
1967 mutex_init(&uvc_driver.ctrl_mutex);
1971 result = usb_register(&uvc_driver.driver);
1973 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
1977 static void __exit uvc_cleanup(void)
1979 usb_deregister(&uvc_driver.driver);
1982 module_init(uvc_init);
1983 module_exit(uvc_cleanup);
1985 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1986 MODULE_PARM_DESC(quirks, "Forced device quirks");
1987 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1988 MODULE_PARM_DESC(trace, "Trace level bitmask");
1990 MODULE_AUTHOR(DRIVER_AUTHOR);
1991 MODULE_DESCRIPTION(DRIVER_DESC);
1992 MODULE_LICENSE("GPL");
1993 MODULE_VERSION(DRIVER_VERSION);