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