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