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,
95 .guid = UVC_GUID_FORMAT_Y800,
96 .fcc = V4L2_PIX_FMT_GREY,
100 .guid = UVC_GUID_FORMAT_BY8,
101 .fcc = V4L2_PIX_FMT_SBGGR8,
105 /* ------------------------------------------------------------------------
109 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
112 struct usb_host_endpoint *ep;
115 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
116 ep = &alts->endpoint[i];
117 if (ep->desc.bEndpointAddress == epaddr)
124 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
126 unsigned int len = ARRAY_SIZE(uvc_fmts);
129 for (i = 0; i < len; ++i) {
130 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
137 static __u32 uvc_colorspace(const __u8 primaries)
139 static const __u8 colorprimaries[] = {
141 V4L2_COLORSPACE_SRGB,
142 V4L2_COLORSPACE_470_SYSTEM_M,
143 V4L2_COLORSPACE_470_SYSTEM_BG,
144 V4L2_COLORSPACE_SMPTE170M,
145 V4L2_COLORSPACE_SMPTE240M,
148 if (primaries < ARRAY_SIZE(colorprimaries))
149 return colorprimaries[primaries];
154 /* Simplify a fraction using a simple continued fraction decomposition. The
155 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
156 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
157 * arbitrary parameters to remove non-significative terms from the simple
158 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
159 * respectively seems to give nice results.
161 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
162 unsigned int n_terms, unsigned int threshold)
168 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
172 /* Convert the fraction to a simple continued fraction. See
173 * http://mathforum.org/dr.math/faq/faq.fractions.html
174 * Stop if the current term is bigger than or equal to the given
180 for (n = 0; n < n_terms && y != 0; ++n) {
182 if (an[n] >= threshold) {
193 /* Expand the simple continued fraction back to an integer fraction. */
197 for (i = n; i > 0; --i) {
208 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
209 * to compute numerator / denominator * 10000000 using 32 bit fixed point
212 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
216 /* Saturate the result if the operation would overflow. */
217 if (denominator == 0 ||
218 numerator/denominator >= ((uint32_t)-1)/10000000)
221 /* Divide both the denominator and the multiplier by two until
222 * numerator * multiplier doesn't overflow. If anyone knows a better
223 * algorithm please let me know.
225 multiplier = 10000000;
226 while (numerator > ((uint32_t)-1)/multiplier) {
231 return denominator ? numerator * multiplier / denominator : 0;
234 /* ------------------------------------------------------------------------
235 * Terminal and unit management
238 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
240 struct uvc_entity *entity;
242 list_for_each_entry(entity, &dev->entities, list) {
243 if (entity->id == id)
250 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
251 int id, struct uvc_entity *entity)
256 entity = list_entry(&dev->entities, struct uvc_entity, list);
258 list_for_each_entry_continue(entity, &dev->entities, list) {
259 for (i = 0; i < entity->bNrInPins; ++i)
260 if (entity->baSourceID[i] == id)
267 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
269 struct uvc_streaming *stream;
271 list_for_each_entry(stream, &dev->streams, list) {
272 if (stream->header.bTerminalLink == id)
279 /* ------------------------------------------------------------------------
280 * Descriptors parsing
283 static int uvc_parse_format(struct uvc_device *dev,
284 struct uvc_streaming *streaming, struct uvc_format *format,
285 __u32 **intervals, unsigned char *buffer, int buflen)
287 struct usb_interface *intf = streaming->intf;
288 struct usb_host_interface *alts = intf->cur_altsetting;
289 struct uvc_format_desc *fmtdesc;
290 struct uvc_frame *frame;
291 const unsigned char *start = buffer;
292 unsigned int interval;
296 format->type = buffer[2];
297 format->index = buffer[3];
300 case UVC_VS_FORMAT_UNCOMPRESSED:
301 case UVC_VS_FORMAT_FRAME_BASED:
302 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
304 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
305 "interface %d FORMAT error\n",
307 alts->desc.bInterfaceNumber);
311 /* Find the format descriptor from its GUID. */
312 fmtdesc = uvc_format_by_guid(&buffer[5]);
314 if (fmtdesc != NULL) {
315 strlcpy(format->name, fmtdesc->name,
316 sizeof format->name);
317 format->fcc = fmtdesc->fcc;
319 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
321 snprintf(format->name, sizeof(format->name), "%pUl\n",
326 format->bpp = buffer[21];
327 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
328 ftype = UVC_VS_FRAME_UNCOMPRESSED;
330 ftype = UVC_VS_FRAME_FRAME_BASED;
332 format->flags = UVC_FMT_FLAG_COMPRESSED;
336 case UVC_VS_FORMAT_MJPEG:
338 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
339 "interface %d FORMAT error\n",
341 alts->desc.bInterfaceNumber);
345 strlcpy(format->name, "MJPEG", sizeof format->name);
346 format->fcc = V4L2_PIX_FMT_MJPEG;
347 format->flags = UVC_FMT_FLAG_COMPRESSED;
349 ftype = UVC_VS_FRAME_MJPEG;
352 case UVC_VS_FORMAT_DV:
354 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
355 "interface %d FORMAT error\n",
357 alts->desc.bInterfaceNumber);
361 switch (buffer[8] & 0x7f) {
363 strlcpy(format->name, "SD-DV", sizeof format->name);
366 strlcpy(format->name, "SDL-DV", sizeof format->name);
369 strlcpy(format->name, "HD-DV", sizeof format->name);
372 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
373 "interface %d: unknown DV format %u\n",
375 alts->desc.bInterfaceNumber, buffer[8]);
379 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
380 sizeof format->name);
382 format->fcc = V4L2_PIX_FMT_DV;
383 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
387 /* Create a dummy frame descriptor. */
388 frame = &format->frame[0];
389 memset(&format->frame[0], 0, sizeof format->frame[0]);
390 frame->bFrameIntervalType = 1;
391 frame->dwDefaultFrameInterval = 1;
392 frame->dwFrameInterval = *intervals;
397 case UVC_VS_FORMAT_MPEG2TS:
398 case UVC_VS_FORMAT_STREAM_BASED:
399 /* Not supported yet. */
401 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
402 "interface %d unsupported format %u\n",
403 dev->udev->devnum, alts->desc.bInterfaceNumber,
408 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
413 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
414 * based formats have frame descriptors.
416 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
417 buffer[2] == ftype) {
418 frame = &format->frame[format->nframes];
419 if (ftype != UVC_VS_FRAME_FRAME_BASED)
420 n = buflen > 25 ? buffer[25] : 0;
422 n = buflen > 21 ? buffer[21] : 0;
426 if (buflen < 26 + 4*n) {
427 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
428 "interface %d FRAME error\n", dev->udev->devnum,
429 alts->desc.bInterfaceNumber);
433 frame->bFrameIndex = buffer[3];
434 frame->bmCapabilities = buffer[4];
435 frame->wWidth = get_unaligned_le16(&buffer[5]);
436 frame->wHeight = get_unaligned_le16(&buffer[7]);
437 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
438 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
439 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
440 frame->dwMaxVideoFrameBufferSize =
441 get_unaligned_le32(&buffer[17]);
442 frame->dwDefaultFrameInterval =
443 get_unaligned_le32(&buffer[21]);
444 frame->bFrameIntervalType = buffer[25];
446 frame->dwMaxVideoFrameBufferSize = 0;
447 frame->dwDefaultFrameInterval =
448 get_unaligned_le32(&buffer[17]);
449 frame->bFrameIntervalType = buffer[21];
451 frame->dwFrameInterval = *intervals;
453 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454 * completely. Observed behaviours range from setting the
455 * value to 1.1x the actual frame size to hardwiring the
456 * 16 low bits to 0. This results in a higher than necessary
457 * memory usage as well as a wrong image size information. For
458 * uncompressed formats this can be fixed by computing the
459 * value from the frame size.
461 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
462 frame->dwMaxVideoFrameBufferSize = format->bpp
463 * frame->wWidth * frame->wHeight / 8;
465 /* Some bogus devices report dwMinFrameInterval equal to
466 * dwMaxFrameInterval and have dwFrameIntervalStep set to
467 * zero. Setting all null intervals to 1 fixes the problem and
468 * some other divisions by zero that could happen.
470 for (i = 0; i < n; ++i) {
471 interval = get_unaligned_le32(&buffer[26+4*i]);
472 *(*intervals)++ = interval ? interval : 1;
475 /* Make sure that the default frame interval stays between
478 n -= frame->bFrameIntervalType ? 1 : 2;
479 frame->dwDefaultFrameInterval =
480 min(frame->dwFrameInterval[n],
481 max(frame->dwFrameInterval[0],
482 frame->dwDefaultFrameInterval));
484 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
485 frame->wWidth, frame->wHeight,
486 10000000/frame->dwDefaultFrameInterval,
487 (100000000/frame->dwDefaultFrameInterval)%10);
494 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
495 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
500 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
501 buffer[2] == UVC_VS_COLORFORMAT) {
503 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
504 "interface %d COLORFORMAT error\n",
506 alts->desc.bInterfaceNumber);
510 format->colorspace = uvc_colorspace(buffer[3]);
516 return buffer - start;
519 static int uvc_parse_streaming(struct uvc_device *dev,
520 struct usb_interface *intf)
522 struct uvc_streaming *streaming = NULL;
523 struct uvc_format *format;
524 struct uvc_frame *frame;
525 struct usb_host_interface *alts = &intf->altsetting[0];
526 unsigned char *_buffer, *buffer = alts->extra;
527 int _buflen, buflen = alts->extralen;
528 unsigned int nformats = 0, nframes = 0, nintervals = 0;
529 unsigned int size, i, n, p;
534 if (intf->cur_altsetting->desc.bInterfaceSubClass
535 != UVC_SC_VIDEOSTREAMING) {
536 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
537 "video streaming interface\n", dev->udev->devnum,
538 intf->altsetting[0].desc.bInterfaceNumber);
542 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
543 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
544 "claimed\n", dev->udev->devnum,
545 intf->altsetting[0].desc.bInterfaceNumber);
549 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
550 if (streaming == NULL) {
551 usb_driver_release_interface(&uvc_driver.driver, intf);
555 mutex_init(&streaming->mutex);
556 streaming->dev = dev;
557 streaming->intf = usb_get_intf(intf);
558 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
560 /* The Pico iMage webcam has its class-specific interface descriptors
561 * after the endpoint descriptors.
564 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
565 struct usb_host_endpoint *ep = &alts->endpoint[i];
567 if (ep->extralen == 0)
570 if (ep->extralen > 2 &&
571 ep->extra[1] == USB_DT_CS_INTERFACE) {
572 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
573 "from endpoint %u.\n", i);
574 buffer = alts->endpoint[i].extra;
575 buflen = alts->endpoint[i].extralen;
581 /* Skip the standard interface descriptors. */
582 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
588 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
589 "interface descriptors found.\n");
593 /* Parse the header descriptor. */
595 case UVC_VS_OUTPUT_HEADER:
596 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
600 case UVC_VS_INPUT_HEADER:
601 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
606 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
607 "%d HEADER descriptor not found.\n", dev->udev->devnum,
608 alts->desc.bInterfaceNumber);
612 p = buflen >= 4 ? buffer[3] : 0;
613 n = buflen >= size ? buffer[size-1] : 0;
615 if (buflen < size + p*n) {
616 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
617 "interface %d HEADER descriptor is invalid.\n",
618 dev->udev->devnum, alts->desc.bInterfaceNumber);
622 streaming->header.bNumFormats = p;
623 streaming->header.bEndpointAddress = buffer[6];
624 if (buffer[2] == UVC_VS_INPUT_HEADER) {
625 streaming->header.bmInfo = buffer[7];
626 streaming->header.bTerminalLink = buffer[8];
627 streaming->header.bStillCaptureMethod = buffer[9];
628 streaming->header.bTriggerSupport = buffer[10];
629 streaming->header.bTriggerUsage = buffer[11];
631 streaming->header.bTerminalLink = buffer[7];
633 streaming->header.bControlSize = n;
635 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
636 if (streaming->header.bmaControls == NULL) {
641 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
649 /* Count the format and frame descriptors. */
650 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
651 switch (_buffer[2]) {
652 case UVC_VS_FORMAT_UNCOMPRESSED:
653 case UVC_VS_FORMAT_MJPEG:
654 case UVC_VS_FORMAT_FRAME_BASED:
658 case UVC_VS_FORMAT_DV:
659 /* DV format has no frame descriptor. We will create a
660 * dummy frame descriptor with a dummy frame interval.
667 case UVC_VS_FORMAT_MPEG2TS:
668 case UVC_VS_FORMAT_STREAM_BASED:
669 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
670 "interface %d FORMAT %u is not supported.\n",
672 alts->desc.bInterfaceNumber, _buffer[2]);
675 case UVC_VS_FRAME_UNCOMPRESSED:
676 case UVC_VS_FRAME_MJPEG:
679 nintervals += _buffer[25] ? _buffer[25] : 3;
682 case UVC_VS_FRAME_FRAME_BASED:
685 nintervals += _buffer[21] ? _buffer[21] : 3;
689 _buflen -= _buffer[0];
690 _buffer += _buffer[0];
694 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
695 "%d has no supported formats defined.\n",
696 dev->udev->devnum, alts->desc.bInterfaceNumber);
700 size = nformats * sizeof *format + nframes * sizeof *frame
701 + nintervals * sizeof *interval;
702 format = kzalloc(size, GFP_KERNEL);
703 if (format == NULL) {
708 frame = (struct uvc_frame *)&format[nformats];
709 interval = (__u32 *)&frame[nframes];
711 streaming->format = format;
712 streaming->nformats = nformats;
714 /* Parse the format descriptors. */
715 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
717 case UVC_VS_FORMAT_UNCOMPRESSED:
718 case UVC_VS_FORMAT_MJPEG:
719 case UVC_VS_FORMAT_DV:
720 case UVC_VS_FORMAT_FRAME_BASED:
721 format->frame = frame;
722 ret = uvc_parse_format(dev, streaming, format,
723 &interval, buffer, buflen);
727 frame += format->nframes;
743 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
744 "%d has %u bytes of trailing descriptor garbage.\n",
745 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
747 /* Parse the alternate settings to find the maximum bandwidth. */
748 for (i = 0; i < intf->num_altsetting; ++i) {
749 struct usb_host_endpoint *ep;
750 alts = &intf->altsetting[i];
751 ep = uvc_find_endpoint(alts,
752 streaming->header.bEndpointAddress);
756 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
757 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
758 if (psize > streaming->maxpsize)
759 streaming->maxpsize = psize;
762 list_add_tail(&streaming->list, &dev->streams);
766 usb_driver_release_interface(&uvc_driver.driver, intf);
768 kfree(streaming->format);
769 kfree(streaming->header.bmaControls);
774 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
775 unsigned int num_pads, unsigned int extra_size)
777 struct uvc_entity *entity;
778 unsigned int num_inputs;
781 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
782 size = sizeof(*entity) + extra_size + num_inputs;
783 entity = kzalloc(size, GFP_KERNEL);
790 entity->bNrInPins = num_inputs;
791 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
796 /* Parse vendor-specific extensions. */
797 static int uvc_parse_vendor_control(struct uvc_device *dev,
798 const unsigned char *buffer, int buflen)
800 struct usb_device *udev = dev->udev;
801 struct usb_host_interface *alts = dev->intf->cur_altsetting;
802 struct uvc_entity *unit;
806 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
807 case 0x046d: /* Logitech */
808 if (buffer[1] != 0x41 || buffer[2] != 0x01)
811 /* Logitech implements several vendor specific functions
812 * through vendor specific extension units (LXU).
814 * The LXU descriptors are similar to XU descriptors
815 * (see "USB Device Video Class for Video Devices", section
816 * 3.7.2.6 "Extension Unit Descriptor") with the following
819 * ----------------------------------------------------------
821 * Size of this descriptor, in bytes: 24+p+n*2
822 * ----------------------------------------------------------
823 * 23+p+n bmControlsType N Bitmap
824 * Individual bits in the set are defined:
828 * This bitset is mapped exactly the same as bmControls.
829 * ----------------------------------------------------------
830 * 23+p+n*2 bReserved 1 Boolean
831 * ----------------------------------------------------------
832 * 24+p+n*2 iExtension 1 Index
833 * Index of a string descriptor that describes this
835 * ----------------------------------------------------------
837 p = buflen >= 22 ? buffer[21] : 0;
838 n = buflen >= 25 + p ? buffer[22+p] : 0;
840 if (buflen < 25 + p + 2*n) {
841 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
842 "interface %d EXTENSION_UNIT error\n",
843 udev->devnum, alts->desc.bInterfaceNumber);
847 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
852 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
853 unit->extension.bNumControls = buffer[20];
854 memcpy(unit->baSourceID, &buffer[22], p);
855 unit->extension.bControlSize = buffer[22+p];
856 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
857 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
859 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
861 if (buffer[24+p+2*n] != 0)
862 usb_string(udev, buffer[24+p+2*n], unit->name,
865 sprintf(unit->name, "Extension %u", buffer[3]);
867 list_add_tail(&unit->list, &dev->entities);
875 static int uvc_parse_standard_control(struct uvc_device *dev,
876 const unsigned char *buffer, int buflen)
878 struct usb_device *udev = dev->udev;
879 struct uvc_entity *unit, *term;
880 struct usb_interface *intf;
881 struct usb_host_interface *alts = dev->intf->cur_altsetting;
882 unsigned int i, n, p, len;
887 n = buflen >= 12 ? buffer[11] : 0;
889 if (buflen < 12 || buflen < 12 + n) {
890 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
891 "interface %d HEADER error\n", udev->devnum,
892 alts->desc.bInterfaceNumber);
896 dev->uvc_version = get_unaligned_le16(&buffer[3]);
897 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
899 /* Parse all USB Video Streaming interfaces. */
900 for (i = 0; i < n; ++i) {
901 intf = usb_ifnum_to_if(udev, buffer[12+i]);
903 uvc_trace(UVC_TRACE_DESCR, "device %d "
904 "interface %d doesn't exists\n",
909 uvc_parse_streaming(dev, intf);
913 case UVC_VC_INPUT_TERMINAL:
915 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
916 "interface %d INPUT_TERMINAL error\n",
917 udev->devnum, alts->desc.bInterfaceNumber);
921 /* Make sure the terminal type MSB is not null, otherwise it
922 * could be confused with a unit.
924 type = get_unaligned_le16(&buffer[4]);
925 if ((type & 0xff00) == 0) {
926 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
927 "interface %d INPUT_TERMINAL %d has invalid "
928 "type 0x%04x, skipping\n", udev->devnum,
929 alts->desc.bInterfaceNumber,
938 if (type == UVC_ITT_CAMERA) {
939 n = buflen >= 15 ? buffer[14] : 0;
942 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
943 n = buflen >= 9 ? buffer[8] : 0;
944 p = buflen >= 10 + n ? buffer[9+n] : 0;
948 if (buflen < len + n + p) {
949 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
950 "interface %d INPUT_TERMINAL error\n",
951 udev->devnum, alts->desc.bInterfaceNumber);
955 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
960 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
961 term->camera.bControlSize = n;
962 term->camera.bmControls = (__u8 *)term + sizeof *term;
963 term->camera.wObjectiveFocalLengthMin =
964 get_unaligned_le16(&buffer[8]);
965 term->camera.wObjectiveFocalLengthMax =
966 get_unaligned_le16(&buffer[10]);
967 term->camera.wOcularFocalLength =
968 get_unaligned_le16(&buffer[12]);
969 memcpy(term->camera.bmControls, &buffer[15], n);
970 } else if (UVC_ENTITY_TYPE(term) ==
971 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
972 term->media.bControlSize = n;
973 term->media.bmControls = (__u8 *)term + sizeof *term;
974 term->media.bTransportModeSize = p;
975 term->media.bmTransportModes = (__u8 *)term
977 memcpy(term->media.bmControls, &buffer[9], n);
978 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
982 usb_string(udev, buffer[7], term->name,
984 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
985 sprintf(term->name, "Camera %u", buffer[3]);
986 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
987 sprintf(term->name, "Media %u", buffer[3]);
989 sprintf(term->name, "Input %u", buffer[3]);
991 list_add_tail(&term->list, &dev->entities);
994 case UVC_VC_OUTPUT_TERMINAL:
996 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
997 "interface %d OUTPUT_TERMINAL error\n",
998 udev->devnum, alts->desc.bInterfaceNumber);
1002 /* Make sure the terminal type MSB is not null, otherwise it
1003 * could be confused with a unit.
1005 type = get_unaligned_le16(&buffer[4]);
1006 if ((type & 0xff00) == 0) {
1007 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1008 "interface %d OUTPUT_TERMINAL %d has invalid "
1009 "type 0x%04x, skipping\n", udev->devnum,
1010 alts->desc.bInterfaceNumber, buffer[3], type);
1014 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1019 memcpy(term->baSourceID, &buffer[7], 1);
1022 usb_string(udev, buffer[8], term->name,
1025 sprintf(term->name, "Output %u", buffer[3]);
1027 list_add_tail(&term->list, &dev->entities);
1030 case UVC_VC_SELECTOR_UNIT:
1031 p = buflen >= 5 ? buffer[4] : 0;
1033 if (buflen < 5 || buflen < 6 + p) {
1034 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1035 "interface %d SELECTOR_UNIT error\n",
1036 udev->devnum, alts->desc.bInterfaceNumber);
1040 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1044 memcpy(unit->baSourceID, &buffer[5], p);
1046 if (buffer[5+p] != 0)
1047 usb_string(udev, buffer[5+p], unit->name,
1050 sprintf(unit->name, "Selector %u", buffer[3]);
1052 list_add_tail(&unit->list, &dev->entities);
1055 case UVC_VC_PROCESSING_UNIT:
1056 n = buflen >= 8 ? buffer[7] : 0;
1057 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1059 if (buflen < p + n) {
1060 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1061 "interface %d PROCESSING_UNIT error\n",
1062 udev->devnum, alts->desc.bInterfaceNumber);
1066 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1070 memcpy(unit->baSourceID, &buffer[4], 1);
1071 unit->processing.wMaxMultiplier =
1072 get_unaligned_le16(&buffer[5]);
1073 unit->processing.bControlSize = buffer[7];
1074 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1075 memcpy(unit->processing.bmControls, &buffer[8], n);
1076 if (dev->uvc_version >= 0x0110)
1077 unit->processing.bmVideoStandards = buffer[9+n];
1079 if (buffer[8+n] != 0)
1080 usb_string(udev, buffer[8+n], unit->name,
1083 sprintf(unit->name, "Processing %u", buffer[3]);
1085 list_add_tail(&unit->list, &dev->entities);
1088 case UVC_VC_EXTENSION_UNIT:
1089 p = buflen >= 22 ? buffer[21] : 0;
1090 n = buflen >= 24 + p ? buffer[22+p] : 0;
1092 if (buflen < 24 + p + n) {
1093 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1094 "interface %d EXTENSION_UNIT error\n",
1095 udev->devnum, alts->desc.bInterfaceNumber);
1099 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1103 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1104 unit->extension.bNumControls = buffer[20];
1105 memcpy(unit->baSourceID, &buffer[22], p);
1106 unit->extension.bControlSize = buffer[22+p];
1107 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1108 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1110 if (buffer[23+p+n] != 0)
1111 usb_string(udev, buffer[23+p+n], unit->name,
1114 sprintf(unit->name, "Extension %u", buffer[3]);
1116 list_add_tail(&unit->list, &dev->entities);
1120 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1121 "descriptor (%u)\n", buffer[2]);
1128 static int uvc_parse_control(struct uvc_device *dev)
1130 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1131 unsigned char *buffer = alts->extra;
1132 int buflen = alts->extralen;
1135 /* Parse the default alternate setting only, as the UVC specification
1136 * defines a single alternate setting, the default alternate setting
1140 while (buflen > 2) {
1141 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1142 buffer[1] != USB_DT_CS_INTERFACE)
1143 goto next_descriptor;
1145 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1149 buflen -= buffer[0];
1150 buffer += buffer[0];
1153 /* Check if the optional status endpoint is present. Built-in iSight
1154 * webcams have an interrupt endpoint but spit proprietary data that
1155 * don't conform to the UVC status endpoint messages. Don't try to
1156 * handle the interrupt endpoint for those cameras.
1158 if (alts->desc.bNumEndpoints == 1 &&
1159 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1160 struct usb_host_endpoint *ep = &alts->endpoint[0];
1161 struct usb_endpoint_descriptor *desc = &ep->desc;
1163 if (usb_endpoint_is_int_in(desc) &&
1164 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1165 desc->bInterval != 0) {
1166 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1167 "(addr %02x).\n", desc->bEndpointAddress);
1175 /* ------------------------------------------------------------------------
1180 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1181 * and containing the following units:
1183 * - one or more Output Terminals (USB Streaming or Display)
1184 * - zero or one Processing Unit
1185 * - zero, one or more single-input Selector Units
1186 * - zero or one multiple-input Selector Units, provided all inputs are
1187 * connected to input terminals
1188 * - zero, one or mode single-input Extension Units
1189 * - one or more Input Terminals (Camera, External or USB Streaming)
1191 * The terminal and units must match on of the following structures:
1193 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1194 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1195 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1197 * +---------+ +---------+ -> OTT_*(0)
1198 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1199 * +---------+ +---------+ -> OTT_*(n)
1201 * The Processing Unit and Extension Units can be in any order. Additional
1202 * Extension Units connected to the main chain as single-unit branches are
1203 * also supported. Single-input Selector Units are ignored.
1205 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1206 struct uvc_entity *entity)
1208 switch (UVC_ENTITY_TYPE(entity)) {
1209 case UVC_VC_EXTENSION_UNIT:
1210 if (uvc_trace_param & UVC_TRACE_PROBE)
1211 printk(" <- XU %d", entity->id);
1213 if (entity->bNrInPins != 1) {
1214 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1215 "than 1 input pin.\n", entity->id);
1221 case UVC_VC_PROCESSING_UNIT:
1222 if (uvc_trace_param & UVC_TRACE_PROBE)
1223 printk(" <- PU %d", entity->id);
1225 if (chain->processing != NULL) {
1226 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1227 "Processing Units in chain.\n");
1231 chain->processing = entity;
1234 case UVC_VC_SELECTOR_UNIT:
1235 if (uvc_trace_param & UVC_TRACE_PROBE)
1236 printk(" <- SU %d", entity->id);
1238 /* Single-input selector units are ignored. */
1239 if (entity->bNrInPins == 1)
1242 if (chain->selector != NULL) {
1243 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1244 "Units in chain.\n");
1248 chain->selector = entity;
1251 case UVC_ITT_VENDOR_SPECIFIC:
1252 case UVC_ITT_CAMERA:
1253 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1254 if (uvc_trace_param & UVC_TRACE_PROBE)
1255 printk(" <- IT %d\n", entity->id);
1259 case UVC_TT_STREAMING:
1260 if (UVC_ENTITY_IS_ITERM(entity)) {
1261 if (uvc_trace_param & UVC_TRACE_PROBE)
1262 printk(" <- IT %d\n", entity->id);
1264 if (uvc_trace_param & UVC_TRACE_PROBE)
1265 printk(" OT %d", entity->id);
1271 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1272 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1276 list_add_tail(&entity->chain, &chain->entities);
1280 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1281 struct uvc_entity *entity, struct uvc_entity *prev)
1283 struct uvc_entity *forward;
1291 forward = uvc_entity_by_reference(chain->dev, entity->id,
1293 if (forward == NULL)
1295 if (forward == prev)
1298 switch (UVC_ENTITY_TYPE(forward)) {
1299 case UVC_VC_EXTENSION_UNIT:
1300 if (forward->bNrInPins != 1) {
1301 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1302 "has more than 1 input pin.\n",
1307 list_add_tail(&forward->chain, &chain->entities);
1308 if (uvc_trace_param & UVC_TRACE_PROBE) {
1312 printk(" XU %d", forward->id);
1317 case UVC_OTT_VENDOR_SPECIFIC:
1318 case UVC_OTT_DISPLAY:
1319 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1320 case UVC_TT_STREAMING:
1321 if (UVC_ENTITY_IS_ITERM(forward)) {
1322 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1323 "terminal %u.\n", forward->id);
1327 list_add_tail(&forward->chain, &chain->entities);
1328 if (uvc_trace_param & UVC_TRACE_PROBE) {
1332 printk(" OT %d", forward->id);
1344 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1345 struct uvc_entity **_entity)
1347 struct uvc_entity *entity = *_entity;
1348 struct uvc_entity *term;
1349 int id = -EINVAL, i;
1351 switch (UVC_ENTITY_TYPE(entity)) {
1352 case UVC_VC_EXTENSION_UNIT:
1353 case UVC_VC_PROCESSING_UNIT:
1354 id = entity->baSourceID[0];
1357 case UVC_VC_SELECTOR_UNIT:
1358 /* Single-input selector units are ignored. */
1359 if (entity->bNrInPins == 1) {
1360 id = entity->baSourceID[0];
1364 if (uvc_trace_param & UVC_TRACE_PROBE)
1367 chain->selector = entity;
1368 for (i = 0; i < entity->bNrInPins; ++i) {
1369 id = entity->baSourceID[i];
1370 term = uvc_entity_by_id(chain->dev, id);
1371 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1372 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1373 "input %d isn't connected to an "
1374 "input terminal\n", entity->id, i);
1378 if (uvc_trace_param & UVC_TRACE_PROBE)
1379 printk(" %d", term->id);
1381 list_add_tail(&term->chain, &chain->entities);
1382 uvc_scan_chain_forward(chain, term, entity);
1385 if (uvc_trace_param & UVC_TRACE_PROBE)
1391 case UVC_ITT_VENDOR_SPECIFIC:
1392 case UVC_ITT_CAMERA:
1393 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1394 case UVC_OTT_VENDOR_SPECIFIC:
1395 case UVC_OTT_DISPLAY:
1396 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1397 case UVC_TT_STREAMING:
1398 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1407 entity = uvc_entity_by_id(chain->dev, id);
1408 if (entity == NULL) {
1409 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1410 "unknown entity %d.\n", id);
1418 static int uvc_scan_chain(struct uvc_video_chain *chain,
1419 struct uvc_entity *term)
1421 struct uvc_entity *entity, *prev;
1423 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1428 while (entity != NULL) {
1429 /* Entity must not be part of an existing chain */
1430 if (entity->chain.next || entity->chain.prev) {
1431 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1432 "entity %d already in chain.\n", entity->id);
1436 /* Process entity */
1437 if (uvc_scan_chain_entity(chain, entity) < 0)
1441 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1446 if (uvc_scan_chain_backward(chain, &entity) < 0)
1453 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1456 struct uvc_entity *term;
1457 unsigned int nterms = 0;
1460 list_for_each_entry(term, terms, chain) {
1461 if (!UVC_ENTITY_IS_TERM(term) ||
1462 UVC_TERM_DIRECTION(term) != dir)
1466 p += sprintf(p, ",");
1467 if (++nterms >= 4) {
1468 p += sprintf(p, "...");
1471 p += sprintf(p, "%u", term->id);
1477 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1479 static char buffer[43];
1482 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1483 p += sprintf(p, " -> ");
1484 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1490 * Scan the device for video chains and register video devices.
1492 * Chains are scanned starting at their output terminals and walked backwards.
1494 static int uvc_scan_device(struct uvc_device *dev)
1496 struct uvc_video_chain *chain;
1497 struct uvc_entity *term;
1499 list_for_each_entry(term, &dev->entities, list) {
1500 if (!UVC_ENTITY_IS_OTERM(term))
1503 /* If the terminal is already included in a chain, skip it.
1504 * This can happen for chains that have multiple output
1505 * terminals, where all output terminals beside the first one
1506 * will be inserted in the chain in forward scans.
1508 if (term->chain.next || term->chain.prev)
1511 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1515 INIT_LIST_HEAD(&chain->entities);
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;
1660 vdev->fops = &uvc_fops;
1661 vdev->release = uvc_release;
1662 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1664 /* Set the driver data before calling video_register_device, otherwise
1665 * uvc_v4l2_open might race us.
1667 stream->vdev = vdev;
1668 video_set_drvdata(vdev, stream);
1670 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1672 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1674 stream->vdev = NULL;
1675 video_device_release(vdev);
1679 atomic_inc(&dev->nstreams);
1684 * Register all video devices in all chains.
1686 static int uvc_register_terms(struct uvc_device *dev,
1687 struct uvc_video_chain *chain)
1689 struct uvc_streaming *stream;
1690 struct uvc_entity *term;
1693 list_for_each_entry(term, &chain->entities, chain) {
1694 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1697 stream = uvc_stream_by_id(dev, term->id);
1698 if (stream == NULL) {
1699 uvc_printk(KERN_INFO, "No streaming interface found "
1700 "for terminal %u.", term->id);
1704 stream->chain = chain;
1705 ret = uvc_register_video(dev, stream);
1713 static int uvc_register_chains(struct uvc_device *dev)
1715 struct uvc_video_chain *chain;
1718 list_for_each_entry(chain, &dev->chains, list) {
1719 ret = uvc_register_terms(dev, chain);
1727 /* ------------------------------------------------------------------------
1728 * USB probe, disconnect, suspend and resume
1731 static int uvc_probe(struct usb_interface *intf,
1732 const struct usb_device_id *id)
1734 struct usb_device *udev = interface_to_usbdev(intf);
1735 struct uvc_device *dev;
1738 if (id->idVendor && id->idProduct)
1739 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1740 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1743 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1746 /* Allocate memory for the device and initialize it. */
1747 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1750 INIT_LIST_HEAD(&dev->entities);
1751 INIT_LIST_HEAD(&dev->chains);
1752 INIT_LIST_HEAD(&dev->streams);
1753 atomic_set(&dev->nstreams, 0);
1754 atomic_set(&dev->users, 0);
1756 dev->udev = usb_get_dev(udev);
1757 dev->intf = usb_get_intf(intf);
1758 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1759 dev->quirks = (uvc_quirks_param == -1)
1760 ? id->driver_info : uvc_quirks_param;
1762 if (udev->product != NULL)
1763 strlcpy(dev->name, udev->product, sizeof dev->name);
1765 snprintf(dev->name, sizeof dev->name,
1766 "UVC Camera (%04x:%04x)",
1767 le16_to_cpu(udev->descriptor.idVendor),
1768 le16_to_cpu(udev->descriptor.idProduct));
1770 /* Parse the Video Class control descriptor. */
1771 if (uvc_parse_control(dev) < 0) {
1772 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1777 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1778 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1779 udev->product ? udev->product : "<unnamed>",
1780 le16_to_cpu(udev->descriptor.idVendor),
1781 le16_to_cpu(udev->descriptor.idProduct));
1783 if (dev->quirks != id->driver_info) {
1784 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1785 "parameter for testing purpose.\n", dev->quirks);
1786 uvc_printk(KERN_INFO, "Please report required quirks to the "
1787 "linux-uvc-devel mailing list.\n");
1790 /* Initialize controls. */
1791 if (uvc_ctrl_init_device(dev) < 0)
1794 /* Scan the device for video chains. */
1795 if (uvc_scan_device(dev) < 0)
1798 /* Register video devices. */
1799 if (uvc_register_chains(dev) < 0)
1802 /* Save our data pointer in the interface data. */
1803 usb_set_intfdata(intf, dev);
1805 /* Initialize the interrupt URB. */
1806 if ((ret = uvc_status_init(dev)) < 0) {
1807 uvc_printk(KERN_INFO, "Unable to initialize the status "
1808 "endpoint (%d), status interrupt will not be "
1809 "supported.\n", ret);
1812 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1816 uvc_unregister_video(dev);
1820 static void uvc_disconnect(struct usb_interface *intf)
1822 struct uvc_device *dev = usb_get_intfdata(intf);
1824 /* Set the USB interface data to NULL. This can be done outside the
1825 * lock, as there's no other reader.
1827 usb_set_intfdata(intf, NULL);
1829 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1830 UVC_SC_VIDEOSTREAMING)
1833 dev->state |= UVC_DEV_DISCONNECTED;
1835 uvc_unregister_video(dev);
1838 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1840 struct uvc_device *dev = usb_get_intfdata(intf);
1841 struct uvc_streaming *stream;
1843 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1844 intf->cur_altsetting->desc.bInterfaceNumber);
1846 /* Controls are cached on the fly so they don't need to be saved. */
1847 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1848 UVC_SC_VIDEOCONTROL)
1849 return uvc_status_suspend(dev);
1851 list_for_each_entry(stream, &dev->streams, list) {
1852 if (stream->intf == intf)
1853 return uvc_video_suspend(stream);
1856 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1861 static int __uvc_resume(struct usb_interface *intf, int reset)
1863 struct uvc_device *dev = usb_get_intfdata(intf);
1864 struct uvc_streaming *stream;
1866 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1867 intf->cur_altsetting->desc.bInterfaceNumber);
1869 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1870 UVC_SC_VIDEOCONTROL) {
1872 int ret = uvc_ctrl_resume_device(dev);
1878 return uvc_status_resume(dev);
1881 list_for_each_entry(stream, &dev->streams, list) {
1882 if (stream->intf == intf)
1883 return uvc_video_resume(stream);
1886 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1891 static int uvc_resume(struct usb_interface *intf)
1893 return __uvc_resume(intf, 0);
1896 static int uvc_reset_resume(struct usb_interface *intf)
1898 return __uvc_resume(intf, 1);
1901 /* ------------------------------------------------------------------------
1905 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1907 if (uvc_clock_param == CLOCK_MONOTONIC)
1908 return sprintf(buffer, "CLOCK_MONOTONIC");
1910 return sprintf(buffer, "CLOCK_REALTIME");
1913 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1915 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1916 val += strlen("clock_");
1918 if (strcasecmp(val, "monotonic") == 0)
1919 uvc_clock_param = CLOCK_MONOTONIC;
1920 else if (strcasecmp(val, "realtime") == 0)
1921 uvc_clock_param = CLOCK_REALTIME;
1928 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1929 &uvc_clock_param, S_IRUGO|S_IWUSR);
1930 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1931 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1932 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1933 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1934 MODULE_PARM_DESC(quirks, "Forced device quirks");
1935 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1936 MODULE_PARM_DESC(trace, "Trace level bitmask");
1937 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1938 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1940 /* ------------------------------------------------------------------------
1941 * Driver initialization and cleanup
1945 * The Logitech cameras listed below have their interface class set to
1946 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1947 * though they are compliant.
1949 static struct usb_device_id uvc_ids[] = {
1950 /* Genius eFace 2025 */
1951 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1952 | USB_DEVICE_ID_MATCH_INT_INFO,
1954 .idProduct = 0x706e,
1955 .bInterfaceClass = USB_CLASS_VIDEO,
1956 .bInterfaceSubClass = 1,
1957 .bInterfaceProtocol = 0,
1958 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1959 /* Microsoft Lifecam NX-6000 */
1960 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1961 | USB_DEVICE_ID_MATCH_INT_INFO,
1963 .idProduct = 0x00f8,
1964 .bInterfaceClass = USB_CLASS_VIDEO,
1965 .bInterfaceSubClass = 1,
1966 .bInterfaceProtocol = 0,
1967 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1968 /* Microsoft Lifecam VX-7000 */
1969 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1970 | USB_DEVICE_ID_MATCH_INT_INFO,
1972 .idProduct = 0x0723,
1973 .bInterfaceClass = USB_CLASS_VIDEO,
1974 .bInterfaceSubClass = 1,
1975 .bInterfaceProtocol = 0,
1976 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1977 /* Logitech Quickcam Fusion */
1978 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1979 | USB_DEVICE_ID_MATCH_INT_INFO,
1981 .idProduct = 0x08c1,
1982 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1983 .bInterfaceSubClass = 1,
1984 .bInterfaceProtocol = 0 },
1985 /* Logitech Quickcam Orbit MP */
1986 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1987 | USB_DEVICE_ID_MATCH_INT_INFO,
1989 .idProduct = 0x08c2,
1990 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1991 .bInterfaceSubClass = 1,
1992 .bInterfaceProtocol = 0 },
1993 /* Logitech Quickcam Pro for Notebook */
1994 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1995 | USB_DEVICE_ID_MATCH_INT_INFO,
1997 .idProduct = 0x08c3,
1998 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1999 .bInterfaceSubClass = 1,
2000 .bInterfaceProtocol = 0 },
2001 /* Logitech Quickcam Pro 5000 */
2002 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2003 | USB_DEVICE_ID_MATCH_INT_INFO,
2005 .idProduct = 0x08c5,
2006 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2007 .bInterfaceSubClass = 1,
2008 .bInterfaceProtocol = 0 },
2009 /* Logitech Quickcam OEM Dell Notebook */
2010 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2011 | USB_DEVICE_ID_MATCH_INT_INFO,
2013 .idProduct = 0x08c6,
2014 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2015 .bInterfaceSubClass = 1,
2016 .bInterfaceProtocol = 0 },
2017 /* Logitech Quickcam OEM Cisco VT Camera II */
2018 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2019 | USB_DEVICE_ID_MATCH_INT_INFO,
2021 .idProduct = 0x08c7,
2022 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2023 .bInterfaceSubClass = 1,
2024 .bInterfaceProtocol = 0 },
2025 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2026 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2027 | USB_DEVICE_ID_MATCH_INT_INFO,
2029 .idProduct = 0x3820,
2030 .bInterfaceClass = USB_CLASS_VIDEO,
2031 .bInterfaceSubClass = 1,
2032 .bInterfaceProtocol = 0,
2033 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2034 /* Apple Built-In iSight */
2035 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2036 | USB_DEVICE_ID_MATCH_INT_INFO,
2038 .idProduct = 0x8501,
2039 .bInterfaceClass = USB_CLASS_VIDEO,
2040 .bInterfaceSubClass = 1,
2041 .bInterfaceProtocol = 0,
2042 .driver_info = UVC_QUIRK_PROBE_MINMAX
2043 | UVC_QUIRK_BUILTIN_ISIGHT },
2044 /* Genesys Logic USB 2.0 PC Camera */
2045 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2046 | USB_DEVICE_ID_MATCH_INT_INFO,
2048 .idProduct = 0x0505,
2049 .bInterfaceClass = USB_CLASS_VIDEO,
2050 .bInterfaceSubClass = 1,
2051 .bInterfaceProtocol = 0,
2052 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2054 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2055 | USB_DEVICE_ID_MATCH_INT_INFO,
2057 .idProduct = 0x332d,
2058 .bInterfaceClass = USB_CLASS_VIDEO,
2059 .bInterfaceSubClass = 1,
2060 .bInterfaceProtocol = 0,
2061 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2062 /* ViMicro - Minoru3D */
2063 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2064 | USB_DEVICE_ID_MATCH_INT_INFO,
2066 .idProduct = 0x3410,
2067 .bInterfaceClass = USB_CLASS_VIDEO,
2068 .bInterfaceSubClass = 1,
2069 .bInterfaceProtocol = 0,
2070 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2071 /* ViMicro Venus - Minoru3D */
2072 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2073 | USB_DEVICE_ID_MATCH_INT_INFO,
2075 .idProduct = 0x3420,
2076 .bInterfaceClass = USB_CLASS_VIDEO,
2077 .bInterfaceSubClass = 1,
2078 .bInterfaceProtocol = 0,
2079 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2081 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2082 | USB_DEVICE_ID_MATCH_INT_INFO,
2084 .idProduct = 0x0004,
2085 .bInterfaceClass = USB_CLASS_VIDEO,
2086 .bInterfaceSubClass = 1,
2087 .bInterfaceProtocol = 0,
2088 .driver_info = UVC_QUIRK_PROBE_MINMAX
2089 | UVC_QUIRK_PROBE_DEF },
2090 /* Syntek (HP Spartan) */
2091 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2092 | USB_DEVICE_ID_MATCH_INT_INFO,
2094 .idProduct = 0x5212,
2095 .bInterfaceClass = USB_CLASS_VIDEO,
2096 .bInterfaceSubClass = 1,
2097 .bInterfaceProtocol = 0,
2098 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2099 /* Syntek (Samsung Q310) */
2100 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2101 | USB_DEVICE_ID_MATCH_INT_INFO,
2103 .idProduct = 0x5931,
2104 .bInterfaceClass = USB_CLASS_VIDEO,
2105 .bInterfaceSubClass = 1,
2106 .bInterfaceProtocol = 0,
2107 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2108 /* Syntek (Asus F9SG) */
2109 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2110 | USB_DEVICE_ID_MATCH_INT_INFO,
2112 .idProduct = 0x8a31,
2113 .bInterfaceClass = USB_CLASS_VIDEO,
2114 .bInterfaceSubClass = 1,
2115 .bInterfaceProtocol = 0,
2116 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2117 /* Syntek (Asus U3S) */
2118 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2119 | USB_DEVICE_ID_MATCH_INT_INFO,
2121 .idProduct = 0x8a33,
2122 .bInterfaceClass = USB_CLASS_VIDEO,
2123 .bInterfaceSubClass = 1,
2124 .bInterfaceProtocol = 0,
2125 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2126 /* Syntek (JAOtech Smart Terminal) */
2127 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2128 | USB_DEVICE_ID_MATCH_INT_INFO,
2130 .idProduct = 0x8a34,
2131 .bInterfaceClass = USB_CLASS_VIDEO,
2132 .bInterfaceSubClass = 1,
2133 .bInterfaceProtocol = 0,
2134 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2135 /* Lenovo Thinkpad SL400/SL500 */
2136 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2137 | USB_DEVICE_ID_MATCH_INT_INFO,
2139 .idProduct = 0x480b,
2140 .bInterfaceClass = USB_CLASS_VIDEO,
2141 .bInterfaceSubClass = 1,
2142 .bInterfaceProtocol = 0,
2143 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2144 /* Aveo Technology USB 2.0 Camera */
2145 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2146 | USB_DEVICE_ID_MATCH_INT_INFO,
2148 .idProduct = 0x0306,
2149 .bInterfaceClass = USB_CLASS_VIDEO,
2150 .bInterfaceSubClass = 1,
2151 .bInterfaceProtocol = 0,
2152 .driver_info = UVC_QUIRK_PROBE_MINMAX
2153 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2154 /* Ecamm Pico iMage */
2155 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2156 | USB_DEVICE_ID_MATCH_INT_INFO,
2158 .idProduct = 0xcafe,
2159 .bInterfaceClass = USB_CLASS_VIDEO,
2160 .bInterfaceSubClass = 1,
2161 .bInterfaceProtocol = 0,
2162 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2163 /* FSC WebCam V30S */
2164 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2165 | USB_DEVICE_ID_MATCH_INT_INFO,
2167 .idProduct = 0x3288,
2168 .bInterfaceClass = USB_CLASS_VIDEO,
2169 .bInterfaceSubClass = 1,
2170 .bInterfaceProtocol = 0,
2171 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2172 /* Bodelin ProScopeHR */
2173 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2174 | USB_DEVICE_ID_MATCH_DEV_HI
2175 | USB_DEVICE_ID_MATCH_INT_INFO,
2177 .idProduct = 0x1000,
2178 .bcdDevice_hi = 0x0126,
2179 .bInterfaceClass = USB_CLASS_VIDEO,
2180 .bInterfaceSubClass = 1,
2181 .bInterfaceProtocol = 0,
2182 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2183 /* MSI StarCam 370i */
2184 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2185 | USB_DEVICE_ID_MATCH_INT_INFO,
2187 .idProduct = 0x2951,
2188 .bInterfaceClass = USB_CLASS_VIDEO,
2189 .bInterfaceSubClass = 1,
2190 .bInterfaceProtocol = 0,
2191 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2192 /* SiGma Micro USB Web Camera */
2193 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2194 | USB_DEVICE_ID_MATCH_INT_INFO,
2196 .idProduct = 0x3000,
2197 .bInterfaceClass = USB_CLASS_VIDEO,
2198 .bInterfaceSubClass = 1,
2199 .bInterfaceProtocol = 0,
2200 .driver_info = UVC_QUIRK_PROBE_MINMAX
2201 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2202 /* Generic USB Video Class */
2203 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2207 MODULE_DEVICE_TABLE(usb, uvc_ids);
2209 struct uvc_driver uvc_driver = {
2213 .disconnect = uvc_disconnect,
2214 .suspend = uvc_suspend,
2215 .resume = uvc_resume,
2216 .reset_resume = uvc_reset_resume,
2217 .id_table = uvc_ids,
2218 .supports_autosuspend = 1,
2222 static int __init uvc_init(void)
2226 INIT_LIST_HEAD(&uvc_driver.devices);
2227 INIT_LIST_HEAD(&uvc_driver.controls);
2228 mutex_init(&uvc_driver.ctrl_mutex);
2232 result = usb_register(&uvc_driver.driver);
2234 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2238 static void __exit uvc_cleanup(void)
2240 usb_deregister(&uvc_driver.driver);
2243 module_init(uvc_init);
2244 module_exit(uvc_cleanup);
2246 MODULE_AUTHOR(DRIVER_AUTHOR);
2247 MODULE_DESCRIPTION(DRIVER_DESC);
2248 MODULE_LICENSE("GPL");
2249 MODULE_VERSION(DRIVER_VERSION);