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