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