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