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