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