Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / uvc / uvc_driver.c
blob38c73cdbef70e85846a33dc0674b709c3bc1ed86
1 /*
2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <linux/version.h>
24 #include <asm/unaligned.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
29 #include "uvcvideo.h"
31 #define DRIVER_AUTHOR "Laurent Pinchart " \
32 "<laurent.pinchart@ideasonboard.com>"
33 #define DRIVER_DESC "USB Video Class driver"
35 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
36 unsigned int uvc_hw_timestamps_param;
37 unsigned int uvc_no_drop_param;
38 static unsigned int uvc_quirks_param = -1;
39 unsigned int uvc_trace_param;
40 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
42 /* ------------------------------------------------------------------------
43 * Video formats
46 static struct uvc_format_desc uvc_fmts[] = {
48 .name = "YUV 4:2:2 (YUYV)",
49 .guid = UVC_GUID_FORMAT_YUY2,
50 .fcc = V4L2_PIX_FMT_YUYV,
53 .name = "YUV 4:2:2 (YUYV)",
54 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
55 .fcc = V4L2_PIX_FMT_YUYV,
58 .name = "YUV 4:2:0 (NV12)",
59 .guid = UVC_GUID_FORMAT_NV12,
60 .fcc = V4L2_PIX_FMT_NV12,
63 .name = "MJPEG",
64 .guid = UVC_GUID_FORMAT_MJPEG,
65 .fcc = V4L2_PIX_FMT_MJPEG,
68 .name = "YVU 4:2:0 (YV12)",
69 .guid = UVC_GUID_FORMAT_YV12,
70 .fcc = V4L2_PIX_FMT_YVU420,
73 .name = "YUV 4:2:0 (I420)",
74 .guid = UVC_GUID_FORMAT_I420,
75 .fcc = V4L2_PIX_FMT_YUV420,
78 .name = "YUV 4:2:0 (M420)",
79 .guid = UVC_GUID_FORMAT_M420,
80 .fcc = V4L2_PIX_FMT_M420,
83 .name = "YUV 4:2:2 (UYVY)",
84 .guid = UVC_GUID_FORMAT_UYVY,
85 .fcc = V4L2_PIX_FMT_UYVY,
88 .name = "Greyscale 8-bit (Y800)",
89 .guid = UVC_GUID_FORMAT_Y800,
90 .fcc = V4L2_PIX_FMT_GREY,
93 .name = "Greyscale 8-bit (Y8 )",
94 .guid = UVC_GUID_FORMAT_Y8,
95 .fcc = V4L2_PIX_FMT_GREY,
98 .name = "Greyscale 8-bit (D3DFMT_L8)",
99 .guid = UVC_GUID_FORMAT_D3DFMT_L8,
100 .fcc = V4L2_PIX_FMT_GREY,
103 .name = "IR 8-bit (L8_IR)",
104 .guid = UVC_GUID_FORMAT_KSMEDIA_L8_IR,
105 .fcc = V4L2_PIX_FMT_GREY,
108 .name = "Greyscale 10-bit (Y10 )",
109 .guid = UVC_GUID_FORMAT_Y10,
110 .fcc = V4L2_PIX_FMT_Y10,
113 .name = "Greyscale 12-bit (Y12 )",
114 .guid = UVC_GUID_FORMAT_Y12,
115 .fcc = V4L2_PIX_FMT_Y12,
118 .name = "Greyscale 16-bit (Y16 )",
119 .guid = UVC_GUID_FORMAT_Y16,
120 .fcc = V4L2_PIX_FMT_Y16,
123 .name = "BGGR Bayer (BY8 )",
124 .guid = UVC_GUID_FORMAT_BY8,
125 .fcc = V4L2_PIX_FMT_SBGGR8,
128 .name = "BGGR Bayer (BA81)",
129 .guid = UVC_GUID_FORMAT_BA81,
130 .fcc = V4L2_PIX_FMT_SBGGR8,
133 .name = "GBRG Bayer (GBRG)",
134 .guid = UVC_GUID_FORMAT_GBRG,
135 .fcc = V4L2_PIX_FMT_SGBRG8,
138 .name = "GRBG Bayer (GRBG)",
139 .guid = UVC_GUID_FORMAT_GRBG,
140 .fcc = V4L2_PIX_FMT_SGRBG8,
143 .name = "RGGB Bayer (RGGB)",
144 .guid = UVC_GUID_FORMAT_RGGB,
145 .fcc = V4L2_PIX_FMT_SRGGB8,
148 .name = "RGB565",
149 .guid = UVC_GUID_FORMAT_RGBP,
150 .fcc = V4L2_PIX_FMT_RGB565,
153 .name = "BGR 8:8:8 (BGR3)",
154 .guid = UVC_GUID_FORMAT_BGR3,
155 .fcc = V4L2_PIX_FMT_BGR24,
158 .name = "H.264",
159 .guid = UVC_GUID_FORMAT_H264,
160 .fcc = V4L2_PIX_FMT_H264,
163 .name = "Greyscale 8 L/R (Y8I)",
164 .guid = UVC_GUID_FORMAT_Y8I,
165 .fcc = V4L2_PIX_FMT_Y8I,
168 .name = "Greyscale 12 L/R (Y12I)",
169 .guid = UVC_GUID_FORMAT_Y12I,
170 .fcc = V4L2_PIX_FMT_Y12I,
173 .name = "Depth data 16-bit (Z16)",
174 .guid = UVC_GUID_FORMAT_Z16,
175 .fcc = V4L2_PIX_FMT_Z16,
178 .name = "Bayer 10-bit (SRGGB10P)",
179 .guid = UVC_GUID_FORMAT_RW10,
180 .fcc = V4L2_PIX_FMT_SRGGB10P,
183 .name = "Bayer 16-bit (SBGGR16)",
184 .guid = UVC_GUID_FORMAT_BG16,
185 .fcc = V4L2_PIX_FMT_SBGGR16,
188 .name = "Bayer 16-bit (SGBRG16)",
189 .guid = UVC_GUID_FORMAT_GB16,
190 .fcc = V4L2_PIX_FMT_SGBRG16,
193 .name = "Bayer 16-bit (SRGGB16)",
194 .guid = UVC_GUID_FORMAT_RG16,
195 .fcc = V4L2_PIX_FMT_SRGGB16,
198 .name = "Bayer 16-bit (SGRBG16)",
199 .guid = UVC_GUID_FORMAT_GR16,
200 .fcc = V4L2_PIX_FMT_SGRBG16,
203 .name = "Depth data 16-bit (Z16)",
204 .guid = UVC_GUID_FORMAT_INVZ,
205 .fcc = V4L2_PIX_FMT_Z16,
208 .name = "Greyscale 10-bit (Y10 )",
209 .guid = UVC_GUID_FORMAT_INVI,
210 .fcc = V4L2_PIX_FMT_Y10,
213 .name = "IR:Depth 26-bit (INZI)",
214 .guid = UVC_GUID_FORMAT_INZI,
215 .fcc = V4L2_PIX_FMT_INZI,
219 /* ------------------------------------------------------------------------
220 * Utility functions
223 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
224 u8 epaddr)
226 struct usb_host_endpoint *ep;
227 unsigned int i;
229 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
230 ep = &alts->endpoint[i];
231 if (ep->desc.bEndpointAddress == epaddr)
232 return ep;
235 return NULL;
238 static struct uvc_format_desc *uvc_format_by_guid(const u8 guid[16])
240 unsigned int len = ARRAY_SIZE(uvc_fmts);
241 unsigned int i;
243 for (i = 0; i < len; ++i) {
244 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
245 return &uvc_fmts[i];
248 return NULL;
251 static u32 uvc_colorspace(const u8 primaries)
253 static const u8 colorprimaries[] = {
255 V4L2_COLORSPACE_SRGB,
256 V4L2_COLORSPACE_470_SYSTEM_M,
257 V4L2_COLORSPACE_470_SYSTEM_BG,
258 V4L2_COLORSPACE_SMPTE170M,
259 V4L2_COLORSPACE_SMPTE240M,
262 if (primaries < ARRAY_SIZE(colorprimaries))
263 return colorprimaries[primaries];
265 return 0;
268 /* Simplify a fraction using a simple continued fraction decomposition. The
269 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
270 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
271 * arbitrary parameters to remove non-significative terms from the simple
272 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
273 * respectively seems to give nice results.
275 void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
276 unsigned int n_terms, unsigned int threshold)
278 u32 *an;
279 u32 x, y, r;
280 unsigned int i, n;
282 an = kmalloc_array(n_terms, sizeof(*an), GFP_KERNEL);
283 if (an == NULL)
284 return;
286 /* Convert the fraction to a simple continued fraction. See
287 * http://mathforum.org/dr.math/faq/faq.fractions.html
288 * Stop if the current term is bigger than or equal to the given
289 * threshold.
291 x = *numerator;
292 y = *denominator;
294 for (n = 0; n < n_terms && y != 0; ++n) {
295 an[n] = x / y;
296 if (an[n] >= threshold) {
297 if (n < 2)
298 n++;
299 break;
302 r = x - an[n] * y;
303 x = y;
304 y = r;
307 /* Expand the simple continued fraction back to an integer fraction. */
308 x = 0;
309 y = 1;
311 for (i = n; i > 0; --i) {
312 r = y;
313 y = an[i-1] * y + x;
314 x = r;
317 *numerator = y;
318 *denominator = x;
319 kfree(an);
322 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
323 * to compute numerator / denominator * 10000000 using 32 bit fixed point
324 * arithmetic only.
326 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator)
328 u32 multiplier;
330 /* Saturate the result if the operation would overflow. */
331 if (denominator == 0 ||
332 numerator/denominator >= ((u32)-1)/10000000)
333 return (u32)-1;
335 /* Divide both the denominator and the multiplier by two until
336 * numerator * multiplier doesn't overflow. If anyone knows a better
337 * algorithm please let me know.
339 multiplier = 10000000;
340 while (numerator > ((u32)-1)/multiplier) {
341 multiplier /= 2;
342 denominator /= 2;
345 return denominator ? numerator * multiplier / denominator : 0;
348 /* ------------------------------------------------------------------------
349 * Terminal and unit management
352 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
354 struct uvc_entity *entity;
356 list_for_each_entry(entity, &dev->entities, list) {
357 if (entity->id == id)
358 return entity;
361 return NULL;
364 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
365 int id, struct uvc_entity *entity)
367 unsigned int i;
369 if (entity == NULL)
370 entity = list_entry(&dev->entities, struct uvc_entity, list);
372 list_for_each_entry_continue(entity, &dev->entities, list) {
373 for (i = 0; i < entity->bNrInPins; ++i)
374 if (entity->baSourceID[i] == id)
375 return entity;
378 return NULL;
381 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
383 struct uvc_streaming *stream;
385 list_for_each_entry(stream, &dev->streams, list) {
386 if (stream->header.bTerminalLink == id)
387 return stream;
390 return NULL;
393 /* ------------------------------------------------------------------------
394 * Streaming Object Management
397 static void uvc_stream_delete(struct uvc_streaming *stream)
399 mutex_destroy(&stream->mutex);
401 usb_put_intf(stream->intf);
403 kfree(stream->format);
404 kfree(stream->header.bmaControls);
405 kfree(stream);
408 static struct uvc_streaming *uvc_stream_new(struct uvc_device *dev,
409 struct usb_interface *intf)
411 struct uvc_streaming *stream;
413 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
414 if (stream == NULL)
415 return NULL;
417 mutex_init(&stream->mutex);
419 stream->dev = dev;
420 stream->intf = usb_get_intf(intf);
421 stream->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
423 return stream;
426 /* ------------------------------------------------------------------------
427 * Descriptors parsing
430 static int uvc_parse_format(struct uvc_device *dev,
431 struct uvc_streaming *streaming, struct uvc_format *format,
432 u32 **intervals, unsigned char *buffer, int buflen)
434 struct usb_interface *intf = streaming->intf;
435 struct usb_host_interface *alts = intf->cur_altsetting;
436 struct uvc_format_desc *fmtdesc;
437 struct uvc_frame *frame;
438 const unsigned char *start = buffer;
439 unsigned int width_multiplier = 1;
440 unsigned int interval;
441 unsigned int i, n;
442 u8 ftype;
444 format->type = buffer[2];
445 format->index = buffer[3];
447 switch (buffer[2]) {
448 case UVC_VS_FORMAT_UNCOMPRESSED:
449 case UVC_VS_FORMAT_FRAME_BASED:
450 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
451 if (buflen < n) {
452 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
453 "interface %d FORMAT error\n",
454 dev->udev->devnum,
455 alts->desc.bInterfaceNumber);
456 return -EINVAL;
459 /* Find the format descriptor from its GUID. */
460 fmtdesc = uvc_format_by_guid(&buffer[5]);
462 if (fmtdesc != NULL) {
463 strlcpy(format->name, fmtdesc->name,
464 sizeof(format->name));
465 format->fcc = fmtdesc->fcc;
466 } else {
467 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
468 &buffer[5]);
469 snprintf(format->name, sizeof(format->name), "%pUl\n",
470 &buffer[5]);
471 format->fcc = 0;
474 format->bpp = buffer[21];
476 /* Some devices report a format that doesn't match what they
477 * really send.
479 if (dev->quirks & UVC_QUIRK_FORCE_Y8) {
480 if (format->fcc == V4L2_PIX_FMT_YUYV) {
481 strlcpy(format->name, "Greyscale 8-bit (Y8 )",
482 sizeof(format->name));
483 format->fcc = V4L2_PIX_FMT_GREY;
484 format->bpp = 8;
485 width_multiplier = 2;
489 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
490 ftype = UVC_VS_FRAME_UNCOMPRESSED;
491 } else {
492 ftype = UVC_VS_FRAME_FRAME_BASED;
493 if (buffer[27])
494 format->flags = UVC_FMT_FLAG_COMPRESSED;
496 break;
498 case UVC_VS_FORMAT_MJPEG:
499 if (buflen < 11) {
500 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
501 "interface %d FORMAT error\n",
502 dev->udev->devnum,
503 alts->desc.bInterfaceNumber);
504 return -EINVAL;
507 strlcpy(format->name, "MJPEG", sizeof(format->name));
508 format->fcc = V4L2_PIX_FMT_MJPEG;
509 format->flags = UVC_FMT_FLAG_COMPRESSED;
510 format->bpp = 0;
511 ftype = UVC_VS_FRAME_MJPEG;
512 break;
514 case UVC_VS_FORMAT_DV:
515 if (buflen < 9) {
516 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
517 "interface %d FORMAT error\n",
518 dev->udev->devnum,
519 alts->desc.bInterfaceNumber);
520 return -EINVAL;
523 switch (buffer[8] & 0x7f) {
524 case 0:
525 strlcpy(format->name, "SD-DV", sizeof(format->name));
526 break;
527 case 1:
528 strlcpy(format->name, "SDL-DV", sizeof(format->name));
529 break;
530 case 2:
531 strlcpy(format->name, "HD-DV", sizeof(format->name));
532 break;
533 default:
534 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
535 "interface %d: unknown DV format %u\n",
536 dev->udev->devnum,
537 alts->desc.bInterfaceNumber, buffer[8]);
538 return -EINVAL;
541 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
542 sizeof(format->name));
544 format->fcc = V4L2_PIX_FMT_DV;
545 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
546 format->bpp = 0;
547 ftype = 0;
549 /* Create a dummy frame descriptor. */
550 frame = &format->frame[0];
551 memset(&format->frame[0], 0, sizeof(format->frame[0]));
552 frame->bFrameIntervalType = 1;
553 frame->dwDefaultFrameInterval = 1;
554 frame->dwFrameInterval = *intervals;
555 *(*intervals)++ = 1;
556 format->nframes = 1;
557 break;
559 case UVC_VS_FORMAT_MPEG2TS:
560 case UVC_VS_FORMAT_STREAM_BASED:
561 /* Not supported yet. */
562 default:
563 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
564 "interface %d unsupported format %u\n",
565 dev->udev->devnum, alts->desc.bInterfaceNumber,
566 buffer[2]);
567 return -EINVAL;
570 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
572 buflen -= buffer[0];
573 buffer += buffer[0];
575 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
576 * based formats have frame descriptors.
578 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
579 buffer[2] == ftype) {
580 frame = &format->frame[format->nframes];
581 if (ftype != UVC_VS_FRAME_FRAME_BASED)
582 n = buflen > 25 ? buffer[25] : 0;
583 else
584 n = buflen > 21 ? buffer[21] : 0;
586 n = n ? n : 3;
588 if (buflen < 26 + 4*n) {
589 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
590 "interface %d FRAME error\n", dev->udev->devnum,
591 alts->desc.bInterfaceNumber);
592 return -EINVAL;
595 frame->bFrameIndex = buffer[3];
596 frame->bmCapabilities = buffer[4];
597 frame->wWidth = get_unaligned_le16(&buffer[5])
598 * width_multiplier;
599 frame->wHeight = get_unaligned_le16(&buffer[7]);
600 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
601 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
602 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
603 frame->dwMaxVideoFrameBufferSize =
604 get_unaligned_le32(&buffer[17]);
605 frame->dwDefaultFrameInterval =
606 get_unaligned_le32(&buffer[21]);
607 frame->bFrameIntervalType = buffer[25];
608 } else {
609 frame->dwMaxVideoFrameBufferSize = 0;
610 frame->dwDefaultFrameInterval =
611 get_unaligned_le32(&buffer[17]);
612 frame->bFrameIntervalType = buffer[21];
614 frame->dwFrameInterval = *intervals;
616 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
617 * completely. Observed behaviours range from setting the
618 * value to 1.1x the actual frame size to hardwiring the
619 * 16 low bits to 0. This results in a higher than necessary
620 * memory usage as well as a wrong image size information. For
621 * uncompressed formats this can be fixed by computing the
622 * value from the frame size.
624 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
625 frame->dwMaxVideoFrameBufferSize = format->bpp
626 * frame->wWidth * frame->wHeight / 8;
628 /* Some bogus devices report dwMinFrameInterval equal to
629 * dwMaxFrameInterval and have dwFrameIntervalStep set to
630 * zero. Setting all null intervals to 1 fixes the problem and
631 * some other divisions by zero that could happen.
633 for (i = 0; i < n; ++i) {
634 interval = get_unaligned_le32(&buffer[26+4*i]);
635 *(*intervals)++ = interval ? interval : 1;
638 /* Make sure that the default frame interval stays between
639 * the boundaries.
641 n -= frame->bFrameIntervalType ? 1 : 2;
642 frame->dwDefaultFrameInterval =
643 min(frame->dwFrameInterval[n],
644 max(frame->dwFrameInterval[0],
645 frame->dwDefaultFrameInterval));
647 if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
648 frame->bFrameIntervalType = 1;
649 frame->dwFrameInterval[0] =
650 frame->dwDefaultFrameInterval;
653 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
654 frame->wWidth, frame->wHeight,
655 10000000/frame->dwDefaultFrameInterval,
656 (100000000/frame->dwDefaultFrameInterval)%10);
658 format->nframes++;
659 buflen -= buffer[0];
660 buffer += buffer[0];
663 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
664 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
665 buflen -= buffer[0];
666 buffer += buffer[0];
669 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
670 buffer[2] == UVC_VS_COLORFORMAT) {
671 if (buflen < 6) {
672 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
673 "interface %d COLORFORMAT error\n",
674 dev->udev->devnum,
675 alts->desc.bInterfaceNumber);
676 return -EINVAL;
679 format->colorspace = uvc_colorspace(buffer[3]);
681 buflen -= buffer[0];
682 buffer += buffer[0];
685 return buffer - start;
688 static int uvc_parse_streaming(struct uvc_device *dev,
689 struct usb_interface *intf)
691 struct uvc_streaming *streaming = NULL;
692 struct uvc_format *format;
693 struct uvc_frame *frame;
694 struct usb_host_interface *alts = &intf->altsetting[0];
695 unsigned char *_buffer, *buffer = alts->extra;
696 int _buflen, buflen = alts->extralen;
697 unsigned int nformats = 0, nframes = 0, nintervals = 0;
698 unsigned int size, i, n, p;
699 u32 *interval;
700 u16 psize;
701 int ret = -EINVAL;
703 if (intf->cur_altsetting->desc.bInterfaceSubClass
704 != UVC_SC_VIDEOSTREAMING) {
705 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
706 "video streaming interface\n", dev->udev->devnum,
707 intf->altsetting[0].desc.bInterfaceNumber);
708 return -EINVAL;
711 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
712 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
713 "claimed\n", dev->udev->devnum,
714 intf->altsetting[0].desc.bInterfaceNumber);
715 return -EINVAL;
718 streaming = uvc_stream_new(dev, intf);
719 if (streaming == NULL) {
720 usb_driver_release_interface(&uvc_driver.driver, intf);
721 return -ENOMEM;
724 /* The Pico iMage webcam has its class-specific interface descriptors
725 * after the endpoint descriptors.
727 if (buflen == 0) {
728 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
729 struct usb_host_endpoint *ep = &alts->endpoint[i];
731 if (ep->extralen == 0)
732 continue;
734 if (ep->extralen > 2 &&
735 ep->extra[1] == USB_DT_CS_INTERFACE) {
736 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
737 "from endpoint %u.\n", i);
738 buffer = alts->endpoint[i].extra;
739 buflen = alts->endpoint[i].extralen;
740 break;
745 /* Skip the standard interface descriptors. */
746 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
747 buflen -= buffer[0];
748 buffer += buffer[0];
751 if (buflen <= 2) {
752 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
753 "interface descriptors found.\n");
754 goto error;
757 /* Parse the header descriptor. */
758 switch (buffer[2]) {
759 case UVC_VS_OUTPUT_HEADER:
760 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
761 size = 9;
762 break;
764 case UVC_VS_INPUT_HEADER:
765 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
766 size = 13;
767 break;
769 default:
770 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
771 "%d HEADER descriptor not found.\n", dev->udev->devnum,
772 alts->desc.bInterfaceNumber);
773 goto error;
776 p = buflen >= 4 ? buffer[3] : 0;
777 n = buflen >= size ? buffer[size-1] : 0;
779 if (buflen < size + p*n) {
780 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
781 "interface %d HEADER descriptor is invalid.\n",
782 dev->udev->devnum, alts->desc.bInterfaceNumber);
783 goto error;
786 streaming->header.bNumFormats = p;
787 streaming->header.bEndpointAddress = buffer[6];
788 if (buffer[2] == UVC_VS_INPUT_HEADER) {
789 streaming->header.bmInfo = buffer[7];
790 streaming->header.bTerminalLink = buffer[8];
791 streaming->header.bStillCaptureMethod = buffer[9];
792 streaming->header.bTriggerSupport = buffer[10];
793 streaming->header.bTriggerUsage = buffer[11];
794 } else {
795 streaming->header.bTerminalLink = buffer[7];
797 streaming->header.bControlSize = n;
799 streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
800 GFP_KERNEL);
801 if (streaming->header.bmaControls == NULL) {
802 ret = -ENOMEM;
803 goto error;
806 buflen -= buffer[0];
807 buffer += buffer[0];
809 _buffer = buffer;
810 _buflen = buflen;
812 /* Count the format and frame descriptors. */
813 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
814 switch (_buffer[2]) {
815 case UVC_VS_FORMAT_UNCOMPRESSED:
816 case UVC_VS_FORMAT_MJPEG:
817 case UVC_VS_FORMAT_FRAME_BASED:
818 nformats++;
819 break;
821 case UVC_VS_FORMAT_DV:
822 /* DV format has no frame descriptor. We will create a
823 * dummy frame descriptor with a dummy frame interval.
825 nformats++;
826 nframes++;
827 nintervals++;
828 break;
830 case UVC_VS_FORMAT_MPEG2TS:
831 case UVC_VS_FORMAT_STREAM_BASED:
832 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
833 "interface %d FORMAT %u is not supported.\n",
834 dev->udev->devnum,
835 alts->desc.bInterfaceNumber, _buffer[2]);
836 break;
838 case UVC_VS_FRAME_UNCOMPRESSED:
839 case UVC_VS_FRAME_MJPEG:
840 nframes++;
841 if (_buflen > 25)
842 nintervals += _buffer[25] ? _buffer[25] : 3;
843 break;
845 case UVC_VS_FRAME_FRAME_BASED:
846 nframes++;
847 if (_buflen > 21)
848 nintervals += _buffer[21] ? _buffer[21] : 3;
849 break;
852 _buflen -= _buffer[0];
853 _buffer += _buffer[0];
856 if (nformats == 0) {
857 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
858 "%d has no supported formats defined.\n",
859 dev->udev->devnum, alts->desc.bInterfaceNumber);
860 goto error;
863 size = nformats * sizeof(*format) + nframes * sizeof(*frame)
864 + nintervals * sizeof(*interval);
865 format = kzalloc(size, GFP_KERNEL);
866 if (format == NULL) {
867 ret = -ENOMEM;
868 goto error;
871 frame = (struct uvc_frame *)&format[nformats];
872 interval = (u32 *)&frame[nframes];
874 streaming->format = format;
875 streaming->nformats = nformats;
877 /* Parse the format descriptors. */
878 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
879 switch (buffer[2]) {
880 case UVC_VS_FORMAT_UNCOMPRESSED:
881 case UVC_VS_FORMAT_MJPEG:
882 case UVC_VS_FORMAT_DV:
883 case UVC_VS_FORMAT_FRAME_BASED:
884 format->frame = frame;
885 ret = uvc_parse_format(dev, streaming, format,
886 &interval, buffer, buflen);
887 if (ret < 0)
888 goto error;
890 frame += format->nframes;
891 format++;
893 buflen -= ret;
894 buffer += ret;
895 continue;
897 default:
898 break;
901 buflen -= buffer[0];
902 buffer += buffer[0];
905 if (buflen)
906 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
907 "%d has %u bytes of trailing descriptor garbage.\n",
908 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
910 /* Parse the alternate settings to find the maximum bandwidth. */
911 for (i = 0; i < intf->num_altsetting; ++i) {
912 struct usb_host_endpoint *ep;
913 alts = &intf->altsetting[i];
914 ep = uvc_find_endpoint(alts,
915 streaming->header.bEndpointAddress);
916 if (ep == NULL)
917 continue;
919 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
920 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
921 if (psize > streaming->maxpsize)
922 streaming->maxpsize = psize;
925 list_add_tail(&streaming->list, &dev->streams);
926 return 0;
928 error:
929 usb_driver_release_interface(&uvc_driver.driver, intf);
930 uvc_stream_delete(streaming);
931 return ret;
934 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
935 unsigned int num_pads, unsigned int extra_size)
937 struct uvc_entity *entity;
938 unsigned int num_inputs;
939 unsigned int size;
940 unsigned int i;
942 extra_size = roundup(extra_size, sizeof(*entity->pads));
943 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
944 size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
945 + num_inputs;
946 entity = kzalloc(size, GFP_KERNEL);
947 if (entity == NULL)
948 return NULL;
950 entity->id = id;
951 entity->type = type;
953 entity->num_links = 0;
954 entity->num_pads = num_pads;
955 entity->pads = ((void *)(entity + 1)) + extra_size;
957 for (i = 0; i < num_inputs; ++i)
958 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
959 if (!UVC_ENTITY_IS_OTERM(entity))
960 entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
962 entity->bNrInPins = num_inputs;
963 entity->baSourceID = (u8 *)(&entity->pads[num_pads]);
965 return entity;
968 /* Parse vendor-specific extensions. */
969 static int uvc_parse_vendor_control(struct uvc_device *dev,
970 const unsigned char *buffer, int buflen)
972 struct usb_device *udev = dev->udev;
973 struct usb_host_interface *alts = dev->intf->cur_altsetting;
974 struct uvc_entity *unit;
975 unsigned int n, p;
976 int handled = 0;
978 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
979 case 0x046d: /* Logitech */
980 if (buffer[1] != 0x41 || buffer[2] != 0x01)
981 break;
983 /* Logitech implements several vendor specific functions
984 * through vendor specific extension units (LXU).
986 * The LXU descriptors are similar to XU descriptors
987 * (see "USB Device Video Class for Video Devices", section
988 * 3.7.2.6 "Extension Unit Descriptor") with the following
989 * differences:
991 * ----------------------------------------------------------
992 * 0 bLength 1 Number
993 * Size of this descriptor, in bytes: 24+p+n*2
994 * ----------------------------------------------------------
995 * 23+p+n bmControlsType N Bitmap
996 * Individual bits in the set are defined:
997 * 0: Absolute
998 * 1: Relative
1000 * This bitset is mapped exactly the same as bmControls.
1001 * ----------------------------------------------------------
1002 * 23+p+n*2 bReserved 1 Boolean
1003 * ----------------------------------------------------------
1004 * 24+p+n*2 iExtension 1 Index
1005 * Index of a string descriptor that describes this
1006 * extension unit.
1007 * ----------------------------------------------------------
1009 p = buflen >= 22 ? buffer[21] : 0;
1010 n = buflen >= 25 + p ? buffer[22+p] : 0;
1012 if (buflen < 25 + p + 2*n) {
1013 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1014 "interface %d EXTENSION_UNIT error\n",
1015 udev->devnum, alts->desc.bInterfaceNumber);
1016 break;
1019 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
1020 p + 1, 2*n);
1021 if (unit == NULL)
1022 return -ENOMEM;
1024 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1025 unit->extension.bNumControls = buffer[20];
1026 memcpy(unit->baSourceID, &buffer[22], p);
1027 unit->extension.bControlSize = buffer[22+p];
1028 unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
1029 unit->extension.bmControlsType = (u8 *)unit + sizeof(*unit)
1030 + n;
1031 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
1033 if (buffer[24+p+2*n] != 0)
1034 usb_string(udev, buffer[24+p+2*n], unit->name,
1035 sizeof(unit->name));
1036 else
1037 sprintf(unit->name, "Extension %u", buffer[3]);
1039 list_add_tail(&unit->list, &dev->entities);
1040 handled = 1;
1041 break;
1044 return handled;
1047 static int uvc_parse_standard_control(struct uvc_device *dev,
1048 const unsigned char *buffer, int buflen)
1050 struct usb_device *udev = dev->udev;
1051 struct uvc_entity *unit, *term;
1052 struct usb_interface *intf;
1053 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1054 unsigned int i, n, p, len;
1055 u16 type;
1057 switch (buffer[2]) {
1058 case UVC_VC_HEADER:
1059 n = buflen >= 12 ? buffer[11] : 0;
1061 if (buflen < 12 + n) {
1062 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1063 "interface %d HEADER error\n", udev->devnum,
1064 alts->desc.bInterfaceNumber);
1065 return -EINVAL;
1068 dev->uvc_version = get_unaligned_le16(&buffer[3]);
1069 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
1071 /* Parse all USB Video Streaming interfaces. */
1072 for (i = 0; i < n; ++i) {
1073 intf = usb_ifnum_to_if(udev, buffer[12+i]);
1074 if (intf == NULL) {
1075 uvc_trace(UVC_TRACE_DESCR, "device %d "
1076 "interface %d doesn't exists\n",
1077 udev->devnum, i);
1078 continue;
1081 uvc_parse_streaming(dev, intf);
1083 break;
1085 case UVC_VC_INPUT_TERMINAL:
1086 if (buflen < 8) {
1087 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1088 "interface %d INPUT_TERMINAL error\n",
1089 udev->devnum, alts->desc.bInterfaceNumber);
1090 return -EINVAL;
1094 * Reject invalid terminal types that would cause issues:
1096 * - The high byte must be non-zero, otherwise it would be
1097 * confused with a unit.
1099 * - Bit 15 must be 0, as we use it internally as a terminal
1100 * direction flag.
1102 * Other unknown types are accepted.
1104 type = get_unaligned_le16(&buffer[4]);
1105 if ((type & 0x7f00) == 0 || (type & 0x8000) != 0) {
1106 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1107 "interface %d INPUT_TERMINAL %d has invalid "
1108 "type 0x%04x, skipping\n", udev->devnum,
1109 alts->desc.bInterfaceNumber,
1110 buffer[3], type);
1111 return 0;
1114 n = 0;
1115 p = 0;
1116 len = 8;
1118 if (type == UVC_ITT_CAMERA) {
1119 n = buflen >= 15 ? buffer[14] : 0;
1120 len = 15;
1122 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
1123 n = buflen >= 9 ? buffer[8] : 0;
1124 p = buflen >= 10 + n ? buffer[9+n] : 0;
1125 len = 10;
1128 if (buflen < len + n + p) {
1129 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1130 "interface %d INPUT_TERMINAL error\n",
1131 udev->devnum, alts->desc.bInterfaceNumber);
1132 return -EINVAL;
1135 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
1136 1, n + p);
1137 if (term == NULL)
1138 return -ENOMEM;
1140 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
1141 term->camera.bControlSize = n;
1142 term->camera.bmControls = (u8 *)term + sizeof(*term);
1143 term->camera.wObjectiveFocalLengthMin =
1144 get_unaligned_le16(&buffer[8]);
1145 term->camera.wObjectiveFocalLengthMax =
1146 get_unaligned_le16(&buffer[10]);
1147 term->camera.wOcularFocalLength =
1148 get_unaligned_le16(&buffer[12]);
1149 memcpy(term->camera.bmControls, &buffer[15], n);
1150 } else if (UVC_ENTITY_TYPE(term) ==
1151 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
1152 term->media.bControlSize = n;
1153 term->media.bmControls = (u8 *)term + sizeof(*term);
1154 term->media.bTransportModeSize = p;
1155 term->media.bmTransportModes = (u8 *)term
1156 + sizeof(*term) + n;
1157 memcpy(term->media.bmControls, &buffer[9], n);
1158 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
1161 if (buffer[7] != 0)
1162 usb_string(udev, buffer[7], term->name,
1163 sizeof(term->name));
1164 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
1165 sprintf(term->name, "Camera %u", buffer[3]);
1166 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
1167 sprintf(term->name, "Media %u", buffer[3]);
1168 else
1169 sprintf(term->name, "Input %u", buffer[3]);
1171 list_add_tail(&term->list, &dev->entities);
1172 break;
1174 case UVC_VC_OUTPUT_TERMINAL:
1175 if (buflen < 9) {
1176 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1177 "interface %d OUTPUT_TERMINAL error\n",
1178 udev->devnum, alts->desc.bInterfaceNumber);
1179 return -EINVAL;
1182 /* Make sure the terminal type MSB is not null, otherwise it
1183 * could be confused with a unit.
1185 type = get_unaligned_le16(&buffer[4]);
1186 if ((type & 0xff00) == 0) {
1187 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1188 "interface %d OUTPUT_TERMINAL %d has invalid "
1189 "type 0x%04x, skipping\n", udev->devnum,
1190 alts->desc.bInterfaceNumber, buffer[3], type);
1191 return 0;
1194 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1195 1, 0);
1196 if (term == NULL)
1197 return -ENOMEM;
1199 memcpy(term->baSourceID, &buffer[7], 1);
1201 if (buffer[8] != 0)
1202 usb_string(udev, buffer[8], term->name,
1203 sizeof(term->name));
1204 else
1205 sprintf(term->name, "Output %u", buffer[3]);
1207 list_add_tail(&term->list, &dev->entities);
1208 break;
1210 case UVC_VC_SELECTOR_UNIT:
1211 p = buflen >= 5 ? buffer[4] : 0;
1213 if (buflen < 5 || buflen < 6 + p) {
1214 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1215 "interface %d SELECTOR_UNIT error\n",
1216 udev->devnum, alts->desc.bInterfaceNumber);
1217 return -EINVAL;
1220 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1221 if (unit == NULL)
1222 return -ENOMEM;
1224 memcpy(unit->baSourceID, &buffer[5], p);
1226 if (buffer[5+p] != 0)
1227 usb_string(udev, buffer[5+p], unit->name,
1228 sizeof(unit->name));
1229 else
1230 sprintf(unit->name, "Selector %u", buffer[3]);
1232 list_add_tail(&unit->list, &dev->entities);
1233 break;
1235 case UVC_VC_PROCESSING_UNIT:
1236 n = buflen >= 8 ? buffer[7] : 0;
1237 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1239 if (buflen < p + n) {
1240 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1241 "interface %d PROCESSING_UNIT error\n",
1242 udev->devnum, alts->desc.bInterfaceNumber);
1243 return -EINVAL;
1246 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1247 if (unit == NULL)
1248 return -ENOMEM;
1250 memcpy(unit->baSourceID, &buffer[4], 1);
1251 unit->processing.wMaxMultiplier =
1252 get_unaligned_le16(&buffer[5]);
1253 unit->processing.bControlSize = buffer[7];
1254 unit->processing.bmControls = (u8 *)unit + sizeof(*unit);
1255 memcpy(unit->processing.bmControls, &buffer[8], n);
1256 if (dev->uvc_version >= 0x0110)
1257 unit->processing.bmVideoStandards = buffer[9+n];
1259 if (buffer[8+n] != 0)
1260 usb_string(udev, buffer[8+n], unit->name,
1261 sizeof(unit->name));
1262 else
1263 sprintf(unit->name, "Processing %u", buffer[3]);
1265 list_add_tail(&unit->list, &dev->entities);
1266 break;
1268 case UVC_VC_EXTENSION_UNIT:
1269 p = buflen >= 22 ? buffer[21] : 0;
1270 n = buflen >= 24 + p ? buffer[22+p] : 0;
1272 if (buflen < 24 + p + n) {
1273 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1274 "interface %d EXTENSION_UNIT error\n",
1275 udev->devnum, alts->desc.bInterfaceNumber);
1276 return -EINVAL;
1279 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1280 if (unit == NULL)
1281 return -ENOMEM;
1283 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1284 unit->extension.bNumControls = buffer[20];
1285 memcpy(unit->baSourceID, &buffer[22], p);
1286 unit->extension.bControlSize = buffer[22+p];
1287 unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
1288 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1290 if (buffer[23+p+n] != 0)
1291 usb_string(udev, buffer[23+p+n], unit->name,
1292 sizeof(unit->name));
1293 else
1294 sprintf(unit->name, "Extension %u", buffer[3]);
1296 list_add_tail(&unit->list, &dev->entities);
1297 break;
1299 default:
1300 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1301 "descriptor (%u)\n", buffer[2]);
1302 break;
1305 return 0;
1308 static int uvc_parse_control(struct uvc_device *dev)
1310 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1311 unsigned char *buffer = alts->extra;
1312 int buflen = alts->extralen;
1313 int ret;
1315 /* Parse the default alternate setting only, as the UVC specification
1316 * defines a single alternate setting, the default alternate setting
1317 * zero.
1320 while (buflen > 2) {
1321 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1322 buffer[1] != USB_DT_CS_INTERFACE)
1323 goto next_descriptor;
1325 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1326 return ret;
1328 next_descriptor:
1329 buflen -= buffer[0];
1330 buffer += buffer[0];
1333 /* Check if the optional status endpoint is present. Built-in iSight
1334 * webcams have an interrupt endpoint but spit proprietary data that
1335 * don't conform to the UVC status endpoint messages. Don't try to
1336 * handle the interrupt endpoint for those cameras.
1338 if (alts->desc.bNumEndpoints == 1 &&
1339 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1340 struct usb_host_endpoint *ep = &alts->endpoint[0];
1341 struct usb_endpoint_descriptor *desc = &ep->desc;
1343 if (usb_endpoint_is_int_in(desc) &&
1344 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1345 desc->bInterval != 0) {
1346 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1347 "(addr %02x).\n", desc->bEndpointAddress);
1348 dev->int_ep = ep;
1352 return 0;
1355 /* ------------------------------------------------------------------------
1356 * UVC device scan
1360 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1361 * and containing the following units:
1363 * - one or more Output Terminals (USB Streaming or Display)
1364 * - zero or one Processing Unit
1365 * - zero, one or more single-input Selector Units
1366 * - zero or one multiple-input Selector Units, provided all inputs are
1367 * connected to input terminals
1368 * - zero, one or mode single-input Extension Units
1369 * - one or more Input Terminals (Camera, External or USB Streaming)
1371 * The terminal and units must match on of the following structures:
1373 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1374 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1375 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1377 * +---------+ +---------+ -> OTT_*(0)
1378 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1379 * +---------+ +---------+ -> OTT_*(n)
1381 * The Processing Unit and Extension Units can be in any order. Additional
1382 * Extension Units connected to the main chain as single-unit branches are
1383 * also supported. Single-input Selector Units are ignored.
1385 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1386 struct uvc_entity *entity)
1388 switch (UVC_ENTITY_TYPE(entity)) {
1389 case UVC_VC_EXTENSION_UNIT:
1390 if (uvc_trace_param & UVC_TRACE_PROBE)
1391 printk(KERN_CONT " <- XU %d", entity->id);
1393 if (entity->bNrInPins != 1) {
1394 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1395 "than 1 input pin.\n", entity->id);
1396 return -1;
1399 break;
1401 case UVC_VC_PROCESSING_UNIT:
1402 if (uvc_trace_param & UVC_TRACE_PROBE)
1403 printk(KERN_CONT " <- PU %d", entity->id);
1405 if (chain->processing != NULL) {
1406 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1407 "Processing Units in chain.\n");
1408 return -1;
1411 chain->processing = entity;
1412 break;
1414 case UVC_VC_SELECTOR_UNIT:
1415 if (uvc_trace_param & UVC_TRACE_PROBE)
1416 printk(KERN_CONT " <- SU %d", entity->id);
1418 /* Single-input selector units are ignored. */
1419 if (entity->bNrInPins == 1)
1420 break;
1422 if (chain->selector != NULL) {
1423 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1424 "Units in chain.\n");
1425 return -1;
1428 chain->selector = entity;
1429 break;
1431 case UVC_ITT_VENDOR_SPECIFIC:
1432 case UVC_ITT_CAMERA:
1433 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1434 if (uvc_trace_param & UVC_TRACE_PROBE)
1435 printk(KERN_CONT " <- IT %d\n", entity->id);
1437 break;
1439 case UVC_OTT_VENDOR_SPECIFIC:
1440 case UVC_OTT_DISPLAY:
1441 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1442 if (uvc_trace_param & UVC_TRACE_PROBE)
1443 printk(KERN_CONT " OT %d", entity->id);
1445 break;
1447 case UVC_TT_STREAMING:
1448 if (UVC_ENTITY_IS_ITERM(entity)) {
1449 if (uvc_trace_param & UVC_TRACE_PROBE)
1450 printk(KERN_CONT " <- IT %d\n", entity->id);
1451 } else {
1452 if (uvc_trace_param & UVC_TRACE_PROBE)
1453 printk(KERN_CONT " OT %d", entity->id);
1456 break;
1458 default:
1459 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1460 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1461 return -1;
1464 list_add_tail(&entity->chain, &chain->entities);
1465 return 0;
1468 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1469 struct uvc_entity *entity, struct uvc_entity *prev)
1471 struct uvc_entity *forward;
1472 int found;
1474 /* Forward scan */
1475 forward = NULL;
1476 found = 0;
1478 while (1) {
1479 forward = uvc_entity_by_reference(chain->dev, entity->id,
1480 forward);
1481 if (forward == NULL)
1482 break;
1483 if (forward == prev)
1484 continue;
1485 if (forward->chain.next || forward->chain.prev) {
1486 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1487 "entity %d already in chain.\n", forward->id);
1488 return -EINVAL;
1491 switch (UVC_ENTITY_TYPE(forward)) {
1492 case UVC_VC_EXTENSION_UNIT:
1493 if (forward->bNrInPins != 1) {
1494 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1495 "has more than 1 input pin.\n",
1496 entity->id);
1497 return -EINVAL;
1500 list_add_tail(&forward->chain, &chain->entities);
1501 if (uvc_trace_param & UVC_TRACE_PROBE) {
1502 if (!found)
1503 printk(KERN_CONT " (->");
1505 printk(KERN_CONT " XU %d", forward->id);
1506 found = 1;
1508 break;
1510 case UVC_OTT_VENDOR_SPECIFIC:
1511 case UVC_OTT_DISPLAY:
1512 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1513 case UVC_TT_STREAMING:
1514 if (UVC_ENTITY_IS_ITERM(forward)) {
1515 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1516 "terminal %u.\n", forward->id);
1517 return -EINVAL;
1520 list_add_tail(&forward->chain, &chain->entities);
1521 if (uvc_trace_param & UVC_TRACE_PROBE) {
1522 if (!found)
1523 printk(KERN_CONT " (->");
1525 printk(KERN_CONT " OT %d", forward->id);
1526 found = 1;
1528 break;
1531 if (found)
1532 printk(KERN_CONT ")");
1534 return 0;
1537 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1538 struct uvc_entity **_entity)
1540 struct uvc_entity *entity = *_entity;
1541 struct uvc_entity *term;
1542 int id = -EINVAL, i;
1544 switch (UVC_ENTITY_TYPE(entity)) {
1545 case UVC_VC_EXTENSION_UNIT:
1546 case UVC_VC_PROCESSING_UNIT:
1547 id = entity->baSourceID[0];
1548 break;
1550 case UVC_VC_SELECTOR_UNIT:
1551 /* Single-input selector units are ignored. */
1552 if (entity->bNrInPins == 1) {
1553 id = entity->baSourceID[0];
1554 break;
1557 if (uvc_trace_param & UVC_TRACE_PROBE)
1558 printk(KERN_CONT " <- IT");
1560 chain->selector = entity;
1561 for (i = 0; i < entity->bNrInPins; ++i) {
1562 id = entity->baSourceID[i];
1563 term = uvc_entity_by_id(chain->dev, id);
1564 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1565 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1566 "input %d isn't connected to an "
1567 "input terminal\n", entity->id, i);
1568 return -1;
1571 if (term->chain.next || term->chain.prev) {
1572 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1573 "entity %d already in chain.\n",
1574 term->id);
1575 return -EINVAL;
1578 if (uvc_trace_param & UVC_TRACE_PROBE)
1579 printk(KERN_CONT " %d", term->id);
1581 list_add_tail(&term->chain, &chain->entities);
1582 uvc_scan_chain_forward(chain, term, entity);
1585 if (uvc_trace_param & UVC_TRACE_PROBE)
1586 printk(KERN_CONT "\n");
1588 id = 0;
1589 break;
1591 case UVC_ITT_VENDOR_SPECIFIC:
1592 case UVC_ITT_CAMERA:
1593 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1594 case UVC_OTT_VENDOR_SPECIFIC:
1595 case UVC_OTT_DISPLAY:
1596 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1597 case UVC_TT_STREAMING:
1598 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1599 break;
1602 if (id <= 0) {
1603 *_entity = NULL;
1604 return id;
1607 entity = uvc_entity_by_id(chain->dev, id);
1608 if (entity == NULL) {
1609 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1610 "unknown entity %d.\n", id);
1611 return -EINVAL;
1614 *_entity = entity;
1615 return 0;
1618 static int uvc_scan_chain(struct uvc_video_chain *chain,
1619 struct uvc_entity *term)
1621 struct uvc_entity *entity, *prev;
1623 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1625 entity = term;
1626 prev = NULL;
1628 while (entity != NULL) {
1629 /* Entity must not be part of an existing chain */
1630 if (entity->chain.next || entity->chain.prev) {
1631 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1632 "entity %d already in chain.\n", entity->id);
1633 return -EINVAL;
1636 /* Process entity */
1637 if (uvc_scan_chain_entity(chain, entity) < 0)
1638 return -EINVAL;
1640 /* Forward scan */
1641 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1642 return -EINVAL;
1644 /* Backward scan */
1645 prev = entity;
1646 if (uvc_scan_chain_backward(chain, &entity) < 0)
1647 return -EINVAL;
1650 return 0;
1653 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1654 char *buffer)
1656 struct uvc_entity *term;
1657 unsigned int nterms = 0;
1658 char *p = buffer;
1660 list_for_each_entry(term, terms, chain) {
1661 if (!UVC_ENTITY_IS_TERM(term) ||
1662 UVC_TERM_DIRECTION(term) != dir)
1663 continue;
1665 if (nterms)
1666 p += sprintf(p, ",");
1667 if (++nterms >= 4) {
1668 p += sprintf(p, "...");
1669 break;
1671 p += sprintf(p, "%u", term->id);
1674 return p - buffer;
1677 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1679 static char buffer[43];
1680 char *p = buffer;
1682 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1683 p += sprintf(p, " -> ");
1684 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1686 return buffer;
1689 static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
1691 struct uvc_video_chain *chain;
1693 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1694 if (chain == NULL)
1695 return NULL;
1697 INIT_LIST_HEAD(&chain->entities);
1698 mutex_init(&chain->ctrl_mutex);
1699 chain->dev = dev;
1700 v4l2_prio_init(&chain->prio);
1702 return chain;
1706 * Fallback heuristic for devices that don't connect units and terminals in a
1707 * valid chain.
1709 * Some devices have invalid baSourceID references, causing uvc_scan_chain()
1710 * to fail, but if we just take the entities we can find and put them together
1711 * in the most sensible chain we can think of, turns out they do work anyway.
1712 * Note: This heuristic assumes there is a single chain.
1714 * At the time of writing, devices known to have such a broken chain are
1715 * - Acer Integrated Camera (5986:055a)
1716 * - Realtek rtl157a7 (0bda:57a7)
1718 static int uvc_scan_fallback(struct uvc_device *dev)
1720 struct uvc_video_chain *chain;
1721 struct uvc_entity *iterm = NULL;
1722 struct uvc_entity *oterm = NULL;
1723 struct uvc_entity *entity;
1724 struct uvc_entity *prev;
1727 * Start by locating the input and output terminals. We only support
1728 * devices with exactly one of each for now.
1730 list_for_each_entry(entity, &dev->entities, list) {
1731 if (UVC_ENTITY_IS_ITERM(entity)) {
1732 if (iterm)
1733 return -EINVAL;
1734 iterm = entity;
1737 if (UVC_ENTITY_IS_OTERM(entity)) {
1738 if (oterm)
1739 return -EINVAL;
1740 oterm = entity;
1744 if (iterm == NULL || oterm == NULL)
1745 return -EINVAL;
1747 /* Allocate the chain and fill it. */
1748 chain = uvc_alloc_chain(dev);
1749 if (chain == NULL)
1750 return -ENOMEM;
1752 if (uvc_scan_chain_entity(chain, oterm) < 0)
1753 goto error;
1755 prev = oterm;
1758 * Add all Processing and Extension Units with two pads. The order
1759 * doesn't matter much, use reverse list traversal to connect units in
1760 * UVC descriptor order as we build the chain from output to input. This
1761 * leads to units appearing in the order meant by the manufacturer for
1762 * the cameras known to require this heuristic.
1764 list_for_each_entry_reverse(entity, &dev->entities, list) {
1765 if (entity->type != UVC_VC_PROCESSING_UNIT &&
1766 entity->type != UVC_VC_EXTENSION_UNIT)
1767 continue;
1769 if (entity->num_pads != 2)
1770 continue;
1772 if (uvc_scan_chain_entity(chain, entity) < 0)
1773 goto error;
1775 prev->baSourceID[0] = entity->id;
1776 prev = entity;
1779 if (uvc_scan_chain_entity(chain, iterm) < 0)
1780 goto error;
1782 prev->baSourceID[0] = iterm->id;
1784 list_add_tail(&chain->list, &dev->chains);
1786 uvc_trace(UVC_TRACE_PROBE,
1787 "Found a video chain by fallback heuristic (%s).\n",
1788 uvc_print_chain(chain));
1790 return 0;
1792 error:
1793 kfree(chain);
1794 return -EINVAL;
1798 * Scan the device for video chains and register video devices.
1800 * Chains are scanned starting at their output terminals and walked backwards.
1802 static int uvc_scan_device(struct uvc_device *dev)
1804 struct uvc_video_chain *chain;
1805 struct uvc_entity *term;
1807 list_for_each_entry(term, &dev->entities, list) {
1808 if (!UVC_ENTITY_IS_OTERM(term))
1809 continue;
1811 /* If the terminal is already included in a chain, skip it.
1812 * This can happen for chains that have multiple output
1813 * terminals, where all output terminals beside the first one
1814 * will be inserted in the chain in forward scans.
1816 if (term->chain.next || term->chain.prev)
1817 continue;
1819 chain = uvc_alloc_chain(dev);
1820 if (chain == NULL)
1821 return -ENOMEM;
1823 term->flags |= UVC_ENTITY_FLAG_DEFAULT;
1825 if (uvc_scan_chain(chain, term) < 0) {
1826 kfree(chain);
1827 continue;
1830 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1831 uvc_print_chain(chain));
1833 list_add_tail(&chain->list, &dev->chains);
1836 if (list_empty(&dev->chains))
1837 uvc_scan_fallback(dev);
1839 if (list_empty(&dev->chains)) {
1840 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1841 return -1;
1844 return 0;
1847 /* ------------------------------------------------------------------------
1848 * Video device registration and unregistration
1852 * Delete the UVC device.
1854 * Called by the kernel when the last reference to the uvc_device structure
1855 * is released.
1857 * As this function is called after or during disconnect(), all URBs have
1858 * already been cancelled by the USB core. There is no need to kill the
1859 * interrupt URB manually.
1861 static void uvc_delete(struct kref *kref)
1863 struct uvc_device *dev = container_of(kref, struct uvc_device, ref);
1864 struct list_head *p, *n;
1866 uvc_status_cleanup(dev);
1867 uvc_ctrl_cleanup_device(dev);
1869 usb_put_intf(dev->intf);
1870 usb_put_dev(dev->udev);
1872 #ifdef CONFIG_MEDIA_CONTROLLER
1873 media_device_cleanup(&dev->mdev);
1874 #endif
1876 list_for_each_safe(p, n, &dev->chains) {
1877 struct uvc_video_chain *chain;
1878 chain = list_entry(p, struct uvc_video_chain, list);
1879 kfree(chain);
1882 list_for_each_safe(p, n, &dev->entities) {
1883 struct uvc_entity *entity;
1884 entity = list_entry(p, struct uvc_entity, list);
1885 #ifdef CONFIG_MEDIA_CONTROLLER
1886 uvc_mc_cleanup_entity(entity);
1887 #endif
1888 kfree(entity);
1891 list_for_each_safe(p, n, &dev->streams) {
1892 struct uvc_streaming *streaming;
1893 streaming = list_entry(p, struct uvc_streaming, list);
1894 usb_driver_release_interface(&uvc_driver.driver,
1895 streaming->intf);
1896 uvc_stream_delete(streaming);
1899 kfree(dev);
1902 static void uvc_release(struct video_device *vdev)
1904 struct uvc_streaming *stream = video_get_drvdata(vdev);
1905 struct uvc_device *dev = stream->dev;
1907 kref_put(&dev->ref, uvc_delete);
1911 * Unregister the video devices.
1913 static void uvc_unregister_video(struct uvc_device *dev)
1915 struct uvc_streaming *stream;
1917 list_for_each_entry(stream, &dev->streams, list) {
1918 if (!video_is_registered(&stream->vdev))
1919 continue;
1921 video_unregister_device(&stream->vdev);
1922 video_unregister_device(&stream->meta.vdev);
1924 uvc_debugfs_cleanup_stream(stream);
1927 uvc_status_unregister(dev);
1929 if (dev->vdev.dev)
1930 v4l2_device_unregister(&dev->vdev);
1931 #ifdef CONFIG_MEDIA_CONTROLLER
1932 if (media_devnode_is_registered(dev->mdev.devnode))
1933 media_device_unregister(&dev->mdev);
1934 #endif
1937 int uvc_register_video_device(struct uvc_device *dev,
1938 struct uvc_streaming *stream,
1939 struct video_device *vdev,
1940 struct uvc_video_queue *queue,
1941 enum v4l2_buf_type type,
1942 const struct v4l2_file_operations *fops,
1943 const struct v4l2_ioctl_ops *ioctl_ops)
1945 int ret;
1947 /* Initialize the video buffers queue. */
1948 ret = uvc_queue_init(queue, type, !uvc_no_drop_param);
1949 if (ret)
1950 return ret;
1952 /* Register the device with V4L. */
1955 * We already hold a reference to dev->udev. The video device will be
1956 * unregistered before the reference is released, so we don't need to
1957 * get another one.
1959 vdev->v4l2_dev = &dev->vdev;
1960 vdev->fops = fops;
1961 vdev->ioctl_ops = ioctl_ops;
1962 vdev->release = uvc_release;
1963 vdev->prio = &stream->chain->prio;
1964 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1965 vdev->vfl_dir = VFL_DIR_TX;
1966 else
1967 vdev->vfl_dir = VFL_DIR_RX;
1969 switch (type) {
1970 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1971 default:
1972 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1973 break;
1974 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1975 vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
1976 break;
1977 case V4L2_BUF_TYPE_META_CAPTURE:
1978 vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
1979 break;
1982 strlcpy(vdev->name, dev->name, sizeof(vdev->name));
1985 * Set the driver data before calling video_register_device, otherwise
1986 * the file open() handler might race us.
1988 video_set_drvdata(vdev, stream);
1990 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1991 if (ret < 0) {
1992 uvc_printk(KERN_ERR, "Failed to register %s device (%d).\n",
1993 v4l2_type_names[type], ret);
1994 return ret;
1997 kref_get(&dev->ref);
1998 return 0;
2001 static int uvc_register_video(struct uvc_device *dev,
2002 struct uvc_streaming *stream)
2004 int ret;
2006 /* Initialize the streaming interface with default parameters. */
2007 ret = uvc_video_init(stream);
2008 if (ret < 0) {
2009 uvc_printk(KERN_ERR, "Failed to initialize the device (%d).\n",
2010 ret);
2011 return ret;
2014 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2015 stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE
2016 | V4L2_CAP_META_CAPTURE;
2017 else
2018 stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
2020 uvc_debugfs_init_stream(stream);
2022 /* Register the device with V4L. */
2023 return uvc_register_video_device(dev, stream, &stream->vdev,
2024 &stream->queue, stream->type,
2025 &uvc_fops, &uvc_ioctl_ops);
2029 * Register all video devices in all chains.
2031 static int uvc_register_terms(struct uvc_device *dev,
2032 struct uvc_video_chain *chain)
2034 struct uvc_streaming *stream;
2035 struct uvc_entity *term;
2036 int ret;
2038 list_for_each_entry(term, &chain->entities, chain) {
2039 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
2040 continue;
2042 stream = uvc_stream_by_id(dev, term->id);
2043 if (stream == NULL) {
2044 uvc_printk(KERN_INFO, "No streaming interface found "
2045 "for terminal %u.", term->id);
2046 continue;
2049 stream->chain = chain;
2050 ret = uvc_register_video(dev, stream);
2051 if (ret < 0)
2052 return ret;
2054 /* Register a metadata node, but ignore a possible failure,
2055 * complete registration of video nodes anyway.
2057 uvc_meta_register(stream);
2059 term->vdev = &stream->vdev;
2062 return 0;
2065 static int uvc_register_chains(struct uvc_device *dev)
2067 struct uvc_video_chain *chain;
2068 int ret;
2070 list_for_each_entry(chain, &dev->chains, list) {
2071 ret = uvc_register_terms(dev, chain);
2072 if (ret < 0)
2073 return ret;
2075 #ifdef CONFIG_MEDIA_CONTROLLER
2076 ret = uvc_mc_register_entities(chain);
2077 if (ret < 0) {
2078 uvc_printk(KERN_INFO, "Failed to register entites "
2079 "(%d).\n", ret);
2081 #endif
2084 return 0;
2087 /* ------------------------------------------------------------------------
2088 * USB probe, disconnect, suspend and resume
2091 struct uvc_device_info {
2092 u32 quirks;
2093 u32 meta_format;
2096 static int uvc_probe(struct usb_interface *intf,
2097 const struct usb_device_id *id)
2099 struct usb_device *udev = interface_to_usbdev(intf);
2100 struct uvc_device *dev;
2101 const struct uvc_device_info *info =
2102 (const struct uvc_device_info *)id->driver_info;
2103 u32 quirks = info ? info->quirks : 0;
2104 int function;
2105 int ret;
2107 if (id->idVendor && id->idProduct)
2108 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
2109 "(%04x:%04x)\n", udev->devpath, id->idVendor,
2110 id->idProduct);
2111 else
2112 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
2113 udev->devpath);
2115 /* Allocate memory for the device and initialize it. */
2116 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2117 if (dev == NULL)
2118 return -ENOMEM;
2120 INIT_LIST_HEAD(&dev->entities);
2121 INIT_LIST_HEAD(&dev->chains);
2122 INIT_LIST_HEAD(&dev->streams);
2123 kref_init(&dev->ref);
2124 atomic_set(&dev->nmappings, 0);
2125 mutex_init(&dev->lock);
2127 dev->udev = usb_get_dev(udev);
2128 dev->intf = usb_get_intf(intf);
2129 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
2130 dev->quirks = (uvc_quirks_param == -1)
2131 ? quirks : uvc_quirks_param;
2132 if (info)
2133 dev->meta_format = info->meta_format;
2135 if (udev->product != NULL)
2136 strlcpy(dev->name, udev->product, sizeof(dev->name));
2137 else
2138 snprintf(dev->name, sizeof(dev->name),
2139 "UVC Camera (%04x:%04x)",
2140 le16_to_cpu(udev->descriptor.idVendor),
2141 le16_to_cpu(udev->descriptor.idProduct));
2144 * Add iFunction or iInterface to names when available as additional
2145 * distinguishers between interfaces. iFunction is prioritized over
2146 * iInterface which matches Windows behavior at the point of writing.
2148 if (intf->intf_assoc && intf->intf_assoc->iFunction != 0)
2149 function = intf->intf_assoc->iFunction;
2150 else
2151 function = intf->cur_altsetting->desc.iInterface;
2152 if (function != 0) {
2153 size_t len;
2155 strlcat(dev->name, ": ", sizeof(dev->name));
2156 len = strlen(dev->name);
2157 usb_string(udev, function, dev->name + len,
2158 sizeof(dev->name) - len);
2161 /* Initialize the media device. */
2162 #ifdef CONFIG_MEDIA_CONTROLLER
2163 dev->mdev.dev = &intf->dev;
2164 strscpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
2165 if (udev->serial)
2166 strscpy(dev->mdev.serial, udev->serial,
2167 sizeof(dev->mdev.serial));
2168 usb_make_path(udev, dev->mdev.bus_info, sizeof(dev->mdev.bus_info));
2169 dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
2170 media_device_init(&dev->mdev);
2172 dev->vdev.mdev = &dev->mdev;
2173 #endif
2175 /* Parse the Video Class control descriptor. */
2176 if (uvc_parse_control(dev) < 0) {
2177 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
2178 "descriptors.\n");
2179 goto error;
2182 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
2183 dev->uvc_version >> 8, dev->uvc_version & 0xff,
2184 udev->product ? udev->product : "<unnamed>",
2185 le16_to_cpu(udev->descriptor.idVendor),
2186 le16_to_cpu(udev->descriptor.idProduct));
2188 if (dev->quirks != quirks) {
2189 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
2190 "parameter for testing purpose.\n", dev->quirks);
2191 uvc_printk(KERN_INFO, "Please report required quirks to the "
2192 "linux-uvc-devel mailing list.\n");
2195 /* Register the V4L2 device. */
2196 if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
2197 goto error;
2199 /* Initialize controls. */
2200 if (uvc_ctrl_init_device(dev) < 0)
2201 goto error;
2203 /* Scan the device for video chains. */
2204 if (uvc_scan_device(dev) < 0)
2205 goto error;
2207 /* Register video device nodes. */
2208 if (uvc_register_chains(dev) < 0)
2209 goto error;
2211 #ifdef CONFIG_MEDIA_CONTROLLER
2212 /* Register the media device node */
2213 if (media_device_register(&dev->mdev) < 0)
2214 goto error;
2215 #endif
2216 /* Save our data pointer in the interface data. */
2217 usb_set_intfdata(intf, dev);
2219 /* Initialize the interrupt URB. */
2220 if ((ret = uvc_status_init(dev)) < 0) {
2221 uvc_printk(KERN_INFO, "Unable to initialize the status "
2222 "endpoint (%d), status interrupt will not be "
2223 "supported.\n", ret);
2226 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
2227 usb_enable_autosuspend(udev);
2228 return 0;
2230 error:
2231 uvc_unregister_video(dev);
2232 kref_put(&dev->ref, uvc_delete);
2233 return -ENODEV;
2236 static void uvc_disconnect(struct usb_interface *intf)
2238 struct uvc_device *dev = usb_get_intfdata(intf);
2240 /* Set the USB interface data to NULL. This can be done outside the
2241 * lock, as there's no other reader.
2243 usb_set_intfdata(intf, NULL);
2245 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2246 UVC_SC_VIDEOSTREAMING)
2247 return;
2249 uvc_unregister_video(dev);
2250 kref_put(&dev->ref, uvc_delete);
2253 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
2255 struct uvc_device *dev = usb_get_intfdata(intf);
2256 struct uvc_streaming *stream;
2258 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
2259 intf->cur_altsetting->desc.bInterfaceNumber);
2261 /* Controls are cached on the fly so they don't need to be saved. */
2262 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2263 UVC_SC_VIDEOCONTROL) {
2264 mutex_lock(&dev->lock);
2265 if (dev->users)
2266 uvc_status_stop(dev);
2267 mutex_unlock(&dev->lock);
2268 return 0;
2271 list_for_each_entry(stream, &dev->streams, list) {
2272 if (stream->intf == intf)
2273 return uvc_video_suspend(stream);
2276 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
2277 "mismatch.\n");
2278 return -EINVAL;
2281 static int __uvc_resume(struct usb_interface *intf, int reset)
2283 struct uvc_device *dev = usb_get_intfdata(intf);
2284 struct uvc_streaming *stream;
2285 int ret = 0;
2287 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
2288 intf->cur_altsetting->desc.bInterfaceNumber);
2290 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2291 UVC_SC_VIDEOCONTROL) {
2292 if (reset) {
2293 ret = uvc_ctrl_restore_values(dev);
2294 if (ret < 0)
2295 return ret;
2298 mutex_lock(&dev->lock);
2299 if (dev->users)
2300 ret = uvc_status_start(dev, GFP_NOIO);
2301 mutex_unlock(&dev->lock);
2303 return ret;
2306 list_for_each_entry(stream, &dev->streams, list) {
2307 if (stream->intf == intf) {
2308 ret = uvc_video_resume(stream, reset);
2309 if (ret < 0)
2310 uvc_queue_streamoff(&stream->queue,
2311 stream->queue.queue.type);
2312 return ret;
2316 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
2317 "mismatch.\n");
2318 return -EINVAL;
2321 static int uvc_resume(struct usb_interface *intf)
2323 return __uvc_resume(intf, 0);
2326 static int uvc_reset_resume(struct usb_interface *intf)
2328 return __uvc_resume(intf, 1);
2331 /* ------------------------------------------------------------------------
2332 * Module parameters
2335 static int uvc_clock_param_get(char *buffer, const struct kernel_param *kp)
2337 if (uvc_clock_param == CLOCK_MONOTONIC)
2338 return sprintf(buffer, "CLOCK_MONOTONIC");
2339 else
2340 return sprintf(buffer, "CLOCK_REALTIME");
2343 static int uvc_clock_param_set(const char *val, const struct kernel_param *kp)
2345 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2346 val += strlen("clock_");
2348 if (strcasecmp(val, "monotonic") == 0)
2349 uvc_clock_param = CLOCK_MONOTONIC;
2350 else if (strcasecmp(val, "realtime") == 0)
2351 uvc_clock_param = CLOCK_REALTIME;
2352 else
2353 return -EINVAL;
2355 return 0;
2358 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2359 &uvc_clock_param, S_IRUGO|S_IWUSR);
2360 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2361 module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR);
2362 MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
2363 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2364 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2365 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2366 MODULE_PARM_DESC(quirks, "Forced device quirks");
2367 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2368 MODULE_PARM_DESC(trace, "Trace level bitmask");
2369 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2370 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2372 /* ------------------------------------------------------------------------
2373 * Driver initialization and cleanup
2376 static const struct uvc_device_info uvc_quirk_probe_minmax = {
2377 .quirks = UVC_QUIRK_PROBE_MINMAX,
2380 static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
2381 .quirks = UVC_QUIRK_FIX_BANDWIDTH,
2384 static const struct uvc_device_info uvc_quirk_probe_def = {
2385 .quirks = UVC_QUIRK_PROBE_DEF,
2388 static const struct uvc_device_info uvc_quirk_stream_no_fid = {
2389 .quirks = UVC_QUIRK_STREAM_NO_FID,
2392 static const struct uvc_device_info uvc_quirk_force_y8 = {
2393 .quirks = UVC_QUIRK_FORCE_Y8,
2396 #define UVC_QUIRK_INFO(q) (kernel_ulong_t)&(struct uvc_device_info){.quirks = q}
2399 * The Logitech cameras listed below have their interface class set to
2400 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2401 * though they are compliant.
2403 static const struct usb_device_id uvc_ids[] = {
2404 /* LogiLink Wireless Webcam */
2405 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2406 | USB_DEVICE_ID_MATCH_INT_INFO,
2407 .idVendor = 0x0416,
2408 .idProduct = 0xa91a,
2409 .bInterfaceClass = USB_CLASS_VIDEO,
2410 .bInterfaceSubClass = 1,
2411 .bInterfaceProtocol = 0,
2412 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2413 /* Genius eFace 2025 */
2414 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2415 | USB_DEVICE_ID_MATCH_INT_INFO,
2416 .idVendor = 0x0458,
2417 .idProduct = 0x706e,
2418 .bInterfaceClass = USB_CLASS_VIDEO,
2419 .bInterfaceSubClass = 1,
2420 .bInterfaceProtocol = 0,
2421 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2422 /* Microsoft Lifecam NX-6000 */
2423 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2424 | USB_DEVICE_ID_MATCH_INT_INFO,
2425 .idVendor = 0x045e,
2426 .idProduct = 0x00f8,
2427 .bInterfaceClass = USB_CLASS_VIDEO,
2428 .bInterfaceSubClass = 1,
2429 .bInterfaceProtocol = 0,
2430 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2431 /* Microsoft Lifecam NX-3000 */
2432 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2433 | USB_DEVICE_ID_MATCH_INT_INFO,
2434 .idVendor = 0x045e,
2435 .idProduct = 0x0721,
2436 .bInterfaceClass = USB_CLASS_VIDEO,
2437 .bInterfaceSubClass = 1,
2438 .bInterfaceProtocol = 0,
2439 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2440 /* Microsoft Lifecam VX-7000 */
2441 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2442 | USB_DEVICE_ID_MATCH_INT_INFO,
2443 .idVendor = 0x045e,
2444 .idProduct = 0x0723,
2445 .bInterfaceClass = USB_CLASS_VIDEO,
2446 .bInterfaceSubClass = 1,
2447 .bInterfaceProtocol = 0,
2448 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2449 /* Logitech Quickcam Fusion */
2450 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2451 | USB_DEVICE_ID_MATCH_INT_INFO,
2452 .idVendor = 0x046d,
2453 .idProduct = 0x08c1,
2454 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2455 .bInterfaceSubClass = 1,
2456 .bInterfaceProtocol = 0 },
2457 /* Logitech Quickcam Orbit MP */
2458 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2459 | USB_DEVICE_ID_MATCH_INT_INFO,
2460 .idVendor = 0x046d,
2461 .idProduct = 0x08c2,
2462 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2463 .bInterfaceSubClass = 1,
2464 .bInterfaceProtocol = 0 },
2465 /* Logitech Quickcam Pro for Notebook */
2466 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2467 | USB_DEVICE_ID_MATCH_INT_INFO,
2468 .idVendor = 0x046d,
2469 .idProduct = 0x08c3,
2470 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2471 .bInterfaceSubClass = 1,
2472 .bInterfaceProtocol = 0 },
2473 /* Logitech Quickcam Pro 5000 */
2474 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2475 | USB_DEVICE_ID_MATCH_INT_INFO,
2476 .idVendor = 0x046d,
2477 .idProduct = 0x08c5,
2478 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2479 .bInterfaceSubClass = 1,
2480 .bInterfaceProtocol = 0 },
2481 /* Logitech Quickcam OEM Dell Notebook */
2482 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2483 | USB_DEVICE_ID_MATCH_INT_INFO,
2484 .idVendor = 0x046d,
2485 .idProduct = 0x08c6,
2486 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2487 .bInterfaceSubClass = 1,
2488 .bInterfaceProtocol = 0 },
2489 /* Logitech Quickcam OEM Cisco VT Camera II */
2490 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2491 | USB_DEVICE_ID_MATCH_INT_INFO,
2492 .idVendor = 0x046d,
2493 .idProduct = 0x08c7,
2494 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2495 .bInterfaceSubClass = 1,
2496 .bInterfaceProtocol = 0 },
2497 /* Logitech HD Pro Webcam C920 */
2498 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2499 | USB_DEVICE_ID_MATCH_INT_INFO,
2500 .idVendor = 0x046d,
2501 .idProduct = 0x082d,
2502 .bInterfaceClass = USB_CLASS_VIDEO,
2503 .bInterfaceSubClass = 1,
2504 .bInterfaceProtocol = 0,
2505 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) },
2506 /* Chicony CNF7129 (Asus EEE 100HE) */
2507 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2508 | USB_DEVICE_ID_MATCH_INT_INFO,
2509 .idVendor = 0x04f2,
2510 .idProduct = 0xb071,
2511 .bInterfaceClass = USB_CLASS_VIDEO,
2512 .bInterfaceSubClass = 1,
2513 .bInterfaceProtocol = 0,
2514 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_RESTRICT_FRAME_RATE) },
2515 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2516 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2517 | USB_DEVICE_ID_MATCH_INT_INFO,
2518 .idVendor = 0x058f,
2519 .idProduct = 0x3820,
2520 .bInterfaceClass = USB_CLASS_VIDEO,
2521 .bInterfaceSubClass = 1,
2522 .bInterfaceProtocol = 0,
2523 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2524 /* Dell XPS m1530 */
2525 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2526 | USB_DEVICE_ID_MATCH_INT_INFO,
2527 .idVendor = 0x05a9,
2528 .idProduct = 0x2640,
2529 .bInterfaceClass = USB_CLASS_VIDEO,
2530 .bInterfaceSubClass = 1,
2531 .bInterfaceProtocol = 0,
2532 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2533 /* Dell SP2008WFP Monitor */
2534 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2535 | USB_DEVICE_ID_MATCH_INT_INFO,
2536 .idVendor = 0x05a9,
2537 .idProduct = 0x2641,
2538 .bInterfaceClass = USB_CLASS_VIDEO,
2539 .bInterfaceSubClass = 1,
2540 .bInterfaceProtocol = 0,
2541 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2542 /* Dell Alienware X51 */
2543 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2544 | USB_DEVICE_ID_MATCH_INT_INFO,
2545 .idVendor = 0x05a9,
2546 .idProduct = 0x2643,
2547 .bInterfaceClass = USB_CLASS_VIDEO,
2548 .bInterfaceSubClass = 1,
2549 .bInterfaceProtocol = 0,
2550 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2551 /* Dell Studio Hybrid 140g (OmniVision webcam) */
2552 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2553 | USB_DEVICE_ID_MATCH_INT_INFO,
2554 .idVendor = 0x05a9,
2555 .idProduct = 0x264a,
2556 .bInterfaceClass = USB_CLASS_VIDEO,
2557 .bInterfaceSubClass = 1,
2558 .bInterfaceProtocol = 0,
2559 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2560 /* Dell XPS M1330 (OmniVision OV7670 webcam) */
2561 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2562 | USB_DEVICE_ID_MATCH_INT_INFO,
2563 .idVendor = 0x05a9,
2564 .idProduct = 0x7670,
2565 .bInterfaceClass = USB_CLASS_VIDEO,
2566 .bInterfaceSubClass = 1,
2567 .bInterfaceProtocol = 0,
2568 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2569 /* Apple Built-In iSight */
2570 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2571 | USB_DEVICE_ID_MATCH_INT_INFO,
2572 .idVendor = 0x05ac,
2573 .idProduct = 0x8501,
2574 .bInterfaceClass = USB_CLASS_VIDEO,
2575 .bInterfaceSubClass = 1,
2576 .bInterfaceProtocol = 0,
2577 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_PROBE_MINMAX
2578 | UVC_QUIRK_BUILTIN_ISIGHT) },
2579 /* Apple Built-In iSight via iBridge */
2580 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2581 | USB_DEVICE_ID_MATCH_INT_INFO,
2582 .idVendor = 0x05ac,
2583 .idProduct = 0x8600,
2584 .bInterfaceClass = USB_CLASS_VIDEO,
2585 .bInterfaceSubClass = 1,
2586 .bInterfaceProtocol = 0,
2587 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2588 /* Foxlink ("HP Webcam" on HP Mini 5103) */
2589 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2590 | USB_DEVICE_ID_MATCH_INT_INFO,
2591 .idVendor = 0x05c8,
2592 .idProduct = 0x0403,
2593 .bInterfaceClass = USB_CLASS_VIDEO,
2594 .bInterfaceSubClass = 1,
2595 .bInterfaceProtocol = 0,
2596 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2597 /* Genesys Logic USB 2.0 PC Camera */
2598 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2599 | USB_DEVICE_ID_MATCH_INT_INFO,
2600 .idVendor = 0x05e3,
2601 .idProduct = 0x0505,
2602 .bInterfaceClass = USB_CLASS_VIDEO,
2603 .bInterfaceSubClass = 1,
2604 .bInterfaceProtocol = 0,
2605 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2606 /* Hercules Classic Silver */
2607 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2608 | USB_DEVICE_ID_MATCH_INT_INFO,
2609 .idVendor = 0x06f8,
2610 .idProduct = 0x300c,
2611 .bInterfaceClass = USB_CLASS_VIDEO,
2612 .bInterfaceSubClass = 1,
2613 .bInterfaceProtocol = 0,
2614 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2615 /* ViMicro Vega */
2616 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2617 | USB_DEVICE_ID_MATCH_INT_INFO,
2618 .idVendor = 0x0ac8,
2619 .idProduct = 0x332d,
2620 .bInterfaceClass = USB_CLASS_VIDEO,
2621 .bInterfaceSubClass = 1,
2622 .bInterfaceProtocol = 0,
2623 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2624 /* ViMicro - Minoru3D */
2625 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2626 | USB_DEVICE_ID_MATCH_INT_INFO,
2627 .idVendor = 0x0ac8,
2628 .idProduct = 0x3410,
2629 .bInterfaceClass = USB_CLASS_VIDEO,
2630 .bInterfaceSubClass = 1,
2631 .bInterfaceProtocol = 0,
2632 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2633 /* ViMicro Venus - Minoru3D */
2634 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2635 | USB_DEVICE_ID_MATCH_INT_INFO,
2636 .idVendor = 0x0ac8,
2637 .idProduct = 0x3420,
2638 .bInterfaceClass = USB_CLASS_VIDEO,
2639 .bInterfaceSubClass = 1,
2640 .bInterfaceProtocol = 0,
2641 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2642 /* Ophir Optronics - SPCAM 620U */
2643 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2644 | USB_DEVICE_ID_MATCH_INT_INFO,
2645 .idVendor = 0x0bd3,
2646 .idProduct = 0x0555,
2647 .bInterfaceClass = USB_CLASS_VIDEO,
2648 .bInterfaceSubClass = 1,
2649 .bInterfaceProtocol = 0,
2650 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2651 /* MT6227 */
2652 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2653 | USB_DEVICE_ID_MATCH_INT_INFO,
2654 .idVendor = 0x0e8d,
2655 .idProduct = 0x0004,
2656 .bInterfaceClass = USB_CLASS_VIDEO,
2657 .bInterfaceSubClass = 1,
2658 .bInterfaceProtocol = 0,
2659 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_PROBE_MINMAX
2660 | UVC_QUIRK_PROBE_DEF) },
2661 /* IMC Networks (Medion Akoya) */
2662 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2663 | USB_DEVICE_ID_MATCH_INT_INFO,
2664 .idVendor = 0x13d3,
2665 .idProduct = 0x5103,
2666 .bInterfaceClass = USB_CLASS_VIDEO,
2667 .bInterfaceSubClass = 1,
2668 .bInterfaceProtocol = 0,
2669 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2670 /* JMicron USB2.0 XGA WebCam */
2671 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2672 | USB_DEVICE_ID_MATCH_INT_INFO,
2673 .idVendor = 0x152d,
2674 .idProduct = 0x0310,
2675 .bInterfaceClass = USB_CLASS_VIDEO,
2676 .bInterfaceSubClass = 1,
2677 .bInterfaceProtocol = 0,
2678 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2679 /* Syntek (HP Spartan) */
2680 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2681 | USB_DEVICE_ID_MATCH_INT_INFO,
2682 .idVendor = 0x174f,
2683 .idProduct = 0x5212,
2684 .bInterfaceClass = USB_CLASS_VIDEO,
2685 .bInterfaceSubClass = 1,
2686 .bInterfaceProtocol = 0,
2687 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2688 /* Syntek (Samsung Q310) */
2689 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2690 | USB_DEVICE_ID_MATCH_INT_INFO,
2691 .idVendor = 0x174f,
2692 .idProduct = 0x5931,
2693 .bInterfaceClass = USB_CLASS_VIDEO,
2694 .bInterfaceSubClass = 1,
2695 .bInterfaceProtocol = 0,
2696 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2697 /* Syntek (Packard Bell EasyNote MX52 */
2698 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2699 | USB_DEVICE_ID_MATCH_INT_INFO,
2700 .idVendor = 0x174f,
2701 .idProduct = 0x8a12,
2702 .bInterfaceClass = USB_CLASS_VIDEO,
2703 .bInterfaceSubClass = 1,
2704 .bInterfaceProtocol = 0,
2705 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2706 /* Syntek (Asus F9SG) */
2707 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2708 | USB_DEVICE_ID_MATCH_INT_INFO,
2709 .idVendor = 0x174f,
2710 .idProduct = 0x8a31,
2711 .bInterfaceClass = USB_CLASS_VIDEO,
2712 .bInterfaceSubClass = 1,
2713 .bInterfaceProtocol = 0,
2714 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2715 /* Syntek (Asus U3S) */
2716 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2717 | USB_DEVICE_ID_MATCH_INT_INFO,
2718 .idVendor = 0x174f,
2719 .idProduct = 0x8a33,
2720 .bInterfaceClass = USB_CLASS_VIDEO,
2721 .bInterfaceSubClass = 1,
2722 .bInterfaceProtocol = 0,
2723 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2724 /* Syntek (JAOtech Smart Terminal) */
2725 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2726 | USB_DEVICE_ID_MATCH_INT_INFO,
2727 .idVendor = 0x174f,
2728 .idProduct = 0x8a34,
2729 .bInterfaceClass = USB_CLASS_VIDEO,
2730 .bInterfaceSubClass = 1,
2731 .bInterfaceProtocol = 0,
2732 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2733 /* Miricle 307K */
2734 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2735 | USB_DEVICE_ID_MATCH_INT_INFO,
2736 .idVendor = 0x17dc,
2737 .idProduct = 0x0202,
2738 .bInterfaceClass = USB_CLASS_VIDEO,
2739 .bInterfaceSubClass = 1,
2740 .bInterfaceProtocol = 0,
2741 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2742 /* Lenovo Thinkpad SL400/SL500 */
2743 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2744 | USB_DEVICE_ID_MATCH_INT_INFO,
2745 .idVendor = 0x17ef,
2746 .idProduct = 0x480b,
2747 .bInterfaceClass = USB_CLASS_VIDEO,
2748 .bInterfaceSubClass = 1,
2749 .bInterfaceProtocol = 0,
2750 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2751 /* Aveo Technology USB 2.0 Camera */
2752 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2753 | USB_DEVICE_ID_MATCH_INT_INFO,
2754 .idVendor = 0x1871,
2755 .idProduct = 0x0306,
2756 .bInterfaceClass = USB_CLASS_VIDEO,
2757 .bInterfaceSubClass = 1,
2758 .bInterfaceProtocol = 0,
2759 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_PROBE_MINMAX
2760 | UVC_QUIRK_PROBE_EXTRAFIELDS) },
2761 /* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
2762 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2763 | USB_DEVICE_ID_MATCH_INT_INFO,
2764 .idVendor = 0x1871,
2765 .idProduct = 0x0516,
2766 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2767 .bInterfaceSubClass = 1,
2768 .bInterfaceProtocol = 0 },
2769 /* Ecamm Pico iMage */
2770 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2771 | USB_DEVICE_ID_MATCH_INT_INFO,
2772 .idVendor = 0x18cd,
2773 .idProduct = 0xcafe,
2774 .bInterfaceClass = USB_CLASS_VIDEO,
2775 .bInterfaceSubClass = 1,
2776 .bInterfaceProtocol = 0,
2777 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_PROBE_EXTRAFIELDS) },
2778 /* Manta MM-353 Plako */
2779 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2780 | USB_DEVICE_ID_MATCH_INT_INFO,
2781 .idVendor = 0x18ec,
2782 .idProduct = 0x3188,
2783 .bInterfaceClass = USB_CLASS_VIDEO,
2784 .bInterfaceSubClass = 1,
2785 .bInterfaceProtocol = 0,
2786 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2787 /* FSC WebCam V30S */
2788 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2789 | USB_DEVICE_ID_MATCH_INT_INFO,
2790 .idVendor = 0x18ec,
2791 .idProduct = 0x3288,
2792 .bInterfaceClass = USB_CLASS_VIDEO,
2793 .bInterfaceSubClass = 1,
2794 .bInterfaceProtocol = 0,
2795 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2796 /* Arkmicro unbranded */
2797 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2798 | USB_DEVICE_ID_MATCH_INT_INFO,
2799 .idVendor = 0x18ec,
2800 .idProduct = 0x3290,
2801 .bInterfaceClass = USB_CLASS_VIDEO,
2802 .bInterfaceSubClass = 1,
2803 .bInterfaceProtocol = 0,
2804 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def },
2805 /* The Imaging Source USB CCD cameras */
2806 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2807 | USB_DEVICE_ID_MATCH_INT_INFO,
2808 .idVendor = 0x199e,
2809 .idProduct = 0x8102,
2810 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2811 .bInterfaceSubClass = 1,
2812 .bInterfaceProtocol = 0 },
2813 /* Bodelin ProScopeHR */
2814 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2815 | USB_DEVICE_ID_MATCH_DEV_HI
2816 | USB_DEVICE_ID_MATCH_INT_INFO,
2817 .idVendor = 0x19ab,
2818 .idProduct = 0x1000,
2819 .bcdDevice_hi = 0x0126,
2820 .bInterfaceClass = USB_CLASS_VIDEO,
2821 .bInterfaceSubClass = 1,
2822 .bInterfaceProtocol = 0,
2823 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_STATUS_INTERVAL) },
2824 /* MSI StarCam 370i */
2825 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2826 | USB_DEVICE_ID_MATCH_INT_INFO,
2827 .idVendor = 0x1b3b,
2828 .idProduct = 0x2951,
2829 .bInterfaceClass = USB_CLASS_VIDEO,
2830 .bInterfaceSubClass = 1,
2831 .bInterfaceProtocol = 0,
2832 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2833 /* Generalplus Technology Inc. 808 Camera */
2834 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2835 | USB_DEVICE_ID_MATCH_INT_INFO,
2836 .idVendor = 0x1b3f,
2837 .idProduct = 0x2002,
2838 .bInterfaceClass = USB_CLASS_VIDEO,
2839 .bInterfaceSubClass = 1,
2840 .bInterfaceProtocol = 0,
2841 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax },
2842 /* SiGma Micro USB Web Camera */
2843 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2844 | USB_DEVICE_ID_MATCH_INT_INFO,
2845 .idVendor = 0x1c4f,
2846 .idProduct = 0x3000,
2847 .bInterfaceClass = USB_CLASS_VIDEO,
2848 .bInterfaceSubClass = 1,
2849 .bInterfaceProtocol = 0,
2850 .driver_info = UVC_QUIRK_INFO(UVC_QUIRK_PROBE_MINMAX
2851 | UVC_QUIRK_IGNORE_SELECTOR_UNIT) },
2852 /* Oculus VR Positional Tracker DK2 */
2853 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2854 | USB_DEVICE_ID_MATCH_INT_INFO,
2855 .idVendor = 0x2833,
2856 .idProduct = 0x0201,
2857 .bInterfaceClass = USB_CLASS_VIDEO,
2858 .bInterfaceSubClass = 1,
2859 .bInterfaceProtocol = 0,
2860 .driver_info = (kernel_ulong_t)&uvc_quirk_force_y8 },
2861 /* Oculus VR Rift Sensor */
2862 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2863 | USB_DEVICE_ID_MATCH_INT_INFO,
2864 .idVendor = 0x2833,
2865 .idProduct = 0x0211,
2866 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2867 .bInterfaceSubClass = 1,
2868 .bInterfaceProtocol = 0,
2869 .driver_info = (kernel_ulong_t)&uvc_quirk_force_y8 },
2870 /* Generic USB Video Class */
2871 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
2872 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
2876 MODULE_DEVICE_TABLE(usb, uvc_ids);
2878 struct uvc_driver uvc_driver = {
2879 .driver = {
2880 .name = "uvcvideo",
2881 .probe = uvc_probe,
2882 .disconnect = uvc_disconnect,
2883 .suspend = uvc_suspend,
2884 .resume = uvc_resume,
2885 .reset_resume = uvc_reset_resume,
2886 .id_table = uvc_ids,
2887 .supports_autosuspend = 1,
2891 static int __init uvc_init(void)
2893 int ret;
2895 uvc_debugfs_init();
2897 ret = usb_register(&uvc_driver.driver);
2898 if (ret < 0) {
2899 uvc_debugfs_cleanup();
2900 return ret;
2903 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2904 return 0;
2907 static void __exit uvc_cleanup(void)
2909 usb_deregister(&uvc_driver.driver);
2910 uvc_debugfs_cleanup();
2913 module_init(uvc_init);
2914 module_exit(uvc_cleanup);
2916 MODULE_AUTHOR(DRIVER_AUTHOR);
2917 MODULE_DESCRIPTION(DRIVER_DESC);
2918 MODULE_LICENSE("GPL");
2919 MODULE_VERSION(DRIVER_VERSION);