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