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/slab.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>
35 #include <asm/unaligned.h>
37 #include <media/v4l2-common.h>
41 #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
42 #define DRIVER_DESC "USB Video Class driver"
43 #ifndef DRIVER_VERSION
44 #define DRIVER_VERSION "v0.1.0"
47 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
48 unsigned int uvc_no_drop_param;
49 static unsigned int uvc_quirks_param = -1;
50 unsigned int uvc_trace_param;
51 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
53 /* ------------------------------------------------------------------------
57 static struct uvc_format_desc uvc_fmts[] = {
59 .name = "YUV 4:2:2 (YUYV)",
60 .guid = UVC_GUID_FORMAT_YUY2,
61 .fcc = V4L2_PIX_FMT_YUYV,
64 .name = "YUV 4:2:2 (YUYV)",
65 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
66 .fcc = V4L2_PIX_FMT_YUYV,
69 .name = "YUV 4:2:0 (NV12)",
70 .guid = UVC_GUID_FORMAT_NV12,
71 .fcc = V4L2_PIX_FMT_NV12,
75 .guid = UVC_GUID_FORMAT_MJPEG,
76 .fcc = V4L2_PIX_FMT_MJPEG,
79 .name = "YVU 4:2:0 (YV12)",
80 .guid = UVC_GUID_FORMAT_YV12,
81 .fcc = V4L2_PIX_FMT_YVU420,
84 .name = "YUV 4:2:0 (I420)",
85 .guid = UVC_GUID_FORMAT_I420,
86 .fcc = V4L2_PIX_FMT_YUV420,
89 .name = "YUV 4:2:2 (UYVY)",
90 .guid = UVC_GUID_FORMAT_UYVY,
91 .fcc = V4L2_PIX_FMT_UYVY,
94 .name = "Greyscale (8-bit)",
95 .guid = UVC_GUID_FORMAT_Y800,
96 .fcc = V4L2_PIX_FMT_GREY,
99 .name = "Greyscale (16-bit)",
100 .guid = UVC_GUID_FORMAT_Y16,
101 .fcc = V4L2_PIX_FMT_Y16,
105 .guid = UVC_GUID_FORMAT_BY8,
106 .fcc = V4L2_PIX_FMT_SBGGR8,
110 /* ------------------------------------------------------------------------
114 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
117 struct usb_host_endpoint *ep;
120 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
121 ep = &alts->endpoint[i];
122 if (ep->desc.bEndpointAddress == epaddr)
129 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
131 unsigned int len = ARRAY_SIZE(uvc_fmts);
134 for (i = 0; i < len; ++i) {
135 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
142 static __u32 uvc_colorspace(const __u8 primaries)
144 static const __u8 colorprimaries[] = {
146 V4L2_COLORSPACE_SRGB,
147 V4L2_COLORSPACE_470_SYSTEM_M,
148 V4L2_COLORSPACE_470_SYSTEM_BG,
149 V4L2_COLORSPACE_SMPTE170M,
150 V4L2_COLORSPACE_SMPTE240M,
153 if (primaries < ARRAY_SIZE(colorprimaries))
154 return colorprimaries[primaries];
159 /* Simplify a fraction using a simple continued fraction decomposition. The
160 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
161 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
162 * arbitrary parameters to remove non-significative terms from the simple
163 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
164 * respectively seems to give nice results.
166 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
167 unsigned int n_terms, unsigned int threshold)
173 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
177 /* Convert the fraction to a simple continued fraction. See
178 * http://mathforum.org/dr.math/faq/faq.fractions.html
179 * Stop if the current term is bigger than or equal to the given
185 for (n = 0; n < n_terms && y != 0; ++n) {
187 if (an[n] >= threshold) {
198 /* Expand the simple continued fraction back to an integer fraction. */
202 for (i = n; i > 0; --i) {
213 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
214 * to compute numerator / denominator * 10000000 using 32 bit fixed point
217 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
221 /* Saturate the result if the operation would overflow. */
222 if (denominator == 0 ||
223 numerator/denominator >= ((uint32_t)-1)/10000000)
226 /* Divide both the denominator and the multiplier by two until
227 * numerator * multiplier doesn't overflow. If anyone knows a better
228 * algorithm please let me know.
230 multiplier = 10000000;
231 while (numerator > ((uint32_t)-1)/multiplier) {
236 return denominator ? numerator * multiplier / denominator : 0;
239 /* ------------------------------------------------------------------------
240 * Terminal and unit management
243 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
245 struct uvc_entity *entity;
247 list_for_each_entry(entity, &dev->entities, list) {
248 if (entity->id == id)
255 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
256 int id, struct uvc_entity *entity)
261 entity = list_entry(&dev->entities, struct uvc_entity, list);
263 list_for_each_entry_continue(entity, &dev->entities, list) {
264 for (i = 0; i < entity->bNrInPins; ++i)
265 if (entity->baSourceID[i] == id)
272 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
274 struct uvc_streaming *stream;
276 list_for_each_entry(stream, &dev->streams, list) {
277 if (stream->header.bTerminalLink == id)
284 /* ------------------------------------------------------------------------
285 * Descriptors parsing
288 static int uvc_parse_format(struct uvc_device *dev,
289 struct uvc_streaming *streaming, struct uvc_format *format,
290 __u32 **intervals, unsigned char *buffer, int buflen)
292 struct usb_interface *intf = streaming->intf;
293 struct usb_host_interface *alts = intf->cur_altsetting;
294 struct uvc_format_desc *fmtdesc;
295 struct uvc_frame *frame;
296 const unsigned char *start = buffer;
297 unsigned int interval;
301 format->type = buffer[2];
302 format->index = buffer[3];
305 case UVC_VS_FORMAT_UNCOMPRESSED:
306 case UVC_VS_FORMAT_FRAME_BASED:
307 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
309 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
310 "interface %d FORMAT error\n",
312 alts->desc.bInterfaceNumber);
316 /* Find the format descriptor from its GUID. */
317 fmtdesc = uvc_format_by_guid(&buffer[5]);
319 if (fmtdesc != NULL) {
320 strlcpy(format->name, fmtdesc->name,
321 sizeof format->name);
322 format->fcc = fmtdesc->fcc;
324 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
326 snprintf(format->name, sizeof(format->name), "%pUl\n",
331 format->bpp = buffer[21];
332 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
333 ftype = UVC_VS_FRAME_UNCOMPRESSED;
335 ftype = UVC_VS_FRAME_FRAME_BASED;
337 format->flags = UVC_FMT_FLAG_COMPRESSED;
341 case UVC_VS_FORMAT_MJPEG:
343 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
344 "interface %d FORMAT error\n",
346 alts->desc.bInterfaceNumber);
350 strlcpy(format->name, "MJPEG", sizeof format->name);
351 format->fcc = V4L2_PIX_FMT_MJPEG;
352 format->flags = UVC_FMT_FLAG_COMPRESSED;
354 ftype = UVC_VS_FRAME_MJPEG;
357 case UVC_VS_FORMAT_DV:
359 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
360 "interface %d FORMAT error\n",
362 alts->desc.bInterfaceNumber);
366 switch (buffer[8] & 0x7f) {
368 strlcpy(format->name, "SD-DV", sizeof format->name);
371 strlcpy(format->name, "SDL-DV", sizeof format->name);
374 strlcpy(format->name, "HD-DV", sizeof format->name);
377 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
378 "interface %d: unknown DV format %u\n",
380 alts->desc.bInterfaceNumber, buffer[8]);
384 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
385 sizeof format->name);
387 format->fcc = V4L2_PIX_FMT_DV;
388 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
392 /* Create a dummy frame descriptor. */
393 frame = &format->frame[0];
394 memset(&format->frame[0], 0, sizeof format->frame[0]);
395 frame->bFrameIntervalType = 1;
396 frame->dwDefaultFrameInterval = 1;
397 frame->dwFrameInterval = *intervals;
402 case UVC_VS_FORMAT_MPEG2TS:
403 case UVC_VS_FORMAT_STREAM_BASED:
404 /* Not supported yet. */
406 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
407 "interface %d unsupported format %u\n",
408 dev->udev->devnum, alts->desc.bInterfaceNumber,
413 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
418 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
419 * based formats have frame descriptors.
421 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
422 buffer[2] == ftype) {
423 frame = &format->frame[format->nframes];
424 if (ftype != UVC_VS_FRAME_FRAME_BASED)
425 n = buflen > 25 ? buffer[25] : 0;
427 n = buflen > 21 ? buffer[21] : 0;
431 if (buflen < 26 + 4*n) {
432 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
433 "interface %d FRAME error\n", dev->udev->devnum,
434 alts->desc.bInterfaceNumber);
438 frame->bFrameIndex = buffer[3];
439 frame->bmCapabilities = buffer[4];
440 frame->wWidth = get_unaligned_le16(&buffer[5]);
441 frame->wHeight = get_unaligned_le16(&buffer[7]);
442 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
443 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
444 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
445 frame->dwMaxVideoFrameBufferSize =
446 get_unaligned_le32(&buffer[17]);
447 frame->dwDefaultFrameInterval =
448 get_unaligned_le32(&buffer[21]);
449 frame->bFrameIntervalType = buffer[25];
451 frame->dwMaxVideoFrameBufferSize = 0;
452 frame->dwDefaultFrameInterval =
453 get_unaligned_le32(&buffer[17]);
454 frame->bFrameIntervalType = buffer[21];
456 frame->dwFrameInterval = *intervals;
458 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
459 * completely. Observed behaviours range from setting the
460 * value to 1.1x the actual frame size to hardwiring the
461 * 16 low bits to 0. This results in a higher than necessary
462 * memory usage as well as a wrong image size information. For
463 * uncompressed formats this can be fixed by computing the
464 * value from the frame size.
466 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
467 frame->dwMaxVideoFrameBufferSize = format->bpp
468 * frame->wWidth * frame->wHeight / 8;
470 /* Some bogus devices report dwMinFrameInterval equal to
471 * dwMaxFrameInterval and have dwFrameIntervalStep set to
472 * zero. Setting all null intervals to 1 fixes the problem and
473 * some other divisions by zero that could happen.
475 for (i = 0; i < n; ++i) {
476 interval = get_unaligned_le32(&buffer[26+4*i]);
477 *(*intervals)++ = interval ? interval : 1;
480 /* Make sure that the default frame interval stays between
483 n -= frame->bFrameIntervalType ? 1 : 2;
484 frame->dwDefaultFrameInterval =
485 min(frame->dwFrameInterval[n],
486 max(frame->dwFrameInterval[0],
487 frame->dwDefaultFrameInterval));
489 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
490 frame->wWidth, frame->wHeight,
491 10000000/frame->dwDefaultFrameInterval,
492 (100000000/frame->dwDefaultFrameInterval)%10);
499 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
500 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
505 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
506 buffer[2] == UVC_VS_COLORFORMAT) {
508 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
509 "interface %d COLORFORMAT error\n",
511 alts->desc.bInterfaceNumber);
515 format->colorspace = uvc_colorspace(buffer[3]);
521 return buffer - start;
524 static int uvc_parse_streaming(struct uvc_device *dev,
525 struct usb_interface *intf)
527 struct uvc_streaming *streaming = NULL;
528 struct uvc_format *format;
529 struct uvc_frame *frame;
530 struct usb_host_interface *alts = &intf->altsetting[0];
531 unsigned char *_buffer, *buffer = alts->extra;
532 int _buflen, buflen = alts->extralen;
533 unsigned int nformats = 0, nframes = 0, nintervals = 0;
534 unsigned int size, i, n, p;
539 if (intf->cur_altsetting->desc.bInterfaceSubClass
540 != UVC_SC_VIDEOSTREAMING) {
541 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
542 "video streaming interface\n", dev->udev->devnum,
543 intf->altsetting[0].desc.bInterfaceNumber);
547 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
548 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
549 "claimed\n", dev->udev->devnum,
550 intf->altsetting[0].desc.bInterfaceNumber);
554 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
555 if (streaming == NULL) {
556 usb_driver_release_interface(&uvc_driver.driver, intf);
560 mutex_init(&streaming->mutex);
561 streaming->dev = dev;
562 streaming->intf = usb_get_intf(intf);
563 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
565 /* The Pico iMage webcam has its class-specific interface descriptors
566 * after the endpoint descriptors.
569 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
570 struct usb_host_endpoint *ep = &alts->endpoint[i];
572 if (ep->extralen == 0)
575 if (ep->extralen > 2 &&
576 ep->extra[1] == USB_DT_CS_INTERFACE) {
577 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
578 "from endpoint %u.\n", i);
579 buffer = alts->endpoint[i].extra;
580 buflen = alts->endpoint[i].extralen;
586 /* Skip the standard interface descriptors. */
587 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
593 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
594 "interface descriptors found.\n");
598 /* Parse the header descriptor. */
600 case UVC_VS_OUTPUT_HEADER:
601 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
605 case UVC_VS_INPUT_HEADER:
606 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
611 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
612 "%d HEADER descriptor not found.\n", dev->udev->devnum,
613 alts->desc.bInterfaceNumber);
617 p = buflen >= 4 ? buffer[3] : 0;
618 n = buflen >= size ? buffer[size-1] : 0;
620 if (buflen < size + p*n) {
621 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
622 "interface %d HEADER descriptor is invalid.\n",
623 dev->udev->devnum, alts->desc.bInterfaceNumber);
627 streaming->header.bNumFormats = p;
628 streaming->header.bEndpointAddress = buffer[6];
629 if (buffer[2] == UVC_VS_INPUT_HEADER) {
630 streaming->header.bmInfo = buffer[7];
631 streaming->header.bTerminalLink = buffer[8];
632 streaming->header.bStillCaptureMethod = buffer[9];
633 streaming->header.bTriggerSupport = buffer[10];
634 streaming->header.bTriggerUsage = buffer[11];
636 streaming->header.bTerminalLink = buffer[7];
638 streaming->header.bControlSize = n;
640 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
641 if (streaming->header.bmaControls == NULL) {
646 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
654 /* Count the format and frame descriptors. */
655 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
656 switch (_buffer[2]) {
657 case UVC_VS_FORMAT_UNCOMPRESSED:
658 case UVC_VS_FORMAT_MJPEG:
659 case UVC_VS_FORMAT_FRAME_BASED:
663 case UVC_VS_FORMAT_DV:
664 /* DV format has no frame descriptor. We will create a
665 * dummy frame descriptor with a dummy frame interval.
672 case UVC_VS_FORMAT_MPEG2TS:
673 case UVC_VS_FORMAT_STREAM_BASED:
674 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
675 "interface %d FORMAT %u is not supported.\n",
677 alts->desc.bInterfaceNumber, _buffer[2]);
680 case UVC_VS_FRAME_UNCOMPRESSED:
681 case UVC_VS_FRAME_MJPEG:
684 nintervals += _buffer[25] ? _buffer[25] : 3;
687 case UVC_VS_FRAME_FRAME_BASED:
690 nintervals += _buffer[21] ? _buffer[21] : 3;
694 _buflen -= _buffer[0];
695 _buffer += _buffer[0];
699 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
700 "%d has no supported formats defined.\n",
701 dev->udev->devnum, alts->desc.bInterfaceNumber);
705 size = nformats * sizeof *format + nframes * sizeof *frame
706 + nintervals * sizeof *interval;
707 format = kzalloc(size, GFP_KERNEL);
708 if (format == NULL) {
713 frame = (struct uvc_frame *)&format[nformats];
714 interval = (__u32 *)&frame[nframes];
716 streaming->format = format;
717 streaming->nformats = nformats;
719 /* Parse the format descriptors. */
720 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
722 case UVC_VS_FORMAT_UNCOMPRESSED:
723 case UVC_VS_FORMAT_MJPEG:
724 case UVC_VS_FORMAT_DV:
725 case UVC_VS_FORMAT_FRAME_BASED:
726 format->frame = frame;
727 ret = uvc_parse_format(dev, streaming, format,
728 &interval, buffer, buflen);
732 frame += format->nframes;
748 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
749 "%d has %u bytes of trailing descriptor garbage.\n",
750 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
752 /* Parse the alternate settings to find the maximum bandwidth. */
753 for (i = 0; i < intf->num_altsetting; ++i) {
754 struct usb_host_endpoint *ep;
755 alts = &intf->altsetting[i];
756 ep = uvc_find_endpoint(alts,
757 streaming->header.bEndpointAddress);
761 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
762 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
763 if (psize > streaming->maxpsize)
764 streaming->maxpsize = psize;
767 list_add_tail(&streaming->list, &dev->streams);
771 usb_driver_release_interface(&uvc_driver.driver, intf);
773 kfree(streaming->format);
774 kfree(streaming->header.bmaControls);
779 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
780 unsigned int num_pads, unsigned int extra_size)
782 struct uvc_entity *entity;
783 unsigned int num_inputs;
786 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
787 size = sizeof(*entity) + extra_size + num_inputs;
788 entity = kzalloc(size, GFP_KERNEL);
795 entity->bNrInPins = num_inputs;
796 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
801 /* Parse vendor-specific extensions. */
802 static int uvc_parse_vendor_control(struct uvc_device *dev,
803 const unsigned char *buffer, int buflen)
805 struct usb_device *udev = dev->udev;
806 struct usb_host_interface *alts = dev->intf->cur_altsetting;
807 struct uvc_entity *unit;
811 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
812 case 0x046d: /* Logitech */
813 if (buffer[1] != 0x41 || buffer[2] != 0x01)
816 /* Logitech implements several vendor specific functions
817 * through vendor specific extension units (LXU).
819 * The LXU descriptors are similar to XU descriptors
820 * (see "USB Device Video Class for Video Devices", section
821 * 3.7.2.6 "Extension Unit Descriptor") with the following
824 * ----------------------------------------------------------
826 * Size of this descriptor, in bytes: 24+p+n*2
827 * ----------------------------------------------------------
828 * 23+p+n bmControlsType N Bitmap
829 * Individual bits in the set are defined:
833 * This bitset is mapped exactly the same as bmControls.
834 * ----------------------------------------------------------
835 * 23+p+n*2 bReserved 1 Boolean
836 * ----------------------------------------------------------
837 * 24+p+n*2 iExtension 1 Index
838 * Index of a string descriptor that describes this
840 * ----------------------------------------------------------
842 p = buflen >= 22 ? buffer[21] : 0;
843 n = buflen >= 25 + p ? buffer[22+p] : 0;
845 if (buflen < 25 + p + 2*n) {
846 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
847 "interface %d EXTENSION_UNIT error\n",
848 udev->devnum, alts->desc.bInterfaceNumber);
852 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
857 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
858 unit->extension.bNumControls = buffer[20];
859 memcpy(unit->baSourceID, &buffer[22], p);
860 unit->extension.bControlSize = buffer[22+p];
861 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
862 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
864 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
866 if (buffer[24+p+2*n] != 0)
867 usb_string(udev, buffer[24+p+2*n], unit->name,
870 sprintf(unit->name, "Extension %u", buffer[3]);
872 list_add_tail(&unit->list, &dev->entities);
880 static int uvc_parse_standard_control(struct uvc_device *dev,
881 const unsigned char *buffer, int buflen)
883 struct usb_device *udev = dev->udev;
884 struct uvc_entity *unit, *term;
885 struct usb_interface *intf;
886 struct usb_host_interface *alts = dev->intf->cur_altsetting;
887 unsigned int i, n, p, len;
892 n = buflen >= 12 ? buffer[11] : 0;
894 if (buflen < 12 || buflen < 12 + n) {
895 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
896 "interface %d HEADER error\n", udev->devnum,
897 alts->desc.bInterfaceNumber);
901 dev->uvc_version = get_unaligned_le16(&buffer[3]);
902 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
904 /* Parse all USB Video Streaming interfaces. */
905 for (i = 0; i < n; ++i) {
906 intf = usb_ifnum_to_if(udev, buffer[12+i]);
908 uvc_trace(UVC_TRACE_DESCR, "device %d "
909 "interface %d doesn't exists\n",
914 uvc_parse_streaming(dev, intf);
918 case UVC_VC_INPUT_TERMINAL:
920 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
921 "interface %d INPUT_TERMINAL error\n",
922 udev->devnum, alts->desc.bInterfaceNumber);
926 /* Make sure the terminal type MSB is not null, otherwise it
927 * could be confused with a unit.
929 type = get_unaligned_le16(&buffer[4]);
930 if ((type & 0xff00) == 0) {
931 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
932 "interface %d INPUT_TERMINAL %d has invalid "
933 "type 0x%04x, skipping\n", udev->devnum,
934 alts->desc.bInterfaceNumber,
943 if (type == UVC_ITT_CAMERA) {
944 n = buflen >= 15 ? buffer[14] : 0;
947 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
948 n = buflen >= 9 ? buffer[8] : 0;
949 p = buflen >= 10 + n ? buffer[9+n] : 0;
953 if (buflen < len + n + p) {
954 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
955 "interface %d INPUT_TERMINAL error\n",
956 udev->devnum, alts->desc.bInterfaceNumber);
960 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
965 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
966 term->camera.bControlSize = n;
967 term->camera.bmControls = (__u8 *)term + sizeof *term;
968 term->camera.wObjectiveFocalLengthMin =
969 get_unaligned_le16(&buffer[8]);
970 term->camera.wObjectiveFocalLengthMax =
971 get_unaligned_le16(&buffer[10]);
972 term->camera.wOcularFocalLength =
973 get_unaligned_le16(&buffer[12]);
974 memcpy(term->camera.bmControls, &buffer[15], n);
975 } else if (UVC_ENTITY_TYPE(term) ==
976 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
977 term->media.bControlSize = n;
978 term->media.bmControls = (__u8 *)term + sizeof *term;
979 term->media.bTransportModeSize = p;
980 term->media.bmTransportModes = (__u8 *)term
982 memcpy(term->media.bmControls, &buffer[9], n);
983 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
987 usb_string(udev, buffer[7], term->name,
989 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
990 sprintf(term->name, "Camera %u", buffer[3]);
991 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
992 sprintf(term->name, "Media %u", buffer[3]);
994 sprintf(term->name, "Input %u", buffer[3]);
996 list_add_tail(&term->list, &dev->entities);
999 case UVC_VC_OUTPUT_TERMINAL:
1001 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1002 "interface %d OUTPUT_TERMINAL error\n",
1003 udev->devnum, alts->desc.bInterfaceNumber);
1007 /* Make sure the terminal type MSB is not null, otherwise it
1008 * could be confused with a unit.
1010 type = get_unaligned_le16(&buffer[4]);
1011 if ((type & 0xff00) == 0) {
1012 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1013 "interface %d OUTPUT_TERMINAL %d has invalid "
1014 "type 0x%04x, skipping\n", udev->devnum,
1015 alts->desc.bInterfaceNumber, buffer[3], type);
1019 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1024 memcpy(term->baSourceID, &buffer[7], 1);
1027 usb_string(udev, buffer[8], term->name,
1030 sprintf(term->name, "Output %u", buffer[3]);
1032 list_add_tail(&term->list, &dev->entities);
1035 case UVC_VC_SELECTOR_UNIT:
1036 p = buflen >= 5 ? buffer[4] : 0;
1038 if (buflen < 5 || buflen < 6 + p) {
1039 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1040 "interface %d SELECTOR_UNIT error\n",
1041 udev->devnum, alts->desc.bInterfaceNumber);
1045 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1049 memcpy(unit->baSourceID, &buffer[5], p);
1051 if (buffer[5+p] != 0)
1052 usb_string(udev, buffer[5+p], unit->name,
1055 sprintf(unit->name, "Selector %u", buffer[3]);
1057 list_add_tail(&unit->list, &dev->entities);
1060 case UVC_VC_PROCESSING_UNIT:
1061 n = buflen >= 8 ? buffer[7] : 0;
1062 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1064 if (buflen < p + n) {
1065 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1066 "interface %d PROCESSING_UNIT error\n",
1067 udev->devnum, alts->desc.bInterfaceNumber);
1071 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1075 memcpy(unit->baSourceID, &buffer[4], 1);
1076 unit->processing.wMaxMultiplier =
1077 get_unaligned_le16(&buffer[5]);
1078 unit->processing.bControlSize = buffer[7];
1079 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1080 memcpy(unit->processing.bmControls, &buffer[8], n);
1081 if (dev->uvc_version >= 0x0110)
1082 unit->processing.bmVideoStandards = buffer[9+n];
1084 if (buffer[8+n] != 0)
1085 usb_string(udev, buffer[8+n], unit->name,
1088 sprintf(unit->name, "Processing %u", buffer[3]);
1090 list_add_tail(&unit->list, &dev->entities);
1093 case UVC_VC_EXTENSION_UNIT:
1094 p = buflen >= 22 ? buffer[21] : 0;
1095 n = buflen >= 24 + p ? buffer[22+p] : 0;
1097 if (buflen < 24 + p + n) {
1098 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1099 "interface %d EXTENSION_UNIT error\n",
1100 udev->devnum, alts->desc.bInterfaceNumber);
1104 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1108 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1109 unit->extension.bNumControls = buffer[20];
1110 memcpy(unit->baSourceID, &buffer[22], p);
1111 unit->extension.bControlSize = buffer[22+p];
1112 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1113 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1115 if (buffer[23+p+n] != 0)
1116 usb_string(udev, buffer[23+p+n], unit->name,
1119 sprintf(unit->name, "Extension %u", buffer[3]);
1121 list_add_tail(&unit->list, &dev->entities);
1125 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1126 "descriptor (%u)\n", buffer[2]);
1133 static int uvc_parse_control(struct uvc_device *dev)
1135 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1136 unsigned char *buffer = alts->extra;
1137 int buflen = alts->extralen;
1140 /* Parse the default alternate setting only, as the UVC specification
1141 * defines a single alternate setting, the default alternate setting
1145 while (buflen > 2) {
1146 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1147 buffer[1] != USB_DT_CS_INTERFACE)
1148 goto next_descriptor;
1150 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1154 buflen -= buffer[0];
1155 buffer += buffer[0];
1158 /* Check if the optional status endpoint is present. Built-in iSight
1159 * webcams have an interrupt endpoint but spit proprietary data that
1160 * don't conform to the UVC status endpoint messages. Don't try to
1161 * handle the interrupt endpoint for those cameras.
1163 if (alts->desc.bNumEndpoints == 1 &&
1164 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1165 struct usb_host_endpoint *ep = &alts->endpoint[0];
1166 struct usb_endpoint_descriptor *desc = &ep->desc;
1168 if (usb_endpoint_is_int_in(desc) &&
1169 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1170 desc->bInterval != 0) {
1171 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1172 "(addr %02x).\n", desc->bEndpointAddress);
1180 /* ------------------------------------------------------------------------
1185 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1186 * and containing the following units:
1188 * - one or more Output Terminals (USB Streaming or Display)
1189 * - zero or one Processing Unit
1190 * - zero, one or more single-input Selector Units
1191 * - zero or one multiple-input Selector Units, provided all inputs are
1192 * connected to input terminals
1193 * - zero, one or mode single-input Extension Units
1194 * - one or more Input Terminals (Camera, External or USB Streaming)
1196 * The terminal and units must match on of the following structures:
1198 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1199 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1200 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1202 * +---------+ +---------+ -> OTT_*(0)
1203 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1204 * +---------+ +---------+ -> OTT_*(n)
1206 * The Processing Unit and Extension Units can be in any order. Additional
1207 * Extension Units connected to the main chain as single-unit branches are
1208 * also supported. Single-input Selector Units are ignored.
1210 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1211 struct uvc_entity *entity)
1213 switch (UVC_ENTITY_TYPE(entity)) {
1214 case UVC_VC_EXTENSION_UNIT:
1215 if (uvc_trace_param & UVC_TRACE_PROBE)
1216 printk(" <- XU %d", entity->id);
1218 if (entity->bNrInPins != 1) {
1219 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1220 "than 1 input pin.\n", entity->id);
1226 case UVC_VC_PROCESSING_UNIT:
1227 if (uvc_trace_param & UVC_TRACE_PROBE)
1228 printk(" <- PU %d", entity->id);
1230 if (chain->processing != NULL) {
1231 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1232 "Processing Units in chain.\n");
1236 chain->processing = entity;
1239 case UVC_VC_SELECTOR_UNIT:
1240 if (uvc_trace_param & UVC_TRACE_PROBE)
1241 printk(" <- SU %d", entity->id);
1243 /* Single-input selector units are ignored. */
1244 if (entity->bNrInPins == 1)
1247 if (chain->selector != NULL) {
1248 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1249 "Units in chain.\n");
1253 chain->selector = entity;
1256 case UVC_ITT_VENDOR_SPECIFIC:
1257 case UVC_ITT_CAMERA:
1258 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1259 if (uvc_trace_param & UVC_TRACE_PROBE)
1260 printk(" <- IT %d\n", entity->id);
1264 case UVC_TT_STREAMING:
1265 if (UVC_ENTITY_IS_ITERM(entity)) {
1266 if (uvc_trace_param & UVC_TRACE_PROBE)
1267 printk(" <- IT %d\n", entity->id);
1269 if (uvc_trace_param & UVC_TRACE_PROBE)
1270 printk(" OT %d", entity->id);
1276 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1277 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1281 list_add_tail(&entity->chain, &chain->entities);
1285 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1286 struct uvc_entity *entity, struct uvc_entity *prev)
1288 struct uvc_entity *forward;
1296 forward = uvc_entity_by_reference(chain->dev, entity->id,
1298 if (forward == NULL)
1300 if (forward == prev)
1303 switch (UVC_ENTITY_TYPE(forward)) {
1304 case UVC_VC_EXTENSION_UNIT:
1305 if (forward->bNrInPins != 1) {
1306 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1307 "has more than 1 input pin.\n",
1312 list_add_tail(&forward->chain, &chain->entities);
1313 if (uvc_trace_param & UVC_TRACE_PROBE) {
1317 printk(" XU %d", forward->id);
1322 case UVC_OTT_VENDOR_SPECIFIC:
1323 case UVC_OTT_DISPLAY:
1324 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1325 case UVC_TT_STREAMING:
1326 if (UVC_ENTITY_IS_ITERM(forward)) {
1327 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1328 "terminal %u.\n", forward->id);
1332 list_add_tail(&forward->chain, &chain->entities);
1333 if (uvc_trace_param & UVC_TRACE_PROBE) {
1337 printk(" OT %d", forward->id);
1349 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1350 struct uvc_entity **_entity)
1352 struct uvc_entity *entity = *_entity;
1353 struct uvc_entity *term;
1354 int id = -EINVAL, i;
1356 switch (UVC_ENTITY_TYPE(entity)) {
1357 case UVC_VC_EXTENSION_UNIT:
1358 case UVC_VC_PROCESSING_UNIT:
1359 id = entity->baSourceID[0];
1362 case UVC_VC_SELECTOR_UNIT:
1363 /* Single-input selector units are ignored. */
1364 if (entity->bNrInPins == 1) {
1365 id = entity->baSourceID[0];
1369 if (uvc_trace_param & UVC_TRACE_PROBE)
1372 chain->selector = entity;
1373 for (i = 0; i < entity->bNrInPins; ++i) {
1374 id = entity->baSourceID[i];
1375 term = uvc_entity_by_id(chain->dev, id);
1376 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1377 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1378 "input %d isn't connected to an "
1379 "input terminal\n", entity->id, i);
1383 if (uvc_trace_param & UVC_TRACE_PROBE)
1384 printk(" %d", term->id);
1386 list_add_tail(&term->chain, &chain->entities);
1387 uvc_scan_chain_forward(chain, term, entity);
1390 if (uvc_trace_param & UVC_TRACE_PROBE)
1396 case UVC_ITT_VENDOR_SPECIFIC:
1397 case UVC_ITT_CAMERA:
1398 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1399 case UVC_OTT_VENDOR_SPECIFIC:
1400 case UVC_OTT_DISPLAY:
1401 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1402 case UVC_TT_STREAMING:
1403 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1412 entity = uvc_entity_by_id(chain->dev, id);
1413 if (entity == NULL) {
1414 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1415 "unknown entity %d.\n", id);
1423 static int uvc_scan_chain(struct uvc_video_chain *chain,
1424 struct uvc_entity *term)
1426 struct uvc_entity *entity, *prev;
1428 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1433 while (entity != NULL) {
1434 /* Entity must not be part of an existing chain */
1435 if (entity->chain.next || entity->chain.prev) {
1436 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1437 "entity %d already in chain.\n", entity->id);
1441 /* Process entity */
1442 if (uvc_scan_chain_entity(chain, entity) < 0)
1446 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1451 if (uvc_scan_chain_backward(chain, &entity) < 0)
1458 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1461 struct uvc_entity *term;
1462 unsigned int nterms = 0;
1465 list_for_each_entry(term, terms, chain) {
1466 if (!UVC_ENTITY_IS_TERM(term) ||
1467 UVC_TERM_DIRECTION(term) != dir)
1471 p += sprintf(p, ",");
1472 if (++nterms >= 4) {
1473 p += sprintf(p, "...");
1476 p += sprintf(p, "%u", term->id);
1482 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1484 static char buffer[43];
1487 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1488 p += sprintf(p, " -> ");
1489 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1495 * Scan the device for video chains and register video devices.
1497 * Chains are scanned starting at their output terminals and walked backwards.
1499 static int uvc_scan_device(struct uvc_device *dev)
1501 struct uvc_video_chain *chain;
1502 struct uvc_entity *term;
1504 list_for_each_entry(term, &dev->entities, list) {
1505 if (!UVC_ENTITY_IS_OTERM(term))
1508 /* If the terminal is already included in a chain, skip it.
1509 * This can happen for chains that have multiple output
1510 * terminals, where all output terminals beside the first one
1511 * will be inserted in the chain in forward scans.
1513 if (term->chain.next || term->chain.prev)
1516 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1520 INIT_LIST_HEAD(&chain->entities);
1521 mutex_init(&chain->ctrl_mutex);
1524 if (uvc_scan_chain(chain, term) < 0) {
1529 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1530 uvc_print_chain(chain));
1532 list_add_tail(&chain->list, &dev->chains);
1535 if (list_empty(&dev->chains)) {
1536 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1543 /* ------------------------------------------------------------------------
1544 * Video device registration and unregistration
1548 * Delete the UVC device.
1550 * Called by the kernel when the last reference to the uvc_device structure
1553 * As this function is called after or during disconnect(), all URBs have
1554 * already been canceled by the USB core. There is no need to kill the
1555 * interrupt URB manually.
1557 static void uvc_delete(struct uvc_device *dev)
1559 struct list_head *p, *n;
1561 usb_put_intf(dev->intf);
1562 usb_put_dev(dev->udev);
1564 uvc_status_cleanup(dev);
1565 uvc_ctrl_cleanup_device(dev);
1567 list_for_each_safe(p, n, &dev->chains) {
1568 struct uvc_video_chain *chain;
1569 chain = list_entry(p, struct uvc_video_chain, list);
1573 list_for_each_safe(p, n, &dev->entities) {
1574 struct uvc_entity *entity;
1575 entity = list_entry(p, struct uvc_entity, list);
1579 list_for_each_safe(p, n, &dev->streams) {
1580 struct uvc_streaming *streaming;
1581 streaming = list_entry(p, struct uvc_streaming, list);
1582 usb_driver_release_interface(&uvc_driver.driver,
1584 usb_put_intf(streaming->intf);
1585 kfree(streaming->format);
1586 kfree(streaming->header.bmaControls);
1593 static void uvc_release(struct video_device *vdev)
1595 struct uvc_streaming *stream = video_get_drvdata(vdev);
1596 struct uvc_device *dev = stream->dev;
1598 video_device_release(vdev);
1600 /* Decrement the registered streams count and delete the device when it
1603 if (atomic_dec_and_test(&dev->nstreams))
1608 * Unregister the video devices.
1610 static void uvc_unregister_video(struct uvc_device *dev)
1612 struct uvc_streaming *stream;
1614 /* Unregistering all video devices might result in uvc_delete() being
1615 * called from inside the loop if there's no open file handle. To avoid
1616 * that, increment the stream count before iterating over the streams
1617 * and decrement it when done.
1619 atomic_inc(&dev->nstreams);
1621 list_for_each_entry(stream, &dev->streams, list) {
1622 if (stream->vdev == NULL)
1625 video_unregister_device(stream->vdev);
1626 stream->vdev = NULL;
1629 /* Decrement the stream count and call uvc_delete explicitly if there
1630 * are no stream left.
1632 if (atomic_dec_and_test(&dev->nstreams))
1636 static int uvc_register_video(struct uvc_device *dev,
1637 struct uvc_streaming *stream)
1639 struct video_device *vdev;
1642 /* Initialize the streaming interface with default streaming
1645 ret = uvc_video_init(stream);
1647 uvc_printk(KERN_ERR, "Failed to initialize the device "
1652 /* Register the device with V4L. */
1653 vdev = video_device_alloc();
1655 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1660 /* We already hold a reference to dev->udev. The video device will be
1661 * unregistered before the reference is released, so we don't need to
1664 vdev->parent = &dev->intf->dev;
1665 vdev->fops = &uvc_fops;
1666 vdev->release = uvc_release;
1667 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1669 /* Set the driver data before calling video_register_device, otherwise
1670 * uvc_v4l2_open might race us.
1672 stream->vdev = vdev;
1673 video_set_drvdata(vdev, stream);
1675 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1677 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1679 stream->vdev = NULL;
1680 video_device_release(vdev);
1684 atomic_inc(&dev->nstreams);
1689 * Register all video devices in all chains.
1691 static int uvc_register_terms(struct uvc_device *dev,
1692 struct uvc_video_chain *chain)
1694 struct uvc_streaming *stream;
1695 struct uvc_entity *term;
1698 list_for_each_entry(term, &chain->entities, chain) {
1699 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1702 stream = uvc_stream_by_id(dev, term->id);
1703 if (stream == NULL) {
1704 uvc_printk(KERN_INFO, "No streaming interface found "
1705 "for terminal %u.", term->id);
1709 stream->chain = chain;
1710 ret = uvc_register_video(dev, stream);
1718 static int uvc_register_chains(struct uvc_device *dev)
1720 struct uvc_video_chain *chain;
1723 list_for_each_entry(chain, &dev->chains, list) {
1724 ret = uvc_register_terms(dev, chain);
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 = (uvc_quirks_param == -1)
1765 ? id->driver_info : uvc_quirks_param;
1767 if (udev->product != NULL)
1768 strlcpy(dev->name, udev->product, sizeof dev->name);
1770 snprintf(dev->name, sizeof dev->name,
1771 "UVC Camera (%04x:%04x)",
1772 le16_to_cpu(udev->descriptor.idVendor),
1773 le16_to_cpu(udev->descriptor.idProduct));
1775 /* Parse the Video Class control descriptor. */
1776 if (uvc_parse_control(dev) < 0) {
1777 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1782 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1783 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1784 udev->product ? udev->product : "<unnamed>",
1785 le16_to_cpu(udev->descriptor.idVendor),
1786 le16_to_cpu(udev->descriptor.idProduct));
1788 if (dev->quirks != id->driver_info) {
1789 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1790 "parameter for testing purpose.\n", dev->quirks);
1791 uvc_printk(KERN_INFO, "Please report required quirks to the "
1792 "linux-uvc-devel mailing list.\n");
1795 /* Initialize controls. */
1796 if (uvc_ctrl_init_device(dev) < 0)
1799 /* Scan the device for video chains. */
1800 if (uvc_scan_device(dev) < 0)
1803 /* Register video devices. */
1804 if (uvc_register_chains(dev) < 0)
1807 /* Save our data pointer in the interface data. */
1808 usb_set_intfdata(intf, dev);
1810 /* Initialize the interrupt URB. */
1811 if ((ret = uvc_status_init(dev)) < 0) {
1812 uvc_printk(KERN_INFO, "Unable to initialize the status "
1813 "endpoint (%d), status interrupt will not be "
1814 "supported.\n", ret);
1817 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1821 uvc_unregister_video(dev);
1825 static void uvc_disconnect(struct usb_interface *intf)
1827 struct uvc_device *dev = usb_get_intfdata(intf);
1829 /* Set the USB interface data to NULL. This can be done outside the
1830 * lock, as there's no other reader.
1832 usb_set_intfdata(intf, NULL);
1834 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1835 UVC_SC_VIDEOSTREAMING)
1838 dev->state |= UVC_DEV_DISCONNECTED;
1840 uvc_unregister_video(dev);
1843 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1845 struct uvc_device *dev = usb_get_intfdata(intf);
1846 struct uvc_streaming *stream;
1848 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1849 intf->cur_altsetting->desc.bInterfaceNumber);
1851 /* Controls are cached on the fly so they don't need to be saved. */
1852 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1853 UVC_SC_VIDEOCONTROL)
1854 return uvc_status_suspend(dev);
1856 list_for_each_entry(stream, &dev->streams, list) {
1857 if (stream->intf == intf)
1858 return uvc_video_suspend(stream);
1861 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1866 static int __uvc_resume(struct usb_interface *intf, int reset)
1868 struct uvc_device *dev = usb_get_intfdata(intf);
1869 struct uvc_streaming *stream;
1871 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1872 intf->cur_altsetting->desc.bInterfaceNumber);
1874 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1875 UVC_SC_VIDEOCONTROL) {
1877 int ret = uvc_ctrl_resume_device(dev);
1883 return uvc_status_resume(dev);
1886 list_for_each_entry(stream, &dev->streams, list) {
1887 if (stream->intf == intf)
1888 return uvc_video_resume(stream);
1891 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1896 static int uvc_resume(struct usb_interface *intf)
1898 return __uvc_resume(intf, 0);
1901 static int uvc_reset_resume(struct usb_interface *intf)
1903 return __uvc_resume(intf, 1);
1906 /* ------------------------------------------------------------------------
1910 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1912 if (uvc_clock_param == CLOCK_MONOTONIC)
1913 return sprintf(buffer, "CLOCK_MONOTONIC");
1915 return sprintf(buffer, "CLOCK_REALTIME");
1918 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1920 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1921 val += strlen("clock_");
1923 if (strcasecmp(val, "monotonic") == 0)
1924 uvc_clock_param = CLOCK_MONOTONIC;
1925 else if (strcasecmp(val, "realtime") == 0)
1926 uvc_clock_param = CLOCK_REALTIME;
1933 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1934 &uvc_clock_param, S_IRUGO|S_IWUSR);
1935 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1936 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1937 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1938 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1939 MODULE_PARM_DESC(quirks, "Forced device quirks");
1940 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1941 MODULE_PARM_DESC(trace, "Trace level bitmask");
1942 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1943 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1945 /* ------------------------------------------------------------------------
1946 * Driver initialization and cleanup
1950 * The Logitech cameras listed below have their interface class set to
1951 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1952 * though they are compliant.
1954 static struct usb_device_id uvc_ids[] = {
1955 /* Genius eFace 2025 */
1956 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1957 | USB_DEVICE_ID_MATCH_INT_INFO,
1959 .idProduct = 0x706e,
1960 .bInterfaceClass = USB_CLASS_VIDEO,
1961 .bInterfaceSubClass = 1,
1962 .bInterfaceProtocol = 0,
1963 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1964 /* Microsoft Lifecam NX-6000 */
1965 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1966 | USB_DEVICE_ID_MATCH_INT_INFO,
1968 .idProduct = 0x00f8,
1969 .bInterfaceClass = USB_CLASS_VIDEO,
1970 .bInterfaceSubClass = 1,
1971 .bInterfaceProtocol = 0,
1972 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1973 /* Microsoft Lifecam VX-7000 */
1974 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1975 | USB_DEVICE_ID_MATCH_INT_INFO,
1977 .idProduct = 0x0723,
1978 .bInterfaceClass = USB_CLASS_VIDEO,
1979 .bInterfaceSubClass = 1,
1980 .bInterfaceProtocol = 0,
1981 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1982 /* Logitech Quickcam Fusion */
1983 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1984 | USB_DEVICE_ID_MATCH_INT_INFO,
1986 .idProduct = 0x08c1,
1987 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1988 .bInterfaceSubClass = 1,
1989 .bInterfaceProtocol = 0 },
1990 /* Logitech Quickcam Orbit MP */
1991 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1992 | USB_DEVICE_ID_MATCH_INT_INFO,
1994 .idProduct = 0x08c2,
1995 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1996 .bInterfaceSubClass = 1,
1997 .bInterfaceProtocol = 0 },
1998 /* Logitech Quickcam Pro for Notebook */
1999 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2000 | USB_DEVICE_ID_MATCH_INT_INFO,
2002 .idProduct = 0x08c3,
2003 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2004 .bInterfaceSubClass = 1,
2005 .bInterfaceProtocol = 0 },
2006 /* Logitech Quickcam Pro 5000 */
2007 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2008 | USB_DEVICE_ID_MATCH_INT_INFO,
2010 .idProduct = 0x08c5,
2011 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2012 .bInterfaceSubClass = 1,
2013 .bInterfaceProtocol = 0 },
2014 /* Logitech Quickcam OEM Dell Notebook */
2015 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2016 | USB_DEVICE_ID_MATCH_INT_INFO,
2018 .idProduct = 0x08c6,
2019 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2020 .bInterfaceSubClass = 1,
2021 .bInterfaceProtocol = 0 },
2022 /* Logitech Quickcam OEM Cisco VT Camera II */
2023 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2024 | USB_DEVICE_ID_MATCH_INT_INFO,
2026 .idProduct = 0x08c7,
2027 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2028 .bInterfaceSubClass = 1,
2029 .bInterfaceProtocol = 0 },
2030 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2031 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2032 | USB_DEVICE_ID_MATCH_INT_INFO,
2034 .idProduct = 0x3820,
2035 .bInterfaceClass = USB_CLASS_VIDEO,
2036 .bInterfaceSubClass = 1,
2037 .bInterfaceProtocol = 0,
2038 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2039 /* Apple Built-In iSight */
2040 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2041 | USB_DEVICE_ID_MATCH_INT_INFO,
2043 .idProduct = 0x8501,
2044 .bInterfaceClass = USB_CLASS_VIDEO,
2045 .bInterfaceSubClass = 1,
2046 .bInterfaceProtocol = 0,
2047 .driver_info = UVC_QUIRK_PROBE_MINMAX
2048 | UVC_QUIRK_BUILTIN_ISIGHT },
2049 /* Genesys Logic USB 2.0 PC Camera */
2050 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2051 | USB_DEVICE_ID_MATCH_INT_INFO,
2053 .idProduct = 0x0505,
2054 .bInterfaceClass = USB_CLASS_VIDEO,
2055 .bInterfaceSubClass = 1,
2056 .bInterfaceProtocol = 0,
2057 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2060 | USB_DEVICE_ID_MATCH_INT_INFO,
2062 .idProduct = 0x332d,
2063 .bInterfaceClass = USB_CLASS_VIDEO,
2064 .bInterfaceSubClass = 1,
2065 .bInterfaceProtocol = 0,
2066 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2067 /* ViMicro - Minoru3D */
2068 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2069 | USB_DEVICE_ID_MATCH_INT_INFO,
2071 .idProduct = 0x3410,
2072 .bInterfaceClass = USB_CLASS_VIDEO,
2073 .bInterfaceSubClass = 1,
2074 .bInterfaceProtocol = 0,
2075 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2076 /* ViMicro Venus - Minoru3D */
2077 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2078 | USB_DEVICE_ID_MATCH_INT_INFO,
2080 .idProduct = 0x3420,
2081 .bInterfaceClass = USB_CLASS_VIDEO,
2082 .bInterfaceSubClass = 1,
2083 .bInterfaceProtocol = 0,
2084 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2086 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2087 | USB_DEVICE_ID_MATCH_INT_INFO,
2089 .idProduct = 0x0004,
2090 .bInterfaceClass = USB_CLASS_VIDEO,
2091 .bInterfaceSubClass = 1,
2092 .bInterfaceProtocol = 0,
2093 .driver_info = UVC_QUIRK_PROBE_MINMAX
2094 | UVC_QUIRK_PROBE_DEF },
2095 /* Syntek (HP Spartan) */
2096 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2097 | USB_DEVICE_ID_MATCH_INT_INFO,
2099 .idProduct = 0x5212,
2100 .bInterfaceClass = USB_CLASS_VIDEO,
2101 .bInterfaceSubClass = 1,
2102 .bInterfaceProtocol = 0,
2103 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2104 /* Syntek (Samsung Q310) */
2105 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2106 | USB_DEVICE_ID_MATCH_INT_INFO,
2108 .idProduct = 0x5931,
2109 .bInterfaceClass = USB_CLASS_VIDEO,
2110 .bInterfaceSubClass = 1,
2111 .bInterfaceProtocol = 0,
2112 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2113 /* Syntek (Packard Bell EasyNote MX52 */
2114 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2115 | USB_DEVICE_ID_MATCH_INT_INFO,
2117 .idProduct = 0x8a12,
2118 .bInterfaceClass = USB_CLASS_VIDEO,
2119 .bInterfaceSubClass = 1,
2120 .bInterfaceProtocol = 0,
2121 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2122 /* Syntek (Asus F9SG) */
2123 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2124 | USB_DEVICE_ID_MATCH_INT_INFO,
2126 .idProduct = 0x8a31,
2127 .bInterfaceClass = USB_CLASS_VIDEO,
2128 .bInterfaceSubClass = 1,
2129 .bInterfaceProtocol = 0,
2130 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2131 /* Syntek (Asus U3S) */
2132 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2133 | USB_DEVICE_ID_MATCH_INT_INFO,
2135 .idProduct = 0x8a33,
2136 .bInterfaceClass = USB_CLASS_VIDEO,
2137 .bInterfaceSubClass = 1,
2138 .bInterfaceProtocol = 0,
2139 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2140 /* Syntek (JAOtech Smart Terminal) */
2141 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2142 | USB_DEVICE_ID_MATCH_INT_INFO,
2144 .idProduct = 0x8a34,
2145 .bInterfaceClass = USB_CLASS_VIDEO,
2146 .bInterfaceSubClass = 1,
2147 .bInterfaceProtocol = 0,
2148 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2149 /* Lenovo Thinkpad SL400/SL500 */
2150 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2151 | USB_DEVICE_ID_MATCH_INT_INFO,
2153 .idProduct = 0x480b,
2154 .bInterfaceClass = USB_CLASS_VIDEO,
2155 .bInterfaceSubClass = 1,
2156 .bInterfaceProtocol = 0,
2157 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2158 /* Aveo Technology USB 2.0 Camera */
2159 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2160 | USB_DEVICE_ID_MATCH_INT_INFO,
2162 .idProduct = 0x0306,
2163 .bInterfaceClass = USB_CLASS_VIDEO,
2164 .bInterfaceSubClass = 1,
2165 .bInterfaceProtocol = 0,
2166 .driver_info = UVC_QUIRK_PROBE_MINMAX
2167 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2168 /* Ecamm Pico iMage */
2169 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2170 | USB_DEVICE_ID_MATCH_INT_INFO,
2172 .idProduct = 0xcafe,
2173 .bInterfaceClass = USB_CLASS_VIDEO,
2174 .bInterfaceSubClass = 1,
2175 .bInterfaceProtocol = 0,
2176 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2177 /* FSC WebCam V30S */
2178 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2179 | USB_DEVICE_ID_MATCH_INT_INFO,
2181 .idProduct = 0x3288,
2182 .bInterfaceClass = USB_CLASS_VIDEO,
2183 .bInterfaceSubClass = 1,
2184 .bInterfaceProtocol = 0,
2185 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2186 /* Arkmicro unbranded */
2187 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2188 | USB_DEVICE_ID_MATCH_INT_INFO,
2190 .idProduct = 0x3290,
2191 .bInterfaceClass = USB_CLASS_VIDEO,
2192 .bInterfaceSubClass = 1,
2193 .bInterfaceProtocol = 0,
2194 .driver_info = UVC_QUIRK_PROBE_DEF },
2195 /* Bodelin ProScopeHR */
2196 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2197 | USB_DEVICE_ID_MATCH_DEV_HI
2198 | USB_DEVICE_ID_MATCH_INT_INFO,
2200 .idProduct = 0x1000,
2201 .bcdDevice_hi = 0x0126,
2202 .bInterfaceClass = USB_CLASS_VIDEO,
2203 .bInterfaceSubClass = 1,
2204 .bInterfaceProtocol = 0,
2205 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2206 /* MSI StarCam 370i */
2207 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2208 | USB_DEVICE_ID_MATCH_INT_INFO,
2210 .idProduct = 0x2951,
2211 .bInterfaceClass = USB_CLASS_VIDEO,
2212 .bInterfaceSubClass = 1,
2213 .bInterfaceProtocol = 0,
2214 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2215 /* SiGma Micro USB Web Camera */
2216 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2217 | USB_DEVICE_ID_MATCH_INT_INFO,
2219 .idProduct = 0x3000,
2220 .bInterfaceClass = USB_CLASS_VIDEO,
2221 .bInterfaceSubClass = 1,
2222 .bInterfaceProtocol = 0,
2223 .driver_info = UVC_QUIRK_PROBE_MINMAX
2224 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2225 /* Generic USB Video Class */
2226 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2230 MODULE_DEVICE_TABLE(usb, uvc_ids);
2232 struct uvc_driver uvc_driver = {
2236 .disconnect = uvc_disconnect,
2237 .suspend = uvc_suspend,
2238 .resume = uvc_resume,
2239 .reset_resume = uvc_reset_resume,
2240 .id_table = uvc_ids,
2241 .supports_autosuspend = 1,
2245 static int __init uvc_init(void)
2249 INIT_LIST_HEAD(&uvc_driver.devices);
2250 INIT_LIST_HEAD(&uvc_driver.controls);
2251 mutex_init(&uvc_driver.ctrl_mutex);
2255 result = usb_register(&uvc_driver.driver);
2257 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2261 static void __exit uvc_cleanup(void)
2263 usb_deregister(&uvc_driver.driver);
2266 module_init(uvc_init);
2267 module_exit(uvc_cleanup);
2269 MODULE_AUTHOR(DRIVER_AUTHOR);
2270 MODULE_DESCRIPTION(DRIVER_DESC);
2271 MODULE_LICENSE("GPL");
2272 MODULE_VERSION(DRIVER_VERSION);