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