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