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