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