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