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