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