2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2009
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 and ouput devices compliant with the
16 * 'USB 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/list.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/videodev2.h>
31 #include <linux/vmalloc.h>
32 #include <linux/wait.h>
33 #include <asm/atomic.h>
34 #include <asm/unaligned.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 unsigned int uvc_no_drop_param;
47 static unsigned int uvc_quirks_param;
48 unsigned int uvc_trace_param;
49 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
51 /* ------------------------------------------------------------------------
55 static struct uvc_format_desc uvc_fmts[] = {
57 .name = "YUV 4:2:2 (YUYV)",
58 .guid = UVC_GUID_FORMAT_YUY2,
59 .fcc = V4L2_PIX_FMT_YUYV,
62 .name = "YUV 4:2:0 (NV12)",
63 .guid = UVC_GUID_FORMAT_NV12,
64 .fcc = V4L2_PIX_FMT_NV12,
68 .guid = UVC_GUID_FORMAT_MJPEG,
69 .fcc = V4L2_PIX_FMT_MJPEG,
72 .name = "YVU 4:2:0 (YV12)",
73 .guid = UVC_GUID_FORMAT_YV12,
74 .fcc = V4L2_PIX_FMT_YVU420,
77 .name = "YUV 4:2:0 (I420)",
78 .guid = UVC_GUID_FORMAT_I420,
79 .fcc = V4L2_PIX_FMT_YUV420,
82 .name = "YUV 4:2:2 (UYVY)",
83 .guid = UVC_GUID_FORMAT_UYVY,
84 .fcc = V4L2_PIX_FMT_UYVY,
88 .guid = UVC_GUID_FORMAT_Y800,
89 .fcc = V4L2_PIX_FMT_GREY,
93 .guid = UVC_GUID_FORMAT_BY8,
94 .fcc = V4L2_PIX_FMT_SBGGR8,
98 /* ------------------------------------------------------------------------
102 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
105 struct usb_host_endpoint *ep;
108 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
109 ep = &alts->endpoint[i];
110 if (ep->desc.bEndpointAddress == epaddr)
117 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
119 unsigned int len = ARRAY_SIZE(uvc_fmts);
122 for (i = 0; i < len; ++i) {
123 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
130 static __u32 uvc_colorspace(const __u8 primaries)
132 static const __u8 colorprimaries[] = {
134 V4L2_COLORSPACE_SRGB,
135 V4L2_COLORSPACE_470_SYSTEM_M,
136 V4L2_COLORSPACE_470_SYSTEM_BG,
137 V4L2_COLORSPACE_SMPTE170M,
138 V4L2_COLORSPACE_SMPTE240M,
141 if (primaries < ARRAY_SIZE(colorprimaries))
142 return colorprimaries[primaries];
147 /* Simplify a fraction using a simple continued fraction decomposition. The
148 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
149 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
150 * arbitrary parameters to remove non-significative terms from the simple
151 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
152 * respectively seems to give nice results.
154 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
155 unsigned int n_terms, unsigned int threshold)
161 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
165 /* Convert the fraction to a simple continued fraction. See
166 * http://mathforum.org/dr.math/faq/faq.fractions.html
167 * Stop if the current term is bigger than or equal to the given
173 for (n = 0; n < n_terms && y != 0; ++n) {
175 if (an[n] >= threshold) {
186 /* Expand the simple continued fraction back to an integer fraction. */
190 for (i = n; i > 0; --i) {
201 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
202 * to compute numerator / denominator * 10000000 using 32 bit fixed point
205 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
209 /* Saturate the result if the operation would overflow. */
210 if (denominator == 0 ||
211 numerator/denominator >= ((uint32_t)-1)/10000000)
214 /* Divide both the denominator and the multiplier by two until
215 * numerator * multiplier doesn't overflow. If anyone knows a better
216 * algorithm please let me know.
218 multiplier = 10000000;
219 while (numerator > ((uint32_t)-1)/multiplier) {
224 return denominator ? numerator * multiplier / denominator : 0;
227 /* ------------------------------------------------------------------------
228 * Terminal and unit management
231 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
233 struct uvc_entity *entity;
235 list_for_each_entry(entity, &dev->entities, list) {
236 if (entity->id == id)
243 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
244 int id, struct uvc_entity *entity)
249 entity = list_entry(&dev->entities, struct uvc_entity, list);
251 list_for_each_entry_continue(entity, &dev->entities, list) {
252 switch (UVC_ENTITY_TYPE(entity)) {
253 case UVC_TT_STREAMING:
254 if (entity->output.bSourceID == id)
258 case UVC_VC_PROCESSING_UNIT:
259 if (entity->processing.bSourceID == id)
263 case UVC_VC_SELECTOR_UNIT:
264 for (i = 0; i < entity->selector.bNrInPins; ++i)
265 if (entity->selector.baSourceID[i] == id)
269 case UVC_VC_EXTENSION_UNIT:
270 for (i = 0; i < entity->extension.bNrInPins; ++i)
271 if (entity->extension.baSourceID[i] == id)
280 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
282 struct uvc_streaming *stream;
284 list_for_each_entry(stream, &dev->streams, list) {
285 if (stream->header.bTerminalLink == id)
292 /* ------------------------------------------------------------------------
293 * Descriptors parsing
296 static int uvc_parse_format(struct uvc_device *dev,
297 struct uvc_streaming *streaming, struct uvc_format *format,
298 __u32 **intervals, unsigned char *buffer, int buflen)
300 struct usb_interface *intf = streaming->intf;
301 struct usb_host_interface *alts = intf->cur_altsetting;
302 struct uvc_format_desc *fmtdesc;
303 struct uvc_frame *frame;
304 const unsigned char *start = buffer;
305 unsigned int interval;
309 format->type = buffer[2];
310 format->index = buffer[3];
313 case UVC_VS_FORMAT_UNCOMPRESSED:
314 case UVC_VS_FORMAT_FRAME_BASED:
315 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
317 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
318 "interface %d FORMAT error\n",
320 alts->desc.bInterfaceNumber);
324 /* Find the format descriptor from its GUID. */
325 fmtdesc = uvc_format_by_guid(&buffer[5]);
327 if (fmtdesc != NULL) {
328 strlcpy(format->name, fmtdesc->name,
329 sizeof format->name);
330 format->fcc = fmtdesc->fcc;
332 uvc_printk(KERN_INFO, "Unknown video format "
333 UVC_GUID_FORMAT "\n",
334 UVC_GUID_ARGS(&buffer[5]));
335 snprintf(format->name, sizeof format->name,
336 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
340 format->bpp = buffer[21];
341 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
342 ftype = UVC_VS_FRAME_UNCOMPRESSED;
344 ftype = UVC_VS_FRAME_FRAME_BASED;
346 format->flags = UVC_FMT_FLAG_COMPRESSED;
350 case UVC_VS_FORMAT_MJPEG:
352 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
353 "interface %d FORMAT error\n",
355 alts->desc.bInterfaceNumber);
359 strlcpy(format->name, "MJPEG", sizeof format->name);
360 format->fcc = V4L2_PIX_FMT_MJPEG;
361 format->flags = UVC_FMT_FLAG_COMPRESSED;
363 ftype = UVC_VS_FRAME_MJPEG;
366 case UVC_VS_FORMAT_DV:
368 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
369 "interface %d FORMAT error\n",
371 alts->desc.bInterfaceNumber);
375 switch (buffer[8] & 0x7f) {
377 strlcpy(format->name, "SD-DV", sizeof format->name);
380 strlcpy(format->name, "SDL-DV", sizeof format->name);
383 strlcpy(format->name, "HD-DV", sizeof format->name);
386 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
387 "interface %d: unknown DV format %u\n",
389 alts->desc.bInterfaceNumber, buffer[8]);
393 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
394 sizeof format->name);
396 format->fcc = V4L2_PIX_FMT_DV;
397 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
401 /* Create a dummy frame descriptor. */
402 frame = &format->frame[0];
403 memset(&format->frame[0], 0, sizeof format->frame[0]);
404 frame->bFrameIntervalType = 1;
405 frame->dwDefaultFrameInterval = 1;
406 frame->dwFrameInterval = *intervals;
411 case UVC_VS_FORMAT_MPEG2TS:
412 case UVC_VS_FORMAT_STREAM_BASED:
413 /* Not supported yet. */
415 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
416 "interface %d unsupported format %u\n",
417 dev->udev->devnum, alts->desc.bInterfaceNumber,
422 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
427 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
428 * based formats have frame descriptors.
430 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
431 buffer[2] == ftype) {
432 frame = &format->frame[format->nframes];
433 if (ftype != UVC_VS_FRAME_FRAME_BASED)
434 n = buflen > 25 ? buffer[25] : 0;
436 n = buflen > 21 ? buffer[21] : 0;
440 if (buflen < 26 + 4*n) {
441 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
442 "interface %d FRAME error\n", dev->udev->devnum,
443 alts->desc.bInterfaceNumber);
447 frame->bFrameIndex = buffer[3];
448 frame->bmCapabilities = buffer[4];
449 frame->wWidth = get_unaligned_le16(&buffer[5]);
450 frame->wHeight = get_unaligned_le16(&buffer[7]);
451 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
452 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
453 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
454 frame->dwMaxVideoFrameBufferSize =
455 get_unaligned_le32(&buffer[17]);
456 frame->dwDefaultFrameInterval =
457 get_unaligned_le32(&buffer[21]);
458 frame->bFrameIntervalType = buffer[25];
460 frame->dwMaxVideoFrameBufferSize = 0;
461 frame->dwDefaultFrameInterval =
462 get_unaligned_le32(&buffer[17]);
463 frame->bFrameIntervalType = buffer[21];
465 frame->dwFrameInterval = *intervals;
467 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
468 * completely. Observed behaviours range from setting the
469 * value to 1.1x the actual frame size to hardwiring the
470 * 16 low bits to 0. This results in a higher than necessary
471 * memory usage as well as a wrong image size information. For
472 * uncompressed formats this can be fixed by computing the
473 * value from the frame size.
475 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
476 frame->dwMaxVideoFrameBufferSize = format->bpp
477 * frame->wWidth * frame->wHeight / 8;
479 /* Some bogus devices report dwMinFrameInterval equal to
480 * dwMaxFrameInterval and have dwFrameIntervalStep set to
481 * zero. Setting all null intervals to 1 fixes the problem and
482 * some other divisions by zero that could happen.
484 for (i = 0; i < n; ++i) {
485 interval = get_unaligned_le32(&buffer[26+4*i]);
486 *(*intervals)++ = interval ? interval : 1;
489 /* Make sure that the default frame interval stays between
492 n -= frame->bFrameIntervalType ? 1 : 2;
493 frame->dwDefaultFrameInterval =
494 min(frame->dwFrameInterval[n],
495 max(frame->dwFrameInterval[0],
496 frame->dwDefaultFrameInterval));
498 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
499 frame->wWidth, frame->wHeight,
500 10000000/frame->dwDefaultFrameInterval,
501 (100000000/frame->dwDefaultFrameInterval)%10);
508 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
509 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
514 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
515 buffer[2] == UVC_VS_COLORFORMAT) {
517 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
518 "interface %d COLORFORMAT error\n",
520 alts->desc.bInterfaceNumber);
524 format->colorspace = uvc_colorspace(buffer[3]);
530 return buffer - start;
533 static int uvc_parse_streaming(struct uvc_device *dev,
534 struct usb_interface *intf)
536 struct uvc_streaming *streaming = NULL;
537 struct uvc_format *format;
538 struct uvc_frame *frame;
539 struct usb_host_interface *alts = &intf->altsetting[0];
540 unsigned char *_buffer, *buffer = alts->extra;
541 int _buflen, buflen = alts->extralen;
542 unsigned int nformats = 0, nframes = 0, nintervals = 0;
543 unsigned int size, i, n, p;
548 if (intf->cur_altsetting->desc.bInterfaceSubClass
549 != UVC_SC_VIDEOSTREAMING) {
550 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
551 "video streaming interface\n", dev->udev->devnum,
552 intf->altsetting[0].desc.bInterfaceNumber);
556 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
557 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
558 "claimed\n", dev->udev->devnum,
559 intf->altsetting[0].desc.bInterfaceNumber);
563 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
564 if (streaming == NULL) {
565 usb_driver_release_interface(&uvc_driver.driver, intf);
569 mutex_init(&streaming->mutex);
570 streaming->dev = dev;
571 streaming->intf = usb_get_intf(intf);
572 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
574 /* The Pico iMage webcam has its class-specific interface descriptors
575 * after the endpoint descriptors.
578 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
579 struct usb_host_endpoint *ep = &alts->endpoint[i];
581 if (ep->extralen == 0)
584 if (ep->extralen > 2 &&
585 ep->extra[1] == USB_DT_CS_INTERFACE) {
586 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
587 "from endpoint %u.\n", i);
588 buffer = alts->endpoint[i].extra;
589 buflen = alts->endpoint[i].extralen;
595 /* Skip the standard interface descriptors. */
596 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
602 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
603 "interface descriptors found.\n");
607 /* Parse the header descriptor. */
609 case UVC_VS_OUTPUT_HEADER:
610 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
614 case UVC_VS_INPUT_HEADER:
615 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
620 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
621 "%d HEADER descriptor not found.\n", dev->udev->devnum,
622 alts->desc.bInterfaceNumber);
626 p = buflen >= 4 ? buffer[3] : 0;
627 n = buflen >= size ? buffer[size-1] : 0;
629 if (buflen < size + p*n) {
630 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
631 "interface %d HEADER descriptor is invalid.\n",
632 dev->udev->devnum, alts->desc.bInterfaceNumber);
636 streaming->header.bNumFormats = p;
637 streaming->header.bEndpointAddress = buffer[6];
638 if (buffer[2] == UVC_VS_INPUT_HEADER) {
639 streaming->header.bmInfo = buffer[7];
640 streaming->header.bTerminalLink = buffer[8];
641 streaming->header.bStillCaptureMethod = buffer[9];
642 streaming->header.bTriggerSupport = buffer[10];
643 streaming->header.bTriggerUsage = buffer[11];
645 streaming->header.bTerminalLink = buffer[7];
647 streaming->header.bControlSize = n;
649 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
650 if (streaming->header.bmaControls == NULL) {
655 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
663 /* Count the format and frame descriptors. */
664 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
665 switch (_buffer[2]) {
666 case UVC_VS_FORMAT_UNCOMPRESSED:
667 case UVC_VS_FORMAT_MJPEG:
668 case UVC_VS_FORMAT_FRAME_BASED:
672 case UVC_VS_FORMAT_DV:
673 /* DV format has no frame descriptor. We will create a
674 * dummy frame descriptor with a dummy frame interval.
681 case UVC_VS_FORMAT_MPEG2TS:
682 case UVC_VS_FORMAT_STREAM_BASED:
683 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
684 "interface %d FORMAT %u is not supported.\n",
686 alts->desc.bInterfaceNumber, _buffer[2]);
689 case UVC_VS_FRAME_UNCOMPRESSED:
690 case UVC_VS_FRAME_MJPEG:
693 nintervals += _buffer[25] ? _buffer[25] : 3;
696 case UVC_VS_FRAME_FRAME_BASED:
699 nintervals += _buffer[21] ? _buffer[21] : 3;
703 _buflen -= _buffer[0];
704 _buffer += _buffer[0];
708 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
709 "%d has no supported formats defined.\n",
710 dev->udev->devnum, alts->desc.bInterfaceNumber);
714 size = nformats * sizeof *format + nframes * sizeof *frame
715 + nintervals * sizeof *interval;
716 format = kzalloc(size, GFP_KERNEL);
717 if (format == NULL) {
722 frame = (struct uvc_frame *)&format[nformats];
723 interval = (__u32 *)&frame[nframes];
725 streaming->format = format;
726 streaming->nformats = nformats;
728 /* Parse the format descriptors. */
729 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
731 case UVC_VS_FORMAT_UNCOMPRESSED:
732 case UVC_VS_FORMAT_MJPEG:
733 case UVC_VS_FORMAT_DV:
734 case UVC_VS_FORMAT_FRAME_BASED:
735 format->frame = frame;
736 ret = uvc_parse_format(dev, streaming, format,
737 &interval, buffer, buflen);
741 frame += format->nframes;
757 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
758 "%d has %u bytes of trailing descriptor garbage.\n",
759 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
761 /* Parse the alternate settings to find the maximum bandwidth. */
762 for (i = 0; i < intf->num_altsetting; ++i) {
763 struct usb_host_endpoint *ep;
764 alts = &intf->altsetting[i];
765 ep = uvc_find_endpoint(alts,
766 streaming->header.bEndpointAddress);
770 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
771 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
772 if (psize > streaming->maxpsize)
773 streaming->maxpsize = psize;
776 list_add_tail(&streaming->list, &dev->streams);
780 usb_driver_release_interface(&uvc_driver.driver, intf);
782 kfree(streaming->format);
783 kfree(streaming->header.bmaControls);
788 /* Parse vendor-specific extensions. */
789 static int uvc_parse_vendor_control(struct uvc_device *dev,
790 const unsigned char *buffer, int buflen)
792 struct usb_device *udev = dev->udev;
793 struct usb_host_interface *alts = dev->intf->cur_altsetting;
794 struct uvc_entity *unit;
798 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
799 case 0x046d: /* Logitech */
800 if (buffer[1] != 0x41 || buffer[2] != 0x01)
803 /* Logitech implements several vendor specific functions
804 * through vendor specific extension units (LXU).
806 * The LXU descriptors are similar to XU descriptors
807 * (see "USB Device Video Class for Video Devices", section
808 * 3.7.2.6 "Extension Unit Descriptor") with the following
811 * ----------------------------------------------------------
813 * Size of this descriptor, in bytes: 24+p+n*2
814 * ----------------------------------------------------------
815 * 23+p+n bmControlsType N Bitmap
816 * Individual bits in the set are defined:
820 * This bitset is mapped exactly the same as bmControls.
821 * ----------------------------------------------------------
822 * 23+p+n*2 bReserved 1 Boolean
823 * ----------------------------------------------------------
824 * 24+p+n*2 iExtension 1 Index
825 * Index of a string descriptor that describes this
827 * ----------------------------------------------------------
829 p = buflen >= 22 ? buffer[21] : 0;
830 n = buflen >= 25 + p ? buffer[22+p] : 0;
832 if (buflen < 25 + p + 2*n) {
833 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
834 "interface %d EXTENSION_UNIT error\n",
835 udev->devnum, alts->desc.bInterfaceNumber);
839 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
843 unit->id = buffer[3];
844 unit->type = UVC_VC_EXTENSION_UNIT;
845 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
846 unit->extension.bNumControls = buffer[20];
847 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
848 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
849 memcpy(unit->extension.baSourceID, &buffer[22], p);
850 unit->extension.bControlSize = buffer[22+p];
851 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
852 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
854 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
856 if (buffer[24+p+2*n] != 0)
857 usb_string(udev, buffer[24+p+2*n], unit->name,
860 sprintf(unit->name, "Extension %u", buffer[3]);
862 list_add_tail(&unit->list, &dev->entities);
870 static int uvc_parse_standard_control(struct uvc_device *dev,
871 const unsigned char *buffer, int buflen)
873 struct usb_device *udev = dev->udev;
874 struct uvc_entity *unit, *term;
875 struct usb_interface *intf;
876 struct usb_host_interface *alts = dev->intf->cur_altsetting;
877 unsigned int i, n, p, len;
882 n = buflen >= 12 ? buffer[11] : 0;
884 if (buflen < 12 || buflen < 12 + n) {
885 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
886 "interface %d HEADER error\n", udev->devnum,
887 alts->desc.bInterfaceNumber);
891 dev->uvc_version = get_unaligned_le16(&buffer[3]);
892 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
894 /* Parse all USB Video Streaming interfaces. */
895 for (i = 0; i < n; ++i) {
896 intf = usb_ifnum_to_if(udev, buffer[12+i]);
898 uvc_trace(UVC_TRACE_DESCR, "device %d "
899 "interface %d doesn't exists\n",
904 uvc_parse_streaming(dev, intf);
908 case UVC_VC_INPUT_TERMINAL:
910 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
911 "interface %d INPUT_TERMINAL error\n",
912 udev->devnum, alts->desc.bInterfaceNumber);
916 /* Make sure the terminal type MSB is not null, otherwise it
917 * could be confused with a unit.
919 type = get_unaligned_le16(&buffer[4]);
920 if ((type & 0xff00) == 0) {
921 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
922 "interface %d INPUT_TERMINAL %d has invalid "
923 "type 0x%04x, skipping\n", udev->devnum,
924 alts->desc.bInterfaceNumber,
933 if (type == UVC_ITT_CAMERA) {
934 n = buflen >= 15 ? buffer[14] : 0;
937 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
938 n = buflen >= 9 ? buffer[8] : 0;
939 p = buflen >= 10 + n ? buffer[9+n] : 0;
943 if (buflen < len + n + p) {
944 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
945 "interface %d INPUT_TERMINAL error\n",
946 udev->devnum, alts->desc.bInterfaceNumber);
950 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
954 term->id = buffer[3];
955 term->type = type | UVC_TERM_INPUT;
957 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
958 term->camera.bControlSize = n;
959 term->camera.bmControls = (__u8 *)term + sizeof *term;
960 term->camera.wObjectiveFocalLengthMin =
961 get_unaligned_le16(&buffer[8]);
962 term->camera.wObjectiveFocalLengthMax =
963 get_unaligned_le16(&buffer[10]);
964 term->camera.wOcularFocalLength =
965 get_unaligned_le16(&buffer[12]);
966 memcpy(term->camera.bmControls, &buffer[15], n);
967 } else if (UVC_ENTITY_TYPE(term) ==
968 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
969 term->media.bControlSize = n;
970 term->media.bmControls = (__u8 *)term + sizeof *term;
971 term->media.bTransportModeSize = p;
972 term->media.bmTransportModes = (__u8 *)term
974 memcpy(term->media.bmControls, &buffer[9], n);
975 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
979 usb_string(udev, buffer[7], term->name,
981 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
982 sprintf(term->name, "Camera %u", buffer[3]);
983 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
984 sprintf(term->name, "Media %u", buffer[3]);
986 sprintf(term->name, "Input %u", buffer[3]);
988 list_add_tail(&term->list, &dev->entities);
991 case UVC_VC_OUTPUT_TERMINAL:
993 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
994 "interface %d OUTPUT_TERMINAL error\n",
995 udev->devnum, alts->desc.bInterfaceNumber);
999 /* Make sure the terminal type MSB is not null, otherwise it
1000 * could be confused with a unit.
1002 type = get_unaligned_le16(&buffer[4]);
1003 if ((type & 0xff00) == 0) {
1004 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1005 "interface %d OUTPUT_TERMINAL %d has invalid "
1006 "type 0x%04x, skipping\n", udev->devnum,
1007 alts->desc.bInterfaceNumber, buffer[3], type);
1011 term = kzalloc(sizeof *term, GFP_KERNEL);
1015 term->id = buffer[3];
1016 term->type = type | UVC_TERM_OUTPUT;
1017 term->output.bSourceID = buffer[7];
1020 usb_string(udev, buffer[8], term->name,
1023 sprintf(term->name, "Output %u", buffer[3]);
1025 list_add_tail(&term->list, &dev->entities);
1028 case UVC_VC_SELECTOR_UNIT:
1029 p = buflen >= 5 ? buffer[4] : 0;
1031 if (buflen < 5 || buflen < 6 + p) {
1032 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1033 "interface %d SELECTOR_UNIT error\n",
1034 udev->devnum, alts->desc.bInterfaceNumber);
1038 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1042 unit->id = buffer[3];
1043 unit->type = buffer[2];
1044 unit->selector.bNrInPins = buffer[4];
1045 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1046 memcpy(unit->selector.baSourceID, &buffer[5], p);
1048 if (buffer[5+p] != 0)
1049 usb_string(udev, buffer[5+p], unit->name,
1052 sprintf(unit->name, "Selector %u", buffer[3]);
1054 list_add_tail(&unit->list, &dev->entities);
1057 case UVC_VC_PROCESSING_UNIT:
1058 n = buflen >= 8 ? buffer[7] : 0;
1059 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1061 if (buflen < p + n) {
1062 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1063 "interface %d PROCESSING_UNIT error\n",
1064 udev->devnum, alts->desc.bInterfaceNumber);
1068 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1072 unit->id = buffer[3];
1073 unit->type = buffer[2];
1074 unit->processing.bSourceID = buffer[4];
1075 unit->processing.wMaxMultiplier =
1076 get_unaligned_le16(&buffer[5]);
1077 unit->processing.bControlSize = buffer[7];
1078 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1079 memcpy(unit->processing.bmControls, &buffer[8], n);
1080 if (dev->uvc_version >= 0x0110)
1081 unit->processing.bmVideoStandards = buffer[9+n];
1083 if (buffer[8+n] != 0)
1084 usb_string(udev, buffer[8+n], unit->name,
1087 sprintf(unit->name, "Processing %u", buffer[3]);
1089 list_add_tail(&unit->list, &dev->entities);
1092 case UVC_VC_EXTENSION_UNIT:
1093 p = buflen >= 22 ? buffer[21] : 0;
1094 n = buflen >= 24 + p ? buffer[22+p] : 0;
1096 if (buflen < 24 + p + n) {
1097 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1098 "interface %d EXTENSION_UNIT error\n",
1099 udev->devnum, alts->desc.bInterfaceNumber);
1103 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1107 unit->id = buffer[3];
1108 unit->type = buffer[2];
1109 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1110 unit->extension.bNumControls = buffer[20];
1111 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
1112 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1113 memcpy(unit->extension.baSourceID, &buffer[22], p);
1114 unit->extension.bControlSize = buffer[22+p];
1115 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1116 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1118 if (buffer[23+p+n] != 0)
1119 usb_string(udev, buffer[23+p+n], unit->name,
1122 sprintf(unit->name, "Extension %u", buffer[3]);
1124 list_add_tail(&unit->list, &dev->entities);
1128 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1129 "descriptor (%u)\n", buffer[2]);
1136 static int uvc_parse_control(struct uvc_device *dev)
1138 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1139 unsigned char *buffer = alts->extra;
1140 int buflen = alts->extralen;
1143 /* Parse the default alternate setting only, as the UVC specification
1144 * defines a single alternate setting, the default alternate setting
1148 while (buflen > 2) {
1149 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1150 buffer[1] != USB_DT_CS_INTERFACE)
1151 goto next_descriptor;
1153 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1157 buflen -= buffer[0];
1158 buffer += buffer[0];
1161 /* Check if the optional status endpoint is present. Built-in iSight
1162 * webcams have an interrupt endpoint but spit proprietary data that
1163 * don't conform to the UVC status endpoint messages. Don't try to
1164 * handle the interrupt endpoint for those cameras.
1166 if (alts->desc.bNumEndpoints == 1 &&
1167 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1168 struct usb_host_endpoint *ep = &alts->endpoint[0];
1169 struct usb_endpoint_descriptor *desc = &ep->desc;
1171 if (usb_endpoint_is_int_in(desc) &&
1172 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1173 desc->bInterval != 0) {
1174 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1175 "(addr %02x).\n", desc->bEndpointAddress);
1183 /* ------------------------------------------------------------------------
1188 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1189 * and containing the following units:
1191 * - one or more Output Terminals (USB Streaming or Display)
1192 * - zero or one Processing Unit
1193 * - zero, one or more single-input Selector Units
1194 * - zero or one multiple-input Selector Units, provided all inputs are
1195 * connected to input terminals
1196 * - zero, one or mode single-input Extension Units
1197 * - one or more Input Terminals (Camera, External or USB Streaming)
1199 * The terminal and units must match on of the following structures:
1201 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1202 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1203 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1205 * +---------+ +---------+ -> OTT_*(0)
1206 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1207 * +---------+ +---------+ -> OTT_*(n)
1209 * The Processing Unit and Extension Units can be in any order. Additional
1210 * Extension Units connected to the main chain as single-unit branches are
1211 * also supported. Single-input Selector Units are ignored.
1213 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1214 struct uvc_entity *entity)
1216 switch (UVC_ENTITY_TYPE(entity)) {
1217 case UVC_VC_EXTENSION_UNIT:
1218 if (uvc_trace_param & UVC_TRACE_PROBE)
1219 printk(" <- XU %d", entity->id);
1221 if (entity->extension.bNrInPins != 1) {
1222 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1223 "than 1 input pin.\n", entity->id);
1227 list_add_tail(&entity->chain, &chain->extensions);
1230 case UVC_VC_PROCESSING_UNIT:
1231 if (uvc_trace_param & UVC_TRACE_PROBE)
1232 printk(" <- PU %d", entity->id);
1234 if (chain->processing != NULL) {
1235 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1236 "Processing Units in chain.\n");
1240 chain->processing = entity;
1243 case UVC_VC_SELECTOR_UNIT:
1244 if (uvc_trace_param & UVC_TRACE_PROBE)
1245 printk(" <- SU %d", entity->id);
1247 /* Single-input selector units are ignored. */
1248 if (entity->selector.bNrInPins == 1)
1251 if (chain->selector != NULL) {
1252 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1253 "Units in chain.\n");
1257 chain->selector = entity;
1260 case UVC_ITT_VENDOR_SPECIFIC:
1261 case UVC_ITT_CAMERA:
1262 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1263 if (uvc_trace_param & UVC_TRACE_PROBE)
1264 printk(" <- IT %d\n", entity->id);
1266 list_add_tail(&entity->chain, &chain->iterms);
1269 case UVC_TT_STREAMING:
1270 if (uvc_trace_param & UVC_TRACE_PROBE)
1271 printk(" <- IT %d\n", entity->id);
1273 if (!UVC_ENTITY_IS_ITERM(entity)) {
1274 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1275 "terminal %u.\n", entity->id);
1279 list_add_tail(&entity->chain, &chain->iterms);
1283 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1284 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1291 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1292 struct uvc_entity *entity, struct uvc_entity *prev)
1294 struct uvc_entity *forward;
1302 forward = uvc_entity_by_reference(chain->dev, entity->id,
1304 if (forward == NULL)
1306 if (forward == prev)
1309 switch (UVC_ENTITY_TYPE(forward)) {
1310 case UVC_VC_EXTENSION_UNIT:
1311 if (forward->extension.bNrInPins != 1) {
1312 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1313 "has more than 1 input pin.\n",
1318 list_add_tail(&forward->chain, &chain->extensions);
1319 if (uvc_trace_param & UVC_TRACE_PROBE) {
1323 printk(" XU %d", forward->id);
1328 case UVC_OTT_VENDOR_SPECIFIC:
1329 case UVC_OTT_DISPLAY:
1330 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1331 case UVC_TT_STREAMING:
1332 if (UVC_ENTITY_IS_ITERM(forward)) {
1333 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1334 "terminal %u.\n", forward->id);
1338 list_add_tail(&forward->chain, &chain->oterms);
1339 if (uvc_trace_param & UVC_TRACE_PROBE) {
1343 printk(" OT %d", forward->id);
1355 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1356 struct uvc_entity *entity)
1358 struct uvc_entity *term;
1361 switch (UVC_ENTITY_TYPE(entity)) {
1362 case UVC_VC_EXTENSION_UNIT:
1363 id = entity->extension.baSourceID[0];
1366 case UVC_VC_PROCESSING_UNIT:
1367 id = entity->processing.bSourceID;
1370 case UVC_VC_SELECTOR_UNIT:
1371 /* Single-input selector units are ignored. */
1372 if (entity->selector.bNrInPins == 1) {
1373 id = entity->selector.baSourceID[0];
1377 if (uvc_trace_param & UVC_TRACE_PROBE)
1380 chain->selector = entity;
1381 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1382 id = entity->selector.baSourceID[i];
1383 term = uvc_entity_by_id(chain->dev, id);
1384 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1385 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1386 "input %d isn't connected to an "
1387 "input terminal\n", entity->id, i);
1391 if (uvc_trace_param & UVC_TRACE_PROBE)
1392 printk(" %d", term->id);
1394 list_add_tail(&term->chain, &chain->iterms);
1395 uvc_scan_chain_forward(chain, term, entity);
1398 if (uvc_trace_param & UVC_TRACE_PROBE)
1408 static int uvc_scan_chain(struct uvc_video_chain *chain,
1409 struct uvc_entity *oterm)
1411 struct uvc_entity *entity, *prev;
1415 list_add_tail(&entity->chain, &chain->oterms);
1416 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1418 id = entity->output.bSourceID;
1421 entity = uvc_entity_by_id(chain->dev, id);
1422 if (entity == NULL) {
1423 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1424 "unknown entity %d.\n", id);
1428 if (entity->chain.next || entity->chain.prev) {
1429 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1430 "entity %d already in chain.\n", id);
1434 /* Process entity */
1435 if (uvc_scan_chain_entity(chain, entity) < 0)
1439 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1442 /* Stop when a terminal is found. */
1443 if (UVC_ENTITY_IS_TERM(entity))
1447 id = uvc_scan_chain_backward(chain, entity);
1455 static unsigned int uvc_print_terms(struct list_head *terms, char *buffer)
1457 struct uvc_entity *term;
1458 unsigned int nterms = 0;
1461 list_for_each_entry(term, terms, chain) {
1462 p += sprintf(p, "%u", term->id);
1463 if (term->chain.next != terms) {
1464 p += sprintf(p, ",");
1465 if (++nterms >= 4) {
1466 p += sprintf(p, "...");
1475 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1477 static char buffer[43];
1480 p += uvc_print_terms(&chain->iterms, p);
1481 p += sprintf(p, " -> ");
1482 uvc_print_terms(&chain->oterms, p);
1488 * Scan the device for video chains and register video devices.
1490 * Chains are scanned starting at their output terminals and walked backwards.
1492 static int uvc_scan_device(struct uvc_device *dev)
1494 struct uvc_video_chain *chain;
1495 struct uvc_entity *term;
1497 list_for_each_entry(term, &dev->entities, list) {
1498 if (!UVC_ENTITY_IS_OTERM(term))
1501 /* If the terminal is already included in a chain, skip it.
1502 * This can happen for chains that have multiple output
1503 * terminals, where all output terminals beside the first one
1504 * will be inserted in the chain in forward scans.
1506 if (term->chain.next || term->chain.prev)
1509 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1513 INIT_LIST_HEAD(&chain->iterms);
1514 INIT_LIST_HEAD(&chain->oterms);
1515 INIT_LIST_HEAD(&chain->extensions);
1516 mutex_init(&chain->ctrl_mutex);
1519 if (uvc_scan_chain(chain, term) < 0) {
1524 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1525 uvc_print_chain(chain));
1527 list_add_tail(&chain->list, &dev->chains);
1530 if (list_empty(&dev->chains)) {
1531 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1538 /* ------------------------------------------------------------------------
1539 * Video device registration and unregistration
1543 * Delete the UVC device.
1545 * Called by the kernel when the last reference to the uvc_device structure
1548 * As this function is called after or during disconnect(), all URBs have
1549 * already been canceled by the USB core. There is no need to kill the
1550 * interrupt URB manually.
1552 static void uvc_delete(struct uvc_device *dev)
1554 struct list_head *p, *n;
1556 usb_put_intf(dev->intf);
1557 usb_put_dev(dev->udev);
1559 uvc_status_cleanup(dev);
1560 uvc_ctrl_cleanup_device(dev);
1562 list_for_each_safe(p, n, &dev->chains) {
1563 struct uvc_video_chain *chain;
1564 chain = list_entry(p, struct uvc_video_chain, list);
1568 list_for_each_safe(p, n, &dev->entities) {
1569 struct uvc_entity *entity;
1570 entity = list_entry(p, struct uvc_entity, list);
1574 list_for_each_safe(p, n, &dev->streams) {
1575 struct uvc_streaming *streaming;
1576 streaming = list_entry(p, struct uvc_streaming, list);
1577 usb_driver_release_interface(&uvc_driver.driver,
1579 usb_put_intf(streaming->intf);
1580 kfree(streaming->format);
1581 kfree(streaming->header.bmaControls);
1588 static void uvc_release(struct video_device *vdev)
1590 struct uvc_streaming *stream = video_get_drvdata(vdev);
1591 struct uvc_device *dev = stream->dev;
1593 video_device_release(vdev);
1595 /* Decrement the registered streams count and delete the device when it
1598 if (atomic_dec_and_test(&dev->nstreams))
1603 * Unregister the video devices.
1605 static void uvc_unregister_video(struct uvc_device *dev)
1607 struct uvc_streaming *stream;
1609 /* Unregistering all video devices might result in uvc_delete() being
1610 * called from inside the loop if there's no open file handle. To avoid
1611 * that, increment the stream count before iterating over the streams
1612 * and decrement it when done.
1614 atomic_inc(&dev->nstreams);
1616 list_for_each_entry(stream, &dev->streams, list) {
1617 if (stream->vdev == NULL)
1620 video_unregister_device(stream->vdev);
1621 stream->vdev = NULL;
1624 /* Decrement the stream count and call uvc_delete explicitly if there
1625 * are no stream left.
1627 if (atomic_dec_and_test(&dev->nstreams))
1631 static int uvc_register_video(struct uvc_device *dev,
1632 struct uvc_streaming *stream)
1634 struct video_device *vdev;
1637 /* Initialize the streaming interface with default streaming
1640 ret = uvc_video_init(stream);
1642 uvc_printk(KERN_ERR, "Failed to initialize the device "
1647 /* Register the device with V4L. */
1648 vdev = video_device_alloc();
1650 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1655 /* We already hold a reference to dev->udev. The video device will be
1656 * unregistered before the reference is released, so we don't need to
1659 vdev->parent = &dev->intf->dev;
1661 vdev->fops = &uvc_fops;
1662 vdev->release = uvc_release;
1663 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1665 /* Set the driver data before calling video_register_device, otherwise
1666 * uvc_v4l2_open might race us.
1668 stream->vdev = vdev;
1669 video_set_drvdata(vdev, stream);
1671 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1673 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1675 stream->vdev = NULL;
1676 video_device_release(vdev);
1680 atomic_inc(&dev->nstreams);
1685 * Register all video devices in all chains.
1687 static int uvc_register_terms(struct uvc_device *dev,
1688 struct uvc_video_chain *chain, struct list_head *terms)
1690 struct uvc_streaming *stream;
1691 struct uvc_entity *term;
1694 list_for_each_entry(term, terms, chain) {
1695 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1698 stream = uvc_stream_by_id(dev, term->id);
1699 if (stream == NULL) {
1700 uvc_printk(KERN_INFO, "No streaming interface found "
1701 "for terminal %u.", term->id);
1705 stream->chain = chain;
1706 ret = uvc_register_video(dev, stream);
1714 static int uvc_register_chains(struct uvc_device *dev)
1716 struct uvc_video_chain *chain;
1719 list_for_each_entry(chain, &dev->chains, list) {
1720 ret = uvc_register_terms(dev, chain, &chain->iterms);
1724 ret = uvc_register_terms(dev, chain, &chain->oterms);
1732 /* ------------------------------------------------------------------------
1733 * USB probe, disconnect, suspend and resume
1736 static int uvc_probe(struct usb_interface *intf,
1737 const struct usb_device_id *id)
1739 struct usb_device *udev = interface_to_usbdev(intf);
1740 struct uvc_device *dev;
1743 if (id->idVendor && id->idProduct)
1744 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1745 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1748 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1751 /* Allocate memory for the device and initialize it. */
1752 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1755 INIT_LIST_HEAD(&dev->entities);
1756 INIT_LIST_HEAD(&dev->chains);
1757 INIT_LIST_HEAD(&dev->streams);
1758 atomic_set(&dev->nstreams, 0);
1759 atomic_set(&dev->users, 0);
1761 dev->udev = usb_get_dev(udev);
1762 dev->intf = usb_get_intf(intf);
1763 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1764 dev->quirks = id->driver_info | uvc_quirks_param;
1766 if (udev->product != NULL)
1767 strlcpy(dev->name, udev->product, sizeof dev->name);
1769 snprintf(dev->name, sizeof dev->name,
1770 "UVC Camera (%04x:%04x)",
1771 le16_to_cpu(udev->descriptor.idVendor),
1772 le16_to_cpu(udev->descriptor.idProduct));
1774 /* Parse the Video Class control descriptor. */
1775 if (uvc_parse_control(dev) < 0) {
1776 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1781 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1782 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1783 udev->product ? udev->product : "<unnamed>",
1784 le16_to_cpu(udev->descriptor.idVendor),
1785 le16_to_cpu(udev->descriptor.idProduct));
1787 if (uvc_quirks_param != 0) {
1788 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1789 "parameter for testing purpose.\n", uvc_quirks_param);
1790 uvc_printk(KERN_INFO, "Please report required quirks to the "
1791 "linux-uvc-devel mailing list.\n");
1794 /* Initialize controls. */
1795 if (uvc_ctrl_init_device(dev) < 0)
1798 /* Scan the device for video chains. */
1799 if (uvc_scan_device(dev) < 0)
1802 /* Register video devices. */
1803 if (uvc_register_chains(dev) < 0)
1806 /* Save our data pointer in the interface data. */
1807 usb_set_intfdata(intf, dev);
1809 /* Initialize the interrupt URB. */
1810 if ((ret = uvc_status_init(dev)) < 0) {
1811 uvc_printk(KERN_INFO, "Unable to initialize the status "
1812 "endpoint (%d), status interrupt will not be "
1813 "supported.\n", ret);
1816 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1820 uvc_unregister_video(dev);
1824 static void uvc_disconnect(struct usb_interface *intf)
1826 struct uvc_device *dev = usb_get_intfdata(intf);
1828 /* Set the USB interface data to NULL. This can be done outside the
1829 * lock, as there's no other reader.
1831 usb_set_intfdata(intf, NULL);
1833 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1834 UVC_SC_VIDEOSTREAMING)
1837 dev->state |= UVC_DEV_DISCONNECTED;
1839 uvc_unregister_video(dev);
1842 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1844 struct uvc_device *dev = usb_get_intfdata(intf);
1845 struct uvc_streaming *stream;
1847 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1848 intf->cur_altsetting->desc.bInterfaceNumber);
1850 /* Controls are cached on the fly so they don't need to be saved. */
1851 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1852 UVC_SC_VIDEOCONTROL)
1853 return uvc_status_suspend(dev);
1855 list_for_each_entry(stream, &dev->streams, list) {
1856 if (stream->intf == intf)
1857 return uvc_video_suspend(stream);
1860 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1865 static int __uvc_resume(struct usb_interface *intf, int reset)
1867 struct uvc_device *dev = usb_get_intfdata(intf);
1868 struct uvc_streaming *stream;
1870 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1871 intf->cur_altsetting->desc.bInterfaceNumber);
1873 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1874 UVC_SC_VIDEOCONTROL) {
1876 int ret = uvc_ctrl_resume_device(dev);
1882 return uvc_status_resume(dev);
1885 list_for_each_entry(stream, &dev->streams, list) {
1886 if (stream->intf == intf)
1887 return uvc_video_resume(stream);
1890 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1895 static int uvc_resume(struct usb_interface *intf)
1897 return __uvc_resume(intf, 0);
1900 static int uvc_reset_resume(struct usb_interface *intf)
1902 return __uvc_resume(intf, 1);
1905 /* ------------------------------------------------------------------------
1906 * Driver initialization and cleanup
1910 * The Logitech cameras listed below have their interface class set to
1911 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1912 * though they are compliant.
1914 static struct usb_device_id uvc_ids[] = {
1915 /* Microsoft Lifecam NX-6000 */
1916 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1917 | USB_DEVICE_ID_MATCH_INT_INFO,
1919 .idProduct = 0x00f8,
1920 .bInterfaceClass = USB_CLASS_VIDEO,
1921 .bInterfaceSubClass = 1,
1922 .bInterfaceProtocol = 0,
1923 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1924 /* Microsoft Lifecam VX-7000 */
1925 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1926 | USB_DEVICE_ID_MATCH_INT_INFO,
1928 .idProduct = 0x0723,
1929 .bInterfaceClass = USB_CLASS_VIDEO,
1930 .bInterfaceSubClass = 1,
1931 .bInterfaceProtocol = 0,
1932 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1933 /* Logitech Quickcam Fusion */
1934 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1935 | USB_DEVICE_ID_MATCH_INT_INFO,
1937 .idProduct = 0x08c1,
1938 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1939 .bInterfaceSubClass = 1,
1940 .bInterfaceProtocol = 0 },
1941 /* Logitech Quickcam Orbit MP */
1942 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1943 | USB_DEVICE_ID_MATCH_INT_INFO,
1945 .idProduct = 0x08c2,
1946 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1947 .bInterfaceSubClass = 1,
1948 .bInterfaceProtocol = 0 },
1949 /* Logitech Quickcam Pro for Notebook */
1950 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1951 | USB_DEVICE_ID_MATCH_INT_INFO,
1953 .idProduct = 0x08c3,
1954 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1955 .bInterfaceSubClass = 1,
1956 .bInterfaceProtocol = 0 },
1957 /* Logitech Quickcam Pro 5000 */
1958 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1959 | USB_DEVICE_ID_MATCH_INT_INFO,
1961 .idProduct = 0x08c5,
1962 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1963 .bInterfaceSubClass = 1,
1964 .bInterfaceProtocol = 0 },
1965 /* Logitech Quickcam OEM Dell Notebook */
1966 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1967 | USB_DEVICE_ID_MATCH_INT_INFO,
1969 .idProduct = 0x08c6,
1970 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1971 .bInterfaceSubClass = 1,
1972 .bInterfaceProtocol = 0 },
1973 /* Logitech Quickcam OEM Cisco VT Camera II */
1974 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1975 | USB_DEVICE_ID_MATCH_INT_INFO,
1977 .idProduct = 0x08c7,
1978 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1979 .bInterfaceSubClass = 1,
1980 .bInterfaceProtocol = 0 },
1981 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1982 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1983 | USB_DEVICE_ID_MATCH_INT_INFO,
1985 .idProduct = 0x3820,
1986 .bInterfaceClass = USB_CLASS_VIDEO,
1987 .bInterfaceSubClass = 1,
1988 .bInterfaceProtocol = 0,
1989 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1990 /* Apple Built-In iSight */
1991 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1992 | USB_DEVICE_ID_MATCH_INT_INFO,
1994 .idProduct = 0x8501,
1995 .bInterfaceClass = USB_CLASS_VIDEO,
1996 .bInterfaceSubClass = 1,
1997 .bInterfaceProtocol = 0,
1998 .driver_info = UVC_QUIRK_PROBE_MINMAX
1999 | UVC_QUIRK_BUILTIN_ISIGHT },
2000 /* Genesys Logic USB 2.0 PC Camera */
2001 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2002 | USB_DEVICE_ID_MATCH_INT_INFO,
2004 .idProduct = 0x0505,
2005 .bInterfaceClass = USB_CLASS_VIDEO,
2006 .bInterfaceSubClass = 1,
2007 .bInterfaceProtocol = 0,
2008 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2010 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2011 | USB_DEVICE_ID_MATCH_INT_INFO,
2013 .idProduct = 0x332d,
2014 .bInterfaceClass = USB_CLASS_VIDEO,
2015 .bInterfaceSubClass = 1,
2016 .bInterfaceProtocol = 0,
2017 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2018 /* ViMicro - Minoru3D */
2019 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2020 | USB_DEVICE_ID_MATCH_INT_INFO,
2022 .idProduct = 0x3410,
2023 .bInterfaceClass = USB_CLASS_VIDEO,
2024 .bInterfaceSubClass = 1,
2025 .bInterfaceProtocol = 0,
2026 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2027 /* ViMicro Venus - Minoru3D */
2028 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2029 | USB_DEVICE_ID_MATCH_INT_INFO,
2031 .idProduct = 0x3420,
2032 .bInterfaceClass = USB_CLASS_VIDEO,
2033 .bInterfaceSubClass = 1,
2034 .bInterfaceProtocol = 0,
2035 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2037 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2038 | USB_DEVICE_ID_MATCH_INT_INFO,
2040 .idProduct = 0x0004,
2041 .bInterfaceClass = USB_CLASS_VIDEO,
2042 .bInterfaceSubClass = 1,
2043 .bInterfaceProtocol = 0,
2044 .driver_info = UVC_QUIRK_PROBE_MINMAX
2045 | UVC_QUIRK_PROBE_DEF },
2046 /* Syntek (HP Spartan) */
2047 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2048 | USB_DEVICE_ID_MATCH_INT_INFO,
2050 .idProduct = 0x5212,
2051 .bInterfaceClass = USB_CLASS_VIDEO,
2052 .bInterfaceSubClass = 1,
2053 .bInterfaceProtocol = 0,
2054 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2055 /* Syntek (Samsung Q310) */
2056 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2057 | USB_DEVICE_ID_MATCH_INT_INFO,
2059 .idProduct = 0x5931,
2060 .bInterfaceClass = USB_CLASS_VIDEO,
2061 .bInterfaceSubClass = 1,
2062 .bInterfaceProtocol = 0,
2063 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2064 /* Syntek (Asus F9SG) */
2065 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2066 | USB_DEVICE_ID_MATCH_INT_INFO,
2068 .idProduct = 0x8a31,
2069 .bInterfaceClass = USB_CLASS_VIDEO,
2070 .bInterfaceSubClass = 1,
2071 .bInterfaceProtocol = 0,
2072 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2073 /* Syntek (Asus U3S) */
2074 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2075 | USB_DEVICE_ID_MATCH_INT_INFO,
2077 .idProduct = 0x8a33,
2078 .bInterfaceClass = USB_CLASS_VIDEO,
2079 .bInterfaceSubClass = 1,
2080 .bInterfaceProtocol = 0,
2081 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2082 /* Syntek (JAOtech Smart Terminal) */
2083 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2084 | USB_DEVICE_ID_MATCH_INT_INFO,
2086 .idProduct = 0x8a34,
2087 .bInterfaceClass = USB_CLASS_VIDEO,
2088 .bInterfaceSubClass = 1,
2089 .bInterfaceProtocol = 0,
2090 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2091 /* Lenovo Thinkpad SL400/SL500 */
2092 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2093 | USB_DEVICE_ID_MATCH_INT_INFO,
2095 .idProduct = 0x480b,
2096 .bInterfaceClass = USB_CLASS_VIDEO,
2097 .bInterfaceSubClass = 1,
2098 .bInterfaceProtocol = 0,
2099 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2100 /* Aveo Technology USB 2.0 Camera */
2101 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2102 | USB_DEVICE_ID_MATCH_INT_INFO,
2104 .idProduct = 0x0306,
2105 .bInterfaceClass = USB_CLASS_VIDEO,
2106 .bInterfaceSubClass = 1,
2107 .bInterfaceProtocol = 0,
2108 .driver_info = UVC_QUIRK_PROBE_MINMAX
2109 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2110 /* Ecamm Pico iMage */
2111 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2112 | USB_DEVICE_ID_MATCH_INT_INFO,
2114 .idProduct = 0xcafe,
2115 .bInterfaceClass = USB_CLASS_VIDEO,
2116 .bInterfaceSubClass = 1,
2117 .bInterfaceProtocol = 0,
2118 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2119 /* FSC WebCam V30S */
2120 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2121 | USB_DEVICE_ID_MATCH_INT_INFO,
2123 .idProduct = 0x3288,
2124 .bInterfaceClass = USB_CLASS_VIDEO,
2125 .bInterfaceSubClass = 1,
2126 .bInterfaceProtocol = 0,
2127 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2128 /* Bodelin ProScopeHR */
2129 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2130 | USB_DEVICE_ID_MATCH_DEV_HI
2131 | USB_DEVICE_ID_MATCH_INT_INFO,
2133 .idProduct = 0x1000,
2134 .bcdDevice_hi = 0x0126,
2135 .bInterfaceClass = USB_CLASS_VIDEO,
2136 .bInterfaceSubClass = 1,
2137 .bInterfaceProtocol = 0,
2138 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2139 /* SiGma Micro USB Web Camera */
2140 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2141 | USB_DEVICE_ID_MATCH_INT_INFO,
2143 .idProduct = 0x3000,
2144 .bInterfaceClass = USB_CLASS_VIDEO,
2145 .bInterfaceSubClass = 1,
2146 .bInterfaceProtocol = 0,
2147 .driver_info = UVC_QUIRK_PROBE_MINMAX
2148 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2149 /* Generic USB Video Class */
2150 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2154 MODULE_DEVICE_TABLE(usb, uvc_ids);
2156 struct uvc_driver uvc_driver = {
2160 .disconnect = uvc_disconnect,
2161 .suspend = uvc_suspend,
2162 .resume = uvc_resume,
2163 .reset_resume = uvc_reset_resume,
2164 .id_table = uvc_ids,
2165 .supports_autosuspend = 1,
2169 static int __init uvc_init(void)
2173 INIT_LIST_HEAD(&uvc_driver.devices);
2174 INIT_LIST_HEAD(&uvc_driver.controls);
2175 mutex_init(&uvc_driver.ctrl_mutex);
2179 result = usb_register(&uvc_driver.driver);
2181 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2185 static void __exit uvc_cleanup(void)
2187 usb_deregister(&uvc_driver.driver);
2190 module_init(uvc_init);
2191 module_exit(uvc_cleanup);
2193 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2194 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2195 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2196 MODULE_PARM_DESC(quirks, "Forced device quirks");
2197 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2198 MODULE_PARM_DESC(trace, "Trace level bitmask");
2199 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2200 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2202 MODULE_AUTHOR(DRIVER_AUTHOR);
2203 MODULE_DESCRIPTION(DRIVER_DESC);
2204 MODULE_LICENSE("GPL");
2205 MODULE_VERSION(DRIVER_VERSION);