Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[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_TT_STREAMING:
1268                 if (UVC_ENTITY_IS_ITERM(entity)) {
1269                         if (uvc_trace_param & UVC_TRACE_PROBE)
1270                                 printk(" <- IT %d\n", entity->id);
1271                 } else {
1272                         if (uvc_trace_param & UVC_TRACE_PROBE)
1273                                 printk(" OT %d", entity->id);
1274                 }
1275
1276                 break;
1277
1278         default:
1279                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1280                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1281                 return -1;
1282         }
1283
1284         list_add_tail(&entity->chain, &chain->entities);
1285         return 0;
1286 }
1287
1288 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1289         struct uvc_entity *entity, struct uvc_entity *prev)
1290 {
1291         struct uvc_entity *forward;
1292         int found;
1293
1294         /* Forward scan */
1295         forward = NULL;
1296         found = 0;
1297
1298         while (1) {
1299                 forward = uvc_entity_by_reference(chain->dev, entity->id,
1300                         forward);
1301                 if (forward == NULL)
1302                         break;
1303                 if (forward == prev)
1304                         continue;
1305
1306                 switch (UVC_ENTITY_TYPE(forward)) {
1307                 case UVC_VC_EXTENSION_UNIT:
1308                         if (forward->bNrInPins != 1) {
1309                                 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1310                                           "has more than 1 input pin.\n",
1311                                           entity->id);
1312                                 return -EINVAL;
1313                         }
1314
1315                         list_add_tail(&forward->chain, &chain->entities);
1316                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1317                                 if (!found)
1318                                         printk(" (->");
1319
1320                                 printk(" XU %d", forward->id);
1321                                 found = 1;
1322                         }
1323                         break;
1324
1325                 case UVC_OTT_VENDOR_SPECIFIC:
1326                 case UVC_OTT_DISPLAY:
1327                 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1328                 case UVC_TT_STREAMING:
1329                         if (UVC_ENTITY_IS_ITERM(forward)) {
1330                                 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1331                                         "terminal %u.\n", forward->id);
1332                                 return -EINVAL;
1333                         }
1334
1335                         list_add_tail(&forward->chain, &chain->entities);
1336                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1337                                 if (!found)
1338                                         printk(" (->");
1339
1340                                 printk(" OT %d", forward->id);
1341                                 found = 1;
1342                         }
1343                         break;
1344                 }
1345         }
1346         if (found)
1347                 printk(")");
1348
1349         return 0;
1350 }
1351
1352 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1353         struct uvc_entity **_entity)
1354 {
1355         struct uvc_entity *entity = *_entity;
1356         struct uvc_entity *term;
1357         int id = -EINVAL, i;
1358
1359         switch (UVC_ENTITY_TYPE(entity)) {
1360         case UVC_VC_EXTENSION_UNIT:
1361         case UVC_VC_PROCESSING_UNIT:
1362                 id = entity->baSourceID[0];
1363                 break;
1364
1365         case UVC_VC_SELECTOR_UNIT:
1366                 /* Single-input selector units are ignored. */
1367                 if (entity->bNrInPins == 1) {
1368                         id = entity->baSourceID[0];
1369                         break;
1370                 }
1371
1372                 if (uvc_trace_param & UVC_TRACE_PROBE)
1373                         printk(" <- IT");
1374
1375                 chain->selector = entity;
1376                 for (i = 0; i < entity->bNrInPins; ++i) {
1377                         id = entity->baSourceID[i];
1378                         term = uvc_entity_by_id(chain->dev, id);
1379                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1380                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1381                                         "input %d isn't connected to an "
1382                                         "input terminal\n", entity->id, i);
1383                                 return -1;
1384                         }
1385
1386                         if (uvc_trace_param & UVC_TRACE_PROBE)
1387                                 printk(" %d", term->id);
1388
1389                         list_add_tail(&term->chain, &chain->entities);
1390                         uvc_scan_chain_forward(chain, term, entity);
1391                 }
1392
1393                 if (uvc_trace_param & UVC_TRACE_PROBE)
1394                         printk("\n");
1395
1396                 id = 0;
1397                 break;
1398
1399         case UVC_ITT_VENDOR_SPECIFIC:
1400         case UVC_ITT_CAMERA:
1401         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1402         case UVC_OTT_VENDOR_SPECIFIC:
1403         case UVC_OTT_DISPLAY:
1404         case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1405         case UVC_TT_STREAMING:
1406                 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1407                 break;
1408         }
1409
1410         if (id <= 0) {
1411                 *_entity = NULL;
1412                 return id;
1413         }
1414
1415         entity = uvc_entity_by_id(chain->dev, id);
1416         if (entity == NULL) {
1417                 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1418                         "unknown entity %d.\n", id);
1419                 return -EINVAL;
1420         }
1421
1422         *_entity = entity;
1423         return 0;
1424 }
1425
1426 static int uvc_scan_chain(struct uvc_video_chain *chain,
1427                           struct uvc_entity *term)
1428 {
1429         struct uvc_entity *entity, *prev;
1430
1431         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1432
1433         entity = term;
1434         prev = NULL;
1435
1436         while (entity != NULL) {
1437                 /* Entity must not be part of an existing chain */
1438                 if (entity->chain.next || entity->chain.prev) {
1439                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1440                                 "entity %d already in chain.\n", entity->id);
1441                         return -EINVAL;
1442                 }
1443
1444                 /* Process entity */
1445                 if (uvc_scan_chain_entity(chain, entity) < 0)
1446                         return -EINVAL;
1447
1448                 /* Forward scan */
1449                 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1450                         return -EINVAL;
1451
1452                 /* Backward scan */
1453                 prev = entity;
1454                 if (uvc_scan_chain_backward(chain, &entity) < 0)
1455                         return -EINVAL;
1456         }
1457
1458         return 0;
1459 }
1460
1461 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1462                 char *buffer)
1463 {
1464         struct uvc_entity *term;
1465         unsigned int nterms = 0;
1466         char *p = buffer;
1467
1468         list_for_each_entry(term, terms, chain) {
1469                 if (!UVC_ENTITY_IS_TERM(term) ||
1470                     UVC_TERM_DIRECTION(term) != dir)
1471                         continue;
1472
1473                 if (nterms)
1474                         p += sprintf(p, ",");
1475                 if (++nterms >= 4) {
1476                         p += sprintf(p, "...");
1477                         break;
1478                 }
1479                 p += sprintf(p, "%u", term->id);
1480         }
1481
1482         return p - buffer;
1483 }
1484
1485 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1486 {
1487         static char buffer[43];
1488         char *p = buffer;
1489
1490         p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1491         p += sprintf(p, " -> ");
1492         uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1493
1494         return buffer;
1495 }
1496
1497 /*
1498  * Scan the device for video chains and register video devices.
1499  *
1500  * Chains are scanned starting at their output terminals and walked backwards.
1501  */
1502 static int uvc_scan_device(struct uvc_device *dev)
1503 {
1504         struct uvc_video_chain *chain;
1505         struct uvc_entity *term;
1506
1507         list_for_each_entry(term, &dev->entities, list) {
1508                 if (!UVC_ENTITY_IS_OTERM(term))
1509                         continue;
1510
1511                 /* If the terminal is already included in a chain, skip it.
1512                  * This can happen for chains that have multiple output
1513                  * terminals, where all output terminals beside the first one
1514                  * will be inserted in the chain in forward scans.
1515                  */
1516                 if (term->chain.next || term->chain.prev)
1517                         continue;
1518
1519                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1520                 if (chain == NULL)
1521                         return -ENOMEM;
1522
1523                 INIT_LIST_HEAD(&chain->entities);
1524                 mutex_init(&chain->ctrl_mutex);
1525                 chain->dev = dev;
1526
1527                 if (uvc_scan_chain(chain, term) < 0) {
1528                         kfree(chain);
1529                         continue;
1530                 }
1531
1532                 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1533                           uvc_print_chain(chain));
1534
1535                 list_add_tail(&chain->list, &dev->chains);
1536         }
1537
1538         if (list_empty(&dev->chains)) {
1539                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1540                 return -1;
1541         }
1542
1543         return 0;
1544 }
1545
1546 /* ------------------------------------------------------------------------
1547  * Video device registration and unregistration
1548  */
1549
1550 /*
1551  * Delete the UVC device.
1552  *
1553  * Called by the kernel when the last reference to the uvc_device structure
1554  * is released.
1555  *
1556  * As this function is called after or during disconnect(), all URBs have
1557  * already been canceled by the USB core. There is no need to kill the
1558  * interrupt URB manually.
1559  */
1560 static void uvc_delete(struct uvc_device *dev)
1561 {
1562         struct list_head *p, *n;
1563
1564         usb_put_intf(dev->intf);
1565         usb_put_dev(dev->udev);
1566
1567         uvc_status_cleanup(dev);
1568         uvc_ctrl_cleanup_device(dev);
1569
1570         list_for_each_safe(p, n, &dev->chains) {
1571                 struct uvc_video_chain *chain;
1572                 chain = list_entry(p, struct uvc_video_chain, list);
1573                 kfree(chain);
1574         }
1575
1576         list_for_each_safe(p, n, &dev->entities) {
1577                 struct uvc_entity *entity;
1578                 entity = list_entry(p, struct uvc_entity, list);
1579                 kfree(entity);
1580         }
1581
1582         list_for_each_safe(p, n, &dev->streams) {
1583                 struct uvc_streaming *streaming;
1584                 streaming = list_entry(p, struct uvc_streaming, list);
1585                 usb_driver_release_interface(&uvc_driver.driver,
1586                         streaming->intf);
1587                 usb_put_intf(streaming->intf);
1588                 kfree(streaming->format);
1589                 kfree(streaming->header.bmaControls);
1590                 kfree(streaming);
1591         }
1592
1593         kfree(dev);
1594 }
1595
1596 static void uvc_release(struct video_device *vdev)
1597 {
1598         struct uvc_streaming *stream = video_get_drvdata(vdev);
1599         struct uvc_device *dev = stream->dev;
1600
1601         video_device_release(vdev);
1602
1603         /* Decrement the registered streams count and delete the device when it
1604          * reaches zero.
1605          */
1606         if (atomic_dec_and_test(&dev->nstreams))
1607                 uvc_delete(dev);
1608 }
1609
1610 /*
1611  * Unregister the video devices.
1612  */
1613 static void uvc_unregister_video(struct uvc_device *dev)
1614 {
1615         struct uvc_streaming *stream;
1616
1617         /* Unregistering all video devices might result in uvc_delete() being
1618          * called from inside the loop if there's no open file handle. To avoid
1619          * that, increment the stream count before iterating over the streams
1620          * and decrement it when done.
1621          */
1622         atomic_inc(&dev->nstreams);
1623
1624         list_for_each_entry(stream, &dev->streams, list) {
1625                 if (stream->vdev == NULL)
1626                         continue;
1627
1628                 video_unregister_device(stream->vdev);
1629                 stream->vdev = NULL;
1630         }
1631
1632         /* Decrement the stream count and call uvc_delete explicitly if there
1633          * are no stream left.
1634          */
1635         if (atomic_dec_and_test(&dev->nstreams))
1636                 uvc_delete(dev);
1637 }
1638
1639 static int uvc_register_video(struct uvc_device *dev,
1640                 struct uvc_streaming *stream)
1641 {
1642         struct video_device *vdev;
1643         int ret;
1644
1645         /* Initialize the streaming interface with default streaming
1646          * parameters.
1647          */
1648         ret = uvc_video_init(stream);
1649         if (ret < 0) {
1650                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1651                         "(%d).\n", ret);
1652                 return ret;
1653         }
1654
1655         /* Register the device with V4L. */
1656         vdev = video_device_alloc();
1657         if (vdev == NULL) {
1658                 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1659                            ret);
1660                 return -ENOMEM;
1661         }
1662
1663         /* We already hold a reference to dev->udev. The video device will be
1664          * unregistered before the reference is released, so we don't need to
1665          * get another one.
1666          */
1667         vdev->parent = &dev->intf->dev;
1668         vdev->fops = &uvc_fops;
1669         vdev->release = uvc_release;
1670         strlcpy(vdev->name, dev->name, sizeof vdev->name);
1671
1672         /* Set the driver data before calling video_register_device, otherwise
1673          * uvc_v4l2_open might race us.
1674          */
1675         stream->vdev = vdev;
1676         video_set_drvdata(vdev, stream);
1677
1678         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1679         if (ret < 0) {
1680                 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1681                            ret);
1682                 stream->vdev = NULL;
1683                 video_device_release(vdev);
1684                 return ret;
1685         }
1686
1687         atomic_inc(&dev->nstreams);
1688         return 0;
1689 }
1690
1691 /*
1692  * Register all video devices in all chains.
1693  */
1694 static int uvc_register_terms(struct uvc_device *dev,
1695         struct uvc_video_chain *chain)
1696 {
1697         struct uvc_streaming *stream;
1698         struct uvc_entity *term;
1699         int ret;
1700
1701         list_for_each_entry(term, &chain->entities, chain) {
1702                 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1703                         continue;
1704
1705                 stream = uvc_stream_by_id(dev, term->id);
1706                 if (stream == NULL) {
1707                         uvc_printk(KERN_INFO, "No streaming interface found "
1708                                    "for terminal %u.", term->id);
1709                         continue;
1710                 }
1711
1712                 stream->chain = chain;
1713                 ret = uvc_register_video(dev, stream);
1714                 if (ret < 0)
1715                         return ret;
1716         }
1717
1718         return 0;
1719 }
1720
1721 static int uvc_register_chains(struct uvc_device *dev)
1722 {
1723         struct uvc_video_chain *chain;
1724         int ret;
1725
1726         list_for_each_entry(chain, &dev->chains, list) {
1727                 ret = uvc_register_terms(dev, chain);
1728                 if (ret < 0)
1729                         return ret;
1730         }
1731
1732         return 0;
1733 }
1734
1735 /* ------------------------------------------------------------------------
1736  * USB probe, disconnect, suspend and resume
1737  */
1738
1739 static int uvc_probe(struct usb_interface *intf,
1740                      const struct usb_device_id *id)
1741 {
1742         struct usb_device *udev = interface_to_usbdev(intf);
1743         struct uvc_device *dev;
1744         int ret;
1745
1746         if (id->idVendor && id->idProduct)
1747                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1748                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1749                                 id->idProduct);
1750         else
1751                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1752                                 udev->devpath);
1753
1754         /* Allocate memory for the device and initialize it. */
1755         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1756                 return -ENOMEM;
1757
1758         INIT_LIST_HEAD(&dev->entities);
1759         INIT_LIST_HEAD(&dev->chains);
1760         INIT_LIST_HEAD(&dev->streams);
1761         atomic_set(&dev->nstreams, 0);
1762         atomic_set(&dev->users, 0);
1763         atomic_set(&dev->nmappings, 0);
1764
1765         dev->udev = usb_get_dev(udev);
1766         dev->intf = usb_get_intf(intf);
1767         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1768         dev->quirks = (uvc_quirks_param == -1)
1769                     ? id->driver_info : uvc_quirks_param;
1770
1771         if (udev->product != NULL)
1772                 strlcpy(dev->name, udev->product, sizeof dev->name);
1773         else
1774                 snprintf(dev->name, sizeof dev->name,
1775                         "UVC Camera (%04x:%04x)",
1776                         le16_to_cpu(udev->descriptor.idVendor),
1777                         le16_to_cpu(udev->descriptor.idProduct));
1778
1779         /* Parse the Video Class control descriptor. */
1780         if (uvc_parse_control(dev) < 0) {
1781                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1782                         "descriptors.\n");
1783                 goto error;
1784         }
1785
1786         uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1787                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1788                 udev->product ? udev->product : "<unnamed>",
1789                 le16_to_cpu(udev->descriptor.idVendor),
1790                 le16_to_cpu(udev->descriptor.idProduct));
1791
1792         if (dev->quirks != id->driver_info) {
1793                 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1794                         "parameter for testing purpose.\n", dev->quirks);
1795                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1796                         "linux-uvc-devel mailing list.\n");
1797         }
1798
1799         /* Initialize controls. */
1800         if (uvc_ctrl_init_device(dev) < 0)
1801                 goto error;
1802
1803         /* Scan the device for video chains. */
1804         if (uvc_scan_device(dev) < 0)
1805                 goto error;
1806
1807         /* Register video devices. */
1808         if (uvc_register_chains(dev) < 0)
1809                 goto error;
1810
1811         /* Save our data pointer in the interface data. */
1812         usb_set_intfdata(intf, dev);
1813
1814         /* Initialize the interrupt URB. */
1815         if ((ret = uvc_status_init(dev)) < 0) {
1816                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1817                         "endpoint (%d), status interrupt will not be "
1818                         "supported.\n", ret);
1819         }
1820
1821         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1822         usb_enable_autosuspend(udev);
1823         return 0;
1824
1825 error:
1826         uvc_unregister_video(dev);
1827         return -ENODEV;
1828 }
1829
1830 static void uvc_disconnect(struct usb_interface *intf)
1831 {
1832         struct uvc_device *dev = usb_get_intfdata(intf);
1833
1834         /* Set the USB interface data to NULL. This can be done outside the
1835          * lock, as there's no other reader.
1836          */
1837         usb_set_intfdata(intf, NULL);
1838
1839         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1840             UVC_SC_VIDEOSTREAMING)
1841                 return;
1842
1843         dev->state |= UVC_DEV_DISCONNECTED;
1844
1845         uvc_unregister_video(dev);
1846 }
1847
1848 static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1849 {
1850         struct uvc_device *dev = usb_get_intfdata(intf);
1851         struct uvc_streaming *stream;
1852
1853         uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1854                 intf->cur_altsetting->desc.bInterfaceNumber);
1855
1856         /* Controls are cached on the fly so they don't need to be saved. */
1857         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1858             UVC_SC_VIDEOCONTROL)
1859                 return uvc_status_suspend(dev);
1860
1861         list_for_each_entry(stream, &dev->streams, list) {
1862                 if (stream->intf == intf)
1863                         return uvc_video_suspend(stream);
1864         }
1865
1866         uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1867                         "mismatch.\n");
1868         return -EINVAL;
1869 }
1870
1871 static int __uvc_resume(struct usb_interface *intf, int reset)
1872 {
1873         struct uvc_device *dev = usb_get_intfdata(intf);
1874         struct uvc_streaming *stream;
1875
1876         uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1877                 intf->cur_altsetting->desc.bInterfaceNumber);
1878
1879         if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1880             UVC_SC_VIDEOCONTROL) {
1881                 if (reset) {
1882                         int ret = uvc_ctrl_resume_device(dev);
1883
1884                         if (ret < 0)
1885                                 return ret;
1886                 }
1887
1888                 return uvc_status_resume(dev);
1889         }
1890
1891         list_for_each_entry(stream, &dev->streams, list) {
1892                 if (stream->intf == intf)
1893                         return uvc_video_resume(stream);
1894         }
1895
1896         uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1897                         "mismatch.\n");
1898         return -EINVAL;
1899 }
1900
1901 static int uvc_resume(struct usb_interface *intf)
1902 {
1903         return __uvc_resume(intf, 0);
1904 }
1905
1906 static int uvc_reset_resume(struct usb_interface *intf)
1907 {
1908         return __uvc_resume(intf, 1);
1909 }
1910
1911 /* ------------------------------------------------------------------------
1912  * Module parameters
1913  */
1914
1915 static int uvc_clock_param_get(char *buffer, struct kernel_param *kp)
1916 {
1917         if (uvc_clock_param == CLOCK_MONOTONIC)
1918                 return sprintf(buffer, "CLOCK_MONOTONIC");
1919         else
1920                 return sprintf(buffer, "CLOCK_REALTIME");
1921 }
1922
1923 static int uvc_clock_param_set(const char *val, struct kernel_param *kp)
1924 {
1925         if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
1926                 val += strlen("clock_");
1927
1928         if (strcasecmp(val, "monotonic") == 0)
1929                 uvc_clock_param = CLOCK_MONOTONIC;
1930         else if (strcasecmp(val, "realtime") == 0)
1931                 uvc_clock_param = CLOCK_REALTIME;
1932         else
1933                 return -EINVAL;
1934
1935         return 0;
1936 }
1937
1938 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
1939                   &uvc_clock_param, S_IRUGO|S_IWUSR);
1940 MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
1941 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
1942 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
1943 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
1944 MODULE_PARM_DESC(quirks, "Forced device quirks");
1945 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
1946 MODULE_PARM_DESC(trace, "Trace level bitmask");
1947 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
1948 MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
1949
1950 /* ------------------------------------------------------------------------
1951  * Driver initialization and cleanup
1952  */
1953
1954 /*
1955  * The Logitech cameras listed below have their interface class set to
1956  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1957  * though they are compliant.
1958  */
1959 static struct usb_device_id uvc_ids[] = {
1960         /* Genius eFace 2025 */
1961         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1962                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1963           .idVendor             = 0x0458,
1964           .idProduct            = 0x706e,
1965           .bInterfaceClass      = USB_CLASS_VIDEO,
1966           .bInterfaceSubClass   = 1,
1967           .bInterfaceProtocol   = 0,
1968           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1969         /* Microsoft Lifecam NX-6000 */
1970         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1971                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1972           .idVendor             = 0x045e,
1973           .idProduct            = 0x00f8,
1974           .bInterfaceClass      = USB_CLASS_VIDEO,
1975           .bInterfaceSubClass   = 1,
1976           .bInterfaceProtocol   = 0,
1977           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1978         /* Microsoft Lifecam VX-7000 */
1979         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1980                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1981           .idVendor             = 0x045e,
1982           .idProduct            = 0x0723,
1983           .bInterfaceClass      = USB_CLASS_VIDEO,
1984           .bInterfaceSubClass   = 1,
1985           .bInterfaceProtocol   = 0,
1986           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1987         /* Logitech Quickcam Fusion */
1988         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1989                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1990           .idVendor             = 0x046d,
1991           .idProduct            = 0x08c1,
1992           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1993           .bInterfaceSubClass   = 1,
1994           .bInterfaceProtocol   = 0 },
1995         /* Logitech Quickcam Orbit MP */
1996         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1997                                 | USB_DEVICE_ID_MATCH_INT_INFO,
1998           .idVendor             = 0x046d,
1999           .idProduct            = 0x08c2,
2000           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2001           .bInterfaceSubClass   = 1,
2002           .bInterfaceProtocol   = 0 },
2003         /* Logitech Quickcam Pro for Notebook */
2004         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2005                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2006           .idVendor             = 0x046d,
2007           .idProduct            = 0x08c3,
2008           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2009           .bInterfaceSubClass   = 1,
2010           .bInterfaceProtocol   = 0 },
2011         /* Logitech Quickcam Pro 5000 */
2012         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2013                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2014           .idVendor             = 0x046d,
2015           .idProduct            = 0x08c5,
2016           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2017           .bInterfaceSubClass   = 1,
2018           .bInterfaceProtocol   = 0 },
2019         /* Logitech Quickcam OEM Dell Notebook */
2020         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2021                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2022           .idVendor             = 0x046d,
2023           .idProduct            = 0x08c6,
2024           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2025           .bInterfaceSubClass   = 1,
2026           .bInterfaceProtocol   = 0 },
2027         /* Logitech Quickcam OEM Cisco VT Camera II */
2028         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2029                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2030           .idVendor             = 0x046d,
2031           .idProduct            = 0x08c7,
2032           .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
2033           .bInterfaceSubClass   = 1,
2034           .bInterfaceProtocol   = 0 },
2035         /* Chicony CNF7129 (Asus EEE 100HE) */
2036         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2037                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2038           .idVendor             = 0x04f2,
2039           .idProduct            = 0xb071,
2040           .bInterfaceClass      = USB_CLASS_VIDEO,
2041           .bInterfaceSubClass   = 1,
2042           .bInterfaceProtocol   = 0,
2043           .driver_info          = UVC_QUIRK_RESTRICT_FRAME_RATE },
2044         /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
2045         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2046                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2047           .idVendor             = 0x058f,
2048           .idProduct            = 0x3820,
2049           .bInterfaceClass      = USB_CLASS_VIDEO,
2050           .bInterfaceSubClass   = 1,
2051           .bInterfaceProtocol   = 0,
2052           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2053         /* Apple Built-In iSight */
2054         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2055                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2056           .idVendor             = 0x05ac,
2057           .idProduct            = 0x8501,
2058           .bInterfaceClass      = USB_CLASS_VIDEO,
2059           .bInterfaceSubClass   = 1,
2060           .bInterfaceProtocol   = 0,
2061           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2062                                 | UVC_QUIRK_BUILTIN_ISIGHT },
2063         /* Genesys Logic USB 2.0 PC Camera */
2064         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2065                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2066           .idVendor             = 0x05e3,
2067           .idProduct            = 0x0505,
2068           .bInterfaceClass      = USB_CLASS_VIDEO,
2069           .bInterfaceSubClass   = 1,
2070           .bInterfaceProtocol   = 0,
2071           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2072         /* ViMicro Vega */
2073         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2074                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2075           .idVendor             = 0x0ac8,
2076           .idProduct            = 0x332d,
2077           .bInterfaceClass      = USB_CLASS_VIDEO,
2078           .bInterfaceSubClass   = 1,
2079           .bInterfaceProtocol   = 0,
2080           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2081         /* ViMicro - Minoru3D */
2082         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2083                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2084           .idVendor             = 0x0ac8,
2085           .idProduct            = 0x3410,
2086           .bInterfaceClass      = USB_CLASS_VIDEO,
2087           .bInterfaceSubClass   = 1,
2088           .bInterfaceProtocol   = 0,
2089           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2090         /* ViMicro Venus - Minoru3D */
2091         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2092                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2093           .idVendor             = 0x0ac8,
2094           .idProduct            = 0x3420,
2095           .bInterfaceClass      = USB_CLASS_VIDEO,
2096           .bInterfaceSubClass   = 1,
2097           .bInterfaceProtocol   = 0,
2098           .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2099         /* MT6227 */
2100         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2101                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2102           .idVendor             = 0x0e8d,
2103           .idProduct            = 0x0004,
2104           .bInterfaceClass      = USB_CLASS_VIDEO,
2105           .bInterfaceSubClass   = 1,
2106           .bInterfaceProtocol   = 0,
2107           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2108                                 | UVC_QUIRK_PROBE_DEF },
2109         /* IMC Networks (Medion Akoya) */
2110         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2111                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2112           .idVendor             = 0x13d3,
2113           .idProduct            = 0x5103,
2114           .bInterfaceClass      = USB_CLASS_VIDEO,
2115           .bInterfaceSubClass   = 1,
2116           .bInterfaceProtocol   = 0,
2117           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2118         /* Syntek (HP Spartan) */
2119         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2120                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2121           .idVendor             = 0x174f,
2122           .idProduct            = 0x5212,
2123           .bInterfaceClass      = USB_CLASS_VIDEO,
2124           .bInterfaceSubClass   = 1,
2125           .bInterfaceProtocol   = 0,
2126           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2127         /* Syntek (Samsung Q310) */
2128         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2129                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2130           .idVendor             = 0x174f,
2131           .idProduct            = 0x5931,
2132           .bInterfaceClass      = USB_CLASS_VIDEO,
2133           .bInterfaceSubClass   = 1,
2134           .bInterfaceProtocol   = 0,
2135           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2136         /* Syntek (Packard Bell EasyNote MX52 */
2137         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2138                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2139           .idVendor             = 0x174f,
2140           .idProduct            = 0x8a12,
2141           .bInterfaceClass      = USB_CLASS_VIDEO,
2142           .bInterfaceSubClass   = 1,
2143           .bInterfaceProtocol   = 0,
2144           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2145         /* Syntek (Asus F9SG) */
2146         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2147                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2148           .idVendor             = 0x174f,
2149           .idProduct            = 0x8a31,
2150           .bInterfaceClass      = USB_CLASS_VIDEO,
2151           .bInterfaceSubClass   = 1,
2152           .bInterfaceProtocol   = 0,
2153           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2154         /* Syntek (Asus U3S) */
2155         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2156                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2157           .idVendor             = 0x174f,
2158           .idProduct            = 0x8a33,
2159           .bInterfaceClass      = USB_CLASS_VIDEO,
2160           .bInterfaceSubClass   = 1,
2161           .bInterfaceProtocol   = 0,
2162           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2163         /* Syntek (JAOtech Smart Terminal) */
2164         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2165                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2166           .idVendor             = 0x174f,
2167           .idProduct            = 0x8a34,
2168           .bInterfaceClass      = USB_CLASS_VIDEO,
2169           .bInterfaceSubClass   = 1,
2170           .bInterfaceProtocol   = 0,
2171           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2172         /* Miricle 307K */
2173         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2174                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2175           .idVendor             = 0x17dc,
2176           .idProduct            = 0x0202,
2177           .bInterfaceClass      = USB_CLASS_VIDEO,
2178           .bInterfaceSubClass   = 1,
2179           .bInterfaceProtocol   = 0,
2180           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2181         /* Lenovo Thinkpad SL400/SL500 */
2182         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2183                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2184           .idVendor             = 0x17ef,
2185           .idProduct            = 0x480b,
2186           .bInterfaceClass      = USB_CLASS_VIDEO,
2187           .bInterfaceSubClass   = 1,
2188           .bInterfaceProtocol   = 0,
2189           .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2190         /* Aveo Technology USB 2.0 Camera */
2191         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2192                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2193           .idVendor             = 0x1871,
2194           .idProduct            = 0x0306,
2195           .bInterfaceClass      = USB_CLASS_VIDEO,
2196           .bInterfaceSubClass   = 1,
2197           .bInterfaceProtocol   = 0,
2198           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2199                                 | UVC_QUIRK_PROBE_EXTRAFIELDS },
2200         /* Ecamm Pico iMage */
2201         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2202                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2203           .idVendor             = 0x18cd,
2204           .idProduct            = 0xcafe,
2205           .bInterfaceClass      = USB_CLASS_VIDEO,
2206           .bInterfaceSubClass   = 1,
2207           .bInterfaceProtocol   = 0,
2208           .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
2209         /* Manta MM-353 Plako */
2210         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2211                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2212           .idVendor             = 0x18ec,
2213           .idProduct            = 0x3188,
2214           .bInterfaceClass      = USB_CLASS_VIDEO,
2215           .bInterfaceSubClass   = 1,
2216           .bInterfaceProtocol   = 0,
2217           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2218         /* FSC WebCam V30S */
2219         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2220                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2221           .idVendor             = 0x18ec,
2222           .idProduct            = 0x3288,
2223           .bInterfaceClass      = USB_CLASS_VIDEO,
2224           .bInterfaceSubClass   = 1,
2225           .bInterfaceProtocol   = 0,
2226           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2227         /* Arkmicro unbranded */
2228         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2229                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2230           .idVendor             = 0x18ec,
2231           .idProduct            = 0x3290,
2232           .bInterfaceClass      = USB_CLASS_VIDEO,
2233           .bInterfaceSubClass   = 1,
2234           .bInterfaceProtocol   = 0,
2235           .driver_info          = UVC_QUIRK_PROBE_DEF },
2236         /* Bodelin ProScopeHR */
2237         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2238                                 | USB_DEVICE_ID_MATCH_DEV_HI
2239                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2240           .idVendor             = 0x19ab,
2241           .idProduct            = 0x1000,
2242           .bcdDevice_hi         = 0x0126,
2243           .bInterfaceClass      = USB_CLASS_VIDEO,
2244           .bInterfaceSubClass   = 1,
2245           .bInterfaceProtocol   = 0,
2246           .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
2247         /* MSI StarCam 370i */
2248         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2249                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2250           .idVendor             = 0x1b3b,
2251           .idProduct            = 0x2951,
2252           .bInterfaceClass      = USB_CLASS_VIDEO,
2253           .bInterfaceSubClass   = 1,
2254           .bInterfaceProtocol   = 0,
2255           .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2256         /* SiGma Micro USB Web Camera */
2257         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2258                                 | USB_DEVICE_ID_MATCH_INT_INFO,
2259           .idVendor             = 0x1c4f,
2260           .idProduct            = 0x3000,
2261           .bInterfaceClass      = USB_CLASS_VIDEO,
2262           .bInterfaceSubClass   = 1,
2263           .bInterfaceProtocol   = 0,
2264           .driver_info          = UVC_QUIRK_PROBE_MINMAX
2265                                 | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2266         /* Generic USB Video Class */
2267         { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2268         {}
2269 };
2270
2271 MODULE_DEVICE_TABLE(usb, uvc_ids);
2272
2273 struct uvc_driver uvc_driver = {
2274         .driver = {
2275                 .name           = "uvcvideo",
2276                 .probe          = uvc_probe,
2277                 .disconnect     = uvc_disconnect,
2278                 .suspend        = uvc_suspend,
2279                 .resume         = uvc_resume,
2280                 .reset_resume   = uvc_reset_resume,
2281                 .id_table       = uvc_ids,
2282                 .supports_autosuspend = 1,
2283         },
2284 };
2285
2286 static int __init uvc_init(void)
2287 {
2288         int result;
2289
2290         result = usb_register(&uvc_driver.driver);
2291         if (result == 0)
2292                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2293         return result;
2294 }
2295
2296 static void __exit uvc_cleanup(void)
2297 {
2298         usb_deregister(&uvc_driver.driver);
2299 }
2300
2301 module_init(uvc_init);
2302 module_exit(uvc_cleanup);
2303
2304 MODULE_AUTHOR(DRIVER_AUTHOR);
2305 MODULE_DESCRIPTION(DRIVER_DESC);
2306 MODULE_LICENSE("GPL");
2307 MODULE_VERSION(DRIVER_VERSION);
2308