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