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