RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / media / video / uvc / uvc_driver.c
blobdd3cb3d600a20c4b70d4106a2ad7aa64d7dc6f71
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/slab.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
35 #include <asm/unaligned.h>
37 #include <media/v4l2-common.h>
39 #include "uvcvideo.h"
41 #define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
42 #define DRIVER_DESC "USB Video Class driver"
43 #ifndef DRIVER_VERSION
44 #define DRIVER_VERSION "v0.1.0"
45 #endif
47 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
48 unsigned int uvc_no_drop_param;
49 static unsigned int uvc_quirks_param = -1;
50 unsigned int uvc_trace_param;
51 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
53 /* ------------------------------------------------------------------------
54 * Video formats
57 static struct uvc_format_desc uvc_fmts[] = {
59 .name = "YUV 4:2:2 (YUYV)",
60 .guid = UVC_GUID_FORMAT_YUY2,
61 .fcc = V4L2_PIX_FMT_YUYV,
64 .name = "YUV 4:2:2 (YUYV)",
65 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
66 .fcc = V4L2_PIX_FMT_YUYV,
69 .name = "YUV 4:2:0 (NV12)",
70 .guid = UVC_GUID_FORMAT_NV12,
71 .fcc = V4L2_PIX_FMT_NV12,
74 .name = "MJPEG",
75 .guid = UVC_GUID_FORMAT_MJPEG,
76 .fcc = V4L2_PIX_FMT_MJPEG,
79 .name = "YVU 4:2:0 (YV12)",
80 .guid = UVC_GUID_FORMAT_YV12,
81 .fcc = V4L2_PIX_FMT_YVU420,
84 .name = "YUV 4:2:0 (I420)",
85 .guid = UVC_GUID_FORMAT_I420,
86 .fcc = V4L2_PIX_FMT_YUV420,
89 .name = "YUV 4:2:2 (UYVY)",
90 .guid = UVC_GUID_FORMAT_UYVY,
91 .fcc = V4L2_PIX_FMT_UYVY,
94 .name = "Greyscale (8-bit)",
95 .guid = UVC_GUID_FORMAT_Y800,
96 .fcc = V4L2_PIX_FMT_GREY,
99 .name = "Greyscale (16-bit)",
100 .guid = UVC_GUID_FORMAT_Y16,
101 .fcc = V4L2_PIX_FMT_Y16,
104 .name = "RGB Bayer",
105 .guid = UVC_GUID_FORMAT_BY8,
106 .fcc = V4L2_PIX_FMT_SBGGR8,
109 .name = "RGB565",
110 .guid = UVC_GUID_FORMAT_RGBP,
111 .fcc = V4L2_PIX_FMT_RGB565,
115 /* ------------------------------------------------------------------------
116 * Utility functions
119 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
120 __u8 epaddr)
122 struct usb_host_endpoint *ep;
123 unsigned int i;
125 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
126 ep = &alts->endpoint[i];
127 if (ep->desc.bEndpointAddress == epaddr)
128 return ep;
131 return NULL;
134 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
136 unsigned int len = ARRAY_SIZE(uvc_fmts);
137 unsigned int i;
139 for (i = 0; i < len; ++i) {
140 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
141 return &uvc_fmts[i];
144 return NULL;
147 static __u32 uvc_colorspace(const __u8 primaries)
149 static const __u8 colorprimaries[] = {
151 V4L2_COLORSPACE_SRGB,
152 V4L2_COLORSPACE_470_SYSTEM_M,
153 V4L2_COLORSPACE_470_SYSTEM_BG,
154 V4L2_COLORSPACE_SMPTE170M,
155 V4L2_COLORSPACE_SMPTE240M,
158 if (primaries < ARRAY_SIZE(colorprimaries))
159 return colorprimaries[primaries];
161 return 0;
164 /* Simplify a fraction using a simple continued fraction decomposition. The
165 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
166 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
167 * arbitrary parameters to remove non-significative terms from the simple
168 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
169 * respectively seems to give nice results.
171 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
172 unsigned int n_terms, unsigned int threshold)
174 uint32_t *an;
175 uint32_t x, y, r;
176 unsigned int i, n;
178 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
179 if (an == NULL)
180 return;
182 /* Convert the fraction to a simple continued fraction. See
183 * http://mathforum.org/dr.math/faq/faq.fractions.html
184 * Stop if the current term is bigger than or equal to the given
185 * threshold.
187 x = *numerator;
188 y = *denominator;
190 for (n = 0; n < n_terms && y != 0; ++n) {
191 an[n] = x / y;
192 if (an[n] >= threshold) {
193 if (n < 2)
194 n++;
195 break;
198 r = x - an[n] * y;
199 x = y;
200 y = r;
203 /* Expand the simple continued fraction back to an integer fraction. */
204 x = 0;
205 y = 1;
207 for (i = n; i > 0; --i) {
208 r = y;
209 y = an[i-1] * y + x;
210 x = r;
213 *numerator = y;
214 *denominator = x;
215 kfree(an);
218 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
219 * to compute numerator / denominator * 10000000 using 32 bit fixed point
220 * arithmetic only.
222 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
224 uint32_t multiplier;
226 /* Saturate the result if the operation would overflow. */
227 if (denominator == 0 ||
228 numerator/denominator >= ((uint32_t)-1)/10000000)
229 return (uint32_t)-1;
231 /* Divide both the denominator and the multiplier by two until
232 * numerator * multiplier doesn't overflow. If anyone knows a better
233 * algorithm please let me know.
235 multiplier = 10000000;
236 while (numerator > ((uint32_t)-1)/multiplier) {
237 multiplier /= 2;
238 denominator /= 2;
241 return denominator ? numerator * multiplier / denominator : 0;
244 /* ------------------------------------------------------------------------
245 * Terminal and unit management
248 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
250 struct uvc_entity *entity;
252 list_for_each_entry(entity, &dev->entities, list) {
253 if (entity->id == id)
254 return entity;
257 return NULL;
260 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
261 int id, struct uvc_entity *entity)
263 unsigned int i;
265 if (entity == NULL)
266 entity = list_entry(&dev->entities, struct uvc_entity, list);
268 list_for_each_entry_continue(entity, &dev->entities, list) {
269 for (i = 0; i < entity->bNrInPins; ++i)
270 if (entity->baSourceID[i] == id)
271 return entity;
274 return NULL;
277 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
279 struct uvc_streaming *stream;
281 list_for_each_entry(stream, &dev->streams, list) {
282 if (stream->header.bTerminalLink == id)
283 return stream;
286 return NULL;
289 /* ------------------------------------------------------------------------
290 * Descriptors parsing
293 static int uvc_parse_format(struct uvc_device *dev,
294 struct uvc_streaming *streaming, struct uvc_format *format,
295 __u32 **intervals, unsigned char *buffer, int buflen)
297 struct usb_interface *intf = streaming->intf;
298 struct usb_host_interface *alts = intf->cur_altsetting;
299 struct uvc_format_desc *fmtdesc;
300 struct uvc_frame *frame;
301 const unsigned char *start = buffer;
302 unsigned int interval;
303 unsigned int i, n;
304 __u8 ftype;
306 format->type = buffer[2];
307 format->index = buffer[3];
309 switch (buffer[2]) {
310 case UVC_VS_FORMAT_UNCOMPRESSED:
311 case UVC_VS_FORMAT_FRAME_BASED:
312 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
313 if (buflen < n) {
314 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
315 "interface %d FORMAT error\n",
316 dev->udev->devnum,
317 alts->desc.bInterfaceNumber);
318 return -EINVAL;
321 /* Find the format descriptor from its GUID. */
322 fmtdesc = uvc_format_by_guid(&buffer[5]);
324 if (fmtdesc != NULL) {
325 strlcpy(format->name, fmtdesc->name,
326 sizeof format->name);
327 format->fcc = fmtdesc->fcc;
328 } else {
329 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
330 &buffer[5]);
331 snprintf(format->name, sizeof(format->name), "%pUl\n",
332 &buffer[5]);
333 format->fcc = 0;
336 format->bpp = buffer[21];
337 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
338 ftype = UVC_VS_FRAME_UNCOMPRESSED;
339 } else {
340 ftype = UVC_VS_FRAME_FRAME_BASED;
341 if (buffer[27])
342 format->flags = UVC_FMT_FLAG_COMPRESSED;
344 break;
346 case UVC_VS_FORMAT_MJPEG:
347 if (buflen < 11) {
348 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
349 "interface %d FORMAT error\n",
350 dev->udev->devnum,
351 alts->desc.bInterfaceNumber);
352 return -EINVAL;
355 strlcpy(format->name, "MJPEG", sizeof format->name);
356 format->fcc = V4L2_PIX_FMT_MJPEG;
357 format->flags = UVC_FMT_FLAG_COMPRESSED;
358 format->bpp = 0;
359 ftype = UVC_VS_FRAME_MJPEG;
360 break;
362 case UVC_VS_FORMAT_DV:
363 if (buflen < 9) {
364 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
365 "interface %d FORMAT error\n",
366 dev->udev->devnum,
367 alts->desc.bInterfaceNumber);
368 return -EINVAL;
371 switch (buffer[8] & 0x7f) {
372 case 0:
373 strlcpy(format->name, "SD-DV", sizeof format->name);
374 break;
375 case 1:
376 strlcpy(format->name, "SDL-DV", sizeof format->name);
377 break;
378 case 2:
379 strlcpy(format->name, "HD-DV", sizeof format->name);
380 break;
381 default:
382 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
383 "interface %d: unknown DV format %u\n",
384 dev->udev->devnum,
385 alts->desc.bInterfaceNumber, buffer[8]);
386 return -EINVAL;
389 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
390 sizeof format->name);
392 format->fcc = V4L2_PIX_FMT_DV;
393 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
394 format->bpp = 0;
395 ftype = 0;
397 /* Create a dummy frame descriptor. */
398 frame = &format->frame[0];
399 memset(&format->frame[0], 0, sizeof format->frame[0]);
400 frame->bFrameIntervalType = 1;
401 frame->dwDefaultFrameInterval = 1;
402 frame->dwFrameInterval = *intervals;
403 *(*intervals)++ = 1;
404 format->nframes = 1;
405 break;
407 case UVC_VS_FORMAT_MPEG2TS:
408 case UVC_VS_FORMAT_STREAM_BASED:
409 /* Not supported yet. */
410 default:
411 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
412 "interface %d unsupported format %u\n",
413 dev->udev->devnum, alts->desc.bInterfaceNumber,
414 buffer[2]);
415 return -EINVAL;
418 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
420 buflen -= buffer[0];
421 buffer += buffer[0];
423 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
424 * based formats have frame descriptors.
426 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
427 buffer[2] == ftype) {
428 frame = &format->frame[format->nframes];
429 if (ftype != UVC_VS_FRAME_FRAME_BASED)
430 n = buflen > 25 ? buffer[25] : 0;
431 else
432 n = buflen > 21 ? buffer[21] : 0;
434 n = n ? n : 3;
436 if (buflen < 26 + 4*n) {
437 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
438 "interface %d FRAME error\n", dev->udev->devnum,
439 alts->desc.bInterfaceNumber);
440 return -EINVAL;
443 frame->bFrameIndex = buffer[3];
444 frame->bmCapabilities = buffer[4];
445 frame->wWidth = get_unaligned((u16 *)&(buffer[5]));
446 frame->wHeight = get_unaligned((u16 *)&(buffer[7]));
447 frame->dwMinBitRate = get_unaligned((u32 *)&(buffer[9]));
448 frame->dwMaxBitRate = get_unaligned((u32 *)&(buffer[13]));
449 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
450 frame->dwMaxVideoFrameBufferSize =
451 get_unaligned((u32 *)&(buffer[17]));
452 frame->dwDefaultFrameInterval =
453 get_unaligned((u32 *)&(buffer[21]));
454 frame->bFrameIntervalType = buffer[25];
455 } else {
456 frame->dwMaxVideoFrameBufferSize = 0;
457 frame->dwDefaultFrameInterval =
458 get_unaligned((u32 *)&(buffer[17]));
459 frame->bFrameIntervalType = buffer[21];
461 frame->dwFrameInterval = *intervals;
463 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
464 * completely. Observed behaviours range from setting the
465 * value to 1.1x the actual frame size to hardwiring the
466 * 16 low bits to 0. This results in a higher than necessary
467 * memory usage as well as a wrong image size information. For
468 * uncompressed formats this can be fixed by computing the
469 * value from the frame size.
471 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
472 frame->dwMaxVideoFrameBufferSize = format->bpp
473 * frame->wWidth * frame->wHeight / 8;
475 /* Some bogus devices report dwMinFrameInterval equal to
476 * dwMaxFrameInterval and have dwFrameIntervalStep set to
477 * zero. Setting all null intervals to 1 fixes the problem and
478 * some other divisions by zero that could happen.
480 for (i = 0; i < n; ++i) {
481 interval = get_unaligned((u32 *)&(buffer[26+4*i]));
482 *(*intervals)++ = interval ? interval : 1;
485 /* Make sure that the default frame interval stays between
486 * the boundaries.
488 n -= frame->bFrameIntervalType ? 1 : 2;
489 frame->dwDefaultFrameInterval =
490 min(frame->dwFrameInterval[n],
491 max(frame->dwFrameInterval[0],
492 frame->dwDefaultFrameInterval));
494 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
495 frame->wWidth, frame->wHeight,
496 10000000/frame->dwDefaultFrameInterval,
497 (100000000/frame->dwDefaultFrameInterval)%10);
499 format->nframes++;
500 buflen -= buffer[0];
501 buffer += buffer[0];
504 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
505 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
506 buflen -= buffer[0];
507 buffer += buffer[0];
510 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
511 buffer[2] == UVC_VS_COLORFORMAT) {
512 if (buflen < 6) {
513 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
514 "interface %d COLORFORMAT error\n",
515 dev->udev->devnum,
516 alts->desc.bInterfaceNumber);
517 return -EINVAL;
520 format->colorspace = uvc_colorspace(buffer[3]);
522 buflen -= buffer[0];
523 buffer += buffer[0];
526 return buffer - start;
529 static int uvc_parse_streaming(struct uvc_device *dev,
530 struct usb_interface *intf)
532 struct uvc_streaming *streaming = NULL;
533 struct uvc_format *format;
534 struct uvc_frame *frame;
535 struct usb_host_interface *alts = &intf->altsetting[0];
536 unsigned char *_buffer, *buffer = alts->extra;
537 int _buflen, buflen = alts->extralen;
538 unsigned int nformats = 0, nframes = 0, nintervals = 0;
539 unsigned int size, i, n, p;
540 __u32 *interval;
541 __u16 psize;
542 int ret = -EINVAL;
544 if (intf->cur_altsetting->desc.bInterfaceSubClass
545 != UVC_SC_VIDEOSTREAMING) {
546 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
547 "video streaming interface\n", dev->udev->devnum,
548 intf->altsetting[0].desc.bInterfaceNumber);
549 return -EINVAL;
552 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
553 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
554 "claimed\n", dev->udev->devnum,
555 intf->altsetting[0].desc.bInterfaceNumber);
556 return -EINVAL;
559 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
560 if (streaming == NULL) {
561 usb_driver_release_interface(&uvc_driver.driver, intf);
562 return -EINVAL;
565 mutex_init(&streaming->mutex);
566 streaming->dev = dev;
567 streaming->intf = usb_get_intf(intf);
568 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
570 /* The Pico iMage webcam has its class-specific interface descriptors
571 * after the endpoint descriptors.
573 if (buflen == 0) {
574 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
575 struct usb_host_endpoint *ep = &alts->endpoint[i];
577 if (ep->extralen == 0)
578 continue;
580 if (ep->extralen > 2 &&
581 ep->extra[1] == USB_DT_CS_INTERFACE) {
582 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
583 "from endpoint %u.\n", i);
584 buffer = alts->endpoint[i].extra;
585 buflen = alts->endpoint[i].extralen;
586 break;
591 /* Skip the standard interface descriptors. */
592 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
593 buflen -= buffer[0];
594 buffer += buffer[0];
597 if (buflen <= 2) {
598 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
599 "interface descriptors found.\n");
600 goto error;
603 /* Parse the header descriptor. */
604 switch (buffer[2]) {
605 case UVC_VS_OUTPUT_HEADER:
606 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
607 size = 9;
608 break;
610 case UVC_VS_INPUT_HEADER:
611 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
612 size = 13;
613 break;
615 default:
616 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
617 "%d HEADER descriptor not found.\n", dev->udev->devnum,
618 alts->desc.bInterfaceNumber);
619 goto error;
622 p = buflen >= 4 ? buffer[3] : 0;
623 n = buflen >= size ? buffer[size-1] : 0;
625 if (buflen < size + p*n) {
626 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
627 "interface %d HEADER descriptor is invalid.\n",
628 dev->udev->devnum, alts->desc.bInterfaceNumber);
629 goto error;
632 streaming->header.bNumFormats = p;
633 streaming->header.bEndpointAddress = buffer[6];
634 if (buffer[2] == UVC_VS_INPUT_HEADER) {
635 streaming->header.bmInfo = buffer[7];
636 streaming->header.bTerminalLink = buffer[8];
637 streaming->header.bStillCaptureMethod = buffer[9];
638 streaming->header.bTriggerSupport = buffer[10];
639 streaming->header.bTriggerUsage = buffer[11];
640 } else {
641 streaming->header.bTerminalLink = buffer[7];
643 streaming->header.bControlSize = n;
645 streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
646 GFP_KERNEL);
647 if (streaming->header.bmaControls == NULL) {
648 ret = -ENOMEM;
649 goto error;
652 buflen -= buffer[0];
653 buffer += buffer[0];
655 _buffer = buffer;
656 _buflen = buflen;
658 /* Count the format and frame descriptors. */
659 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
660 switch (_buffer[2]) {
661 case UVC_VS_FORMAT_UNCOMPRESSED:
662 case UVC_VS_FORMAT_MJPEG:
663 case UVC_VS_FORMAT_FRAME_BASED:
664 nformats++;
665 break;
667 case UVC_VS_FORMAT_DV:
668 /* DV format has no frame descriptor. We will create a
669 * dummy frame descriptor with a dummy frame interval.
671 nformats++;
672 nframes++;
673 nintervals++;
674 break;
676 case UVC_VS_FORMAT_MPEG2TS:
677 case UVC_VS_FORMAT_STREAM_BASED:
678 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
679 "interface %d FORMAT %u is not supported.\n",
680 dev->udev->devnum,
681 alts->desc.bInterfaceNumber, _buffer[2]);
682 break;
684 case UVC_VS_FRAME_UNCOMPRESSED:
685 case UVC_VS_FRAME_MJPEG:
686 nframes++;
687 if (_buflen > 25)
688 nintervals += _buffer[25] ? _buffer[25] : 3;
689 break;
691 case UVC_VS_FRAME_FRAME_BASED:
692 nframes++;
693 if (_buflen > 21)
694 nintervals += _buffer[21] ? _buffer[21] : 3;
695 break;
698 _buflen -= _buffer[0];
699 _buffer += _buffer[0];
702 if (nformats == 0) {
703 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
704 "%d has no supported formats defined.\n",
705 dev->udev->devnum, alts->desc.bInterfaceNumber);
706 goto error;
709 size = nformats * sizeof *format + nframes * sizeof *frame
710 + nintervals * sizeof *interval;
711 format = kzalloc(size, GFP_KERNEL);
712 if (format == NULL) {
713 ret = -ENOMEM;
714 goto error;
717 frame = (struct uvc_frame *)&format[nformats];
718 interval = (__u32 *)&frame[nframes];
720 streaming->format = format;
721 streaming->nformats = nformats;
723 /* Parse the format descriptors. */
724 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
725 switch (buffer[2]) {
726 case UVC_VS_FORMAT_UNCOMPRESSED:
727 case UVC_VS_FORMAT_MJPEG:
728 case UVC_VS_FORMAT_DV:
729 case UVC_VS_FORMAT_FRAME_BASED:
730 format->frame = frame;
731 ret = uvc_parse_format(dev, streaming, format,
732 &interval, buffer, buflen);
733 if (ret < 0)
734 goto error;
736 frame += format->nframes;
737 format++;
739 buflen -= ret;
740 buffer += ret;
741 continue;
743 default:
744 break;
747 buflen -= buffer[0];
748 buffer += buffer[0];
751 if (buflen)
752 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
753 "%d has %u bytes of trailing descriptor garbage.\n",
754 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
756 /* Parse the alternate settings to find the maximum bandwidth. */
757 for (i = 0; i < intf->num_altsetting; ++i) {
758 struct usb_host_endpoint *ep;
759 alts = &intf->altsetting[i];
760 ep = uvc_find_endpoint(alts,
761 streaming->header.bEndpointAddress);
762 if (ep == NULL)
763 continue;
765 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
766 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
767 if (psize > streaming->maxpsize)
768 streaming->maxpsize = psize;
771 list_add_tail(&streaming->list, &dev->streams);
772 return 0;
774 error:
775 usb_driver_release_interface(&uvc_driver.driver, intf);
776 usb_put_intf(intf);
777 kfree(streaming->format);
778 kfree(streaming->header.bmaControls);
779 kfree(streaming);
780 return ret;
783 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
784 unsigned int num_pads, unsigned int extra_size)
786 struct uvc_entity *entity;
787 unsigned int num_inputs;
788 unsigned int size;
790 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
791 size = sizeof(*entity) + extra_size + num_inputs;
792 entity = kzalloc(size, GFP_KERNEL);
793 if (entity == NULL)
794 return NULL;
796 entity->id = id;
797 entity->type = type;
799 entity->bNrInPins = num_inputs;
800 entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
802 return entity;
805 /* Parse vendor-specific extensions. */
806 static int uvc_parse_vendor_control(struct uvc_device *dev,
807 const unsigned char *buffer, int buflen)
809 struct usb_device *udev = dev->udev;
810 struct usb_host_interface *alts = dev->intf->cur_altsetting;
811 struct uvc_entity *unit;
812 unsigned int n, p;
813 int handled = 0;
815 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
816 case 0x046d: /* Logitech */
817 if (buffer[1] != 0x41 || buffer[2] != 0x01)
818 break;
820 /* Logitech implements several vendor specific functions
821 * through vendor specific extension units (LXU).
823 * The LXU descriptors are similar to XU descriptors
824 * (see "USB Device Video Class for Video Devices", section
825 * 3.7.2.6 "Extension Unit Descriptor") with the following
826 * differences:
828 * ----------------------------------------------------------
829 * 0 bLength 1 Number
830 * Size of this descriptor, in bytes: 24+p+n*2
831 * ----------------------------------------------------------
832 * 23+p+n bmControlsType N Bitmap
833 * Individual bits in the set are defined:
834 * 0: Absolute
835 * 1: Relative
837 * This bitset is mapped exactly the same as bmControls.
838 * ----------------------------------------------------------
839 * 23+p+n*2 bReserved 1 Boolean
840 * ----------------------------------------------------------
841 * 24+p+n*2 iExtension 1 Index
842 * Index of a string descriptor that describes this
843 * extension unit.
844 * ----------------------------------------------------------
846 p = buflen >= 22 ? buffer[21] : 0;
847 n = buflen >= 25 + p ? buffer[22+p] : 0;
849 if (buflen < 25 + p + 2*n) {
850 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
851 "interface %d EXTENSION_UNIT error\n",
852 udev->devnum, alts->desc.bInterfaceNumber);
853 break;
856 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
857 p + 1, 2*n);
858 if (unit == NULL)
859 return -ENOMEM;
861 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
862 unit->extension.bNumControls = buffer[20];
863 memcpy(unit->baSourceID, &buffer[22], p);
864 unit->extension.bControlSize = buffer[22+p];
865 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
866 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
867 + n;
868 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
870 if (buffer[24+p+2*n] != 0)
871 usb_string(udev, buffer[24+p+2*n], unit->name,
872 sizeof unit->name);
873 else
874 sprintf(unit->name, "Extension %u", buffer[3]);
876 list_add_tail(&unit->list, &dev->entities);
877 handled = 1;
878 break;
881 return handled;
884 static int uvc_parse_standard_control(struct uvc_device *dev,
885 const unsigned char *buffer, int buflen)
887 struct usb_device *udev = dev->udev;
888 struct uvc_entity *unit, *term;
889 struct usb_interface *intf;
890 struct usb_host_interface *alts = dev->intf->cur_altsetting;
891 unsigned int i, n, p, len;
892 __u16 type;
894 switch (buffer[2]) {
895 case UVC_VC_HEADER:
896 n = buflen >= 12 ? buffer[11] : 0;
898 if (buflen < 12 || buflen < 12 + n) {
899 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
900 "interface %d HEADER error\n", udev->devnum,
901 alts->desc.bInterfaceNumber);
902 return -EINVAL;
905 dev->uvc_version = get_unaligned((u16 *)&(buffer[3]));
906 dev->clock_frequency = get_unaligned((u32 *)&(buffer[7]));
908 /* Parse all USB Video Streaming interfaces. */
909 for (i = 0; i < n; ++i) {
910 intf = usb_ifnum_to_if(udev, buffer[12+i]);
911 if (intf == NULL) {
912 uvc_trace(UVC_TRACE_DESCR, "device %d "
913 "interface %d doesn't exists\n",
914 udev->devnum, i);
915 continue;
918 uvc_parse_streaming(dev, intf);
920 break;
922 case UVC_VC_INPUT_TERMINAL:
923 if (buflen < 8) {
924 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
925 "interface %d INPUT_TERMINAL error\n",
926 udev->devnum, alts->desc.bInterfaceNumber);
927 return -EINVAL;
930 /* Make sure the terminal type MSB is not null, otherwise it
931 * could be confused with a unit.
933 type = get_unaligned((u16 *)&(buffer[4]));
934 if ((type & 0xff00) == 0) {
935 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
936 "interface %d INPUT_TERMINAL %d has invalid "
937 "type 0x%04x, skipping\n", udev->devnum,
938 alts->desc.bInterfaceNumber,
939 buffer[3], type);
940 return 0;
943 n = 0;
944 p = 0;
945 len = 8;
947 if (type == UVC_ITT_CAMERA) {
948 n = buflen >= 15 ? buffer[14] : 0;
949 len = 15;
951 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
952 n = buflen >= 9 ? buffer[8] : 0;
953 p = buflen >= 10 + n ? buffer[9+n] : 0;
954 len = 10;
957 if (buflen < len + n + p) {
958 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
959 "interface %d INPUT_TERMINAL error\n",
960 udev->devnum, alts->desc.bInterfaceNumber);
961 return -EINVAL;
964 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
965 1, n + p);
966 if (term == NULL)
967 return -ENOMEM;
969 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
970 term->camera.bControlSize = n;
971 term->camera.bmControls = (__u8 *)term + sizeof *term;
972 term->camera.wObjectiveFocalLengthMin =
973 get_unaligned((u16 *)&(buffer[8]));
974 term->camera.wObjectiveFocalLengthMax =
975 get_unaligned((u16 *)&(buffer[10]));
976 term->camera.wOcularFocalLength =
977 get_unaligned((u16 *)&(buffer[12]));
978 memcpy(term->camera.bmControls, &buffer[15], n);
979 } else if (UVC_ENTITY_TYPE(term) ==
980 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
981 term->media.bControlSize = n;
982 term->media.bmControls = (__u8 *)term + sizeof *term;
983 term->media.bTransportModeSize = p;
984 term->media.bmTransportModes = (__u8 *)term
985 + sizeof *term + n;
986 memcpy(term->media.bmControls, &buffer[9], n);
987 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
990 if (buffer[7] != 0)
991 usb_string(udev, buffer[7], term->name,
992 sizeof term->name);
993 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
994 sprintf(term->name, "Camera %u", buffer[3]);
995 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
996 sprintf(term->name, "Media %u", buffer[3]);
997 else
998 sprintf(term->name, "Input %u", buffer[3]);
1000 list_add_tail(&term->list, &dev->entities);
1001 break;
1003 case UVC_VC_OUTPUT_TERMINAL:
1004 if (buflen < 9) {
1005 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1006 "interface %d OUTPUT_TERMINAL error\n",
1007 udev->devnum, alts->desc.bInterfaceNumber);
1008 return -EINVAL;
1011 /* Make sure the terminal type MSB is not null, otherwise it
1012 * could be confused with a unit.
1014 type = get_unaligned((u16 *)&(buffer[4]));
1015 if ((type & 0xff00) == 0) {
1016 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1017 "interface %d OUTPUT_TERMINAL %d has invalid "
1018 "type 0x%04x, skipping\n", udev->devnum,
1019 alts->desc.bInterfaceNumber, buffer[3], type);
1020 return 0;
1023 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1024 1, 0);
1025 if (term == NULL)
1026 return -ENOMEM;
1028 memcpy(term->baSourceID, &buffer[7], 1);
1030 if (buffer[8] != 0)
1031 usb_string(udev, buffer[8], term->name,
1032 sizeof term->name);
1033 else
1034 sprintf(term->name, "Output %u", buffer[3]);
1036 list_add_tail(&term->list, &dev->entities);
1037 break;
1039 case UVC_VC_SELECTOR_UNIT:
1040 p = buflen >= 5 ? buffer[4] : 0;
1042 if (buflen < 5 || buflen < 6 + p) {
1043 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1044 "interface %d SELECTOR_UNIT error\n",
1045 udev->devnum, alts->desc.bInterfaceNumber);
1046 return -EINVAL;
1049 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1050 if (unit == NULL)
1051 return -ENOMEM;
1053 memcpy(unit->baSourceID, &buffer[5], p);
1055 if (buffer[5+p] != 0)
1056 usb_string(udev, buffer[5+p], unit->name,
1057 sizeof unit->name);
1058 else
1059 sprintf(unit->name, "Selector %u", buffer[3]);
1061 list_add_tail(&unit->list, &dev->entities);
1062 break;
1064 case UVC_VC_PROCESSING_UNIT:
1065 n = buflen >= 8 ? buffer[7] : 0;
1066 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1068 if (buflen < p + n) {
1069 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1070 "interface %d PROCESSING_UNIT error\n",
1071 udev->devnum, alts->desc.bInterfaceNumber);
1072 return -EINVAL;
1075 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1076 if (unit == NULL)
1077 return -ENOMEM;
1079 memcpy(unit->baSourceID, &buffer[4], 1);
1080 unit->processing.wMaxMultiplier =
1081 get_unaligned((u16 *)&(buffer[5]));
1082 unit->processing.bControlSize = buffer[7];
1083 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1084 memcpy(unit->processing.bmControls, &buffer[8], n);
1085 if (dev->uvc_version >= 0x0110)
1086 unit->processing.bmVideoStandards = buffer[9+n];
1088 if (buffer[8+n] != 0)
1089 usb_string(udev, buffer[8+n], unit->name,
1090 sizeof unit->name);
1091 else
1092 sprintf(unit->name, "Processing %u", buffer[3]);
1094 list_add_tail(&unit->list, &dev->entities);
1095 break;
1097 case UVC_VC_EXTENSION_UNIT:
1098 p = buflen >= 22 ? buffer[21] : 0;
1099 n = buflen >= 24 + p ? buffer[22+p] : 0;
1101 if (buflen < 24 + p + n) {
1102 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1103 "interface %d EXTENSION_UNIT error\n",
1104 udev->devnum, alts->desc.bInterfaceNumber);
1105 return -EINVAL;
1108 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1109 if (unit == NULL)
1110 return -ENOMEM;
1112 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1113 unit->extension.bNumControls = buffer[20];
1114 memcpy(unit->baSourceID, &buffer[22], p);
1115 unit->extension.bControlSize = buffer[22+p];
1116 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1117 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1119 if (buffer[23+p+n] != 0)
1120 usb_string(udev, buffer[23+p+n], unit->name,
1121 sizeof unit->name);
1122 else
1123 sprintf(unit->name, "Extension %u", buffer[3]);
1125 list_add_tail(&unit->list, &dev->entities);
1126 break;
1128 default:
1129 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1130 "descriptor (%u)\n", buffer[2]);
1131 break;
1134 return 0;
1137 static int uvc_parse_control(struct uvc_device *dev)
1139 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1140 unsigned char *buffer = alts->extra;
1141 int buflen = alts->extralen;
1142 int ret;
1144 /* Parse the default alternate setting only, as the UVC specification
1145 * defines a single alternate setting, the default alternate setting
1146 * zero.
1149 while (buflen > 2) {
1150 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1151 buffer[1] != USB_DT_CS_INTERFACE)
1152 goto next_descriptor;
1154 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1155 return ret;
1157 next_descriptor:
1158 buflen -= buffer[0];
1159 buffer += buffer[0];
1162 /* Check if the optional status endpoint is present. Built-in iSight
1163 * webcams have an interrupt endpoint but spit proprietary data that
1164 * don't conform to the UVC status endpoint messages. Don't try to
1165 * handle the interrupt endpoint for those cameras.
1167 if (alts->desc.bNumEndpoints == 1 &&
1168 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1169 struct usb_host_endpoint *ep = &alts->endpoint[0];
1170 struct usb_endpoint_descriptor *desc = &ep->desc;
1172 if (usb_endpoint_is_int_in(desc) &&
1173 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1174 desc->bInterval != 0) {
1175 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1176 "(addr %02x).\n", desc->bEndpointAddress);
1177 dev->int_ep = ep;
1181 return 0;
1184 /* ------------------------------------------------------------------------
1185 * UVC device scan
1189 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1190 * and containing the following units:
1192 * - one or more Output Terminals (USB Streaming or Display)
1193 * - zero or one Processing Unit
1194 * - zero, one or more single-input Selector Units
1195 * - zero or one multiple-input Selector Units, provided all inputs are
1196 * connected to input terminals
1197 * - zero, one or mode single-input Extension Units
1198 * - one or more Input Terminals (Camera, External or USB Streaming)
1200 * The terminal and units must match on of the following structures:
1202 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1203 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1204 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1206 * +---------+ +---------+ -> OTT_*(0)
1207 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1208 * +---------+ +---------+ -> OTT_*(n)
1210 * The Processing Unit and Extension Units can be in any order. Additional
1211 * Extension Units connected to the main chain as single-unit branches are
1212 * also supported. Single-input Selector Units are ignored.
1214 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1215 struct uvc_entity *entity)
1217 switch (UVC_ENTITY_TYPE(entity)) {
1218 case UVC_VC_EXTENSION_UNIT:
1219 if (uvc_trace_param & UVC_TRACE_PROBE)
1220 printk(" <- XU %d", entity->id);
1222 if (entity->bNrInPins != 1) {
1223 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1224 "than 1 input pin.\n", entity->id);
1225 return -1;
1228 break;
1230 case UVC_VC_PROCESSING_UNIT:
1231 if (uvc_trace_param & UVC_TRACE_PROBE)
1232 printk(" <- PU %d", entity->id);
1234 if (chain->processing != NULL) {
1235 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1236 "Processing Units in chain.\n");
1237 return -1;
1240 chain->processing = entity;
1241 break;
1243 case UVC_VC_SELECTOR_UNIT:
1244 if (uvc_trace_param & UVC_TRACE_PROBE)
1245 printk(" <- SU %d", entity->id);
1247 /* Single-input selector units are ignored. */
1248 if (entity->bNrInPins == 1)
1249 break;
1251 if (chain->selector != NULL) {
1252 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1253 "Units in chain.\n");
1254 return -1;
1257 chain->selector = entity;
1258 break;
1260 case UVC_ITT_VENDOR_SPECIFIC:
1261 case UVC_ITT_CAMERA:
1262 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1263 if (uvc_trace_param & UVC_TRACE_PROBE)
1264 printk(" <- IT %d\n", entity->id);
1266 break;
1268 case UVC_TT_STREAMING:
1269 if (UVC_ENTITY_IS_ITERM(entity)) {
1270 if (uvc_trace_param & UVC_TRACE_PROBE)
1271 printk(" <- IT %d\n", entity->id);
1272 } else {
1273 if (uvc_trace_param & UVC_TRACE_PROBE)
1274 printk(" OT %d", entity->id);
1277 break;
1279 default:
1280 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1281 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1282 return -1;
1285 list_add_tail(&entity->chain, &chain->entities);
1286 return 0;
1289 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1290 struct uvc_entity *entity, struct uvc_entity *prev)
1292 struct uvc_entity *forward;
1293 int found;
1295 /* Forward scan */
1296 forward = NULL;
1297 found = 0;
1299 while (1) {
1300 forward = uvc_entity_by_reference(chain->dev, entity->id,
1301 forward);
1302 if (forward == NULL)
1303 break;
1304 if (forward == prev)
1305 continue;
1307 switch (UVC_ENTITY_TYPE(forward)) {
1308 case UVC_VC_EXTENSION_UNIT:
1309 if (forward->bNrInPins != 1) {
1310 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1311 "has more than 1 input pin.\n",
1312 entity->id);
1313 return -EINVAL;
1316 list_add_tail(&forward->chain, &chain->entities);
1317 if (uvc_trace_param & UVC_TRACE_PROBE) {
1318 if (!found)
1319 printk(" (->");
1321 printk(" XU %d", forward->id);
1322 found = 1;
1324 break;
1326 case UVC_OTT_VENDOR_SPECIFIC:
1327 case UVC_OTT_DISPLAY:
1328 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1329 case UVC_TT_STREAMING:
1330 if (UVC_ENTITY_IS_ITERM(forward)) {
1331 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1332 "terminal %u.\n", forward->id);
1333 return -EINVAL;
1336 list_add_tail(&forward->chain, &chain->entities);
1337 if (uvc_trace_param & UVC_TRACE_PROBE) {
1338 if (!found)
1339 printk(" (->");
1341 printk(" OT %d", forward->id);
1342 found = 1;
1344 break;
1347 if (found)
1348 printk(")");
1350 return 0;
1353 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1354 struct uvc_entity **_entity)
1356 struct uvc_entity *entity = *_entity;
1357 struct uvc_entity *term;
1358 int id = -EINVAL, i;
1360 switch (UVC_ENTITY_TYPE(entity)) {
1361 case UVC_VC_EXTENSION_UNIT:
1362 case UVC_VC_PROCESSING_UNIT:
1363 id = entity->baSourceID[0];
1364 break;
1366 case UVC_VC_SELECTOR_UNIT:
1367 /* Single-input selector units are ignored. */
1368 if (entity->bNrInPins == 1) {
1369 id = entity->baSourceID[0];
1370 break;
1373 if (uvc_trace_param & UVC_TRACE_PROBE)
1374 printk(" <- IT");
1376 chain->selector = entity;
1377 for (i = 0; i < entity->bNrInPins; ++i) {
1378 id = entity->baSourceID[i];
1379 term = uvc_entity_by_id(chain->dev, id);
1380 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1381 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1382 "input %d isn't connected to an "
1383 "input terminal\n", entity->id, i);
1384 return -1;
1387 if (uvc_trace_param & UVC_TRACE_PROBE)
1388 printk(" %d", term->id);
1390 list_add_tail(&term->chain, &chain->entities);
1391 uvc_scan_chain_forward(chain, term, entity);
1394 if (uvc_trace_param & UVC_TRACE_PROBE)
1395 printk("\n");
1397 id = 0;
1398 break;
1400 case UVC_ITT_VENDOR_SPECIFIC:
1401 case UVC_ITT_CAMERA:
1402 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1403 case UVC_OTT_VENDOR_SPECIFIC:
1404 case UVC_OTT_DISPLAY:
1405 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1406 case UVC_TT_STREAMING:
1407 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1408 break;
1411 if (id <= 0) {
1412 *_entity = NULL;
1413 return id;
1416 entity = uvc_entity_by_id(chain->dev, id);
1417 if (entity == NULL) {
1418 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1419 "unknown entity %d.\n", id);
1420 return -EINVAL;
1423 *_entity = entity;
1424 return 0;
1427 static int uvc_scan_chain(struct uvc_video_chain *chain,
1428 struct uvc_entity *term)
1430 struct uvc_entity *entity, *prev;
1432 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1434 entity = term;
1435 prev = NULL;
1437 while (entity != NULL) {
1438 /* Entity must not be part of an existing chain */
1439 if (entity->chain.next || entity->chain.prev) {
1440 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1441 "entity %d already in chain.\n", entity->id);
1442 return -EINVAL;
1445 /* Process entity */
1446 if (uvc_scan_chain_entity(chain, entity) < 0)
1447 return -EINVAL;
1449 /* Forward scan */
1450 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1451 return -EINVAL;
1453 /* Backward scan */
1454 prev = entity;
1455 if (uvc_scan_chain_backward(chain, &entity) < 0)
1456 return -EINVAL;
1459 return 0;
1462 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1463 char *buffer)
1465 struct uvc_entity *term;
1466 unsigned int nterms = 0;
1467 char *p = buffer;
1469 list_for_each_entry(term, terms, chain) {
1470 if (!UVC_ENTITY_IS_TERM(term) ||
1471 UVC_TERM_DIRECTION(term) != dir)
1472 continue;
1474 if (nterms)
1475 p += sprintf(p, ",");
1476 if (++nterms >= 4) {
1477 p += sprintf(p, "...");
1478 break;
1480 p += sprintf(p, "%u", term->id);
1483 return p - buffer;
1486 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1488 static char buffer[43];
1489 char *p = buffer;
1491 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1492 p += sprintf(p, " -> ");
1493 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1495 return buffer;
1499 * Scan the device for video chains and register video devices.
1501 * Chains are scanned starting at their output terminals and walked backwards.
1503 static int uvc_scan_device(struct uvc_device *dev)
1505 struct uvc_video_chain *chain;
1506 struct uvc_entity *term;
1508 list_for_each_entry(term, &dev->entities, list) {
1509 if (!UVC_ENTITY_IS_OTERM(term))
1510 continue;
1512 /* If the terminal is already included in a chain, skip it.
1513 * This can happen for chains that have multiple output
1514 * terminals, where all output terminals beside the first one
1515 * will be inserted in the chain in forward scans.
1517 if (term->chain.next || term->chain.prev)
1518 continue;
1520 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1521 if (chain == NULL)
1522 return -ENOMEM;
1524 INIT_LIST_HEAD(&chain->entities);
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 * Delete the UVC device.
1554 * Called by the kernel when the last reference to the uvc_device structure
1555 * is released.
1557 * As this function is called after or during disconnect(), all URBs have
1558 * already been canceled by the USB core. There is no need to kill the
1559 * interrupt URB manually.
1561 static void uvc_delete(struct uvc_device *dev)
1563 struct list_head *p, *n;
1565 usb_put_intf(dev->intf);
1566 usb_put_dev(dev->udev);
1568 uvc_status_cleanup(dev);
1569 uvc_ctrl_cleanup_device(dev);
1571 list_for_each_safe(p, n, &dev->chains) {
1572 struct uvc_video_chain *chain;
1573 chain = list_entry(p, struct uvc_video_chain, list);
1574 kfree(chain);
1577 list_for_each_safe(p, n, &dev->entities) {
1578 struct uvc_entity *entity;
1579 entity = list_entry(p, struct uvc_entity, list);
1580 kfree(entity);
1583 list_for_each_safe(p, n, &dev->streams) {
1584 struct uvc_streaming *streaming;
1585 streaming = list_entry(p, struct uvc_streaming, list);
1586 usb_driver_release_interface(&uvc_driver.driver,
1587 streaming->intf);
1588 usb_put_intf(streaming->intf);
1589 kfree(streaming->format);
1590 kfree(streaming->header.bmaControls);
1591 kfree(streaming);
1594 kfree(dev);
1597 static void uvc_release(struct video_device *vdev)
1599 struct uvc_streaming *stream = video_get_drvdata(vdev);
1600 struct uvc_device *dev = stream->dev;
1602 video_device_release(vdev);
1604 /* Decrement the registered streams count and delete the device when it
1605 * reaches zero.
1607 if (atomic_dec_and_test(&dev->nstreams))
1608 uvc_delete(dev);
1612 * Unregister the video devices.
1614 static void uvc_unregister_video(struct uvc_device *dev)
1616 struct uvc_streaming *stream;
1618 /* Unregistering all video devices might result in uvc_delete() being
1619 * called from inside the loop if there's no open file handle. To avoid
1620 * that, increment the stream count before iterating over the streams
1621 * and decrement it when done.
1623 atomic_inc(&dev->nstreams);
1625 list_for_each_entry(stream, &dev->streams, list) {
1626 if (stream->vdev == NULL)
1627 continue;
1629 video_unregister_device(stream->vdev);
1630 stream->vdev = NULL;
1633 /* Decrement the stream count and call uvc_delete explicitly if there
1634 * are no stream left.
1636 if (atomic_dec_and_test(&dev->nstreams))
1637 uvc_delete(dev);
1640 static int uvc_register_video(struct uvc_device *dev,
1641 struct uvc_streaming *stream)
1643 struct video_device *vdev;
1644 int ret;
1646 /* Initialize the streaming interface with default streaming
1647 * parameters.
1649 ret = uvc_video_init(stream);
1650 if (ret < 0) {
1651 uvc_printk(KERN_ERR, "Failed to initialize the device "
1652 "(%d).\n", ret);
1653 return ret;
1656 /* Register the device with V4L. */
1657 vdev = video_device_alloc();
1658 if (vdev == NULL) {
1659 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1660 ret);
1661 return -ENOMEM;
1664 /* We already hold a reference to dev->udev. The video device will be
1665 * unregistered before the reference is released, so we don't need to
1666 * get another one.
1668 vdev->parent = &dev->intf->dev;
1669 vdev->fops = &uvc_fops;
1670 vdev->release = uvc_release;
1671 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1673 /* Set the driver data before calling video_register_device, otherwise
1674 * uvc_v4l2_open might race us.
1676 stream->vdev = vdev;
1677 video_set_drvdata(vdev, stream);
1679 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1680 if (ret < 0) {
1681 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1682 ret);
1683 stream->vdev = NULL;
1684 video_device_release(vdev);
1685 return ret;
1688 atomic_inc(&dev->nstreams);
1689 return 0;
1693 * Register all video devices in all chains.
1695 static int uvc_register_terms(struct uvc_device *dev,
1696 struct uvc_video_chain *chain)
1698 struct uvc_streaming *stream;
1699 struct uvc_entity *term;
1700 int ret;
1702 list_for_each_entry(term, &chain->entities, chain) {
1703 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1704 continue;
1706 stream = uvc_stream_by_id(dev, term->id);
1707 if (stream == NULL) {
1708 uvc_printk(KERN_INFO, "No streaming interface found "
1709 "for terminal %u.", term->id);
1710 continue;
1713 stream->chain = chain;
1714 ret = uvc_register_video(dev, stream);
1715 if (ret < 0)
1716 return ret;
1719 return 0;
1722 static int uvc_register_chains(struct uvc_device *dev)
1724 struct uvc_video_chain *chain;
1725 int ret;
1727 list_for_each_entry(chain, &dev->chains, list) {
1728 ret = uvc_register_terms(dev, chain);
1729 if (ret < 0)
1730 return ret;
1733 return 0;
1736 /* ------------------------------------------------------------------------
1737 * USB probe, disconnect, suspend and resume
1740 static int uvc_probe(struct usb_interface *intf,
1741 const struct usb_device_id *id)
1743 struct usb_device *udev = interface_to_usbdev(intf);
1744 struct uvc_device *dev;
1745 int ret;
1747 if (id->idVendor && id->idProduct)
1748 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1749 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1750 id->idProduct);
1751 else
1752 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1753 udev->devpath);
1755 /* Allocate memory for the device and initialize it. */
1756 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1757 return -ENOMEM;
1759 INIT_LIST_HEAD(&dev->entities);
1760 INIT_LIST_HEAD(&dev->chains);
1761 INIT_LIST_HEAD(&dev->streams);
1762 atomic_set(&dev->nstreams, 0);
1763 atomic_set(&dev->users, 0);
1765 dev->udev = usb_get_dev(udev);
1766 dev->intf = usb_get_intf(intf);
1767 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1768 dev->quirks = (uvc_quirks_param == -1)
1769 ? id->driver_info : uvc_quirks_param;
1771 if (udev->product != NULL)
1772 strlcpy(dev->name, udev->product, sizeof dev->name);
1773 else
1774 snprintf(dev->name, sizeof dev->name,
1775 "UVC Camera (%04x:%04x)",
1776 le16_to_cpu(udev->descriptor.idVendor),
1777 le16_to_cpu(udev->descriptor.idProduct));
1779 /* Parse the Video Class control descriptor. */
1780 if (uvc_parse_control(dev) < 0) {
1781 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1782 "descriptors.\n");
1783 goto error;
1786 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1787 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1788 udev->product ? udev->product : "<unnamed>",
1789 le16_to_cpu(udev->descriptor.idVendor),
1790 le16_to_cpu(udev->descriptor.idProduct));
1792 if (dev->quirks != id->driver_info) {
1793 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1794 "parameter for testing purpose.\n", dev->quirks);
1795 uvc_printk(KERN_INFO, "Please report required quirks to the "
1796 "linux-uvc-devel mailing list.\n");
1799 /* Initialize controls. */
1800 if (uvc_ctrl_init_device(dev) < 0)
1801 goto error;
1803 /* Scan the device for video chains. */
1804 if (uvc_scan_device(dev) < 0)
1805 goto error;
1807 /* Register video devices. */
1808 if (uvc_register_chains(dev) < 0)
1809 goto error;
1811 /* Save our data pointer in the interface data. */
1812 usb_set_intfdata(intf, dev);
1814 /* Initialize the interrupt URB. */
1815 if ((ret = uvc_status_init(dev)) < 0) {
1816 uvc_printk(KERN_INFO, "Unable to initialize the status "
1817 "endpoint (%d), status interrupt will not be "
1818 "supported.\n", ret);
1821 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1822 return 0;
1824 error:
1825 uvc_unregister_video(dev);
1826 return -ENODEV;
1829 static void uvc_disconnect(struct usb_interface *intf)
1831 struct uvc_device *dev = usb_get_intfdata(intf);
1833 /* Set the USB interface data to NULL. This can be done outside the
1834 * lock, as there's no other reader.
1836 usb_set_intfdata(intf, NULL);
1838 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1839 UVC_SC_VIDEOSTREAMING)
1840 return;
1842 dev->state |= UVC_DEV_DISCONNECTED;
1844 uvc_unregister_video(dev);
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);
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 * Module parameters
1914 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1916 if (uvc_clock_param == CLOCK_MONOTONIC)
1917 return sprintf(buffer, "CLOCK_MONOTONIC");
1918 else
1919 return sprintf(buffer, "CLOCK_REALTIME");
1922 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1924 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1925 val += strlen("clock_");
1927 if (strcasecmp(val, "monotonic") == 0)
1928 uvc_clock_param = CLOCK_MONOTONIC;
1929 else if (strcasecmp(val, "realtime") == 0)
1930 uvc_clock_param = CLOCK_REALTIME;
1931 else
1932 return -EINVAL;
1934 return 0;
1937 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1938 &uvc_clock_param, S_IRUGO|S_IWUSR);
1939 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1940 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1941 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1942 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1943 MODULE_PARM_DESC(quirks, "Forced device quirks");
1944 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1945 MODULE_PARM_DESC(trace, "Trace level bitmask");
1946 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1947 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1949 /* ------------------------------------------------------------------------
1950 * Driver initialization and cleanup
1954 * The Logitech cameras listed below have their interface class set to
1955 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1956 * though they are compliant.
1958 static struct usb_device_id uvc_ids[] = {
1959 /* Genius eFace 2025 */
1960 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1961 | USB_DEVICE_ID_MATCH_INT_INFO,
1962 .idVendor = 0x0458,
1963 .idProduct = 0x706e,
1964 .bInterfaceClass = USB_CLASS_VIDEO,
1965 .bInterfaceSubClass = 1,
1966 .bInterfaceProtocol = 0,
1967 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1968 /* Microsoft Lifecam NX-6000 */
1969 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1970 | USB_DEVICE_ID_MATCH_INT_INFO,
1971 .idVendor = 0x045e,
1972 .idProduct = 0x00f8,
1973 .bInterfaceClass = USB_CLASS_VIDEO,
1974 .bInterfaceSubClass = 1,
1975 .bInterfaceProtocol = 0,
1976 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1977 /* Microsoft Lifecam VX-7000 */
1978 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1979 | USB_DEVICE_ID_MATCH_INT_INFO,
1980 .idVendor = 0x045e,
1981 .idProduct = 0x0723,
1982 .bInterfaceClass = USB_CLASS_VIDEO,
1983 .bInterfaceSubClass = 1,
1984 .bInterfaceProtocol = 0,
1985 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1986 /* Logitech Quickcam Fusion */
1987 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1988 | USB_DEVICE_ID_MATCH_INT_INFO,
1989 .idVendor = 0x046d,
1990 .idProduct = 0x08c1,
1991 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1992 .bInterfaceSubClass = 1,
1993 .bInterfaceProtocol = 0 },
1994 /* Logitech Quickcam Orbit MP */
1995 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1996 | USB_DEVICE_ID_MATCH_INT_INFO,
1997 .idVendor = 0x046d,
1998 .idProduct = 0x08c2,
1999 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2000 .bInterfaceSubClass = 1,
2001 .bInterfaceProtocol = 0 },
2002 /* Logitech Quickcam Pro for Notebook */
2003 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2004 | USB_DEVICE_ID_MATCH_INT_INFO,
2005 .idVendor = 0x046d,
2006 .idProduct = 0x08c3,
2007 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2008 .bInterfaceSubClass = 1,
2009 .bInterfaceProtocol = 0 },
2010 /* Logitech Quickcam Pro 5000 */
2011 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2012 | USB_DEVICE_ID_MATCH_INT_INFO,
2013 .idVendor = 0x046d,
2014 .idProduct = 0x08c5,
2015 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2016 .bInterfaceSubClass = 1,
2017 .bInterfaceProtocol = 0 },
2018 /* Logitech Quickcam OEM Dell Notebook */
2019 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2020 | USB_DEVICE_ID_MATCH_INT_INFO,
2021 .idVendor = 0x046d,
2022 .idProduct = 0x08c6,
2023 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2024 .bInterfaceSubClass = 1,
2025 .bInterfaceProtocol = 0 },
2026 /* Logitech Quickcam OEM Cisco VT Camera II */
2027 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2028 | USB_DEVICE_ID_MATCH_INT_INFO,
2029 .idVendor = 0x046d,
2030 .idProduct = 0x08c7,
2031 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2032 .bInterfaceSubClass = 1,
2033 .bInterfaceProtocol = 0 },
2034 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2035 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2036 | USB_DEVICE_ID_MATCH_INT_INFO,
2037 .idVendor = 0x058f,
2038 .idProduct = 0x3820,
2039 .bInterfaceClass = USB_CLASS_VIDEO,
2040 .bInterfaceSubClass = 1,
2041 .bInterfaceProtocol = 0,
2042 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2043 /* Apple Built-In iSight */
2044 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2045 | USB_DEVICE_ID_MATCH_INT_INFO,
2046 .idVendor = 0x05ac,
2047 .idProduct = 0x8501,
2048 .bInterfaceClass = USB_CLASS_VIDEO,
2049 .bInterfaceSubClass = 1,
2050 .bInterfaceProtocol = 0,
2051 .driver_info = UVC_QUIRK_PROBE_MINMAX
2052 | UVC_QUIRK_BUILTIN_ISIGHT },
2053 /* Genesys Logic USB 2.0 PC Camera */
2054 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2055 | USB_DEVICE_ID_MATCH_INT_INFO,
2056 .idVendor = 0x05e3,
2057 .idProduct = 0x0505,
2058 .bInterfaceClass = USB_CLASS_VIDEO,
2059 .bInterfaceSubClass = 1,
2060 .bInterfaceProtocol = 0,
2061 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2062 /* ViMicro Vega */
2063 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2064 | USB_DEVICE_ID_MATCH_INT_INFO,
2065 .idVendor = 0x0ac8,
2066 .idProduct = 0x332d,
2067 .bInterfaceClass = USB_CLASS_VIDEO,
2068 .bInterfaceSubClass = 1,
2069 .bInterfaceProtocol = 0,
2070 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2071 /* ViMicro - Minoru3D */
2072 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2073 | USB_DEVICE_ID_MATCH_INT_INFO,
2074 .idVendor = 0x0ac8,
2075 .idProduct = 0x3410,
2076 .bInterfaceClass = USB_CLASS_VIDEO,
2077 .bInterfaceSubClass = 1,
2078 .bInterfaceProtocol = 0,
2079 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2080 /* ViMicro Venus - Minoru3D */
2081 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2082 | USB_DEVICE_ID_MATCH_INT_INFO,
2083 .idVendor = 0x0ac8,
2084 .idProduct = 0x3420,
2085 .bInterfaceClass = USB_CLASS_VIDEO,
2086 .bInterfaceSubClass = 1,
2087 .bInterfaceProtocol = 0,
2088 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2089 /* MT6227 */
2090 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2091 | USB_DEVICE_ID_MATCH_INT_INFO,
2092 .idVendor = 0x0e8d,
2093 .idProduct = 0x0004,
2094 .bInterfaceClass = USB_CLASS_VIDEO,
2095 .bInterfaceSubClass = 1,
2096 .bInterfaceProtocol = 0,
2097 .driver_info = UVC_QUIRK_PROBE_MINMAX
2098 | UVC_QUIRK_PROBE_DEF },
2099 /* Syntek (HP Spartan) */
2100 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2101 | USB_DEVICE_ID_MATCH_INT_INFO,
2102 .idVendor = 0x174f,
2103 .idProduct = 0x5212,
2104 .bInterfaceClass = USB_CLASS_VIDEO,
2105 .bInterfaceSubClass = 1,
2106 .bInterfaceProtocol = 0,
2107 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2108 /* Syntek (Samsung Q310) */
2109 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2110 | USB_DEVICE_ID_MATCH_INT_INFO,
2111 .idVendor = 0x174f,
2112 .idProduct = 0x5931,
2113 .bInterfaceClass = USB_CLASS_VIDEO,
2114 .bInterfaceSubClass = 1,
2115 .bInterfaceProtocol = 0,
2116 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2117 /* Syntek (Packard Bell EasyNote MX52 */
2118 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2119 | USB_DEVICE_ID_MATCH_INT_INFO,
2120 .idVendor = 0x174f,
2121 .idProduct = 0x8a12,
2122 .bInterfaceClass = USB_CLASS_VIDEO,
2123 .bInterfaceSubClass = 1,
2124 .bInterfaceProtocol = 0,
2125 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2126 /* Syntek (Asus F9SG) */
2127 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2128 | USB_DEVICE_ID_MATCH_INT_INFO,
2129 .idVendor = 0x174f,
2130 .idProduct = 0x8a31,
2131 .bInterfaceClass = USB_CLASS_VIDEO,
2132 .bInterfaceSubClass = 1,
2133 .bInterfaceProtocol = 0,
2134 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2135 /* Syntek (Asus U3S) */
2136 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2137 | USB_DEVICE_ID_MATCH_INT_INFO,
2138 .idVendor = 0x174f,
2139 .idProduct = 0x8a33,
2140 .bInterfaceClass = USB_CLASS_VIDEO,
2141 .bInterfaceSubClass = 1,
2142 .bInterfaceProtocol = 0,
2143 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2144 /* Syntek (JAOtech Smart Terminal) */
2145 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2146 | USB_DEVICE_ID_MATCH_INT_INFO,
2147 .idVendor = 0x174f,
2148 .idProduct = 0x8a34,
2149 .bInterfaceClass = USB_CLASS_VIDEO,
2150 .bInterfaceSubClass = 1,
2151 .bInterfaceProtocol = 0,
2152 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2153 /* Miricle 307K */
2154 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2155 | USB_DEVICE_ID_MATCH_INT_INFO,
2156 .idVendor = 0x17dc,
2157 .idProduct = 0x0202,
2158 .bInterfaceClass = USB_CLASS_VIDEO,
2159 .bInterfaceSubClass = 1,
2160 .bInterfaceProtocol = 0,
2161 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2162 /* Lenovo Thinkpad SL400/SL500 */
2163 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2164 | USB_DEVICE_ID_MATCH_INT_INFO,
2165 .idVendor = 0x17ef,
2166 .idProduct = 0x480b,
2167 .bInterfaceClass = USB_CLASS_VIDEO,
2168 .bInterfaceSubClass = 1,
2169 .bInterfaceProtocol = 0,
2170 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2171 /* Aveo Technology USB 2.0 Camera */
2172 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2173 | USB_DEVICE_ID_MATCH_INT_INFO,
2174 .idVendor = 0x1871,
2175 .idProduct = 0x0306,
2176 .bInterfaceClass = USB_CLASS_VIDEO,
2177 .bInterfaceSubClass = 1,
2178 .bInterfaceProtocol = 0,
2179 .driver_info = UVC_QUIRK_PROBE_MINMAX
2180 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2181 /* Ecamm Pico iMage */
2182 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2183 | USB_DEVICE_ID_MATCH_INT_INFO,
2184 .idVendor = 0x18cd,
2185 .idProduct = 0xcafe,
2186 .bInterfaceClass = USB_CLASS_VIDEO,
2187 .bInterfaceSubClass = 1,
2188 .bInterfaceProtocol = 0,
2189 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2190 /* Manta MM-353 Plako */
2191 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2192 | USB_DEVICE_ID_MATCH_INT_INFO,
2193 .idVendor = 0x18ec,
2194 .idProduct = 0x3188,
2195 .bInterfaceClass = USB_CLASS_VIDEO,
2196 .bInterfaceSubClass = 1,
2197 .bInterfaceProtocol = 0,
2198 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2199 /* FSC WebCam V30S */
2200 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2201 | USB_DEVICE_ID_MATCH_INT_INFO,
2202 .idVendor = 0x18ec,
2203 .idProduct = 0x3288,
2204 .bInterfaceClass = USB_CLASS_VIDEO,
2205 .bInterfaceSubClass = 1,
2206 .bInterfaceProtocol = 0,
2207 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2208 /* Arkmicro unbranded */
2209 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2210 | USB_DEVICE_ID_MATCH_INT_INFO,
2211 .idVendor = 0x18ec,
2212 .idProduct = 0x3290,
2213 .bInterfaceClass = USB_CLASS_VIDEO,
2214 .bInterfaceSubClass = 1,
2215 .bInterfaceProtocol = 0,
2216 .driver_info = UVC_QUIRK_PROBE_DEF },
2217 /* Bodelin ProScopeHR */
2218 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2219 | USB_DEVICE_ID_MATCH_DEV_HI
2220 | USB_DEVICE_ID_MATCH_INT_INFO,
2221 .idVendor = 0x19ab,
2222 .idProduct = 0x1000,
2223 .bcdDevice_hi = 0x0126,
2224 .bInterfaceClass = USB_CLASS_VIDEO,
2225 .bInterfaceSubClass = 1,
2226 .bInterfaceProtocol = 0,
2227 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
2228 /* MSI StarCam 370i */
2229 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2230 | USB_DEVICE_ID_MATCH_INT_INFO,
2231 .idVendor = 0x1b3b,
2232 .idProduct = 0x2951,
2233 .bInterfaceClass = USB_CLASS_VIDEO,
2234 .bInterfaceSubClass = 1,
2235 .bInterfaceProtocol = 0,
2236 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2237 /* SiGma Micro USB Web Camera */
2238 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2239 | USB_DEVICE_ID_MATCH_INT_INFO,
2240 .idVendor = 0x1c4f,
2241 .idProduct = 0x3000,
2242 .bInterfaceClass = USB_CLASS_VIDEO,
2243 .bInterfaceSubClass = 1,
2244 .bInterfaceProtocol = 0,
2245 .driver_info = UVC_QUIRK_PROBE_MINMAX
2246 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2247 /* Generic USB Video Class */
2248 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2252 MODULE_DEVICE_TABLE(usb, uvc_ids);
2254 struct uvc_driver uvc_driver = {
2255 .driver = {
2256 .name = "uvcvideo",
2257 .probe = uvc_probe,
2258 .disconnect = uvc_disconnect,
2259 .suspend = uvc_suspend,
2260 .resume = uvc_resume,
2261 .reset_resume = uvc_reset_resume,
2262 .id_table = uvc_ids,
2263 .supports_autosuspend = 1,
2267 static int __init uvc_init(void)
2269 int result;
2271 INIT_LIST_HEAD(&uvc_driver.devices);
2272 INIT_LIST_HEAD(&uvc_driver.controls);
2273 mutex_init(&uvc_driver.ctrl_mutex);
2275 uvc_ctrl_init();
2277 result = usb_register(&uvc_driver.driver);
2278 if (result == 0)
2279 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2280 return result;
2283 static void __exit uvc_cleanup(void)
2285 usb_deregister(&uvc_driver.driver);
2286 uvc_ctrl_cleanup();
2289 module_init(uvc_init);
2290 module_exit(uvc_cleanup);
2292 MODULE_AUTHOR(DRIVER_AUTHOR);
2293 MODULE_DESCRIPTION(DRIVER_DESC);
2294 MODULE_LICENSE("GPL");
2295 MODULE_VERSION(DRIVER_VERSION);