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