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