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