uvcvideo: Fix descriptor parsing for video output devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / uvc / uvc_driver.c
blob86699b11dbd02c08f6f857f500ede308db06515a
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;
49 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
51 /* ------------------------------------------------------------------------
52 * Video formats
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:2 (YUYV)",
63 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
64 .fcc = V4L2_PIX_FMT_YUYV,
67 .name = "YUV 4:2:0 (NV12)",
68 .guid = UVC_GUID_FORMAT_NV12,
69 .fcc = V4L2_PIX_FMT_NV12,
72 .name = "MJPEG",
73 .guid = UVC_GUID_FORMAT_MJPEG,
74 .fcc = V4L2_PIX_FMT_MJPEG,
77 .name = "YVU 4:2:0 (YV12)",
78 .guid = UVC_GUID_FORMAT_YV12,
79 .fcc = V4L2_PIX_FMT_YVU420,
82 .name = "YUV 4:2:0 (I420)",
83 .guid = UVC_GUID_FORMAT_I420,
84 .fcc = V4L2_PIX_FMT_YUV420,
87 .name = "YUV 4:2:2 (UYVY)",
88 .guid = UVC_GUID_FORMAT_UYVY,
89 .fcc = V4L2_PIX_FMT_UYVY,
92 .name = "Greyscale (8-bit)",
93 .guid = UVC_GUID_FORMAT_Y800,
94 .fcc = V4L2_PIX_FMT_GREY,
97 .name = "Greyscale (16-bit)",
98 .guid = UVC_GUID_FORMAT_Y16,
99 .fcc = V4L2_PIX_FMT_Y16,
102 .name = "RGB Bayer",
103 .guid = UVC_GUID_FORMAT_BY8,
104 .fcc = V4L2_PIX_FMT_SBGGR8,
108 /* ------------------------------------------------------------------------
109 * Utility functions
112 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
113 __u8 epaddr)
115 struct usb_host_endpoint *ep;
116 unsigned int i;
118 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
119 ep = &alts->endpoint[i];
120 if (ep->desc.bEndpointAddress == epaddr)
121 return ep;
124 return NULL;
127 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
129 unsigned int len = ARRAY_SIZE(uvc_fmts);
130 unsigned int i;
132 for (i = 0; i < len; ++i) {
133 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
134 return &uvc_fmts[i];
137 return NULL;
140 static __u32 uvc_colorspace(const __u8 primaries)
142 static const __u8 colorprimaries[] = {
144 V4L2_COLORSPACE_SRGB,
145 V4L2_COLORSPACE_470_SYSTEM_M,
146 V4L2_COLORSPACE_470_SYSTEM_BG,
147 V4L2_COLORSPACE_SMPTE170M,
148 V4L2_COLORSPACE_SMPTE240M,
151 if (primaries < ARRAY_SIZE(colorprimaries))
152 return colorprimaries[primaries];
154 return 0;
157 /* Simplify a fraction using a simple continued fraction decomposition. The
158 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
159 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
160 * arbitrary parameters to remove non-significative terms from the simple
161 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
162 * respectively seems to give nice results.
164 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
165 unsigned int n_terms, unsigned int threshold)
167 uint32_t *an;
168 uint32_t x, y, r;
169 unsigned int i, n;
171 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
172 if (an == NULL)
173 return;
175 /* Convert the fraction to a simple continued fraction. See
176 * http://mathforum.org/dr.math/faq/faq.fractions.html
177 * Stop if the current term is bigger than or equal to the given
178 * threshold.
180 x = *numerator;
181 y = *denominator;
183 for (n = 0; n < n_terms && y != 0; ++n) {
184 an[n] = x / y;
185 if (an[n] >= threshold) {
186 if (n < 2)
187 n++;
188 break;
191 r = x - an[n] * y;
192 x = y;
193 y = r;
196 /* Expand the simple continued fraction back to an integer fraction. */
197 x = 0;
198 y = 1;
200 for (i = n; i > 0; --i) {
201 r = y;
202 y = an[i-1] * y + x;
203 x = r;
206 *numerator = y;
207 *denominator = x;
208 kfree(an);
211 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
212 * to compute numerator / denominator * 10000000 using 32 bit fixed point
213 * arithmetic only.
215 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
217 uint32_t multiplier;
219 /* Saturate the result if the operation would overflow. */
220 if (denominator == 0 ||
221 numerator/denominator >= ((uint32_t)-1)/10000000)
222 return (uint32_t)-1;
224 /* Divide both the denominator and the multiplier by two until
225 * numerator * multiplier doesn't overflow. If anyone knows a better
226 * algorithm please let me know.
228 multiplier = 10000000;
229 while (numerator > ((uint32_t)-1)/multiplier) {
230 multiplier /= 2;
231 denominator /= 2;
234 return denominator ? numerator * multiplier / denominator : 0;
237 /* ------------------------------------------------------------------------
238 * Terminal and unit management
241 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
243 struct uvc_entity *entity;
245 list_for_each_entry(entity, &dev->entities, list) {
246 if (entity->id == id)
247 return entity;
250 return NULL;
253 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
254 int id, struct uvc_entity *entity)
256 unsigned int i;
258 if (entity == NULL)
259 entity = list_entry(&dev->entities, struct uvc_entity, list);
261 list_for_each_entry_continue(entity, &dev->entities, list) {
262 for (i = 0; i < entity->bNrInPins; ++i)
263 if (entity->baSourceID[i] == id)
264 return entity;
267 return NULL;
270 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
272 struct uvc_streaming *stream;
274 list_for_each_entry(stream, &dev->streams, list) {
275 if (stream->header.bTerminalLink == id)
276 return stream;
279 return NULL;
282 /* ------------------------------------------------------------------------
283 * Descriptors parsing
286 static int uvc_parse_format(struct uvc_device *dev,
287 struct uvc_streaming *streaming, struct uvc_format *format,
288 __u32 **intervals, unsigned char *buffer, int buflen)
290 struct usb_interface *intf = streaming->intf;
291 struct usb_host_interface *alts = intf->cur_altsetting;
292 struct uvc_format_desc *fmtdesc;
293 struct uvc_frame *frame;
294 const unsigned char *start = buffer;
295 unsigned int interval;
296 unsigned int i, n;
297 __u8 ftype;
299 format->type = buffer[2];
300 format->index = buffer[3];
302 switch (buffer[2]) {
303 case UVC_VS_FORMAT_UNCOMPRESSED:
304 case UVC_VS_FORMAT_FRAME_BASED:
305 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
306 if (buflen < n) {
307 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
308 "interface %d FORMAT error\n",
309 dev->udev->devnum,
310 alts->desc.bInterfaceNumber);
311 return -EINVAL;
314 /* Find the format descriptor from its GUID. */
315 fmtdesc = uvc_format_by_guid(&buffer[5]);
317 if (fmtdesc != NULL) {
318 strlcpy(format->name, fmtdesc->name,
319 sizeof format->name);
320 format->fcc = fmtdesc->fcc;
321 } else {
322 uvc_printk(KERN_INFO, "Unknown video format "
323 UVC_GUID_FORMAT "\n",
324 UVC_GUID_ARGS(&buffer[5]));
325 snprintf(format->name, sizeof format->name,
326 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
327 format->fcc = 0;
330 format->bpp = buffer[21];
331 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
332 ftype = UVC_VS_FRAME_UNCOMPRESSED;
333 } else {
334 ftype = UVC_VS_FRAME_FRAME_BASED;
335 if (buffer[27])
336 format->flags = UVC_FMT_FLAG_COMPRESSED;
338 break;
340 case UVC_VS_FORMAT_MJPEG:
341 if (buflen < 11) {
342 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
343 "interface %d FORMAT error\n",
344 dev->udev->devnum,
345 alts->desc.bInterfaceNumber);
346 return -EINVAL;
349 strlcpy(format->name, "MJPEG", sizeof format->name);
350 format->fcc = V4L2_PIX_FMT_MJPEG;
351 format->flags = UVC_FMT_FLAG_COMPRESSED;
352 format->bpp = 0;
353 ftype = UVC_VS_FRAME_MJPEG;
354 break;
356 case UVC_VS_FORMAT_DV:
357 if (buflen < 9) {
358 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
359 "interface %d FORMAT error\n",
360 dev->udev->devnum,
361 alts->desc.bInterfaceNumber);
362 return -EINVAL;
365 switch (buffer[8] & 0x7f) {
366 case 0:
367 strlcpy(format->name, "SD-DV", sizeof format->name);
368 break;
369 case 1:
370 strlcpy(format->name, "SDL-DV", sizeof format->name);
371 break;
372 case 2:
373 strlcpy(format->name, "HD-DV", sizeof format->name);
374 break;
375 default:
376 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
377 "interface %d: unknown DV format %u\n",
378 dev->udev->devnum,
379 alts->desc.bInterfaceNumber, buffer[8]);
380 return -EINVAL;
383 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
384 sizeof format->name);
386 format->fcc = V4L2_PIX_FMT_DV;
387 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
388 format->bpp = 0;
389 ftype = 0;
391 /* Create a dummy frame descriptor. */
392 frame = &format->frame[0];
393 memset(&format->frame[0], 0, sizeof format->frame[0]);
394 frame->bFrameIntervalType = 1;
395 frame->dwDefaultFrameInterval = 1;
396 frame->dwFrameInterval = *intervals;
397 *(*intervals)++ = 1;
398 format->nframes = 1;
399 break;
401 case UVC_VS_FORMAT_MPEG2TS:
402 case UVC_VS_FORMAT_STREAM_BASED:
403 /* Not supported yet. */
404 default:
405 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
406 "interface %d unsupported format %u\n",
407 dev->udev->devnum, alts->desc.bInterfaceNumber,
408 buffer[2]);
409 return -EINVAL;
412 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
414 buflen -= buffer[0];
415 buffer += buffer[0];
417 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
418 * based formats have frame descriptors.
420 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
421 buffer[2] == ftype) {
422 frame = &format->frame[format->nframes];
423 if (ftype != UVC_VS_FRAME_FRAME_BASED)
424 n = buflen > 25 ? buffer[25] : 0;
425 else
426 n = buflen > 21 ? buffer[21] : 0;
428 n = n ? n : 3;
430 if (buflen < 26 + 4*n) {
431 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
432 "interface %d FRAME error\n", dev->udev->devnum,
433 alts->desc.bInterfaceNumber);
434 return -EINVAL;
437 frame->bFrameIndex = buffer[3];
438 frame->bmCapabilities = buffer[4];
439 frame->wWidth = get_unaligned_le16(&buffer[5]);
440 frame->wHeight = get_unaligned_le16(&buffer[7]);
441 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
442 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
443 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
444 frame->dwMaxVideoFrameBufferSize =
445 get_unaligned_le32(&buffer[17]);
446 frame->dwDefaultFrameInterval =
447 get_unaligned_le32(&buffer[21]);
448 frame->bFrameIntervalType = buffer[25];
449 } else {
450 frame->dwMaxVideoFrameBufferSize = 0;
451 frame->dwDefaultFrameInterval =
452 get_unaligned_le32(&buffer[17]);
453 frame->bFrameIntervalType = buffer[21];
455 frame->dwFrameInterval = *intervals;
457 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
458 * completely. Observed behaviours range from setting the
459 * value to 1.1x the actual frame size to hardwiring the
460 * 16 low bits to 0. This results in a higher than necessary
461 * memory usage as well as a wrong image size information. For
462 * uncompressed formats this can be fixed by computing the
463 * value from the frame size.
465 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
466 frame->dwMaxVideoFrameBufferSize = format->bpp
467 * frame->wWidth * frame->wHeight / 8;
469 /* Some bogus devices report dwMinFrameInterval equal to
470 * dwMaxFrameInterval and have dwFrameIntervalStep set to
471 * zero. Setting all null intervals to 1 fixes the problem and
472 * some other divisions by zero that could happen.
474 for (i = 0; i < n; ++i) {
475 interval = get_unaligned_le32(&buffer[26+4*i]);
476 *(*intervals)++ = interval ? interval : 1;
479 /* Make sure that the default frame interval stays between
480 * the boundaries.
482 n -= frame->bFrameIntervalType ? 1 : 2;
483 frame->dwDefaultFrameInterval =
484 min(frame->dwFrameInterval[n],
485 max(frame->dwFrameInterval[0],
486 frame->dwDefaultFrameInterval));
488 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
489 frame->wWidth, frame->wHeight,
490 10000000/frame->dwDefaultFrameInterval,
491 (100000000/frame->dwDefaultFrameInterval)%10);
493 format->nframes++;
494 buflen -= buffer[0];
495 buffer += buffer[0];
498 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
499 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
500 buflen -= buffer[0];
501 buffer += buffer[0];
504 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
505 buffer[2] == UVC_VS_COLORFORMAT) {
506 if (buflen < 6) {
507 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
508 "interface %d COLORFORMAT error\n",
509 dev->udev->devnum,
510 alts->desc.bInterfaceNumber);
511 return -EINVAL;
514 format->colorspace = uvc_colorspace(buffer[3]);
516 buflen -= buffer[0];
517 buffer += buffer[0];
520 return buffer - start;
523 static int uvc_parse_streaming(struct uvc_device *dev,
524 struct usb_interface *intf)
526 struct uvc_streaming *streaming = NULL;
527 struct uvc_format *format;
528 struct uvc_frame *frame;
529 struct usb_host_interface *alts = &intf->altsetting[0];
530 unsigned char *_buffer, *buffer = alts->extra;
531 int _buflen, buflen = alts->extralen;
532 unsigned int nformats = 0, nframes = 0, nintervals = 0;
533 unsigned int size, i, n, p;
534 __u32 *interval;
535 __u16 psize;
536 int ret = -EINVAL;
538 if (intf->cur_altsetting->desc.bInterfaceSubClass
539 != UVC_SC_VIDEOSTREAMING) {
540 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
541 "video streaming interface\n", dev->udev->devnum,
542 intf->altsetting[0].desc.bInterfaceNumber);
543 return -EINVAL;
546 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
547 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
548 "claimed\n", dev->udev->devnum,
549 intf->altsetting[0].desc.bInterfaceNumber);
550 return -EINVAL;
553 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
554 if (streaming == NULL) {
555 usb_driver_release_interface(&uvc_driver.driver, intf);
556 return -EINVAL;
559 mutex_init(&streaming->mutex);
560 streaming->dev = dev;
561 streaming->intf = usb_get_intf(intf);
562 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
564 /* The Pico iMage webcam has its class-specific interface descriptors
565 * after the endpoint descriptors.
567 if (buflen == 0) {
568 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
569 struct usb_host_endpoint *ep = &alts->endpoint[i];
571 if (ep->extralen == 0)
572 continue;
574 if (ep->extralen > 2 &&
575 ep->extra[1] == USB_DT_CS_INTERFACE) {
576 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
577 "from endpoint %u.\n", i);
578 buffer = alts->endpoint[i].extra;
579 buflen = alts->endpoint[i].extralen;
580 break;
585 /* Skip the standard interface descriptors. */
586 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
587 buflen -= buffer[0];
588 buffer += buffer[0];
591 if (buflen <= 2) {
592 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
593 "interface descriptors found.\n");
594 goto error;
597 /* Parse the header descriptor. */
598 switch (buffer[2]) {
599 case UVC_VS_OUTPUT_HEADER:
600 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
601 size = 9;
602 break;
604 case UVC_VS_INPUT_HEADER:
605 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
606 size = 13;
607 break;
609 default:
610 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
611 "%d HEADER descriptor not found.\n", dev->udev->devnum,
612 alts->desc.bInterfaceNumber);
613 goto error;
616 p = buflen >= 4 ? buffer[3] : 0;
617 n = buflen >= size ? buffer[size-1] : 0;
619 if (buflen < size + p*n) {
620 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
621 "interface %d HEADER descriptor is invalid.\n",
622 dev->udev->devnum, alts->desc.bInterfaceNumber);
623 goto error;
626 streaming->header.bNumFormats = p;
627 streaming->header.bEndpointAddress = buffer[6];
628 if (buffer[2] == UVC_VS_INPUT_HEADER) {
629 streaming->header.bmInfo = buffer[7];
630 streaming->header.bTerminalLink = buffer[8];
631 streaming->header.bStillCaptureMethod = buffer[9];
632 streaming->header.bTriggerSupport = buffer[10];
633 streaming->header.bTriggerUsage = buffer[11];
634 } else {
635 streaming->header.bTerminalLink = buffer[7];
637 streaming->header.bControlSize = n;
639 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
640 if (streaming->header.bmaControls == NULL) {
641 ret = -ENOMEM;
642 goto error;
645 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
647 buflen -= buffer[0];
648 buffer += buffer[0];
650 _buffer = buffer;
651 _buflen = buflen;
653 /* Count the format and frame descriptors. */
654 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
655 switch (_buffer[2]) {
656 case UVC_VS_FORMAT_UNCOMPRESSED:
657 case UVC_VS_FORMAT_MJPEG:
658 case UVC_VS_FORMAT_FRAME_BASED:
659 nformats++;
660 break;
662 case UVC_VS_FORMAT_DV:
663 /* DV format has no frame descriptor. We will create a
664 * dummy frame descriptor with a dummy frame interval.
666 nformats++;
667 nframes++;
668 nintervals++;
669 break;
671 case UVC_VS_FORMAT_MPEG2TS:
672 case UVC_VS_FORMAT_STREAM_BASED:
673 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
674 "interface %d FORMAT %u is not supported.\n",
675 dev->udev->devnum,
676 alts->desc.bInterfaceNumber, _buffer[2]);
677 break;
679 case UVC_VS_FRAME_UNCOMPRESSED:
680 case UVC_VS_FRAME_MJPEG:
681 nframes++;
682 if (_buflen > 25)
683 nintervals += _buffer[25] ? _buffer[25] : 3;
684 break;
686 case UVC_VS_FRAME_FRAME_BASED:
687 nframes++;
688 if (_buflen > 21)
689 nintervals += _buffer[21] ? _buffer[21] : 3;
690 break;
693 _buflen -= _buffer[0];
694 _buffer += _buffer[0];
697 if (nformats == 0) {
698 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
699 "%d has no supported formats defined.\n",
700 dev->udev->devnum, alts->desc.bInterfaceNumber);
701 goto error;
704 size = nformats * sizeof *format + nframes * sizeof *frame
705 + nintervals * sizeof *interval;
706 format = kzalloc(size, GFP_KERNEL);
707 if (format == NULL) {
708 ret = -ENOMEM;
709 goto error;
712 frame = (struct uvc_frame *)&format[nformats];
713 interval = (__u32 *)&frame[nframes];
715 streaming->format = format;
716 streaming->nformats = nformats;
718 /* Parse the format descriptors. */
719 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
720 switch (buffer[2]) {
721 case UVC_VS_FORMAT_UNCOMPRESSED:
722 case UVC_VS_FORMAT_MJPEG:
723 case UVC_VS_FORMAT_DV:
724 case UVC_VS_FORMAT_FRAME_BASED:
725 format->frame = frame;
726 ret = uvc_parse_format(dev, streaming, format,
727 &interval, buffer, buflen);
728 if (ret < 0)
729 goto error;
731 frame += format->nframes;
732 format++;
734 buflen -= ret;
735 buffer += ret;
736 continue;
738 default:
739 break;
742 buflen -= buffer[0];
743 buffer += buffer[0];
746 if (buflen)
747 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
748 "%d has %u bytes of trailing descriptor garbage.\n",
749 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
751 /* Parse the alternate settings to find the maximum bandwidth. */
752 for (i = 0; i < intf->num_altsetting; ++i) {
753 struct usb_host_endpoint *ep;
754 alts = &intf->altsetting[i];
755 ep = uvc_find_endpoint(alts,
756 streaming->header.bEndpointAddress);
757 if (ep == NULL)
758 continue;
760 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
761 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
762 if (psize > streaming->maxpsize)
763 streaming->maxpsize = psize;
766 list_add_tail(&streaming->list, &dev->streams);
767 return 0;
769 error:
770 usb_driver_release_interface(&uvc_driver.driver, intf);
771 usb_put_intf(intf);
772 kfree(streaming->format);
773 kfree(streaming->header.bmaControls);
774 kfree(streaming);
775 return ret;
778 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
779 unsigned int num_pads, unsigned int extra_size)
781 struct uvc_entity *entity;
782 unsigned int num_inputs;
783 unsigned int size;
785 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
786 size = sizeof(*entity) + extra_size + num_inputs;
787 entity = kzalloc(size, GFP_KERNEL);
788 if (entity == NULL)
789 return NULL;
791 entity->id = id;
792 entity->type = type;
794 entity->bNrInPins = num_inputs;
795 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
797 return entity;
800 /* Parse vendor-specific extensions. */
801 static int uvc_parse_vendor_control(struct uvc_device *dev,
802 const unsigned char *buffer, int buflen)
804 struct usb_device *udev = dev->udev;
805 struct usb_host_interface *alts = dev->intf->cur_altsetting;
806 struct uvc_entity *unit;
807 unsigned int n, p;
808 int handled = 0;
810 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
811 case 0x046d: /* Logitech */
812 if (buffer[1] != 0x41 || buffer[2] != 0x01)
813 break;
815 /* Logitech implements several vendor specific functions
816 * through vendor specific extension units (LXU).
818 * The LXU descriptors are similar to XU descriptors
819 * (see "USB Device Video Class for Video Devices", section
820 * 3.7.2.6 "Extension Unit Descriptor") with the following
821 * differences:
823 * ----------------------------------------------------------
824 * 0 bLength 1 Number
825 * Size of this descriptor, in bytes: 24+p+n*2
826 * ----------------------------------------------------------
827 * 23+p+n bmControlsType N Bitmap
828 * Individual bits in the set are defined:
829 * 0: Absolute
830 * 1: Relative
832 * This bitset is mapped exactly the same as bmControls.
833 * ----------------------------------------------------------
834 * 23+p+n*2 bReserved 1 Boolean
835 * ----------------------------------------------------------
836 * 24+p+n*2 iExtension 1 Index
837 * Index of a string descriptor that describes this
838 * extension unit.
839 * ----------------------------------------------------------
841 p = buflen >= 22 ? buffer[21] : 0;
842 n = buflen >= 25 + p ? buffer[22+p] : 0;
844 if (buflen < 25 + p + 2*n) {
845 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
846 "interface %d EXTENSION_UNIT error\n",
847 udev->devnum, alts->desc.bInterfaceNumber);
848 break;
851 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
852 p + 1, 2*n);
853 if (unit == NULL)
854 return -ENOMEM;
856 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
857 unit->extension.bNumControls = buffer[20];
858 memcpy(unit->baSourceID, &buffer[22], p);
859 unit->extension.bControlSize = buffer[22+p];
860 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
861 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
862 + 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 = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
960 1, n + p);
961 if (term == NULL)
962 return -ENOMEM;
964 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
965 term->camera.bControlSize = n;
966 term->camera.bmControls = (__u8 *)term + sizeof *term;
967 term->camera.wObjectiveFocalLengthMin =
968 get_unaligned_le16(&buffer[8]);
969 term->camera.wObjectiveFocalLengthMax =
970 get_unaligned_le16(&buffer[10]);
971 term->camera.wOcularFocalLength =
972 get_unaligned_le16(&buffer[12]);
973 memcpy(term->camera.bmControls, &buffer[15], n);
974 } else if (UVC_ENTITY_TYPE(term) ==
975 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
976 term->media.bControlSize = n;
977 term->media.bmControls = (__u8 *)term + sizeof *term;
978 term->media.bTransportModeSize = p;
979 term->media.bmTransportModes = (__u8 *)term
980 + sizeof *term + n;
981 memcpy(term->media.bmControls, &buffer[9], n);
982 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
985 if (buffer[7] != 0)
986 usb_string(udev, buffer[7], term->name,
987 sizeof term->name);
988 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
989 sprintf(term->name, "Camera %u", buffer[3]);
990 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
991 sprintf(term->name, "Media %u", buffer[3]);
992 else
993 sprintf(term->name, "Input %u", buffer[3]);
995 list_add_tail(&term->list, &dev->entities);
996 break;
998 case UVC_VC_OUTPUT_TERMINAL:
999 if (buflen < 9) {
1000 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1001 "interface %d OUTPUT_TERMINAL error\n",
1002 udev->devnum, alts->desc.bInterfaceNumber);
1003 return -EINVAL;
1006 /* Make sure the terminal type MSB is not null, otherwise it
1007 * could be confused with a unit.
1009 type = get_unaligned_le16(&buffer[4]);
1010 if ((type & 0xff00) == 0) {
1011 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1012 "interface %d OUTPUT_TERMINAL %d has invalid "
1013 "type 0x%04x, skipping\n", udev->devnum,
1014 alts->desc.bInterfaceNumber, buffer[3], type);
1015 return 0;
1018 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1019 1, 0);
1020 if (term == NULL)
1021 return -ENOMEM;
1023 memcpy(term->baSourceID, &buffer[7], 1);
1025 if (buffer[8] != 0)
1026 usb_string(udev, buffer[8], term->name,
1027 sizeof term->name);
1028 else
1029 sprintf(term->name, "Output %u", buffer[3]);
1031 list_add_tail(&term->list, &dev->entities);
1032 break;
1034 case UVC_VC_SELECTOR_UNIT:
1035 p = buflen >= 5 ? buffer[4] : 0;
1037 if (buflen < 5 || buflen < 6 + p) {
1038 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1039 "interface %d SELECTOR_UNIT error\n",
1040 udev->devnum, alts->desc.bInterfaceNumber);
1041 return -EINVAL;
1044 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1045 if (unit == NULL)
1046 return -ENOMEM;
1048 memcpy(unit->baSourceID, &buffer[5], p);
1050 if (buffer[5+p] != 0)
1051 usb_string(udev, buffer[5+p], unit->name,
1052 sizeof unit->name);
1053 else
1054 sprintf(unit->name, "Selector %u", buffer[3]);
1056 list_add_tail(&unit->list, &dev->entities);
1057 break;
1059 case UVC_VC_PROCESSING_UNIT:
1060 n = buflen >= 8 ? buffer[7] : 0;
1061 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1063 if (buflen < p + n) {
1064 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1065 "interface %d PROCESSING_UNIT error\n",
1066 udev->devnum, alts->desc.bInterfaceNumber);
1067 return -EINVAL;
1070 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1071 if (unit == NULL)
1072 return -ENOMEM;
1074 memcpy(unit->baSourceID, &buffer[4], 1);
1075 unit->processing.wMaxMultiplier =
1076 get_unaligned_le16(&buffer[5]);
1077 unit->processing.bControlSize = buffer[7];
1078 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1079 memcpy(unit->processing.bmControls, &buffer[8], n);
1080 if (dev->uvc_version >= 0x0110)
1081 unit->processing.bmVideoStandards = buffer[9+n];
1083 if (buffer[8+n] != 0)
1084 usb_string(udev, buffer[8+n], unit->name,
1085 sizeof unit->name);
1086 else
1087 sprintf(unit->name, "Processing %u", buffer[3]);
1089 list_add_tail(&unit->list, &dev->entities);
1090 break;
1092 case UVC_VC_EXTENSION_UNIT:
1093 p = buflen >= 22 ? buffer[21] : 0;
1094 n = buflen >= 24 + p ? buffer[22+p] : 0;
1096 if (buflen < 24 + p + n) {
1097 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1098 "interface %d EXTENSION_UNIT error\n",
1099 udev->devnum, alts->desc.bInterfaceNumber);
1100 return -EINVAL;
1103 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1104 if (unit == NULL)
1105 return -ENOMEM;
1107 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1108 unit->extension.bNumControls = buffer[20];
1109 memcpy(unit->baSourceID, &buffer[22], p);
1110 unit->extension.bControlSize = buffer[22+p];
1111 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1112 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1114 if (buffer[23+p+n] != 0)
1115 usb_string(udev, buffer[23+p+n], unit->name,
1116 sizeof unit->name);
1117 else
1118 sprintf(unit->name, "Extension %u", buffer[3]);
1120 list_add_tail(&unit->list, &dev->entities);
1121 break;
1123 default:
1124 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1125 "descriptor (%u)\n", buffer[2]);
1126 break;
1129 return 0;
1132 static int uvc_parse_control(struct uvc_device *dev)
1134 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1135 unsigned char *buffer = alts->extra;
1136 int buflen = alts->extralen;
1137 int ret;
1139 /* Parse the default alternate setting only, as the UVC specification
1140 * defines a single alternate setting, the default alternate setting
1141 * zero.
1144 while (buflen > 2) {
1145 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1146 buffer[1] != USB_DT_CS_INTERFACE)
1147 goto next_descriptor;
1149 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1150 return ret;
1152 next_descriptor:
1153 buflen -= buffer[0];
1154 buffer += buffer[0];
1157 /* Check if the optional status endpoint is present. Built-in iSight
1158 * webcams have an interrupt endpoint but spit proprietary data that
1159 * don't conform to the UVC status endpoint messages. Don't try to
1160 * handle the interrupt endpoint for those cameras.
1162 if (alts->desc.bNumEndpoints == 1 &&
1163 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1164 struct usb_host_endpoint *ep = &alts->endpoint[0];
1165 struct usb_endpoint_descriptor *desc = &ep->desc;
1167 if (usb_endpoint_is_int_in(desc) &&
1168 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1169 desc->bInterval != 0) {
1170 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1171 "(addr %02x).\n", desc->bEndpointAddress);
1172 dev->int_ep = ep;
1176 return 0;
1179 /* ------------------------------------------------------------------------
1180 * UVC device scan
1184 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1185 * and containing the following units:
1187 * - one or more Output Terminals (USB Streaming or Display)
1188 * - zero or one Processing Unit
1189 * - zero, one or more single-input Selector Units
1190 * - zero or one multiple-input Selector Units, provided all inputs are
1191 * connected to input terminals
1192 * - zero, one or mode single-input Extension Units
1193 * - one or more Input Terminals (Camera, External or USB Streaming)
1195 * The terminal and units must match on of the following structures:
1197 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1198 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1199 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1201 * +---------+ +---------+ -> OTT_*(0)
1202 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1203 * +---------+ +---------+ -> OTT_*(n)
1205 * The Processing Unit and Extension Units can be in any order. Additional
1206 * Extension Units connected to the main chain as single-unit branches are
1207 * also supported. Single-input Selector Units are ignored.
1209 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1210 struct uvc_entity *entity)
1212 switch (UVC_ENTITY_TYPE(entity)) {
1213 case UVC_VC_EXTENSION_UNIT:
1214 if (uvc_trace_param & UVC_TRACE_PROBE)
1215 printk(" <- XU %d", entity->id);
1217 if (entity->bNrInPins != 1) {
1218 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1219 "than 1 input pin.\n", entity->id);
1220 return -1;
1223 break;
1225 case UVC_VC_PROCESSING_UNIT:
1226 if (uvc_trace_param & UVC_TRACE_PROBE)
1227 printk(" <- PU %d", entity->id);
1229 if (chain->processing != NULL) {
1230 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1231 "Processing Units in chain.\n");
1232 return -1;
1235 chain->processing = entity;
1236 break;
1238 case UVC_VC_SELECTOR_UNIT:
1239 if (uvc_trace_param & UVC_TRACE_PROBE)
1240 printk(" <- SU %d", entity->id);
1242 /* Single-input selector units are ignored. */
1243 if (entity->bNrInPins == 1)
1244 break;
1246 if (chain->selector != NULL) {
1247 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1248 "Units in chain.\n");
1249 return -1;
1252 chain->selector = entity;
1253 break;
1255 case UVC_ITT_VENDOR_SPECIFIC:
1256 case UVC_ITT_CAMERA:
1257 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1258 if (uvc_trace_param & UVC_TRACE_PROBE)
1259 printk(" <- IT %d\n", entity->id);
1261 break;
1263 case UVC_OTT_VENDOR_SPECIFIC:
1264 case UVC_OTT_DISPLAY:
1265 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1266 if (uvc_trace_param & UVC_TRACE_PROBE)
1267 printk(" OT %d", entity->id);
1269 break;
1271 case UVC_TT_STREAMING:
1272 if (UVC_ENTITY_IS_ITERM(entity)) {
1273 if (uvc_trace_param & UVC_TRACE_PROBE)
1274 printk(" <- IT %d\n", entity->id);
1275 } else {
1276 if (uvc_trace_param & UVC_TRACE_PROBE)
1277 printk(" OT %d", entity->id);
1280 break;
1282 default:
1283 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1284 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1285 return -1;
1288 list_add_tail(&entity->chain, &chain->entities);
1289 return 0;
1292 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1293 struct uvc_entity *entity, struct uvc_entity *prev)
1295 struct uvc_entity *forward;
1296 int found;
1298 /* Forward scan */
1299 forward = NULL;
1300 found = 0;
1302 while (1) {
1303 forward = uvc_entity_by_reference(chain->dev, entity->id,
1304 forward);
1305 if (forward == NULL)
1306 break;
1307 if (forward == prev)
1308 continue;
1310 switch (UVC_ENTITY_TYPE(forward)) {
1311 case UVC_VC_EXTENSION_UNIT:
1312 if (forward->bNrInPins != 1) {
1313 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1314 "has more than 1 input pin.\n",
1315 entity->id);
1316 return -EINVAL;
1319 list_add_tail(&forward->chain, &chain->entities);
1320 if (uvc_trace_param & UVC_TRACE_PROBE) {
1321 if (!found)
1322 printk(" (->");
1324 printk(" XU %d", forward->id);
1325 found = 1;
1327 break;
1329 case UVC_OTT_VENDOR_SPECIFIC:
1330 case UVC_OTT_DISPLAY:
1331 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1332 case UVC_TT_STREAMING:
1333 if (UVC_ENTITY_IS_ITERM(forward)) {
1334 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1335 "terminal %u.\n", forward->id);
1336 return -EINVAL;
1339 list_add_tail(&forward->chain, &chain->entities);
1340 if (uvc_trace_param & UVC_TRACE_PROBE) {
1341 if (!found)
1342 printk(" (->");
1344 printk(" OT %d", forward->id);
1345 found = 1;
1347 break;
1350 if (found)
1351 printk(")");
1353 return 0;
1356 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1357 struct uvc_entity **_entity)
1359 struct uvc_entity *entity = *_entity;
1360 struct uvc_entity *term;
1361 int id = -EINVAL, i;
1363 switch (UVC_ENTITY_TYPE(entity)) {
1364 case UVC_VC_EXTENSION_UNIT:
1365 case UVC_VC_PROCESSING_UNIT:
1366 id = entity->baSourceID[0];
1367 break;
1369 case UVC_VC_SELECTOR_UNIT:
1370 /* Single-input selector units are ignored. */
1371 if (entity->bNrInPins == 1) {
1372 id = entity->baSourceID[0];
1373 break;
1376 if (uvc_trace_param & UVC_TRACE_PROBE)
1377 printk(" <- IT");
1379 chain->selector = entity;
1380 for (i = 0; i < entity->bNrInPins; ++i) {
1381 id = entity->baSourceID[i];
1382 term = uvc_entity_by_id(chain->dev, id);
1383 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1384 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1385 "input %d isn't connected to an "
1386 "input terminal\n", entity->id, i);
1387 return -1;
1390 if (uvc_trace_param & UVC_TRACE_PROBE)
1391 printk(" %d", term->id);
1393 list_add_tail(&term->chain, &chain->entities);
1394 uvc_scan_chain_forward(chain, term, entity);
1397 if (uvc_trace_param & UVC_TRACE_PROBE)
1398 printk("\n");
1400 id = 0;
1401 break;
1403 case UVC_ITT_VENDOR_SPECIFIC:
1404 case UVC_ITT_CAMERA:
1405 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1406 case UVC_OTT_VENDOR_SPECIFIC:
1407 case UVC_OTT_DISPLAY:
1408 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1409 case UVC_TT_STREAMING:
1410 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1411 break;
1414 if (id <= 0) {
1415 *_entity = NULL;
1416 return id;
1419 entity = uvc_entity_by_id(chain->dev, id);
1420 if (entity == NULL) {
1421 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1422 "unknown entity %d.\n", id);
1423 return -EINVAL;
1426 *_entity = entity;
1427 return 0;
1430 static int uvc_scan_chain(struct uvc_video_chain *chain,
1431 struct uvc_entity *term)
1433 struct uvc_entity *entity, *prev;
1435 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1437 entity = term;
1438 prev = NULL;
1440 while (entity != NULL) {
1441 /* Entity must not be part of an existing chain */
1442 if (entity->chain.next || entity->chain.prev) {
1443 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1444 "entity %d already in chain.\n", entity->id);
1445 return -EINVAL;
1448 /* Process entity */
1449 if (uvc_scan_chain_entity(chain, entity) < 0)
1450 return -EINVAL;
1452 /* Forward scan */
1453 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1454 return -EINVAL;
1456 /* Backward scan */
1457 prev = entity;
1458 if (uvc_scan_chain_backward(chain, &entity) < 0)
1459 return -EINVAL;
1462 return 0;
1465 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1466 char *buffer)
1468 struct uvc_entity *term;
1469 unsigned int nterms = 0;
1470 char *p = buffer;
1472 list_for_each_entry(term, terms, chain) {
1473 if (!UVC_ENTITY_IS_TERM(term) ||
1474 UVC_TERM_DIRECTION(term) != dir)
1475 continue;
1477 if (nterms)
1478 p += sprintf(p, ",");
1479 if (++nterms >= 4) {
1480 p += sprintf(p, "...");
1481 break;
1483 p += sprintf(p, "%u", term->id);
1486 return p - buffer;
1489 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1491 static char buffer[43];
1492 char *p = buffer;
1494 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1495 p += sprintf(p, " -> ");
1496 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1498 return buffer;
1502 * Scan the device for video chains and register video devices.
1504 * Chains are scanned starting at their output terminals and walked backwards.
1506 static int uvc_scan_device(struct uvc_device *dev)
1508 struct uvc_video_chain *chain;
1509 struct uvc_entity *term;
1511 list_for_each_entry(term, &dev->entities, list) {
1512 if (!UVC_ENTITY_IS_OTERM(term))
1513 continue;
1515 /* If the terminal is already included in a chain, skip it.
1516 * This can happen for chains that have multiple output
1517 * terminals, where all output terminals beside the first one
1518 * will be inserted in the chain in forward scans.
1520 if (term->chain.next || term->chain.prev)
1521 continue;
1523 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1524 if (chain == NULL)
1525 return -ENOMEM;
1527 INIT_LIST_HEAD(&chain->entities);
1528 mutex_init(&chain->ctrl_mutex);
1529 chain->dev = dev;
1531 if (uvc_scan_chain(chain, term) < 0) {
1532 kfree(chain);
1533 continue;
1536 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1537 uvc_print_chain(chain));
1539 list_add_tail(&chain->list, &dev->chains);
1542 if (list_empty(&dev->chains)) {
1543 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1544 return -1;
1547 return 0;
1550 /* ------------------------------------------------------------------------
1551 * Video device registration and unregistration
1555 * Delete the UVC device.
1557 * Called by the kernel when the last reference to the uvc_device structure
1558 * is released.
1560 * As this function is called after or during disconnect(), all URBs have
1561 * already been canceled by the USB core. There is no need to kill the
1562 * interrupt URB manually.
1564 static void uvc_delete(struct uvc_device *dev)
1566 struct list_head *p, *n;
1568 usb_put_intf(dev->intf);
1569 usb_put_dev(dev->udev);
1571 uvc_status_cleanup(dev);
1572 uvc_ctrl_cleanup_device(dev);
1574 list_for_each_safe(p, n, &dev->chains) {
1575 struct uvc_video_chain *chain;
1576 chain = list_entry(p, struct uvc_video_chain, list);
1577 kfree(chain);
1580 list_for_each_safe(p, n, &dev->entities) {
1581 struct uvc_entity *entity;
1582 entity = list_entry(p, struct uvc_entity, list);
1583 kfree(entity);
1586 list_for_each_safe(p, n, &dev->streams) {
1587 struct uvc_streaming *streaming;
1588 streaming = list_entry(p, struct uvc_streaming, list);
1589 usb_driver_release_interface(&uvc_driver.driver,
1590 streaming->intf);
1591 usb_put_intf(streaming->intf);
1592 kfree(streaming->format);
1593 kfree(streaming->header.bmaControls);
1594 kfree(streaming);
1597 kfree(dev);
1600 static void uvc_release(struct video_device *vdev)
1602 struct uvc_streaming *stream = video_get_drvdata(vdev);
1603 struct uvc_device *dev = stream->dev;
1605 video_device_release(vdev);
1607 /* Decrement the registered streams count and delete the device when it
1608 * reaches zero.
1610 if (atomic_dec_and_test(&dev->nstreams))
1611 uvc_delete(dev);
1615 * Unregister the video devices.
1617 static void uvc_unregister_video(struct uvc_device *dev)
1619 struct uvc_streaming *stream;
1621 /* Unregistering all video devices might result in uvc_delete() being
1622 * called from inside the loop if there's no open file handle. To avoid
1623 * that, increment the stream count before iterating over the streams
1624 * and decrement it when done.
1626 atomic_inc(&dev->nstreams);
1628 list_for_each_entry(stream, &dev->streams, list) {
1629 if (stream->vdev == NULL)
1630 continue;
1632 video_unregister_device(stream->vdev);
1633 stream->vdev = NULL;
1636 /* Decrement the stream count and call uvc_delete explicitly if there
1637 * are no stream left.
1639 if (atomic_dec_and_test(&dev->nstreams))
1640 uvc_delete(dev);
1643 static int uvc_register_video(struct uvc_device *dev,
1644 struct uvc_streaming *stream)
1646 struct video_device *vdev;
1647 int ret;
1649 /* Initialize the streaming interface with default streaming
1650 * parameters.
1652 ret = uvc_video_init(stream);
1653 if (ret < 0) {
1654 uvc_printk(KERN_ERR, "Failed to initialize the device "
1655 "(%d).\n", ret);
1656 return ret;
1659 /* Register the device with V4L. */
1660 vdev = video_device_alloc();
1661 if (vdev == NULL) {
1662 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1663 ret);
1664 return -ENOMEM;
1667 /* We already hold a reference to dev->udev. The video device will be
1668 * unregistered before the reference is released, so we don't need to
1669 * get another one.
1671 vdev->parent = &dev->intf->dev;
1672 vdev->fops = &uvc_fops;
1673 vdev->release = uvc_release;
1674 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1676 /* Set the driver data before calling video_register_device, otherwise
1677 * uvc_v4l2_open might race us.
1679 stream->vdev = vdev;
1680 video_set_drvdata(vdev, stream);
1682 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1683 if (ret < 0) {
1684 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1685 ret);
1686 stream->vdev = NULL;
1687 video_device_release(vdev);
1688 return ret;
1691 atomic_inc(&dev->nstreams);
1692 return 0;
1696 * Register all video devices in all chains.
1698 static int uvc_register_terms(struct uvc_device *dev,
1699 struct uvc_video_chain *chain)
1701 struct uvc_streaming *stream;
1702 struct uvc_entity *term;
1703 int ret;
1705 list_for_each_entry(term, &chain->entities, chain) {
1706 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1707 continue;
1709 stream = uvc_stream_by_id(dev, term->id);
1710 if (stream == NULL) {
1711 uvc_printk(KERN_INFO, "No streaming interface found "
1712 "for terminal %u.", term->id);
1713 continue;
1716 stream->chain = chain;
1717 ret = uvc_register_video(dev, stream);
1718 if (ret < 0)
1719 return ret;
1722 return 0;
1725 static int uvc_register_chains(struct uvc_device *dev)
1727 struct uvc_video_chain *chain;
1728 int ret;
1730 list_for_each_entry(chain, &dev->chains, list) {
1731 ret = uvc_register_terms(dev, chain);
1732 if (ret < 0)
1733 return ret;
1736 return 0;
1739 /* ------------------------------------------------------------------------
1740 * USB probe, disconnect, suspend and resume
1743 static int uvc_probe(struct usb_interface *intf,
1744 const struct usb_device_id *id)
1746 struct usb_device *udev = interface_to_usbdev(intf);
1747 struct uvc_device *dev;
1748 int ret;
1750 if (id->idVendor && id->idProduct)
1751 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1752 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1753 id->idProduct);
1754 else
1755 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1756 udev->devpath);
1758 /* Allocate memory for the device and initialize it. */
1759 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1760 return -ENOMEM;
1762 INIT_LIST_HEAD(&dev->entities);
1763 INIT_LIST_HEAD(&dev->chains);
1764 INIT_LIST_HEAD(&dev->streams);
1765 atomic_set(&dev->nstreams, 0);
1766 atomic_set(&dev->users, 0);
1768 dev->udev = usb_get_dev(udev);
1769 dev->intf = usb_get_intf(intf);
1770 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1771 dev->quirks = id->driver_info | uvc_quirks_param;
1773 if (udev->product != NULL)
1774 strlcpy(dev->name, udev->product, sizeof dev->name);
1775 else
1776 snprintf(dev->name, sizeof dev->name,
1777 "UVC Camera (%04x:%04x)",
1778 le16_to_cpu(udev->descriptor.idVendor),
1779 le16_to_cpu(udev->descriptor.idProduct));
1781 /* Parse the Video Class control descriptor. */
1782 if (uvc_parse_control(dev) < 0) {
1783 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1784 "descriptors.\n");
1785 goto error;
1788 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1789 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1790 udev->product ? udev->product : "<unnamed>",
1791 le16_to_cpu(udev->descriptor.idVendor),
1792 le16_to_cpu(udev->descriptor.idProduct));
1794 if (uvc_quirks_param != 0) {
1795 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1796 "parameter for testing purpose.\n", uvc_quirks_param);
1797 uvc_printk(KERN_INFO, "Please report required quirks to the "
1798 "linux-uvc-devel mailing list.\n");
1801 /* Initialize controls. */
1802 if (uvc_ctrl_init_device(dev) < 0)
1803 goto error;
1805 /* Scan the device for video chains. */
1806 if (uvc_scan_device(dev) < 0)
1807 goto error;
1809 /* Register video devices. */
1810 if (uvc_register_chains(dev) < 0)
1811 goto error;
1813 /* Save our data pointer in the interface data. */
1814 usb_set_intfdata(intf, dev);
1816 /* Initialize the interrupt URB. */
1817 if ((ret = uvc_status_init(dev)) < 0) {
1818 uvc_printk(KERN_INFO, "Unable to initialize the status "
1819 "endpoint (%d), status interrupt will not be "
1820 "supported.\n", ret);
1823 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1824 return 0;
1826 error:
1827 uvc_unregister_video(dev);
1828 return -ENODEV;
1831 static void uvc_disconnect(struct usb_interface *intf)
1833 struct uvc_device *dev = usb_get_intfdata(intf);
1835 /* Set the USB interface data to NULL. This can be done outside the
1836 * lock, as there's no other reader.
1838 usb_set_intfdata(intf, NULL);
1840 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1841 UVC_SC_VIDEOSTREAMING)
1842 return;
1844 dev->state |= UVC_DEV_DISCONNECTED;
1846 uvc_unregister_video(dev);
1849 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1851 struct uvc_device *dev = usb_get_intfdata(intf);
1852 struct uvc_streaming *stream;
1854 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1855 intf->cur_altsetting->desc.bInterfaceNumber);
1857 /* Controls are cached on the fly so they don't need to be saved. */
1858 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1859 UVC_SC_VIDEOCONTROL)
1860 return uvc_status_suspend(dev);
1862 list_for_each_entry(stream, &dev->streams, list) {
1863 if (stream->intf == intf)
1864 return uvc_video_suspend(stream);
1867 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1868 "mismatch.\n");
1869 return -EINVAL;
1872 static int __uvc_resume(struct usb_interface *intf, int reset)
1874 struct uvc_device *dev = usb_get_intfdata(intf);
1875 struct uvc_streaming *stream;
1877 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1878 intf->cur_altsetting->desc.bInterfaceNumber);
1880 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1881 UVC_SC_VIDEOCONTROL) {
1882 if (reset) {
1883 int ret = uvc_ctrl_resume_device(dev);
1885 if (ret < 0)
1886 return ret;
1889 return uvc_status_resume(dev);
1892 list_for_each_entry(stream, &dev->streams, list) {
1893 if (stream->intf == intf)
1894 return uvc_video_resume(stream);
1897 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1898 "mismatch.\n");
1899 return -EINVAL;
1902 static int uvc_resume(struct usb_interface *intf)
1904 return __uvc_resume(intf, 0);
1907 static int uvc_reset_resume(struct usb_interface *intf)
1909 return __uvc_resume(intf, 1);
1912 /* ------------------------------------------------------------------------
1913 * Driver initialization and cleanup
1917 * The Logitech cameras listed below have their interface class set to
1918 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1919 * though they are compliant.
1921 static struct usb_device_id uvc_ids[] = {
1922 /* Genius eFace 2025 */
1923 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1924 | USB_DEVICE_ID_MATCH_INT_INFO,
1925 .idVendor = 0x0458,
1926 .idProduct = 0x706e,
1927 .bInterfaceClass = USB_CLASS_VIDEO,
1928 .bInterfaceSubClass = 1,
1929 .bInterfaceProtocol = 0,
1930 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1931 /* Microsoft Lifecam NX-6000 */
1932 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1933 | USB_DEVICE_ID_MATCH_INT_INFO,
1934 .idVendor = 0x045e,
1935 .idProduct = 0x00f8,
1936 .bInterfaceClass = USB_CLASS_VIDEO,
1937 .bInterfaceSubClass = 1,
1938 .bInterfaceProtocol = 0,
1939 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1940 /* Microsoft Lifecam VX-7000 */
1941 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1942 | USB_DEVICE_ID_MATCH_INT_INFO,
1943 .idVendor = 0x045e,
1944 .idProduct = 0x0723,
1945 .bInterfaceClass = USB_CLASS_VIDEO,
1946 .bInterfaceSubClass = 1,
1947 .bInterfaceProtocol = 0,
1948 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1949 /* Logitech Quickcam Fusion */
1950 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1951 | USB_DEVICE_ID_MATCH_INT_INFO,
1952 .idVendor = 0x046d,
1953 .idProduct = 0x08c1,
1954 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1955 .bInterfaceSubClass = 1,
1956 .bInterfaceProtocol = 0 },
1957 /* Logitech Quickcam Orbit MP */
1958 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1959 | USB_DEVICE_ID_MATCH_INT_INFO,
1960 .idVendor = 0x046d,
1961 .idProduct = 0x08c2,
1962 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1963 .bInterfaceSubClass = 1,
1964 .bInterfaceProtocol = 0 },
1965 /* Logitech Quickcam Pro for Notebook */
1966 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1967 | USB_DEVICE_ID_MATCH_INT_INFO,
1968 .idVendor = 0x046d,
1969 .idProduct = 0x08c3,
1970 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1971 .bInterfaceSubClass = 1,
1972 .bInterfaceProtocol = 0 },
1973 /* Logitech Quickcam Pro 5000 */
1974 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1975 | USB_DEVICE_ID_MATCH_INT_INFO,
1976 .idVendor = 0x046d,
1977 .idProduct = 0x08c5,
1978 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1979 .bInterfaceSubClass = 1,
1980 .bInterfaceProtocol = 0 },
1981 /* Logitech Quickcam OEM Dell Notebook */
1982 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1983 | USB_DEVICE_ID_MATCH_INT_INFO,
1984 .idVendor = 0x046d,
1985 .idProduct = 0x08c6,
1986 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1987 .bInterfaceSubClass = 1,
1988 .bInterfaceProtocol = 0 },
1989 /* Logitech Quickcam OEM Cisco VT Camera II */
1990 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1991 | USB_DEVICE_ID_MATCH_INT_INFO,
1992 .idVendor = 0x046d,
1993 .idProduct = 0x08c7,
1994 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1995 .bInterfaceSubClass = 1,
1996 .bInterfaceProtocol = 0 },
1997 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1998 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1999 | USB_DEVICE_ID_MATCH_INT_INFO,
2000 .idVendor = 0x058f,
2001 .idProduct = 0x3820,
2002 .bInterfaceClass = USB_CLASS_VIDEO,
2003 .bInterfaceSubClass = 1,
2004 .bInterfaceProtocol = 0,
2005 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2006 /* Apple Built-In iSight */
2007 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2008 | USB_DEVICE_ID_MATCH_INT_INFO,
2009 .idVendor = 0x05ac,
2010 .idProduct = 0x8501,
2011 .bInterfaceClass = USB_CLASS_VIDEO,
2012 .bInterfaceSubClass = 1,
2013 .bInterfaceProtocol = 0,
2014 .driver_info = UVC_QUIRK_PROBE_MINMAX
2015 | UVC_QUIRK_BUILTIN_ISIGHT },
2016 /* Genesys Logic USB 2.0 PC Camera */
2017 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2018 | USB_DEVICE_ID_MATCH_INT_INFO,
2019 .idVendor = 0x05e3,
2020 .idProduct = 0x0505,
2021 .bInterfaceClass = USB_CLASS_VIDEO,
2022 .bInterfaceSubClass = 1,
2023 .bInterfaceProtocol = 0,
2024 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2025 /* ViMicro Vega */
2026 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2027 | USB_DEVICE_ID_MATCH_INT_INFO,
2028 .idVendor = 0x0ac8,
2029 .idProduct = 0x332d,
2030 .bInterfaceClass = USB_CLASS_VIDEO,
2031 .bInterfaceSubClass = 1,
2032 .bInterfaceProtocol = 0,
2033 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2034 /* ViMicro - Minoru3D */
2035 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2036 | USB_DEVICE_ID_MATCH_INT_INFO,
2037 .idVendor = 0x0ac8,
2038 .idProduct = 0x3410,
2039 .bInterfaceClass = USB_CLASS_VIDEO,
2040 .bInterfaceSubClass = 1,
2041 .bInterfaceProtocol = 0,
2042 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2043 /* ViMicro Venus - Minoru3D */
2044 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2045 | USB_DEVICE_ID_MATCH_INT_INFO,
2046 .idVendor = 0x0ac8,
2047 .idProduct = 0x3420,
2048 .bInterfaceClass = USB_CLASS_VIDEO,
2049 .bInterfaceSubClass = 1,
2050 .bInterfaceProtocol = 0,
2051 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2052 /* MT6227 */
2053 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2054 | USB_DEVICE_ID_MATCH_INT_INFO,
2055 .idVendor = 0x0e8d,
2056 .idProduct = 0x0004,
2057 .bInterfaceClass = USB_CLASS_VIDEO,
2058 .bInterfaceSubClass = 1,
2059 .bInterfaceProtocol = 0,
2060 .driver_info = UVC_QUIRK_PROBE_MINMAX
2061 | UVC_QUIRK_PROBE_DEF },
2062 /* Syntek (HP Spartan) */
2063 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2064 | USB_DEVICE_ID_MATCH_INT_INFO,
2065 .idVendor = 0x174f,
2066 .idProduct = 0x5212,
2067 .bInterfaceClass = USB_CLASS_VIDEO,
2068 .bInterfaceSubClass = 1,
2069 .bInterfaceProtocol = 0,
2070 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2071 /* Syntek (Samsung Q310) */
2072 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2073 | USB_DEVICE_ID_MATCH_INT_INFO,
2074 .idVendor = 0x174f,
2075 .idProduct = 0x5931,
2076 .bInterfaceClass = USB_CLASS_VIDEO,
2077 .bInterfaceSubClass = 1,
2078 .bInterfaceProtocol = 0,
2079 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2080 /* Syntek (Packard Bell EasyNote MX52 */
2081 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2082 | USB_DEVICE_ID_MATCH_INT_INFO,
2083 .idVendor = 0x174f,
2084 .idProduct = 0x8a12,
2085 .bInterfaceClass = USB_CLASS_VIDEO,
2086 .bInterfaceSubClass = 1,
2087 .bInterfaceProtocol = 0,
2088 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2089 /* Syntek (Asus F9SG) */
2090 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2091 | USB_DEVICE_ID_MATCH_INT_INFO,
2092 .idVendor = 0x174f,
2093 .idProduct = 0x8a31,
2094 .bInterfaceClass = USB_CLASS_VIDEO,
2095 .bInterfaceSubClass = 1,
2096 .bInterfaceProtocol = 0,
2097 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2098 /* Syntek (Asus U3S) */
2099 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2100 | USB_DEVICE_ID_MATCH_INT_INFO,
2101 .idVendor = 0x174f,
2102 .idProduct = 0x8a33,
2103 .bInterfaceClass = USB_CLASS_VIDEO,
2104 .bInterfaceSubClass = 1,
2105 .bInterfaceProtocol = 0,
2106 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2107 /* Syntek (JAOtech Smart Terminal) */
2108 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2109 | USB_DEVICE_ID_MATCH_INT_INFO,
2110 .idVendor = 0x174f,
2111 .idProduct = 0x8a34,
2112 .bInterfaceClass = USB_CLASS_VIDEO,
2113 .bInterfaceSubClass = 1,
2114 .bInterfaceProtocol = 0,
2115 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2116 /* Lenovo Thinkpad SL400/SL500 */
2117 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2118 | USB_DEVICE_ID_MATCH_INT_INFO,
2119 .idVendor = 0x17ef,
2120 .idProduct = 0x480b,
2121 .bInterfaceClass = USB_CLASS_VIDEO,
2122 .bInterfaceSubClass = 1,
2123 .bInterfaceProtocol = 0,
2124 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2125 /* Aveo Technology USB 2.0 Camera */
2126 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2127 | USB_DEVICE_ID_MATCH_INT_INFO,
2128 .idVendor = 0x1871,
2129 .idProduct = 0x0306,
2130 .bInterfaceClass = USB_CLASS_VIDEO,
2131 .bInterfaceSubClass = 1,
2132 .bInterfaceProtocol = 0,
2133 .driver_info = UVC_QUIRK_PROBE_MINMAX
2134 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2135 /* Ecamm Pico iMage */
2136 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2137 | USB_DEVICE_ID_MATCH_INT_INFO,
2138 .idVendor = 0x18cd,
2139 .idProduct = 0xcafe,
2140 .bInterfaceClass = USB_CLASS_VIDEO,
2141 .bInterfaceSubClass = 1,
2142 .bInterfaceProtocol = 0,
2143 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2144 /* FSC WebCam V30S */
2145 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2146 | USB_DEVICE_ID_MATCH_INT_INFO,
2147 .idVendor = 0x18ec,
2148 .idProduct = 0x3288,
2149 .bInterfaceClass = USB_CLASS_VIDEO,
2150 .bInterfaceSubClass = 1,
2151 .bInterfaceProtocol = 0,
2152 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2153 /* Arkmicro unbranded */
2154 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2155 | USB_DEVICE_ID_MATCH_INT_INFO,
2156 .idVendor = 0x18ec,
2157 .idProduct = 0x3290,
2158 .bInterfaceClass = USB_CLASS_VIDEO,
2159 .bInterfaceSubClass = 1,
2160 .bInterfaceProtocol = 0,
2161 .driver_info = UVC_QUIRK_PROBE_DEF },
2162 /* Bodelin ProScopeHR */
2163 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2164 | USB_DEVICE_ID_MATCH_DEV_HI
2165 | USB_DEVICE_ID_MATCH_INT_INFO,
2166 .idVendor = 0x19ab,
2167 .idProduct = 0x1000,
2168 .bcdDevice_hi = 0x0126,
2169 .bInterfaceClass = USB_CLASS_VIDEO,
2170 .bInterfaceSubClass = 1,
2171 .bInterfaceProtocol = 0,
2172 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2173 /* MSI StarCam 370i */
2174 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2175 | USB_DEVICE_ID_MATCH_INT_INFO,
2176 .idVendor = 0x1b3b,
2177 .idProduct = 0x2951,
2178 .bInterfaceClass = USB_CLASS_VIDEO,
2179 .bInterfaceSubClass = 1,
2180 .bInterfaceProtocol = 0,
2181 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2182 /* SiGma Micro USB Web Camera */
2183 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2184 | USB_DEVICE_ID_MATCH_INT_INFO,
2185 .idVendor = 0x1c4f,
2186 .idProduct = 0x3000,
2187 .bInterfaceClass = USB_CLASS_VIDEO,
2188 .bInterfaceSubClass = 1,
2189 .bInterfaceProtocol = 0,
2190 .driver_info = UVC_QUIRK_PROBE_MINMAX
2191 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2192 /* Generic USB Video Class */
2193 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2197 MODULE_DEVICE_TABLE(usb, uvc_ids);
2199 struct uvc_driver uvc_driver = {
2200 .driver = {
2201 .name = "uvcvideo",
2202 .probe = uvc_probe,
2203 .disconnect = uvc_disconnect,
2204 .suspend = uvc_suspend,
2205 .resume = uvc_resume,
2206 .reset_resume = uvc_reset_resume,
2207 .id_table = uvc_ids,
2208 .supports_autosuspend = 1,
2212 static int __init uvc_init(void)
2214 int result;
2216 INIT_LIST_HEAD(&uvc_driver.devices);
2217 INIT_LIST_HEAD(&uvc_driver.controls);
2218 mutex_init(&uvc_driver.ctrl_mutex);
2220 uvc_ctrl_init();
2222 result = usb_register(&uvc_driver.driver);
2223 if (result == 0)
2224 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2225 return result;
2228 static void __exit uvc_cleanup(void)
2230 usb_deregister(&uvc_driver.driver);
2233 module_init(uvc_init);
2234 module_exit(uvc_cleanup);
2236 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2237 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2238 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2239 MODULE_PARM_DESC(quirks, "Forced device quirks");
2240 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2241 MODULE_PARM_DESC(trace, "Trace level bitmask");
2242 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2243 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2245 MODULE_AUTHOR(DRIVER_AUTHOR);
2246 MODULE_DESCRIPTION(DRIVER_DESC);
2247 MODULE_LICENSE("GPL");
2248 MODULE_VERSION(DRIVER_VERSION);