xref: /openbmc/linux/drivers/media/usb/uvc/uvcvideo.h (revision b830f94f)
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 #define UVC_GUID_FORMAT_CNF4 \
158 	{ 'C',  ' ',  ' ',  ' ', 0x00, 0x00, 0x10, 0x00, \
159 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
160 
161 #define UVC_GUID_FORMAT_D3DFMT_L8 \
162 	{0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, \
163 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
164 #define UVC_GUID_FORMAT_KSMEDIA_L8_IR \
165 	{0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, \
166 	 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
167 
168 
169 /* ------------------------------------------------------------------------
170  * Driver specific constants.
171  */
172 
173 #define DRIVER_VERSION		"1.1.1"
174 
175 /* Number of isochronous URBs. */
176 #define UVC_URBS		5
177 /* Maximum number of packets per URB. */
178 #define UVC_MAX_PACKETS		32
179 /* Maximum status buffer size in bytes of interrupt URB. */
180 #define UVC_MAX_STATUS_SIZE	16
181 
182 #define UVC_CTRL_CONTROL_TIMEOUT	500
183 #define UVC_CTRL_STREAMING_TIMEOUT	5000
184 
185 /* Maximum allowed number of control mappings per device */
186 #define UVC_MAX_CONTROL_MAPPINGS	1024
187 #define UVC_MAX_CONTROL_MENU_ENTRIES	32
188 
189 /* Devices quirks */
190 #define UVC_QUIRK_STATUS_INTERVAL	0x00000001
191 #define UVC_QUIRK_PROBE_MINMAX		0x00000002
192 #define UVC_QUIRK_PROBE_EXTRAFIELDS	0x00000004
193 #define UVC_QUIRK_BUILTIN_ISIGHT	0x00000008
194 #define UVC_QUIRK_STREAM_NO_FID		0x00000010
195 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT	0x00000020
196 #define UVC_QUIRK_FIX_BANDWIDTH		0x00000080
197 #define UVC_QUIRK_PROBE_DEF		0x00000100
198 #define UVC_QUIRK_RESTRICT_FRAME_RATE	0x00000200
199 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT	0x00000400
200 #define UVC_QUIRK_FORCE_Y8		0x00000800
201 
202 /* Format flags */
203 #define UVC_FMT_FLAG_COMPRESSED		0x00000001
204 #define UVC_FMT_FLAG_STREAM		0x00000002
205 
206 /* ------------------------------------------------------------------------
207  * Structures.
208  */
209 
210 struct uvc_device;
211 
212 /* TODO: Put the most frequently accessed fields at the beginning of
213  * structures to maximize cache efficiency.
214  */
215 struct uvc_control_info {
216 	struct list_head mappings;
217 
218 	u8 entity[16];
219 	u8 index;	/* Bit index in bmControls */
220 	u8 selector;
221 
222 	u16 size;
223 	u32 flags;
224 };
225 
226 struct uvc_control_mapping {
227 	struct list_head list;
228 	struct list_head ev_subs;
229 
230 	u32 id;
231 	u8 name[32];
232 	u8 entity[16];
233 	u8 selector;
234 
235 	u8 size;
236 	u8 offset;
237 	enum v4l2_ctrl_type v4l2_type;
238 	u32 data_type;
239 
240 	const struct uvc_menu_info *menu_info;
241 	u32 menu_count;
242 
243 	u32 master_id;
244 	s32 master_manual;
245 	u32 slave_ids[2];
246 
247 	s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
248 		   const u8 *data);
249 	void (*set)(struct uvc_control_mapping *mapping, s32 value,
250 		    u8 *data);
251 };
252 
253 struct uvc_control {
254 	struct uvc_entity *entity;
255 	struct uvc_control_info info;
256 
257 	u8 index;	/* Used to match the uvc_control entry with a
258 			   uvc_control_info. */
259 	u8 dirty:1,
260 	   loaded:1,
261 	   modified:1,
262 	   cached:1,
263 	   initialized:1;
264 
265 	u8 *uvc_data;
266 
267 	struct uvc_fh *handle;	/* File handle that last changed the control. */
268 };
269 
270 struct uvc_format_desc {
271 	char *name;
272 	u8 guid[16];
273 	u32 fcc;
274 };
275 
276 /* The term 'entity' refers to both UVC units and UVC terminals.
277  *
278  * The type field is either the terminal type (wTerminalType in the terminal
279  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
280  * As the bDescriptorSubtype field is one byte long, the type value will
281  * always have a null MSB for units. All terminal types defined by the UVC
282  * specification have a non-null MSB, so it is safe to use the MSB to
283  * differentiate between units and terminals as long as the descriptor parsing
284  * code makes sure terminal types have a non-null MSB.
285  *
286  * For terminals, the type's most significant bit stores the terminal
287  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
288  * always be accessed with the UVC_ENTITY_* macros and never directly.
289  */
290 
291 #define UVC_ENTITY_FLAG_DEFAULT		(1 << 0)
292 
293 struct uvc_entity {
294 	struct list_head list;		/* Entity as part of a UVC device. */
295 	struct list_head chain;		/* Entity as part of a video device
296 					 * chain. */
297 	unsigned int flags;
298 
299 	u8 id;
300 	u16 type;
301 	char name[64];
302 
303 	/* Media controller-related fields. */
304 	struct video_device *vdev;
305 	struct v4l2_subdev subdev;
306 	unsigned int num_pads;
307 	unsigned int num_links;
308 	struct media_pad *pads;
309 
310 	union {
311 		struct {
312 			u16 wObjectiveFocalLengthMin;
313 			u16 wObjectiveFocalLengthMax;
314 			u16 wOcularFocalLength;
315 			u8  bControlSize;
316 			u8  *bmControls;
317 		} camera;
318 
319 		struct {
320 			u8  bControlSize;
321 			u8  *bmControls;
322 			u8  bTransportModeSize;
323 			u8  *bmTransportModes;
324 		} media;
325 
326 		struct {
327 		} output;
328 
329 		struct {
330 			u16 wMaxMultiplier;
331 			u8  bControlSize;
332 			u8  *bmControls;
333 			u8  bmVideoStandards;
334 		} processing;
335 
336 		struct {
337 		} selector;
338 
339 		struct {
340 			u8  guidExtensionCode[16];
341 			u8  bNumControls;
342 			u8  bControlSize;
343 			u8  *bmControls;
344 			u8  *bmControlsType;
345 		} extension;
346 	};
347 
348 	u8 bNrInPins;
349 	u8 *baSourceID;
350 
351 	unsigned int ncontrols;
352 	struct uvc_control *controls;
353 };
354 
355 struct uvc_frame {
356 	u8  bFrameIndex;
357 	u8  bmCapabilities;
358 	u16 wWidth;
359 	u16 wHeight;
360 	u32 dwMinBitRate;
361 	u32 dwMaxBitRate;
362 	u32 dwMaxVideoFrameBufferSize;
363 	u8  bFrameIntervalType;
364 	u32 dwDefaultFrameInterval;
365 	u32 *dwFrameInterval;
366 };
367 
368 struct uvc_format {
369 	u8 type;
370 	u8 index;
371 	u8 bpp;
372 	u8 colorspace;
373 	u32 fcc;
374 	u32 flags;
375 
376 	char name[32];
377 
378 	unsigned int nframes;
379 	struct uvc_frame *frame;
380 };
381 
382 struct uvc_streaming_header {
383 	u8 bNumFormats;
384 	u8 bEndpointAddress;
385 	u8 bTerminalLink;
386 	u8 bControlSize;
387 	u8 *bmaControls;
388 	/* The following fields are used by input headers only. */
389 	u8 bmInfo;
390 	u8 bStillCaptureMethod;
391 	u8 bTriggerSupport;
392 	u8 bTriggerUsage;
393 };
394 
395 enum uvc_buffer_state {
396 	UVC_BUF_STATE_IDLE	= 0,
397 	UVC_BUF_STATE_QUEUED	= 1,
398 	UVC_BUF_STATE_ACTIVE	= 2,
399 	UVC_BUF_STATE_READY	= 3,
400 	UVC_BUF_STATE_DONE	= 4,
401 	UVC_BUF_STATE_ERROR	= 5,
402 };
403 
404 struct uvc_buffer {
405 	struct vb2_v4l2_buffer buf;
406 	struct list_head queue;
407 
408 	enum uvc_buffer_state state;
409 	unsigned int error;
410 
411 	void *mem;
412 	unsigned int length;
413 	unsigned int bytesused;
414 
415 	u32 pts;
416 
417 	/* Asynchronous buffer handling. */
418 	struct kref ref;
419 };
420 
421 #define UVC_QUEUE_DISCONNECTED		(1 << 0)
422 #define UVC_QUEUE_DROP_CORRUPTED	(1 << 1)
423 
424 struct uvc_video_queue {
425 	struct vb2_queue queue;
426 	struct mutex mutex;			/* Protects queue */
427 
428 	unsigned int flags;
429 	unsigned int buf_used;
430 
431 	spinlock_t irqlock;			/* Protects irqqueue */
432 	struct list_head irqqueue;
433 };
434 
435 struct uvc_video_chain {
436 	struct uvc_device *dev;
437 	struct list_head list;
438 
439 	struct list_head entities;		/* All entities */
440 	struct uvc_entity *processing;		/* Processing unit */
441 	struct uvc_entity *selector;		/* Selector unit */
442 
443 	struct mutex ctrl_mutex;		/* Protects ctrl.info */
444 
445 	struct v4l2_prio_state prio;		/* V4L2 priority state */
446 	u32 caps;				/* V4L2 chain-wide caps */
447 };
448 
449 struct uvc_stats_frame {
450 	unsigned int size;		/* Number of bytes captured */
451 	unsigned int first_data;	/* Index of the first non-empty packet */
452 
453 	unsigned int nb_packets;	/* Number of packets */
454 	unsigned int nb_empty;		/* Number of empty packets */
455 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
456 	unsigned int nb_errors;		/* Number of packets with the error bit set */
457 
458 	unsigned int nb_pts;		/* Number of packets with a PTS timestamp */
459 	unsigned int nb_pts_diffs;	/* Number of PTS differences inside a frame */
460 	unsigned int last_pts_diff;	/* Index of the last PTS difference */
461 	bool has_initial_pts;		/* Whether the first non-empty packet has a PTS */
462 	bool has_early_pts;		/* Whether a PTS is present before the first non-empty packet */
463 	u32 pts;			/* PTS of the last packet */
464 
465 	unsigned int nb_scr;		/* Number of packets with a SCR timestamp */
466 	unsigned int nb_scr_diffs;	/* Number of SCR.STC differences inside a frame */
467 	u16 scr_sof;			/* SCR.SOF of the last packet */
468 	u32 scr_stc;			/* SCR.STC of the last packet */
469 };
470 
471 struct uvc_stats_stream {
472 	ktime_t start_ts;		/* Stream start timestamp */
473 	ktime_t stop_ts;		/* Stream stop timestamp */
474 
475 	unsigned int nb_frames;		/* Number of frames */
476 
477 	unsigned int nb_packets;	/* Number of packets */
478 	unsigned int nb_empty;		/* Number of empty packets */
479 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
480 	unsigned int nb_errors;		/* Number of packets with the error bit set */
481 
482 	unsigned int nb_pts_constant;	/* Number of frames with constant PTS */
483 	unsigned int nb_pts_early;	/* Number of frames with early PTS */
484 	unsigned int nb_pts_initial;	/* Number of frames with initial PTS */
485 
486 	unsigned int nb_scr_count_ok;	/* Number of frames with at least one SCR per non empty packet */
487 	unsigned int nb_scr_diffs_ok;	/* Number of frames with varying SCR.STC */
488 	unsigned int scr_sof_count;	/* STC.SOF counter accumulated since stream start */
489 	unsigned int scr_sof;		/* STC.SOF of the last packet */
490 	unsigned int min_sof;		/* Minimum STC.SOF value */
491 	unsigned int max_sof;		/* Maximum STC.SOF value */
492 };
493 
494 #define UVC_METATADA_BUF_SIZE 1024
495 
496 /**
497  * struct uvc_copy_op: Context structure to schedule asynchronous memcpy
498  *
499  * @buf: active buf object for this operation
500  * @dst: copy destination address
501  * @src: copy source address
502  * @len: copy length
503  */
504 struct uvc_copy_op {
505 	struct uvc_buffer *buf;
506 	void *dst;
507 	const __u8 *src;
508 	size_t len;
509 };
510 
511 /**
512  * struct uvc_urb - URB context management structure
513  *
514  * @urb: the URB described by this context structure
515  * @stream: UVC streaming context
516  * @buffer: memory storage for the URB
517  * @dma: DMA coherent addressing for the urb_buffer
518  * @async_operations: counter to indicate the number of copy operations
519  * @copy_operations: work descriptors for asynchronous copy operations
520  * @work: work queue entry for asynchronous decode
521  */
522 struct uvc_urb {
523 	struct urb *urb;
524 	struct uvc_streaming *stream;
525 
526 	char *buffer;
527 	dma_addr_t dma;
528 
529 	unsigned int async_operations;
530 	struct uvc_copy_op copy_operations[UVC_MAX_PACKETS];
531 	struct work_struct work;
532 };
533 
534 struct uvc_streaming {
535 	struct list_head list;
536 	struct uvc_device *dev;
537 	struct video_device vdev;
538 	struct uvc_video_chain *chain;
539 	atomic_t active;
540 
541 	struct usb_interface *intf;
542 	int intfnum;
543 	u16 maxpsize;
544 
545 	struct uvc_streaming_header header;
546 	enum v4l2_buf_type type;
547 
548 	unsigned int nformats;
549 	struct uvc_format *format;
550 
551 	struct uvc_streaming_control ctrl;
552 	struct uvc_format *def_format;
553 	struct uvc_format *cur_format;
554 	struct uvc_frame *cur_frame;
555 
556 	/* Protect access to ctrl, cur_format, cur_frame and hardware video
557 	 * probe control.
558 	 */
559 	struct mutex mutex;
560 
561 	/* Buffers queue. */
562 	unsigned int frozen : 1;
563 	struct uvc_video_queue queue;
564 	struct workqueue_struct *async_wq;
565 	void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
566 		       struct uvc_buffer *meta_buf);
567 
568 	struct {
569 		struct video_device vdev;
570 		struct uvc_video_queue queue;
571 		u32 format;
572 	} meta;
573 
574 	/* Context data used by the bulk completion handler. */
575 	struct {
576 		u8 header[256];
577 		unsigned int header_size;
578 		int skip_payload;
579 		u32 payload_size;
580 		u32 max_payload_size;
581 	} bulk;
582 
583 	struct uvc_urb uvc_urb[UVC_URBS];
584 	unsigned int urb_size;
585 
586 	u32 sequence;
587 	u8 last_fid;
588 
589 	/* debugfs */
590 	struct dentry *debugfs_dir;
591 	struct {
592 		struct uvc_stats_frame frame;
593 		struct uvc_stats_stream stream;
594 	} stats;
595 
596 	/* Timestamps support. */
597 	struct uvc_clock {
598 		struct uvc_clock_sample {
599 			u32 dev_stc;
600 			u16 dev_sof;
601 			u16 host_sof;
602 			ktime_t host_time;
603 		} *samples;
604 
605 		unsigned int head;
606 		unsigned int count;
607 		unsigned int size;
608 
609 		u16 last_sof;
610 		u16 sof_offset;
611 
612 		u8 last_scr[6];
613 
614 		spinlock_t lock;
615 	} clock;
616 };
617 
618 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \
619 	for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \
620 	     (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \
621 	     ++(uvc_urb))
622 
623 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
624 {
625 	return uvc_urb - &uvc_urb->stream->uvc_urb[0];
626 }
627 
628 struct uvc_device_info {
629 	u32	quirks;
630 	u32	meta_format;
631 };
632 
633 struct uvc_device {
634 	struct usb_device *udev;
635 	struct usb_interface *intf;
636 	unsigned long warnings;
637 	u32 quirks;
638 	int intfnum;
639 	char name[32];
640 
641 	const struct uvc_device_info *info;
642 
643 	struct mutex lock;		/* Protects users */
644 	unsigned int users;
645 	atomic_t nmappings;
646 
647 	/* Video control interface */
648 #ifdef CONFIG_MEDIA_CONTROLLER
649 	struct media_device mdev;
650 #endif
651 	struct v4l2_device vdev;
652 	u16 uvc_version;
653 	u32 clock_frequency;
654 
655 	struct list_head entities;
656 	struct list_head chains;
657 
658 	/* Video Streaming interfaces */
659 	struct list_head streams;
660 	struct kref ref;
661 
662 	/* Status Interrupt Endpoint */
663 	struct usb_host_endpoint *int_ep;
664 	struct urb *int_urb;
665 	u8 *status;
666 	struct input_dev *input;
667 	char input_phys[64];
668 
669 	struct uvc_ctrl_work {
670 		struct work_struct work;
671 		struct urb *urb;
672 		struct uvc_video_chain *chain;
673 		struct uvc_control *ctrl;
674 		const void *data;
675 	} async_ctrl;
676 };
677 
678 enum uvc_handle_state {
679 	UVC_HANDLE_PASSIVE	= 0,
680 	UVC_HANDLE_ACTIVE	= 1,
681 };
682 
683 struct uvc_fh {
684 	struct v4l2_fh vfh;
685 	struct uvc_video_chain *chain;
686 	struct uvc_streaming *stream;
687 	enum uvc_handle_state state;
688 };
689 
690 struct uvc_driver {
691 	struct usb_driver driver;
692 };
693 
694 /* ------------------------------------------------------------------------
695  * Debugging, printing and logging
696  */
697 
698 #define UVC_TRACE_PROBE		(1 << 0)
699 #define UVC_TRACE_DESCR		(1 << 1)
700 #define UVC_TRACE_CONTROL	(1 << 2)
701 #define UVC_TRACE_FORMAT	(1 << 3)
702 #define UVC_TRACE_CAPTURE	(1 << 4)
703 #define UVC_TRACE_CALLS		(1 << 5)
704 #define UVC_TRACE_FRAME		(1 << 7)
705 #define UVC_TRACE_SUSPEND	(1 << 8)
706 #define UVC_TRACE_STATUS	(1 << 9)
707 #define UVC_TRACE_VIDEO		(1 << 10)
708 #define UVC_TRACE_STATS		(1 << 11)
709 #define UVC_TRACE_CLOCK		(1 << 12)
710 
711 #define UVC_WARN_MINMAX		0
712 #define UVC_WARN_PROBE_DEF	1
713 #define UVC_WARN_XU_GET_RES	2
714 
715 extern unsigned int uvc_clock_param;
716 extern unsigned int uvc_no_drop_param;
717 extern unsigned int uvc_trace_param;
718 extern unsigned int uvc_timeout_param;
719 extern unsigned int uvc_hw_timestamps_param;
720 
721 #define uvc_trace(flag, msg...) \
722 	do { \
723 		if (uvc_trace_param & flag) \
724 			printk(KERN_DEBUG "uvcvideo: " msg); \
725 	} while (0)
726 
727 #define uvc_warn_once(dev, warn, msg...) \
728 	do { \
729 		if (!test_and_set_bit(warn, &dev->warnings)) \
730 			printk(KERN_INFO "uvcvideo: " msg); \
731 	} while (0)
732 
733 #define uvc_printk(level, msg...) \
734 	printk(level "uvcvideo: " msg)
735 
736 /* --------------------------------------------------------------------------
737  * Internal functions.
738  */
739 
740 /* Core driver */
741 extern struct uvc_driver uvc_driver;
742 
743 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
744 
745 /* Video buffers queue management. */
746 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
747 		   int drop_corrupted);
748 void uvc_queue_release(struct uvc_video_queue *queue);
749 int uvc_request_buffers(struct uvc_video_queue *queue,
750 			struct v4l2_requestbuffers *rb);
751 int uvc_query_buffer(struct uvc_video_queue *queue,
752 		     struct v4l2_buffer *v4l2_buf);
753 int uvc_create_buffers(struct uvc_video_queue *queue,
754 		       struct v4l2_create_buffers *v4l2_cb);
755 int uvc_queue_buffer(struct uvc_video_queue *queue,
756 		     struct media_device *mdev,
757 		     struct v4l2_buffer *v4l2_buf);
758 int uvc_export_buffer(struct uvc_video_queue *queue,
759 		      struct v4l2_exportbuffer *exp);
760 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
761 		       struct v4l2_buffer *v4l2_buf, int nonblocking);
762 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
763 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
764 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
765 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
766 					 struct uvc_buffer *buf);
767 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
768 void uvc_queue_buffer_release(struct uvc_buffer *buf);
769 int uvc_queue_mmap(struct uvc_video_queue *queue,
770 		   struct vm_area_struct *vma);
771 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
772 			poll_table *wait);
773 #ifndef CONFIG_MMU
774 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
775 					  unsigned long pgoff);
776 #endif
777 int uvc_queue_allocated(struct uvc_video_queue *queue);
778 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
779 {
780 	return vb2_is_streaming(&queue->queue);
781 }
782 
783 /* V4L2 interface */
784 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
785 extern const struct v4l2_file_operations uvc_fops;
786 
787 /* Media controller */
788 int uvc_mc_register_entities(struct uvc_video_chain *chain);
789 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
790 
791 /* Video */
792 int uvc_video_init(struct uvc_streaming *stream);
793 int uvc_video_suspend(struct uvc_streaming *stream);
794 int uvc_video_resume(struct uvc_streaming *stream, int reset);
795 int uvc_video_start_streaming(struct uvc_streaming *stream);
796 void uvc_video_stop_streaming(struct uvc_streaming *stream);
797 int uvc_probe_video(struct uvc_streaming *stream,
798 		    struct uvc_streaming_control *probe);
799 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
800 		   u8 intfnum, u8 cs, void *data, u16 size);
801 void uvc_video_clock_update(struct uvc_streaming *stream,
802 			    struct vb2_v4l2_buffer *vbuf,
803 			    struct uvc_buffer *buf);
804 int uvc_meta_register(struct uvc_streaming *stream);
805 
806 int uvc_register_video_device(struct uvc_device *dev,
807 			      struct uvc_streaming *stream,
808 			      struct video_device *vdev,
809 			      struct uvc_video_queue *queue,
810 			      enum v4l2_buf_type type,
811 			      const struct v4l2_file_operations *fops,
812 			      const struct v4l2_ioctl_ops *ioctl_ops);
813 
814 /* Status */
815 int uvc_status_init(struct uvc_device *dev);
816 void uvc_status_unregister(struct uvc_device *dev);
817 void uvc_status_cleanup(struct uvc_device *dev);
818 int uvc_status_start(struct uvc_device *dev, gfp_t flags);
819 void uvc_status_stop(struct uvc_device *dev);
820 
821 /* Controls */
822 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
823 
824 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
825 			struct v4l2_queryctrl *v4l2_ctrl);
826 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
827 			struct v4l2_querymenu *query_menu);
828 
829 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
830 			 const struct uvc_control_mapping *mapping);
831 int uvc_ctrl_init_device(struct uvc_device *dev);
832 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
833 int uvc_ctrl_restore_values(struct uvc_device *dev);
834 bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
835 			   struct uvc_control *ctrl, const u8 *data);
836 
837 int uvc_ctrl_begin(struct uvc_video_chain *chain);
838 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
839 		      const struct v4l2_ext_control *xctrls,
840 		      unsigned int xctrls_count);
841 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
842 				  const struct v4l2_ext_control *xctrls,
843 				  unsigned int xctrls_count)
844 {
845 	return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
846 }
847 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
848 {
849 	return __uvc_ctrl_commit(handle, 1, NULL, 0);
850 }
851 
852 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
853 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
854 
855 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
856 		      struct uvc_xu_control_query *xqry);
857 
858 /* Utility functions */
859 void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
860 			   unsigned int n_terms, unsigned int threshold);
861 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator);
862 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
863 					    u8 epaddr);
864 
865 /* Quirks support */
866 void uvc_video_decode_isight(struct uvc_urb *uvc_urb,
867 			     struct uvc_buffer *buf,
868 			     struct uvc_buffer *meta_buf);
869 
870 /* debugfs and statistics */
871 void uvc_debugfs_init(void);
872 void uvc_debugfs_cleanup(void);
873 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
874 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
875 
876 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
877 			    size_t size);
878 
879 #endif
880