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