2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
15 * This driver aims to support video input and ouput devices compliant with the
16 * 'USB Video Class' specification.
18 * The driver doesn't support the deprecated v4l1 interface. It implements the
19 * mmap capture method only, and doesn't do any image format conversion in
20 * software. If your user-space application doesn't support YUYV or MJPEG, fix
21 * it :-). Please note that the MJPEG data have been stripped from their
22 * Huffman tables (DHT marker), you will need to add it back if your JPEG
23 * codec can't handle MJPEG data.
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/videodev2.h>
31 #include <linux/vmalloc.h>
32 #include <linux/wait.h>
33 #include <asm/atomic.h>
34 #include <asm/unaligned.h>
36 #include <media/v4l2-common.h>
40 #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
41 #define DRIVER_DESC "USB Video Class driver"
42 #ifndef DRIVER_VERSION
43 #define DRIVER_VERSION "v0.1.0"
46 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
47 unsigned int uvc_no_drop_param;
48 static unsigned int uvc_quirks_param = -1;
49 unsigned int uvc_trace_param;
50 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
52 /* ------------------------------------------------------------------------
56 static struct uvc_format_desc uvc_fmts[] = {
58 .name = "YUV 4:2:2 (YUYV)",
59 .guid = UVC_GUID_FORMAT_YUY2,
60 .fcc = V4L2_PIX_FMT_YUYV,
63 .name = "YUV 4:2:2 (YUYV)",
64 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
65 .fcc = V4L2_PIX_FMT_YUYV,
68 .name = "YUV 4:2:0 (NV12)",
69 .guid = UVC_GUID_FORMAT_NV12,
70 .fcc = V4L2_PIX_FMT_NV12,
74 .guid = UVC_GUID_FORMAT_MJPEG,
75 .fcc = V4L2_PIX_FMT_MJPEG,
78 .name = "YVU 4:2:0 (YV12)",
79 .guid = UVC_GUID_FORMAT_YV12,
80 .fcc = V4L2_PIX_FMT_YVU420,
83 .name = "YUV 4:2:0 (I420)",
84 .guid = UVC_GUID_FORMAT_I420,
85 .fcc = V4L2_PIX_FMT_YUV420,
88 .name = "YUV 4:2:2 (UYVY)",
89 .guid = UVC_GUID_FORMAT_UYVY,
90 .fcc = V4L2_PIX_FMT_UYVY,
94 .guid = UVC_GUID_FORMAT_Y800,
95 .fcc = V4L2_PIX_FMT_GREY,
99 .guid = UVC_GUID_FORMAT_BY8,
100 .fcc = V4L2_PIX_FMT_SBGGR8,
104 /* ------------------------------------------------------------------------
108 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
111 struct usb_host_endpoint *ep;
114 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
115 ep = &alts->endpoint[i];
116 if (ep->desc.bEndpointAddress == epaddr)
123 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
125 unsigned int len = ARRAY_SIZE(uvc_fmts);
128 for (i = 0; i < len; ++i) {
129 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
136 static __u32 uvc_colorspace(const __u8 primaries)
138 static const __u8 colorprimaries[] = {
140 V4L2_COLORSPACE_SRGB,
141 V4L2_COLORSPACE_470_SYSTEM_M,
142 V4L2_COLORSPACE_470_SYSTEM_BG,
143 V4L2_COLORSPACE_SMPTE170M,
144 V4L2_COLORSPACE_SMPTE240M,
147 if (primaries < ARRAY_SIZE(colorprimaries))
148 return colorprimaries[primaries];
153 /* Simplify a fraction using a simple continued fraction decomposition. The
154 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
155 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
156 * arbitrary parameters to remove non-significative terms from the simple
157 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
158 * respectively seems to give nice results.
160 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
161 unsigned int n_terms, unsigned int threshold)
167 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
171 /* Convert the fraction to a simple continued fraction. See
172 * http://mathforum.org/dr.math/faq/faq.fractions.html
173 * Stop if the current term is bigger than or equal to the given
179 for (n = 0; n < n_terms && y != 0; ++n) {
181 if (an[n] >= threshold) {
192 /* Expand the simple continued fraction back to an integer fraction. */
196 for (i = n; i > 0; --i) {
207 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
208 * to compute numerator / denominator * 10000000 using 32 bit fixed point
211 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
215 /* Saturate the result if the operation would overflow. */
216 if (denominator == 0 ||
217 numerator/denominator >= ((uint32_t)-1)/10000000)
220 /* Divide both the denominator and the multiplier by two until
221 * numerator * multiplier doesn't overflow. If anyone knows a better
222 * algorithm please let me know.
224 multiplier = 10000000;
225 while (numerator > ((uint32_t)-1)/multiplier) {
230 return denominator ? numerator * multiplier / denominator : 0;
233 /* ------------------------------------------------------------------------
234 * Terminal and unit management
237 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
239 struct uvc_entity *entity;
241 list_for_each_entry(entity, &dev->entities, list) {
242 if (entity->id == id)
249 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
250 int id, struct uvc_entity *entity)
255 entity = list_entry(&dev->entities, struct uvc_entity, list);
257 list_for_each_entry_continue(entity, &dev->entities, list) {
258 for (i = 0; i < entity->bNrInPins; ++i)
259 if (entity->baSourceID[i] == id)
266 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
268 struct uvc_streaming *stream;
270 list_for_each_entry(stream, &dev->streams, list) {
271 if (stream->header.bTerminalLink == id)
278 /* ------------------------------------------------------------------------
279 * Descriptors parsing
282 static int uvc_parse_format(struct uvc_device *dev,
283 struct uvc_streaming *streaming, struct uvc_format *format,
284 __u32 **intervals, unsigned char *buffer, int buflen)
286 struct usb_interface *intf = streaming->intf;
287 struct usb_host_interface *alts = intf->cur_altsetting;
288 struct uvc_format_desc *fmtdesc;
289 struct uvc_frame *frame;
290 const unsigned char *start = buffer;
291 unsigned int interval;
295 format->type = buffer[2];
296 format->index = buffer[3];
299 case UVC_VS_FORMAT_UNCOMPRESSED:
300 case UVC_VS_FORMAT_FRAME_BASED:
301 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
303 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
304 "interface %d FORMAT error\n",
306 alts->desc.bInterfaceNumber);
310 /* Find the format descriptor from its GUID. */
311 fmtdesc = uvc_format_by_guid(&buffer[5]);
313 if (fmtdesc != NULL) {
314 strlcpy(format->name, fmtdesc->name,
315 sizeof format->name);
316 format->fcc = fmtdesc->fcc;
318 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
320 snprintf(format->name, sizeof(format->name), "%pUl\n",
325 format->bpp = buffer[21];
326 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
327 ftype = UVC_VS_FRAME_UNCOMPRESSED;
329 ftype = UVC_VS_FRAME_FRAME_BASED;
331 format->flags = UVC_FMT_FLAG_COMPRESSED;
335 case UVC_VS_FORMAT_MJPEG:
337 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
338 "interface %d FORMAT error\n",
340 alts->desc.bInterfaceNumber);
344 strlcpy(format->name, "MJPEG", sizeof format->name);
345 format->fcc = V4L2_PIX_FMT_MJPEG;
346 format->flags = UVC_FMT_FLAG_COMPRESSED;
348 ftype = UVC_VS_FRAME_MJPEG;
351 case UVC_VS_FORMAT_DV:
353 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
354 "interface %d FORMAT error\n",
356 alts->desc.bInterfaceNumber);
360 switch (buffer[8] & 0x7f) {
362 strlcpy(format->name, "SD-DV", sizeof format->name);
365 strlcpy(format->name, "SDL-DV", sizeof format->name);
368 strlcpy(format->name, "HD-DV", sizeof format->name);
371 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
372 "interface %d: unknown DV format %u\n",
374 alts->desc.bInterfaceNumber, buffer[8]);
378 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
379 sizeof format->name);
381 format->fcc = V4L2_PIX_FMT_DV;
382 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
386 /* Create a dummy frame descriptor. */
387 frame = &format->frame[0];
388 memset(&format->frame[0], 0, sizeof format->frame[0]);
389 frame->bFrameIntervalType = 1;
390 frame->dwDefaultFrameInterval = 1;
391 frame->dwFrameInterval = *intervals;
396 case UVC_VS_FORMAT_MPEG2TS:
397 case UVC_VS_FORMAT_STREAM_BASED:
398 /* Not supported yet. */
400 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
401 "interface %d unsupported format %u\n",
402 dev->udev->devnum, alts->desc.bInterfaceNumber,
407 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
412 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
413 * based formats have frame descriptors.
415 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
416 buffer[2] == ftype) {
417 frame = &format->frame[format->nframes];
418 if (ftype != UVC_VS_FRAME_FRAME_BASED)
419 n = buflen > 25 ? buffer[25] : 0;
421 n = buflen > 21 ? buffer[21] : 0;
425 if (buflen < 26 + 4*n) {
426 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
427 "interface %d FRAME error\n", dev->udev->devnum,
428 alts->desc.bInterfaceNumber);
432 frame->bFrameIndex = buffer[3];
433 frame->bmCapabilities = buffer[4];
434 frame->wWidth = get_unaligned_le16(&buffer[5]);
435 frame->wHeight = get_unaligned_le16(&buffer[7]);
436 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
437 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
438 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
439 frame->dwMaxVideoFrameBufferSize =
440 get_unaligned_le32(&buffer[17]);
441 frame->dwDefaultFrameInterval =
442 get_unaligned_le32(&buffer[21]);
443 frame->bFrameIntervalType = buffer[25];
445 frame->dwMaxVideoFrameBufferSize = 0;
446 frame->dwDefaultFrameInterval =
447 get_unaligned_le32(&buffer[17]);
448 frame->bFrameIntervalType = buffer[21];
450 frame->dwFrameInterval = *intervals;
452 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
453 * completely. Observed behaviours range from setting the
454 * value to 1.1x the actual frame size to hardwiring the
455 * 16 low bits to 0. This results in a higher than necessary
456 * memory usage as well as a wrong image size information. For
457 * uncompressed formats this can be fixed by computing the
458 * value from the frame size.
460 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
461 frame->dwMaxVideoFrameBufferSize = format->bpp
462 * frame->wWidth * frame->wHeight / 8;
464 /* Some bogus devices report dwMinFrameInterval equal to
465 * dwMaxFrameInterval and have dwFrameIntervalStep set to
466 * zero. Setting all null intervals to 1 fixes the problem and
467 * some other divisions by zero that could happen.
469 for (i = 0; i < n; ++i) {
470 interval = get_unaligned_le32(&buffer[26+4*i]);
471 *(*intervals)++ = interval ? interval : 1;
474 /* Make sure that the default frame interval stays between
477 n -= frame->bFrameIntervalType ? 1 : 2;
478 frame->dwDefaultFrameInterval =
479 min(frame->dwFrameInterval[n],
480 max(frame->dwFrameInterval[0],
481 frame->dwDefaultFrameInterval));
483 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
484 frame->wWidth, frame->wHeight,
485 10000000/frame->dwDefaultFrameInterval,
486 (100000000/frame->dwDefaultFrameInterval)%10);
493 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
494 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
499 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
500 buffer[2] == UVC_VS_COLORFORMAT) {
502 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
503 "interface %d COLORFORMAT error\n",
505 alts->desc.bInterfaceNumber);
509 format->colorspace = uvc_colorspace(buffer[3]);
515 return buffer - start;
518 static int uvc_parse_streaming(struct uvc_device *dev,
519 struct usb_interface *intf)
521 struct uvc_streaming *streaming = NULL;
522 struct uvc_format *format;
523 struct uvc_frame *frame;
524 struct usb_host_interface *alts = &intf->altsetting[0];
525 unsigned char *_buffer, *buffer = alts->extra;
526 int _buflen, buflen = alts->extralen;
527 unsigned int nformats = 0, nframes = 0, nintervals = 0;
528 unsigned int size, i, n, p;
533 if (intf->cur_altsetting->desc.bInterfaceSubClass
534 != UVC_SC_VIDEOSTREAMING) {
535 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
536 "video streaming interface\n", dev->udev->devnum,
537 intf->altsetting[0].desc.bInterfaceNumber);
541 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
542 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
543 "claimed\n", dev->udev->devnum,
544 intf->altsetting[0].desc.bInterfaceNumber);
548 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
549 if (streaming == NULL) {
550 usb_driver_release_interface(&uvc_driver.driver, intf);
554 mutex_init(&streaming->mutex);
555 streaming->dev = dev;
556 streaming->intf = usb_get_intf(intf);
557 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
559 /* The Pico iMage webcam has its class-specific interface descriptors
560 * after the endpoint descriptors.
563 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
564 struct usb_host_endpoint *ep = &alts->endpoint[i];
566 if (ep->extralen == 0)
569 if (ep->extralen > 2 &&
570 ep->extra[1] == USB_DT_CS_INTERFACE) {
571 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
572 "from endpoint %u.\n", i);
573 buffer = alts->endpoint[i].extra;
574 buflen = alts->endpoint[i].extralen;
580 /* Skip the standard interface descriptors. */
581 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
587 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
588 "interface descriptors found.\n");
592 /* Parse the header descriptor. */
594 case UVC_VS_OUTPUT_HEADER:
595 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
599 case UVC_VS_INPUT_HEADER:
600 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
605 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
606 "%d HEADER descriptor not found.\n", dev->udev->devnum,
607 alts->desc.bInterfaceNumber);
611 p = buflen >= 4 ? buffer[3] : 0;
612 n = buflen >= size ? buffer[size-1] : 0;
614 if (buflen < size + p*n) {
615 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
616 "interface %d HEADER descriptor is invalid.\n",
617 dev->udev->devnum, alts->desc.bInterfaceNumber);
621 streaming->header.bNumFormats = p;
622 streaming->header.bEndpointAddress = buffer[6];
623 if (buffer[2] == UVC_VS_INPUT_HEADER) {
624 streaming->header.bmInfo = buffer[7];
625 streaming->header.bTerminalLink = buffer[8];
626 streaming->header.bStillCaptureMethod = buffer[9];
627 streaming->header.bTriggerSupport = buffer[10];
628 streaming->header.bTriggerUsage = buffer[11];
630 streaming->header.bTerminalLink = buffer[7];
632 streaming->header.bControlSize = n;
634 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
635 if (streaming->header.bmaControls == NULL) {
640 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
648 /* Count the format and frame descriptors. */
649 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
650 switch (_buffer[2]) {
651 case UVC_VS_FORMAT_UNCOMPRESSED:
652 case UVC_VS_FORMAT_MJPEG:
653 case UVC_VS_FORMAT_FRAME_BASED:
657 case UVC_VS_FORMAT_DV:
658 /* DV format has no frame descriptor. We will create a
659 * dummy frame descriptor with a dummy frame interval.
666 case UVC_VS_FORMAT_MPEG2TS:
667 case UVC_VS_FORMAT_STREAM_BASED:
668 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
669 "interface %d FORMAT %u is not supported.\n",
671 alts->desc.bInterfaceNumber, _buffer[2]);
674 case UVC_VS_FRAME_UNCOMPRESSED:
675 case UVC_VS_FRAME_MJPEG:
678 nintervals += _buffer[25] ? _buffer[25] : 3;
681 case UVC_VS_FRAME_FRAME_BASED:
684 nintervals += _buffer[21] ? _buffer[21] : 3;
688 _buflen -= _buffer[0];
689 _buffer += _buffer[0];
693 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
694 "%d has no supported formats defined.\n",
695 dev->udev->devnum, alts->desc.bInterfaceNumber);
699 size = nformats * sizeof *format + nframes * sizeof *frame
700 + nintervals * sizeof *interval;
701 format = kzalloc(size, GFP_KERNEL);
702 if (format == NULL) {
707 frame = (struct uvc_frame *)&format[nformats];
708 interval = (__u32 *)&frame[nframes];
710 streaming->format = format;
711 streaming->nformats = nformats;
713 /* Parse the format descriptors. */
714 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
716 case UVC_VS_FORMAT_UNCOMPRESSED:
717 case UVC_VS_FORMAT_MJPEG:
718 case UVC_VS_FORMAT_DV:
719 case UVC_VS_FORMAT_FRAME_BASED:
720 format->frame = frame;
721 ret = uvc_parse_format(dev, streaming, format,
722 &interval, buffer, buflen);
726 frame += format->nframes;
742 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
743 "%d has %u bytes of trailing descriptor garbage.\n",
744 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
746 /* Parse the alternate settings to find the maximum bandwidth. */
747 for (i = 0; i < intf->num_altsetting; ++i) {
748 struct usb_host_endpoint *ep;
749 alts = &intf->altsetting[i];
750 ep = uvc_find_endpoint(alts,
751 streaming->header.bEndpointAddress);
755 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
756 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
757 if (psize > streaming->maxpsize)
758 streaming->maxpsize = psize;
761 list_add_tail(&streaming->list, &dev->streams);
765 usb_driver_release_interface(&uvc_driver.driver, intf);
767 kfree(streaming->format);
768 kfree(streaming->header.bmaControls);
773 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
774 unsigned int num_pads, unsigned int extra_size)
776 struct uvc_entity *entity;
777 unsigned int num_inputs;
780 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
781 size = sizeof(*entity) + extra_size + num_inputs;
782 entity = kzalloc(size, GFP_KERNEL);
789 entity->bNrInPins = num_inputs;
790 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
795 /* Parse vendor-specific extensions. */
796 static int uvc_parse_vendor_control(struct uvc_device *dev,
797 const unsigned char *buffer, int buflen)
799 struct usb_device *udev = dev->udev;
800 struct usb_host_interface *alts = dev->intf->cur_altsetting;
801 struct uvc_entity *unit;
805 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
806 case 0x046d: /* Logitech */
807 if (buffer[1] != 0x41 || buffer[2] != 0x01)
810 /* Logitech implements several vendor specific functions
811 * through vendor specific extension units (LXU).
813 * The LXU descriptors are similar to XU descriptors
814 * (see "USB Device Video Class for Video Devices", section
815 * 3.7.2.6 "Extension Unit Descriptor") with the following
818 * ----------------------------------------------------------
820 * Size of this descriptor, in bytes: 24+p+n*2
821 * ----------------------------------------------------------
822 * 23+p+n bmControlsType N Bitmap
823 * Individual bits in the set are defined:
827 * This bitset is mapped exactly the same as bmControls.
828 * ----------------------------------------------------------
829 * 23+p+n*2 bReserved 1 Boolean
830 * ----------------------------------------------------------
831 * 24+p+n*2 iExtension 1 Index
832 * Index of a string descriptor that describes this
834 * ----------------------------------------------------------
836 p = buflen >= 22 ? buffer[21] : 0;
837 n = buflen >= 25 + p ? buffer[22+p] : 0;
839 if (buflen < 25 + p + 2*n) {
840 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
841 "interface %d EXTENSION_UNIT error\n",
842 udev->devnum, alts->desc.bInterfaceNumber);
846 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
851 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
852 unit->extension.bNumControls = buffer[20];
853 memcpy(unit->baSourceID, &buffer[22], p);
854 unit->extension.bControlSize = buffer[22+p];
855 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
856 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
858 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
860 if (buffer[24+p+2*n] != 0)
861 usb_string(udev, buffer[24+p+2*n], unit->name,
864 sprintf(unit->name, "Extension %u", buffer[3]);
866 list_add_tail(&unit->list, &dev->entities);
874 static int uvc_parse_standard_control(struct uvc_device *dev,
875 const unsigned char *buffer, int buflen)
877 struct usb_device *udev = dev->udev;
878 struct uvc_entity *unit, *term;
879 struct usb_interface *intf;
880 struct usb_host_interface *alts = dev->intf->cur_altsetting;
881 unsigned int i, n, p, len;
886 n = buflen >= 12 ? buffer[11] : 0;
888 if (buflen < 12 || buflen < 12 + n) {
889 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
890 "interface %d HEADER error\n", udev->devnum,
891 alts->desc.bInterfaceNumber);
895 dev->uvc_version = get_unaligned_le16(&buffer[3]);
896 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
898 /* Parse all USB Video Streaming interfaces. */
899 for (i = 0; i < n; ++i) {
900 intf = usb_ifnum_to_if(udev, buffer[12+i]);
902 uvc_trace(UVC_TRACE_DESCR, "device %d "
903 "interface %d doesn't exists\n",
908 uvc_parse_streaming(dev, intf);
912 case UVC_VC_INPUT_TERMINAL:
914 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
915 "interface %d INPUT_TERMINAL error\n",
916 udev->devnum, alts->desc.bInterfaceNumber);
920 /* Make sure the terminal type MSB is not null, otherwise it
921 * could be confused with a unit.
923 type = get_unaligned_le16(&buffer[4]);
924 if ((type & 0xff00) == 0) {
925 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
926 "interface %d INPUT_TERMINAL %d has invalid "
927 "type 0x%04x, skipping\n", udev->devnum,
928 alts->desc.bInterfaceNumber,
937 if (type == UVC_ITT_CAMERA) {
938 n = buflen >= 15 ? buffer[14] : 0;
941 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
942 n = buflen >= 9 ? buffer[8] : 0;
943 p = buflen >= 10 + n ? buffer[9+n] : 0;
947 if (buflen < len + n + p) {
948 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
949 "interface %d INPUT_TERMINAL error\n",
950 udev->devnum, alts->desc.bInterfaceNumber);
954 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
959 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
960 term->camera.bControlSize = n;
961 term->camera.bmControls = (__u8 *)term + sizeof *term;
962 term->camera.wObjectiveFocalLengthMin =
963 get_unaligned_le16(&buffer[8]);
964 term->camera.wObjectiveFocalLengthMax =
965 get_unaligned_le16(&buffer[10]);
966 term->camera.wOcularFocalLength =
967 get_unaligned_le16(&buffer[12]);
968 memcpy(term->camera.bmControls, &buffer[15], n);
969 } else if (UVC_ENTITY_TYPE(term) ==
970 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
971 term->media.bControlSize = n;
972 term->media.bmControls = (__u8 *)term + sizeof *term;
973 term->media.bTransportModeSize = p;
974 term->media.bmTransportModes = (__u8 *)term
976 memcpy(term->media.bmControls, &buffer[9], n);
977 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
981 usb_string(udev, buffer[7], term->name,
983 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
984 sprintf(term->name, "Camera %u", buffer[3]);
985 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
986 sprintf(term->name, "Media %u", buffer[3]);
988 sprintf(term->name, "Input %u", buffer[3]);
990 list_add_tail(&term->list, &dev->entities);
993 case UVC_VC_OUTPUT_TERMINAL:
995 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
996 "interface %d OUTPUT_TERMINAL error\n",
997 udev->devnum, alts->desc.bInterfaceNumber);
1001 /* Make sure the terminal type MSB is not null, otherwise it
1002 * could be confused with a unit.
1004 type = get_unaligned_le16(&buffer[4]);
1005 if ((type & 0xff00) == 0) {
1006 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1007 "interface %d OUTPUT_TERMINAL %d has invalid "
1008 "type 0x%04x, skipping\n", udev->devnum,
1009 alts->desc.bInterfaceNumber, buffer[3], type);
1013 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1018 memcpy(term->baSourceID, &buffer[7], 1);
1021 usb_string(udev, buffer[8], term->name,
1024 sprintf(term->name, "Output %u", buffer[3]);
1026 list_add_tail(&term->list, &dev->entities);
1029 case UVC_VC_SELECTOR_UNIT:
1030 p = buflen >= 5 ? buffer[4] : 0;
1032 if (buflen < 5 || buflen < 6 + p) {
1033 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1034 "interface %d SELECTOR_UNIT error\n",
1035 udev->devnum, alts->desc.bInterfaceNumber);
1039 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1043 memcpy(unit->baSourceID, &buffer[5], p);
1045 if (buffer[5+p] != 0)
1046 usb_string(udev, buffer[5+p], unit->name,
1049 sprintf(unit->name, "Selector %u", buffer[3]);
1051 list_add_tail(&unit->list, &dev->entities);
1054 case UVC_VC_PROCESSING_UNIT:
1055 n = buflen >= 8 ? buffer[7] : 0;
1056 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1058 if (buflen < p + n) {
1059 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1060 "interface %d PROCESSING_UNIT error\n",
1061 udev->devnum, alts->desc.bInterfaceNumber);
1065 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1069 memcpy(unit->baSourceID, &buffer[4], 1);
1070 unit->processing.wMaxMultiplier =
1071 get_unaligned_le16(&buffer[5]);
1072 unit->processing.bControlSize = buffer[7];
1073 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1074 memcpy(unit->processing.bmControls, &buffer[8], n);
1075 if (dev->uvc_version >= 0x0110)
1076 unit->processing.bmVideoStandards = buffer[9+n];
1078 if (buffer[8+n] != 0)
1079 usb_string(udev, buffer[8+n], unit->name,
1082 sprintf(unit->name, "Processing %u", buffer[3]);
1084 list_add_tail(&unit->list, &dev->entities);
1087 case UVC_VC_EXTENSION_UNIT:
1088 p = buflen >= 22 ? buffer[21] : 0;
1089 n = buflen >= 24 + p ? buffer[22+p] : 0;
1091 if (buflen < 24 + p + n) {
1092 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1093 "interface %d EXTENSION_UNIT error\n",
1094 udev->devnum, alts->desc.bInterfaceNumber);
1098 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1102 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1103 unit->extension.bNumControls = buffer[20];
1104 memcpy(unit->baSourceID, &buffer[22], p);
1105 unit->extension.bControlSize = buffer[22+p];
1106 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1107 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1109 if (buffer[23+p+n] != 0)
1110 usb_string(udev, buffer[23+p+n], unit->name,
1113 sprintf(unit->name, "Extension %u", buffer[3]);
1115 list_add_tail(&unit->list, &dev->entities);
1119 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1120 "descriptor (%u)\n", buffer[2]);
1127 static int uvc_parse_control(struct uvc_device *dev)
1129 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1130 unsigned char *buffer = alts->extra;
1131 int buflen = alts->extralen;
1134 /* Parse the default alternate setting only, as the UVC specification
1135 * defines a single alternate setting, the default alternate setting
1139 while (buflen > 2) {
1140 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1141 buffer[1] != USB_DT_CS_INTERFACE)
1142 goto next_descriptor;
1144 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1148 buflen -= buffer[0];
1149 buffer += buffer[0];
1152 /* Check if the optional status endpoint is present. Built-in iSight
1153 * webcams have an interrupt endpoint but spit proprietary data that
1154 * don't conform to the UVC status endpoint messages. Don't try to
1155 * handle the interrupt endpoint for those cameras.
1157 if (alts->desc.bNumEndpoints == 1 &&
1158 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1159 struct usb_host_endpoint *ep = &alts->endpoint[0];
1160 struct usb_endpoint_descriptor *desc = &ep->desc;
1162 if (usb_endpoint_is_int_in(desc) &&
1163 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1164 desc->bInterval != 0) {
1165 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1166 "(addr %02x).\n", desc->bEndpointAddress);
1174 /* ------------------------------------------------------------------------
1179 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1180 * and containing the following units:
1182 * - one or more Output Terminals (USB Streaming or Display)
1183 * - zero or one Processing Unit
1184 * - zero, one or more single-input Selector Units
1185 * - zero or one multiple-input Selector Units, provided all inputs are
1186 * connected to input terminals
1187 * - zero, one or mode single-input Extension Units
1188 * - one or more Input Terminals (Camera, External or USB Streaming)
1190 * The terminal and units must match on of the following structures:
1192 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1193 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1194 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1196 * +---------+ +---------+ -> OTT_*(0)
1197 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1198 * +---------+ +---------+ -> OTT_*(n)
1200 * The Processing Unit and Extension Units can be in any order. Additional
1201 * Extension Units connected to the main chain as single-unit branches are
1202 * also supported. Single-input Selector Units are ignored.
1204 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1205 struct uvc_entity *entity)
1207 switch (UVC_ENTITY_TYPE(entity)) {
1208 case UVC_VC_EXTENSION_UNIT:
1209 if (uvc_trace_param & UVC_TRACE_PROBE)
1210 printk(" <- XU %d", entity->id);
1212 if (entity->bNrInPins != 1) {
1213 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1214 "than 1 input pin.\n", entity->id);
1220 case UVC_VC_PROCESSING_UNIT:
1221 if (uvc_trace_param & UVC_TRACE_PROBE)
1222 printk(" <- PU %d", entity->id);
1224 if (chain->processing != NULL) {
1225 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1226 "Processing Units in chain.\n");
1230 chain->processing = entity;
1233 case UVC_VC_SELECTOR_UNIT:
1234 if (uvc_trace_param & UVC_TRACE_PROBE)
1235 printk(" <- SU %d", entity->id);
1237 /* Single-input selector units are ignored. */
1238 if (entity->bNrInPins == 1)
1241 if (chain->selector != NULL) {
1242 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1243 "Units in chain.\n");
1247 chain->selector = entity;
1250 case UVC_ITT_VENDOR_SPECIFIC:
1251 case UVC_ITT_CAMERA:
1252 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1253 if (uvc_trace_param & UVC_TRACE_PROBE)
1254 printk(" <- IT %d\n", entity->id);
1258 case UVC_TT_STREAMING:
1259 if (UVC_ENTITY_IS_ITERM(entity)) {
1260 if (uvc_trace_param & UVC_TRACE_PROBE)
1261 printk(" <- IT %d\n", entity->id);
1263 if (uvc_trace_param & UVC_TRACE_PROBE)
1264 printk(" OT %d", entity->id);
1270 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1271 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1275 list_add_tail(&entity->chain, &chain->entities);
1279 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1280 struct uvc_entity *entity, struct uvc_entity *prev)
1282 struct uvc_entity *forward;
1290 forward = uvc_entity_by_reference(chain->dev, entity->id,
1292 if (forward == NULL)
1294 if (forward == prev)
1297 switch (UVC_ENTITY_TYPE(forward)) {
1298 case UVC_VC_EXTENSION_UNIT:
1299 if (forward->bNrInPins != 1) {
1300 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1301 "has more than 1 input pin.\n",
1306 list_add_tail(&forward->chain, &chain->entities);
1307 if (uvc_trace_param & UVC_TRACE_PROBE) {
1311 printk(" XU %d", forward->id);
1316 case UVC_OTT_VENDOR_SPECIFIC:
1317 case UVC_OTT_DISPLAY:
1318 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1319 case UVC_TT_STREAMING:
1320 if (UVC_ENTITY_IS_ITERM(forward)) {
1321 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1322 "terminal %u.\n", forward->id);
1326 list_add_tail(&forward->chain, &chain->entities);
1327 if (uvc_trace_param & UVC_TRACE_PROBE) {
1331 printk(" OT %d", forward->id);
1343 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1344 struct uvc_entity **_entity)
1346 struct uvc_entity *entity = *_entity;
1347 struct uvc_entity *term;
1348 int id = -EINVAL, i;
1350 switch (UVC_ENTITY_TYPE(entity)) {
1351 case UVC_VC_EXTENSION_UNIT:
1352 case UVC_VC_PROCESSING_UNIT:
1353 id = entity->baSourceID[0];
1356 case UVC_VC_SELECTOR_UNIT:
1357 /* Single-input selector units are ignored. */
1358 if (entity->bNrInPins == 1) {
1359 id = entity->baSourceID[0];
1363 if (uvc_trace_param & UVC_TRACE_PROBE)
1366 chain->selector = entity;
1367 for (i = 0; i < entity->bNrInPins; ++i) {
1368 id = entity->baSourceID[i];
1369 term = uvc_entity_by_id(chain->dev, id);
1370 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1371 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1372 "input %d isn't connected to an "
1373 "input terminal\n", entity->id, i);
1377 if (uvc_trace_param & UVC_TRACE_PROBE)
1378 printk(" %d", term->id);
1380 list_add_tail(&term->chain, &chain->entities);
1381 uvc_scan_chain_forward(chain, term, entity);
1384 if (uvc_trace_param & UVC_TRACE_PROBE)
1390 case UVC_ITT_VENDOR_SPECIFIC:
1391 case UVC_ITT_CAMERA:
1392 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1393 case UVC_OTT_VENDOR_SPECIFIC:
1394 case UVC_OTT_DISPLAY:
1395 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1396 case UVC_TT_STREAMING:
1397 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1406 entity = uvc_entity_by_id(chain->dev, id);
1407 if (entity == NULL) {
1408 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1409 "unknown entity %d.\n", id);
1417 static int uvc_scan_chain(struct uvc_video_chain *chain,
1418 struct uvc_entity *term)
1420 struct uvc_entity *entity, *prev;
1422 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1427 while (entity != NULL) {
1428 /* Entity must not be part of an existing chain */
1429 if (entity->chain.next || entity->chain.prev) {
1430 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1431 "entity %d already in chain.\n", entity->id);
1435 /* Process entity */
1436 if (uvc_scan_chain_entity(chain, entity) < 0)
1440 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1445 if (uvc_scan_chain_backward(chain, &entity) < 0)
1452 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1455 struct uvc_entity *term;
1456 unsigned int nterms = 0;
1459 list_for_each_entry(term, terms, chain) {
1460 if (!UVC_ENTITY_IS_TERM(term) ||
1461 UVC_TERM_DIRECTION(term) != dir)
1465 p += sprintf(p, ",");
1466 if (++nterms >= 4) {
1467 p += sprintf(p, "...");
1470 p += sprintf(p, "%u", term->id);
1476 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1478 static char buffer[43];
1481 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1482 p += sprintf(p, " -> ");
1483 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1489 * Scan the device for video chains and register video devices.
1491 * Chains are scanned starting at their output terminals and walked backwards.
1493 static int uvc_scan_device(struct uvc_device *dev)
1495 struct uvc_video_chain *chain;
1496 struct uvc_entity *term;
1498 list_for_each_entry(term, &dev->entities, list) {
1499 if (!UVC_ENTITY_IS_OTERM(term))
1502 /* If the terminal is already included in a chain, skip it.
1503 * This can happen for chains that have multiple output
1504 * terminals, where all output terminals beside the first one
1505 * will be inserted in the chain in forward scans.
1507 if (term->chain.next || term->chain.prev)
1510 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1514 INIT_LIST_HEAD(&chain->entities);
1515 mutex_init(&chain->ctrl_mutex);
1518 if (uvc_scan_chain(chain, term) < 0) {
1523 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1524 uvc_print_chain(chain));
1526 list_add_tail(&chain->list, &dev->chains);
1529 if (list_empty(&dev->chains)) {
1530 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1537 /* ------------------------------------------------------------------------
1538 * Video device registration and unregistration
1542 * Delete the UVC device.
1544 * Called by the kernel when the last reference to the uvc_device structure
1547 * As this function is called after or during disconnect(), all URBs have
1548 * already been canceled by the USB core. There is no need to kill the
1549 * interrupt URB manually.
1551 static void uvc_delete(struct uvc_device *dev)
1553 struct list_head *p, *n;
1555 usb_put_intf(dev->intf);
1556 usb_put_dev(dev->udev);
1558 uvc_status_cleanup(dev);
1559 uvc_ctrl_cleanup_device(dev);
1561 list_for_each_safe(p, n, &dev->chains) {
1562 struct uvc_video_chain *chain;
1563 chain = list_entry(p, struct uvc_video_chain, list);
1567 list_for_each_safe(p, n, &dev->entities) {
1568 struct uvc_entity *entity;
1569 entity = list_entry(p, struct uvc_entity, list);
1573 list_for_each_safe(p, n, &dev->streams) {
1574 struct uvc_streaming *streaming;
1575 streaming = list_entry(p, struct uvc_streaming, list);
1576 usb_driver_release_interface(&uvc_driver.driver,
1578 usb_put_intf(streaming->intf);
1579 kfree(streaming->format);
1580 kfree(streaming->header.bmaControls);
1587 static void uvc_release(struct video_device *vdev)
1589 struct uvc_streaming *stream = video_get_drvdata(vdev);
1590 struct uvc_device *dev = stream->dev;
1592 video_device_release(vdev);
1594 /* Decrement the registered streams count and delete the device when it
1597 if (atomic_dec_and_test(&dev->nstreams))
1602 * Unregister the video devices.
1604 static void uvc_unregister_video(struct uvc_device *dev)
1606 struct uvc_streaming *stream;
1608 /* Unregistering all video devices might result in uvc_delete() being
1609 * called from inside the loop if there's no open file handle. To avoid
1610 * that, increment the stream count before iterating over the streams
1611 * and decrement it when done.
1613 atomic_inc(&dev->nstreams);
1615 list_for_each_entry(stream, &dev->streams, list) {
1616 if (stream->vdev == NULL)
1619 video_unregister_device(stream->vdev);
1620 stream->vdev = NULL;
1623 /* Decrement the stream count and call uvc_delete explicitly if there
1624 * are no stream left.
1626 if (atomic_dec_and_test(&dev->nstreams))
1630 static int uvc_register_video(struct uvc_device *dev,
1631 struct uvc_streaming *stream)
1633 struct video_device *vdev;
1636 /* Initialize the streaming interface with default streaming
1639 ret = uvc_video_init(stream);
1641 uvc_printk(KERN_ERR, "Failed to initialize the device "
1646 /* Register the device with V4L. */
1647 vdev = video_device_alloc();
1649 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1654 /* We already hold a reference to dev->udev. The video device will be
1655 * unregistered before the reference is released, so we don't need to
1658 vdev->parent = &dev->intf->dev;
1659 vdev->fops = &uvc_fops;
1660 vdev->release = uvc_release;
1661 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1663 /* Set the driver data before calling video_register_device, otherwise
1664 * uvc_v4l2_open might race us.
1666 stream->vdev = vdev;
1667 video_set_drvdata(vdev, stream);
1669 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1671 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1673 stream->vdev = NULL;
1674 video_device_release(vdev);
1678 atomic_inc(&dev->nstreams);
1683 * Register all video devices in all chains.
1685 static int uvc_register_terms(struct uvc_device *dev,
1686 struct uvc_video_chain *chain)
1688 struct uvc_streaming *stream;
1689 struct uvc_entity *term;
1692 list_for_each_entry(term, &chain->entities, chain) {
1693 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1696 stream = uvc_stream_by_id(dev, term->id);
1697 if (stream == NULL) {
1698 uvc_printk(KERN_INFO, "No streaming interface found "
1699 "for terminal %u.", term->id);
1703 stream->chain = chain;
1704 ret = uvc_register_video(dev, stream);
1712 static int uvc_register_chains(struct uvc_device *dev)
1714 struct uvc_video_chain *chain;
1717 list_for_each_entry(chain, &dev->chains, list) {
1718 ret = uvc_register_terms(dev, chain);
1726 /* ------------------------------------------------------------------------
1727 * USB probe, disconnect, suspend and resume
1730 static int uvc_probe(struct usb_interface *intf,
1731 const struct usb_device_id *id)
1733 struct usb_device *udev = interface_to_usbdev(intf);
1734 struct uvc_device *dev;
1737 if (id->idVendor && id->idProduct)
1738 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1739 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1742 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1745 /* Allocate memory for the device and initialize it. */
1746 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1749 INIT_LIST_HEAD(&dev->entities);
1750 INIT_LIST_HEAD(&dev->chains);
1751 INIT_LIST_HEAD(&dev->streams);
1752 atomic_set(&dev->nstreams, 0);
1753 atomic_set(&dev->users, 0);
1755 dev->udev = usb_get_dev(udev);
1756 dev->intf = usb_get_intf(intf);
1757 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1758 dev->quirks = (uvc_quirks_param == -1)
1759 ? id->driver_info : uvc_quirks_param;
1761 if (udev->product != NULL)
1762 strlcpy(dev->name, udev->product, sizeof dev->name);
1764 snprintf(dev->name, sizeof dev->name,
1765 "UVC Camera (%04x:%04x)",
1766 le16_to_cpu(udev->descriptor.idVendor),
1767 le16_to_cpu(udev->descriptor.idProduct));
1769 /* Parse the Video Class control descriptor. */
1770 if (uvc_parse_control(dev) < 0) {
1771 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1776 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1777 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1778 udev->product ? udev->product : "<unnamed>",
1779 le16_to_cpu(udev->descriptor.idVendor),
1780 le16_to_cpu(udev->descriptor.idProduct));
1782 if (dev->quirks != id->driver_info) {
1783 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1784 "parameter for testing purpose.\n", dev->quirks);
1785 uvc_printk(KERN_INFO, "Please report required quirks to the "
1786 "linux-uvc-devel mailing list.\n");
1789 /* Initialize controls. */
1790 if (uvc_ctrl_init_device(dev) < 0)
1793 /* Scan the device for video chains. */
1794 if (uvc_scan_device(dev) < 0)
1797 /* Register video devices. */
1798 if (uvc_register_chains(dev) < 0)
1801 /* Save our data pointer in the interface data. */
1802 usb_set_intfdata(intf, dev);
1804 /* Initialize the interrupt URB. */
1805 if ((ret = uvc_status_init(dev)) < 0) {
1806 uvc_printk(KERN_INFO, "Unable to initialize the status "
1807 "endpoint (%d), status interrupt will not be "
1808 "supported.\n", ret);
1811 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1815 uvc_unregister_video(dev);
1819 static void uvc_disconnect(struct usb_interface *intf)
1821 struct uvc_device *dev = usb_get_intfdata(intf);
1823 /* Set the USB interface data to NULL. This can be done outside the
1824 * lock, as there's no other reader.
1826 usb_set_intfdata(intf, NULL);
1828 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1829 UVC_SC_VIDEOSTREAMING)
1832 dev->state |= UVC_DEV_DISCONNECTED;
1834 uvc_unregister_video(dev);
1837 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1839 struct uvc_device *dev = usb_get_intfdata(intf);
1840 struct uvc_streaming *stream;
1842 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1843 intf->cur_altsetting->desc.bInterfaceNumber);
1845 /* Controls are cached on the fly so they don't need to be saved. */
1846 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1847 UVC_SC_VIDEOCONTROL)
1848 return uvc_status_suspend(dev);
1850 list_for_each_entry(stream, &dev->streams, list) {
1851 if (stream->intf == intf)
1852 return uvc_video_suspend(stream);
1855 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1860 static int __uvc_resume(struct usb_interface *intf, int reset)
1862 struct uvc_device *dev = usb_get_intfdata(intf);
1863 struct uvc_streaming *stream;
1865 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1866 intf->cur_altsetting->desc.bInterfaceNumber);
1868 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1869 UVC_SC_VIDEOCONTROL) {
1871 int ret = uvc_ctrl_resume_device(dev);
1877 return uvc_status_resume(dev);
1880 list_for_each_entry(stream, &dev->streams, list) {
1881 if (stream->intf == intf)
1882 return uvc_video_resume(stream);
1885 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1890 static int uvc_resume(struct usb_interface *intf)
1892 return __uvc_resume(intf, 0);
1895 static int uvc_reset_resume(struct usb_interface *intf)
1897 return __uvc_resume(intf, 1);
1900 /* ------------------------------------------------------------------------
1904 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1906 if (uvc_clock_param == CLOCK_MONOTONIC)
1907 return sprintf(buffer, "CLOCK_MONOTONIC");
1909 return sprintf(buffer, "CLOCK_REALTIME");
1912 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1914 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1915 val += strlen("clock_");
1917 if (strcasecmp(val, "monotonic") == 0)
1918 uvc_clock_param = CLOCK_MONOTONIC;
1919 else if (strcasecmp(val, "realtime") == 0)
1920 uvc_clock_param = CLOCK_REALTIME;
1927 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1928 &uvc_clock_param, S_IRUGO|S_IWUSR);
1929 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1930 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1931 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1932 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1933 MODULE_PARM_DESC(quirks, "Forced device quirks");
1934 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1935 MODULE_PARM_DESC(trace, "Trace level bitmask");
1936 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1937 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1939 /* ------------------------------------------------------------------------
1940 * Driver initialization and cleanup
1944 * The Logitech cameras listed below have their interface class set to
1945 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1946 * though they are compliant.
1948 static struct usb_device_id uvc_ids[] = {
1949 /* Genius eFace 2025 */
1950 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1951 | USB_DEVICE_ID_MATCH_INT_INFO,
1953 .idProduct = 0x706e,
1954 .bInterfaceClass = USB_CLASS_VIDEO,
1955 .bInterfaceSubClass = 1,
1956 .bInterfaceProtocol = 0,
1957 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1958 /* Microsoft Lifecam NX-6000 */
1959 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1960 | USB_DEVICE_ID_MATCH_INT_INFO,
1962 .idProduct = 0x00f8,
1963 .bInterfaceClass = USB_CLASS_VIDEO,
1964 .bInterfaceSubClass = 1,
1965 .bInterfaceProtocol = 0,
1966 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1967 /* Microsoft Lifecam VX-7000 */
1968 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1969 | USB_DEVICE_ID_MATCH_INT_INFO,
1971 .idProduct = 0x0723,
1972 .bInterfaceClass = USB_CLASS_VIDEO,
1973 .bInterfaceSubClass = 1,
1974 .bInterfaceProtocol = 0,
1975 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1976 /* Logitech Quickcam Fusion */
1977 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1978 | USB_DEVICE_ID_MATCH_INT_INFO,
1980 .idProduct = 0x08c1,
1981 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1982 .bInterfaceSubClass = 1,
1983 .bInterfaceProtocol = 0 },
1984 /* Logitech Quickcam Orbit MP */
1985 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1986 | USB_DEVICE_ID_MATCH_INT_INFO,
1988 .idProduct = 0x08c2,
1989 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1990 .bInterfaceSubClass = 1,
1991 .bInterfaceProtocol = 0 },
1992 /* Logitech Quickcam Pro for Notebook */
1993 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1994 | USB_DEVICE_ID_MATCH_INT_INFO,
1996 .idProduct = 0x08c3,
1997 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1998 .bInterfaceSubClass = 1,
1999 .bInterfaceProtocol = 0 },
2000 /* Logitech Quickcam Pro 5000 */
2001 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2002 | USB_DEVICE_ID_MATCH_INT_INFO,
2004 .idProduct = 0x08c5,
2005 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2006 .bInterfaceSubClass = 1,
2007 .bInterfaceProtocol = 0 },
2008 /* Logitech Quickcam OEM Dell Notebook */
2009 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2010 | USB_DEVICE_ID_MATCH_INT_INFO,
2012 .idProduct = 0x08c6,
2013 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2014 .bInterfaceSubClass = 1,
2015 .bInterfaceProtocol = 0 },
2016 /* Logitech Quickcam OEM Cisco VT Camera II */
2017 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2018 | USB_DEVICE_ID_MATCH_INT_INFO,
2020 .idProduct = 0x08c7,
2021 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2022 .bInterfaceSubClass = 1,
2023 .bInterfaceProtocol = 0 },
2024 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2025 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2026 | USB_DEVICE_ID_MATCH_INT_INFO,
2028 .idProduct = 0x3820,
2029 .bInterfaceClass = USB_CLASS_VIDEO,
2030 .bInterfaceSubClass = 1,
2031 .bInterfaceProtocol = 0,
2032 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2033 /* Apple Built-In iSight */
2034 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2035 | USB_DEVICE_ID_MATCH_INT_INFO,
2037 .idProduct = 0x8501,
2038 .bInterfaceClass = USB_CLASS_VIDEO,
2039 .bInterfaceSubClass = 1,
2040 .bInterfaceProtocol = 0,
2041 .driver_info = UVC_QUIRK_PROBE_MINMAX
2042 | UVC_QUIRK_BUILTIN_ISIGHT },
2043 /* Genesys Logic USB 2.0 PC Camera */
2044 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2045 | USB_DEVICE_ID_MATCH_INT_INFO,
2047 .idProduct = 0x0505,
2048 .bInterfaceClass = USB_CLASS_VIDEO,
2049 .bInterfaceSubClass = 1,
2050 .bInterfaceProtocol = 0,
2051 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2053 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2054 | USB_DEVICE_ID_MATCH_INT_INFO,
2056 .idProduct = 0x332d,
2057 .bInterfaceClass = USB_CLASS_VIDEO,
2058 .bInterfaceSubClass = 1,
2059 .bInterfaceProtocol = 0,
2060 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2061 /* ViMicro - Minoru3D */
2062 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2063 | USB_DEVICE_ID_MATCH_INT_INFO,
2065 .idProduct = 0x3410,
2066 .bInterfaceClass = USB_CLASS_VIDEO,
2067 .bInterfaceSubClass = 1,
2068 .bInterfaceProtocol = 0,
2069 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2070 /* ViMicro Venus - Minoru3D */
2071 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2072 | USB_DEVICE_ID_MATCH_INT_INFO,
2074 .idProduct = 0x3420,
2075 .bInterfaceClass = USB_CLASS_VIDEO,
2076 .bInterfaceSubClass = 1,
2077 .bInterfaceProtocol = 0,
2078 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2080 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2081 | USB_DEVICE_ID_MATCH_INT_INFO,
2083 .idProduct = 0x0004,
2084 .bInterfaceClass = USB_CLASS_VIDEO,
2085 .bInterfaceSubClass = 1,
2086 .bInterfaceProtocol = 0,
2087 .driver_info = UVC_QUIRK_PROBE_MINMAX
2088 | UVC_QUIRK_PROBE_DEF },
2089 /* Syntek (HP Spartan) */
2090 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2091 | USB_DEVICE_ID_MATCH_INT_INFO,
2093 .idProduct = 0x5212,
2094 .bInterfaceClass = USB_CLASS_VIDEO,
2095 .bInterfaceSubClass = 1,
2096 .bInterfaceProtocol = 0,
2097 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2098 /* Syntek (Samsung Q310) */
2099 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2100 | USB_DEVICE_ID_MATCH_INT_INFO,
2102 .idProduct = 0x5931,
2103 .bInterfaceClass = USB_CLASS_VIDEO,
2104 .bInterfaceSubClass = 1,
2105 .bInterfaceProtocol = 0,
2106 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2107 /* Syntek (Asus F9SG) */
2108 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2109 | USB_DEVICE_ID_MATCH_INT_INFO,
2111 .idProduct = 0x8a31,
2112 .bInterfaceClass = USB_CLASS_VIDEO,
2113 .bInterfaceSubClass = 1,
2114 .bInterfaceProtocol = 0,
2115 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2116 /* Syntek (Asus U3S) */
2117 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2118 | USB_DEVICE_ID_MATCH_INT_INFO,
2120 .idProduct = 0x8a33,
2121 .bInterfaceClass = USB_CLASS_VIDEO,
2122 .bInterfaceSubClass = 1,
2123 .bInterfaceProtocol = 0,
2124 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2125 /* Syntek (JAOtech Smart Terminal) */
2126 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2127 | USB_DEVICE_ID_MATCH_INT_INFO,
2129 .idProduct = 0x8a34,
2130 .bInterfaceClass = USB_CLASS_VIDEO,
2131 .bInterfaceSubClass = 1,
2132 .bInterfaceProtocol = 0,
2133 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2134 /* Lenovo Thinkpad SL400/SL500 */
2135 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2136 | USB_DEVICE_ID_MATCH_INT_INFO,
2138 .idProduct = 0x480b,
2139 .bInterfaceClass = USB_CLASS_VIDEO,
2140 .bInterfaceSubClass = 1,
2141 .bInterfaceProtocol = 0,
2142 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2143 /* Aveo Technology USB 2.0 Camera */
2144 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2145 | USB_DEVICE_ID_MATCH_INT_INFO,
2147 .idProduct = 0x0306,
2148 .bInterfaceClass = USB_CLASS_VIDEO,
2149 .bInterfaceSubClass = 1,
2150 .bInterfaceProtocol = 0,
2151 .driver_info = UVC_QUIRK_PROBE_MINMAX
2152 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2153 /* Ecamm Pico iMage */
2154 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2155 | USB_DEVICE_ID_MATCH_INT_INFO,
2157 .idProduct = 0xcafe,
2158 .bInterfaceClass = USB_CLASS_VIDEO,
2159 .bInterfaceSubClass = 1,
2160 .bInterfaceProtocol = 0,
2161 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2162 /* FSC WebCam V30S */
2163 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2164 | USB_DEVICE_ID_MATCH_INT_INFO,
2166 .idProduct = 0x3288,
2167 .bInterfaceClass = USB_CLASS_VIDEO,
2168 .bInterfaceSubClass = 1,
2169 .bInterfaceProtocol = 0,
2170 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2171 /* Bodelin ProScopeHR */
2172 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2173 | USB_DEVICE_ID_MATCH_DEV_HI
2174 | USB_DEVICE_ID_MATCH_INT_INFO,
2176 .idProduct = 0x1000,
2177 .bcdDevice_hi = 0x0126,
2178 .bInterfaceClass = USB_CLASS_VIDEO,
2179 .bInterfaceSubClass = 1,
2180 .bInterfaceProtocol = 0,
2181 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2182 /* MSI StarCam 370i */
2183 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2184 | USB_DEVICE_ID_MATCH_INT_INFO,
2186 .idProduct = 0x2951,
2187 .bInterfaceClass = USB_CLASS_VIDEO,
2188 .bInterfaceSubClass = 1,
2189 .bInterfaceProtocol = 0,
2190 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2191 /* SiGma Micro USB Web Camera */
2192 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2193 | USB_DEVICE_ID_MATCH_INT_INFO,
2195 .idProduct = 0x3000,
2196 .bInterfaceClass = USB_CLASS_VIDEO,
2197 .bInterfaceSubClass = 1,
2198 .bInterfaceProtocol = 0,
2199 .driver_info = UVC_QUIRK_PROBE_MINMAX
2200 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2201 /* Generic USB Video Class */
2202 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2206 MODULE_DEVICE_TABLE(usb, uvc_ids);
2208 struct uvc_driver uvc_driver = {
2212 .disconnect = uvc_disconnect,
2213 .suspend = uvc_suspend,
2214 .resume = uvc_resume,
2215 .reset_resume = uvc_reset_resume,
2216 .id_table = uvc_ids,
2217 .supports_autosuspend = 1,
2221 static int __init uvc_init(void)
2225 INIT_LIST_HEAD(&uvc_driver.devices);
2226 INIT_LIST_HEAD(&uvc_driver.controls);
2227 mutex_init(&uvc_driver.ctrl_mutex);
2231 result = usb_register(&uvc_driver.driver);
2233 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2237 static void __exit uvc_cleanup(void)
2239 usb_deregister(&uvc_driver.driver);
2242 module_init(uvc_init);
2243 module_exit(uvc_cleanup);
2245 MODULE_AUTHOR(DRIVER_AUTHOR);
2246 MODULE_DESCRIPTION(DRIVER_DESC);
2247 MODULE_LICENSE("GPL");
2248 MODULE_VERSION(DRIVER_VERSION);