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