2 * uvc_driver.c -- USB Video Class driver
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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.
15 * This driver aims to support video input and ouput devices compliant with the
16 * 'USB Video Class' specification.
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.
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>
36 #include <media/v4l2-common.h>
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"
46 unsigned int uvc_no_drop_param
;
47 static unsigned int uvc_quirks_param
;
48 unsigned int uvc_trace_param
;
50 /* ------------------------------------------------------------------------
54 static struct uvc_format_desc uvc_fmts
[] = {
56 .name
= "YUV 4:2:2 (YUYV)",
57 .guid
= UVC_GUID_FORMAT_YUY2
,
58 .fcc
= V4L2_PIX_FMT_YUYV
,
61 .name
= "YUV 4:2:0 (NV12)",
62 .guid
= UVC_GUID_FORMAT_NV12
,
63 .fcc
= V4L2_PIX_FMT_NV12
,
67 .guid
= UVC_GUID_FORMAT_MJPEG
,
68 .fcc
= V4L2_PIX_FMT_MJPEG
,
71 .name
= "YVU 4:2:0 (YV12)",
72 .guid
= UVC_GUID_FORMAT_YV12
,
73 .fcc
= V4L2_PIX_FMT_YVU420
,
76 .name
= "YUV 4:2:0 (I420)",
77 .guid
= UVC_GUID_FORMAT_I420
,
78 .fcc
= V4L2_PIX_FMT_YUV420
,
81 .name
= "YUV 4:2:2 (UYVY)",
82 .guid
= UVC_GUID_FORMAT_UYVY
,
83 .fcc
= V4L2_PIX_FMT_UYVY
,
87 .guid
= UVC_GUID_FORMAT_Y800
,
88 .fcc
= V4L2_PIX_FMT_GREY
,
92 .guid
= UVC_GUID_FORMAT_BY8
,
93 .fcc
= V4L2_PIX_FMT_SBGGR8
,
97 /* ------------------------------------------------------------------------
101 struct usb_host_endpoint
*uvc_find_endpoint(struct usb_host_interface
*alts
,
104 struct usb_host_endpoint
*ep
;
107 for (i
= 0; i
< alts
->desc
.bNumEndpoints
; ++i
) {
108 ep
= &alts
->endpoint
[i
];
109 if (ep
->desc
.bEndpointAddress
== epaddr
)
116 static struct uvc_format_desc
*uvc_format_by_guid(const __u8 guid
[16])
118 unsigned int len
= ARRAY_SIZE(uvc_fmts
);
121 for (i
= 0; i
< len
; ++i
) {
122 if (memcmp(guid
, uvc_fmts
[i
].guid
, 16) == 0)
129 static __u32
uvc_colorspace(const __u8 primaries
)
131 static const __u8 colorprimaries
[] = {
133 V4L2_COLORSPACE_SRGB
,
134 V4L2_COLORSPACE_470_SYSTEM_M
,
135 V4L2_COLORSPACE_470_SYSTEM_BG
,
136 V4L2_COLORSPACE_SMPTE170M
,
137 V4L2_COLORSPACE_SMPTE240M
,
140 if (primaries
< ARRAY_SIZE(colorprimaries
))
141 return colorprimaries
[primaries
];
146 /* Simplify a fraction using a simple continued fraction decomposition. The
147 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
148 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
149 * arbitrary parameters to remove non-significative terms from the simple
150 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
151 * respectively seems to give nice results.
153 void uvc_simplify_fraction(uint32_t *numerator
, uint32_t *denominator
,
154 unsigned int n_terms
, unsigned int threshold
)
160 an
= kmalloc(n_terms
* sizeof *an
, GFP_KERNEL
);
164 /* Convert the fraction to a simple continued fraction. See
165 * http://mathforum.org/dr.math/faq/faq.fractions.html
166 * Stop if the current term is bigger than or equal to the given
172 for (n
= 0; n
< n_terms
&& y
!= 0; ++n
) {
174 if (an
[n
] >= threshold
) {
185 /* Expand the simple continued fraction back to an integer fraction. */
189 for (i
= n
; i
> 0; --i
) {
200 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
201 * to compute numerator / denominator * 10000000 using 32 bit fixed point
204 uint32_t uvc_fraction_to_interval(uint32_t numerator
, uint32_t denominator
)
208 /* Saturate the result if the operation would overflow. */
209 if (denominator
== 0 ||
210 numerator
/denominator
>= ((uint32_t)-1)/10000000)
213 /* Divide both the denominator and the multiplier by two until
214 * numerator * multiplier doesn't overflow. If anyone knows a better
215 * algorithm please let me know.
217 multiplier
= 10000000;
218 while (numerator
> ((uint32_t)-1)/multiplier
) {
223 return denominator
? numerator
* multiplier
/ denominator
: 0;
226 /* ------------------------------------------------------------------------
227 * Terminal and unit management
230 static struct uvc_entity
*uvc_entity_by_id(struct uvc_device
*dev
, int id
)
232 struct uvc_entity
*entity
;
234 list_for_each_entry(entity
, &dev
->entities
, list
) {
235 if (entity
->id
== id
)
242 static struct uvc_entity
*uvc_entity_by_reference(struct uvc_device
*dev
,
243 int id
, struct uvc_entity
*entity
)
248 entity
= list_entry(&dev
->entities
, struct uvc_entity
, list
);
250 list_for_each_entry_continue(entity
, &dev
->entities
, list
) {
251 switch (UVC_ENTITY_TYPE(entity
)) {
253 if (entity
->output
.bSourceID
== id
)
257 case VC_PROCESSING_UNIT
:
258 if (entity
->processing
.bSourceID
== id
)
262 case VC_SELECTOR_UNIT
:
263 for (i
= 0; i
< entity
->selector
.bNrInPins
; ++i
)
264 if (entity
->selector
.baSourceID
[i
] == id
)
268 case VC_EXTENSION_UNIT
:
269 for (i
= 0; i
< entity
->extension
.bNrInPins
; ++i
)
270 if (entity
->extension
.baSourceID
[i
] == id
)
279 /* ------------------------------------------------------------------------
280 * Descriptors handling
283 static int uvc_parse_format(struct uvc_device
*dev
,
284 struct uvc_streaming
*streaming
, struct uvc_format
*format
,
285 __u32
**intervals
, unsigned char *buffer
, int buflen
)
287 struct usb_interface
*intf
= streaming
->intf
;
288 struct usb_host_interface
*alts
= intf
->cur_altsetting
;
289 struct uvc_format_desc
*fmtdesc
;
290 struct uvc_frame
*frame
;
291 const unsigned char *start
= buffer
;
292 unsigned int interval
;
296 format
->type
= buffer
[2];
297 format
->index
= buffer
[3];
300 case VS_FORMAT_UNCOMPRESSED
:
301 case VS_FORMAT_FRAME_BASED
:
302 n
= buffer
[2] == VS_FORMAT_UNCOMPRESSED
? 27 : 28;
304 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
305 "interface %d FORMAT error\n",
307 alts
->desc
.bInterfaceNumber
);
311 /* Find the format descriptor from its GUID. */
312 fmtdesc
= uvc_format_by_guid(&buffer
[5]);
314 if (fmtdesc
!= NULL
) {
315 strlcpy(format
->name
, fmtdesc
->name
,
316 sizeof format
->name
);
317 format
->fcc
= fmtdesc
->fcc
;
319 uvc_printk(KERN_INFO
, "Unknown video format "
320 UVC_GUID_FORMAT
"\n",
321 UVC_GUID_ARGS(&buffer
[5]));
322 snprintf(format
->name
, sizeof format
->name
,
323 UVC_GUID_FORMAT
, UVC_GUID_ARGS(&buffer
[5]));
327 format
->bpp
= buffer
[21];
328 if (buffer
[2] == VS_FORMAT_UNCOMPRESSED
) {
329 ftype
= VS_FRAME_UNCOMPRESSED
;
331 ftype
= VS_FRAME_FRAME_BASED
;
333 format
->flags
= UVC_FMT_FLAG_COMPRESSED
;
337 case VS_FORMAT_MJPEG
:
339 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
340 "interface %d FORMAT error\n",
342 alts
->desc
.bInterfaceNumber
);
346 strlcpy(format
->name
, "MJPEG", sizeof format
->name
);
347 format
->fcc
= V4L2_PIX_FMT_MJPEG
;
348 format
->flags
= UVC_FMT_FLAG_COMPRESSED
;
350 ftype
= VS_FRAME_MJPEG
;
355 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
356 "interface %d FORMAT error\n",
358 alts
->desc
.bInterfaceNumber
);
362 switch (buffer
[8] & 0x7f) {
364 strlcpy(format
->name
, "SD-DV", sizeof format
->name
);
367 strlcpy(format
->name
, "SDL-DV", sizeof format
->name
);
370 strlcpy(format
->name
, "HD-DV", sizeof format
->name
);
373 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
374 "interface %d: unknown DV format %u\n",
376 alts
->desc
.bInterfaceNumber
, buffer
[8]);
380 strlcat(format
->name
, buffer
[8] & (1 << 7) ? " 60Hz" : " 50Hz",
381 sizeof format
->name
);
383 format
->fcc
= V4L2_PIX_FMT_DV
;
384 format
->flags
= UVC_FMT_FLAG_COMPRESSED
| UVC_FMT_FLAG_STREAM
;
388 /* Create a dummy frame descriptor. */
389 frame
= &format
->frame
[0];
390 memset(&format
->frame
[0], 0, sizeof format
->frame
[0]);
391 frame
->bFrameIntervalType
= 1;
392 frame
->dwDefaultFrameInterval
= 1;
393 frame
->dwFrameInterval
= *intervals
;
398 case VS_FORMAT_MPEG2TS
:
399 case VS_FORMAT_STREAM_BASED
:
400 /* Not supported yet. */
402 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
403 "interface %d unsupported format %u\n",
404 dev
->udev
->devnum
, alts
->desc
.bInterfaceNumber
,
409 uvc_trace(UVC_TRACE_DESCR
, "Found format %s.\n", format
->name
);
414 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
415 * based formats have frame descriptors.
417 while (buflen
> 2 && buffer
[2] == ftype
) {
418 frame
= &format
->frame
[format
->nframes
];
419 if (ftype
!= VS_FRAME_FRAME_BASED
)
420 n
= buflen
> 25 ? buffer
[25] : 0;
422 n
= buflen
> 21 ? buffer
[21] : 0;
426 if (buflen
< 26 + 4*n
) {
427 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
428 "interface %d FRAME error\n", dev
->udev
->devnum
,
429 alts
->desc
.bInterfaceNumber
);
433 frame
->bFrameIndex
= buffer
[3];
434 frame
->bmCapabilities
= buffer
[4];
435 frame
->wWidth
= get_unaligned_le16(&buffer
[5]);
436 frame
->wHeight
= get_unaligned_le16(&buffer
[7]);
437 frame
->dwMinBitRate
= get_unaligned_le32(&buffer
[9]);
438 frame
->dwMaxBitRate
= get_unaligned_le32(&buffer
[13]);
439 if (ftype
!= VS_FRAME_FRAME_BASED
) {
440 frame
->dwMaxVideoFrameBufferSize
=
441 get_unaligned_le32(&buffer
[17]);
442 frame
->dwDefaultFrameInterval
=
443 get_unaligned_le32(&buffer
[21]);
444 frame
->bFrameIntervalType
= buffer
[25];
446 frame
->dwMaxVideoFrameBufferSize
= 0;
447 frame
->dwDefaultFrameInterval
=
448 get_unaligned_le32(&buffer
[17]);
449 frame
->bFrameIntervalType
= buffer
[21];
451 frame
->dwFrameInterval
= *intervals
;
453 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
454 * completely. Observed behaviours range from setting the
455 * value to 1.1x the actual frame size to hardwiring the
456 * 16 low bits to 0. This results in a higher than necessary
457 * memory usage as well as a wrong image size information. For
458 * uncompressed formats this can be fixed by computing the
459 * value from the frame size.
461 if (!(format
->flags
& UVC_FMT_FLAG_COMPRESSED
))
462 frame
->dwMaxVideoFrameBufferSize
= format
->bpp
463 * frame
->wWidth
* frame
->wHeight
/ 8;
465 /* Some bogus devices report dwMinFrameInterval equal to
466 * dwMaxFrameInterval and have dwFrameIntervalStep set to
467 * zero. Setting all null intervals to 1 fixes the problem and
468 * some other divisions by zero that could happen.
470 for (i
= 0; i
< n
; ++i
) {
471 interval
= get_unaligned_le32(&buffer
[26+4*i
]);
472 *(*intervals
)++ = interval
? interval
: 1;
475 /* Make sure that the default frame interval stays between
478 n
-= frame
->bFrameIntervalType
? 1 : 2;
479 frame
->dwDefaultFrameInterval
=
480 min(frame
->dwFrameInterval
[n
],
481 max(frame
->dwFrameInterval
[0],
482 frame
->dwDefaultFrameInterval
));
484 uvc_trace(UVC_TRACE_DESCR
, "- %ux%u (%u.%u fps)\n",
485 frame
->wWidth
, frame
->wHeight
,
486 10000000/frame
->dwDefaultFrameInterval
,
487 (100000000/frame
->dwDefaultFrameInterval
)%10);
494 if (buflen
> 2 && buffer
[2] == VS_STILL_IMAGE_FRAME
) {
499 if (buflen
> 2 && buffer
[2] == VS_COLORFORMAT
) {
501 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
502 "interface %d COLORFORMAT error\n",
504 alts
->desc
.bInterfaceNumber
);
508 format
->colorspace
= uvc_colorspace(buffer
[3]);
514 return buffer
- start
;
517 static int uvc_parse_streaming(struct uvc_device
*dev
,
518 struct usb_interface
*intf
)
520 struct uvc_streaming
*streaming
= NULL
;
521 struct uvc_format
*format
;
522 struct uvc_frame
*frame
;
523 struct usb_host_interface
*alts
= &intf
->altsetting
[0];
524 unsigned char *_buffer
, *buffer
= alts
->extra
;
525 int _buflen
, buflen
= alts
->extralen
;
526 unsigned int nformats
= 0, nframes
= 0, nintervals
= 0;
527 unsigned int size
, i
, n
, p
;
532 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
533 != SC_VIDEOSTREAMING
) {
534 uvc_trace(UVC_TRACE_DESCR
, "device %d interface %d isn't a "
535 "video streaming interface\n", dev
->udev
->devnum
,
536 intf
->altsetting
[0].desc
.bInterfaceNumber
);
540 if (usb_driver_claim_interface(&uvc_driver
.driver
, intf
, dev
)) {
541 uvc_trace(UVC_TRACE_DESCR
, "device %d interface %d is already "
542 "claimed\n", dev
->udev
->devnum
,
543 intf
->altsetting
[0].desc
.bInterfaceNumber
);
547 streaming
= kzalloc(sizeof *streaming
, GFP_KERNEL
);
548 if (streaming
== NULL
) {
549 usb_driver_release_interface(&uvc_driver
.driver
, intf
);
553 mutex_init(&streaming
->mutex
);
554 streaming
->intf
= usb_get_intf(intf
);
555 streaming
->intfnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
557 /* The Pico iMage webcam has its class-specific interface descriptors
558 * after the endpoint descriptors.
561 for (i
= 0; i
< alts
->desc
.bNumEndpoints
; ++i
) {
562 struct usb_host_endpoint
*ep
= &alts
->endpoint
[i
];
564 if (ep
->extralen
== 0)
567 if (ep
->extralen
> 2 &&
568 ep
->extra
[1] == USB_DT_CS_INTERFACE
) {
569 uvc_trace(UVC_TRACE_DESCR
, "trying extra data "
570 "from endpoint %u.\n", i
);
571 buffer
= alts
->endpoint
[i
].extra
;
572 buflen
= alts
->endpoint
[i
].extralen
;
578 /* Skip the standard interface descriptors. */
579 while (buflen
> 2 && buffer
[1] != USB_DT_CS_INTERFACE
) {
585 uvc_trace(UVC_TRACE_DESCR
, "no class-specific streaming "
586 "interface descriptors found.\n");
590 /* Parse the header descriptor. */
592 case VS_OUTPUT_HEADER
:
593 streaming
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
597 case VS_INPUT_HEADER
:
598 streaming
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
603 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming interface "
604 "%d HEADER descriptor not found.\n", dev
->udev
->devnum
,
605 alts
->desc
.bInterfaceNumber
);
609 p
= buflen
>= 4 ? buffer
[3] : 0;
610 n
= buflen
>= size
? buffer
[size
-1] : 0;
612 if (buflen
< size
+ p
*n
) {
613 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
614 "interface %d HEADER descriptor is invalid.\n",
615 dev
->udev
->devnum
, alts
->desc
.bInterfaceNumber
);
619 streaming
->header
.bNumFormats
= p
;
620 streaming
->header
.bEndpointAddress
= buffer
[6];
621 if (buffer
[2] == VS_INPUT_HEADER
) {
622 streaming
->header
.bmInfo
= buffer
[7];
623 streaming
->header
.bTerminalLink
= buffer
[8];
624 streaming
->header
.bStillCaptureMethod
= buffer
[9];
625 streaming
->header
.bTriggerSupport
= buffer
[10];
626 streaming
->header
.bTriggerUsage
= buffer
[11];
628 streaming
->header
.bTerminalLink
= buffer
[7];
630 streaming
->header
.bControlSize
= n
;
632 streaming
->header
.bmaControls
= kmalloc(p
*n
, GFP_KERNEL
);
633 if (streaming
->header
.bmaControls
== NULL
) {
638 memcpy(streaming
->header
.bmaControls
, &buffer
[size
], p
*n
);
646 /* Count the format and frame descriptors. */
647 while (_buflen
> 2 && _buffer
[1] == CS_INTERFACE
) {
648 switch (_buffer
[2]) {
649 case VS_FORMAT_UNCOMPRESSED
:
650 case VS_FORMAT_MJPEG
:
651 case VS_FORMAT_FRAME_BASED
:
656 /* DV format has no frame descriptor. We will create a
657 * dummy frame descriptor with a dummy frame interval.
664 case VS_FORMAT_MPEG2TS
:
665 case VS_FORMAT_STREAM_BASED
:
666 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming "
667 "interface %d FORMAT %u is not supported.\n",
669 alts
->desc
.bInterfaceNumber
, _buffer
[2]);
672 case VS_FRAME_UNCOMPRESSED
:
676 nintervals
+= _buffer
[25] ? _buffer
[25] : 3;
679 case VS_FRAME_FRAME_BASED
:
682 nintervals
+= _buffer
[21] ? _buffer
[21] : 3;
686 _buflen
-= _buffer
[0];
687 _buffer
+= _buffer
[0];
691 uvc_trace(UVC_TRACE_DESCR
, "device %d videostreaming interface "
692 "%d has no supported formats defined.\n",
693 dev
->udev
->devnum
, alts
->desc
.bInterfaceNumber
);
697 size
= nformats
* sizeof *format
+ nframes
* sizeof *frame
698 + nintervals
* sizeof *interval
;
699 format
= kzalloc(size
, GFP_KERNEL
);
700 if (format
== NULL
) {
705 frame
= (struct uvc_frame
*)&format
[nformats
];
706 interval
= (__u32
*)&frame
[nframes
];
708 streaming
->format
= format
;
709 streaming
->nformats
= nformats
;
711 /* Parse the format descriptors. */
712 while (buflen
> 2 && buffer
[1] == CS_INTERFACE
) {
714 case VS_FORMAT_UNCOMPRESSED
:
715 case VS_FORMAT_MJPEG
:
717 case VS_FORMAT_FRAME_BASED
:
718 format
->frame
= frame
;
719 ret
= uvc_parse_format(dev
, streaming
, format
,
720 &interval
, buffer
, buflen
);
724 frame
+= format
->nframes
;
739 /* Parse the alternate settings to find the maximum bandwidth. */
740 for (i
= 0; i
< intf
->num_altsetting
; ++i
) {
741 struct usb_host_endpoint
*ep
;
742 alts
= &intf
->altsetting
[i
];
743 ep
= uvc_find_endpoint(alts
,
744 streaming
->header
.bEndpointAddress
);
748 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
749 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
750 if (psize
> streaming
->maxpsize
)
751 streaming
->maxpsize
= psize
;
754 list_add_tail(&streaming
->list
, &dev
->streaming
);
758 usb_driver_release_interface(&uvc_driver
.driver
, intf
);
760 kfree(streaming
->format
);
761 kfree(streaming
->header
.bmaControls
);
766 /* Parse vendor-specific extensions. */
767 static int uvc_parse_vendor_control(struct uvc_device
*dev
,
768 const unsigned char *buffer
, int buflen
)
770 struct usb_device
*udev
= dev
->udev
;
771 struct usb_host_interface
*alts
= dev
->intf
->cur_altsetting
;
772 struct uvc_entity
*unit
;
776 switch (le16_to_cpu(dev
->udev
->descriptor
.idVendor
)) {
777 case 0x046d: /* Logitech */
778 if (buffer
[1] != 0x41 || buffer
[2] != 0x01)
781 /* Logitech implements several vendor specific functions
782 * through vendor specific extension units (LXU).
784 * The LXU descriptors are similar to XU descriptors
785 * (see "USB Device Video Class for Video Devices", section
786 * 3.7.2.6 "Extension Unit Descriptor") with the following
789 * ----------------------------------------------------------
791 * Size of this descriptor, in bytes: 24+p+n*2
792 * ----------------------------------------------------------
793 * 23+p+n bmControlsType N Bitmap
794 * Individual bits in the set are defined:
798 * This bitset is mapped exactly the same as bmControls.
799 * ----------------------------------------------------------
800 * 23+p+n*2 bReserved 1 Boolean
801 * ----------------------------------------------------------
802 * 24+p+n*2 iExtension 1 Index
803 * Index of a string descriptor that describes this
805 * ----------------------------------------------------------
807 p
= buflen
>= 22 ? buffer
[21] : 0;
808 n
= buflen
>= 25 + p
? buffer
[22+p
] : 0;
810 if (buflen
< 25 + p
+ 2*n
) {
811 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
812 "interface %d EXTENSION_UNIT error\n",
813 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
817 unit
= kzalloc(sizeof *unit
+ p
+ 2*n
, GFP_KERNEL
);
821 unit
->id
= buffer
[3];
822 unit
->type
= VC_EXTENSION_UNIT
;
823 memcpy(unit
->extension
.guidExtensionCode
, &buffer
[4], 16);
824 unit
->extension
.bNumControls
= buffer
[20];
825 unit
->extension
.bNrInPins
= get_unaligned_le16(&buffer
[21]);
826 unit
->extension
.baSourceID
= (__u8
*)unit
+ sizeof *unit
;
827 memcpy(unit
->extension
.baSourceID
, &buffer
[22], p
);
828 unit
->extension
.bControlSize
= buffer
[22+p
];
829 unit
->extension
.bmControls
= (__u8
*)unit
+ sizeof *unit
+ p
;
830 unit
->extension
.bmControlsType
= (__u8
*)unit
+ sizeof *unit
832 memcpy(unit
->extension
.bmControls
, &buffer
[23+p
], 2*n
);
834 if (buffer
[24+p
+2*n
] != 0)
835 usb_string(udev
, buffer
[24+p
+2*n
], unit
->name
,
838 sprintf(unit
->name
, "Extension %u", buffer
[3]);
840 list_add_tail(&unit
->list
, &dev
->entities
);
848 static int uvc_parse_standard_control(struct uvc_device
*dev
,
849 const unsigned char *buffer
, int buflen
)
851 struct usb_device
*udev
= dev
->udev
;
852 struct uvc_entity
*unit
, *term
;
853 struct usb_interface
*intf
;
854 struct usb_host_interface
*alts
= dev
->intf
->cur_altsetting
;
855 unsigned int i
, n
, p
, len
;
860 n
= buflen
>= 12 ? buffer
[11] : 0;
862 if (buflen
< 12 || buflen
< 12 + n
) {
863 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
864 "interface %d HEADER error\n", udev
->devnum
,
865 alts
->desc
.bInterfaceNumber
);
869 dev
->uvc_version
= get_unaligned_le16(&buffer
[3]);
870 dev
->clock_frequency
= get_unaligned_le32(&buffer
[7]);
872 /* Parse all USB Video Streaming interfaces. */
873 for (i
= 0; i
< n
; ++i
) {
874 intf
= usb_ifnum_to_if(udev
, buffer
[12+i
]);
876 uvc_trace(UVC_TRACE_DESCR
, "device %d "
877 "interface %d doesn't exists\n",
882 uvc_parse_streaming(dev
, intf
);
886 case VC_INPUT_TERMINAL
:
888 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
889 "interface %d INPUT_TERMINAL error\n",
890 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
894 /* Make sure the terminal type MSB is not null, otherwise it
895 * could be confused with a unit.
897 type
= get_unaligned_le16(&buffer
[4]);
898 if ((type
& 0xff00) == 0) {
899 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
900 "interface %d INPUT_TERMINAL %d has invalid "
901 "type 0x%04x, skipping\n", udev
->devnum
,
902 alts
->desc
.bInterfaceNumber
,
911 if (type
== ITT_CAMERA
) {
912 n
= buflen
>= 15 ? buffer
[14] : 0;
915 } else if (type
== ITT_MEDIA_TRANSPORT_INPUT
) {
916 n
= buflen
>= 9 ? buffer
[8] : 0;
917 p
= buflen
>= 10 + n
? buffer
[9+n
] : 0;
921 if (buflen
< len
+ n
+ p
) {
922 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
923 "interface %d INPUT_TERMINAL error\n",
924 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
928 term
= kzalloc(sizeof *term
+ n
+ p
, GFP_KERNEL
);
932 term
->id
= buffer
[3];
933 term
->type
= type
| UVC_TERM_INPUT
;
935 if (UVC_ENTITY_TYPE(term
) == ITT_CAMERA
) {
936 term
->camera
.bControlSize
= n
;
937 term
->camera
.bmControls
= (__u8
*)term
+ sizeof *term
;
938 term
->camera
.wObjectiveFocalLengthMin
=
939 get_unaligned_le16(&buffer
[8]);
940 term
->camera
.wObjectiveFocalLengthMax
=
941 get_unaligned_le16(&buffer
[10]);
942 term
->camera
.wOcularFocalLength
=
943 get_unaligned_le16(&buffer
[12]);
944 memcpy(term
->camera
.bmControls
, &buffer
[15], n
);
945 } else if (UVC_ENTITY_TYPE(term
) == ITT_MEDIA_TRANSPORT_INPUT
) {
946 term
->media
.bControlSize
= n
;
947 term
->media
.bmControls
= (__u8
*)term
+ sizeof *term
;
948 term
->media
.bTransportModeSize
= p
;
949 term
->media
.bmTransportModes
= (__u8
*)term
951 memcpy(term
->media
.bmControls
, &buffer
[9], n
);
952 memcpy(term
->media
.bmTransportModes
, &buffer
[10+n
], p
);
956 usb_string(udev
, buffer
[7], term
->name
,
958 else if (UVC_ENTITY_TYPE(term
) == ITT_CAMERA
)
959 sprintf(term
->name
, "Camera %u", buffer
[3]);
960 else if (UVC_ENTITY_TYPE(term
) == ITT_MEDIA_TRANSPORT_INPUT
)
961 sprintf(term
->name
, "Media %u", buffer
[3]);
963 sprintf(term
->name
, "Input %u", buffer
[3]);
965 list_add_tail(&term
->list
, &dev
->entities
);
968 case VC_OUTPUT_TERMINAL
:
970 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
971 "interface %d OUTPUT_TERMINAL error\n",
972 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
976 /* Make sure the terminal type MSB is not null, otherwise it
977 * could be confused with a unit.
979 type
= get_unaligned_le16(&buffer
[4]);
980 if ((type
& 0xff00) == 0) {
981 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
982 "interface %d OUTPUT_TERMINAL %d has invalid "
983 "type 0x%04x, skipping\n", udev
->devnum
,
984 alts
->desc
.bInterfaceNumber
, buffer
[3], type
);
988 term
= kzalloc(sizeof *term
, GFP_KERNEL
);
992 term
->id
= buffer
[3];
993 term
->type
= type
| UVC_TERM_OUTPUT
;
994 term
->output
.bSourceID
= buffer
[7];
997 usb_string(udev
, buffer
[8], term
->name
,
1000 sprintf(term
->name
, "Output %u", buffer
[3]);
1002 list_add_tail(&term
->list
, &dev
->entities
);
1005 case VC_SELECTOR_UNIT
:
1006 p
= buflen
>= 5 ? buffer
[4] : 0;
1008 if (buflen
< 5 || buflen
< 6 + p
) {
1009 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
1010 "interface %d SELECTOR_UNIT error\n",
1011 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
1015 unit
= kzalloc(sizeof *unit
+ p
, GFP_KERNEL
);
1019 unit
->id
= buffer
[3];
1020 unit
->type
= buffer
[2];
1021 unit
->selector
.bNrInPins
= buffer
[4];
1022 unit
->selector
.baSourceID
= (__u8
*)unit
+ sizeof *unit
;
1023 memcpy(unit
->selector
.baSourceID
, &buffer
[5], p
);
1025 if (buffer
[5+p
] != 0)
1026 usb_string(udev
, buffer
[5+p
], unit
->name
,
1029 sprintf(unit
->name
, "Selector %u", buffer
[3]);
1031 list_add_tail(&unit
->list
, &dev
->entities
);
1034 case VC_PROCESSING_UNIT
:
1035 n
= buflen
>= 8 ? buffer
[7] : 0;
1036 p
= dev
->uvc_version
>= 0x0110 ? 10 : 9;
1038 if (buflen
< p
+ n
) {
1039 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
1040 "interface %d PROCESSING_UNIT error\n",
1041 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
1045 unit
= kzalloc(sizeof *unit
+ n
, GFP_KERNEL
);
1049 unit
->id
= buffer
[3];
1050 unit
->type
= buffer
[2];
1051 unit
->processing
.bSourceID
= buffer
[4];
1052 unit
->processing
.wMaxMultiplier
=
1053 get_unaligned_le16(&buffer
[5]);
1054 unit
->processing
.bControlSize
= buffer
[7];
1055 unit
->processing
.bmControls
= (__u8
*)unit
+ sizeof *unit
;
1056 memcpy(unit
->processing
.bmControls
, &buffer
[8], n
);
1057 if (dev
->uvc_version
>= 0x0110)
1058 unit
->processing
.bmVideoStandards
= buffer
[9+n
];
1060 if (buffer
[8+n
] != 0)
1061 usb_string(udev
, buffer
[8+n
], unit
->name
,
1064 sprintf(unit
->name
, "Processing %u", buffer
[3]);
1066 list_add_tail(&unit
->list
, &dev
->entities
);
1069 case VC_EXTENSION_UNIT
:
1070 p
= buflen
>= 22 ? buffer
[21] : 0;
1071 n
= buflen
>= 24 + p
? buffer
[22+p
] : 0;
1073 if (buflen
< 24 + p
+ n
) {
1074 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
1075 "interface %d EXTENSION_UNIT error\n",
1076 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
1080 unit
= kzalloc(sizeof *unit
+ p
+ n
, GFP_KERNEL
);
1084 unit
->id
= buffer
[3];
1085 unit
->type
= buffer
[2];
1086 memcpy(unit
->extension
.guidExtensionCode
, &buffer
[4], 16);
1087 unit
->extension
.bNumControls
= buffer
[20];
1088 unit
->extension
.bNrInPins
= get_unaligned_le16(&buffer
[21]);
1089 unit
->extension
.baSourceID
= (__u8
*)unit
+ sizeof *unit
;
1090 memcpy(unit
->extension
.baSourceID
, &buffer
[22], p
);
1091 unit
->extension
.bControlSize
= buffer
[22+p
];
1092 unit
->extension
.bmControls
= (__u8
*)unit
+ sizeof *unit
+ p
;
1093 memcpy(unit
->extension
.bmControls
, &buffer
[23+p
], n
);
1095 if (buffer
[23+p
+n
] != 0)
1096 usb_string(udev
, buffer
[23+p
+n
], unit
->name
,
1099 sprintf(unit
->name
, "Extension %u", buffer
[3]);
1101 list_add_tail(&unit
->list
, &dev
->entities
);
1105 uvc_trace(UVC_TRACE_DESCR
, "Found an unknown CS_INTERFACE "
1106 "descriptor (%u)\n", buffer
[2]);
1113 static int uvc_parse_control(struct uvc_device
*dev
)
1115 struct usb_host_interface
*alts
= dev
->intf
->cur_altsetting
;
1116 unsigned char *buffer
= alts
->extra
;
1117 int buflen
= alts
->extralen
;
1120 /* Parse the default alternate setting only, as the UVC specification
1121 * defines a single alternate setting, the default alternate setting
1125 while (buflen
> 2) {
1126 if (uvc_parse_vendor_control(dev
, buffer
, buflen
) ||
1127 buffer
[1] != USB_DT_CS_INTERFACE
)
1128 goto next_descriptor
;
1130 if ((ret
= uvc_parse_standard_control(dev
, buffer
, buflen
)) < 0)
1134 buflen
-= buffer
[0];
1135 buffer
+= buffer
[0];
1138 /* Check if the optional status endpoint is present. Built-in iSight
1139 * webcams have an interrupt endpoint but spit proprietary data that
1140 * don't conform to the UVC status endpoint messages. Don't try to
1141 * handle the interrupt endpoint for those cameras.
1143 if (alts
->desc
.bNumEndpoints
== 1 &&
1144 !(dev
->quirks
& UVC_QUIRK_BUILTIN_ISIGHT
)) {
1145 struct usb_host_endpoint
*ep
= &alts
->endpoint
[0];
1146 struct usb_endpoint_descriptor
*desc
= &ep
->desc
;
1148 if (usb_endpoint_is_int_in(desc
) &&
1149 le16_to_cpu(desc
->wMaxPacketSize
) >= 8 &&
1150 desc
->bInterval
!= 0) {
1151 uvc_trace(UVC_TRACE_DESCR
, "Found a Status endpoint "
1152 "(addr %02x).\n", desc
->bEndpointAddress
);
1160 /* ------------------------------------------------------------------------
1161 * USB probe and disconnect
1165 * Unregister the video devices.
1167 static void uvc_unregister_video(struct uvc_device
*dev
)
1169 if (dev
->video
.vdev
) {
1170 if (dev
->video
.vdev
->minor
== -1)
1171 video_device_release(dev
->video
.vdev
);
1173 video_unregister_device(dev
->video
.vdev
);
1174 dev
->video
.vdev
= NULL
;
1179 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1180 * and containing the following units:
1182 * - one Output Terminal (USB Streaming or Display)
1183 * - zero or one Processing Unit
1184 * - zero, one or mode single-input Selector Units
1185 * - zero or one multiple-input Selector Units, provided all inputs are
1186 * connected to input terminals
1187 * - zero, one or mode single-input Extension Units
1188 * - one or more Input Terminals (Camera, External or USB Streaming)
1190 * A side forward scan is made on each detected entity to check for additional
1193 static int uvc_scan_chain_entity(struct uvc_video_device
*video
,
1194 struct uvc_entity
*entity
)
1196 switch (UVC_ENTITY_TYPE(entity
)) {
1197 case VC_EXTENSION_UNIT
:
1198 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1199 printk(" <- XU %d", entity
->id
);
1201 if (entity
->extension
.bNrInPins
!= 1) {
1202 uvc_trace(UVC_TRACE_DESCR
, "Extension unit %d has more "
1203 "than 1 input pin.\n", entity
->id
);
1207 list_add_tail(&entity
->chain
, &video
->extensions
);
1210 case VC_PROCESSING_UNIT
:
1211 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1212 printk(" <- PU %d", entity
->id
);
1214 if (video
->processing
!= NULL
) {
1215 uvc_trace(UVC_TRACE_DESCR
, "Found multiple "
1216 "Processing Units in chain.\n");
1220 video
->processing
= entity
;
1223 case VC_SELECTOR_UNIT
:
1224 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1225 printk(" <- SU %d", entity
->id
);
1227 /* Single-input selector units are ignored. */
1228 if (entity
->selector
.bNrInPins
== 1)
1231 if (video
->selector
!= NULL
) {
1232 uvc_trace(UVC_TRACE_DESCR
, "Found multiple Selector "
1233 "Units in chain.\n");
1237 video
->selector
= entity
;
1240 case ITT_VENDOR_SPECIFIC
:
1242 case ITT_MEDIA_TRANSPORT_INPUT
:
1243 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1244 printk(" <- IT %d\n", entity
->id
);
1246 list_add_tail(&entity
->chain
, &video
->iterms
);
1250 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1251 printk(" <- IT %d\n", entity
->id
);
1253 if (!UVC_ENTITY_IS_ITERM(entity
)) {
1254 uvc_trace(UVC_TRACE_DESCR
, "Unsupported input "
1255 "terminal %u.\n", entity
->id
);
1259 if (video
->sterm
!= NULL
) {
1260 uvc_trace(UVC_TRACE_DESCR
, "Found multiple streaming "
1261 "entities in chain.\n");
1265 list_add_tail(&entity
->chain
, &video
->iterms
);
1266 video
->sterm
= entity
;
1270 uvc_trace(UVC_TRACE_DESCR
, "Unsupported entity type "
1271 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity
));
1278 static int uvc_scan_chain_forward(struct uvc_video_device
*video
,
1279 struct uvc_entity
*entity
, struct uvc_entity
*prev
)
1281 struct uvc_entity
*forward
;
1289 forward
= uvc_entity_by_reference(video
->dev
, entity
->id
,
1291 if (forward
== NULL
)
1294 if (UVC_ENTITY_TYPE(forward
) != VC_EXTENSION_UNIT
||
1298 if (forward
->extension
.bNrInPins
!= 1) {
1299 uvc_trace(UVC_TRACE_DESCR
, "Extension unit %d has "
1300 "more than 1 input pin.\n", entity
->id
);
1304 list_add_tail(&forward
->chain
, &video
->extensions
);
1305 if (uvc_trace_param
& UVC_TRACE_PROBE
) {
1309 printk(" %d", forward
->id
);
1319 static int uvc_scan_chain_backward(struct uvc_video_device
*video
,
1320 struct uvc_entity
*entity
)
1322 struct uvc_entity
*term
;
1325 switch (UVC_ENTITY_TYPE(entity
)) {
1326 case VC_EXTENSION_UNIT
:
1327 id
= entity
->extension
.baSourceID
[0];
1330 case VC_PROCESSING_UNIT
:
1331 id
= entity
->processing
.bSourceID
;
1334 case VC_SELECTOR_UNIT
:
1335 /* Single-input selector units are ignored. */
1336 if (entity
->selector
.bNrInPins
== 1) {
1337 id
= entity
->selector
.baSourceID
[0];
1341 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1344 video
->selector
= entity
;
1345 for (i
= 0; i
< entity
->selector
.bNrInPins
; ++i
) {
1346 id
= entity
->selector
.baSourceID
[i
];
1347 term
= uvc_entity_by_id(video
->dev
, id
);
1348 if (term
== NULL
|| !UVC_ENTITY_IS_ITERM(term
)) {
1349 uvc_trace(UVC_TRACE_DESCR
, "Selector unit %d "
1350 "input %d isn't connected to an "
1351 "input terminal\n", entity
->id
, i
);
1355 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1356 printk(" %d", term
->id
);
1358 list_add_tail(&term
->chain
, &video
->iterms
);
1359 uvc_scan_chain_forward(video
, term
, entity
);
1362 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1372 static int uvc_scan_chain(struct uvc_video_device
*video
)
1374 struct uvc_entity
*entity
, *prev
;
1377 entity
= video
->oterm
;
1378 uvc_trace(UVC_TRACE_PROBE
, "Scanning UVC chain: OT %d", entity
->id
);
1380 if (UVC_ENTITY_TYPE(entity
) == TT_STREAMING
)
1381 video
->sterm
= entity
;
1383 id
= entity
->output
.bSourceID
;
1386 entity
= uvc_entity_by_id(video
->dev
, id
);
1387 if (entity
== NULL
) {
1388 uvc_trace(UVC_TRACE_DESCR
, "Found reference to "
1389 "unknown entity %d.\n", id
);
1393 /* Process entity */
1394 if (uvc_scan_chain_entity(video
, entity
) < 0)
1398 if (uvc_scan_chain_forward(video
, entity
, prev
) < 0)
1401 /* Stop when a terminal is found. */
1402 if (!UVC_ENTITY_IS_UNIT(entity
))
1406 id
= uvc_scan_chain_backward(video
, entity
);
1411 if (video
->sterm
== NULL
) {
1412 uvc_trace(UVC_TRACE_DESCR
, "No streaming entity found in "
1421 * Register the video devices.
1423 * The driver currently supports a single video device per control interface
1424 * only. The terminal and units must match the following structure:
1426 * ITT_* -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1427 * TT_STREAMING -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> OTT_*
1429 * The Extension Units, if present, must have a single input pin. The
1430 * Processing Unit and Extension Units can be in any order. Additional
1431 * Extension Units connected to the main chain as single-unit branches are
1434 static int uvc_register_video(struct uvc_device
*dev
)
1436 struct video_device
*vdev
;
1437 struct uvc_entity
*term
;
1440 /* Check if the control interface matches the structure we expect. */
1441 list_for_each_entry(term
, &dev
->entities
, list
) {
1442 struct uvc_streaming
*streaming
;
1444 if (!UVC_ENTITY_IS_TERM(term
) || !UVC_ENTITY_IS_OTERM(term
))
1447 memset(&dev
->video
, 0, sizeof dev
->video
);
1448 mutex_init(&dev
->video
.ctrl_mutex
);
1449 INIT_LIST_HEAD(&dev
->video
.iterms
);
1450 INIT_LIST_HEAD(&dev
->video
.extensions
);
1451 dev
->video
.oterm
= term
;
1452 dev
->video
.dev
= dev
;
1453 if (uvc_scan_chain(&dev
->video
) < 0)
1456 list_for_each_entry(streaming
, &dev
->streaming
, list
) {
1457 if (streaming
->header
.bTerminalLink
==
1458 dev
->video
.sterm
->id
) {
1459 dev
->video
.streaming
= streaming
;
1470 uvc_printk(KERN_INFO
, "No valid video chain found.\n");
1474 if (uvc_trace_param
& UVC_TRACE_PROBE
) {
1475 uvc_printk(KERN_INFO
, "Found a valid video chain (");
1476 list_for_each_entry(term
, &dev
->video
.iterms
, chain
) {
1477 printk("%d", term
->id
);
1478 if (term
->chain
.next
!= &dev
->video
.iterms
)
1481 printk(" -> %d).\n", dev
->video
.oterm
->id
);
1484 /* Initialize the video buffers queue. */
1485 uvc_queue_init(&dev
->video
.queue
, dev
->video
.streaming
->type
);
1487 /* Initialize the streaming interface with default streaming
1490 if ((ret
= uvc_video_init(&dev
->video
)) < 0) {
1491 uvc_printk(KERN_ERR
, "Failed to initialize the device "
1496 /* Register the device with V4L. */
1497 vdev
= video_device_alloc();
1501 /* We already hold a reference to dev->udev. The video device will be
1502 * unregistered before the reference is released, so we don't need to
1505 vdev
->parent
= &dev
->intf
->dev
;
1507 vdev
->fops
= &uvc_fops
;
1508 vdev
->release
= video_device_release
;
1509 strlcpy(vdev
->name
, dev
->name
, sizeof vdev
->name
);
1511 /* Set the driver data before calling video_register_device, otherwise
1512 * uvc_v4l2_open might race us.
1514 dev
->video
.vdev
= vdev
;
1515 video_set_drvdata(vdev
, &dev
->video
);
1517 if (video_register_device(vdev
, VFL_TYPE_GRABBER
, -1) < 0) {
1518 dev
->video
.vdev
= NULL
;
1519 video_device_release(vdev
);
1527 * Delete the UVC device.
1529 * Called by the kernel when the last reference to the uvc_device structure
1532 * Unregistering the video devices is done here because every opened instance
1533 * must be closed before the device can be unregistered. An alternative would
1534 * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1535 * unregister the video devices on disconnect when that reference count drops
1538 * As this function is called after or during disconnect(), all URBs have
1539 * already been canceled by the USB core. There is no need to kill the
1540 * interrupt URB manually.
1542 void uvc_delete(struct kref
*kref
)
1544 struct uvc_device
*dev
= container_of(kref
, struct uvc_device
, kref
);
1545 struct list_head
*p
, *n
;
1547 /* Unregister the video device. */
1548 uvc_unregister_video(dev
);
1549 usb_put_intf(dev
->intf
);
1550 usb_put_dev(dev
->udev
);
1552 uvc_status_cleanup(dev
);
1553 uvc_ctrl_cleanup_device(dev
);
1555 list_for_each_safe(p
, n
, &dev
->entities
) {
1556 struct uvc_entity
*entity
;
1557 entity
= list_entry(p
, struct uvc_entity
, list
);
1561 list_for_each_safe(p
, n
, &dev
->streaming
) {
1562 struct uvc_streaming
*streaming
;
1563 streaming
= list_entry(p
, struct uvc_streaming
, list
);
1564 usb_driver_release_interface(&uvc_driver
.driver
,
1566 usb_put_intf(streaming
->intf
);
1567 kfree(streaming
->format
);
1568 kfree(streaming
->header
.bmaControls
);
1575 static int uvc_probe(struct usb_interface
*intf
,
1576 const struct usb_device_id
*id
)
1578 struct usb_device
*udev
= interface_to_usbdev(intf
);
1579 struct uvc_device
*dev
;
1582 if (id
->idVendor
&& id
->idProduct
)
1583 uvc_trace(UVC_TRACE_PROBE
, "Probing known UVC device %s "
1584 "(%04x:%04x)\n", udev
->devpath
, id
->idVendor
,
1587 uvc_trace(UVC_TRACE_PROBE
, "Probing generic UVC device %s\n",
1590 /* Allocate memory for the device and initialize it. */
1591 if ((dev
= kzalloc(sizeof *dev
, GFP_KERNEL
)) == NULL
)
1594 INIT_LIST_HEAD(&dev
->entities
);
1595 INIT_LIST_HEAD(&dev
->streaming
);
1596 kref_init(&dev
->kref
);
1597 atomic_set(&dev
->users
, 0);
1599 dev
->udev
= usb_get_dev(udev
);
1600 dev
->intf
= usb_get_intf(intf
);
1601 dev
->intfnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
1602 dev
->quirks
= id
->driver_info
| uvc_quirks_param
;
1604 if (udev
->product
!= NULL
)
1605 strlcpy(dev
->name
, udev
->product
, sizeof dev
->name
);
1607 snprintf(dev
->name
, sizeof dev
->name
,
1608 "UVC Camera (%04x:%04x)",
1609 le16_to_cpu(udev
->descriptor
.idVendor
),
1610 le16_to_cpu(udev
->descriptor
.idProduct
));
1612 /* Parse the Video Class control descriptor. */
1613 if (uvc_parse_control(dev
) < 0) {
1614 uvc_trace(UVC_TRACE_PROBE
, "Unable to parse UVC "
1619 uvc_printk(KERN_INFO
, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1620 dev
->uvc_version
>> 8, dev
->uvc_version
& 0xff,
1621 udev
->product
? udev
->product
: "<unnamed>",
1622 le16_to_cpu(udev
->descriptor
.idVendor
),
1623 le16_to_cpu(udev
->descriptor
.idProduct
));
1625 if (uvc_quirks_param
!= 0) {
1626 uvc_printk(KERN_INFO
, "Forcing device quirks 0x%x by module "
1627 "parameter for testing purpose.\n", uvc_quirks_param
);
1628 uvc_printk(KERN_INFO
, "Please report required quirks to the "
1629 "linux-uvc-devel mailing list.\n");
1632 /* Initialize controls. */
1633 if (uvc_ctrl_init_device(dev
) < 0)
1636 /* Register the video devices. */
1637 if (uvc_register_video(dev
) < 0)
1640 /* Save our data pointer in the interface data. */
1641 usb_set_intfdata(intf
, dev
);
1643 /* Initialize the interrupt URB. */
1644 if ((ret
= uvc_status_init(dev
)) < 0) {
1645 uvc_printk(KERN_INFO
, "Unable to initialize the status "
1646 "endpoint (%d), status interrupt will not be "
1647 "supported.\n", ret
);
1650 uvc_trace(UVC_TRACE_PROBE
, "UVC device initialized.\n");
1654 kref_put(&dev
->kref
, uvc_delete
);
1658 static void uvc_disconnect(struct usb_interface
*intf
)
1660 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1662 /* Set the USB interface data to NULL. This can be done outside the
1663 * lock, as there's no other reader.
1665 usb_set_intfdata(intf
, NULL
);
1667 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
== SC_VIDEOSTREAMING
)
1670 /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1671 * lock is needed to prevent uvc_disconnect from releasing its
1672 * reference to the uvc_device instance after uvc_v4l2_open() received
1673 * the pointer to the device (video_devdata) but before it got the
1674 * chance to increase the reference count (kref_get).
1676 * Note that the reference can't be released with the lock held,
1677 * otherwise a AB-BA deadlock can occur with videodev_lock that
1678 * videodev acquires in videodev_open() and video_unregister_device().
1680 mutex_lock(&uvc_driver
.open_mutex
);
1681 dev
->state
|= UVC_DEV_DISCONNECTED
;
1682 mutex_unlock(&uvc_driver
.open_mutex
);
1684 kref_put(&dev
->kref
, uvc_delete
);
1687 static int uvc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1689 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1691 uvc_trace(UVC_TRACE_SUSPEND
, "Suspending interface %u\n",
1692 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1694 /* Controls are cached on the fly so they don't need to be saved. */
1695 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
== SC_VIDEOCONTROL
)
1696 return uvc_status_suspend(dev
);
1698 if (dev
->video
.streaming
->intf
!= intf
) {
1699 uvc_trace(UVC_TRACE_SUSPEND
, "Suspend: video streaming USB "
1700 "interface mismatch.\n");
1704 return uvc_video_suspend(&dev
->video
);
1707 static int __uvc_resume(struct usb_interface
*intf
, int reset
)
1709 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1711 uvc_trace(UVC_TRACE_SUSPEND
, "Resuming interface %u\n",
1712 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1714 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
== SC_VIDEOCONTROL
) {
1716 int ret
= uvc_ctrl_resume_device(dev
);
1722 return uvc_status_resume(dev
);
1725 if (dev
->video
.streaming
->intf
!= intf
) {
1726 uvc_trace(UVC_TRACE_SUSPEND
, "Resume: video streaming USB "
1727 "interface mismatch.\n");
1731 return uvc_video_resume(&dev
->video
);
1734 static int uvc_resume(struct usb_interface
*intf
)
1736 return __uvc_resume(intf
, 0);
1739 static int uvc_reset_resume(struct usb_interface
*intf
)
1741 return __uvc_resume(intf
, 1);
1744 /* ------------------------------------------------------------------------
1745 * Driver initialization and cleanup
1749 * The Logitech cameras listed below have their interface class set to
1750 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1751 * though they are compliant.
1753 static struct usb_device_id uvc_ids
[] = {
1754 /* Microsoft Lifecam NX-6000 */
1755 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1756 | USB_DEVICE_ID_MATCH_INT_INFO
,
1758 .idProduct
= 0x00f8,
1759 .bInterfaceClass
= USB_CLASS_VIDEO
,
1760 .bInterfaceSubClass
= 1,
1761 .bInterfaceProtocol
= 0,
1762 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1763 /* Microsoft Lifecam VX-7000 */
1764 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1765 | USB_DEVICE_ID_MATCH_INT_INFO
,
1767 .idProduct
= 0x0723,
1768 .bInterfaceClass
= USB_CLASS_VIDEO
,
1769 .bInterfaceSubClass
= 1,
1770 .bInterfaceProtocol
= 0,
1771 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1772 /* Logitech Quickcam Fusion */
1773 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1774 | USB_DEVICE_ID_MATCH_INT_INFO
,
1776 .idProduct
= 0x08c1,
1777 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1778 .bInterfaceSubClass
= 1,
1779 .bInterfaceProtocol
= 0 },
1780 /* Logitech Quickcam Orbit MP */
1781 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1782 | USB_DEVICE_ID_MATCH_INT_INFO
,
1784 .idProduct
= 0x08c2,
1785 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1786 .bInterfaceSubClass
= 1,
1787 .bInterfaceProtocol
= 0 },
1788 /* Logitech Quickcam Pro for Notebook */
1789 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1790 | USB_DEVICE_ID_MATCH_INT_INFO
,
1792 .idProduct
= 0x08c3,
1793 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1794 .bInterfaceSubClass
= 1,
1795 .bInterfaceProtocol
= 0 },
1796 /* Logitech Quickcam Pro 5000 */
1797 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1798 | USB_DEVICE_ID_MATCH_INT_INFO
,
1800 .idProduct
= 0x08c5,
1801 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1802 .bInterfaceSubClass
= 1,
1803 .bInterfaceProtocol
= 0 },
1804 /* Logitech Quickcam OEM Dell Notebook */
1805 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1806 | USB_DEVICE_ID_MATCH_INT_INFO
,
1808 .idProduct
= 0x08c6,
1809 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1810 .bInterfaceSubClass
= 1,
1811 .bInterfaceProtocol
= 0 },
1812 /* Logitech Quickcam OEM Cisco VT Camera II */
1813 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1814 | USB_DEVICE_ID_MATCH_INT_INFO
,
1816 .idProduct
= 0x08c7,
1817 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1818 .bInterfaceSubClass
= 1,
1819 .bInterfaceProtocol
= 0 },
1820 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1821 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1822 | USB_DEVICE_ID_MATCH_INT_INFO
,
1824 .idProduct
= 0x3820,
1825 .bInterfaceClass
= USB_CLASS_VIDEO
,
1826 .bInterfaceSubClass
= 1,
1827 .bInterfaceProtocol
= 0,
1828 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1829 /* Apple Built-In iSight */
1830 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1831 | USB_DEVICE_ID_MATCH_INT_INFO
,
1833 .idProduct
= 0x8501,
1834 .bInterfaceClass
= USB_CLASS_VIDEO
,
1835 .bInterfaceSubClass
= 1,
1836 .bInterfaceProtocol
= 0,
1837 .driver_info
= UVC_QUIRK_PROBE_MINMAX
1838 | UVC_QUIRK_BUILTIN_ISIGHT
},
1839 /* Genesys Logic USB 2.0 PC Camera */
1840 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1841 | USB_DEVICE_ID_MATCH_INT_INFO
,
1843 .idProduct
= 0x0505,
1844 .bInterfaceClass
= USB_CLASS_VIDEO
,
1845 .bInterfaceSubClass
= 1,
1846 .bInterfaceProtocol
= 0,
1847 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1849 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1850 | USB_DEVICE_ID_MATCH_INT_INFO
,
1852 .idProduct
= 0x332d,
1853 .bInterfaceClass
= USB_CLASS_VIDEO
,
1854 .bInterfaceSubClass
= 1,
1855 .bInterfaceProtocol
= 0,
1856 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1857 /* ViMicro - Minoru3D */
1858 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1859 | USB_DEVICE_ID_MATCH_INT_INFO
,
1861 .idProduct
= 0x3410,
1862 .bInterfaceClass
= USB_CLASS_VIDEO
,
1863 .bInterfaceSubClass
= 1,
1864 .bInterfaceProtocol
= 0,
1865 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1866 /* ViMicro Venus - Minoru3D */
1867 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1868 | USB_DEVICE_ID_MATCH_INT_INFO
,
1870 .idProduct
= 0x3420,
1871 .bInterfaceClass
= USB_CLASS_VIDEO
,
1872 .bInterfaceSubClass
= 1,
1873 .bInterfaceProtocol
= 0,
1874 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1876 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1877 | USB_DEVICE_ID_MATCH_INT_INFO
,
1879 .idProduct
= 0x0004,
1880 .bInterfaceClass
= USB_CLASS_VIDEO
,
1881 .bInterfaceSubClass
= 1,
1882 .bInterfaceProtocol
= 0,
1883 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1884 /* Syntek (HP Spartan) */
1885 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1886 | USB_DEVICE_ID_MATCH_INT_INFO
,
1888 .idProduct
= 0x5212,
1889 .bInterfaceClass
= USB_CLASS_VIDEO
,
1890 .bInterfaceSubClass
= 1,
1891 .bInterfaceProtocol
= 0,
1892 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1893 /* Syntek (Samsung Q310) */
1894 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1895 | USB_DEVICE_ID_MATCH_INT_INFO
,
1897 .idProduct
= 0x5931,
1898 .bInterfaceClass
= USB_CLASS_VIDEO
,
1899 .bInterfaceSubClass
= 1,
1900 .bInterfaceProtocol
= 0,
1901 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1902 /* Syntek (Asus F9SG) */
1903 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1904 | USB_DEVICE_ID_MATCH_INT_INFO
,
1906 .idProduct
= 0x8a31,
1907 .bInterfaceClass
= USB_CLASS_VIDEO
,
1908 .bInterfaceSubClass
= 1,
1909 .bInterfaceProtocol
= 0,
1910 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1911 /* Syntek (Asus U3S) */
1912 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1913 | USB_DEVICE_ID_MATCH_INT_INFO
,
1915 .idProduct
= 0x8a33,
1916 .bInterfaceClass
= USB_CLASS_VIDEO
,
1917 .bInterfaceSubClass
= 1,
1918 .bInterfaceProtocol
= 0,
1919 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1920 /* Syntek (JAOtech Smart Terminal) */
1921 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1922 | USB_DEVICE_ID_MATCH_INT_INFO
,
1924 .idProduct
= 0x8a34,
1925 .bInterfaceClass
= USB_CLASS_VIDEO
,
1926 .bInterfaceSubClass
= 1,
1927 .bInterfaceProtocol
= 0,
1928 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1929 /* Lenovo Thinkpad SL400/SL500 */
1930 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1931 | USB_DEVICE_ID_MATCH_INT_INFO
,
1933 .idProduct
= 0x480b,
1934 .bInterfaceClass
= USB_CLASS_VIDEO
,
1935 .bInterfaceSubClass
= 1,
1936 .bInterfaceProtocol
= 0,
1937 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1938 /* Aveo Technology USB 2.0 Camera */
1939 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1940 | USB_DEVICE_ID_MATCH_INT_INFO
,
1942 .idProduct
= 0x0306,
1943 .bInterfaceClass
= USB_CLASS_VIDEO
,
1944 .bInterfaceSubClass
= 1,
1945 .bInterfaceProtocol
= 0,
1946 .driver_info
= UVC_QUIRK_PROBE_EXTRAFIELDS
},
1947 /* Ecamm Pico iMage */
1948 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1949 | USB_DEVICE_ID_MATCH_INT_INFO
,
1951 .idProduct
= 0xcafe,
1952 .bInterfaceClass
= USB_CLASS_VIDEO
,
1953 .bInterfaceSubClass
= 1,
1954 .bInterfaceProtocol
= 0,
1955 .driver_info
= UVC_QUIRK_PROBE_EXTRAFIELDS
},
1956 /* FSC WebCam V30S */
1957 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1958 | USB_DEVICE_ID_MATCH_INT_INFO
,
1960 .idProduct
= 0x3288,
1961 .bInterfaceClass
= USB_CLASS_VIDEO
,
1962 .bInterfaceSubClass
= 1,
1963 .bInterfaceProtocol
= 0,
1964 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1965 /* Bodelin ProScopeHR */
1966 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1967 | USB_DEVICE_ID_MATCH_DEV_HI
1968 | USB_DEVICE_ID_MATCH_INT_INFO
,
1970 .idProduct
= 0x1000,
1971 .bcdDevice_hi
= 0x0126,
1972 .bInterfaceClass
= USB_CLASS_VIDEO
,
1973 .bInterfaceSubClass
= 1,
1974 .bInterfaceProtocol
= 0,
1975 .driver_info
= UVC_QUIRK_STATUS_INTERVAL
},
1976 /* SiGma Micro USB Web Camera */
1977 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1978 | USB_DEVICE_ID_MATCH_INT_INFO
,
1980 .idProduct
= 0x3000,
1981 .bInterfaceClass
= USB_CLASS_VIDEO
,
1982 .bInterfaceSubClass
= 1,
1983 .bInterfaceProtocol
= 0,
1984 .driver_info
= UVC_QUIRK_PROBE_MINMAX
1985 | UVC_QUIRK_IGNORE_SELECTOR_UNIT
},
1986 /* Generic USB Video Class */
1987 { USB_INTERFACE_INFO(USB_CLASS_VIDEO
, 1, 0) },
1991 MODULE_DEVICE_TABLE(usb
, uvc_ids
);
1993 struct uvc_driver uvc_driver
= {
1997 .disconnect
= uvc_disconnect
,
1998 .suspend
= uvc_suspend
,
1999 .resume
= uvc_resume
,
2000 .reset_resume
= uvc_reset_resume
,
2001 .id_table
= uvc_ids
,
2002 .supports_autosuspend
= 1,
2006 static int __init
uvc_init(void)
2010 INIT_LIST_HEAD(&uvc_driver
.devices
);
2011 INIT_LIST_HEAD(&uvc_driver
.controls
);
2012 mutex_init(&uvc_driver
.open_mutex
);
2013 mutex_init(&uvc_driver
.ctrl_mutex
);
2017 result
= usb_register(&uvc_driver
.driver
);
2019 printk(KERN_INFO DRIVER_DESC
" (" DRIVER_VERSION
")\n");
2023 static void __exit
uvc_cleanup(void)
2025 usb_deregister(&uvc_driver
.driver
);
2028 module_init(uvc_init
);
2029 module_exit(uvc_cleanup
);
2031 module_param_named(nodrop
, uvc_no_drop_param
, uint
, S_IRUGO
|S_IWUSR
);
2032 MODULE_PARM_DESC(nodrop
, "Don't drop incomplete frames");
2033 module_param_named(quirks
, uvc_quirks_param
, uint
, S_IRUGO
|S_IWUSR
);
2034 MODULE_PARM_DESC(quirks
, "Forced device quirks");
2035 module_param_named(trace
, uvc_trace_param
, uint
, S_IRUGO
|S_IWUSR
);
2036 MODULE_PARM_DESC(trace
, "Trace level bitmask");
2038 MODULE_AUTHOR(DRIVER_AUTHOR
);
2039 MODULE_DESCRIPTION(DRIVER_DESC
);
2040 MODULE_LICENSE("GPL");
2041 MODULE_VERSION(DRIVER_VERSION
);