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