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