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