xref: /openbmc/linux/drivers/media/usb/uvc/uvc_driver.c (revision 53c264544dfce30f7741a2b1e78f90d046e5d8ff)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab  *      uvc_driver.c  --  USB Video Class driver
40c0d06caSMauro Carvalho Chehab  *
50c0d06caSMauro Carvalho Chehab  *      Copyright (C) 2005-2010
60c0d06caSMauro Carvalho Chehab  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
70c0d06caSMauro Carvalho Chehab  */
80c0d06caSMauro Carvalho Chehab 
90c0d06caSMauro Carvalho Chehab #include <linux/atomic.h>
102886477fSRicardo Ribalda #include <linux/gpio/consumer.h>
110c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
120c0d06caSMauro Carvalho Chehab #include <linux/list.h>
130c0d06caSMauro Carvalho Chehab #include <linux/module.h>
140c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
150c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
160c0d06caSMauro Carvalho Chehab #include <linux/videodev2.h>
170c0d06caSMauro Carvalho Chehab #include <linux/vmalloc.h>
180c0d06caSMauro Carvalho Chehab #include <linux/wait.h>
190c0d06caSMauro Carvalho Chehab #include <asm/unaligned.h>
200c0d06caSMauro Carvalho Chehab 
210c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
2231a96f4cSLaurent Pinchart #include <media/v4l2-ioctl.h>
230c0d06caSMauro Carvalho Chehab 
240c0d06caSMauro Carvalho Chehab #include "uvcvideo.h"
250c0d06caSMauro Carvalho Chehab 
260c0d06caSMauro Carvalho Chehab #define DRIVER_AUTHOR		"Laurent Pinchart " \
270c0d06caSMauro Carvalho Chehab 				"<laurent.pinchart@ideasonboard.com>"
280c0d06caSMauro Carvalho Chehab #define DRIVER_DESC		"USB Video Class driver"
290c0d06caSMauro Carvalho Chehab 
300c0d06caSMauro Carvalho Chehab unsigned int uvc_clock_param = CLOCK_MONOTONIC;
315d0fd3c8SLaurent Pinchart unsigned int uvc_hw_timestamps_param;
320c0d06caSMauro Carvalho Chehab unsigned int uvc_no_drop_param;
330c0d06caSMauro Carvalho Chehab static unsigned int uvc_quirks_param = -1;
349e56380aSJoe Perches unsigned int uvc_dbg_param;
350c0d06caSMauro Carvalho Chehab unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT;
360c0d06caSMauro Carvalho Chehab 
370c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
380c0d06caSMauro Carvalho Chehab  * Video formats
390c0d06caSMauro Carvalho Chehab  */
400c0d06caSMauro Carvalho Chehab 
410c0d06caSMauro Carvalho Chehab static struct uvc_format_desc uvc_fmts[] = {
420c0d06caSMauro Carvalho Chehab 	{
430c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:2 (YUYV)",
440c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_YUY2,
450c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_YUYV,
460c0d06caSMauro Carvalho Chehab 	},
470c0d06caSMauro Carvalho Chehab 	{
480c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:2 (YUYV)",
490c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_YUY2_ISIGHT,
500c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_YUYV,
510c0d06caSMauro Carvalho Chehab 	},
520c0d06caSMauro Carvalho Chehab 	{
530c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:0 (NV12)",
540c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_NV12,
550c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_NV12,
560c0d06caSMauro Carvalho Chehab 	},
570c0d06caSMauro Carvalho Chehab 	{
580c0d06caSMauro Carvalho Chehab 		.name		= "MJPEG",
590c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_MJPEG,
600c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_MJPEG,
610c0d06caSMauro Carvalho Chehab 	},
620c0d06caSMauro Carvalho Chehab 	{
630c0d06caSMauro Carvalho Chehab 		.name		= "YVU 4:2:0 (YV12)",
640c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_YV12,
650c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_YVU420,
660c0d06caSMauro Carvalho Chehab 	},
670c0d06caSMauro Carvalho Chehab 	{
680c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:0 (I420)",
690c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_I420,
700c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_YUV420,
710c0d06caSMauro Carvalho Chehab 	},
720c0d06caSMauro Carvalho Chehab 	{
730c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:0 (M420)",
740c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_M420,
750c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_M420,
760c0d06caSMauro Carvalho Chehab 	},
770c0d06caSMauro Carvalho Chehab 	{
780c0d06caSMauro Carvalho Chehab 		.name		= "YUV 4:2:2 (UYVY)",
790c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_UYVY,
800c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_UYVY,
810c0d06caSMauro Carvalho Chehab 	},
820c0d06caSMauro Carvalho Chehab 	{
830c0d06caSMauro Carvalho Chehab 		.name		= "Greyscale 8-bit (Y800)",
840c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_Y800,
850c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_GREY,
860c0d06caSMauro Carvalho Chehab 	},
870c0d06caSMauro Carvalho Chehab 	{
880c0d06caSMauro Carvalho Chehab 		.name		= "Greyscale 8-bit (Y8  )",
890c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_Y8,
900c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_GREY,
910c0d06caSMauro Carvalho Chehab 	},
920c0d06caSMauro Carvalho Chehab 	{
93e96cdc9aSNicolas Dufresne 		.name		= "Greyscale 8-bit (D3DFMT_L8)",
94e96cdc9aSNicolas Dufresne 		.guid		= UVC_GUID_FORMAT_D3DFMT_L8,
95e96cdc9aSNicolas Dufresne 		.fcc		= V4L2_PIX_FMT_GREY,
96e96cdc9aSNicolas Dufresne 	},
97e96cdc9aSNicolas Dufresne 	{
98557a5c7fSLaurent Pinchart 		.name		= "IR 8-bit (L8_IR)",
99557a5c7fSLaurent Pinchart 		.guid		= UVC_GUID_FORMAT_KSMEDIA_L8_IR,
100557a5c7fSLaurent Pinchart 		.fcc		= V4L2_PIX_FMT_GREY,
101557a5c7fSLaurent Pinchart 	},
102557a5c7fSLaurent Pinchart 	{
1030c0d06caSMauro Carvalho Chehab 		.name		= "Greyscale 10-bit (Y10 )",
1040c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_Y10,
1050c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_Y10,
1060c0d06caSMauro Carvalho Chehab 	},
1070c0d06caSMauro Carvalho Chehab 	{
1080c0d06caSMauro Carvalho Chehab 		.name		= "Greyscale 12-bit (Y12 )",
1090c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_Y12,
1100c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_Y12,
1110c0d06caSMauro Carvalho Chehab 	},
1120c0d06caSMauro Carvalho Chehab 	{
1130c0d06caSMauro Carvalho Chehab 		.name		= "Greyscale 16-bit (Y16 )",
1140c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_Y16,
1150c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_Y16,
1160c0d06caSMauro Carvalho Chehab 	},
1170c0d06caSMauro Carvalho Chehab 	{
118e72ed08eSEdgar Thier 		.name		= "BGGR Bayer (BY8 )",
1190c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_BY8,
1200c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_SBGGR8,
1210c0d06caSMauro Carvalho Chehab 	},
1220c0d06caSMauro Carvalho Chehab 	{
123e72ed08eSEdgar Thier 		.name		= "BGGR Bayer (BA81)",
124e72ed08eSEdgar Thier 		.guid		= UVC_GUID_FORMAT_BA81,
125e72ed08eSEdgar Thier 		.fcc		= V4L2_PIX_FMT_SBGGR8,
126e72ed08eSEdgar Thier 	},
127e72ed08eSEdgar Thier 	{
128e72ed08eSEdgar Thier 		.name		= "GBRG Bayer (GBRG)",
129e72ed08eSEdgar Thier 		.guid		= UVC_GUID_FORMAT_GBRG,
130e72ed08eSEdgar Thier 		.fcc		= V4L2_PIX_FMT_SGBRG8,
131e72ed08eSEdgar Thier 	},
132e72ed08eSEdgar Thier 	{
133e72ed08eSEdgar Thier 		.name		= "GRBG Bayer (GRBG)",
134e72ed08eSEdgar Thier 		.guid		= UVC_GUID_FORMAT_GRBG,
135e72ed08eSEdgar Thier 		.fcc		= V4L2_PIX_FMT_SGRBG8,
136e72ed08eSEdgar Thier 	},
137e72ed08eSEdgar Thier 	{
138e72ed08eSEdgar Thier 		.name		= "RGGB Bayer (RGGB)",
139e72ed08eSEdgar Thier 		.guid		= UVC_GUID_FORMAT_RGGB,
140e72ed08eSEdgar Thier 		.fcc		= V4L2_PIX_FMT_SRGGB8,
141e72ed08eSEdgar Thier 	},
142e72ed08eSEdgar Thier 	{
1430c0d06caSMauro Carvalho Chehab 		.name		= "RGB565",
1440c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_RGBP,
1450c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_RGB565,
1460c0d06caSMauro Carvalho Chehab 	},
1470c0d06caSMauro Carvalho Chehab 	{
1481b8dc322SWilliam Manley 		.name		= "BGR 8:8:8 (BGR3)",
1491b8dc322SWilliam Manley 		.guid		= UVC_GUID_FORMAT_BGR3,
1501b8dc322SWilliam Manley 		.fcc		= V4L2_PIX_FMT_BGR24,
1511b8dc322SWilliam Manley 	},
1521b8dc322SWilliam Manley 	{
1530c0d06caSMauro Carvalho Chehab 		.name		= "H.264",
1540c0d06caSMauro Carvalho Chehab 		.guid		= UVC_GUID_FORMAT_H264,
1550c0d06caSMauro Carvalho Chehab 		.fcc		= V4L2_PIX_FMT_H264,
1560c0d06caSMauro Carvalho Chehab 	},
1575d8d8db8SAviv Greenberg 	{
1585d8d8db8SAviv Greenberg 		.name		= "Greyscale 8 L/R (Y8I)",
1595d8d8db8SAviv Greenberg 		.guid		= UVC_GUID_FORMAT_Y8I,
1605d8d8db8SAviv Greenberg 		.fcc		= V4L2_PIX_FMT_Y8I,
1615d8d8db8SAviv Greenberg 	},
1625d8d8db8SAviv Greenberg 	{
1635d8d8db8SAviv Greenberg 		.name		= "Greyscale 12 L/R (Y12I)",
1645d8d8db8SAviv Greenberg 		.guid		= UVC_GUID_FORMAT_Y12I,
1655d8d8db8SAviv Greenberg 		.fcc		= V4L2_PIX_FMT_Y12I,
1665d8d8db8SAviv Greenberg 	},
1675d8d8db8SAviv Greenberg 	{
1685d8d8db8SAviv Greenberg 		.name		= "Depth data 16-bit (Z16)",
1695d8d8db8SAviv Greenberg 		.guid		= UVC_GUID_FORMAT_Z16,
1705d8d8db8SAviv Greenberg 		.fcc		= V4L2_PIX_FMT_Z16,
1715d8d8db8SAviv Greenberg 	},
1725d8d8db8SAviv Greenberg 	{
1735d8d8db8SAviv Greenberg 		.name		= "Bayer 10-bit (SRGGB10P)",
1745d8d8db8SAviv Greenberg 		.guid		= UVC_GUID_FORMAT_RW10,
1755d8d8db8SAviv Greenberg 		.fcc		= V4L2_PIX_FMT_SRGGB10P,
1765d8d8db8SAviv Greenberg 	},
177eb165a20SEdgar Thier 	{
178eb165a20SEdgar Thier 		.name		= "Bayer 16-bit (SBGGR16)",
179eb165a20SEdgar Thier 		.guid		= UVC_GUID_FORMAT_BG16,
180eb165a20SEdgar Thier 		.fcc		= V4L2_PIX_FMT_SBGGR16,
181eb165a20SEdgar Thier 	},
182eb165a20SEdgar Thier 	{
183eb165a20SEdgar Thier 		.name		= "Bayer 16-bit (SGBRG16)",
184eb165a20SEdgar Thier 		.guid		= UVC_GUID_FORMAT_GB16,
185eb165a20SEdgar Thier 		.fcc		= V4L2_PIX_FMT_SGBRG16,
186eb165a20SEdgar Thier 	},
187eb165a20SEdgar Thier 	{
188eb165a20SEdgar Thier 		.name		= "Bayer 16-bit (SRGGB16)",
189eb165a20SEdgar Thier 		.guid		= UVC_GUID_FORMAT_RG16,
190eb165a20SEdgar Thier 		.fcc		= V4L2_PIX_FMT_SRGGB16,
191eb165a20SEdgar Thier 	},
192eb165a20SEdgar Thier 	{
193eb165a20SEdgar Thier 		.name		= "Bayer 16-bit (SGRBG16)",
194eb165a20SEdgar Thier 		.guid		= UVC_GUID_FORMAT_GR16,
195eb165a20SEdgar Thier 		.fcc		= V4L2_PIX_FMT_SGRBG16,
196eb165a20SEdgar Thier 	},
197c4a0968aSDaniel Patrick Johnson 	{
198c4a0968aSDaniel Patrick Johnson 		.name		= "Depth data 16-bit (Z16)",
199c4a0968aSDaniel Patrick Johnson 		.guid		= UVC_GUID_FORMAT_INVZ,
200c4a0968aSDaniel Patrick Johnson 		.fcc		= V4L2_PIX_FMT_Z16,
201c4a0968aSDaniel Patrick Johnson 	},
202c4a0968aSDaniel Patrick Johnson 	{
203c4a0968aSDaniel Patrick Johnson 		.name		= "Greyscale 10-bit (Y10 )",
204c4a0968aSDaniel Patrick Johnson 		.guid		= UVC_GUID_FORMAT_INVI,
205c4a0968aSDaniel Patrick Johnson 		.fcc		= V4L2_PIX_FMT_Y10,
206c4a0968aSDaniel Patrick Johnson 	},
207c4a0968aSDaniel Patrick Johnson 	{
208c4a0968aSDaniel Patrick Johnson 		.name		= "IR:Depth 26-bit (INZI)",
209c4a0968aSDaniel Patrick Johnson 		.guid		= UVC_GUID_FORMAT_INZI,
210c4a0968aSDaniel Patrick Johnson 		.fcc		= V4L2_PIX_FMT_INZI,
211c4a0968aSDaniel Patrick Johnson 	},
21238e9b928SSergey Dorodnicov 	{
21338e9b928SSergey Dorodnicov 		.name		= "4-bit Depth Confidence (Packed)",
21438e9b928SSergey Dorodnicov 		.guid		= UVC_GUID_FORMAT_CNF4,
21538e9b928SSergey Dorodnicov 		.fcc		= V4L2_PIX_FMT_CNF4,
21638e9b928SSergey Dorodnicov 	},
21740b222b5SDmitry Buzdyk 	{
21840b222b5SDmitry Buzdyk 		.name		= "HEVC",
21940b222b5SDmitry Buzdyk 		.guid		= UVC_GUID_FORMAT_HEVC,
22040b222b5SDmitry Buzdyk 		.fcc		= V4L2_PIX_FMT_HEVC,
22140b222b5SDmitry Buzdyk 	},
2220c0d06caSMauro Carvalho Chehab };
2230c0d06caSMauro Carvalho Chehab 
2240c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
2250c0d06caSMauro Carvalho Chehab  * Utility functions
2260c0d06caSMauro Carvalho Chehab  */
2270c0d06caSMauro Carvalho Chehab 
2280c0d06caSMauro Carvalho Chehab struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
2292c6b222cSLaurent Pinchart 		u8 epaddr)
2300c0d06caSMauro Carvalho Chehab {
2310c0d06caSMauro Carvalho Chehab 	struct usb_host_endpoint *ep;
2320c0d06caSMauro Carvalho Chehab 	unsigned int i;
2330c0d06caSMauro Carvalho Chehab 
2340c0d06caSMauro Carvalho Chehab 	for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
2350c0d06caSMauro Carvalho Chehab 		ep = &alts->endpoint[i];
2360c0d06caSMauro Carvalho Chehab 		if (ep->desc.bEndpointAddress == epaddr)
2370c0d06caSMauro Carvalho Chehab 			return ep;
2380c0d06caSMauro Carvalho Chehab 	}
2390c0d06caSMauro Carvalho Chehab 
2400c0d06caSMauro Carvalho Chehab 	return NULL;
2410c0d06caSMauro Carvalho Chehab }
2420c0d06caSMauro Carvalho Chehab 
2432c6b222cSLaurent Pinchart static struct uvc_format_desc *uvc_format_by_guid(const u8 guid[16])
2440c0d06caSMauro Carvalho Chehab {
2450c0d06caSMauro Carvalho Chehab 	unsigned int len = ARRAY_SIZE(uvc_fmts);
2460c0d06caSMauro Carvalho Chehab 	unsigned int i;
2470c0d06caSMauro Carvalho Chehab 
2480c0d06caSMauro Carvalho Chehab 	for (i = 0; i < len; ++i) {
2490c0d06caSMauro Carvalho Chehab 		if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
2500c0d06caSMauro Carvalho Chehab 			return &uvc_fmts[i];
2510c0d06caSMauro Carvalho Chehab 	}
2520c0d06caSMauro Carvalho Chehab 
2530c0d06caSMauro Carvalho Chehab 	return NULL;
2540c0d06caSMauro Carvalho Chehab }
2550c0d06caSMauro Carvalho Chehab 
256ec2c23f6SAdam Goode static enum v4l2_colorspace uvc_colorspace(const u8 primaries)
2570c0d06caSMauro Carvalho Chehab {
258ec2c23f6SAdam Goode 	static const enum v4l2_colorspace colorprimaries[] = {
259e82822faSRicardo Ribalda 		V4L2_COLORSPACE_SRGB,  /* Unspecified */
2600c0d06caSMauro Carvalho Chehab 		V4L2_COLORSPACE_SRGB,
2610c0d06caSMauro Carvalho Chehab 		V4L2_COLORSPACE_470_SYSTEM_M,
2620c0d06caSMauro Carvalho Chehab 		V4L2_COLORSPACE_470_SYSTEM_BG,
2630c0d06caSMauro Carvalho Chehab 		V4L2_COLORSPACE_SMPTE170M,
2640c0d06caSMauro Carvalho Chehab 		V4L2_COLORSPACE_SMPTE240M,
2650c0d06caSMauro Carvalho Chehab 	};
2660c0d06caSMauro Carvalho Chehab 
2670c0d06caSMauro Carvalho Chehab 	if (primaries < ARRAY_SIZE(colorprimaries))
2680c0d06caSMauro Carvalho Chehab 		return colorprimaries[primaries];
2690c0d06caSMauro Carvalho Chehab 
270e82822faSRicardo Ribalda 	return V4L2_COLORSPACE_SRGB;  /* Reserved */
271ec2c23f6SAdam Goode }
272ec2c23f6SAdam Goode 
273ec2c23f6SAdam Goode static enum v4l2_xfer_func uvc_xfer_func(const u8 transfer_characteristics)
274ec2c23f6SAdam Goode {
275ec2c23f6SAdam Goode 	/*
276ec2c23f6SAdam Goode 	 * V4L2 does not currently have definitions for all possible values of
277ec2c23f6SAdam Goode 	 * UVC transfer characteristics. If v4l2_xfer_func is extended with new
278ec2c23f6SAdam Goode 	 * values, the mapping below should be updated.
279ec2c23f6SAdam Goode 	 *
280ec2c23f6SAdam Goode 	 * Substitutions are taken from the mapping given for
281ec2c23f6SAdam Goode 	 * V4L2_XFER_FUNC_DEFAULT documented in videodev2.h.
282ec2c23f6SAdam Goode 	 */
283ec2c23f6SAdam Goode 	static const enum v4l2_xfer_func xfer_funcs[] = {
284ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_DEFAULT,    /* Unspecified */
285ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_709,
286ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_709,        /* Substitution for BT.470-2 M */
287ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_709,        /* Substitution for BT.470-2 B, G */
288ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_709,        /* Substitution for SMPTE 170M */
289ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_SMPTE240M,
290ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_NONE,
291ec2c23f6SAdam Goode 		V4L2_XFER_FUNC_SRGB,
292ec2c23f6SAdam Goode 	};
293ec2c23f6SAdam Goode 
294ec2c23f6SAdam Goode 	if (transfer_characteristics < ARRAY_SIZE(xfer_funcs))
295ec2c23f6SAdam Goode 		return xfer_funcs[transfer_characteristics];
296ec2c23f6SAdam Goode 
297ec2c23f6SAdam Goode 	return V4L2_XFER_FUNC_DEFAULT;  /* Reserved */
298ec2c23f6SAdam Goode }
299ec2c23f6SAdam Goode 
300ec2c23f6SAdam Goode static enum v4l2_ycbcr_encoding uvc_ycbcr_enc(const u8 matrix_coefficients)
301ec2c23f6SAdam Goode {
302ec2c23f6SAdam Goode 	/*
303ec2c23f6SAdam Goode 	 * V4L2 does not currently have definitions for all possible values of
304ec2c23f6SAdam Goode 	 * UVC matrix coefficients. If v4l2_ycbcr_encoding is extended with new
305ec2c23f6SAdam Goode 	 * values, the mapping below should be updated.
306ec2c23f6SAdam Goode 	 *
307ec2c23f6SAdam Goode 	 * Substitutions are taken from the mapping given for
308ec2c23f6SAdam Goode 	 * V4L2_YCBCR_ENC_DEFAULT documented in videodev2.h.
309ec2c23f6SAdam Goode 	 *
310ec2c23f6SAdam Goode 	 * FCC is assumed to be close enough to 601.
311ec2c23f6SAdam Goode 	 */
312ec2c23f6SAdam Goode 	static const enum v4l2_ycbcr_encoding ycbcr_encs[] = {
313ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_DEFAULT,  /* Unspecified */
314ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_709,
315ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_601,      /* Substitution for FCC */
316ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_601,      /* Substitution for BT.470-2 B, G */
317ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_601,
318ec2c23f6SAdam Goode 		V4L2_YCBCR_ENC_SMPTE240M,
319ec2c23f6SAdam Goode 	};
320ec2c23f6SAdam Goode 
321ec2c23f6SAdam Goode 	if (matrix_coefficients < ARRAY_SIZE(ycbcr_encs))
322ec2c23f6SAdam Goode 		return ycbcr_encs[matrix_coefficients];
323ec2c23f6SAdam Goode 
324ec2c23f6SAdam Goode 	return V4L2_YCBCR_ENC_DEFAULT;  /* Reserved */
3250c0d06caSMauro Carvalho Chehab }
3260c0d06caSMauro Carvalho Chehab 
3270c0d06caSMauro Carvalho Chehab /* Simplify a fraction using a simple continued fraction decomposition. The
3280c0d06caSMauro Carvalho Chehab  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
3290c0d06caSMauro Carvalho Chehab  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
3300c0d06caSMauro Carvalho Chehab  * arbitrary parameters to remove non-significative terms from the simple
3310c0d06caSMauro Carvalho Chehab  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
3320c0d06caSMauro Carvalho Chehab  * respectively seems to give nice results.
3330c0d06caSMauro Carvalho Chehab  */
3341e304c47SLaurent Pinchart void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
3350c0d06caSMauro Carvalho Chehab 		unsigned int n_terms, unsigned int threshold)
3360c0d06caSMauro Carvalho Chehab {
3371e304c47SLaurent Pinchart 	u32 *an;
3381e304c47SLaurent Pinchart 	u32 x, y, r;
3390c0d06caSMauro Carvalho Chehab 	unsigned int i, n;
3400c0d06caSMauro Carvalho Chehab 
341f14d4988SLaurent Pinchart 	an = kmalloc_array(n_terms, sizeof(*an), GFP_KERNEL);
3420c0d06caSMauro Carvalho Chehab 	if (an == NULL)
3430c0d06caSMauro Carvalho Chehab 		return;
3440c0d06caSMauro Carvalho Chehab 
3450c0d06caSMauro Carvalho Chehab 	/* Convert the fraction to a simple continued fraction. See
346f5a3048aSAlexander A. Klimov 	 * https://mathforum.org/dr.math/faq/faq.fractions.html
3470c0d06caSMauro Carvalho Chehab 	 * Stop if the current term is bigger than or equal to the given
3480c0d06caSMauro Carvalho Chehab 	 * threshold.
3490c0d06caSMauro Carvalho Chehab 	 */
3500c0d06caSMauro Carvalho Chehab 	x = *numerator;
3510c0d06caSMauro Carvalho Chehab 	y = *denominator;
3520c0d06caSMauro Carvalho Chehab 
3530c0d06caSMauro Carvalho Chehab 	for (n = 0; n < n_terms && y != 0; ++n) {
3540c0d06caSMauro Carvalho Chehab 		an[n] = x / y;
3550c0d06caSMauro Carvalho Chehab 		if (an[n] >= threshold) {
3560c0d06caSMauro Carvalho Chehab 			if (n < 2)
3570c0d06caSMauro Carvalho Chehab 				n++;
3580c0d06caSMauro Carvalho Chehab 			break;
3590c0d06caSMauro Carvalho Chehab 		}
3600c0d06caSMauro Carvalho Chehab 
3610c0d06caSMauro Carvalho Chehab 		r = x - an[n] * y;
3620c0d06caSMauro Carvalho Chehab 		x = y;
3630c0d06caSMauro Carvalho Chehab 		y = r;
3640c0d06caSMauro Carvalho Chehab 	}
3650c0d06caSMauro Carvalho Chehab 
3660c0d06caSMauro Carvalho Chehab 	/* Expand the simple continued fraction back to an integer fraction. */
3670c0d06caSMauro Carvalho Chehab 	x = 0;
3680c0d06caSMauro Carvalho Chehab 	y = 1;
3690c0d06caSMauro Carvalho Chehab 
3700c0d06caSMauro Carvalho Chehab 	for (i = n; i > 0; --i) {
3710c0d06caSMauro Carvalho Chehab 		r = y;
3720c0d06caSMauro Carvalho Chehab 		y = an[i-1] * y + x;
3730c0d06caSMauro Carvalho Chehab 		x = r;
3740c0d06caSMauro Carvalho Chehab 	}
3750c0d06caSMauro Carvalho Chehab 
3760c0d06caSMauro Carvalho Chehab 	*numerator = y;
3770c0d06caSMauro Carvalho Chehab 	*denominator = x;
3780c0d06caSMauro Carvalho Chehab 	kfree(an);
3790c0d06caSMauro Carvalho Chehab }
3800c0d06caSMauro Carvalho Chehab 
3810c0d06caSMauro Carvalho Chehab /* Convert a fraction to a frame interval in 100ns multiples. The idea here is
3820c0d06caSMauro Carvalho Chehab  * to compute numerator / denominator * 10000000 using 32 bit fixed point
3830c0d06caSMauro Carvalho Chehab  * arithmetic only.
3840c0d06caSMauro Carvalho Chehab  */
3851e304c47SLaurent Pinchart u32 uvc_fraction_to_interval(u32 numerator, u32 denominator)
3860c0d06caSMauro Carvalho Chehab {
3871e304c47SLaurent Pinchart 	u32 multiplier;
3880c0d06caSMauro Carvalho Chehab 
3890c0d06caSMauro Carvalho Chehab 	/* Saturate the result if the operation would overflow. */
3900c0d06caSMauro Carvalho Chehab 	if (denominator == 0 ||
3911e304c47SLaurent Pinchart 	    numerator/denominator >= ((u32)-1)/10000000)
3921e304c47SLaurent Pinchart 		return (u32)-1;
3930c0d06caSMauro Carvalho Chehab 
3940c0d06caSMauro Carvalho Chehab 	/* Divide both the denominator and the multiplier by two until
3950c0d06caSMauro Carvalho Chehab 	 * numerator * multiplier doesn't overflow. If anyone knows a better
3960c0d06caSMauro Carvalho Chehab 	 * algorithm please let me know.
3970c0d06caSMauro Carvalho Chehab 	 */
3980c0d06caSMauro Carvalho Chehab 	multiplier = 10000000;
3991e304c47SLaurent Pinchart 	while (numerator > ((u32)-1)/multiplier) {
4000c0d06caSMauro Carvalho Chehab 		multiplier /= 2;
4010c0d06caSMauro Carvalho Chehab 		denominator /= 2;
4020c0d06caSMauro Carvalho Chehab 	}
4030c0d06caSMauro Carvalho Chehab 
4040c0d06caSMauro Carvalho Chehab 	return denominator ? numerator * multiplier / denominator : 0;
4050c0d06caSMauro Carvalho Chehab }
4060c0d06caSMauro Carvalho Chehab 
4070c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
4080c0d06caSMauro Carvalho Chehab  * Terminal and unit management
4090c0d06caSMauro Carvalho Chehab  */
4100c0d06caSMauro Carvalho Chehab 
4110c0d06caSMauro Carvalho Chehab struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
4120c0d06caSMauro Carvalho Chehab {
4130c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity;
4140c0d06caSMauro Carvalho Chehab 
4150c0d06caSMauro Carvalho Chehab 	list_for_each_entry(entity, &dev->entities, list) {
4160c0d06caSMauro Carvalho Chehab 		if (entity->id == id)
4170c0d06caSMauro Carvalho Chehab 			return entity;
4180c0d06caSMauro Carvalho Chehab 	}
4190c0d06caSMauro Carvalho Chehab 
4200c0d06caSMauro Carvalho Chehab 	return NULL;
4210c0d06caSMauro Carvalho Chehab }
4220c0d06caSMauro Carvalho Chehab 
4230c0d06caSMauro Carvalho Chehab static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
4240c0d06caSMauro Carvalho Chehab 	int id, struct uvc_entity *entity)
4250c0d06caSMauro Carvalho Chehab {
4260c0d06caSMauro Carvalho Chehab 	unsigned int i;
4270c0d06caSMauro Carvalho Chehab 
4280c0d06caSMauro Carvalho Chehab 	if (entity == NULL)
4290c0d06caSMauro Carvalho Chehab 		entity = list_entry(&dev->entities, struct uvc_entity, list);
4300c0d06caSMauro Carvalho Chehab 
4310c0d06caSMauro Carvalho Chehab 	list_for_each_entry_continue(entity, &dev->entities, list) {
4320c0d06caSMauro Carvalho Chehab 		for (i = 0; i < entity->bNrInPins; ++i)
4330c0d06caSMauro Carvalho Chehab 			if (entity->baSourceID[i] == id)
4340c0d06caSMauro Carvalho Chehab 				return entity;
4350c0d06caSMauro Carvalho Chehab 	}
4360c0d06caSMauro Carvalho Chehab 
4370c0d06caSMauro Carvalho Chehab 	return NULL;
4380c0d06caSMauro Carvalho Chehab }
4390c0d06caSMauro Carvalho Chehab 
4400c0d06caSMauro Carvalho Chehab static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
4410c0d06caSMauro Carvalho Chehab {
4420c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream;
4430c0d06caSMauro Carvalho Chehab 
4440c0d06caSMauro Carvalho Chehab 	list_for_each_entry(stream, &dev->streams, list) {
4450c0d06caSMauro Carvalho Chehab 		if (stream->header.bTerminalLink == id)
4460c0d06caSMauro Carvalho Chehab 			return stream;
4470c0d06caSMauro Carvalho Chehab 	}
4480c0d06caSMauro Carvalho Chehab 
4490c0d06caSMauro Carvalho Chehab 	return NULL;
4500c0d06caSMauro Carvalho Chehab }
4510c0d06caSMauro Carvalho Chehab 
4520c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
453ece41454SKieran Bingham  * Streaming Object Management
454ece41454SKieran Bingham  */
455ece41454SKieran Bingham 
456ece41454SKieran Bingham static void uvc_stream_delete(struct uvc_streaming *stream)
457ece41454SKieran Bingham {
458b012186aSKieran Bingham 	if (stream->async_wq)
459b012186aSKieran Bingham 		destroy_workqueue(stream->async_wq);
460b012186aSKieran Bingham 
461ece41454SKieran Bingham 	mutex_destroy(&stream->mutex);
462ece41454SKieran Bingham 
463ece41454SKieran Bingham 	usb_put_intf(stream->intf);
464ece41454SKieran Bingham 
465ece41454SKieran Bingham 	kfree(stream->format);
466ece41454SKieran Bingham 	kfree(stream->header.bmaControls);
467ece41454SKieran Bingham 	kfree(stream);
468ece41454SKieran Bingham }
469ece41454SKieran Bingham 
470ece41454SKieran Bingham static struct uvc_streaming *uvc_stream_new(struct uvc_device *dev,
471ece41454SKieran Bingham 					    struct usb_interface *intf)
472ece41454SKieran Bingham {
473ece41454SKieran Bingham 	struct uvc_streaming *stream;
474ece41454SKieran Bingham 
475ece41454SKieran Bingham 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
476ece41454SKieran Bingham 	if (stream == NULL)
477ece41454SKieran Bingham 		return NULL;
478ece41454SKieran Bingham 
479ece41454SKieran Bingham 	mutex_init(&stream->mutex);
480ece41454SKieran Bingham 
481ece41454SKieran Bingham 	stream->dev = dev;
482ece41454SKieran Bingham 	stream->intf = usb_get_intf(intf);
483ece41454SKieran Bingham 	stream->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
484ece41454SKieran Bingham 
485b012186aSKieran Bingham 	/* Allocate a stream specific work queue for asynchronous tasks. */
486b012186aSKieran Bingham 	stream->async_wq = alloc_workqueue("uvcvideo", WQ_UNBOUND | WQ_HIGHPRI,
487b012186aSKieran Bingham 					   0);
488b012186aSKieran Bingham 	if (!stream->async_wq) {
489b012186aSKieran Bingham 		uvc_stream_delete(stream);
490b012186aSKieran Bingham 		return NULL;
491b012186aSKieran Bingham 	}
492b012186aSKieran Bingham 
493ece41454SKieran Bingham 	return stream;
494ece41454SKieran Bingham }
495ece41454SKieran Bingham 
496ece41454SKieran Bingham /* ------------------------------------------------------------------------
4970c0d06caSMauro Carvalho Chehab  * Descriptors parsing
4980c0d06caSMauro Carvalho Chehab  */
4990c0d06caSMauro Carvalho Chehab 
5000c0d06caSMauro Carvalho Chehab static int uvc_parse_format(struct uvc_device *dev,
5010c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *streaming, struct uvc_format *format,
5022c6b222cSLaurent Pinchart 	u32 **intervals, unsigned char *buffer, int buflen)
5030c0d06caSMauro Carvalho Chehab {
5040c0d06caSMauro Carvalho Chehab 	struct usb_interface *intf = streaming->intf;
5050c0d06caSMauro Carvalho Chehab 	struct usb_host_interface *alts = intf->cur_altsetting;
5060c0d06caSMauro Carvalho Chehab 	struct uvc_format_desc *fmtdesc;
5070c0d06caSMauro Carvalho Chehab 	struct uvc_frame *frame;
5080c0d06caSMauro Carvalho Chehab 	const unsigned char *start = buffer;
509e1b78a33SPhilipp Zabel 	unsigned int width_multiplier = 1;
5100c0d06caSMauro Carvalho Chehab 	unsigned int interval;
5110c0d06caSMauro Carvalho Chehab 	unsigned int i, n;
5122c6b222cSLaurent Pinchart 	u8 ftype;
5130c0d06caSMauro Carvalho Chehab 
5140c0d06caSMauro Carvalho Chehab 	format->type = buffer[2];
5150c0d06caSMauro Carvalho Chehab 	format->index = buffer[3];
5160c0d06caSMauro Carvalho Chehab 
5170c0d06caSMauro Carvalho Chehab 	switch (buffer[2]) {
5180c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_UNCOMPRESSED:
5190c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_FRAME_BASED:
5200c0d06caSMauro Carvalho Chehab 		n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
5210c0d06caSMauro Carvalho Chehab 		if (buflen < n) {
5229e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
523ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d FORMAT error\n",
5240c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
5250c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber);
5260c0d06caSMauro Carvalho Chehab 			return -EINVAL;
5270c0d06caSMauro Carvalho Chehab 		}
5280c0d06caSMauro Carvalho Chehab 
5290c0d06caSMauro Carvalho Chehab 		/* Find the format descriptor from its GUID. */
5300c0d06caSMauro Carvalho Chehab 		fmtdesc = uvc_format_by_guid(&buffer[5]);
5310c0d06caSMauro Carvalho Chehab 
5320c0d06caSMauro Carvalho Chehab 		if (fmtdesc != NULL) {
533c0decac1SMauro Carvalho Chehab 			strscpy(format->name, fmtdesc->name,
534f14d4988SLaurent Pinchart 				sizeof(format->name));
5350c0d06caSMauro Carvalho Chehab 			format->fcc = fmtdesc->fcc;
5360c0d06caSMauro Carvalho Chehab 		} else {
53769df0954SRicardo Ribalda 			dev_info(&streaming->intf->dev,
53869df0954SRicardo Ribalda 				 "Unknown video format %pUl\n", &buffer[5]);
5390c0d06caSMauro Carvalho Chehab 			snprintf(format->name, sizeof(format->name), "%pUl\n",
5400c0d06caSMauro Carvalho Chehab 				&buffer[5]);
5410c0d06caSMauro Carvalho Chehab 			format->fcc = 0;
5420c0d06caSMauro Carvalho Chehab 		}
5430c0d06caSMauro Carvalho Chehab 
5440c0d06caSMauro Carvalho Chehab 		format->bpp = buffer[21];
545e1b78a33SPhilipp Zabel 
546e1b78a33SPhilipp Zabel 		/* Some devices report a format that doesn't match what they
547e1b78a33SPhilipp Zabel 		 * really send.
548e1b78a33SPhilipp Zabel 		 */
549e1b78a33SPhilipp Zabel 		if (dev->quirks & UVC_QUIRK_FORCE_Y8) {
550e1b78a33SPhilipp Zabel 			if (format->fcc == V4L2_PIX_FMT_YUYV) {
551c0decac1SMauro Carvalho Chehab 				strscpy(format->name, "Greyscale 8-bit (Y8  )",
552e1b78a33SPhilipp Zabel 					sizeof(format->name));
553e1b78a33SPhilipp Zabel 				format->fcc = V4L2_PIX_FMT_GREY;
554e1b78a33SPhilipp Zabel 				format->bpp = 8;
555e1b78a33SPhilipp Zabel 				width_multiplier = 2;
556e1b78a33SPhilipp Zabel 			}
557e1b78a33SPhilipp Zabel 		}
558e1b78a33SPhilipp Zabel 
5591dd2e8f9SSergey Zakharchenko 		/* Some devices report bpp that doesn't match the format. */
5601dd2e8f9SSergey Zakharchenko 		if (dev->quirks & UVC_QUIRK_FORCE_BPP) {
5611dd2e8f9SSergey Zakharchenko 			const struct v4l2_format_info *info =
5621dd2e8f9SSergey Zakharchenko 				v4l2_format_info(format->fcc);
5631dd2e8f9SSergey Zakharchenko 
5641dd2e8f9SSergey Zakharchenko 			if (info) {
5651dd2e8f9SSergey Zakharchenko 				unsigned int div = info->hdiv * info->vdiv;
5661dd2e8f9SSergey Zakharchenko 
5671dd2e8f9SSergey Zakharchenko 				n = info->bpp[0] * div;
5681dd2e8f9SSergey Zakharchenko 				for (i = 1; i < info->comp_planes; i++)
5691dd2e8f9SSergey Zakharchenko 					n += info->bpp[i];
5701dd2e8f9SSergey Zakharchenko 
5711dd2e8f9SSergey Zakharchenko 				format->bpp = DIV_ROUND_UP(8 * n, div);
5721dd2e8f9SSergey Zakharchenko 			}
5731dd2e8f9SSergey Zakharchenko 		}
5741dd2e8f9SSergey Zakharchenko 
5750c0d06caSMauro Carvalho Chehab 		if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
5760c0d06caSMauro Carvalho Chehab 			ftype = UVC_VS_FRAME_UNCOMPRESSED;
5770c0d06caSMauro Carvalho Chehab 		} else {
5780c0d06caSMauro Carvalho Chehab 			ftype = UVC_VS_FRAME_FRAME_BASED;
5790c0d06caSMauro Carvalho Chehab 			if (buffer[27])
5800c0d06caSMauro Carvalho Chehab 				format->flags = UVC_FMT_FLAG_COMPRESSED;
5810c0d06caSMauro Carvalho Chehab 		}
5820c0d06caSMauro Carvalho Chehab 		break;
5830c0d06caSMauro Carvalho Chehab 
5840c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_MJPEG:
5850c0d06caSMauro Carvalho Chehab 		if (buflen < 11) {
5869e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
587ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d FORMAT error\n",
5880c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
5890c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber);
5900c0d06caSMauro Carvalho Chehab 			return -EINVAL;
5910c0d06caSMauro Carvalho Chehab 		}
5920c0d06caSMauro Carvalho Chehab 
593c0decac1SMauro Carvalho Chehab 		strscpy(format->name, "MJPEG", sizeof(format->name));
5940c0d06caSMauro Carvalho Chehab 		format->fcc = V4L2_PIX_FMT_MJPEG;
5950c0d06caSMauro Carvalho Chehab 		format->flags = UVC_FMT_FLAG_COMPRESSED;
5960c0d06caSMauro Carvalho Chehab 		format->bpp = 0;
5970c0d06caSMauro Carvalho Chehab 		ftype = UVC_VS_FRAME_MJPEG;
5980c0d06caSMauro Carvalho Chehab 		break;
5990c0d06caSMauro Carvalho Chehab 
6000c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_DV:
6010c0d06caSMauro Carvalho Chehab 		if (buflen < 9) {
6029e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
603ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d FORMAT error\n",
6040c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
6050c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber);
6060c0d06caSMauro Carvalho Chehab 			return -EINVAL;
6070c0d06caSMauro Carvalho Chehab 		}
6080c0d06caSMauro Carvalho Chehab 
6090c0d06caSMauro Carvalho Chehab 		switch (buffer[8] & 0x7f) {
6100c0d06caSMauro Carvalho Chehab 		case 0:
611c0decac1SMauro Carvalho Chehab 			strscpy(format->name, "SD-DV", sizeof(format->name));
6120c0d06caSMauro Carvalho Chehab 			break;
6130c0d06caSMauro Carvalho Chehab 		case 1:
614c0decac1SMauro Carvalho Chehab 			strscpy(format->name, "SDL-DV", sizeof(format->name));
6150c0d06caSMauro Carvalho Chehab 			break;
6160c0d06caSMauro Carvalho Chehab 		case 2:
617c0decac1SMauro Carvalho Chehab 			strscpy(format->name, "HD-DV", sizeof(format->name));
6180c0d06caSMauro Carvalho Chehab 			break;
6190c0d06caSMauro Carvalho Chehab 		default:
6209e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
621ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d: unknown DV format %u\n",
6220c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
6230c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber, buffer[8]);
6240c0d06caSMauro Carvalho Chehab 			return -EINVAL;
6250c0d06caSMauro Carvalho Chehab 		}
6260c0d06caSMauro Carvalho Chehab 
6270c0d06caSMauro Carvalho Chehab 		strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
628f14d4988SLaurent Pinchart 			sizeof(format->name));
6290c0d06caSMauro Carvalho Chehab 
6300c0d06caSMauro Carvalho Chehab 		format->fcc = V4L2_PIX_FMT_DV;
6310c0d06caSMauro Carvalho Chehab 		format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
6320c0d06caSMauro Carvalho Chehab 		format->bpp = 0;
6330c0d06caSMauro Carvalho Chehab 		ftype = 0;
6340c0d06caSMauro Carvalho Chehab 
6350c0d06caSMauro Carvalho Chehab 		/* Create a dummy frame descriptor. */
6360c0d06caSMauro Carvalho Chehab 		frame = &format->frame[0];
637f14d4988SLaurent Pinchart 		memset(&format->frame[0], 0, sizeof(format->frame[0]));
6380c0d06caSMauro Carvalho Chehab 		frame->bFrameIntervalType = 1;
6390c0d06caSMauro Carvalho Chehab 		frame->dwDefaultFrameInterval = 1;
6400c0d06caSMauro Carvalho Chehab 		frame->dwFrameInterval = *intervals;
6410c0d06caSMauro Carvalho Chehab 		*(*intervals)++ = 1;
6420c0d06caSMauro Carvalho Chehab 		format->nframes = 1;
6430c0d06caSMauro Carvalho Chehab 		break;
6440c0d06caSMauro Carvalho Chehab 
6450c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_MPEG2TS:
6460c0d06caSMauro Carvalho Chehab 	case UVC_VS_FORMAT_STREAM_BASED:
6470c0d06caSMauro Carvalho Chehab 		/* Not supported yet. */
6480c0d06caSMauro Carvalho Chehab 	default:
6499e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
650ed4c5fa4SRicardo Ribalda 			"device %d videostreaming interface %d unsupported format %u\n",
6510c0d06caSMauro Carvalho Chehab 			dev->udev->devnum, alts->desc.bInterfaceNumber,
6520c0d06caSMauro Carvalho Chehab 			buffer[2]);
6530c0d06caSMauro Carvalho Chehab 		return -EINVAL;
6540c0d06caSMauro Carvalho Chehab 	}
6550c0d06caSMauro Carvalho Chehab 
6569e56380aSJoe Perches 	uvc_dbg(dev, DESCR, "Found format %s\n", format->name);
6570c0d06caSMauro Carvalho Chehab 
6580c0d06caSMauro Carvalho Chehab 	buflen -= buffer[0];
6590c0d06caSMauro Carvalho Chehab 	buffer += buffer[0];
6600c0d06caSMauro Carvalho Chehab 
6610c0d06caSMauro Carvalho Chehab 	/* Parse the frame descriptors. Only uncompressed, MJPEG and frame
6620c0d06caSMauro Carvalho Chehab 	 * based formats have frame descriptors.
6630c0d06caSMauro Carvalho Chehab 	 */
6640c0d06caSMauro Carvalho Chehab 	while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
6650c0d06caSMauro Carvalho Chehab 	       buffer[2] == ftype) {
6660c0d06caSMauro Carvalho Chehab 		frame = &format->frame[format->nframes];
6670c0d06caSMauro Carvalho Chehab 		if (ftype != UVC_VS_FRAME_FRAME_BASED)
6680c0d06caSMauro Carvalho Chehab 			n = buflen > 25 ? buffer[25] : 0;
6690c0d06caSMauro Carvalho Chehab 		else
6700c0d06caSMauro Carvalho Chehab 			n = buflen > 21 ? buffer[21] : 0;
6710c0d06caSMauro Carvalho Chehab 
6720c0d06caSMauro Carvalho Chehab 		n = n ? n : 3;
6730c0d06caSMauro Carvalho Chehab 
6740c0d06caSMauro Carvalho Chehab 		if (buflen < 26 + 4*n) {
6759e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
676ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d FRAME error\n",
677ed4c5fa4SRicardo Ribalda 				dev->udev->devnum,
6780c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber);
6790c0d06caSMauro Carvalho Chehab 			return -EINVAL;
6800c0d06caSMauro Carvalho Chehab 		}
6810c0d06caSMauro Carvalho Chehab 
6820c0d06caSMauro Carvalho Chehab 		frame->bFrameIndex = buffer[3];
6830c0d06caSMauro Carvalho Chehab 		frame->bmCapabilities = buffer[4];
684e1b78a33SPhilipp Zabel 		frame->wWidth = get_unaligned_le16(&buffer[5])
685e1b78a33SPhilipp Zabel 			      * width_multiplier;
6860c0d06caSMauro Carvalho Chehab 		frame->wHeight = get_unaligned_le16(&buffer[7]);
6870c0d06caSMauro Carvalho Chehab 		frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
6880c0d06caSMauro Carvalho Chehab 		frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
6890c0d06caSMauro Carvalho Chehab 		if (ftype != UVC_VS_FRAME_FRAME_BASED) {
6900c0d06caSMauro Carvalho Chehab 			frame->dwMaxVideoFrameBufferSize =
6910c0d06caSMauro Carvalho Chehab 				get_unaligned_le32(&buffer[17]);
6920c0d06caSMauro Carvalho Chehab 			frame->dwDefaultFrameInterval =
6930c0d06caSMauro Carvalho Chehab 				get_unaligned_le32(&buffer[21]);
6940c0d06caSMauro Carvalho Chehab 			frame->bFrameIntervalType = buffer[25];
6950c0d06caSMauro Carvalho Chehab 		} else {
6960c0d06caSMauro Carvalho Chehab 			frame->dwMaxVideoFrameBufferSize = 0;
6970c0d06caSMauro Carvalho Chehab 			frame->dwDefaultFrameInterval =
6980c0d06caSMauro Carvalho Chehab 				get_unaligned_le32(&buffer[17]);
6990c0d06caSMauro Carvalho Chehab 			frame->bFrameIntervalType = buffer[21];
7000c0d06caSMauro Carvalho Chehab 		}
7010c0d06caSMauro Carvalho Chehab 		frame->dwFrameInterval = *intervals;
7020c0d06caSMauro Carvalho Chehab 
7030c0d06caSMauro Carvalho Chehab 		/* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
7040c0d06caSMauro Carvalho Chehab 		 * completely. Observed behaviours range from setting the
7050c0d06caSMauro Carvalho Chehab 		 * value to 1.1x the actual frame size to hardwiring the
7060c0d06caSMauro Carvalho Chehab 		 * 16 low bits to 0. This results in a higher than necessary
7070c0d06caSMauro Carvalho Chehab 		 * memory usage as well as a wrong image size information. For
7080c0d06caSMauro Carvalho Chehab 		 * uncompressed formats this can be fixed by computing the
7090c0d06caSMauro Carvalho Chehab 		 * value from the frame size.
7100c0d06caSMauro Carvalho Chehab 		 */
7110c0d06caSMauro Carvalho Chehab 		if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
7120c0d06caSMauro Carvalho Chehab 			frame->dwMaxVideoFrameBufferSize = format->bpp
7130c0d06caSMauro Carvalho Chehab 				* frame->wWidth * frame->wHeight / 8;
7140c0d06caSMauro Carvalho Chehab 
7150c0d06caSMauro Carvalho Chehab 		/* Some bogus devices report dwMinFrameInterval equal to
7160c0d06caSMauro Carvalho Chehab 		 * dwMaxFrameInterval and have dwFrameIntervalStep set to
7170c0d06caSMauro Carvalho Chehab 		 * zero. Setting all null intervals to 1 fixes the problem and
7180c0d06caSMauro Carvalho Chehab 		 * some other divisions by zero that could happen.
7190c0d06caSMauro Carvalho Chehab 		 */
7200c0d06caSMauro Carvalho Chehab 		for (i = 0; i < n; ++i) {
7210c0d06caSMauro Carvalho Chehab 			interval = get_unaligned_le32(&buffer[26+4*i]);
7220c0d06caSMauro Carvalho Chehab 			*(*intervals)++ = interval ? interval : 1;
7230c0d06caSMauro Carvalho Chehab 		}
7240c0d06caSMauro Carvalho Chehab 
7250c0d06caSMauro Carvalho Chehab 		/* Make sure that the default frame interval stays between
7260c0d06caSMauro Carvalho Chehab 		 * the boundaries.
7270c0d06caSMauro Carvalho Chehab 		 */
7280c0d06caSMauro Carvalho Chehab 		n -= frame->bFrameIntervalType ? 1 : 2;
7290c0d06caSMauro Carvalho Chehab 		frame->dwDefaultFrameInterval =
7300c0d06caSMauro Carvalho Chehab 			min(frame->dwFrameInterval[n],
7310c0d06caSMauro Carvalho Chehab 			    max(frame->dwFrameInterval[0],
7320c0d06caSMauro Carvalho Chehab 				frame->dwDefaultFrameInterval));
7330c0d06caSMauro Carvalho Chehab 
7340c0d06caSMauro Carvalho Chehab 		if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) {
7350c0d06caSMauro Carvalho Chehab 			frame->bFrameIntervalType = 1;
7360c0d06caSMauro Carvalho Chehab 			frame->dwFrameInterval[0] =
7370c0d06caSMauro Carvalho Chehab 				frame->dwDefaultFrameInterval;
7380c0d06caSMauro Carvalho Chehab 		}
7390c0d06caSMauro Carvalho Chehab 
7409e56380aSJoe Perches 		uvc_dbg(dev, DESCR, "- %ux%u (%u.%u fps)\n",
7410c0d06caSMauro Carvalho Chehab 			frame->wWidth, frame->wHeight,
7420c0d06caSMauro Carvalho Chehab 			10000000 / frame->dwDefaultFrameInterval,
7430c0d06caSMauro Carvalho Chehab 			(100000000 / frame->dwDefaultFrameInterval) % 10);
7440c0d06caSMauro Carvalho Chehab 
7450c0d06caSMauro Carvalho Chehab 		format->nframes++;
7460c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
7470c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
7480c0d06caSMauro Carvalho Chehab 	}
7490c0d06caSMauro Carvalho Chehab 
7500c0d06caSMauro Carvalho Chehab 	if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
7510c0d06caSMauro Carvalho Chehab 	    buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
7520c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
7530c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
7540c0d06caSMauro Carvalho Chehab 	}
7550c0d06caSMauro Carvalho Chehab 
7560c0d06caSMauro Carvalho Chehab 	if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE &&
7570c0d06caSMauro Carvalho Chehab 	    buffer[2] == UVC_VS_COLORFORMAT) {
7580c0d06caSMauro Carvalho Chehab 		if (buflen < 6) {
7599e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
760ed4c5fa4SRicardo Ribalda 				"device %d videostreaming interface %d COLORFORMAT error\n",
7610c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
7620c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber);
7630c0d06caSMauro Carvalho Chehab 			return -EINVAL;
7640c0d06caSMauro Carvalho Chehab 		}
7650c0d06caSMauro Carvalho Chehab 
7660c0d06caSMauro Carvalho Chehab 		format->colorspace = uvc_colorspace(buffer[3]);
767ec2c23f6SAdam Goode 		format->xfer_func = uvc_xfer_func(buffer[4]);
768ec2c23f6SAdam Goode 		format->ycbcr_enc = uvc_ycbcr_enc(buffer[5]);
7690c0d06caSMauro Carvalho Chehab 
7700c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
7710c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
772e82822faSRicardo Ribalda 	} else {
773e82822faSRicardo Ribalda 		format->colorspace = V4L2_COLORSPACE_SRGB;
7740c0d06caSMauro Carvalho Chehab 	}
7750c0d06caSMauro Carvalho Chehab 
7760c0d06caSMauro Carvalho Chehab 	return buffer - start;
7770c0d06caSMauro Carvalho Chehab }
7780c0d06caSMauro Carvalho Chehab 
7790c0d06caSMauro Carvalho Chehab static int uvc_parse_streaming(struct uvc_device *dev,
7800c0d06caSMauro Carvalho Chehab 	struct usb_interface *intf)
7810c0d06caSMauro Carvalho Chehab {
7820c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *streaming = NULL;
7830c0d06caSMauro Carvalho Chehab 	struct uvc_format *format;
7840c0d06caSMauro Carvalho Chehab 	struct uvc_frame *frame;
7850c0d06caSMauro Carvalho Chehab 	struct usb_host_interface *alts = &intf->altsetting[0];
7860c0d06caSMauro Carvalho Chehab 	unsigned char *_buffer, *buffer = alts->extra;
7870c0d06caSMauro Carvalho Chehab 	int _buflen, buflen = alts->extralen;
7880c0d06caSMauro Carvalho Chehab 	unsigned int nformats = 0, nframes = 0, nintervals = 0;
7890c0d06caSMauro Carvalho Chehab 	unsigned int size, i, n, p;
7902c6b222cSLaurent Pinchart 	u32 *interval;
7912c6b222cSLaurent Pinchart 	u16 psize;
7920c0d06caSMauro Carvalho Chehab 	int ret = -EINVAL;
7930c0d06caSMauro Carvalho Chehab 
7940c0d06caSMauro Carvalho Chehab 	if (intf->cur_altsetting->desc.bInterfaceSubClass
7950c0d06caSMauro Carvalho Chehab 		!= UVC_SC_VIDEOSTREAMING) {
7969e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
797ed4c5fa4SRicardo Ribalda 			"device %d interface %d isn't a video streaming interface\n",
798ed4c5fa4SRicardo Ribalda 			dev->udev->devnum,
7990c0d06caSMauro Carvalho Chehab 			intf->altsetting[0].desc.bInterfaceNumber);
8000c0d06caSMauro Carvalho Chehab 		return -EINVAL;
8010c0d06caSMauro Carvalho Chehab 	}
8020c0d06caSMauro Carvalho Chehab 
8030c0d06caSMauro Carvalho Chehab 	if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
8049e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
805ed4c5fa4SRicardo Ribalda 			"device %d interface %d is already claimed\n",
806ed4c5fa4SRicardo Ribalda 			dev->udev->devnum,
8070c0d06caSMauro Carvalho Chehab 			intf->altsetting[0].desc.bInterfaceNumber);
8080c0d06caSMauro Carvalho Chehab 		return -EINVAL;
8090c0d06caSMauro Carvalho Chehab 	}
8100c0d06caSMauro Carvalho Chehab 
811ece41454SKieran Bingham 	streaming = uvc_stream_new(dev, intf);
8120c0d06caSMauro Carvalho Chehab 	if (streaming == NULL) {
8130c0d06caSMauro Carvalho Chehab 		usb_driver_release_interface(&uvc_driver.driver, intf);
814ece41454SKieran Bingham 		return -ENOMEM;
8150c0d06caSMauro Carvalho Chehab 	}
8160c0d06caSMauro Carvalho Chehab 
8170c0d06caSMauro Carvalho Chehab 	/* The Pico iMage webcam has its class-specific interface descriptors
8180c0d06caSMauro Carvalho Chehab 	 * after the endpoint descriptors.
8190c0d06caSMauro Carvalho Chehab 	 */
8200c0d06caSMauro Carvalho Chehab 	if (buflen == 0) {
8210c0d06caSMauro Carvalho Chehab 		for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
8220c0d06caSMauro Carvalho Chehab 			struct usb_host_endpoint *ep = &alts->endpoint[i];
8230c0d06caSMauro Carvalho Chehab 
8240c0d06caSMauro Carvalho Chehab 			if (ep->extralen == 0)
8250c0d06caSMauro Carvalho Chehab 				continue;
8260c0d06caSMauro Carvalho Chehab 
8270c0d06caSMauro Carvalho Chehab 			if (ep->extralen > 2 &&
8280c0d06caSMauro Carvalho Chehab 			    ep->extra[1] == USB_DT_CS_INTERFACE) {
8299e56380aSJoe Perches 				uvc_dbg(dev, DESCR,
8309e56380aSJoe Perches 					"trying extra data from endpoint %u\n",
831ed4c5fa4SRicardo Ribalda 					i);
8320c0d06caSMauro Carvalho Chehab 				buffer = alts->endpoint[i].extra;
8330c0d06caSMauro Carvalho Chehab 				buflen = alts->endpoint[i].extralen;
8340c0d06caSMauro Carvalho Chehab 				break;
8350c0d06caSMauro Carvalho Chehab 			}
8360c0d06caSMauro Carvalho Chehab 		}
8370c0d06caSMauro Carvalho Chehab 	}
8380c0d06caSMauro Carvalho Chehab 
8390c0d06caSMauro Carvalho Chehab 	/* Skip the standard interface descriptors. */
8400c0d06caSMauro Carvalho Chehab 	while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
8410c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
8420c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
8430c0d06caSMauro Carvalho Chehab 	}
8440c0d06caSMauro Carvalho Chehab 
8450c0d06caSMauro Carvalho Chehab 	if (buflen <= 2) {
8469e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
8479e56380aSJoe Perches 			"no class-specific streaming interface descriptors found\n");
8480c0d06caSMauro Carvalho Chehab 		goto error;
8490c0d06caSMauro Carvalho Chehab 	}
8500c0d06caSMauro Carvalho Chehab 
8510c0d06caSMauro Carvalho Chehab 	/* Parse the header descriptor. */
8520c0d06caSMauro Carvalho Chehab 	switch (buffer[2]) {
8530c0d06caSMauro Carvalho Chehab 	case UVC_VS_OUTPUT_HEADER:
8540c0d06caSMauro Carvalho Chehab 		streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
8550c0d06caSMauro Carvalho Chehab 		size = 9;
8560c0d06caSMauro Carvalho Chehab 		break;
8570c0d06caSMauro Carvalho Chehab 
8580c0d06caSMauro Carvalho Chehab 	case UVC_VS_INPUT_HEADER:
8590c0d06caSMauro Carvalho Chehab 		streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
8600c0d06caSMauro Carvalho Chehab 		size = 13;
8610c0d06caSMauro Carvalho Chehab 		break;
8620c0d06caSMauro Carvalho Chehab 
8630c0d06caSMauro Carvalho Chehab 	default:
8649e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
8659e56380aSJoe Perches 			"device %d videostreaming interface %d HEADER descriptor not found\n",
866ed4c5fa4SRicardo Ribalda 			dev->udev->devnum, alts->desc.bInterfaceNumber);
8670c0d06caSMauro Carvalho Chehab 		goto error;
8680c0d06caSMauro Carvalho Chehab 	}
8690c0d06caSMauro Carvalho Chehab 
8700c0d06caSMauro Carvalho Chehab 	p = buflen >= 4 ? buffer[3] : 0;
8710c0d06caSMauro Carvalho Chehab 	n = buflen >= size ? buffer[size-1] : 0;
8720c0d06caSMauro Carvalho Chehab 
8730c0d06caSMauro Carvalho Chehab 	if (buflen < size + p*n) {
8749e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
8759e56380aSJoe Perches 			"device %d videostreaming interface %d HEADER descriptor is invalid\n",
8760c0d06caSMauro Carvalho Chehab 			dev->udev->devnum, alts->desc.bInterfaceNumber);
8770c0d06caSMauro Carvalho Chehab 		goto error;
8780c0d06caSMauro Carvalho Chehab 	}
8790c0d06caSMauro Carvalho Chehab 
8800c0d06caSMauro Carvalho Chehab 	streaming->header.bNumFormats = p;
8810c0d06caSMauro Carvalho Chehab 	streaming->header.bEndpointAddress = buffer[6];
8820c0d06caSMauro Carvalho Chehab 	if (buffer[2] == UVC_VS_INPUT_HEADER) {
8830c0d06caSMauro Carvalho Chehab 		streaming->header.bmInfo = buffer[7];
8840c0d06caSMauro Carvalho Chehab 		streaming->header.bTerminalLink = buffer[8];
8850c0d06caSMauro Carvalho Chehab 		streaming->header.bStillCaptureMethod = buffer[9];
8860c0d06caSMauro Carvalho Chehab 		streaming->header.bTriggerSupport = buffer[10];
8870c0d06caSMauro Carvalho Chehab 		streaming->header.bTriggerUsage = buffer[11];
8880c0d06caSMauro Carvalho Chehab 	} else {
8890c0d06caSMauro Carvalho Chehab 		streaming->header.bTerminalLink = buffer[7];
8900c0d06caSMauro Carvalho Chehab 	}
8910c0d06caSMauro Carvalho Chehab 	streaming->header.bControlSize = n;
8920c0d06caSMauro Carvalho Chehab 
8930c0d06caSMauro Carvalho Chehab 	streaming->header.bmaControls = kmemdup(&buffer[size], p * n,
8940c0d06caSMauro Carvalho Chehab 						GFP_KERNEL);
8950c0d06caSMauro Carvalho Chehab 	if (streaming->header.bmaControls == NULL) {
8960c0d06caSMauro Carvalho Chehab 		ret = -ENOMEM;
8970c0d06caSMauro Carvalho Chehab 		goto error;
8980c0d06caSMauro Carvalho Chehab 	}
8990c0d06caSMauro Carvalho Chehab 
9000c0d06caSMauro Carvalho Chehab 	buflen -= buffer[0];
9010c0d06caSMauro Carvalho Chehab 	buffer += buffer[0];
9020c0d06caSMauro Carvalho Chehab 
9030c0d06caSMauro Carvalho Chehab 	_buffer = buffer;
9040c0d06caSMauro Carvalho Chehab 	_buflen = buflen;
9050c0d06caSMauro Carvalho Chehab 
9060c0d06caSMauro Carvalho Chehab 	/* Count the format and frame descriptors. */
9070c0d06caSMauro Carvalho Chehab 	while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
9080c0d06caSMauro Carvalho Chehab 		switch (_buffer[2]) {
9090c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_UNCOMPRESSED:
9100c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_MJPEG:
9110c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_FRAME_BASED:
9120c0d06caSMauro Carvalho Chehab 			nformats++;
9130c0d06caSMauro Carvalho Chehab 			break;
9140c0d06caSMauro Carvalho Chehab 
9150c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_DV:
9160c0d06caSMauro Carvalho Chehab 			/* DV format has no frame descriptor. We will create a
9170c0d06caSMauro Carvalho Chehab 			 * dummy frame descriptor with a dummy frame interval.
9180c0d06caSMauro Carvalho Chehab 			 */
9190c0d06caSMauro Carvalho Chehab 			nformats++;
9200c0d06caSMauro Carvalho Chehab 			nframes++;
9210c0d06caSMauro Carvalho Chehab 			nintervals++;
9220c0d06caSMauro Carvalho Chehab 			break;
9230c0d06caSMauro Carvalho Chehab 
9240c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_MPEG2TS:
9250c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_STREAM_BASED:
9269e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
9279e56380aSJoe Perches 				"device %d videostreaming interface %d FORMAT %u is not supported\n",
9280c0d06caSMauro Carvalho Chehab 				dev->udev->devnum,
9290c0d06caSMauro Carvalho Chehab 				alts->desc.bInterfaceNumber, _buffer[2]);
9300c0d06caSMauro Carvalho Chehab 			break;
9310c0d06caSMauro Carvalho Chehab 
9320c0d06caSMauro Carvalho Chehab 		case UVC_VS_FRAME_UNCOMPRESSED:
9330c0d06caSMauro Carvalho Chehab 		case UVC_VS_FRAME_MJPEG:
9340c0d06caSMauro Carvalho Chehab 			nframes++;
9350c0d06caSMauro Carvalho Chehab 			if (_buflen > 25)
9360c0d06caSMauro Carvalho Chehab 				nintervals += _buffer[25] ? _buffer[25] : 3;
9370c0d06caSMauro Carvalho Chehab 			break;
9380c0d06caSMauro Carvalho Chehab 
9390c0d06caSMauro Carvalho Chehab 		case UVC_VS_FRAME_FRAME_BASED:
9400c0d06caSMauro Carvalho Chehab 			nframes++;
9410c0d06caSMauro Carvalho Chehab 			if (_buflen > 21)
9420c0d06caSMauro Carvalho Chehab 				nintervals += _buffer[21] ? _buffer[21] : 3;
9430c0d06caSMauro Carvalho Chehab 			break;
9440c0d06caSMauro Carvalho Chehab 		}
9450c0d06caSMauro Carvalho Chehab 
9460c0d06caSMauro Carvalho Chehab 		_buflen -= _buffer[0];
9470c0d06caSMauro Carvalho Chehab 		_buffer += _buffer[0];
9480c0d06caSMauro Carvalho Chehab 	}
9490c0d06caSMauro Carvalho Chehab 
9500c0d06caSMauro Carvalho Chehab 	if (nformats == 0) {
9519e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
9529e56380aSJoe Perches 			"device %d videostreaming interface %d has no supported formats defined\n",
9530c0d06caSMauro Carvalho Chehab 			dev->udev->devnum, alts->desc.bInterfaceNumber);
9540c0d06caSMauro Carvalho Chehab 		goto error;
9550c0d06caSMauro Carvalho Chehab 	}
9560c0d06caSMauro Carvalho Chehab 
957f14d4988SLaurent Pinchart 	size = nformats * sizeof(*format) + nframes * sizeof(*frame)
958f14d4988SLaurent Pinchart 	     + nintervals * sizeof(*interval);
9590c0d06caSMauro Carvalho Chehab 	format = kzalloc(size, GFP_KERNEL);
9600c0d06caSMauro Carvalho Chehab 	if (format == NULL) {
9610c0d06caSMauro Carvalho Chehab 		ret = -ENOMEM;
9620c0d06caSMauro Carvalho Chehab 		goto error;
9630c0d06caSMauro Carvalho Chehab 	}
9640c0d06caSMauro Carvalho Chehab 
9650c0d06caSMauro Carvalho Chehab 	frame = (struct uvc_frame *)&format[nformats];
9662c6b222cSLaurent Pinchart 	interval = (u32 *)&frame[nframes];
9670c0d06caSMauro Carvalho Chehab 
9680c0d06caSMauro Carvalho Chehab 	streaming->format = format;
9690c0d06caSMauro Carvalho Chehab 	streaming->nformats = nformats;
9700c0d06caSMauro Carvalho Chehab 
9710c0d06caSMauro Carvalho Chehab 	/* Parse the format descriptors. */
9720c0d06caSMauro Carvalho Chehab 	while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
9730c0d06caSMauro Carvalho Chehab 		switch (buffer[2]) {
9740c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_UNCOMPRESSED:
9750c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_MJPEG:
9760c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_DV:
9770c0d06caSMauro Carvalho Chehab 		case UVC_VS_FORMAT_FRAME_BASED:
9780c0d06caSMauro Carvalho Chehab 			format->frame = frame;
9790c0d06caSMauro Carvalho Chehab 			ret = uvc_parse_format(dev, streaming, format,
9800c0d06caSMauro Carvalho Chehab 				&interval, buffer, buflen);
9810c0d06caSMauro Carvalho Chehab 			if (ret < 0)
9820c0d06caSMauro Carvalho Chehab 				goto error;
9830c0d06caSMauro Carvalho Chehab 
9840c0d06caSMauro Carvalho Chehab 			frame += format->nframes;
9850c0d06caSMauro Carvalho Chehab 			format++;
9860c0d06caSMauro Carvalho Chehab 
9870c0d06caSMauro Carvalho Chehab 			buflen -= ret;
9880c0d06caSMauro Carvalho Chehab 			buffer += ret;
9890c0d06caSMauro Carvalho Chehab 			continue;
9900c0d06caSMauro Carvalho Chehab 
9910c0d06caSMauro Carvalho Chehab 		default:
9920c0d06caSMauro Carvalho Chehab 			break;
9930c0d06caSMauro Carvalho Chehab 		}
9940c0d06caSMauro Carvalho Chehab 
9950c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
9960c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
9970c0d06caSMauro Carvalho Chehab 	}
9980c0d06caSMauro Carvalho Chehab 
9990c0d06caSMauro Carvalho Chehab 	if (buflen)
10009e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
10019e56380aSJoe Perches 			"device %d videostreaming interface %d has %u bytes of trailing descriptor garbage\n",
10029e56380aSJoe Perches 			dev->udev->devnum, alts->desc.bInterfaceNumber, buflen);
10030c0d06caSMauro Carvalho Chehab 
10040c0d06caSMauro Carvalho Chehab 	/* Parse the alternate settings to find the maximum bandwidth. */
10050c0d06caSMauro Carvalho Chehab 	for (i = 0; i < intf->num_altsetting; ++i) {
10060c0d06caSMauro Carvalho Chehab 		struct usb_host_endpoint *ep;
10070c0d06caSMauro Carvalho Chehab 		alts = &intf->altsetting[i];
10080c0d06caSMauro Carvalho Chehab 		ep = uvc_find_endpoint(alts,
10090c0d06caSMauro Carvalho Chehab 				streaming->header.bEndpointAddress);
10100c0d06caSMauro Carvalho Chehab 		if (ep == NULL)
10110c0d06caSMauro Carvalho Chehab 			continue;
10120c0d06caSMauro Carvalho Chehab 
10130c0d06caSMauro Carvalho Chehab 		psize = le16_to_cpu(ep->desc.wMaxPacketSize);
10140c0d06caSMauro Carvalho Chehab 		psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
10150c0d06caSMauro Carvalho Chehab 		if (psize > streaming->maxpsize)
10160c0d06caSMauro Carvalho Chehab 			streaming->maxpsize = psize;
10170c0d06caSMauro Carvalho Chehab 	}
10180c0d06caSMauro Carvalho Chehab 
10190c0d06caSMauro Carvalho Chehab 	list_add_tail(&streaming->list, &dev->streams);
10200c0d06caSMauro Carvalho Chehab 	return 0;
10210c0d06caSMauro Carvalho Chehab 
10220c0d06caSMauro Carvalho Chehab error:
10230c0d06caSMauro Carvalho Chehab 	usb_driver_release_interface(&uvc_driver.driver, intf);
1024ece41454SKieran Bingham 	uvc_stream_delete(streaming);
10250c0d06caSMauro Carvalho Chehab 	return ret;
10260c0d06caSMauro Carvalho Chehab }
10270c0d06caSMauro Carvalho Chehab 
1028351509c6SRicardo Ribalda static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
10292886477fSRicardo Ribalda static const u8 uvc_gpio_guid[16] = UVC_GUID_EXT_GPIO_CONTROLLER;
1030351509c6SRicardo Ribalda static const u8 uvc_media_transport_input_guid[16] =
1031351509c6SRicardo Ribalda 	UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
1032351509c6SRicardo Ribalda static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
1033351509c6SRicardo Ribalda 
1034cae79e50SRicardo Ribalda static struct uvc_entity *uvc_alloc_entity(u16 type, u16 id,
10350c0d06caSMauro Carvalho Chehab 		unsigned int num_pads, unsigned int extra_size)
10360c0d06caSMauro Carvalho Chehab {
10370c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity;
10380c0d06caSMauro Carvalho Chehab 	unsigned int num_inputs;
10390c0d06caSMauro Carvalho Chehab 	unsigned int size;
10400c0d06caSMauro Carvalho Chehab 	unsigned int i;
10410c0d06caSMauro Carvalho Chehab 
104289dd34caSNadav Amit 	extra_size = roundup(extra_size, sizeof(*entity->pads));
10437532dad6SRicardo Ribalda 	if (num_pads)
10447532dad6SRicardo Ribalda 		num_inputs = type & UVC_TERM_OUTPUT ? num_pads : num_pads - 1;
10457532dad6SRicardo Ribalda 	else
10467532dad6SRicardo Ribalda 		num_inputs = 0;
10470c0d06caSMauro Carvalho Chehab 	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
10480c0d06caSMauro Carvalho Chehab 	     + num_inputs;
10490c0d06caSMauro Carvalho Chehab 	entity = kzalloc(size, GFP_KERNEL);
10500c0d06caSMauro Carvalho Chehab 	if (entity == NULL)
10510c0d06caSMauro Carvalho Chehab 		return NULL;
10520c0d06caSMauro Carvalho Chehab 
10530c0d06caSMauro Carvalho Chehab 	entity->id = id;
10540c0d06caSMauro Carvalho Chehab 	entity->type = type;
10550c0d06caSMauro Carvalho Chehab 
1056351509c6SRicardo Ribalda 	/*
1057351509c6SRicardo Ribalda 	 * Set the GUID for standard entity types. For extension units, the GUID
1058351509c6SRicardo Ribalda 	 * is initialized by the caller.
1059351509c6SRicardo Ribalda 	 */
1060351509c6SRicardo Ribalda 	switch (type) {
10612886477fSRicardo Ribalda 	case UVC_EXT_GPIO_UNIT:
10622886477fSRicardo Ribalda 		memcpy(entity->guid, uvc_gpio_guid, 16);
10632886477fSRicardo Ribalda 		break;
1064351509c6SRicardo Ribalda 	case UVC_ITT_CAMERA:
1065351509c6SRicardo Ribalda 		memcpy(entity->guid, uvc_camera_guid, 16);
1066351509c6SRicardo Ribalda 		break;
1067351509c6SRicardo Ribalda 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1068351509c6SRicardo Ribalda 		memcpy(entity->guid, uvc_media_transport_input_guid, 16);
1069351509c6SRicardo Ribalda 		break;
1070351509c6SRicardo Ribalda 	case UVC_VC_PROCESSING_UNIT:
1071351509c6SRicardo Ribalda 		memcpy(entity->guid, uvc_processing_guid, 16);
1072351509c6SRicardo Ribalda 		break;
1073351509c6SRicardo Ribalda 	}
1074351509c6SRicardo Ribalda 
10750c0d06caSMauro Carvalho Chehab 	entity->num_links = 0;
10760c0d06caSMauro Carvalho Chehab 	entity->num_pads = num_pads;
10770c0d06caSMauro Carvalho Chehab 	entity->pads = ((void *)(entity + 1)) + extra_size;
10780c0d06caSMauro Carvalho Chehab 
10790c0d06caSMauro Carvalho Chehab 	for (i = 0; i < num_inputs; ++i)
10800c0d06caSMauro Carvalho Chehab 		entity->pads[i].flags = MEDIA_PAD_FL_SINK;
10817532dad6SRicardo Ribalda 	if (!UVC_ENTITY_IS_OTERM(entity) && num_pads)
10820c0d06caSMauro Carvalho Chehab 		entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE;
10830c0d06caSMauro Carvalho Chehab 
10840c0d06caSMauro Carvalho Chehab 	entity->bNrInPins = num_inputs;
10852c6b222cSLaurent Pinchart 	entity->baSourceID = (u8 *)(&entity->pads[num_pads]);
10860c0d06caSMauro Carvalho Chehab 
10870c0d06caSMauro Carvalho Chehab 	return entity;
10880c0d06caSMauro Carvalho Chehab }
10890c0d06caSMauro Carvalho Chehab 
10900c0d06caSMauro Carvalho Chehab /* Parse vendor-specific extensions. */
10910c0d06caSMauro Carvalho Chehab static int uvc_parse_vendor_control(struct uvc_device *dev,
10920c0d06caSMauro Carvalho Chehab 	const unsigned char *buffer, int buflen)
10930c0d06caSMauro Carvalho Chehab {
10940c0d06caSMauro Carvalho Chehab 	struct usb_device *udev = dev->udev;
10950c0d06caSMauro Carvalho Chehab 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
10960c0d06caSMauro Carvalho Chehab 	struct uvc_entity *unit;
10970c0d06caSMauro Carvalho Chehab 	unsigned int n, p;
10980c0d06caSMauro Carvalho Chehab 	int handled = 0;
10990c0d06caSMauro Carvalho Chehab 
11000c0d06caSMauro Carvalho Chehab 	switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
11010c0d06caSMauro Carvalho Chehab 	case 0x046d:		/* Logitech */
11020c0d06caSMauro Carvalho Chehab 		if (buffer[1] != 0x41 || buffer[2] != 0x01)
11030c0d06caSMauro Carvalho Chehab 			break;
11040c0d06caSMauro Carvalho Chehab 
11050c0d06caSMauro Carvalho Chehab 		/* Logitech implements several vendor specific functions
11060c0d06caSMauro Carvalho Chehab 		 * through vendor specific extension units (LXU).
11070c0d06caSMauro Carvalho Chehab 		 *
11080c0d06caSMauro Carvalho Chehab 		 * The LXU descriptors are similar to XU descriptors
11090c0d06caSMauro Carvalho Chehab 		 * (see "USB Device Video Class for Video Devices", section
11100c0d06caSMauro Carvalho Chehab 		 * 3.7.2.6 "Extension Unit Descriptor") with the following
11110c0d06caSMauro Carvalho Chehab 		 * differences:
11120c0d06caSMauro Carvalho Chehab 		 *
11130c0d06caSMauro Carvalho Chehab 		 * ----------------------------------------------------------
11140c0d06caSMauro Carvalho Chehab 		 * 0		bLength		1	 Number
11150c0d06caSMauro Carvalho Chehab 		 *	Size of this descriptor, in bytes: 24+p+n*2
11160c0d06caSMauro Carvalho Chehab 		 * ----------------------------------------------------------
11170c0d06caSMauro Carvalho Chehab 		 * 23+p+n	bmControlsType	N	Bitmap
11180c0d06caSMauro Carvalho Chehab 		 *	Individual bits in the set are defined:
11190c0d06caSMauro Carvalho Chehab 		 *	0: Absolute
11200c0d06caSMauro Carvalho Chehab 		 *	1: Relative
11210c0d06caSMauro Carvalho Chehab 		 *
11220c0d06caSMauro Carvalho Chehab 		 *	This bitset is mapped exactly the same as bmControls.
11230c0d06caSMauro Carvalho Chehab 		 * ----------------------------------------------------------
11240c0d06caSMauro Carvalho Chehab 		 * 23+p+n*2	bReserved	1	Boolean
11250c0d06caSMauro Carvalho Chehab 		 * ----------------------------------------------------------
11260c0d06caSMauro Carvalho Chehab 		 * 24+p+n*2	iExtension	1	Index
11270c0d06caSMauro Carvalho Chehab 		 *	Index of a string descriptor that describes this
11280c0d06caSMauro Carvalho Chehab 		 *	extension unit.
11290c0d06caSMauro Carvalho Chehab 		 * ----------------------------------------------------------
11300c0d06caSMauro Carvalho Chehab 		 */
11310c0d06caSMauro Carvalho Chehab 		p = buflen >= 22 ? buffer[21] : 0;
11320c0d06caSMauro Carvalho Chehab 		n = buflen >= 25 + p ? buffer[22+p] : 0;
11330c0d06caSMauro Carvalho Chehab 
11340c0d06caSMauro Carvalho Chehab 		if (buflen < 25 + p + 2*n) {
11359e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1136ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d EXTENSION_UNIT error\n",
11370c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
11380c0d06caSMauro Carvalho Chehab 			break;
11390c0d06caSMauro Carvalho Chehab 		}
11400c0d06caSMauro Carvalho Chehab 
11410c0d06caSMauro Carvalho Chehab 		unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3],
11420c0d06caSMauro Carvalho Chehab 					p + 1, 2*n);
11430c0d06caSMauro Carvalho Chehab 		if (unit == NULL)
11440c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
11450c0d06caSMauro Carvalho Chehab 
1146351509c6SRicardo Ribalda 		memcpy(unit->guid, &buffer[4], 16);
11470c0d06caSMauro Carvalho Chehab 		unit->extension.bNumControls = buffer[20];
11480c0d06caSMauro Carvalho Chehab 		memcpy(unit->baSourceID, &buffer[22], p);
11490c0d06caSMauro Carvalho Chehab 		unit->extension.bControlSize = buffer[22+p];
11502c6b222cSLaurent Pinchart 		unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
11512c6b222cSLaurent Pinchart 		unit->extension.bmControlsType = (u8 *)unit + sizeof(*unit)
11520c0d06caSMauro Carvalho Chehab 					       + n;
11530c0d06caSMauro Carvalho Chehab 		memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
11540c0d06caSMauro Carvalho Chehab 
11550c0d06caSMauro Carvalho Chehab 		if (buffer[24+p+2*n] != 0)
11560c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[24+p+2*n], unit->name,
1157f14d4988SLaurent Pinchart 				   sizeof(unit->name));
11580c0d06caSMauro Carvalho Chehab 		else
11590c0d06caSMauro Carvalho Chehab 			sprintf(unit->name, "Extension %u", buffer[3]);
11600c0d06caSMauro Carvalho Chehab 
11610c0d06caSMauro Carvalho Chehab 		list_add_tail(&unit->list, &dev->entities);
11620c0d06caSMauro Carvalho Chehab 		handled = 1;
11630c0d06caSMauro Carvalho Chehab 		break;
11640c0d06caSMauro Carvalho Chehab 	}
11650c0d06caSMauro Carvalho Chehab 
11660c0d06caSMauro Carvalho Chehab 	return handled;
11670c0d06caSMauro Carvalho Chehab }
11680c0d06caSMauro Carvalho Chehab 
11690c0d06caSMauro Carvalho Chehab static int uvc_parse_standard_control(struct uvc_device *dev,
11700c0d06caSMauro Carvalho Chehab 	const unsigned char *buffer, int buflen)
11710c0d06caSMauro Carvalho Chehab {
11720c0d06caSMauro Carvalho Chehab 	struct usb_device *udev = dev->udev;
11730c0d06caSMauro Carvalho Chehab 	struct uvc_entity *unit, *term;
11740c0d06caSMauro Carvalho Chehab 	struct usb_interface *intf;
11750c0d06caSMauro Carvalho Chehab 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
11760c0d06caSMauro Carvalho Chehab 	unsigned int i, n, p, len;
11772c6b222cSLaurent Pinchart 	u16 type;
11780c0d06caSMauro Carvalho Chehab 
11790c0d06caSMauro Carvalho Chehab 	switch (buffer[2]) {
11800c0d06caSMauro Carvalho Chehab 	case UVC_VC_HEADER:
11810c0d06caSMauro Carvalho Chehab 		n = buflen >= 12 ? buffer[11] : 0;
11820c0d06caSMauro Carvalho Chehab 
1183daf41ac2SOliver Neukum 		if (buflen < 12 + n) {
11849e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1185ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d HEADER error\n",
1186ed4c5fa4SRicardo Ribalda 				udev->devnum, alts->desc.bInterfaceNumber);
11870c0d06caSMauro Carvalho Chehab 			return -EINVAL;
11880c0d06caSMauro Carvalho Chehab 		}
11890c0d06caSMauro Carvalho Chehab 
11900c0d06caSMauro Carvalho Chehab 		dev->uvc_version = get_unaligned_le16(&buffer[3]);
11910c0d06caSMauro Carvalho Chehab 		dev->clock_frequency = get_unaligned_le32(&buffer[7]);
11920c0d06caSMauro Carvalho Chehab 
11930c0d06caSMauro Carvalho Chehab 		/* Parse all USB Video Streaming interfaces. */
11940c0d06caSMauro Carvalho Chehab 		for (i = 0; i < n; ++i) {
11950c0d06caSMauro Carvalho Chehab 			intf = usb_ifnum_to_if(udev, buffer[12+i]);
11960c0d06caSMauro Carvalho Chehab 			if (intf == NULL) {
11979e56380aSJoe Perches 				uvc_dbg(dev, DESCR,
1198ed4c5fa4SRicardo Ribalda 					"device %d interface %d doesn't exists\n",
11990c0d06caSMauro Carvalho Chehab 					udev->devnum, i);
12000c0d06caSMauro Carvalho Chehab 				continue;
12010c0d06caSMauro Carvalho Chehab 			}
12020c0d06caSMauro Carvalho Chehab 
12030c0d06caSMauro Carvalho Chehab 			uvc_parse_streaming(dev, intf);
12040c0d06caSMauro Carvalho Chehab 		}
12050c0d06caSMauro Carvalho Chehab 		break;
12060c0d06caSMauro Carvalho Chehab 
12070c0d06caSMauro Carvalho Chehab 	case UVC_VC_INPUT_TERMINAL:
12080c0d06caSMauro Carvalho Chehab 		if (buflen < 8) {
12099e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1210ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d INPUT_TERMINAL error\n",
12110c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
12120c0d06caSMauro Carvalho Chehab 			return -EINVAL;
12130c0d06caSMauro Carvalho Chehab 		}
12140c0d06caSMauro Carvalho Chehab 
121547bb1179SAlistair Strachan 		/*
121647bb1179SAlistair Strachan 		 * Reject invalid terminal types that would cause issues:
121747bb1179SAlistair Strachan 		 *
121847bb1179SAlistair Strachan 		 * - The high byte must be non-zero, otherwise it would be
121947bb1179SAlistair Strachan 		 *   confused with a unit.
122047bb1179SAlistair Strachan 		 *
122147bb1179SAlistair Strachan 		 * - Bit 15 must be 0, as we use it internally as a terminal
122247bb1179SAlistair Strachan 		 *   direction flag.
122347bb1179SAlistair Strachan 		 *
122447bb1179SAlistair Strachan 		 * Other unknown types are accepted.
12250c0d06caSMauro Carvalho Chehab 		 */
12260c0d06caSMauro Carvalho Chehab 		type = get_unaligned_le16(&buffer[4]);
122747bb1179SAlistair Strachan 		if ((type & 0x7f00) == 0 || (type & 0x8000) != 0) {
12289e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1229ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d INPUT_TERMINAL %d has invalid type 0x%04x, skipping\n",
1230ed4c5fa4SRicardo Ribalda 				udev->devnum, alts->desc.bInterfaceNumber,
12310c0d06caSMauro Carvalho Chehab 				buffer[3], type);
12320c0d06caSMauro Carvalho Chehab 			return 0;
12330c0d06caSMauro Carvalho Chehab 		}
12340c0d06caSMauro Carvalho Chehab 
12350c0d06caSMauro Carvalho Chehab 		n = 0;
12360c0d06caSMauro Carvalho Chehab 		p = 0;
12370c0d06caSMauro Carvalho Chehab 		len = 8;
12380c0d06caSMauro Carvalho Chehab 
12390c0d06caSMauro Carvalho Chehab 		if (type == UVC_ITT_CAMERA) {
12400c0d06caSMauro Carvalho Chehab 			n = buflen >= 15 ? buffer[14] : 0;
12410c0d06caSMauro Carvalho Chehab 			len = 15;
12420c0d06caSMauro Carvalho Chehab 
12430c0d06caSMauro Carvalho Chehab 		} else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
12440c0d06caSMauro Carvalho Chehab 			n = buflen >= 9 ? buffer[8] : 0;
12450c0d06caSMauro Carvalho Chehab 			p = buflen >= 10 + n ? buffer[9+n] : 0;
12460c0d06caSMauro Carvalho Chehab 			len = 10;
12470c0d06caSMauro Carvalho Chehab 		}
12480c0d06caSMauro Carvalho Chehab 
12490c0d06caSMauro Carvalho Chehab 		if (buflen < len + n + p) {
12509e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1251ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d INPUT_TERMINAL error\n",
12520c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
12530c0d06caSMauro Carvalho Chehab 			return -EINVAL;
12540c0d06caSMauro Carvalho Chehab 		}
12550c0d06caSMauro Carvalho Chehab 
12560c0d06caSMauro Carvalho Chehab 		term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3],
12570c0d06caSMauro Carvalho Chehab 					1, n + p);
12580c0d06caSMauro Carvalho Chehab 		if (term == NULL)
12590c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
12600c0d06caSMauro Carvalho Chehab 
12610c0d06caSMauro Carvalho Chehab 		if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
12620c0d06caSMauro Carvalho Chehab 			term->camera.bControlSize = n;
1263f14d4988SLaurent Pinchart 			term->camera.bmControls = (u8 *)term + sizeof(*term);
12640c0d06caSMauro Carvalho Chehab 			term->camera.wObjectiveFocalLengthMin =
12650c0d06caSMauro Carvalho Chehab 				get_unaligned_le16(&buffer[8]);
12660c0d06caSMauro Carvalho Chehab 			term->camera.wObjectiveFocalLengthMax =
12670c0d06caSMauro Carvalho Chehab 				get_unaligned_le16(&buffer[10]);
12680c0d06caSMauro Carvalho Chehab 			term->camera.wOcularFocalLength =
12690c0d06caSMauro Carvalho Chehab 				get_unaligned_le16(&buffer[12]);
12700c0d06caSMauro Carvalho Chehab 			memcpy(term->camera.bmControls, &buffer[15], n);
12710c0d06caSMauro Carvalho Chehab 		} else if (UVC_ENTITY_TYPE(term) ==
12720c0d06caSMauro Carvalho Chehab 			   UVC_ITT_MEDIA_TRANSPORT_INPUT) {
12730c0d06caSMauro Carvalho Chehab 			term->media.bControlSize = n;
1274f14d4988SLaurent Pinchart 			term->media.bmControls = (u8 *)term + sizeof(*term);
12750c0d06caSMauro Carvalho Chehab 			term->media.bTransportModeSize = p;
12762c6b222cSLaurent Pinchart 			term->media.bmTransportModes = (u8 *)term
1277f14d4988SLaurent Pinchart 						     + sizeof(*term) + n;
12780c0d06caSMauro Carvalho Chehab 			memcpy(term->media.bmControls, &buffer[9], n);
12790c0d06caSMauro Carvalho Chehab 			memcpy(term->media.bmTransportModes, &buffer[10+n], p);
12800c0d06caSMauro Carvalho Chehab 		}
12810c0d06caSMauro Carvalho Chehab 
12820c0d06caSMauro Carvalho Chehab 		if (buffer[7] != 0)
12830c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[7], term->name,
1284f14d4988SLaurent Pinchart 				   sizeof(term->name));
12850c0d06caSMauro Carvalho Chehab 		else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
12860c0d06caSMauro Carvalho Chehab 			sprintf(term->name, "Camera %u", buffer[3]);
12870c0d06caSMauro Carvalho Chehab 		else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
12880c0d06caSMauro Carvalho Chehab 			sprintf(term->name, "Media %u", buffer[3]);
12890c0d06caSMauro Carvalho Chehab 		else
12900c0d06caSMauro Carvalho Chehab 			sprintf(term->name, "Input %u", buffer[3]);
12910c0d06caSMauro Carvalho Chehab 
12920c0d06caSMauro Carvalho Chehab 		list_add_tail(&term->list, &dev->entities);
12930c0d06caSMauro Carvalho Chehab 		break;
12940c0d06caSMauro Carvalho Chehab 
12950c0d06caSMauro Carvalho Chehab 	case UVC_VC_OUTPUT_TERMINAL:
12960c0d06caSMauro Carvalho Chehab 		if (buflen < 9) {
12979e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1298ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d OUTPUT_TERMINAL error\n",
12990c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
13000c0d06caSMauro Carvalho Chehab 			return -EINVAL;
13010c0d06caSMauro Carvalho Chehab 		}
13020c0d06caSMauro Carvalho Chehab 
13030c0d06caSMauro Carvalho Chehab 		/* Make sure the terminal type MSB is not null, otherwise it
13040c0d06caSMauro Carvalho Chehab 		 * could be confused with a unit.
13050c0d06caSMauro Carvalho Chehab 		 */
13060c0d06caSMauro Carvalho Chehab 		type = get_unaligned_le16(&buffer[4]);
13070c0d06caSMauro Carvalho Chehab 		if ((type & 0xff00) == 0) {
13089e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1309ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d OUTPUT_TERMINAL %d has invalid type 0x%04x, skipping\n",
1310ed4c5fa4SRicardo Ribalda 				udev->devnum, alts->desc.bInterfaceNumber,
1311ed4c5fa4SRicardo Ribalda 				buffer[3], type);
13120c0d06caSMauro Carvalho Chehab 			return 0;
13130c0d06caSMauro Carvalho Chehab 		}
13140c0d06caSMauro Carvalho Chehab 
13150c0d06caSMauro Carvalho Chehab 		term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3],
13160c0d06caSMauro Carvalho Chehab 					1, 0);
13170c0d06caSMauro Carvalho Chehab 		if (term == NULL)
13180c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
13190c0d06caSMauro Carvalho Chehab 
13200c0d06caSMauro Carvalho Chehab 		memcpy(term->baSourceID, &buffer[7], 1);
13210c0d06caSMauro Carvalho Chehab 
13220c0d06caSMauro Carvalho Chehab 		if (buffer[8] != 0)
13230c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[8], term->name,
1324f14d4988SLaurent Pinchart 				   sizeof(term->name));
13250c0d06caSMauro Carvalho Chehab 		else
13260c0d06caSMauro Carvalho Chehab 			sprintf(term->name, "Output %u", buffer[3]);
13270c0d06caSMauro Carvalho Chehab 
13280c0d06caSMauro Carvalho Chehab 		list_add_tail(&term->list, &dev->entities);
13290c0d06caSMauro Carvalho Chehab 		break;
13300c0d06caSMauro Carvalho Chehab 
13310c0d06caSMauro Carvalho Chehab 	case UVC_VC_SELECTOR_UNIT:
13320c0d06caSMauro Carvalho Chehab 		p = buflen >= 5 ? buffer[4] : 0;
13330c0d06caSMauro Carvalho Chehab 
13340c0d06caSMauro Carvalho Chehab 		if (buflen < 5 || buflen < 6 + p) {
13359e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1336ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d SELECTOR_UNIT error\n",
13370c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
13380c0d06caSMauro Carvalho Chehab 			return -EINVAL;
13390c0d06caSMauro Carvalho Chehab 		}
13400c0d06caSMauro Carvalho Chehab 
13410c0d06caSMauro Carvalho Chehab 		unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0);
13420c0d06caSMauro Carvalho Chehab 		if (unit == NULL)
13430c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
13440c0d06caSMauro Carvalho Chehab 
13450c0d06caSMauro Carvalho Chehab 		memcpy(unit->baSourceID, &buffer[5], p);
13460c0d06caSMauro Carvalho Chehab 
13470c0d06caSMauro Carvalho Chehab 		if (buffer[5+p] != 0)
13480c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[5+p], unit->name,
1349f14d4988SLaurent Pinchart 				   sizeof(unit->name));
13500c0d06caSMauro Carvalho Chehab 		else
13510c0d06caSMauro Carvalho Chehab 			sprintf(unit->name, "Selector %u", buffer[3]);
13520c0d06caSMauro Carvalho Chehab 
13530c0d06caSMauro Carvalho Chehab 		list_add_tail(&unit->list, &dev->entities);
13540c0d06caSMauro Carvalho Chehab 		break;
13550c0d06caSMauro Carvalho Chehab 
13560c0d06caSMauro Carvalho Chehab 	case UVC_VC_PROCESSING_UNIT:
13570c0d06caSMauro Carvalho Chehab 		n = buflen >= 8 ? buffer[7] : 0;
13580c0d06caSMauro Carvalho Chehab 		p = dev->uvc_version >= 0x0110 ? 10 : 9;
13590c0d06caSMauro Carvalho Chehab 
13600c0d06caSMauro Carvalho Chehab 		if (buflen < p + n) {
13619e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1362ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d PROCESSING_UNIT error\n",
13630c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
13640c0d06caSMauro Carvalho Chehab 			return -EINVAL;
13650c0d06caSMauro Carvalho Chehab 		}
13660c0d06caSMauro Carvalho Chehab 
13670c0d06caSMauro Carvalho Chehab 		unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n);
13680c0d06caSMauro Carvalho Chehab 		if (unit == NULL)
13690c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
13700c0d06caSMauro Carvalho Chehab 
13710c0d06caSMauro Carvalho Chehab 		memcpy(unit->baSourceID, &buffer[4], 1);
13720c0d06caSMauro Carvalho Chehab 		unit->processing.wMaxMultiplier =
13730c0d06caSMauro Carvalho Chehab 			get_unaligned_le16(&buffer[5]);
13740c0d06caSMauro Carvalho Chehab 		unit->processing.bControlSize = buffer[7];
1375f14d4988SLaurent Pinchart 		unit->processing.bmControls = (u8 *)unit + sizeof(*unit);
13760c0d06caSMauro Carvalho Chehab 		memcpy(unit->processing.bmControls, &buffer[8], n);
13770c0d06caSMauro Carvalho Chehab 		if (dev->uvc_version >= 0x0110)
13780c0d06caSMauro Carvalho Chehab 			unit->processing.bmVideoStandards = buffer[9+n];
13790c0d06caSMauro Carvalho Chehab 
13800c0d06caSMauro Carvalho Chehab 		if (buffer[8+n] != 0)
13810c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[8+n], unit->name,
1382f14d4988SLaurent Pinchart 				   sizeof(unit->name));
13830c0d06caSMauro Carvalho Chehab 		else
13840c0d06caSMauro Carvalho Chehab 			sprintf(unit->name, "Processing %u", buffer[3]);
13850c0d06caSMauro Carvalho Chehab 
13860c0d06caSMauro Carvalho Chehab 		list_add_tail(&unit->list, &dev->entities);
13870c0d06caSMauro Carvalho Chehab 		break;
13880c0d06caSMauro Carvalho Chehab 
13890c0d06caSMauro Carvalho Chehab 	case UVC_VC_EXTENSION_UNIT:
13900c0d06caSMauro Carvalho Chehab 		p = buflen >= 22 ? buffer[21] : 0;
13910c0d06caSMauro Carvalho Chehab 		n = buflen >= 24 + p ? buffer[22+p] : 0;
13920c0d06caSMauro Carvalho Chehab 
13930c0d06caSMauro Carvalho Chehab 		if (buflen < 24 + p + n) {
13949e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
1395ed4c5fa4SRicardo Ribalda 				"device %d videocontrol interface %d EXTENSION_UNIT error\n",
13960c0d06caSMauro Carvalho Chehab 				udev->devnum, alts->desc.bInterfaceNumber);
13970c0d06caSMauro Carvalho Chehab 			return -EINVAL;
13980c0d06caSMauro Carvalho Chehab 		}
13990c0d06caSMauro Carvalho Chehab 
14000c0d06caSMauro Carvalho Chehab 		unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n);
14010c0d06caSMauro Carvalho Chehab 		if (unit == NULL)
14020c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
14030c0d06caSMauro Carvalho Chehab 
1404351509c6SRicardo Ribalda 		memcpy(unit->guid, &buffer[4], 16);
14050c0d06caSMauro Carvalho Chehab 		unit->extension.bNumControls = buffer[20];
14060c0d06caSMauro Carvalho Chehab 		memcpy(unit->baSourceID, &buffer[22], p);
14070c0d06caSMauro Carvalho Chehab 		unit->extension.bControlSize = buffer[22+p];
1408f14d4988SLaurent Pinchart 		unit->extension.bmControls = (u8 *)unit + sizeof(*unit);
14090c0d06caSMauro Carvalho Chehab 		memcpy(unit->extension.bmControls, &buffer[23+p], n);
14100c0d06caSMauro Carvalho Chehab 
14110c0d06caSMauro Carvalho Chehab 		if (buffer[23+p+n] != 0)
14120c0d06caSMauro Carvalho Chehab 			usb_string(udev, buffer[23+p+n], unit->name,
1413f14d4988SLaurent Pinchart 				   sizeof(unit->name));
14140c0d06caSMauro Carvalho Chehab 		else
14150c0d06caSMauro Carvalho Chehab 			sprintf(unit->name, "Extension %u", buffer[3]);
14160c0d06caSMauro Carvalho Chehab 
14170c0d06caSMauro Carvalho Chehab 		list_add_tail(&unit->list, &dev->entities);
14180c0d06caSMauro Carvalho Chehab 		break;
14190c0d06caSMauro Carvalho Chehab 
14200c0d06caSMauro Carvalho Chehab 	default:
14219e56380aSJoe Perches 		uvc_dbg(dev, DESCR,
1422ed4c5fa4SRicardo Ribalda 			"Found an unknown CS_INTERFACE descriptor (%u)\n",
1423ed4c5fa4SRicardo Ribalda 			buffer[2]);
14240c0d06caSMauro Carvalho Chehab 		break;
14250c0d06caSMauro Carvalho Chehab 	}
14260c0d06caSMauro Carvalho Chehab 
14270c0d06caSMauro Carvalho Chehab 	return 0;
14280c0d06caSMauro Carvalho Chehab }
14290c0d06caSMauro Carvalho Chehab 
14300c0d06caSMauro Carvalho Chehab static int uvc_parse_control(struct uvc_device *dev)
14310c0d06caSMauro Carvalho Chehab {
14320c0d06caSMauro Carvalho Chehab 	struct usb_host_interface *alts = dev->intf->cur_altsetting;
14330c0d06caSMauro Carvalho Chehab 	unsigned char *buffer = alts->extra;
14340c0d06caSMauro Carvalho Chehab 	int buflen = alts->extralen;
14350c0d06caSMauro Carvalho Chehab 	int ret;
14360c0d06caSMauro Carvalho Chehab 
14370c0d06caSMauro Carvalho Chehab 	/* Parse the default alternate setting only, as the UVC specification
14380c0d06caSMauro Carvalho Chehab 	 * defines a single alternate setting, the default alternate setting
14390c0d06caSMauro Carvalho Chehab 	 * zero.
14400c0d06caSMauro Carvalho Chehab 	 */
14410c0d06caSMauro Carvalho Chehab 
14420c0d06caSMauro Carvalho Chehab 	while (buflen > 2) {
14430c0d06caSMauro Carvalho Chehab 		if (uvc_parse_vendor_control(dev, buffer, buflen) ||
14440c0d06caSMauro Carvalho Chehab 		    buffer[1] != USB_DT_CS_INTERFACE)
14450c0d06caSMauro Carvalho Chehab 			goto next_descriptor;
14460c0d06caSMauro Carvalho Chehab 
14470c0d06caSMauro Carvalho Chehab 		if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
14480c0d06caSMauro Carvalho Chehab 			return ret;
14490c0d06caSMauro Carvalho Chehab 
14500c0d06caSMauro Carvalho Chehab next_descriptor:
14510c0d06caSMauro Carvalho Chehab 		buflen -= buffer[0];
14520c0d06caSMauro Carvalho Chehab 		buffer += buffer[0];
14530c0d06caSMauro Carvalho Chehab 	}
14540c0d06caSMauro Carvalho Chehab 
14550c0d06caSMauro Carvalho Chehab 	/* Check if the optional status endpoint is present. Built-in iSight
14560c0d06caSMauro Carvalho Chehab 	 * webcams have an interrupt endpoint but spit proprietary data that
14570c0d06caSMauro Carvalho Chehab 	 * don't conform to the UVC status endpoint messages. Don't try to
14580c0d06caSMauro Carvalho Chehab 	 * handle the interrupt endpoint for those cameras.
14590c0d06caSMauro Carvalho Chehab 	 */
14600c0d06caSMauro Carvalho Chehab 	if (alts->desc.bNumEndpoints == 1 &&
14610c0d06caSMauro Carvalho Chehab 	    !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
14620c0d06caSMauro Carvalho Chehab 		struct usb_host_endpoint *ep = &alts->endpoint[0];
14630c0d06caSMauro Carvalho Chehab 		struct usb_endpoint_descriptor *desc = &ep->desc;
14640c0d06caSMauro Carvalho Chehab 
14650c0d06caSMauro Carvalho Chehab 		if (usb_endpoint_is_int_in(desc) &&
14660c0d06caSMauro Carvalho Chehab 		    le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
14670c0d06caSMauro Carvalho Chehab 		    desc->bInterval != 0) {
14689e56380aSJoe Perches 			uvc_dbg(dev, DESCR,
14699e56380aSJoe Perches 				"Found a Status endpoint (addr %02x)\n",
1470ed4c5fa4SRicardo Ribalda 				desc->bEndpointAddress);
14710c0d06caSMauro Carvalho Chehab 			dev->int_ep = ep;
14720c0d06caSMauro Carvalho Chehab 		}
14730c0d06caSMauro Carvalho Chehab 	}
14740c0d06caSMauro Carvalho Chehab 
14750c0d06caSMauro Carvalho Chehab 	return 0;
14760c0d06caSMauro Carvalho Chehab }
14770c0d06caSMauro Carvalho Chehab 
14782886477fSRicardo Ribalda /* -----------------------------------------------------------------------------
14792886477fSRicardo Ribalda  * Privacy GPIO
14802886477fSRicardo Ribalda  */
14812886477fSRicardo Ribalda 
14822886477fSRicardo Ribalda static void uvc_gpio_event(struct uvc_device *dev)
14832886477fSRicardo Ribalda {
14842886477fSRicardo Ribalda 	struct uvc_entity *unit = dev->gpio_unit;
14852886477fSRicardo Ribalda 	struct uvc_video_chain *chain;
14862886477fSRicardo Ribalda 	u8 new_val;
14872886477fSRicardo Ribalda 
14882886477fSRicardo Ribalda 	if (!unit)
14892886477fSRicardo Ribalda 		return;
14902886477fSRicardo Ribalda 
14912886477fSRicardo Ribalda 	new_val = gpiod_get_value_cansleep(unit->gpio.gpio_privacy);
14922886477fSRicardo Ribalda 
14932886477fSRicardo Ribalda 	/* GPIO entities are always on the first chain. */
14942886477fSRicardo Ribalda 	chain = list_first_entry(&dev->chains, struct uvc_video_chain, list);
14952886477fSRicardo Ribalda 	uvc_ctrl_status_event(chain, unit->controls, &new_val);
14962886477fSRicardo Ribalda }
14972886477fSRicardo Ribalda 
14982886477fSRicardo Ribalda static int uvc_gpio_get_cur(struct uvc_device *dev, struct uvc_entity *entity,
14992886477fSRicardo Ribalda 			    u8 cs, void *data, u16 size)
15002886477fSRicardo Ribalda {
15012886477fSRicardo Ribalda 	if (cs != UVC_CT_PRIVACY_CONTROL || size < 1)
15022886477fSRicardo Ribalda 		return -EINVAL;
15032886477fSRicardo Ribalda 
15042886477fSRicardo Ribalda 	*(u8 *)data = gpiod_get_value_cansleep(entity->gpio.gpio_privacy);
15052886477fSRicardo Ribalda 
15062886477fSRicardo Ribalda 	return 0;
15072886477fSRicardo Ribalda }
15082886477fSRicardo Ribalda 
15092886477fSRicardo Ribalda static int uvc_gpio_get_info(struct uvc_device *dev, struct uvc_entity *entity,
15102886477fSRicardo Ribalda 			     u8 cs, u8 *caps)
15112886477fSRicardo Ribalda {
15122886477fSRicardo Ribalda 	if (cs != UVC_CT_PRIVACY_CONTROL)
15132886477fSRicardo Ribalda 		return -EINVAL;
15142886477fSRicardo Ribalda 
15152886477fSRicardo Ribalda 	*caps = UVC_CONTROL_CAP_GET | UVC_CONTROL_CAP_AUTOUPDATE;
15162886477fSRicardo Ribalda 	return 0;
15172886477fSRicardo Ribalda }
15182886477fSRicardo Ribalda 
15192886477fSRicardo Ribalda static irqreturn_t uvc_gpio_irq(int irq, void *data)
15202886477fSRicardo Ribalda {
15212886477fSRicardo Ribalda 	struct uvc_device *dev = data;
15222886477fSRicardo Ribalda 
15232886477fSRicardo Ribalda 	uvc_gpio_event(dev);
15242886477fSRicardo Ribalda 	return IRQ_HANDLED;
15252886477fSRicardo Ribalda }
15262886477fSRicardo Ribalda 
15272886477fSRicardo Ribalda static int uvc_gpio_parse(struct uvc_device *dev)
15282886477fSRicardo Ribalda {
15292886477fSRicardo Ribalda 	struct uvc_entity *unit;
15302886477fSRicardo Ribalda 	struct gpio_desc *gpio_privacy;
15312886477fSRicardo Ribalda 	int irq;
15322886477fSRicardo Ribalda 
15332886477fSRicardo Ribalda 	gpio_privacy = devm_gpiod_get_optional(&dev->udev->dev, "privacy",
15342886477fSRicardo Ribalda 					       GPIOD_IN);
15352886477fSRicardo Ribalda 	if (IS_ERR_OR_NULL(gpio_privacy))
15362886477fSRicardo Ribalda 		return PTR_ERR_OR_ZERO(gpio_privacy);
15372886477fSRicardo Ribalda 
15382886477fSRicardo Ribalda 	unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
15392886477fSRicardo Ribalda 	if (!unit)
15402886477fSRicardo Ribalda 		return -ENOMEM;
15412886477fSRicardo Ribalda 
15422886477fSRicardo Ribalda 	irq = gpiod_to_irq(gpio_privacy);
15432886477fSRicardo Ribalda 	if (irq < 0) {
15442886477fSRicardo Ribalda 		if (irq != EPROBE_DEFER)
15452886477fSRicardo Ribalda 			dev_err(&dev->udev->dev,
15462886477fSRicardo Ribalda 				"No IRQ for privacy GPIO (%d)\n", irq);
15472886477fSRicardo Ribalda 		return irq;
15482886477fSRicardo Ribalda 	}
15492886477fSRicardo Ribalda 
15502886477fSRicardo Ribalda 	unit->gpio.gpio_privacy = gpio_privacy;
15512886477fSRicardo Ribalda 	unit->gpio.irq = irq;
15522886477fSRicardo Ribalda 	unit->gpio.bControlSize = 1;
15532886477fSRicardo Ribalda 	unit->gpio.bmControls = (u8 *)unit + sizeof(*unit);
15542886477fSRicardo Ribalda 	unit->gpio.bmControls[0] = 1;
15552886477fSRicardo Ribalda 	unit->get_cur = uvc_gpio_get_cur;
15562886477fSRicardo Ribalda 	unit->get_info = uvc_gpio_get_info;
1557063b811fSHans Verkuil 	strscpy(unit->name, "GPIO", sizeof(unit->name));
15582886477fSRicardo Ribalda 
15592886477fSRicardo Ribalda 	list_add_tail(&unit->list, &dev->entities);
15602886477fSRicardo Ribalda 
15612886477fSRicardo Ribalda 	dev->gpio_unit = unit;
15622886477fSRicardo Ribalda 
15632886477fSRicardo Ribalda 	return 0;
15642886477fSRicardo Ribalda }
15652886477fSRicardo Ribalda 
15662886477fSRicardo Ribalda static int uvc_gpio_init_irq(struct uvc_device *dev)
15672886477fSRicardo Ribalda {
15682886477fSRicardo Ribalda 	struct uvc_entity *unit = dev->gpio_unit;
15692886477fSRicardo Ribalda 
15702886477fSRicardo Ribalda 	if (!unit || unit->gpio.irq < 0)
15712886477fSRicardo Ribalda 		return 0;
15722886477fSRicardo Ribalda 
15732886477fSRicardo Ribalda 	return devm_request_threaded_irq(&dev->udev->dev, unit->gpio.irq, NULL,
15742886477fSRicardo Ribalda 					 uvc_gpio_irq,
15752886477fSRicardo Ribalda 					 IRQF_ONESHOT | IRQF_TRIGGER_FALLING |
15762886477fSRicardo Ribalda 					 IRQF_TRIGGER_RISING,
15772886477fSRicardo Ribalda 					 "uvc_privacy_gpio", dev);
15782886477fSRicardo Ribalda }
15792886477fSRicardo Ribalda 
15800c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
15810c0d06caSMauro Carvalho Chehab  * UVC device scan
15820c0d06caSMauro Carvalho Chehab  */
15830c0d06caSMauro Carvalho Chehab 
15840c0d06caSMauro Carvalho Chehab /*
15850c0d06caSMauro Carvalho Chehab  * Scan the UVC descriptors to locate a chain starting at an Output Terminal
15860c0d06caSMauro Carvalho Chehab  * and containing the following units:
15870c0d06caSMauro Carvalho Chehab  *
15880c0d06caSMauro Carvalho Chehab  * - one or more Output Terminals (USB Streaming or Display)
15890c0d06caSMauro Carvalho Chehab  * - zero or one Processing Unit
15900c0d06caSMauro Carvalho Chehab  * - zero, one or more single-input Selector Units
15910c0d06caSMauro Carvalho Chehab  * - zero or one multiple-input Selector Units, provided all inputs are
15920c0d06caSMauro Carvalho Chehab  *   connected to input terminals
15930c0d06caSMauro Carvalho Chehab  * - zero, one or mode single-input Extension Units
15940c0d06caSMauro Carvalho Chehab  * - one or more Input Terminals (Camera, External or USB Streaming)
15950c0d06caSMauro Carvalho Chehab  *
15960c0d06caSMauro Carvalho Chehab  * The terminal and units must match on of the following structures:
15970c0d06caSMauro Carvalho Chehab  *
15980c0d06caSMauro Carvalho Chehab  * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
15990c0d06caSMauro Carvalho Chehab  * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
16000c0d06caSMauro Carvalho Chehab  * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
16010c0d06caSMauro Carvalho Chehab  *
16020c0d06caSMauro Carvalho Chehab  *                 +---------+    +---------+ -> OTT_*(0)
16030c0d06caSMauro Carvalho Chehab  * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
16040c0d06caSMauro Carvalho Chehab  *                 +---------+    +---------+ -> OTT_*(n)
16050c0d06caSMauro Carvalho Chehab  *
16060c0d06caSMauro Carvalho Chehab  * The Processing Unit and Extension Units can be in any order. Additional
16070c0d06caSMauro Carvalho Chehab  * Extension Units connected to the main chain as single-unit branches are
16080c0d06caSMauro Carvalho Chehab  * also supported. Single-input Selector Units are ignored.
16090c0d06caSMauro Carvalho Chehab  */
16100c0d06caSMauro Carvalho Chehab static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
16110c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity)
16120c0d06caSMauro Carvalho Chehab {
16130c0d06caSMauro Carvalho Chehab 	switch (UVC_ENTITY_TYPE(entity)) {
16140c0d06caSMauro Carvalho Chehab 	case UVC_VC_EXTENSION_UNIT:
16159e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " <- XU %d", entity->id);
16160c0d06caSMauro Carvalho Chehab 
16170c0d06caSMauro Carvalho Chehab 		if (entity->bNrInPins != 1) {
16189e56380aSJoe Perches 			uvc_dbg(chain->dev, DESCR,
16199e56380aSJoe Perches 				"Extension unit %d has more than 1 input pin\n",
1620ed4c5fa4SRicardo Ribalda 				entity->id);
16210c0d06caSMauro Carvalho Chehab 			return -1;
16220c0d06caSMauro Carvalho Chehab 		}
16230c0d06caSMauro Carvalho Chehab 
16240c0d06caSMauro Carvalho Chehab 		break;
16250c0d06caSMauro Carvalho Chehab 
16260c0d06caSMauro Carvalho Chehab 	case UVC_VC_PROCESSING_UNIT:
16279e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " <- PU %d", entity->id);
16280c0d06caSMauro Carvalho Chehab 
16290c0d06caSMauro Carvalho Chehab 		if (chain->processing != NULL) {
16309e56380aSJoe Perches 			uvc_dbg(chain->dev, DESCR,
16319e56380aSJoe Perches 				"Found multiple Processing Units in chain\n");
16320c0d06caSMauro Carvalho Chehab 			return -1;
16330c0d06caSMauro Carvalho Chehab 		}
16340c0d06caSMauro Carvalho Chehab 
16350c0d06caSMauro Carvalho Chehab 		chain->processing = entity;
16360c0d06caSMauro Carvalho Chehab 		break;
16370c0d06caSMauro Carvalho Chehab 
16380c0d06caSMauro Carvalho Chehab 	case UVC_VC_SELECTOR_UNIT:
16399e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " <- SU %d", entity->id);
16400c0d06caSMauro Carvalho Chehab 
16410c0d06caSMauro Carvalho Chehab 		/* Single-input selector units are ignored. */
16420c0d06caSMauro Carvalho Chehab 		if (entity->bNrInPins == 1)
16430c0d06caSMauro Carvalho Chehab 			break;
16440c0d06caSMauro Carvalho Chehab 
16450c0d06caSMauro Carvalho Chehab 		if (chain->selector != NULL) {
16469e56380aSJoe Perches 			uvc_dbg(chain->dev, DESCR,
16479e56380aSJoe Perches 				"Found multiple Selector Units in chain\n");
16480c0d06caSMauro Carvalho Chehab 			return -1;
16490c0d06caSMauro Carvalho Chehab 		}
16500c0d06caSMauro Carvalho Chehab 
16510c0d06caSMauro Carvalho Chehab 		chain->selector = entity;
16520c0d06caSMauro Carvalho Chehab 		break;
16530c0d06caSMauro Carvalho Chehab 
16540c0d06caSMauro Carvalho Chehab 	case UVC_ITT_VENDOR_SPECIFIC:
16550c0d06caSMauro Carvalho Chehab 	case UVC_ITT_CAMERA:
16560c0d06caSMauro Carvalho Chehab 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
16579e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " <- IT %d\n", entity->id);
16580c0d06caSMauro Carvalho Chehab 
16590c0d06caSMauro Carvalho Chehab 		break;
16600c0d06caSMauro Carvalho Chehab 
16610c0d06caSMauro Carvalho Chehab 	case UVC_OTT_VENDOR_SPECIFIC:
16620c0d06caSMauro Carvalho Chehab 	case UVC_OTT_DISPLAY:
16630c0d06caSMauro Carvalho Chehab 	case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
16649e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " OT %d", entity->id);
16650c0d06caSMauro Carvalho Chehab 
16660c0d06caSMauro Carvalho Chehab 		break;
16670c0d06caSMauro Carvalho Chehab 
16680c0d06caSMauro Carvalho Chehab 	case UVC_TT_STREAMING:
166959e92bf6SRicardo Ribalda 		if (UVC_ENTITY_IS_ITERM(entity))
16709e56380aSJoe Perches 			uvc_dbg_cont(PROBE, " <- IT %d\n", entity->id);
167159e92bf6SRicardo Ribalda 		else
16729e56380aSJoe Perches 			uvc_dbg_cont(PROBE, " OT %d", entity->id);
16730c0d06caSMauro Carvalho Chehab 
16740c0d06caSMauro Carvalho Chehab 		break;
16750c0d06caSMauro Carvalho Chehab 
16760c0d06caSMauro Carvalho Chehab 	default:
16779e56380aSJoe Perches 		uvc_dbg(chain->dev, DESCR,
16789e56380aSJoe Perches 			"Unsupported entity type 0x%04x found in chain\n",
1679ed4c5fa4SRicardo Ribalda 			UVC_ENTITY_TYPE(entity));
16800c0d06caSMauro Carvalho Chehab 		return -1;
16810c0d06caSMauro Carvalho Chehab 	}
16820c0d06caSMauro Carvalho Chehab 
16830c0d06caSMauro Carvalho Chehab 	list_add_tail(&entity->chain, &chain->entities);
16840c0d06caSMauro Carvalho Chehab 	return 0;
16850c0d06caSMauro Carvalho Chehab }
16860c0d06caSMauro Carvalho Chehab 
16870c0d06caSMauro Carvalho Chehab static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
16880c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity, struct uvc_entity *prev)
16890c0d06caSMauro Carvalho Chehab {
16900c0d06caSMauro Carvalho Chehab 	struct uvc_entity *forward;
16910c0d06caSMauro Carvalho Chehab 	int found;
16920c0d06caSMauro Carvalho Chehab 
16930c0d06caSMauro Carvalho Chehab 	/* Forward scan */
16940c0d06caSMauro Carvalho Chehab 	forward = NULL;
16950c0d06caSMauro Carvalho Chehab 	found = 0;
16960c0d06caSMauro Carvalho Chehab 
16970c0d06caSMauro Carvalho Chehab 	while (1) {
16980c0d06caSMauro Carvalho Chehab 		forward = uvc_entity_by_reference(chain->dev, entity->id,
16990c0d06caSMauro Carvalho Chehab 			forward);
17000c0d06caSMauro Carvalho Chehab 		if (forward == NULL)
17010c0d06caSMauro Carvalho Chehab 			break;
17020c0d06caSMauro Carvalho Chehab 		if (forward == prev)
17030c0d06caSMauro Carvalho Chehab 			continue;
170468035c80SWill Deacon 		if (forward->chain.next || forward->chain.prev) {
17059e56380aSJoe Perches 			uvc_dbg(chain->dev, DESCR,
17069e56380aSJoe Perches 				"Found reference to entity %d already in chain\n",
1707ed4c5fa4SRicardo Ribalda 				forward->id);
170868035c80SWill Deacon 			return -EINVAL;
170968035c80SWill Deacon 		}
17100c0d06caSMauro Carvalho Chehab 
17110c0d06caSMauro Carvalho Chehab 		switch (UVC_ENTITY_TYPE(forward)) {
17120c0d06caSMauro Carvalho Chehab 		case UVC_VC_EXTENSION_UNIT:
17130c0d06caSMauro Carvalho Chehab 			if (forward->bNrInPins != 1) {
17149e56380aSJoe Perches 				uvc_dbg(chain->dev, DESCR,
17159e56380aSJoe Perches 					"Extension unit %d has more than 1 input pin\n",
171632934486SLaurent Pinchart 					forward->id);
17170c0d06caSMauro Carvalho Chehab 				return -EINVAL;
17180c0d06caSMauro Carvalho Chehab 			}
17190c0d06caSMauro Carvalho Chehab 
17204ca052b4SLaurent Pinchart 			/*
17214ca052b4SLaurent Pinchart 			 * Some devices reference an output terminal as the
17224ca052b4SLaurent Pinchart 			 * source of extension units. This is incorrect, as
17234ca052b4SLaurent Pinchart 			 * output terminals only have an input pin, and thus
17244ca052b4SLaurent Pinchart 			 * can't be connected to any entity in the forward
17254ca052b4SLaurent Pinchart 			 * direction. The resulting topology would cause issues
17264ca052b4SLaurent Pinchart 			 * when registering the media controller graph. To
17274ca052b4SLaurent Pinchart 			 * avoid this problem, connect the extension unit to
17284ca052b4SLaurent Pinchart 			 * the source of the output terminal instead.
17294ca052b4SLaurent Pinchart 			 */
17304ca052b4SLaurent Pinchart 			if (UVC_ENTITY_IS_OTERM(entity)) {
17314ca052b4SLaurent Pinchart 				struct uvc_entity *source;
17324ca052b4SLaurent Pinchart 
17334ca052b4SLaurent Pinchart 				source = uvc_entity_by_id(chain->dev,
17344ca052b4SLaurent Pinchart 							  entity->baSourceID[0]);
17354ca052b4SLaurent Pinchart 				if (!source) {
17364ca052b4SLaurent Pinchart 					uvc_dbg(chain->dev, DESCR,
17374ca052b4SLaurent Pinchart 						"Can't connect extension unit %u in chain\n",
17384ca052b4SLaurent Pinchart 						forward->id);
17394ca052b4SLaurent Pinchart 					break;
17404ca052b4SLaurent Pinchart 				}
17414ca052b4SLaurent Pinchart 
17424ca052b4SLaurent Pinchart 				forward->baSourceID[0] = source->id;
17434ca052b4SLaurent Pinchart 			}
17444ca052b4SLaurent Pinchart 
17450c0d06caSMauro Carvalho Chehab 			list_add_tail(&forward->chain, &chain->entities);
17460c0d06caSMauro Carvalho Chehab 			if (!found)
17479e56380aSJoe Perches 				uvc_dbg_cont(PROBE, " (->");
17480c0d06caSMauro Carvalho Chehab 
17499e56380aSJoe Perches 			uvc_dbg_cont(PROBE, " XU %d", forward->id);
17500c0d06caSMauro Carvalho Chehab 			found = 1;
17510c0d06caSMauro Carvalho Chehab 			break;
17520c0d06caSMauro Carvalho Chehab 
17530c0d06caSMauro Carvalho Chehab 		case UVC_OTT_VENDOR_SPECIFIC:
17540c0d06caSMauro Carvalho Chehab 		case UVC_OTT_DISPLAY:
17550c0d06caSMauro Carvalho Chehab 		case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
17560c0d06caSMauro Carvalho Chehab 		case UVC_TT_STREAMING:
17570c0d06caSMauro Carvalho Chehab 			if (UVC_ENTITY_IS_ITERM(forward)) {
17589e56380aSJoe Perches 				uvc_dbg(chain->dev, DESCR,
17599e56380aSJoe Perches 					"Unsupported input terminal %u\n",
1760ed4c5fa4SRicardo Ribalda 					forward->id);
17610c0d06caSMauro Carvalho Chehab 				return -EINVAL;
17620c0d06caSMauro Carvalho Chehab 			}
17630c0d06caSMauro Carvalho Chehab 
17644ca052b4SLaurent Pinchart 			if (UVC_ENTITY_IS_OTERM(entity)) {
17654ca052b4SLaurent Pinchart 				uvc_dbg(chain->dev, DESCR,
17664ca052b4SLaurent Pinchart 					"Unsupported connection between output terminals %u and %u\n",
17674ca052b4SLaurent Pinchart 					entity->id, forward->id);
17684ca052b4SLaurent Pinchart 				break;
17694ca052b4SLaurent Pinchart 			}
17704ca052b4SLaurent Pinchart 
17710c0d06caSMauro Carvalho Chehab 			list_add_tail(&forward->chain, &chain->entities);
17720c0d06caSMauro Carvalho Chehab 			if (!found)
17739e56380aSJoe Perches 				uvc_dbg_cont(PROBE, " (->");
17740c0d06caSMauro Carvalho Chehab 
17759e56380aSJoe Perches 			uvc_dbg_cont(PROBE, " OT %d", forward->id);
17760c0d06caSMauro Carvalho Chehab 			found = 1;
17770c0d06caSMauro Carvalho Chehab 			break;
17780c0d06caSMauro Carvalho Chehab 		}
17790c0d06caSMauro Carvalho Chehab 	}
17800c0d06caSMauro Carvalho Chehab 	if (found)
17819e56380aSJoe Perches 		uvc_dbg_cont(PROBE, ")");
17820c0d06caSMauro Carvalho Chehab 
17830c0d06caSMauro Carvalho Chehab 	return 0;
17840c0d06caSMauro Carvalho Chehab }
17850c0d06caSMauro Carvalho Chehab 
17860c0d06caSMauro Carvalho Chehab static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
17870c0d06caSMauro Carvalho Chehab 	struct uvc_entity **_entity)
17880c0d06caSMauro Carvalho Chehab {
17890c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity = *_entity;
17900c0d06caSMauro Carvalho Chehab 	struct uvc_entity *term;
17910c0d06caSMauro Carvalho Chehab 	int id = -EINVAL, i;
17920c0d06caSMauro Carvalho Chehab 
17930c0d06caSMauro Carvalho Chehab 	switch (UVC_ENTITY_TYPE(entity)) {
17940c0d06caSMauro Carvalho Chehab 	case UVC_VC_EXTENSION_UNIT:
17950c0d06caSMauro Carvalho Chehab 	case UVC_VC_PROCESSING_UNIT:
17960c0d06caSMauro Carvalho Chehab 		id = entity->baSourceID[0];
17970c0d06caSMauro Carvalho Chehab 		break;
17980c0d06caSMauro Carvalho Chehab 
17990c0d06caSMauro Carvalho Chehab 	case UVC_VC_SELECTOR_UNIT:
18000c0d06caSMauro Carvalho Chehab 		/* Single-input selector units are ignored. */
18010c0d06caSMauro Carvalho Chehab 		if (entity->bNrInPins == 1) {
18020c0d06caSMauro Carvalho Chehab 			id = entity->baSourceID[0];
18030c0d06caSMauro Carvalho Chehab 			break;
18040c0d06caSMauro Carvalho Chehab 		}
18050c0d06caSMauro Carvalho Chehab 
18069e56380aSJoe Perches 		uvc_dbg_cont(PROBE, " <- IT");
18070c0d06caSMauro Carvalho Chehab 
18080c0d06caSMauro Carvalho Chehab 		chain->selector = entity;
18090c0d06caSMauro Carvalho Chehab 		for (i = 0; i < entity->bNrInPins; ++i) {
18100c0d06caSMauro Carvalho Chehab 			id = entity->baSourceID[i];
18110c0d06caSMauro Carvalho Chehab 			term = uvc_entity_by_id(chain->dev, id);
18120c0d06caSMauro Carvalho Chehab 			if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
18139e56380aSJoe Perches 				uvc_dbg(chain->dev, DESCR,
1814ed4c5fa4SRicardo Ribalda 					"Selector unit %d input %d isn't connected to an input terminal\n",
1815ed4c5fa4SRicardo Ribalda 					entity->id, i);
18160c0d06caSMauro Carvalho Chehab 				return -1;
18170c0d06caSMauro Carvalho Chehab 			}
18180c0d06caSMauro Carvalho Chehab 
181968035c80SWill Deacon 			if (term->chain.next || term->chain.prev) {
18209e56380aSJoe Perches 				uvc_dbg(chain->dev, DESCR,
18219e56380aSJoe Perches 					"Found reference to entity %d already in chain\n",
182268035c80SWill Deacon 					term->id);
182368035c80SWill Deacon 				return -EINVAL;
182468035c80SWill Deacon 			}
182568035c80SWill Deacon 
18269e56380aSJoe Perches 			uvc_dbg_cont(PROBE, " %d", term->id);
18270c0d06caSMauro Carvalho Chehab 
18280c0d06caSMauro Carvalho Chehab 			list_add_tail(&term->chain, &chain->entities);
18290c0d06caSMauro Carvalho Chehab 			uvc_scan_chain_forward(chain, term, entity);
18300c0d06caSMauro Carvalho Chehab 		}
18310c0d06caSMauro Carvalho Chehab 
18329e56380aSJoe Perches 		uvc_dbg_cont(PROBE, "\n");
18330c0d06caSMauro Carvalho Chehab 
18340c0d06caSMauro Carvalho Chehab 		id = 0;
18350c0d06caSMauro Carvalho Chehab 		break;
18360c0d06caSMauro Carvalho Chehab 
18370c0d06caSMauro Carvalho Chehab 	case UVC_ITT_VENDOR_SPECIFIC:
18380c0d06caSMauro Carvalho Chehab 	case UVC_ITT_CAMERA:
18390c0d06caSMauro Carvalho Chehab 	case UVC_ITT_MEDIA_TRANSPORT_INPUT:
18400c0d06caSMauro Carvalho Chehab 	case UVC_OTT_VENDOR_SPECIFIC:
18410c0d06caSMauro Carvalho Chehab 	case UVC_OTT_DISPLAY:
18420c0d06caSMauro Carvalho Chehab 	case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
18430c0d06caSMauro Carvalho Chehab 	case UVC_TT_STREAMING:
18440c0d06caSMauro Carvalho Chehab 		id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0;
18450c0d06caSMauro Carvalho Chehab 		break;
18460c0d06caSMauro Carvalho Chehab 	}
18470c0d06caSMauro Carvalho Chehab 
18480c0d06caSMauro Carvalho Chehab 	if (id <= 0) {
18490c0d06caSMauro Carvalho Chehab 		*_entity = NULL;
18500c0d06caSMauro Carvalho Chehab 		return id;
18510c0d06caSMauro Carvalho Chehab 	}
18520c0d06caSMauro Carvalho Chehab 
18530c0d06caSMauro Carvalho Chehab 	entity = uvc_entity_by_id(chain->dev, id);
18540c0d06caSMauro Carvalho Chehab 	if (entity == NULL) {
18559e56380aSJoe Perches 		uvc_dbg(chain->dev, DESCR,
18569e56380aSJoe Perches 			"Found reference to unknown entity %d\n", id);
18570c0d06caSMauro Carvalho Chehab 		return -EINVAL;
18580c0d06caSMauro Carvalho Chehab 	}
18590c0d06caSMauro Carvalho Chehab 
18600c0d06caSMauro Carvalho Chehab 	*_entity = entity;
18610c0d06caSMauro Carvalho Chehab 	return 0;
18620c0d06caSMauro Carvalho Chehab }
18630c0d06caSMauro Carvalho Chehab 
18640c0d06caSMauro Carvalho Chehab static int uvc_scan_chain(struct uvc_video_chain *chain,
18650c0d06caSMauro Carvalho Chehab 			  struct uvc_entity *term)
18660c0d06caSMauro Carvalho Chehab {
18670c0d06caSMauro Carvalho Chehab 	struct uvc_entity *entity, *prev;
18680c0d06caSMauro Carvalho Chehab 
18699e56380aSJoe Perches 	uvc_dbg(chain->dev, PROBE, "Scanning UVC chain:");
18700c0d06caSMauro Carvalho Chehab 
18710c0d06caSMauro Carvalho Chehab 	entity = term;
18720c0d06caSMauro Carvalho Chehab 	prev = NULL;
18730c0d06caSMauro Carvalho Chehab 
18740c0d06caSMauro Carvalho Chehab 	while (entity != NULL) {
18750c0d06caSMauro Carvalho Chehab 		/* Entity must not be part of an existing chain */
18760c0d06caSMauro Carvalho Chehab 		if (entity->chain.next || entity->chain.prev) {
18779e56380aSJoe Perches 			uvc_dbg(chain->dev, DESCR,
18789e56380aSJoe Perches 				"Found reference to entity %d already in chain\n",
1879ed4c5fa4SRicardo Ribalda 				entity->id);
18800c0d06caSMauro Carvalho Chehab 			return -EINVAL;
18810c0d06caSMauro Carvalho Chehab 		}
18820c0d06caSMauro Carvalho Chehab 
18830c0d06caSMauro Carvalho Chehab 		/* Process entity */
18840c0d06caSMauro Carvalho Chehab 		if (uvc_scan_chain_entity(chain, entity) < 0)
18850c0d06caSMauro Carvalho Chehab 			return -EINVAL;
18860c0d06caSMauro Carvalho Chehab 
18870c0d06caSMauro Carvalho Chehab 		/* Forward scan */
18880c0d06caSMauro Carvalho Chehab 		if (uvc_scan_chain_forward(chain, entity, prev) < 0)
18890c0d06caSMauro Carvalho Chehab 			return -EINVAL;
18900c0d06caSMauro Carvalho Chehab 
18910c0d06caSMauro Carvalho Chehab 		/* Backward scan */
18920c0d06caSMauro Carvalho Chehab 		prev = entity;
18930c0d06caSMauro Carvalho Chehab 		if (uvc_scan_chain_backward(chain, &entity) < 0)
18940c0d06caSMauro Carvalho Chehab 			return -EINVAL;
18950c0d06caSMauro Carvalho Chehab 	}
18960c0d06caSMauro Carvalho Chehab 
18970c0d06caSMauro Carvalho Chehab 	return 0;
18980c0d06caSMauro Carvalho Chehab }
18990c0d06caSMauro Carvalho Chehab 
19000c0d06caSMauro Carvalho Chehab static unsigned int uvc_print_terms(struct list_head *terms, u16 dir,
19010c0d06caSMauro Carvalho Chehab 		char *buffer)
19020c0d06caSMauro Carvalho Chehab {
19030c0d06caSMauro Carvalho Chehab 	struct uvc_entity *term;
19040c0d06caSMauro Carvalho Chehab 	unsigned int nterms = 0;
19050c0d06caSMauro Carvalho Chehab 	char *p = buffer;
19060c0d06caSMauro Carvalho Chehab 
19070c0d06caSMauro Carvalho Chehab 	list_for_each_entry(term, terms, chain) {
19080c0d06caSMauro Carvalho Chehab 		if (!UVC_ENTITY_IS_TERM(term) ||
19090c0d06caSMauro Carvalho Chehab 		    UVC_TERM_DIRECTION(term) != dir)
19100c0d06caSMauro Carvalho Chehab 			continue;
19110c0d06caSMauro Carvalho Chehab 
19120c0d06caSMauro Carvalho Chehab 		if (nterms)
19130c0d06caSMauro Carvalho Chehab 			p += sprintf(p, ",");
19140c0d06caSMauro Carvalho Chehab 		if (++nterms >= 4) {
19150c0d06caSMauro Carvalho Chehab 			p += sprintf(p, "...");
19160c0d06caSMauro Carvalho Chehab 			break;
19170c0d06caSMauro Carvalho Chehab 		}
19180c0d06caSMauro Carvalho Chehab 		p += sprintf(p, "%u", term->id);
19190c0d06caSMauro Carvalho Chehab 	}
19200c0d06caSMauro Carvalho Chehab 
19210c0d06caSMauro Carvalho Chehab 	return p - buffer;
19220c0d06caSMauro Carvalho Chehab }
19230c0d06caSMauro Carvalho Chehab 
19240c0d06caSMauro Carvalho Chehab static const char *uvc_print_chain(struct uvc_video_chain *chain)
19250c0d06caSMauro Carvalho Chehab {
19260c0d06caSMauro Carvalho Chehab 	static char buffer[43];
19270c0d06caSMauro Carvalho Chehab 	char *p = buffer;
19280c0d06caSMauro Carvalho Chehab 
19290c0d06caSMauro Carvalho Chehab 	p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p);
19300c0d06caSMauro Carvalho Chehab 	p += sprintf(p, " -> ");
19310c0d06caSMauro Carvalho Chehab 	uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p);
19320c0d06caSMauro Carvalho Chehab 
19330c0d06caSMauro Carvalho Chehab 	return buffer;
19340c0d06caSMauro Carvalho Chehab }
19350c0d06caSMauro Carvalho Chehab 
1936e950267aSHenrik Ingo static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev)
1937e950267aSHenrik Ingo {
1938e950267aSHenrik Ingo 	struct uvc_video_chain *chain;
1939e950267aSHenrik Ingo 
1940e950267aSHenrik Ingo 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1941e950267aSHenrik Ingo 	if (chain == NULL)
1942e950267aSHenrik Ingo 		return NULL;
1943e950267aSHenrik Ingo 
1944e950267aSHenrik Ingo 	INIT_LIST_HEAD(&chain->entities);
1945e950267aSHenrik Ingo 	mutex_init(&chain->ctrl_mutex);
1946e950267aSHenrik Ingo 	chain->dev = dev;
1947e950267aSHenrik Ingo 	v4l2_prio_init(&chain->prio);
1948e950267aSHenrik Ingo 
1949e950267aSHenrik Ingo 	return chain;
1950e950267aSHenrik Ingo }
1951e950267aSHenrik Ingo 
1952e950267aSHenrik Ingo /*
1953e950267aSHenrik Ingo  * Fallback heuristic for devices that don't connect units and terminals in a
1954e950267aSHenrik Ingo  * valid chain.
1955e950267aSHenrik Ingo  *
1956e950267aSHenrik Ingo  * Some devices have invalid baSourceID references, causing uvc_scan_chain()
1957e950267aSHenrik Ingo  * to fail, but if we just take the entities we can find and put them together
1958e950267aSHenrik Ingo  * in the most sensible chain we can think of, turns out they do work anyway.
1959e950267aSHenrik Ingo  * Note: This heuristic assumes there is a single chain.
1960e950267aSHenrik Ingo  *
1961e950267aSHenrik Ingo  * At the time of writing, devices known to have such a broken chain are
1962e950267aSHenrik Ingo  *  - Acer Integrated Camera (5986:055a)
1963e950267aSHenrik Ingo  *  - Realtek rtl157a7 (0bda:57a7)
1964e950267aSHenrik Ingo  */
1965e950267aSHenrik Ingo static int uvc_scan_fallback(struct uvc_device *dev)
1966e950267aSHenrik Ingo {
1967e950267aSHenrik Ingo 	struct uvc_video_chain *chain;
1968e950267aSHenrik Ingo 	struct uvc_entity *iterm = NULL;
1969e950267aSHenrik Ingo 	struct uvc_entity *oterm = NULL;
1970e950267aSHenrik Ingo 	struct uvc_entity *entity;
1971e950267aSHenrik Ingo 	struct uvc_entity *prev;
1972e950267aSHenrik Ingo 
1973e950267aSHenrik Ingo 	/*
1974e950267aSHenrik Ingo 	 * Start by locating the input and output terminals. We only support
1975e950267aSHenrik Ingo 	 * devices with exactly one of each for now.
1976e950267aSHenrik Ingo 	 */
1977e950267aSHenrik Ingo 	list_for_each_entry(entity, &dev->entities, list) {
1978e950267aSHenrik Ingo 		if (UVC_ENTITY_IS_ITERM(entity)) {
1979e950267aSHenrik Ingo 			if (iterm)
1980e950267aSHenrik Ingo 				return -EINVAL;
1981e950267aSHenrik Ingo 			iterm = entity;
1982e950267aSHenrik Ingo 		}
1983e950267aSHenrik Ingo 
1984e950267aSHenrik Ingo 		if (UVC_ENTITY_IS_OTERM(entity)) {
1985e950267aSHenrik Ingo 			if (oterm)
1986e950267aSHenrik Ingo 				return -EINVAL;
1987e950267aSHenrik Ingo 			oterm = entity;
1988e950267aSHenrik Ingo 		}
1989e950267aSHenrik Ingo 	}
1990e950267aSHenrik Ingo 
1991e950267aSHenrik Ingo 	if (iterm == NULL || oterm == NULL)
1992e950267aSHenrik Ingo 		return -EINVAL;
1993e950267aSHenrik Ingo 
1994e950267aSHenrik Ingo 	/* Allocate the chain and fill it. */
1995e950267aSHenrik Ingo 	chain = uvc_alloc_chain(dev);
1996e950267aSHenrik Ingo 	if (chain == NULL)
1997e950267aSHenrik Ingo 		return -ENOMEM;
1998e950267aSHenrik Ingo 
1999e950267aSHenrik Ingo 	if (uvc_scan_chain_entity(chain, oterm) < 0)
2000e950267aSHenrik Ingo 		goto error;
2001e950267aSHenrik Ingo 
2002e950267aSHenrik Ingo 	prev = oterm;
2003e950267aSHenrik Ingo 
2004e950267aSHenrik Ingo 	/*
2005e950267aSHenrik Ingo 	 * Add all Processing and Extension Units with two pads. The order
2006e950267aSHenrik Ingo 	 * doesn't matter much, use reverse list traversal to connect units in
2007e950267aSHenrik Ingo 	 * UVC descriptor order as we build the chain from output to input. This
2008e950267aSHenrik Ingo 	 * leads to units appearing in the order meant by the manufacturer for
2009e950267aSHenrik Ingo 	 * the cameras known to require this heuristic.
2010e950267aSHenrik Ingo 	 */
2011e950267aSHenrik Ingo 	list_for_each_entry_reverse(entity, &dev->entities, list) {
2012e950267aSHenrik Ingo 		if (entity->type != UVC_VC_PROCESSING_UNIT &&
2013e950267aSHenrik Ingo 		    entity->type != UVC_VC_EXTENSION_UNIT)
2014e950267aSHenrik Ingo 			continue;
2015e950267aSHenrik Ingo 
2016e950267aSHenrik Ingo 		if (entity->num_pads != 2)
2017e950267aSHenrik Ingo 			continue;
2018e950267aSHenrik Ingo 
2019e950267aSHenrik Ingo 		if (uvc_scan_chain_entity(chain, entity) < 0)
2020e950267aSHenrik Ingo 			goto error;
2021e950267aSHenrik Ingo 
2022e950267aSHenrik Ingo 		prev->baSourceID[0] = entity->id;
2023e950267aSHenrik Ingo 		prev = entity;
2024e950267aSHenrik Ingo 	}
2025e950267aSHenrik Ingo 
2026e950267aSHenrik Ingo 	if (uvc_scan_chain_entity(chain, iterm) < 0)
2027e950267aSHenrik Ingo 		goto error;
2028e950267aSHenrik Ingo 
2029e950267aSHenrik Ingo 	prev->baSourceID[0] = iterm->id;
2030e950267aSHenrik Ingo 
2031e950267aSHenrik Ingo 	list_add_tail(&chain->list, &dev->chains);
2032e950267aSHenrik Ingo 
20339e56380aSJoe Perches 	uvc_dbg(dev, PROBE, "Found a video chain by fallback heuristic (%s)\n",
2034e950267aSHenrik Ingo 		uvc_print_chain(chain));
2035e950267aSHenrik Ingo 
2036e950267aSHenrik Ingo 	return 0;
2037e950267aSHenrik Ingo 
2038e950267aSHenrik Ingo error:
2039e950267aSHenrik Ingo 	kfree(chain);
2040e950267aSHenrik Ingo 	return -EINVAL;
2041e950267aSHenrik Ingo }
2042e950267aSHenrik Ingo 
20430c0d06caSMauro Carvalho Chehab /*
20440c0d06caSMauro Carvalho Chehab  * Scan the device for video chains and register video devices.
20450c0d06caSMauro Carvalho Chehab  *
20460c0d06caSMauro Carvalho Chehab  * Chains are scanned starting at their output terminals and walked backwards.
20470c0d06caSMauro Carvalho Chehab  */
20480c0d06caSMauro Carvalho Chehab static int uvc_scan_device(struct uvc_device *dev)
20490c0d06caSMauro Carvalho Chehab {
20500c0d06caSMauro Carvalho Chehab 	struct uvc_video_chain *chain;
20510c0d06caSMauro Carvalho Chehab 	struct uvc_entity *term;
20520c0d06caSMauro Carvalho Chehab 
20530c0d06caSMauro Carvalho Chehab 	list_for_each_entry(term, &dev->entities, list) {
20540c0d06caSMauro Carvalho Chehab 		if (!UVC_ENTITY_IS_OTERM(term))
20550c0d06caSMauro Carvalho Chehab 			continue;
20560c0d06caSMauro Carvalho Chehab 
20570c0d06caSMauro Carvalho Chehab 		/* If the terminal is already included in a chain, skip it.
20580c0d06caSMauro Carvalho Chehab 		 * This can happen for chains that have multiple output
20590c0d06caSMauro Carvalho Chehab 		 * terminals, where all output terminals beside the first one
20600c0d06caSMauro Carvalho Chehab 		 * will be inserted in the chain in forward scans.
20610c0d06caSMauro Carvalho Chehab 		 */
20620c0d06caSMauro Carvalho Chehab 		if (term->chain.next || term->chain.prev)
20630c0d06caSMauro Carvalho Chehab 			continue;
20640c0d06caSMauro Carvalho Chehab 
2065e950267aSHenrik Ingo 		chain = uvc_alloc_chain(dev);
20660c0d06caSMauro Carvalho Chehab 		if (chain == NULL)
20670c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
20680c0d06caSMauro Carvalho Chehab 
20698be8ec6eSLaurent Pinchart 		term->flags |= UVC_ENTITY_FLAG_DEFAULT;
20708be8ec6eSLaurent Pinchart 
20710c0d06caSMauro Carvalho Chehab 		if (uvc_scan_chain(chain, term) < 0) {
20720c0d06caSMauro Carvalho Chehab 			kfree(chain);
20730c0d06caSMauro Carvalho Chehab 			continue;
20740c0d06caSMauro Carvalho Chehab 		}
20750c0d06caSMauro Carvalho Chehab 
20769e56380aSJoe Perches 		uvc_dbg(dev, PROBE, "Found a valid video chain (%s)\n",
20770c0d06caSMauro Carvalho Chehab 			uvc_print_chain(chain));
20780c0d06caSMauro Carvalho Chehab 
20790c0d06caSMauro Carvalho Chehab 		list_add_tail(&chain->list, &dev->chains);
20800c0d06caSMauro Carvalho Chehab 	}
20810c0d06caSMauro Carvalho Chehab 
2082e950267aSHenrik Ingo 	if (list_empty(&dev->chains))
2083e950267aSHenrik Ingo 		uvc_scan_fallback(dev);
2084e950267aSHenrik Ingo 
20850c0d06caSMauro Carvalho Chehab 	if (list_empty(&dev->chains)) {
208669df0954SRicardo Ribalda 		dev_info(&dev->udev->dev, "No valid video chain found.\n");
20870c0d06caSMauro Carvalho Chehab 		return -1;
20880c0d06caSMauro Carvalho Chehab 	}
20890c0d06caSMauro Carvalho Chehab 
20902886477fSRicardo Ribalda 	/* Add GPIO entity to the first chain. */
20912886477fSRicardo Ribalda 	if (dev->gpio_unit) {
20922886477fSRicardo Ribalda 		chain = list_first_entry(&dev->chains,
20932886477fSRicardo Ribalda 					 struct uvc_video_chain, list);
20942886477fSRicardo Ribalda 		list_add_tail(&dev->gpio_unit->chain, &chain->entities);
20952886477fSRicardo Ribalda 	}
20962886477fSRicardo Ribalda 
20970c0d06caSMauro Carvalho Chehab 	return 0;
20980c0d06caSMauro Carvalho Chehab }
20990c0d06caSMauro Carvalho Chehab 
21000c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
21010c0d06caSMauro Carvalho Chehab  * Video device registration and unregistration
21020c0d06caSMauro Carvalho Chehab  */
21030c0d06caSMauro Carvalho Chehab 
21040c0d06caSMauro Carvalho Chehab /*
21050c0d06caSMauro Carvalho Chehab  * Delete the UVC device.
21060c0d06caSMauro Carvalho Chehab  *
21070c0d06caSMauro Carvalho Chehab  * Called by the kernel when the last reference to the uvc_device structure
21080c0d06caSMauro Carvalho Chehab  * is released.
21090c0d06caSMauro Carvalho Chehab  *
21100c0d06caSMauro Carvalho Chehab  * As this function is called after or during disconnect(), all URBs have
2111ece41454SKieran Bingham  * already been cancelled by the USB core. There is no need to kill the
21120c0d06caSMauro Carvalho Chehab  * interrupt URB manually.
21130c0d06caSMauro Carvalho Chehab  */
21149d15cd95SGuennadi Liakhovetski static void uvc_delete(struct kref *kref)
21150c0d06caSMauro Carvalho Chehab {
21169d15cd95SGuennadi Liakhovetski 	struct uvc_device *dev = container_of(kref, struct uvc_device, ref);
21170c0d06caSMauro Carvalho Chehab 	struct list_head *p, *n;
21180c0d06caSMauro Carvalho Chehab 
21190c0d06caSMauro Carvalho Chehab 	uvc_status_cleanup(dev);
21200c0d06caSMauro Carvalho Chehab 	uvc_ctrl_cleanup_device(dev);
21210c0d06caSMauro Carvalho Chehab 
21222228d80dSTakashi Iwai 	usb_put_intf(dev->intf);
21232228d80dSTakashi Iwai 	usb_put_dev(dev->udev);
21242228d80dSTakashi Iwai 
21250c0d06caSMauro Carvalho Chehab #ifdef CONFIG_MEDIA_CONTROLLER
21269832e155SJavier Martinez Canillas 	media_device_cleanup(&dev->mdev);
21270c0d06caSMauro Carvalho Chehab #endif
21280c0d06caSMauro Carvalho Chehab 
21290c0d06caSMauro Carvalho Chehab 	list_for_each_safe(p, n, &dev->chains) {
21300c0d06caSMauro Carvalho Chehab 		struct uvc_video_chain *chain;
21310c0d06caSMauro Carvalho Chehab 		chain = list_entry(p, struct uvc_video_chain, list);
21320c0d06caSMauro Carvalho Chehab 		kfree(chain);
21330c0d06caSMauro Carvalho Chehab 	}
21340c0d06caSMauro Carvalho Chehab 
21350c0d06caSMauro Carvalho Chehab 	list_for_each_safe(p, n, &dev->entities) {
21360c0d06caSMauro Carvalho Chehab 		struct uvc_entity *entity;
21370c0d06caSMauro Carvalho Chehab 		entity = list_entry(p, struct uvc_entity, list);
21380c0d06caSMauro Carvalho Chehab #ifdef CONFIG_MEDIA_CONTROLLER
21390c0d06caSMauro Carvalho Chehab 		uvc_mc_cleanup_entity(entity);
21400c0d06caSMauro Carvalho Chehab #endif
21410c0d06caSMauro Carvalho Chehab 		kfree(entity);
21420c0d06caSMauro Carvalho Chehab 	}
21430c0d06caSMauro Carvalho Chehab 
21440c0d06caSMauro Carvalho Chehab 	list_for_each_safe(p, n, &dev->streams) {
21450c0d06caSMauro Carvalho Chehab 		struct uvc_streaming *streaming;
21460c0d06caSMauro Carvalho Chehab 		streaming = list_entry(p, struct uvc_streaming, list);
21470c0d06caSMauro Carvalho Chehab 		usb_driver_release_interface(&uvc_driver.driver,
21480c0d06caSMauro Carvalho Chehab 			streaming->intf);
2149ece41454SKieran Bingham 		uvc_stream_delete(streaming);
21500c0d06caSMauro Carvalho Chehab 	}
21510c0d06caSMauro Carvalho Chehab 
21520c0d06caSMauro Carvalho Chehab 	kfree(dev);
21530c0d06caSMauro Carvalho Chehab }
21540c0d06caSMauro Carvalho Chehab 
21550c0d06caSMauro Carvalho Chehab static void uvc_release(struct video_device *vdev)
21560c0d06caSMauro Carvalho Chehab {
21570c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream = video_get_drvdata(vdev);
21580c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = stream->dev;
21590c0d06caSMauro Carvalho Chehab 
21609d15cd95SGuennadi Liakhovetski 	kref_put(&dev->ref, uvc_delete);
21610c0d06caSMauro Carvalho Chehab }
21620c0d06caSMauro Carvalho Chehab 
21630c0d06caSMauro Carvalho Chehab /*
21640c0d06caSMauro Carvalho Chehab  * Unregister the video devices.
21650c0d06caSMauro Carvalho Chehab  */
21660c0d06caSMauro Carvalho Chehab static void uvc_unregister_video(struct uvc_device *dev)
21670c0d06caSMauro Carvalho Chehab {
21680c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream;
21690c0d06caSMauro Carvalho Chehab 
21700c0d06caSMauro Carvalho Chehab 	list_for_each_entry(stream, &dev->streams, list) {
2171d8da7513SHans Verkuil 		if (!video_is_registered(&stream->vdev))
21720c0d06caSMauro Carvalho Chehab 			continue;
21730c0d06caSMauro Carvalho Chehab 
2174d8da7513SHans Verkuil 		video_unregister_device(&stream->vdev);
2175088ead25SGuennadi Liakhovetski 		video_unregister_device(&stream->meta.vdev);
21760c0d06caSMauro Carvalho Chehab 
21770c0d06caSMauro Carvalho Chehab 		uvc_debugfs_cleanup_stream(stream);
21780c0d06caSMauro Carvalho Chehab 	}
217910e1fdb9SDaniel Axtens 
218010e1fdb9SDaniel Axtens 	uvc_status_unregister(dev);
218110e1fdb9SDaniel Axtens 
218210e1fdb9SDaniel Axtens 	if (dev->vdev.dev)
218310e1fdb9SDaniel Axtens 		v4l2_device_unregister(&dev->vdev);
218410e1fdb9SDaniel Axtens #ifdef CONFIG_MEDIA_CONTROLLER
218510e1fdb9SDaniel Axtens 	if (media_devnode_is_registered(dev->mdev.devnode))
218610e1fdb9SDaniel Axtens 		media_device_unregister(&dev->mdev);
218710e1fdb9SDaniel Axtens #endif
21880c0d06caSMauro Carvalho Chehab }
21890c0d06caSMauro Carvalho Chehab 
219031a96f4cSLaurent Pinchart int uvc_register_video_device(struct uvc_device *dev,
219131a96f4cSLaurent Pinchart 			      struct uvc_streaming *stream,
219231a96f4cSLaurent Pinchart 			      struct video_device *vdev,
219331a96f4cSLaurent Pinchart 			      struct uvc_video_queue *queue,
219431a96f4cSLaurent Pinchart 			      enum v4l2_buf_type type,
219531a96f4cSLaurent Pinchart 			      const struct v4l2_file_operations *fops,
219631a96f4cSLaurent Pinchart 			      const struct v4l2_ioctl_ops *ioctl_ops)
21970c0d06caSMauro Carvalho Chehab {
21980c0d06caSMauro Carvalho Chehab 	int ret;
21990c0d06caSMauro Carvalho Chehab 
2200b83bba24SLaurent Pinchart 	/* Initialize the video buffers queue. */
220131a96f4cSLaurent Pinchart 	ret = uvc_queue_init(queue, type, !uvc_no_drop_param);
2202b83bba24SLaurent Pinchart 	if (ret)
2203b83bba24SLaurent Pinchart 		return ret;
2204b83bba24SLaurent Pinchart 
22050c0d06caSMauro Carvalho Chehab 	/* Register the device with V4L. */
22060c0d06caSMauro Carvalho Chehab 
220731a96f4cSLaurent Pinchart 	/*
220831a96f4cSLaurent Pinchart 	 * We already hold a reference to dev->udev. The video device will be
22090c0d06caSMauro Carvalho Chehab 	 * unregistered before the reference is released, so we don't need to
22100c0d06caSMauro Carvalho Chehab 	 * get another one.
22110c0d06caSMauro Carvalho Chehab 	 */
22120c0d06caSMauro Carvalho Chehab 	vdev->v4l2_dev = &dev->vdev;
221331a96f4cSLaurent Pinchart 	vdev->fops = fops;
221431a96f4cSLaurent Pinchart 	vdev->ioctl_ops = ioctl_ops;
22150c0d06caSMauro Carvalho Chehab 	vdev->release = uvc_release;
22160550513cSLaurent Pinchart 	vdev->prio = &stream->chain->prio;
221731a96f4cSLaurent Pinchart 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2218954f340fSHans Verkuil 		vdev->vfl_dir = VFL_DIR_TX;
221931a96f4cSLaurent Pinchart 	else
222031a96f4cSLaurent Pinchart 		vdev->vfl_dir = VFL_DIR_RX;
222194c53e26SLaurent Pinchart 
222294c53e26SLaurent Pinchart 	switch (type) {
222394c53e26SLaurent Pinchart 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
222494c53e26SLaurent Pinchart 	default:
222594c53e26SLaurent Pinchart 		vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
222694c53e26SLaurent Pinchart 		break;
222794c53e26SLaurent Pinchart 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
222894c53e26SLaurent Pinchart 		vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
222994c53e26SLaurent Pinchart 		break;
2230088ead25SGuennadi Liakhovetski 	case V4L2_BUF_TYPE_META_CAPTURE:
2231088ead25SGuennadi Liakhovetski 		vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
2232088ead25SGuennadi Liakhovetski 		break;
223394c53e26SLaurent Pinchart 	}
223494c53e26SLaurent Pinchart 
2235f66dcb32SRicardo Ribalda 	strscpy(vdev->name, dev->name, sizeof(vdev->name));
22360c0d06caSMauro Carvalho Chehab 
223731a96f4cSLaurent Pinchart 	/*
223831a96f4cSLaurent Pinchart 	 * Set the driver data before calling video_register_device, otherwise
223931a96f4cSLaurent Pinchart 	 * the file open() handler might race us.
22400c0d06caSMauro Carvalho Chehab 	 */
22410c0d06caSMauro Carvalho Chehab 	video_set_drvdata(vdev, stream);
22420c0d06caSMauro Carvalho Chehab 
22437fbbbc78SHans Verkuil 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
22440c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
224569df0954SRicardo Ribalda 		dev_err(&stream->intf->dev,
224669df0954SRicardo Ribalda 			"Failed to register %s device (%d).\n",
224731a96f4cSLaurent Pinchart 			v4l2_type_names[type], ret);
224831a96f4cSLaurent Pinchart 		return ret;
224931a96f4cSLaurent Pinchart 	}
225031a96f4cSLaurent Pinchart 
225131a96f4cSLaurent Pinchart 	kref_get(&dev->ref);
225231a96f4cSLaurent Pinchart 	return 0;
225331a96f4cSLaurent Pinchart }
225431a96f4cSLaurent Pinchart 
225531a96f4cSLaurent Pinchart static int uvc_register_video(struct uvc_device *dev,
225631a96f4cSLaurent Pinchart 		struct uvc_streaming *stream)
225731a96f4cSLaurent Pinchart {
225831a96f4cSLaurent Pinchart 	int ret;
225931a96f4cSLaurent Pinchart 
226031a96f4cSLaurent Pinchart 	/* Initialize the streaming interface with default parameters. */
226131a96f4cSLaurent Pinchart 	ret = uvc_video_init(stream);
226231a96f4cSLaurent Pinchart 	if (ret < 0) {
226369df0954SRicardo Ribalda 		dev_err(&stream->intf->dev,
226469df0954SRicardo Ribalda 			"Failed to initialize the device (%d).\n", ret);
22650c0d06caSMauro Carvalho Chehab 		return ret;
22660c0d06caSMauro Carvalho Chehab 	}
22670c0d06caSMauro Carvalho Chehab 
2268f887e99aSLaurent Pinchart 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2269088ead25SGuennadi Liakhovetski 		stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE
2270088ead25SGuennadi Liakhovetski 			| V4L2_CAP_META_CAPTURE;
2271f887e99aSLaurent Pinchart 	else
2272f887e99aSLaurent Pinchart 		stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT;
2273f887e99aSLaurent Pinchart 
227431a96f4cSLaurent Pinchart 	uvc_debugfs_init_stream(stream);
227531a96f4cSLaurent Pinchart 
227631a96f4cSLaurent Pinchart 	/* Register the device with V4L. */
227731a96f4cSLaurent Pinchart 	return uvc_register_video_device(dev, stream, &stream->vdev,
227831a96f4cSLaurent Pinchart 					 &stream->queue, stream->type,
227931a96f4cSLaurent Pinchart 					 &uvc_fops, &uvc_ioctl_ops);
22800c0d06caSMauro Carvalho Chehab }
22810c0d06caSMauro Carvalho Chehab 
22820c0d06caSMauro Carvalho Chehab /*
22830c0d06caSMauro Carvalho Chehab  * Register all video devices in all chains.
22840c0d06caSMauro Carvalho Chehab  */
22850c0d06caSMauro Carvalho Chehab static int uvc_register_terms(struct uvc_device *dev,
22860c0d06caSMauro Carvalho Chehab 	struct uvc_video_chain *chain)
22870c0d06caSMauro Carvalho Chehab {
22880c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream;
22890c0d06caSMauro Carvalho Chehab 	struct uvc_entity *term;
22900c0d06caSMauro Carvalho Chehab 	int ret;
22910c0d06caSMauro Carvalho Chehab 
22920c0d06caSMauro Carvalho Chehab 	list_for_each_entry(term, &chain->entities, chain) {
22930c0d06caSMauro Carvalho Chehab 		if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
22940c0d06caSMauro Carvalho Chehab 			continue;
22950c0d06caSMauro Carvalho Chehab 
22960c0d06caSMauro Carvalho Chehab 		stream = uvc_stream_by_id(dev, term->id);
22970c0d06caSMauro Carvalho Chehab 		if (stream == NULL) {
229869df0954SRicardo Ribalda 			dev_info(&dev->udev->dev,
229969df0954SRicardo Ribalda 				 "No streaming interface found for terminal %u.",
230069df0954SRicardo Ribalda 				 term->id);
23010c0d06caSMauro Carvalho Chehab 			continue;
23020c0d06caSMauro Carvalho Chehab 		}
23030c0d06caSMauro Carvalho Chehab 
23040c0d06caSMauro Carvalho Chehab 		stream->chain = chain;
23050c0d06caSMauro Carvalho Chehab 		ret = uvc_register_video(dev, stream);
23060c0d06caSMauro Carvalho Chehab 		if (ret < 0)
23070c0d06caSMauro Carvalho Chehab 			return ret;
23080c0d06caSMauro Carvalho Chehab 
2309088ead25SGuennadi Liakhovetski 		/* Register a metadata node, but ignore a possible failure,
2310088ead25SGuennadi Liakhovetski 		 * complete registration of video nodes anyway.
2311088ead25SGuennadi Liakhovetski 		 */
2312088ead25SGuennadi Liakhovetski 		uvc_meta_register(stream);
2313088ead25SGuennadi Liakhovetski 
2314d8da7513SHans Verkuil 		term->vdev = &stream->vdev;
23150c0d06caSMauro Carvalho Chehab 	}
23160c0d06caSMauro Carvalho Chehab 
23170c0d06caSMauro Carvalho Chehab 	return 0;
23180c0d06caSMauro Carvalho Chehab }
23190c0d06caSMauro Carvalho Chehab 
23200c0d06caSMauro Carvalho Chehab static int uvc_register_chains(struct uvc_device *dev)
23210c0d06caSMauro Carvalho Chehab {
23220c0d06caSMauro Carvalho Chehab 	struct uvc_video_chain *chain;
23230c0d06caSMauro Carvalho Chehab 	int ret;
23240c0d06caSMauro Carvalho Chehab 
23250c0d06caSMauro Carvalho Chehab 	list_for_each_entry(chain, &dev->chains, list) {
23260c0d06caSMauro Carvalho Chehab 		ret = uvc_register_terms(dev, chain);
23270c0d06caSMauro Carvalho Chehab 		if (ret < 0)
23280c0d06caSMauro Carvalho Chehab 			return ret;
23290c0d06caSMauro Carvalho Chehab 
23300c0d06caSMauro Carvalho Chehab #ifdef CONFIG_MEDIA_CONTROLLER
23310c0d06caSMauro Carvalho Chehab 		ret = uvc_mc_register_entities(chain);
23326689df06SColin Ian King 		if (ret < 0)
233369df0954SRicardo Ribalda 			dev_info(&dev->udev->dev,
23346689df06SColin Ian King 				 "Failed to register entities (%d).\n", ret);
23350c0d06caSMauro Carvalho Chehab #endif
23360c0d06caSMauro Carvalho Chehab 	}
23370c0d06caSMauro Carvalho Chehab 
23380c0d06caSMauro Carvalho Chehab 	return 0;
23390c0d06caSMauro Carvalho Chehab }
23400c0d06caSMauro Carvalho Chehab 
23410c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
23420c0d06caSMauro Carvalho Chehab  * USB probe, disconnect, suspend and resume
23430c0d06caSMauro Carvalho Chehab  */
23440c0d06caSMauro Carvalho Chehab 
23453a03284dSLaurent Pinchart static const struct uvc_device_info uvc_quirk_none = { 0 };
23463bc85817SGuennadi Liakhovetski 
23470c0d06caSMauro Carvalho Chehab static int uvc_probe(struct usb_interface *intf,
23480c0d06caSMauro Carvalho Chehab 		     const struct usb_device_id *id)
23490c0d06caSMauro Carvalho Chehab {
23500c0d06caSMauro Carvalho Chehab 	struct usb_device *udev = interface_to_usbdev(intf);
23510c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev;
23523bc85817SGuennadi Liakhovetski 	const struct uvc_device_info *info =
23533bc85817SGuennadi Liakhovetski 		(const struct uvc_device_info *)id->driver_info;
2354e7b09f18SPeter Boström 	int function;
23550c0d06caSMauro Carvalho Chehab 	int ret;
23560c0d06caSMauro Carvalho Chehab 
23570c0d06caSMauro Carvalho Chehab 	/* Allocate memory for the device and initialize it. */
2358f14d4988SLaurent Pinchart 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2359f14d4988SLaurent Pinchart 	if (dev == NULL)
23600c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
23610c0d06caSMauro Carvalho Chehab 
23620c0d06caSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->entities);
23630c0d06caSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->chains);
23640c0d06caSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->streams);
23659d15cd95SGuennadi Liakhovetski 	kref_init(&dev->ref);
23660c0d06caSMauro Carvalho Chehab 	atomic_set(&dev->nmappings, 0);
236717706f56SLaurent Pinchart 	mutex_init(&dev->lock);
23680c0d06caSMauro Carvalho Chehab 
23690c0d06caSMauro Carvalho Chehab 	dev->udev = usb_get_dev(udev);
23700c0d06caSMauro Carvalho Chehab 	dev->intf = usb_get_intf(intf);
23710c0d06caSMauro Carvalho Chehab 	dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
23723a03284dSLaurent Pinchart 	dev->info = info ? info : &uvc_quirk_none;
23733a03284dSLaurent Pinchart 	dev->quirks = uvc_quirks_param == -1
23743a03284dSLaurent Pinchart 		    ? dev->info->quirks : uvc_quirks_param;
23750c0d06caSMauro Carvalho Chehab 
2376ed4c5fa4SRicardo Ribalda 	if (id->idVendor && id->idProduct)
23779e56380aSJoe Perches 		uvc_dbg(dev, PROBE, "Probing known UVC device %s (%04x:%04x)\n",
2378ed4c5fa4SRicardo Ribalda 			udev->devpath, id->idVendor, id->idProduct);
2379ed4c5fa4SRicardo Ribalda 	else
23809e56380aSJoe Perches 		uvc_dbg(dev, PROBE, "Probing generic UVC device %s\n",
23819e56380aSJoe Perches 			udev->devpath);
2382ed4c5fa4SRicardo Ribalda 
23830c0d06caSMauro Carvalho Chehab 	if (udev->product != NULL)
2384c0decac1SMauro Carvalho Chehab 		strscpy(dev->name, udev->product, sizeof(dev->name));
23850c0d06caSMauro Carvalho Chehab 	else
2386f14d4988SLaurent Pinchart 		snprintf(dev->name, sizeof(dev->name),
23870c0d06caSMauro Carvalho Chehab 			 "UVC Camera (%04x:%04x)",
23880c0d06caSMauro Carvalho Chehab 			 le16_to_cpu(udev->descriptor.idVendor),
23890c0d06caSMauro Carvalho Chehab 			 le16_to_cpu(udev->descriptor.idProduct));
23900c0d06caSMauro Carvalho Chehab 
2391e7b09f18SPeter Boström 	/*
2392e7b09f18SPeter Boström 	 * Add iFunction or iInterface to names when available as additional
2393e7b09f18SPeter Boström 	 * distinguishers between interfaces. iFunction is prioritized over
2394e7b09f18SPeter Boström 	 * iInterface which matches Windows behavior at the point of writing.
2395e7b09f18SPeter Boström 	 */
2396e7b09f18SPeter Boström 	if (intf->intf_assoc && intf->intf_assoc->iFunction != 0)
2397e7b09f18SPeter Boström 		function = intf->intf_assoc->iFunction;
2398e7b09f18SPeter Boström 	else
2399e7b09f18SPeter Boström 		function = intf->cur_altsetting->desc.iInterface;
2400e7b09f18SPeter Boström 	if (function != 0) {
2401e7b09f18SPeter Boström 		size_t len;
2402e7b09f18SPeter Boström 
2403e7b09f18SPeter Boström 		strlcat(dev->name, ": ", sizeof(dev->name));
2404e7b09f18SPeter Boström 		len = strlen(dev->name);
2405e7b09f18SPeter Boström 		usb_string(udev, function, dev->name + len,
2406e7b09f18SPeter Boström 			   sizeof(dev->name) - len);
2407e7b09f18SPeter Boström 	}
2408e7b09f18SPeter Boström 
24098c279e93SLaurent Pinchart 	/* Initialize the media device. */
24108c279e93SLaurent Pinchart #ifdef CONFIG_MEDIA_CONTROLLER
24118c279e93SLaurent Pinchart 	dev->mdev.dev = &intf->dev;
24128c279e93SLaurent Pinchart 	strscpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model));
24138c279e93SLaurent Pinchart 	if (udev->serial)
24148c279e93SLaurent Pinchart 		strscpy(dev->mdev.serial, udev->serial,
24158c279e93SLaurent Pinchart 			sizeof(dev->mdev.serial));
24168c279e93SLaurent Pinchart 	usb_make_path(udev, dev->mdev.bus_info, sizeof(dev->mdev.bus_info));
24178c279e93SLaurent Pinchart 	dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
24188c279e93SLaurent Pinchart 	media_device_init(&dev->mdev);
24198c279e93SLaurent Pinchart 
24208c279e93SLaurent Pinchart 	dev->vdev.mdev = &dev->mdev;
24218c279e93SLaurent Pinchart #endif
24228c279e93SLaurent Pinchart 
24230c0d06caSMauro Carvalho Chehab 	/* Parse the Video Class control descriptor. */
24240c0d06caSMauro Carvalho Chehab 	if (uvc_parse_control(dev) < 0) {
24259e56380aSJoe Perches 		uvc_dbg(dev, PROBE, "Unable to parse UVC descriptors\n");
24260c0d06caSMauro Carvalho Chehab 		goto error;
24270c0d06caSMauro Carvalho Chehab 	}
24280c0d06caSMauro Carvalho Chehab 
24292886477fSRicardo Ribalda 	/* Parse the associated GPIOs. */
24302886477fSRicardo Ribalda 	if (uvc_gpio_parse(dev) < 0) {
24319e56380aSJoe Perches 		uvc_dbg(dev, PROBE, "Unable to parse UVC GPIOs\n");
24322886477fSRicardo Ribalda 		goto error;
24332886477fSRicardo Ribalda 	}
24342886477fSRicardo Ribalda 
243569df0954SRicardo Ribalda 	dev_info(&dev->udev->dev, "Found UVC %u.%02x device %s (%04x:%04x)\n",
24360c0d06caSMauro Carvalho Chehab 		 dev->uvc_version >> 8, dev->uvc_version & 0xff,
24370c0d06caSMauro Carvalho Chehab 		 udev->product ? udev->product : "<unnamed>",
24380c0d06caSMauro Carvalho Chehab 		 le16_to_cpu(udev->descriptor.idVendor),
24390c0d06caSMauro Carvalho Chehab 		 le16_to_cpu(udev->descriptor.idProduct));
24400c0d06caSMauro Carvalho Chehab 
24413a03284dSLaurent Pinchart 	if (dev->quirks != dev->info->quirks) {
244269df0954SRicardo Ribalda 		dev_info(&dev->udev->dev,
244369df0954SRicardo Ribalda 			 "Forcing device quirks to 0x%x by module parameter for testing purpose.\n",
244469df0954SRicardo Ribalda 			 dev->quirks);
244569df0954SRicardo Ribalda 		dev_info(&dev->udev->dev,
244669df0954SRicardo Ribalda 			 "Please report required quirks to the linux-uvc-devel mailing list.\n");
24470c0d06caSMauro Carvalho Chehab 	}
24480c0d06caSMauro Carvalho Chehab 
2449b400b6f2SLaurent Pinchart 	if (dev->info->uvc_version) {
2450b400b6f2SLaurent Pinchart 		dev->uvc_version = dev->info->uvc_version;
245169df0954SRicardo Ribalda 		dev_info(&dev->udev->dev, "Forcing UVC version to %u.%02x\n",
2452b400b6f2SLaurent Pinchart 			 dev->uvc_version >> 8, dev->uvc_version & 0xff);
2453b400b6f2SLaurent Pinchart 	}
2454b400b6f2SLaurent Pinchart 
24558c279e93SLaurent Pinchart 	/* Register the V4L2 device. */
24560c0d06caSMauro Carvalho Chehab 	if (v4l2_device_register(&intf->dev, &dev->vdev) < 0)
24570c0d06caSMauro Carvalho Chehab 		goto error;
24580c0d06caSMauro Carvalho Chehab 
24590c0d06caSMauro Carvalho Chehab 	/* Scan the device for video chains. */
24600c0d06caSMauro Carvalho Chehab 	if (uvc_scan_device(dev) < 0)
24610c0d06caSMauro Carvalho Chehab 		goto error;
24620c0d06caSMauro Carvalho Chehab 
2463866c6bddSRicardo Ribalda 	/* Initialize controls. */
2464866c6bddSRicardo Ribalda 	if (uvc_ctrl_init_device(dev) < 0)
2465866c6bddSRicardo Ribalda 		goto error;
2466866c6bddSRicardo Ribalda 
24670c0d06caSMauro Carvalho Chehab 	/* Register video device nodes. */
24680c0d06caSMauro Carvalho Chehab 	if (uvc_register_chains(dev) < 0)
24690c0d06caSMauro Carvalho Chehab 		goto error;
24700c0d06caSMauro Carvalho Chehab 
24719832e155SJavier Martinez Canillas #ifdef CONFIG_MEDIA_CONTROLLER
24729832e155SJavier Martinez Canillas 	/* Register the media device node */
24739832e155SJavier Martinez Canillas 	if (media_device_register(&dev->mdev) < 0)
24749832e155SJavier Martinez Canillas 		goto error;
24759832e155SJavier Martinez Canillas #endif
24760c0d06caSMauro Carvalho Chehab 	/* Save our data pointer in the interface data. */
24770c0d06caSMauro Carvalho Chehab 	usb_set_intfdata(intf, dev);
24780c0d06caSMauro Carvalho Chehab 
24790c0d06caSMauro Carvalho Chehab 	/* Initialize the interrupt URB. */
24800c0d06caSMauro Carvalho Chehab 	if ((ret = uvc_status_init(dev)) < 0) {
248169df0954SRicardo Ribalda 		dev_info(&dev->udev->dev,
248269df0954SRicardo Ribalda 			 "Unable to initialize the status endpoint (%d), status interrupt will not be supported.\n",
248369df0954SRicardo Ribalda 			 ret);
24840c0d06caSMauro Carvalho Chehab 	}
24850c0d06caSMauro Carvalho Chehab 
24862886477fSRicardo Ribalda 	ret = uvc_gpio_init_irq(dev);
24872886477fSRicardo Ribalda 	if (ret < 0) {
24882886477fSRicardo Ribalda 		dev_err(&dev->udev->dev,
24892886477fSRicardo Ribalda 			"Unable to request privacy GPIO IRQ (%d)\n", ret);
24902886477fSRicardo Ribalda 		goto error;
24912886477fSRicardo Ribalda 	}
24922886477fSRicardo Ribalda 
24939e56380aSJoe Perches 	uvc_dbg(dev, PROBE, "UVC device initialized\n");
24940c0d06caSMauro Carvalho Chehab 	usb_enable_autosuspend(udev);
24950c0d06caSMauro Carvalho Chehab 	return 0;
24960c0d06caSMauro Carvalho Chehab 
24970c0d06caSMauro Carvalho Chehab error:
24980c0d06caSMauro Carvalho Chehab 	uvc_unregister_video(dev);
2499f9ffcb0aSPhilipp Zabel 	kref_put(&dev->ref, uvc_delete);
25000c0d06caSMauro Carvalho Chehab 	return -ENODEV;
25010c0d06caSMauro Carvalho Chehab }
25020c0d06caSMauro Carvalho Chehab 
25030c0d06caSMauro Carvalho Chehab static void uvc_disconnect(struct usb_interface *intf)
25040c0d06caSMauro Carvalho Chehab {
25050c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = usb_get_intfdata(intf);
25060c0d06caSMauro Carvalho Chehab 
25070c0d06caSMauro Carvalho Chehab 	/* Set the USB interface data to NULL. This can be done outside the
25080c0d06caSMauro Carvalho Chehab 	 * lock, as there's no other reader.
25090c0d06caSMauro Carvalho Chehab 	 */
25100c0d06caSMauro Carvalho Chehab 	usb_set_intfdata(intf, NULL);
25110c0d06caSMauro Carvalho Chehab 
25120c0d06caSMauro Carvalho Chehab 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
25130c0d06caSMauro Carvalho Chehab 	    UVC_SC_VIDEOSTREAMING)
25140c0d06caSMauro Carvalho Chehab 		return;
25150c0d06caSMauro Carvalho Chehab 
25160c0d06caSMauro Carvalho Chehab 	uvc_unregister_video(dev);
2517f9ffcb0aSPhilipp Zabel 	kref_put(&dev->ref, uvc_delete);
25180c0d06caSMauro Carvalho Chehab }
25190c0d06caSMauro Carvalho Chehab 
25200c0d06caSMauro Carvalho Chehab static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
25210c0d06caSMauro Carvalho Chehab {
25220c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = usb_get_intfdata(intf);
25230c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream;
25240c0d06caSMauro Carvalho Chehab 
25259e56380aSJoe Perches 	uvc_dbg(dev, SUSPEND, "Suspending interface %u\n",
25260c0d06caSMauro Carvalho Chehab 		intf->cur_altsetting->desc.bInterfaceNumber);
25270c0d06caSMauro Carvalho Chehab 
25280c0d06caSMauro Carvalho Chehab 	/* Controls are cached on the fly so they don't need to be saved. */
25290c0d06caSMauro Carvalho Chehab 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
253017706f56SLaurent Pinchart 	    UVC_SC_VIDEOCONTROL) {
253117706f56SLaurent Pinchart 		mutex_lock(&dev->lock);
253217706f56SLaurent Pinchart 		if (dev->users)
253317706f56SLaurent Pinchart 			uvc_status_stop(dev);
253417706f56SLaurent Pinchart 		mutex_unlock(&dev->lock);
253517706f56SLaurent Pinchart 		return 0;
253617706f56SLaurent Pinchart 	}
25370c0d06caSMauro Carvalho Chehab 
25380c0d06caSMauro Carvalho Chehab 	list_for_each_entry(stream, &dev->streams, list) {
25390c0d06caSMauro Carvalho Chehab 		if (stream->intf == intf)
25400c0d06caSMauro Carvalho Chehab 			return uvc_video_suspend(stream);
25410c0d06caSMauro Carvalho Chehab 	}
25420c0d06caSMauro Carvalho Chehab 
25439e56380aSJoe Perches 	uvc_dbg(dev, SUSPEND,
25449e56380aSJoe Perches 		"Suspend: video streaming USB interface mismatch\n");
25450c0d06caSMauro Carvalho Chehab 	return -EINVAL;
25460c0d06caSMauro Carvalho Chehab }
25470c0d06caSMauro Carvalho Chehab 
25480c0d06caSMauro Carvalho Chehab static int __uvc_resume(struct usb_interface *intf, int reset)
25490c0d06caSMauro Carvalho Chehab {
25500c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = usb_get_intfdata(intf);
25510c0d06caSMauro Carvalho Chehab 	struct uvc_streaming *stream;
2552b83bba24SLaurent Pinchart 	int ret = 0;
25530c0d06caSMauro Carvalho Chehab 
25549e56380aSJoe Perches 	uvc_dbg(dev, SUSPEND, "Resuming interface %u\n",
25550c0d06caSMauro Carvalho Chehab 		intf->cur_altsetting->desc.bInterfaceNumber);
25560c0d06caSMauro Carvalho Chehab 
25570c0d06caSMauro Carvalho Chehab 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
25580c0d06caSMauro Carvalho Chehab 	    UVC_SC_VIDEOCONTROL) {
255917706f56SLaurent Pinchart 		if (reset) {
256017e1319fSWilliam Manley 			ret = uvc_ctrl_restore_values(dev);
25610c0d06caSMauro Carvalho Chehab 			if (ret < 0)
25620c0d06caSMauro Carvalho Chehab 				return ret;
25630c0d06caSMauro Carvalho Chehab 		}
25640c0d06caSMauro Carvalho Chehab 
256517706f56SLaurent Pinchart 		mutex_lock(&dev->lock);
256617706f56SLaurent Pinchart 		if (dev->users)
256717706f56SLaurent Pinchart 			ret = uvc_status_start(dev, GFP_NOIO);
256817706f56SLaurent Pinchart 		mutex_unlock(&dev->lock);
256917706f56SLaurent Pinchart 
257017706f56SLaurent Pinchart 		return ret;
25710c0d06caSMauro Carvalho Chehab 	}
25720c0d06caSMauro Carvalho Chehab 
25730c0d06caSMauro Carvalho Chehab 	list_for_each_entry(stream, &dev->streams, list) {
2574b83bba24SLaurent Pinchart 		if (stream->intf == intf) {
2575b83bba24SLaurent Pinchart 			ret = uvc_video_resume(stream, reset);
2576b83bba24SLaurent Pinchart 			if (ret < 0)
25770da4ab98SLaurent Pinchart 				uvc_queue_streamoff(&stream->queue,
25780da4ab98SLaurent Pinchart 						    stream->queue.queue.type);
2579b83bba24SLaurent Pinchart 			return ret;
2580b83bba24SLaurent Pinchart 		}
25810c0d06caSMauro Carvalho Chehab 	}
25820c0d06caSMauro Carvalho Chehab 
25839e56380aSJoe Perches 	uvc_dbg(dev, SUSPEND,
25849e56380aSJoe Perches 		"Resume: video streaming USB interface mismatch\n");
25850c0d06caSMauro Carvalho Chehab 	return -EINVAL;
25860c0d06caSMauro Carvalho Chehab }
25870c0d06caSMauro Carvalho Chehab 
25880c0d06caSMauro Carvalho Chehab static int uvc_resume(struct usb_interface *intf)
25890c0d06caSMauro Carvalho Chehab {
25900c0d06caSMauro Carvalho Chehab 	return __uvc_resume(intf, 0);
25910c0d06caSMauro Carvalho Chehab }
25920c0d06caSMauro Carvalho Chehab 
25930c0d06caSMauro Carvalho Chehab static int uvc_reset_resume(struct usb_interface *intf)
25940c0d06caSMauro Carvalho Chehab {
25950c0d06caSMauro Carvalho Chehab 	return __uvc_resume(intf, 1);
25960c0d06caSMauro Carvalho Chehab }
25970c0d06caSMauro Carvalho Chehab 
25980c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
25990c0d06caSMauro Carvalho Chehab  * Module parameters
26000c0d06caSMauro Carvalho Chehab  */
26010c0d06caSMauro Carvalho Chehab 
2602e4dca7b7SKees Cook static int uvc_clock_param_get(char *buffer, const struct kernel_param *kp)
26030c0d06caSMauro Carvalho Chehab {
26040c0d06caSMauro Carvalho Chehab 	if (uvc_clock_param == CLOCK_MONOTONIC)
26050c0d06caSMauro Carvalho Chehab 		return sprintf(buffer, "CLOCK_MONOTONIC");
26060c0d06caSMauro Carvalho Chehab 	else
26070c0d06caSMauro Carvalho Chehab 		return sprintf(buffer, "CLOCK_REALTIME");
26080c0d06caSMauro Carvalho Chehab }
26090c0d06caSMauro Carvalho Chehab 
2610e4dca7b7SKees Cook static int uvc_clock_param_set(const char *val, const struct kernel_param *kp)
26110c0d06caSMauro Carvalho Chehab {
26120c0d06caSMauro Carvalho Chehab 	if (strncasecmp(val, "clock_", strlen("clock_")) == 0)
26130c0d06caSMauro Carvalho Chehab 		val += strlen("clock_");
26140c0d06caSMauro Carvalho Chehab 
26150c0d06caSMauro Carvalho Chehab 	if (strcasecmp(val, "monotonic") == 0)
26160c0d06caSMauro Carvalho Chehab 		uvc_clock_param = CLOCK_MONOTONIC;
26170c0d06caSMauro Carvalho Chehab 	else if (strcasecmp(val, "realtime") == 0)
26180c0d06caSMauro Carvalho Chehab 		uvc_clock_param = CLOCK_REALTIME;
26190c0d06caSMauro Carvalho Chehab 	else
26200c0d06caSMauro Carvalho Chehab 		return -EINVAL;
26210c0d06caSMauro Carvalho Chehab 
26220c0d06caSMauro Carvalho Chehab 	return 0;
26230c0d06caSMauro Carvalho Chehab }
26240c0d06caSMauro Carvalho Chehab 
26250c0d06caSMauro Carvalho Chehab module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get,
26260c0d06caSMauro Carvalho Chehab 		  &uvc_clock_param, S_IRUGO|S_IWUSR);
26270c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(clock, "Video buffers timestamp clock");
26285d0fd3c8SLaurent Pinchart module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR);
26295d0fd3c8SLaurent Pinchart MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps");
26300c0d06caSMauro Carvalho Chehab module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
26310c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
26320c0d06caSMauro Carvalho Chehab module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
26330c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(quirks, "Forced device quirks");
26349e56380aSJoe Perches module_param_named(trace, uvc_dbg_param, uint, S_IRUGO|S_IWUSR);
26350c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(trace, "Trace level bitmask");
26360c0d06caSMauro Carvalho Chehab module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR);
26370c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(timeout, "Streaming control requests timeout");
26380c0d06caSMauro Carvalho Chehab 
26390c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------------
26400c0d06caSMauro Carvalho Chehab  * Driver initialization and cleanup
26410c0d06caSMauro Carvalho Chehab  */
26420c0d06caSMauro Carvalho Chehab 
26433bc85817SGuennadi Liakhovetski static const struct uvc_device_info uvc_quirk_probe_minmax = {
26443bc85817SGuennadi Liakhovetski 	.quirks = UVC_QUIRK_PROBE_MINMAX,
26453bc85817SGuennadi Liakhovetski };
26463bc85817SGuennadi Liakhovetski 
26473bc85817SGuennadi Liakhovetski static const struct uvc_device_info uvc_quirk_fix_bandwidth = {
26483bc85817SGuennadi Liakhovetski 	.quirks = UVC_QUIRK_FIX_BANDWIDTH,
26493bc85817SGuennadi Liakhovetski };
26503bc85817SGuennadi Liakhovetski 
26513bc85817SGuennadi Liakhovetski static const struct uvc_device_info uvc_quirk_probe_def = {
26523bc85817SGuennadi Liakhovetski 	.quirks = UVC_QUIRK_PROBE_DEF,
26533bc85817SGuennadi Liakhovetski };
26543bc85817SGuennadi Liakhovetski 
26553bc85817SGuennadi Liakhovetski static const struct uvc_device_info uvc_quirk_stream_no_fid = {
26563bc85817SGuennadi Liakhovetski 	.quirks = UVC_QUIRK_STREAM_NO_FID,
26573bc85817SGuennadi Liakhovetski };
26583bc85817SGuennadi Liakhovetski 
26593bc85817SGuennadi Liakhovetski static const struct uvc_device_info uvc_quirk_force_y8 = {
26603bc85817SGuennadi Liakhovetski 	.quirks = UVC_QUIRK_FORCE_Y8,
26613bc85817SGuennadi Liakhovetski };
26623bc85817SGuennadi Liakhovetski 
266388d8034cSGuennadi Liakhovetski #define UVC_INFO_QUIRK(q) (kernel_ulong_t)&(struct uvc_device_info){.quirks = q}
26646ea0d588SGuennadi Liakhovetski #define UVC_INFO_META(m) (kernel_ulong_t)&(struct uvc_device_info) \
26656ea0d588SGuennadi Liakhovetski 	{.meta_format = m}
26663bc85817SGuennadi Liakhovetski 
26670c0d06caSMauro Carvalho Chehab /*
26680c0d06caSMauro Carvalho Chehab  * The Logitech cameras listed below have their interface class set to
26690c0d06caSMauro Carvalho Chehab  * VENDOR_SPEC because they don't announce themselves as UVC devices, even
26700c0d06caSMauro Carvalho Chehab  * though they are compliant.
26710c0d06caSMauro Carvalho Chehab  */
26727fb2e072SArvind Yadav static const struct usb_device_id uvc_ids[] = {
26730c0d06caSMauro Carvalho Chehab 	/* LogiLink Wireless Webcam */
26740c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
26750c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
26760c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0416,
26770c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0xa91a,
26780c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
26790c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
26800c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
26813bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
26820c0d06caSMauro Carvalho Chehab 	/* Genius eFace 2025 */
26830c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
26840c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
26850c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0458,
26860c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x706e,
26870c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
26880c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
26890c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
26903bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
26910c0d06caSMauro Carvalho Chehab 	/* Microsoft Lifecam NX-6000 */
26920c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
26930c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
26940c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x045e,
26950c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x00f8,
26960c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
26970c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
26980c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
26993bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
27001558ec83SLaurent Pinchart 	/* Microsoft Lifecam NX-3000 */
27011558ec83SLaurent Pinchart 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27021558ec83SLaurent Pinchart 				| USB_DEVICE_ID_MATCH_INT_INFO,
27031558ec83SLaurent Pinchart 	  .idVendor		= 0x045e,
27041558ec83SLaurent Pinchart 	  .idProduct		= 0x0721,
27051558ec83SLaurent Pinchart 	  .bInterfaceClass	= USB_CLASS_VIDEO,
27061558ec83SLaurent Pinchart 	  .bInterfaceSubClass	= 1,
27071558ec83SLaurent Pinchart 	  .bInterfaceProtocol	= 0,
27083bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
27090c0d06caSMauro Carvalho Chehab 	/* Microsoft Lifecam VX-7000 */
27100c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27110c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27120c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x045e,
27130c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0723,
27140c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
27150c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27160c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
27173bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
27180c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam Fusion */
27190c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27200c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27210c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27220c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c1,
27230c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27240c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27250c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
27260c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam Orbit MP */
27270c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27280c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27290c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27300c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c2,
27310c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27320c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27330c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
27340c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam Pro for Notebook */
27350c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27360c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27370c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27380c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c3,
27390c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27400c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27410c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
27420c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam Pro 5000 */
27430c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27440c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27450c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27460c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c5,
27470c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27480c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27490c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
27500c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam OEM Dell Notebook */
27510c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27520c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27530c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27540c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c6,
27550c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27560c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27570c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
27580c0d06caSMauro Carvalho Chehab 	/* Logitech Quickcam OEM Cisco VT Camera II */
27590c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27600c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27610c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x046d,
27620c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x08c7,
27630c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
27640c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27650c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
276617e1319fSWilliam Manley 	/* Logitech HD Pro Webcam C920 */
276717e1319fSWilliam Manley 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
276817e1319fSWilliam Manley 				| USB_DEVICE_ID_MATCH_INT_INFO,
276917e1319fSWilliam Manley 	  .idVendor		= 0x046d,
277017e1319fSWilliam Manley 	  .idProduct		= 0x082d,
277117e1319fSWilliam Manley 	  .bInterfaceClass	= USB_CLASS_VIDEO,
277217e1319fSWilliam Manley 	  .bInterfaceSubClass	= 1,
277317e1319fSWilliam Manley 	  .bInterfaceProtocol	= 0,
277488d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) },
27750c0d06caSMauro Carvalho Chehab 	/* Chicony CNF7129 (Asus EEE 100HE) */
27760c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27770c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27780c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x04f2,
27790c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0xb071,
27800c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
27810c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27820c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
278388d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_RESTRICT_FRAME_RATE) },
27840c0d06caSMauro Carvalho Chehab 	/* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
27850c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27860c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27870c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x058f,
27880c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3820,
27890c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
27900c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
27910c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
27923bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
27930c0d06caSMauro Carvalho Chehab 	/* Dell XPS m1530 */
27940c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
27950c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
27960c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x05a9,
27970c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x2640,
27980c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
27990c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
28000c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
28013bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
280289e0f248SJoseph Salisbury 	/* Dell SP2008WFP Monitor */
280389e0f248SJoseph Salisbury 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
280489e0f248SJoseph Salisbury 				| USB_DEVICE_ID_MATCH_INT_INFO,
280589e0f248SJoseph Salisbury 	  .idVendor		= 0x05a9,
280689e0f248SJoseph Salisbury 	  .idProduct		= 0x2641,
280789e0f248SJoseph Salisbury 	  .bInterfaceClass	= USB_CLASS_VIDEO,
280889e0f248SJoseph Salisbury 	  .bInterfaceSubClass	= 1,
280989e0f248SJoseph Salisbury 	  .bInterfaceProtocol	= 0,
28103bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2811c2a273b2SJoseph Salisbury 	/* Dell Alienware X51 */
2812c2a273b2SJoseph Salisbury 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2813c2a273b2SJoseph Salisbury 				| USB_DEVICE_ID_MATCH_INT_INFO,
2814c2a273b2SJoseph Salisbury 	  .idVendor		= 0x05a9,
2815c2a273b2SJoseph Salisbury 	  .idProduct		= 0x2643,
2816c2a273b2SJoseph Salisbury 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2817c2a273b2SJoseph Salisbury 	  .bInterfaceSubClass	= 1,
2818c2a273b2SJoseph Salisbury 	  .bInterfaceProtocol	= 0,
28193bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
2820afcf44c7SKamal Mostafa 	/* Dell Studio Hybrid 140g (OmniVision webcam) */
2821afcf44c7SKamal Mostafa 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2822afcf44c7SKamal Mostafa 				| USB_DEVICE_ID_MATCH_INT_INFO,
2823afcf44c7SKamal Mostafa 	  .idVendor		= 0x05a9,
2824afcf44c7SKamal Mostafa 	  .idProduct		= 0x264a,
2825afcf44c7SKamal Mostafa 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2826afcf44c7SKamal Mostafa 	  .bInterfaceSubClass	= 1,
2827afcf44c7SKamal Mostafa 	  .bInterfaceProtocol	= 0,
28283bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
282962ea864fSPaul Fertser 	/* Dell XPS M1330 (OmniVision OV7670 webcam) */
283062ea864fSPaul Fertser 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
283162ea864fSPaul Fertser 				| USB_DEVICE_ID_MATCH_INT_INFO,
283262ea864fSPaul Fertser 	  .idVendor		= 0x05a9,
283362ea864fSPaul Fertser 	  .idProduct		= 0x7670,
283462ea864fSPaul Fertser 	  .bInterfaceClass	= USB_CLASS_VIDEO,
283562ea864fSPaul Fertser 	  .bInterfaceSubClass	= 1,
283662ea864fSPaul Fertser 	  .bInterfaceProtocol	= 0,
28373bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
28380c0d06caSMauro Carvalho Chehab 	/* Apple Built-In iSight */
28390c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28400c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
28410c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x05ac,
28420c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8501,
28430c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28440c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
28450c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
284688d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
28473bc85817SGuennadi Liakhovetski 					| UVC_QUIRK_BUILTIN_ISIGHT) },
2848*53c26454SPaul Pawlowski 	/* Apple FaceTime HD Camera (Built-In) */
2849*53c26454SPaul Pawlowski 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
2850*53c26454SPaul Pawlowski 				| USB_DEVICE_ID_MATCH_INT_INFO,
2851*53c26454SPaul Pawlowski 	  .idVendor		= 0x05ac,
2852*53c26454SPaul Pawlowski 	  .idProduct		= 0x8514,
2853*53c26454SPaul Pawlowski 	  .bInterfaceClass	= USB_CLASS_VIDEO,
2854*53c26454SPaul Pawlowski 	  .bInterfaceSubClass	= 1,
2855*53c26454SPaul Pawlowski 	  .bInterfaceProtocol	= 0,
2856*53c26454SPaul Pawlowski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
28577b848ed6SDaniel Roschka 	/* Apple Built-In iSight via iBridge */
28587b848ed6SDaniel Roschka 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28597b848ed6SDaniel Roschka 				| USB_DEVICE_ID_MATCH_INT_INFO,
28607b848ed6SDaniel Roschka 	  .idVendor		= 0x05ac,
28617b848ed6SDaniel Roschka 	  .idProduct		= 0x8600,
28627b848ed6SDaniel Roschka 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28637b848ed6SDaniel Roschka 	  .bInterfaceSubClass	= 1,
28647b848ed6SDaniel Roschka 	  .bInterfaceProtocol	= 0,
28653bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
28660c0d06caSMauro Carvalho Chehab 	/* Foxlink ("HP Webcam" on HP Mini 5103) */
28670c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28680c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
28690c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x05c8,
28700c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0403,
28710c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28720c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
28730c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
28743bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
28750c0d06caSMauro Carvalho Chehab 	/* Genesys Logic USB 2.0 PC Camera */
28760c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28770c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
28780c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x05e3,
28790c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0505,
28800c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28810c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
28820c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
28833bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
28840c0d06caSMauro Carvalho Chehab 	/* Hercules Classic Silver */
28850c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28860c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
28870c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x06f8,
28880c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x300c,
28890c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28900c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
28910c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
28923bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
28930c0d06caSMauro Carvalho Chehab 	/* ViMicro Vega */
28940c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
28950c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
28960c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0ac8,
28970c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x332d,
28980c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
28990c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29000c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29013bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
29020c0d06caSMauro Carvalho Chehab 	/* ViMicro - Minoru3D */
29030c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29040c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29050c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0ac8,
29060c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3410,
29070c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29080c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29090c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29103bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
29110c0d06caSMauro Carvalho Chehab 	/* ViMicro Venus - Minoru3D */
29120c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29130c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29140c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0ac8,
29150c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3420,
29160c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29170c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29180c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29193bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_fix_bandwidth },
29200c0d06caSMauro Carvalho Chehab 	/* Ophir Optronics - SPCAM 620U */
29210c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29220c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29230c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0bd3,
29240c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0555,
29250c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29260c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29270c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29283bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
29290c0d06caSMauro Carvalho Chehab 	/* MT6227 */
29300c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29310c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29320c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x0e8d,
29330c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0004,
29340c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29350c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29360c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
293788d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
29383bc85817SGuennadi Liakhovetski 					| UVC_QUIRK_PROBE_DEF) },
29390c0d06caSMauro Carvalho Chehab 	/* IMC Networks (Medion Akoya) */
29400c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29410c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29420c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x13d3,
29430c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x5103,
29440c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29450c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29460c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29473bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
29480c0d06caSMauro Carvalho Chehab 	/* JMicron USB2.0 XGA WebCam */
29490c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29500c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29510c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x152d,
29520c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0310,
29530c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29540c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29550c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29563bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
29570c0d06caSMauro Carvalho Chehab 	/* Syntek (HP Spartan) */
29580c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29590c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29600c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
29610c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x5212,
29620c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29630c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29640c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29653bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
29660c0d06caSMauro Carvalho Chehab 	/* Syntek (Samsung Q310) */
29670c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29680c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29690c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
29700c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x5931,
29710c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29720c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29730c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29743bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
29750c0d06caSMauro Carvalho Chehab 	/* Syntek (Packard Bell EasyNote MX52 */
29760c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29770c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29780c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
29790c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8a12,
29800c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29810c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29820c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29833bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
29840c0d06caSMauro Carvalho Chehab 	/* Syntek (Asus F9SG) */
29850c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29860c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29870c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
29880c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8a31,
29890c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29900c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
29910c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
29923bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
29930c0d06caSMauro Carvalho Chehab 	/* Syntek (Asus U3S) */
29940c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
29950c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
29960c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
29970c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8a33,
29980c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
29990c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30000c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30013bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
30020c0d06caSMauro Carvalho Chehab 	/* Syntek (JAOtech Smart Terminal) */
30030c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30040c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30050c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x174f,
30060c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8a34,
30070c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30080c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30090c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30103bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
30110c0d06caSMauro Carvalho Chehab 	/* Miricle 307K */
30120c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30130c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30140c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x17dc,
30150c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0202,
30160c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30170c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30180c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30193bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
30200c0d06caSMauro Carvalho Chehab 	/* Lenovo Thinkpad SL400/SL500 */
30210c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30220c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30230c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x17ef,
30240c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x480b,
30250c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30260c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30270c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30283bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_stream_no_fid },
30290c0d06caSMauro Carvalho Chehab 	/* Aveo Technology USB 2.0 Camera */
30300c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30310c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30320c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x1871,
30330c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x0306,
30340c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30350c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30360c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
303788d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
30383bc85817SGuennadi Liakhovetski 					| UVC_QUIRK_PROBE_EXTRAFIELDS) },
3039fe652471SLaurent Pinchart 	/* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */
3040fe652471SLaurent Pinchart 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3041fe652471SLaurent Pinchart 				| USB_DEVICE_ID_MATCH_INT_INFO,
3042fe652471SLaurent Pinchart 	  .idVendor		= 0x1871,
3043fe652471SLaurent Pinchart 	  .idProduct		= 0x0516,
3044fe652471SLaurent Pinchart 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
3045fe652471SLaurent Pinchart 	  .bInterfaceSubClass	= 1,
3046fe652471SLaurent Pinchart 	  .bInterfaceProtocol	= 0 },
30470c0d06caSMauro Carvalho Chehab 	/* Ecamm Pico iMage */
30480c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30490c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30500c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x18cd,
30510c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0xcafe,
30520c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30530c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30540c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
305588d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_EXTRAFIELDS) },
30560c0d06caSMauro Carvalho Chehab 	/* Manta MM-353 Plako */
30570c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30580c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30590c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x18ec,
30600c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3188,
30610c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30620c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30630c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30643bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
30650c0d06caSMauro Carvalho Chehab 	/* FSC WebCam V30S */
30660c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30670c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30680c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x18ec,
30690c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3288,
30700c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30710c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30720c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30733bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
30740c0d06caSMauro Carvalho Chehab 	/* Arkmicro unbranded */
30750c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30760c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30770c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x18ec,
30780c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3290,
30790c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30800c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30810c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
30823bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_def },
30830c0d06caSMauro Carvalho Chehab 	/* The Imaging Source USB CCD cameras */
30840c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30850c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30860c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x199e,
30870c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x8102,
30880c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
30890c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
30900c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0 },
30910c0d06caSMauro Carvalho Chehab 	/* Bodelin ProScopeHR */
30920c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
30930c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_DEV_HI
30940c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
30950c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x19ab,
30960c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x1000,
30970c0d06caSMauro Carvalho Chehab 	  .bcdDevice_hi		= 0x0126,
30980c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
30990c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
31000c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
310188d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_STATUS_INTERVAL) },
31020c0d06caSMauro Carvalho Chehab 	/* MSI StarCam 370i */
31030c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
31040c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
31050c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x1b3b,
31060c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x2951,
31070c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
31080c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
31090c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
31103bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3111589266bdSNeil Armstrong 	/* Generalplus Technology Inc. 808 Camera */
3112589266bdSNeil Armstrong 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3113589266bdSNeil Armstrong 				| USB_DEVICE_ID_MATCH_INT_INFO,
3114589266bdSNeil Armstrong 	  .idVendor		= 0x1b3f,
3115589266bdSNeil Armstrong 	  .idProduct		= 0x2002,
3116589266bdSNeil Armstrong 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3117589266bdSNeil Armstrong 	  .bInterfaceSubClass	= 1,
3118589266bdSNeil Armstrong 	  .bInterfaceProtocol	= 0,
3119589266bdSNeil Armstrong 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_probe_minmax },
3120b400b6f2SLaurent Pinchart 	/* Shenzhen Aoni Electronic Co.,Ltd 2K FHD camera */
3121b400b6f2SLaurent Pinchart 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3122b400b6f2SLaurent Pinchart 				| USB_DEVICE_ID_MATCH_INT_INFO,
3123b400b6f2SLaurent Pinchart 	  .idVendor		= 0x1bcf,
3124b400b6f2SLaurent Pinchart 	  .idProduct		= 0x0b40,
3125b400b6f2SLaurent Pinchart 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3126b400b6f2SLaurent Pinchart 	  .bInterfaceSubClass	= 1,
3127b400b6f2SLaurent Pinchart 	  .bInterfaceProtocol	= 0,
3128b400b6f2SLaurent Pinchart 	  .driver_info		= (kernel_ulong_t)&(const struct uvc_device_info){
3129b400b6f2SLaurent Pinchart 		.uvc_version = 0x010a,
3130b400b6f2SLaurent Pinchart 	  } },
31310c0d06caSMauro Carvalho Chehab 	/* SiGma Micro USB Web Camera */
31320c0d06caSMauro Carvalho Chehab 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
31330c0d06caSMauro Carvalho Chehab 				| USB_DEVICE_ID_MATCH_INT_INFO,
31340c0d06caSMauro Carvalho Chehab 	  .idVendor		= 0x1c4f,
31350c0d06caSMauro Carvalho Chehab 	  .idProduct		= 0x3000,
31360c0d06caSMauro Carvalho Chehab 	  .bInterfaceClass	= USB_CLASS_VIDEO,
31370c0d06caSMauro Carvalho Chehab 	  .bInterfaceSubClass	= 1,
31380c0d06caSMauro Carvalho Chehab 	  .bInterfaceProtocol	= 0,
313988d8034cSGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX
31403bc85817SGuennadi Liakhovetski 					| UVC_QUIRK_IGNORE_SELECTOR_UNIT) },
3141e1b78a33SPhilipp Zabel 	/* Oculus VR Positional Tracker DK2 */
3142e1b78a33SPhilipp Zabel 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
3143e1b78a33SPhilipp Zabel 				| USB_DEVICE_ID_MATCH_INT_INFO,
3144e1b78a33SPhilipp Zabel 	  .idVendor		= 0x2833,
3145e1b78a33SPhilipp Zabel 	  .idProduct		= 0x0201,
3146e1b78a33SPhilipp Zabel 	  .bInterfaceClass	= USB_CLASS_VIDEO,
3147e1b78a33SPhilipp Zabel 	  .bInterfaceSubClass	= 1,
3148e1b78a33SPhilipp Zabel 	  .bInterfaceProtocol	= 0,
31493bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
315003c47aaeSPhilipp Zabel 	/* Oculus VR Rift Sensor */
315103c47aaeSPhilipp Zabel 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
315203c47aaeSPhilipp Zabel 				| USB_DEVICE_ID_MATCH_INT_INFO,
315303c47aaeSPhilipp Zabel 	  .idVendor		= 0x2833,
315403c47aaeSPhilipp Zabel 	  .idProduct		= 0x0211,
315503c47aaeSPhilipp Zabel 	  .bInterfaceClass	= USB_CLASS_VENDOR_SPEC,
315603c47aaeSPhilipp Zabel 	  .bInterfaceSubClass	= 1,
315703c47aaeSPhilipp Zabel 	  .bInterfaceProtocol	= 0,
31583bc85817SGuennadi Liakhovetski 	  .driver_info		= (kernel_ulong_t)&uvc_quirk_force_y8 },
31591dd2e8f9SSergey Zakharchenko 	/* GEO Semiconductor GC6500 */
31601dd2e8f9SSergey Zakharchenko 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
31611dd2e8f9SSergey Zakharchenko 				| USB_DEVICE_ID_MATCH_INT_INFO,
31621dd2e8f9SSergey Zakharchenko 	  .idVendor		= 0x29fe,
31631dd2e8f9SSergey Zakharchenko 	  .idProduct		= 0x4d53,
31641dd2e8f9SSergey Zakharchenko 	  .bInterfaceClass	= USB_CLASS_VIDEO,
31651dd2e8f9SSergey Zakharchenko 	  .bInterfaceSubClass	= 1,
31661dd2e8f9SSergey Zakharchenko 	  .bInterfaceProtocol	= 0,
31671dd2e8f9SSergey Zakharchenko 	  .driver_info		= UVC_INFO_QUIRK(UVC_QUIRK_FORCE_BPP) },
31686ea0d588SGuennadi Liakhovetski 	/* Intel RealSense D4M */
31696ea0d588SGuennadi Liakhovetski 	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE
31706ea0d588SGuennadi Liakhovetski 				| USB_DEVICE_ID_MATCH_INT_INFO,
31716ea0d588SGuennadi Liakhovetski 	  .idVendor		= 0x8086,
31726ea0d588SGuennadi Liakhovetski 	  .idProduct		= 0x0b03,
31736ea0d588SGuennadi Liakhovetski 	  .bInterfaceClass	= USB_CLASS_VIDEO,
31746ea0d588SGuennadi Liakhovetski 	  .bInterfaceSubClass	= 1,
31756ea0d588SGuennadi Liakhovetski 	  .bInterfaceProtocol	= 0,
31766ea0d588SGuennadi Liakhovetski 	  .driver_info		= UVC_INFO_META(V4L2_META_FMT_D4XX) },
31770c0d06caSMauro Carvalho Chehab 	/* Generic USB Video Class */
31788afe97beSLaurent Pinchart 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) },
31798afe97beSLaurent Pinchart 	{ USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) },
31800c0d06caSMauro Carvalho Chehab 	{}
31810c0d06caSMauro Carvalho Chehab };
31820c0d06caSMauro Carvalho Chehab 
31830c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, uvc_ids);
31840c0d06caSMauro Carvalho Chehab 
31850c0d06caSMauro Carvalho Chehab struct uvc_driver uvc_driver = {
31860c0d06caSMauro Carvalho Chehab 	.driver = {
31870c0d06caSMauro Carvalho Chehab 		.name		= "uvcvideo",
31880c0d06caSMauro Carvalho Chehab 		.probe		= uvc_probe,
31890c0d06caSMauro Carvalho Chehab 		.disconnect	= uvc_disconnect,
31900c0d06caSMauro Carvalho Chehab 		.suspend	= uvc_suspend,
31910c0d06caSMauro Carvalho Chehab 		.resume		= uvc_resume,
31920c0d06caSMauro Carvalho Chehab 		.reset_resume	= uvc_reset_resume,
31930c0d06caSMauro Carvalho Chehab 		.id_table	= uvc_ids,
31940c0d06caSMauro Carvalho Chehab 		.supports_autosuspend = 1,
31950c0d06caSMauro Carvalho Chehab 	},
31960c0d06caSMauro Carvalho Chehab };
31970c0d06caSMauro Carvalho Chehab 
31980c0d06caSMauro Carvalho Chehab static int __init uvc_init(void)
31990c0d06caSMauro Carvalho Chehab {
32000c0d06caSMauro Carvalho Chehab 	int ret;
32010c0d06caSMauro Carvalho Chehab 
32020c0d06caSMauro Carvalho Chehab 	uvc_debugfs_init();
32030c0d06caSMauro Carvalho Chehab 
32040c0d06caSMauro Carvalho Chehab 	ret = usb_register(&uvc_driver.driver);
32050c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
32060c0d06caSMauro Carvalho Chehab 		uvc_debugfs_cleanup();
32070c0d06caSMauro Carvalho Chehab 		return ret;
32080c0d06caSMauro Carvalho Chehab 	}
32090c0d06caSMauro Carvalho Chehab 
32100c0d06caSMauro Carvalho Chehab 	return 0;
32110c0d06caSMauro Carvalho Chehab }
32120c0d06caSMauro Carvalho Chehab 
32130c0d06caSMauro Carvalho Chehab static void __exit uvc_cleanup(void)
32140c0d06caSMauro Carvalho Chehab {
32150c0d06caSMauro Carvalho Chehab 	usb_deregister(&uvc_driver.driver);
32160c0d06caSMauro Carvalho Chehab 	uvc_debugfs_cleanup();
32170c0d06caSMauro Carvalho Chehab }
32180c0d06caSMauro Carvalho Chehab 
32190c0d06caSMauro Carvalho Chehab module_init(uvc_init);
32200c0d06caSMauro Carvalho Chehab module_exit(uvc_cleanup);
32210c0d06caSMauro Carvalho Chehab 
32220c0d06caSMauro Carvalho Chehab MODULE_AUTHOR(DRIVER_AUTHOR);
32230c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION(DRIVER_DESC);
32240c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
32250c0d06caSMauro Carvalho Chehab MODULE_VERSION(DRIVER_VERSION);
32260c0d06caSMauro Carvalho Chehab 
3227