Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / media / video / uvc / uvc_driver.c
1 /*
2  *      uvc_driver.c  --  USB Video Class driver
3  *
4  *      Copyright (C) 2005-2009
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13
14 /*
15  * This driver aims to support video input and ouput devices compliant with the
16  * 'USB Video Class' specification.
17  *
18  * The driver doesn't support the deprecated v4l1 interface. It implements the
19  * mmap capture method only, and doesn't do any image format conversion in
20  * software. If your user-space application doesn't support YUYV or MJPEG, fix
21  * it :-). Please note that the MJPEG data have been stripped from their
22  * Huffman tables (DHT marker), you will need to add it back if your JPEG
23  * codec can't handle MJPEG data.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/usb.h>
31 #include <linux/videodev2.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <asm/atomic.h>
35 #include <asm/unaligned.h>
36
37 #include <media/v4l2-common.h>
38
39 #include "uvcvideo.h"
40
41 #define DRIVER_AUTHOR           "Laurent Pinchart <laurent.pinchart@skynet.be>"
42 #define DRIVER_DESC             "USB Video Class driver"
43 #ifndef DRIVER_VERSION
44 #define DRIVER_VERSION          "v0.1.0"
45 #endif
46
47 unsigned int uvc_clock_param = CLOCK_MONOTONIC;
48 unsigned int uvc_no_drop_param;
49 static unsigned int uvc_quirks_param = -1;
50 unsigned int uvc_trace_param;
51 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
52
53 /* ------------------------------------------------------------------------
54  * Video formats
55  */
56
57 static struct uvc_format_desc uvc_fmts[] = {
58         {
59                 .name           = "YUV 4:2:2 (YUYV)",
60                 .guid           = UVC_GUID_FORMAT_YUY2,
61                 .fcc            = V4L2_PIX_FMT_YUYV,
62         },
63         {
64                 .name           = "YUV 4:2:2 (YUYV)",
65                 .guid           = UVC_GUID_FORMAT_YUY2_ISIGHT,
66                 .fcc            = V4L2_PIX_FMT_YUYV,
67         },
68         {
69                 .name           = "YUV 4:2:0 (NV12)",
70                 .guid           = UVC_GUID_FORMAT_NV12,
71                 .fcc            = V4L2_PIX_FMT_NV12,
72         },
73         {
74                 .name           = "MJPEG",
75                 .guid           = UVC_GUID_FORMAT_MJPEG,
76                 .fcc            = V4L2_PIX_FMT_MJPEG,
77         },
78         {
79                 .name           = "YVU 4:2:0 (YV12)",
80                 .guid           = UVC_GUID_FORMAT_YV12,
81                 .fcc            = V4L2_PIX_FMT_YVU420,
82         },
83         {
84                 .name           = "YUV 4:2:0 (I420)",
85                 .guid           = UVC_GUID_FORMAT_I420,
86                 .fcc            = V4L2_PIX_FMT_YUV420,
87         },
88         {
89                 .name           = "YUV 4:2:2 (UYVY)",
90                 .guid           = UVC_GUID_FORMAT_UYVY,
91                 .fcc            = V4L2_PIX_FMT_UYVY,
92         },
93         {
94                 .name           = "Greyscale (8-bit)",
95                 .guid           = UVC_GUID_FORMAT_Y800,
96                 .fcc            = V4L2_PIX_FMT_GREY,
97         },
98         {
99                 .name           = "Greyscale (16-bit)",
100                 .guid           = UVC_GUID_FORMAT_Y16,
101                 .fcc            = V4L2_PIX_FMT_Y16,
102         },
103         {
104                 .name           = "RGB Bayer",
105                 .guid           = UVC_GUID_FORMAT_BY8,
106                 .fcc            = V4L2_PIX_FMT_SBGGR8,
107         },
108 };
109
110 /* ------------------------------------------------------------------------
111  * Utility functions
112  */
113
114 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
115                 __u8 epaddr)
116 {
117         struct usb_host_endpoint *ep;
118         unsigned int i;
119
120         for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
121                 ep = &alts->endpoint[i];
122                 if (ep->desc.bEndpointAddress == epaddr)
123                         return ep;
124         }
125
126         return NULL;
127 }
128
129 static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
130 {
131         unsigned int len = ARRAY_SIZE(uvc_fmts);
132         unsigned int i;
133
134         for (i = 0; i < len; ++i) {
135                 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
136                         return &uvc_fmts[i];
137         }
138
139         return NULL;
140 }
141
142 static __u32 uvc_colorspace(const __u8 primaries)
143 {
144         static const __u8 colorprimaries[] = {
145                 0,
146                 V4L2_COLORSPACE_SRGB,
147                 V4L2_COLORSPACE_470_SYSTEM_M,
148                 V4L2_COLORSPACE_470_SYSTEM_BG,
149                 V4L2_COLORSPACE_SMPTE170M,
150                 V4L2_COLORSPACE_SMPTE240M,
151         };
152
153         if (primaries < ARRAY_SIZE(colorprimaries))
154                 return colorprimaries[primaries];
155
156         return 0;
157 }
158
159 /* Simplify a fraction using a simple continued fraction decomposition. The
160  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
161  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
162  * arbitrary parameters to remove non-significative terms from the simple
163  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
164  * respectively seems to give nice results.
165  */
166 void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
167                 unsigned int n_terms, unsigned int threshold)
168 {
169         uint32_t *an;
170         uint32_t x, y, r;
171         unsigned int i, n;
172
173         an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
174         if (an == NULL)
175                 return;
176
177         /* Convert the fraction to a simple continued fraction. See
178          * http://mathforum.org/dr.math/faq/faq.fractions.html
179          * Stop if the current term is bigger than or equal to the given
180          * threshold.
181          */
182         x = *numerator;
183         y = *denominator;
184
185         for (n = 0; n < n_terms && y != 0; ++n) {
186                 an[n] = x / y;
187                 if (an[n] >= threshold) {
188                         if (n < 2)
189                                 n++;
190                         break;
191                 }
192
193                 r = x - an[n] * y;
194                 x = y;
195                 y = r;
196         }
197
198         /* Expand the simple continued fraction back to an integer fraction. */
199         x = 0;
200         y = 1;
201
202         for (i = n; i > 0; --i) {
203                 r = y;
204                 y = an[i-1] * y + x;
205                 x = r;
206         }
207
208         *numerator = y;
209         *denominator = x;
210         kfree(an);
211 }
212
213 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
214  * to compute numerator / denominator * 10000000 using 32 bit fixed point
215  * arithmetic only.
216  */
217 uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
218 {
219         uint32_t multiplier;
220
221         /* Saturate the result if the operation would overflow. */
222         if (denominator == 0 ||
223             numerator/denominator >= ((uint32_t)-1)/10000000)
224                 return (uint32_t)-1;
225
226         /* Divide both the denominator and the multiplier by two until
227          * numerator * multiplier doesn't overflow. If anyone knows a better
228          * algorithm please let me know.
229          */
230         multiplier = 10000000;
231         while (numerator > ((uint32_t)-1)/multiplier) {
232                 multiplier /= 2;
233                 denominator /= 2;
234         }
235
236         return denominator ? numerator * multiplier / denominator : 0;
237 }
238
239 /* ------------------------------------------------------------------------
240  * Terminal and unit management
241  */
242
243 static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
244 {
245         struct uvc_entity *entity;
246
247         list_for_each_entry(entity, &dev->entities, list) {
248                 if (entity->id == id)
249                         return entity;
250         }
251
252         return NULL;
253 }
254
255 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
256         int id, struct uvc_entity *entity)
257 {
258         unsigned int i;
259
260         if (entity == NULL)
261                 entity = list_entry(&dev->entities, struct uvc_entity, list);
262
263         list_for_each_entry_continue(entity, &dev->entities, list) {
264                 for (i = 0; i < entity->bNrInPins; ++i)
265                         if (entity->baSourceID[i] == id)
266                                 return entity;
267         }
268
269         return NULL;
270 }
271
272 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
273 {
274         struct uvc_streaming *stream;
275
276         list_for_each_entry(stream, &dev->streams, list) {
277                 if (stream->header.bTerminalLink == id)
278                         return stream;
279         }
280
281         return NULL;
282 }
283
284 /* ------------------------------------------------------------------------
285  * Descriptors parsing
286  */
287
288 static int uvc_parse_format(struct uvc_device *dev,
289         struct uvc_streaming *streaming, struct uvc_format *format,
290         __u32 **intervals, unsigned char *buffer, int buflen)
291 {
292         struct usb_interface *intf = streaming->intf;
293         struct usb_host_interface *alts = intf->cur_altsetting;
294         struct uvc_format_desc *fmtdesc;
295         struct uvc_frame *frame;
296         const unsigned char *start = buffer;
297         unsigned int interval;
298         unsigned int i, n;
299         __u8 ftype;
300
301         format->type = buffer[2];
302         format->index = buffer[3];
303
304         switch (buffer[2]) {
305         case UVC_VS_FORMAT_UNCOMPRESSED:
306         case UVC_VS_FORMAT_FRAME_BASED:
307                 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
308                 if (buflen < n) {
309                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
310                                "interface %d FORMAT error\n",
311                                dev->udev->devnum,
312                                alts->desc.bInterfaceNumber);
313                         return -EINVAL;
314                 }
315
316                 /* Find the format descriptor from its GUID. */
317                 fmtdesc = uvc_format_by_guid(&buffer[5]);
318
319                 if (fmtdesc != NULL) {
320                         strlcpy(format->name, fmtdesc->name,
321                                 sizeof format->name);
322                         format->fcc = fmtdesc->fcc;
323                 } else {
324                         uvc_printk(KERN_INFO, "Unknown video format %pUl\n",
325                                 &buffer[5]);
326                         snprintf(format->name, sizeof(format->name), "%pUl\n",
327                                 &buffer[5]);
328                         format->fcc = 0;
329                 }
330
331                 format->bpp = buffer[21];
332                 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
333                         ftype = UVC_VS_FRAME_UNCOMPRESSED;
334                 } else {
335                         ftype = UVC_VS_FRAME_FRAME_BASED;
336                         if (buffer[27])
337                                 format->flags = UVC_FMT_FLAG_COMPRESSED;
338                 }
339                 break;
340
341         case UVC_VS_FORMAT_MJPEG:
342                 if (buflen < 11) {
343                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
344                                "interface %d FORMAT error\n",
345                                dev->udev->devnum,
346                                alts->desc.bInterfaceNumber);
347                         return -EINVAL;
348                 }
349
350                 strlcpy(format->name, "MJPEG", sizeof format->name);
351                 format->fcc = V4L2_PIX_FMT_MJPEG;
352                 format->flags = UVC_FMT_FLAG_COMPRESSED;
353                 format->bpp = 0;
354                 ftype = UVC_VS_FRAME_MJPEG;
355                 break;
356
357         case UVC_VS_FORMAT_DV:
358                 if (buflen < 9) {
359                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
360                                "interface %d FORMAT error\n",
361                                dev->udev->devnum,
362                                alts->desc.bInterfaceNumber);
363                         return -EINVAL;
364                 }
365
366                 switch (buffer[8] & 0x7f) {
367                 case 0:
368                         strlcpy(format->name, "SD-DV", sizeof format->name);
369                         break;
370                 case 1:
371                         strlcpy(format->name, "SDL-DV", sizeof format->name);
372                         break;
373                 case 2:
374                         strlcpy(format->name, "HD-DV", sizeof format->name);
375                         break;
376                 default:
377                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
378                                "interface %d: unknown DV format %u\n",
379                                dev->udev->devnum,
380                                alts->desc.bInterfaceNumber, buffer[8]);
381                         return -EINVAL;
382                 }
383
384                 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
385                         sizeof format->name);
386
387                 format->fcc = V4L2_PIX_FMT_DV;
388                 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
389                 format->bpp = 0;
390                 ftype = 0;
391
392                 /* Create a dummy frame descriptor. */
393                 frame = &format->frame[0];
394                 memset(&format->frame[0], 0, sizeof format->frame[0]);
395                 frame->bFrameIntervalType = 1;
396                 frame->dwDefaultFrameInterval = 1;
397                 frame->dwFrameInterval = *intervals;
398                 *(*intervals)++ = 1;
399                 format->nframes = 1;
400                 break;
401
402         case UVC_VS_FORMAT_MPEG2TS:
403         case UVC_VS_FORMAT_STREAM_BASED:
404                 /* Not supported yet. */
405         default:
406                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
407                        "interface %d unsupported format %u\n",
408                        dev->udev->devnum, alts->desc.bInterfaceNumber,
409                        buffer[2]);
410                 return -EINVAL;
411         }
412
413         uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
414
415         buflen -= buffer[0];
416         buffer += buffer[0];
417
418         /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
419          * based formats have frame descriptors.
420          */
421         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
422                buffer[2] == ftype) {
423                 frame = &format->frame[format->nframes];
424                 if (ftype != UVC_VS_FRAME_FRAME_BASED)
425                         n = buflen > 25 ? buffer[25] : 0;
426                 else
427                         n = buflen > 21 ? buffer[21] : 0;
428
429                 n = n ? n : 3;
430
431                 if (buflen < 26 + 4*n) {
432                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
433                                "interface %d FRAME error\n", dev->udev->devnum,
434                                alts->desc.bInterfaceNumber);
435                         return -EINVAL;
436                 }
437
438                 frame->bFrameIndex = buffer[3];
439                 frame->bmCapabilities = buffer[4];
440                 frame->wWidth = get_unaligned_le16(&buffer[5]);
441                 frame->wHeight = get_unaligned_le16(&buffer[7]);
442                 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
443                 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
444                 if (ftype != UVC_VS_FRAME_FRAME_BASED) {
445                         frame->dwMaxVideoFrameBufferSize =
446                                 get_unaligned_le32(&buffer[17]);
447                         frame->dwDefaultFrameInterval =
448                                 get_unaligned_le32(&buffer[21]);
449                         frame->bFrameIntervalType = buffer[25];
450                 } else {
451                         frame->dwMaxVideoFrameBufferSize = 0;
452                         frame->dwDefaultFrameInterval =
453                                 get_unaligned_le32(&buffer[17]);
454                         frame->bFrameIntervalType = buffer[21];
455                 }
456                 frame->dwFrameInterval = *intervals;
457
458                 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
459                  * completely. Observed behaviours range from setting the
460                  * value to 1.1x the actual frame size to hardwiring the
461                  * 16 low bits to 0. This results in a higher than necessary
462                  * memory usage as well as a wrong image size information. For
463                  * uncompressed formats this can be fixed by computing the
464                  * value from the frame size.
465                  */
466                 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
467                         frame->dwMaxVideoFrameBufferSize = format->bpp
468                                 * frame->wWidth * frame->wHeight / 8;
469
470                 /* Some bogus devices report dwMinFrameInterval equal to
471                  * dwMaxFrameInterval and have dwFrameIntervalStep set to
472                  * zero. Setting all null intervals to 1 fixes the problem and
473                  * some other divisions by zero that could happen.
474                  */
475                 for (i = 0; i < n; ++i) {
476                         interval = get_unaligned_le32(&buffer[26+4*i]);
477                         *(*intervals)++ = interval ? interval : 1;
478                 }
479
480                 /* Make sure that the default frame interval stays between
481                  * the boundaries.
482                  */
483                 n -= frame->bFrameIntervalType ? 1 : 2;
484                 frame->dwDefaultFrameInterval =
485                         min(frame->dwFrameInterval[n],
486                             max(frame->dwFrameInterval[0],
487                                 frame->dwDefaultFrameInterval));
488
489                 if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
490                         frame->bFrameIntervalType = 1;
491                         frame->dwFrameInterval[0] =
492                                 frame->dwDefaultFrameInterval;
493                 }
494
495                 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
496                         frame->wWidth, frame->wHeight,
497                         10000000/frame->dwDefaultFrameInterval,
498                         (100000000/frame->dwDefaultFrameInterval)%10);
499
500                 format->nframes++;
501                 buflen -= buffer[0];
502                 buffer += buffer[0];
503         }
504
505         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
506             buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
507                 buflen -= buffer[0];
508                 buffer += buffer[0];
509         }
510
511         if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
512             buffer[2] == UVC_VS_COLORFORMAT) {
513                 if (buflen < 6) {
514                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
515                                "interface %d COLORFORMAT error\n",
516                                dev->udev->devnum,
517                                alts->desc.bInterfaceNumber);
518                         return -EINVAL;
519                 }
520
521                 format->colorspace = uvc_colorspace(buffer[3]);
522
523                 buflen -= buffer[0];
524                 buffer += buffer[0];
525         }
526
527         return buffer - start;
528 }
529
530 static int uvc_parse_streaming(struct uvc_device *dev,
531         struct usb_interface *intf)
532 {
533         struct uvc_streaming *streaming = NULL;
534         struct uvc_format *format;
535         struct uvc_frame *frame;
536         struct usb_host_interface *alts = &intf->altsetting[0];
537         unsigned char *_buffer, *buffer = alts->extra;
538         int _buflen, buflen = alts->extralen;
539         unsigned int nformats = 0, nframes = 0, nintervals = 0;
540         unsigned int size, i, n, p;
541         __u32 *interval;
542         __u16 psize;
543         int ret = -EINVAL;
544
545         if (intf->cur_altsetting->desc.bInterfaceSubClass
546                 != UVC_SC_VIDEOSTREAMING) {
547                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
548                         "video streaming interface\n", dev->udev->devnum,
549                         intf->altsetting[0].desc.bInterfaceNumber);
550                 return -EINVAL;
551         }
552
553         if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
554                 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
555                         "claimed\n", dev->udev->devnum,
556                         intf->altsetting[0].desc.bInterfaceNumber);
557                 return -EINVAL;
558         }
559
560         streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
561         if (streaming == NULL) {
562                 usb_driver_release_interface(&uvc_driver.driver, intf);
563                 return -EINVAL;
564         }
565
566         mutex_init(&streaming->mutex);
567         streaming->dev = dev;
568         streaming->intf = usb_get_intf(intf);
569         streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
570
571         /* The Pico iMage webcam has its class-specific interface descriptors
572          * after the endpoint descriptors.
573          */
574         if (buflen == 0) {
575                 for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
576                         struct usb_host_endpoint *ep = &alts->endpoint[i];
577
578                         if (ep->extralen == 0)
579                                 continue;
580
581                         if (ep->extralen > 2 &&
582                             ep->extra[1] == USB_DT_CS_INTERFACE) {
583                                 uvc_trace(UVC_TRACE_DESCR, "trying extra data "
584                                         "from endpoint %u.\n", i);
585                                 buffer = alts->endpoint[i].extra;
586                                 buflen = alts->endpoint[i].extralen;
587                                 break;
588                         }
589                 }
590         }
591
592         /* Skip the standard interface descriptors. */
593         while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
594                 buflen -= buffer[0];
595                 buffer += buffer[0];
596         }
597
598         if (buflen <= 2) {
599                 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
600                         "interface descriptors found.\n");
601                 goto error;
602         }
603
604         /* Parse the header descriptor. */
605         switch (buffer[2]) {
606         case UVC_VS_OUTPUT_HEADER:
607                 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
608                 size = 9;
609                 break;
610
611         case UVC_VS_INPUT_HEADER:
612                 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
613                 size = 13;
614                 break;
615
616         default:
617                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
618                         "%d HEADER descriptor not found.\n", dev->udev->devnum,
619                         alts->desc.bInterfaceNumber);
620                 goto error;
621         }
622
623         p = buflen >= 4 ? buffer[3] : 0;
624         n = buflen >= size ? buffer[size-1] : 0;
625
626         if (buflen < size + p*n) {
627                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
628                         "interface %d HEADER descriptor is invalid.\n",
629                         dev->udev->devnum, alts->desc.bInterfaceNumber);
630                 goto error;
631         }
632
633         streaming->header.bNumFormats = p;
634         streaming->header.bEndpointAddress = buffer[6];
635         if (buffer[2] == UVC_VS_INPUT_HEADER) {
636                 streaming->header.bmInfo = buffer[7];
637                 streaming->header.bTerminalLink = buffer[8];
638                 streaming->header.bStillCaptureMethod = buffer[9];
639                 streaming->header.bTriggerSupport = buffer[10];
640                 streaming->header.bTriggerUsage = buffer[11];
641         } else {
642                 streaming->header.bTerminalLink = buffer[7];
643         }
644         streaming->header.bControlSize = n;
645
646         streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
647                                                 GFP_KERNEL);
648         if (streaming->header.bmaControls == NULL) {
649                 ret = -ENOMEM;
650                 goto error;
651         }
652
653         buflen -= buffer[0];
654         buffer += buffer[0];
655
656         _buffer = buffer;
657         _buflen = buflen;
658
659         /* Count the format and frame descriptors. */
660         while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
661                 switch (_buffer[2]) {
662                 case UVC_VS_FORMAT_UNCOMPRESSED:
663                 case UVC_VS_FORMAT_MJPEG:
664                 case UVC_VS_FORMAT_FRAME_BASED:
665                         nformats++;
666                         break;
667
668                 case UVC_VS_FORMAT_DV:
669                         /* DV format has no frame descriptor. We will create a
670                          * dummy frame descriptor with a dummy frame interval.
671                          */
672                         nformats++;
673                         nframes++;
674                         nintervals++;
675                         break;
676
677                 case UVC_VS_FORMAT_MPEG2TS:
678                 case UVC_VS_FORMAT_STREAM_BASED:
679                         uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
680                                 "interface %d FORMAT %u is not supported.\n",
681                                 dev->udev->devnum,
682                                 alts->desc.bInterfaceNumber, _buffer[2]);
683                         break;
684
685                 case UVC_VS_FRAME_UNCOMPRESSED:
686                 case UVC_VS_FRAME_MJPEG:
687                         nframes++;
688                         if (_buflen > 25)
689                                 nintervals += _buffer[25] ? _buffer[25] : 3;
690                         break;
691
692                 case UVC_VS_FRAME_FRAME_BASED:
693                         nframes++;
694                         if (_buflen > 21)
695                                 nintervals += _buffer[21] ? _buffer[21] : 3;
696                         break;
697                 }
698
699                 _buflen -= _buffer[0];
700                 _buffer += _buffer[0];
701         }
702
703         if (nformats == 0) {
704                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
705                         "%d has no supported formats defined.\n",
706                         dev->udev->devnum, alts->desc.bInterfaceNumber);
707                 goto error;
708         }
709
710         size = nformats * sizeof *format + nframes * sizeof *frame
711              + nintervals * sizeof *interval;
712         format = kzalloc(size, GFP_KERNEL);
713         if (format == NULL) {
714                 ret = -ENOMEM;
715                 goto error;
716         }
717
718         frame = (struct uvc_frame *)&format[nformats];
719         interval = (__u32 *)&frame[nframes];
720
721         streaming->format = format;
722         streaming->nformats = nformats;
723
724         /* Parse the format descriptors. */
725         while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
726                 switch (buffer[2]) {
727                 case UVC_VS_FORMAT_UNCOMPRESSED:
728                 case UVC_VS_FORMAT_MJPEG:
729                 case UVC_VS_FORMAT_DV:
730                 case UVC_VS_FORMAT_FRAME_BASED:
731                         format->frame = frame;
732                         ret = uvc_parse_format(dev, streaming, format,
733                                 &interval, buffer, buflen);
734                         if (ret < 0)
735                                 goto error;
736
737                         frame += format->nframes;
738                         format++;
739
740                         buflen -= ret;
741                         buffer += ret;
742                         continue;
743
744                 default:
745                         break;
746                 }
747
748                 buflen -= buffer[0];
749                 buffer += buffer[0];
750         }
751
752         if (buflen)
753                 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
754                         "%d has %u bytes of trailing descriptor garbage.\n",
755                         dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
756
757         /* Parse the alternate settings to find the maximum bandwidth. */
758         for (i = 0; i < intf->num_altsetting; ++i) {
759                 struct usb_host_endpoint *ep;
760                 alts = &intf->altsetting[i];
761                 ep = uvc_find_endpoint(alts,
762                                 streaming->header.bEndpointAddress);
763                 if (ep == NULL)
764                         continue;
765
766                 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
767                 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
768                 if (psize > streaming->maxpsize)
769                         streaming->maxpsize = psize;
770         }
771
772         list_add_tail(&streaming->list, &dev->streams);
773         return 0;
774
775 error:
776         usb_driver_release_interface(&uvc_driver.driver, intf);
777         usb_put_intf(intf);
778         kfree(streaming->format);
779         kfree(streaming->header.bmaControls);
780         kfree(streaming);
781         return ret;
782 }
783
784 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
785                 unsigned int num_pads, unsigned int extra_size)
786 {
787         struct uvc_entity *entity;
788         unsigned int num_inputs;
789         unsigned int size;
790
791         num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
792         size = sizeof(*entity) + extra_size + num_inputs;
793         entity = kzalloc(size, GFP_KERNEL);
794         if (entity == NULL)
795                 return NULL;
796
797         entity->id = id;
798         entity->type = type;
799
800         entity->bNrInPins = num_inputs;
801         entity->baSourceID = ((__u8 *)entity) + sizeof(*entity) + extra_size;
802
803         return entity;
804 }
805
806 /* Parse vendor-specific extensions. */
807 static int uvc_parse_vendor_control(struct uvc_device *dev,
808         const unsigned char *buffer, int buflen)
809 {
810         struct usb_device *udev = dev->udev;
811         struct usb_host_interface *alts = dev->intf->cur_altsetting;
812         struct uvc_entity *unit;
813         unsigned int n, p;
814         int handled = 0;
815
816         switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
817         case 0x046d:            /* Logitech */
818                 if (buffer[1] != 0x41 || buffer[2] != 0x01)
819                         break;
820
821                 /* Logitech implements several vendor specific functions
822                  * through vendor specific extension units (LXU).
823                  *
824                  * The LXU descriptors are similar to XU descriptors
825                  * (see "USB Device Video Class for Video Devices", section
826                  * 3.7.2.6 "Extension Unit Descriptor") with the following
827                  * differences:
828                  *
829                  * ----------------------------------------------------------
830                  * 0            bLength         1        Number
831                  *      Size of this descriptor, in bytes: 24+p+n*2
832                  * ----------------------------------------------------------
833                  * 23+p+n       bmControlsType  N       Bitmap
834                  *      Individual bits in the set are defined:
835                  *      0: Absolute
836                  *      1: Relative
837                  *
838                  *      This bitset is mapped exactly the same as bmControls.
839                  * ----------------------------------------------------------
840                  * 23+p+n*2     bReserved       1       Boolean
841                  * ----------------------------------------------------------
842                  * 24+p+n*2     iExtension      1       Index
843                  *      Index of a string descriptor that describes this
844                  *      extension unit.
845                  * ----------------------------------------------------------
846                  */
847                 p = buflen >= 22 ? buffer[21] : 0;
848                 n = buflen >= 25 + p ? buffer[22+p] : 0;
849
850                 if (buflen < 25 + p + 2*n) {
851                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
852                                 "interface %d EXTENSION_UNIT error\n",
853                                 udev->devnum, alts->desc.bInterfaceNumber);
854                         break;
855                 }
856
857                 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
858                                         p + 1, 2*n);
859                 if (unit == NULL)
860                         return -ENOMEM;
861
862                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
863                 unit->extension.bNumControls = buffer[20];
864                 memcpy(unit->baSourceID, &buffer[22], p);
865                 unit->extension.bControlSize = buffer[22+p];
866                 unit->extension.bmControls = (__u8 *)unit + sizeof(*unit);
867                 unit->extension.bmControlsType = (__u8 *)unit + sizeof(*unit)
868                                                + n;
869                 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
870
871                 if (buffer[24+p+2*n] != 0)
872                         usb_string(udev, buffer[24+p+2*n], unit->name,
873                                    sizeof unit->name);
874                 else
875                         sprintf(unit->name, "Extension %u", buffer[3]);
876
877                 list_add_tail(&unit->list, &dev->entities);
878                 handled = 1;
879                 break;
880         }
881
882         return handled;
883 }
884
885 static int uvc_parse_standard_control(struct uvc_device *dev,
886         const unsigned char *buffer, int buflen)
887 {
888         struct usb_device *udev = dev->udev;
889         struct uvc_entity *unit, *term;
890         struct usb_interface *intf;
891         struct usb_host_interface *alts = dev->intf->cur_altsetting;
892         unsigned int i, n, p, len;
893         __u16 type;
894
895         switch (buffer[2]) {
896         case UVC_VC_HEADER:
897                 n = buflen >= 12 ? buffer[11] : 0;
898
899                 if (buflen < 12 || buflen < 12 + n) {
900                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
901                                 "interface %d HEADER error\n", udev->devnum,
902                                 alts->desc.bInterfaceNumber);
903                         return -EINVAL;
904                 }
905
906                 dev->uvc_version = get_unaligned_le16(&buffer[3]);
907                 dev->clock_frequency = get_unaligned_le32(&buffer[7]);
908
909                 /* Parse all USB Video Streaming interfaces. */
910                 for (i = 0; i < n; ++i) {
911                         intf = usb_ifnum_to_if(udev, buffer[12+i]);
912                         if (intf == NULL) {
913                                 uvc_trace(UVC_TRACE_DESCR, "device %d "
914                                         "interface %d doesn't exists\n",
915                                         udev->devnum, i);
916                                 continue;
917                         }
918
919                         uvc_parse_streaming(dev, intf);
920                 }
921                 break;
922
923         case UVC_VC_INPUT_TERMINAL:
924                 if (buflen < 8) {
925                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
926                                 "interface %d INPUT_TERMINAL error\n",
927                                 udev->devnum, alts->desc.bInterfaceNumber);
928                         return -EINVAL;
929                 }
930
931                 /* Make sure the terminal type MSB is not null, otherwise it
932                  * could be confused with a unit.
933                  */
934                 type = get_unaligned_le16(&buffer[4]);
935                 if ((type & 0xff00) == 0) {
936                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
937                                 "interface %d INPUT_TERMINAL %d has invalid "
938                                 "type 0x%04x, skipping\n", udev->devnum,
939                                 alts->desc.bInterfaceNumber,
940                                 buffer[3], type);
941                         return 0;
942                 }
943
944                 n = 0;
945                 p = 0;
946                 len = 8;
947
948                 if (type == UVC_ITT_CAMERA) {
949                         n = buflen >= 15 ? buffer[14] : 0;
950                         len = 15;
951
952                 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
953                         n = buflen >= 9 ? buffer[8] : 0;
954                         p = buflen >= 10 + n ? buffer[9+n] : 0;
955                         len = 10;
956                 }
957
958                 if (buflen < len + n + p) {
959                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
960                                 "interface %d INPUT_TERMINAL error\n",
961                                 udev->devnum, alts->desc.bInterfaceNumber);
962                         return -EINVAL;
963                 }
964
965                 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
966                                         1, n + p);
967                 if (term == NULL)
968                         return -ENOMEM;
969
970                 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
971                         term->camera.bControlSize = n;
972                         term->camera.bmControls = (__u8 *)term + sizeof *term;
973                         term->camera.wObjectiveFocalLengthMin =
974                                 get_unaligned_le16(&buffer[8]);
975                         term->camera.wObjectiveFocalLengthMax =
976                                 get_unaligned_le16(&buffer[10]);
977                         term->camera.wOcularFocalLength =
978                                 get_unaligned_le16(&buffer[12]);
979                         memcpy(term->camera.bmControls, &buffer[15], n);
980                 } else if (UVC_ENTITY_TYPE(term) ==
981                            UVC_ITT_MEDIA_TRANSPORT_INPUT) {
982                         term->media.bControlSize = n;
983                         term->media.bmControls = (__u8 *)term + sizeof *term;
984                         term->media.bTransportModeSize = p;
985                         term->media.bmTransportModes = (__u8 *)term
986                                                      + sizeof *term + n;
987                         memcpy(term->media.bmControls, &buffer[9], n);
988                         memcpy(term->media.bmTransportModes, &buffer[10+n], p);
989                 }
990
991                 if (buffer[7] != 0)
992                         usb_string(udev, buffer[7], term->name,
993                                    sizeof term->name);
994                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
995                         sprintf(term->name, "Camera %u", buffer[3]);
996                 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
997                         sprintf(term->name, "Media %u", buffer[3]);
998                 else
999                         sprintf(term->name, "Input %u", buffer[3]);
1000
1001                 list_add_tail(&term->list, &dev->entities);
1002                 break;
1003
1004         case UVC_VC_OUTPUT_TERMINAL:
1005                 if (buflen < 9) {
1006                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1007                                 "interface %d OUTPUT_TERMINAL error\n",
1008                                 udev->devnum, alts->desc.bInterfaceNumber);
1009                         return -EINVAL;
1010                 }
1011
1012                 /* Make sure the terminal type MSB is not null, otherwise it
1013                  * could be confused with a unit.
1014                  */
1015                 type = get_unaligned_le16(&buffer[4]);
1016                 if ((type & 0xff00) == 0) {
1017                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1018                                 "interface %d OUTPUT_TERMINAL %d has invalid "
1019                                 "type 0x%04x, skipping\n", udev->devnum,
1020                                 alts->desc.bInterfaceNumber, buffer[3], type);
1021                         return 0;
1022                 }
1023
1024                 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1025                                         1, 0);
1026                 if (term == NULL)
1027                         return -ENOMEM;
1028
1029                 memcpy(term->baSourceID, &buffer[7], 1);
1030
1031                 if (buffer[8] != 0)
1032                         usb_string(udev, buffer[8], term->name,
1033                                    sizeof term->name);
1034                 else
1035                         sprintf(term->name, "Output %u", buffer[3]);
1036
1037                 list_add_tail(&term->list, &dev->entities);
1038                 break;
1039
1040         case UVC_VC_SELECTOR_UNIT:
1041                 p = buflen >= 5 ? buffer[4] : 0;
1042
1043                 if (buflen < 5 || buflen < 6 + p) {
1044                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1045                                 "interface %d SELECTOR_UNIT error\n",
1046                                 udev->devnum, alts->desc.bInterfaceNumber);
1047                         return -EINVAL;
1048                 }
1049
1050                 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1051                 if (unit == NULL)
1052                         return -ENOMEM;
1053
1054                 memcpy(unit->baSourceID, &buffer[5], p);
1055
1056                 if (buffer[5+p] != 0)
1057                         usb_string(udev, buffer[5+p], unit->name,
1058                                    sizeof unit->name);
1059                 else
1060                         sprintf(unit->name, "Selector %u", buffer[3]);
1061
1062                 list_add_tail(&unit->list, &dev->entities);
1063                 break;
1064
1065         case UVC_VC_PROCESSING_UNIT:
1066                 n = buflen >= 8 ? buffer[7] : 0;
1067                 p = dev->uvc_version >= 0x0110 ? 10 : 9;
1068
1069                 if (buflen < p + n) {
1070                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1071                                 "interface %d PROCESSING_UNIT error\n",
1072                                 udev->devnum, alts->desc.bInterfaceNumber);
1073                         return -EINVAL;
1074                 }
1075
1076                 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1077                 if (unit == NULL)
1078                         return -ENOMEM;
1079
1080                 memcpy(unit->baSourceID, &buffer[4], 1);
1081                 unit->processing.wMaxMultiplier =
1082                         get_unaligned_le16(&buffer[5]);
1083                 unit->processing.bControlSize = buffer[7];
1084                 unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1085                 memcpy(unit->processing.bmControls, &buffer[8], n);
1086                 if (dev->uvc_version >= 0x0110)
1087                         unit->processing.bmVideoStandards = buffer[9+n];
1088
1089                 if (buffer[8+n] != 0)
1090                         usb_string(udev, buffer[8+n], unit->name,
1091                                    sizeof unit->name);
1092                 else
1093                         sprintf(unit->name, "Processing %u", buffer[3]);
1094
1095                 list_add_tail(&unit->list, &dev->entities);
1096                 break;
1097
1098         case UVC_VC_EXTENSION_UNIT:
1099                 p = buflen >= 22 ? buffer[21] : 0;
1100                 n = buflen >= 24 + p ? buffer[22+p] : 0;
1101
1102                 if (buflen < 24 + p + n) {
1103                         uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1104                                 "interface %d EXTENSION_UNIT error\n",
1105                                 udev->devnum, alts->desc.bInterfaceNumber);
1106                         return -EINVAL;
1107                 }
1108
1109                 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1110                 if (unit == NULL)
1111                         return -ENOMEM;
1112
1113                 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1114                 unit->extension.bNumControls = buffer[20];
1115                 memcpy(unit->baSourceID, &buffer[22], p);
1116                 unit->extension.bControlSize = buffer[22+p];
1117                 unit->extension.bmControls = (__u8 *)unit + sizeof *unit;
1118                 memcpy(unit->extension.bmControls, &buffer[23+p], n);
1119
1120                 if (buffer[23+p+n] != 0)
1121                         usb_string(udev, buffer[23+p+n], unit->name,
1122                                    sizeof unit->name);
1123                 else
1124                         sprintf(unit->name, "Extension %u", buffer[3]);
1125
1126                 list_add_tail(&unit->list, &dev->entities);
1127                 break;
1128
1129         default:
1130                 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1131                         "descriptor (%u)\n", buffer[2]);
1132                 break;
1133         }
1134
1135         return 0;
1136 }
1137
1138 static int uvc_parse_control(struct uvc_device *dev)
1139 {
1140         struct usb_host_interface *alts = dev->intf->cur_altsetting;
1141         unsigned char *buffer = alts->extra;
1142         int buflen = alts->extralen;
1143         int ret;
1144
1145         /* Parse the default alternate setting only, as the UVC specification
1146          * defines a single alternate setting, the default alternate setting
1147          * zero.
1148          */
1149
1150         while (buflen > 2) {
1151                 if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1152                     buffer[1] != USB_DT_CS_INTERFACE)
1153                         goto next_descriptor;
1154
1155                 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1156                         return ret;
1157
1158 next_descriptor:
1159                 buflen -= buffer[0];
1160                 buffer += buffer[0];
1161         }
1162
1163         /* Check if the optional status endpoint is present. Built-in iSight
1164          * webcams have an interrupt endpoint but spit proprietary data that
1165          * don't conform to the UVC status endpoint messages. Don't try to
1166          * handle the interrupt endpoint for those cameras.
1167          */
1168         if (alts->desc.bNumEndpoints == 1 &&
1169             !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1170                 struct usb_host_endpoint *ep = &alts->endpoint[0];
1171                 struct usb_endpoint_descriptor *desc = &ep->desc;
1172
1173                 if (usb_endpoint_is_int_in(desc) &&
1174                     le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1175                     desc->bInterval != 0) {
1176                         uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1177                                 "(addr %02x).\n", desc->bEndpointAddress);
1178                         dev->int_ep = ep;
1179                 }
1180         }
1181
1182         return 0;
1183 }
1184
1185 /* ------------------------------------------------------------------------
1186  * UVC device scan
1187  */
1188
1189 /*
1190  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1191  * and containing the following units:
1192  *
1193  * - one or more Output Terminals (USB Streaming or Display)
1194  * - zero or one Processing Unit
1195  * - zero, one or more single-input Selector Units
1196  * - zero or one multiple-input Selector Units, provided all inputs are
1197  *   connected to input terminals
1198  * - zero, one or mode single-input Extension Units
1199  * - one or more Input Terminals (Camera, External or USB Streaming)
1200  *
1201  * The terminal and units must match on of the following structures:
1202  *
1203  * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
1204  * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
1205  * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
1206  *
1207  *                 +---------+    +---------+ -> OTT_*(0)
1208  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
1209  *                 +---------+    +---------+ -> OTT_*(n)
1210  *
1211  * The Processing Unit and Extension Units can be in any order. Additional
1212  * Extension Units connected to the main chain as single-unit branches are
1213  * also supported. Single-input Selector Units are ignored.
1214  */
1215 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1216         struct uvc_entity *entity)
1217 {
1218         switch (UVC_ENTITY_TYPE(entity)) {
1219         case UVC_VC_EXTENSION_UNIT:
1220                 if (uvc_trace_param & UVC_TRACE_PROBE)
1221                         printk(" <- XU %d", entity->id);
1222
1223                 if (entity->bNrInPins != 1) {
1224                         uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1225                                 "than 1 input pin.\n", entity->id);
1226                         return -1;
1227                 }
1228
1229                 break;
1230
1231         case UVC_VC_PROCESSING_UNIT:
1232                 if (uvc_trace_param & UVC_TRACE_PROBE)
1233                         printk(" <- PU %d", entity->id);
1234
1235                 if (chain->processing != NULL) {
1236                         uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1237                                 "Processing Units in chain.\n");
1238                         return -1;
1239                 }
1240
1241                 chain->processing = entity;
1242                 break;
1243
1244         case UVC_VC_SELECTOR_UNIT:
1245                 if (uvc_trace_param & UVC_TRACE_PROBE)
1246                         printk(" <- SU %d", entity->id);
1247
1248                 /* Single-input selector units are ignored. */
1249                 if (entity->bNrInPins == 1)
1250                         break;
1251
1252                 if (chain->selector != NULL) {
1253                         uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1254                                 "Units in chain.\n");
1255                         return -1;
1256                 }
1257
1258                 chain->selector = entity;
1259                 break;
1260
1261         case UVC_ITT_VENDOR_SPECIFIC:
1262         case UVC_ITT_CAMERA:
1263         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1264                 if (uvc_trace_param & UVC_TRACE_PROBE)
1265                         printk(" <- IT %d\n", entity->id);
1266
1267                 break;
1268
1269         case UVC_TT_STREAMING:
1270                 if (UVC_ENTITY_IS_ITERM(entity)) {
1271                         if (uvc_trace_param & UVC_TRACE_PROBE)
1272                                 printk(" <- IT %d\n", entity->id);
1273                 } else {
1274                         if (uvc_trace_param & UVC_TRACE_PROBE)
1275                                 printk(" OT %d", entity->id);
1276                 }
1277
1278                 break;
1279
1280         default:
1281                 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1282                         "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1283                 return -1;
1284         }
1285
1286         list_add_tail(&entity->chain, &chain->entities);
1287         return 0;
1288 }
1289
1290 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1291         struct uvc_entity *entity, struct uvc_entity *prev)
1292 {
1293         struct uvc_entity *forward;
1294         int found;
1295
1296         /* Forward scan */
1297         forward = NULL;
1298         found = 0;
1299
1300         while (1) {
1301                 forward = uvc_entity_by_reference(chain->dev, entity->id,
1302                         forward);
1303                 if (forward == NULL)
1304                         break;
1305                 if (forward == prev)
1306                         continue;
1307
1308                 switch (UVC_ENTITY_TYPE(forward)) {
1309                 case UVC_VC_EXTENSION_UNIT:
1310                         if (forward->bNrInPins != 1) {
1311                                 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1312                                           "has more than 1 input pin.\n",
1313                                           entity->id);
1314                                 return -EINVAL;
1315                         }
1316
1317                         list_add_tail(&forward->chain, &chain->entities);
1318                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1319                                 if (!found)
1320                                         printk(" (->");
1321
1322                                 printk(" XU %d", forward->id);
1323                                 found = 1;
1324                         }
1325                         break;
1326
1327                 case UVC_OTT_VENDOR_SPECIFIC:
1328                 case UVC_OTT_DISPLAY:
1329                 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1330                 case UVC_TT_STREAMING:
1331                         if (UVC_ENTITY_IS_ITERM(forward)) {
1332                                 uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1333                                         "terminal %u.\n", forward->id);
1334                                 return -EINVAL;
1335                         }
1336
1337                         list_add_tail(&forward->chain, &chain->entities);
1338                         if (uvc_trace_param & UVC_TRACE_PROBE) {
1339                                 if (!found)
1340                                         printk(" (->");
1341
1342                                 printk(" OT %d", forward->id);
1343                                 found = 1;
1344                         }
1345                         break;
1346                 }
1347         }
1348         if (found)
1349                 printk(")");
1350
1351         return 0;
1352 }
1353
1354 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1355         struct uvc_entity **_entity)
1356 {
1357         struct uvc_entity *entity = *_entity;
1358         struct uvc_entity *term;
1359         int id = -EINVAL, i;
1360
1361         switch (UVC_ENTITY_TYPE(entity)) {
1362         case UVC_VC_EXTENSION_UNIT:
1363         case UVC_VC_PROCESSING_UNIT:
1364                 id = entity->baSourceID[0];
1365                 break;
1366
1367         case UVC_VC_SELECTOR_UNIT:
1368                 /* Single-input selector units are ignored. */
1369                 if (entity->bNrInPins == 1) {
1370                         id = entity->baSourceID[0];
1371                         break;
1372                 }
1373
1374                 if (uvc_trace_param & UVC_TRACE_PROBE)
1375                         printk(" <- IT");
1376
1377                 chain->selector = entity;
1378                 for (i = 0; i < entity->bNrInPins; ++i) {
1379                         id = entity->baSourceID[i];
1380                         term = uvc_entity_by_id(chain->dev, id);
1381                         if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1382                                 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1383                                         "input %d isn't connected to an "
1384                                         "input terminal\n", entity->id, i);
1385                                 return -1;
1386                         }
1387
1388                         if (uvc_trace_param & UVC_TRACE_PROBE)
1389                                 printk(" %d", term->id);
1390
1391                         list_add_tail(&term->chain, &chain->entities);
1392                         uvc_scan_chain_forward(chain, term, entity);
1393                 }
1394
1395                 if (uvc_trace_param & UVC_TRACE_PROBE)
1396                         printk("\n");
1397
1398                 id = 0;
1399                 break;
1400
1401         case UVC_ITT_VENDOR_SPECIFIC:
1402         case UVC_ITT_CAMERA:
1403         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1404         case UVC_OTT_VENDOR_SPECIFIC:
1405         case UVC_OTT_DISPLAY:
1406         case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1407         case UVC_TT_STREAMING:
1408                 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1409                 break;
1410         }
1411
1412         if (id <= 0) {
1413                 *_entity = NULL;
1414                 return id;
1415         }
1416
1417         entity = uvc_entity_by_id(chain->dev, id);
1418         if (entity == NULL) {
1419                 uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1420                         "unknown entity %d.\n", id);
1421                 return -EINVAL;
1422         }
1423
1424         *_entity = entity;
1425         return 0;
1426 }
1427
1428 static int uvc_scan_chain(struct uvc_video_chain *chain,
1429                           struct uvc_entity *term)
1430 {
1431         struct uvc_entity *entity, *prev;
1432
1433         uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:");
1434
1435         entity = term;
1436         prev = NULL;
1437
1438         while (entity != NULL) {
1439                 /* Entity must not be part of an existing chain */
1440                 if (entity->chain.next || entity->chain.prev) {
1441                         uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1442                                 "entity %d already in chain.\n", entity->id);
1443                         return -EINVAL;
1444                 }
1445
1446                 /* Process entity */
1447                 if (uvc_scan_chain_entity(chain, entity) < 0)
1448                         return -EINVAL;
1449
1450                 /* Forward scan */
1451                 if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1452                         return -EINVAL;
1453
1454                 /* Backward scan */
1455                 prev = entity;
1456                 if (uvc_scan_chain_backward(chain, &entity) < 0)
1457                         return -EINVAL;
1458         }
1459
1460         return 0;
1461 }
1462
1463 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1464                 char *buffer)
1465 {
1466         struct uvc_entity *term;
1467         unsigned int nterms = 0;
1468         char *p = buffer;
1469
1470         list_for_each_entry(term, terms, chain) {
1471                 if (!UVC_ENTITY_IS_TERM(term) ||
1472                     UVC_TERM_DIRECTION(term) != dir)
1473                         continue;
1474
1475                 if (nterms)
1476                         p += sprintf(p, ",");
1477                 if (++nterms >= 4) {
1478                         p += sprintf(p, "...");
1479                         break;
1480                 }
1481                 p += sprintf(p, "%u", term->id);
1482         }
1483
1484         return p - buffer;
1485 }
1486
1487 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1488 {
1489         static char buffer[43];
1490         char *p = buffer;
1491
1492         p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1493         p += sprintf(p, " -> ");
1494         uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1495
1496         return buffer;
1497 }
1498
1499 /*
1500  * Scan the device for video chains and register video devices.
1501  *
1502  * Chains are scanned starting at their output terminals and walked backwards.
1503  */
1504 static int uvc_scan_device(struct uvc_device *dev)
1505 {
1506         struct uvc_video_chain *chain;
1507         struct uvc_entity *term;
1508
1509         list_for_each_entry(term, &dev->entities, list) {
1510                 if (!UVC_ENTITY_IS_OTERM(term))
1511                         continue;
1512
1513                 /* If the terminal is already included in a chain, skip it.
1514                  * This can happen for chains that have multiple output
1515                  * terminals, where all output terminals beside the first one
1516                  * will be inserted in the chain in forward scans.
1517                  */
1518                 if (term->chain.next || term->chain.prev)
1519                         continue;
1520
1521                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1522                 if (chain == NULL)
1523                         return -ENOMEM;
1524
1525                 INIT_LIST_HEAD(&chain->entities);
1526                 mutex_init(&chain->ctrl_mutex);
1527                 chain->dev = dev;
1528
1529                 if (uvc_scan_chain(chain, term) < 0) {
1530                         kfree(chain);
1531                         continue;
1532                 }
1533
1534                 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1535                           uvc_print_chain(chain));
1536
1537                 list_add_tail(&chain->list, &dev->chains);
1538         }
1539
1540         if (list_empty(&dev->chains)) {
1541                 uvc_printk(KERN_INFO, "No valid video chain found.\n");
1542                 return -1;
1543         }
1544
1545         return 0;
1546 }
1547
1548 /* ------------------------------------------------------------------------
1549  * Video device registration and unregistration
1550  */
1551
1552 /*
1553  * Delete the UVC device.
1554  *
1555  * Called by the kernel when the last reference to the uvc_device structure
1556  * is released.
1557  *
1558  * As this function is called after or during disconnect(), all URBs have
1559  * already been canceled by the USB core. There is no need to kill the
1560  * interrupt URB manually.
1561  */
1562 static void uvc_delete(struct uvc_device *dev)
1563 {
1564         struct list_head *p, *n;
1565
1566         usb_put_intf(dev->intf);
1567         usb_put_dev(dev->udev);
1568
1569         uvc_status_cleanup(dev);
1570         uvc_ctrl_cleanup_device(dev);
1571
1572         list_for_each_safe(p, n, &dev->chains) {
1573                 struct uvc_video_chain *chain;
1574                 chain = list_entry(p, struct uvc_video_chain, list);
1575                 kfree(chain);
1576         }
1577
1578         list_for_each_safe(p, n, &dev->entities) {
1579                 struct uvc_entity *entity;
1580                 entity = list_entry(p, struct uvc_entity, list);
1581                 kfree(entity);
1582         }
1583
1584         list_for_each_safe(p, n, &dev->streams) {
1585                 struct uvc_streaming *streaming;
1586                 streaming = list_entry(p, struct uvc_streaming, list);
1587                 usb_driver_release_interface(&uvc_driver.driver,
1588                         streaming->intf);
1589                 usb_put_intf(streaming->intf);
1590                 kfree(streaming->format);
1591                 kfree(streaming->header.bmaControls);
1592                 kfree(streaming);
1593         }
1594
1595         kfree(dev);
1596 }
1597
1598 static void uvc_release(struct video_device *vdev)
1599 {
1600         struct uvc_streaming *stream = video_get_drvdata(vdev);
1601         struct uvc_device *dev = stream->dev;
1602
1603         video_device_release(vdev);
1604
1605         /* Decrement the registered streams count and delete the device when it
1606          * reaches zero.
1607          */
1608         if (atomic_dec_and_test(&dev->nstreams))
1609                 uvc_delete(dev);
1610 }
1611
1612 /*
1613  * Unregister the video devices.
1614  */
1615 static void uvc_unregister_video(struct uvc_device *dev)
1616 {
1617         struct uvc_streaming *stream;
1618
1619         /* Unregistering all video devices might result in uvc_delete() being
1620          * called from inside the loop if there's no open file handle. To avoid
1621          * that, increment the stream count before iterating over the streams
1622          * and decrement it when done.
1623          */
1624         atomic_inc(&dev->nstreams);
1625
1626         list_for_each_entry(stream, &dev->streams, list) {
1627                 if (stream->vdev == NULL)
1628                         continue;
1629
1630                 video_unregister_device(stream->vdev);
1631                 stream->vdev = NULL;
1632         }
1633
1634         /* Decrement the stream count and call uvc_delete explicitly if there
1635          * are no stream left.
1636          */
1637         if (atomic_dec_and_test(&dev->nstreams))
1638                 uvc_delete(dev);
1639 }
1640
1641 static int uvc_register_video(struct uvc_device *dev,
1642                 struct uvc_streaming *stream)
1643 {
1644         struct video_device *vdev;
1645         int ret;
1646
1647         /* Initialize the streaming interface with default streaming
1648          * parameters.
1649          */
1650         ret = uvc_video_init(stream);
1651         if (ret < 0) {
1652                 uvc_printk(KERN_ERR, "Failed to initialize the device "
1653                         "(%d).\n", ret);
1654                 return ret;
1655         }
1656
1657         /* Register the device with V4L. */
1658         vdev = video_device_alloc();
1659         if (vdev == NULL) {
1660                 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1661                            ret);
1662                 return -ENOMEM;
1663         }
1664
1665         /* We already hold a reference to dev->udev. The video device will be
1666          * unregistered before the reference is released, so we don't need to
1667          * get another one.
1668          */
1669         vdev->parent = &dev->intf->dev;
1670         vdev->fops = &uvc_fops;
1671         vdev->release = uvc_release;
1672         strlcpy(vdev->name, dev->name, sizeof vdev->name);
1673
1674         /* Set the driver data before calling video_register_device, otherwise
1675          * uvc_v4l2_open might race us.
1676          */
1677         stream->vdev = vdev;
1678         video_set_drvdata(vdev, stream);
1679
1680         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1681         if (ret < 0) {
1682                 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1683                            ret);
1684                 stream->vdev = NULL;
1685                 video_device_release(vdev);
1686                 return ret;
1687         }
1688
1689         atomic_inc(&dev->nstreams);
1690         return 0;
1691 }
1692
1693 /*
1694  * Register all video devices in all chains.
1695  */
1696 static int uvc_register_terms(struct uvc_device *dev,
1697         struct uvc_video_chain *chain)
1698 {
1699         struct uvc_streaming *stream;
1700         struct uvc_entity *term;
1701         int ret;
1702
1703         list_for_each_entry(term, &chain->entities, chain) {
1704                 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1705                         continue;
1706
1707                 stream = uvc_stream_by_id(dev, term->id);
1708                 if (stream == NULL) {
1709                         uvc_printk(KERN_INFO, "No streaming interface found "
1710                                    "for terminal %u.", term->id);
1711                         continue;
1712                 }
1713
1714                 stream->chain = chain;
1715                 ret = uvc_register_video(dev, stream);
1716                 if (ret < 0)
1717                         return ret;
1718         }
1719
1720         return 0;
1721 }
1722
1723 static int uvc_register_chains(struct uvc_device *dev)
1724 {
1725         struct uvc_video_chain *chain;
1726         int ret;
1727
1728         list_for_each_entry(chain, &dev->chains, list) {
1729                 ret = uvc_register_terms(dev, chain);
1730                 if (ret < 0)
1731                         return ret;
1732         }
1733
1734         return 0;
1735 }
1736
1737 /* ------------------------------------------------------------------------
1738  * USB probe, disconnect, suspend and resume
1739  */
1740
1741 static int uvc_probe(struct usb_interface *intf,
1742                      const struct usb_device_id *id)
1743 {
1744         struct usb_device *udev = interface_to_usbdev(intf);
1745         struct uvc_device *dev;
1746         int ret;
1747
1748         if (id->idVendor && id->idProduct)
1749                 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1750                                 "(%04x:%04x)\n", udev->devpath, id->idVendor,
1751                                 id->idProduct);
1752         else
1753                 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1754                                 udev->devpath);
1755
1756         /* Allocate memory for the device and initialize it. */
1757         if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1758                 return -ENOMEM;
1759
1760         INIT_LIST_HEAD(&dev->entities);
1761         INIT_LIST_HEAD(&dev->chains);
1762         INIT_LIST_HEAD(&dev->streams);
1763         atomic_set(&dev->nstreams, 0);
1764         atomic_set(&dev->users, 0);
1765
1766         dev->udev = usb_get_dev(udev);
1767         dev->intf = usb_get_intf(intf);
1768         dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1769         dev->quirks = (uvc_quirks_param == -1)
1770                     ? id->driver_info : uvc_quirks_param;
1771
1772         if (udev->product != NULL)
1773                 strlcpy(dev->name, udev->product, sizeof dev->name);
1774         else
1775                 snprintf(dev->name, sizeof dev->name,
1776                         "UVC Camera (%04x:%04x)",
1777                         le16_to_cpu(udev->descriptor.idVendor),
1778                         le16_to_cpu(udev->descriptor.idProduct));
1779
1780         /* Parse the Video Class control descriptor. */
1781         if (uvc_parse_control(dev) < 0) {
1782                 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1783                         "descriptors.\n");
1784                 goto error;
1785         }
1786
1787         uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1788                 dev->uvc_version >> 8, dev->uvc_version & 0xff,
1789                 udev->product ? udev->product : "<unnamed>",
1790                 le16_to_cpu(udev->descriptor.idVendor),
1791                 le16_to_cpu(udev->descriptor.idProduct));
1792
1793         if (dev->quirks != id->driver_info) {
1794                 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module "
1795                         "parameter for testing purpose.\n", dev->quirks);
1796                 uvc_printk(KERN_INFO, "Please report required quirks to the "
1797                         "linux-uvc-devel mailing list.\n");
1798         }
1799
1800         /* Initialize controls. */
1801         if (uvc_ctrl_init_device(dev) < 0)
1802                 goto error;
1803
1804         /* Scan the device for video chains. */
1805         if (uvc_scan_device(dev) < 0)
1806                 goto error;
1807
1808         /* Register video devices. */
1809         if (uvc_register_chains(dev) < 0)
1810                 goto error;
1811
1812         /* Save our data pointer in the interface data. */
1813         usb_set_intfdata(intf, dev);
1814
1815         /* Initialize the interrupt URB. */
1816         if ((ret = uvc_status_init(dev)) < 0) {
1817                 uvc_printk(KERN_INFO, "Unable to initialize the status "
1818                         "endpoint (%d), status interrupt will not be "
1819                         "supported.\n", ret);
1820         }
1821
1822         uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
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         INIT_LIST_HEAD(&uvc_driver.devices);
2291         INIT_LIST_HEAD(&uvc_driver.controls);
2292         mutex_init(&uvc_driver.ctrl_mutex);
2293
2294         uvc_ctrl_init();
2295
2296         result = usb_register(&uvc_driver.driver);
2297         if (result == 0)
2298                 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2299         return result;
2300 }
2301
2302 static void __exit uvc_cleanup(void)
2303 {
2304         usb_deregister(&uvc_driver.driver);
2305         uvc_ctrl_cleanup();
2306 }
2307
2308 module_init(uvc_init);
2309 module_exit(uvc_cleanup);
2310
2311 MODULE_AUTHOR(DRIVER_AUTHOR);
2312 MODULE_DESCRIPTION(DRIVER_DESC);
2313 MODULE_LICENSE("GPL");
2314 MODULE_VERSION(DRIVER_VERSION);
2315