Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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