xref: /openbmc/linux/drivers/media/usb/uvc/uvc_driver.c (revision 465191d6)
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		= "H.265",
159 		.guid		= UVC_GUID_FORMAT_H265,
160 		.fcc		= V4L2_PIX_FMT_HEVC,
161 	},
162 	{
163 		.name		= "Greyscale 8 L/R (Y8I)",
164 		.guid		= UVC_GUID_FORMAT_Y8I,
165 		.fcc		= V4L2_PIX_FMT_Y8I,
166 	},
167 	{
168 		.name		= "Greyscale 12 L/R (Y12I)",
169 		.guid		= UVC_GUID_FORMAT_Y12I,
170 		.fcc		= V4L2_PIX_FMT_Y12I,
171 	},
172 	{
173 		.name		= "Depth data 16-bit (Z16)",
174 		.guid		= UVC_GUID_FORMAT_Z16,
175 		.fcc		= V4L2_PIX_FMT_Z16,
176 	},
177 	{
178 		.name		= "Bayer 10-bit (SRGGB10P)",
179 		.guid		= UVC_GUID_FORMAT_RW10,
180 		.fcc		= V4L2_PIX_FMT_SRGGB10P,
181 	},
182 	{
183 		.name		= "Bayer 16-bit (SBGGR16)",
184 		.guid		= UVC_GUID_FORMAT_BG16,
185 		.fcc		= V4L2_PIX_FMT_SBGGR16,
186 	},
187 	{
188 		.name		= "Bayer 16-bit (SGBRG16)",
189 		.guid		= UVC_GUID_FORMAT_GB16,
190 		.fcc		= V4L2_PIX_FMT_SGBRG16,
191 	},
192 	{
193 		.name		= "Bayer 16-bit (SRGGB16)",
194 		.guid		= UVC_GUID_FORMAT_RG16,
195 		.fcc		= V4L2_PIX_FMT_SRGGB16,
196 	},
197 	{
198 		.name		= "Bayer 16-bit (SGRBG16)",
199 		.guid		= UVC_GUID_FORMAT_GR16,
200 		.fcc		= V4L2_PIX_FMT_SGRBG16,
201 	},
202 	{
203 		.name		= "Depth data 16-bit (Z16)",
204 		.guid		= UVC_GUID_FORMAT_INVZ,
205 		.fcc		= V4L2_PIX_FMT_Z16,
206 	},
207 	{
208 		.name		= "Greyscale 10-bit (Y10 )",
209 		.guid		= UVC_GUID_FORMAT_INVI,
210 		.fcc		= V4L2_PIX_FMT_Y10,
211 	},
212 	{
213 		.name		= "IR:Depth 26-bit (INZI)",
214 		.guid		= UVC_GUID_FORMAT_INZI,
215 		.fcc		= V4L2_PIX_FMT_INZI,
216 	},
217 	{
218 		.name		= "4-bit Depth Confidence (Packed)",
219 		.guid		= UVC_GUID_FORMAT_CNF4,
220 		.fcc		= V4L2_PIX_FMT_CNF4,
221 	},
222 	{
223 		.name		= "HEVC",
224 		.guid		= UVC_GUID_FORMAT_HEVC,
225 		.fcc		= V4L2_PIX_FMT_HEVC,
226 	},
227 };
228 
229 /* ------------------------------------------------------------------------
230  * Utility functions
231  */
232 
233 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
234 		u8 epaddr)
235 {
236 	struct usb_host_endpoint *ep;
237 	unsigned int i;
238 
239 	for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
240 		ep = &alts->endpoint[i];
241 		if (ep->desc.bEndpointAddress == epaddr)
242 			return ep;
243 	}
244 
245 	return NULL;
246 }
247 
248 static struct uvc_format_desc *uvc_format_by_guid(const u8 guid[16])
249 {
250 	unsigned int len = ARRAY_SIZE(uvc_fmts);
251 	unsigned int i;
252 
253 	for (i = 0; i < len; ++i) {
254 		if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
255 			return &uvc_fmts[i];
256 	}
257 
258 	return NULL;
259 }
260 
261 static enum v4l2_colorspace uvc_colorspace(const u8 primaries)
262 {
263 	static const enum v4l2_colorspace colorprimaries[] = {
264 		V4L2_COLORSPACE_SRGB,  /* Unspecified */
265 		V4L2_COLORSPACE_SRGB,
266 		V4L2_COLORSPACE_470_SYSTEM_M,
267 		V4L2_COLORSPACE_470_SYSTEM_BG,
268 		V4L2_COLORSPACE_SMPTE170M,
269 		V4L2_COLORSPACE_SMPTE240M,
270 	};
271 
272 	if (primaries < ARRAY_SIZE(colorprimaries))
273 		return colorprimaries[primaries];
274 
275 	return V4L2_COLORSPACE_SRGB;  /* Reserved */
276 }
277 
278 static enum v4l2_xfer_func uvc_xfer_func(const u8 transfer_characteristics)
279 {
280 	/*
281 	 * V4L2 does not currently have definitions for all possible values of
282 	 * UVC transfer characteristics. If v4l2_xfer_func is extended with new
283 	 * values, the mapping below should be updated.
284 	 *
285 	 * Substitutions are taken from the mapping given for
286 	 * V4L2_XFER_FUNC_DEFAULT documented in videodev2.h.
287 	 */
288 	static const enum v4l2_xfer_func xfer_funcs[] = {
289 		V4L2_XFER_FUNC_DEFAULT,    /* Unspecified */
290 		V4L2_XFER_FUNC_709,
291 		V4L2_XFER_FUNC_709,        /* Substitution for BT.470-2 M */
292 		V4L2_XFER_FUNC_709,        /* Substitution for BT.470-2 B, G */
293 		V4L2_XFER_FUNC_709,        /* Substitution for SMPTE 170M */
294 		V4L2_XFER_FUNC_SMPTE240M,
295 		V4L2_XFER_FUNC_NONE,
296 		V4L2_XFER_FUNC_SRGB,
297 	};
298 
299 	if (transfer_characteristics < ARRAY_SIZE(xfer_funcs))
300 		return xfer_funcs[transfer_characteristics];
301 
302 	return V4L2_XFER_FUNC_DEFAULT;  /* Reserved */
303 }
304 
305 static enum v4l2_ycbcr_encoding uvc_ycbcr_enc(const u8 matrix_coefficients)
306 {
307 	/*
308 	 * V4L2 does not currently have definitions for all possible values of
309 	 * UVC matrix coefficients. If v4l2_ycbcr_encoding is extended with new
310 	 * values, the mapping below should be updated.
311 	 *
312 	 * Substitutions are taken from the mapping given for
313 	 * V4L2_YCBCR_ENC_DEFAULT documented in videodev2.h.
314 	 *
315 	 * FCC is assumed to be close enough to 601.
316 	 */
317 	static const enum v4l2_ycbcr_encoding ycbcr_encs[] = {
318 		V4L2_YCBCR_ENC_DEFAULT,  /* Unspecified */
319 		V4L2_YCBCR_ENC_709,
320 		V4L2_YCBCR_ENC_601,      /* Substitution for FCC */
321 		V4L2_YCBCR_ENC_601,      /* Substitution for BT.470-2 B, G */
322 		V4L2_YCBCR_ENC_601,
323 		V4L2_YCBCR_ENC_SMPTE240M,
324 	};
325 
326 	if (matrix_coefficients < ARRAY_SIZE(ycbcr_encs))
327 		return ycbcr_encs[matrix_coefficients];
328 
329 	return V4L2_YCBCR_ENC_DEFAULT;  /* Reserved */
330 }
331 
332 /* Simplify a fraction using a simple continued fraction decomposition. The
333  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
334  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
335  * arbitrary parameters to remove non-significative terms from the simple
336  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
337  * respectively seems to give nice results.
338  */
339 void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
340 		unsigned int n_terms, unsigned int threshold)
341 {
342 	u32 *an;
343 	u32 x, y, r;
344 	unsigned int i, n;
345 
346 	an = kmalloc_array(n_terms, sizeof(*an), GFP_KERNEL);
347 	if (an == NULL)
348 		return;
349 
350 	/* Convert the fraction to a simple continued fraction. See
351 	 * https://mathforum.org/dr.math/faq/faq.fractions.html
352 	 * Stop if the current term is bigger than or equal to the given
353 	 * threshold.
354 	 */
355 	x = *numerator;
356 	y = *denominator;
357 
358 	for (n = 0; n < n_terms && y != 0; ++n) {
359 		an[n] = x / y;
360 		if (an[n] >= threshold) {
361 			if (n < 2)
362 				n++;
363 			break;
364 		}
365 
366 		r = x - an[n] * y;
367 		x = y;
368 		y = r;
369 	}
370 
371 	/* Expand the simple continued fraction back to an integer fraction. */
372 	x = 0;
373 	y = 1;
374 
375 	for (i = n; i > 0; --i) {
376 		r = y;
377 		y = an[i-1] * y + x;
378 		x = r;
379 	}
380 
381 	*numerator = y;
382 	*denominator = x;
383 	kfree(an);
384 }
385 
386 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
387  * to compute numerator / denominator * 10000000 using 32 bit fixed point
388  * arithmetic only.
389  */
390 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator)
391 {
392 	u32 multiplier;
393 
394 	/* Saturate the result if the operation would overflow. */
395 	if (denominator == 0 ||
396 	    numerator/denominator >= ((u32)-1)/10000000)
397 		return (u32)-1;
398 
399 	/* Divide both the denominator and the multiplier by two until
400 	 * numerator * multiplier doesn't overflow. If anyone knows a better
401 	 * algorithm please let me know.
402 	 */
403 	multiplier = 10000000;
404 	while (numerator > ((u32)-1)/multiplier) {
405 		multiplier /= 2;
406 		denominator /= 2;
407 	}
408 
409 	return denominator ? numerator * multiplier / denominator : 0;
410 }
411 
412 /* ------------------------------------------------------------------------
413  * Terminal and unit management
414  */
415 
416 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
417 {
418 	struct uvc_entity *entity;
419 
420 	list_for_each_entry(entity, &dev->entities, list) {
421 		if (entity->id == id)
422 			return entity;
423 	}
424 
425 	return NULL;
426 }
427 
428 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
429 	int id, struct uvc_entity *entity)
430 {
431 	unsigned int i;
432 
433 	if (entity == NULL)
434 		entity = list_entry(&dev->entities, struct uvc_entity, list);
435 
436 	list_for_each_entry_continue(entity, &dev->entities, list) {
437 		for (i = 0; i < entity->bNrInPins; ++i)
438 			if (entity->baSourceID[i] == id)
439 				return entity;
440 	}
441 
442 	return NULL;
443 }
444 
445 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
446 {
447 	struct uvc_streaming *stream;
448 
449 	list_for_each_entry(stream, &dev->streams, list) {
450 		if (stream->header.bTerminalLink == id)
451 			return stream;
452 	}
453 
454 	return NULL;
455 }
456 
457 /* ------------------------------------------------------------------------
458  * Streaming Object Management
459  */
460 
461 static void uvc_stream_delete(struct uvc_streaming *stream)
462 {
463 	if (stream->async_wq)
464 		destroy_workqueue(stream->async_wq);
465 
466 	mutex_destroy(&stream->mutex);
467 
468 	usb_put_intf(stream->intf);
469 
470 	kfree(stream->format);
471 	kfree(stream->header.bmaControls);
472 	kfree(stream);
473 }
474 
475 static struct uvc_streaming *uvc_stream_new(struct uvc_device *dev,
476 					    struct usb_interface *intf)
477 {
478 	struct uvc_streaming *stream;
479 
480 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
481 	if (stream == NULL)
482 		return NULL;
483 
484 	mutex_init(&stream->mutex);
485 
486 	stream->dev = dev;
487 	stream->intf = usb_get_intf(intf);
488 	stream->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
489 
490 	/* Allocate a stream specific work queue for asynchronous tasks. */
491 	stream->async_wq = alloc_workqueue("uvcvideo", WQ_UNBOUND | WQ_HIGHPRI,
492 					   0);
493 	if (!stream->async_wq) {
494 		uvc_stream_delete(stream);
495 		return NULL;
496 	}
497 
498 	return stream;
499 }
500 
501 /* ------------------------------------------------------------------------
502  * Descriptors parsing
503  */
504 
505 static int uvc_parse_format(struct uvc_device *dev,
506 	struct uvc_streaming *streaming, struct uvc_format *format,
507 	u32 **intervals, unsigned char *buffer, int buflen)
508 {
509 	struct usb_interface *intf = streaming->intf;
510 	struct usb_host_interface *alts = intf->cur_altsetting;
511 	struct uvc_format_desc *fmtdesc;
512 	struct uvc_frame *frame;
513 	const unsigned char *start = buffer;
514 	unsigned int width_multiplier = 1;
515 	unsigned int interval;
516 	unsigned int i, n;
517 	u8 ftype;
518 
519 	format->type = buffer[2];
520 	format->index = buffer[3];
521 
522 	switch (buffer[2]) {
523 	case UVC_VS_FORMAT_UNCOMPRESSED:
524 	case UVC_VS_FORMAT_FRAME_BASED:
525 		n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
526 		if (buflen < n) {
527 			uvc_dbg(dev, DESCR,
528 				"device %d videostreaming interface %d FORMAT error\n",
529 				dev->udev->devnum,
530 				alts->desc.bInterfaceNumber);
531 			return -EINVAL;
532 		}
533 
534 		/* Find the format descriptor from its GUID. */
535 		fmtdesc = uvc_format_by_guid(&buffer[5]);
536 
537 		if (fmtdesc != NULL) {
538 			strscpy(format->name, fmtdesc->name,
539 				sizeof(format->name));
540 			format->fcc = fmtdesc->fcc;
541 		} else {
542 			dev_info(&streaming->intf->dev,
543 				 "Unknown video format %pUl\n", &buffer[5]);
544 			snprintf(format->name, sizeof(format->name), "%pUl\n",
545 				&buffer[5]);
546 			format->fcc = 0;
547 		}
548 
549 		format->bpp = buffer[21];
550 
551 		/* Some devices report a format that doesn't match what they
552 		 * really send.
553 		 */
554 		if (dev->quirks & UVC_QUIRK_FORCE_Y8) {
555 			if (format->fcc == V4L2_PIX_FMT_YUYV) {
556 				strscpy(format->name, "Greyscale 8-bit (Y8  )",
557 					sizeof(format->name));
558 				format->fcc = V4L2_PIX_FMT_GREY;
559 				format->bpp = 8;
560 				width_multiplier = 2;
561 			}
562 		}
563 
564 		/* Some devices report bpp that doesn't match the format. */
565 		if (dev->quirks & UVC_QUIRK_FORCE_BPP) {
566 			const struct v4l2_format_info *info =
567 				v4l2_format_info(format->fcc);
568 
569 			if (info) {
570 				unsigned int div = info->hdiv * info->vdiv;
571 
572 				n = info->bpp[0] * div;
573 				for (i = 1; i < info->comp_planes; i++)
574 					n += info->bpp[i];
575 
576 				format->bpp = DIV_ROUND_UP(8 * n, div);
577 			}
578 		}
579 
580 		if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
581 			ftype = UVC_VS_FRAME_UNCOMPRESSED;
582 		} else {
583 			ftype = UVC_VS_FRAME_FRAME_BASED;
584 			if (buffer[27])
585 				format->flags = UVC_FMT_FLAG_COMPRESSED;
586 		}
587 		break;
588 
589 	case UVC_VS_FORMAT_MJPEG:
590 		if (buflen < 11) {
591 			uvc_dbg(dev, DESCR,
592 				"device %d videostreaming interface %d FORMAT error\n",
593 				dev->udev->devnum,
594 				alts->desc.bInterfaceNumber);
595 			return -EINVAL;
596 		}
597 
598 		strscpy(format->name, "MJPEG", sizeof(format->name));
599 		format->fcc = V4L2_PIX_FMT_MJPEG;
600 		format->flags = UVC_FMT_FLAG_COMPRESSED;
601 		format->bpp = 0;
602 		ftype = UVC_VS_FRAME_MJPEG;
603 		break;
604 
605 	case UVC_VS_FORMAT_DV:
606 		if (buflen < 9) {
607 			uvc_dbg(dev, DESCR,
608 				"device %d videostreaming interface %d FORMAT error\n",
609 				dev->udev->devnum,
610 				alts->desc.bInterfaceNumber);
611 			return -EINVAL;
612 		}
613 
614 		switch (buffer[8] & 0x7f) {
615 		case 0:
616 			strscpy(format->name, "SD-DV", sizeof(format->name));
617 			break;
618 		case 1:
619 			strscpy(format->name, "SDL-DV", sizeof(format->name));
620 			break;
621 		case 2:
622 			strscpy(format->name, "HD-DV", sizeof(format->name));
623 			break;
624 		default:
625 			uvc_dbg(dev, DESCR,
626 				"device %d videostreaming interface %d: unknown DV format %u\n",
627 				dev->udev->devnum,
628 				alts->desc.bInterfaceNumber, buffer[8]);
629 			return -EINVAL;
630 		}
631 
632 		strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
633 			sizeof(format->name));
634 
635 		format->fcc = V4L2_PIX_FMT_DV;
636 		format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
637 		format->bpp = 0;
638 		ftype = 0;
639 
640 		/* Create a dummy frame descriptor. */
641 		frame = &format->frame[0];
642 		memset(&format->frame[0], 0, sizeof(format->frame[0]));
643 		frame->bFrameIntervalType = 1;
644 		frame->dwDefaultFrameInterval = 1;
645 		frame->dwFrameInterval = *intervals;
646 		*(*intervals)++ = 1;
647 		format->nframes = 1;
648 		break;
649 
650 	case UVC_VS_FORMAT_MPEG2TS:
651 	case UVC_VS_FORMAT_STREAM_BASED:
652 		/* Not supported yet. */
653 	default:
654 		uvc_dbg(dev, DESCR,
655 			"device %d videostreaming interface %d unsupported format %u\n",
656 			dev->udev->devnum, alts->desc.bInterfaceNumber,
657 			buffer[2]);
658 		return -EINVAL;
659 	}
660 
661 	uvc_dbg(dev, DESCR, "Found format %s\n", format->name);
662 
663 	buflen -= buffer[0];
664 	buffer += buffer[0];
665 
666 	/* Parse the frame descriptors. Only uncompressed, MJPEG and frame
667 	 * based formats have frame descriptors.
668 	 */
669 	while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
670 	       buffer[2] == ftype) {
671 		frame = &format->frame[format->nframes];
672 		if (ftype != UVC_VS_FRAME_FRAME_BASED)
673 			n = buflen > 25 ? buffer[25] : 0;
674 		else
675 			n = buflen > 21 ? buffer[21] : 0;
676 
677 		n = n ? n : 3;
678 
679 		if (buflen < 26 + 4*n) {
680 			uvc_dbg(dev, DESCR,
681 				"device %d videostreaming interface %d FRAME error\n",
682 				dev->udev->devnum,
683 				alts->desc.bInterfaceNumber);
684 			return -EINVAL;
685 		}
686 
687 		frame->bFrameIndex = buffer[3];
688 		frame->bmCapabilities = buffer[4];
689 		frame->wWidth = get_unaligned_le16(&buffer[5])
690 			      * width_multiplier;
691 		frame->wHeight = get_unaligned_le16(&buffer[7]);
692 		frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
693 		frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
694 		if (ftype != UVC_VS_FRAME_FRAME_BASED) {
695 			frame->dwMaxVideoFrameBufferSize =
696 				get_unaligned_le32(&buffer[17]);
697 			frame->dwDefaultFrameInterval =
698 				get_unaligned_le32(&buffer[21]);
699 			frame->bFrameIntervalType = buffer[25];
700 		} else {
701 			frame->dwMaxVideoFrameBufferSize = 0;
702 			frame->dwDefaultFrameInterval =
703 				get_unaligned_le32(&buffer[17]);
704 			frame->bFrameIntervalType = buffer[21];
705 		}
706 		frame->dwFrameInterval = *intervals;
707 
708 		/* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
709 		 * completely. Observed behaviours range from setting the
710 		 * value to 1.1x the actual frame size to hardwiring the
711 		 * 16 low bits to 0. This results in a higher than necessary
712 		 * memory usage as well as a wrong image size information. For
713 		 * uncompressed formats this can be fixed by computing the
714 		 * value from the frame size.
715 		 */
716 		if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
717 			frame->dwMaxVideoFrameBufferSize = format->bpp
718 				* frame->wWidth * frame->wHeight / 8;
719 
720 		/* Some bogus devices report dwMinFrameInterval equal to
721 		 * dwMaxFrameInterval and have dwFrameIntervalStep set to
722 		 * zero. Setting all null intervals to 1 fixes the problem and
723 		 * some other divisions by zero that could happen.
724 		 */
725 		for (i = 0; i < n; ++i) {
726 			interval = get_unaligned_le32(&buffer[26+4*i]);
727 			*(*intervals)++ = interval ? interval : 1;
728 		}
729 
730 		/* Make sure that the default frame interval stays between
731 		 * the boundaries.
732 		 */
733 		n -= frame->bFrameIntervalType ? 1 : 2;
734 		frame->dwDefaultFrameInterval =
735 			min(frame->dwFrameInterval[n],
736 			    max(frame->dwFrameInterval[0],
737 				frame->dwDefaultFrameInterval));
738 
739 		if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
740 			frame->bFrameIntervalType = 1;
741 			frame->dwFrameInterval[0] =
742 				frame->dwDefaultFrameInterval;
743 		}
744 
745 		uvc_dbg(dev, DESCR, "- %ux%u (%u.%u fps)\n",
746 			frame->wWidth, frame->wHeight,
747 			10000000 / frame->dwDefaultFrameInterval,
748 			(100000000 / frame->dwDefaultFrameInterval) % 10);
749 
750 		format->nframes++;
751 		buflen -= buffer[0];
752 		buffer += buffer[0];
753 	}
754 
755 	if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
756 	    buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
757 		buflen -= buffer[0];
758 		buffer += buffer[0];
759 	}
760 
761 	if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
762 	    buffer[2] == UVC_VS_COLORFORMAT) {
763 		if (buflen < 6) {
764 			uvc_dbg(dev, DESCR,
765 				"device %d videostreaming interface %d COLORFORMAT error\n",
766 				dev->udev->devnum,
767 				alts->desc.bInterfaceNumber);
768 			return -EINVAL;
769 		}
770 
771 		format->colorspace = uvc_colorspace(buffer[3]);
772 		format->xfer_func = uvc_xfer_func(buffer[4]);
773 		format->ycbcr_enc = uvc_ycbcr_enc(buffer[5]);
774 
775 		buflen -= buffer[0];
776 		buffer += buffer[0];
777 	} else {
778 		format->colorspace = V4L2_COLORSPACE_SRGB;
779 	}
780 
781 	return buffer - start;
782 }
783 
784 static int uvc_parse_streaming(struct uvc_device *dev,
785 	struct usb_interface *intf)
786 {
787 	struct uvc_streaming *streaming = NULL;
788 	struct uvc_format *format;
789 	struct uvc_frame *frame;
790 	struct usb_host_interface *alts = &intf->altsetting[0];
791 	unsigned char *_buffer, *buffer = alts->extra;
792 	int _buflen, buflen = alts->extralen;
793 	unsigned int nformats = 0, nframes = 0, nintervals = 0;
794 	unsigned int size, i, n, p;
795 	u32 *interval;
796 	u16 psize;
797 	int ret = -EINVAL;
798 
799 	if (intf->cur_altsetting->desc.bInterfaceSubClass
800 		!= UVC_SC_VIDEOSTREAMING) {
801 		uvc_dbg(dev, DESCR,
802 			"device %d interface %d isn't a video streaming interface\n",
803 			dev->udev->devnum,
804 			intf->altsetting[0].desc.bInterfaceNumber);
805 		return -EINVAL;
806 	}
807 
808 	if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
809 		uvc_dbg(dev, DESCR,
810 			"device %d interface %d is already claimed\n",
811 			dev->udev->devnum,
812 			intf->altsetting[0].desc.bInterfaceNumber);
813 		return -EINVAL;
814 	}
815 
816 	streaming = uvc_stream_new(dev, intf);
817 	if (streaming == NULL) {
818 		usb_driver_release_interface(&uvc_driver.driver, intf);
819 		return -ENOMEM;
820 	}
821 
822 	/* The Pico iMage webcam has its class-specific interface descriptors
823 	 * after the endpoint descriptors.
824 	 */
825 	if (buflen == 0) {
826 		for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
827 			struct usb_host_endpoint *ep = &alts->endpoint[i];
828 
829 			if (ep->extralen == 0)
830 				continue;
831 
832 			if (ep->extralen > 2 &&
833 			    ep->extra[1] == USB_DT_CS_INTERFACE) {
834 				uvc_dbg(dev, DESCR,
835 					"trying extra data from endpoint %u\n",
836 					i);
837 				buffer = alts->endpoint[i].extra;
838 				buflen = alts->endpoint[i].extralen;
839 				break;
840 			}
841 		}
842 	}
843 
844 	/* Skip the standard interface descriptors. */
845 	while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
846 		buflen -= buffer[0];
847 		buffer += buffer[0];
848 	}
849 
850 	if (buflen <= 2) {
851 		uvc_dbg(dev, DESCR,
852 			"no class-specific streaming interface descriptors found\n");
853 		goto error;
854 	}
855 
856 	/* Parse the header descriptor. */
857 	switch (buffer[2]) {
858 	case UVC_VS_OUTPUT_HEADER:
859 		streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
860 		size = 9;
861 		break;
862 
863 	case UVC_VS_INPUT_HEADER:
864 		streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
865 		size = 13;
866 		break;
867 
868 	default:
869 		uvc_dbg(dev, DESCR,
870 			"device %d videostreaming interface %d HEADER descriptor not found\n",
871 			dev->udev->devnum, alts->desc.bInterfaceNumber);
872 		goto error;
873 	}
874 
875 	p = buflen >= 4 ? buffer[3] : 0;
876 	n = buflen >= size ? buffer[size-1] : 0;
877 
878 	if (buflen < size + p*n) {
879 		uvc_dbg(dev, DESCR,
880 			"device %d videostreaming interface %d HEADER descriptor is invalid\n",
881 			dev->udev->devnum, alts->desc.bInterfaceNumber);
882 		goto error;
883 	}
884 
885 	streaming->header.bNumFormats = p;
886 	streaming->header.bEndpointAddress = buffer[6];
887 	if (buffer[2] == UVC_VS_INPUT_HEADER) {
888 		streaming->header.bmInfo = buffer[7];
889 		streaming->header.bTerminalLink = buffer[8];
890 		streaming->header.bStillCaptureMethod = buffer[9];
891 		streaming->header.bTriggerSupport = buffer[10];
892 		streaming->header.bTriggerUsage = buffer[11];
893 	} else {
894 		streaming->header.bTerminalLink = buffer[7];
895 	}
896 	streaming->header.bControlSize = n;
897 
898 	streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
899 						GFP_KERNEL);
900 	if (streaming->header.bmaControls == NULL) {
901 		ret = -ENOMEM;
902 		goto error;
903 	}
904 
905 	buflen -= buffer[0];
906 	buffer += buffer[0];
907 
908 	_buffer = buffer;
909 	_buflen = buflen;
910 
911 	/* Count the format and frame descriptors. */
912 	while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
913 		switch (_buffer[2]) {
914 		case UVC_VS_FORMAT_UNCOMPRESSED:
915 		case UVC_VS_FORMAT_MJPEG:
916 		case UVC_VS_FORMAT_FRAME_BASED:
917 			nformats++;
918 			break;
919 
920 		case UVC_VS_FORMAT_DV:
921 			/* DV format has no frame descriptor. We will create a
922 			 * dummy frame descriptor with a dummy frame interval.
923 			 */
924 			nformats++;
925 			nframes++;
926 			nintervals++;
927 			break;
928 
929 		case UVC_VS_FORMAT_MPEG2TS:
930 		case UVC_VS_FORMAT_STREAM_BASED:
931 			uvc_dbg(dev, DESCR,
932 				"device %d videostreaming interface %d FORMAT %u is not supported\n",
933 				dev->udev->devnum,
934 				alts->desc.bInterfaceNumber, _buffer[2]);
935 			break;
936 
937 		case UVC_VS_FRAME_UNCOMPRESSED:
938 		case UVC_VS_FRAME_MJPEG:
939 			nframes++;
940 			if (_buflen > 25)
941 				nintervals += _buffer[25] ? _buffer[25] : 3;
942 			break;
943 
944 		case UVC_VS_FRAME_FRAME_BASED:
945 			nframes++;
946 			if (_buflen > 21)
947 				nintervals += _buffer[21] ? _buffer[21] : 3;
948 			break;
949 		}
950 
951 		_buflen -= _buffer[0];
952 		_buffer += _buffer[0];
953 	}
954 
955 	if (nformats == 0) {
956 		uvc_dbg(dev, DESCR,
957 			"device %d videostreaming interface %d has no supported formats defined\n",
958 			dev->udev->devnum, alts->desc.bInterfaceNumber);
959 		goto error;
960 	}
961 
962 	size = nformats * sizeof(*format) + nframes * sizeof(*frame)
963 	     + nintervals * sizeof(*interval);
964 	format = kzalloc(size, GFP_KERNEL);
965 	if (format == NULL) {
966 		ret = -ENOMEM;
967 		goto error;
968 	}
969 
970 	frame = (struct uvc_frame *)&format[nformats];
971 	interval = (u32 *)&frame[nframes];
972 
973 	streaming->format = format;
974 	streaming->nformats = nformats;
975 
976 	/* Parse the format descriptors. */
977 	while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
978 		switch (buffer[2]) {
979 		case UVC_VS_FORMAT_UNCOMPRESSED:
980 		case UVC_VS_FORMAT_MJPEG:
981 		case UVC_VS_FORMAT_DV:
982 		case UVC_VS_FORMAT_FRAME_BASED:
983 			format->frame = frame;
984 			ret = uvc_parse_format(dev, streaming, format,
985 				&interval, buffer, buflen);
986 			if (ret < 0)
987 				goto error;
988 
989 			frame += format->nframes;
990 			format++;
991 
992 			buflen -= ret;
993 			buffer += ret;
994 			continue;
995 
996 		default:
997 			break;
998 		}
999 
1000 		buflen -= buffer[0];
1001 		buffer += buffer[0];
1002 	}
1003 
1004 	if (buflen)
1005 		uvc_dbg(dev, DESCR,
1006 			"device %d videostreaming interface %d has %u bytes of trailing descriptor garbage\n",
1007 			dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
1008 
1009 	/* Parse the alternate settings to find the maximum bandwidth. */
1010 	for (i = 0; i < intf->num_altsetting; ++i) {
1011 		struct usb_host_endpoint *ep;
1012 		alts = &intf->altsetting[i];
1013 		ep = uvc_find_endpoint(alts,
1014 				streaming->header.bEndpointAddress);
1015 		if (ep == NULL)
1016 			continue;
1017 		psize = uvc_endpoint_max_bpi(dev->udev, ep);
1018 		if (psize > streaming->maxpsize)
1019 			streaming->maxpsize = psize;
1020 	}
1021 
1022 	list_add_tail(&streaming->list, &dev->streams);
1023 	return 0;
1024 
1025 error:
1026 	usb_driver_release_interface(&uvc_driver.driver, intf);
1027 	uvc_stream_delete(streaming);
1028 	return ret;
1029 }
1030 
1031 static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
1032 static const u8 uvc_gpio_guid[16] = UVC_GUID_EXT_GPIO_CONTROLLER;
1033 static const u8 uvc_media_transport_input_guid[16] =
1034 	UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
1035 static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
1036 
1037 static struct uvc_entity *uvc_alloc_entity(u16 type, u16 id,
1038 		unsigned int num_pads, unsigned int extra_size)
1039 {
1040 	struct uvc_entity *entity;
1041 	unsigned int num_inputs;
1042 	unsigned int size;
1043 	unsigned int i;
1044 
1045 	extra_size = roundup(extra_size, sizeof(*entity->pads));
1046 	if (num_pads)
1047 		num_inputs = type & UVC_TERM_OUTPUT ? num_pads : num_pads - 1;
1048 	else
1049 		num_inputs = 0;
1050 	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
1051 	     + num_inputs;
1052 	entity = kzalloc(size, GFP_KERNEL);
1053 	if (entity == NULL)
1054 		return NULL;
1055 
1056 	entity->id = id;
1057 	entity->type = type;
1058 
1059 	/*
1060 	 * Set the GUID for standard entity types. For extension units, the GUID
1061 	 * is initialized by the caller.
1062 	 */
1063 	switch (type) {
1064 	case UVC_EXT_GPIO_UNIT:
1065 		memcpy(entity->guid, uvc_gpio_guid, 16);
1066 		break;
1067 	case UVC_ITT_CAMERA:
1068 		memcpy(entity->guid, uvc_camera_guid, 16);
1069 		break;
1070 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1071 		memcpy(entity->guid, uvc_media_transport_input_guid, 16);
1072 		break;
1073 	case UVC_VC_PROCESSING_UNIT:
1074 		memcpy(entity->guid, uvc_processing_guid, 16);
1075 		break;
1076 	}
1077 
1078 	entity->num_links = 0;
1079 	entity->num_pads = num_pads;
1080 	entity->pads = ((void *)(entity + 1)) + extra_size;
1081 
1082 	for (i = 0; i < num_inputs; ++i)
1083 		entity->pads[i].flags = MEDIA_PAD_FL_SINK;
1084 	if (!UVC_ENTITY_IS_OTERM(entity) && num_pads)
1085 		entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
1086 
1087 	entity->bNrInPins = num_inputs;
1088 	entity->baSourceID = (u8 *)(&entity->pads[num_pads]);
1089 
1090 	return entity;
1091 }
1092 
1093 /* Parse vendor-specific extensions. */
1094 static int uvc_parse_vendor_control(struct uvc_device *dev,
1095 	const unsigned char *buffer, int buflen)
1096 {
1097 	struct usb_device *udev = dev->udev;
1098 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
1099 	struct uvc_entity *unit;
1100 	unsigned int n, p;
1101 	int handled = 0;
1102 
1103 	switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
1104 	case 0x046d:		/* Logitech */
1105 		if (buffer[1] != 0x41 || buffer[2] != 0x01)
1106 			break;
1107 
1108 		/* Logitech implements several vendor specific functions
1109 		 * through vendor specific extension units (LXU).
1110 		 *
1111 		 * The LXU descriptors are similar to XU descriptors
1112 		 * (see "USB Device Video Class for Video Devices", section
1113 		 * 3.7.2.6 "Extension Unit Descriptor") with the following
1114 		 * differences:
1115 		 *
1116 		 * ----------------------------------------------------------
1117 		 * 0		bLength		1	 Number
1118 		 *	Size of this descriptor, in bytes: 24+p+n*2
1119 		 * ----------------------------------------------------------
1120 		 * 23+p+n	bmControlsType	N	Bitmap
1121 		 *	Individual bits in the set are defined:
1122 		 *	0: Absolute
1123 		 *	1: Relative
1124 		 *
1125 		 *	This bitset is mapped exactly the same as bmControls.
1126 		 * ----------------------------------------------------------
1127 		 * 23+p+n*2	bReserved	1	Boolean
1128 		 * ----------------------------------------------------------
1129 		 * 24+p+n*2	iExtension	1	Index
1130 		 *	Index of a string descriptor that describes this
1131 		 *	extension unit.
1132 		 * ----------------------------------------------------------
1133 		 */
1134 		p = buflen >= 22 ? buffer[21] : 0;
1135 		n = buflen >= 25 + p ? buffer[22+p] : 0;
1136 
1137 		if (buflen < 25 + p + 2*n) {
1138 			uvc_dbg(dev, DESCR,
1139 				"device %d videocontrol interface %d EXTENSION_UNIT error\n",
1140 				udev->devnum, alts->desc.bInterfaceNumber);
1141 			break;
1142 		}
1143 
1144 		unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
1145 					p + 1, 2*n);
1146 		if (unit == NULL)
1147 			return -ENOMEM;
1148 
1149 		memcpy(unit->guid, &buffer[4], 16);
1150 		unit->extension.bNumControls = buffer[20];
1151 		memcpy(unit->baSourceID, &buffer[22], p);
1152 		unit->extension.bControlSize = buffer[22+p];
1153 		unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
1154 		unit->extension.bmControlsType = (u8 *)unit + sizeof(*unit)
1155 					       + n;
1156 		memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
1157 
1158 		if (buffer[24+p+2*n] != 0)
1159 			usb_string(udev, buffer[24+p+2*n], unit->name,
1160 				   sizeof(unit->name));
1161 		else
1162 			sprintf(unit->name, "Extension %u", buffer[3]);
1163 
1164 		list_add_tail(&unit->list, &dev->entities);
1165 		handled = 1;
1166 		break;
1167 	}
1168 
1169 	return handled;
1170 }
1171 
1172 static int uvc_parse_standard_control(struct uvc_device *dev,
1173 	const unsigned char *buffer, int buflen)
1174 {
1175 	struct usb_device *udev = dev->udev;
1176 	struct uvc_entity *unit, *term;
1177 	struct usb_interface *intf;
1178 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
1179 	unsigned int i, n, p, len;
1180 	u16 type;
1181 
1182 	switch (buffer[2]) {
1183 	case UVC_VC_HEADER:
1184 		n = buflen >= 12 ? buffer[11] : 0;
1185 
1186 		if (buflen < 12 + n) {
1187 			uvc_dbg(dev, DESCR,
1188 				"device %d videocontrol interface %d HEADER error\n",
1189 				udev->devnum, alts->desc.bInterfaceNumber);
1190 			return -EINVAL;
1191 		}
1192 
1193 		dev->uvc_version = get_unaligned_le16(&buffer[3]);
1194 		dev->clock_frequency = get_unaligned_le32(&buffer[7]);
1195 
1196 		/* Parse all USB Video Streaming interfaces. */
1197 		for (i = 0; i < n; ++i) {
1198 			intf = usb_ifnum_to_if(udev, buffer[12+i]);
1199 			if (intf == NULL) {
1200 				uvc_dbg(dev, DESCR,
1201 					"device %d interface %d doesn't exists\n",
1202 					udev->devnum, i);
1203 				continue;
1204 			}
1205 
1206 			uvc_parse_streaming(dev, intf);
1207 		}
1208 		break;
1209 
1210 	case UVC_VC_INPUT_TERMINAL:
1211 		if (buflen < 8) {
1212 			uvc_dbg(dev, DESCR,
1213 				"device %d videocontrol interface %d INPUT_TERMINAL error\n",
1214 				udev->devnum, alts->desc.bInterfaceNumber);
1215 			return -EINVAL;
1216 		}
1217 
1218 		/*
1219 		 * Reject invalid terminal types that would cause issues:
1220 		 *
1221 		 * - The high byte must be non-zero, otherwise it would be
1222 		 *   confused with a unit.
1223 		 *
1224 		 * - Bit 15 must be 0, as we use it internally as a terminal
1225 		 *   direction flag.
1226 		 *
1227 		 * Other unknown types are accepted.
1228 		 */
1229 		type = get_unaligned_le16(&buffer[4]);
1230 		if ((type & 0x7f00) == 0 || (type & 0x8000) != 0) {
1231 			uvc_dbg(dev, DESCR,
1232 				"device %d videocontrol interface %d INPUT_TERMINAL %d has invalid type 0x%04x, skipping\n",
1233 				udev->devnum, alts->desc.bInterfaceNumber,
1234 				buffer[3], type);
1235 			return 0;
1236 		}
1237 
1238 		n = 0;
1239 		p = 0;
1240 		len = 8;
1241 
1242 		if (type == UVC_ITT_CAMERA) {
1243 			n = buflen >= 15 ? buffer[14] : 0;
1244 			len = 15;
1245 
1246 		} else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
1247 			n = buflen >= 9 ? buffer[8] : 0;
1248 			p = buflen >= 10 + n ? buffer[9+n] : 0;
1249 			len = 10;
1250 		}
1251 
1252 		if (buflen < len + n + p) {
1253 			uvc_dbg(dev, DESCR,
1254 				"device %d videocontrol interface %d INPUT_TERMINAL error\n",
1255 				udev->devnum, alts->desc.bInterfaceNumber);
1256 			return -EINVAL;
1257 		}
1258 
1259 		term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
1260 					1, n + p);
1261 		if (term == NULL)
1262 			return -ENOMEM;
1263 
1264 		if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
1265 			term->camera.bControlSize = n;
1266 			term->camera.bmControls = (u8 *)term + sizeof(*term);
1267 			term->camera.wObjectiveFocalLengthMin =
1268 				get_unaligned_le16(&buffer[8]);
1269 			term->camera.wObjectiveFocalLengthMax =
1270 				get_unaligned_le16(&buffer[10]);
1271 			term->camera.wOcularFocalLength =
1272 				get_unaligned_le16(&buffer[12]);
1273 			memcpy(term->camera.bmControls, &buffer[15], n);
1274 		} else if (UVC_ENTITY_TYPE(term) ==
1275 			   UVC_ITT_MEDIA_TRANSPORT_INPUT) {
1276 			term->media.bControlSize = n;
1277 			term->media.bmControls = (u8 *)term + sizeof(*term);
1278 			term->media.bTransportModeSize = p;
1279 			term->media.bmTransportModes = (u8 *)term
1280 						     + sizeof(*term) + n;
1281 			memcpy(term->media.bmControls, &buffer[9], n);
1282 			memcpy(term->media.bmTransportModes, &buffer[10+n], p);
1283 		}
1284 
1285 		if (buffer[7] != 0)
1286 			usb_string(udev, buffer[7], term->name,
1287 				   sizeof(term->name));
1288 		else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
1289 			sprintf(term->name, "Camera %u", buffer[3]);
1290 		else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
1291 			sprintf(term->name, "Media %u", buffer[3]);
1292 		else
1293 			sprintf(term->name, "Input %u", buffer[3]);
1294 
1295 		list_add_tail(&term->list, &dev->entities);
1296 		break;
1297 
1298 	case UVC_VC_OUTPUT_TERMINAL:
1299 		if (buflen < 9) {
1300 			uvc_dbg(dev, DESCR,
1301 				"device %d videocontrol interface %d OUTPUT_TERMINAL error\n",
1302 				udev->devnum, alts->desc.bInterfaceNumber);
1303 			return -EINVAL;
1304 		}
1305 
1306 		/* Make sure the terminal type MSB is not null, otherwise it
1307 		 * could be confused with a unit.
1308 		 */
1309 		type = get_unaligned_le16(&buffer[4]);
1310 		if ((type & 0xff00) == 0) {
1311 			uvc_dbg(dev, DESCR,
1312 				"device %d videocontrol interface %d OUTPUT_TERMINAL %d has invalid type 0x%04x, skipping\n",
1313 				udev->devnum, alts->desc.bInterfaceNumber,
1314 				buffer[3], type);
1315 			return 0;
1316 		}
1317 
1318 		term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
1319 					1, 0);
1320 		if (term == NULL)
1321 			return -ENOMEM;
1322 
1323 		memcpy(term->baSourceID, &buffer[7], 1);
1324 
1325 		if (buffer[8] != 0)
1326 			usb_string(udev, buffer[8], term->name,
1327 				   sizeof(term->name));
1328 		else
1329 			sprintf(term->name, "Output %u", buffer[3]);
1330 
1331 		list_add_tail(&term->list, &dev->entities);
1332 		break;
1333 
1334 	case UVC_VC_SELECTOR_UNIT:
1335 		p = buflen >= 5 ? buffer[4] : 0;
1336 
1337 		if (buflen < 5 || buflen < 6 + p) {
1338 			uvc_dbg(dev, DESCR,
1339 				"device %d videocontrol interface %d SELECTOR_UNIT error\n",
1340 				udev->devnum, alts->desc.bInterfaceNumber);
1341 			return -EINVAL;
1342 		}
1343 
1344 		unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
1345 		if (unit == NULL)
1346 			return -ENOMEM;
1347 
1348 		memcpy(unit->baSourceID, &buffer[5], p);
1349 
1350 		if (buffer[5+p] != 0)
1351 			usb_string(udev, buffer[5+p], unit->name,
1352 				   sizeof(unit->name));
1353 		else
1354 			sprintf(unit->name, "Selector %u", buffer[3]);
1355 
1356 		list_add_tail(&unit->list, &dev->entities);
1357 		break;
1358 
1359 	case UVC_VC_PROCESSING_UNIT:
1360 		n = buflen >= 8 ? buffer[7] : 0;
1361 		p = dev->uvc_version >= 0x0110 ? 10 : 9;
1362 
1363 		if (buflen < p + n) {
1364 			uvc_dbg(dev, DESCR,
1365 				"device %d videocontrol interface %d PROCESSING_UNIT error\n",
1366 				udev->devnum, alts->desc.bInterfaceNumber);
1367 			return -EINVAL;
1368 		}
1369 
1370 		unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
1371 		if (unit == NULL)
1372 			return -ENOMEM;
1373 
1374 		memcpy(unit->baSourceID, &buffer[4], 1);
1375 		unit->processing.wMaxMultiplier =
1376 			get_unaligned_le16(&buffer[5]);
1377 		unit->processing.bControlSize = buffer[7];
1378 		unit->processing.bmControls = (u8 *)unit + sizeof(*unit);
1379 		memcpy(unit->processing.bmControls, &buffer[8], n);
1380 		if (dev->uvc_version >= 0x0110)
1381 			unit->processing.bmVideoStandards = buffer[9+n];
1382 
1383 		if (buffer[8+n] != 0)
1384 			usb_string(udev, buffer[8+n], unit->name,
1385 				   sizeof(unit->name));
1386 		else
1387 			sprintf(unit->name, "Processing %u", buffer[3]);
1388 
1389 		list_add_tail(&unit->list, &dev->entities);
1390 		break;
1391 
1392 	case UVC_VC_EXTENSION_UNIT:
1393 		p = buflen >= 22 ? buffer[21] : 0;
1394 		n = buflen >= 24 + p ? buffer[22+p] : 0;
1395 
1396 		if (buflen < 24 + p + n) {
1397 			uvc_dbg(dev, DESCR,
1398 				"device %d videocontrol interface %d EXTENSION_UNIT error\n",
1399 				udev->devnum, alts->desc.bInterfaceNumber);
1400 			return -EINVAL;
1401 		}
1402 
1403 		unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
1404 		if (unit == NULL)
1405 			return -ENOMEM;
1406 
1407 		memcpy(unit->guid, &buffer[4], 16);
1408 		unit->extension.bNumControls = buffer[20];
1409 		memcpy(unit->baSourceID, &buffer[22], p);
1410 		unit->extension.bControlSize = buffer[22+p];
1411 		unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
1412 		memcpy(unit->extension.bmControls, &buffer[23+p], n);
1413 
1414 		if (buffer[23+p+n] != 0)
1415 			usb_string(udev, buffer[23+p+n], unit->name,
1416 				   sizeof(unit->name));
1417 		else
1418 			sprintf(unit->name, "Extension %u", buffer[3]);
1419 
1420 		list_add_tail(&unit->list, &dev->entities);
1421 		break;
1422 
1423 	default:
1424 		uvc_dbg(dev, DESCR,
1425 			"Found an unknown CS_INTERFACE descriptor (%u)\n",
1426 			buffer[2]);
1427 		break;
1428 	}
1429 
1430 	return 0;
1431 }
1432 
1433 static int uvc_parse_control(struct uvc_device *dev)
1434 {
1435 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
1436 	unsigned char *buffer = alts->extra;
1437 	int buflen = alts->extralen;
1438 	int ret;
1439 
1440 	/* Parse the default alternate setting only, as the UVC specification
1441 	 * defines a single alternate setting, the default alternate setting
1442 	 * zero.
1443 	 */
1444 
1445 	while (buflen > 2) {
1446 		if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1447 		    buffer[1] != USB_DT_CS_INTERFACE)
1448 			goto next_descriptor;
1449 
1450 		if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1451 			return ret;
1452 
1453 next_descriptor:
1454 		buflen -= buffer[0];
1455 		buffer += buffer[0];
1456 	}
1457 
1458 	/* Check if the optional status endpoint is present. Built-in iSight
1459 	 * webcams have an interrupt endpoint but spit proprietary data that
1460 	 * don't conform to the UVC status endpoint messages. Don't try to
1461 	 * handle the interrupt endpoint for those cameras.
1462 	 */
1463 	if (alts->desc.bNumEndpoints == 1 &&
1464 	    !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1465 		struct usb_host_endpoint *ep = &alts->endpoint[0];
1466 		struct usb_endpoint_descriptor *desc = &ep->desc;
1467 
1468 		if (usb_endpoint_is_int_in(desc) &&
1469 		    le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1470 		    desc->bInterval != 0) {
1471 			uvc_dbg(dev, DESCR,
1472 				"Found a Status endpoint (addr %02x)\n",
1473 				desc->bEndpointAddress);
1474 			dev->int_ep = ep;
1475 		}
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 /* -----------------------------------------------------------------------------
1482  * Privacy GPIO
1483  */
1484 
1485 static void uvc_gpio_event(struct uvc_device *dev)
1486 {
1487 	struct uvc_entity *unit = dev->gpio_unit;
1488 	struct uvc_video_chain *chain;
1489 	u8 new_val;
1490 
1491 	if (!unit)
1492 		return;
1493 
1494 	new_val = gpiod_get_value_cansleep(unit->gpio.gpio_privacy);
1495 
1496 	/* GPIO entities are always on the first chain. */
1497 	chain = list_first_entry(&dev->chains, struct uvc_video_chain, list);
1498 	uvc_ctrl_status_event(chain, unit->controls, &new_val);
1499 }
1500 
1501 static int uvc_gpio_get_cur(struct uvc_device *dev, struct uvc_entity *entity,
1502 			    u8 cs, void *data, u16 size)
1503 {
1504 	if (cs != UVC_CT_PRIVACY_CONTROL || size < 1)
1505 		return -EINVAL;
1506 
1507 	*(u8 *)data = gpiod_get_value_cansleep(entity->gpio.gpio_privacy);
1508 
1509 	return 0;
1510 }
1511 
1512 static int uvc_gpio_get_info(struct uvc_device *dev, struct uvc_entity *entity,
1513 			     u8 cs, u8 *caps)
1514 {
1515 	if (cs != UVC_CT_PRIVACY_CONTROL)
1516 		return -EINVAL;
1517 
1518 	*caps = UVC_CONTROL_CAP_GET | UVC_CONTROL_CAP_AUTOUPDATE;
1519 	return 0;
1520 }
1521 
1522 static irqreturn_t uvc_gpio_irq(int irq, void *data)
1523 {
1524 	struct uvc_device *dev = data;
1525 
1526 	uvc_gpio_event(dev);
1527 	return IRQ_HANDLED;
1528 }
1529 
1530 static int uvc_gpio_parse(struct uvc_device *dev)
1531 {
1532 	struct uvc_entity *unit;
1533 	struct gpio_desc *gpio_privacy;
1534 	int irq;
1535 
1536 	gpio_privacy = devm_gpiod_get_optional(&dev->udev->dev, "privacy",
1537 					       GPIOD_IN);
1538 	if (IS_ERR_OR_NULL(gpio_privacy))
1539 		return PTR_ERR_OR_ZERO(gpio_privacy);
1540 
1541 	unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
1542 	if (!unit)
1543 		return -ENOMEM;
1544 
1545 	irq = gpiod_to_irq(gpio_privacy);
1546 	if (irq < 0) {
1547 		if (irq != EPROBE_DEFER)
1548 			dev_err(&dev->udev->dev,
1549 				"No IRQ for privacy GPIO (%d)\n", irq);
1550 		return irq;
1551 	}
1552 
1553 	unit->gpio.gpio_privacy = gpio_privacy;
1554 	unit->gpio.irq = irq;
1555 	unit->gpio.bControlSize = 1;
1556 	unit->gpio.bmControls = (u8 *)unit + sizeof(*unit);
1557 	unit->gpio.bmControls[0] = 1;
1558 	unit->get_cur = uvc_gpio_get_cur;
1559 	unit->get_info = uvc_gpio_get_info;
1560 	strscpy(unit->name, "GPIO", sizeof(unit->name));
1561 
1562 	list_add_tail(&unit->list, &dev->entities);
1563 
1564 	dev->gpio_unit = unit;
1565 
1566 	return 0;
1567 }
1568 
1569 static int uvc_gpio_init_irq(struct uvc_device *dev)
1570 {
1571 	struct uvc_entity *unit = dev->gpio_unit;
1572 
1573 	if (!unit || unit->gpio.irq < 0)
1574 		return 0;
1575 
1576 	return devm_request_threaded_irq(&dev->udev->dev, unit->gpio.irq, NULL,
1577 					 uvc_gpio_irq,
1578 					 IRQF_ONESHOT | IRQF_TRIGGER_FALLING |
1579 					 IRQF_TRIGGER_RISING,
1580 					 "uvc_privacy_gpio", dev);
1581 }
1582 
1583 /* ------------------------------------------------------------------------
1584  * UVC device scan
1585  */
1586 
1587 /*
1588  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1589  * and containing the following units:
1590  *
1591  * - one or more Output Terminals (USB Streaming or Display)
1592  * - zero or one Processing Unit
1593  * - zero, one or more single-input Selector Units
1594  * - zero or one multiple-input Selector Units, provided all inputs are
1595  *   connected to input terminals
1596  * - zero, one or mode single-input Extension Units
1597  * - one or more Input Terminals (Camera, External or USB Streaming)
1598  *
1599  * The terminal and units must match on of the following structures:
1600  *
1601  * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
1602  * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
1603  * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
1604  *
1605  *                 +---------+    +---------+ -> OTT_*(0)
1606  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
1607  *                 +---------+    +---------+ -> OTT_*(n)
1608  *
1609  * The Processing Unit and Extension Units can be in any order. Additional
1610  * Extension Units connected to the main chain as single-unit branches are
1611  * also supported. Single-input Selector Units are ignored.
1612  */
1613 static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1614 	struct uvc_entity *entity)
1615 {
1616 	switch (UVC_ENTITY_TYPE(entity)) {
1617 	case UVC_VC_EXTENSION_UNIT:
1618 		uvc_dbg_cont(PROBE, " <- XU %d", entity->id);
1619 
1620 		if (entity->bNrInPins != 1) {
1621 			uvc_dbg(chain->dev, DESCR,
1622 				"Extension unit %d has more than 1 input pin\n",
1623 				entity->id);
1624 			return -1;
1625 		}
1626 
1627 		break;
1628 
1629 	case UVC_VC_PROCESSING_UNIT:
1630 		uvc_dbg_cont(PROBE, " <- PU %d", entity->id);
1631 
1632 		if (chain->processing != NULL) {
1633 			uvc_dbg(chain->dev, DESCR,
1634 				"Found multiple Processing Units in chain\n");
1635 			return -1;
1636 		}
1637 
1638 		chain->processing = entity;
1639 		break;
1640 
1641 	case UVC_VC_SELECTOR_UNIT:
1642 		uvc_dbg_cont(PROBE, " <- SU %d", entity->id);
1643 
1644 		/* Single-input selector units are ignored. */
1645 		if (entity->bNrInPins == 1)
1646 			break;
1647 
1648 		if (chain->selector != NULL) {
1649 			uvc_dbg(chain->dev, DESCR,
1650 				"Found multiple Selector Units in chain\n");
1651 			return -1;
1652 		}
1653 
1654 		chain->selector = entity;
1655 		break;
1656 
1657 	case UVC_ITT_VENDOR_SPECIFIC:
1658 	case UVC_ITT_CAMERA:
1659 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1660 		uvc_dbg_cont(PROBE, " <- IT %d\n", entity->id);
1661 
1662 		break;
1663 
1664 	case UVC_OTT_VENDOR_SPECIFIC:
1665 	case UVC_OTT_DISPLAY:
1666 	case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1667 		uvc_dbg_cont(PROBE, " OT %d", entity->id);
1668 
1669 		break;
1670 
1671 	case UVC_TT_STREAMING:
1672 		if (UVC_ENTITY_IS_ITERM(entity))
1673 			uvc_dbg_cont(PROBE, " <- IT %d\n", entity->id);
1674 		else
1675 			uvc_dbg_cont(PROBE, " OT %d", entity->id);
1676 
1677 		break;
1678 
1679 	default:
1680 		uvc_dbg(chain->dev, DESCR,
1681 			"Unsupported entity type 0x%04x found in chain\n",
1682 			UVC_ENTITY_TYPE(entity));
1683 		return -1;
1684 	}
1685 
1686 	list_add_tail(&entity->chain, &chain->entities);
1687 	return 0;
1688 }
1689 
1690 static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1691 	struct uvc_entity *entity, struct uvc_entity *prev)
1692 {
1693 	struct uvc_entity *forward;
1694 	int found;
1695 
1696 	/* Forward scan */
1697 	forward = NULL;
1698 	found = 0;
1699 
1700 	while (1) {
1701 		forward = uvc_entity_by_reference(chain->dev, entity->id,
1702 			forward);
1703 		if (forward == NULL)
1704 			break;
1705 		if (forward == prev)
1706 			continue;
1707 		if (forward->chain.next || forward->chain.prev) {
1708 			uvc_dbg(chain->dev, DESCR,
1709 				"Found reference to entity %d already in chain\n",
1710 				forward->id);
1711 			return -EINVAL;
1712 		}
1713 
1714 		switch (UVC_ENTITY_TYPE(forward)) {
1715 		case UVC_VC_EXTENSION_UNIT:
1716 			if (forward->bNrInPins != 1) {
1717 				uvc_dbg(chain->dev, DESCR,
1718 					"Extension unit %d has more than 1 input pin\n",
1719 					forward->id);
1720 				return -EINVAL;
1721 			}
1722 
1723 			/*
1724 			 * Some devices reference an output terminal as the
1725 			 * source of extension units. This is incorrect, as
1726 			 * output terminals only have an input pin, and thus
1727 			 * can't be connected to any entity in the forward
1728 			 * direction. The resulting topology would cause issues
1729 			 * when registering the media controller graph. To
1730 			 * avoid this problem, connect the extension unit to
1731 			 * the source of the output terminal instead.
1732 			 */
1733 			if (UVC_ENTITY_IS_OTERM(entity)) {
1734 				struct uvc_entity *source;
1735 
1736 				source = uvc_entity_by_id(chain->dev,
1737 							  entity->baSourceID[0]);
1738 				if (!source) {
1739 					uvc_dbg(chain->dev, DESCR,
1740 						"Can't connect extension unit %u in chain\n",
1741 						forward->id);
1742 					break;
1743 				}
1744 
1745 				forward->baSourceID[0] = source->id;
1746 			}
1747 
1748 			list_add_tail(&forward->chain, &chain->entities);
1749 			if (!found)
1750 				uvc_dbg_cont(PROBE, " (->");
1751 
1752 			uvc_dbg_cont(PROBE, " XU %d", forward->id);
1753 			found = 1;
1754 			break;
1755 
1756 		case UVC_OTT_VENDOR_SPECIFIC:
1757 		case UVC_OTT_DISPLAY:
1758 		case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1759 		case UVC_TT_STREAMING:
1760 			if (UVC_ENTITY_IS_ITERM(forward)) {
1761 				uvc_dbg(chain->dev, DESCR,
1762 					"Unsupported input terminal %u\n",
1763 					forward->id);
1764 				return -EINVAL;
1765 			}
1766 
1767 			if (UVC_ENTITY_IS_OTERM(entity)) {
1768 				uvc_dbg(chain->dev, DESCR,
1769 					"Unsupported connection between output terminals %u and %u\n",
1770 					entity->id, forward->id);
1771 				break;
1772 			}
1773 
1774 			list_add_tail(&forward->chain, &chain->entities);
1775 			if (!found)
1776 				uvc_dbg_cont(PROBE, " (->");
1777 
1778 			uvc_dbg_cont(PROBE, " OT %d", forward->id);
1779 			found = 1;
1780 			break;
1781 		}
1782 	}
1783 	if (found)
1784 		uvc_dbg_cont(PROBE, ")");
1785 
1786 	return 0;
1787 }
1788 
1789 static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1790 	struct uvc_entity **_entity)
1791 {
1792 	struct uvc_entity *entity = *_entity;
1793 	struct uvc_entity *term;
1794 	int id = -EINVAL, i;
1795 
1796 	switch (UVC_ENTITY_TYPE(entity)) {
1797 	case UVC_VC_EXTENSION_UNIT:
1798 	case UVC_VC_PROCESSING_UNIT:
1799 		id = entity->baSourceID[0];
1800 		break;
1801 
1802 	case UVC_VC_SELECTOR_UNIT:
1803 		/* Single-input selector units are ignored. */
1804 		if (entity->bNrInPins == 1) {
1805 			id = entity->baSourceID[0];
1806 			break;
1807 		}
1808 
1809 		uvc_dbg_cont(PROBE, " <- IT");
1810 
1811 		chain->selector = entity;
1812 		for (i = 0; i < entity->bNrInPins; ++i) {
1813 			id = entity->baSourceID[i];
1814 			term = uvc_entity_by_id(chain->dev, id);
1815 			if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1816 				uvc_dbg(chain->dev, DESCR,
1817 					"Selector unit %d input %d isn't connected to an input terminal\n",
1818 					entity->id, i);
1819 				return -1;
1820 			}
1821 
1822 			if (term->chain.next || term->chain.prev) {
1823 				uvc_dbg(chain->dev, DESCR,
1824 					"Found reference to entity %d already in chain\n",
1825 					term->id);
1826 				return -EINVAL;
1827 			}
1828 
1829 			uvc_dbg_cont(PROBE, " %d", term->id);
1830 
1831 			list_add_tail(&term->chain, &chain->entities);
1832 			uvc_scan_chain_forward(chain, term, entity);
1833 		}
1834 
1835 		uvc_dbg_cont(PROBE, "\n");
1836 
1837 		id = 0;
1838 		break;
1839 
1840 	case UVC_ITT_VENDOR_SPECIFIC:
1841 	case UVC_ITT_CAMERA:
1842 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1843 	case UVC_OTT_VENDOR_SPECIFIC:
1844 	case UVC_OTT_DISPLAY:
1845 	case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1846 	case UVC_TT_STREAMING:
1847 		id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
1848 		break;
1849 	}
1850 
1851 	if (id <= 0) {
1852 		*_entity = NULL;
1853 		return id;
1854 	}
1855 
1856 	entity = uvc_entity_by_id(chain->dev, id);
1857 	if (entity == NULL) {
1858 		uvc_dbg(chain->dev, DESCR,
1859 			"Found reference to unknown entity %d\n", id);
1860 		return -EINVAL;
1861 	}
1862 
1863 	*_entity = entity;
1864 	return 0;
1865 }
1866 
1867 static int uvc_scan_chain(struct uvc_video_chain *chain,
1868 			  struct uvc_entity *term)
1869 {
1870 	struct uvc_entity *entity, *prev;
1871 
1872 	uvc_dbg(chain->dev, PROBE, "Scanning UVC chain:");
1873 
1874 	entity = term;
1875 	prev = NULL;
1876 
1877 	while (entity != NULL) {
1878 		/* Entity must not be part of an existing chain */
1879 		if (entity->chain.next || entity->chain.prev) {
1880 			uvc_dbg(chain->dev, DESCR,
1881 				"Found reference to entity %d already in chain\n",
1882 				entity->id);
1883 			return -EINVAL;
1884 		}
1885 
1886 		/* Process entity */
1887 		if (uvc_scan_chain_entity(chain, entity) < 0)
1888 			return -EINVAL;
1889 
1890 		/* Forward scan */
1891 		if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1892 			return -EINVAL;
1893 
1894 		/* Backward scan */
1895 		prev = entity;
1896 		if (uvc_scan_chain_backward(chain, &entity) < 0)
1897 			return -EINVAL;
1898 	}
1899 
1900 	return 0;
1901 }
1902 
1903 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
1904 		char *buffer)
1905 {
1906 	struct uvc_entity *term;
1907 	unsigned int nterms = 0;
1908 	char *p = buffer;
1909 
1910 	list_for_each_entry(term, terms, chain) {
1911 		if (!UVC_ENTITY_IS_TERM(term) ||
1912 		    UVC_TERM_DIRECTION(term) != dir)
1913 			continue;
1914 
1915 		if (nterms)
1916 			p += sprintf(p, ",");
1917 		if (++nterms >= 4) {
1918 			p += sprintf(p, "...");
1919 			break;
1920 		}
1921 		p += sprintf(p, "%u", term->id);
1922 	}
1923 
1924 	return p - buffer;
1925 }
1926 
1927 static const char *uvc_print_chain(struct uvc_video_chain *chain)
1928 {
1929 	static char buffer[43];
1930 	char *p = buffer;
1931 
1932 	p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
1933 	p += sprintf(p, " -> ");
1934 	uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
1935 
1936 	return buffer;
1937 }
1938 
1939 static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
1940 {
1941 	struct uvc_video_chain *chain;
1942 
1943 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1944 	if (chain == NULL)
1945 		return NULL;
1946 
1947 	INIT_LIST_HEAD(&chain->entities);
1948 	mutex_init(&chain->ctrl_mutex);
1949 	chain->dev = dev;
1950 	v4l2_prio_init(&chain->prio);
1951 
1952 	return chain;
1953 }
1954 
1955 /*
1956  * Fallback heuristic for devices that don't connect units and terminals in a
1957  * valid chain.
1958  *
1959  * Some devices have invalid baSourceID references, causing uvc_scan_chain()
1960  * to fail, but if we just take the entities we can find and put them together
1961  * in the most sensible chain we can think of, turns out they do work anyway.
1962  * Note: This heuristic assumes there is a single chain.
1963  *
1964  * At the time of writing, devices known to have such a broken chain are
1965  *  - Acer Integrated Camera (5986:055a)
1966  *  - Realtek rtl157a7 (0bda:57a7)
1967  */
1968 static int uvc_scan_fallback(struct uvc_device *dev)
1969 {
1970 	struct uvc_video_chain *chain;
1971 	struct uvc_entity *iterm = NULL;
1972 	struct uvc_entity *oterm = NULL;
1973 	struct uvc_entity *entity;
1974 	struct uvc_entity *prev;
1975 
1976 	/*
1977 	 * Start by locating the input and output terminals. We only support
1978 	 * devices with exactly one of each for now.
1979 	 */
1980 	list_for_each_entry(entity, &dev->entities, list) {
1981 		if (UVC_ENTITY_IS_ITERM(entity)) {
1982 			if (iterm)
1983 				return -EINVAL;
1984 			iterm = entity;
1985 		}
1986 
1987 		if (UVC_ENTITY_IS_OTERM(entity)) {
1988 			if (oterm)
1989 				return -EINVAL;
1990 			oterm = entity;
1991 		}
1992 	}
1993 
1994 	if (iterm == NULL || oterm == NULL)
1995 		return -EINVAL;
1996 
1997 	/* Allocate the chain and fill it. */
1998 	chain = uvc_alloc_chain(dev);
1999 	if (chain == NULL)
2000 		return -ENOMEM;
2001 
2002 	if (uvc_scan_chain_entity(chain, oterm) < 0)
2003 		goto error;
2004 
2005 	prev = oterm;
2006 
2007 	/*
2008 	 * Add all Processing and Extension Units with two pads. The order
2009 	 * doesn't matter much, use reverse list traversal to connect units in
2010 	 * UVC descriptor order as we build the chain from output to input. This
2011 	 * leads to units appearing in the order meant by the manufacturer for
2012 	 * the cameras known to require this heuristic.
2013 	 */
2014 	list_for_each_entry_reverse(entity, &dev->entities, list) {
2015 		if (entity->type != UVC_VC_PROCESSING_UNIT &&
2016 		    entity->type != UVC_VC_EXTENSION_UNIT)
2017 			continue;
2018 
2019 		if (entity->num_pads != 2)
2020 			continue;
2021 
2022 		if (uvc_scan_chain_entity(chain, entity) < 0)
2023 			goto error;
2024 
2025 		prev->baSourceID[0] = entity->id;
2026 		prev = entity;
2027 	}
2028 
2029 	if (uvc_scan_chain_entity(chain, iterm) < 0)
2030 		goto error;
2031 
2032 	prev->baSourceID[0] = iterm->id;
2033 
2034 	list_add_tail(&chain->list, &dev->chains);
2035 
2036 	uvc_dbg(dev, PROBE, "Found a video chain by fallback heuristic (%s)\n",
2037 		uvc_print_chain(chain));
2038 
2039 	return 0;
2040 
2041 error:
2042 	kfree(chain);
2043 	return -EINVAL;
2044 }
2045 
2046 /*
2047  * Scan the device for video chains and register video devices.
2048  *
2049  * Chains are scanned starting at their output terminals and walked backwards.
2050  */
2051 static int uvc_scan_device(struct uvc_device *dev)
2052 {
2053 	struct uvc_video_chain *chain;
2054 	struct uvc_entity *term;
2055 
2056 	list_for_each_entry(term, &dev->entities, list) {
2057 		if (!UVC_ENTITY_IS_OTERM(term))
2058 			continue;
2059 
2060 		/* If the terminal is already included in a chain, skip it.
2061 		 * This can happen for chains that have multiple output
2062 		 * terminals, where all output terminals beside the first one
2063 		 * will be inserted in the chain in forward scans.
2064 		 */
2065 		if (term->chain.next || term->chain.prev)
2066 			continue;
2067 
2068 		chain = uvc_alloc_chain(dev);
2069 		if (chain == NULL)
2070 			return -ENOMEM;
2071 
2072 		term->flags |= UVC_ENTITY_FLAG_DEFAULT;
2073 
2074 		if (uvc_scan_chain(chain, term) < 0) {
2075 			kfree(chain);
2076 			continue;
2077 		}
2078 
2079 		uvc_dbg(dev, PROBE, "Found a valid video chain (%s)\n",
2080 			uvc_print_chain(chain));
2081 
2082 		list_add_tail(&chain->list, &dev->chains);
2083 	}
2084 
2085 	if (list_empty(&dev->chains))
2086 		uvc_scan_fallback(dev);
2087 
2088 	if (list_empty(&dev->chains)) {
2089 		dev_info(&dev->udev->dev, "No valid video chain found.\n");
2090 		return -1;
2091 	}
2092 
2093 	/* Add GPIO entity to the first chain. */
2094 	if (dev->gpio_unit) {
2095 		chain = list_first_entry(&dev->chains,
2096 					 struct uvc_video_chain, list);
2097 		list_add_tail(&dev->gpio_unit->chain, &chain->entities);
2098 	}
2099 
2100 	return 0;
2101 }
2102 
2103 /* ------------------------------------------------------------------------
2104  * Video device registration and unregistration
2105  */
2106 
2107 /*
2108  * Delete the UVC device.
2109  *
2110  * Called by the kernel when the last reference to the uvc_device structure
2111  * is released.
2112  *
2113  * As this function is called after or during disconnect(), all URBs have
2114  * already been cancelled by the USB core. There is no need to kill the
2115  * interrupt URB manually.
2116  */
2117 static void uvc_delete(struct kref *kref)
2118 {
2119 	struct uvc_device *dev = container_of(kref, struct uvc_device, ref);
2120 	struct list_head *p, *n;
2121 
2122 	uvc_status_cleanup(dev);
2123 	uvc_ctrl_cleanup_device(dev);
2124 
2125 	usb_put_intf(dev->intf);
2126 	usb_put_dev(dev->udev);
2127 
2128 #ifdef CONFIG_MEDIA_CONTROLLER
2129 	media_device_cleanup(&dev->mdev);
2130 #endif
2131 
2132 	list_for_each_safe(p, n, &dev->chains) {
2133 		struct uvc_video_chain *chain;
2134 		chain = list_entry(p, struct uvc_video_chain, list);
2135 		kfree(chain);
2136 	}
2137 
2138 	list_for_each_safe(p, n, &dev->entities) {
2139 		struct uvc_entity *entity;
2140 		entity = list_entry(p, struct uvc_entity, list);
2141 #ifdef CONFIG_MEDIA_CONTROLLER
2142 		uvc_mc_cleanup_entity(entity);
2143 #endif
2144 		kfree(entity);
2145 	}
2146 
2147 	list_for_each_safe(p, n, &dev->streams) {
2148 		struct uvc_streaming *streaming;
2149 		streaming = list_entry(p, struct uvc_streaming, list);
2150 		usb_driver_release_interface(&uvc_driver.driver,
2151 			streaming->intf);
2152 		uvc_stream_delete(streaming);
2153 	}
2154 
2155 	kfree(dev);
2156 }
2157 
2158 static void uvc_release(struct video_device *vdev)
2159 {
2160 	struct uvc_streaming *stream = video_get_drvdata(vdev);
2161 	struct uvc_device *dev = stream->dev;
2162 
2163 	kref_put(&dev->ref, uvc_delete);
2164 }
2165 
2166 /*
2167  * Unregister the video devices.
2168  */
2169 static void uvc_unregister_video(struct uvc_device *dev)
2170 {
2171 	struct uvc_streaming *stream;
2172 
2173 	list_for_each_entry(stream, &dev->streams, list) {
2174 		if (!video_is_registered(&stream->vdev))
2175 			continue;
2176 
2177 		video_unregister_device(&stream->vdev);
2178 		video_unregister_device(&stream->meta.vdev);
2179 
2180 		uvc_debugfs_cleanup_stream(stream);
2181 	}
2182 
2183 	uvc_status_unregister(dev);
2184 
2185 	if (dev->vdev.dev)
2186 		v4l2_device_unregister(&dev->vdev);
2187 #ifdef CONFIG_MEDIA_CONTROLLER
2188 	if (media_devnode_is_registered(dev->mdev.devnode))
2189 		media_device_unregister(&dev->mdev);
2190 #endif
2191 }
2192 
2193 int uvc_register_video_device(struct uvc_device *dev,
2194 			      struct uvc_streaming *stream,
2195 			      struct video_device *vdev,
2196 			      struct uvc_video_queue *queue,
2197 			      enum v4l2_buf_type type,
2198 			      const struct v4l2_file_operations *fops,
2199 			      const struct v4l2_ioctl_ops *ioctl_ops)
2200 {
2201 	int ret;
2202 
2203 	/* Initialize the video buffers queue. */
2204 	ret = uvc_queue_init(queue, type, !uvc_no_drop_param);
2205 	if (ret)
2206 		return ret;
2207 
2208 	/* Register the device with V4L. */
2209 
2210 	/*
2211 	 * We already hold a reference to dev->udev. The video device will be
2212 	 * unregistered before the reference is released, so we don't need to
2213 	 * get another one.
2214 	 */
2215 	vdev->v4l2_dev = &dev->vdev;
2216 	vdev->fops = fops;
2217 	vdev->ioctl_ops = ioctl_ops;
2218 	vdev->release = uvc_release;
2219 	vdev->prio = &stream->chain->prio;
2220 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2221 		vdev->vfl_dir = VFL_DIR_TX;
2222 	else
2223 		vdev->vfl_dir = VFL_DIR_RX;
2224 
2225 	switch (type) {
2226 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2227 	default:
2228 		vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2229 		break;
2230 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2231 		vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
2232 		break;
2233 	case V4L2_BUF_TYPE_META_CAPTURE:
2234 		vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
2235 		break;
2236 	}
2237 
2238 	strscpy(vdev->name, dev->name, sizeof(vdev->name));
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-media 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 FaceTime HD Camera (Built-In) */
2852 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2853 				| USB_DEVICE_ID_MATCH_INT_INFO,
2854 	  .idVendor		= 0x05ac,
2855 	  .idProduct		= 0x8514,
2856 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2857 	  .bInterfaceSubClass	= 1,
2858 	  .bInterfaceProtocol	= 0,
2859 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2860 	/* Apple Built-In iSight via iBridge */
2861 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2862 				| USB_DEVICE_ID_MATCH_INT_INFO,
2863 	  .idVendor		= 0x05ac,
2864 	  .idProduct		= 0x8600,
2865 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2866 	  .bInterfaceSubClass	= 1,
2867 	  .bInterfaceProtocol	= 0,
2868 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2869 	/* Foxlink ("HP Webcam" on HP Mini 5103) */
2870 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2871 				| USB_DEVICE_ID_MATCH_INT_INFO,
2872 	  .idVendor		= 0x05c8,
2873 	  .idProduct		= 0x0403,
2874 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2875 	  .bInterfaceSubClass	= 1,
2876 	  .bInterfaceProtocol	= 0,
2877 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2878 	/* Genesys Logic USB 2.0 PC Camera */
2879 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2880 				| USB_DEVICE_ID_MATCH_INT_INFO,
2881 	  .idVendor		= 0x05e3,
2882 	  .idProduct		= 0x0505,
2883 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2884 	  .bInterfaceSubClass	= 1,
2885 	  .bInterfaceProtocol	= 0,
2886 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2887 	/* Hercules Classic Silver */
2888 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2889 				| USB_DEVICE_ID_MATCH_INT_INFO,
2890 	  .idVendor		= 0x06f8,
2891 	  .idProduct		= 0x300c,
2892 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2893 	  .bInterfaceSubClass	= 1,
2894 	  .bInterfaceProtocol	= 0,
2895 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2896 	/* ViMicro Vega */
2897 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2898 				| USB_DEVICE_ID_MATCH_INT_INFO,
2899 	  .idVendor		= 0x0ac8,
2900 	  .idProduct		= 0x332d,
2901 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2902 	  .bInterfaceSubClass	= 1,
2903 	  .bInterfaceProtocol	= 0,
2904 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2905 	/* ViMicro - Minoru3D */
2906 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2907 				| USB_DEVICE_ID_MATCH_INT_INFO,
2908 	  .idVendor		= 0x0ac8,
2909 	  .idProduct		= 0x3410,
2910 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2911 	  .bInterfaceSubClass	= 1,
2912 	  .bInterfaceProtocol	= 0,
2913 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2914 	/* ViMicro Venus - Minoru3D */
2915 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2916 				| USB_DEVICE_ID_MATCH_INT_INFO,
2917 	  .idVendor		= 0x0ac8,
2918 	  .idProduct		= 0x3420,
2919 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2920 	  .bInterfaceSubClass	= 1,
2921 	  .bInterfaceProtocol	= 0,
2922 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
2923 	/* Ophir Optronics - SPCAM 620U */
2924 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2925 				| USB_DEVICE_ID_MATCH_INT_INFO,
2926 	  .idVendor		= 0x0bd3,
2927 	  .idProduct		= 0x0555,
2928 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2929 	  .bInterfaceSubClass	= 1,
2930 	  .bInterfaceProtocol	= 0,
2931 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2932 	/* MT6227 */
2933 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2934 				| USB_DEVICE_ID_MATCH_INT_INFO,
2935 	  .idVendor		= 0x0e8d,
2936 	  .idProduct		= 0x0004,
2937 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2938 	  .bInterfaceSubClass	= 1,
2939 	  .bInterfaceProtocol	= 0,
2940 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
2941 					| UVC_QUIRK_PROBE_DEF) },
2942 	/* IMC Networks (Medion Akoya) */
2943 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2944 				| USB_DEVICE_ID_MATCH_INT_INFO,
2945 	  .idVendor		= 0x13d3,
2946 	  .idProduct		= 0x5103,
2947 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2948 	  .bInterfaceSubClass	= 1,
2949 	  .bInterfaceProtocol	= 0,
2950 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2951 	/* JMicron USB2.0 XGA WebCam */
2952 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2953 				| USB_DEVICE_ID_MATCH_INT_INFO,
2954 	  .idVendor		= 0x152d,
2955 	  .idProduct		= 0x0310,
2956 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2957 	  .bInterfaceSubClass	= 1,
2958 	  .bInterfaceProtocol	= 0,
2959 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
2960 	/* Syntek (HP Spartan) */
2961 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2962 				| USB_DEVICE_ID_MATCH_INT_INFO,
2963 	  .idVendor		= 0x174f,
2964 	  .idProduct		= 0x5212,
2965 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2966 	  .bInterfaceSubClass	= 1,
2967 	  .bInterfaceProtocol	= 0,
2968 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2969 	/* Syntek (Samsung Q310) */
2970 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2971 				| USB_DEVICE_ID_MATCH_INT_INFO,
2972 	  .idVendor		= 0x174f,
2973 	  .idProduct		= 0x5931,
2974 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2975 	  .bInterfaceSubClass	= 1,
2976 	  .bInterfaceProtocol	= 0,
2977 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2978 	/* Syntek (Packard Bell EasyNote MX52 */
2979 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2980 				| USB_DEVICE_ID_MATCH_INT_INFO,
2981 	  .idVendor		= 0x174f,
2982 	  .idProduct		= 0x8a12,
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 F9SG) */
2988 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2989 				| USB_DEVICE_ID_MATCH_INT_INFO,
2990 	  .idVendor		= 0x174f,
2991 	  .idProduct		= 0x8a31,
2992 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2993 	  .bInterfaceSubClass	= 1,
2994 	  .bInterfaceProtocol	= 0,
2995 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
2996 	/* Syntek (Asus U3S) */
2997 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2998 				| USB_DEVICE_ID_MATCH_INT_INFO,
2999 	  .idVendor		= 0x174f,
3000 	  .idProduct		= 0x8a33,
3001 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3002 	  .bInterfaceSubClass	= 1,
3003 	  .bInterfaceProtocol	= 0,
3004 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
3005 	/* Syntek (JAOtech Smart Terminal) */
3006 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3007 				| USB_DEVICE_ID_MATCH_INT_INFO,
3008 	  .idVendor		= 0x174f,
3009 	  .idProduct		= 0x8a34,
3010 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3011 	  .bInterfaceSubClass	= 1,
3012 	  .bInterfaceProtocol	= 0,
3013 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
3014 	/* Miricle 307K */
3015 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3016 				| USB_DEVICE_ID_MATCH_INT_INFO,
3017 	  .idVendor		= 0x17dc,
3018 	  .idProduct		= 0x0202,
3019 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3020 	  .bInterfaceSubClass	= 1,
3021 	  .bInterfaceProtocol	= 0,
3022 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
3023 	/* Lenovo Thinkpad SL400/SL500 */
3024 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3025 				| USB_DEVICE_ID_MATCH_INT_INFO,
3026 	  .idVendor		= 0x17ef,
3027 	  .idProduct		= 0x480b,
3028 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3029 	  .bInterfaceSubClass	= 1,
3030 	  .bInterfaceProtocol	= 0,
3031 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
3032 	/* Aveo Technology USB 2.0 Camera */
3033 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3034 				| USB_DEVICE_ID_MATCH_INT_INFO,
3035 	  .idVendor		= 0x1871,
3036 	  .idProduct		= 0x0306,
3037 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3038 	  .bInterfaceSubClass	= 1,
3039 	  .bInterfaceProtocol	= 0,
3040 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
3041 					| UVC_QUIRK_PROBE_EXTRAFIELDS) },
3042 	/* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
3043 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3044 				| USB_DEVICE_ID_MATCH_INT_INFO,
3045 	  .idVendor		= 0x1871,
3046 	  .idProduct		= 0x0516,
3047 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3048 	  .bInterfaceSubClass	= 1,
3049 	  .bInterfaceProtocol	= 0 },
3050 	/* Ecamm Pico iMage */
3051 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3052 				| USB_DEVICE_ID_MATCH_INT_INFO,
3053 	  .idVendor		= 0x18cd,
3054 	  .idProduct		= 0xcafe,
3055 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3056 	  .bInterfaceSubClass	= 1,
3057 	  .bInterfaceProtocol	= 0,
3058 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_EXTRAFIELDS) },
3059 	/* Manta MM-353 Plako */
3060 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3061 				| USB_DEVICE_ID_MATCH_INT_INFO,
3062 	  .idVendor		= 0x18ec,
3063 	  .idProduct		= 0x3188,
3064 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3065 	  .bInterfaceSubClass	= 1,
3066 	  .bInterfaceProtocol	= 0,
3067 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3068 	/* FSC WebCam V30S */
3069 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3070 				| USB_DEVICE_ID_MATCH_INT_INFO,
3071 	  .idVendor		= 0x18ec,
3072 	  .idProduct		= 0x3288,
3073 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3074 	  .bInterfaceSubClass	= 1,
3075 	  .bInterfaceProtocol	= 0,
3076 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3077 	/* Arkmicro unbranded */
3078 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3079 				| USB_DEVICE_ID_MATCH_INT_INFO,
3080 	  .idVendor		= 0x18ec,
3081 	  .idProduct		= 0x3290,
3082 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3083 	  .bInterfaceSubClass	= 1,
3084 	  .bInterfaceProtocol	= 0,
3085 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
3086 	/* The Imaging Source USB CCD cameras */
3087 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3088 				| USB_DEVICE_ID_MATCH_INT_INFO,
3089 	  .idVendor		= 0x199e,
3090 	  .idProduct		= 0x8102,
3091 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3092 	  .bInterfaceSubClass	= 1,
3093 	  .bInterfaceProtocol	= 0 },
3094 	/* Bodelin ProScopeHR */
3095 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3096 				| USB_DEVICE_ID_MATCH_DEV_HI
3097 				| USB_DEVICE_ID_MATCH_INT_INFO,
3098 	  .idVendor		= 0x19ab,
3099 	  .idProduct		= 0x1000,
3100 	  .bcdDevice_hi		= 0x0126,
3101 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3102 	  .bInterfaceSubClass	= 1,
3103 	  .bInterfaceProtocol	= 0,
3104 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_STATUS_INTERVAL) },
3105 	/* MSI StarCam 370i */
3106 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3107 				| USB_DEVICE_ID_MATCH_INT_INFO,
3108 	  .idVendor		= 0x1b3b,
3109 	  .idProduct		= 0x2951,
3110 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3111 	  .bInterfaceSubClass	= 1,
3112 	  .bInterfaceProtocol	= 0,
3113 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3114 	/* Generalplus Technology Inc. 808 Camera */
3115 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3116 				| USB_DEVICE_ID_MATCH_INT_INFO,
3117 	  .idVendor		= 0x1b3f,
3118 	  .idProduct		= 0x2002,
3119 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3120 	  .bInterfaceSubClass	= 1,
3121 	  .bInterfaceProtocol	= 0,
3122 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3123 	/* Shenzhen Aoni Electronic Co.,Ltd 2K FHD camera */
3124 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3125 				| USB_DEVICE_ID_MATCH_INT_INFO,
3126 	  .idVendor		= 0x1bcf,
3127 	  .idProduct		= 0x0b40,
3128 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3129 	  .bInterfaceSubClass	= 1,
3130 	  .bInterfaceProtocol	= 0,
3131 	  .driver_info		= (kernel_ulong_t)&(const struct uvc_device_info){
3132 		.uvc_version = 0x010a,
3133 	  } },
3134 	/* SiGma Micro USB Web Camera */
3135 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3136 				| USB_DEVICE_ID_MATCH_INT_INFO,
3137 	  .idVendor		= 0x1c4f,
3138 	  .idProduct		= 0x3000,
3139 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3140 	  .bInterfaceSubClass	= 1,
3141 	  .bInterfaceProtocol	= 0,
3142 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
3143 					| UVC_QUIRK_IGNORE_SELECTOR_UNIT) },
3144 	/* Oculus VR Positional Tracker DK2 */
3145 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3146 				| USB_DEVICE_ID_MATCH_INT_INFO,
3147 	  .idVendor		= 0x2833,
3148 	  .idProduct		= 0x0201,
3149 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3150 	  .bInterfaceSubClass	= 1,
3151 	  .bInterfaceProtocol	= 0,
3152 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
3153 	/* Oculus VR Rift Sensor */
3154 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3155 				| USB_DEVICE_ID_MATCH_INT_INFO,
3156 	  .idVendor		= 0x2833,
3157 	  .idProduct		= 0x0211,
3158 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3159 	  .bInterfaceSubClass	= 1,
3160 	  .bInterfaceProtocol	= 0,
3161 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
3162 	/* GEO Semiconductor GC6500 */
3163 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3164 				| USB_DEVICE_ID_MATCH_INT_INFO,
3165 	  .idVendor		= 0x29fe,
3166 	  .idProduct		= 0x4d53,
3167 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3168 	  .bInterfaceSubClass	= 1,
3169 	  .bInterfaceProtocol	= 0,
3170 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_FORCE_BPP) },
3171 	/* Intel RealSense D4M */
3172 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3173 				| USB_DEVICE_ID_MATCH_INT_INFO,
3174 	  .idVendor		= 0x8086,
3175 	  .idProduct		= 0x0b03,
3176 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3177 	  .bInterfaceSubClass	= 1,
3178 	  .bInterfaceProtocol	= 0,
3179 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
3180 	/* Generic USB Video Class */
3181 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
3182 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
3183 	{}
3184 };
3185 
3186 MODULE_DEVICE_TABLE(usb, uvc_ids);
3187 
3188 struct uvc_driver uvc_driver = {
3189 	.driver = {
3190 		.name		= "uvcvideo",
3191 		.probe		= uvc_probe,
3192 		.disconnect	= uvc_disconnect,
3193 		.suspend	= uvc_suspend,
3194 		.resume		= uvc_resume,
3195 		.reset_resume	= uvc_reset_resume,
3196 		.id_table	= uvc_ids,
3197 		.supports_autosuspend = 1,
3198 	},
3199 };
3200 
3201 static int __init uvc_init(void)
3202 {
3203 	int ret;
3204 
3205 	uvc_debugfs_init();
3206 
3207 	ret = usb_register(&uvc_driver.driver);
3208 	if (ret < 0) {
3209 		uvc_debugfs_cleanup();
3210 		return ret;
3211 	}
3212 
3213 	return 0;
3214 }
3215 
3216 static void __exit uvc_cleanup(void)
3217 {
3218 	usb_deregister(&uvc_driver.driver);
3219 	uvc_debugfs_cleanup();
3220 }
3221 
3222 module_init(uvc_init);
3223 module_exit(uvc_cleanup);
3224 
3225 MODULE_AUTHOR(DRIVER_AUTHOR);
3226 MODULE_DESCRIPTION(DRIVER_DESC);
3227 MODULE_LICENSE("GPL");
3228 MODULE_VERSION(DRIVER_VERSION);
3229 
3230