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