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
)) {
252 case UVC_TT_STREAMING
:
253 if (entity
->output
.bSourceID
== id
)
257 case UVC_VC_PROCESSING_UNIT
:
258 if (entity
->processing
.bSourceID
== id
)
262 case UVC_VC_SELECTOR_UNIT
:
263 for (i
= 0; i
< entity
->selector
.bNrInPins
; ++i
)
264 if (entity
->selector
.baSourceID
[i
] == id
)
268 case UVC_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 UVC_VS_FORMAT_UNCOMPRESSED
:
301 case UVC_VS_FORMAT_FRAME_BASED
:
302 n
= buffer
[2] == UVC_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] == UVC_VS_FORMAT_UNCOMPRESSED
) {
329 ftype
= UVC_VS_FRAME_UNCOMPRESSED
;
331 ftype
= UVC_VS_FRAME_FRAME_BASED
;
333 format
->flags
= UVC_FMT_FLAG_COMPRESSED
;
337 case UVC_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
= UVC_VS_FRAME_MJPEG
;
353 case UVC_VS_FORMAT_DV
:
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 UVC_VS_FORMAT_MPEG2TS
:
399 case UVC_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
!= UVC_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
!= UVC_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] == UVC_VS_STILL_IMAGE_FRAME
) {
499 if (buflen
> 2 && buffer
[2] == UVC_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 != UVC_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 UVC_VS_OUTPUT_HEADER
:
593 streaming
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
597 case UVC_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] == UVC_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] == USB_DT_CS_INTERFACE
) {
648 switch (_buffer
[2]) {
649 case UVC_VS_FORMAT_UNCOMPRESSED
:
650 case UVC_VS_FORMAT_MJPEG
:
651 case UVC_VS_FORMAT_FRAME_BASED
:
655 case UVC_VS_FORMAT_DV
:
656 /* DV format has no frame descriptor. We will create a
657 * dummy frame descriptor with a dummy frame interval.
664 case UVC_VS_FORMAT_MPEG2TS
:
665 case UVC_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 UVC_VS_FRAME_UNCOMPRESSED
:
673 case UVC_VS_FRAME_MJPEG
:
676 nintervals
+= _buffer
[25] ? _buffer
[25] : 3;
679 case UVC_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] == USB_DT_CS_INTERFACE
) {
714 case UVC_VS_FORMAT_UNCOMPRESSED
:
715 case UVC_VS_FORMAT_MJPEG
:
716 case UVC_VS_FORMAT_DV
:
717 case UVC_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
= UVC_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 UVC_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
== UVC_ITT_CAMERA
) {
912 n
= buflen
>= 15 ? buffer
[14] : 0;
915 } else if (type
== UVC_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
) == UVC_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
) ==
946 UVC_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
952 memcpy(term
->media
.bmControls
, &buffer
[9], n
);
953 memcpy(term
->media
.bmTransportModes
, &buffer
[10+n
], p
);
957 usb_string(udev
, buffer
[7], term
->name
,
959 else if (UVC_ENTITY_TYPE(term
) == UVC_ITT_CAMERA
)
960 sprintf(term
->name
, "Camera %u", buffer
[3]);
961 else if (UVC_ENTITY_TYPE(term
) == UVC_ITT_MEDIA_TRANSPORT_INPUT
)
962 sprintf(term
->name
, "Media %u", buffer
[3]);
964 sprintf(term
->name
, "Input %u", buffer
[3]);
966 list_add_tail(&term
->list
, &dev
->entities
);
969 case UVC_VC_OUTPUT_TERMINAL
:
971 uvc_trace(UVC_TRACE_DESCR
, "device %d videocontrol "
972 "interface %d OUTPUT_TERMINAL error\n",
973 udev
->devnum
, alts
->desc
.bInterfaceNumber
);
977 /* Make sure the terminal type MSB is not null, otherwise it
978 * could be confused with a unit.
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
);
989 term
= kzalloc(sizeof *term
, GFP_KERNEL
);
993 term
->id
= buffer
[3];
994 term
->type
= type
| UVC_TERM_OUTPUT
;
995 term
->output
.bSourceID
= buffer
[7];
998 usb_string(udev
, buffer
[8], term
->name
,
1001 sprintf(term
->name
, "Output %u", buffer
[3]);
1003 list_add_tail(&term
->list
, &dev
->entities
);
1006 case UVC_VC_SELECTOR_UNIT
:
1007 p
= buflen
>= 5 ? buffer
[4] : 0;
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
);
1016 unit
= kzalloc(sizeof *unit
+ p
, GFP_KERNEL
);
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
);
1026 if (buffer
[5+p
] != 0)
1027 usb_string(udev
, buffer
[5+p
], unit
->name
,
1030 sprintf(unit
->name
, "Selector %u", buffer
[3]);
1032 list_add_tail(&unit
->list
, &dev
->entities
);
1035 case UVC_VC_PROCESSING_UNIT
:
1036 n
= buflen
>= 8 ? buffer
[7] : 0;
1037 p
= dev
->uvc_version
>= 0x0110 ? 10 : 9;
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
);
1046 unit
= kzalloc(sizeof *unit
+ n
, GFP_KERNEL
);
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
];
1061 if (buffer
[8+n
] != 0)
1062 usb_string(udev
, buffer
[8+n
], unit
->name
,
1065 sprintf(unit
->name
, "Processing %u", buffer
[3]);
1067 list_add_tail(&unit
->list
, &dev
->entities
);
1070 case UVC_VC_EXTENSION_UNIT
:
1071 p
= buflen
>= 22 ? buffer
[21] : 0;
1072 n
= buflen
>= 24 + p
? buffer
[22+p
] : 0;
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
);
1081 unit
= kzalloc(sizeof *unit
+ p
+ n
, GFP_KERNEL
);
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
);
1096 if (buffer
[23+p
+n
] != 0)
1097 usb_string(udev
, buffer
[23+p
+n
], unit
->name
,
1100 sprintf(unit
->name
, "Extension %u", buffer
[3]);
1102 list_add_tail(&unit
->list
, &dev
->entities
);
1106 uvc_trace(UVC_TRACE_DESCR
, "Found an unknown CS_INTERFACE "
1107 "descriptor (%u)\n", buffer
[2]);
1114 static int uvc_parse_control(struct uvc_device
*dev
)
1116 struct usb_host_interface
*alts
= dev
->intf
->cur_altsetting
;
1117 unsigned char *buffer
= alts
->extra
;
1118 int buflen
= alts
->extralen
;
1121 /* Parse the default alternate setting only, as the UVC specification
1122 * defines a single alternate setting, the default alternate setting
1126 while (buflen
> 2) {
1127 if (uvc_parse_vendor_control(dev
, buffer
, buflen
) ||
1128 buffer
[1] != USB_DT_CS_INTERFACE
)
1129 goto next_descriptor
;
1131 if ((ret
= uvc_parse_standard_control(dev
, buffer
, buflen
)) < 0)
1135 buflen
-= buffer
[0];
1136 buffer
+= buffer
[0];
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.
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
;
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
);
1161 /* ------------------------------------------------------------------------
1162 * USB probe and disconnect
1166 * Unregister the video devices.
1168 static void uvc_unregister_video(struct uvc_device
*dev
)
1170 if (dev
->video
.vdev
) {
1171 if (dev
->video
.vdev
->minor
== -1)
1172 video_device_release(dev
->video
.vdev
);
1174 video_unregister_device(dev
->video
.vdev
);
1175 dev
->video
.vdev
= NULL
;
1180 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1181 * and containing the following units:
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)
1191 * A side forward scan is made on each detected entity to check for additional
1194 static int uvc_scan_chain_entity(struct uvc_video_device
*video
,
1195 struct uvc_entity
*entity
)
1197 switch (UVC_ENTITY_TYPE(entity
)) {
1198 case UVC_VC_EXTENSION_UNIT
:
1199 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1200 printk(" <- XU %d", entity
->id
);
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
);
1208 list_add_tail(&entity
->chain
, &video
->extensions
);
1211 case UVC_VC_PROCESSING_UNIT
:
1212 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1213 printk(" <- PU %d", entity
->id
);
1215 if (video
->processing
!= NULL
) {
1216 uvc_trace(UVC_TRACE_DESCR
, "Found multiple "
1217 "Processing Units in chain.\n");
1221 video
->processing
= entity
;
1224 case UVC_VC_SELECTOR_UNIT
:
1225 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1226 printk(" <- SU %d", entity
->id
);
1228 /* Single-input selector units are ignored. */
1229 if (entity
->selector
.bNrInPins
== 1)
1232 if (video
->selector
!= NULL
) {
1233 uvc_trace(UVC_TRACE_DESCR
, "Found multiple Selector "
1234 "Units in chain.\n");
1238 video
->selector
= entity
;
1241 case UVC_ITT_VENDOR_SPECIFIC
:
1242 case UVC_ITT_CAMERA
:
1243 case UVC_ITT_MEDIA_TRANSPORT_INPUT
:
1244 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1245 printk(" <- IT %d\n", entity
->id
);
1247 list_add_tail(&entity
->chain
, &video
->iterms
);
1250 case UVC_TT_STREAMING
:
1251 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1252 printk(" <- IT %d\n", entity
->id
);
1254 if (!UVC_ENTITY_IS_ITERM(entity
)) {
1255 uvc_trace(UVC_TRACE_DESCR
, "Unsupported input "
1256 "terminal %u.\n", entity
->id
);
1260 if (video
->sterm
!= NULL
) {
1261 uvc_trace(UVC_TRACE_DESCR
, "Found multiple streaming "
1262 "entities in chain.\n");
1266 list_add_tail(&entity
->chain
, &video
->iterms
);
1267 video
->sterm
= entity
;
1271 uvc_trace(UVC_TRACE_DESCR
, "Unsupported entity type "
1272 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity
));
1279 static int uvc_scan_chain_forward(struct uvc_video_device
*video
,
1280 struct uvc_entity
*entity
, struct uvc_entity
*prev
)
1282 struct uvc_entity
*forward
;
1290 forward
= uvc_entity_by_reference(video
->dev
, entity
->id
,
1292 if (forward
== NULL
)
1295 if (UVC_ENTITY_TYPE(forward
) != UVC_VC_EXTENSION_UNIT
||
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
);
1305 list_add_tail(&forward
->chain
, &video
->extensions
);
1306 if (uvc_trace_param
& UVC_TRACE_PROBE
) {
1310 printk(" %d", forward
->id
);
1320 static int uvc_scan_chain_backward(struct uvc_video_device
*video
,
1321 struct uvc_entity
*entity
)
1323 struct uvc_entity
*term
;
1326 switch (UVC_ENTITY_TYPE(entity
)) {
1327 case UVC_VC_EXTENSION_UNIT
:
1328 id
= entity
->extension
.baSourceID
[0];
1331 case UVC_VC_PROCESSING_UNIT
:
1332 id
= entity
->processing
.bSourceID
;
1335 case UVC_VC_SELECTOR_UNIT
:
1336 /* Single-input selector units are ignored. */
1337 if (entity
->selector
.bNrInPins
== 1) {
1338 id
= entity
->selector
.baSourceID
[0];
1342 if (uvc_trace_param
& UVC_TRACE_PROBE
)
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
);
1356 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1357 printk(" %d", term
->id
);
1359 list_add_tail(&term
->chain
, &video
->iterms
);
1360 uvc_scan_chain_forward(video
, term
, entity
);
1363 if (uvc_trace_param
& UVC_TRACE_PROBE
)
1373 static int uvc_scan_chain(struct uvc_video_device
*video
)
1375 struct uvc_entity
*entity
, *prev
;
1378 entity
= video
->oterm
;
1379 uvc_trace(UVC_TRACE_PROBE
, "Scanning UVC chain: OT %d", entity
->id
);
1381 if (UVC_ENTITY_TYPE(entity
) == UVC_TT_STREAMING
)
1382 video
->sterm
= entity
;
1384 id
= entity
->output
.bSourceID
;
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
);
1394 /* Process entity */
1395 if (uvc_scan_chain_entity(video
, entity
) < 0)
1399 if (uvc_scan_chain_forward(video
, entity
, prev
) < 0)
1402 /* Stop when a terminal is found. */
1403 if (!UVC_ENTITY_IS_UNIT(entity
))
1407 id
= uvc_scan_chain_backward(video
, entity
);
1412 if (video
->sterm
== NULL
) {
1413 uvc_trace(UVC_TRACE_DESCR
, "No streaming entity found in "
1422 * Register the video devices.
1424 * The driver currently supports a single video device per control interface
1425 * only. The terminal and units must match the following structure:
1427 * ITT_* -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> TT_STREAMING
1428 * TT_STREAMING -> VC_PROCESSING_UNIT -> VC_EXTENSION_UNIT{0,n} -> OTT_*
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
1435 static int uvc_register_video(struct uvc_device
*dev
)
1437 struct video_device
*vdev
;
1438 struct uvc_entity
*term
;
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
;
1445 if (!UVC_ENTITY_IS_TERM(term
) || !UVC_ENTITY_IS_OTERM(term
))
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)
1457 list_for_each_entry(streaming
, &dev
->streaming
, list
) {
1458 if (streaming
->header
.bTerminalLink
==
1459 dev
->video
.sterm
->id
) {
1460 dev
->video
.streaming
= streaming
;
1471 uvc_printk(KERN_INFO
, "No valid video chain found.\n");
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
)
1482 printk(" -> %d).\n", dev
->video
.oterm
->id
);
1485 /* Initialize the video buffers queue. */
1486 uvc_queue_init(&dev
->video
.queue
, dev
->video
.streaming
->type
);
1488 /* Initialize the streaming interface with default streaming
1491 if ((ret
= uvc_video_init(&dev
->video
)) < 0) {
1492 uvc_printk(KERN_ERR
, "Failed to initialize the device "
1497 /* Register the device with V4L. */
1498 vdev
= video_device_alloc();
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
1506 vdev
->parent
= &dev
->intf
->dev
;
1508 vdev
->fops
= &uvc_fops
;
1509 vdev
->release
= video_device_release
;
1510 strlcpy(vdev
->name
, dev
->name
, sizeof vdev
->name
);
1512 /* Set the driver data before calling video_register_device, otherwise
1513 * uvc_v4l2_open might race us.
1515 dev
->video
.vdev
= vdev
;
1516 video_set_drvdata(vdev
, &dev
->video
);
1518 if (video_register_device(vdev
, VFL_TYPE_GRABBER
, -1) < 0) {
1519 dev
->video
.vdev
= NULL
;
1520 video_device_release(vdev
);
1528 * Delete the UVC device.
1530 * Called by the kernel when the last reference to the uvc_device structure
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
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.
1543 void uvc_delete(struct kref
*kref
)
1545 struct uvc_device
*dev
= container_of(kref
, struct uvc_device
, kref
);
1546 struct list_head
*p
, *n
;
1548 /* Unregister the video device. */
1549 uvc_unregister_video(dev
);
1550 usb_put_intf(dev
->intf
);
1551 usb_put_dev(dev
->udev
);
1553 uvc_status_cleanup(dev
);
1554 uvc_ctrl_cleanup_device(dev
);
1556 list_for_each_safe(p
, n
, &dev
->entities
) {
1557 struct uvc_entity
*entity
;
1558 entity
= list_entry(p
, struct uvc_entity
, list
);
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
,
1567 usb_put_intf(streaming
->intf
);
1568 kfree(streaming
->format
);
1569 kfree(streaming
->header
.bmaControls
);
1576 static int uvc_probe(struct usb_interface
*intf
,
1577 const struct usb_device_id
*id
)
1579 struct usb_device
*udev
= interface_to_usbdev(intf
);
1580 struct uvc_device
*dev
;
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
,
1588 uvc_trace(UVC_TRACE_PROBE
, "Probing generic UVC device %s\n",
1591 /* Allocate memory for the device and initialize it. */
1592 if ((dev
= kzalloc(sizeof *dev
, GFP_KERNEL
)) == NULL
)
1595 INIT_LIST_HEAD(&dev
->entities
);
1596 INIT_LIST_HEAD(&dev
->streaming
);
1597 kref_init(&dev
->kref
);
1598 atomic_set(&dev
->users
, 0);
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
;
1605 if (udev
->product
!= NULL
)
1606 strlcpy(dev
->name
, udev
->product
, sizeof dev
->name
);
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
));
1613 /* Parse the Video Class control descriptor. */
1614 if (uvc_parse_control(dev
) < 0) {
1615 uvc_trace(UVC_TRACE_PROBE
, "Unable to parse UVC "
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
));
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");
1633 /* Initialize controls. */
1634 if (uvc_ctrl_init_device(dev
) < 0)
1637 /* Register the video devices. */
1638 if (uvc_register_video(dev
) < 0)
1641 /* Save our data pointer in the interface data. */
1642 usb_set_intfdata(intf
, dev
);
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
);
1651 uvc_trace(UVC_TRACE_PROBE
, "UVC device initialized.\n");
1655 kref_put(&dev
->kref
, uvc_delete
);
1659 static void uvc_disconnect(struct usb_interface
*intf
)
1661 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1663 /* Set the USB interface data to NULL. This can be done outside the
1664 * lock, as there's no other reader.
1666 usb_set_intfdata(intf
, NULL
);
1668 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
==
1669 UVC_SC_VIDEOSTREAMING
)
1672 /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1673 * lock is needed to prevent uvc_disconnect from releasing its
1674 * reference to the uvc_device instance after uvc_v4l2_open() received
1675 * the pointer to the device (video_devdata) but before it got the
1676 * chance to increase the reference count (kref_get).
1678 * Note that the reference can't be released with the lock held,
1679 * otherwise a AB-BA deadlock can occur with videodev_lock that
1680 * videodev acquires in videodev_open() and video_unregister_device().
1682 mutex_lock(&uvc_driver
.open_mutex
);
1683 dev
->state
|= UVC_DEV_DISCONNECTED
;
1684 mutex_unlock(&uvc_driver
.open_mutex
);
1686 kref_put(&dev
->kref
, uvc_delete
);
1689 static int uvc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1691 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1693 uvc_trace(UVC_TRACE_SUSPEND
, "Suspending interface %u\n",
1694 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1696 /* Controls are cached on the fly so they don't need to be saved. */
1697 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
==
1698 UVC_SC_VIDEOCONTROL
)
1699 return uvc_status_suspend(dev
);
1701 if (dev
->video
.streaming
->intf
!= intf
) {
1702 uvc_trace(UVC_TRACE_SUSPEND
, "Suspend: video streaming USB "
1703 "interface mismatch.\n");
1707 return uvc_video_suspend(&dev
->video
);
1710 static int __uvc_resume(struct usb_interface
*intf
, int reset
)
1712 struct uvc_device
*dev
= usb_get_intfdata(intf
);
1714 uvc_trace(UVC_TRACE_SUSPEND
, "Resuming interface %u\n",
1715 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1717 if (intf
->cur_altsetting
->desc
.bInterfaceSubClass
==
1718 UVC_SC_VIDEOCONTROL
) {
1720 int ret
= uvc_ctrl_resume_device(dev
);
1726 return uvc_status_resume(dev
);
1729 if (dev
->video
.streaming
->intf
!= intf
) {
1730 uvc_trace(UVC_TRACE_SUSPEND
, "Resume: video streaming USB "
1731 "interface mismatch.\n");
1735 return uvc_video_resume(&dev
->video
);
1738 static int uvc_resume(struct usb_interface
*intf
)
1740 return __uvc_resume(intf
, 0);
1743 static int uvc_reset_resume(struct usb_interface
*intf
)
1745 return __uvc_resume(intf
, 1);
1748 /* ------------------------------------------------------------------------
1749 * Driver initialization and cleanup
1753 * The Logitech cameras listed below have their interface class set to
1754 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1755 * though they are compliant.
1757 static struct usb_device_id uvc_ids
[] = {
1758 /* Microsoft Lifecam NX-6000 */
1759 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1760 | USB_DEVICE_ID_MATCH_INT_INFO
,
1762 .idProduct
= 0x00f8,
1763 .bInterfaceClass
= USB_CLASS_VIDEO
,
1764 .bInterfaceSubClass
= 1,
1765 .bInterfaceProtocol
= 0,
1766 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1767 /* Microsoft Lifecam VX-7000 */
1768 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1769 | USB_DEVICE_ID_MATCH_INT_INFO
,
1771 .idProduct
= 0x0723,
1772 .bInterfaceClass
= USB_CLASS_VIDEO
,
1773 .bInterfaceSubClass
= 1,
1774 .bInterfaceProtocol
= 0,
1775 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1776 /* Logitech Quickcam Fusion */
1777 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1778 | USB_DEVICE_ID_MATCH_INT_INFO
,
1780 .idProduct
= 0x08c1,
1781 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1782 .bInterfaceSubClass
= 1,
1783 .bInterfaceProtocol
= 0 },
1784 /* Logitech Quickcam Orbit MP */
1785 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1786 | USB_DEVICE_ID_MATCH_INT_INFO
,
1788 .idProduct
= 0x08c2,
1789 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1790 .bInterfaceSubClass
= 1,
1791 .bInterfaceProtocol
= 0 },
1792 /* Logitech Quickcam Pro for Notebook */
1793 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1794 | USB_DEVICE_ID_MATCH_INT_INFO
,
1796 .idProduct
= 0x08c3,
1797 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1798 .bInterfaceSubClass
= 1,
1799 .bInterfaceProtocol
= 0 },
1800 /* Logitech Quickcam Pro 5000 */
1801 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1802 | USB_DEVICE_ID_MATCH_INT_INFO
,
1804 .idProduct
= 0x08c5,
1805 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1806 .bInterfaceSubClass
= 1,
1807 .bInterfaceProtocol
= 0 },
1808 /* Logitech Quickcam OEM Dell Notebook */
1809 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1810 | USB_DEVICE_ID_MATCH_INT_INFO
,
1812 .idProduct
= 0x08c6,
1813 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1814 .bInterfaceSubClass
= 1,
1815 .bInterfaceProtocol
= 0 },
1816 /* Logitech Quickcam OEM Cisco VT Camera II */
1817 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1818 | USB_DEVICE_ID_MATCH_INT_INFO
,
1820 .idProduct
= 0x08c7,
1821 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
1822 .bInterfaceSubClass
= 1,
1823 .bInterfaceProtocol
= 0 },
1824 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1825 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1826 | USB_DEVICE_ID_MATCH_INT_INFO
,
1828 .idProduct
= 0x3820,
1829 .bInterfaceClass
= USB_CLASS_VIDEO
,
1830 .bInterfaceSubClass
= 1,
1831 .bInterfaceProtocol
= 0,
1832 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1833 /* Apple Built-In iSight */
1834 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1835 | USB_DEVICE_ID_MATCH_INT_INFO
,
1837 .idProduct
= 0x8501,
1838 .bInterfaceClass
= USB_CLASS_VIDEO
,
1839 .bInterfaceSubClass
= 1,
1840 .bInterfaceProtocol
= 0,
1841 .driver_info
= UVC_QUIRK_PROBE_MINMAX
1842 | UVC_QUIRK_BUILTIN_ISIGHT
},
1843 /* Genesys Logic USB 2.0 PC Camera */
1844 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1845 | USB_DEVICE_ID_MATCH_INT_INFO
,
1847 .idProduct
= 0x0505,
1848 .bInterfaceClass
= USB_CLASS_VIDEO
,
1849 .bInterfaceSubClass
= 1,
1850 .bInterfaceProtocol
= 0,
1851 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1853 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1854 | USB_DEVICE_ID_MATCH_INT_INFO
,
1856 .idProduct
= 0x332d,
1857 .bInterfaceClass
= USB_CLASS_VIDEO
,
1858 .bInterfaceSubClass
= 1,
1859 .bInterfaceProtocol
= 0,
1860 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1861 /* ViMicro - Minoru3D */
1862 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1863 | USB_DEVICE_ID_MATCH_INT_INFO
,
1865 .idProduct
= 0x3410,
1866 .bInterfaceClass
= USB_CLASS_VIDEO
,
1867 .bInterfaceSubClass
= 1,
1868 .bInterfaceProtocol
= 0,
1869 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1870 /* ViMicro Venus - Minoru3D */
1871 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1872 | USB_DEVICE_ID_MATCH_INT_INFO
,
1874 .idProduct
= 0x3420,
1875 .bInterfaceClass
= USB_CLASS_VIDEO
,
1876 .bInterfaceSubClass
= 1,
1877 .bInterfaceProtocol
= 0,
1878 .driver_info
= UVC_QUIRK_FIX_BANDWIDTH
},
1880 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1881 | USB_DEVICE_ID_MATCH_INT_INFO
,
1883 .idProduct
= 0x0004,
1884 .bInterfaceClass
= USB_CLASS_VIDEO
,
1885 .bInterfaceSubClass
= 1,
1886 .bInterfaceProtocol
= 0,
1887 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1888 /* Syntek (HP Spartan) */
1889 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1890 | USB_DEVICE_ID_MATCH_INT_INFO
,
1892 .idProduct
= 0x5212,
1893 .bInterfaceClass
= USB_CLASS_VIDEO
,
1894 .bInterfaceSubClass
= 1,
1895 .bInterfaceProtocol
= 0,
1896 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1897 /* Syntek (Samsung Q310) */
1898 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1899 | USB_DEVICE_ID_MATCH_INT_INFO
,
1901 .idProduct
= 0x5931,
1902 .bInterfaceClass
= USB_CLASS_VIDEO
,
1903 .bInterfaceSubClass
= 1,
1904 .bInterfaceProtocol
= 0,
1905 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1906 /* Syntek (Asus F9SG) */
1907 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1908 | USB_DEVICE_ID_MATCH_INT_INFO
,
1910 .idProduct
= 0x8a31,
1911 .bInterfaceClass
= USB_CLASS_VIDEO
,
1912 .bInterfaceSubClass
= 1,
1913 .bInterfaceProtocol
= 0,
1914 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1915 /* Syntek (Asus U3S) */
1916 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1917 | USB_DEVICE_ID_MATCH_INT_INFO
,
1919 .idProduct
= 0x8a33,
1920 .bInterfaceClass
= USB_CLASS_VIDEO
,
1921 .bInterfaceSubClass
= 1,
1922 .bInterfaceProtocol
= 0,
1923 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1924 /* Syntek (JAOtech Smart Terminal) */
1925 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1926 | USB_DEVICE_ID_MATCH_INT_INFO
,
1928 .idProduct
= 0x8a34,
1929 .bInterfaceClass
= USB_CLASS_VIDEO
,
1930 .bInterfaceSubClass
= 1,
1931 .bInterfaceProtocol
= 0,
1932 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1933 /* Lenovo Thinkpad SL400/SL500 */
1934 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1935 | USB_DEVICE_ID_MATCH_INT_INFO
,
1937 .idProduct
= 0x480b,
1938 .bInterfaceClass
= USB_CLASS_VIDEO
,
1939 .bInterfaceSubClass
= 1,
1940 .bInterfaceProtocol
= 0,
1941 .driver_info
= UVC_QUIRK_STREAM_NO_FID
},
1942 /* Aveo Technology USB 2.0 Camera */
1943 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1944 | USB_DEVICE_ID_MATCH_INT_INFO
,
1946 .idProduct
= 0x0306,
1947 .bInterfaceClass
= USB_CLASS_VIDEO
,
1948 .bInterfaceSubClass
= 1,
1949 .bInterfaceProtocol
= 0,
1950 .driver_info
= UVC_QUIRK_PROBE_EXTRAFIELDS
},
1951 /* Ecamm Pico iMage */
1952 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1953 | USB_DEVICE_ID_MATCH_INT_INFO
,
1955 .idProduct
= 0xcafe,
1956 .bInterfaceClass
= USB_CLASS_VIDEO
,
1957 .bInterfaceSubClass
= 1,
1958 .bInterfaceProtocol
= 0,
1959 .driver_info
= UVC_QUIRK_PROBE_EXTRAFIELDS
},
1960 /* FSC WebCam V30S */
1961 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1962 | USB_DEVICE_ID_MATCH_INT_INFO
,
1964 .idProduct
= 0x3288,
1965 .bInterfaceClass
= USB_CLASS_VIDEO
,
1966 .bInterfaceSubClass
= 1,
1967 .bInterfaceProtocol
= 0,
1968 .driver_info
= UVC_QUIRK_PROBE_MINMAX
},
1969 /* Bodelin ProScopeHR */
1970 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1971 | USB_DEVICE_ID_MATCH_DEV_HI
1972 | USB_DEVICE_ID_MATCH_INT_INFO
,
1974 .idProduct
= 0x1000,
1975 .bcdDevice_hi
= 0x0126,
1976 .bInterfaceClass
= USB_CLASS_VIDEO
,
1977 .bInterfaceSubClass
= 1,
1978 .bInterfaceProtocol
= 0,
1979 .driver_info
= UVC_QUIRK_STATUS_INTERVAL
},
1980 /* SiGma Micro USB Web Camera */
1981 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
1982 | USB_DEVICE_ID_MATCH_INT_INFO
,
1984 .idProduct
= 0x3000,
1985 .bInterfaceClass
= USB_CLASS_VIDEO
,
1986 .bInterfaceSubClass
= 1,
1987 .bInterfaceProtocol
= 0,
1988 .driver_info
= UVC_QUIRK_PROBE_MINMAX
1989 | UVC_QUIRK_IGNORE_SELECTOR_UNIT
},
1990 /* Generic USB Video Class */
1991 { USB_INTERFACE_INFO(USB_CLASS_VIDEO
, 1, 0) },
1995 MODULE_DEVICE_TABLE(usb
, uvc_ids
);
1997 struct uvc_driver uvc_driver
= {
2001 .disconnect
= uvc_disconnect
,
2002 .suspend
= uvc_suspend
,
2003 .resume
= uvc_resume
,
2004 .reset_resume
= uvc_reset_resume
,
2005 .id_table
= uvc_ids
,
2006 .supports_autosuspend
= 1,
2010 static int __init
uvc_init(void)
2014 INIT_LIST_HEAD(&uvc_driver
.devices
);
2015 INIT_LIST_HEAD(&uvc_driver
.controls
);
2016 mutex_init(&uvc_driver
.open_mutex
);
2017 mutex_init(&uvc_driver
.ctrl_mutex
);
2021 result
= usb_register(&uvc_driver
.driver
);
2023 printk(KERN_INFO DRIVER_DESC
" (" DRIVER_VERSION
")\n");
2027 static void __exit
uvc_cleanup(void)
2029 usb_deregister(&uvc_driver
.driver
);
2032 module_init(uvc_init
);
2033 module_exit(uvc_cleanup
);
2035 module_param_named(nodrop
, uvc_no_drop_param
, uint
, S_IRUGO
|S_IWUSR
);
2036 MODULE_PARM_DESC(nodrop
, "Don't drop incomplete frames");
2037 module_param_named(quirks
, uvc_quirks_param
, uint
, S_IRUGO
|S_IWUSR
);
2038 MODULE_PARM_DESC(quirks
, "Forced device quirks");
2039 module_param_named(trace
, uvc_trace_param
, uint
, S_IRUGO
|S_IWUSR
);
2040 MODULE_PARM_DESC(trace
, "Trace level bitmask");
2042 MODULE_AUTHOR(DRIVER_AUTHOR
);
2043 MODULE_DESCRIPTION(DRIVER_DESC
);
2044 MODULE_LICENSE("GPL");
2045 MODULE_VERSION(DRIVER_VERSION
);