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