initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / media / video / uvc / uvc_driver.c
blob6689e8c2ae43b1109888d79c08a7e6fdf3f20779
1 /*
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>
38 #include "uvcvideo.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"
44 #endif
46 unsigned int uvc_no_drop_param;
47 static unsigned int uvc_quirks_param;
48 unsigned int uvc_trace_param;
50 /* ------------------------------------------------------------------------
51 * Video formats
54 static struct uvc_format_desc uvc_fmts[] = {
56 .name = "YUV 4:2:2 (YUYV)",
57 .guid = UVC_GUID_FORMAT_YUY2,
58 .fcc = V4L2_PIX_FMT_YUYV,
61 .name = "YUV 4:2:2 (YUYV)",
62 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
63 .fcc = V4L2_PIX_FMT_YUYV,
66 .name = "YUV 4:2:0 (NV12)",
67 .guid = UVC_GUID_FORMAT_NV12,
68 .fcc = V4L2_PIX_FMT_NV12,
71 .name = "MJPEG",
72 .guid = UVC_GUID_FORMAT_MJPEG,
73 .fcc = V4L2_PIX_FMT_MJPEG,
76 .name = "YVU 4:2:0 (YV12)",
77 .guid = UVC_GUID_FORMAT_YV12,
78 .fcc = V4L2_PIX_FMT_YVU420,
81 .name = "YUV 4:2:0 (I420)",
82 .guid = UVC_GUID_FORMAT_I420,
83 .fcc = V4L2_PIX_FMT_YUV420,
86 .name = "YUV 4:2:2 (UYVY)",
87 .guid = UVC_GUID_FORMAT_UYVY,
88 .fcc = V4L2_PIX_FMT_UYVY,
91 .name = "Greyscale (8-bit)",
92 .guid = UVC_GUID_FORMAT_Y800,
93 .fcc = V4L2_PIX_FMT_GREY,
96 .name = "Greyscale (16-bit)",
97 .guid = UVC_GUID_FORMAT_Y16,
98 .fcc = V4L2_PIX_FMT_Y16,
101 .name = "RGB Bayer",
102 .guid = UVC_GUID_FORMAT_BY8,
103 .fcc = V4L2_PIX_FMT_SBGGR8,
107 /* ------------------------------------------------------------------------
108 * Utility functions
111 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
112 __u8 epaddr)
114 struct usb_host_endpoint *ep;
115 unsigned int i;
117 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
118 ep = &alts->endpoint[i];
119 if (ep->desc.bEndpointAddress == epaddr)
120 return ep;
123 return NULL;
126 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
128 unsigned int len = ARRAY_SIZE(uvc_fmts);
129 unsigned int i;
131 for (i = 0; i < len; ++i) {
132 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
133 return &uvc_fmts[i];
136 return NULL;
139 static __u32 uvc_colorspace(const __u8 primaries)
141 static const __u8 colorprimaries[] = {
143 V4L2_COLORSPACE_SRGB,
144 V4L2_COLORSPACE_470_SYSTEM_M,
145 V4L2_COLORSPACE_470_SYSTEM_BG,
146 V4L2_COLORSPACE_SMPTE170M,
147 V4L2_COLORSPACE_SMPTE240M,
150 if (primaries < ARRAY_SIZE(colorprimaries))
151 return colorprimaries[primaries];
153 return 0;
156 /* Simplify a fraction using a simple continued fraction decomposition. The
157 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
158 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
159 * arbitrary parameters to remove non-significative terms from the simple
160 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
161 * respectively seems to give nice results.
163 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
164 unsigned int n_terms, unsigned int threshold)
166 uint32_t *an;
167 uint32_t x, y, r;
168 unsigned int i, n;
170 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
171 if (an == NULL)
172 return;
174 /* Convert the fraction to a simple continued fraction. See
175 * http://mathforum.org/dr.math/faq/faq.fractions.html
176 * Stop if the current term is bigger than or equal to the given
177 * threshold.
179 x = *numerator;
180 y = *denominator;
182 for (n = 0; n < n_terms && y != 0; ++n) {
183 an[n] = x / y;
184 if (an[n] >= threshold) {
185 if (n < 2)
186 n++;
187 break;
190 r = x - an[n] * y;
191 x = y;
192 y = r;
195 /* Expand the simple continued fraction back to an integer fraction. */
196 x = 0;
197 y = 1;
199 for (i = n; i > 0; --i) {
200 r = y;
201 y = an[i-1] * y + x;
202 x = r;
205 *numerator = y;
206 *denominator = x;
207 kfree(an);
210 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
211 * to compute numerator / denominator * 10000000 using 32 bit fixed point
212 * arithmetic only.
214 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
216 uint32_t multiplier;
218 /* Saturate the result if the operation would overflow. */
219 if (denominator == 0 ||
220 numerator/denominator >= ((uint32_t)-1)/10000000)
221 return (uint32_t)-1;
223 /* Divide both the denominator and the multiplier by two until
224 * numerator * multiplier doesn't overflow. If anyone knows a better
225 * algorithm please let me know.
227 multiplier = 10000000;
228 while (numerator > ((uint32_t)-1)/multiplier) {
229 multiplier /= 2;
230 denominator /= 2;
233 return denominator ? numerator * multiplier / denominator : 0;
236 /* ------------------------------------------------------------------------
237 * Terminal and unit management
240 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
242 struct uvc_entity *entity;
244 list_for_each_entry(entity, &dev->entities, list) {
245 if (entity->id == id)
246 return entity;
249 return NULL;
252 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
253 int id, struct uvc_entity *entity)
255 unsigned int i;
257 if (entity == NULL)
258 entity = list_entry(&dev->entities, struct uvc_entity, list);
260 list_for_each_entry_continue(entity, &dev->entities, list) {
261 switch (UVC_ENTITY_TYPE(entity)) {
262 case UVC_TT_STREAMING:
263 if (entity->output.bSourceID == id)
264 return entity;
265 break;
267 case UVC_VC_PROCESSING_UNIT:
268 if (entity->processing.bSourceID == id)
269 return entity;
270 break;
272 case UVC_VC_SELECTOR_UNIT:
273 for (i = 0; i < entity->selector.bNrInPins; ++i)
274 if (entity->selector.baSourceID[i] == id)
275 return entity;
276 break;
278 case UVC_VC_EXTENSION_UNIT:
279 for (i = 0; i < entity->extension.bNrInPins; ++i)
280 if (entity->extension.baSourceID[i] == id)
281 return entity;
282 break;
286 return NULL;
289 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
291 struct uvc_streaming *stream;
293 list_for_each_entry(stream, &dev->streams, list) {
294 if (stream->header.bTerminalLink == id)
295 return stream;
298 return NULL;
301 /* ------------------------------------------------------------------------
302 * Descriptors parsing
305 static int uvc_parse_format(struct uvc_device *dev,
306 struct uvc_streaming *streaming, struct uvc_format *format,
307 __u32 **intervals, unsigned char *buffer, int buflen)
309 struct usb_interface *intf = streaming->intf;
310 struct usb_host_interface *alts = intf->cur_altsetting;
311 struct uvc_format_desc *fmtdesc;
312 struct uvc_frame *frame;
313 const unsigned char *start = buffer;
314 unsigned int interval;
315 unsigned int i, n;
316 __u8 ftype;
318 format->type = buffer[2];
319 format->index = buffer[3];
321 switch (buffer[2]) {
322 case UVC_VS_FORMAT_UNCOMPRESSED:
323 case UVC_VS_FORMAT_FRAME_BASED:
324 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
325 if (buflen < n) {
326 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
327 "interface %d FORMAT error\n",
328 dev->udev->devnum,
329 alts->desc.bInterfaceNumber);
330 return -EINVAL;
333 /* Find the format descriptor from its GUID. */
334 fmtdesc = uvc_format_by_guid(&buffer[5]);
336 if (fmtdesc != NULL) {
337 strlcpy(format->name, fmtdesc->name,
338 sizeof format->name);
339 format->fcc = fmtdesc->fcc;
340 } else {
341 uvc_printk(KERN_INFO, "Unknown video format "
342 UVC_GUID_FORMAT "\n",
343 UVC_GUID_ARGS(&buffer[5]));
344 snprintf(format->name, sizeof format->name,
345 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
346 format->fcc = 0;
349 format->bpp = buffer[21];
350 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
351 ftype = UVC_VS_FRAME_UNCOMPRESSED;
352 } else {
353 ftype = UVC_VS_FRAME_FRAME_BASED;
354 if (buffer[27])
355 format->flags = UVC_FMT_FLAG_COMPRESSED;
357 break;
359 case UVC_VS_FORMAT_MJPEG:
360 if (buflen < 11) {
361 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
362 "interface %d FORMAT error\n",
363 dev->udev->devnum,
364 alts->desc.bInterfaceNumber);
365 return -EINVAL;
368 strlcpy(format->name, "MJPEG", sizeof format->name);
369 format->fcc = V4L2_PIX_FMT_MJPEG;
370 format->flags = UVC_FMT_FLAG_COMPRESSED;
371 format->bpp = 0;
372 ftype = UVC_VS_FRAME_MJPEG;
373 break;
375 case UVC_VS_FORMAT_DV:
376 if (buflen < 9) {
377 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
378 "interface %d FORMAT error\n",
379 dev->udev->devnum,
380 alts->desc.bInterfaceNumber);
381 return -EINVAL;
384 switch (buffer[8] & 0x7f) {
385 case 0:
386 strlcpy(format->name, "SD-DV", sizeof format->name);
387 break;
388 case 1:
389 strlcpy(format->name, "SDL-DV", sizeof format->name);
390 break;
391 case 2:
392 strlcpy(format->name, "HD-DV", sizeof format->name);
393 break;
394 default:
395 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
396 "interface %d: unknown DV format %u\n",
397 dev->udev->devnum,
398 alts->desc.bInterfaceNumber, buffer[8]);
399 return -EINVAL;
402 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
403 sizeof format->name);
405 format->fcc = V4L2_PIX_FMT_DV;
406 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
407 format->bpp = 0;
408 ftype = 0;
410 /* Create a dummy frame descriptor. */
411 frame = &format->frame[0];
412 memset(&format->frame[0], 0, sizeof format->frame[0]);
413 frame->bFrameIntervalType = 1;
414 frame->dwDefaultFrameInterval = 1;
415 frame->dwFrameInterval = *intervals;
416 *(*intervals)++ = 1;
417 format->nframes = 1;
418 break;
420 case UVC_VS_FORMAT_MPEG2TS:
421 case UVC_VS_FORMAT_STREAM_BASED:
422 /* Not supported yet. */
423 default:
424 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
425 "interface %d unsupported format %u\n",
426 dev->udev->devnum, alts->desc.bInterfaceNumber,
427 buffer[2]);
428 return -EINVAL;
431 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
433 buflen -= buffer[0];
434 buffer += buffer[0];
436 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
437 * based formats have frame descriptors.
439 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
440 buffer[2] == ftype) {
441 frame = &format->frame[format->nframes];
442 if (ftype != UVC_VS_FRAME_FRAME_BASED)
443 n = buflen > 25 ? buffer[25] : 0;
444 else
445 n = buflen > 21 ? buffer[21] : 0;
447 n = n ? n : 3;
449 if (buflen < 26 + 4*n) {
450 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
451 "interface %d FRAME error\n", dev->udev->devnum,
452 alts->desc.bInterfaceNumber);
453 return -EINVAL;
456 frame->bFrameIndex = buffer[3];
457 frame->bmCapabilities = buffer[4];
458 frame->wWidth = get_unaligned_le16(&buffer[5]);
459 frame->wHeight = get_unaligned_le16(&buffer[7]);
460 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
461 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
462 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
463 frame->dwMaxVideoFrameBufferSize =
464 get_unaligned_le32(&buffer[17]);
465 frame->dwDefaultFrameInterval =
466 get_unaligned_le32(&buffer[21]);
467 frame->bFrameIntervalType = buffer[25];
468 } else {
469 frame->dwMaxVideoFrameBufferSize = 0;
470 frame->dwDefaultFrameInterval =
471 get_unaligned_le32(&buffer[17]);
472 frame->bFrameIntervalType = buffer[21];
474 frame->dwFrameInterval = *intervals;
476 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
477 * completely. Observed behaviours range from setting the
478 * value to 1.1x the actual frame size to hardwiring the
479 * 16 low bits to 0. This results in a higher than necessary
480 * memory usage as well as a wrong image size information. For
481 * uncompressed formats this can be fixed by computing the
482 * value from the frame size.
484 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
485 frame->dwMaxVideoFrameBufferSize = format->bpp
486 * frame->wWidth * frame->wHeight / 8;
488 /* Some bogus devices report dwMinFrameInterval equal to
489 * dwMaxFrameInterval and have dwFrameIntervalStep set to
490 * zero. Setting all null intervals to 1 fixes the problem and
491 * some other divisions by zero that could happen.
493 for (i = 0; i < n; ++i) {
494 interval = get_unaligned_le32(&buffer[26+4*i]);
495 *(*intervals)++ = interval ? interval : 1;
498 /* Make sure that the default frame interval stays between
499 * the boundaries.
501 n -= frame->bFrameIntervalType ? 1 : 2;
502 frame->dwDefaultFrameInterval =
503 min(frame->dwFrameInterval[n],
504 max(frame->dwFrameInterval[0],
505 frame->dwDefaultFrameInterval));
507 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
508 frame->wWidth, frame->wHeight,
509 10000000/frame->dwDefaultFrameInterval,
510 (100000000/frame->dwDefaultFrameInterval)%10);
512 format->nframes++;
513 buflen -= buffer[0];
514 buffer += buffer[0];
517 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
518 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
519 buflen -= buffer[0];
520 buffer += buffer[0];
523 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
524 buffer[2] == UVC_VS_COLORFORMAT) {
525 if (buflen < 6) {
526 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
527 "interface %d COLORFORMAT error\n",
528 dev->udev->devnum,
529 alts->desc.bInterfaceNumber);
530 return -EINVAL;
533 format->colorspace = uvc_colorspace(buffer[3]);
535 buflen -= buffer[0];
536 buffer += buffer[0];
539 return buffer - start;
542 static int uvc_parse_streaming(struct uvc_device *dev,
543 struct usb_interface *intf)
545 struct uvc_streaming *streaming = NULL;
546 struct uvc_format *format;
547 struct uvc_frame *frame;
548 struct usb_host_interface *alts = &intf->altsetting[0];
549 unsigned char *_buffer, *buffer = alts->extra;
550 int _buflen, buflen = alts->extralen;
551 unsigned int nformats = 0, nframes = 0, nintervals = 0;
552 unsigned int size, i, n, p;
553 __u32 *interval;
554 __u16 psize;
555 int ret = -EINVAL;
557 if (intf->cur_altsetting->desc.bInterfaceSubClass
558 != UVC_SC_VIDEOSTREAMING) {
559 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
560 "video streaming interface\n", dev->udev->devnum,
561 intf->altsetting[0].desc.bInterfaceNumber);
562 return -EINVAL;
565 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
566 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
567 "claimed\n", dev->udev->devnum,
568 intf->altsetting[0].desc.bInterfaceNumber);
569 return -EINVAL;
572 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
573 if (streaming == NULL) {
574 usb_driver_release_interface(&uvc_driver.driver, intf);
575 return -EINVAL;
578 mutex_init(&streaming->mutex);
579 streaming->dev = dev;
580 streaming->intf = usb_get_intf(intf);
581 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
583 /* The Pico iMage webcam has its class-specific interface descriptors
584 * after the endpoint descriptors.
586 if (buflen == 0) {
587 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
588 struct usb_host_endpoint *ep = &alts->endpoint[i];
590 if (ep->extralen == 0)
591 continue;
593 if (ep->extralen > 2 &&
594 ep->extra[1] == USB_DT_CS_INTERFACE) {
595 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
596 "from endpoint %u.\n", i);
597 buffer = alts->endpoint[i].extra;
598 buflen = alts->endpoint[i].extralen;
599 break;
604 /* Skip the standard interface descriptors. */
605 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
606 buflen -= buffer[0];
607 buffer += buffer[0];
610 if (buflen <= 2) {
611 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
612 "interface descriptors found.\n");
613 goto error;
616 /* Parse the header descriptor. */
617 switch (buffer[2]) {
618 case UVC_VS_OUTPUT_HEADER:
619 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
620 size = 9;
621 break;
623 case UVC_VS_INPUT_HEADER:
624 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
625 size = 13;
626 break;
628 default:
629 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
630 "%d HEADER descriptor not found.\n", dev->udev->devnum,
631 alts->desc.bInterfaceNumber);
632 goto error;
635 p = buflen >= 4 ? buffer[3] : 0;
636 n = buflen >= size ? buffer[size-1] : 0;
638 if (buflen < size + p*n) {
639 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
640 "interface %d HEADER descriptor is invalid.\n",
641 dev->udev->devnum, alts->desc.bInterfaceNumber);
642 goto error;
645 streaming->header.bNumFormats = p;
646 streaming->header.bEndpointAddress = buffer[6];
647 if (buffer[2] == UVC_VS_INPUT_HEADER) {
648 streaming->header.bmInfo = buffer[7];
649 streaming->header.bTerminalLink = buffer[8];
650 streaming->header.bStillCaptureMethod = buffer[9];
651 streaming->header.bTriggerSupport = buffer[10];
652 streaming->header.bTriggerUsage = buffer[11];
653 } else {
654 streaming->header.bTerminalLink = buffer[7];
656 streaming->header.bControlSize = n;
658 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
659 if (streaming->header.bmaControls == NULL) {
660 ret = -ENOMEM;
661 goto error;
664 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
666 buflen -= buffer[0];
667 buffer += buffer[0];
669 _buffer = buffer;
670 _buflen = buflen;
672 /* Count the format and frame descriptors. */
673 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
674 switch (_buffer[2]) {
675 case UVC_VS_FORMAT_UNCOMPRESSED:
676 case UVC_VS_FORMAT_MJPEG:
677 case UVC_VS_FORMAT_FRAME_BASED:
678 nformats++;
679 break;
681 case UVC_VS_FORMAT_DV:
682 /* DV format has no frame descriptor. We will create a
683 * dummy frame descriptor with a dummy frame interval.
685 nformats++;
686 nframes++;
687 nintervals++;
688 break;
690 case UVC_VS_FORMAT_MPEG2TS:
691 case UVC_VS_FORMAT_STREAM_BASED:
692 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
693 "interface %d FORMAT %u is not supported.\n",
694 dev->udev->devnum,
695 alts->desc.bInterfaceNumber, _buffer[2]);
696 break;
698 case UVC_VS_FRAME_UNCOMPRESSED:
699 case UVC_VS_FRAME_MJPEG:
700 nframes++;
701 if (_buflen > 25)
702 nintervals += _buffer[25] ? _buffer[25] : 3;
703 break;
705 case UVC_VS_FRAME_FRAME_BASED:
706 nframes++;
707 if (_buflen > 21)
708 nintervals += _buffer[21] ? _buffer[21] : 3;
709 break;
712 _buflen -= _buffer[0];
713 _buffer += _buffer[0];
716 if (nformats == 0) {
717 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
718 "%d has no supported formats defined.\n",
719 dev->udev->devnum, alts->desc.bInterfaceNumber);
720 goto error;
723 size = nformats * sizeof *format + nframes * sizeof *frame
724 + nintervals * sizeof *interval;
725 format = kzalloc(size, GFP_KERNEL);
726 if (format == NULL) {
727 ret = -ENOMEM;
728 goto error;
731 frame = (struct uvc_frame *)&format[nformats];
732 interval = (__u32 *)&frame[nframes];
734 streaming->format = format;
735 streaming->nformats = nformats;
737 /* Parse the format descriptors. */
738 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
739 switch (buffer[2]) {
740 case UVC_VS_FORMAT_UNCOMPRESSED:
741 case UVC_VS_FORMAT_MJPEG:
742 case UVC_VS_FORMAT_DV:
743 case UVC_VS_FORMAT_FRAME_BASED:
744 format->frame = frame;
745 ret = uvc_parse_format(dev, streaming, format,
746 &interval, buffer, buflen);
747 if (ret < 0)
748 goto error;
750 frame += format->nframes;
751 format++;
753 buflen -= ret;
754 buffer += ret;
755 continue;
757 default:
758 break;
761 buflen -= buffer[0];
762 buffer += buffer[0];
765 if (buflen)
766 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
767 "%d has %u bytes of trailing descriptor garbage.\n",
768 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
770 /* Parse the alternate settings to find the maximum bandwidth. */
771 for (i = 0; i < intf->num_altsetting; ++i) {
772 struct usb_host_endpoint *ep;
773 alts = &intf->altsetting[i];
774 ep = uvc_find_endpoint(alts,
775 streaming->header.bEndpointAddress);
776 if (ep == NULL)
777 continue;
779 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
780 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
781 if (psize > streaming->maxpsize)
782 streaming->maxpsize = psize;
785 list_add_tail(&streaming->list, &dev->streams);
786 return 0;
788 error:
789 usb_driver_release_interface(&uvc_driver.driver, intf);
790 usb_put_intf(intf);
791 kfree(streaming->format);
792 kfree(streaming->header.bmaControls);
793 kfree(streaming);
794 return ret;
797 /* Parse vendor-specific extensions. */
798 static int uvc_parse_vendor_control(struct uvc_device *dev,
799 const unsigned char *buffer, int buflen)
801 struct usb_device *udev = dev->udev;
802 struct usb_host_interface *alts = dev->intf->cur_altsetting;
803 struct uvc_entity *unit;
804 unsigned int n, p;
805 int handled = 0;
807 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
808 case 0x046d: /* Logitech */
809 if (buffer[1] != 0x41 || buffer[2] != 0x01)
810 break;
812 /* Logitech implements several vendor specific functions
813 * through vendor specific extension units (LXU).
815 * The LXU descriptors are similar to XU descriptors
816 * (see "USB Device Video Class for Video Devices", section
817 * 3.7.2.6 "Extension Unit Descriptor") with the following
818 * differences:
820 * ----------------------------------------------------------
821 * 0 bLength 1 Number
822 * Size of this descriptor, in bytes: 24+p+n*2
823 * ----------------------------------------------------------
824 * 23+p+n bmControlsType N Bitmap
825 * Individual bits in the set are defined:
826 * 0: Absolute
827 * 1: Relative
829 * This bitset is mapped exactly the same as bmControls.
830 * ----------------------------------------------------------
831 * 23+p+n*2 bReserved 1 Boolean
832 * ----------------------------------------------------------
833 * 24+p+n*2 iExtension 1 Index
834 * Index of a string descriptor that describes this
835 * extension unit.
836 * ----------------------------------------------------------
838 p = buflen >= 22 ? buffer[21] : 0;
839 n = buflen >= 25 + p ? buffer[22+p] : 0;
841 if (buflen < 25 + p + 2*n) {
842 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
843 "interface %d EXTENSION_UNIT error\n",
844 udev->devnum, alts->desc.bInterfaceNumber);
845 break;
848 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
849 if (unit == NULL)
850 return -ENOMEM;
852 unit->id = buffer[3];
853 unit->type = UVC_VC_EXTENSION_UNIT;
854 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
855 unit->extension.bNumControls = buffer[20];
856 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
857 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
858 memcpy(unit->extension.baSourceID, &buffer[22], p);
859 unit->extension.bControlSize = buffer[22+p];
860 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
861 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
862 + p + n;
863 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
865 if (buffer[24+p+2*n] != 0)
866 usb_string(udev, buffer[24+p+2*n], unit->name,
867 sizeof unit->name);
868 else
869 sprintf(unit->name, "Extension %u", buffer[3]);
871 list_add_tail(&unit->list, &dev->entities);
872 handled = 1;
873 break;
876 return handled;
879 static int uvc_parse_standard_control(struct uvc_device *dev,
880 const unsigned char *buffer, int buflen)
882 struct usb_device *udev = dev->udev;
883 struct uvc_entity *unit, *term;
884 struct usb_interface *intf;
885 struct usb_host_interface *alts = dev->intf->cur_altsetting;
886 unsigned int i, n, p, len;
887 __u16 type;
889 switch (buffer[2]) {
890 case UVC_VC_HEADER:
891 n = buflen >= 12 ? buffer[11] : 0;
893 if (buflen < 12 || buflen < 12 + n) {
894 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
895 "interface %d HEADER error\n", udev->devnum,
896 alts->desc.bInterfaceNumber);
897 return -EINVAL;
900 dev->uvc_version = get_unaligned_le16(&buffer[3]);
901 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
903 /* Parse all USB Video Streaming interfaces. */
904 for (i = 0; i < n; ++i) {
905 intf = usb_ifnum_to_if(udev, buffer[12+i]);
906 if (intf == NULL) {
907 uvc_trace(UVC_TRACE_DESCR, "device %d "
908 "interface %d doesn't exists\n",
909 udev->devnum, i);
910 continue;
913 uvc_parse_streaming(dev, intf);
915 break;
917 case UVC_VC_INPUT_TERMINAL:
918 if (buflen < 8) {
919 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
920 "interface %d INPUT_TERMINAL error\n",
921 udev->devnum, alts->desc.bInterfaceNumber);
922 return -EINVAL;
925 /* Make sure the terminal type MSB is not null, otherwise it
926 * could be confused with a unit.
928 type = get_unaligned_le16(&buffer[4]);
929 if ((type & 0xff00) == 0) {
930 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
931 "interface %d INPUT_TERMINAL %d has invalid "
932 "type 0x%04x, skipping\n", udev->devnum,
933 alts->desc.bInterfaceNumber,
934 buffer[3], type);
935 return 0;
938 n = 0;
939 p = 0;
940 len = 8;
942 if (type == UVC_ITT_CAMERA) {
943 n = buflen >= 15 ? buffer[14] : 0;
944 len = 15;
946 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
947 n = buflen >= 9 ? buffer[8] : 0;
948 p = buflen >= 10 + n ? buffer[9+n] : 0;
949 len = 10;
952 if (buflen < len + n + p) {
953 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
954 "interface %d INPUT_TERMINAL error\n",
955 udev->devnum, alts->desc.bInterfaceNumber);
956 return -EINVAL;
959 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
960 if (term == NULL)
961 return -ENOMEM;
963 term->id = buffer[3];
964 term->type = type | UVC_TERM_INPUT;
966 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
967 term->camera.bControlSize = n;
968 term->camera.bmControls = (__u8 *)term + sizeof *term;
969 term->camera.wObjectiveFocalLengthMin =
970 get_unaligned_le16(&buffer[8]);
971 term->camera.wObjectiveFocalLengthMax =
972 get_unaligned_le16(&buffer[10]);
973 term->camera.wOcularFocalLength =
974 get_unaligned_le16(&buffer[12]);
975 memcpy(term->camera.bmControls, &buffer[15], n);
976 } else if (UVC_ENTITY_TYPE(term) ==
977 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
978 term->media.bControlSize = n;
979 term->media.bmControls = (__u8 *)term + sizeof *term;
980 term->media.bTransportModeSize = p;
981 term->media.bmTransportModes = (__u8 *)term
982 + sizeof *term + n;
983 memcpy(term->media.bmControls, &buffer[9], n);
984 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
987 if (buffer[7] != 0)
988 usb_string(udev, buffer[7], term->name,
989 sizeof term->name);
990 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
991 sprintf(term->name, "Camera %u", buffer[3]);
992 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
993 sprintf(term->name, "Media %u", buffer[3]);
994 else
995 sprintf(term->name, "Input %u", buffer[3]);
997 list_add_tail(&term->list, &dev->entities);
998 break;
1000 case UVC_VC_OUTPUT_TERMINAL:
1001 if (buflen < 9) {
1002 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1003 "interface %d OUTPUT_TERMINAL error\n",
1004 udev->devnum, alts->desc.bInterfaceNumber);
1005 return -EINVAL;
1008 /* Make sure the terminal type MSB is not null, otherwise it
1009 * could be confused with a unit.
1011 type = get_unaligned_le16(&buffer[4]);
1012 if ((type & 0xff00) == 0) {
1013 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1014 "interface %d OUTPUT_TERMINAL %d has invalid "
1015 "type 0x%04x, skipping\n", udev->devnum,
1016 alts->desc.bInterfaceNumber, buffer[3], type);
1017 return 0;
1020 term = kzalloc(sizeof *term, GFP_KERNEL);
1021 if (term == NULL)
1022 return -ENOMEM;
1024 term->id = buffer[3];
1025 term->type = type | UVC_TERM_OUTPUT;
1026 term->output.bSourceID = buffer[7];
1028 if (buffer[8] != 0)
1029 usb_string(udev, buffer[8], term->name,
1030 sizeof term->name);
1031 else
1032 sprintf(term->name, "Output %u", buffer[3]);
1034 list_add_tail(&term->list, &dev->entities);
1035 break;
1037 case UVC_VC_SELECTOR_UNIT:
1038 p = buflen >= 5 ? buffer[4] : 0;
1040 if (buflen < 5 || buflen < 6 + p) {
1041 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1042 "interface %d SELECTOR_UNIT error\n",
1043 udev->devnum, alts->desc.bInterfaceNumber);
1044 return -EINVAL;
1047 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1048 if (unit == NULL)
1049 return -ENOMEM;
1051 unit->id = buffer[3];
1052 unit->type = buffer[2];
1053 unit->selector.bNrInPins = buffer[4];
1054 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1055 memcpy(unit->selector.baSourceID, &buffer[5], p);
1057 if (buffer[5+p] != 0)
1058 usb_string(udev, buffer[5+p], unit->name,
1059 sizeof unit->name);
1060 else
1061 sprintf(unit->name, "Selector %u", buffer[3]);
1063 list_add_tail(&unit->list, &dev->entities);
1064 break;
1066 case UVC_VC_PROCESSING_UNIT:
1067 n = buflen >= 8 ? buffer[7] : 0;
1068 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1070 if (buflen < p + n) {
1071 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1072 "interface %d PROCESSING_UNIT error\n",
1073 udev->devnum, alts->desc.bInterfaceNumber);
1074 return -EINVAL;
1077 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1078 if (unit == NULL)
1079 return -ENOMEM;
1081 unit->id = buffer[3];
1082 unit->type = buffer[2];
1083 unit->processing.bSourceID = buffer[4];
1084 unit->processing.wMaxMultiplier =
1085 get_unaligned_le16(&buffer[5]);
1086 unit->processing.bControlSize = buffer[7];
1087 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1088 memcpy(unit->processing.bmControls, &buffer[8], n);
1089 if (dev->uvc_version >= 0x0110)
1090 unit->processing.bmVideoStandards = buffer[9+n];
1092 if (buffer[8+n] != 0)
1093 usb_string(udev, buffer[8+n], unit->name,
1094 sizeof unit->name);
1095 else
1096 sprintf(unit->name, "Processing %u", buffer[3]);
1098 list_add_tail(&unit->list, &dev->entities);
1099 break;
1101 case UVC_VC_EXTENSION_UNIT:
1102 p = buflen >= 22 ? buffer[21] : 0;
1103 n = buflen >= 24 + p ? buffer[22+p] : 0;
1105 if (buflen < 24 + p + n) {
1106 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1107 "interface %d EXTENSION_UNIT error\n",
1108 udev->devnum, alts->desc.bInterfaceNumber);
1109 return -EINVAL;
1112 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1113 if (unit == NULL)
1114 return -ENOMEM;
1116 unit->id = buffer[3];
1117 unit->type = buffer[2];
1118 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1119 unit->extension.bNumControls = buffer[20];
1120 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
1121 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1122 memcpy(unit->extension.baSourceID, &buffer[22], p);
1123 unit->extension.bControlSize = buffer[22+p];
1124 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1125 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1127 if (buffer[23+p+n] != 0)
1128 usb_string(udev, buffer[23+p+n], unit->name,
1129 sizeof unit->name);
1130 else
1131 sprintf(unit->name, "Extension %u", buffer[3]);
1133 list_add_tail(&unit->list, &dev->entities);
1134 break;
1136 default:
1137 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1138 "descriptor (%u)\n", buffer[2]);
1139 break;
1142 return 0;
1145 static int uvc_parse_control(struct uvc_device *dev)
1147 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1148 unsigned char *buffer = alts->extra;
1149 int buflen = alts->extralen;
1150 int ret;
1152 /* Parse the default alternate setting only, as the UVC specification
1153 * defines a single alternate setting, the default alternate setting
1154 * zero.
1157 while (buflen > 2) {
1158 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1159 buffer[1] != USB_DT_CS_INTERFACE)
1160 goto next_descriptor;
1162 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1163 return ret;
1165 next_descriptor:
1166 buflen -= buffer[0];
1167 buffer += buffer[0];
1170 /* Check if the optional status endpoint is present. Built-in iSight
1171 * webcams have an interrupt endpoint but spit proprietary data that
1172 * don't conform to the UVC status endpoint messages. Don't try to
1173 * handle the interrupt endpoint for those cameras.
1175 if (alts->desc.bNumEndpoints == 1 &&
1176 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1177 struct usb_host_endpoint *ep = &alts->endpoint[0];
1178 struct usb_endpoint_descriptor *desc = &ep->desc;
1180 if (usb_endpoint_is_int_in(desc) &&
1181 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1182 desc->bInterval != 0) {
1183 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1184 "(addr %02x).\n", desc->bEndpointAddress);
1185 dev->int_ep = ep;
1189 return 0;
1192 /* ------------------------------------------------------------------------
1193 * UVC device scan
1197 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1198 * and containing the following units:
1200 * - one or more Output Terminals (USB Streaming or Display)
1201 * - zero or one Processing Unit
1202 * - zero, one or more single-input Selector Units
1203 * - zero or one multiple-input Selector Units, provided all inputs are
1204 * connected to input terminals
1205 * - zero, one or mode single-input Extension Units
1206 * - one or more Input Terminals (Camera, External or USB Streaming)
1208 * The terminal and units must match on of the following structures:
1210 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1211 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1212 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1214 * +---------+ +---------+ -> OTT_*(0)
1215 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1216 * +---------+ +---------+ -> OTT_*(n)
1218 * The Processing Unit and Extension Units can be in any order. Additional
1219 * Extension Units connected to the main chain as single-unit branches are
1220 * also supported. Single-input Selector Units are ignored.
1222 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1223 struct uvc_entity *entity)
1225 switch (UVC_ENTITY_TYPE(entity)) {
1226 case UVC_VC_EXTENSION_UNIT:
1227 if (uvc_trace_param & UVC_TRACE_PROBE)
1228 printk(" <- XU %d", entity->id);
1230 if (entity->extension.bNrInPins != 1) {
1231 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1232 "than 1 input pin.\n", entity->id);
1233 return -1;
1236 list_add_tail(&entity->chain, &chain->extensions);
1237 break;
1239 case UVC_VC_PROCESSING_UNIT:
1240 if (uvc_trace_param & UVC_TRACE_PROBE)
1241 printk(" <- PU %d", entity->id);
1243 if (chain->processing != NULL) {
1244 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1245 "Processing Units in chain.\n");
1246 return -1;
1249 chain->processing = entity;
1250 break;
1252 case UVC_VC_SELECTOR_UNIT:
1253 if (uvc_trace_param & UVC_TRACE_PROBE)
1254 printk(" <- SU %d", entity->id);
1256 /* Single-input selector units are ignored. */
1257 if (entity->selector.bNrInPins == 1)
1258 break;
1260 if (chain->selector != NULL) {
1261 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1262 "Units in chain.\n");
1263 return -1;
1266 chain->selector = entity;
1267 break;
1269 case UVC_ITT_VENDOR_SPECIFIC:
1270 case UVC_ITT_CAMERA:
1271 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1272 if (uvc_trace_param & UVC_TRACE_PROBE)
1273 printk(" <- IT %d\n", entity->id);
1275 list_add_tail(&entity->chain, &chain->iterms);
1276 break;
1278 case UVC_TT_STREAMING:
1279 if (uvc_trace_param & UVC_TRACE_PROBE)
1280 printk(" <- IT %d\n", entity->id);
1282 if (!UVC_ENTITY_IS_ITERM(entity)) {
1283 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1284 "terminal %u.\n", entity->id);
1285 return -1;
1288 list_add_tail(&entity->chain, &chain->iterms);
1289 break;
1291 default:
1292 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1293 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1294 return -1;
1297 return 0;
1300 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1301 struct uvc_entity *entity, struct uvc_entity *prev)
1303 struct uvc_entity *forward;
1304 int found;
1306 /* Forward scan */
1307 forward = NULL;
1308 found = 0;
1310 while (1) {
1311 forward = uvc_entity_by_reference(chain->dev, entity->id,
1312 forward);
1313 if (forward == NULL)
1314 break;
1315 if (forward == prev)
1316 continue;
1318 switch (UVC_ENTITY_TYPE(forward)) {
1319 case UVC_VC_EXTENSION_UNIT:
1320 if (forward->extension.bNrInPins != 1) {
1321 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1322 "has more than 1 input pin.\n",
1323 entity->id);
1324 return -EINVAL;
1327 list_add_tail(&forward->chain, &chain->extensions);
1328 if (uvc_trace_param & UVC_TRACE_PROBE) {
1329 if (!found)
1330 printk(" (->");
1332 printk(" XU %d", forward->id);
1333 found = 1;
1335 break;
1337 case UVC_OTT_VENDOR_SPECIFIC:
1338 case UVC_OTT_DISPLAY:
1339 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1340 case UVC_TT_STREAMING:
1341 if (UVC_ENTITY_IS_ITERM(forward)) {
1342 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1343 "terminal %u.\n", forward->id);
1344 return -EINVAL;
1347 list_add_tail(&forward->chain, &chain->oterms);
1348 if (uvc_trace_param & UVC_TRACE_PROBE) {
1349 if (!found)
1350 printk(" (->");
1352 printk(" OT %d", forward->id);
1353 found = 1;
1355 break;
1358 if (found)
1359 printk(")");
1361 return 0;
1364 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1365 struct uvc_entity *entity)
1367 struct uvc_entity *term;
1368 int id = -1, i;
1370 switch (UVC_ENTITY_TYPE(entity)) {
1371 case UVC_VC_EXTENSION_UNIT:
1372 id = entity->extension.baSourceID[0];
1373 break;
1375 case UVC_VC_PROCESSING_UNIT:
1376 id = entity->processing.bSourceID;
1377 break;
1379 case UVC_VC_SELECTOR_UNIT:
1380 /* Single-input selector units are ignored. */
1381 if (entity->selector.bNrInPins == 1) {
1382 id = entity->selector.baSourceID[0];
1383 break;
1386 if (uvc_trace_param & UVC_TRACE_PROBE)
1387 printk(" <- IT");
1389 chain->selector = entity;
1390 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1391 id = entity->selector.baSourceID[i];
1392 term = uvc_entity_by_id(chain->dev, id);
1393 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1394 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1395 "input %d isn't connected to an "
1396 "input terminal\n", entity->id, i);
1397 return -1;
1400 if (uvc_trace_param & UVC_TRACE_PROBE)
1401 printk(" %d", term->id);
1403 list_add_tail(&term->chain, &chain->iterms);
1404 uvc_scan_chain_forward(chain, term, entity);
1407 if (uvc_trace_param & UVC_TRACE_PROBE)
1408 printk("\n");
1410 id = 0;
1411 break;
1414 return id;
1417 static int uvc_scan_chain(struct uvc_video_chain *chain,
1418 struct uvc_entity *oterm)
1420 struct uvc_entity *entity, *prev;
1421 int id;
1423 entity = oterm;
1424 list_add_tail(&entity->chain, &chain->oterms);
1425 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1427 id = entity->output.bSourceID;
1428 while (id != 0) {
1429 prev = entity;
1430 entity = uvc_entity_by_id(chain->dev, id);
1431 if (entity == NULL) {
1432 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1433 "unknown entity %d.\n", id);
1434 return -EINVAL;
1437 if (entity->chain.next || entity->chain.prev) {
1438 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1439 "entity %d already in chain.\n", id);
1440 return -EINVAL;
1443 /* Process entity */
1444 if (uvc_scan_chain_entity(chain, entity) < 0)
1445 return -EINVAL;
1447 /* Forward scan */
1448 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1449 return -EINVAL;
1451 /* Stop when a terminal is found. */
1452 if (UVC_ENTITY_IS_TERM(entity))
1453 break;
1455 /* Backward scan */
1456 id = uvc_scan_chain_backward(chain, entity);
1457 if (id < 0)
1458 return id;
1461 return 0;
1464 static unsigned int uvc_print_terms(struct list_head *terms, char *buffer)
1466 struct uvc_entity *term;
1467 unsigned int nterms = 0;
1468 char *p = buffer;
1470 list_for_each_entry(term, terms, chain) {
1471 p += sprintf(p, "%u", term->id);
1472 if (term->chain.next != terms) {
1473 p += sprintf(p, ",");
1474 if (++nterms >= 4) {
1475 p += sprintf(p, "...");
1476 break;
1481 return p - buffer;
1484 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1486 static char buffer[43];
1487 char *p = buffer;
1489 p += uvc_print_terms(&chain->iterms, p);
1490 p += sprintf(p, " -> ");
1491 uvc_print_terms(&chain->oterms, p);
1493 return buffer;
1497 * Scan the device for video chains and register video devices.
1499 * Chains are scanned starting at their output terminals and walked backwards.
1501 static int uvc_scan_device(struct uvc_device *dev)
1503 struct uvc_video_chain *chain;
1504 struct uvc_entity *term;
1506 list_for_each_entry(term, &dev->entities, list) {
1507 if (!UVC_ENTITY_IS_OTERM(term))
1508 continue;
1510 /* If the terminal is already included in a chain, skip it.
1511 * This can happen for chains that have multiple output
1512 * terminals, where all output terminals beside the first one
1513 * will be inserted in the chain in forward scans.
1515 if (term->chain.next || term->chain.prev)
1516 continue;
1518 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1519 if (chain == NULL)
1520 return -ENOMEM;
1522 INIT_LIST_HEAD(&chain->iterms);
1523 INIT_LIST_HEAD(&chain->oterms);
1524 INIT_LIST_HEAD(&chain->extensions);
1525 mutex_init(&chain->ctrl_mutex);
1526 chain->dev = dev;
1528 if (uvc_scan_chain(chain, term) < 0) {
1529 kfree(chain);
1530 continue;
1533 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1534 uvc_print_chain(chain));
1536 list_add_tail(&chain->list, &dev->chains);
1539 if (list_empty(&dev->chains)) {
1540 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1541 return -1;
1544 return 0;
1547 /* ------------------------------------------------------------------------
1548 * Video device registration and unregistration
1552 * Unregister the video devices.
1554 static void uvc_unregister_video(struct uvc_device *dev)
1556 struct uvc_streaming *stream;
1558 list_for_each_entry(stream, &dev->streams, list) {
1559 if (stream->vdev == NULL)
1560 continue;
1562 if (stream->vdev->minor == -1)
1563 video_device_release(stream->vdev);
1564 else
1565 video_unregister_device(stream->vdev);
1566 stream->vdev = NULL;
1570 static int uvc_register_video(struct uvc_device *dev,
1571 struct uvc_streaming *stream)
1573 struct video_device *vdev;
1574 int ret;
1576 /* Initialize the streaming interface with default streaming
1577 * parameters.
1579 ret = uvc_video_init(stream);
1580 if (ret < 0) {
1581 uvc_printk(KERN_ERR, "Failed to initialize the device "
1582 "(%d).\n", ret);
1583 return ret;
1586 /* Register the device with V4L. */
1587 vdev = video_device_alloc();
1588 if (vdev == NULL) {
1589 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1590 ret);
1591 return -ENOMEM;
1594 /* We already hold a reference to dev->udev. The video device will be
1595 * unregistered before the reference is released, so we don't need to
1596 * get another one.
1598 vdev->parent = &dev->intf->dev;
1599 vdev->minor = -1;
1600 vdev->fops = &uvc_fops;
1601 vdev->release = video_device_release;
1602 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1604 /* Set the driver data before calling video_register_device, otherwise
1605 * uvc_v4l2_open might race us.
1607 stream->vdev = vdev;
1608 video_set_drvdata(vdev, stream);
1610 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1611 if (ret < 0) {
1612 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1613 ret);
1614 stream->vdev = NULL;
1615 video_device_release(vdev);
1616 return ret;
1619 return 0;
1623 * Register all video devices in all chains.
1625 static int uvc_register_terms(struct uvc_device *dev,
1626 struct uvc_video_chain *chain, struct list_head *terms)
1628 struct uvc_streaming *stream;
1629 struct uvc_entity *term;
1630 int ret;
1632 list_for_each_entry(term, terms, chain) {
1633 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1634 continue;
1636 stream = uvc_stream_by_id(dev, term->id);
1637 if (stream == NULL) {
1638 uvc_printk(KERN_INFO, "No streaming interface found "
1639 "for terminal %u.", term->id);
1640 continue;
1643 stream->chain = chain;
1644 ret = uvc_register_video(dev, stream);
1645 if (ret < 0)
1646 return ret;
1649 return 0;
1652 static int uvc_register_chains(struct uvc_device *dev)
1654 struct uvc_video_chain *chain;
1655 int ret;
1657 list_for_each_entry(chain, &dev->chains, list) {
1658 ret = uvc_register_terms(dev, chain, &chain->iterms);
1659 if (ret < 0)
1660 return ret;
1662 ret = uvc_register_terms(dev, chain, &chain->oterms);
1663 if (ret < 0)
1664 return ret;
1667 return 0;
1670 /* ------------------------------------------------------------------------
1671 * USB probe, disconnect, suspend and resume
1675 * Delete the UVC device.
1677 * Called by the kernel when the last reference to the uvc_device structure
1678 * is released.
1680 * Unregistering the video devices is done here because every opened instance
1681 * must be closed before the device can be unregistered. An alternative would
1682 * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1683 * unregister the video devices on disconnect when that reference count drops
1684 * to zero.
1686 * As this function is called after or during disconnect(), all URBs have
1687 * already been canceled by the USB core. There is no need to kill the
1688 * interrupt URB manually.
1690 void uvc_delete(struct kref *kref)
1692 struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1693 struct list_head *p, *n;
1695 /* Unregister the video devices. */
1696 uvc_unregister_video(dev);
1697 usb_put_intf(dev->intf);
1698 usb_put_dev(dev->udev);
1700 uvc_status_cleanup(dev);
1701 uvc_ctrl_cleanup_device(dev);
1703 list_for_each_safe(p, n, &dev->chains) {
1704 struct uvc_video_chain *chain;
1705 chain = list_entry(p, struct uvc_video_chain, list);
1706 kfree(chain);
1709 list_for_each_safe(p, n, &dev->entities) {
1710 struct uvc_entity *entity;
1711 entity = list_entry(p, struct uvc_entity, list);
1712 kfree(entity);
1715 list_for_each_safe(p, n, &dev->streams) {
1716 struct uvc_streaming *streaming;
1717 streaming = list_entry(p, struct uvc_streaming, list);
1718 usb_driver_release_interface(&uvc_driver.driver,
1719 streaming->intf);
1720 usb_put_intf(streaming->intf);
1721 kfree(streaming->format);
1722 kfree(streaming->header.bmaControls);
1723 kfree(streaming);
1726 kfree(dev);
1729 static int uvc_probe(struct usb_interface *intf,
1730 const struct usb_device_id *id)
1732 struct usb_device *udev = interface_to_usbdev(intf);
1733 struct uvc_device *dev;
1734 int ret;
1736 if (id->idVendor && id->idProduct)
1737 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1738 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1739 id->idProduct);
1740 else
1741 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1742 udev->devpath);
1744 /* Allocate memory for the device and initialize it. */
1745 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1746 return -ENOMEM;
1748 INIT_LIST_HEAD(&dev->entities);
1749 INIT_LIST_HEAD(&dev->chains);
1750 INIT_LIST_HEAD(&dev->streams);
1751 kref_init(&dev->kref);
1752 atomic_set(&dev->users, 0);
1754 dev->udev = usb_get_dev(udev);
1755 dev->intf = usb_get_intf(intf);
1756 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1757 dev->quirks = id->driver_info | uvc_quirks_param;
1759 if (udev->product != NULL)
1760 strlcpy(dev->name, udev->product, sizeof dev->name);
1761 else
1762 snprintf(dev->name, sizeof dev->name,
1763 "UVC Camera (%04x:%04x)",
1764 le16_to_cpu(udev->descriptor.idVendor),
1765 le16_to_cpu(udev->descriptor.idProduct));
1767 /* Parse the Video Class control descriptor. */
1768 if (uvc_parse_control(dev) < 0) {
1769 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1770 "descriptors.\n");
1771 goto error;
1774 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1775 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1776 udev->product ? udev->product : "<unnamed>",
1777 le16_to_cpu(udev->descriptor.idVendor),
1778 le16_to_cpu(udev->descriptor.idProduct));
1780 if (uvc_quirks_param != 0) {
1781 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1782 "parameter for testing purpose.\n", uvc_quirks_param);
1783 uvc_printk(KERN_INFO, "Please report required quirks to the "
1784 "linux-uvc-devel mailing list.\n");
1787 /* Initialize controls. */
1788 if (uvc_ctrl_init_device(dev) < 0)
1789 goto error;
1791 /* Scan the device for video chains. */
1792 if (uvc_scan_device(dev) < 0)
1793 goto error;
1795 /* Register video devices. */
1796 if (uvc_register_chains(dev) < 0)
1797 goto error;
1799 /* Save our data pointer in the interface data. */
1800 usb_set_intfdata(intf, dev);
1802 /* Initialize the interrupt URB. */
1803 if ((ret = uvc_status_init(dev)) < 0) {
1804 uvc_printk(KERN_INFO, "Unable to initialize the status "
1805 "endpoint (%d), status interrupt will not be "
1806 "supported.\n", ret);
1809 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1810 return 0;
1812 error:
1813 kref_put(&dev->kref, uvc_delete);
1814 return -ENODEV;
1817 static void uvc_disconnect(struct usb_interface *intf)
1819 struct uvc_device *dev = usb_get_intfdata(intf);
1821 /* Set the USB interface data to NULL. This can be done outside the
1822 * lock, as there's no other reader.
1824 usb_set_intfdata(intf, NULL);
1826 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1827 UVC_SC_VIDEOSTREAMING)
1828 return;
1830 /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1831 * lock is needed to prevent uvc_disconnect from releasing its
1832 * reference to the uvc_device instance after uvc_v4l2_open() received
1833 * the pointer to the device (video_devdata) but before it got the
1834 * chance to increase the reference count (kref_get).
1836 * Note that the reference can't be released with the lock held,
1837 * otherwise a AB-BA deadlock can occur with videodev_lock that
1838 * videodev acquires in videodev_open() and video_unregister_device().
1840 mutex_lock(&uvc_driver.open_mutex);
1841 dev->state |= UVC_DEV_DISCONNECTED;
1842 mutex_unlock(&uvc_driver.open_mutex);
1844 kref_put(&dev->kref, uvc_delete);
1847 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1849 struct uvc_device *dev = usb_get_intfdata(intf);
1850 struct uvc_streaming *stream;
1852 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1853 intf->cur_altsetting->desc.bInterfaceNumber);
1855 /* Controls are cached on the fly so they don't need to be saved. */
1856 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1857 UVC_SC_VIDEOCONTROL)
1858 return uvc_status_suspend(dev);
1860 list_for_each_entry(stream, &dev->streams, list) {
1861 if (stream->intf == intf)
1862 return uvc_video_suspend(stream);
1865 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1866 "mismatch.\n");
1867 return -EINVAL;
1870 static int __uvc_resume(struct usb_interface *intf, int reset)
1872 struct uvc_device *dev = usb_get_intfdata(intf);
1873 struct uvc_streaming *stream;
1875 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1876 intf->cur_altsetting->desc.bInterfaceNumber);
1878 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1879 UVC_SC_VIDEOCONTROL) {
1880 if (reset) {
1881 int ret = uvc_ctrl_resume_device(dev);
1883 if (ret < 0)
1884 return ret;
1887 return uvc_status_resume(dev);
1890 list_for_each_entry(stream, &dev->streams, list) {
1891 if (stream->intf == intf)
1892 return uvc_video_resume(stream, reset);
1895 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1896 "mismatch.\n");
1897 return -EINVAL;
1900 static int uvc_resume(struct usb_interface *intf)
1902 return __uvc_resume(intf, 0);
1905 static int uvc_reset_resume(struct usb_interface *intf)
1907 return __uvc_resume(intf, 1);
1910 /* ------------------------------------------------------------------------
1911 * Driver initialization and cleanup
1915 * The Logitech cameras listed below have their interface class set to
1916 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1917 * though they are compliant.
1919 static struct usb_device_id uvc_ids[] = {
1920 /* Microsoft Lifecam NX-6000 */
1921 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1922 | USB_DEVICE_ID_MATCH_INT_INFO,
1923 .idVendor = 0x045e,
1924 .idProduct = 0x00f8,
1925 .bInterfaceClass = USB_CLASS_VIDEO,
1926 .bInterfaceSubClass = 1,
1927 .bInterfaceProtocol = 0,
1928 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1929 /* Microsoft Lifecam VX-7000 */
1930 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1931 | USB_DEVICE_ID_MATCH_INT_INFO,
1932 .idVendor = 0x045e,
1933 .idProduct = 0x0723,
1934 .bInterfaceClass = USB_CLASS_VIDEO,
1935 .bInterfaceSubClass = 1,
1936 .bInterfaceProtocol = 0,
1937 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1938 /* Logitech Quickcam Fusion */
1939 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1940 | USB_DEVICE_ID_MATCH_INT_INFO,
1941 .idVendor = 0x046d,
1942 .idProduct = 0x08c1,
1943 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1944 .bInterfaceSubClass = 1,
1945 .bInterfaceProtocol = 0 },
1946 /* Logitech Quickcam Orbit MP */
1947 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1948 | USB_DEVICE_ID_MATCH_INT_INFO,
1949 .idVendor = 0x046d,
1950 .idProduct = 0x08c2,
1951 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1952 .bInterfaceSubClass = 1,
1953 .bInterfaceProtocol = 0 },
1954 /* Logitech Quickcam Pro for Notebook */
1955 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1956 | USB_DEVICE_ID_MATCH_INT_INFO,
1957 .idVendor = 0x046d,
1958 .idProduct = 0x08c3,
1959 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1960 .bInterfaceSubClass = 1,
1961 .bInterfaceProtocol = 0 },
1962 /* Logitech Quickcam Pro 5000 */
1963 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1964 | USB_DEVICE_ID_MATCH_INT_INFO,
1965 .idVendor = 0x046d,
1966 .idProduct = 0x08c5,
1967 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1968 .bInterfaceSubClass = 1,
1969 .bInterfaceProtocol = 0 },
1970 /* Logitech Quickcam OEM Dell Notebook */
1971 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1972 | USB_DEVICE_ID_MATCH_INT_INFO,
1973 .idVendor = 0x046d,
1974 .idProduct = 0x08c6,
1975 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1976 .bInterfaceSubClass = 1,
1977 .bInterfaceProtocol = 0 },
1978 /* Logitech Quickcam OEM Cisco VT Camera II */
1979 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1980 | USB_DEVICE_ID_MATCH_INT_INFO,
1981 .idVendor = 0x046d,
1982 .idProduct = 0x08c7,
1983 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1984 .bInterfaceSubClass = 1,
1985 .bInterfaceProtocol = 0 },
1986 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1987 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1988 | USB_DEVICE_ID_MATCH_INT_INFO,
1989 .idVendor = 0x058f,
1990 .idProduct = 0x3820,
1991 .bInterfaceClass = USB_CLASS_VIDEO,
1992 .bInterfaceSubClass = 1,
1993 .bInterfaceProtocol = 0,
1994 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1995 /* Apple Built-In iSight */
1996 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1997 | USB_DEVICE_ID_MATCH_INT_INFO,
1998 .idVendor = 0x05ac,
1999 .idProduct = 0x8501,
2000 .bInterfaceClass = USB_CLASS_VIDEO,
2001 .bInterfaceSubClass = 1,
2002 .bInterfaceProtocol = 0,
2003 .driver_info = UVC_QUIRK_PROBE_MINMAX
2004 | UVC_QUIRK_BUILTIN_ISIGHT },
2005 /* Genesys Logic USB 2.0 PC Camera */
2006 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2007 | USB_DEVICE_ID_MATCH_INT_INFO,
2008 .idVendor = 0x05e3,
2009 .idProduct = 0x0505,
2010 .bInterfaceClass = USB_CLASS_VIDEO,
2011 .bInterfaceSubClass = 1,
2012 .bInterfaceProtocol = 0,
2013 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2014 /* ViMicro Vega */
2015 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2016 | USB_DEVICE_ID_MATCH_INT_INFO,
2017 .idVendor = 0x0ac8,
2018 .idProduct = 0x332d,
2019 .bInterfaceClass = USB_CLASS_VIDEO,
2020 .bInterfaceSubClass = 1,
2021 .bInterfaceProtocol = 0,
2022 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2023 /* ViMicro - Minoru3D */
2024 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2025 | USB_DEVICE_ID_MATCH_INT_INFO,
2026 .idVendor = 0x0ac8,
2027 .idProduct = 0x3410,
2028 .bInterfaceClass = USB_CLASS_VIDEO,
2029 .bInterfaceSubClass = 1,
2030 .bInterfaceProtocol = 0,
2031 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2032 /* ViMicro Venus - Minoru3D */
2033 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2034 | USB_DEVICE_ID_MATCH_INT_INFO,
2035 .idVendor = 0x0ac8,
2036 .idProduct = 0x3420,
2037 .bInterfaceClass = USB_CLASS_VIDEO,
2038 .bInterfaceSubClass = 1,
2039 .bInterfaceProtocol = 0,
2040 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2041 /* MT6227 */
2042 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2043 | USB_DEVICE_ID_MATCH_INT_INFO,
2044 .idVendor = 0x0e8d,
2045 .idProduct = 0x0004,
2046 .bInterfaceClass = USB_CLASS_VIDEO,
2047 .bInterfaceSubClass = 1,
2048 .bInterfaceProtocol = 0,
2049 .driver_info = UVC_QUIRK_PROBE_MINMAX
2050 | UVC_QUIRK_PROBE_DEF },
2051 /* Syntek (HP Spartan) */
2052 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2053 | USB_DEVICE_ID_MATCH_INT_INFO,
2054 .idVendor = 0x174f,
2055 .idProduct = 0x5212,
2056 .bInterfaceClass = USB_CLASS_VIDEO,
2057 .bInterfaceSubClass = 1,
2058 .bInterfaceProtocol = 0,
2059 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2060 /* Syntek (Samsung Q310) */
2061 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2062 | USB_DEVICE_ID_MATCH_INT_INFO,
2063 .idVendor = 0x174f,
2064 .idProduct = 0x5931,
2065 .bInterfaceClass = USB_CLASS_VIDEO,
2066 .bInterfaceSubClass = 1,
2067 .bInterfaceProtocol = 0,
2068 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2069 /* Syntek (Packard Bell EasyNote MX52 */
2070 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2071 | USB_DEVICE_ID_MATCH_INT_INFO,
2072 .idVendor = 0x174f,
2073 .idProduct = 0x8a12,
2074 .bInterfaceClass = USB_CLASS_VIDEO,
2075 .bInterfaceSubClass = 1,
2076 .bInterfaceProtocol = 0,
2077 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2078 /* Syntek (Asus F9SG) */
2079 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2080 | USB_DEVICE_ID_MATCH_INT_INFO,
2081 .idVendor = 0x174f,
2082 .idProduct = 0x8a31,
2083 .bInterfaceClass = USB_CLASS_VIDEO,
2084 .bInterfaceSubClass = 1,
2085 .bInterfaceProtocol = 0,
2086 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2087 /* Syntek (Asus U3S) */
2088 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2089 | USB_DEVICE_ID_MATCH_INT_INFO,
2090 .idVendor = 0x174f,
2091 .idProduct = 0x8a33,
2092 .bInterfaceClass = USB_CLASS_VIDEO,
2093 .bInterfaceSubClass = 1,
2094 .bInterfaceProtocol = 0,
2095 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2096 /* Syntek (JAOtech Smart Terminal) */
2097 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2098 | USB_DEVICE_ID_MATCH_INT_INFO,
2099 .idVendor = 0x174f,
2100 .idProduct = 0x8a34,
2101 .bInterfaceClass = USB_CLASS_VIDEO,
2102 .bInterfaceSubClass = 1,
2103 .bInterfaceProtocol = 0,
2104 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2105 /* Lenovo Thinkpad SL400/SL500 */
2106 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2107 | USB_DEVICE_ID_MATCH_INT_INFO,
2108 .idVendor = 0x17ef,
2109 .idProduct = 0x480b,
2110 .bInterfaceClass = USB_CLASS_VIDEO,
2111 .bInterfaceSubClass = 1,
2112 .bInterfaceProtocol = 0,
2113 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2114 /* Aveo Technology USB 2.0 Camera */
2115 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2116 | USB_DEVICE_ID_MATCH_INT_INFO,
2117 .idVendor = 0x1871,
2118 .idProduct = 0x0306,
2119 .bInterfaceClass = USB_CLASS_VIDEO,
2120 .bInterfaceSubClass = 1,
2121 .bInterfaceProtocol = 0,
2122 .driver_info = UVC_QUIRK_PROBE_MINMAX
2123 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2124 /* Ecamm Pico iMage */
2125 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2126 | USB_DEVICE_ID_MATCH_INT_INFO,
2127 .idVendor = 0x18cd,
2128 .idProduct = 0xcafe,
2129 .bInterfaceClass = USB_CLASS_VIDEO,
2130 .bInterfaceSubClass = 1,
2131 .bInterfaceProtocol = 0,
2132 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2133 /* FSC WebCam V30S */
2134 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2135 | USB_DEVICE_ID_MATCH_INT_INFO,
2136 .idVendor = 0x18ec,
2137 .idProduct = 0x3288,
2138 .bInterfaceClass = USB_CLASS_VIDEO,
2139 .bInterfaceSubClass = 1,
2140 .bInterfaceProtocol = 0,
2141 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2142 /* Arkmicro unbranded */
2143 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2144 | USB_DEVICE_ID_MATCH_INT_INFO,
2145 .idVendor = 0x18ec,
2146 .idProduct = 0x3290,
2147 .bInterfaceClass = USB_CLASS_VIDEO,
2148 .bInterfaceSubClass = 1,
2149 .bInterfaceProtocol = 0,
2150 .driver_info = UVC_QUIRK_PROBE_DEF },
2151 /* Bodelin ProScopeHR */
2152 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2153 | USB_DEVICE_ID_MATCH_DEV_HI
2154 | USB_DEVICE_ID_MATCH_INT_INFO,
2155 .idVendor = 0x19ab,
2156 .idProduct = 0x1000,
2157 .bcdDevice_hi = 0x0126,
2158 .bInterfaceClass = USB_CLASS_VIDEO,
2159 .bInterfaceSubClass = 1,
2160 .bInterfaceProtocol = 0,
2161 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2162 /* SiGma Micro USB Web Camera */
2163 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2164 | USB_DEVICE_ID_MATCH_INT_INFO,
2165 .idVendor = 0x1c4f,
2166 .idProduct = 0x3000,
2167 .bInterfaceClass = USB_CLASS_VIDEO,
2168 .bInterfaceSubClass = 1,
2169 .bInterfaceProtocol = 0,
2170 .driver_info = UVC_QUIRK_PROBE_MINMAX
2171 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2172 /* Generic USB Video Class */
2173 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2177 MODULE_DEVICE_TABLE(usb, uvc_ids);
2179 struct uvc_driver uvc_driver = {
2180 .driver = {
2181 .name = "uvcvideo",
2182 .probe = uvc_probe,
2183 .disconnect = uvc_disconnect,
2184 .suspend = uvc_suspend,
2185 .resume = uvc_resume,
2186 .reset_resume = uvc_reset_resume,
2187 .id_table = uvc_ids,
2188 .supports_autosuspend = 1,
2192 static int __init uvc_init(void)
2194 int result;
2196 INIT_LIST_HEAD(&uvc_driver.devices);
2197 INIT_LIST_HEAD(&uvc_driver.controls);
2198 mutex_init(&uvc_driver.open_mutex);
2199 mutex_init(&uvc_driver.ctrl_mutex);
2201 uvc_ctrl_init();
2203 result = usb_register(&uvc_driver.driver);
2204 if (result == 0)
2205 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2206 return result;
2209 static void __exit uvc_cleanup(void)
2211 usb_deregister(&uvc_driver.driver);
2214 module_init(uvc_init);
2215 module_exit(uvc_cleanup);
2217 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2218 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2219 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2220 MODULE_PARM_DESC(quirks, "Forced device quirks");
2221 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2222 MODULE_PARM_DESC(trace, "Trace level bitmask");
2224 MODULE_AUTHOR(DRIVER_AUTHOR);
2225 MODULE_DESCRIPTION(DRIVER_DESC);
2226 MODULE_LICENSE("GPL");
2227 MODULE_VERSION(DRIVER_VERSION);