]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/media/video/uvc/uvc_driver.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-3.10.git] / drivers / media / video / uvc / uvc_driver.c
1 /*
2  *      uvc_driver.c  --  USB Video Class driver
3  *
4  *      Copyright (C) 2005-2009
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
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 /*
15  * This driver aims to support video input and ouput devices compliant with the
16  * 'USB Video Class' specification.
17  *
18  * The driver doesn't support the deprecated v4l1 interface. It implements the
19  * mmap capture method only, and doesn't do any image format conversion in
20  * software. If your user-space application doesn't support YUYV or MJPEG, fix
21  * it :-). Please note that the MJPEG data have been stripped from their
22  * Huffman tables (DHT marker), you will need to add it back if your JPEG
23  * codec can't handle MJPEG data.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
35 #include <asm/unaligned.h>
36
37 #include <media/v4l2-common.h>
38
39 #include "uvcvideo.h"
40
41 #define DRIVER_AUTHOR           "Laurent Pinchart <laurent.pinchart@skynet.be>"
42 #define DRIVER_DESC             "USB Video Class driver"
43 #ifndef DRIVER_VERSION
44 #define DRIVER_VERSION          "v0.1.0"
45 #endif
46
47 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
48 unsigned int uvc_no_drop_param;
49 static unsigned int uvc_quirks_param = -1;
50 unsigned int uvc_trace_param;
51 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
52
53 /* ------------------------------------------------------------------------
54  * Video formats
55  */
56
57 static struct uvc_format_desc uvc_fmts[] = {
58         {
59                 .name           = "YUV 4:2:2 (YUYV)",
60                 .guid           = UVC_GUID_FORMAT_YUY2,
61                 .fcc            = V4L2_PIX_FMT_YUYV,
62         },
63         {
64                 .name           = "YUV 4:2:2 (YUYV)",
65                 .guid           = UVC_GUID_FORMAT_YUY2_ISIGHT,
66                 .fcc            = V4L2_PIX_FMT_YUYV,
67         },
68         {
69                 .name           = "YUV 4:2:0 (NV12)",
70                 .guid           = UVC_GUID_FORMAT_NV12,
71                 .fcc            = V4L2_PIX_FMT_NV12,
72         },
73         {
74                 .name           = "MJPEG",
75                 .guid           = UVC_GUID_FORMAT_MJPEG,
76                 .fcc            = V4L2_PIX_FMT_MJPEG,
77         },
78         {
79                 .name           = "YVU 4:2:0 (YV12)",
80                 .guid           = UVC_GUID_FORMAT_YV12,
81                 .fcc            = V4L2_PIX_FMT_YVU420,
82         },
83         {
84                 .name           = "YUV 4:2:0 (I420)",
85                 .guid           = UVC_GUID_FORMAT_I420,
86                 .fcc            = V4L2_PIX_FMT_YUV420,
87         },
88         {
89                 .name           = "YUV 4:2:2 (UYVY)",
90                 .guid           = UVC_GUID_FORMAT_UYVY,
91                 .fcc            = V4L2_PIX_FMT_UYVY,
92         },
93         {
94                 .name           = "Greyscale",
95                 .guid           = UVC_GUID_FORMAT_Y800,
96                 .fcc            = V4L2_PIX_FMT_GREY,
97         },
98         {
99                 .name           = "RGB Bayer",
100                 .guid           = UVC_GUID_FORMAT_BY8,
101                 .fcc            = V4L2_PIX_FMT_SBGGR8,
102         },
103 };
104
105 /* ------------------------------------------------------------------------
106  * Utility functions
107  */
108
109 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
110                 __u8 epaddr)
111 {
112         struct usb_host_endpoint *ep;
113         unsigned int i;
114
115         for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
116                 ep = &alts->endpoint[i];
117                 if (ep->desc.bEndpointAddress == epaddr)
118                         return ep;
119         }
120
121         return NULL;
122 }
123
124 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
125 {
126         unsigned int len = ARRAY_SIZE(uvc_fmts);
127         unsigned int i;
128
129         for (i = 0; i < len; ++i) {
130                 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
131                         return &uvc_fmts[i];
132         }
133
134         return NULL;
135 }
136
137 static __u32 uvc_colorspace(const __u8 primaries)
138 {
139         static const __u8 colorprimaries[] = {
140                 0,
141                 V4L2_COLORSPACE_SRGB,
142                 V4L2_COLORSPACE_470_SYSTEM_M,
143                 V4L2_COLORSPACE_470_SYSTEM_BG,
144                 V4L2_COLORSPACE_SMPTE170M,
145                 V4L2_COLORSPACE_SMPTE240M,
146         };
147
148         if (primaries < ARRAY_SIZE(colorprimaries))
149                 return colorprimaries[primaries];
150
151         return 0;
152 }
153
154 /* Simplify a fraction using a simple continued fraction decomposition. The
155  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
156  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
157  * arbitrary parameters to remove non-significative terms from the simple
158  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
159  * respectively seems to give nice results.
160  */
161 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
162                 unsigned int n_terms, unsigned int threshold)
163 {
164         uint32_t *an;
165         uint32_t x, y, r;
166         unsigned int i, n;
167
168         an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
169         if (an == NULL)
170                 return;
171
172         /* Convert the fraction to a simple continued fraction. See
173          * http://mathforum.org/dr.math/faq/faq.fractions.html
174          * Stop if the current term is bigger than or equal to the given
175          * threshold.
176          */
177         x = *numerator;
178         y = *denominator;
179
180         for (n = 0; n < n_terms && y != 0; ++n) {
181                 an[n] = x / y;
182                 if (an[n] >= threshold) {
183                         if (n < 2)
184                                 n++;
185                         break;
186                 }
187
188                 r = x - an[n] * y;
189                 x = y;
190                 y = r;
191         }
192
193         /* Expand the simple continued fraction back to an integer fraction. */
194         x = 0;
195         y = 1;
196
197         for (i = n; i > 0; --i) {
198                 r = y;
199                 y = an[i-1] * y + x;
200                 x = r;
201         }
202
203         *numerator = y;
204         *denominator = x;
205         kfree(an);
206 }
207
208 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
209  * to compute numerator / denominator * 10000000 using 32 bit fixed point
210  * arithmetic only.
211  */
212 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
213 {
214         uint32_t multiplier;
215
216         /* Saturate the result if the operation would overflow. */
217         if (denominator == 0 ||
218             numerator/denominator >= ((uint32_t)-1)/10000000)
219                 return (uint32_t)-1;
220
221         /* Divide both the denominator and the multiplier by two until
222          * numerator * multiplier doesn't overflow. If anyone knows a better
223          * algorithm please let me know.
224          */
225         multiplier = 10000000;
226         while (numerator > ((uint32_t)-1)/multiplier) {
227                 multiplier /= 2;
228                 denominator /= 2;
229         }
230
231         return denominator ? numerator * multiplier / denominator : 0;
232 }
233
234 /* ------------------------------------------------------------------------
235  * Terminal and unit management
236  */
237
238 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
239 {
240         struct uvc_entity *entity;
241
242         list_for_each_entry(entity, &dev->entities, list) {
243                 if (entity->id == id)
244                         return entity;
245         }
246
247         return NULL;
248 }
249
250 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
251         int id, struct uvc_entity *entity)
252 {
253         unsigned int i;
254
255         if (entity == NULL)
256                 entity = list_entry(&dev->entities, struct uvc_entity, list);
257
258         list_for_each_entry_continue(entity, &dev->entities, list) {
259                 for (i = 0; i < entity->bNrInPins; ++i)
260                         if (entity->baSourceID[i] == id)
261                                 return entity;
262         }
263
264         return NULL;
265 }
266
267 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
268 {
269         struct uvc_streaming *stream;
270
271         list_for_each_entry(stream, &dev->streams, list) {
272                 if (stream->header.bTerminalLink == id)
273                         return stream;
274         }
275
276         return NULL;
277 }
278
279 /* ------------------------------------------------------------------------
280  * Descriptors parsing
281  */
282
283 static int uvc_parse_format(struct uvc_device *dev,
284         struct uvc_streaming *streaming, struct uvc_format *format,
285         __u32 **intervals, unsigned char *buffer, int buflen)
286 {
287         struct usb_interface *intf = streaming->intf;
288         struct usb_host_interface *alts = intf->cur_altsetting;
289         struct uvc_format_desc *fmtdesc;
290         struct uvc_frame *frame;
291         const unsigned char *start = buffer;
292         unsigned int interval;
293         unsigned int i, n;
294         __u8 ftype;
295
296         format->type = buffer[2];
297         format->index = buffer[3];
298
299         switch (buffer[2]) {
300         case UVC_VS_FORMAT_UNCOMPRESSED:
301         case UVC_VS_FORMAT_FRAME_BASED:
302                 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
303                 if (buflen < n) {
304                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
305                                "interface %d FORMAT error\n",
306                                dev->udev->devnum,
307                                alts->desc.bInterfaceNumber);
308                         return -EINVAL;
309                 }
310
311                 /* Find the format descriptor from its GUID. */
312                 fmtdesc = uvc_format_by_guid(&buffer[5]);
313
314                 if (fmtdesc != NULL) {
315                         strlcpy(format->name, fmtdesc->name,
316                                 sizeof format->name);
317                         format->fcc = fmtdesc->fcc;
318                 } else {
319                         uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
320                                 &buffer[5]);
321                         snprintf(format->name, sizeof(format->name), "%pUl\n",
322                                 &buffer[5]);
323                         format->fcc = 0;
324                 }
325
326                 format->bpp = buffer[21];
327                 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
328                         ftype = UVC_VS_FRAME_UNCOMPRESSED;
329                 } else {
330                         ftype = UVC_VS_FRAME_FRAME_BASED;
331                         if (buffer[27])
332                                 format->flags = UVC_FMT_FLAG_COMPRESSED;
333                 }
334                 break;
335
336         case UVC_VS_FORMAT_MJPEG:
337                 if (buflen < 11) {
338                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
339                                "interface %d FORMAT error\n",
340                                dev->udev->devnum,
341                                alts->desc.bInterfaceNumber);
342                         return -EINVAL;
343                 }
344
345                 strlcpy(format->name, "MJPEG", sizeof format->name);
346                 format->fcc = V4L2_PIX_FMT_MJPEG;
347                 format->flags = UVC_FMT_FLAG_COMPRESSED;
348                 format->bpp = 0;
349                 ftype = UVC_VS_FRAME_MJPEG;
350                 break;
351
352         case UVC_VS_FORMAT_DV:
353                 if (buflen < 9) {
354                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
355                                "interface %d FORMAT error\n",
356                                dev->udev->devnum,
357                                alts->desc.bInterfaceNumber);
358                         return -EINVAL;
359                 }
360
361                 switch (buffer[8] & 0x7f) {
362                 case 0:
363                         strlcpy(format->name, "SD-DV", sizeof format->name);
364                         break;
365                 case 1:
366                         strlcpy(format->name, "SDL-DV", sizeof format->name);
367                         break;
368                 case 2:
369                         strlcpy(format->name, "HD-DV", sizeof format->name);
370                         break;
371                 default:
372                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
373                                "interface %d: unknown DV format %u\n",
374                                dev->udev->devnum,
375                                alts->desc.bInterfaceNumber, buffer[8]);
376                         return -EINVAL;
377                 }
378
379                 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
380                         sizeof format->name);
381
382                 format->fcc = V4L2_PIX_FMT_DV;
383                 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
384                 format->bpp = 0;
385                 ftype = 0;
386
387                 /* Create a dummy frame descriptor. */
388                 frame = &format->frame[0];
389                 memset(&format->frame[0], 0, sizeof format->frame[0]);
390                 frame->bFrameIntervalType = 1;
391                 frame->dwDefaultFrameInterval = 1;
392                 frame->dwFrameInterval = *intervals;
393                 *(*intervals)++ = 1;
394                 format->nframes = 1;
395                 break;
396
397         case UVC_VS_FORMAT_MPEG2TS:
398         case UVC_VS_FORMAT_STREAM_BASED:
399                 /* Not supported yet. */
400         default:
401                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
402                        "interface %d unsupported format %u\n",
403                        dev->udev->devnum, alts->desc.bInterfaceNumber,
404                        buffer[2]);
405                 return -EINVAL;
406         }
407
408         uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
409
410         buflen -= buffer[0];
411         buffer += buffer[0];
412
413         /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
414          * based formats have frame descriptors.
415          */
416         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
417                buffer[2] == ftype) {
418                 frame = &format->frame[format->nframes];
419                 if (ftype != UVC_VS_FRAME_FRAME_BASED)
420                         n = buflen > 25 ? buffer[25] : 0;
421                 else
422                         n = buflen > 21 ? buffer[21] : 0;
423
424                 n = n ? n : 3;
425
426                 if (buflen < 26 + 4*n) {
427                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
428                                "interface %d FRAME error\n", dev->udev->devnum,
429                                alts->desc.bInterfaceNumber);
430                         return -EINVAL;
431                 }
432
433                 frame->bFrameIndex = buffer[3];
434                 frame->bmCapabilities = buffer[4];
435                 frame->wWidth = get_unaligned_le16(&buffer[5]);
436                 frame->wHeight = get_unaligned_le16(&buffer[7]);
437                 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
438                 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
439                 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
440                         frame->dwMaxVideoFrameBufferSize =
441                                 get_unaligned_le32(&buffer[17]);
442                         frame->dwDefaultFrameInterval =
443                                 get_unaligned_le32(&buffer[21]);
444                         frame->bFrameIntervalType = buffer[25];
445                 } else {
446                         frame->dwMaxVideoFrameBufferSize = 0;
447                         frame->dwDefaultFrameInterval =
448                                 get_unaligned_le32(&buffer[17]);
449                         frame->bFrameIntervalType = buffer[21];
450                 }
451                 frame->dwFrameInterval = *intervals;
452
453                 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454                  * completely. Observed behaviours range from setting the
455                  * value to 1.1x the actual frame size to hardwiring the
456                  * 16 low bits to 0. This results in a higher than necessary
457                  * memory usage as well as a wrong image size information. For
458                  * uncompressed formats this can be fixed by computing the
459                  * value from the frame size.
460                  */
461                 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
462                         frame->dwMaxVideoFrameBufferSize = format->bpp
463                                 * frame->wWidth * frame->wHeight / 8;
464
465                 /* Some bogus devices report dwMinFrameInterval equal to
466                  * dwMaxFrameInterval and have dwFrameIntervalStep set to
467                  * zero. Setting all null intervals to 1 fixes the problem and
468                  * some other divisions by zero that could happen.
469                  */
470                 for (i = 0; i < n; ++i) {
471                         interval = get_unaligned_le32(&buffer[26+4*i]);
472                         *(*intervals)++ = interval ? interval : 1;
473                 }
474
475                 /* Make sure that the default frame interval stays between
476                  * the boundaries.
477                  */
478                 n -= frame->bFrameIntervalType ? 1 : 2;
479                 frame->dwDefaultFrameInterval =
480                         min(frame->dwFrameInterval[n],
481                             max(frame->dwFrameInterval[0],
482                                 frame->dwDefaultFrameInterval));
483
484                 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
485                         frame->wWidth, frame->wHeight,
486                         10000000/frame->dwDefaultFrameInterval,
487                         (100000000/frame->dwDefaultFrameInterval)%10);
488
489                 format->nframes++;
490                 buflen -= buffer[0];
491                 buffer += buffer[0];
492         }
493
494         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
495             buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
496                 buflen -= buffer[0];
497                 buffer += buffer[0];
498         }
499
500         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
501             buffer[2] == UVC_VS_COLORFORMAT) {
502                 if (buflen < 6) {
503                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
504                                "interface %d COLORFORMAT error\n",
505                                dev->udev->devnum,
506                                alts->desc.bInterfaceNumber);
507                         return -EINVAL;
508                 }
509
510                 format->colorspace = uvc_colorspace(buffer[3]);
511
512                 buflen -= buffer[0];
513                 buffer += buffer[0];
514         }
515
516         return buffer - start;
517 }
518
519 static int uvc_parse_streaming(struct uvc_device *dev,
520         struct usb_interface *intf)
521 {
522         struct uvc_streaming *streaming = NULL;
523         struct uvc_format *format;
524         struct uvc_frame *frame;
525         struct usb_host_interface *alts = &intf->altsetting[0];
526         unsigned char *_buffer, *buffer = alts->extra;
527         int _buflen, buflen = alts->extralen;
528         unsigned int nformats = 0, nframes = 0, nintervals = 0;
529         unsigned int size, i, n, p;
530         __u32 *interval;
531         __u16 psize;
532         int ret = -EINVAL;
533
534         if (intf->cur_altsetting->desc.bInterfaceSubClass
535                 != UVC_SC_VIDEOSTREAMING) {
536                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
537                         "video streaming interface\n", dev->udev->devnum,
538                         intf->altsetting[0].desc.bInterfaceNumber);
539                 return -EINVAL;
540         }
541
542         if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
543                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
544                         "claimed\n", dev->udev->devnum,
545                         intf->altsetting[0].desc.bInterfaceNumber);
546                 return -EINVAL;
547         }
548
549         streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
550         if (streaming == NULL) {
551                 usb_driver_release_interface(&uvc_driver.driver, intf);
552                 return -EINVAL;
553         }
554
555         mutex_init(&streaming->mutex);
556         streaming->dev = dev;
557         streaming->intf = usb_get_intf(intf);
558         streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
559
560         /* The Pico iMage webcam has its class-specific interface descriptors
561          * after the endpoint descriptors.
562          */
563         if (buflen == 0) {
564                 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
565                         struct usb_host_endpoint *ep = &alts->endpoint[i];
566
567                         if (ep->extralen == 0)
568                                 continue;
569
570                         if (ep->extralen > 2 &&
571                             ep->extra[1] == USB_DT_CS_INTERFACE) {
572                                 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
573                                         "from endpoint %u.\n", i);
574                                 buffer = alts->endpoint[i].extra;
575                                 buflen = alts->endpoint[i].extralen;
576                                 break;
577                         }
578                 }
579         }
580
581         /* Skip the standard interface descriptors. */
582         while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
583                 buflen -= buffer[0];
584                 buffer += buffer[0];
585         }
586
587         if (buflen <= 2) {
588                 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
589                         "interface descriptors found.\n");
590                 goto error;
591         }
592
593         /* Parse the header descriptor. */
594         switch (buffer[2]) {
595         case UVC_VS_OUTPUT_HEADER:
596                 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
597                 size = 9;
598                 break;
599
600         case UVC_VS_INPUT_HEADER:
601                 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602                 size = 13;
603                 break;
604
605         default:
606                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
607                         "%d HEADER descriptor not found.\n", dev->udev->devnum,
608                         alts->desc.bInterfaceNumber);
609                 goto error;
610         }
611
612         p = buflen >= 4 ? buffer[3] : 0;
613         n = buflen >= size ? buffer[size-1] : 0;
614
615         if (buflen < size + p*n) {
616                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
617                         "interface %d HEADER descriptor is invalid.\n",
618                         dev->udev->devnum, alts->desc.bInterfaceNumber);
619                 goto error;
620         }
621
622         streaming->header.bNumFormats = p;
623         streaming->header.bEndpointAddress = buffer[6];
624         if (buffer[2] == UVC_VS_INPUT_HEADER) {
625                 streaming->header.bmInfo = buffer[7];
626                 streaming->header.bTerminalLink = buffer[8];
627                 streaming->header.bStillCaptureMethod = buffer[9];
628                 streaming->header.bTriggerSupport = buffer[10];
629                 streaming->header.bTriggerUsage = buffer[11];
630         } else {
631                 streaming->header.bTerminalLink = buffer[7];
632         }
633         streaming->header.bControlSize = n;
634
635         streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
636         if (streaming->header.bmaControls == NULL) {
637                 ret = -ENOMEM;
638                 goto error;
639         }
640
641         memcpy(streaming->header.bmaControls, &buffer[size], p*n);
642
643         buflen -= buffer[0];
644         buffer += buffer[0];
645
646         _buffer = buffer;
647         _buflen = buflen;
648
649         /* Count the format and frame descriptors. */
650         while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
651                 switch (_buffer[2]) {
652                 case UVC_VS_FORMAT_UNCOMPRESSED:
653                 case UVC_VS_FORMAT_MJPEG:
654                 case UVC_VS_FORMAT_FRAME_BASED:
655                         nformats++;
656                         break;
657
658                 case UVC_VS_FORMAT_DV:
659                         /* DV format has no frame descriptor. We will create a
660                          * dummy frame descriptor with a dummy frame interval.
661                          */
662                         nformats++;
663                         nframes++;
664                         nintervals++;
665                         break;
666
667                 case UVC_VS_FORMAT_MPEG2TS:
668                 case UVC_VS_FORMAT_STREAM_BASED:
669                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
670                                 "interface %d FORMAT %u is not supported.\n",
671                                 dev->udev->devnum,
672                                 alts->desc.bInterfaceNumber, _buffer[2]);
673                         break;
674
675                 case UVC_VS_FRAME_UNCOMPRESSED:
676                 case UVC_VS_FRAME_MJPEG:
677                         nframes++;
678                         if (_buflen > 25)
679                                 nintervals += _buffer[25] ? _buffer[25] : 3;
680                         break;
681
682                 case UVC_VS_FRAME_FRAME_BASED:
683                         nframes++;
684                         if (_buflen > 21)
685                                 nintervals += _buffer[21] ? _buffer[21] : 3;
686                         break;
687                 }
688
689                 _buflen -= _buffer[0];
690                 _buffer += _buffer[0];
691         }
692
693         if (nformats == 0) {
694                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
695                         "%d has no supported formats defined.\n",
696                         dev->udev->devnum, alts->desc.bInterfaceNumber);
697                 goto error;
698         }
699
700         size = nformats * sizeof *format + nframes * sizeof *frame
701              + nintervals * sizeof *interval;
702         format = kzalloc(size, GFP_KERNEL);
703         if (format == NULL) {
704                 ret = -ENOMEM;
705                 goto error;
706         }
707
708         frame = (struct uvc_frame *)&format[nformats];
709         interval = (__u32 *)&frame[nframes];
710
711         streaming->format = format;
712         streaming->nformats = nformats;
713
714         /* Parse the format descriptors. */
715         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
716                 switch (buffer[2]) {
717                 case UVC_VS_FORMAT_UNCOMPRESSED:
718                 case UVC_VS_FORMAT_MJPEG:
719                 case UVC_VS_FORMAT_DV:
720                 case UVC_VS_FORMAT_FRAME_BASED:
721                         format->frame = frame;
722                         ret = uvc_parse_format(dev, streaming, format,
723                                 &interval, buffer, buflen);
724                         if (ret < 0)
725                                 goto error;
726
727                         frame += format->nframes;
728                         format++;
729
730                         buflen -= ret;
731                         buffer += ret;
732                         continue;
733
734                 default:
735                         break;
736                 }
737
738                 buflen -= buffer[0];
739                 buffer += buffer[0];
740         }
741
742         if (buflen)
743                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
744                         "%d has %u bytes of trailing descriptor garbage.\n",
745                         dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
746
747         /* Parse the alternate settings to find the maximum bandwidth. */
748         for (i = 0; i < intf->num_altsetting; ++i) {
749                 struct usb_host_endpoint *ep;
750                 alts = &intf->altsetting[i];
751                 ep = uvc_find_endpoint(alts,
752                                 streaming->header.bEndpointAddress);
753                 if (ep == NULL)
754                         continue;
755
756                 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
757                 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
758                 if (psize > streaming->maxpsize)
759                         streaming->maxpsize = psize;
760         }
761
762         list_add_tail(&streaming->list, &dev->streams);
763         return 0;
764
765 error:
766         usb_driver_release_interface(&uvc_driver.driver, intf);
767         usb_put_intf(intf);
768         kfree(streaming->format);
769         kfree(streaming->header.bmaControls);
770         kfree(streaming);
771         return ret;
772 }
773
774 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
775                 unsigned int num_pads, unsigned int extra_size)
776 {
777         struct uvc_entity *entity;
778         unsigned int num_inputs;
779         unsigned int size;
780
781         num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
782         size = sizeof(*entity) + extra_size + num_inputs;
783         entity = kzalloc(size, GFP_KERNEL);
784         if (entity == NULL)
785                 return NULL;
786
787         entity->id = id;
788         entity->type = type;
789
790         entity->bNrInPins = num_inputs;
791         entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
792
793         return entity;
794 }
795
796 /* Parse vendor-specific extensions. */
797 static int uvc_parse_vendor_control(struct uvc_device *dev,
798         const unsigned char *buffer, int buflen)
799 {
800         struct usb_device *udev = dev->udev;
801         struct usb_host_interface *alts = dev->intf->cur_altsetting;
802         struct uvc_entity *unit;
803         unsigned int n, p;
804         int handled = 0;
805
806         switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
807         case 0x046d:            /* Logitech */
808                 if (buffer[1] != 0x41 || buffer[2] != 0x01)
809                         break;
810
811                 /* Logitech implements several vendor specific functions
812                  * through vendor specific extension units (LXU).
813                  *
814                  * The LXU descriptors are similar to XU descriptors
815                  * (see "USB Device Video Class for Video Devices", section
816                  * 3.7.2.6 "Extension Unit Descriptor") with the following
817                  * differences:
818                  *
819                  * ----------------------------------------------------------
820                  * 0            bLength         1        Number
821                  *      Size of this descriptor, in bytes: 24+p+n*2
822                  * ----------------------------------------------------------
823                  * 23+p+n       bmControlsType  N       Bitmap
824                  *      Individual bits in the set are defined:
825                  *      0: Absolute
826                  *      1: Relative
827                  *
828                  *      This bitset is mapped exactly the same as bmControls.
829                  * ----------------------------------------------------------
830                  * 23+p+n*2     bReserved       1       Boolean
831                  * ----------------------------------------------------------
832                  * 24+p+n*2     iExtension      1       Index
833                  *      Index of a string descriptor that describes this
834                  *      extension unit.
835                  * ----------------------------------------------------------
836                  */
837                 p = buflen >= 22 ? buffer[21] : 0;
838                 n = buflen >= 25 + p ? buffer[22+p] : 0;
839
840                 if (buflen < 25 + p + 2*n) {
841                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
842                                 "interface %d EXTENSION_UNIT error\n",
843                                 udev->devnum, alts->desc.bInterfaceNumber);
844                         break;
845                 }
846
847                 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
848                                         p + 1, 2*n);
849                 if (unit == NULL)
850                         return -ENOMEM;
851
852                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
853                 unit->extension.bNumControls = buffer[20];
854                 memcpy(unit->baSourceID, &buffer[22], p);
855                 unit->extension.bControlSize = buffer[22+p];
856                 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
857                 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
858                                                + n;
859                 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
860
861                 if (buffer[24+p+2*n] != 0)
862                         usb_string(udev, buffer[24+p+2*n], unit->name,
863                                    sizeof unit->name);
864                 else
865                         sprintf(unit->name, "Extension %u", buffer[3]);
866
867                 list_add_tail(&unit->list, &dev->entities);
868                 handled = 1;
869                 break;
870         }
871
872         return handled;
873 }
874
875 static int uvc_parse_standard_control(struct uvc_device *dev,
876         const unsigned char *buffer, int buflen)
877 {
878         struct usb_device *udev = dev->udev;
879         struct uvc_entity *unit, *term;
880         struct usb_interface *intf;
881         struct usb_host_interface *alts = dev->intf->cur_altsetting;
882         unsigned int i, n, p, len;
883         __u16 type;
884
885         switch (buffer[2]) {
886         case UVC_VC_HEADER:
887                 n = buflen >= 12 ? buffer[11] : 0;
888
889                 if (buflen < 12 || buflen < 12 + n) {
890                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
891                                 "interface %d HEADER error\n", udev->devnum,
892                                 alts->desc.bInterfaceNumber);
893                         return -EINVAL;
894                 }
895
896                 dev->uvc_version = get_unaligned_le16(&buffer[3]);
897                 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
898
899                 /* Parse all USB Video Streaming interfaces. */
900                 for (i = 0; i < n; ++i) {
901                         intf = usb_ifnum_to_if(udev, buffer[12+i]);
902                         if (intf == NULL) {
903                                 uvc_trace(UVC_TRACE_DESCR, "device %d "
904                                         "interface %d doesn't exists\n",
905                                         udev->devnum, i);
906                                 continue;
907                         }
908
909                         uvc_parse_streaming(dev, intf);
910                 }
911                 break;
912
913         case UVC_VC_INPUT_TERMINAL:
914                 if (buflen < 8) {
915                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
916                                 "interface %d INPUT_TERMINAL error\n",
917                                 udev->devnum, alts->desc.bInterfaceNumber);
918                         return -EINVAL;
919                 }
920
921                 /* Make sure the terminal type MSB is not null, otherwise it
922                  * could be confused with a unit.
923                  */
924                 type = get_unaligned_le16(&buffer[4]);
925                 if ((type & 0xff00) == 0) {
926                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
927                                 "interface %d INPUT_TERMINAL %d has invalid "
928                                 "type 0x%04x, skipping\n", udev->devnum,
929                                 alts->desc.bInterfaceNumber,
930                                 buffer[3], type);
931                         return 0;
932                 }
933
934                 n = 0;
935                 p = 0;
936                 len = 8;
937
938                 if (type == UVC_ITT_CAMERA) {
939                         n = buflen >= 15 ? buffer[14] : 0;
940                         len = 15;
941
942                 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
943                         n = buflen >= 9 ? buffer[8] : 0;
944                         p = buflen >= 10 + n ? buffer[9+n] : 0;
945                         len = 10;
946                 }
947
948                 if (buflen < len + n + p) {
949                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
950                                 "interface %d INPUT_TERMINAL error\n",
951                                 udev->devnum, alts->desc.bInterfaceNumber);
952                         return -EINVAL;
953                 }
954
955                 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
956                                         1, n + p);
957                 if (term == NULL)
958                         return -ENOMEM;
959
960                 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
961                         term->camera.bControlSize = n;
962                         term->camera.bmControls = (__u8 *)term + sizeof *term;
963                         term->camera.wObjectiveFocalLengthMin =
964                                 get_unaligned_le16(&buffer[8]);
965                         term->camera.wObjectiveFocalLengthMax =
966                                 get_unaligned_le16(&buffer[10]);
967                         term->camera.wOcularFocalLength =
968                                 get_unaligned_le16(&buffer[12]);
969                         memcpy(term->camera.bmControls, &buffer[15], n);
970                 } else if (UVC_ENTITY_TYPE(term) ==
971                            UVC_ITT_MEDIA_TRANSPORT_INPUT) {
972                         term->media.bControlSize = n;
973                         term->media.bmControls = (__u8 *)term + sizeof *term;
974                         term->media.bTransportModeSize = p;
975                         term->media.bmTransportModes = (__u8 *)term
976                                                      + sizeof *term + n;
977                         memcpy(term->media.bmControls, &buffer[9], n);
978                         memcpy(term->media.bmTransportModes, &buffer[10+n], p);
979                 }
980
981                 if (buffer[7] != 0)
982                         usb_string(udev, buffer[7], term->name,
983                                    sizeof term->name);
984                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
985                         sprintf(term->name, "Camera %u", buffer[3]);
986                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
987                         sprintf(term->name, "Media %u", buffer[3]);
988                 else
989                         sprintf(term->name, "Input %u", buffer[3]);
990
991                 list_add_tail(&term->list, &dev->entities);
992                 break;
993
994         case UVC_VC_OUTPUT_TERMINAL:
995                 if (buflen < 9) {
996                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
997                                 "interface %d OUTPUT_TERMINAL error\n",
998                                 udev->devnum, alts->desc.bInterfaceNumber);
999                         return -EINVAL;
1000                 }
1001
1002                 /* Make sure the terminal type MSB is not null, otherwise it
1003                  * could be confused with a unit.
1004                  */
1005                 type = get_unaligned_le16(&buffer[4]);
1006                 if ((type & 0xff00) == 0) {
1007                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1008                                 "interface %d OUTPUT_TERMINAL %d has invalid "
1009                                 "type 0x%04x, skipping\n", udev->devnum,
1010                                 alts->desc.bInterfaceNumber, buffer[3], type);
1011                         return 0;
1012                 }
1013
1014                 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1015                                         1, 0);
1016                 if (term == NULL)
1017                         return -ENOMEM;
1018
1019                 memcpy(term->baSourceID, &buffer[7], 1);
1020
1021                 if (buffer[8] != 0)
1022                         usb_string(udev, buffer[8], term->name,
1023                                    sizeof term->name);
1024                 else
1025                         sprintf(term->name, "Output %u", buffer[3]);
1026
1027                 list_add_tail(&term->list, &dev->entities);
1028                 break;
1029
1030         case UVC_VC_SELECTOR_UNIT:
1031                 p = buflen >= 5 ? buffer[4] : 0;
1032
1033                 if (buflen < 5 || buflen < 6 + p) {
1034                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1035                                 "interface %d SELECTOR_UNIT error\n",
1036                                 udev->devnum, alts->desc.bInterfaceNumber);
1037                         return -EINVAL;
1038                 }
1039
1040                 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1041                 if (unit == NULL)
1042                         return -ENOMEM;
1043
1044                 memcpy(unit->baSourceID, &buffer[5], p);
1045
1046                 if (buffer[5+p] != 0)
1047                         usb_string(udev, buffer[5+p], unit->name,
1048                                    sizeof unit->name);
1049                 else
1050                         sprintf(unit->name, "Selector %u", buffer[3]);
1051
1052                 list_add_tail(&unit->list, &dev->entities);
1053                 break;
1054
1055         case UVC_VC_PROCESSING_UNIT:
1056                 n = buflen >= 8 ? buffer[7] : 0;
1057                 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1058
1059                 if (buflen < p + n) {
1060                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1061                                 "interface %d PROCESSING_UNIT error\n",
1062                                 udev->devnum, alts->desc.bInterfaceNumber);
1063                         return -EINVAL;
1064                 }
1065
1066                 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1067                 if (unit == NULL)
1068                         return -ENOMEM;
1069
1070                 memcpy(unit->baSourceID, &buffer[4], 1);
1071                 unit->processing.wMaxMultiplier =
1072                         get_unaligned_le16(&buffer[5]);
1073                 unit->processing.bControlSize = buffer[7];
1074                 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1075                 memcpy(unit->processing.bmControls, &buffer[8], n);
1076                 if (dev->uvc_version >= 0x0110)
1077                         unit->processing.bmVideoStandards = buffer[9+n];
1078
1079                 if (buffer[8+n] != 0)
1080                         usb_string(udev, buffer[8+n], unit->name,
1081                                    sizeof unit->name);
1082                 else
1083                         sprintf(unit->name, "Processing %u", buffer[3]);
1084
1085                 list_add_tail(&unit->list, &dev->entities);
1086                 break;
1087
1088         case UVC_VC_EXTENSION_UNIT:
1089                 p = buflen >= 22 ? buffer[21] : 0;
1090                 n = buflen >= 24 + p ? buffer[22+p] : 0;
1091
1092                 if (buflen < 24 + p + n) {
1093                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1094                                 "interface %d EXTENSION_UNIT error\n",
1095                                 udev->devnum, alts->desc.bInterfaceNumber);
1096                         return -EINVAL;
1097                 }
1098
1099                 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1100                 if (unit == NULL)
1101                         return -ENOMEM;
1102
1103                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1104                 unit->extension.bNumControls = buffer[20];
1105                 memcpy(unit->baSourceID, &buffer[22], p);
1106                 unit->extension.bControlSize = buffer[22+p];
1107                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1108                 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1109
1110                 if (buffer[23+p+n] != 0)
1111                         usb_string(udev, buffer[23+p+n], unit->name,
1112                                    sizeof unit->name);
1113                 else
1114                         sprintf(unit->name, "Extension %u", buffer[3]);
1115
1116                 list_add_tail(&unit->list, &dev->entities);
1117                 break;
1118
1119         default:
1120                 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1121                         "descriptor (%u)\n", buffer[2]);
1122                 break;
1123         }
1124
1125         return 0;
1126 }
1127
1128 static int uvc_parse_control(struct uvc_device *dev)
1129 {
1130         struct usb_host_interface *alts = dev->intf->cur_altsetting;
1131         unsigned char *buffer = alts->extra;
1132         int buflen = alts->extralen;
1133         int ret;
1134
1135         /* Parse the default alternate setting only, as the UVC specification
1136          * defines a single alternate setting, the default alternate setting
1137          * zero.
1138          */
1139
1140         while (buflen > 2) {
1141                 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1142                     buffer[1] != USB_DT_CS_INTERFACE)
1143                         goto next_descriptor;
1144
1145                 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1146                         return ret;
1147
1148 next_descriptor:
1149                 buflen -= buffer[0];
1150                 buffer += buffer[0];
1151         }
1152
1153         /* Check if the optional status endpoint is present. Built-in iSight
1154          * webcams have an interrupt endpoint but spit proprietary data that
1155          * don't conform to the UVC status endpoint messages. Don't try to
1156          * handle the interrupt endpoint for those cameras.
1157          */
1158         if (alts->desc.bNumEndpoints == 1 &&
1159             !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1160                 struct usb_host_endpoint *ep = &alts->endpoint[0];
1161                 struct usb_endpoint_descriptor *desc = &ep->desc;
1162
1163                 if (usb_endpoint_is_int_in(desc) &&
1164                     le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1165                     desc->bInterval != 0) {
1166                         uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1167                                 "(addr %02x).\n", desc->bEndpointAddress);
1168                         dev->int_ep = ep;
1169                 }
1170         }
1171
1172         return 0;
1173 }
1174
1175 /* ------------------------------------------------------------------------
1176  * UVC device scan
1177  */
1178
1179 /*
1180  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1181  * and containing the following units:
1182  *
1183  * - one or more Output Terminals (USB Streaming or Display)
1184  * - zero or one Processing Unit
1185  * - zero, one or more single-input Selector Units
1186  * - zero or one multiple-input Selector Units, provided all inputs are
1187  *   connected to input terminals
1188  * - zero, one or mode single-input Extension Units
1189  * - one or more Input Terminals (Camera, External or USB Streaming)
1190  *
1191  * The terminal and units must match on of the following structures:
1192  *
1193  * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
1194  * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
1195  * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
1196  *
1197  *                 +---------+    +---------+ -> OTT_*(0)
1198  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
1199  *                 +---------+    +---------+ -> OTT_*(n)
1200  *
1201  * The Processing Unit and Extension Units can be in any order. Additional
1202  * Extension Units connected to the main chain as single-unit branches are
1203  * also supported. Single-input Selector Units are ignored.
1204  */
1205 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1206         struct uvc_entity *entity)
1207 {
1208         switch (UVC_ENTITY_TYPE(entity)) {
1209         case UVC_VC_EXTENSION_UNIT:
1210                 if (uvc_trace_param & UVC_TRACE_PROBE)
1211                         printk(" <- XU %d", entity->id);
1212
1213                 if (entity->bNrInPins != 1) {
1214                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1215                                 "than 1 input pin.\n", entity->id);
1216                         return -1;
1217                 }
1218
1219                 break;
1220
1221         case UVC_VC_PROCESSING_UNIT:
1222                 if (uvc_trace_param & UVC_TRACE_PROBE)
1223                         printk(" <- PU %d", entity->id);
1224
1225                 if (chain->processing != NULL) {
1226                         uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1227                                 "Processing Units in chain.\n");
1228                         return -1;
1229                 }
1230
1231                 chain->processing = entity;
1232                 break;
1233
1234         case UVC_VC_SELECTOR_UNIT:
1235                 if (uvc_trace_param & UVC_TRACE_PROBE)
1236                         printk(" <- SU %d", entity->id);
1237
1238                 /* Single-input selector units are ignored. */
1239                 if (entity->bNrInPins == 1)
1240                         break;
1241
1242                 if (chain->selector != NULL) {
1243                         uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1244                                 "Units in chain.\n");
1245                         return -1;
1246                 }
1247
1248                 chain->selector = entity;
1249                 break;
1250
1251         case UVC_ITT_VENDOR_SPECIFIC:
1252         case UVC_ITT_CAMERA:
1253         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1254                 if (uvc_trace_param & UVC_TRACE_PROBE)
1255                         printk(" <- IT %d\n", entity->id);
1256
1257                 break;
1258
1259         case UVC_TT_STREAMING:
1260                 if (UVC_ENTITY_IS_ITERM(entity)) {
1261                         if (uvc_trace_param & UVC_TRACE_PROBE)
1262                                 printk(" <- IT %d\n", entity->id);
1263                 } else {
1264                         if (uvc_trace_param & UVC_TRACE_PROBE)
1265                                 printk(" OT %d", entity->id);
1266                 }
1267
1268                 break;
1269
1270         default:
1271                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1272                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1273                 return -1;
1274         }
1275
1276         list_add_tail(&entity->chain, &chain->entities);
1277         return 0;
1278 }
1279
1280 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1281         struct uvc_entity *entity, struct uvc_entity *prev)
1282 {
1283         struct uvc_entity *forward;
1284         int found;
1285
1286         /* Forward scan */
1287         forward = NULL;
1288         found = 0;
1289
1290         while (1) {
1291                 forward = uvc_entity_by_reference(chain->dev, entity->id,
1292                         forward);
1293                 if (forward == NULL)
1294                         break;
1295                 if (forward == prev)
1296                         continue;
1297
1298                 switch (UVC_ENTITY_TYPE(forward)) {
1299                 case UVC_VC_EXTENSION_UNIT:
1300                         if (forward->bNrInPins != 1) {
1301                                 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1302                                           "has more than 1 input pin.\n",
1303                                           entity->id);
1304                                 return -EINVAL;
1305                         }
1306
1307                         list_add_tail(&forward->chain, &chain->entities);
1308                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1309                                 if (!found)
1310                                         printk(" (->");
1311
1312                                 printk(" XU %d", forward->id);
1313                                 found = 1;
1314                         }
1315                         break;
1316
1317                 case UVC_OTT_VENDOR_SPECIFIC:
1318                 case UVC_OTT_DISPLAY:
1319                 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1320                 case UVC_TT_STREAMING:
1321                         if (UVC_ENTITY_IS_ITERM(forward)) {
1322                                 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1323                                         "terminal %u.\n", forward->id);
1324                                 return -EINVAL;
1325                         }
1326
1327                         list_add_tail(&forward->chain, &chain->entities);
1328                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1329                                 if (!found)
1330                                         printk(" (->");
1331
1332                                 printk(" OT %d", forward->id);
1333                                 found = 1;
1334                         }
1335                         break;
1336                 }
1337         }
1338         if (found)
1339                 printk(")");
1340
1341         return 0;
1342 }
1343
1344 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1345         struct uvc_entity **_entity)
1346 {
1347         struct uvc_entity *entity = *_entity;
1348         struct uvc_entity *term;
1349         int id = -EINVAL, i;
1350
1351         switch (UVC_ENTITY_TYPE(entity)) {
1352         case UVC_VC_EXTENSION_UNIT:
1353         case UVC_VC_PROCESSING_UNIT:
1354                 id = entity->baSourceID[0];
1355                 break;
1356
1357         case UVC_VC_SELECTOR_UNIT:
1358                 /* Single-input selector units are ignored. */
1359                 if (entity->bNrInPins == 1) {
1360                         id = entity->baSourceID[0];
1361                         break;
1362                 }
1363
1364                 if (uvc_trace_param & UVC_TRACE_PROBE)
1365                         printk(" <- IT");
1366
1367                 chain->selector = entity;
1368                 for (i = 0; i < entity->bNrInPins; ++i) {
1369                         id = entity->baSourceID[i];
1370                         term = uvc_entity_by_id(chain->dev, id);
1371                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1372                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1373                                         "input %d isn't connected to an "
1374                                         "input terminal\n", entity->id, i);
1375                                 return -1;
1376                         }
1377
1378                         if (uvc_trace_param & UVC_TRACE_PROBE)
1379                                 printk(" %d", term->id);
1380
1381                         list_add_tail(&term->chain, &chain->entities);
1382                         uvc_scan_chain_forward(chain, term, entity);
1383                 }
1384
1385                 if (uvc_trace_param & UVC_TRACE_PROBE)
1386                         printk("\n");
1387
1388                 id = 0;
1389                 break;
1390
1391         case UVC_ITT_VENDOR_SPECIFIC:
1392         case UVC_ITT_CAMERA:
1393         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1394         case UVC_OTT_VENDOR_SPECIFIC:
1395         case UVC_OTT_DISPLAY:
1396         case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1397         case UVC_TT_STREAMING:
1398                 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1399                 break;
1400         }
1401
1402         if (id <= 0) {
1403                 *_entity = NULL;
1404                 return id;
1405         }
1406
1407         entity = uvc_entity_by_id(chain->dev, id);
1408         if (entity == NULL) {
1409                 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1410                         "unknown entity %d.\n", id);
1411                 return -EINVAL;
1412         }
1413
1414         *_entity = entity;
1415         return 0;
1416 }
1417
1418 static int uvc_scan_chain(struct uvc_video_chain *chain,
1419                           struct uvc_entity *term)
1420 {
1421         struct uvc_entity *entity, *prev;
1422
1423         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1424
1425         entity = term;
1426         prev = NULL;
1427
1428         while (entity != NULL) {
1429                 /* Entity must not be part of an existing chain */
1430                 if (entity->chain.next || entity->chain.prev) {
1431                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1432                                 "entity %d already in chain.\n", entity->id);
1433                         return -EINVAL;
1434                 }
1435
1436                 /* Process entity */
1437                 if (uvc_scan_chain_entity(chain, entity) < 0)
1438                         return -EINVAL;
1439
1440                 /* Forward scan */
1441                 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1442                         return -EINVAL;
1443
1444                 /* Backward scan */
1445                 prev = entity;
1446                 if (uvc_scan_chain_backward(chain, &entity) < 0)
1447                         return -EINVAL;
1448         }
1449
1450         return 0;
1451 }
1452
1453 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1454                 char *buffer)
1455 {
1456         struct uvc_entity *term;
1457         unsigned int nterms = 0;
1458         char *p = buffer;
1459
1460         list_for_each_entry(term, terms, chain) {
1461                 if (!UVC_ENTITY_IS_TERM(term) ||
1462                     UVC_TERM_DIRECTION(term) != dir)
1463                         continue;
1464
1465                 if (nterms)
1466                         p += sprintf(p, ",");
1467                 if (++nterms >= 4) {
1468                         p += sprintf(p, "...");
1469                         break;
1470                 }
1471                 p += sprintf(p, "%u", term->id);
1472         }
1473
1474         return p - buffer;
1475 }
1476
1477 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1478 {
1479         static char buffer[43];
1480         char *p = buffer;
1481
1482         p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1483         p += sprintf(p, " -> ");
1484         uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1485
1486         return buffer;
1487 }
1488
1489 /*
1490  * Scan the device for video chains and register video devices.
1491  *
1492  * Chains are scanned starting at their output terminals and walked backwards.
1493  */
1494 static int uvc_scan_device(struct uvc_device *dev)
1495 {
1496         struct uvc_video_chain *chain;
1497         struct uvc_entity *term;
1498
1499         list_for_each_entry(term, &dev->entities, list) {
1500                 if (!UVC_ENTITY_IS_OTERM(term))
1501                         continue;
1502
1503                 /* If the terminal is already included in a chain, skip it.
1504                  * This can happen for chains that have multiple output
1505                  * terminals, where all output terminals beside the first one
1506                  * will be inserted in the chain in forward scans.
1507                  */
1508                 if (term->chain.next || term->chain.prev)
1509                         continue;
1510
1511                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1512                 if (chain == NULL)
1513                         return -ENOMEM;
1514
1515                 INIT_LIST_HEAD(&chain->entities);
1516                 mutex_init(&chain->ctrl_mutex);
1517                 chain->dev = dev;
1518
1519                 if (uvc_scan_chain(chain, term) < 0) {
1520                         kfree(chain);
1521                         continue;
1522                 }
1523
1524                 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1525                           uvc_print_chain(chain));
1526
1527                 list_add_tail(&chain->list, &dev->chains);
1528         }
1529
1530         if (list_empty(&dev->chains)) {
1531                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1532                 return -1;
1533         }
1534
1535         return 0;
1536 }
1537
1538 /* ------------------------------------------------------------------------
1539  * Video device registration and unregistration
1540  */
1541
1542 /*
1543  * Delete the UVC device.
1544  *
1545  * Called by the kernel when the last reference to the uvc_device structure
1546  * is released.
1547  *
1548  * As this function is called after or during disconnect(), all URBs have
1549  * already been canceled by the USB core. There is no need to kill the
1550  * interrupt URB manually.
1551  */
1552 static void uvc_delete(struct uvc_device *dev)
1553 {
1554         struct list_head *p, *n;
1555
1556         usb_put_intf(dev->intf);
1557         usb_put_dev(dev->udev);
1558
1559         uvc_status_cleanup(dev);
1560         uvc_ctrl_cleanup_device(dev);
1561
1562         list_for_each_safe(p, n, &dev->chains) {
1563                 struct uvc_video_chain *chain;
1564                 chain = list_entry(p, struct uvc_video_chain, list);
1565                 kfree(chain);
1566         }
1567
1568         list_for_each_safe(p, n, &dev->entities) {
1569                 struct uvc_entity *entity;
1570                 entity = list_entry(p, struct uvc_entity, list);
1571                 kfree(entity);
1572         }
1573
1574         list_for_each_safe(p, n, &dev->streams) {
1575                 struct uvc_streaming *streaming;
1576                 streaming = list_entry(p, struct uvc_streaming, list);
1577                 usb_driver_release_interface(&uvc_driver.driver,
1578                         streaming->intf);
1579                 usb_put_intf(streaming->intf);
1580                 kfree(streaming->format);
1581                 kfree(streaming->header.bmaControls);
1582                 kfree(streaming);
1583         }
1584
1585         kfree(dev);
1586 }
1587
1588 static void uvc_release(struct video_device *vdev)
1589 {
1590         struct uvc_streaming *stream = video_get_drvdata(vdev);
1591         struct uvc_device *dev = stream->dev;
1592
1593         video_device_release(vdev);
1594
1595         /* Decrement the registered streams count and delete the device when it
1596          * reaches zero.
1597          */
1598         if (atomic_dec_and_test(&dev->nstreams))
1599                 uvc_delete(dev);
1600 }
1601
1602 /*
1603  * Unregister the video devices.
1604  */
1605 static void uvc_unregister_video(struct uvc_device *dev)
1606 {
1607         struct uvc_streaming *stream;
1608
1609         /* Unregistering all video devices might result in uvc_delete() being
1610          * called from inside the loop if there's no open file handle. To avoid
1611          * that, increment the stream count before iterating over the streams
1612          * and decrement it when done.
1613          */
1614         atomic_inc(&dev->nstreams);
1615
1616         list_for_each_entry(stream, &dev->streams, list) {
1617                 if (stream->vdev == NULL)
1618                         continue;
1619
1620                 video_unregister_device(stream->vdev);
1621                 stream->vdev = NULL;
1622         }
1623
1624         /* Decrement the stream count and call uvc_delete explicitly if there
1625          * are no stream left.
1626          */
1627         if (atomic_dec_and_test(&dev->nstreams))
1628                 uvc_delete(dev);
1629 }
1630
1631 static int uvc_register_video(struct uvc_device *dev,
1632                 struct uvc_streaming *stream)
1633 {
1634         struct video_device *vdev;
1635         int ret;
1636
1637         /* Initialize the streaming interface with default streaming
1638          * parameters.
1639          */
1640         ret = uvc_video_init(stream);
1641         if (ret < 0) {
1642                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1643                         "(%d).\n", ret);
1644                 return ret;
1645         }
1646
1647         /* Register the device with V4L. */
1648         vdev = video_device_alloc();
1649         if (vdev == NULL) {
1650                 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1651                            ret);
1652                 return -ENOMEM;
1653         }
1654
1655         /* We already hold a reference to dev->udev. The video device will be
1656          * unregistered before the reference is released, so we don't need to
1657          * get another one.
1658          */
1659         vdev->parent = &dev->intf->dev;
1660         vdev->fops = &uvc_fops;
1661         vdev->release = uvc_release;
1662         strlcpy(vdev->name, dev->name, sizeof vdev->name);
1663
1664         /* Set the driver data before calling video_register_device, otherwise
1665          * uvc_v4l2_open might race us.
1666          */
1667         stream->vdev = vdev;
1668         video_set_drvdata(vdev, stream);
1669
1670         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1671         if (ret < 0) {
1672                 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1673                            ret);
1674                 stream->vdev = NULL;
1675                 video_device_release(vdev);
1676                 return ret;
1677         }
1678
1679         atomic_inc(&dev->nstreams);
1680         return 0;
1681 }
1682
1683 /*
1684  * Register all video devices in all chains.
1685  */
1686 static int uvc_register_terms(struct uvc_device *dev,
1687         struct uvc_video_chain *chain)
1688 {
1689         struct uvc_streaming *stream;
1690         struct uvc_entity *term;
1691         int ret;
1692
1693         list_for_each_entry(term, &chain->entities, chain) {
1694                 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1695                         continue;
1696
1697                 stream = uvc_stream_by_id(dev, term->id);
1698                 if (stream == NULL) {
1699                         uvc_printk(KERN_INFO, "No streaming interface found "
1700                                    "for terminal %u.", term->id);
1701                         continue;
1702                 }
1703
1704                 stream->chain = chain;
1705                 ret = uvc_register_video(dev, stream);
1706                 if (ret < 0)
1707                         return ret;
1708         }
1709
1710         return 0;
1711 }
1712
1713 static int uvc_register_chains(struct uvc_device *dev)
1714 {
1715         struct uvc_video_chain *chain;
1716         int ret;
1717
1718         list_for_each_entry(chain, &dev->chains, list) {
1719                 ret = uvc_register_terms(dev, chain);
1720                 if (ret < 0)
1721                         return ret;
1722         }
1723
1724         return 0;
1725 }
1726
1727 /* ------------------------------------------------------------------------
1728  * USB probe, disconnect, suspend and resume
1729  */
1730
1731 static int uvc_probe(struct usb_interface *intf,
1732                      const struct usb_device_id *id)
1733 {
1734         struct usb_device *udev = interface_to_usbdev(intf);
1735         struct uvc_device *dev;
1736         int ret;
1737
1738         if (id->idVendor && id->idProduct)
1739                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1740                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1741                                 id->idProduct);
1742         else
1743                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1744                                 udev->devpath);
1745
1746         /* Allocate memory for the device and initialize it. */
1747         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1748                 return -ENOMEM;
1749
1750         INIT_LIST_HEAD(&dev->entities);
1751         INIT_LIST_HEAD(&dev->chains);
1752         INIT_LIST_HEAD(&dev->streams);
1753         atomic_set(&dev->nstreams, 0);
1754         atomic_set(&dev->users, 0);
1755
1756         dev->udev = usb_get_dev(udev);
1757         dev->intf = usb_get_intf(intf);
1758         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1759         dev->quirks = (uvc_quirks_param == -1)
1760                     ? id->driver_info : uvc_quirks_param;
1761
1762         if (udev->product != NULL)
1763                 strlcpy(dev->name, udev->product, sizeof dev->name);
1764         else
1765                 snprintf(dev->name, sizeof dev->name,
1766                         "UVC Camera (%04x:%04x)",
1767                         le16_to_cpu(udev->descriptor.idVendor),
1768                         le16_to_cpu(udev->descriptor.idProduct));
1769
1770         /* Parse the Video Class control descriptor. */
1771         if (uvc_parse_control(dev) < 0) {
1772                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1773                         "descriptors.\n");
1774                 goto error;
1775         }
1776
1777         uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1778                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1779                 udev->product ? udev->product : "<unnamed>",
1780                 le16_to_cpu(udev->descriptor.idVendor),
1781                 le16_to_cpu(udev->descriptor.idProduct));
1782
1783         if (dev->quirks != id->driver_info) {
1784                 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1785                         "parameter for testing purpose.\n", dev->quirks);
1786                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1787                         "linux-uvc-devel mailing list.\n");
1788         }
1789
1790         /* Initialize controls. */
1791         if (uvc_ctrl_init_device(dev) < 0)
1792                 goto error;
1793
1794         /* Scan the device for video chains. */
1795         if (uvc_scan_device(dev) < 0)
1796                 goto error;
1797
1798         /* Register video devices. */
1799         if (uvc_register_chains(dev) < 0)
1800                 goto error;
1801
1802         /* Save our data pointer in the interface data. */
1803         usb_set_intfdata(intf, dev);
1804
1805         /* Initialize the interrupt URB. */
1806         if ((ret = uvc_status_init(dev)) < 0) {
1807                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1808                         "endpoint (%d), status interrupt will not be "
1809                         "supported.\n", ret);
1810         }
1811
1812         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1813         return 0;
1814
1815 error:
1816         uvc_unregister_video(dev);
1817         return -ENODEV;
1818 }
1819
1820 static void uvc_disconnect(struct usb_interface *intf)
1821 {
1822         struct uvc_device *dev = usb_get_intfdata(intf);
1823
1824         /* Set the USB interface data to NULL. This can be done outside the
1825          * lock, as there's no other reader.
1826          */
1827         usb_set_intfdata(intf, NULL);
1828
1829         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1830             UVC_SC_VIDEOSTREAMING)
1831                 return;
1832
1833         dev->state |= UVC_DEV_DISCONNECTED;
1834
1835         uvc_unregister_video(dev);
1836 }
1837
1838 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1839 {
1840         struct uvc_device *dev = usb_get_intfdata(intf);
1841         struct uvc_streaming *stream;
1842
1843         uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1844                 intf->cur_altsetting->desc.bInterfaceNumber);
1845
1846         /* Controls are cached on the fly so they don't need to be saved. */
1847         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1848             UVC_SC_VIDEOCONTROL)
1849                 return uvc_status_suspend(dev);
1850
1851         list_for_each_entry(stream, &dev->streams, list) {
1852                 if (stream->intf == intf)
1853                         return uvc_video_suspend(stream);
1854         }
1855
1856         uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1857                         "mismatch.\n");
1858         return -EINVAL;
1859 }
1860
1861 static int __uvc_resume(struct usb_interface *intf, int reset)
1862 {
1863         struct uvc_device *dev = usb_get_intfdata(intf);
1864         struct uvc_streaming *stream;
1865
1866         uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1867                 intf->cur_altsetting->desc.bInterfaceNumber);
1868
1869         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1870             UVC_SC_VIDEOCONTROL) {
1871                 if (reset) {
1872                         int ret = uvc_ctrl_resume_device(dev);
1873
1874                         if (ret < 0)
1875                                 return ret;
1876                 }
1877
1878                 return uvc_status_resume(dev);
1879         }
1880
1881         list_for_each_entry(stream, &dev->streams, list) {
1882                 if (stream->intf == intf)
1883                         return uvc_video_resume(stream);
1884         }
1885
1886         uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1887                         "mismatch.\n");
1888         return -EINVAL;
1889 }
1890
1891 static int uvc_resume(struct usb_interface *intf)
1892 {
1893         return __uvc_resume(intf, 0);
1894 }
1895
1896 static int uvc_reset_resume(struct usb_interface *intf)
1897 {
1898         return __uvc_resume(intf, 1);
1899 }
1900
1901 /* ------------------------------------------------------------------------
1902  * Module parameters
1903  */
1904
1905 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1906 {
1907         if (uvc_clock_param == CLOCK_MONOTONIC)
1908                 return sprintf(buffer, "CLOCK_MONOTONIC");
1909         else
1910                 return sprintf(buffer, "CLOCK_REALTIME");
1911 }
1912
1913 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1914 {
1915         if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1916                 val += strlen("clock_");
1917
1918         if (strcasecmp(val, "monotonic") == 0)
1919                 uvc_clock_param = CLOCK_MONOTONIC;
1920         else if (strcasecmp(val, "realtime") == 0)
1921                 uvc_clock_param = CLOCK_REALTIME;
1922         else
1923                 return -EINVAL;
1924
1925         return 0;
1926 }
1927
1928 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1929                   &uvc_clock_param, S_IRUGO|S_IWUSR);
1930 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1931 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1932 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1933 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1934 MODULE_PARM_DESC(quirks, "Forced device quirks");
1935 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1936 MODULE_PARM_DESC(trace, "Trace level bitmask");
1937 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1938 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1939
1940 /* ------------------------------------------------------------------------
1941  * Driver initialization and cleanup
1942  */
1943
1944 /*
1945  * The Logitech cameras listed below have their interface class set to
1946  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1947  * though they are compliant.
1948  */
1949 static struct usb_device_id uvc_ids[] = {
1950         /* Genius eFace 2025 */
1951         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1952                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1953           .idVendor             = 0x0458,
1954           .idProduct            = 0x706e,
1955           .bInterfaceClass      = USB_CLASS_VIDEO,
1956           .bInterfaceSubClass   = 1,
1957           .bInterfaceProtocol   = 0,
1958           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1959         /* Microsoft Lifecam NX-6000 */
1960         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1961                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1962           .idVendor             = 0x045e,
1963           .idProduct            = 0x00f8,
1964           .bInterfaceClass      = USB_CLASS_VIDEO,
1965           .bInterfaceSubClass   = 1,
1966           .bInterfaceProtocol   = 0,
1967           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1968         /* Microsoft Lifecam VX-7000 */
1969         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1970                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1971           .idVendor             = 0x045e,
1972           .idProduct            = 0x0723,
1973           .bInterfaceClass      = USB_CLASS_VIDEO,
1974           .bInterfaceSubClass   = 1,
1975           .bInterfaceProtocol   = 0,
1976           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1977         /* Logitech Quickcam Fusion */
1978         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1979                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1980           .idVendor             = 0x046d,
1981           .idProduct            = 0x08c1,
1982           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1983           .bInterfaceSubClass   = 1,
1984           .bInterfaceProtocol   = 0 },
1985         /* Logitech Quickcam Orbit MP */
1986         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1987                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1988           .idVendor             = 0x046d,
1989           .idProduct            = 0x08c2,
1990           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1991           .bInterfaceSubClass   = 1,
1992           .bInterfaceProtocol   = 0 },
1993         /* Logitech Quickcam Pro for Notebook */
1994         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1995                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1996           .idVendor             = 0x046d,
1997           .idProduct            = 0x08c3,
1998           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1999           .bInterfaceSubClass   = 1,
2000           .bInterfaceProtocol   = 0 },
2001         /* Logitech Quickcam Pro 5000 */
2002         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2003                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2004           .idVendor             = 0x046d,
2005           .idProduct            = 0x08c5,
2006           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2007           .bInterfaceSubClass   = 1,
2008           .bInterfaceProtocol   = 0 },
2009         /* Logitech Quickcam OEM Dell Notebook */
2010         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2011                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2012           .idVendor             = 0x046d,
2013           .idProduct            = 0x08c6,
2014           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2015           .bInterfaceSubClass   = 1,
2016           .bInterfaceProtocol   = 0 },
2017         /* Logitech Quickcam OEM Cisco VT Camera II */
2018         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2019                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2020           .idVendor             = 0x046d,
2021           .idProduct            = 0x08c7,
2022           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2023           .bInterfaceSubClass   = 1,
2024           .bInterfaceProtocol   = 0 },
2025         /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2026         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2027                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2028           .idVendor             = 0x058f,
2029           .idProduct            = 0x3820,
2030           .bInterfaceClass      = USB_CLASS_VIDEO,
2031           .bInterfaceSubClass   = 1,
2032           .bInterfaceProtocol   = 0,
2033           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2034         /* Apple Built-In iSight */
2035         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2036                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2037           .idVendor             = 0x05ac,
2038           .idProduct            = 0x8501,
2039           .bInterfaceClass      = USB_CLASS_VIDEO,
2040           .bInterfaceSubClass   = 1,
2041           .bInterfaceProtocol   = 0,
2042           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2043                                 | UVC_QUIRK_BUILTIN_ISIGHT },
2044         /* Genesys Logic USB 2.0 PC Camera */
2045         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2046                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2047           .idVendor             = 0x05e3,
2048           .idProduct            = 0x0505,
2049           .bInterfaceClass      = USB_CLASS_VIDEO,
2050           .bInterfaceSubClass   = 1,
2051           .bInterfaceProtocol   = 0,
2052           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2053         /* ViMicro Vega */
2054         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2055                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2056           .idVendor             = 0x0ac8,
2057           .idProduct            = 0x332d,
2058           .bInterfaceClass      = USB_CLASS_VIDEO,
2059           .bInterfaceSubClass   = 1,
2060           .bInterfaceProtocol   = 0,
2061           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2062         /* ViMicro - Minoru3D */
2063         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2064                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2065           .idVendor             = 0x0ac8,
2066           .idProduct            = 0x3410,
2067           .bInterfaceClass      = USB_CLASS_VIDEO,
2068           .bInterfaceSubClass   = 1,
2069           .bInterfaceProtocol   = 0,
2070           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2071         /* ViMicro Venus - Minoru3D */
2072         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2073                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2074           .idVendor             = 0x0ac8,
2075           .idProduct            = 0x3420,
2076           .bInterfaceClass      = USB_CLASS_VIDEO,
2077           .bInterfaceSubClass   = 1,
2078           .bInterfaceProtocol   = 0,
2079           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2080         /* MT6227 */
2081         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2082                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2083           .idVendor             = 0x0e8d,
2084           .idProduct            = 0x0004,
2085           .bInterfaceClass      = USB_CLASS_VIDEO,
2086           .bInterfaceSubClass   = 1,
2087           .bInterfaceProtocol   = 0,
2088           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2089                                 | UVC_QUIRK_PROBE_DEF },
2090         /* Syntek (HP Spartan) */
2091         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2092                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2093           .idVendor             = 0x174f,
2094           .idProduct            = 0x5212,
2095           .bInterfaceClass      = USB_CLASS_VIDEO,
2096           .bInterfaceSubClass   = 1,
2097           .bInterfaceProtocol   = 0,
2098           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2099         /* Syntek (Samsung Q310) */
2100         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2101                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2102           .idVendor             = 0x174f,
2103           .idProduct            = 0x5931,
2104           .bInterfaceClass      = USB_CLASS_VIDEO,
2105           .bInterfaceSubClass   = 1,
2106           .bInterfaceProtocol   = 0,
2107           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2108         /* Syntek (Asus F9SG) */
2109         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2110                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2111           .idVendor             = 0x174f,
2112           .idProduct            = 0x8a31,
2113           .bInterfaceClass      = USB_CLASS_VIDEO,
2114           .bInterfaceSubClass   = 1,
2115           .bInterfaceProtocol   = 0,
2116           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2117         /* Syntek (Asus U3S) */
2118         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2119                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2120           .idVendor             = 0x174f,
2121           .idProduct            = 0x8a33,
2122           .bInterfaceClass      = USB_CLASS_VIDEO,
2123           .bInterfaceSubClass   = 1,
2124           .bInterfaceProtocol   = 0,
2125           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2126         /* Syntek (JAOtech Smart Terminal) */
2127         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2128                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2129           .idVendor             = 0x174f,
2130           .idProduct            = 0x8a34,
2131           .bInterfaceClass      = USB_CLASS_VIDEO,
2132           .bInterfaceSubClass   = 1,
2133           .bInterfaceProtocol   = 0,
2134           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2135         /* Lenovo Thinkpad SL400/SL500 */
2136         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2137                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2138           .idVendor             = 0x17ef,
2139           .idProduct            = 0x480b,
2140           .bInterfaceClass      = USB_CLASS_VIDEO,
2141           .bInterfaceSubClass   = 1,
2142           .bInterfaceProtocol   = 0,
2143           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2144         /* Aveo Technology USB 2.0 Camera */
2145         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2146                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2147           .idVendor             = 0x1871,
2148           .idProduct            = 0x0306,
2149           .bInterfaceClass      = USB_CLASS_VIDEO,
2150           .bInterfaceSubClass   = 1,
2151           .bInterfaceProtocol   = 0,
2152           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2153                                 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2154         /* Ecamm Pico iMage */
2155         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2156                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2157           .idVendor             = 0x18cd,
2158           .idProduct            = 0xcafe,
2159           .bInterfaceClass      = USB_CLASS_VIDEO,
2160           .bInterfaceSubClass   = 1,
2161           .bInterfaceProtocol   = 0,
2162           .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
2163         /* FSC WebCam V30S */
2164         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2165                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2166           .idVendor             = 0x18ec,
2167           .idProduct            = 0x3288,
2168           .bInterfaceClass      = USB_CLASS_VIDEO,
2169           .bInterfaceSubClass   = 1,
2170           .bInterfaceProtocol   = 0,
2171           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2172         /* Bodelin ProScopeHR */
2173         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2174                                 | USB_DEVICE_ID_MATCH_DEV_HI
2175                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2176           .idVendor             = 0x19ab,
2177           .idProduct            = 0x1000,
2178           .bcdDevice_hi         = 0x0126,
2179           .bInterfaceClass      = USB_CLASS_VIDEO,
2180           .bInterfaceSubClass   = 1,
2181           .bInterfaceProtocol   = 0,
2182           .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
2183         /* MSI StarCam 370i */
2184         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2185                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2186           .idVendor             = 0x1b3b,
2187           .idProduct            = 0x2951,
2188           .bInterfaceClass      = USB_CLASS_VIDEO,
2189           .bInterfaceSubClass   = 1,
2190           .bInterfaceProtocol   = 0,
2191           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2192         /* SiGma Micro USB Web Camera */
2193         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2194                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2195           .idVendor             = 0x1c4f,
2196           .idProduct            = 0x3000,
2197           .bInterfaceClass      = USB_CLASS_VIDEO,
2198           .bInterfaceSubClass   = 1,
2199           .bInterfaceProtocol   = 0,
2200           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2201                                 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2202         /* Generic USB Video Class */
2203         { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2204         {}
2205 };
2206
2207 MODULE_DEVICE_TABLE(usb, uvc_ids);
2208
2209 struct uvc_driver uvc_driver = {
2210         .driver = {
2211                 .name           = "uvcvideo",
2212                 .probe          = uvc_probe,
2213                 .disconnect     = uvc_disconnect,
2214                 .suspend        = uvc_suspend,
2215                 .resume         = uvc_resume,
2216                 .reset_resume   = uvc_reset_resume,
2217                 .id_table       = uvc_ids,
2218                 .supports_autosuspend = 1,
2219         },
2220 };
2221
2222 static int __init uvc_init(void)
2223 {
2224         int result;
2225
2226         INIT_LIST_HEAD(&uvc_driver.devices);
2227         INIT_LIST_HEAD(&uvc_driver.controls);
2228         mutex_init(&uvc_driver.ctrl_mutex);
2229
2230         uvc_ctrl_init();
2231
2232         result = usb_register(&uvc_driver.driver);
2233         if (result == 0)
2234                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2235         return result;
2236 }
2237
2238 static void __exit uvc_cleanup(void)
2239 {
2240         usb_deregister(&uvc_driver.driver);
2241 }
2242
2243 module_init(uvc_init);
2244 module_exit(uvc_cleanup);
2245
2246 MODULE_AUTHOR(DRIVER_AUTHOR);
2247 MODULE_DESCRIPTION(DRIVER_DESC);
2248 MODULE_LICENSE("GPL");
2249 MODULE_VERSION(DRIVER_VERSION);
2250