xref: /openbmc/linux/drivers/media/usb/uvc/uvcvideo.h (revision 2e7c04aec86758e0adfcad4a24c86593b45807a3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _USB_VIDEO_H_
3 #define _USB_VIDEO_H_
4 
5 #ifndef __KERNEL__
6 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
7 #endif /* __KERNEL__ */
8 
9 #include <linux/kernel.h>
10 #include <linux/poll.h>
11 #include <linux/usb.h>
12 #include <linux/usb/video.h>
13 #include <linux/uvcvideo.h>
14 #include <linux/videodev2.h>
15 #include <linux/workqueue.h>
16 #include <media/media-device.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-fh.h>
20 #include <media/videobuf2-v4l2.h>
21 
22 /* --------------------------------------------------------------------------
23  * UVC constants
24  */
25 
26 #define UVC_TERM_INPUT			0x0000
27 #define UVC_TERM_OUTPUT			0x8000
28 #define UVC_TERM_DIRECTION(term)	((term)->type & 0x8000)
29 
30 #define UVC_ENTITY_TYPE(entity)		((entity)->type & 0x7fff)
31 #define UVC_ENTITY_IS_UNIT(entity)	(((entity)->type & 0xff00) == 0)
32 #define UVC_ENTITY_IS_TERM(entity)	(((entity)->type & 0xff00) != 0)
33 #define UVC_ENTITY_IS_ITERM(entity) \
34 	(UVC_ENTITY_IS_TERM(entity) && \
35 	((entity)->type & 0x8000) == UVC_TERM_INPUT)
36 #define UVC_ENTITY_IS_OTERM(entity) \
37 	(UVC_ENTITY_IS_TERM(entity) && \
38 	((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
39 
40 
41 /* ------------------------------------------------------------------------
42  * GUIDs
43  */
44 #define UVC_GUID_UVC_CAMERA \
45 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
46 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
47 #define UVC_GUID_UVC_OUTPUT \
48 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
49 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
50 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
51 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
52 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
53 #define UVC_GUID_UVC_PROCESSING \
54 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
55 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
56 #define UVC_GUID_UVC_SELECTOR \
57 	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
58 	 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
59 
60 #define UVC_GUID_FORMAT_MJPEG \
61 	{ 'M',  'J',  'P',  'G', 0x00, 0x00, 0x10, 0x00, \
62 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
63 #define UVC_GUID_FORMAT_YUY2 \
64 	{ 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
65 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
66 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
67 	{ 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
68 	 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
69 #define UVC_GUID_FORMAT_NV12 \
70 	{ 'N',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
71 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
72 #define UVC_GUID_FORMAT_YV12 \
73 	{ 'Y',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
74 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
75 #define UVC_GUID_FORMAT_I420 \
76 	{ 'I',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
77 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
78 #define UVC_GUID_FORMAT_UYVY \
79 	{ 'U',  'Y',  'V',  'Y', 0x00, 0x00, 0x10, 0x00, \
80 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
81 #define UVC_GUID_FORMAT_Y800 \
82 	{ 'Y',  '8',  '0',  '0', 0x00, 0x00, 0x10, 0x00, \
83 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
84 #define UVC_GUID_FORMAT_Y8 \
85 	{ 'Y',  '8',  ' ',  ' ', 0x00, 0x00, 0x10, 0x00, \
86 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
87 #define UVC_GUID_FORMAT_Y10 \
88 	{ 'Y',  '1',  '0',  ' ', 0x00, 0x00, 0x10, 0x00, \
89 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
90 #define UVC_GUID_FORMAT_Y12 \
91 	{ 'Y',  '1',  '2',  ' ', 0x00, 0x00, 0x10, 0x00, \
92 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
93 #define UVC_GUID_FORMAT_Y16 \
94 	{ 'Y',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
95 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
96 #define UVC_GUID_FORMAT_BY8 \
97 	{ 'B',  'Y',  '8',  ' ', 0x00, 0x00, 0x10, 0x00, \
98 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
99 #define UVC_GUID_FORMAT_BA81 \
100 	{ 'B',  'A',  '8',  '1', 0x00, 0x00, 0x10, 0x00, \
101 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
102 #define UVC_GUID_FORMAT_GBRG \
103 	{ 'G',  'B',  'R',  'G', 0x00, 0x00, 0x10, 0x00, \
104 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
105 #define UVC_GUID_FORMAT_GRBG \
106 	{ 'G',  'R',  'B',  'G', 0x00, 0x00, 0x10, 0x00, \
107 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
108 #define UVC_GUID_FORMAT_RGGB \
109 	{ 'R',  'G',  'G',  'B', 0x00, 0x00, 0x10, 0x00, \
110 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
111 #define UVC_GUID_FORMAT_BG16 \
112 	{ 'B',  'G',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
113 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
114 #define UVC_GUID_FORMAT_GB16 \
115 	{ 'G',  'B',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
116 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
117 #define UVC_GUID_FORMAT_RG16 \
118 	{ 'R',  'G',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
119 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
120 #define UVC_GUID_FORMAT_GR16 \
121 	{ 'G',  'R',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
122 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
123 #define UVC_GUID_FORMAT_RGBP \
124 	{ 'R',  'G',  'B',  'P', 0x00, 0x00, 0x10, 0x00, \
125 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
126 #define UVC_GUID_FORMAT_BGR3 \
127 	{ 0x7d, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, \
128 	 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}
129 #define UVC_GUID_FORMAT_M420 \
130 	{ 'M',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
131 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
132 
133 #define UVC_GUID_FORMAT_H264 \
134 	{ 'H',  '2',  '6',  '4', 0x00, 0x00, 0x10, 0x00, \
135 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
136 #define UVC_GUID_FORMAT_Y8I \
137 	{ 'Y',  '8',  'I',  ' ', 0x00, 0x00, 0x10, 0x00, \
138 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
139 #define UVC_GUID_FORMAT_Y12I \
140 	{ 'Y',  '1',  '2',  'I', 0x00, 0x00, 0x10, 0x00, \
141 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
142 #define UVC_GUID_FORMAT_Z16 \
143 	{ 'Z',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
144 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
145 #define UVC_GUID_FORMAT_RW10 \
146 	{ 'R',  'W',  '1',  '0', 0x00, 0x00, 0x10, 0x00, \
147 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
148 #define UVC_GUID_FORMAT_INVZ \
149 	{ 'I',  'N',  'V',  'Z', 0x90, 0x2d, 0x58, 0x4a, \
150 	 0x92, 0x0b, 0x77, 0x3f, 0x1f, 0x2c, 0x55, 0x6b}
151 #define UVC_GUID_FORMAT_INZI \
152 	{ 'I',  'N',  'Z',  'I', 0x66, 0x1a, 0x42, 0xa2, \
153 	 0x90, 0x65, 0xd0, 0x18, 0x14, 0xa8, 0xef, 0x8a}
154 #define UVC_GUID_FORMAT_INVI \
155 	{ 'I',  'N',  'V',  'I', 0xdb, 0x57, 0x49, 0x5e, \
156 	 0x8e, 0x3f, 0xf4, 0x79, 0x53, 0x2b, 0x94, 0x6f}
157 
158 #define UVC_GUID_FORMAT_D3DFMT_L8 \
159 	{0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, \
160 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
161 #define UVC_GUID_FORMAT_KSMEDIA_L8_IR \
162 	{0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, \
163 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
164 
165 
166 /* ------------------------------------------------------------------------
167  * Driver specific constants.
168  */
169 
170 #define DRIVER_VERSION		"1.1.1"
171 
172 /* Number of isochronous URBs. */
173 #define UVC_URBS		5
174 /* Maximum number of packets per URB. */
175 #define UVC_MAX_PACKETS		32
176 /* Maximum status buffer size in bytes of interrupt URB. */
177 #define UVC_MAX_STATUS_SIZE	16
178 
179 #define UVC_CTRL_CONTROL_TIMEOUT	500
180 #define UVC_CTRL_STREAMING_TIMEOUT	5000
181 
182 /* Maximum allowed number of control mappings per device */
183 #define UVC_MAX_CONTROL_MAPPINGS	1024
184 #define UVC_MAX_CONTROL_MENU_ENTRIES	32
185 
186 /* Devices quirks */
187 #define UVC_QUIRK_STATUS_INTERVAL	0x00000001
188 #define UVC_QUIRK_PROBE_MINMAX		0x00000002
189 #define UVC_QUIRK_PROBE_EXTRAFIELDS	0x00000004
190 #define UVC_QUIRK_BUILTIN_ISIGHT	0x00000008
191 #define UVC_QUIRK_STREAM_NO_FID		0x00000010
192 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT	0x00000020
193 #define UVC_QUIRK_FIX_BANDWIDTH		0x00000080
194 #define UVC_QUIRK_PROBE_DEF		0x00000100
195 #define UVC_QUIRK_RESTRICT_FRAME_RATE	0x00000200
196 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT	0x00000400
197 #define UVC_QUIRK_FORCE_Y8		0x00000800
198 
199 /* Format flags */
200 #define UVC_FMT_FLAG_COMPRESSED		0x00000001
201 #define UVC_FMT_FLAG_STREAM		0x00000002
202 
203 /* ------------------------------------------------------------------------
204  * Structures.
205  */
206 
207 struct uvc_device;
208 
209 /* TODO: Put the most frequently accessed fields at the beginning of
210  * structures to maximize cache efficiency.
211  */
212 struct uvc_control_info {
213 	struct list_head mappings;
214 
215 	u8 entity[16];
216 	u8 index;	/* Bit index in bmControls */
217 	u8 selector;
218 
219 	u16 size;
220 	u32 flags;
221 };
222 
223 struct uvc_control_mapping {
224 	struct list_head list;
225 	struct list_head ev_subs;
226 
227 	u32 id;
228 	u8 name[32];
229 	u8 entity[16];
230 	u8 selector;
231 
232 	u8 size;
233 	u8 offset;
234 	enum v4l2_ctrl_type v4l2_type;
235 	u32 data_type;
236 
237 	struct uvc_menu_info *menu_info;
238 	u32 menu_count;
239 
240 	u32 master_id;
241 	s32 master_manual;
242 	u32 slave_ids[2];
243 
244 	s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
245 		   const u8 *data);
246 	void (*set)(struct uvc_control_mapping *mapping, s32 value,
247 		    u8 *data);
248 };
249 
250 struct uvc_control {
251 	struct uvc_entity *entity;
252 	struct uvc_control_info info;
253 
254 	u8 index;	/* Used to match the uvc_control entry with a
255 			   uvc_control_info. */
256 	u8 dirty:1,
257 	   loaded:1,
258 	   modified:1,
259 	   cached:1,
260 	   initialized:1;
261 
262 	u8 *uvc_data;
263 
264 	struct uvc_fh *handle;	/* File handle that last changed the control. */
265 };
266 
267 struct uvc_format_desc {
268 	char *name;
269 	u8 guid[16];
270 	u32 fcc;
271 };
272 
273 /* The term 'entity' refers to both UVC units and UVC terminals.
274  *
275  * The type field is either the terminal type (wTerminalType in the terminal
276  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
277  * As the bDescriptorSubtype field is one byte long, the type value will
278  * always have a null MSB for units. All terminal types defined by the UVC
279  * specification have a non-null MSB, so it is safe to use the MSB to
280  * differentiate between units and terminals as long as the descriptor parsing
281  * code makes sure terminal types have a non-null MSB.
282  *
283  * For terminals, the type's most significant bit stores the terminal
284  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
285  * always be accessed with the UVC_ENTITY_* macros and never directly.
286  */
287 
288 #define UVC_ENTITY_FLAG_DEFAULT		(1 << 0)
289 
290 struct uvc_entity {
291 	struct list_head list;		/* Entity as part of a UVC device. */
292 	struct list_head chain;		/* Entity as part of a video device
293 					 * chain. */
294 	unsigned int flags;
295 
296 	u8 id;
297 	u16 type;
298 	char name[64];
299 
300 	/* Media controller-related fields. */
301 	struct video_device *vdev;
302 	struct v4l2_subdev subdev;
303 	unsigned int num_pads;
304 	unsigned int num_links;
305 	struct media_pad *pads;
306 
307 	union {
308 		struct {
309 			u16 wObjectiveFocalLengthMin;
310 			u16 wObjectiveFocalLengthMax;
311 			u16 wOcularFocalLength;
312 			u8  bControlSize;
313 			u8  *bmControls;
314 		} camera;
315 
316 		struct {
317 			u8  bControlSize;
318 			u8  *bmControls;
319 			u8  bTransportModeSize;
320 			u8  *bmTransportModes;
321 		} media;
322 
323 		struct {
324 		} output;
325 
326 		struct {
327 			u16 wMaxMultiplier;
328 			u8  bControlSize;
329 			u8  *bmControls;
330 			u8  bmVideoStandards;
331 		} processing;
332 
333 		struct {
334 		} selector;
335 
336 		struct {
337 			u8  guidExtensionCode[16];
338 			u8  bNumControls;
339 			u8  bControlSize;
340 			u8  *bmControls;
341 			u8  *bmControlsType;
342 		} extension;
343 	};
344 
345 	u8 bNrInPins;
346 	u8 *baSourceID;
347 
348 	unsigned int ncontrols;
349 	struct uvc_control *controls;
350 };
351 
352 struct uvc_frame {
353 	u8  bFrameIndex;
354 	u8  bmCapabilities;
355 	u16 wWidth;
356 	u16 wHeight;
357 	u32 dwMinBitRate;
358 	u32 dwMaxBitRate;
359 	u32 dwMaxVideoFrameBufferSize;
360 	u8  bFrameIntervalType;
361 	u32 dwDefaultFrameInterval;
362 	u32 *dwFrameInterval;
363 };
364 
365 struct uvc_format {
366 	u8 type;
367 	u8 index;
368 	u8 bpp;
369 	u8 colorspace;
370 	u32 fcc;
371 	u32 flags;
372 
373 	char name[32];
374 
375 	unsigned int nframes;
376 	struct uvc_frame *frame;
377 };
378 
379 struct uvc_streaming_header {
380 	u8 bNumFormats;
381 	u8 bEndpointAddress;
382 	u8 bTerminalLink;
383 	u8 bControlSize;
384 	u8 *bmaControls;
385 	/* The following fields are used by input headers only. */
386 	u8 bmInfo;
387 	u8 bStillCaptureMethod;
388 	u8 bTriggerSupport;
389 	u8 bTriggerUsage;
390 };
391 
392 enum uvc_buffer_state {
393 	UVC_BUF_STATE_IDLE	= 0,
394 	UVC_BUF_STATE_QUEUED	= 1,
395 	UVC_BUF_STATE_ACTIVE	= 2,
396 	UVC_BUF_STATE_READY	= 3,
397 	UVC_BUF_STATE_DONE	= 4,
398 	UVC_BUF_STATE_ERROR	= 5,
399 };
400 
401 struct uvc_buffer {
402 	struct vb2_v4l2_buffer buf;
403 	struct list_head queue;
404 
405 	enum uvc_buffer_state state;
406 	unsigned int error;
407 
408 	void *mem;
409 	unsigned int length;
410 	unsigned int bytesused;
411 
412 	u32 pts;
413 };
414 
415 #define UVC_QUEUE_DISCONNECTED		(1 << 0)
416 #define UVC_QUEUE_DROP_CORRUPTED	(1 << 1)
417 
418 struct uvc_video_queue {
419 	struct vb2_queue queue;
420 	struct mutex mutex;			/* Protects queue */
421 
422 	unsigned int flags;
423 	unsigned int buf_used;
424 
425 	spinlock_t irqlock;			/* Protects irqqueue */
426 	struct list_head irqqueue;
427 };
428 
429 struct uvc_video_chain {
430 	struct uvc_device *dev;
431 	struct list_head list;
432 
433 	struct list_head entities;		/* All entities */
434 	struct uvc_entity *processing;		/* Processing unit */
435 	struct uvc_entity *selector;		/* Selector unit */
436 
437 	struct mutex ctrl_mutex;		/* Protects ctrl.info */
438 
439 	struct v4l2_prio_state prio;		/* V4L2 priority state */
440 	u32 caps;				/* V4L2 chain-wide caps */
441 };
442 
443 struct uvc_stats_frame {
444 	unsigned int size;		/* Number of bytes captured */
445 	unsigned int first_data;	/* Index of the first non-empty packet */
446 
447 	unsigned int nb_packets;	/* Number of packets */
448 	unsigned int nb_empty;		/* Number of empty packets */
449 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
450 	unsigned int nb_errors;		/* Number of packets with the error bit set */
451 
452 	unsigned int nb_pts;		/* Number of packets with a PTS timestamp */
453 	unsigned int nb_pts_diffs;	/* Number of PTS differences inside a frame */
454 	unsigned int last_pts_diff;	/* Index of the last PTS difference */
455 	bool has_initial_pts;		/* Whether the first non-empty packet has a PTS */
456 	bool has_early_pts;		/* Whether a PTS is present before the first non-empty packet */
457 	u32 pts;			/* PTS of the last packet */
458 
459 	unsigned int nb_scr;		/* Number of packets with a SCR timestamp */
460 	unsigned int nb_scr_diffs;	/* Number of SCR.STC differences inside a frame */
461 	u16 scr_sof;			/* SCR.SOF of the last packet */
462 	u32 scr_stc;			/* SCR.STC of the last packet */
463 };
464 
465 struct uvc_stats_stream {
466 	ktime_t start_ts;		/* Stream start timestamp */
467 	ktime_t stop_ts;		/* Stream stop timestamp */
468 
469 	unsigned int nb_frames;		/* Number of frames */
470 
471 	unsigned int nb_packets;	/* Number of packets */
472 	unsigned int nb_empty;		/* Number of empty packets */
473 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
474 	unsigned int nb_errors;		/* Number of packets with the error bit set */
475 
476 	unsigned int nb_pts_constant;	/* Number of frames with constant PTS */
477 	unsigned int nb_pts_early;	/* Number of frames with early PTS */
478 	unsigned int nb_pts_initial;	/* Number of frames with initial PTS */
479 
480 	unsigned int nb_scr_count_ok;	/* Number of frames with at least one SCR per non empty packet */
481 	unsigned int nb_scr_diffs_ok;	/* Number of frames with varying SCR.STC */
482 	unsigned int scr_sof_count;	/* STC.SOF counter accumulated since stream start */
483 	unsigned int scr_sof;		/* STC.SOF of the last packet */
484 	unsigned int min_sof;		/* Minimum STC.SOF value */
485 	unsigned int max_sof;		/* Maximum STC.SOF value */
486 };
487 
488 #define UVC_METATADA_BUF_SIZE 1024
489 
490 struct uvc_streaming {
491 	struct list_head list;
492 	struct uvc_device *dev;
493 	struct video_device vdev;
494 	struct uvc_video_chain *chain;
495 	atomic_t active;
496 
497 	struct usb_interface *intf;
498 	int intfnum;
499 	u16 maxpsize;
500 
501 	struct uvc_streaming_header header;
502 	enum v4l2_buf_type type;
503 
504 	unsigned int nformats;
505 	struct uvc_format *format;
506 
507 	struct uvc_streaming_control ctrl;
508 	struct uvc_format *def_format;
509 	struct uvc_format *cur_format;
510 	struct uvc_frame *cur_frame;
511 
512 	/* Protect access to ctrl, cur_format, cur_frame and hardware video
513 	 * probe control.
514 	 */
515 	struct mutex mutex;
516 
517 	/* Buffers queue. */
518 	unsigned int frozen : 1;
519 	struct uvc_video_queue queue;
520 	void (*decode) (struct urb *urb, struct uvc_streaming *video,
521 			struct uvc_buffer *buf, struct uvc_buffer *meta_buf);
522 
523 	struct {
524 		struct video_device vdev;
525 		struct uvc_video_queue queue;
526 		u32 format;
527 	} meta;
528 
529 	/* Context data used by the bulk completion handler. */
530 	struct {
531 		u8 header[256];
532 		unsigned int header_size;
533 		int skip_payload;
534 		u32 payload_size;
535 		u32 max_payload_size;
536 	} bulk;
537 
538 	struct urb *urb[UVC_URBS];
539 	char *urb_buffer[UVC_URBS];
540 	dma_addr_t urb_dma[UVC_URBS];
541 	unsigned int urb_size;
542 
543 	u32 sequence;
544 	u8 last_fid;
545 
546 	/* debugfs */
547 	struct dentry *debugfs_dir;
548 	struct {
549 		struct uvc_stats_frame frame;
550 		struct uvc_stats_stream stream;
551 	} stats;
552 
553 	/* Timestamps support. */
554 	struct uvc_clock {
555 		struct uvc_clock_sample {
556 			u32 dev_stc;
557 			u16 dev_sof;
558 			u16 host_sof;
559 			ktime_t host_time;
560 		} *samples;
561 
562 		unsigned int head;
563 		unsigned int count;
564 		unsigned int size;
565 
566 		u16 last_sof;
567 		u16 sof_offset;
568 
569 		u8 last_scr[6];
570 
571 		spinlock_t lock;
572 	} clock;
573 };
574 
575 struct uvc_device {
576 	struct usb_device *udev;
577 	struct usb_interface *intf;
578 	unsigned long warnings;
579 	u32 quirks;
580 	u32 meta_format;
581 	int intfnum;
582 	char name[32];
583 
584 	struct mutex lock;		/* Protects users */
585 	unsigned int users;
586 	atomic_t nmappings;
587 
588 	/* Video control interface */
589 #ifdef CONFIG_MEDIA_CONTROLLER
590 	struct media_device mdev;
591 #endif
592 	struct v4l2_device vdev;
593 	u16 uvc_version;
594 	u32 clock_frequency;
595 
596 	struct list_head entities;
597 	struct list_head chains;
598 
599 	/* Video Streaming interfaces */
600 	struct list_head streams;
601 	struct kref ref;
602 
603 	/* Status Interrupt Endpoint */
604 	struct usb_host_endpoint *int_ep;
605 	struct urb *int_urb;
606 	u8 *status;
607 	struct input_dev *input;
608 	char input_phys[64];
609 
610 	struct uvc_ctrl_work {
611 		struct work_struct work;
612 		struct urb *urb;
613 		struct uvc_video_chain *chain;
614 		struct uvc_control *ctrl;
615 		const void *data;
616 	} async_ctrl;
617 };
618 
619 enum uvc_handle_state {
620 	UVC_HANDLE_PASSIVE	= 0,
621 	UVC_HANDLE_ACTIVE	= 1,
622 };
623 
624 struct uvc_fh {
625 	struct v4l2_fh vfh;
626 	struct uvc_video_chain *chain;
627 	struct uvc_streaming *stream;
628 	enum uvc_handle_state state;
629 };
630 
631 struct uvc_driver {
632 	struct usb_driver driver;
633 };
634 
635 /* ------------------------------------------------------------------------
636  * Debugging, printing and logging
637  */
638 
639 #define UVC_TRACE_PROBE		(1 << 0)
640 #define UVC_TRACE_DESCR		(1 << 1)
641 #define UVC_TRACE_CONTROL	(1 << 2)
642 #define UVC_TRACE_FORMAT	(1 << 3)
643 #define UVC_TRACE_CAPTURE	(1 << 4)
644 #define UVC_TRACE_CALLS		(1 << 5)
645 #define UVC_TRACE_FRAME		(1 << 7)
646 #define UVC_TRACE_SUSPEND	(1 << 8)
647 #define UVC_TRACE_STATUS	(1 << 9)
648 #define UVC_TRACE_VIDEO		(1 << 10)
649 #define UVC_TRACE_STATS		(1 << 11)
650 #define UVC_TRACE_CLOCK		(1 << 12)
651 
652 #define UVC_WARN_MINMAX		0
653 #define UVC_WARN_PROBE_DEF	1
654 #define UVC_WARN_XU_GET_RES	2
655 
656 extern unsigned int uvc_clock_param;
657 extern unsigned int uvc_no_drop_param;
658 extern unsigned int uvc_trace_param;
659 extern unsigned int uvc_timeout_param;
660 extern unsigned int uvc_hw_timestamps_param;
661 
662 #define uvc_trace(flag, msg...) \
663 	do { \
664 		if (uvc_trace_param & flag) \
665 			printk(KERN_DEBUG "uvcvideo: " msg); \
666 	} while (0)
667 
668 #define uvc_warn_once(dev, warn, msg...) \
669 	do { \
670 		if (!test_and_set_bit(warn, &dev->warnings)) \
671 			printk(KERN_INFO "uvcvideo: " msg); \
672 	} while (0)
673 
674 #define uvc_printk(level, msg...) \
675 	printk(level "uvcvideo: " msg)
676 
677 /* --------------------------------------------------------------------------
678  * Internal functions.
679  */
680 
681 /* Core driver */
682 extern struct uvc_driver uvc_driver;
683 
684 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
685 
686 /* Video buffers queue management. */
687 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
688 		   int drop_corrupted);
689 void uvc_queue_release(struct uvc_video_queue *queue);
690 int uvc_request_buffers(struct uvc_video_queue *queue,
691 			struct v4l2_requestbuffers *rb);
692 int uvc_query_buffer(struct uvc_video_queue *queue,
693 		     struct v4l2_buffer *v4l2_buf);
694 int uvc_create_buffers(struct uvc_video_queue *queue,
695 		       struct v4l2_create_buffers *v4l2_cb);
696 int uvc_queue_buffer(struct uvc_video_queue *queue,
697 		     struct v4l2_buffer *v4l2_buf);
698 int uvc_export_buffer(struct uvc_video_queue *queue,
699 		      struct v4l2_exportbuffer *exp);
700 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
701 		       struct v4l2_buffer *v4l2_buf, int nonblocking);
702 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
703 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
704 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
705 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
706 					 struct uvc_buffer *buf);
707 int uvc_queue_mmap(struct uvc_video_queue *queue,
708 		   struct vm_area_struct *vma);
709 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
710 			poll_table *wait);
711 #ifndef CONFIG_MMU
712 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
713 					  unsigned long pgoff);
714 #endif
715 int uvc_queue_allocated(struct uvc_video_queue *queue);
716 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
717 {
718 	return vb2_is_streaming(&queue->queue);
719 }
720 
721 /* V4L2 interface */
722 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
723 extern const struct v4l2_file_operations uvc_fops;
724 
725 /* Media controller */
726 int uvc_mc_register_entities(struct uvc_video_chain *chain);
727 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
728 
729 /* Video */
730 int uvc_video_init(struct uvc_streaming *stream);
731 int uvc_video_suspend(struct uvc_streaming *stream);
732 int uvc_video_resume(struct uvc_streaming *stream, int reset);
733 int uvc_video_enable(struct uvc_streaming *stream, int enable);
734 int uvc_probe_video(struct uvc_streaming *stream,
735 		    struct uvc_streaming_control *probe);
736 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
737 		   u8 intfnum, u8 cs, void *data, u16 size);
738 void uvc_video_clock_update(struct uvc_streaming *stream,
739 			    struct vb2_v4l2_buffer *vbuf,
740 			    struct uvc_buffer *buf);
741 int uvc_meta_register(struct uvc_streaming *stream);
742 
743 int uvc_register_video_device(struct uvc_device *dev,
744 			      struct uvc_streaming *stream,
745 			      struct video_device *vdev,
746 			      struct uvc_video_queue *queue,
747 			      enum v4l2_buf_type type,
748 			      const struct v4l2_file_operations *fops,
749 			      const struct v4l2_ioctl_ops *ioctl_ops);
750 
751 /* Status */
752 int uvc_status_init(struct uvc_device *dev);
753 void uvc_status_cleanup(struct uvc_device *dev);
754 int uvc_status_start(struct uvc_device *dev, gfp_t flags);
755 void uvc_status_stop(struct uvc_device *dev);
756 
757 /* Controls */
758 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
759 
760 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
761 			struct v4l2_queryctrl *v4l2_ctrl);
762 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
763 			struct v4l2_querymenu *query_menu);
764 
765 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
766 			 const struct uvc_control_mapping *mapping);
767 int uvc_ctrl_init_device(struct uvc_device *dev);
768 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
769 int uvc_ctrl_restore_values(struct uvc_device *dev);
770 bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
771 			   struct uvc_control *ctrl, const u8 *data);
772 
773 int uvc_ctrl_begin(struct uvc_video_chain *chain);
774 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
775 		      const struct v4l2_ext_control *xctrls,
776 		      unsigned int xctrls_count);
777 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
778 				  const struct v4l2_ext_control *xctrls,
779 				  unsigned int xctrls_count)
780 {
781 	return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
782 }
783 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
784 {
785 	return __uvc_ctrl_commit(handle, 1, NULL, 0);
786 }
787 
788 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
789 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
790 
791 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
792 		      struct uvc_xu_control_query *xqry);
793 
794 /* Utility functions */
795 void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
796 			   unsigned int n_terms, unsigned int threshold);
797 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator);
798 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
799 					    u8 epaddr);
800 
801 /* Quirks support */
802 void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
803 			     struct uvc_buffer *buf,
804 			     struct uvc_buffer *meta_buf);
805 
806 /* debugfs and statistics */
807 void uvc_debugfs_init(void);
808 void uvc_debugfs_cleanup(void);
809 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
810 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
811 
812 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
813 			    size_t size);
814 
815 #endif
816