2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
15 * This driver aims to support video input and ouput devices compliant with the
16 * 'USB Video Class' specification.
18 * The driver doesn't support the deprecated v4l1 interface. It implements the
19 * mmap capture method only, and doesn't do any image format conversion in
20 * software. If your user-space application doesn't support YUYV or MJPEG, fix
21 * it :-). Please note that the MJPEG data have been stripped from their
22 * Huffman tables (DHT marker), you will need to add it back if your JPEG
23 * codec can't handle MJPEG data.
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/videodev2.h>
31 #include <linux/vmalloc.h>
32 #include <linux/wait.h>
33 #include <asm/atomic.h>
34 #include <asm/unaligned.h>
36 #include <media/v4l2-common.h>
40 #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
41 #define DRIVER_DESC "USB Video Class driver"
42 #ifndef DRIVER_VERSION
43 #define DRIVER_VERSION "v0.1.0"
46 unsigned int uvc_no_drop_param;
47 static unsigned int uvc_quirks_param;
48 unsigned int uvc_trace_param;
49 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
51 /* ------------------------------------------------------------------------
55 static struct uvc_format_desc uvc_fmts[] = {
57 .name = "YUV 4:2:2 (YUYV)",
58 .guid = UVC_GUID_FORMAT_YUY2,
59 .fcc = V4L2_PIX_FMT_YUYV,
62 .name = "YUV 4:2:0 (NV12)",
63 .guid = UVC_GUID_FORMAT_NV12,
64 .fcc = V4L2_PIX_FMT_NV12,
68 .guid = UVC_GUID_FORMAT_MJPEG,
69 .fcc = V4L2_PIX_FMT_MJPEG,
72 .name = "YVU 4:2:0 (YV12)",
73 .guid = UVC_GUID_FORMAT_YV12,
74 .fcc = V4L2_PIX_FMT_YVU420,
77 .name = "YUV 4:2:0 (I420)",
78 .guid = UVC_GUID_FORMAT_I420,
79 .fcc = V4L2_PIX_FMT_YUV420,
82 .name = "YUV 4:2:2 (UYVY)",
83 .guid = UVC_GUID_FORMAT_UYVY,
84 .fcc = V4L2_PIX_FMT_UYVY,
88 .guid = UVC_GUID_FORMAT_Y800,
89 .fcc = V4L2_PIX_FMT_GREY,
93 .guid = UVC_GUID_FORMAT_BY8,
94 .fcc = V4L2_PIX_FMT_SBGGR8,
98 /* ------------------------------------------------------------------------
102 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
105 struct usb_host_endpoint *ep;
108 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
109 ep = &alts->endpoint[i];
110 if (ep->desc.bEndpointAddress == epaddr)
117 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
119 unsigned int len = ARRAY_SIZE(uvc_fmts);
122 for (i = 0; i < len; ++i) {
123 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
130 static __u32 uvc_colorspace(const __u8 primaries)
132 static const __u8 colorprimaries[] = {
134 V4L2_COLORSPACE_SRGB,
135 V4L2_COLORSPACE_470_SYSTEM_M,
136 V4L2_COLORSPACE_470_SYSTEM_BG,
137 V4L2_COLORSPACE_SMPTE170M,
138 V4L2_COLORSPACE_SMPTE240M,
141 if (primaries < ARRAY_SIZE(colorprimaries))
142 return colorprimaries[primaries];
147 /* Simplify a fraction using a simple continued fraction decomposition. The
148 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
149 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
150 * arbitrary parameters to remove non-significative terms from the simple
151 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
152 * respectively seems to give nice results.
154 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
155 unsigned int n_terms, unsigned int threshold)
161 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
165 /* Convert the fraction to a simple continued fraction. See
166 * http://mathforum.org/dr.math/faq/faq.fractions.html
167 * Stop if the current term is bigger than or equal to the given
173 for (n = 0; n < n_terms && y != 0; ++n) {
175 if (an[n] >= threshold) {
186 /* Expand the simple continued fraction back to an integer fraction. */
190 for (i = n; i > 0; --i) {
201 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
202 * to compute numerator / denominator * 10000000 using 32 bit fixed point
205 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
209 /* Saturate the result if the operation would overflow. */
210 if (denominator == 0 ||
211 numerator/denominator >= ((uint32_t)-1)/10000000)
214 /* Divide both the denominator and the multiplier by two until
215 * numerator * multiplier doesn't overflow. If anyone knows a better
216 * algorithm please let me know.
218 multiplier = 10000000;
219 while (numerator > ((uint32_t)-1)/multiplier) {
224 return denominator ? numerator * multiplier / denominator : 0;
227 /* ------------------------------------------------------------------------
228 * Terminal and unit management
231 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
233 struct uvc_entity *entity;
235 list_for_each_entry(entity, &dev->entities, list) {
236 if (entity->id == id)
243 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
244 int id, struct uvc_entity *entity)
249 entity = list_entry(&dev->entities, struct uvc_entity, list);
251 list_for_each_entry_continue(entity, &dev->entities, list) {
252 for (i = 0; i < entity->bNrInPins; ++i)
253 if (entity->baSourceID[i] == id)
260 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
262 struct uvc_streaming *stream;
264 list_for_each_entry(stream, &dev->streams, list) {
265 if (stream->header.bTerminalLink == id)
272 /* ------------------------------------------------------------------------
273 * Descriptors parsing
276 static int uvc_parse_format(struct uvc_device *dev,
277 struct uvc_streaming *streaming, struct uvc_format *format,
278 __u32 **intervals, unsigned char *buffer, int buflen)
280 struct usb_interface *intf = streaming->intf;
281 struct usb_host_interface *alts = intf->cur_altsetting;
282 struct uvc_format_desc *fmtdesc;
283 struct uvc_frame *frame;
284 const unsigned char *start = buffer;
285 unsigned int interval;
289 format->type = buffer[2];
290 format->index = buffer[3];
293 case UVC_VS_FORMAT_UNCOMPRESSED:
294 case UVC_VS_FORMAT_FRAME_BASED:
295 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
297 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
298 "interface %d FORMAT error\n",
300 alts->desc.bInterfaceNumber);
304 /* Find the format descriptor from its GUID. */
305 fmtdesc = uvc_format_by_guid(&buffer[5]);
307 if (fmtdesc != NULL) {
308 strlcpy(format->name, fmtdesc->name,
309 sizeof format->name);
310 format->fcc = fmtdesc->fcc;
312 uvc_printk(KERN_INFO, "Unknown video format "
313 UVC_GUID_FORMAT "\n",
314 UVC_GUID_ARGS(&buffer[5]));
315 snprintf(format->name, sizeof format->name,
316 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
320 format->bpp = buffer[21];
321 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
322 ftype = UVC_VS_FRAME_UNCOMPRESSED;
324 ftype = UVC_VS_FRAME_FRAME_BASED;
326 format->flags = UVC_FMT_FLAG_COMPRESSED;
330 case UVC_VS_FORMAT_MJPEG:
332 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
333 "interface %d FORMAT error\n",
335 alts->desc.bInterfaceNumber);
339 strlcpy(format->name, "MJPEG", sizeof format->name);
340 format->fcc = V4L2_PIX_FMT_MJPEG;
341 format->flags = UVC_FMT_FLAG_COMPRESSED;
343 ftype = UVC_VS_FRAME_MJPEG;
346 case UVC_VS_FORMAT_DV:
348 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
349 "interface %d FORMAT error\n",
351 alts->desc.bInterfaceNumber);
355 switch (buffer[8] & 0x7f) {
357 strlcpy(format->name, "SD-DV", sizeof format->name);
360 strlcpy(format->name, "SDL-DV", sizeof format->name);
363 strlcpy(format->name, "HD-DV", sizeof format->name);
366 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
367 "interface %d: unknown DV format %u\n",
369 alts->desc.bInterfaceNumber, buffer[8]);
373 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
374 sizeof format->name);
376 format->fcc = V4L2_PIX_FMT_DV;
377 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
381 /* Create a dummy frame descriptor. */
382 frame = &format->frame[0];
383 memset(&format->frame[0], 0, sizeof format->frame[0]);
384 frame->bFrameIntervalType = 1;
385 frame->dwDefaultFrameInterval = 1;
386 frame->dwFrameInterval = *intervals;
391 case UVC_VS_FORMAT_MPEG2TS:
392 case UVC_VS_FORMAT_STREAM_BASED:
393 /* Not supported yet. */
395 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
396 "interface %d unsupported format %u\n",
397 dev->udev->devnum, alts->desc.bInterfaceNumber,
402 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
407 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
408 * based formats have frame descriptors.
410 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
411 buffer[2] == ftype) {
412 frame = &format->frame[format->nframes];
413 if (ftype != UVC_VS_FRAME_FRAME_BASED)
414 n = buflen > 25 ? buffer[25] : 0;
416 n = buflen > 21 ? buffer[21] : 0;
420 if (buflen < 26 + 4*n) {
421 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
422 "interface %d FRAME error\n", dev->udev->devnum,
423 alts->desc.bInterfaceNumber);
427 frame->bFrameIndex = buffer[3];
428 frame->bmCapabilities = buffer[4];
429 frame->wWidth = get_unaligned_le16(&buffer[5]);
430 frame->wHeight = get_unaligned_le16(&buffer[7]);
431 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
432 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
433 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
434 frame->dwMaxVideoFrameBufferSize =
435 get_unaligned_le32(&buffer[17]);
436 frame->dwDefaultFrameInterval =
437 get_unaligned_le32(&buffer[21]);
438 frame->bFrameIntervalType = buffer[25];
440 frame->dwMaxVideoFrameBufferSize = 0;
441 frame->dwDefaultFrameInterval =
442 get_unaligned_le32(&buffer[17]);
443 frame->bFrameIntervalType = buffer[21];
445 frame->dwFrameInterval = *intervals;
447 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
448 * completely. Observed behaviours range from setting the
449 * value to 1.1x the actual frame size to hardwiring the
450 * 16 low bits to 0. This results in a higher than necessary
451 * memory usage as well as a wrong image size information. For
452 * uncompressed formats this can be fixed by computing the
453 * value from the frame size.
455 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
456 frame->dwMaxVideoFrameBufferSize = format->bpp
457 * frame->wWidth * frame->wHeight / 8;
459 /* Some bogus devices report dwMinFrameInterval equal to
460 * dwMaxFrameInterval and have dwFrameIntervalStep set to
461 * zero. Setting all null intervals to 1 fixes the problem and
462 * some other divisions by zero that could happen.
464 for (i = 0; i < n; ++i) {
465 interval = get_unaligned_le32(&buffer[26+4*i]);
466 *(*intervals)++ = interval ? interval : 1;
469 /* Make sure that the default frame interval stays between
472 n -= frame->bFrameIntervalType ? 1 : 2;
473 frame->dwDefaultFrameInterval =
474 min(frame->dwFrameInterval[n],
475 max(frame->dwFrameInterval[0],
476 frame->dwDefaultFrameInterval));
478 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
479 frame->wWidth, frame->wHeight,
480 10000000/frame->dwDefaultFrameInterval,
481 (100000000/frame->dwDefaultFrameInterval)%10);
488 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
489 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
494 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
495 buffer[2] == UVC_VS_COLORFORMAT) {
497 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
498 "interface %d COLORFORMAT error\n",
500 alts->desc.bInterfaceNumber);
504 format->colorspace = uvc_colorspace(buffer[3]);
510 return buffer - start;
513 static int uvc_parse_streaming(struct uvc_device *dev,
514 struct usb_interface *intf)
516 struct uvc_streaming *streaming = NULL;
517 struct uvc_format *format;
518 struct uvc_frame *frame;
519 struct usb_host_interface *alts = &intf->altsetting[0];
520 unsigned char *_buffer, *buffer = alts->extra;
521 int _buflen, buflen = alts->extralen;
522 unsigned int nformats = 0, nframes = 0, nintervals = 0;
523 unsigned int size, i, n, p;
528 if (intf->cur_altsetting->desc.bInterfaceSubClass
529 != UVC_SC_VIDEOSTREAMING) {
530 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
531 "video streaming interface\n", dev->udev->devnum,
532 intf->altsetting[0].desc.bInterfaceNumber);
536 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
537 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
538 "claimed\n", dev->udev->devnum,
539 intf->altsetting[0].desc.bInterfaceNumber);
543 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
544 if (streaming == NULL) {
545 usb_driver_release_interface(&uvc_driver.driver, intf);
549 mutex_init(&streaming->mutex);
550 streaming->dev = dev;
551 streaming->intf = usb_get_intf(intf);
552 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
554 /* The Pico iMage webcam has its class-specific interface descriptors
555 * after the endpoint descriptors.
558 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
559 struct usb_host_endpoint *ep = &alts->endpoint[i];
561 if (ep->extralen == 0)
564 if (ep->extralen > 2 &&
565 ep->extra[1] == USB_DT_CS_INTERFACE) {
566 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
567 "from endpoint %u.\n", i);
568 buffer = alts->endpoint[i].extra;
569 buflen = alts->endpoint[i].extralen;
575 /* Skip the standard interface descriptors. */
576 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
582 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
583 "interface descriptors found.\n");
587 /* Parse the header descriptor. */
589 case UVC_VS_OUTPUT_HEADER:
590 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
594 case UVC_VS_INPUT_HEADER:
595 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
600 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
601 "%d HEADER descriptor not found.\n", dev->udev->devnum,
602 alts->desc.bInterfaceNumber);
606 p = buflen >= 4 ? buffer[3] : 0;
607 n = buflen >= size ? buffer[size-1] : 0;
609 if (buflen < size + p*n) {
610 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
611 "interface %d HEADER descriptor is invalid.\n",
612 dev->udev->devnum, alts->desc.bInterfaceNumber);
616 streaming->header.bNumFormats = p;
617 streaming->header.bEndpointAddress = buffer[6];
618 if (buffer[2] == UVC_VS_INPUT_HEADER) {
619 streaming->header.bmInfo = buffer[7];
620 streaming->header.bTerminalLink = buffer[8];
621 streaming->header.bStillCaptureMethod = buffer[9];
622 streaming->header.bTriggerSupport = buffer[10];
623 streaming->header.bTriggerUsage = buffer[11];
625 streaming->header.bTerminalLink = buffer[7];
627 streaming->header.bControlSize = n;
629 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
630 if (streaming->header.bmaControls == NULL) {
635 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
643 /* Count the format and frame descriptors. */
644 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
645 switch (_buffer[2]) {
646 case UVC_VS_FORMAT_UNCOMPRESSED:
647 case UVC_VS_FORMAT_MJPEG:
648 case UVC_VS_FORMAT_FRAME_BASED:
652 case UVC_VS_FORMAT_DV:
653 /* DV format has no frame descriptor. We will create a
654 * dummy frame descriptor with a dummy frame interval.
661 case UVC_VS_FORMAT_MPEG2TS:
662 case UVC_VS_FORMAT_STREAM_BASED:
663 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
664 "interface %d FORMAT %u is not supported.\n",
666 alts->desc.bInterfaceNumber, _buffer[2]);
669 case UVC_VS_FRAME_UNCOMPRESSED:
670 case UVC_VS_FRAME_MJPEG:
673 nintervals += _buffer[25] ? _buffer[25] : 3;
676 case UVC_VS_FRAME_FRAME_BASED:
679 nintervals += _buffer[21] ? _buffer[21] : 3;
683 _buflen -= _buffer[0];
684 _buffer += _buffer[0];
688 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
689 "%d has no supported formats defined.\n",
690 dev->udev->devnum, alts->desc.bInterfaceNumber);
694 size = nformats * sizeof *format + nframes * sizeof *frame
695 + nintervals * sizeof *interval;
696 format = kzalloc(size, GFP_KERNEL);
697 if (format == NULL) {
702 frame = (struct uvc_frame *)&format[nformats];
703 interval = (__u32 *)&frame[nframes];
705 streaming->format = format;
706 streaming->nformats = nformats;
708 /* Parse the format descriptors. */
709 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
711 case UVC_VS_FORMAT_UNCOMPRESSED:
712 case UVC_VS_FORMAT_MJPEG:
713 case UVC_VS_FORMAT_DV:
714 case UVC_VS_FORMAT_FRAME_BASED:
715 format->frame = frame;
716 ret = uvc_parse_format(dev, streaming, format,
717 &interval, buffer, buflen);
721 frame += format->nframes;
737 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
738 "%d has %u bytes of trailing descriptor garbage.\n",
739 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
741 /* Parse the alternate settings to find the maximum bandwidth. */
742 for (i = 0; i < intf->num_altsetting; ++i) {
743 struct usb_host_endpoint *ep;
744 alts = &intf->altsetting[i];
745 ep = uvc_find_endpoint(alts,
746 streaming->header.bEndpointAddress);
750 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
751 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
752 if (psize > streaming->maxpsize)
753 streaming->maxpsize = psize;
756 list_add_tail(&streaming->list, &dev->streams);
760 usb_driver_release_interface(&uvc_driver.driver, intf);
762 kfree(streaming->format);
763 kfree(streaming->header.bmaControls);
768 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
769 unsigned int num_pads, unsigned int extra_size)
771 struct uvc_entity *entity;
772 unsigned int num_inputs;
775 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
776 size = sizeof(*entity) + extra_size + num_inputs;
777 entity = kzalloc(size, GFP_KERNEL);
784 entity->bNrInPins = num_inputs;
785 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
790 /* Parse vendor-specific extensions. */
791 static int uvc_parse_vendor_control(struct uvc_device *dev,
792 const unsigned char *buffer, int buflen)
794 struct usb_device *udev = dev->udev;
795 struct usb_host_interface *alts = dev->intf->cur_altsetting;
796 struct uvc_entity *unit;
800 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
801 case 0x046d: /* Logitech */
802 if (buffer[1] != 0x41 || buffer[2] != 0x01)
805 /* Logitech implements several vendor specific functions
806 * through vendor specific extension units (LXU).
808 * The LXU descriptors are similar to XU descriptors
809 * (see "USB Device Video Class for Video Devices", section
810 * 3.7.2.6 "Extension Unit Descriptor") with the following
813 * ----------------------------------------------------------
815 * Size of this descriptor, in bytes: 24+p+n*2
816 * ----------------------------------------------------------
817 * 23+p+n bmControlsType N Bitmap
818 * Individual bits in the set are defined:
822 * This bitset is mapped exactly the same as bmControls.
823 * ----------------------------------------------------------
824 * 23+p+n*2 bReserved 1 Boolean
825 * ----------------------------------------------------------
826 * 24+p+n*2 iExtension 1 Index
827 * Index of a string descriptor that describes this
829 * ----------------------------------------------------------
831 p = buflen >= 22 ? buffer[21] : 0;
832 n = buflen >= 25 + p ? buffer[22+p] : 0;
834 if (buflen < 25 + p + 2*n) {
835 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
836 "interface %d EXTENSION_UNIT error\n",
837 udev->devnum, alts->desc.bInterfaceNumber);
841 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
846 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
847 unit->extension.bNumControls = buffer[20];
848 memcpy(unit->baSourceID, &buffer[22], p);
849 unit->extension.bControlSize = buffer[22+p];
850 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
851 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
853 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
855 if (buffer[24+p+2*n] != 0)
856 usb_string(udev, buffer[24+p+2*n], unit->name,
859 sprintf(unit->name, "Extension %u", buffer[3]);
861 list_add_tail(&unit->list, &dev->entities);
869 static int uvc_parse_standard_control(struct uvc_device *dev,
870 const unsigned char *buffer, int buflen)
872 struct usb_device *udev = dev->udev;
873 struct uvc_entity *unit, *term;
874 struct usb_interface *intf;
875 struct usb_host_interface *alts = dev->intf->cur_altsetting;
876 unsigned int i, n, p, len;
881 n = buflen >= 12 ? buffer[11] : 0;
883 if (buflen < 12 || buflen < 12 + n) {
884 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
885 "interface %d HEADER error\n", udev->devnum,
886 alts->desc.bInterfaceNumber);
890 dev->uvc_version = get_unaligned_le16(&buffer[3]);
891 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
893 /* Parse all USB Video Streaming interfaces. */
894 for (i = 0; i < n; ++i) {
895 intf = usb_ifnum_to_if(udev, buffer[12+i]);
897 uvc_trace(UVC_TRACE_DESCR, "device %d "
898 "interface %d doesn't exists\n",
903 uvc_parse_streaming(dev, intf);
907 case UVC_VC_INPUT_TERMINAL:
909 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
910 "interface %d INPUT_TERMINAL error\n",
911 udev->devnum, alts->desc.bInterfaceNumber);
915 /* Make sure the terminal type MSB is not null, otherwise it
916 * could be confused with a unit.
918 type = get_unaligned_le16(&buffer[4]);
919 if ((type & 0xff00) == 0) {
920 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
921 "interface %d INPUT_TERMINAL %d has invalid "
922 "type 0x%04x, skipping\n", udev->devnum,
923 alts->desc.bInterfaceNumber,
932 if (type == UVC_ITT_CAMERA) {
933 n = buflen >= 15 ? buffer[14] : 0;
936 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
937 n = buflen >= 9 ? buffer[8] : 0;
938 p = buflen >= 10 + n ? buffer[9+n] : 0;
942 if (buflen < len + n + p) {
943 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
944 "interface %d INPUT_TERMINAL error\n",
945 udev->devnum, alts->desc.bInterfaceNumber);
949 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
954 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
955 term->camera.bControlSize = n;
956 term->camera.bmControls = (__u8 *)term + sizeof *term;
957 term->camera.wObjectiveFocalLengthMin =
958 get_unaligned_le16(&buffer[8]);
959 term->camera.wObjectiveFocalLengthMax =
960 get_unaligned_le16(&buffer[10]);
961 term->camera.wOcularFocalLength =
962 get_unaligned_le16(&buffer[12]);
963 memcpy(term->camera.bmControls, &buffer[15], n);
964 } else if (UVC_ENTITY_TYPE(term) ==
965 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
966 term->media.bControlSize = n;
967 term->media.bmControls = (__u8 *)term + sizeof *term;
968 term->media.bTransportModeSize = p;
969 term->media.bmTransportModes = (__u8 *)term
971 memcpy(term->media.bmControls, &buffer[9], n);
972 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
976 usb_string(udev, buffer[7], term->name,
978 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
979 sprintf(term->name, "Camera %u", buffer[3]);
980 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
981 sprintf(term->name, "Media %u", buffer[3]);
983 sprintf(term->name, "Input %u", buffer[3]);
985 list_add_tail(&term->list, &dev->entities);
988 case UVC_VC_OUTPUT_TERMINAL:
990 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
991 "interface %d OUTPUT_TERMINAL error\n",
992 udev->devnum, alts->desc.bInterfaceNumber);
996 /* Make sure the terminal type MSB is not null, otherwise it
997 * could be confused with a unit.
999 type = get_unaligned_le16(&buffer[4]);
1000 if ((type & 0xff00) == 0) {
1001 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1002 "interface %d OUTPUT_TERMINAL %d has invalid "
1003 "type 0x%04x, skipping\n", udev->devnum,
1004 alts->desc.bInterfaceNumber, buffer[3], type);
1008 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1013 memcpy(term->baSourceID, &buffer[7], 1);
1016 usb_string(udev, buffer[8], term->name,
1019 sprintf(term->name, "Output %u", buffer[3]);
1021 list_add_tail(&term->list, &dev->entities);
1024 case UVC_VC_SELECTOR_UNIT:
1025 p = buflen >= 5 ? buffer[4] : 0;
1027 if (buflen < 5 || buflen < 6 + p) {
1028 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1029 "interface %d SELECTOR_UNIT error\n",
1030 udev->devnum, alts->desc.bInterfaceNumber);
1034 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1038 memcpy(unit->baSourceID, &buffer[5], p);
1040 if (buffer[5+p] != 0)
1041 usb_string(udev, buffer[5+p], unit->name,
1044 sprintf(unit->name, "Selector %u", buffer[3]);
1046 list_add_tail(&unit->list, &dev->entities);
1049 case UVC_VC_PROCESSING_UNIT:
1050 n = buflen >= 8 ? buffer[7] : 0;
1051 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1053 if (buflen < p + n) {
1054 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1055 "interface %d PROCESSING_UNIT error\n",
1056 udev->devnum, alts->desc.bInterfaceNumber);
1060 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1064 memcpy(unit->baSourceID, &buffer[4], 1);
1065 unit->processing.wMaxMultiplier =
1066 get_unaligned_le16(&buffer[5]);
1067 unit->processing.bControlSize = buffer[7];
1068 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1069 memcpy(unit->processing.bmControls, &buffer[8], n);
1070 if (dev->uvc_version >= 0x0110)
1071 unit->processing.bmVideoStandards = buffer[9+n];
1073 if (buffer[8+n] != 0)
1074 usb_string(udev, buffer[8+n], unit->name,
1077 sprintf(unit->name, "Processing %u", buffer[3]);
1079 list_add_tail(&unit->list, &dev->entities);
1082 case UVC_VC_EXTENSION_UNIT:
1083 p = buflen >= 22 ? buffer[21] : 0;
1084 n = buflen >= 24 + p ? buffer[22+p] : 0;
1086 if (buflen < 24 + p + n) {
1087 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1088 "interface %d EXTENSION_UNIT error\n",
1089 udev->devnum, alts->desc.bInterfaceNumber);
1093 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1097 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1098 unit->extension.bNumControls = buffer[20];
1099 memcpy(unit->baSourceID, &buffer[22], p);
1100 unit->extension.bControlSize = buffer[22+p];
1101 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1102 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1104 if (buffer[23+p+n] != 0)
1105 usb_string(udev, buffer[23+p+n], unit->name,
1108 sprintf(unit->name, "Extension %u", buffer[3]);
1110 list_add_tail(&unit->list, &dev->entities);
1114 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1115 "descriptor (%u)\n", buffer[2]);
1122 static int uvc_parse_control(struct uvc_device *dev)
1124 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1125 unsigned char *buffer = alts->extra;
1126 int buflen = alts->extralen;
1129 /* Parse the default alternate setting only, as the UVC specification
1130 * defines a single alternate setting, the default alternate setting
1134 while (buflen > 2) {
1135 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1136 buffer[1] != USB_DT_CS_INTERFACE)
1137 goto next_descriptor;
1139 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1143 buflen -= buffer[0];
1144 buffer += buffer[0];
1147 /* Check if the optional status endpoint is present. Built-in iSight
1148 * webcams have an interrupt endpoint but spit proprietary data that
1149 * don't conform to the UVC status endpoint messages. Don't try to
1150 * handle the interrupt endpoint for those cameras.
1152 if (alts->desc.bNumEndpoints == 1 &&
1153 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1154 struct usb_host_endpoint *ep = &alts->endpoint[0];
1155 struct usb_endpoint_descriptor *desc = &ep->desc;
1157 if (usb_endpoint_is_int_in(desc) &&
1158 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1159 desc->bInterval != 0) {
1160 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1161 "(addr %02x).\n", desc->bEndpointAddress);
1169 /* ------------------------------------------------------------------------
1174 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1175 * and containing the following units:
1177 * - one or more Output Terminals (USB Streaming or Display)
1178 * - zero or one Processing Unit
1179 * - zero, one or more single-input Selector Units
1180 * - zero or one multiple-input Selector Units, provided all inputs are
1181 * connected to input terminals
1182 * - zero, one or mode single-input Extension Units
1183 * - one or more Input Terminals (Camera, External or USB Streaming)
1185 * The terminal and units must match on of the following structures:
1187 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1188 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1189 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1191 * +---------+ +---------+ -> OTT_*(0)
1192 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1193 * +---------+ +---------+ -> OTT_*(n)
1195 * The Processing Unit and Extension Units can be in any order. Additional
1196 * Extension Units connected to the main chain as single-unit branches are
1197 * also supported. Single-input Selector Units are ignored.
1199 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1200 struct uvc_entity *entity)
1202 switch (UVC_ENTITY_TYPE(entity)) {
1203 case UVC_VC_EXTENSION_UNIT:
1204 if (uvc_trace_param & UVC_TRACE_PROBE)
1205 printk(" <- XU %d", entity->id);
1207 if (entity->bNrInPins != 1) {
1208 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1209 "than 1 input pin.\n", entity->id);
1215 case UVC_VC_PROCESSING_UNIT:
1216 if (uvc_trace_param & UVC_TRACE_PROBE)
1217 printk(" <- PU %d", entity->id);
1219 if (chain->processing != NULL) {
1220 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1221 "Processing Units in chain.\n");
1225 chain->processing = entity;
1228 case UVC_VC_SELECTOR_UNIT:
1229 if (uvc_trace_param & UVC_TRACE_PROBE)
1230 printk(" <- SU %d", entity->id);
1232 /* Single-input selector units are ignored. */
1233 if (entity->bNrInPins == 1)
1236 if (chain->selector != NULL) {
1237 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1238 "Units in chain.\n");
1242 chain->selector = entity;
1245 case UVC_ITT_VENDOR_SPECIFIC:
1246 case UVC_ITT_CAMERA:
1247 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1248 if (uvc_trace_param & UVC_TRACE_PROBE)
1249 printk(" <- IT %d\n", entity->id);
1253 case UVC_TT_STREAMING:
1254 if (UVC_ENTITY_IS_ITERM(entity)) {
1255 if (uvc_trace_param & UVC_TRACE_PROBE)
1256 printk(" <- IT %d\n", entity->id);
1258 if (uvc_trace_param & UVC_TRACE_PROBE)
1259 printk(" OT %d", entity->id);
1265 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1266 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1270 list_add_tail(&entity->chain, &chain->entities);
1274 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1275 struct uvc_entity *entity, struct uvc_entity *prev)
1277 struct uvc_entity *forward;
1285 forward = uvc_entity_by_reference(chain->dev, entity->id,
1287 if (forward == NULL)
1289 if (forward == prev)
1292 switch (UVC_ENTITY_TYPE(forward)) {
1293 case UVC_VC_EXTENSION_UNIT:
1294 if (forward->bNrInPins != 1) {
1295 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1296 "has more than 1 input pin.\n",
1301 list_add_tail(&forward->chain, &chain->entities);
1302 if (uvc_trace_param & UVC_TRACE_PROBE) {
1306 printk(" XU %d", forward->id);
1311 case UVC_OTT_VENDOR_SPECIFIC:
1312 case UVC_OTT_DISPLAY:
1313 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1314 case UVC_TT_STREAMING:
1315 if (UVC_ENTITY_IS_ITERM(forward)) {
1316 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1317 "terminal %u.\n", forward->id);
1321 list_add_tail(&forward->chain, &chain->entities);
1322 if (uvc_trace_param & UVC_TRACE_PROBE) {
1326 printk(" OT %d", forward->id);
1338 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1339 struct uvc_entity **_entity)
1341 struct uvc_entity *entity = *_entity;
1342 struct uvc_entity *term;
1343 int id = -EINVAL, i;
1345 switch (UVC_ENTITY_TYPE(entity)) {
1346 case UVC_VC_EXTENSION_UNIT:
1347 case UVC_VC_PROCESSING_UNIT:
1348 id = entity->baSourceID[0];
1351 case UVC_VC_SELECTOR_UNIT:
1352 /* Single-input selector units are ignored. */
1353 if (entity->bNrInPins == 1) {
1354 id = entity->baSourceID[0];
1358 if (uvc_trace_param & UVC_TRACE_PROBE)
1361 chain->selector = entity;
1362 for (i = 0; i < entity->bNrInPins; ++i) {
1363 id = entity->baSourceID[i];
1364 term = uvc_entity_by_id(chain->dev, id);
1365 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1366 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1367 "input %d isn't connected to an "
1368 "input terminal\n", entity->id, i);
1372 if (uvc_trace_param & UVC_TRACE_PROBE)
1373 printk(" %d", term->id);
1375 list_add_tail(&term->chain, &chain->entities);
1376 uvc_scan_chain_forward(chain, term, entity);
1379 if (uvc_trace_param & UVC_TRACE_PROBE)
1385 case UVC_ITT_VENDOR_SPECIFIC:
1386 case UVC_ITT_CAMERA:
1387 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1388 case UVC_OTT_VENDOR_SPECIFIC:
1389 case UVC_OTT_DISPLAY:
1390 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1391 case UVC_TT_STREAMING:
1392 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1401 entity = uvc_entity_by_id(chain->dev, id);
1402 if (entity == NULL) {
1403 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1404 "unknown entity %d.\n", id);
1412 static int uvc_scan_chain(struct uvc_video_chain *chain,
1413 struct uvc_entity *term)
1415 struct uvc_entity *entity, *prev;
1417 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1422 while (entity != NULL) {
1423 /* Entity must not be part of an existing chain */
1424 if (entity->chain.next || entity->chain.prev) {
1425 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1426 "entity %d already in chain.\n", entity->id);
1430 /* Process entity */
1431 if (uvc_scan_chain_entity(chain, entity) < 0)
1435 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1440 if (uvc_scan_chain_backward(chain, &entity) < 0)
1447 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1450 struct uvc_entity *term;
1451 unsigned int nterms = 0;
1454 list_for_each_entry(term, terms, chain) {
1455 if (!UVC_ENTITY_IS_TERM(term) ||
1456 UVC_TERM_DIRECTION(term) != dir)
1460 p += sprintf(p, ",");
1461 if (++nterms >= 4) {
1462 p += sprintf(p, "...");
1465 p += sprintf(p, "%u", term->id);
1471 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1473 static char buffer[43];
1476 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1477 p += sprintf(p, " -> ");
1478 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1484 * Scan the device for video chains and register video devices.
1486 * Chains are scanned starting at their output terminals and walked backwards.
1488 static int uvc_scan_device(struct uvc_device *dev)
1490 struct uvc_video_chain *chain;
1491 struct uvc_entity *term;
1493 list_for_each_entry(term, &dev->entities, list) {
1494 if (!UVC_ENTITY_IS_OTERM(term))
1497 /* If the terminal is already included in a chain, skip it.
1498 * This can happen for chains that have multiple output
1499 * terminals, where all output terminals beside the first one
1500 * will be inserted in the chain in forward scans.
1502 if (term->chain.next || term->chain.prev)
1505 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1509 INIT_LIST_HEAD(&chain->entities);
1510 mutex_init(&chain->ctrl_mutex);
1513 if (uvc_scan_chain(chain, term) < 0) {
1518 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1519 uvc_print_chain(chain));
1521 list_add_tail(&chain->list, &dev->chains);
1524 if (list_empty(&dev->chains)) {
1525 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1532 /* ------------------------------------------------------------------------
1533 * Video device registration and unregistration
1537 * Delete the UVC device.
1539 * Called by the kernel when the last reference to the uvc_device structure
1542 * As this function is called after or during disconnect(), all URBs have
1543 * already been canceled by the USB core. There is no need to kill the
1544 * interrupt URB manually.
1546 static void uvc_delete(struct uvc_device *dev)
1548 struct list_head *p, *n;
1550 usb_put_intf(dev->intf);
1551 usb_put_dev(dev->udev);
1553 uvc_status_cleanup(dev);
1554 uvc_ctrl_cleanup_device(dev);
1556 list_for_each_safe(p, n, &dev->chains) {
1557 struct uvc_video_chain *chain;
1558 chain = list_entry(p, struct uvc_video_chain, list);
1562 list_for_each_safe(p, n, &dev->entities) {
1563 struct uvc_entity *entity;
1564 entity = list_entry(p, struct uvc_entity, list);
1568 list_for_each_safe(p, n, &dev->streams) {
1569 struct uvc_streaming *streaming;
1570 streaming = list_entry(p, struct uvc_streaming, list);
1571 usb_driver_release_interface(&uvc_driver.driver,
1573 usb_put_intf(streaming->intf);
1574 kfree(streaming->format);
1575 kfree(streaming->header.bmaControls);
1582 static void uvc_release(struct video_device *vdev)
1584 struct uvc_streaming *stream = video_get_drvdata(vdev);
1585 struct uvc_device *dev = stream->dev;
1587 video_device_release(vdev);
1589 /* Decrement the registered streams count and delete the device when it
1592 if (atomic_dec_and_test(&dev->nstreams))
1597 * Unregister the video devices.
1599 static void uvc_unregister_video(struct uvc_device *dev)
1601 struct uvc_streaming *stream;
1603 /* Unregistering all video devices might result in uvc_delete() being
1604 * called from inside the loop if there's no open file handle. To avoid
1605 * that, increment the stream count before iterating over the streams
1606 * and decrement it when done.
1608 atomic_inc(&dev->nstreams);
1610 list_for_each_entry(stream, &dev->streams, list) {
1611 if (stream->vdev == NULL)
1614 video_unregister_device(stream->vdev);
1615 stream->vdev = NULL;
1618 /* Decrement the stream count and call uvc_delete explicitly if there
1619 * are no stream left.
1621 if (atomic_dec_and_test(&dev->nstreams))
1625 static int uvc_register_video(struct uvc_device *dev,
1626 struct uvc_streaming *stream)
1628 struct video_device *vdev;
1631 /* Initialize the streaming interface with default streaming
1634 ret = uvc_video_init(stream);
1636 uvc_printk(KERN_ERR, "Failed to initialize the device "
1641 /* Register the device with V4L. */
1642 vdev = video_device_alloc();
1644 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1649 /* We already hold a reference to dev->udev. The video device will be
1650 * unregistered before the reference is released, so we don't need to
1653 vdev->parent = &dev->intf->dev;
1654 vdev->fops = &uvc_fops;
1655 vdev->release = uvc_release;
1656 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1658 /* Set the driver data before calling video_register_device, otherwise
1659 * uvc_v4l2_open might race us.
1661 stream->vdev = vdev;
1662 video_set_drvdata(vdev, stream);
1664 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1666 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1668 stream->vdev = NULL;
1669 video_device_release(vdev);
1673 atomic_inc(&dev->nstreams);
1678 * Register all video devices in all chains.
1680 static int uvc_register_terms(struct uvc_device *dev,
1681 struct uvc_video_chain *chain)
1683 struct uvc_streaming *stream;
1684 struct uvc_entity *term;
1687 list_for_each_entry(term, &chain->entities, chain) {
1688 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1691 stream = uvc_stream_by_id(dev, term->id);
1692 if (stream == NULL) {
1693 uvc_printk(KERN_INFO, "No streaming interface found "
1694 "for terminal %u.", term->id);
1698 stream->chain = chain;
1699 ret = uvc_register_video(dev, stream);
1707 static int uvc_register_chains(struct uvc_device *dev)
1709 struct uvc_video_chain *chain;
1712 list_for_each_entry(chain, &dev->chains, list) {
1713 ret = uvc_register_terms(dev, chain);
1721 /* ------------------------------------------------------------------------
1722 * USB probe, disconnect, suspend and resume
1725 static int uvc_probe(struct usb_interface *intf,
1726 const struct usb_device_id *id)
1728 struct usb_device *udev = interface_to_usbdev(intf);
1729 struct uvc_device *dev;
1732 if (id->idVendor && id->idProduct)
1733 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1734 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1737 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1740 /* Allocate memory for the device and initialize it. */
1741 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1744 INIT_LIST_HEAD(&dev->entities);
1745 INIT_LIST_HEAD(&dev->chains);
1746 INIT_LIST_HEAD(&dev->streams);
1747 atomic_set(&dev->nstreams, 0);
1748 atomic_set(&dev->users, 0);
1750 dev->udev = usb_get_dev(udev);
1751 dev->intf = usb_get_intf(intf);
1752 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1753 dev->quirks = id->driver_info | uvc_quirks_param;
1755 if (udev->product != NULL)
1756 strlcpy(dev->name, udev->product, sizeof dev->name);
1758 snprintf(dev->name, sizeof dev->name,
1759 "UVC Camera (%04x:%04x)",
1760 le16_to_cpu(udev->descriptor.idVendor),
1761 le16_to_cpu(udev->descriptor.idProduct));
1763 /* Parse the Video Class control descriptor. */
1764 if (uvc_parse_control(dev) < 0) {
1765 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1770 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1771 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1772 udev->product ? udev->product : "<unnamed>",
1773 le16_to_cpu(udev->descriptor.idVendor),
1774 le16_to_cpu(udev->descriptor.idProduct));
1776 if (uvc_quirks_param != 0) {
1777 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1778 "parameter for testing purpose.\n", uvc_quirks_param);
1779 uvc_printk(KERN_INFO, "Please report required quirks to the "
1780 "linux-uvc-devel mailing list.\n");
1783 /* Initialize controls. */
1784 if (uvc_ctrl_init_device(dev) < 0)
1787 /* Scan the device for video chains. */
1788 if (uvc_scan_device(dev) < 0)
1791 /* Register video devices. */
1792 if (uvc_register_chains(dev) < 0)
1795 /* Save our data pointer in the interface data. */
1796 usb_set_intfdata(intf, dev);
1798 /* Initialize the interrupt URB. */
1799 if ((ret = uvc_status_init(dev)) < 0) {
1800 uvc_printk(KERN_INFO, "Unable to initialize the status "
1801 "endpoint (%d), status interrupt will not be "
1802 "supported.\n", ret);
1805 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1809 uvc_unregister_video(dev);
1813 static void uvc_disconnect(struct usb_interface *intf)
1815 struct uvc_device *dev = usb_get_intfdata(intf);
1817 /* Set the USB interface data to NULL. This can be done outside the
1818 * lock, as there's no other reader.
1820 usb_set_intfdata(intf, NULL);
1822 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1823 UVC_SC_VIDEOSTREAMING)
1826 dev->state |= UVC_DEV_DISCONNECTED;
1828 uvc_unregister_video(dev);
1831 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1833 struct uvc_device *dev = usb_get_intfdata(intf);
1834 struct uvc_streaming *stream;
1836 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1837 intf->cur_altsetting->desc.bInterfaceNumber);
1839 /* Controls are cached on the fly so they don't need to be saved. */
1840 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1841 UVC_SC_VIDEOCONTROL)
1842 return uvc_status_suspend(dev);
1844 list_for_each_entry(stream, &dev->streams, list) {
1845 if (stream->intf == intf)
1846 return uvc_video_suspend(stream);
1849 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1854 static int __uvc_resume(struct usb_interface *intf, int reset)
1856 struct uvc_device *dev = usb_get_intfdata(intf);
1857 struct uvc_streaming *stream;
1859 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1860 intf->cur_altsetting->desc.bInterfaceNumber);
1862 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1863 UVC_SC_VIDEOCONTROL) {
1865 int ret = uvc_ctrl_resume_device(dev);
1871 return uvc_status_resume(dev);
1874 list_for_each_entry(stream, &dev->streams, list) {
1875 if (stream->intf == intf)
1876 return uvc_video_resume(stream);
1879 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1884 static int uvc_resume(struct usb_interface *intf)
1886 return __uvc_resume(intf, 0);
1889 static int uvc_reset_resume(struct usb_interface *intf)
1891 return __uvc_resume(intf, 1);
1894 /* ------------------------------------------------------------------------
1895 * Driver initialization and cleanup
1899 * The Logitech cameras listed below have their interface class set to
1900 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1901 * though they are compliant.
1903 static struct usb_device_id uvc_ids[] = {
1904 /* Genius eFace 2025 */
1905 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1906 | USB_DEVICE_ID_MATCH_INT_INFO,
1908 .idProduct = 0x706e,
1909 .bInterfaceClass = USB_CLASS_VIDEO,
1910 .bInterfaceSubClass = 1,
1911 .bInterfaceProtocol = 0,
1912 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1913 /* Microsoft Lifecam NX-6000 */
1914 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1915 | USB_DEVICE_ID_MATCH_INT_INFO,
1917 .idProduct = 0x00f8,
1918 .bInterfaceClass = USB_CLASS_VIDEO,
1919 .bInterfaceSubClass = 1,
1920 .bInterfaceProtocol = 0,
1921 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1922 /* Microsoft Lifecam VX-7000 */
1923 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1924 | USB_DEVICE_ID_MATCH_INT_INFO,
1926 .idProduct = 0x0723,
1927 .bInterfaceClass = USB_CLASS_VIDEO,
1928 .bInterfaceSubClass = 1,
1929 .bInterfaceProtocol = 0,
1930 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1931 /* Logitech Quickcam Fusion */
1932 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1933 | USB_DEVICE_ID_MATCH_INT_INFO,
1935 .idProduct = 0x08c1,
1936 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1937 .bInterfaceSubClass = 1,
1938 .bInterfaceProtocol = 0 },
1939 /* Logitech Quickcam Orbit MP */
1940 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1941 | USB_DEVICE_ID_MATCH_INT_INFO,
1943 .idProduct = 0x08c2,
1944 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1945 .bInterfaceSubClass = 1,
1946 .bInterfaceProtocol = 0 },
1947 /* Logitech Quickcam Pro for Notebook */
1948 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1949 | USB_DEVICE_ID_MATCH_INT_INFO,
1951 .idProduct = 0x08c3,
1952 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1953 .bInterfaceSubClass = 1,
1954 .bInterfaceProtocol = 0 },
1955 /* Logitech Quickcam Pro 5000 */
1956 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1957 | USB_DEVICE_ID_MATCH_INT_INFO,
1959 .idProduct = 0x08c5,
1960 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1961 .bInterfaceSubClass = 1,
1962 .bInterfaceProtocol = 0 },
1963 /* Logitech Quickcam OEM Dell Notebook */
1964 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1965 | USB_DEVICE_ID_MATCH_INT_INFO,
1967 .idProduct = 0x08c6,
1968 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1969 .bInterfaceSubClass = 1,
1970 .bInterfaceProtocol = 0 },
1971 /* Logitech Quickcam OEM Cisco VT Camera II */
1972 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1973 | USB_DEVICE_ID_MATCH_INT_INFO,
1975 .idProduct = 0x08c7,
1976 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1977 .bInterfaceSubClass = 1,
1978 .bInterfaceProtocol = 0 },
1979 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1980 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1981 | USB_DEVICE_ID_MATCH_INT_INFO,
1983 .idProduct = 0x3820,
1984 .bInterfaceClass = USB_CLASS_VIDEO,
1985 .bInterfaceSubClass = 1,
1986 .bInterfaceProtocol = 0,
1987 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1988 /* Apple Built-In iSight */
1989 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1990 | USB_DEVICE_ID_MATCH_INT_INFO,
1992 .idProduct = 0x8501,
1993 .bInterfaceClass = USB_CLASS_VIDEO,
1994 .bInterfaceSubClass = 1,
1995 .bInterfaceProtocol = 0,
1996 .driver_info = UVC_QUIRK_PROBE_MINMAX
1997 | UVC_QUIRK_BUILTIN_ISIGHT },
1998 /* Genesys Logic USB 2.0 PC Camera */
1999 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2000 | USB_DEVICE_ID_MATCH_INT_INFO,
2002 .idProduct = 0x0505,
2003 .bInterfaceClass = USB_CLASS_VIDEO,
2004 .bInterfaceSubClass = 1,
2005 .bInterfaceProtocol = 0,
2006 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2008 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2009 | USB_DEVICE_ID_MATCH_INT_INFO,
2011 .idProduct = 0x332d,
2012 .bInterfaceClass = USB_CLASS_VIDEO,
2013 .bInterfaceSubClass = 1,
2014 .bInterfaceProtocol = 0,
2015 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2016 /* ViMicro - Minoru3D */
2017 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2018 | USB_DEVICE_ID_MATCH_INT_INFO,
2020 .idProduct = 0x3410,
2021 .bInterfaceClass = USB_CLASS_VIDEO,
2022 .bInterfaceSubClass = 1,
2023 .bInterfaceProtocol = 0,
2024 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2025 /* ViMicro Venus - Minoru3D */
2026 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2027 | USB_DEVICE_ID_MATCH_INT_INFO,
2029 .idProduct = 0x3420,
2030 .bInterfaceClass = USB_CLASS_VIDEO,
2031 .bInterfaceSubClass = 1,
2032 .bInterfaceProtocol = 0,
2033 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2035 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2036 | USB_DEVICE_ID_MATCH_INT_INFO,
2038 .idProduct = 0x0004,
2039 .bInterfaceClass = USB_CLASS_VIDEO,
2040 .bInterfaceSubClass = 1,
2041 .bInterfaceProtocol = 0,
2042 .driver_info = UVC_QUIRK_PROBE_MINMAX
2043 | UVC_QUIRK_PROBE_DEF },
2044 /* Syntek (HP Spartan) */
2045 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2046 | USB_DEVICE_ID_MATCH_INT_INFO,
2048 .idProduct = 0x5212,
2049 .bInterfaceClass = USB_CLASS_VIDEO,
2050 .bInterfaceSubClass = 1,
2051 .bInterfaceProtocol = 0,
2052 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2053 /* Syntek (Samsung Q310) */
2054 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2055 | USB_DEVICE_ID_MATCH_INT_INFO,
2057 .idProduct = 0x5931,
2058 .bInterfaceClass = USB_CLASS_VIDEO,
2059 .bInterfaceSubClass = 1,
2060 .bInterfaceProtocol = 0,
2061 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2062 /* Syntek (Asus F9SG) */
2063 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2064 | USB_DEVICE_ID_MATCH_INT_INFO,
2066 .idProduct = 0x8a31,
2067 .bInterfaceClass = USB_CLASS_VIDEO,
2068 .bInterfaceSubClass = 1,
2069 .bInterfaceProtocol = 0,
2070 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2071 /* Syntek (Asus U3S) */
2072 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2073 | USB_DEVICE_ID_MATCH_INT_INFO,
2075 .idProduct = 0x8a33,
2076 .bInterfaceClass = USB_CLASS_VIDEO,
2077 .bInterfaceSubClass = 1,
2078 .bInterfaceProtocol = 0,
2079 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2080 /* Syntek (JAOtech Smart Terminal) */
2081 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2082 | USB_DEVICE_ID_MATCH_INT_INFO,
2084 .idProduct = 0x8a34,
2085 .bInterfaceClass = USB_CLASS_VIDEO,
2086 .bInterfaceSubClass = 1,
2087 .bInterfaceProtocol = 0,
2088 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2089 /* Lenovo Thinkpad SL400/SL500 */
2090 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2091 | USB_DEVICE_ID_MATCH_INT_INFO,
2093 .idProduct = 0x480b,
2094 .bInterfaceClass = USB_CLASS_VIDEO,
2095 .bInterfaceSubClass = 1,
2096 .bInterfaceProtocol = 0,
2097 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2098 /* Aveo Technology USB 2.0 Camera */
2099 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2100 | USB_DEVICE_ID_MATCH_INT_INFO,
2102 .idProduct = 0x0306,
2103 .bInterfaceClass = USB_CLASS_VIDEO,
2104 .bInterfaceSubClass = 1,
2105 .bInterfaceProtocol = 0,
2106 .driver_info = UVC_QUIRK_PROBE_MINMAX
2107 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2108 /* Ecamm Pico iMage */
2109 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2110 | USB_DEVICE_ID_MATCH_INT_INFO,
2112 .idProduct = 0xcafe,
2113 .bInterfaceClass = USB_CLASS_VIDEO,
2114 .bInterfaceSubClass = 1,
2115 .bInterfaceProtocol = 0,
2116 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2117 /* FSC WebCam V30S */
2118 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2119 | USB_DEVICE_ID_MATCH_INT_INFO,
2121 .idProduct = 0x3288,
2122 .bInterfaceClass = USB_CLASS_VIDEO,
2123 .bInterfaceSubClass = 1,
2124 .bInterfaceProtocol = 0,
2125 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2126 /* Bodelin ProScopeHR */
2127 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2128 | USB_DEVICE_ID_MATCH_DEV_HI
2129 | USB_DEVICE_ID_MATCH_INT_INFO,
2131 .idProduct = 0x1000,
2132 .bcdDevice_hi = 0x0126,
2133 .bInterfaceClass = USB_CLASS_VIDEO,
2134 .bInterfaceSubClass = 1,
2135 .bInterfaceProtocol = 0,
2136 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2137 /* MSI StarCam 370i */
2138 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2139 | USB_DEVICE_ID_MATCH_INT_INFO,
2141 .idProduct = 0x2951,
2142 .bInterfaceClass = USB_CLASS_VIDEO,
2143 .bInterfaceSubClass = 1,
2144 .bInterfaceProtocol = 0,
2145 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2146 /* SiGma Micro USB Web Camera */
2147 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2148 | USB_DEVICE_ID_MATCH_INT_INFO,
2150 .idProduct = 0x3000,
2151 .bInterfaceClass = USB_CLASS_VIDEO,
2152 .bInterfaceSubClass = 1,
2153 .bInterfaceProtocol = 0,
2154 .driver_info = UVC_QUIRK_PROBE_MINMAX
2155 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2156 /* Generic USB Video Class */
2157 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2161 MODULE_DEVICE_TABLE(usb, uvc_ids);
2163 struct uvc_driver uvc_driver = {
2167 .disconnect = uvc_disconnect,
2168 .suspend = uvc_suspend,
2169 .resume = uvc_resume,
2170 .reset_resume = uvc_reset_resume,
2171 .id_table = uvc_ids,
2172 .supports_autosuspend = 1,
2176 static int __init uvc_init(void)
2180 INIT_LIST_HEAD(&uvc_driver.devices);
2181 INIT_LIST_HEAD(&uvc_driver.controls);
2182 mutex_init(&uvc_driver.ctrl_mutex);
2186 result = usb_register(&uvc_driver.driver);
2188 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2192 static void __exit uvc_cleanup(void)
2194 usb_deregister(&uvc_driver.driver);
2197 module_init(uvc_init);
2198 module_exit(uvc_cleanup);
2200 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2201 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2202 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2203 MODULE_PARM_DESC(quirks, "Forced device quirks");
2204 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2205 MODULE_PARM_DESC(trace, "Trace level bitmask");
2206 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2207 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2209 MODULE_AUTHOR(DRIVER_AUTHOR);
2210 MODULE_DESCRIPTION(DRIVER_DESC);
2211 MODULE_LICENSE("GPL");
2212 MODULE_VERSION(DRIVER_VERSION);