xref: /linux/drivers/media/usb/uvc/uvcvideo.h (revision 23ca32e4ead48f68e37000f2552b973ef1439acb)
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/atomic.h>
10 #include <linux/kernel.h>
11 #include <linux/poll.h>
12 #include <linux/usb.h>
13 #include <linux/usb/video.h>
14 #include <linux/uvcvideo.h>
15 #include <linux/videodev2.h>
16 #include <linux/workqueue.h>
17 #include <media/media-device.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-fh.h>
21 #include <media/videobuf2-v4l2.h>
22 
23 /* --------------------------------------------------------------------------
24  * UVC constants
25  */
26 
27 #define UVC_TERM_INPUT			0x0000
28 #define UVC_TERM_OUTPUT			0x8000
29 #define UVC_TERM_DIRECTION(term)	((term)->type & 0x8000)
30 
31 #define UVC_ENTITY_TYPE(entity)		((entity)->type & 0x7fff)
32 #define UVC_ENTITY_IS_UNIT(entity)	(((entity)->type & 0xff00) == 0)
33 #define UVC_ENTITY_IS_TERM(entity)	(((entity)->type & 0xff00) != 0)
34 #define UVC_ENTITY_IS_ITERM(entity) \
35 	(UVC_ENTITY_IS_TERM(entity) && \
36 	((entity)->type & 0x8000) == UVC_TERM_INPUT)
37 #define UVC_ENTITY_IS_OTERM(entity) \
38 	(UVC_ENTITY_IS_TERM(entity) && \
39 	((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
40 
41 #define UVC_EXT_GPIO_UNIT		0x7ffe
42 #define UVC_EXT_GPIO_UNIT_ID		0x100
43 
44 /* ------------------------------------------------------------------------
45  * Driver specific constants.
46  */
47 
48 #define DRIVER_VERSION		"1.1.1"
49 
50 /* Number of isochronous URBs. */
51 #define UVC_URBS		5
52 /* Maximum number of packets per URB. */
53 #define UVC_MAX_PACKETS		32
54 
55 #define UVC_CTRL_CONTROL_TIMEOUT	5000
56 #define UVC_CTRL_STREAMING_TIMEOUT	5000
57 
58 /* Maximum allowed number of control mappings per device */
59 #define UVC_MAX_CONTROL_MAPPINGS	1024
60 #define UVC_MAX_CONTROL_MENU_ENTRIES	32
61 
62 /* Devices quirks */
63 #define UVC_QUIRK_STATUS_INTERVAL	0x00000001
64 #define UVC_QUIRK_PROBE_MINMAX		0x00000002
65 #define UVC_QUIRK_PROBE_EXTRAFIELDS	0x00000004
66 #define UVC_QUIRK_BUILTIN_ISIGHT	0x00000008
67 #define UVC_QUIRK_STREAM_NO_FID		0x00000010
68 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT	0x00000020
69 #define UVC_QUIRK_FIX_BANDWIDTH		0x00000080
70 #define UVC_QUIRK_PROBE_DEF		0x00000100
71 #define UVC_QUIRK_RESTRICT_FRAME_RATE	0x00000200
72 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT	0x00000400
73 #define UVC_QUIRK_FORCE_Y8		0x00000800
74 #define UVC_QUIRK_FORCE_BPP		0x00001000
75 #define UVC_QUIRK_WAKE_AUTOSUSPEND	0x00002000
76 #define UVC_QUIRK_NO_RESET_RESUME	0x00004000
77 #define UVC_QUIRK_DISABLE_AUTOSUSPEND	0x00008000
78 #define UVC_QUIRK_INVALID_DEVICE_SOF	0x00010000
79 #define UVC_QUIRK_MJPEG_NO_EOF		0x00020000
80 
81 /* Format flags */
82 #define UVC_FMT_FLAG_COMPRESSED		0x00000001
83 #define UVC_FMT_FLAG_STREAM		0x00000002
84 
85 /* ------------------------------------------------------------------------
86  * Structures.
87  */
88 
89 struct gpio_desc;
90 struct sg_table;
91 struct uvc_control;
92 struct uvc_device;
93 struct uvc_video_chain;
94 
95 /*
96  * TODO: Put the most frequently accessed fields at the beginning of
97  * structures to maximize cache efficiency.
98  */
99 struct uvc_control_info {
100 	struct list_head mappings;
101 
102 	u8 entity[16];
103 	u8 index;	/* Bit index in bmControls */
104 	u8 selector;
105 
106 	u16 size;
107 	u32 flags;
108 };
109 
110 struct uvc_control_mapping {
111 	struct list_head list;
112 	struct list_head ev_subs;
113 
114 	u32 id;
115 	char *name;
116 	u8 entity[16];
117 	u8 selector;
118 
119 	/*
120 	 * Size of the control data in the payload of the UVC control GET and
121 	 * SET requests, expressed in bits.
122 	 */
123 	u8 size;
124 
125 	u8 offset;
126 	enum v4l2_ctrl_type v4l2_type;
127 	u32 data_type;
128 
129 	const u32 *menu_mapping;
130 	const char (*menu_names)[UVC_MENU_NAME_LEN];
131 	unsigned long menu_mask;
132 
133 	u32 master_id;
134 	s32 master_manual;
135 	u32 slave_ids[2];
136 
137 	const struct uvc_control_mapping *(*filter_mapping)
138 				(struct uvc_video_chain *chain,
139 				struct uvc_control *ctrl);
140 	int (*get)(struct uvc_control_mapping *mapping, u8 query,
141 		   const void *uvc_in, size_t v4l2_size, void *v4l2_out);
142 	int (*set)(struct uvc_control_mapping *mapping, size_t v4l2_size,
143 		   const void *v4l2_in, void *uvc_out);
144 };
145 
146 struct uvc_control {
147 	struct uvc_entity *entity;
148 	struct uvc_control_info info;
149 
150 	u8 index;	/* Used to match the uvc_control entry with a uvc_control_info. */
151 	u8 dirty:1,
152 	   loaded:1,
153 	   modified:1,
154 	   cached:1,
155 	   initialized:1;
156 
157 	u8 *uvc_data;
158 
159 	struct uvc_fh *handle;	/* File handle that last changed the control. */
160 };
161 
162 /*
163  * The term 'entity' refers to both UVC units and UVC terminals.
164  *
165  * The type field is either the terminal type (wTerminalType in the terminal
166  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
167  * As the bDescriptorSubtype field is one byte long, the type value will
168  * always have a null MSB for units. All terminal types defined by the UVC
169  * specification have a non-null MSB, so it is safe to use the MSB to
170  * differentiate between units and terminals as long as the descriptor parsing
171  * code makes sure terminal types have a non-null MSB.
172  *
173  * For terminals, the type's most significant bit stores the terminal
174  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
175  * always be accessed with the UVC_ENTITY_* macros and never directly.
176  */
177 
178 #define UVC_ENTITY_FLAG_DEFAULT		(1 << 0)
179 
180 struct uvc_entity {
181 	struct list_head list;		/* Entity as part of a UVC device. */
182 	struct list_head chain;		/* Entity as part of a video device chain. */
183 	unsigned int flags;
184 
185 	/*
186 	 * Entities exposed by the UVC device use IDs 0-255, extra entities
187 	 * implemented by the driver (such as the GPIO entity) use IDs 256 and
188 	 * up.
189 	 */
190 	u16 id;
191 	u16 type;
192 	char name[64];
193 	u8 guid[16];
194 
195 	/* Media controller-related fields. */
196 	struct video_device *vdev;
197 	struct v4l2_subdev subdev;
198 	unsigned int num_pads;
199 	unsigned int num_links;
200 	struct media_pad *pads;
201 
202 	union {
203 		struct {
204 			u16 wObjectiveFocalLengthMin;
205 			u16 wObjectiveFocalLengthMax;
206 			u16 wOcularFocalLength;
207 			u8  bControlSize;
208 			u8  *bmControls;
209 		} camera;
210 
211 		struct {
212 			u8  bControlSize;
213 			u8  *bmControls;
214 			u8  bTransportModeSize;
215 			u8  *bmTransportModes;
216 		} media;
217 
218 		struct {
219 		} output;
220 
221 		struct {
222 			u16 wMaxMultiplier;
223 			u8  bControlSize;
224 			u8  *bmControls;
225 			u8  bmVideoStandards;
226 		} processing;
227 
228 		struct {
229 		} selector;
230 
231 		struct {
232 			u8  bNumControls;
233 			u8  bControlSize;
234 			u8  *bmControls;
235 			u8  *bmControlsType;
236 		} extension;
237 
238 		struct {
239 			u8  bControlSize;
240 			u8  *bmControls;
241 			struct gpio_desc *gpio_privacy;
242 			int irq;
243 			bool initialized;
244 		} gpio;
245 	};
246 
247 	u8 bNrInPins;
248 	u8 *baSourceID;
249 
250 	int (*get_info)(struct uvc_device *dev, struct uvc_entity *entity,
251 			u8 cs, u8 *caps);
252 	int (*get_cur)(struct uvc_device *dev, struct uvc_entity *entity,
253 		       u8 cs, void *data, u16 size);
254 
255 	unsigned int ncontrols;
256 	struct uvc_control *controls;
257 };
258 
259 struct uvc_frame {
260 	u8  bFrameIndex;
261 	u8  bmCapabilities;
262 	u16 wWidth;
263 	u16 wHeight;
264 	u32 dwMinBitRate;
265 	u32 dwMaxBitRate;
266 	u32 dwMaxVideoFrameBufferSize;
267 	u8  bFrameIntervalType;
268 	u32 dwDefaultFrameInterval;
269 	const u32 *dwFrameInterval;
270 };
271 
272 struct uvc_format {
273 	u8 type;
274 	u8 index;
275 	u8 bpp;
276 	enum v4l2_colorspace colorspace;
277 	enum v4l2_xfer_func xfer_func;
278 	enum v4l2_ycbcr_encoding ycbcr_enc;
279 	u32 fcc;
280 	u32 flags;
281 
282 	unsigned int nframes;
283 	const struct uvc_frame *frames;
284 };
285 
286 struct uvc_streaming_header {
287 	u8 bNumFormats;
288 	u8 bEndpointAddress;
289 	u8 bTerminalLink;
290 	u8 bControlSize;
291 	u8 *bmaControls;
292 	/* The following fields are used by input headers only. */
293 	u8 bmInfo;
294 	u8 bStillCaptureMethod;
295 	u8 bTriggerSupport;
296 	u8 bTriggerUsage;
297 };
298 
299 enum uvc_buffer_state {
300 	UVC_BUF_STATE_IDLE	= 0,
301 	UVC_BUF_STATE_QUEUED	= 1,
302 	UVC_BUF_STATE_ACTIVE	= 2,
303 	UVC_BUF_STATE_READY	= 3,
304 	UVC_BUF_STATE_DONE	= 4,
305 	UVC_BUF_STATE_ERROR	= 5,
306 };
307 
308 struct uvc_buffer {
309 	struct vb2_v4l2_buffer buf;
310 	struct list_head queue;
311 
312 	enum uvc_buffer_state state;
313 	unsigned int error;
314 
315 	void *mem;
316 	unsigned int length;
317 	unsigned int bytesused;
318 
319 	u32 pts;
320 
321 	/* Asynchronous buffer handling. */
322 	struct kref ref;
323 };
324 
325 #define UVC_QUEUE_DISCONNECTED		(1 << 0)
326 
327 struct uvc_video_queue {
328 	struct vb2_queue queue;
329 	struct mutex mutex;			/* Protects queue */
330 
331 	unsigned int flags;
332 	unsigned int buf_used;
333 
334 	spinlock_t irqlock;			/* Protects irqqueue */
335 	struct list_head irqqueue;
336 };
337 
338 struct uvc_video_chain {
339 	struct uvc_device *dev;
340 	struct list_head list;
341 
342 	struct list_head entities;		/* All entities */
343 	struct uvc_entity *processing;		/* Processing unit */
344 	struct uvc_entity *selector;		/* Selector unit */
345 
346 	struct mutex ctrl_mutex;		/*
347 						 * Protects ctrl.info,
348 						 * ctrl.handle and
349 						 * uvc_fh.pending_async_ctrls
350 						 */
351 
352 	struct v4l2_prio_state prio;		/* V4L2 priority state */
353 	u32 caps;				/* V4L2 chain-wide caps */
354 	u8 ctrl_class_bitmap;			/* Bitmap of valid classes */
355 };
356 
357 struct uvc_stats_frame {
358 	unsigned int size;		/* Number of bytes captured */
359 	unsigned int first_data;	/* Index of the first non-empty packet */
360 
361 	unsigned int nb_packets;	/* Number of packets */
362 	unsigned int nb_empty;		/* Number of empty packets */
363 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
364 	unsigned int nb_errors;		/* Number of packets with the error bit set */
365 
366 	unsigned int nb_pts;		/* Number of packets with a PTS timestamp */
367 	unsigned int nb_pts_diffs;	/* Number of PTS differences inside a frame */
368 	unsigned int last_pts_diff;	/* Index of the last PTS difference */
369 	bool has_initial_pts;		/* Whether the first non-empty packet has a PTS */
370 	bool has_early_pts;		/* Whether a PTS is present before the first non-empty packet */
371 	u32 pts;			/* PTS of the last packet */
372 
373 	unsigned int nb_scr;		/* Number of packets with a SCR timestamp */
374 	unsigned int nb_scr_diffs;	/* Number of SCR.STC differences inside a frame */
375 	u16 scr_sof;			/* SCR.SOF of the last packet */
376 	u32 scr_stc;			/* SCR.STC of the last packet */
377 };
378 
379 struct uvc_stats_stream {
380 	ktime_t start_ts;		/* Stream start timestamp */
381 	ktime_t stop_ts;		/* Stream stop timestamp */
382 
383 	unsigned int nb_frames;		/* Number of frames */
384 
385 	unsigned int nb_packets;	/* Number of packets */
386 	unsigned int nb_empty;		/* Number of empty packets */
387 	unsigned int nb_invalid;	/* Number of packets with an invalid header */
388 	unsigned int nb_errors;		/* Number of packets with the error bit set */
389 
390 	unsigned int nb_pts_constant;	/* Number of frames with constant PTS */
391 	unsigned int nb_pts_early;	/* Number of frames with early PTS */
392 	unsigned int nb_pts_initial;	/* Number of frames with initial PTS */
393 
394 	unsigned int nb_scr_count_ok;	/* Number of frames with at least one SCR per non empty packet */
395 	unsigned int nb_scr_diffs_ok;	/* Number of frames with varying SCR.STC */
396 	unsigned int scr_sof_count;	/* STC.SOF counter accumulated since stream start */
397 	unsigned int scr_sof;		/* STC.SOF of the last packet */
398 	unsigned int min_sof;		/* Minimum STC.SOF value */
399 	unsigned int max_sof;		/* Maximum STC.SOF value */
400 };
401 
402 #define UVC_METADATA_BUF_SIZE 10240
403 
404 /**
405  * struct uvc_copy_op: Context structure to schedule asynchronous memcpy
406  *
407  * @buf: active buf object for this operation
408  * @dst: copy destination address
409  * @src: copy source address
410  * @len: copy length
411  */
412 struct uvc_copy_op {
413 	struct uvc_buffer *buf;
414 	void *dst;
415 	const __u8 *src;
416 	size_t len;
417 };
418 
419 /**
420  * struct uvc_urb - URB context management structure
421  *
422  * @urb: the URB described by this context structure
423  * @stream: UVC streaming context
424  * @buffer: memory storage for the URB
425  * @dma: Allocated DMA handle
426  * @sgt: sgt_table with the urb locations in memory
427  * @async_operations: counter to indicate the number of copy operations
428  * @copy_operations: work descriptors for asynchronous copy operations
429  * @work: work queue entry for asynchronous decode
430  */
431 struct uvc_urb {
432 	struct urb *urb;
433 	struct uvc_streaming *stream;
434 
435 	char *buffer;
436 	dma_addr_t dma;
437 	struct sg_table *sgt;
438 
439 	unsigned int async_operations;
440 	struct uvc_copy_op copy_operations[UVC_MAX_PACKETS];
441 	struct work_struct work;
442 };
443 
444 struct uvc_streaming {
445 	struct list_head list;
446 	struct uvc_device *dev;
447 	struct video_device vdev;
448 	struct uvc_video_chain *chain;
449 	atomic_t active;
450 
451 	struct usb_interface *intf;
452 	int intfnum;
453 	u16 maxpsize;
454 
455 	struct uvc_streaming_header header;
456 	enum v4l2_buf_type type;
457 
458 	unsigned int nformats;
459 	const struct uvc_format *formats;
460 
461 	struct uvc_streaming_control ctrl;
462 	const struct uvc_format *def_format;
463 	const struct uvc_format *cur_format;
464 	const struct uvc_frame *cur_frame;
465 
466 	/*
467 	 * Protect access to ctrl, cur_format, cur_frame and hardware video
468 	 * probe control.
469 	 */
470 	struct mutex mutex;
471 
472 	/* Buffers queue. */
473 	unsigned int frozen : 1;
474 	struct uvc_video_queue queue;
475 	struct workqueue_struct *async_wq;
476 	void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
477 		       struct uvc_buffer *meta_buf);
478 
479 	struct {
480 		struct video_device vdev;
481 		struct uvc_video_queue queue;
482 		u32 format;
483 	} meta;
484 
485 	/* Context data used by the bulk completion handler. */
486 	struct {
487 		u8 header[256];
488 		unsigned int header_size;
489 		int skip_payload;
490 		u32 payload_size;
491 		u32 max_payload_size;
492 	} bulk;
493 
494 	struct uvc_urb uvc_urb[UVC_URBS];
495 	unsigned int urb_size;
496 
497 	u32 sequence;
498 	u8 last_fid;
499 
500 	/* debugfs */
501 	struct dentry *debugfs_dir;
502 	struct {
503 		struct uvc_stats_frame frame;
504 		struct uvc_stats_stream stream;
505 	} stats;
506 
507 	/* Timestamps support. */
508 	struct uvc_clock {
509 		struct uvc_clock_sample {
510 			u32 dev_stc;
511 			u16 dev_sof;
512 			u16 host_sof;
513 			ktime_t host_time;
514 		} *samples;
515 
516 		unsigned int head;
517 		unsigned int count;
518 		unsigned int size;
519 		unsigned int last_sof_overflow;
520 
521 		u16 last_sof;
522 		u16 sof_offset;
523 
524 		u8 last_scr[6];
525 
526 		spinlock_t lock;
527 	} clock;
528 };
529 
530 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \
531 	for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \
532 	     (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \
533 	     ++(uvc_urb))
534 
535 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
536 {
537 	return uvc_urb - &uvc_urb->stream->uvc_urb[0];
538 }
539 
540 struct uvc_device_info {
541 	u32	quirks;
542 	u32	meta_format;
543 	u16	uvc_version;
544 };
545 
546 struct uvc_rect {
547 	u16 top;
548 	u16 left;
549 	u16 bottom;
550 	u16 right;
551 } __packed;
552 
553 struct uvc_status_streaming {
554 	u8	button;
555 } __packed;
556 
557 struct uvc_status_control {
558 	u8	bSelector;
559 	u8	bAttribute;
560 	u8	bValue[11];
561 } __packed;
562 
563 struct uvc_status {
564 	u8	bStatusType;
565 	u8	bOriginator;
566 	u8	bEvent;
567 	union {
568 		struct uvc_status_control control;
569 		struct uvc_status_streaming streaming;
570 	};
571 } __packed;
572 
573 struct uvc_device {
574 	struct usb_device *udev;
575 	struct usb_interface *intf;
576 	unsigned long warnings;
577 	u32 quirks;
578 	int intfnum;
579 	char name[32];
580 
581 	const struct uvc_device_info *info;
582 
583 	atomic_t nmappings;
584 
585 	/* Video control interface */
586 #ifdef CONFIG_MEDIA_CONTROLLER
587 	struct media_device mdev;
588 #endif
589 	struct v4l2_device vdev;
590 	u16 uvc_version;
591 	u32 clock_frequency;
592 
593 	struct list_head entities;
594 	struct list_head chains;
595 
596 	/* Video Streaming interfaces */
597 	struct list_head streams;
598 	struct kref ref;
599 
600 	/* Status Interrupt Endpoint */
601 	struct usb_host_endpoint *int_ep;
602 	struct urb *int_urb;
603 	struct uvc_status *status;
604 	struct mutex status_lock; /* Protects status_users */
605 	unsigned int status_users;
606 	bool flush_status;
607 
608 	struct input_dev *input;
609 	char input_phys[64];
610 
611 	struct uvc_ctrl_work {
612 		struct work_struct work;
613 		struct urb *urb;
614 		struct uvc_video_chain *chain;
615 		struct uvc_control *ctrl;
616 		const void *data;
617 	} async_ctrl;
618 
619 	struct uvc_entity *gpio_unit;
620 };
621 
622 enum uvc_handle_state {
623 	UVC_HANDLE_PASSIVE	= 0,
624 	UVC_HANDLE_ACTIVE	= 1,
625 };
626 
627 struct uvc_fh {
628 	struct v4l2_fh vfh;
629 	struct uvc_video_chain *chain;
630 	struct uvc_streaming *stream;
631 	enum uvc_handle_state state;
632 	unsigned int pending_async_ctrls;
633 	bool is_streaming;
634 };
635 
636 /* ------------------------------------------------------------------------
637  * Debugging, printing and logging
638  */
639 
640 #define UVC_DBG_PROBE		(1 << 0)
641 #define UVC_DBG_DESCR		(1 << 1)
642 #define UVC_DBG_CONTROL		(1 << 2)
643 #define UVC_DBG_FORMAT		(1 << 3)
644 #define UVC_DBG_CAPTURE		(1 << 4)
645 #define UVC_DBG_CALLS		(1 << 5)
646 #define UVC_DBG_FRAME		(1 << 7)
647 #define UVC_DBG_SUSPEND		(1 << 8)
648 #define UVC_DBG_STATUS		(1 << 9)
649 #define UVC_DBG_VIDEO		(1 << 10)
650 #define UVC_DBG_STATS		(1 << 11)
651 #define UVC_DBG_CLOCK		(1 << 12)
652 
653 #define UVC_WARN_MINMAX		0
654 #define UVC_WARN_PROBE_DEF	1
655 #define UVC_WARN_XU_GET_RES	2
656 
657 extern unsigned int uvc_clock_param;
658 extern unsigned int uvc_no_drop_param;
659 extern unsigned int uvc_dbg_param;
660 extern unsigned int uvc_timeout_param;
661 extern unsigned int uvc_hw_timestamps_param;
662 
663 #define uvc_dbg(_dev, flag, fmt, ...)					\
664 do {									\
665 	if (uvc_dbg_param & UVC_DBG_##flag)				\
666 		dev_printk(KERN_DEBUG, &(_dev)->udev->dev, fmt,		\
667 			   ##__VA_ARGS__);				\
668 } while (0)
669 
670 #define uvc_dbg_cont(flag, fmt, ...)					\
671 do {									\
672 	if (uvc_dbg_param & UVC_DBG_##flag)				\
673 		pr_cont(fmt, ##__VA_ARGS__);				\
674 } while (0)
675 
676 #define uvc_warn_once(_dev, warn, fmt, ...)				\
677 do {									\
678 	if (!test_and_set_bit(warn, &(_dev)->warnings))			\
679 		dev_info(&(_dev)->udev->dev, fmt, ##__VA_ARGS__);	\
680 } while (0)
681 
682 /* --------------------------------------------------------------------------
683  * Internal functions.
684  */
685 
686 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
687 
688 /* Video buffers queue management. */
689 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type);
690 void uvc_queue_release(struct uvc_video_queue *queue);
691 int uvc_request_buffers(struct uvc_video_queue *queue,
692 			struct v4l2_requestbuffers *rb);
693 int uvc_query_buffer(struct uvc_video_queue *queue,
694 		     struct v4l2_buffer *v4l2_buf);
695 int uvc_create_buffers(struct uvc_video_queue *queue,
696 		       struct v4l2_create_buffers *v4l2_cb);
697 int uvc_queue_buffer(struct uvc_video_queue *queue,
698 		     struct media_device *mdev,
699 		     struct v4l2_buffer *v4l2_buf);
700 int uvc_export_buffer(struct uvc_video_queue *queue,
701 		      struct v4l2_exportbuffer *exp);
702 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
703 		       struct v4l2_buffer *v4l2_buf, int nonblocking);
704 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
705 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
706 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
707 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
708 					 struct uvc_buffer *buf);
709 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
710 void uvc_queue_buffer_release(struct uvc_buffer *buf);
711 int uvc_queue_mmap(struct uvc_video_queue *queue,
712 		   struct vm_area_struct *vma);
713 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
714 			poll_table *wait);
715 #ifndef CONFIG_MMU
716 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
717 					  unsigned long pgoff);
718 #endif
719 int uvc_queue_allocated(struct uvc_video_queue *queue);
720 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
721 {
722 	return vb2_is_streaming(&queue->queue);
723 }
724 
725 static inline struct uvc_streaming *
726 uvc_queue_to_stream(struct uvc_video_queue *queue)
727 {
728 	return container_of(queue, struct uvc_streaming, queue);
729 }
730 
731 /* V4L2 interface */
732 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
733 extern const struct v4l2_file_operations uvc_fops;
734 
735 /* Media controller */
736 int uvc_mc_register_entities(struct uvc_video_chain *chain);
737 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
738 
739 /* Video */
740 int uvc_video_init(struct uvc_streaming *stream);
741 int uvc_video_suspend(struct uvc_streaming *stream);
742 int uvc_video_resume(struct uvc_streaming *stream, int reset);
743 int uvc_video_start_streaming(struct uvc_streaming *stream);
744 void uvc_video_stop_streaming(struct uvc_streaming *stream);
745 int uvc_probe_video(struct uvc_streaming *stream,
746 		    struct uvc_streaming_control *probe);
747 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
748 		   u8 intfnum, u8 cs, void *data, u16 size);
749 void uvc_video_clock_update(struct uvc_streaming *stream,
750 			    struct vb2_v4l2_buffer *vbuf,
751 			    struct uvc_buffer *buf);
752 int uvc_meta_register(struct uvc_streaming *stream);
753 
754 int uvc_register_video_device(struct uvc_device *dev,
755 			      struct uvc_streaming *stream,
756 			      struct video_device *vdev,
757 			      struct uvc_video_queue *queue,
758 			      enum v4l2_buf_type type,
759 			      const struct v4l2_file_operations *fops,
760 			      const struct v4l2_ioctl_ops *ioctl_ops);
761 
762 /* Status */
763 int uvc_status_init(struct uvc_device *dev);
764 void uvc_status_unregister(struct uvc_device *dev);
765 void uvc_status_cleanup(struct uvc_device *dev);
766 int uvc_status_resume(struct uvc_device *dev);
767 void uvc_status_suspend(struct uvc_device *dev);
768 int uvc_status_get(struct uvc_device *dev);
769 void uvc_status_put(struct uvc_device *dev);
770 
771 /* PM */
772 int uvc_pm_get(struct uvc_device *dev);
773 void uvc_pm_put(struct uvc_device *dev);
774 
775 /* Controls */
776 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
777 
778 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
779 			struct v4l2_query_ext_ctrl *v4l2_ctrl);
780 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
781 			struct v4l2_querymenu *query_menu);
782 
783 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
784 			 const struct uvc_control_mapping *mapping);
785 int uvc_ctrl_init_device(struct uvc_device *dev);
786 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
787 int uvc_ctrl_restore_values(struct uvc_device *dev);
788 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
789 				 struct uvc_control *ctrl, const u8 *data);
790 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
791 			   struct uvc_control *ctrl, const u8 *data);
792 
793 int uvc_ctrl_begin(struct uvc_video_chain *chain);
794 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
795 		      struct v4l2_ext_controls *ctrls);
796 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
797 				  struct v4l2_ext_controls *ctrls)
798 {
799 	return __uvc_ctrl_commit(handle, 0, ctrls);
800 }
801 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
802 {
803 	return __uvc_ctrl_commit(handle, 1, NULL);
804 }
805 
806 int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which,
807 		 struct v4l2_ext_control *xctrl);
808 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
809 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
810 			   const struct v4l2_ext_controls *ctrls,
811 			   unsigned long ioctl);
812 
813 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
814 		      struct uvc_xu_control_query *xqry);
815 
816 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle);
817 
818 /* Utility functions */
819 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
820 					    u8 epaddr);
821 u16 uvc_endpoint_max_bpi(struct usb_device *dev, struct usb_host_endpoint *ep);
822 
823 /* Quirks support */
824 void uvc_video_decode_isight(struct uvc_urb *uvc_urb,
825 			     struct uvc_buffer *buf,
826 			     struct uvc_buffer *meta_buf);
827 
828 /* debugfs and statistics */
829 void uvc_debugfs_init(void);
830 void uvc_debugfs_cleanup(void);
831 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
832 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
833 
834 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
835 			    size_t size);
836 
837 #endif
838