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