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