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