Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / usb / uvc / uvc_driver.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_driver.c -- USB Video Class driver
3 *
11fc5baf
LP
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
c0efd232
LP
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
66ede038 14#include <linux/atomic.h>
c0efd232 15#include <linux/kernel.h>
c0efd232
LP
16#include <linux/list.h>
17#include <linux/module.h>
5a0e3ad6 18#include <linux/slab.h>
c0efd232
LP
19#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
fd3e5824 23#include <linux/version.h>
9bc6218d 24#include <asm/unaligned.h>
c0efd232
LP
25
26#include <media/v4l2-common.h>
27
28#include "uvcvideo.h"
29
11fc5baf
LP
30#define DRIVER_AUTHOR "Laurent Pinchart " \
31 "<laurent.pinchart@ideasonboard.com>"
c0efd232 32#define DRIVER_DESC "USB Video Class driver"
c0efd232 33
310fe524 34unsigned int uvc_clock_param = CLOCK_MONOTONIC;
0fbd8ee6 35unsigned int uvc_no_drop_param;
73de3592 36static unsigned int uvc_quirks_param = -1;
c0efd232 37unsigned int uvc_trace_param;
b232a012 38unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
c0efd232
LP
39
40/* ------------------------------------------------------------------------
2c2d264b 41 * Video formats
c0efd232
LP
42 */
43
44static struct uvc_format_desc uvc_fmts[] = {
45 {
46 .name = "YUV 4:2:2 (YUYV)",
47 .guid = UVC_GUID_FORMAT_YUY2,
48 .fcc = V4L2_PIX_FMT_YUYV,
49 },
68f194e0
DR
50 {
51 .name = "YUV 4:2:2 (YUYV)",
52 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT,
53 .fcc = V4L2_PIX_FMT_YUYV,
54 },
c0efd232
LP
55 {
56 .name = "YUV 4:2:0 (NV12)",
57 .guid = UVC_GUID_FORMAT_NV12,
58 .fcc = V4L2_PIX_FMT_NV12,
59 },
60 {
61 .name = "MJPEG",
62 .guid = UVC_GUID_FORMAT_MJPEG,
63 .fcc = V4L2_PIX_FMT_MJPEG,
64 },
65 {
66 .name = "YVU 4:2:0 (YV12)",
67 .guid = UVC_GUID_FORMAT_YV12,
68 .fcc = V4L2_PIX_FMT_YVU420,
69 },
70 {
71 .name = "YUV 4:2:0 (I420)",
72 .guid = UVC_GUID_FORMAT_I420,
73 .fcc = V4L2_PIX_FMT_YUV420,
74 },
7225a1dc
HG
75 {
76 .name = "YUV 4:2:0 (M420)",
77 .guid = UVC_GUID_FORMAT_M420,
78 .fcc = V4L2_PIX_FMT_M420,
79 },
c0efd232
LP
80 {
81 .name = "YUV 4:2:2 (UYVY)",
82 .guid = UVC_GUID_FORMAT_UYVY,
83 .fcc = V4L2_PIX_FMT_UYVY,
84 },
85 {
ca47e719 86 .name = "Greyscale 8-bit (Y800)",
c0efd232
LP
87 .guid = UVC_GUID_FORMAT_Y800,
88 .fcc = V4L2_PIX_FMT_GREY,
89 },
61421206 90 {
ca47e719
SM
91 .name = "Greyscale 8-bit (Y8 )",
92 .guid = UVC_GUID_FORMAT_Y8,
93 .fcc = V4L2_PIX_FMT_GREY,
94 },
95 {
96 .name = "Greyscale 10-bit (Y10 )",
97 .guid = UVC_GUID_FORMAT_Y10,
98 .fcc = V4L2_PIX_FMT_Y10,
99 },
100 {
101 .name = "Greyscale 12-bit (Y12 )",
102 .guid = UVC_GUID_FORMAT_Y12,
103 .fcc = V4L2_PIX_FMT_Y12,
104 },
105 {
106 .name = "Greyscale 16-bit (Y16 )",
61421206
LP
107 .guid = UVC_GUID_FORMAT_Y16,
108 .fcc = V4L2_PIX_FMT_Y16,
109 },
c0efd232
LP
110 {
111 .name = "RGB Bayer",
112 .guid = UVC_GUID_FORMAT_BY8,
113 .fcc = V4L2_PIX_FMT_SBGGR8,
114 },
50791079
LP
115 {
116 .name = "RGB565",
117 .guid = UVC_GUID_FORMAT_RGBP,
118 .fcc = V4L2_PIX_FMT_RGB565,
119 },
25ad8a8d
SL
120 {
121 .name = "H.264",
122 .guid = UVC_GUID_FORMAT_H264,
123 .fcc = V4L2_PIX_FMT_H264,
124 },
c0efd232
LP
125};
126
127/* ------------------------------------------------------------------------
128 * Utility functions
129 */
130
131struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
132 __u8 epaddr)
133{
134 struct usb_host_endpoint *ep;
135 unsigned int i;
136
137 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
138 ep = &alts->endpoint[i];
139 if (ep->desc.bEndpointAddress == epaddr)
140 return ep;
141 }
142
143 return NULL;
144}
145
146static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
147{
148 unsigned int len = ARRAY_SIZE(uvc_fmts);
149 unsigned int i;
150
151 for (i = 0; i < len; ++i) {
152 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
153 return &uvc_fmts[i];
154 }
155
156 return NULL;
157}
158
159static __u32 uvc_colorspace(const __u8 primaries)
160{
161 static const __u8 colorprimaries[] = {
162 0,
163 V4L2_COLORSPACE_SRGB,
164 V4L2_COLORSPACE_470_SYSTEM_M,
165 V4L2_COLORSPACE_470_SYSTEM_BG,
166 V4L2_COLORSPACE_SMPTE170M,
167 V4L2_COLORSPACE_SMPTE240M,
168 };
169
170 if (primaries < ARRAY_SIZE(colorprimaries))
171 return colorprimaries[primaries];
172
173 return 0;
174}
175
176/* Simplify a fraction using a simple continued fraction decomposition. The
177 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
178 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
179 * arbitrary parameters to remove non-significative terms from the simple
180 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
181 * respectively seems to give nice results.
182 */
183void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
184 unsigned int n_terms, unsigned int threshold)
185{
186 uint32_t *an;
187 uint32_t x, y, r;
188 unsigned int i, n;
189
190 an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
191 if (an == NULL)
192 return;
193
194 /* Convert the fraction to a simple continued fraction. See
195 * http://mathforum.org/dr.math/faq/faq.fractions.html
196 * Stop if the current term is bigger than or equal to the given
197 * threshold.
198 */
199 x = *numerator;
200 y = *denominator;
201
202 for (n = 0; n < n_terms && y != 0; ++n) {
203 an[n] = x / y;
204 if (an[n] >= threshold) {
205 if (n < 2)
206 n++;
207 break;
208 }
209
210 r = x - an[n] * y;
211 x = y;
212 y = r;
213 }
214
215 /* Expand the simple continued fraction back to an integer fraction. */
216 x = 0;
217 y = 1;
218
219 for (i = n; i > 0; --i) {
220 r = y;
221 y = an[i-1] * y + x;
222 x = r;
223 }
224
225 *numerator = y;
226 *denominator = x;
227 kfree(an);
228}
229
230/* Convert a fraction to a frame interval in 100ns multiples. The idea here is
231 * to compute numerator / denominator * 10000000 using 32 bit fixed point
232 * arithmetic only.
233 */
234uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
235{
236 uint32_t multiplier;
237
238 /* Saturate the result if the operation would overflow. */
239 if (denominator == 0 ||
240 numerator/denominator >= ((uint32_t)-1)/10000000)
241 return (uint32_t)-1;
242
243 /* Divide both the denominator and the multiplier by two until
244 * numerator * multiplier doesn't overflow. If anyone knows a better
245 * algorithm please let me know.
246 */
247 multiplier = 10000000;
248 while (numerator > ((uint32_t)-1)/multiplier) {
249 multiplier /= 2;
250 denominator /= 2;
251 }
252
253 return denominator ? numerator * multiplier / denominator : 0;
254}
255
256/* ------------------------------------------------------------------------
257 * Terminal and unit management
258 */
259
4ffc2d89 260struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
c0efd232
LP
261{
262 struct uvc_entity *entity;
263
264 list_for_each_entry(entity, &dev->entities, list) {
265 if (entity->id == id)
266 return entity;
267 }
268
269 return NULL;
270}
271
272static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
273 int id, struct uvc_entity *entity)
274{
275 unsigned int i;
276
277 if (entity == NULL)
278 entity = list_entry(&dev->entities, struct uvc_entity, list);
279
280 list_for_each_entry_continue(entity, &dev->entities, list) {
8ca5a639
LP
281 for (i = 0; i < entity->bNrInPins; ++i)
282 if (entity->baSourceID[i] == id)
c0efd232 283 return entity;
c0efd232
LP
284 }
285
286 return NULL;
287}
288
8e113595
LP
289static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
290{
291 struct uvc_streaming *stream;
292
293 list_for_each_entry(stream, &dev->streams, list) {
294 if (stream->header.bTerminalLink == id)
295 return stream;
296 }
297
298 return NULL;
299}
300
c0efd232 301/* ------------------------------------------------------------------------
8e113595 302 * Descriptors parsing
c0efd232
LP
303 */
304
305static int uvc_parse_format(struct uvc_device *dev,
306 struct uvc_streaming *streaming, struct uvc_format *format,
307 __u32 **intervals, unsigned char *buffer, int buflen)
308{
309 struct usb_interface *intf = streaming->intf;
310 struct usb_host_interface *alts = intf->cur_altsetting;
311 struct uvc_format_desc *fmtdesc;
312 struct uvc_frame *frame;
313 const unsigned char *start = buffer;
314 unsigned int interval;
315 unsigned int i, n;
316 __u8 ftype;
317
318 format->type = buffer[2];
319 format->index = buffer[3];
320
321 switch (buffer[2]) {
b482d923
LP
322 case UVC_VS_FORMAT_UNCOMPRESSED:
323 case UVC_VS_FORMAT_FRAME_BASED:
324 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
233548a2 325 if (buflen < n) {
b2d9cc42 326 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
327 "interface %d FORMAT error\n",
328 dev->udev->devnum,
329 alts->desc.bInterfaceNumber);
330 return -EINVAL;
331 }
332
333 /* Find the format descriptor from its GUID. */
334 fmtdesc = uvc_format_by_guid(&buffer[5]);
335
336 if (fmtdesc != NULL) {
d0ebf307 337 strlcpy(format->name, fmtdesc->name,
c0efd232
LP
338 sizeof format->name);
339 format->fcc = fmtdesc->fcc;
340 } else {
36bd883e
LP
341 uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
342 &buffer[5]);
343 snprintf(format->name, sizeof(format->name), "%pUl\n",
344 &buffer[5]);
c0efd232
LP
345 format->fcc = 0;
346 }
347
348 format->bpp = buffer[21];
b482d923
LP
349 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
350 ftype = UVC_VS_FRAME_UNCOMPRESSED;
c0efd232 351 } else {
b482d923 352 ftype = UVC_VS_FRAME_FRAME_BASED;
c0efd232
LP
353 if (buffer[27])
354 format->flags = UVC_FMT_FLAG_COMPRESSED;
355 }
356 break;
357
b482d923 358 case UVC_VS_FORMAT_MJPEG:
c0efd232 359 if (buflen < 11) {
b2d9cc42 360 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
361 "interface %d FORMAT error\n",
362 dev->udev->devnum,
363 alts->desc.bInterfaceNumber);
364 return -EINVAL;
365 }
366
d0ebf307 367 strlcpy(format->name, "MJPEG", sizeof format->name);
c0efd232
LP
368 format->fcc = V4L2_PIX_FMT_MJPEG;
369 format->flags = UVC_FMT_FLAG_COMPRESSED;
370 format->bpp = 0;
b482d923 371 ftype = UVC_VS_FRAME_MJPEG;
c0efd232
LP
372 break;
373
b482d923 374 case UVC_VS_FORMAT_DV:
c0efd232 375 if (buflen < 9) {
b2d9cc42 376 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
377 "interface %d FORMAT error\n",
378 dev->udev->devnum,
379 alts->desc.bInterfaceNumber);
380 return -EINVAL;
381 }
382
383 switch (buffer[8] & 0x7f) {
384 case 0:
d0ebf307 385 strlcpy(format->name, "SD-DV", sizeof format->name);
c0efd232
LP
386 break;
387 case 1:
d0ebf307 388 strlcpy(format->name, "SDL-DV", sizeof format->name);
c0efd232
LP
389 break;
390 case 2:
d0ebf307 391 strlcpy(format->name, "HD-DV", sizeof format->name);
c0efd232
LP
392 break;
393 default:
b2d9cc42 394 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
395 "interface %d: unknown DV format %u\n",
396 dev->udev->devnum,
397 alts->desc.bInterfaceNumber, buffer[8]);
398 return -EINVAL;
399 }
400
d0ebf307 401 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
c0efd232
LP
402 sizeof format->name);
403
404 format->fcc = V4L2_PIX_FMT_DV;
405 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
406 format->bpp = 0;
407 ftype = 0;
408
409 /* Create a dummy frame descriptor. */
410 frame = &format->frame[0];
411 memset(&format->frame[0], 0, sizeof format->frame[0]);
412 frame->bFrameIntervalType = 1;
413 frame->dwDefaultFrameInterval = 1;
414 frame->dwFrameInterval = *intervals;
415 *(*intervals)++ = 1;
416 format->nframes = 1;
417 break;
418
b482d923
LP
419 case UVC_VS_FORMAT_MPEG2TS:
420 case UVC_VS_FORMAT_STREAM_BASED:
c0efd232
LP
421 /* Not supported yet. */
422 default:
b2d9cc42 423 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
424 "interface %d unsupported format %u\n",
425 dev->udev->devnum, alts->desc.bInterfaceNumber,
426 buffer[2]);
427 return -EINVAL;
428 }
429
430 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
431
432 buflen -= buffer[0];
433 buffer += buffer[0];
434
435 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
436 * based formats have frame descriptors.
437 */
c4ed8c66
LP
438 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
439 buffer[2] == ftype) {
078f8947 440 frame = &format->frame[format->nframes];
b482d923 441 if (ftype != UVC_VS_FRAME_FRAME_BASED)
c0efd232
LP
442 n = buflen > 25 ? buffer[25] : 0;
443 else
444 n = buflen > 21 ? buffer[21] : 0;
445
446 n = n ? n : 3;
447
448 if (buflen < 26 + 4*n) {
b2d9cc42 449 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
450 "interface %d FRAME error\n", dev->udev->devnum,
451 alts->desc.bInterfaceNumber);
452 return -EINVAL;
453 }
454
455 frame->bFrameIndex = buffer[3];
456 frame->bmCapabilities = buffer[4];
9bc6218d
MH
457 frame->wWidth = get_unaligned_le16(&buffer[5]);
458 frame->wHeight = get_unaligned_le16(&buffer[7]);
459 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
460 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
b482d923 461 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
c0efd232 462 frame->dwMaxVideoFrameBufferSize =
9bc6218d 463 get_unaligned_le32(&buffer[17]);
c0efd232 464 frame->dwDefaultFrameInterval =
9bc6218d 465 get_unaligned_le32(&buffer[21]);
c0efd232
LP
466 frame->bFrameIntervalType = buffer[25];
467 } else {
468 frame->dwMaxVideoFrameBufferSize = 0;
469 frame->dwDefaultFrameInterval =
9bc6218d 470 get_unaligned_le32(&buffer[17]);
c0efd232
LP
471 frame->bFrameIntervalType = buffer[21];
472 }
473 frame->dwFrameInterval = *intervals;
474
475 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
476 * completely. Observed behaviours range from setting the
2c2d264b 477 * value to 1.1x the actual frame size to hardwiring the
c0efd232
LP
478 * 16 low bits to 0. This results in a higher than necessary
479 * memory usage as well as a wrong image size information. For
480 * uncompressed formats this can be fixed by computing the
481 * value from the frame size.
482 */
483 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
484 frame->dwMaxVideoFrameBufferSize = format->bpp
485 * frame->wWidth * frame->wHeight / 8;
486
487 /* Some bogus devices report dwMinFrameInterval equal to
488 * dwMaxFrameInterval and have dwFrameIntervalStep set to
489 * zero. Setting all null intervals to 1 fixes the problem and
2c2d264b 490 * some other divisions by zero that could happen.
c0efd232
LP
491 */
492 for (i = 0; i < n; ++i) {
9bc6218d 493 interval = get_unaligned_le32(&buffer[26+4*i]);
c0efd232
LP
494 *(*intervals)++ = interval ? interval : 1;
495 }
496
497 /* Make sure that the default frame interval stays between
498 * the boundaries.
499 */
500 n -= frame->bFrameIntervalType ? 1 : 2;
501 frame->dwDefaultFrameInterval =
502 min(frame->dwFrameInterval[n],
503 max(frame->dwFrameInterval[0],
504 frame->dwDefaultFrameInterval));
505
86d8b6ab
LP
506 if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
507 frame->bFrameIntervalType = 1;
508 frame->dwFrameInterval[0] =
509 frame->dwDefaultFrameInterval;
510 }
511
c0efd232
LP
512 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
513 frame->wWidth, frame->wHeight,
514 10000000/frame->dwDefaultFrameInterval,
515 (100000000/frame->dwDefaultFrameInterval)%10);
516
078f8947 517 format->nframes++;
c0efd232
LP
518 buflen -= buffer[0];
519 buffer += buffer[0];
520 }
521
c4ed8c66
LP
522 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
523 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
c0efd232
LP
524 buflen -= buffer[0];
525 buffer += buffer[0];
526 }
527
c4ed8c66
LP
528 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
529 buffer[2] == UVC_VS_COLORFORMAT) {
c0efd232 530 if (buflen < 6) {
b2d9cc42 531 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
c0efd232
LP
532 "interface %d COLORFORMAT error\n",
533 dev->udev->devnum,
534 alts->desc.bInterfaceNumber);
535 return -EINVAL;
536 }
537
538 format->colorspace = uvc_colorspace(buffer[3]);
539
540 buflen -= buffer[0];
541 buffer += buffer[0];
542 }
543
544 return buffer - start;
545}
546
547static int uvc_parse_streaming(struct uvc_device *dev,
548 struct usb_interface *intf)
549{
550 struct uvc_streaming *streaming = NULL;
551 struct uvc_format *format;
552 struct uvc_frame *frame;
553 struct usb_host_interface *alts = &intf->altsetting[0];
554 unsigned char *_buffer, *buffer = alts->extra;
555 int _buflen, buflen = alts->extralen;
556 unsigned int nformats = 0, nframes = 0, nintervals = 0;
557 unsigned int size, i, n, p;
558 __u32 *interval;
559 __u16 psize;
560 int ret = -EINVAL;
561
562 if (intf->cur_altsetting->desc.bInterfaceSubClass
b482d923 563 != UVC_SC_VIDEOSTREAMING) {
c0efd232
LP
564 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
565 "video streaming interface\n", dev->udev->devnum,
566 intf->altsetting[0].desc.bInterfaceNumber);
567 return -EINVAL;
568 }
569
570 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
571 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
572 "claimed\n", dev->udev->devnum,
573 intf->altsetting[0].desc.bInterfaceNumber);
574 return -EINVAL;
575 }
576
577 streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
578 if (streaming == NULL) {
579 usb_driver_release_interface(&uvc_driver.driver, intf);
580 return -EINVAL;
581 }
582
583 mutex_init(&streaming->mutex);
35f02a68 584 streaming->dev = dev;
c0efd232
LP
585 streaming->intf = usb_get_intf(intf);
586 streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
587
588 /* The Pico iMage webcam has its class-specific interface descriptors
589 * after the endpoint descriptors.
590 */
591 if (buflen == 0) {
592 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
593 struct usb_host_endpoint *ep = &alts->endpoint[i];
594
595 if (ep->extralen == 0)
596 continue;
597
598 if (ep->extralen > 2 &&
599 ep->extra[1] == USB_DT_CS_INTERFACE) {
600 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
601 "from endpoint %u.\n", i);
602 buffer = alts->endpoint[i].extra;
603 buflen = alts->endpoint[i].extralen;
604 break;
605 }
606 }
607 }
608
609 /* Skip the standard interface descriptors. */
610 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
611 buflen -= buffer[0];
612 buffer += buffer[0];
613 }
614
615 if (buflen <= 2) {
616 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
617 "interface descriptors found.\n");
618 goto error;
619 }
620
621 /* Parse the header descriptor. */
ff924203 622 switch (buffer[2]) {
b482d923 623 case UVC_VS_OUTPUT_HEADER:
ff924203
LP
624 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
625 size = 9;
626 break;
627
b482d923 628 case UVC_VS_INPUT_HEADER:
ff924203
LP
629 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
630 size = 13;
631 break;
632
633 default:
c0efd232 634 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
ff924203
LP
635 "%d HEADER descriptor not found.\n", dev->udev->devnum,
636 alts->desc.bInterfaceNumber);
c0efd232 637 goto error;
ff924203 638 }
c0efd232 639
ff924203
LP
640 p = buflen >= 4 ? buffer[3] : 0;
641 n = buflen >= size ? buffer[size-1] : 0;
642
643 if (buflen < size + p*n) {
644 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
645 "interface %d HEADER descriptor is invalid.\n",
646 dev->udev->devnum, alts->desc.bInterfaceNumber);
647 goto error;
648 }
c0efd232 649
ff924203
LP
650 streaming->header.bNumFormats = p;
651 streaming->header.bEndpointAddress = buffer[6];
b482d923 652 if (buffer[2] == UVC_VS_INPUT_HEADER) {
c0efd232
LP
653 streaming->header.bmInfo = buffer[7];
654 streaming->header.bTerminalLink = buffer[8];
655 streaming->header.bStillCaptureMethod = buffer[9];
656 streaming->header.bTriggerSupport = buffer[10];
657 streaming->header.bTriggerUsage = buffer[11];
c0efd232 658 } else {
ff924203
LP
659 streaming->header.bTerminalLink = buffer[7];
660 }
661 streaming->header.bControlSize = n;
662
0b21d55f
JL
663 streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
664 GFP_KERNEL);
ff924203
LP
665 if (streaming->header.bmaControls == NULL) {
666 ret = -ENOMEM;
c0efd232
LP
667 goto error;
668 }
669
670 buflen -= buffer[0];
671 buffer += buffer[0];
672
673 _buffer = buffer;
674 _buflen = buflen;
675
676 /* Count the format and frame descriptors. */
042e143e 677 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
c0efd232 678 switch (_buffer[2]) {
b482d923
LP
679 case UVC_VS_FORMAT_UNCOMPRESSED:
680 case UVC_VS_FORMAT_MJPEG:
681 case UVC_VS_FORMAT_FRAME_BASED:
c0efd232
LP
682 nformats++;
683 break;
684
b482d923 685 case UVC_VS_FORMAT_DV:
c0efd232
LP
686 /* DV format has no frame descriptor. We will create a
687 * dummy frame descriptor with a dummy frame interval.
688 */
689 nformats++;
690 nframes++;
691 nintervals++;
692 break;
693
b482d923
LP
694 case UVC_VS_FORMAT_MPEG2TS:
695 case UVC_VS_FORMAT_STREAM_BASED:
c0efd232
LP
696 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
697 "interface %d FORMAT %u is not supported.\n",
698 dev->udev->devnum,
699 alts->desc.bInterfaceNumber, _buffer[2]);
700 break;
701
b482d923
LP
702 case UVC_VS_FRAME_UNCOMPRESSED:
703 case UVC_VS_FRAME_MJPEG:
c0efd232
LP
704 nframes++;
705 if (_buflen > 25)
706 nintervals += _buffer[25] ? _buffer[25] : 3;
707 break;
708
b482d923 709 case UVC_VS_FRAME_FRAME_BASED:
c0efd232
LP
710 nframes++;
711 if (_buflen > 21)
712 nintervals += _buffer[21] ? _buffer[21] : 3;
713 break;
714 }
715
716 _buflen -= _buffer[0];
717 _buffer += _buffer[0];
718 }
719
720 if (nformats == 0) {
721 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
722 "%d has no supported formats defined.\n",
723 dev->udev->devnum, alts->desc.bInterfaceNumber);
724 goto error;
725 }
726
727 size = nformats * sizeof *format + nframes * sizeof *frame
728 + nintervals * sizeof *interval;
729 format = kzalloc(size, GFP_KERNEL);
730 if (format == NULL) {
731 ret = -ENOMEM;
732 goto error;
733 }
734
735 frame = (struct uvc_frame *)&format[nformats];
736 interval = (__u32 *)&frame[nframes];
737
738 streaming->format = format;
739 streaming->nformats = nformats;
740
741 /* Parse the format descriptors. */
042e143e 742 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
c0efd232 743 switch (buffer[2]) {
b482d923
LP
744 case UVC_VS_FORMAT_UNCOMPRESSED:
745 case UVC_VS_FORMAT_MJPEG:
746 case UVC_VS_FORMAT_DV:
747 case UVC_VS_FORMAT_FRAME_BASED:
c0efd232
LP
748 format->frame = frame;
749 ret = uvc_parse_format(dev, streaming, format,
750 &interval, buffer, buflen);
751 if (ret < 0)
752 goto error;
753
754 frame += format->nframes;
755 format++;
756
757 buflen -= ret;
758 buffer += ret;
759 continue;
760
761 default:
762 break;
763 }
764
765 buflen -= buffer[0];
766 buffer += buffer[0];
767 }
768
c4ed8c66
LP
769 if (buflen)
770 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
771 "%d has %u bytes of trailing descriptor garbage.\n",
772 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
773
c0efd232
LP
774 /* Parse the alternate settings to find the maximum bandwidth. */
775 for (i = 0; i < intf->num_altsetting; ++i) {
776 struct usb_host_endpoint *ep;
777 alts = &intf->altsetting[i];
778 ep = uvc_find_endpoint(alts,
779 streaming->header.bEndpointAddress);
780 if (ep == NULL)
781 continue;
782
783 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
784 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
785 if (psize > streaming->maxpsize)
786 streaming->maxpsize = psize;
787 }
788
35f02a68 789 list_add_tail(&streaming->list, &dev->streams);
c0efd232
LP
790 return 0;
791
792error:
793 usb_driver_release_interface(&uvc_driver.driver, intf);
794 usb_put_intf(intf);
795 kfree(streaming->format);
796 kfree(streaming->header.bmaControls);
797 kfree(streaming);
798 return ret;
799}
800
8ca5a639
LP
801static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
802 unsigned int num_pads, unsigned int extra_size)
803{
804 struct uvc_entity *entity;
805 unsigned int num_inputs;
806 unsigned int size;
4ffc2d89 807 unsigned int i;
8ca5a639 808
4ffc2d89 809 extra_size = ALIGN(extra_size, sizeof(*entity->pads));
8ca5a639 810 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
4ffc2d89
LP
811 size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
812 + num_inputs;
8ca5a639
LP
813 entity = kzalloc(size, GFP_KERNEL);
814 if (entity == NULL)
815 return NULL;
816
817 entity->id = id;
818 entity->type = type;
819
4ffc2d89
LP
820 entity->num_links = 0;
821 entity->num_pads = num_pads;
822 entity->pads = ((void *)(entity + 1)) + extra_size;
823
824 for (i = 0; i < num_inputs; ++i)
825 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
826 if (!UVC_ENTITY_IS_OTERM(entity))
827 entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
828
8ca5a639 829 entity->bNrInPins = num_inputs;
4ffc2d89 830 entity->baSourceID = (__u8 *)(&entity->pads[num_pads]);
8ca5a639
LP
831
832 return entity;
833}
834
c0efd232
LP
835/* Parse vendor-specific extensions. */
836static int uvc_parse_vendor_control(struct uvc_device *dev,
837 const unsigned char *buffer, int buflen)
838{
839 struct usb_device *udev = dev->udev;
840 struct usb_host_interface *alts = dev->intf->cur_altsetting;
841 struct uvc_entity *unit;
842 unsigned int n, p;
843 int handled = 0;
844
845 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
846 case 0x046d: /* Logitech */
847 if (buffer[1] != 0x41 || buffer[2] != 0x01)
848 break;
849
850 /* Logitech implements several vendor specific functions
851 * through vendor specific extension units (LXU).
852 *
853 * The LXU descriptors are similar to XU descriptors
854 * (see "USB Device Video Class for Video Devices", section
855 * 3.7.2.6 "Extension Unit Descriptor") with the following
856 * differences:
857 *
858 * ----------------------------------------------------------
859 * 0 bLength 1 Number
860 * Size of this descriptor, in bytes: 24+p+n*2
861 * ----------------------------------------------------------
862 * 23+p+n bmControlsType N Bitmap
863 * Individual bits in the set are defined:
864 * 0: Absolute
865 * 1: Relative
866 *
867 * This bitset is mapped exactly the same as bmControls.
868 * ----------------------------------------------------------
869 * 23+p+n*2 bReserved 1 Boolean
870 * ----------------------------------------------------------
871 * 24+p+n*2 iExtension 1 Index
872 * Index of a string descriptor that describes this
873 * extension unit.
874 * ----------------------------------------------------------
875 */
876 p = buflen >= 22 ? buffer[21] : 0;
877 n = buflen >= 25 + p ? buffer[22+p] : 0;
878
879 if (buflen < 25 + p + 2*n) {
880 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
881 "interface %d EXTENSION_UNIT error\n",
882 udev->devnum, alts->desc.bInterfaceNumber);
883 break;
884 }
885
8ca5a639
LP
886 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
887 p + 1, 2*n);
c0efd232
LP
888 if (unit == NULL)
889 return -ENOMEM;
890
c0efd232
LP
891 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
892 unit->extension.bNumControls = buffer[20];
8ca5a639 893 memcpy(unit->baSourceID, &buffer[22], p);
c0efd232 894 unit->extension.bControlSize = buffer[22+p];
8ca5a639
LP
895 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
896 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
897 + n;
c0efd232
LP
898 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
899
900 if (buffer[24+p+2*n] != 0)
901 usb_string(udev, buffer[24+p+2*n], unit->name,
902 sizeof unit->name);
903 else
904 sprintf(unit->name, "Extension %u", buffer[3]);
905
906 list_add_tail(&unit->list, &dev->entities);
907 handled = 1;
908 break;
909 }
910
911 return handled;
912}
913
914static int uvc_parse_standard_control(struct uvc_device *dev,
915 const unsigned char *buffer, int buflen)
916{
917 struct usb_device *udev = dev->udev;
918 struct uvc_entity *unit, *term;
919 struct usb_interface *intf;
920 struct usb_host_interface *alts = dev->intf->cur_altsetting;
921 unsigned int i, n, p, len;
922 __u16 type;
923
924 switch (buffer[2]) {
b482d923 925 case UVC_VC_HEADER:
c0efd232
LP
926 n = buflen >= 12 ? buffer[11] : 0;
927
928 if (buflen < 12 || buflen < 12 + n) {
929 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
930 "interface %d HEADER error\n", udev->devnum,
931 alts->desc.bInterfaceNumber);
932 return -EINVAL;
933 }
934
9bc6218d
MH
935 dev->uvc_version = get_unaligned_le16(&buffer[3]);
936 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
c0efd232
LP
937
938 /* Parse all USB Video Streaming interfaces. */
939 for (i = 0; i < n; ++i) {
940 intf = usb_ifnum_to_if(udev, buffer[12+i]);
941 if (intf == NULL) {
942 uvc_trace(UVC_TRACE_DESCR, "device %d "
943 "interface %d doesn't exists\n",
944 udev->devnum, i);
945 continue;
946 }
947
948 uvc_parse_streaming(dev, intf);
949 }
950 break;
951
b482d923 952 case UVC_VC_INPUT_TERMINAL:
c0efd232
LP
953 if (buflen < 8) {
954 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
955 "interface %d INPUT_TERMINAL error\n",
956 udev->devnum, alts->desc.bInterfaceNumber);
957 return -EINVAL;
958 }
959
960 /* Make sure the terminal type MSB is not null, otherwise it
961 * could be confused with a unit.
962 */
9bc6218d 963 type = get_unaligned_le16(&buffer[4]);
c0efd232
LP
964 if ((type & 0xff00) == 0) {
965 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
966 "interface %d INPUT_TERMINAL %d has invalid "
967 "type 0x%04x, skipping\n", udev->devnum,
968 alts->desc.bInterfaceNumber,
969 buffer[3], type);
970 return 0;
971 }
972
973 n = 0;
974 p = 0;
975 len = 8;
976
b482d923 977 if (type == UVC_ITT_CAMERA) {
c0efd232
LP
978 n = buflen >= 15 ? buffer[14] : 0;
979 len = 15;
980
b482d923 981 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
c0efd232
LP
982 n = buflen >= 9 ? buffer[8] : 0;
983 p = buflen >= 10 + n ? buffer[9+n] : 0;
984 len = 10;
985 }
986
987 if (buflen < len + n + p) {
988 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
989 "interface %d INPUT_TERMINAL error\n",
990 udev->devnum, alts->desc.bInterfaceNumber);
991 return -EINVAL;
992 }
993
8ca5a639
LP
994 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
995 1, n + p);
c0efd232
LP
996 if (term == NULL)
997 return -ENOMEM;
998
b482d923 999 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
c0efd232
LP
1000 term->camera.bControlSize = n;
1001 term->camera.bmControls = (__u8 *)term + sizeof *term;
1002 term->camera.wObjectiveFocalLengthMin =
9bc6218d 1003 get_unaligned_le16(&buffer[8]);
c0efd232 1004 term->camera.wObjectiveFocalLengthMax =
9bc6218d 1005 get_unaligned_le16(&buffer[10]);
c0efd232 1006 term->camera.wOcularFocalLength =
9bc6218d 1007 get_unaligned_le16(&buffer[12]);
c0efd232 1008 memcpy(term->camera.bmControls, &buffer[15], n);
b482d923
LP
1009 } else if (UVC_ENTITY_TYPE(term) ==
1010 UVC_ITT_MEDIA_TRANSPORT_INPUT) {
c0efd232
LP
1011 term->media.bControlSize = n;
1012 term->media.bmControls = (__u8 *)term + sizeof *term;
1013 term->media.bTransportModeSize = p;
1014 term->media.bmTransportModes = (__u8 *)term
1015 + sizeof *term + n;
1016 memcpy(term->media.bmControls, &buffer[9], n);
1017 memcpy(term->media.bmTransportModes, &buffer[10+n], p);
1018 }
1019
1020 if (buffer[7] != 0)
1021 usb_string(udev, buffer[7], term->name,
1022 sizeof term->name);
b482d923 1023 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
c0efd232 1024 sprintf(term->name, "Camera %u", buffer[3]);
b482d923 1025 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
c0efd232
LP
1026 sprintf(term->name, "Media %u", buffer[3]);
1027 else
1028 sprintf(term->name, "Input %u", buffer[3]);
1029
1030 list_add_tail(&term->list, &dev->entities);
1031 break;
1032
b482d923 1033 case UVC_VC_OUTPUT_TERMINAL:
c0efd232
LP
1034 if (buflen < 9) {
1035 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1036 "interface %d OUTPUT_TERMINAL error\n",
1037 udev->devnum, alts->desc.bInterfaceNumber);
1038 return -EINVAL;
1039 }
1040
1041 /* Make sure the terminal type MSB is not null, otherwise it
1042 * could be confused with a unit.
1043 */
9bc6218d 1044 type = get_unaligned_le16(&buffer[4]);
c0efd232
LP
1045 if ((type & 0xff00) == 0) {
1046 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1047 "interface %d OUTPUT_TERMINAL %d has invalid "
1048 "type 0x%04x, skipping\n", udev->devnum,
1049 alts->desc.bInterfaceNumber, buffer[3], type);
1050 return 0;
1051 }
1052
8ca5a639
LP
1053 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1054 1, 0);
c0efd232
LP
1055 if (term == NULL)
1056 return -ENOMEM;
1057
8ca5a639 1058 memcpy(term->baSourceID, &buffer[7], 1);
c0efd232
LP
1059
1060 if (buffer[8] != 0)
1061 usb_string(udev, buffer[8], term->name,
1062 sizeof term->name);
1063 else
1064 sprintf(term->name, "Output %u", buffer[3]);
1065
1066 list_add_tail(&term->list, &dev->entities);
1067 break;
1068
b482d923 1069 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
1070 p = buflen >= 5 ? buffer[4] : 0;
1071
1072 if (buflen < 5 || buflen < 6 + p) {
1073 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1074 "interface %d SELECTOR_UNIT error\n",
1075 udev->devnum, alts->desc.bInterfaceNumber);
1076 return -EINVAL;
1077 }
1078
8ca5a639 1079 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
c0efd232
LP
1080 if (unit == NULL)
1081 return -ENOMEM;
1082
8ca5a639 1083 memcpy(unit->baSourceID, &buffer[5], p);
c0efd232
LP
1084
1085 if (buffer[5+p] != 0)
1086 usb_string(udev, buffer[5+p], unit->name,
1087 sizeof unit->name);
1088 else
1089 sprintf(unit->name, "Selector %u", buffer[3]);
1090
1091 list_add_tail(&unit->list, &dev->entities);
1092 break;
1093
b482d923 1094 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
1095 n = buflen >= 8 ? buffer[7] : 0;
1096 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1097
1098 if (buflen < p + n) {
1099 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1100 "interface %d PROCESSING_UNIT error\n",
1101 udev->devnum, alts->desc.bInterfaceNumber);
1102 return -EINVAL;
1103 }
1104
8ca5a639 1105 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
c0efd232
LP
1106 if (unit == NULL)
1107 return -ENOMEM;
1108
8ca5a639 1109 memcpy(unit->baSourceID, &buffer[4], 1);
c0efd232 1110 unit->processing.wMaxMultiplier =
9bc6218d 1111 get_unaligned_le16(&buffer[5]);
c0efd232
LP
1112 unit->processing.bControlSize = buffer[7];
1113 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1114 memcpy(unit->processing.bmControls, &buffer[8], n);
1115 if (dev->uvc_version >= 0x0110)
1116 unit->processing.bmVideoStandards = buffer[9+n];
1117
1118 if (buffer[8+n] != 0)
1119 usb_string(udev, buffer[8+n], unit->name,
1120 sizeof unit->name);
1121 else
1122 sprintf(unit->name, "Processing %u", buffer[3]);
1123
1124 list_add_tail(&unit->list, &dev->entities);
1125 break;
1126
b482d923 1127 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
1128 p = buflen >= 22 ? buffer[21] : 0;
1129 n = buflen >= 24 + p ? buffer[22+p] : 0;
1130
1131 if (buflen < 24 + p + n) {
1132 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1133 "interface %d EXTENSION_UNIT error\n",
1134 udev->devnum, alts->desc.bInterfaceNumber);
1135 return -EINVAL;
1136 }
1137
8ca5a639 1138 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
c0efd232
LP
1139 if (unit == NULL)
1140 return -ENOMEM;
1141
c0efd232
LP
1142 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1143 unit->extension.bNumControls = buffer[20];
8ca5a639 1144 memcpy(unit->baSourceID, &buffer[22], p);
c0efd232 1145 unit->extension.bControlSize = buffer[22+p];
8ca5a639 1146 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
c0efd232
LP
1147 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1148
1149 if (buffer[23+p+n] != 0)
1150 usb_string(udev, buffer[23+p+n], unit->name,
1151 sizeof unit->name);
1152 else
1153 sprintf(unit->name, "Extension %u", buffer[3]);
1154
1155 list_add_tail(&unit->list, &dev->entities);
1156 break;
1157
1158 default:
1159 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1160 "descriptor (%u)\n", buffer[2]);
1161 break;
1162 }
1163
1164 return 0;
1165}
1166
1167static int uvc_parse_control(struct uvc_device *dev)
1168{
1169 struct usb_host_interface *alts = dev->intf->cur_altsetting;
1170 unsigned char *buffer = alts->extra;
1171 int buflen = alts->extralen;
1172 int ret;
1173
1174 /* Parse the default alternate setting only, as the UVC specification
1175 * defines a single alternate setting, the default alternate setting
1176 * zero.
1177 */
1178
1179 while (buflen > 2) {
1180 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1181 buffer[1] != USB_DT_CS_INTERFACE)
1182 goto next_descriptor;
1183
1184 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1185 return ret;
1186
1187next_descriptor:
1188 buflen -= buffer[0];
1189 buffer += buffer[0];
1190 }
1191
538e7a00
LP
1192 /* Check if the optional status endpoint is present. Built-in iSight
1193 * webcams have an interrupt endpoint but spit proprietary data that
1194 * don't conform to the UVC status endpoint messages. Don't try to
1195 * handle the interrupt endpoint for those cameras.
1196 */
1197 if (alts->desc.bNumEndpoints == 1 &&
1198 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
c0efd232
LP
1199 struct usb_host_endpoint *ep = &alts->endpoint[0];
1200 struct usb_endpoint_descriptor *desc = &ep->desc;
1201
1202 if (usb_endpoint_is_int_in(desc) &&
1203 le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1204 desc->bInterval != 0) {
1205 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1206 "(addr %02x).\n", desc->bEndpointAddress);
1207 dev->int_ep = ep;
1208 }
1209 }
1210
1211 return 0;
1212}
1213
1214/* ------------------------------------------------------------------------
8e113595 1215 * UVC device scan
c0efd232
LP
1216 */
1217
c0efd232
LP
1218/*
1219 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1220 * and containing the following units:
1221 *
8e113595 1222 * - one or more Output Terminals (USB Streaming or Display)
c0efd232 1223 * - zero or one Processing Unit
8e113595 1224 * - zero, one or more single-input Selector Units
c0efd232
LP
1225 * - zero or one multiple-input Selector Units, provided all inputs are
1226 * connected to input terminals
1227 * - zero, one or mode single-input Extension Units
2c2d264b 1228 * - one or more Input Terminals (Camera, External or USB Streaming)
c0efd232 1229 *
8e113595
LP
1230 * The terminal and units must match on of the following structures:
1231 *
1232 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0)
1233 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ...
1234 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n)
1235 *
1236 * +---------+ +---------+ -> OTT_*(0)
1237 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ...
1238 * +---------+ +---------+ -> OTT_*(n)
1239 *
1240 * The Processing Unit and Extension Units can be in any order. Additional
1241 * Extension Units connected to the main chain as single-unit branches are
1242 * also supported. Single-input Selector Units are ignored.
c0efd232 1243 */
8e113595 1244static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
c0efd232
LP
1245 struct uvc_entity *entity)
1246{
1247 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 1248 case UVC_VC_EXTENSION_UNIT:
c0efd232
LP
1249 if (uvc_trace_param & UVC_TRACE_PROBE)
1250 printk(" <- XU %d", entity->id);
1251
8ca5a639 1252 if (entity->bNrInPins != 1) {
c0efd232
LP
1253 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1254 "than 1 input pin.\n", entity->id);
1255 return -1;
1256 }
1257
c0efd232
LP
1258 break;
1259
b482d923 1260 case UVC_VC_PROCESSING_UNIT:
c0efd232
LP
1261 if (uvc_trace_param & UVC_TRACE_PROBE)
1262 printk(" <- PU %d", entity->id);
1263
8e113595 1264 if (chain->processing != NULL) {
c0efd232
LP
1265 uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1266 "Processing Units in chain.\n");
1267 return -1;
1268 }
1269
8e113595 1270 chain->processing = entity;
c0efd232
LP
1271 break;
1272
b482d923 1273 case UVC_VC_SELECTOR_UNIT:
c0efd232
LP
1274 if (uvc_trace_param & UVC_TRACE_PROBE)
1275 printk(" <- SU %d", entity->id);
1276
1277 /* Single-input selector units are ignored. */
8ca5a639 1278 if (entity->bNrInPins == 1)
c0efd232
LP
1279 break;
1280
8e113595 1281 if (chain->selector != NULL) {
c0efd232
LP
1282 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1283 "Units in chain.\n");
1284 return -1;
1285 }
1286
8e113595 1287 chain->selector = entity;
c0efd232
LP
1288 break;
1289
b482d923
LP
1290 case UVC_ITT_VENDOR_SPECIFIC:
1291 case UVC_ITT_CAMERA:
1292 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
c0efd232
LP
1293 if (uvc_trace_param & UVC_TRACE_PROBE)
1294 printk(" <- IT %d\n", entity->id);
1295
c0efd232
LP
1296 break;
1297
4093a5c4
LP
1298 case UVC_OTT_VENDOR_SPECIFIC:
1299 case UVC_OTT_DISPLAY:
1300 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1301 if (uvc_trace_param & UVC_TRACE_PROBE)
1302 printk(" OT %d", entity->id);
1303
1304 break;
1305
b482d923 1306 case UVC_TT_STREAMING:
4057ac6c
LP
1307 if (UVC_ENTITY_IS_ITERM(entity)) {
1308 if (uvc_trace_param & UVC_TRACE_PROBE)
1309 printk(" <- IT %d\n", entity->id);
1310 } else {
1311 if (uvc_trace_param & UVC_TRACE_PROBE)
1312 printk(" OT %d", entity->id);
ff924203
LP
1313 }
1314
ff924203
LP
1315 break;
1316
c0efd232
LP
1317 default:
1318 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1319 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1320 return -1;
1321 }
1322
6241d8ca 1323 list_add_tail(&entity->chain, &chain->entities);
c0efd232
LP
1324 return 0;
1325}
1326
8e113595 1327static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
c0efd232
LP
1328 struct uvc_entity *entity, struct uvc_entity *prev)
1329{
1330 struct uvc_entity *forward;
1331 int found;
1332
1333 /* Forward scan */
1334 forward = NULL;
1335 found = 0;
1336
1337 while (1) {
8e113595 1338 forward = uvc_entity_by_reference(chain->dev, entity->id,
c0efd232
LP
1339 forward);
1340 if (forward == NULL)
1341 break;
8e113595 1342 if (forward == prev)
c0efd232
LP
1343 continue;
1344
8e113595
LP
1345 switch (UVC_ENTITY_TYPE(forward)) {
1346 case UVC_VC_EXTENSION_UNIT:
8ca5a639 1347 if (forward->bNrInPins != 1) {
8e113595
LP
1348 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1349 "has more than 1 input pin.\n",
1350 entity->id);
1351 return -EINVAL;
1352 }
c0efd232 1353
6241d8ca 1354 list_add_tail(&forward->chain, &chain->entities);
8e113595
LP
1355 if (uvc_trace_param & UVC_TRACE_PROBE) {
1356 if (!found)
1357 printk(" (->");
1358
1359 printk(" XU %d", forward->id);
1360 found = 1;
1361 }
1362 break;
1363
1364 case UVC_OTT_VENDOR_SPECIFIC:
1365 case UVC_OTT_DISPLAY:
1366 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1367 case UVC_TT_STREAMING:
1368 if (UVC_ENTITY_IS_ITERM(forward)) {
1369 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1370 "terminal %u.\n", forward->id);
1371 return -EINVAL;
1372 }
c0efd232 1373
6241d8ca 1374 list_add_tail(&forward->chain, &chain->entities);
8e113595
LP
1375 if (uvc_trace_param & UVC_TRACE_PROBE) {
1376 if (!found)
1377 printk(" (->");
1378
1379 printk(" OT %d", forward->id);
1380 found = 1;
1381 }
1382 break;
c0efd232
LP
1383 }
1384 }
1385 if (found)
1386 printk(")");
1387
1388 return 0;
1389}
1390
8e113595 1391static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
4057ac6c 1392 struct uvc_entity **_entity)
c0efd232 1393{
4057ac6c 1394 struct uvc_entity *entity = *_entity;
c0efd232 1395 struct uvc_entity *term;
4057ac6c 1396 int id = -EINVAL, i;
c0efd232
LP
1397
1398 switch (UVC_ENTITY_TYPE(entity)) {
b482d923 1399 case UVC_VC_EXTENSION_UNIT:
b482d923 1400 case UVC_VC_PROCESSING_UNIT:
8ca5a639 1401 id = entity->baSourceID[0];
c0efd232
LP
1402 break;
1403
b482d923 1404 case UVC_VC_SELECTOR_UNIT:
c0efd232 1405 /* Single-input selector units are ignored. */
8ca5a639
LP
1406 if (entity->bNrInPins == 1) {
1407 id = entity->baSourceID[0];
c0efd232
LP
1408 break;
1409 }
1410
1411 if (uvc_trace_param & UVC_TRACE_PROBE)
1412 printk(" <- IT");
1413
8e113595 1414 chain->selector = entity;
8ca5a639
LP
1415 for (i = 0; i < entity->bNrInPins; ++i) {
1416 id = entity->baSourceID[i];
8e113595 1417 term = uvc_entity_by_id(chain->dev, id);
c0efd232
LP
1418 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1419 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1420 "input %d isn't connected to an "
1421 "input terminal\n", entity->id, i);
1422 return -1;
1423 }
1424
1425 if (uvc_trace_param & UVC_TRACE_PROBE)
1426 printk(" %d", term->id);
1427
6241d8ca 1428 list_add_tail(&term->chain, &chain->entities);
8e113595 1429 uvc_scan_chain_forward(chain, term, entity);
c0efd232
LP
1430 }
1431
1432 if (uvc_trace_param & UVC_TRACE_PROBE)
1433 printk("\n");
1434
1435 id = 0;
1436 break;
4057ac6c
LP
1437
1438 case UVC_ITT_VENDOR_SPECIFIC:
1439 case UVC_ITT_CAMERA:
1440 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1441 case UVC_OTT_VENDOR_SPECIFIC:
1442 case UVC_OTT_DISPLAY:
1443 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1444 case UVC_TT_STREAMING:
8ca5a639 1445 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
4057ac6c 1446 break;
c0efd232
LP
1447 }
1448
4057ac6c
LP
1449 if (id <= 0) {
1450 *_entity = NULL;
1451 return id;
1452 }
1453
1454 entity = uvc_entity_by_id(chain->dev, id);
1455 if (entity == NULL) {
1456 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1457 "unknown entity %d.\n", id);
1458 return -EINVAL;
1459 }
1460
1461 *_entity = entity;
1462 return 0;
c0efd232
LP
1463}
1464
8e113595 1465static int uvc_scan_chain(struct uvc_video_chain *chain,
4057ac6c 1466 struct uvc_entity *term)
c0efd232
LP
1467{
1468 struct uvc_entity *entity, *prev;
c0efd232 1469
4057ac6c 1470 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
ff924203 1471
4057ac6c
LP
1472 entity = term;
1473 prev = NULL;
8e113595 1474
4057ac6c
LP
1475 while (entity != NULL) {
1476 /* Entity must not be part of an existing chain */
8e113595
LP
1477 if (entity->chain.next || entity->chain.prev) {
1478 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
4057ac6c 1479 "entity %d already in chain.\n", entity->id);
8e113595 1480 return -EINVAL;
c0efd232
LP
1481 }
1482
1483 /* Process entity */
8e113595
LP
1484 if (uvc_scan_chain_entity(chain, entity) < 0)
1485 return -EINVAL;
c0efd232
LP
1486
1487 /* Forward scan */
8e113595
LP
1488 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1489 return -EINVAL;
c0efd232 1490
c0efd232 1491 /* Backward scan */
4057ac6c
LP
1492 prev = entity;
1493 if (uvc_scan_chain_backward(chain, &entity) < 0)
1494 return -EINVAL;
c0efd232
LP
1495 }
1496
8e113595
LP
1497 return 0;
1498}
1499
6241d8ca
LP
1500static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1501 char *buffer)
8e113595
LP
1502{
1503 struct uvc_entity *term;
1504 unsigned int nterms = 0;
1505 char *p = buffer;
1506
1507 list_for_each_entry(term, terms, chain) {
6241d8ca
LP
1508 if (!UVC_ENTITY_IS_TERM(term) ||
1509 UVC_TERM_DIRECTION(term) != dir)
1510 continue;
1511
1512 if (nterms)
8e113595 1513 p += sprintf(p, ",");
6241d8ca
LP
1514 if (++nterms >= 4) {
1515 p += sprintf(p, "...");
1516 break;
8e113595 1517 }
6241d8ca 1518 p += sprintf(p, "%u", term->id);
ff924203 1519 }
c0efd232 1520
8e113595
LP
1521 return p - buffer;
1522}
1523
1524static const char *uvc_print_chain(struct uvc_video_chain *chain)
1525{
1526 static char buffer[43];
1527 char *p = buffer;
1528
6241d8ca 1529 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
8e113595 1530 p += sprintf(p, " -> ");
6241d8ca 1531 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
8e113595
LP
1532
1533 return buffer;
c0efd232
LP
1534}
1535
935896e8
HI
1536static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
1537{
1538 struct uvc_video_chain *chain;
1539
1540 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1541 if (chain == NULL)
1542 return NULL;
1543
1544 INIT_LIST_HEAD(&chain->entities);
1545 mutex_init(&chain->ctrl_mutex);
1546 chain->dev = dev;
1547 v4l2_prio_init(&chain->prio);
1548
1549 return chain;
1550}
1551
1552/*
1553 * Fallback heuristic for devices that don't connect units and terminals in a
1554 * valid chain.
1555 *
1556 * Some devices have invalid baSourceID references, causing uvc_scan_chain()
1557 * to fail, but if we just take the entities we can find and put them together
1558 * in the most sensible chain we can think of, turns out they do work anyway.
1559 * Note: This heuristic assumes there is a single chain.
1560 *
1561 * At the time of writing, devices known to have such a broken chain are
1562 * - Acer Integrated Camera (5986:055a)
1563 * - Realtek rtl157a7 (0bda:57a7)
1564 */
1565static int uvc_scan_fallback(struct uvc_device *dev)
1566{
1567 struct uvc_video_chain *chain;
1568 struct uvc_entity *iterm = NULL;
1569 struct uvc_entity *oterm = NULL;
1570 struct uvc_entity *entity;
1571 struct uvc_entity *prev;
1572
1573 /*
1574 * Start by locating the input and output terminals. We only support
1575 * devices with exactly one of each for now.
1576 */
1577 list_for_each_entry(entity, &dev->entities, list) {
1578 if (UVC_ENTITY_IS_ITERM(entity)) {
1579 if (iterm)
1580 return -EINVAL;
1581 iterm = entity;
1582 }
1583
1584 if (UVC_ENTITY_IS_OTERM(entity)) {
1585 if (oterm)
1586 return -EINVAL;
1587 oterm = entity;
1588 }
1589 }
1590
1591 if (iterm == NULL || oterm == NULL)
1592 return -EINVAL;
1593
1594 /* Allocate the chain and fill it. */
1595 chain = uvc_alloc_chain(dev);
1596 if (chain == NULL)
1597 return -ENOMEM;
1598
1599 if (uvc_scan_chain_entity(chain, oterm) < 0)
1600 goto error;
1601
1602 prev = oterm;
1603
1604 /*
1605 * Add all Processing and Extension Units with two pads. The order
1606 * doesn't matter much, use reverse list traversal to connect units in
1607 * UVC descriptor order as we build the chain from output to input. This
1608 * leads to units appearing in the order meant by the manufacturer for
1609 * the cameras known to require this heuristic.
1610 */
1611 list_for_each_entry_reverse(entity, &dev->entities, list) {
1612 if (entity->type != UVC_VC_PROCESSING_UNIT &&
1613 entity->type != UVC_VC_EXTENSION_UNIT)
1614 continue;
1615
1616 if (entity->num_pads != 2)
1617 continue;
1618
1619 if (uvc_scan_chain_entity(chain, entity) < 0)
1620 goto error;
1621
1622 prev->baSourceID[0] = entity->id;
1623 prev = entity;
1624 }
1625
1626 if (uvc_scan_chain_entity(chain, iterm) < 0)
1627 goto error;
1628
1629 prev->baSourceID[0] = iterm->id;
1630
1631 list_add_tail(&chain->list, &dev->chains);
1632
1633 uvc_trace(UVC_TRACE_PROBE,
1634 "Found a video chain by fallback heuristic (%s).\n",
1635 uvc_print_chain(chain));
1636
1637 return 0;
1638
1639error:
1640 kfree(chain);
1641 return -EINVAL;
1642}
1643
c0efd232 1644/*
35f02a68 1645 * Scan the device for video chains and register video devices.
c0efd232 1646 *
8e113595 1647 * Chains are scanned starting at their output terminals and walked backwards.
c0efd232 1648 */
35f02a68 1649static int uvc_scan_device(struct uvc_device *dev)
c0efd232 1650{
8e113595 1651 struct uvc_video_chain *chain;
c0efd232 1652 struct uvc_entity *term;
c0efd232 1653
c0efd232 1654 list_for_each_entry(term, &dev->entities, list) {
8e113595 1655 if (!UVC_ENTITY_IS_OTERM(term))
c0efd232
LP
1656 continue;
1657
8e113595
LP
1658 /* If the terminal is already included in a chain, skip it.
1659 * This can happen for chains that have multiple output
1660 * terminals, where all output terminals beside the first one
1661 * will be inserted in the chain in forward scans.
1662 */
1663 if (term->chain.next || term->chain.prev)
c0efd232
LP
1664 continue;
1665
935896e8 1666 chain = uvc_alloc_chain(dev);
8e113595
LP
1667 if (chain == NULL)
1668 return -ENOMEM;
1669
8be8ec6e
LP
1670 term->flags |= UVC_ENTITY_FLAG_DEFAULT;
1671
8e113595
LP
1672 if (uvc_scan_chain(chain, term) < 0) {
1673 kfree(chain);
1674 continue;
c0efd232 1675 }
8e113595
LP
1676
1677 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1678 uvc_print_chain(chain));
1679
1680 list_add_tail(&chain->list, &dev->chains);
c0efd232
LP
1681 }
1682
935896e8
HI
1683 if (list_empty(&dev->chains))
1684 uvc_scan_fallback(dev);
1685
8e113595 1686 if (list_empty(&dev->chains)) {
c0efd232
LP
1687 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1688 return -1;
1689 }
1690
c0efd232
LP
1691 return 0;
1692}
1693
8e113595
LP
1694/* ------------------------------------------------------------------------
1695 * Video device registration and unregistration
1696 */
1697
716fdee1
LP
1698/*
1699 * Delete the UVC device.
1700 *
1701 * Called by the kernel when the last reference to the uvc_device structure
1702 * is released.
1703 *
1704 * As this function is called after or during disconnect(), all URBs have
1705 * already been canceled by the USB core. There is no need to kill the
1706 * interrupt URB manually.
1707 */
1708static void uvc_delete(struct uvc_device *dev)
1709{
1710 struct list_head *p, *n;
1711
716fdee1
LP
1712 uvc_status_cleanup(dev);
1713 uvc_ctrl_cleanup_device(dev);
1714
555d980e
TI
1715 usb_put_intf(dev->intf);
1716 usb_put_dev(dev->udev);
1717
5a254d75
LP
1718 if (dev->vdev.dev)
1719 v4l2_device_unregister(&dev->vdev);
1720#ifdef CONFIG_MEDIA_CONTROLLER
1721 if (media_devnode_is_registered(&dev->mdev.devnode))
1722 media_device_unregister(&dev->mdev);
1723#endif
1724
716fdee1
LP
1725 list_for_each_safe(p, n, &dev->chains) {
1726 struct uvc_video_chain *chain;
1727 chain = list_entry(p, struct uvc_video_chain, list);
1728 kfree(chain);
1729 }
1730
1731 list_for_each_safe(p, n, &dev->entities) {
1732 struct uvc_entity *entity;
1733 entity = list_entry(p, struct uvc_entity, list);
4ffc2d89
LP
1734#ifdef CONFIG_MEDIA_CONTROLLER
1735 uvc_mc_cleanup_entity(entity);
1736#endif
8a65a948
LP
1737 if (entity->vdev) {
1738 video_device_release(entity->vdev);
1739 entity->vdev = NULL;
1740 }
716fdee1
LP
1741 kfree(entity);
1742 }
1743
1744 list_for_each_safe(p, n, &dev->streams) {
1745 struct uvc_streaming *streaming;
1746 streaming = list_entry(p, struct uvc_streaming, list);
1747 usb_driver_release_interface(&uvc_driver.driver,
1748 streaming->intf);
1749 usb_put_intf(streaming->intf);
1750 kfree(streaming->format);
1751 kfree(streaming->header.bmaControls);
1752 kfree(streaming);
1753 }
1754
1755 kfree(dev);
1756}
1757
1758static void uvc_release(struct video_device *vdev)
1759{
1760 struct uvc_streaming *stream = video_get_drvdata(vdev);
1761 struct uvc_device *dev = stream->dev;
1762
716fdee1
LP
1763 /* Decrement the registered streams count and delete the device when it
1764 * reaches zero.
1765 */
1766 if (atomic_dec_and_test(&dev->nstreams))
1767 uvc_delete(dev);
1768}
1769
8e113595
LP
1770/*
1771 * Unregister the video devices.
1772 */
1773static void uvc_unregister_video(struct uvc_device *dev)
1774{
1775 struct uvc_streaming *stream;
1776
716fdee1
LP
1777 /* Unregistering all video devices might result in uvc_delete() being
1778 * called from inside the loop if there's no open file handle. To avoid
1779 * that, increment the stream count before iterating over the streams
1780 * and decrement it when done.
1781 */
1782 atomic_inc(&dev->nstreams);
1783
8e113595
LP
1784 list_for_each_entry(stream, &dev->streams, list) {
1785 if (stream->vdev == NULL)
1786 continue;
1787
716fdee1 1788 video_unregister_device(stream->vdev);
8e113595 1789 stream->vdev = NULL;
edbaa398
AF
1790
1791 uvc_debugfs_cleanup_stream(stream);
8e113595 1792 }
716fdee1
LP
1793
1794 /* Decrement the stream count and call uvc_delete explicitly if there
1795 * are no stream left.
1796 */
1797 if (atomic_dec_and_test(&dev->nstreams))
1798 uvc_delete(dev);
8e113595
LP
1799}
1800
1801static int uvc_register_video(struct uvc_device *dev,
1802 struct uvc_streaming *stream)
1803{
1804 struct video_device *vdev;
1805 int ret;
1806
1807 /* Initialize the streaming interface with default streaming
1808 * parameters.
1809 */
1810 ret = uvc_video_init(stream);
1811 if (ret < 0) {
1812 uvc_printk(KERN_ERR, "Failed to initialize the device "
1813 "(%d).\n", ret);
1814 return ret;
1815 }
1816
edbaa398
AF
1817 uvc_debugfs_init_stream(stream);
1818
8e113595
LP
1819 /* Register the device with V4L. */
1820 vdev = video_device_alloc();
1821 if (vdev == NULL) {
1822 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1823 ret);
1824 return -ENOMEM;
1825 }
1826
1827 /* We already hold a reference to dev->udev. The video device will be
1828 * unregistered before the reference is released, so we don't need to
1829 * get another one.
1830 */
5a254d75 1831 vdev->v4l2_dev = &dev->vdev;
8e113595 1832 vdev->fops = &uvc_fops;
716fdee1 1833 vdev->release = uvc_release;
0550513c
LP
1834 vdev->prio = &stream->chain->prio;
1835 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
954f340f
HV
1836 if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1837 vdev->vfl_dir = VFL_DIR_TX;
8e113595
LP
1838 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1839
1840 /* Set the driver data before calling video_register_device, otherwise
1841 * uvc_v4l2_open might race us.
1842 */
1843 stream->vdev = vdev;
1844 video_set_drvdata(vdev, stream);
1845
1846 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1847 if (ret < 0) {
1848 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1849 ret);
1850 stream->vdev = NULL;
1851 video_device_release(vdev);
1852 return ret;
1853 }
1854
f887e99a
LP
1855 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1856 stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE;
1857 else
1858 stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
1859
716fdee1 1860 atomic_inc(&dev->nstreams);
8e113595
LP
1861 return 0;
1862}
1863
1864/*
1865 * Register all video devices in all chains.
1866 */
1867static int uvc_register_terms(struct uvc_device *dev,
6241d8ca 1868 struct uvc_video_chain *chain)
8e113595
LP
1869{
1870 struct uvc_streaming *stream;
1871 struct uvc_entity *term;
1872 int ret;
1873
6241d8ca 1874 list_for_each_entry(term, &chain->entities, chain) {
8e113595
LP
1875 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1876 continue;
1877
1878 stream = uvc_stream_by_id(dev, term->id);
1879 if (stream == NULL) {
1880 uvc_printk(KERN_INFO, "No streaming interface found "
1881 "for terminal %u.", term->id);
1882 continue;
1883 }
1884
1885 stream->chain = chain;
1886 ret = uvc_register_video(dev, stream);
1887 if (ret < 0)
1888 return ret;
8a65a948
LP
1889
1890 term->vdev = stream->vdev;
8e113595
LP
1891 }
1892
1893 return 0;
1894}
1895
1896static int uvc_register_chains(struct uvc_device *dev)
1897{
1898 struct uvc_video_chain *chain;
1899 int ret;
1900
1901 list_for_each_entry(chain, &dev->chains, list) {
6241d8ca 1902 ret = uvc_register_terms(dev, chain);
8e113595
LP
1903 if (ret < 0)
1904 return ret;
4ffc2d89
LP
1905
1906#ifdef CONFIG_MEDIA_CONTROLLER
1907 ret = uvc_mc_register_entities(chain);
1908 if (ret < 0) {
1909 uvc_printk(KERN_INFO, "Failed to register entites "
1910 "(%d).\n", ret);
1911 }
1912#endif
8e113595
LP
1913 }
1914
1915 return 0;
1916}
1917
1918/* ------------------------------------------------------------------------
1919 * USB probe, disconnect, suspend and resume
1920 */
1921
c0efd232
LP
1922static int uvc_probe(struct usb_interface *intf,
1923 const struct usb_device_id *id)
1924{
1925 struct usb_device *udev = interface_to_usbdev(intf);
1926 struct uvc_device *dev;
1927 int ret;
1928
1929 if (id->idVendor && id->idProduct)
1930 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1931 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1932 id->idProduct);
1933 else
1934 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1935 udev->devpath);
1936
2c2d264b 1937 /* Allocate memory for the device and initialize it. */
c0efd232
LP
1938 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1939 return -ENOMEM;
1940
1941 INIT_LIST_HEAD(&dev->entities);
8e113595 1942 INIT_LIST_HEAD(&dev->chains);
35f02a68 1943 INIT_LIST_HEAD(&dev->streams);
716fdee1 1944 atomic_set(&dev->nstreams, 0);
04a37e0f 1945 atomic_set(&dev->users, 0);
8fb91b33 1946 atomic_set(&dev->nmappings, 0);
c0efd232
LP
1947
1948 dev->udev = usb_get_dev(udev);
1949 dev->intf = usb_get_intf(intf);
1950 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
73de3592
LP
1951 dev->quirks = (uvc_quirks_param == -1)
1952 ? id->driver_info : uvc_quirks_param;
c0efd232
LP
1953
1954 if (udev->product != NULL)
d0ebf307 1955 strlcpy(dev->name, udev->product, sizeof dev->name);
c0efd232
LP
1956 else
1957 snprintf(dev->name, sizeof dev->name,
1958 "UVC Camera (%04x:%04x)",
1959 le16_to_cpu(udev->descriptor.idVendor),
1960 le16_to_cpu(udev->descriptor.idProduct));
1961
2c2d264b 1962 /* Parse the Video Class control descriptor. */
c0efd232
LP
1963 if (uvc_parse_control(dev) < 0) {
1964 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1965 "descriptors.\n");
1966 goto error;
1967 }
1968
fba4578e 1969 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
c0efd232
LP
1970 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1971 udev->product ? udev->product : "<unnamed>",
1972 le16_to_cpu(udev->descriptor.idVendor),
1973 le16_to_cpu(udev->descriptor.idProduct));
1974
73de3592
LP
1975 if (dev->quirks != id->driver_info) {
1976 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1977 "parameter for testing purpose.\n", dev->quirks);
c0efd232
LP
1978 uvc_printk(KERN_INFO, "Please report required quirks to the "
1979 "linux-uvc-devel mailing list.\n");
1980 }
1981
5a254d75
LP
1982 /* Register the media and V4L2 devices. */
1983#ifdef CONFIG_MEDIA_CONTROLLER
1984 dev->mdev.dev = &intf->dev;
1985 strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
1986 if (udev->serial)
1987 strlcpy(dev->mdev.serial, udev->serial,
1988 sizeof(dev->mdev.serial));
1989 strcpy(dev->mdev.bus_info, udev->devpath);
1990 dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
fd3e5824 1991 dev->mdev.driver_version = LINUX_VERSION_CODE;
5a254d75
LP
1992 if (media_device_register(&dev->mdev) < 0)
1993 goto error;
1994
1995 dev->vdev.mdev = &dev->mdev;
1996#endif
1997 if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
1998 goto error;
1999
2c2d264b 2000 /* Initialize controls. */
c0efd232
LP
2001 if (uvc_ctrl_init_device(dev) < 0)
2002 goto error;
2003
8e113595 2004 /* Scan the device for video chains. */
35f02a68 2005 if (uvc_scan_device(dev) < 0)
c0efd232
LP
2006 goto error;
2007
5a254d75 2008 /* Register video device nodes. */
8e113595
LP
2009 if (uvc_register_chains(dev) < 0)
2010 goto error;
2011
2c2d264b 2012 /* Save our data pointer in the interface data. */
c0efd232
LP
2013 usb_set_intfdata(intf, dev);
2014
2c2d264b 2015 /* Initialize the interrupt URB. */
c0efd232
LP
2016 if ((ret = uvc_status_init(dev)) < 0) {
2017 uvc_printk(KERN_INFO, "Unable to initialize the status "
2018 "endpoint (%d), status interrupt will not be "
2019 "supported.\n", ret);
2020 }
2021
2022 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
3dae8b41 2023 usb_enable_autosuspend(udev);
c0efd232
LP
2024 return 0;
2025
2026error:
716fdee1 2027 uvc_unregister_video(dev);
c0efd232
LP
2028 return -ENODEV;
2029}
2030
2031static void uvc_disconnect(struct usb_interface *intf)
2032{
2033 struct uvc_device *dev = usb_get_intfdata(intf);
2034
2035 /* Set the USB interface data to NULL. This can be done outside the
2036 * lock, as there's no other reader.
2037 */
2038 usb_set_intfdata(intf, NULL);
2039
b482d923
LP
2040 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2041 UVC_SC_VIDEOSTREAMING)
c0efd232
LP
2042 return;
2043
c0efd232 2044 dev->state |= UVC_DEV_DISCONNECTED;
a9e28585 2045
716fdee1 2046 uvc_unregister_video(dev);
c0efd232
LP
2047}
2048
2049static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
2050{
2051 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 2052 struct uvc_streaming *stream;
c0efd232
LP
2053
2054 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
2055 intf->cur_altsetting->desc.bInterfaceNumber);
2056
2057 /* Controls are cached on the fly so they don't need to be saved. */
b482d923
LP
2058 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2059 UVC_SC_VIDEOCONTROL)
c0efd232
LP
2060 return uvc_status_suspend(dev);
2061
35f02a68
LP
2062 list_for_each_entry(stream, &dev->streams, list) {
2063 if (stream->intf == intf)
2064 return uvc_video_suspend(stream);
c0efd232
LP
2065 }
2066
35f02a68
LP
2067 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
2068 "mismatch.\n");
2069 return -EINVAL;
c0efd232
LP
2070}
2071
9b0ae867 2072static int __uvc_resume(struct usb_interface *intf, int reset)
c0efd232
LP
2073{
2074 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 2075 struct uvc_streaming *stream;
c0efd232
LP
2076
2077 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
2078 intf->cur_altsetting->desc.bInterfaceNumber);
2079
b482d923
LP
2080 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
2081 UVC_SC_VIDEOCONTROL) {
7564f67d
HV
2082 if (reset) {
2083 int ret = uvc_ctrl_resume_device(dev);
2084
2085 if (ret < 0)
2086 return ret;
2087 }
c0efd232
LP
2088
2089 return uvc_status_resume(dev);
2090 }
2091
35f02a68
LP
2092 list_for_each_entry(stream, &dev->streams, list) {
2093 if (stream->intf == intf)
d59a7b1d 2094 return uvc_video_resume(stream, reset);
c0efd232
LP
2095 }
2096
35f02a68
LP
2097 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
2098 "mismatch.\n");
2099 return -EINVAL;
c0efd232
LP
2100}
2101
9b0ae867
LP
2102static int uvc_resume(struct usb_interface *intf)
2103{
2104 return __uvc_resume(intf, 0);
2105}
2106
2107static int uvc_reset_resume(struct usb_interface *intf)
2108{
2109 return __uvc_resume(intf, 1);
2110}
2111
310fe524
LP
2112/* ------------------------------------------------------------------------
2113 * Module parameters
2114 */
2115
2116static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
2117{
2118 if (uvc_clock_param == CLOCK_MONOTONIC)
2119 return sprintf(buffer, "CLOCK_MONOTONIC");
2120 else
2121 return sprintf(buffer, "CLOCK_REALTIME");
2122}
2123
2124static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
2125{
2126 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2127 val += strlen("clock_");
2128
2129 if (strcasecmp(val, "monotonic") == 0)
2130 uvc_clock_param = CLOCK_MONOTONIC;
2131 else if (strcasecmp(val, "realtime") == 0)
2132 uvc_clock_param = CLOCK_REALTIME;
2133 else
2134 return -EINVAL;
2135
2136 return 0;
2137}
2138
2139module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2140 &uvc_clock_param, S_IRUGO|S_IWUSR);
2141MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2142module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2143MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2144module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2145MODULE_PARM_DESC(quirks, "Forced device quirks");
2146module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2147MODULE_PARM_DESC(trace, "Trace level bitmask");
2148module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2149MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2150
c0efd232
LP
2151/* ------------------------------------------------------------------------
2152 * Driver initialization and cleanup
2153 */
2154
2155/*
2156 * The Logitech cameras listed below have their interface class set to
2157 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2158 * though they are compliant.
2159 */
2160static struct usb_device_id uvc_ids[] = {
4eb2697e
LP
2161 /* LogiLink Wireless Webcam */
2162 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2163 | USB_DEVICE_ID_MATCH_INT_INFO,
2164 .idVendor = 0x0416,
2165 .idProduct = 0xa91a,
2166 .bInterfaceClass = USB_CLASS_VIDEO,
2167 .bInterfaceSubClass = 1,
2168 .bInterfaceProtocol = 0,
2169 .driver_info = UVC_QUIRK_PROBE_MINMAX },
bce039c0
LP
2170 /* Genius eFace 2025 */
2171 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2172 | USB_DEVICE_ID_MATCH_INT_INFO,
2173 .idVendor = 0x0458,
2174 .idProduct = 0x706e,
2175 .bInterfaceClass = USB_CLASS_VIDEO,
2176 .bInterfaceSubClass = 1,
2177 .bInterfaceProtocol = 0,
2178 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2179 /* Microsoft Lifecam NX-6000 */
2180 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2181 | USB_DEVICE_ID_MATCH_INT_INFO,
2182 .idVendor = 0x045e,
2183 .idProduct = 0x00f8,
2184 .bInterfaceClass = USB_CLASS_VIDEO,
2185 .bInterfaceSubClass = 1,
2186 .bInterfaceProtocol = 0,
2187 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2188 /* Microsoft Lifecam VX-7000 */
2189 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2190 | USB_DEVICE_ID_MATCH_INT_INFO,
2191 .idVendor = 0x045e,
2192 .idProduct = 0x0723,
2193 .bInterfaceClass = USB_CLASS_VIDEO,
2194 .bInterfaceSubClass = 1,
2195 .bInterfaceProtocol = 0,
2196 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2197 /* Logitech Quickcam Fusion */
2198 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2199 | USB_DEVICE_ID_MATCH_INT_INFO,
2200 .idVendor = 0x046d,
2201 .idProduct = 0x08c1,
2202 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2203 .bInterfaceSubClass = 1,
2204 .bInterfaceProtocol = 0 },
2205 /* Logitech Quickcam Orbit MP */
2206 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2207 | USB_DEVICE_ID_MATCH_INT_INFO,
2208 .idVendor = 0x046d,
2209 .idProduct = 0x08c2,
2210 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2211 .bInterfaceSubClass = 1,
2212 .bInterfaceProtocol = 0 },
2213 /* Logitech Quickcam Pro for Notebook */
2214 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2215 | USB_DEVICE_ID_MATCH_INT_INFO,
2216 .idVendor = 0x046d,
2217 .idProduct = 0x08c3,
2218 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2219 .bInterfaceSubClass = 1,
2220 .bInterfaceProtocol = 0 },
2221 /* Logitech Quickcam Pro 5000 */
2222 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2223 | USB_DEVICE_ID_MATCH_INT_INFO,
2224 .idVendor = 0x046d,
2225 .idProduct = 0x08c5,
2226 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2227 .bInterfaceSubClass = 1,
2228 .bInterfaceProtocol = 0 },
2229 /* Logitech Quickcam OEM Dell Notebook */
2230 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2231 | USB_DEVICE_ID_MATCH_INT_INFO,
2232 .idVendor = 0x046d,
2233 .idProduct = 0x08c6,
2234 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2235 .bInterfaceSubClass = 1,
2236 .bInterfaceProtocol = 0 },
2237 /* Logitech Quickcam OEM Cisco VT Camera II */
2238 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2239 | USB_DEVICE_ID_MATCH_INT_INFO,
2240 .idVendor = 0x046d,
2241 .idProduct = 0x08c7,
2242 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2243 .bInterfaceSubClass = 1,
2244 .bInterfaceProtocol = 0 },
86d8b6ab
LP
2245 /* Chicony CNF7129 (Asus EEE 100HE) */
2246 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2247 | USB_DEVICE_ID_MATCH_INT_INFO,
2248 .idVendor = 0x04f2,
2249 .idProduct = 0xb071,
2250 .bInterfaceClass = USB_CLASS_VIDEO,
2251 .bInterfaceSubClass = 1,
2252 .bInterfaceProtocol = 0,
2253 .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
f61d1d8a
LP
2254 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2255 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2256 | USB_DEVICE_ID_MATCH_INT_INFO,
2257 .idVendor = 0x058f,
2258 .idProduct = 0x3820,
2259 .bInterfaceClass = USB_CLASS_VIDEO,
2260 .bInterfaceSubClass = 1,
2261 .bInterfaceProtocol = 0,
2262 .driver_info = UVC_QUIRK_PROBE_MINMAX },
3efe2f1b
LP
2263 /* Dell XPS m1530 */
2264 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2265 | USB_DEVICE_ID_MATCH_INT_INFO,
2266 .idVendor = 0x05a9,
2267 .idProduct = 0x2640,
2268 .bInterfaceClass = USB_CLASS_VIDEO,
2269 .bInterfaceSubClass = 1,
2270 .bInterfaceProtocol = 0,
2271 .driver_info = UVC_QUIRK_PROBE_DEF },
c0efd232 2272 /* Apple Built-In iSight */
2c2d264b 2273 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232
LP
2274 | USB_DEVICE_ID_MATCH_INT_INFO,
2275 .idVendor = 0x05ac,
2276 .idProduct = 0x8501,
2c2d264b
LP
2277 .bInterfaceClass = USB_CLASS_VIDEO,
2278 .bInterfaceSubClass = 1,
2279 .bInterfaceProtocol = 0,
c0efd232
LP
2280 .driver_info = UVC_QUIRK_PROBE_MINMAX
2281 | UVC_QUIRK_BUILTIN_ISIGHT },
949d9264
KS
2282 /* Foxlink ("HP Webcam" on HP Mini 5103) */
2283 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2284 | USB_DEVICE_ID_MATCH_INT_INFO,
2285 .idVendor = 0x05c8,
2286 .idProduct = 0x0403,
2287 .bInterfaceClass = USB_CLASS_VIDEO,
2288 .bInterfaceSubClass = 1,
2289 .bInterfaceProtocol = 0,
2290 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
c0efd232 2291 /* Genesys Logic USB 2.0 PC Camera */
2c2d264b 2292 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232 2293 | USB_DEVICE_ID_MATCH_INT_INFO,
2c2d264b
LP
2294 .idVendor = 0x05e3,
2295 .idProduct = 0x0505,
2296 .bInterfaceClass = USB_CLASS_VIDEO,
2297 .bInterfaceSubClass = 1,
2298 .bInterfaceProtocol = 0,
2299 .driver_info = UVC_QUIRK_STREAM_NO_FID },
e01a2344
LP
2300 /* Hercules Classic Silver */
2301 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2302 | USB_DEVICE_ID_MATCH_INT_INFO,
2303 .idVendor = 0x06f8,
2304 .idProduct = 0x300c,
2305 .bInterfaceClass = USB_CLASS_VIDEO,
2306 .bInterfaceSubClass = 1,
2307 .bInterfaceProtocol = 0,
2308 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
d79cd839
LP
2309 /* ViMicro Vega */
2310 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2311 | USB_DEVICE_ID_MATCH_INT_INFO,
2312 .idVendor = 0x0ac8,
2313 .idProduct = 0x332d,
2314 .bInterfaceClass = USB_CLASS_VIDEO,
2315 .bInterfaceSubClass = 1,
2316 .bInterfaceProtocol = 0,
2317 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2318 /* ViMicro - Minoru3D */
2319 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2320 | USB_DEVICE_ID_MATCH_INT_INFO,
2321 .idVendor = 0x0ac8,
2322 .idProduct = 0x3410,
2323 .bInterfaceClass = USB_CLASS_VIDEO,
2324 .bInterfaceSubClass = 1,
2325 .bInterfaceProtocol = 0,
2326 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2327 /* ViMicro Venus - Minoru3D */
2328 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
50144aee
LP
2329 | USB_DEVICE_ID_MATCH_INT_INFO,
2330 .idVendor = 0x0ac8,
d79cd839 2331 .idProduct = 0x3420,
50144aee
LP
2332 .bInterfaceClass = USB_CLASS_VIDEO,
2333 .bInterfaceSubClass = 1,
2334 .bInterfaceProtocol = 0,
2335 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
d584b838
LP
2336 /* Ophir Optronics - SPCAM 620U */
2337 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2338 | USB_DEVICE_ID_MATCH_INT_INFO,
2339 .idVendor = 0x0bd3,
2340 .idProduct = 0x0555,
2341 .bInterfaceClass = USB_CLASS_VIDEO,
2342 .bInterfaceSubClass = 1,
2343 .bInterfaceProtocol = 0,
2344 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2345 /* MT6227 */
2346 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2347 | USB_DEVICE_ID_MATCH_INT_INFO,
2348 .idVendor = 0x0e8d,
2349 .idProduct = 0x0004,
2350 .bInterfaceClass = USB_CLASS_VIDEO,
2351 .bInterfaceSubClass = 1,
2352 .bInterfaceProtocol = 0,
bab6f66c
LP
2353 .driver_info = UVC_QUIRK_PROBE_MINMAX
2354 | UVC_QUIRK_PROBE_DEF },
9275b32b
LP
2355 /* IMC Networks (Medion Akoya) */
2356 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2357 | USB_DEVICE_ID_MATCH_INT_INFO,
2358 .idVendor = 0x13d3,
2359 .idProduct = 0x5103,
2360 .bInterfaceClass = USB_CLASS_VIDEO,
2361 .bInterfaceSubClass = 1,
2362 .bInterfaceProtocol = 0,
2363 .driver_info = UVC_QUIRK_STREAM_NO_FID },
d1787b1f
LP
2364 /* JMicron USB2.0 XGA WebCam */
2365 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2366 | USB_DEVICE_ID_MATCH_INT_INFO,
2367 .idVendor = 0x152d,
2368 .idProduct = 0x0310,
2369 .bInterfaceClass = USB_CLASS_VIDEO,
2370 .bInterfaceSubClass = 1,
2371 .bInterfaceProtocol = 0,
2372 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2373 /* Syntek (HP Spartan) */
2374 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2375 | USB_DEVICE_ID_MATCH_INT_INFO,
2376 .idVendor = 0x174f,
2377 .idProduct = 0x5212,
2378 .bInterfaceClass = USB_CLASS_VIDEO,
2379 .bInterfaceSubClass = 1,
2380 .bInterfaceProtocol = 0,
25e69850 2381 .driver_info = UVC_QUIRK_STREAM_NO_FID },
562f0fed
LP
2382 /* Syntek (Samsung Q310) */
2383 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2384 | USB_DEVICE_ID_MATCH_INT_INFO,
2385 .idVendor = 0x174f,
2386 .idProduct = 0x5931,
2387 .bInterfaceClass = USB_CLASS_VIDEO,
2388 .bInterfaceSubClass = 1,
2389 .bInterfaceProtocol = 0,
2390 .driver_info = UVC_QUIRK_STREAM_NO_FID },
f129b03b
LP
2391 /* Syntek (Packard Bell EasyNote MX52 */
2392 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2393 | USB_DEVICE_ID_MATCH_INT_INFO,
2394 .idVendor = 0x174f,
2395 .idProduct = 0x8a12,
2396 .bInterfaceClass = USB_CLASS_VIDEO,
2397 .bInterfaceSubClass = 1,
2398 .bInterfaceProtocol = 0,
2399 .driver_info = UVC_QUIRK_STREAM_NO_FID },
f61d1d8a 2400 /* Syntek (Asus F9SG) */
25e69850
LP
2401 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2402 | USB_DEVICE_ID_MATCH_INT_INFO,
2403 .idVendor = 0x174f,
2404 .idProduct = 0x8a31,
2405 .bInterfaceClass = USB_CLASS_VIDEO,
2406 .bInterfaceSubClass = 1,
2407 .bInterfaceProtocol = 0,
c0efd232
LP
2408 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2409 /* Syntek (Asus U3S) */
2410 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2411 | USB_DEVICE_ID_MATCH_INT_INFO,
2412 .idVendor = 0x174f,
2413 .idProduct = 0x8a33,
2414 .bInterfaceClass = USB_CLASS_VIDEO,
2415 .bInterfaceSubClass = 1,
2416 .bInterfaceProtocol = 0,
2417 .driver_info = UVC_QUIRK_STREAM_NO_FID },
0ce566da
LP
2418 /* Syntek (JAOtech Smart Terminal) */
2419 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2420 | USB_DEVICE_ID_MATCH_INT_INFO,
2421 .idVendor = 0x174f,
2422 .idProduct = 0x8a34,
2423 .bInterfaceClass = USB_CLASS_VIDEO,
2424 .bInterfaceSubClass = 1,
2425 .bInterfaceProtocol = 0,
2426 .driver_info = UVC_QUIRK_STREAM_NO_FID },
70092c26
LP
2427 /* Miricle 307K */
2428 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2429 | USB_DEVICE_ID_MATCH_INT_INFO,
2430 .idVendor = 0x17dc,
2431 .idProduct = 0x0202,
2432 .bInterfaceClass = USB_CLASS_VIDEO,
2433 .bInterfaceSubClass = 1,
2434 .bInterfaceProtocol = 0,
2435 .driver_info = UVC_QUIRK_STREAM_NO_FID },
849a3aba 2436 /* Lenovo Thinkpad SL400/SL500 */
2f38483b
LP
2437 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2438 | USB_DEVICE_ID_MATCH_INT_INFO,
2439 .idVendor = 0x17ef,
2440 .idProduct = 0x480b,
2441 .bInterfaceClass = USB_CLASS_VIDEO,
2442 .bInterfaceSubClass = 1,
2443 .bInterfaceProtocol = 0,
2444 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2d2bf2a3
LP
2445 /* Aveo Technology USB 2.0 Camera */
2446 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2447 | USB_DEVICE_ID_MATCH_INT_INFO,
2448 .idVendor = 0x1871,
2449 .idProduct = 0x0306,
2450 .bInterfaceClass = USB_CLASS_VIDEO,
2451 .bInterfaceSubClass = 1,
2452 .bInterfaceProtocol = 0,
5bdf1377
LP
2453 .driver_info = UVC_QUIRK_PROBE_MINMAX
2454 | UVC_QUIRK_PROBE_EXTRAFIELDS },
c0efd232
LP
2455 /* Ecamm Pico iMage */
2456 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2457 | USB_DEVICE_ID_MATCH_INT_INFO,
2458 .idVendor = 0x18cd,
2459 .idProduct = 0xcafe,
2460 .bInterfaceClass = USB_CLASS_VIDEO,
2461 .bInterfaceSubClass = 1,
2462 .bInterfaceProtocol = 0,
2463 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2bb00fe6
LP
2464 /* Manta MM-353 Plako */
2465 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2466 | USB_DEVICE_ID_MATCH_INT_INFO,
2467 .idVendor = 0x18ec,
2468 .idProduct = 0x3188,
2469 .bInterfaceClass = USB_CLASS_VIDEO,
2470 .bInterfaceSubClass = 1,
2471 .bInterfaceProtocol = 0,
2472 .driver_info = UVC_QUIRK_PROBE_MINMAX },
ca4a3456
LP
2473 /* FSC WebCam V30S */
2474 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2475 | USB_DEVICE_ID_MATCH_INT_INFO,
2476 .idVendor = 0x18ec,
2477 .idProduct = 0x3288,
2478 .bInterfaceClass = USB_CLASS_VIDEO,
2479 .bInterfaceSubClass = 1,
2480 .bInterfaceProtocol = 0,
2481 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1e4d05bc
LP
2482 /* Arkmicro unbranded */
2483 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2484 | USB_DEVICE_ID_MATCH_INT_INFO,
2485 .idVendor = 0x18ec,
2486 .idProduct = 0x3290,
2487 .bInterfaceClass = USB_CLASS_VIDEO,
2488 .bInterfaceSubClass = 1,
2489 .bInterfaceProtocol = 0,
2490 .driver_info = UVC_QUIRK_PROBE_DEF },
fd3e9f2f
AC
2491 /* The Imaging Source USB CCD cameras */
2492 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2493 | USB_DEVICE_ID_MATCH_INT_INFO,
2494 .idVendor = 0x199e,
2495 .idProduct = 0x8102,
2496 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2497 .bInterfaceSubClass = 1,
2498 .bInterfaceProtocol = 0 },
c0efd232
LP
2499 /* Bodelin ProScopeHR */
2500 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2501 | USB_DEVICE_ID_MATCH_DEV_HI
2502 | USB_DEVICE_ID_MATCH_INT_INFO,
2503 .idVendor = 0x19ab,
2504 .idProduct = 0x1000,
2505 .bcdDevice_hi = 0x0126,
2506 .bInterfaceClass = USB_CLASS_VIDEO,
2507 .bInterfaceSubClass = 1,
2508 .bInterfaceProtocol = 0,
2509 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
3bc766ad
LP
2510 /* MSI StarCam 370i */
2511 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2512 | USB_DEVICE_ID_MATCH_INT_INFO,
2513 .idVendor = 0x1b3b,
2514 .idProduct = 0x2951,
2515 .bInterfaceClass = USB_CLASS_VIDEO,
2516 .bInterfaceSubClass = 1,
2517 .bInterfaceProtocol = 0,
2518 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2519 /* SiGma Micro USB Web Camera */
2520 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2521 | USB_DEVICE_ID_MATCH_INT_INFO,
2522 .idVendor = 0x1c4f,
2523 .idProduct = 0x3000,
2524 .bInterfaceClass = USB_CLASS_VIDEO,
2525 .bInterfaceSubClass = 1,
2526 .bInterfaceProtocol = 0,
2527 .driver_info = UVC_QUIRK_PROBE_MINMAX
d732c44c 2528 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
c0efd232
LP
2529 /* Generic USB Video Class */
2530 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2531 {}
2532};
2533
2534MODULE_DEVICE_TABLE(usb, uvc_ids);
2535
2536struct uvc_driver uvc_driver = {
2537 .driver = {
2538 .name = "uvcvideo",
2539 .probe = uvc_probe,
2540 .disconnect = uvc_disconnect,
2541 .suspend = uvc_suspend,
2542 .resume = uvc_resume,
9b0ae867 2543 .reset_resume = uvc_reset_resume,
c0efd232
LP
2544 .id_table = uvc_ids,
2545 .supports_autosuspend = 1,
2546 },
2547};
2548
2549static int __init uvc_init(void)
2550{
edbaa398 2551 int ret;
c0efd232 2552
edbaa398
AF
2553 uvc_debugfs_init();
2554
2555 ret = usb_register(&uvc_driver.driver);
2556 if (ret < 0) {
2557 uvc_debugfs_cleanup();
2558 return ret;
2559 }
2560
2561 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2562 return 0;
c0efd232
LP
2563}
2564
2565static void __exit uvc_cleanup(void)
2566{
2567 usb_deregister(&uvc_driver.driver);
edbaa398 2568 uvc_debugfs_cleanup();
c0efd232
LP
2569}
2570
2571module_init(uvc_init);
2572module_exit(uvc_cleanup);
2573
c0efd232
LP
2574MODULE_AUTHOR(DRIVER_AUTHOR);
2575MODULE_DESCRIPTION(DRIVER_DESC);
2576MODULE_LICENSE("GPL");
2577MODULE_VERSION(DRIVER_VERSION);
f87086e3 2578