V4L/DVB (12378): uvcvideo: Restructure the driver to support multiple simultaneous...
[deliverable/linux.git] / drivers / media / video / uvc / uvc_driver.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_driver.c -- USB Video Class driver
3 *
2c2d264b 4 * Copyright (C) 2005-2009
c0efd232
LP
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
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.
11 *
12 */
13
14/*
ff924203
LP
15 * This driver aims to support video input and ouput devices compliant with the
16 * 'USB Video Class' specification.
c0efd232
LP
17 *
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.
24 */
25
26#include <linux/kernel.h>
c0efd232
LP
27#include <linux/list.h>
28#include <linux/module.h>
29#include <linux/usb.h>
30#include <linux/videodev2.h>
31#include <linux/vmalloc.h>
32#include <linux/wait.h>
33#include <asm/atomic.h>
9bc6218d 34#include <asm/unaligned.h>
c0efd232
LP
35
36#include <media/v4l2-common.h>
37
38#include "uvcvideo.h"
39
40#define DRIVER_AUTHOR "Laurent Pinchart <laurent.pinchart@skynet.be>"
41#define DRIVER_DESC "USB Video Class driver"
42#ifndef DRIVER_VERSION
43#define DRIVER_VERSION "v0.1.0"
44#endif
45
0fbd8ee6 46unsigned int uvc_no_drop_param;
c0efd232
LP
47static unsigned int uvc_quirks_param;
48unsigned int uvc_trace_param;
49
50/* ------------------------------------------------------------------------
2c2d264b 51 * Video formats
c0efd232
LP
52 */
53
54static struct uvc_format_desc uvc_fmts[] = {
55 {
56 .name = "YUV 4:2:2 (YUYV)",
57 .guid = UVC_GUID_FORMAT_YUY2,
58 .fcc = V4L2_PIX_FMT_YUYV,
59 },
60 {
61 .name = "YUV 4:2:0 (NV12)",
62 .guid = UVC_GUID_FORMAT_NV12,
63 .fcc = V4L2_PIX_FMT_NV12,
64 },
65 {
66 .name = "MJPEG",
67 .guid = UVC_GUID_FORMAT_MJPEG,
68 .fcc = V4L2_PIX_FMT_MJPEG,
69 },
70 {
71 .name = "YVU 4:2:0 (YV12)",
72 .guid = UVC_GUID_FORMAT_YV12,
73 .fcc = V4L2_PIX_FMT_YVU420,
74 },
75 {
76 .name = "YUV 4:2:0 (I420)",
77 .guid = UVC_GUID_FORMAT_I420,
78 .fcc = V4L2_PIX_FMT_YUV420,
79 },
80 {
81 .name = "YUV 4:2:2 (UYVY)",
82 .guid = UVC_GUID_FORMAT_UYVY,
83 .fcc = V4L2_PIX_FMT_UYVY,
84 },
85 {
86 .name = "Greyscale",
87 .guid = UVC_GUID_FORMAT_Y800,
88 .fcc = V4L2_PIX_FMT_GREY,
89 },
90 {
91 .name = "RGB Bayer",
92 .guid = UVC_GUID_FORMAT_BY8,
93 .fcc = V4L2_PIX_FMT_SBGGR8,
94 },
95};
96
97/* ------------------------------------------------------------------------
98 * Utility functions
99 */
100
101struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
102 __u8 epaddr)
103{
104 struct usb_host_endpoint *ep;
105 unsigned int i;
106
107 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
108 ep = &alts->endpoint[i];
109 if (ep->desc.bEndpointAddress == epaddr)
110 return ep;
111 }
112
113 return NULL;
114}
115
116static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
117{
118 unsigned int len = ARRAY_SIZE(uvc_fmts);
119 unsigned int i;
120
121 for (i = 0; i < len; ++i) {
122 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
123 return &uvc_fmts[i];
124 }
125
126 return NULL;
127}
128
129static __u32 uvc_colorspace(const __u8 primaries)
130{
131 static const __u8 colorprimaries[] = {
132 0,
133 V4L2_COLORSPACE_SRGB,
134 V4L2_COLORSPACE_470_SYSTEM_M,
135 V4L2_COLORSPACE_470_SYSTEM_BG,
136 V4L2_COLORSPACE_SMPTE170M,
137 V4L2_COLORSPACE_SMPTE240M,
138 };
139
140 if (primaries < ARRAY_SIZE(colorprimaries))
141 return colorprimaries[primaries];
142
143 return 0;
144}
145
146/* Simplify a fraction using a simple continued fraction decomposition. The
147 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
148 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
149 * arbitrary parameters to remove non-significative terms from the simple
150 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
151 * respectively seems to give nice results.
152 */
153void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
154 unsigned int n_terms, unsigned int threshold)
155{
156 uint32_t *an;
157 uint32_t x, y, r;
158 unsigned int i, n;
159
160 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
161 if (an == NULL)
162 return;
163
164 /* Convert the fraction to a simple continued fraction. See
165 * http://mathforum.org/dr.math/faq/faq.fractions.html
166 * Stop if the current term is bigger than or equal to the given
167 * threshold.
168 */
169 x = *numerator;
170 y = *denominator;
171
172 for (n = 0; n < n_terms && y != 0; ++n) {
173 an[n] = x / y;
174 if (an[n] >= threshold) {
175 if (n < 2)
176 n++;
177 break;
178 }
179
180 r = x - an[n] * y;
181 x = y;
182 y = r;
183 }
184
185 /* Expand the simple continued fraction back to an integer fraction. */
186 x = 0;
187 y = 1;
188
189 for (i = n; i > 0; --i) {
190 r = y;
191 y = an[i-1] * y + x;
192 x = r;
193 }
194
195 *numerator = y;
196 *denominator = x;
197 kfree(an);
198}
199
200/* Convert a fraction to a frame interval in 100ns multiples. The idea here is
201 * to compute numerator / denominator * 10000000 using 32 bit fixed point
202 * arithmetic only.
203 */
204uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
205{
206 uint32_t multiplier;
207
208 /* Saturate the result if the operation would overflow. */
209 if (denominator == 0 ||
210 numerator/denominator >= ((uint32_t)-1)/10000000)
211 return (uint32_t)-1;
212
213 /* Divide both the denominator and the multiplier by two until
214 * numerator * multiplier doesn't overflow. If anyone knows a better
215 * algorithm please let me know.
216 */
217 multiplier = 10000000;
218 while (numerator > ((uint32_t)-1)/multiplier) {
219 multiplier /= 2;
220 denominator /= 2;
221 }
222
223 return denominator ? numerator * multiplier / denominator : 0;
224}
225
226/* ------------------------------------------------------------------------
227 * Terminal and unit management
228 */
229
230static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
231{
232 struct uvc_entity *entity;
233
234 list_for_each_entry(entity, &dev->entities, list) {
235 if (entity->id == id)
236 return entity;
237 }
238
239 return NULL;
240}
241
242static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
243 int id, struct uvc_entity *entity)
244{
245 unsigned int i;
246
247 if (entity == NULL)
248 entity = list_entry(&dev->entities, struct uvc_entity, list);
249
250 list_for_each_entry_continue(entity, &dev->entities, list) {
251 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 252 case UVC_TT_STREAMING:
c0efd232
LP
253 if (entity->output.bSourceID == id)
254 return entity;
255 break;
256
b482d923 257 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
258 if (entity->processing.bSourceID == id)
259 return entity;
260 break;
261
b482d923 262 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
263 for (i = 0; i < entity->selector.bNrInPins; ++i)
264 if (entity->selector.baSourceID[i] == id)
265 return entity;
266 break;
267
b482d923 268 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
269 for (i = 0; i < entity->extension.bNrInPins; ++i)
270 if (entity->extension.baSourceID[i] == id)
271 return entity;
272 break;
273 }
274 }
275
276 return NULL;
277}
278
279/* ------------------------------------------------------------------------
280 * Descriptors handling
281 */
282
283static int uvc_parse_format(struct uvc_device *dev,
284 struct uvc_streaming *streaming, struct uvc_format *format,
285 __u32 **intervals, unsigned char *buffer, int buflen)
286{
287 struct usb_interface *intf = streaming->intf;
288 struct usb_host_interface *alts = intf->cur_altsetting;
289 struct uvc_format_desc *fmtdesc;
290 struct uvc_frame *frame;
291 const unsigned char *start = buffer;
292 unsigned int interval;
293 unsigned int i, n;
294 __u8 ftype;
295
296 format->type = buffer[2];
297 format->index = buffer[3];
298
299 switch (buffer[2]) {
b482d923
LP
300 case UVC_VS_FORMAT_UNCOMPRESSED:
301 case UVC_VS_FORMAT_FRAME_BASED:
302 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
233548a2 303 if (buflen < n) {
b2d9cc42 304 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
305 "interface %d FORMAT error\n",
306 dev->udev->devnum,
307 alts->desc.bInterfaceNumber);
308 return -EINVAL;
309 }
310
311 /* Find the format descriptor from its GUID. */
312 fmtdesc = uvc_format_by_guid(&buffer[5]);
313
314 if (fmtdesc != NULL) {
d0ebf307 315 strlcpy(format->name, fmtdesc->name,
c0efd232
LP
316 sizeof format->name);
317 format->fcc = fmtdesc->fcc;
318 } else {
319 uvc_printk(KERN_INFO, "Unknown video format "
320 UVC_GUID_FORMAT "\n",
321 UVC_GUID_ARGS(&buffer[5]));
322 snprintf(format->name, sizeof format->name,
323 UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
324 format->fcc = 0;
325 }
326
327 format->bpp = buffer[21];
b482d923
LP
328 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
329 ftype = UVC_VS_FRAME_UNCOMPRESSED;
c0efd232 330 } else {
b482d923 331 ftype = UVC_VS_FRAME_FRAME_BASED;
c0efd232
LP
332 if (buffer[27])
333 format->flags = UVC_FMT_FLAG_COMPRESSED;
334 }
335 break;
336
b482d923 337 case UVC_VS_FORMAT_MJPEG:
c0efd232 338 if (buflen < 11) {
b2d9cc42 339 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
340 "interface %d FORMAT error\n",
341 dev->udev->devnum,
342 alts->desc.bInterfaceNumber);
343 return -EINVAL;
344 }
345
d0ebf307 346 strlcpy(format->name, "MJPEG", sizeof format->name);
c0efd232
LP
347 format->fcc = V4L2_PIX_FMT_MJPEG;
348 format->flags = UVC_FMT_FLAG_COMPRESSED;
349 format->bpp = 0;
b482d923 350 ftype = UVC_VS_FRAME_MJPEG;
c0efd232
LP
351 break;
352
b482d923 353 case UVC_VS_FORMAT_DV:
c0efd232 354 if (buflen < 9) {
b2d9cc42 355 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
356 "interface %d FORMAT error\n",
357 dev->udev->devnum,
358 alts->desc.bInterfaceNumber);
359 return -EINVAL;
360 }
361
362 switch (buffer[8] & 0x7f) {
363 case 0:
d0ebf307 364 strlcpy(format->name, "SD-DV", sizeof format->name);
c0efd232
LP
365 break;
366 case 1:
d0ebf307 367 strlcpy(format->name, "SDL-DV", sizeof format->name);
c0efd232
LP
368 break;
369 case 2:
d0ebf307 370 strlcpy(format->name, "HD-DV", sizeof format->name);
c0efd232
LP
371 break;
372 default:
b2d9cc42 373 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
374 "interface %d: unknown DV format %u\n",
375 dev->udev->devnum,
376 alts->desc.bInterfaceNumber, buffer[8]);
377 return -EINVAL;
378 }
379
d0ebf307 380 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
c0efd232
LP
381 sizeof format->name);
382
383 format->fcc = V4L2_PIX_FMT_DV;
384 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
385 format->bpp = 0;
386 ftype = 0;
387
388 /* Create a dummy frame descriptor. */
389 frame = &format->frame[0];
390 memset(&format->frame[0], 0, sizeof format->frame[0]);
391 frame->bFrameIntervalType = 1;
392 frame->dwDefaultFrameInterval = 1;
393 frame->dwFrameInterval = *intervals;
394 *(*intervals)++ = 1;
395 format->nframes = 1;
396 break;
397
b482d923
LP
398 case UVC_VS_FORMAT_MPEG2TS:
399 case UVC_VS_FORMAT_STREAM_BASED:
c0efd232
LP
400 /* Not supported yet. */
401 default:
b2d9cc42 402 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
403 "interface %d unsupported format %u\n",
404 dev->udev->devnum, alts->desc.bInterfaceNumber,
405 buffer[2]);
406 return -EINVAL;
407 }
408
409 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
410
411 buflen -= buffer[0];
412 buffer += buffer[0];
413
414 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
415 * based formats have frame descriptors.
416 */
417 while (buflen > 2 && buffer[2] == ftype) {
078f8947 418 frame = &format->frame[format->nframes];
b482d923 419 if (ftype != UVC_VS_FRAME_FRAME_BASED)
c0efd232
LP
420 n = buflen > 25 ? buffer[25] : 0;
421 else
422 n = buflen > 21 ? buffer[21] : 0;
423
424 n = n ? n : 3;
425
426 if (buflen < 26 + 4*n) {
b2d9cc42 427 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
428 "interface %d FRAME error\n", dev->udev->devnum,
429 alts->desc.bInterfaceNumber);
430 return -EINVAL;
431 }
432
433 frame->bFrameIndex = buffer[3];
434 frame->bmCapabilities = buffer[4];
9bc6218d
MH
435 frame->wWidth = get_unaligned_le16(&buffer[5]);
436 frame->wHeight = get_unaligned_le16(&buffer[7]);
437 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
438 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
b482d923 439 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
c0efd232 440 frame->dwMaxVideoFrameBufferSize =
9bc6218d 441 get_unaligned_le32(&buffer[17]);
c0efd232 442 frame->dwDefaultFrameInterval =
9bc6218d 443 get_unaligned_le32(&buffer[21]);
c0efd232
LP
444 frame->bFrameIntervalType = buffer[25];
445 } else {
446 frame->dwMaxVideoFrameBufferSize = 0;
447 frame->dwDefaultFrameInterval =
9bc6218d 448 get_unaligned_le32(&buffer[17]);
c0efd232
LP
449 frame->bFrameIntervalType = buffer[21];
450 }
451 frame->dwFrameInterval = *intervals;
452
453 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454 * completely. Observed behaviours range from setting the
2c2d264b 455 * value to 1.1x the actual frame size to hardwiring the
c0efd232
LP
456 * 16 low bits to 0. This results in a higher than necessary
457 * memory usage as well as a wrong image size information. For
458 * uncompressed formats this can be fixed by computing the
459 * value from the frame size.
460 */
461 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
462 frame->dwMaxVideoFrameBufferSize = format->bpp
463 * frame->wWidth * frame->wHeight / 8;
464
465 /* Some bogus devices report dwMinFrameInterval equal to
466 * dwMaxFrameInterval and have dwFrameIntervalStep set to
467 * zero. Setting all null intervals to 1 fixes the problem and
2c2d264b 468 * some other divisions by zero that could happen.
c0efd232
LP
469 */
470 for (i = 0; i < n; ++i) {
9bc6218d 471 interval = get_unaligned_le32(&buffer[26+4*i]);
c0efd232
LP
472 *(*intervals)++ = interval ? interval : 1;
473 }
474
475 /* Make sure that the default frame interval stays between
476 * the boundaries.
477 */
478 n -= frame->bFrameIntervalType ? 1 : 2;
479 frame->dwDefaultFrameInterval =
480 min(frame->dwFrameInterval[n],
481 max(frame->dwFrameInterval[0],
482 frame->dwDefaultFrameInterval));
483
484 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
485 frame->wWidth, frame->wHeight,
486 10000000/frame->dwDefaultFrameInterval,
487 (100000000/frame->dwDefaultFrameInterval)%10);
488
078f8947 489 format->nframes++;
c0efd232
LP
490 buflen -= buffer[0];
491 buffer += buffer[0];
492 }
493
b482d923 494 if (buflen > 2 && buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
c0efd232
LP
495 buflen -= buffer[0];
496 buffer += buffer[0];
497 }
498
b482d923 499 if (buflen > 2 && buffer[2] == UVC_VS_COLORFORMAT) {
c0efd232 500 if (buflen < 6) {
b2d9cc42 501 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
502 "interface %d COLORFORMAT error\n",
503 dev->udev->devnum,
504 alts->desc.bInterfaceNumber);
505 return -EINVAL;
506 }
507
508 format->colorspace = uvc_colorspace(buffer[3]);
509
510 buflen -= buffer[0];
511 buffer += buffer[0];
512 }
513
514 return buffer - start;
515}
516
517static int uvc_parse_streaming(struct uvc_device *dev,
518 struct usb_interface *intf)
519{
520 struct uvc_streaming *streaming = NULL;
521 struct uvc_format *format;
522 struct uvc_frame *frame;
523 struct usb_host_interface *alts = &intf->altsetting[0];
524 unsigned char *_buffer, *buffer = alts->extra;
525 int _buflen, buflen = alts->extralen;
526 unsigned int nformats = 0, nframes = 0, nintervals = 0;
527 unsigned int size, i, n, p;
528 __u32 *interval;
529 __u16 psize;
530 int ret = -EINVAL;
531
532 if (intf->cur_altsetting->desc.bInterfaceSubClass
b482d923 533 != UVC_SC_VIDEOSTREAMING) {
c0efd232
LP
534 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
535 "video streaming interface\n", dev->udev->devnum,
536 intf->altsetting[0].desc.bInterfaceNumber);
537 return -EINVAL;
538 }
539
540 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
541 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
542 "claimed\n", dev->udev->devnum,
543 intf->altsetting[0].desc.bInterfaceNumber);
544 return -EINVAL;
545 }
546
547 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
548 if (streaming == NULL) {
549 usb_driver_release_interface(&uvc_driver.driver, intf);
550 return -EINVAL;
551 }
552
553 mutex_init(&streaming->mutex);
35f02a68 554 streaming->dev = dev;
c0efd232
LP
555 streaming->intf = usb_get_intf(intf);
556 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
557
558 /* The Pico iMage webcam has its class-specific interface descriptors
559 * after the endpoint descriptors.
560 */
561 if (buflen == 0) {
562 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
563 struct usb_host_endpoint *ep = &alts->endpoint[i];
564
565 if (ep->extralen == 0)
566 continue;
567
568 if (ep->extralen > 2 &&
569 ep->extra[1] == USB_DT_CS_INTERFACE) {
570 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
571 "from endpoint %u.\n", i);
572 buffer = alts->endpoint[i].extra;
573 buflen = alts->endpoint[i].extralen;
574 break;
575 }
576 }
577 }
578
579 /* Skip the standard interface descriptors. */
580 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
581 buflen -= buffer[0];
582 buffer += buffer[0];
583 }
584
585 if (buflen <= 2) {
586 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
587 "interface descriptors found.\n");
588 goto error;
589 }
590
591 /* Parse the header descriptor. */
ff924203 592 switch (buffer[2]) {
b482d923 593 case UVC_VS_OUTPUT_HEADER:
ff924203
LP
594 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
595 size = 9;
596 break;
597
b482d923 598 case UVC_VS_INPUT_HEADER:
ff924203
LP
599 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
600 size = 13;
601 break;
602
603 default:
c0efd232 604 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
ff924203
LP
605 "%d HEADER descriptor not found.\n", dev->udev->devnum,
606 alts->desc.bInterfaceNumber);
c0efd232 607 goto error;
ff924203 608 }
c0efd232 609
ff924203
LP
610 p = buflen >= 4 ? buffer[3] : 0;
611 n = buflen >= size ? buffer[size-1] : 0;
612
613 if (buflen < size + p*n) {
614 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
615 "interface %d HEADER descriptor is invalid.\n",
616 dev->udev->devnum, alts->desc.bInterfaceNumber);
617 goto error;
618 }
c0efd232 619
ff924203
LP
620 streaming->header.bNumFormats = p;
621 streaming->header.bEndpointAddress = buffer[6];
b482d923 622 if (buffer[2] == UVC_VS_INPUT_HEADER) {
c0efd232
LP
623 streaming->header.bmInfo = buffer[7];
624 streaming->header.bTerminalLink = buffer[8];
625 streaming->header.bStillCaptureMethod = buffer[9];
626 streaming->header.bTriggerSupport = buffer[10];
627 streaming->header.bTriggerUsage = buffer[11];
c0efd232 628 } else {
ff924203
LP
629 streaming->header.bTerminalLink = buffer[7];
630 }
631 streaming->header.bControlSize = n;
632
633 streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
634 if (streaming->header.bmaControls == NULL) {
635 ret = -ENOMEM;
c0efd232
LP
636 goto error;
637 }
638
ff924203
LP
639 memcpy(streaming->header.bmaControls, &buffer[size], p*n);
640
c0efd232
LP
641 buflen -= buffer[0];
642 buffer += buffer[0];
643
644 _buffer = buffer;
645 _buflen = buflen;
646
647 /* Count the format and frame descriptors. */
042e143e 648 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
c0efd232 649 switch (_buffer[2]) {
b482d923
LP
650 case UVC_VS_FORMAT_UNCOMPRESSED:
651 case UVC_VS_FORMAT_MJPEG:
652 case UVC_VS_FORMAT_FRAME_BASED:
c0efd232
LP
653 nformats++;
654 break;
655
b482d923 656 case UVC_VS_FORMAT_DV:
c0efd232
LP
657 /* DV format has no frame descriptor. We will create a
658 * dummy frame descriptor with a dummy frame interval.
659 */
660 nformats++;
661 nframes++;
662 nintervals++;
663 break;
664
b482d923
LP
665 case UVC_VS_FORMAT_MPEG2TS:
666 case UVC_VS_FORMAT_STREAM_BASED:
c0efd232
LP
667 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
668 "interface %d FORMAT %u is not supported.\n",
669 dev->udev->devnum,
670 alts->desc.bInterfaceNumber, _buffer[2]);
671 break;
672
b482d923
LP
673 case UVC_VS_FRAME_UNCOMPRESSED:
674 case UVC_VS_FRAME_MJPEG:
c0efd232
LP
675 nframes++;
676 if (_buflen > 25)
677 nintervals += _buffer[25] ? _buffer[25] : 3;
678 break;
679
b482d923 680 case UVC_VS_FRAME_FRAME_BASED:
c0efd232
LP
681 nframes++;
682 if (_buflen > 21)
683 nintervals += _buffer[21] ? _buffer[21] : 3;
684 break;
685 }
686
687 _buflen -= _buffer[0];
688 _buffer += _buffer[0];
689 }
690
691 if (nformats == 0) {
692 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
693 "%d has no supported formats defined.\n",
694 dev->udev->devnum, alts->desc.bInterfaceNumber);
695 goto error;
696 }
697
698 size = nformats * sizeof *format + nframes * sizeof *frame
699 + nintervals * sizeof *interval;
700 format = kzalloc(size, GFP_KERNEL);
701 if (format == NULL) {
702 ret = -ENOMEM;
703 goto error;
704 }
705
706 frame = (struct uvc_frame *)&format[nformats];
707 interval = (__u32 *)&frame[nframes];
708
709 streaming->format = format;
710 streaming->nformats = nformats;
711
712 /* Parse the format descriptors. */
042e143e 713 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
c0efd232 714 switch (buffer[2]) {
b482d923
LP
715 case UVC_VS_FORMAT_UNCOMPRESSED:
716 case UVC_VS_FORMAT_MJPEG:
717 case UVC_VS_FORMAT_DV:
718 case UVC_VS_FORMAT_FRAME_BASED:
c0efd232
LP
719 format->frame = frame;
720 ret = uvc_parse_format(dev, streaming, format,
721 &interval, buffer, buflen);
722 if (ret < 0)
723 goto error;
724
725 frame += format->nframes;
726 format++;
727
728 buflen -= ret;
729 buffer += ret;
730 continue;
731
732 default:
733 break;
734 }
735
736 buflen -= buffer[0];
737 buffer += buffer[0];
738 }
739
740 /* Parse the alternate settings to find the maximum bandwidth. */
741 for (i = 0; i < intf->num_altsetting; ++i) {
742 struct usb_host_endpoint *ep;
743 alts = &intf->altsetting[i];
744 ep = uvc_find_endpoint(alts,
745 streaming->header.bEndpointAddress);
746 if (ep == NULL)
747 continue;
748
749 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
750 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
751 if (psize > streaming->maxpsize)
752 streaming->maxpsize = psize;
753 }
754
35f02a68 755 list_add_tail(&streaming->list, &dev->streams);
c0efd232
LP
756 return 0;
757
758error:
759 usb_driver_release_interface(&uvc_driver.driver, intf);
760 usb_put_intf(intf);
761 kfree(streaming->format);
762 kfree(streaming->header.bmaControls);
763 kfree(streaming);
764 return ret;
765}
766
767/* Parse vendor-specific extensions. */
768static int uvc_parse_vendor_control(struct uvc_device *dev,
769 const unsigned char *buffer, int buflen)
770{
771 struct usb_device *udev = dev->udev;
772 struct usb_host_interface *alts = dev->intf->cur_altsetting;
773 struct uvc_entity *unit;
774 unsigned int n, p;
775 int handled = 0;
776
777 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
778 case 0x046d: /* Logitech */
779 if (buffer[1] != 0x41 || buffer[2] != 0x01)
780 break;
781
782 /* Logitech implements several vendor specific functions
783 * through vendor specific extension units (LXU).
784 *
785 * The LXU descriptors are similar to XU descriptors
786 * (see "USB Device Video Class for Video Devices", section
787 * 3.7.2.6 "Extension Unit Descriptor") with the following
788 * differences:
789 *
790 * ----------------------------------------------------------
791 * 0 bLength 1 Number
792 * Size of this descriptor, in bytes: 24+p+n*2
793 * ----------------------------------------------------------
794 * 23+p+n bmControlsType N Bitmap
795 * Individual bits in the set are defined:
796 * 0: Absolute
797 * 1: Relative
798 *
799 * This bitset is mapped exactly the same as bmControls.
800 * ----------------------------------------------------------
801 * 23+p+n*2 bReserved 1 Boolean
802 * ----------------------------------------------------------
803 * 24+p+n*2 iExtension 1 Index
804 * Index of a string descriptor that describes this
805 * extension unit.
806 * ----------------------------------------------------------
807 */
808 p = buflen >= 22 ? buffer[21] : 0;
809 n = buflen >= 25 + p ? buffer[22+p] : 0;
810
811 if (buflen < 25 + p + 2*n) {
812 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
813 "interface %d EXTENSION_UNIT error\n",
814 udev->devnum, alts->desc.bInterfaceNumber);
815 break;
816 }
817
818 unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
819 if (unit == NULL)
820 return -ENOMEM;
821
822 unit->id = buffer[3];
b482d923 823 unit->type = UVC_VC_EXTENSION_UNIT;
c0efd232
LP
824 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
825 unit->extension.bNumControls = buffer[20];
9bc6218d 826 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
c0efd232
LP
827 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
828 memcpy(unit->extension.baSourceID, &buffer[22], p);
829 unit->extension.bControlSize = buffer[22+p];
830 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
831 unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
832 + p + n;
833 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
834
835 if (buffer[24+p+2*n] != 0)
836 usb_string(udev, buffer[24+p+2*n], unit->name,
837 sizeof unit->name);
838 else
839 sprintf(unit->name, "Extension %u", buffer[3]);
840
841 list_add_tail(&unit->list, &dev->entities);
842 handled = 1;
843 break;
844 }
845
846 return handled;
847}
848
849static int uvc_parse_standard_control(struct uvc_device *dev,
850 const unsigned char *buffer, int buflen)
851{
852 struct usb_device *udev = dev->udev;
853 struct uvc_entity *unit, *term;
854 struct usb_interface *intf;
855 struct usb_host_interface *alts = dev->intf->cur_altsetting;
856 unsigned int i, n, p, len;
857 __u16 type;
858
859 switch (buffer[2]) {
b482d923 860 case UVC_VC_HEADER:
c0efd232
LP
861 n = buflen >= 12 ? buffer[11] : 0;
862
863 if (buflen < 12 || buflen < 12 + n) {
864 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
865 "interface %d HEADER error\n", udev->devnum,
866 alts->desc.bInterfaceNumber);
867 return -EINVAL;
868 }
869
9bc6218d
MH
870 dev->uvc_version = get_unaligned_le16(&buffer[3]);
871 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
c0efd232
LP
872
873 /* Parse all USB Video Streaming interfaces. */
874 for (i = 0; i < n; ++i) {
875 intf = usb_ifnum_to_if(udev, buffer[12+i]);
876 if (intf == NULL) {
877 uvc_trace(UVC_TRACE_DESCR, "device %d "
878 "interface %d doesn't exists\n",
879 udev->devnum, i);
880 continue;
881 }
882
883 uvc_parse_streaming(dev, intf);
884 }
885 break;
886
b482d923 887 case UVC_VC_INPUT_TERMINAL:
c0efd232
LP
888 if (buflen < 8) {
889 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
890 "interface %d INPUT_TERMINAL error\n",
891 udev->devnum, alts->desc.bInterfaceNumber);
892 return -EINVAL;
893 }
894
895 /* Make sure the terminal type MSB is not null, otherwise it
896 * could be confused with a unit.
897 */
9bc6218d 898 type = get_unaligned_le16(&buffer[4]);
c0efd232
LP
899 if ((type & 0xff00) == 0) {
900 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
901 "interface %d INPUT_TERMINAL %d has invalid "
902 "type 0x%04x, skipping\n", udev->devnum,
903 alts->desc.bInterfaceNumber,
904 buffer[3], type);
905 return 0;
906 }
907
908 n = 0;
909 p = 0;
910 len = 8;
911
b482d923 912 if (type == UVC_ITT_CAMERA) {
c0efd232
LP
913 n = buflen >= 15 ? buffer[14] : 0;
914 len = 15;
915
b482d923 916 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
c0efd232
LP
917 n = buflen >= 9 ? buffer[8] : 0;
918 p = buflen >= 10 + n ? buffer[9+n] : 0;
919 len = 10;
920 }
921
922 if (buflen < len + n + p) {
923 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
924 "interface %d INPUT_TERMINAL error\n",
925 udev->devnum, alts->desc.bInterfaceNumber);
926 return -EINVAL;
927 }
928
929 term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
930 if (term == NULL)
931 return -ENOMEM;
932
933 term->id = buffer[3];
934 term->type = type | UVC_TERM_INPUT;
935
b482d923 936 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
c0efd232
LP
937 term->camera.bControlSize = n;
938 term->camera.bmControls = (__u8 *)term + sizeof *term;
939 term->camera.wObjectiveFocalLengthMin =
9bc6218d 940 get_unaligned_le16(&buffer[8]);
c0efd232 941 term->camera.wObjectiveFocalLengthMax =
9bc6218d 942 get_unaligned_le16(&buffer[10]);
c0efd232 943 term->camera.wOcularFocalLength =
9bc6218d 944 get_unaligned_le16(&buffer[12]);
c0efd232 945 memcpy(term->camera.bmControls, &buffer[15], n);
b482d923
LP
946 } else if (UVC_ENTITY_TYPE(term) ==
947 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
c0efd232
LP
948 term->media.bControlSize = n;
949 term->media.bmControls = (__u8 *)term + sizeof *term;
950 term->media.bTransportModeSize = p;
951 term->media.bmTransportModes = (__u8 *)term
952 + sizeof *term + n;
953 memcpy(term->media.bmControls, &buffer[9], n);
954 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
955 }
956
957 if (buffer[7] != 0)
958 usb_string(udev, buffer[7], term->name,
959 sizeof term->name);
b482d923 960 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
c0efd232 961 sprintf(term->name, "Camera %u", buffer[3]);
b482d923 962 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
c0efd232
LP
963 sprintf(term->name, "Media %u", buffer[3]);
964 else
965 sprintf(term->name, "Input %u", buffer[3]);
966
967 list_add_tail(&term->list, &dev->entities);
968 break;
969
b482d923 970 case UVC_VC_OUTPUT_TERMINAL:
c0efd232
LP
971 if (buflen < 9) {
972 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
973 "interface %d OUTPUT_TERMINAL error\n",
974 udev->devnum, alts->desc.bInterfaceNumber);
975 return -EINVAL;
976 }
977
978 /* Make sure the terminal type MSB is not null, otherwise it
979 * could be confused with a unit.
980 */
9bc6218d 981 type = get_unaligned_le16(&buffer[4]);
c0efd232
LP
982 if ((type & 0xff00) == 0) {
983 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
984 "interface %d OUTPUT_TERMINAL %d has invalid "
985 "type 0x%04x, skipping\n", udev->devnum,
986 alts->desc.bInterfaceNumber, buffer[3], type);
987 return 0;
988 }
989
990 term = kzalloc(sizeof *term, GFP_KERNEL);
991 if (term == NULL)
992 return -ENOMEM;
993
994 term->id = buffer[3];
995 term->type = type | UVC_TERM_OUTPUT;
996 term->output.bSourceID = buffer[7];
997
998 if (buffer[8] != 0)
999 usb_string(udev, buffer[8], term->name,
1000 sizeof term->name);
1001 else
1002 sprintf(term->name, "Output %u", buffer[3]);
1003
1004 list_add_tail(&term->list, &dev->entities);
1005 break;
1006
b482d923 1007 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
1008 p = buflen >= 5 ? buffer[4] : 0;
1009
1010 if (buflen < 5 || buflen < 6 + p) {
1011 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1012 "interface %d SELECTOR_UNIT error\n",
1013 udev->devnum, alts->desc.bInterfaceNumber);
1014 return -EINVAL;
1015 }
1016
1017 unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1018 if (unit == NULL)
1019 return -ENOMEM;
1020
1021 unit->id = buffer[3];
1022 unit->type = buffer[2];
1023 unit->selector.bNrInPins = buffer[4];
1024 unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1025 memcpy(unit->selector.baSourceID, &buffer[5], p);
1026
1027 if (buffer[5+p] != 0)
1028 usb_string(udev, buffer[5+p], unit->name,
1029 sizeof unit->name);
1030 else
1031 sprintf(unit->name, "Selector %u", buffer[3]);
1032
1033 list_add_tail(&unit->list, &dev->entities);
1034 break;
1035
b482d923 1036 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
1037 n = buflen >= 8 ? buffer[7] : 0;
1038 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1039
1040 if (buflen < p + n) {
1041 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1042 "interface %d PROCESSING_UNIT error\n",
1043 udev->devnum, alts->desc.bInterfaceNumber);
1044 return -EINVAL;
1045 }
1046
1047 unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1048 if (unit == NULL)
1049 return -ENOMEM;
1050
1051 unit->id = buffer[3];
1052 unit->type = buffer[2];
1053 unit->processing.bSourceID = buffer[4];
1054 unit->processing.wMaxMultiplier =
9bc6218d 1055 get_unaligned_le16(&buffer[5]);
c0efd232
LP
1056 unit->processing.bControlSize = buffer[7];
1057 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1058 memcpy(unit->processing.bmControls, &buffer[8], n);
1059 if (dev->uvc_version >= 0x0110)
1060 unit->processing.bmVideoStandards = buffer[9+n];
1061
1062 if (buffer[8+n] != 0)
1063 usb_string(udev, buffer[8+n], unit->name,
1064 sizeof unit->name);
1065 else
1066 sprintf(unit->name, "Processing %u", buffer[3]);
1067
1068 list_add_tail(&unit->list, &dev->entities);
1069 break;
1070
b482d923 1071 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
1072 p = buflen >= 22 ? buffer[21] : 0;
1073 n = buflen >= 24 + p ? buffer[22+p] : 0;
1074
1075 if (buflen < 24 + p + n) {
1076 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1077 "interface %d EXTENSION_UNIT error\n",
1078 udev->devnum, alts->desc.bInterfaceNumber);
1079 return -EINVAL;
1080 }
1081
1082 unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1083 if (unit == NULL)
1084 return -ENOMEM;
1085
1086 unit->id = buffer[3];
1087 unit->type = buffer[2];
1088 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1089 unit->extension.bNumControls = buffer[20];
9bc6218d 1090 unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
c0efd232
LP
1091 unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1092 memcpy(unit->extension.baSourceID, &buffer[22], p);
1093 unit->extension.bControlSize = buffer[22+p];
1094 unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1095 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1096
1097 if (buffer[23+p+n] != 0)
1098 usb_string(udev, buffer[23+p+n], unit->name,
1099 sizeof unit->name);
1100 else
1101 sprintf(unit->name, "Extension %u", buffer[3]);
1102
1103 list_add_tail(&unit->list, &dev->entities);
1104 break;
1105
1106 default:
1107 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1108 "descriptor (%u)\n", buffer[2]);
1109 break;
1110 }
1111
1112 return 0;
1113}
1114
1115static int uvc_parse_control(struct uvc_device *dev)
1116{
1117 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1118 unsigned char *buffer = alts->extra;
1119 int buflen = alts->extralen;
1120 int ret;
1121
1122 /* Parse the default alternate setting only, as the UVC specification
1123 * defines a single alternate setting, the default alternate setting
1124 * zero.
1125 */
1126
1127 while (buflen > 2) {
1128 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1129 buffer[1] != USB_DT_CS_INTERFACE)
1130 goto next_descriptor;
1131
1132 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1133 return ret;
1134
1135next_descriptor:
1136 buflen -= buffer[0];
1137 buffer += buffer[0];
1138 }
1139
538e7a00
LP
1140 /* Check if the optional status endpoint is present. Built-in iSight
1141 * webcams have an interrupt endpoint but spit proprietary data that
1142 * don't conform to the UVC status endpoint messages. Don't try to
1143 * handle the interrupt endpoint for those cameras.
1144 */
1145 if (alts->desc.bNumEndpoints == 1 &&
1146 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
c0efd232
LP
1147 struct usb_host_endpoint *ep = &alts->endpoint[0];
1148 struct usb_endpoint_descriptor *desc = &ep->desc;
1149
1150 if (usb_endpoint_is_int_in(desc) &&
1151 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1152 desc->bInterval != 0) {
1153 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1154 "(addr %02x).\n", desc->bEndpointAddress);
1155 dev->int_ep = ep;
1156 }
1157 }
1158
1159 return 0;
1160}
1161
1162/* ------------------------------------------------------------------------
1163 * USB probe and disconnect
1164 */
1165
1166/*
1167 * Unregister the video devices.
1168 */
1169static void uvc_unregister_video(struct uvc_device *dev)
1170{
35f02a68
LP
1171 struct uvc_streaming *streaming;
1172
1173 list_for_each_entry(streaming, &dev->streams, list) {
1174 if (streaming->vdev == NULL)
1175 continue;
1176
1177 if (streaming->vdev->minor == -1)
1178 video_device_release(streaming->vdev);
c0efd232 1179 else
35f02a68
LP
1180 video_unregister_device(streaming->vdev);
1181 streaming->vdev = NULL;
1182 }
1183}
1184
1185static int uvc_register_video(struct uvc_device *dev,
1186 struct uvc_streaming *stream)
1187{
1188 struct video_device *vdev;
1189 struct uvc_entity *term;
1190 int ret;
1191
1192 if (uvc_trace_param & UVC_TRACE_PROBE) {
1193 uvc_printk(KERN_INFO, "Found a valid video chain (");
1194 list_for_each_entry(term, &dev->video.iterms, chain) {
1195 printk("%d", term->id);
1196 if (term->chain.next != &dev->video.iterms)
1197 printk(",");
1198 }
1199 printk(" -> %d).\n", dev->video.oterm->id);
1200 }
1201
1202 /* Initialize the streaming interface with default streaming
1203 * parameters.
1204 */
1205 ret = uvc_video_init(stream);
1206 if (ret < 0) {
1207 uvc_printk(KERN_ERR, "Failed to initialize the device "
1208 "(%d).\n", ret);
1209 return ret;
1210 }
1211
1212 /* Register the device with V4L. */
1213 vdev = video_device_alloc();
1214 if (vdev == NULL)
1215 return -1;
1216
1217 /* We already hold a reference to dev->udev. The video device will be
1218 * unregistered before the reference is released, so we don't need to
1219 * get another one.
1220 */
1221 vdev->parent = &dev->intf->dev;
1222 vdev->minor = -1;
1223 vdev->fops = &uvc_fops;
1224 vdev->release = video_device_release;
1225 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1226
1227 /* Set the driver data before calling video_register_device, otherwise
1228 * uvc_v4l2_open might race us.
1229 */
1230 stream->vdev = vdev;
1231 video_set_drvdata(vdev, stream);
1232
1233 if (video_register_device(vdev, VFL_TYPE_GRABBER, -1) < 0) {
1234 stream->vdev = NULL;
1235 video_device_release(vdev);
1236 return -1;
c0efd232 1237 }
35f02a68
LP
1238
1239 return 0;
c0efd232
LP
1240}
1241
1242/*
1243 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1244 * and containing the following units:
1245 *
2c2d264b 1246 * - one Output Terminal (USB Streaming or Display)
c0efd232
LP
1247 * - zero or one Processing Unit
1248 * - zero, one or mode single-input Selector Units
1249 * - zero or one multiple-input Selector Units, provided all inputs are
1250 * connected to input terminals
1251 * - zero, one or mode single-input Extension Units
2c2d264b 1252 * - one or more Input Terminals (Camera, External or USB Streaming)
c0efd232
LP
1253 *
1254 * A side forward scan is made on each detected entity to check for additional
1255 * extension units.
1256 */
1257static int uvc_scan_chain_entity(struct uvc_video_device *video,
1258 struct uvc_entity *entity)
1259{
1260 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 1261 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
1262 if (uvc_trace_param & UVC_TRACE_PROBE)
1263 printk(" <- XU %d", entity->id);
1264
1265 if (entity->extension.bNrInPins != 1) {
1266 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1267 "than 1 input pin.\n", entity->id);
1268 return -1;
1269 }
1270
1271 list_add_tail(&entity->chain, &video->extensions);
1272 break;
1273
b482d923 1274 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
1275 if (uvc_trace_param & UVC_TRACE_PROBE)
1276 printk(" <- PU %d", entity->id);
1277
1278 if (video->processing != NULL) {
1279 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1280 "Processing Units in chain.\n");
1281 return -1;
1282 }
1283
1284 video->processing = entity;
1285 break;
1286
b482d923 1287 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
1288 if (uvc_trace_param & UVC_TRACE_PROBE)
1289 printk(" <- SU %d", entity->id);
1290
1291 /* Single-input selector units are ignored. */
1292 if (entity->selector.bNrInPins == 1)
1293 break;
1294
1295 if (video->selector != NULL) {
1296 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1297 "Units in chain.\n");
1298 return -1;
1299 }
1300
1301 video->selector = entity;
1302 break;
1303
b482d923
LP
1304 case UVC_ITT_VENDOR_SPECIFIC:
1305 case UVC_ITT_CAMERA:
1306 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
c0efd232
LP
1307 if (uvc_trace_param & UVC_TRACE_PROBE)
1308 printk(" <- IT %d\n", entity->id);
1309
1310 list_add_tail(&entity->chain, &video->iterms);
1311 break;
1312
b482d923 1313 case UVC_TT_STREAMING:
ff924203
LP
1314 if (uvc_trace_param & UVC_TRACE_PROBE)
1315 printk(" <- IT %d\n", entity->id);
1316
1317 if (!UVC_ENTITY_IS_ITERM(entity)) {
1318 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1319 "terminal %u.\n", entity->id);
1320 return -1;
1321 }
1322
1323 if (video->sterm != NULL) {
1324 uvc_trace(UVC_TRACE_DESCR, "Found multiple streaming "
1325 "entities in chain.\n");
1326 return -1;
1327 }
1328
1329 list_add_tail(&entity->chain, &video->iterms);
1330 video->sterm = entity;
1331 break;
1332
c0efd232
LP
1333 default:
1334 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1335 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1336 return -1;
1337 }
1338
1339 return 0;
1340}
1341
1342static int uvc_scan_chain_forward(struct uvc_video_device *video,
1343 struct uvc_entity *entity, struct uvc_entity *prev)
1344{
1345 struct uvc_entity *forward;
1346 int found;
1347
1348 /* Forward scan */
1349 forward = NULL;
1350 found = 0;
1351
1352 while (1) {
1353 forward = uvc_entity_by_reference(video->dev, entity->id,
1354 forward);
1355 if (forward == NULL)
1356 break;
1357
b482d923 1358 if (UVC_ENTITY_TYPE(forward) != UVC_VC_EXTENSION_UNIT ||
c0efd232
LP
1359 forward == prev)
1360 continue;
1361
1362 if (forward->extension.bNrInPins != 1) {
b2d9cc42 1363 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has "
c0efd232
LP
1364 "more than 1 input pin.\n", entity->id);
1365 return -1;
1366 }
1367
1368 list_add_tail(&forward->chain, &video->extensions);
1369 if (uvc_trace_param & UVC_TRACE_PROBE) {
1370 if (!found)
1371 printk(" (-> XU");
1372
1373 printk(" %d", forward->id);
1374 found = 1;
1375 }
1376 }
1377 if (found)
1378 printk(")");
1379
1380 return 0;
1381}
1382
1383static int uvc_scan_chain_backward(struct uvc_video_device *video,
1384 struct uvc_entity *entity)
1385{
1386 struct uvc_entity *term;
1387 int id = -1, i;
1388
1389 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 1390 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
1391 id = entity->extension.baSourceID[0];
1392 break;
1393
b482d923 1394 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
1395 id = entity->processing.bSourceID;
1396 break;
1397
b482d923 1398 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
1399 /* Single-input selector units are ignored. */
1400 if (entity->selector.bNrInPins == 1) {
1401 id = entity->selector.baSourceID[0];
1402 break;
1403 }
1404
1405 if (uvc_trace_param & UVC_TRACE_PROBE)
1406 printk(" <- IT");
1407
1408 video->selector = entity;
1409 for (i = 0; i < entity->selector.bNrInPins; ++i) {
1410 id = entity->selector.baSourceID[i];
1411 term = uvc_entity_by_id(video->dev, id);
1412 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1413 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1414 "input %d isn't connected to an "
1415 "input terminal\n", entity->id, i);
1416 return -1;
1417 }
1418
1419 if (uvc_trace_param & UVC_TRACE_PROBE)
1420 printk(" %d", term->id);
1421
1422 list_add_tail(&term->chain, &video->iterms);
1423 uvc_scan_chain_forward(video, term, entity);
1424 }
1425
1426 if (uvc_trace_param & UVC_TRACE_PROBE)
1427 printk("\n");
1428
1429 id = 0;
1430 break;
1431 }
1432
1433 return id;
1434}
1435
1436static int uvc_scan_chain(struct uvc_video_device *video)
1437{
1438 struct uvc_entity *entity, *prev;
1439 int id;
1440
1441 entity = video->oterm;
1442 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
ff924203 1443
b482d923 1444 if (UVC_ENTITY_TYPE(entity) == UVC_TT_STREAMING)
ff924203
LP
1445 video->sterm = entity;
1446
c0efd232
LP
1447 id = entity->output.bSourceID;
1448 while (id != 0) {
1449 prev = entity;
1450 entity = uvc_entity_by_id(video->dev, id);
1451 if (entity == NULL) {
1452 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1453 "unknown entity %d.\n", id);
1454 return -1;
1455 }
1456
1457 /* Process entity */
1458 if (uvc_scan_chain_entity(video, entity) < 0)
1459 return -1;
1460
1461 /* Forward scan */
1462 if (uvc_scan_chain_forward(video, entity, prev) < 0)
1463 return -1;
1464
1465 /* Stop when a terminal is found. */
1466 if (!UVC_ENTITY_IS_UNIT(entity))
1467 break;
1468
1469 /* Backward scan */
1470 id = uvc_scan_chain_backward(video, entity);
1471 if (id < 0)
1472 return id;
1473 }
1474
ff924203
LP
1475 if (video->sterm == NULL) {
1476 uvc_trace(UVC_TRACE_DESCR, "No streaming entity found in "
1477 "chain.\n");
1478 return -1;
1479 }
c0efd232
LP
1480
1481 return 0;
1482}
1483
1484/*
35f02a68 1485 * Scan the device for video chains and register video devices.
c0efd232
LP
1486 *
1487 * The driver currently supports a single video device per control interface
1488 * only. The terminal and units must match the following structure:
1489 *
ff924203
LP
1490 * ITT_* -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1491 * TT_STREAMING -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> OTT_*
c0efd232
LP
1492 *
1493 * The Extension Units, if present, must have a single input pin. The
1494 * Processing Unit and Extension Units can be in any order. Additional
1495 * Extension Units connected to the main chain as single-unit branches are
1496 * also supported.
1497 */
35f02a68 1498static int uvc_scan_device(struct uvc_device *dev)
c0efd232 1499{
c0efd232 1500 struct uvc_entity *term;
35f02a68 1501 int found = 0;
c0efd232
LP
1502
1503 /* Check if the control interface matches the structure we expect. */
1504 list_for_each_entry(term, &dev->entities, list) {
35f02a68 1505 struct uvc_streaming *stream;
c0efd232 1506
ff924203 1507 if (!UVC_ENTITY_IS_TERM(term) || !UVC_ENTITY_IS_OTERM(term))
c0efd232
LP
1508 continue;
1509
1510 memset(&dev->video, 0, sizeof dev->video);
1511 mutex_init(&dev->video.ctrl_mutex);
1512 INIT_LIST_HEAD(&dev->video.iterms);
1513 INIT_LIST_HEAD(&dev->video.extensions);
1514 dev->video.oterm = term;
1515 dev->video.dev = dev;
1516 if (uvc_scan_chain(&dev->video) < 0)
1517 continue;
1518
35f02a68
LP
1519 list_for_each_entry(stream, &dev->streams, list) {
1520 if (stream->header.bTerminalLink ==
ff924203 1521 dev->video.sterm->id) {
35f02a68 1522 uvc_register_video(dev, stream);
c0efd232
LP
1523 found = 1;
1524 break;
1525 }
1526 }
c0efd232
LP
1527 }
1528
1529 if (!found) {
1530 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1531 return -1;
1532 }
1533
c0efd232
LP
1534 return 0;
1535}
1536
1537/*
1538 * Delete the UVC device.
1539 *
1540 * Called by the kernel when the last reference to the uvc_device structure
1541 * is released.
1542 *
1543 * Unregistering the video devices is done here because every opened instance
1544 * must be closed before the device can be unregistered. An alternative would
1545 * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1546 * unregister the video devices on disconnect when that reference count drops
1547 * to zero.
1548 *
1549 * As this function is called after or during disconnect(), all URBs have
1550 * already been canceled by the USB core. There is no need to kill the
1551 * interrupt URB manually.
1552 */
1553void uvc_delete(struct kref *kref)
1554{
1555 struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1556 struct list_head *p, *n;
1557
2c2d264b 1558 /* Unregister the video device. */
c0efd232
LP
1559 uvc_unregister_video(dev);
1560 usb_put_intf(dev->intf);
1561 usb_put_dev(dev->udev);
1562
1563 uvc_status_cleanup(dev);
1564 uvc_ctrl_cleanup_device(dev);
1565
1566 list_for_each_safe(p, n, &dev->entities) {
1567 struct uvc_entity *entity;
1568 entity = list_entry(p, struct uvc_entity, list);
1569 kfree(entity);
1570 }
1571
35f02a68 1572 list_for_each_safe(p, n, &dev->streams) {
c0efd232
LP
1573 struct uvc_streaming *streaming;
1574 streaming = list_entry(p, struct uvc_streaming, list);
1575 usb_driver_release_interface(&uvc_driver.driver,
1576 streaming->intf);
1577 usb_put_intf(streaming->intf);
1578 kfree(streaming->format);
1579 kfree(streaming->header.bmaControls);
1580 kfree(streaming);
1581 }
1582
1583 kfree(dev);
1584}
1585
1586static int uvc_probe(struct usb_interface *intf,
1587 const struct usb_device_id *id)
1588{
1589 struct usb_device *udev = interface_to_usbdev(intf);
1590 struct uvc_device *dev;
1591 int ret;
1592
1593 if (id->idVendor && id->idProduct)
1594 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1595 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1596 id->idProduct);
1597 else
1598 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1599 udev->devpath);
1600
2c2d264b 1601 /* Allocate memory for the device and initialize it. */
c0efd232
LP
1602 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1603 return -ENOMEM;
1604
1605 INIT_LIST_HEAD(&dev->entities);
35f02a68 1606 INIT_LIST_HEAD(&dev->streams);
c0efd232 1607 kref_init(&dev->kref);
04a37e0f 1608 atomic_set(&dev->users, 0);
c0efd232
LP
1609
1610 dev->udev = usb_get_dev(udev);
1611 dev->intf = usb_get_intf(intf);
1612 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1613 dev->quirks = id->driver_info | uvc_quirks_param;
1614
1615 if (udev->product != NULL)
d0ebf307 1616 strlcpy(dev->name, udev->product, sizeof dev->name);
c0efd232
LP
1617 else
1618 snprintf(dev->name, sizeof dev->name,
1619 "UVC Camera (%04x:%04x)",
1620 le16_to_cpu(udev->descriptor.idVendor),
1621 le16_to_cpu(udev->descriptor.idProduct));
1622
2c2d264b 1623 /* Parse the Video Class control descriptor. */
c0efd232
LP
1624 if (uvc_parse_control(dev) < 0) {
1625 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1626 "descriptors.\n");
1627 goto error;
1628 }
1629
fba4578e 1630 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
c0efd232
LP
1631 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1632 udev->product ? udev->product : "<unnamed>",
1633 le16_to_cpu(udev->descriptor.idVendor),
1634 le16_to_cpu(udev->descriptor.idProduct));
1635
1636 if (uvc_quirks_param != 0) {
1637 uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1638 "parameter for testing purpose.\n", uvc_quirks_param);
1639 uvc_printk(KERN_INFO, "Please report required quirks to the "
1640 "linux-uvc-devel mailing list.\n");
1641 }
1642
2c2d264b 1643 /* Initialize controls. */
c0efd232
LP
1644 if (uvc_ctrl_init_device(dev) < 0)
1645 goto error;
1646
35f02a68
LP
1647 /* Scan the device for video chains and register video devices. */
1648 if (uvc_scan_device(dev) < 0)
c0efd232
LP
1649 goto error;
1650
2c2d264b 1651 /* Save our data pointer in the interface data. */
c0efd232
LP
1652 usb_set_intfdata(intf, dev);
1653
2c2d264b 1654 /* Initialize the interrupt URB. */
c0efd232
LP
1655 if ((ret = uvc_status_init(dev)) < 0) {
1656 uvc_printk(KERN_INFO, "Unable to initialize the status "
1657 "endpoint (%d), status interrupt will not be "
1658 "supported.\n", ret);
1659 }
1660
1661 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1662 return 0;
1663
1664error:
1665 kref_put(&dev->kref, uvc_delete);
1666 return -ENODEV;
1667}
1668
1669static void uvc_disconnect(struct usb_interface *intf)
1670{
1671 struct uvc_device *dev = usb_get_intfdata(intf);
1672
1673 /* Set the USB interface data to NULL. This can be done outside the
1674 * lock, as there's no other reader.
1675 */
1676 usb_set_intfdata(intf, NULL);
1677
b482d923
LP
1678 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1679 UVC_SC_VIDEOSTREAMING)
c0efd232
LP
1680 return;
1681
1682 /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1683 * lock is needed to prevent uvc_disconnect from releasing its
1684 * reference to the uvc_device instance after uvc_v4l2_open() received
1685 * the pointer to the device (video_devdata) but before it got the
1686 * chance to increase the reference count (kref_get).
a9e28585
LP
1687 *
1688 * Note that the reference can't be released with the lock held,
1689 * otherwise a AB-BA deadlock can occur with videodev_lock that
1690 * videodev acquires in videodev_open() and video_unregister_device().
c0efd232
LP
1691 */
1692 mutex_lock(&uvc_driver.open_mutex);
c0efd232 1693 dev->state |= UVC_DEV_DISCONNECTED;
c0efd232 1694 mutex_unlock(&uvc_driver.open_mutex);
a9e28585
LP
1695
1696 kref_put(&dev->kref, uvc_delete);
c0efd232
LP
1697}
1698
1699static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1700{
1701 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 1702 struct uvc_streaming *stream;
c0efd232
LP
1703
1704 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1705 intf->cur_altsetting->desc.bInterfaceNumber);
1706
1707 /* Controls are cached on the fly so they don't need to be saved. */
b482d923
LP
1708 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1709 UVC_SC_VIDEOCONTROL)
c0efd232
LP
1710 return uvc_status_suspend(dev);
1711
35f02a68
LP
1712 list_for_each_entry(stream, &dev->streams, list) {
1713 if (stream->intf == intf)
1714 return uvc_video_suspend(stream);
c0efd232
LP
1715 }
1716
35f02a68
LP
1717 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1718 "mismatch.\n");
1719 return -EINVAL;
c0efd232
LP
1720}
1721
9b0ae867 1722static int __uvc_resume(struct usb_interface *intf, int reset)
c0efd232
LP
1723{
1724 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 1725 struct uvc_streaming *stream;
c0efd232
LP
1726
1727 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1728 intf->cur_altsetting->desc.bInterfaceNumber);
1729
b482d923
LP
1730 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1731 UVC_SC_VIDEOCONTROL) {
7564f67d
HV
1732 if (reset) {
1733 int ret = uvc_ctrl_resume_device(dev);
1734
1735 if (ret < 0)
1736 return ret;
1737 }
c0efd232
LP
1738
1739 return uvc_status_resume(dev);
1740 }
1741
35f02a68
LP
1742 list_for_each_entry(stream, &dev->streams, list) {
1743 if (stream->intf == intf)
1744 return uvc_video_resume(stream);
c0efd232
LP
1745 }
1746
35f02a68
LP
1747 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1748 "mismatch.\n");
1749 return -EINVAL;
c0efd232
LP
1750}
1751
9b0ae867
LP
1752static int uvc_resume(struct usb_interface *intf)
1753{
1754 return __uvc_resume(intf, 0);
1755}
1756
1757static int uvc_reset_resume(struct usb_interface *intf)
1758{
1759 return __uvc_resume(intf, 1);
1760}
1761
c0efd232
LP
1762/* ------------------------------------------------------------------------
1763 * Driver initialization and cleanup
1764 */
1765
1766/*
1767 * The Logitech cameras listed below have their interface class set to
1768 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1769 * though they are compliant.
1770 */
1771static struct usb_device_id uvc_ids[] = {
c0efd232
LP
1772 /* Microsoft Lifecam NX-6000 */
1773 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1774 | USB_DEVICE_ID_MATCH_INT_INFO,
1775 .idVendor = 0x045e,
1776 .idProduct = 0x00f8,
1777 .bInterfaceClass = USB_CLASS_VIDEO,
1778 .bInterfaceSubClass = 1,
1779 .bInterfaceProtocol = 0,
1780 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1781 /* Microsoft Lifecam VX-7000 */
1782 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1783 | USB_DEVICE_ID_MATCH_INT_INFO,
1784 .idVendor = 0x045e,
1785 .idProduct = 0x0723,
1786 .bInterfaceClass = USB_CLASS_VIDEO,
1787 .bInterfaceSubClass = 1,
1788 .bInterfaceProtocol = 0,
1789 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1790 /* Logitech Quickcam Fusion */
1791 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1792 | USB_DEVICE_ID_MATCH_INT_INFO,
1793 .idVendor = 0x046d,
1794 .idProduct = 0x08c1,
1795 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1796 .bInterfaceSubClass = 1,
1797 .bInterfaceProtocol = 0 },
1798 /* Logitech Quickcam Orbit MP */
1799 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1800 | USB_DEVICE_ID_MATCH_INT_INFO,
1801 .idVendor = 0x046d,
1802 .idProduct = 0x08c2,
1803 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1804 .bInterfaceSubClass = 1,
1805 .bInterfaceProtocol = 0 },
1806 /* Logitech Quickcam Pro for Notebook */
1807 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1808 | USB_DEVICE_ID_MATCH_INT_INFO,
1809 .idVendor = 0x046d,
1810 .idProduct = 0x08c3,
1811 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1812 .bInterfaceSubClass = 1,
1813 .bInterfaceProtocol = 0 },
1814 /* Logitech Quickcam Pro 5000 */
1815 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1816 | USB_DEVICE_ID_MATCH_INT_INFO,
1817 .idVendor = 0x046d,
1818 .idProduct = 0x08c5,
1819 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1820 .bInterfaceSubClass = 1,
1821 .bInterfaceProtocol = 0 },
1822 /* Logitech Quickcam OEM Dell Notebook */
1823 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1824 | USB_DEVICE_ID_MATCH_INT_INFO,
1825 .idVendor = 0x046d,
1826 .idProduct = 0x08c6,
1827 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1828 .bInterfaceSubClass = 1,
1829 .bInterfaceProtocol = 0 },
1830 /* Logitech Quickcam OEM Cisco VT Camera II */
1831 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1832 | USB_DEVICE_ID_MATCH_INT_INFO,
1833 .idVendor = 0x046d,
1834 .idProduct = 0x08c7,
1835 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1836 .bInterfaceSubClass = 1,
1837 .bInterfaceProtocol = 0 },
f61d1d8a
LP
1838 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1839 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1840 | USB_DEVICE_ID_MATCH_INT_INFO,
1841 .idVendor = 0x058f,
1842 .idProduct = 0x3820,
1843 .bInterfaceClass = USB_CLASS_VIDEO,
1844 .bInterfaceSubClass = 1,
1845 .bInterfaceProtocol = 0,
1846 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232 1847 /* Apple Built-In iSight */
2c2d264b 1848 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232
LP
1849 | USB_DEVICE_ID_MATCH_INT_INFO,
1850 .idVendor = 0x05ac,
1851 .idProduct = 0x8501,
2c2d264b
LP
1852 .bInterfaceClass = USB_CLASS_VIDEO,
1853 .bInterfaceSubClass = 1,
1854 .bInterfaceProtocol = 0,
c0efd232
LP
1855 .driver_info = UVC_QUIRK_PROBE_MINMAX
1856 | UVC_QUIRK_BUILTIN_ISIGHT },
1857 /* Genesys Logic USB 2.0 PC Camera */
2c2d264b 1858 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232 1859 | USB_DEVICE_ID_MATCH_INT_INFO,
2c2d264b
LP
1860 .idVendor = 0x05e3,
1861 .idProduct = 0x0505,
1862 .bInterfaceClass = USB_CLASS_VIDEO,
1863 .bInterfaceSubClass = 1,
1864 .bInterfaceProtocol = 0,
1865 .driver_info = UVC_QUIRK_STREAM_NO_FID },
d79cd839
LP
1866 /* ViMicro Vega */
1867 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1868 | USB_DEVICE_ID_MATCH_INT_INFO,
1869 .idVendor = 0x0ac8,
1870 .idProduct = 0x332d,
1871 .bInterfaceClass = USB_CLASS_VIDEO,
1872 .bInterfaceSubClass = 1,
1873 .bInterfaceProtocol = 0,
1874 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
1875 /* ViMicro - Minoru3D */
1876 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1877 | USB_DEVICE_ID_MATCH_INT_INFO,
1878 .idVendor = 0x0ac8,
1879 .idProduct = 0x3410,
1880 .bInterfaceClass = USB_CLASS_VIDEO,
1881 .bInterfaceSubClass = 1,
1882 .bInterfaceProtocol = 0,
1883 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
1884 /* ViMicro Venus - Minoru3D */
1885 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
50144aee
LP
1886 | USB_DEVICE_ID_MATCH_INT_INFO,
1887 .idVendor = 0x0ac8,
d79cd839 1888 .idProduct = 0x3420,
50144aee
LP
1889 .bInterfaceClass = USB_CLASS_VIDEO,
1890 .bInterfaceSubClass = 1,
1891 .bInterfaceProtocol = 0,
1892 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
c0efd232
LP
1893 /* MT6227 */
1894 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1895 | USB_DEVICE_ID_MATCH_INT_INFO,
1896 .idVendor = 0x0e8d,
1897 .idProduct = 0x0004,
1898 .bInterfaceClass = USB_CLASS_VIDEO,
1899 .bInterfaceSubClass = 1,
1900 .bInterfaceProtocol = 0,
bab6f66c
LP
1901 .driver_info = UVC_QUIRK_PROBE_MINMAX
1902 | UVC_QUIRK_PROBE_DEF },
c0efd232
LP
1903 /* Syntek (HP Spartan) */
1904 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1905 | USB_DEVICE_ID_MATCH_INT_INFO,
1906 .idVendor = 0x174f,
1907 .idProduct = 0x5212,
1908 .bInterfaceClass = USB_CLASS_VIDEO,
1909 .bInterfaceSubClass = 1,
1910 .bInterfaceProtocol = 0,
25e69850 1911 .driver_info = UVC_QUIRK_STREAM_NO_FID },
562f0fed
LP
1912 /* Syntek (Samsung Q310) */
1913 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1914 | USB_DEVICE_ID_MATCH_INT_INFO,
1915 .idVendor = 0x174f,
1916 .idProduct = 0x5931,
1917 .bInterfaceClass = USB_CLASS_VIDEO,
1918 .bInterfaceSubClass = 1,
1919 .bInterfaceProtocol = 0,
1920 .driver_info = UVC_QUIRK_STREAM_NO_FID },
f61d1d8a 1921 /* Syntek (Asus F9SG) */
25e69850
LP
1922 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1923 | USB_DEVICE_ID_MATCH_INT_INFO,
1924 .idVendor = 0x174f,
1925 .idProduct = 0x8a31,
1926 .bInterfaceClass = USB_CLASS_VIDEO,
1927 .bInterfaceSubClass = 1,
1928 .bInterfaceProtocol = 0,
c0efd232
LP
1929 .driver_info = UVC_QUIRK_STREAM_NO_FID },
1930 /* Syntek (Asus U3S) */
1931 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1932 | USB_DEVICE_ID_MATCH_INT_INFO,
1933 .idVendor = 0x174f,
1934 .idProduct = 0x8a33,
1935 .bInterfaceClass = USB_CLASS_VIDEO,
1936 .bInterfaceSubClass = 1,
1937 .bInterfaceProtocol = 0,
1938 .driver_info = UVC_QUIRK_STREAM_NO_FID },
0ce566da
LP
1939 /* Syntek (JAOtech Smart Terminal) */
1940 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1941 | USB_DEVICE_ID_MATCH_INT_INFO,
1942 .idVendor = 0x174f,
1943 .idProduct = 0x8a34,
1944 .bInterfaceClass = USB_CLASS_VIDEO,
1945 .bInterfaceSubClass = 1,
1946 .bInterfaceProtocol = 0,
1947 .driver_info = UVC_QUIRK_STREAM_NO_FID },
849a3aba 1948 /* Lenovo Thinkpad SL400/SL500 */
2f38483b
LP
1949 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1950 | USB_DEVICE_ID_MATCH_INT_INFO,
1951 .idVendor = 0x17ef,
1952 .idProduct = 0x480b,
1953 .bInterfaceClass = USB_CLASS_VIDEO,
1954 .bInterfaceSubClass = 1,
1955 .bInterfaceProtocol = 0,
1956 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2d2bf2a3
LP
1957 /* Aveo Technology USB 2.0 Camera */
1958 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1959 | USB_DEVICE_ID_MATCH_INT_INFO,
1960 .idVendor = 0x1871,
1961 .idProduct = 0x0306,
1962 .bInterfaceClass = USB_CLASS_VIDEO,
1963 .bInterfaceSubClass = 1,
1964 .bInterfaceProtocol = 0,
5bdf1377
LP
1965 .driver_info = UVC_QUIRK_PROBE_MINMAX
1966 | UVC_QUIRK_PROBE_EXTRAFIELDS },
c0efd232
LP
1967 /* Ecamm Pico iMage */
1968 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1969 | USB_DEVICE_ID_MATCH_INT_INFO,
1970 .idVendor = 0x18cd,
1971 .idProduct = 0xcafe,
1972 .bInterfaceClass = USB_CLASS_VIDEO,
1973 .bInterfaceSubClass = 1,
1974 .bInterfaceProtocol = 0,
1975 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
ca4a3456
LP
1976 /* FSC WebCam V30S */
1977 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1978 | USB_DEVICE_ID_MATCH_INT_INFO,
1979 .idVendor = 0x18ec,
1980 .idProduct = 0x3288,
1981 .bInterfaceClass = USB_CLASS_VIDEO,
1982 .bInterfaceSubClass = 1,
1983 .bInterfaceProtocol = 0,
1984 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
1985 /* Bodelin ProScopeHR */
1986 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1987 | USB_DEVICE_ID_MATCH_DEV_HI
1988 | USB_DEVICE_ID_MATCH_INT_INFO,
1989 .idVendor = 0x19ab,
1990 .idProduct = 0x1000,
1991 .bcdDevice_hi = 0x0126,
1992 .bInterfaceClass = USB_CLASS_VIDEO,
1993 .bInterfaceSubClass = 1,
1994 .bInterfaceProtocol = 0,
1995 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
1996 /* SiGma Micro USB Web Camera */
1997 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
1998 | USB_DEVICE_ID_MATCH_INT_INFO,
1999 .idVendor = 0x1c4f,
2000 .idProduct = 0x3000,
2001 .bInterfaceClass = USB_CLASS_VIDEO,
2002 .bInterfaceSubClass = 1,
2003 .bInterfaceProtocol = 0,
2004 .driver_info = UVC_QUIRK_PROBE_MINMAX
d732c44c 2005 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
c0efd232
LP
2006 /* Generic USB Video Class */
2007 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2008 {}
2009};
2010
2011MODULE_DEVICE_TABLE(usb, uvc_ids);
2012
2013struct uvc_driver uvc_driver = {
2014 .driver = {
2015 .name = "uvcvideo",
2016 .probe = uvc_probe,
2017 .disconnect = uvc_disconnect,
2018 .suspend = uvc_suspend,
2019 .resume = uvc_resume,
9b0ae867 2020 .reset_resume = uvc_reset_resume,
c0efd232
LP
2021 .id_table = uvc_ids,
2022 .supports_autosuspend = 1,
2023 },
2024};
2025
2026static int __init uvc_init(void)
2027{
2028 int result;
2029
2030 INIT_LIST_HEAD(&uvc_driver.devices);
2031 INIT_LIST_HEAD(&uvc_driver.controls);
2032 mutex_init(&uvc_driver.open_mutex);
2033 mutex_init(&uvc_driver.ctrl_mutex);
2034
2035 uvc_ctrl_init();
2036
2037 result = usb_register(&uvc_driver.driver);
2038 if (result == 0)
2039 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2040 return result;
2041}
2042
2043static void __exit uvc_cleanup(void)
2044{
2045 usb_deregister(&uvc_driver.driver);
2046}
2047
2048module_init(uvc_init);
2049module_exit(uvc_cleanup);
2050
0fbd8ee6
LP
2051module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2052MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
c0efd232
LP
2053module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2054MODULE_PARM_DESC(quirks, "Forced device quirks");
2055module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2056MODULE_PARM_DESC(trace, "Trace level bitmask");
2057
2058MODULE_AUTHOR(DRIVER_AUTHOR);
2059MODULE_DESCRIPTION(DRIVER_DESC);
2060MODULE_LICENSE("GPL");
2061MODULE_VERSION(DRIVER_VERSION);
f87086e3 2062
This page took 0.26235 seconds and 5 git commands to generate.