]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/usb/uvc/uvc_driver.c
[media] v4l2-dev: add new VFL_DIR_ defines
[mirror_ubuntu-bionic-kernel.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
1536/*
35f02a68 1537 * Scan the device for video chains and register video devices.
c0efd232 1538 *
8e113595 1539 * Chains are scanned starting at their output terminals and walked backwards.
c0efd232 1540 */
35f02a68 1541static int uvc_scan_device(struct uvc_device *dev)
c0efd232 1542{
8e113595 1543 struct uvc_video_chain *chain;
c0efd232 1544 struct uvc_entity *term;
c0efd232 1545
c0efd232 1546 list_for_each_entry(term, &dev->entities, list) {
8e113595 1547 if (!UVC_ENTITY_IS_OTERM(term))
c0efd232
LP
1548 continue;
1549
8e113595
LP
1550 /* If the terminal is already included in a chain, skip it.
1551 * This can happen for chains that have multiple output
1552 * terminals, where all output terminals beside the first one
1553 * will be inserted in the chain in forward scans.
1554 */
1555 if (term->chain.next || term->chain.prev)
c0efd232
LP
1556 continue;
1557
8e113595
LP
1558 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1559 if (chain == NULL)
1560 return -ENOMEM;
1561
6241d8ca 1562 INIT_LIST_HEAD(&chain->entities);
8e113595
LP
1563 mutex_init(&chain->ctrl_mutex);
1564 chain->dev = dev;
1565
1566 if (uvc_scan_chain(chain, term) < 0) {
1567 kfree(chain);
1568 continue;
c0efd232 1569 }
8e113595
LP
1570
1571 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1572 uvc_print_chain(chain));
1573
1574 list_add_tail(&chain->list, &dev->chains);
c0efd232
LP
1575 }
1576
8e113595 1577 if (list_empty(&dev->chains)) {
c0efd232
LP
1578 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1579 return -1;
1580 }
1581
c0efd232
LP
1582 return 0;
1583}
1584
8e113595
LP
1585/* ------------------------------------------------------------------------
1586 * Video device registration and unregistration
1587 */
1588
716fdee1
LP
1589/*
1590 * Delete the UVC device.
1591 *
1592 * Called by the kernel when the last reference to the uvc_device structure
1593 * is released.
1594 *
1595 * As this function is called after or during disconnect(), all URBs have
1596 * already been canceled by the USB core. There is no need to kill the
1597 * interrupt URB manually.
1598 */
1599static void uvc_delete(struct uvc_device *dev)
1600{
1601 struct list_head *p, *n;
1602
1603 usb_put_intf(dev->intf);
1604 usb_put_dev(dev->udev);
1605
1606 uvc_status_cleanup(dev);
1607 uvc_ctrl_cleanup_device(dev);
1608
5a254d75
LP
1609 if (dev->vdev.dev)
1610 v4l2_device_unregister(&dev->vdev);
1611#ifdef CONFIG_MEDIA_CONTROLLER
1612 if (media_devnode_is_registered(&dev->mdev.devnode))
1613 media_device_unregister(&dev->mdev);
1614#endif
1615
716fdee1
LP
1616 list_for_each_safe(p, n, &dev->chains) {
1617 struct uvc_video_chain *chain;
1618 chain = list_entry(p, struct uvc_video_chain, list);
1619 kfree(chain);
1620 }
1621
1622 list_for_each_safe(p, n, &dev->entities) {
1623 struct uvc_entity *entity;
1624 entity = list_entry(p, struct uvc_entity, list);
4ffc2d89
LP
1625#ifdef CONFIG_MEDIA_CONTROLLER
1626 uvc_mc_cleanup_entity(entity);
1627#endif
8a65a948
LP
1628 if (entity->vdev) {
1629 video_device_release(entity->vdev);
1630 entity->vdev = NULL;
1631 }
716fdee1
LP
1632 kfree(entity);
1633 }
1634
1635 list_for_each_safe(p, n, &dev->streams) {
1636 struct uvc_streaming *streaming;
1637 streaming = list_entry(p, struct uvc_streaming, list);
1638 usb_driver_release_interface(&uvc_driver.driver,
1639 streaming->intf);
1640 usb_put_intf(streaming->intf);
1641 kfree(streaming->format);
1642 kfree(streaming->header.bmaControls);
1643 kfree(streaming);
1644 }
1645
1646 kfree(dev);
1647}
1648
1649static void uvc_release(struct video_device *vdev)
1650{
1651 struct uvc_streaming *stream = video_get_drvdata(vdev);
1652 struct uvc_device *dev = stream->dev;
1653
716fdee1
LP
1654 /* Decrement the registered streams count and delete the device when it
1655 * reaches zero.
1656 */
1657 if (atomic_dec_and_test(&dev->nstreams))
1658 uvc_delete(dev);
1659}
1660
8e113595
LP
1661/*
1662 * Unregister the video devices.
1663 */
1664static void uvc_unregister_video(struct uvc_device *dev)
1665{
1666 struct uvc_streaming *stream;
1667
716fdee1
LP
1668 /* Unregistering all video devices might result in uvc_delete() being
1669 * called from inside the loop if there's no open file handle. To avoid
1670 * that, increment the stream count before iterating over the streams
1671 * and decrement it when done.
1672 */
1673 atomic_inc(&dev->nstreams);
1674
8e113595
LP
1675 list_for_each_entry(stream, &dev->streams, list) {
1676 if (stream->vdev == NULL)
1677 continue;
1678
716fdee1 1679 video_unregister_device(stream->vdev);
8e113595 1680 stream->vdev = NULL;
edbaa398
AF
1681
1682 uvc_debugfs_cleanup_stream(stream);
8e113595 1683 }
716fdee1
LP
1684
1685 /* Decrement the stream count and call uvc_delete explicitly if there
1686 * are no stream left.
1687 */
1688 if (atomic_dec_and_test(&dev->nstreams))
1689 uvc_delete(dev);
8e113595
LP
1690}
1691
1692static int uvc_register_video(struct uvc_device *dev,
1693 struct uvc_streaming *stream)
1694{
1695 struct video_device *vdev;
1696 int ret;
1697
1698 /* Initialize the streaming interface with default streaming
1699 * parameters.
1700 */
1701 ret = uvc_video_init(stream);
1702 if (ret < 0) {
1703 uvc_printk(KERN_ERR, "Failed to initialize the device "
1704 "(%d).\n", ret);
1705 return ret;
1706 }
1707
edbaa398
AF
1708 uvc_debugfs_init_stream(stream);
1709
8e113595
LP
1710 /* Register the device with V4L. */
1711 vdev = video_device_alloc();
1712 if (vdev == NULL) {
1713 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1714 ret);
1715 return -ENOMEM;
1716 }
1717
1718 /* We already hold a reference to dev->udev. The video device will be
1719 * unregistered before the reference is released, so we don't need to
1720 * get another one.
1721 */
5a254d75 1722 vdev->v4l2_dev = &dev->vdev;
8e113595 1723 vdev->fops = &uvc_fops;
716fdee1 1724 vdev->release = uvc_release;
8e113595
LP
1725 strlcpy(vdev->name, dev->name, sizeof vdev->name);
1726
1727 /* Set the driver data before calling video_register_device, otherwise
1728 * uvc_v4l2_open might race us.
1729 */
1730 stream->vdev = vdev;
1731 video_set_drvdata(vdev, stream);
1732
1733 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1734 if (ret < 0) {
1735 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1736 ret);
1737 stream->vdev = NULL;
1738 video_device_release(vdev);
1739 return ret;
1740 }
1741
716fdee1 1742 atomic_inc(&dev->nstreams);
8e113595
LP
1743 return 0;
1744}
1745
1746/*
1747 * Register all video devices in all chains.
1748 */
1749static int uvc_register_terms(struct uvc_device *dev,
6241d8ca 1750 struct uvc_video_chain *chain)
8e113595
LP
1751{
1752 struct uvc_streaming *stream;
1753 struct uvc_entity *term;
1754 int ret;
1755
6241d8ca 1756 list_for_each_entry(term, &chain->entities, chain) {
8e113595
LP
1757 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1758 continue;
1759
1760 stream = uvc_stream_by_id(dev, term->id);
1761 if (stream == NULL) {
1762 uvc_printk(KERN_INFO, "No streaming interface found "
1763 "for terminal %u.", term->id);
1764 continue;
1765 }
1766
1767 stream->chain = chain;
1768 ret = uvc_register_video(dev, stream);
1769 if (ret < 0)
1770 return ret;
8a65a948
LP
1771
1772 term->vdev = stream->vdev;
8e113595
LP
1773 }
1774
1775 return 0;
1776}
1777
1778static int uvc_register_chains(struct uvc_device *dev)
1779{
1780 struct uvc_video_chain *chain;
1781 int ret;
1782
1783 list_for_each_entry(chain, &dev->chains, list) {
6241d8ca 1784 ret = uvc_register_terms(dev, chain);
8e113595
LP
1785 if (ret < 0)
1786 return ret;
4ffc2d89
LP
1787
1788#ifdef CONFIG_MEDIA_CONTROLLER
1789 ret = uvc_mc_register_entities(chain);
1790 if (ret < 0) {
1791 uvc_printk(KERN_INFO, "Failed to register entites "
1792 "(%d).\n", ret);
1793 }
1794#endif
8e113595
LP
1795 }
1796
1797 return 0;
1798}
1799
1800/* ------------------------------------------------------------------------
1801 * USB probe, disconnect, suspend and resume
1802 */
1803
c0efd232
LP
1804static int uvc_probe(struct usb_interface *intf,
1805 const struct usb_device_id *id)
1806{
1807 struct usb_device *udev = interface_to_usbdev(intf);
1808 struct uvc_device *dev;
1809 int ret;
1810
1811 if (id->idVendor && id->idProduct)
1812 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1813 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1814 id->idProduct);
1815 else
1816 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1817 udev->devpath);
1818
2c2d264b 1819 /* Allocate memory for the device and initialize it. */
c0efd232
LP
1820 if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1821 return -ENOMEM;
1822
1823 INIT_LIST_HEAD(&dev->entities);
8e113595 1824 INIT_LIST_HEAD(&dev->chains);
35f02a68 1825 INIT_LIST_HEAD(&dev->streams);
716fdee1 1826 atomic_set(&dev->nstreams, 0);
04a37e0f 1827 atomic_set(&dev->users, 0);
8fb91b33 1828 atomic_set(&dev->nmappings, 0);
c0efd232
LP
1829
1830 dev->udev = usb_get_dev(udev);
1831 dev->intf = usb_get_intf(intf);
1832 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
73de3592
LP
1833 dev->quirks = (uvc_quirks_param == -1)
1834 ? id->driver_info : uvc_quirks_param;
c0efd232
LP
1835
1836 if (udev->product != NULL)
d0ebf307 1837 strlcpy(dev->name, udev->product, sizeof dev->name);
c0efd232
LP
1838 else
1839 snprintf(dev->name, sizeof dev->name,
1840 "UVC Camera (%04x:%04x)",
1841 le16_to_cpu(udev->descriptor.idVendor),
1842 le16_to_cpu(udev->descriptor.idProduct));
1843
2c2d264b 1844 /* Parse the Video Class control descriptor. */
c0efd232
LP
1845 if (uvc_parse_control(dev) < 0) {
1846 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1847 "descriptors.\n");
1848 goto error;
1849 }
1850
fba4578e 1851 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
c0efd232
LP
1852 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1853 udev->product ? udev->product : "<unnamed>",
1854 le16_to_cpu(udev->descriptor.idVendor),
1855 le16_to_cpu(udev->descriptor.idProduct));
1856
73de3592
LP
1857 if (dev->quirks != id->driver_info) {
1858 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1859 "parameter for testing purpose.\n", dev->quirks);
c0efd232
LP
1860 uvc_printk(KERN_INFO, "Please report required quirks to the "
1861 "linux-uvc-devel mailing list.\n");
1862 }
1863
5a254d75
LP
1864 /* Register the media and V4L2 devices. */
1865#ifdef CONFIG_MEDIA_CONTROLLER
1866 dev->mdev.dev = &intf->dev;
1867 strlcpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
1868 if (udev->serial)
1869 strlcpy(dev->mdev.serial, udev->serial,
1870 sizeof(dev->mdev.serial));
1871 strcpy(dev->mdev.bus_info, udev->devpath);
1872 dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
fd3e5824 1873 dev->mdev.driver_version = LINUX_VERSION_CODE;
5a254d75
LP
1874 if (media_device_register(&dev->mdev) < 0)
1875 goto error;
1876
1877 dev->vdev.mdev = &dev->mdev;
1878#endif
1879 if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
1880 goto error;
1881
2c2d264b 1882 /* Initialize controls. */
c0efd232
LP
1883 if (uvc_ctrl_init_device(dev) < 0)
1884 goto error;
1885
8e113595 1886 /* Scan the device for video chains. */
35f02a68 1887 if (uvc_scan_device(dev) < 0)
c0efd232
LP
1888 goto error;
1889
5a254d75 1890 /* Register video device nodes. */
8e113595
LP
1891 if (uvc_register_chains(dev) < 0)
1892 goto error;
1893
2c2d264b 1894 /* Save our data pointer in the interface data. */
c0efd232
LP
1895 usb_set_intfdata(intf, dev);
1896
2c2d264b 1897 /* Initialize the interrupt URB. */
c0efd232
LP
1898 if ((ret = uvc_status_init(dev)) < 0) {
1899 uvc_printk(KERN_INFO, "Unable to initialize the status "
1900 "endpoint (%d), status interrupt will not be "
1901 "supported.\n", ret);
1902 }
1903
1904 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
3dae8b41 1905 usb_enable_autosuspend(udev);
c0efd232
LP
1906 return 0;
1907
1908error:
716fdee1 1909 uvc_unregister_video(dev);
c0efd232
LP
1910 return -ENODEV;
1911}
1912
1913static void uvc_disconnect(struct usb_interface *intf)
1914{
1915 struct uvc_device *dev = usb_get_intfdata(intf);
1916
1917 /* Set the USB interface data to NULL. This can be done outside the
1918 * lock, as there's no other reader.
1919 */
1920 usb_set_intfdata(intf, NULL);
1921
b482d923
LP
1922 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1923 UVC_SC_VIDEOSTREAMING)
c0efd232
LP
1924 return;
1925
c0efd232 1926 dev->state |= UVC_DEV_DISCONNECTED;
a9e28585 1927
716fdee1 1928 uvc_unregister_video(dev);
c0efd232
LP
1929}
1930
1931static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1932{
1933 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 1934 struct uvc_streaming *stream;
c0efd232
LP
1935
1936 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1937 intf->cur_altsetting->desc.bInterfaceNumber);
1938
1939 /* Controls are cached on the fly so they don't need to be saved. */
b482d923
LP
1940 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1941 UVC_SC_VIDEOCONTROL)
c0efd232
LP
1942 return uvc_status_suspend(dev);
1943
35f02a68
LP
1944 list_for_each_entry(stream, &dev->streams, list) {
1945 if (stream->intf == intf)
1946 return uvc_video_suspend(stream);
c0efd232
LP
1947 }
1948
35f02a68
LP
1949 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1950 "mismatch.\n");
1951 return -EINVAL;
c0efd232
LP
1952}
1953
9b0ae867 1954static int __uvc_resume(struct usb_interface *intf, int reset)
c0efd232
LP
1955{
1956 struct uvc_device *dev = usb_get_intfdata(intf);
35f02a68 1957 struct uvc_streaming *stream;
c0efd232
LP
1958
1959 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1960 intf->cur_altsetting->desc.bInterfaceNumber);
1961
b482d923
LP
1962 if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1963 UVC_SC_VIDEOCONTROL) {
7564f67d
HV
1964 if (reset) {
1965 int ret = uvc_ctrl_resume_device(dev);
1966
1967 if (ret < 0)
1968 return ret;
1969 }
c0efd232
LP
1970
1971 return uvc_status_resume(dev);
1972 }
1973
35f02a68
LP
1974 list_for_each_entry(stream, &dev->streams, list) {
1975 if (stream->intf == intf)
d59a7b1d 1976 return uvc_video_resume(stream, reset);
c0efd232
LP
1977 }
1978
35f02a68
LP
1979 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1980 "mismatch.\n");
1981 return -EINVAL;
c0efd232
LP
1982}
1983
9b0ae867
LP
1984static int uvc_resume(struct usb_interface *intf)
1985{
1986 return __uvc_resume(intf, 0);
1987}
1988
1989static int uvc_reset_resume(struct usb_interface *intf)
1990{
1991 return __uvc_resume(intf, 1);
1992}
1993
310fe524
LP
1994/* ------------------------------------------------------------------------
1995 * Module parameters
1996 */
1997
1998static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1999{
2000 if (uvc_clock_param == CLOCK_MONOTONIC)
2001 return sprintf(buffer, "CLOCK_MONOTONIC");
2002 else
2003 return sprintf(buffer, "CLOCK_REALTIME");
2004}
2005
2006static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
2007{
2008 if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
2009 val += strlen("clock_");
2010
2011 if (strcasecmp(val, "monotonic") == 0)
2012 uvc_clock_param = CLOCK_MONOTONIC;
2013 else if (strcasecmp(val, "realtime") == 0)
2014 uvc_clock_param = CLOCK_REALTIME;
2015 else
2016 return -EINVAL;
2017
2018 return 0;
2019}
2020
2021module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
2022 &uvc_clock_param, S_IRUGO|S_IWUSR);
2023MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
2024module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2025MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2026module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2027MODULE_PARM_DESC(quirks, "Forced device quirks");
2028module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2029MODULE_PARM_DESC(trace, "Trace level bitmask");
2030module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
2031MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
2032
c0efd232
LP
2033/* ------------------------------------------------------------------------
2034 * Driver initialization and cleanup
2035 */
2036
2037/*
2038 * The Logitech cameras listed below have their interface class set to
2039 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
2040 * though they are compliant.
2041 */
2042static struct usb_device_id uvc_ids[] = {
4eb2697e
LP
2043 /* LogiLink Wireless Webcam */
2044 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2045 | USB_DEVICE_ID_MATCH_INT_INFO,
2046 .idVendor = 0x0416,
2047 .idProduct = 0xa91a,
2048 .bInterfaceClass = USB_CLASS_VIDEO,
2049 .bInterfaceSubClass = 1,
2050 .bInterfaceProtocol = 0,
2051 .driver_info = UVC_QUIRK_PROBE_MINMAX },
bce039c0
LP
2052 /* Genius eFace 2025 */
2053 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2054 | USB_DEVICE_ID_MATCH_INT_INFO,
2055 .idVendor = 0x0458,
2056 .idProduct = 0x706e,
2057 .bInterfaceClass = USB_CLASS_VIDEO,
2058 .bInterfaceSubClass = 1,
2059 .bInterfaceProtocol = 0,
2060 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2061 /* Microsoft Lifecam NX-6000 */
2062 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2063 | USB_DEVICE_ID_MATCH_INT_INFO,
2064 .idVendor = 0x045e,
2065 .idProduct = 0x00f8,
2066 .bInterfaceClass = USB_CLASS_VIDEO,
2067 .bInterfaceSubClass = 1,
2068 .bInterfaceProtocol = 0,
2069 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2070 /* Microsoft Lifecam VX-7000 */
2071 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2072 | USB_DEVICE_ID_MATCH_INT_INFO,
2073 .idVendor = 0x045e,
2074 .idProduct = 0x0723,
2075 .bInterfaceClass = USB_CLASS_VIDEO,
2076 .bInterfaceSubClass = 1,
2077 .bInterfaceProtocol = 0,
2078 .driver_info = UVC_QUIRK_PROBE_MINMAX },
2079 /* Logitech Quickcam Fusion */
2080 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2081 | USB_DEVICE_ID_MATCH_INT_INFO,
2082 .idVendor = 0x046d,
2083 .idProduct = 0x08c1,
2084 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2085 .bInterfaceSubClass = 1,
2086 .bInterfaceProtocol = 0 },
2087 /* Logitech Quickcam Orbit MP */
2088 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2089 | USB_DEVICE_ID_MATCH_INT_INFO,
2090 .idVendor = 0x046d,
2091 .idProduct = 0x08c2,
2092 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2093 .bInterfaceSubClass = 1,
2094 .bInterfaceProtocol = 0 },
2095 /* Logitech Quickcam Pro for Notebook */
2096 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2097 | USB_DEVICE_ID_MATCH_INT_INFO,
2098 .idVendor = 0x046d,
2099 .idProduct = 0x08c3,
2100 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2101 .bInterfaceSubClass = 1,
2102 .bInterfaceProtocol = 0 },
2103 /* Logitech Quickcam Pro 5000 */
2104 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2105 | USB_DEVICE_ID_MATCH_INT_INFO,
2106 .idVendor = 0x046d,
2107 .idProduct = 0x08c5,
2108 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2109 .bInterfaceSubClass = 1,
2110 .bInterfaceProtocol = 0 },
2111 /* Logitech Quickcam OEM Dell Notebook */
2112 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2113 | USB_DEVICE_ID_MATCH_INT_INFO,
2114 .idVendor = 0x046d,
2115 .idProduct = 0x08c6,
2116 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2117 .bInterfaceSubClass = 1,
2118 .bInterfaceProtocol = 0 },
2119 /* Logitech Quickcam OEM Cisco VT Camera II */
2120 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2121 | USB_DEVICE_ID_MATCH_INT_INFO,
2122 .idVendor = 0x046d,
2123 .idProduct = 0x08c7,
2124 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2125 .bInterfaceSubClass = 1,
2126 .bInterfaceProtocol = 0 },
86d8b6ab
LP
2127 /* Chicony CNF7129 (Asus EEE 100HE) */
2128 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2129 | USB_DEVICE_ID_MATCH_INT_INFO,
2130 .idVendor = 0x04f2,
2131 .idProduct = 0xb071,
2132 .bInterfaceClass = USB_CLASS_VIDEO,
2133 .bInterfaceSubClass = 1,
2134 .bInterfaceProtocol = 0,
2135 .driver_info = UVC_QUIRK_RESTRICT_FRAME_RATE },
f61d1d8a
LP
2136 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2137 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2138 | USB_DEVICE_ID_MATCH_INT_INFO,
2139 .idVendor = 0x058f,
2140 .idProduct = 0x3820,
2141 .bInterfaceClass = USB_CLASS_VIDEO,
2142 .bInterfaceSubClass = 1,
2143 .bInterfaceProtocol = 0,
2144 .driver_info = UVC_QUIRK_PROBE_MINMAX },
3efe2f1b
LP
2145 /* Dell XPS m1530 */
2146 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2147 | USB_DEVICE_ID_MATCH_INT_INFO,
2148 .idVendor = 0x05a9,
2149 .idProduct = 0x2640,
2150 .bInterfaceClass = USB_CLASS_VIDEO,
2151 .bInterfaceSubClass = 1,
2152 .bInterfaceProtocol = 0,
2153 .driver_info = UVC_QUIRK_PROBE_DEF },
c0efd232 2154 /* Apple Built-In iSight */
2c2d264b 2155 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232
LP
2156 | USB_DEVICE_ID_MATCH_INT_INFO,
2157 .idVendor = 0x05ac,
2158 .idProduct = 0x8501,
2c2d264b
LP
2159 .bInterfaceClass = USB_CLASS_VIDEO,
2160 .bInterfaceSubClass = 1,
2161 .bInterfaceProtocol = 0,
c0efd232
LP
2162 .driver_info = UVC_QUIRK_PROBE_MINMAX
2163 | UVC_QUIRK_BUILTIN_ISIGHT },
949d9264
KS
2164 /* Foxlink ("HP Webcam" on HP Mini 5103) */
2165 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2166 | USB_DEVICE_ID_MATCH_INT_INFO,
2167 .idVendor = 0x05c8,
2168 .idProduct = 0x0403,
2169 .bInterfaceClass = USB_CLASS_VIDEO,
2170 .bInterfaceSubClass = 1,
2171 .bInterfaceProtocol = 0,
2172 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
c0efd232 2173 /* Genesys Logic USB 2.0 PC Camera */
2c2d264b 2174 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
c0efd232 2175 | USB_DEVICE_ID_MATCH_INT_INFO,
2c2d264b
LP
2176 .idVendor = 0x05e3,
2177 .idProduct = 0x0505,
2178 .bInterfaceClass = USB_CLASS_VIDEO,
2179 .bInterfaceSubClass = 1,
2180 .bInterfaceProtocol = 0,
2181 .driver_info = UVC_QUIRK_STREAM_NO_FID },
e01a2344
LP
2182 /* Hercules Classic Silver */
2183 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2184 | USB_DEVICE_ID_MATCH_INT_INFO,
2185 .idVendor = 0x06f8,
2186 .idProduct = 0x300c,
2187 .bInterfaceClass = USB_CLASS_VIDEO,
2188 .bInterfaceSubClass = 1,
2189 .bInterfaceProtocol = 0,
2190 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
d79cd839
LP
2191 /* ViMicro Vega */
2192 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2193 | USB_DEVICE_ID_MATCH_INT_INFO,
2194 .idVendor = 0x0ac8,
2195 .idProduct = 0x332d,
2196 .bInterfaceClass = USB_CLASS_VIDEO,
2197 .bInterfaceSubClass = 1,
2198 .bInterfaceProtocol = 0,
2199 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2200 /* ViMicro - Minoru3D */
2201 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2202 | USB_DEVICE_ID_MATCH_INT_INFO,
2203 .idVendor = 0x0ac8,
2204 .idProduct = 0x3410,
2205 .bInterfaceClass = USB_CLASS_VIDEO,
2206 .bInterfaceSubClass = 1,
2207 .bInterfaceProtocol = 0,
2208 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
2209 /* ViMicro Venus - Minoru3D */
2210 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
50144aee
LP
2211 | USB_DEVICE_ID_MATCH_INT_INFO,
2212 .idVendor = 0x0ac8,
d79cd839 2213 .idProduct = 0x3420,
50144aee
LP
2214 .bInterfaceClass = USB_CLASS_VIDEO,
2215 .bInterfaceSubClass = 1,
2216 .bInterfaceProtocol = 0,
2217 .driver_info = UVC_QUIRK_FIX_BANDWIDTH },
d584b838
LP
2218 /* Ophir Optronics - SPCAM 620U */
2219 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2220 | USB_DEVICE_ID_MATCH_INT_INFO,
2221 .idVendor = 0x0bd3,
2222 .idProduct = 0x0555,
2223 .bInterfaceClass = USB_CLASS_VIDEO,
2224 .bInterfaceSubClass = 1,
2225 .bInterfaceProtocol = 0,
2226 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2227 /* MT6227 */
2228 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2229 | USB_DEVICE_ID_MATCH_INT_INFO,
2230 .idVendor = 0x0e8d,
2231 .idProduct = 0x0004,
2232 .bInterfaceClass = USB_CLASS_VIDEO,
2233 .bInterfaceSubClass = 1,
2234 .bInterfaceProtocol = 0,
bab6f66c
LP
2235 .driver_info = UVC_QUIRK_PROBE_MINMAX
2236 | UVC_QUIRK_PROBE_DEF },
9275b32b
LP
2237 /* IMC Networks (Medion Akoya) */
2238 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2239 | USB_DEVICE_ID_MATCH_INT_INFO,
2240 .idVendor = 0x13d3,
2241 .idProduct = 0x5103,
2242 .bInterfaceClass = USB_CLASS_VIDEO,
2243 .bInterfaceSubClass = 1,
2244 .bInterfaceProtocol = 0,
2245 .driver_info = UVC_QUIRK_STREAM_NO_FID },
d1787b1f
LP
2246 /* JMicron USB2.0 XGA WebCam */
2247 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2248 | USB_DEVICE_ID_MATCH_INT_INFO,
2249 .idVendor = 0x152d,
2250 .idProduct = 0x0310,
2251 .bInterfaceClass = USB_CLASS_VIDEO,
2252 .bInterfaceSubClass = 1,
2253 .bInterfaceProtocol = 0,
2254 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2255 /* Syntek (HP Spartan) */
2256 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2257 | USB_DEVICE_ID_MATCH_INT_INFO,
2258 .idVendor = 0x174f,
2259 .idProduct = 0x5212,
2260 .bInterfaceClass = USB_CLASS_VIDEO,
2261 .bInterfaceSubClass = 1,
2262 .bInterfaceProtocol = 0,
25e69850 2263 .driver_info = UVC_QUIRK_STREAM_NO_FID },
562f0fed
LP
2264 /* Syntek (Samsung Q310) */
2265 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2266 | USB_DEVICE_ID_MATCH_INT_INFO,
2267 .idVendor = 0x174f,
2268 .idProduct = 0x5931,
2269 .bInterfaceClass = USB_CLASS_VIDEO,
2270 .bInterfaceSubClass = 1,
2271 .bInterfaceProtocol = 0,
2272 .driver_info = UVC_QUIRK_STREAM_NO_FID },
f129b03b
LP
2273 /* Syntek (Packard Bell EasyNote MX52 */
2274 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2275 | USB_DEVICE_ID_MATCH_INT_INFO,
2276 .idVendor = 0x174f,
2277 .idProduct = 0x8a12,
2278 .bInterfaceClass = USB_CLASS_VIDEO,
2279 .bInterfaceSubClass = 1,
2280 .bInterfaceProtocol = 0,
2281 .driver_info = UVC_QUIRK_STREAM_NO_FID },
f61d1d8a 2282 /* Syntek (Asus F9SG) */
25e69850
LP
2283 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2284 | USB_DEVICE_ID_MATCH_INT_INFO,
2285 .idVendor = 0x174f,
2286 .idProduct = 0x8a31,
2287 .bInterfaceClass = USB_CLASS_VIDEO,
2288 .bInterfaceSubClass = 1,
2289 .bInterfaceProtocol = 0,
c0efd232
LP
2290 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2291 /* Syntek (Asus U3S) */
2292 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2293 | USB_DEVICE_ID_MATCH_INT_INFO,
2294 .idVendor = 0x174f,
2295 .idProduct = 0x8a33,
2296 .bInterfaceClass = USB_CLASS_VIDEO,
2297 .bInterfaceSubClass = 1,
2298 .bInterfaceProtocol = 0,
2299 .driver_info = UVC_QUIRK_STREAM_NO_FID },
0ce566da
LP
2300 /* Syntek (JAOtech Smart Terminal) */
2301 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2302 | USB_DEVICE_ID_MATCH_INT_INFO,
2303 .idVendor = 0x174f,
2304 .idProduct = 0x8a34,
2305 .bInterfaceClass = USB_CLASS_VIDEO,
2306 .bInterfaceSubClass = 1,
2307 .bInterfaceProtocol = 0,
2308 .driver_info = UVC_QUIRK_STREAM_NO_FID },
70092c26
LP
2309 /* Miricle 307K */
2310 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2311 | USB_DEVICE_ID_MATCH_INT_INFO,
2312 .idVendor = 0x17dc,
2313 .idProduct = 0x0202,
2314 .bInterfaceClass = USB_CLASS_VIDEO,
2315 .bInterfaceSubClass = 1,
2316 .bInterfaceProtocol = 0,
2317 .driver_info = UVC_QUIRK_STREAM_NO_FID },
849a3aba 2318 /* Lenovo Thinkpad SL400/SL500 */
2f38483b
LP
2319 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2320 | USB_DEVICE_ID_MATCH_INT_INFO,
2321 .idVendor = 0x17ef,
2322 .idProduct = 0x480b,
2323 .bInterfaceClass = USB_CLASS_VIDEO,
2324 .bInterfaceSubClass = 1,
2325 .bInterfaceProtocol = 0,
2326 .driver_info = UVC_QUIRK_STREAM_NO_FID },
2d2bf2a3
LP
2327 /* Aveo Technology USB 2.0 Camera */
2328 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2329 | USB_DEVICE_ID_MATCH_INT_INFO,
2330 .idVendor = 0x1871,
2331 .idProduct = 0x0306,
2332 .bInterfaceClass = USB_CLASS_VIDEO,
2333 .bInterfaceSubClass = 1,
2334 .bInterfaceProtocol = 0,
5bdf1377
LP
2335 .driver_info = UVC_QUIRK_PROBE_MINMAX
2336 | UVC_QUIRK_PROBE_EXTRAFIELDS },
c0efd232
LP
2337 /* Ecamm Pico iMage */
2338 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2339 | USB_DEVICE_ID_MATCH_INT_INFO,
2340 .idVendor = 0x18cd,
2341 .idProduct = 0xcafe,
2342 .bInterfaceClass = USB_CLASS_VIDEO,
2343 .bInterfaceSubClass = 1,
2344 .bInterfaceProtocol = 0,
2345 .driver_info = UVC_QUIRK_PROBE_EXTRAFIELDS },
2bb00fe6
LP
2346 /* Manta MM-353 Plako */
2347 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2348 | USB_DEVICE_ID_MATCH_INT_INFO,
2349 .idVendor = 0x18ec,
2350 .idProduct = 0x3188,
2351 .bInterfaceClass = USB_CLASS_VIDEO,
2352 .bInterfaceSubClass = 1,
2353 .bInterfaceProtocol = 0,
2354 .driver_info = UVC_QUIRK_PROBE_MINMAX },
ca4a3456
LP
2355 /* FSC WebCam V30S */
2356 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2357 | USB_DEVICE_ID_MATCH_INT_INFO,
2358 .idVendor = 0x18ec,
2359 .idProduct = 0x3288,
2360 .bInterfaceClass = USB_CLASS_VIDEO,
2361 .bInterfaceSubClass = 1,
2362 .bInterfaceProtocol = 0,
2363 .driver_info = UVC_QUIRK_PROBE_MINMAX },
1e4d05bc
LP
2364 /* Arkmicro unbranded */
2365 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2366 | USB_DEVICE_ID_MATCH_INT_INFO,
2367 .idVendor = 0x18ec,
2368 .idProduct = 0x3290,
2369 .bInterfaceClass = USB_CLASS_VIDEO,
2370 .bInterfaceSubClass = 1,
2371 .bInterfaceProtocol = 0,
2372 .driver_info = UVC_QUIRK_PROBE_DEF },
fd3e9f2f
AC
2373 /* The Imaging Source USB CCD cameras */
2374 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2375 | USB_DEVICE_ID_MATCH_INT_INFO,
2376 .idVendor = 0x199e,
2377 .idProduct = 0x8102,
2378 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
2379 .bInterfaceSubClass = 1,
2380 .bInterfaceProtocol = 0 },
c0efd232
LP
2381 /* Bodelin ProScopeHR */
2382 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2383 | USB_DEVICE_ID_MATCH_DEV_HI
2384 | USB_DEVICE_ID_MATCH_INT_INFO,
2385 .idVendor = 0x19ab,
2386 .idProduct = 0x1000,
2387 .bcdDevice_hi = 0x0126,
2388 .bInterfaceClass = USB_CLASS_VIDEO,
2389 .bInterfaceSubClass = 1,
2390 .bInterfaceProtocol = 0,
2391 .driver_info = UVC_QUIRK_STATUS_INTERVAL },
3bc766ad
LP
2392 /* MSI StarCam 370i */
2393 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2394 | USB_DEVICE_ID_MATCH_INT_INFO,
2395 .idVendor = 0x1b3b,
2396 .idProduct = 0x2951,
2397 .bInterfaceClass = USB_CLASS_VIDEO,
2398 .bInterfaceSubClass = 1,
2399 .bInterfaceProtocol = 0,
2400 .driver_info = UVC_QUIRK_PROBE_MINMAX },
c0efd232
LP
2401 /* SiGma Micro USB Web Camera */
2402 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE
2403 | USB_DEVICE_ID_MATCH_INT_INFO,
2404 .idVendor = 0x1c4f,
2405 .idProduct = 0x3000,
2406 .bInterfaceClass = USB_CLASS_VIDEO,
2407 .bInterfaceSubClass = 1,
2408 .bInterfaceProtocol = 0,
2409 .driver_info = UVC_QUIRK_PROBE_MINMAX
d732c44c 2410 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
c0efd232
LP
2411 /* Generic USB Video Class */
2412 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2413 {}
2414};
2415
2416MODULE_DEVICE_TABLE(usb, uvc_ids);
2417
2418struct uvc_driver uvc_driver = {
2419 .driver = {
2420 .name = "uvcvideo",
2421 .probe = uvc_probe,
2422 .disconnect = uvc_disconnect,
2423 .suspend = uvc_suspend,
2424 .resume = uvc_resume,
9b0ae867 2425 .reset_resume = uvc_reset_resume,
c0efd232
LP
2426 .id_table = uvc_ids,
2427 .supports_autosuspend = 1,
2428 },
2429};
2430
2431static int __init uvc_init(void)
2432{
edbaa398 2433 int ret;
c0efd232 2434
edbaa398
AF
2435 uvc_debugfs_init();
2436
2437 ret = usb_register(&uvc_driver.driver);
2438 if (ret < 0) {
2439 uvc_debugfs_cleanup();
2440 return ret;
2441 }
2442
2443 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2444 return 0;
c0efd232
LP
2445}
2446
2447static void __exit uvc_cleanup(void)
2448{
2449 usb_deregister(&uvc_driver.driver);
edbaa398 2450 uvc_debugfs_cleanup();
c0efd232
LP
2451}
2452
2453module_init(uvc_init);
2454module_exit(uvc_cleanup);
2455
c0efd232
LP
2456MODULE_AUTHOR(DRIVER_AUTHOR);
2457MODULE_DESCRIPTION(DRIVER_DESC);
2458MODULE_LICENSE("GPL");
2459MODULE_VERSION(DRIVER_VERSION);
f87086e3 2460