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