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