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