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;
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:0 (NV12)",
64 .guid = UVC_GUID_FORMAT_NV12,
65 .fcc = V4L2_PIX_FMT_NV12,
69 .guid = UVC_GUID_FORMAT_MJPEG,
70 .fcc = V4L2_PIX_FMT_MJPEG,
73 .name = "YVU 4:2:0 (YV12)",
74 .guid = UVC_GUID_FORMAT_YV12,
75 .fcc = V4L2_PIX_FMT_YVU420,
78 .name = "YUV 4:2:0 (I420)",
79 .guid = UVC_GUID_FORMAT_I420,
80 .fcc = V4L2_PIX_FMT_YUV420,
83 .name = "YUV 4:2:2 (UYVY)",
84 .guid = UVC_GUID_FORMAT_UYVY,
85 .fcc = V4L2_PIX_FMT_UYVY,
89 .guid = UVC_GUID_FORMAT_Y800,
90 .fcc = V4L2_PIX_FMT_GREY,
94 .guid = UVC_GUID_FORMAT_BY8,
95 .fcc = V4L2_PIX_FMT_SBGGR8,
99 /* ------------------------------------------------------------------------
103 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
106 struct usb_host_endpoint *ep;
109 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
110 ep = &alts->endpoint[i];
111 if (ep->desc.bEndpointAddress == epaddr)
118 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
120 unsigned int len = ARRAY_SIZE(uvc_fmts);
123 for (i = 0; i < len; ++i) {
124 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
131 static __u32 uvc_colorspace(const __u8 primaries)
133 static const __u8 colorprimaries[] = {
135 V4L2_COLORSPACE_SRGB,
136 V4L2_COLORSPACE_470_SYSTEM_M,
137 V4L2_COLORSPACE_470_SYSTEM_BG,
138 V4L2_COLORSPACE_SMPTE170M,
139 V4L2_COLORSPACE_SMPTE240M,
142 if (primaries < ARRAY_SIZE(colorprimaries))
143 return colorprimaries[primaries];
148 /* Simplify a fraction using a simple continued fraction decomposition. The
149 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
150 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
151 * arbitrary parameters to remove non-significative terms from the simple
152 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
153 * respectively seems to give nice results.
155 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
156 unsigned int n_terms, unsigned int threshold)
162 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
166 /* Convert the fraction to a simple continued fraction. See
167 * http://mathforum.org/dr.math/faq/faq.fractions.html
168 * Stop if the current term is bigger than or equal to the given
174 for (n = 0; n < n_terms && y != 0; ++n) {
176 if (an[n] >= threshold) {
187 /* Expand the simple continued fraction back to an integer fraction. */
191 for (i = n; i > 0; --i) {
202 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
203 * to compute numerator / denominator * 10000000 using 32 bit fixed point
206 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
210 /* Saturate the result if the operation would overflow. */
211 if (denominator == 0 ||
212 numerator/denominator >= ((uint32_t)-1)/10000000)
215 /* Divide both the denominator and the multiplier by two until
216 * numerator * multiplier doesn't overflow. If anyone knows a better
217 * algorithm please let me know.
219 multiplier = 10000000;
220 while (numerator > ((uint32_t)-1)/multiplier) {
225 return denominator ? numerator * multiplier / denominator : 0;
228 /* ------------------------------------------------------------------------
229 * Terminal and unit management
232 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
234 struct uvc_entity *entity;
236 list_for_each_entry(entity, &dev->entities, list) {
237 if (entity->id == id)
244 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
245 int id, struct uvc_entity *entity)
250 entity = list_entry(&dev->entities, struct uvc_entity, list);
252 list_for_each_entry_continue(entity, &dev->entities, list) {
253 for (i = 0; i < entity->bNrInPins; ++i)
254 if (entity->baSourceID[i] == id)
261 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
263 struct uvc_streaming *stream;
265 list_for_each_entry(stream, &dev->streams, list) {
266 if (stream->header.bTerminalLink == id)
273 /* ------------------------------------------------------------------------
274 * Descriptors parsing
277 static int uvc_parse_format(struct uvc_device *dev,
278 struct uvc_streaming *streaming, struct uvc_format *format,
279 __u32 **intervals, unsigned char *buffer, int buflen)
281 struct usb_interface *intf = streaming->intf;
282 struct usb_host_interface *alts = intf->cur_altsetting;
283 struct uvc_format_desc *fmtdesc;
284 struct uvc_frame *frame;
285 const unsigned char *start = buffer;
286 unsigned int interval;
290 format->type = buffer[2];
291 format->index = buffer[3];
294 case UVC_VS_FORMAT_UNCOMPRESSED:
295 case UVC_VS_FORMAT_FRAME_BASED:
296 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
298 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
299 "interface %d FORMAT error\n",
301 alts->desc.bInterfaceNumber);
305 /* Find the format descriptor from its GUID. */
306 fmtdesc = uvc_format_by_guid(&buffer[5]);
308 if (fmtdesc != NULL) {
309 strlcpy(format->name, fmtdesc->name,
310 sizeof format->name);
311 format->fcc = fmtdesc->fcc;
313 uvc_printk(KERN_INFO, "Unknown video format "
314 UVC_GUID_FORMAT "\n",
315 UVC_GUID_ARGS(&buffer[5]));
316 snprintf(format->name, sizeof format->name,
317 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
321 format->bpp = buffer[21];
322 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
323 ftype = UVC_VS_FRAME_UNCOMPRESSED;
325 ftype = UVC_VS_FRAME_FRAME_BASED;
327 format->flags = UVC_FMT_FLAG_COMPRESSED;
331 case UVC_VS_FORMAT_MJPEG:
333 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
334 "interface %d FORMAT error\n",
336 alts->desc.bInterfaceNumber);
340 strlcpy(format->name, "MJPEG", sizeof format->name);
341 format->fcc = V4L2_PIX_FMT_MJPEG;
342 format->flags = UVC_FMT_FLAG_COMPRESSED;
344 ftype = UVC_VS_FRAME_MJPEG;
347 case UVC_VS_FORMAT_DV:
349 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
350 "interface %d FORMAT error\n",
352 alts->desc.bInterfaceNumber);
356 switch (buffer[8] & 0x7f) {
358 strlcpy(format->name, "SD-DV", sizeof format->name);
361 strlcpy(format->name, "SDL-DV", sizeof format->name);
364 strlcpy(format->name, "HD-DV", sizeof format->name);
367 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
368 "interface %d: unknown DV format %u\n",
370 alts->desc.bInterfaceNumber, buffer[8]);
374 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
375 sizeof format->name);
377 format->fcc = V4L2_PIX_FMT_DV;
378 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
382 /* Create a dummy frame descriptor. */
383 frame = &format->frame[0];
384 memset(&format->frame[0], 0, sizeof format->frame[0]);
385 frame->bFrameIntervalType = 1;
386 frame->dwDefaultFrameInterval = 1;
387 frame->dwFrameInterval = *intervals;
392 case UVC_VS_FORMAT_MPEG2TS:
393 case UVC_VS_FORMAT_STREAM_BASED:
394 /* Not supported yet. */
396 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
397 "interface %d unsupported format %u\n",
398 dev->udev->devnum, alts->desc.bInterfaceNumber,
403 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
408 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
409 * based formats have frame descriptors.
411 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
412 buffer[2] == ftype) {
413 frame = &format->frame[format->nframes];
414 if (ftype != UVC_VS_FRAME_FRAME_BASED)
415 n = buflen > 25 ? buffer[25] : 0;
417 n = buflen > 21 ? buffer[21] : 0;
421 if (buflen < 26 + 4*n) {
422 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
423 "interface %d FRAME error\n", dev->udev->devnum,
424 alts->desc.bInterfaceNumber);
428 frame->bFrameIndex = buffer[3];
429 frame->bmCapabilities = buffer[4];
430 frame->wWidth = get_unaligned_le16(&buffer[5]);
431 frame->wHeight = get_unaligned_le16(&buffer[7]);
432 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
433 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
434 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
435 frame->dwMaxVideoFrameBufferSize =
436 get_unaligned_le32(&buffer[17]);
437 frame->dwDefaultFrameInterval =
438 get_unaligned_le32(&buffer[21]);
439 frame->bFrameIntervalType = buffer[25];
441 frame->dwMaxVideoFrameBufferSize = 0;
442 frame->dwDefaultFrameInterval =
443 get_unaligned_le32(&buffer[17]);
444 frame->bFrameIntervalType = buffer[21];
446 frame->dwFrameInterval = *intervals;
448 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
449 * completely. Observed behaviours range from setting the
450 * value to 1.1x the actual frame size to hardwiring the
451 * 16 low bits to 0. This results in a higher than necessary
452 * memory usage as well as a wrong image size information. For
453 * uncompressed formats this can be fixed by computing the
454 * value from the frame size.
456 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
457 frame->dwMaxVideoFrameBufferSize = format->bpp
458 * frame->wWidth * frame->wHeight / 8;
460 /* Some bogus devices report dwMinFrameInterval equal to
461 * dwMaxFrameInterval and have dwFrameIntervalStep set to
462 * zero. Setting all null intervals to 1 fixes the problem and
463 * some other divisions by zero that could happen.
465 for (i = 0; i < n; ++i) {
466 interval = get_unaligned_le32(&buffer[26+4*i]);
467 *(*intervals)++ = interval ? interval : 1;
470 /* Make sure that the default frame interval stays between
473 n -= frame->bFrameIntervalType ? 1 : 2;
474 frame->dwDefaultFrameInterval =
475 min(frame->dwFrameInterval[n],
476 max(frame->dwFrameInterval[0],
477 frame->dwDefaultFrameInterval));
479 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
480 frame->wWidth, frame->wHeight,
481 10000000/frame->dwDefaultFrameInterval,
482 (100000000/frame->dwDefaultFrameInterval)%10);
489 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
490 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
495 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
496 buffer[2] == UVC_VS_COLORFORMAT) {
498 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
499 "interface %d COLORFORMAT error\n",
501 alts->desc.bInterfaceNumber);
505 format->colorspace = uvc_colorspace(buffer[3]);
511 return buffer - start;
514 static int uvc_parse_streaming(struct uvc_device *dev,
515 struct usb_interface *intf)
517 struct uvc_streaming *streaming = NULL;
518 struct uvc_format *format;
519 struct uvc_frame *frame;
520 struct usb_host_interface *alts = &intf->altsetting[0];
521 unsigned char *_buffer, *buffer = alts->extra;
522 int _buflen, buflen = alts->extralen;
523 unsigned int nformats = 0, nframes = 0, nintervals = 0;
524 unsigned int size, i, n, p;
529 if (intf->cur_altsetting->desc.bInterfaceSubClass
530 != UVC_SC_VIDEOSTREAMING) {
531 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
532 "video streaming interface\n", dev->udev->devnum,
533 intf->altsetting[0].desc.bInterfaceNumber);
537 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
538 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
539 "claimed\n", dev->udev->devnum,
540 intf->altsetting[0].desc.bInterfaceNumber);
544 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
545 if (streaming == NULL) {
546 usb_driver_release_interface(&uvc_driver.driver, intf);
550 mutex_init(&streaming->mutex);
551 streaming->dev = dev;
552 streaming->intf = usb_get_intf(intf);
553 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
555 /* The Pico iMage webcam has its class-specific interface descriptors
556 * after the endpoint descriptors.
559 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
560 struct usb_host_endpoint *ep = &alts->endpoint[i];
562 if (ep->extralen == 0)
565 if (ep->extralen > 2 &&
566 ep->extra[1] == USB_DT_CS_INTERFACE) {
567 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
568 "from endpoint %u.\n", i);
569 buffer = alts->endpoint[i].extra;
570 buflen = alts->endpoint[i].extralen;
576 /* Skip the standard interface descriptors. */
577 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
583 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
584 "interface descriptors found.\n");
588 /* Parse the header descriptor. */
590 case UVC_VS_OUTPUT_HEADER:
591 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
595 case UVC_VS_INPUT_HEADER:
596 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
601 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
602 "%d HEADER descriptor not found.\n", dev->udev->devnum,
603 alts->desc.bInterfaceNumber);
607 p = buflen >= 4 ? buffer[3] : 0;
608 n = buflen >= size ? buffer[size-1] : 0;
610 if (buflen < size + p*n) {
611 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
612 "interface %d HEADER descriptor is invalid.\n",
613 dev->udev->devnum, alts->desc.bInterfaceNumber);
617 streaming->header.bNumFormats = p;
618 streaming->header.bEndpointAddress = buffer[6];
619 if (buffer[2] == UVC_VS_INPUT_HEADER) {
620 streaming->header.bmInfo = buffer[7];
621 streaming->header.bTerminalLink = buffer[8];
622 streaming->header.bStillCaptureMethod = buffer[9];
623 streaming->header.bTriggerSupport = buffer[10];
624 streaming->header.bTriggerUsage = buffer[11];
626 streaming->header.bTerminalLink = buffer[7];
628 streaming->header.bControlSize = n;
630 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
631 if (streaming->header.bmaControls == NULL) {
636 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
644 /* Count the format and frame descriptors. */
645 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
646 switch (_buffer[2]) {
647 case UVC_VS_FORMAT_UNCOMPRESSED:
648 case UVC_VS_FORMAT_MJPEG:
649 case UVC_VS_FORMAT_FRAME_BASED:
653 case UVC_VS_FORMAT_DV:
654 /* DV format has no frame descriptor. We will create a
655 * dummy frame descriptor with a dummy frame interval.
662 case UVC_VS_FORMAT_MPEG2TS:
663 case UVC_VS_FORMAT_STREAM_BASED:
664 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
665 "interface %d FORMAT %u is not supported.\n",
667 alts->desc.bInterfaceNumber, _buffer[2]);
670 case UVC_VS_FRAME_UNCOMPRESSED:
671 case UVC_VS_FRAME_MJPEG:
674 nintervals += _buffer[25] ? _buffer[25] : 3;
677 case UVC_VS_FRAME_FRAME_BASED:
680 nintervals += _buffer[21] ? _buffer[21] : 3;
684 _buflen -= _buffer[0];
685 _buffer += _buffer[0];
689 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
690 "%d has no supported formats defined.\n",
691 dev->udev->devnum, alts->desc.bInterfaceNumber);
695 size = nformats * sizeof *format + nframes * sizeof *frame
696 + nintervals * sizeof *interval;
697 format = kzalloc(size, GFP_KERNEL);
698 if (format == NULL) {
703 frame = (struct uvc_frame *)&format[nformats];
704 interval = (__u32 *)&frame[nframes];
706 streaming->format = format;
707 streaming->nformats = nformats;
709 /* Parse the format descriptors. */
710 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
712 case UVC_VS_FORMAT_UNCOMPRESSED:
713 case UVC_VS_FORMAT_MJPEG:
714 case UVC_VS_FORMAT_DV:
715 case UVC_VS_FORMAT_FRAME_BASED:
716 format->frame = frame;
717 ret = uvc_parse_format(dev, streaming, format,
718 &interval, buffer, buflen);
722 frame += format->nframes;
738 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
739 "%d has %u bytes of trailing descriptor garbage.\n",
740 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
742 /* Parse the alternate settings to find the maximum bandwidth. */
743 for (i = 0; i < intf->num_altsetting; ++i) {
744 struct usb_host_endpoint *ep;
745 alts = &intf->altsetting[i];
746 ep = uvc_find_endpoint(alts,
747 streaming->header.bEndpointAddress);
751 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
752 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
753 if (psize > streaming->maxpsize)
754 streaming->maxpsize = psize;
757 list_add_tail(&streaming->list, &dev->streams);
761 usb_driver_release_interface(&uvc_driver.driver, intf);
763 kfree(streaming->format);
764 kfree(streaming->header.bmaControls);
769 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
770 unsigned int num_pads, unsigned int extra_size)
772 struct uvc_entity *entity;
773 unsigned int num_inputs;
776 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
777 size = sizeof(*entity) + extra_size + num_inputs;
778 entity = kzalloc(size, GFP_KERNEL);
785 entity->bNrInPins = num_inputs;
786 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
791 /* Parse vendor-specific extensions. */
792 static int uvc_parse_vendor_control(struct uvc_device *dev,
793 const unsigned char *buffer, int buflen)
795 struct usb_device *udev = dev->udev;
796 struct usb_host_interface *alts = dev->intf->cur_altsetting;
797 struct uvc_entity *unit;
801 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
802 case 0x046d: /* Logitech */
803 if (buffer[1] != 0x41 || buffer[2] != 0x01)
806 /* Logitech implements several vendor specific functions
807 * through vendor specific extension units (LXU).
809 * The LXU descriptors are similar to XU descriptors
810 * (see "USB Device Video Class for Video Devices", section
811 * 3.7.2.6 "Extension Unit Descriptor") with the following
814 * ----------------------------------------------------------
816 * Size of this descriptor, in bytes: 24+p+n*2
817 * ----------------------------------------------------------
818 * 23+p+n bmControlsType N Bitmap
819 * Individual bits in the set are defined:
823 * This bitset is mapped exactly the same as bmControls.
824 * ----------------------------------------------------------
825 * 23+p+n*2 bReserved 1 Boolean
826 * ----------------------------------------------------------
827 * 24+p+n*2 iExtension 1 Index
828 * Index of a string descriptor that describes this
830 * ----------------------------------------------------------
832 p = buflen >= 22 ? buffer[21] : 0;
833 n = buflen >= 25 + p ? buffer[22+p] : 0;
835 if (buflen < 25 + p + 2*n) {
836 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
837 "interface %d EXTENSION_UNIT error\n",
838 udev->devnum, alts->desc.bInterfaceNumber);
842 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
847 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
848 unit->extension.bNumControls = buffer[20];
849 memcpy(unit->baSourceID, &buffer[22], p);
850 unit->extension.bControlSize = buffer[22+p];
851 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
852 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
854 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
856 if (buffer[24+p+2*n] != 0)
857 usb_string(udev, buffer[24+p+2*n], unit->name,
860 sprintf(unit->name, "Extension %u", buffer[3]);
862 list_add_tail(&unit->list, &dev->entities);
870 static int uvc_parse_standard_control(struct uvc_device *dev,
871 const unsigned char *buffer, int buflen)
873 struct usb_device *udev = dev->udev;
874 struct uvc_entity *unit, *term;
875 struct usb_interface *intf;
876 struct usb_host_interface *alts = dev->intf->cur_altsetting;
877 unsigned int i, n, p, len;
882 n = buflen >= 12 ? buffer[11] : 0;
884 if (buflen < 12 || buflen < 12 + n) {
885 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
886 "interface %d HEADER error\n", udev->devnum,
887 alts->desc.bInterfaceNumber);
891 dev->uvc_version = get_unaligned_le16(&buffer[3]);
892 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
894 /* Parse all USB Video Streaming interfaces. */
895 for (i = 0; i < n; ++i) {
896 intf = usb_ifnum_to_if(udev, buffer[12+i]);
898 uvc_trace(UVC_TRACE_DESCR, "device %d "
899 "interface %d doesn't exists\n",
904 uvc_parse_streaming(dev, intf);
908 case UVC_VC_INPUT_TERMINAL:
910 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
911 "interface %d INPUT_TERMINAL error\n",
912 udev->devnum, alts->desc.bInterfaceNumber);
916 /* Make sure the terminal type MSB is not null, otherwise it
917 * could be confused with a unit.
919 type = get_unaligned_le16(&buffer[4]);
920 if ((type & 0xff00) == 0) {
921 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
922 "interface %d INPUT_TERMINAL %d has invalid "
923 "type 0x%04x, skipping\n", udev->devnum,
924 alts->desc.bInterfaceNumber,
933 if (type == UVC_ITT_CAMERA) {
934 n = buflen >= 15 ? buffer[14] : 0;
937 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
938 n = buflen >= 9 ? buffer[8] : 0;
939 p = buflen >= 10 + n ? buffer[9+n] : 0;
943 if (buflen < len + n + p) {
944 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
945 "interface %d INPUT_TERMINAL error\n",
946 udev->devnum, alts->desc.bInterfaceNumber);
950 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
955 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
956 term->camera.bControlSize = n;
957 term->camera.bmControls = (__u8 *)term + sizeof *term;
958 term->camera.wObjectiveFocalLengthMin =
959 get_unaligned_le16(&buffer[8]);
960 term->camera.wObjectiveFocalLengthMax =
961 get_unaligned_le16(&buffer[10]);
962 term->camera.wOcularFocalLength =
963 get_unaligned_le16(&buffer[12]);
964 memcpy(term->camera.bmControls, &buffer[15], n);
965 } else if (UVC_ENTITY_TYPE(term) ==
966 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
967 term->media.bControlSize = n;
968 term->media.bmControls = (__u8 *)term + sizeof *term;
969 term->media.bTransportModeSize = p;
970 term->media.bmTransportModes = (__u8 *)term
972 memcpy(term->media.bmControls, &buffer[9], n);
973 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
977 usb_string(udev, buffer[7], term->name,
979 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
980 sprintf(term->name, "Camera %u", buffer[3]);
981 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
982 sprintf(term->name, "Media %u", buffer[3]);
984 sprintf(term->name, "Input %u", buffer[3]);
986 list_add_tail(&term->list, &dev->entities);
989 case UVC_VC_OUTPUT_TERMINAL:
991 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
992 "interface %d OUTPUT_TERMINAL error\n",
993 udev->devnum, alts->desc.bInterfaceNumber);
997 /* Make sure the terminal type MSB is not null, otherwise it
998 * could be confused with a unit.
1000 type = get_unaligned_le16(&buffer[4]);
1001 if ((type & 0xff00) == 0) {
1002 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1003 "interface %d OUTPUT_TERMINAL %d has invalid "
1004 "type 0x%04x, skipping\n", udev->devnum,
1005 alts->desc.bInterfaceNumber, buffer[3], type);
1009 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1014 memcpy(term->baSourceID, &buffer[7], 1);
1017 usb_string(udev, buffer[8], term->name,
1020 sprintf(term->name, "Output %u", buffer[3]);
1022 list_add_tail(&term->list, &dev->entities);
1025 case UVC_VC_SELECTOR_UNIT:
1026 p = buflen >= 5 ? buffer[4] : 0;
1028 if (buflen < 5 || buflen < 6 + p) {
1029 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1030 "interface %d SELECTOR_UNIT error\n",
1031 udev->devnum, alts->desc.bInterfaceNumber);
1035 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1039 memcpy(unit->baSourceID, &buffer[5], p);
1041 if (buffer[5+p] != 0)
1042 usb_string(udev, buffer[5+p], unit->name,
1045 sprintf(unit->name, "Selector %u", buffer[3]);
1047 list_add_tail(&unit->list, &dev->entities);
1050 case UVC_VC_PROCESSING_UNIT:
1051 n = buflen >= 8 ? buffer[7] : 0;
1052 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1054 if (buflen < p + n) {
1055 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1056 "interface %d PROCESSING_UNIT error\n",
1057 udev->devnum, alts->desc.bInterfaceNumber);
1061 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1065 memcpy(unit->baSourceID, &buffer[4], 1);
1066 unit->processing.wMaxMultiplier =
1067 get_unaligned_le16(&buffer[5]);
1068 unit->processing.bControlSize = buffer[7];
1069 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1070 memcpy(unit->processing.bmControls, &buffer[8], n);
1071 if (dev->uvc_version >= 0x0110)
1072 unit->processing.bmVideoStandards = buffer[9+n];
1074 if (buffer[8+n] != 0)
1075 usb_string(udev, buffer[8+n], unit->name,
1078 sprintf(unit->name, "Processing %u", buffer[3]);
1080 list_add_tail(&unit->list, &dev->entities);
1083 case UVC_VC_EXTENSION_UNIT:
1084 p = buflen >= 22 ? buffer[21] : 0;
1085 n = buflen >= 24 + p ? buffer[22+p] : 0;
1087 if (buflen < 24 + p + n) {
1088 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1089 "interface %d EXTENSION_UNIT error\n",
1090 udev->devnum, alts->desc.bInterfaceNumber);
1094 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1098 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1099 unit->extension.bNumControls = buffer[20];
1100 memcpy(unit->baSourceID, &buffer[22], p);
1101 unit->extension.bControlSize = buffer[22+p];
1102 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1103 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1105 if (buffer[23+p+n] != 0)
1106 usb_string(udev, buffer[23+p+n], unit->name,
1109 sprintf(unit->name, "Extension %u", buffer[3]);
1111 list_add_tail(&unit->list, &dev->entities);
1115 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1116 "descriptor (%u)\n", buffer[2]);
1123 static int uvc_parse_control(struct uvc_device *dev)
1125 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1126 unsigned char *buffer = alts->extra;
1127 int buflen = alts->extralen;
1130 /* Parse the default alternate setting only, as the UVC specification
1131 * defines a single alternate setting, the default alternate setting
1135 while (buflen > 2) {
1136 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1137 buffer[1] != USB_DT_CS_INTERFACE)
1138 goto next_descriptor;
1140 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1144 buflen -= buffer[0];
1145 buffer += buffer[0];
1148 /* Check if the optional status endpoint is present. Built-in iSight
1149 * webcams have an interrupt endpoint but spit proprietary data that
1150 * don't conform to the UVC status endpoint messages. Don't try to
1151 * handle the interrupt endpoint for those cameras.
1153 if (alts->desc.bNumEndpoints == 1 &&
1154 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1155 struct usb_host_endpoint *ep = &alts->endpoint[0];
1156 struct usb_endpoint_descriptor *desc = &ep->desc;
1158 if (usb_endpoint_is_int_in(desc) &&
1159 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1160 desc->bInterval != 0) {
1161 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1162 "(addr %02x).\n", desc->bEndpointAddress);
1170 /* ------------------------------------------------------------------------
1175 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1176 * and containing the following units:
1178 * - one or more Output Terminals (USB Streaming or Display)
1179 * - zero or one Processing Unit
1180 * - zero, one or more single-input Selector Units
1181 * - zero or one multiple-input Selector Units, provided all inputs are
1182 * connected to input terminals
1183 * - zero, one or mode single-input Extension Units
1184 * - one or more Input Terminals (Camera, External or USB Streaming)
1186 * The terminal and units must match on of the following structures:
1188 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1189 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1190 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1192 * +---------+ +---------+ -> OTT_*(0)
1193 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1194 * +---------+ +---------+ -> OTT_*(n)
1196 * The Processing Unit and Extension Units can be in any order. Additional
1197 * Extension Units connected to the main chain as single-unit branches are
1198 * also supported. Single-input Selector Units are ignored.
1200 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1201 struct uvc_entity *entity)
1203 switch (UVC_ENTITY_TYPE(entity)) {
1204 case UVC_VC_EXTENSION_UNIT:
1205 if (uvc_trace_param & UVC_TRACE_PROBE)
1206 printk(" <- XU %d", entity->id);
1208 if (entity->bNrInPins != 1) {
1209 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1210 "than 1 input pin.\n", entity->id);
1216 case UVC_VC_PROCESSING_UNIT:
1217 if (uvc_trace_param & UVC_TRACE_PROBE)
1218 printk(" <- PU %d", entity->id);
1220 if (chain->processing != NULL) {
1221 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1222 "Processing Units in chain.\n");
1226 chain->processing = entity;
1229 case UVC_VC_SELECTOR_UNIT:
1230 if (uvc_trace_param & UVC_TRACE_PROBE)
1231 printk(" <- SU %d", entity->id);
1233 /* Single-input selector units are ignored. */
1234 if (entity->bNrInPins == 1)
1237 if (chain->selector != NULL) {
1238 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1239 "Units in chain.\n");
1243 chain->selector = entity;
1246 case UVC_ITT_VENDOR_SPECIFIC:
1247 case UVC_ITT_CAMERA:
1248 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1249 if (uvc_trace_param & UVC_TRACE_PROBE)
1250 printk(" <- IT %d\n", entity->id);
1254 case UVC_TT_STREAMING:
1255 if (UVC_ENTITY_IS_ITERM(entity)) {
1256 if (uvc_trace_param & UVC_TRACE_PROBE)
1257 printk(" <- IT %d\n", entity->id);
1259 if (uvc_trace_param & UVC_TRACE_PROBE)
1260 printk(" OT %d", entity->id);
1266 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1267 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1271 list_add_tail(&entity->chain, &chain->entities);
1275 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1276 struct uvc_entity *entity, struct uvc_entity *prev)
1278 struct uvc_entity *forward;
1286 forward = uvc_entity_by_reference(chain->dev, entity->id,
1288 if (forward == NULL)
1290 if (forward == prev)
1293 switch (UVC_ENTITY_TYPE(forward)) {
1294 case UVC_VC_EXTENSION_UNIT:
1295 if (forward->bNrInPins != 1) {
1296 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1297 "has more than 1 input pin.\n",
1302 list_add_tail(&forward->chain, &chain->entities);
1303 if (uvc_trace_param & UVC_TRACE_PROBE) {
1307 printk(" XU %d", forward->id);
1312 case UVC_OTT_VENDOR_SPECIFIC:
1313 case UVC_OTT_DISPLAY:
1314 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1315 case UVC_TT_STREAMING:
1316 if (UVC_ENTITY_IS_ITERM(forward)) {
1317 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1318 "terminal %u.\n", forward->id);
1322 list_add_tail(&forward->chain, &chain->entities);
1323 if (uvc_trace_param & UVC_TRACE_PROBE) {
1327 printk(" OT %d", forward->id);
1339 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1340 struct uvc_entity **_entity)
1342 struct uvc_entity *entity = *_entity;
1343 struct uvc_entity *term;
1344 int id = -EINVAL, i;
1346 switch (UVC_ENTITY_TYPE(entity)) {
1347 case UVC_VC_EXTENSION_UNIT:
1348 case UVC_VC_PROCESSING_UNIT:
1349 id = entity->baSourceID[0];
1352 case UVC_VC_SELECTOR_UNIT:
1353 /* Single-input selector units are ignored. */
1354 if (entity->bNrInPins == 1) {
1355 id = entity->baSourceID[0];
1359 if (uvc_trace_param & UVC_TRACE_PROBE)
1362 chain->selector = entity;
1363 for (i = 0; i < entity->bNrInPins; ++i) {
1364 id = entity->baSourceID[i];
1365 term = uvc_entity_by_id(chain->dev, id);
1366 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1367 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1368 "input %d isn't connected to an "
1369 "input terminal\n", entity->id, i);
1373 if (uvc_trace_param & UVC_TRACE_PROBE)
1374 printk(" %d", term->id);
1376 list_add_tail(&term->chain, &chain->entities);
1377 uvc_scan_chain_forward(chain, term, entity);
1380 if (uvc_trace_param & UVC_TRACE_PROBE)
1386 case UVC_ITT_VENDOR_SPECIFIC:
1387 case UVC_ITT_CAMERA:
1388 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1389 case UVC_OTT_VENDOR_SPECIFIC:
1390 case UVC_OTT_DISPLAY:
1391 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1392 case UVC_TT_STREAMING:
1393 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1402 entity = uvc_entity_by_id(chain->dev, id);
1403 if (entity == NULL) {
1404 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1405 "unknown entity %d.\n", id);
1413 static int uvc_scan_chain(struct uvc_video_chain *chain,
1414 struct uvc_entity *term)
1416 struct uvc_entity *entity, *prev;
1418 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1423 while (entity != NULL) {
1424 /* Entity must not be part of an existing chain */
1425 if (entity->chain.next || entity->chain.prev) {
1426 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1427 "entity %d already in chain.\n", entity->id);
1431 /* Process entity */
1432 if (uvc_scan_chain_entity(chain, entity) < 0)
1436 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1441 if (uvc_scan_chain_backward(chain, &entity) < 0)
1448 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1451 struct uvc_entity *term;
1452 unsigned int nterms = 0;
1455 list_for_each_entry(term, terms, chain) {
1456 if (!UVC_ENTITY_IS_TERM(term) ||
1457 UVC_TERM_DIRECTION(term) != dir)
1461 p += sprintf(p, ",");
1462 if (++nterms >= 4) {
1463 p += sprintf(p, "...");
1466 p += sprintf(p, "%u", term->id);
1472 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1474 static char buffer[43];
1477 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1478 p += sprintf(p, " -> ");
1479 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1485 * Scan the device for video chains and register video devices.
1487 * Chains are scanned starting at their output terminals and walked backwards.
1489 static int uvc_scan_device(struct uvc_device *dev)
1491 struct uvc_video_chain *chain;
1492 struct uvc_entity *term;
1494 list_for_each_entry(term, &dev->entities, list) {
1495 if (!UVC_ENTITY_IS_OTERM(term))
1498 /* If the terminal is already included in a chain, skip it.
1499 * This can happen for chains that have multiple output
1500 * terminals, where all output terminals beside the first one
1501 * will be inserted in the chain in forward scans.
1503 if (term->chain.next || term->chain.prev)
1506 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1510 INIT_LIST_HEAD(&chain->entities);
1511 mutex_init(&chain->ctrl_mutex);
1514 if (uvc_scan_chain(chain, term) < 0) {
1519 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1520 uvc_print_chain(chain));
1522 list_add_tail(&chain->list, &dev->chains);
1525 if (list_empty(&dev->chains)) {
1526 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1533 /* ------------------------------------------------------------------------
1534 * Video device registration and unregistration
1538 * Delete the UVC device.
1540 * Called by the kernel when the last reference to the uvc_device structure
1543 * As this function is called after or during disconnect(), all URBs have
1544 * already been canceled by the USB core. There is no need to kill the
1545 * interrupt URB manually.
1547 static void uvc_delete(struct uvc_device *dev)
1549 struct list_head *p, *n;
1551 usb_put_intf(dev->intf);
1552 usb_put_dev(dev->udev);
1554 uvc_status_cleanup(dev);
1555 uvc_ctrl_cleanup_device(dev);
1557 list_for_each_safe(p, n, &dev->chains) {
1558 struct uvc_video_chain *chain;
1559 chain = list_entry(p, struct uvc_video_chain, list);
1563 list_for_each_safe(p, n, &dev->entities) {
1564 struct uvc_entity *entity;
1565 entity = list_entry(p, struct uvc_entity, list);
1569 list_for_each_safe(p, n, &dev->streams) {
1570 struct uvc_streaming *streaming;
1571 streaming = list_entry(p, struct uvc_streaming, list);
1572 usb_driver_release_interface(&uvc_driver.driver,
1574 usb_put_intf(streaming->intf);
1575 kfree(streaming->format);
1576 kfree(streaming->header.bmaControls);
1583 static void uvc_release(struct video_device *vdev)
1585 struct uvc_streaming *stream = video_get_drvdata(vdev);
1586 struct uvc_device *dev = stream->dev;
1588 video_device_release(vdev);
1590 /* Decrement the registered streams count and delete the device when it
1593 if (atomic_dec_and_test(&dev->nstreams))
1598 * Unregister the video devices.
1600 static void uvc_unregister_video(struct uvc_device *dev)
1602 struct uvc_streaming *stream;
1604 /* Unregistering all video devices might result in uvc_delete() being
1605 * called from inside the loop if there's no open file handle. To avoid
1606 * that, increment the stream count before iterating over the streams
1607 * and decrement it when done.
1609 atomic_inc(&dev->nstreams);
1611 list_for_each_entry(stream, &dev->streams, list) {
1612 if (stream->vdev == NULL)
1615 video_unregister_device(stream->vdev);
1616 stream->vdev = NULL;
1619 /* Decrement the stream count and call uvc_delete explicitly if there
1620 * are no stream left.
1622 if (atomic_dec_and_test(&dev->nstreams))
1626 static int uvc_register_video(struct uvc_device *dev,
1627 struct uvc_streaming *stream)
1629 struct video_device *vdev;
1632 /* Initialize the streaming interface with default streaming
1635 ret = uvc_video_init(stream);
1637 uvc_printk(KERN_ERR, "Failed to initialize the device "
1642 /* Register the device with V4L. */
1643 vdev = video_device_alloc();
1645 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1650 /* We already hold a reference to dev->udev. The video device will be
1651 * unregistered before the reference is released, so we don't need to
1654 vdev->parent = &dev->intf->dev;
1655 vdev->fops = &uvc_fops;
1656 vdev->release = uvc_release;
1657 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1659 /* Set the driver data before calling video_register_device, otherwise
1660 * uvc_v4l2_open might race us.
1662 stream->vdev = vdev;
1663 video_set_drvdata(vdev, stream);
1665 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1667 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1669 stream->vdev = NULL;
1670 video_device_release(vdev);
1674 atomic_inc(&dev->nstreams);
1679 * Register all video devices in all chains.
1681 static int uvc_register_terms(struct uvc_device *dev,
1682 struct uvc_video_chain *chain)
1684 struct uvc_streaming *stream;
1685 struct uvc_entity *term;
1688 list_for_each_entry(term, &chain->entities, chain) {
1689 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1692 stream = uvc_stream_by_id(dev, term->id);
1693 if (stream == NULL) {
1694 uvc_printk(KERN_INFO, "No streaming interface found "
1695 "for terminal %u.", term->id);
1699 stream->chain = chain;
1700 ret = uvc_register_video(dev, stream);
1708 static int uvc_register_chains(struct uvc_device *dev)
1710 struct uvc_video_chain *chain;
1713 list_for_each_entry(chain, &dev->chains, list) {
1714 ret = uvc_register_terms(dev, chain);
1722 /* ------------------------------------------------------------------------
1723 * USB probe, disconnect, suspend and resume
1726 static int uvc_probe(struct usb_interface *intf,
1727 const struct usb_device_id *id)
1729 struct usb_device *udev = interface_to_usbdev(intf);
1730 struct uvc_device *dev;
1733 if (id->idVendor && id->idProduct)
1734 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1735 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1738 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1741 /* Allocate memory for the device and initialize it. */
1742 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1745 INIT_LIST_HEAD(&dev->entities);
1746 INIT_LIST_HEAD(&dev->chains);
1747 INIT_LIST_HEAD(&dev->streams);
1748 atomic_set(&dev->nstreams, 0);
1749 atomic_set(&dev->users, 0);
1751 dev->udev = usb_get_dev(udev);
1752 dev->intf = usb_get_intf(intf);
1753 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1754 dev->quirks = id->driver_info | uvc_quirks_param;
1756 if (udev->product != NULL)
1757 strlcpy(dev->name, udev->product, sizeof dev->name);
1759 snprintf(dev->name, sizeof dev->name,
1760 "UVC Camera (%04x:%04x)",
1761 le16_to_cpu(udev->descriptor.idVendor),
1762 le16_to_cpu(udev->descriptor.idProduct));
1764 /* Parse the Video Class control descriptor. */
1765 if (uvc_parse_control(dev) < 0) {
1766 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1771 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1772 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1773 udev->product ? udev->product : "<unnamed>",
1774 le16_to_cpu(udev->descriptor.idVendor),
1775 le16_to_cpu(udev->descriptor.idProduct));
1777 if (uvc_quirks_param != 0) {
1778 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1779 "parameter for testing purpose.\n", uvc_quirks_param);
1780 uvc_printk(KERN_INFO, "Please report required quirks to the "
1781 "linux-uvc-devel mailing list.\n");
1784 /* Initialize controls. */
1785 if (uvc_ctrl_init_device(dev) < 0)
1788 /* Scan the device for video chains. */
1789 if (uvc_scan_device(dev) < 0)
1792 /* Register video devices. */
1793 if (uvc_register_chains(dev) < 0)
1796 /* Save our data pointer in the interface data. */
1797 usb_set_intfdata(intf, dev);
1799 /* Initialize the interrupt URB. */
1800 if ((ret = uvc_status_init(dev)) < 0) {
1801 uvc_printk(KERN_INFO, "Unable to initialize the status "
1802 "endpoint (%d), status interrupt will not be "
1803 "supported.\n", ret);
1806 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1810 uvc_unregister_video(dev);
1814 static void uvc_disconnect(struct usb_interface *intf)
1816 struct uvc_device *dev = usb_get_intfdata(intf);
1818 /* Set the USB interface data to NULL. This can be done outside the
1819 * lock, as there's no other reader.
1821 usb_set_intfdata(intf, NULL);
1823 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1824 UVC_SC_VIDEOSTREAMING)
1827 dev->state |= UVC_DEV_DISCONNECTED;
1829 uvc_unregister_video(dev);
1832 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1834 struct uvc_device *dev = usb_get_intfdata(intf);
1835 struct uvc_streaming *stream;
1837 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1838 intf->cur_altsetting->desc.bInterfaceNumber);
1840 /* Controls are cached on the fly so they don't need to be saved. */
1841 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1842 UVC_SC_VIDEOCONTROL)
1843 return uvc_status_suspend(dev);
1845 list_for_each_entry(stream, &dev->streams, list) {
1846 if (stream->intf == intf)
1847 return uvc_video_suspend(stream);
1850 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1855 static int __uvc_resume(struct usb_interface *intf, int reset)
1857 struct uvc_device *dev = usb_get_intfdata(intf);
1858 struct uvc_streaming *stream;
1860 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1861 intf->cur_altsetting->desc.bInterfaceNumber);
1863 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1864 UVC_SC_VIDEOCONTROL) {
1866 int ret = uvc_ctrl_resume_device(dev);
1872 return uvc_status_resume(dev);
1875 list_for_each_entry(stream, &dev->streams, list) {
1876 if (stream->intf == intf)
1877 return uvc_video_resume(stream);
1880 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1885 static int uvc_resume(struct usb_interface *intf)
1887 return __uvc_resume(intf, 0);
1890 static int uvc_reset_resume(struct usb_interface *intf)
1892 return __uvc_resume(intf, 1);
1895 /* ------------------------------------------------------------------------
1899 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1901 if (uvc_clock_param == CLOCK_MONOTONIC)
1902 return sprintf(buffer, "CLOCK_MONOTONIC");
1904 return sprintf(buffer, "CLOCK_REALTIME");
1907 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1909 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1910 val += strlen("clock_");
1912 if (strcasecmp(val, "monotonic") == 0)
1913 uvc_clock_param = CLOCK_MONOTONIC;
1914 else if (strcasecmp(val, "realtime") == 0)
1915 uvc_clock_param = CLOCK_REALTIME;
1922 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1923 &uvc_clock_param, S_IRUGO|S_IWUSR);
1924 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1925 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1926 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1927 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1928 MODULE_PARM_DESC(quirks, "Forced device quirks");
1929 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1930 MODULE_PARM_DESC(trace, "Trace level bitmask");
1931 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1932 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1934 /* ------------------------------------------------------------------------
1935 * Driver initialization and cleanup
1939 * The Logitech cameras listed below have their interface class set to
1940 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1941 * though they are compliant.
1943 static struct usb_device_id uvc_ids[] = {
1944 /* Genius eFace 2025 */
1945 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1946 | USB_DEVICE_ID_MATCH_INT_INFO,
1948 .idProduct = 0x706e,
1949 .bInterfaceClass = USB_CLASS_VIDEO,
1950 .bInterfaceSubClass = 1,
1951 .bInterfaceProtocol = 0,
1952 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1953 /* Microsoft Lifecam NX-6000 */
1954 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1955 | USB_DEVICE_ID_MATCH_INT_INFO,
1957 .idProduct = 0x00f8,
1958 .bInterfaceClass = USB_CLASS_VIDEO,
1959 .bInterfaceSubClass = 1,
1960 .bInterfaceProtocol = 0,
1961 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1962 /* Microsoft Lifecam VX-7000 */
1963 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1964 | USB_DEVICE_ID_MATCH_INT_INFO,
1966 .idProduct = 0x0723,
1967 .bInterfaceClass = USB_CLASS_VIDEO,
1968 .bInterfaceSubClass = 1,
1969 .bInterfaceProtocol = 0,
1970 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1971 /* Logitech Quickcam Fusion */
1972 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1973 | USB_DEVICE_ID_MATCH_INT_INFO,
1975 .idProduct = 0x08c1,
1976 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1977 .bInterfaceSubClass = 1,
1978 .bInterfaceProtocol = 0 },
1979 /* Logitech Quickcam Orbit MP */
1980 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1981 | USB_DEVICE_ID_MATCH_INT_INFO,
1983 .idProduct = 0x08c2,
1984 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1985 .bInterfaceSubClass = 1,
1986 .bInterfaceProtocol = 0 },
1987 /* Logitech Quickcam Pro for Notebook */
1988 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1989 | USB_DEVICE_ID_MATCH_INT_INFO,
1991 .idProduct = 0x08c3,
1992 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1993 .bInterfaceSubClass = 1,
1994 .bInterfaceProtocol = 0 },
1995 /* Logitech Quickcam Pro 5000 */
1996 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1997 | USB_DEVICE_ID_MATCH_INT_INFO,
1999 .idProduct = 0x08c5,
2000 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2001 .bInterfaceSubClass = 1,
2002 .bInterfaceProtocol = 0 },
2003 /* Logitech Quickcam OEM Dell Notebook */
2004 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2005 | USB_DEVICE_ID_MATCH_INT_INFO,
2007 .idProduct = 0x08c6,
2008 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2009 .bInterfaceSubClass = 1,
2010 .bInterfaceProtocol = 0 },
2011 /* Logitech Quickcam OEM Cisco VT Camera II */
2012 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2013 | USB_DEVICE_ID_MATCH_INT_INFO,
2015 .idProduct = 0x08c7,
2016 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2017 .bInterfaceSubClass = 1,
2018 .bInterfaceProtocol = 0 },
2019 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2020 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2021 | USB_DEVICE_ID_MATCH_INT_INFO,
2023 .idProduct = 0x3820,
2024 .bInterfaceClass = USB_CLASS_VIDEO,
2025 .bInterfaceSubClass = 1,
2026 .bInterfaceProtocol = 0,
2027 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2028 /* Apple Built-In iSight */
2029 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2030 | USB_DEVICE_ID_MATCH_INT_INFO,
2032 .idProduct = 0x8501,
2033 .bInterfaceClass = USB_CLASS_VIDEO,
2034 .bInterfaceSubClass = 1,
2035 .bInterfaceProtocol = 0,
2036 .driver_info = UVC_QUIRK_PROBE_MINMAX
2037 | UVC_QUIRK_BUILTIN_ISIGHT },
2038 /* Genesys Logic USB 2.0 PC Camera */
2039 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2040 | USB_DEVICE_ID_MATCH_INT_INFO,
2042 .idProduct = 0x0505,
2043 .bInterfaceClass = USB_CLASS_VIDEO,
2044 .bInterfaceSubClass = 1,
2045 .bInterfaceProtocol = 0,
2046 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2048 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2049 | USB_DEVICE_ID_MATCH_INT_INFO,
2051 .idProduct = 0x332d,
2052 .bInterfaceClass = USB_CLASS_VIDEO,
2053 .bInterfaceSubClass = 1,
2054 .bInterfaceProtocol = 0,
2055 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2056 /* ViMicro - Minoru3D */
2057 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2058 | USB_DEVICE_ID_MATCH_INT_INFO,
2060 .idProduct = 0x3410,
2061 .bInterfaceClass = USB_CLASS_VIDEO,
2062 .bInterfaceSubClass = 1,
2063 .bInterfaceProtocol = 0,
2064 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2065 /* ViMicro Venus - Minoru3D */
2066 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2067 | USB_DEVICE_ID_MATCH_INT_INFO,
2069 .idProduct = 0x3420,
2070 .bInterfaceClass = USB_CLASS_VIDEO,
2071 .bInterfaceSubClass = 1,
2072 .bInterfaceProtocol = 0,
2073 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2075 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2076 | USB_DEVICE_ID_MATCH_INT_INFO,
2078 .idProduct = 0x0004,
2079 .bInterfaceClass = USB_CLASS_VIDEO,
2080 .bInterfaceSubClass = 1,
2081 .bInterfaceProtocol = 0,
2082 .driver_info = UVC_QUIRK_PROBE_MINMAX
2083 | UVC_QUIRK_PROBE_DEF },
2084 /* Syntek (HP Spartan) */
2085 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2086 | USB_DEVICE_ID_MATCH_INT_INFO,
2088 .idProduct = 0x5212,
2089 .bInterfaceClass = USB_CLASS_VIDEO,
2090 .bInterfaceSubClass = 1,
2091 .bInterfaceProtocol = 0,
2092 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2093 /* Syntek (Samsung Q310) */
2094 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2095 | USB_DEVICE_ID_MATCH_INT_INFO,
2097 .idProduct = 0x5931,
2098 .bInterfaceClass = USB_CLASS_VIDEO,
2099 .bInterfaceSubClass = 1,
2100 .bInterfaceProtocol = 0,
2101 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2102 /* Syntek (Asus F9SG) */
2103 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2104 | USB_DEVICE_ID_MATCH_INT_INFO,
2106 .idProduct = 0x8a31,
2107 .bInterfaceClass = USB_CLASS_VIDEO,
2108 .bInterfaceSubClass = 1,
2109 .bInterfaceProtocol = 0,
2110 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2111 /* Syntek (Asus U3S) */
2112 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2113 | USB_DEVICE_ID_MATCH_INT_INFO,
2115 .idProduct = 0x8a33,
2116 .bInterfaceClass = USB_CLASS_VIDEO,
2117 .bInterfaceSubClass = 1,
2118 .bInterfaceProtocol = 0,
2119 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2120 /* Syntek (JAOtech Smart Terminal) */
2121 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2122 | USB_DEVICE_ID_MATCH_INT_INFO,
2124 .idProduct = 0x8a34,
2125 .bInterfaceClass = USB_CLASS_VIDEO,
2126 .bInterfaceSubClass = 1,
2127 .bInterfaceProtocol = 0,
2128 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2129 /* Lenovo Thinkpad SL400/SL500 */
2130 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2131 | USB_DEVICE_ID_MATCH_INT_INFO,
2133 .idProduct = 0x480b,
2134 .bInterfaceClass = USB_CLASS_VIDEO,
2135 .bInterfaceSubClass = 1,
2136 .bInterfaceProtocol = 0,
2137 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2138 /* Aveo Technology USB 2.0 Camera */
2139 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2140 | USB_DEVICE_ID_MATCH_INT_INFO,
2142 .idProduct = 0x0306,
2143 .bInterfaceClass = USB_CLASS_VIDEO,
2144 .bInterfaceSubClass = 1,
2145 .bInterfaceProtocol = 0,
2146 .driver_info = UVC_QUIRK_PROBE_MINMAX
2147 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2148 /* Ecamm Pico iMage */
2149 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2150 | USB_DEVICE_ID_MATCH_INT_INFO,
2152 .idProduct = 0xcafe,
2153 .bInterfaceClass = USB_CLASS_VIDEO,
2154 .bInterfaceSubClass = 1,
2155 .bInterfaceProtocol = 0,
2156 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2157 /* FSC WebCam V30S */
2158 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2159 | USB_DEVICE_ID_MATCH_INT_INFO,
2161 .idProduct = 0x3288,
2162 .bInterfaceClass = USB_CLASS_VIDEO,
2163 .bInterfaceSubClass = 1,
2164 .bInterfaceProtocol = 0,
2165 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2166 /* Bodelin ProScopeHR */
2167 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2168 | USB_DEVICE_ID_MATCH_DEV_HI
2169 | USB_DEVICE_ID_MATCH_INT_INFO,
2171 .idProduct = 0x1000,
2172 .bcdDevice_hi = 0x0126,
2173 .bInterfaceClass = USB_CLASS_VIDEO,
2174 .bInterfaceSubClass = 1,
2175 .bInterfaceProtocol = 0,
2176 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2177 /* MSI StarCam 370i */
2178 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2179 | USB_DEVICE_ID_MATCH_INT_INFO,
2181 .idProduct = 0x2951,
2182 .bInterfaceClass = USB_CLASS_VIDEO,
2183 .bInterfaceSubClass = 1,
2184 .bInterfaceProtocol = 0,
2185 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2186 /* SiGma Micro USB Web Camera */
2187 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2188 | USB_DEVICE_ID_MATCH_INT_INFO,
2190 .idProduct = 0x3000,
2191 .bInterfaceClass = USB_CLASS_VIDEO,
2192 .bInterfaceSubClass = 1,
2193 .bInterfaceProtocol = 0,
2194 .driver_info = UVC_QUIRK_PROBE_MINMAX
2195 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2196 /* Generic USB Video Class */
2197 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2201 MODULE_DEVICE_TABLE(usb, uvc_ids);
2203 struct uvc_driver uvc_driver = {
2207 .disconnect = uvc_disconnect,
2208 .suspend = uvc_suspend,
2209 .resume = uvc_resume,
2210 .reset_resume = uvc_reset_resume,
2211 .id_table = uvc_ids,
2212 .supports_autosuspend = 1,
2216 static int __init uvc_init(void)
2220 INIT_LIST_HEAD(&uvc_driver.devices);
2221 INIT_LIST_HEAD(&uvc_driver.controls);
2222 mutex_init(&uvc_driver.ctrl_mutex);
2226 result = usb_register(&uvc_driver.driver);
2228 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2232 static void __exit uvc_cleanup(void)
2234 usb_deregister(&uvc_driver.driver);
2237 module_init(uvc_init);
2238 module_exit(uvc_cleanup);
2240 MODULE_AUTHOR(DRIVER_AUTHOR);
2241 MODULE_DESCRIPTION(DRIVER_DESC);
2242 MODULE_LICENSE("GPL");
2243 MODULE_VERSION(DRIVER_VERSION);