xref: /OK3568_Linux_fs/kernel/include/media/videobuf2-core.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * videobuf2-core.h - Video Buffer 2 Core Framework
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2010 Samsung Electronics
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Author: Pawel Osciak <pawel@osciak.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
10*4882a593Smuzhiyun  * the Free Software Foundation.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun #ifndef _MEDIA_VIDEOBUF2_CORE_H
13*4882a593Smuzhiyun #define _MEDIA_VIDEOBUF2_CORE_H
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/mm_types.h>
16*4882a593Smuzhiyun #include <linux/mutex.h>
17*4882a593Smuzhiyun #include <linux/poll.h>
18*4882a593Smuzhiyun #include <linux/dma-buf.h>
19*4882a593Smuzhiyun #include <linux/bitops.h>
20*4882a593Smuzhiyun #include <media/media-request.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define VB2_MAX_FRAME	(64)
23*4882a593Smuzhiyun #define VB2_MAX_PLANES	(8)
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * enum vb2_memory - type of memory model used to make the buffers visible
27*4882a593Smuzhiyun  *	on userspace.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * @VB2_MEMORY_UNKNOWN:	Buffer status is unknown or it is not used yet on
30*4882a593Smuzhiyun  *			userspace.
31*4882a593Smuzhiyun  * @VB2_MEMORY_MMAP:	The buffers are allocated by the Kernel and it is
32*4882a593Smuzhiyun  *			memory mapped via mmap() ioctl. This model is
33*4882a593Smuzhiyun  *			also used when the user is using the buffers via
34*4882a593Smuzhiyun  *			read() or write() system calls.
35*4882a593Smuzhiyun  * @VB2_MEMORY_USERPTR:	The buffers was allocated in userspace and it is
36*4882a593Smuzhiyun  *			memory mapped via mmap() ioctl.
37*4882a593Smuzhiyun  * @VB2_MEMORY_DMABUF:	The buffers are passed to userspace via DMA buffer.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun enum vb2_memory {
40*4882a593Smuzhiyun 	VB2_MEMORY_UNKNOWN	= 0,
41*4882a593Smuzhiyun 	VB2_MEMORY_MMAP		= 1,
42*4882a593Smuzhiyun 	VB2_MEMORY_USERPTR	= 2,
43*4882a593Smuzhiyun 	VB2_MEMORY_DMABUF	= 4,
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct vb2_fileio_data;
47*4882a593Smuzhiyun struct vb2_threadio_data;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /**
50*4882a593Smuzhiyun  * struct vb2_mem_ops - memory handling/memory allocator operations.
51*4882a593Smuzhiyun  * @alloc:	allocate video memory and, optionally, allocator private data,
52*4882a593Smuzhiyun  *		return ERR_PTR() on failure or a pointer to allocator private,
53*4882a593Smuzhiyun  *		per-buffer data on success; the returned private structure
54*4882a593Smuzhiyun  *		will then be passed as @buf_priv argument to other ops in this
55*4882a593Smuzhiyun  *		structure. Additional gfp_flags to use when allocating the
56*4882a593Smuzhiyun  *		are also passed to this operation. These flags are from the
57*4882a593Smuzhiyun  *		gfp_flags field of vb2_queue. The size argument to this function
58*4882a593Smuzhiyun  *		shall be *page aligned*.
59*4882a593Smuzhiyun  * @put:	inform the allocator that the buffer will no longer be used;
60*4882a593Smuzhiyun  *		usually will result in the allocator freeing the buffer (if
61*4882a593Smuzhiyun  *		no other users of this buffer are present); the @buf_priv
62*4882a593Smuzhiyun  *		argument is the allocator private per-buffer structure
63*4882a593Smuzhiyun  *		previously returned from the alloc callback.
64*4882a593Smuzhiyun  * @get_dmabuf: acquire userspace memory for a hardware operation; used for
65*4882a593Smuzhiyun  *		 DMABUF memory types.
66*4882a593Smuzhiyun  * @get_userptr: acquire userspace memory for a hardware operation; used for
67*4882a593Smuzhiyun  *		 USERPTR memory types; vaddr is the address passed to the
68*4882a593Smuzhiyun  *		 videobuf layer when queuing a video buffer of USERPTR type;
69*4882a593Smuzhiyun  *		 should return an allocator private per-buffer structure
70*4882a593Smuzhiyun  *		 associated with the buffer on success, ERR_PTR() on failure;
71*4882a593Smuzhiyun  *		 the returned private structure will then be passed as @buf_priv
72*4882a593Smuzhiyun  *		 argument to other ops in this structure.
73*4882a593Smuzhiyun  * @put_userptr: inform the allocator that a USERPTR buffer will no longer
74*4882a593Smuzhiyun  *		 be used.
75*4882a593Smuzhiyun  * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation;
76*4882a593Smuzhiyun  *		   used for DMABUF memory types; dev is the alloc device
77*4882a593Smuzhiyun  *		   dbuf is the shared dma_buf; returns ERR_PTR() on failure;
78*4882a593Smuzhiyun  *		   allocator private per-buffer structure on success;
79*4882a593Smuzhiyun  *		   this needs to be used for further accesses to the buffer.
80*4882a593Smuzhiyun  * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
81*4882a593Smuzhiyun  *		   buffer is no longer used; the @buf_priv argument is the
82*4882a593Smuzhiyun  *		   allocator private per-buffer structure previously returned
83*4882a593Smuzhiyun  *		   from the attach_dmabuf callback.
84*4882a593Smuzhiyun  * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
85*4882a593Smuzhiyun  *		of dmabuf is informed that this driver is going to use the
86*4882a593Smuzhiyun  *		dmabuf.
87*4882a593Smuzhiyun  * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
88*4882a593Smuzhiyun  *		  that this driver is done using the dmabuf for now.
89*4882a593Smuzhiyun  * @prepare:	called every time the buffer is passed from userspace to the
90*4882a593Smuzhiyun  *		driver, useful for cache synchronisation, optional.
91*4882a593Smuzhiyun  * @finish:	called every time the buffer is passed back from the driver
92*4882a593Smuzhiyun  *		to the userspace, also optional.
93*4882a593Smuzhiyun  * @vaddr:	return a kernel virtual address to a given memory buffer
94*4882a593Smuzhiyun  *		associated with the passed private structure or NULL if no
95*4882a593Smuzhiyun  *		such mapping exists.
96*4882a593Smuzhiyun  * @cookie:	return allocator specific cookie for a given memory buffer
97*4882a593Smuzhiyun  *		associated with the passed private structure or NULL if not
98*4882a593Smuzhiyun  *		available.
99*4882a593Smuzhiyun  * @num_users:	return the current number of users of a memory buffer;
100*4882a593Smuzhiyun  *		return 1 if the videobuf layer (or actually the driver using
101*4882a593Smuzhiyun  *		it) is the only user.
102*4882a593Smuzhiyun  * @mmap:	setup a userspace mapping for a given memory buffer under
103*4882a593Smuzhiyun  *		the provided virtual memory region.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * Those operations are used by the videobuf2 core to implement the memory
106*4882a593Smuzhiyun  * handling/memory allocators for each type of supported streaming I/O method.
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * .. note::
109*4882a593Smuzhiyun  *    #) Required ops for USERPTR types: get_userptr, put_userptr.
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  *    #) Required ops for MMAP types: alloc, put, num_users, mmap.
112*4882a593Smuzhiyun  *
113*4882a593Smuzhiyun  *    #) Required ops for read/write access types: alloc, put, num_users, vaddr.
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  *    #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
116*4882a593Smuzhiyun  *       map_dmabuf, unmap_dmabuf.
117*4882a593Smuzhiyun  */
118*4882a593Smuzhiyun struct vb2_mem_ops {
119*4882a593Smuzhiyun 	void		*(*alloc)(struct device *dev, unsigned long attrs,
120*4882a593Smuzhiyun 				  unsigned long size,
121*4882a593Smuzhiyun 				  enum dma_data_direction dma_dir,
122*4882a593Smuzhiyun 				  gfp_t gfp_flags);
123*4882a593Smuzhiyun 	void		(*put)(void *buf_priv);
124*4882a593Smuzhiyun 	struct dma_buf *(*get_dmabuf)(void *buf_priv, unsigned long flags);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	void		*(*get_userptr)(struct device *dev, unsigned long vaddr,
127*4882a593Smuzhiyun 					unsigned long size,
128*4882a593Smuzhiyun 					enum dma_data_direction dma_dir);
129*4882a593Smuzhiyun 	void		(*put_userptr)(void *buf_priv);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	void		(*prepare)(void *buf_priv);
132*4882a593Smuzhiyun 	void		(*finish)(void *buf_priv);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	void		*(*attach_dmabuf)(struct device *dev,
135*4882a593Smuzhiyun 					  struct dma_buf *dbuf,
136*4882a593Smuzhiyun 					  unsigned long size,
137*4882a593Smuzhiyun 					  enum dma_data_direction dma_dir);
138*4882a593Smuzhiyun 	void		(*detach_dmabuf)(void *buf_priv);
139*4882a593Smuzhiyun 	int		(*map_dmabuf)(void *buf_priv);
140*4882a593Smuzhiyun 	void		(*unmap_dmabuf)(void *buf_priv);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	void		*(*vaddr)(void *buf_priv);
143*4882a593Smuzhiyun 	void		*(*cookie)(void *buf_priv);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	unsigned int	(*num_users)(void *buf_priv);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	int		(*mmap)(void *buf_priv, struct vm_area_struct *vma);
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun  * struct vb2_plane - plane information.
152*4882a593Smuzhiyun  * @mem_priv:	private data with this plane.
153*4882a593Smuzhiyun  * @dbuf:	dma_buf - shared buffer object.
154*4882a593Smuzhiyun  * @dbuf_mapped:	flag to show whether dbuf is mapped or not
155*4882a593Smuzhiyun  * @bytesused:	number of bytes occupied by data in the plane (payload).
156*4882a593Smuzhiyun  * @length:	size of this plane (NOT the payload) in bytes.
157*4882a593Smuzhiyun  * @min_length:	minimum required size of this plane (NOT the payload) in bytes.
158*4882a593Smuzhiyun  *		@length is always greater or equal to @min_length.
159*4882a593Smuzhiyun  * @m:		Union with memtype-specific data.
160*4882a593Smuzhiyun  * @m.offset:	when memory in the associated struct vb2_buffer is
161*4882a593Smuzhiyun  *		%VB2_MEMORY_MMAP, equals the offset from the start of
162*4882a593Smuzhiyun  *		the device memory for this plane (or is a "cookie" that
163*4882a593Smuzhiyun  *		should be passed to mmap() called on the video node).
164*4882a593Smuzhiyun  * @m.userptr:	when memory is %VB2_MEMORY_USERPTR, a userspace pointer
165*4882a593Smuzhiyun  *		pointing to this plane.
166*4882a593Smuzhiyun  * @m.fd:	when memory is %VB2_MEMORY_DMABUF, a userspace file
167*4882a593Smuzhiyun  *		descriptor associated with this plane.
168*4882a593Smuzhiyun  * @data_offset:	offset in the plane to the start of data; usually 0,
169*4882a593Smuzhiyun  *		unless there is a header in front of the data.
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  * Should contain enough information to be able to cover all the fields
172*4882a593Smuzhiyun  * of &struct v4l2_plane at videodev2.h.
173*4882a593Smuzhiyun  */
174*4882a593Smuzhiyun struct vb2_plane {
175*4882a593Smuzhiyun 	void			*mem_priv;
176*4882a593Smuzhiyun 	struct dma_buf		*dbuf;
177*4882a593Smuzhiyun 	unsigned int		dbuf_mapped;
178*4882a593Smuzhiyun 	unsigned int		bytesused;
179*4882a593Smuzhiyun 	unsigned int		length;
180*4882a593Smuzhiyun 	unsigned int		min_length;
181*4882a593Smuzhiyun 	union {
182*4882a593Smuzhiyun 		unsigned int	offset;
183*4882a593Smuzhiyun 		unsigned long	userptr;
184*4882a593Smuzhiyun 		int		fd;
185*4882a593Smuzhiyun 	} m;
186*4882a593Smuzhiyun 	unsigned int		data_offset;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /**
190*4882a593Smuzhiyun  * enum vb2_io_modes - queue access methods.
191*4882a593Smuzhiyun  * @VB2_MMAP:		driver supports MMAP with streaming API.
192*4882a593Smuzhiyun  * @VB2_USERPTR:	driver supports USERPTR with streaming API.
193*4882a593Smuzhiyun  * @VB2_READ:		driver supports read() style access.
194*4882a593Smuzhiyun  * @VB2_WRITE:		driver supports write() style access.
195*4882a593Smuzhiyun  * @VB2_DMABUF:		driver supports DMABUF with streaming API.
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun enum vb2_io_modes {
198*4882a593Smuzhiyun 	VB2_MMAP	= BIT(0),
199*4882a593Smuzhiyun 	VB2_USERPTR	= BIT(1),
200*4882a593Smuzhiyun 	VB2_READ	= BIT(2),
201*4882a593Smuzhiyun 	VB2_WRITE	= BIT(3),
202*4882a593Smuzhiyun 	VB2_DMABUF	= BIT(4),
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun  * enum vb2_buffer_state - current video buffer state.
207*4882a593Smuzhiyun  * @VB2_BUF_STATE_DEQUEUED:	buffer under userspace control.
208*4882a593Smuzhiyun  * @VB2_BUF_STATE_IN_REQUEST:	buffer is queued in media request.
209*4882a593Smuzhiyun  * @VB2_BUF_STATE_PREPARING:	buffer is being prepared in videobuf.
210*4882a593Smuzhiyun  * @VB2_BUF_STATE_QUEUED:	buffer queued in videobuf, but not in driver.
211*4882a593Smuzhiyun  * @VB2_BUF_STATE_ACTIVE:	buffer queued in driver and possibly used
212*4882a593Smuzhiyun  *				in a hardware operation.
213*4882a593Smuzhiyun  * @VB2_BUF_STATE_DONE:		buffer returned from driver to videobuf, but
214*4882a593Smuzhiyun  *				not yet dequeued to userspace.
215*4882a593Smuzhiyun  * @VB2_BUF_STATE_ERROR:	same as above, but the operation on the buffer
216*4882a593Smuzhiyun  *				has ended with an error, which will be reported
217*4882a593Smuzhiyun  *				to the userspace when it is dequeued.
218*4882a593Smuzhiyun  */
219*4882a593Smuzhiyun enum vb2_buffer_state {
220*4882a593Smuzhiyun 	VB2_BUF_STATE_DEQUEUED,
221*4882a593Smuzhiyun 	VB2_BUF_STATE_IN_REQUEST,
222*4882a593Smuzhiyun 	VB2_BUF_STATE_PREPARING,
223*4882a593Smuzhiyun 	VB2_BUF_STATE_QUEUED,
224*4882a593Smuzhiyun 	VB2_BUF_STATE_ACTIVE,
225*4882a593Smuzhiyun 	VB2_BUF_STATE_DONE,
226*4882a593Smuzhiyun 	VB2_BUF_STATE_ERROR,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun struct vb2_queue;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun  * struct vb2_buffer - represents a video buffer.
233*4882a593Smuzhiyun  * @vb2_queue:		pointer to &struct vb2_queue with the queue to
234*4882a593Smuzhiyun  *			which this driver belongs.
235*4882a593Smuzhiyun  * @index:		id number of the buffer.
236*4882a593Smuzhiyun  * @type:		buffer type.
237*4882a593Smuzhiyun  * @memory:		the method, in which the actual data is passed.
238*4882a593Smuzhiyun  * @num_planes:		number of planes in the buffer
239*4882a593Smuzhiyun  *			on an internal driver queue.
240*4882a593Smuzhiyun  * @timestamp:		frame timestamp in ns.
241*4882a593Smuzhiyun  * @request:		the request this buffer is associated with.
242*4882a593Smuzhiyun  * @req_obj:		used to bind this buffer to a request. This
243*4882a593Smuzhiyun  *			request object has a refcount.
244*4882a593Smuzhiyun  */
245*4882a593Smuzhiyun struct vb2_buffer {
246*4882a593Smuzhiyun 	struct vb2_queue	*vb2_queue;
247*4882a593Smuzhiyun 	unsigned int		index;
248*4882a593Smuzhiyun 	unsigned int		type;
249*4882a593Smuzhiyun 	unsigned int		memory;
250*4882a593Smuzhiyun 	unsigned int		num_planes;
251*4882a593Smuzhiyun 	u64			timestamp;
252*4882a593Smuzhiyun 	struct media_request	*request;
253*4882a593Smuzhiyun 	struct media_request_object	req_obj;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* private: internal use only
256*4882a593Smuzhiyun 	 *
257*4882a593Smuzhiyun 	 * state:		current buffer state; do not change
258*4882a593Smuzhiyun 	 * synced:		this buffer has been synced for DMA, i.e. the
259*4882a593Smuzhiyun 	 *			'prepare' memop was called. It is cleared again
260*4882a593Smuzhiyun 	 *			after the 'finish' memop is called.
261*4882a593Smuzhiyun 	 * prepared:		this buffer has been prepared, i.e. the
262*4882a593Smuzhiyun 	 *			buf_prepare op was called. It is cleared again
263*4882a593Smuzhiyun 	 *			after the 'buf_finish' op is called.
264*4882a593Smuzhiyun 	 * copied_timestamp:	the timestamp of this capture buffer was copied
265*4882a593Smuzhiyun 	 *			from an output buffer.
266*4882a593Smuzhiyun 	 * need_cache_sync_on_prepare: when set buffer's ->prepare() function
267*4882a593Smuzhiyun 	 *			performs cache sync/invalidation.
268*4882a593Smuzhiyun 	 * need_cache_sync_on_finish: when set buffer's ->finish() function
269*4882a593Smuzhiyun 	 *			performs cache sync/invalidation.
270*4882a593Smuzhiyun 	 * queued_entry:	entry on the queued buffers list, which holds
271*4882a593Smuzhiyun 	 *			all buffers queued from userspace
272*4882a593Smuzhiyun 	 * done_entry:		entry on the list that stores all buffers ready
273*4882a593Smuzhiyun 	 *			to be dequeued to userspace
274*4882a593Smuzhiyun 	 * vb2_plane:		per-plane information; do not change
275*4882a593Smuzhiyun 	 */
276*4882a593Smuzhiyun 	enum vb2_buffer_state	state;
277*4882a593Smuzhiyun 	unsigned int		synced:1;
278*4882a593Smuzhiyun 	unsigned int		prepared:1;
279*4882a593Smuzhiyun 	unsigned int		copied_timestamp:1;
280*4882a593Smuzhiyun 	unsigned int		need_cache_sync_on_prepare:1;
281*4882a593Smuzhiyun 	unsigned int		need_cache_sync_on_finish:1;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	struct vb2_plane	planes[VB2_MAX_PLANES];
284*4882a593Smuzhiyun 	struct list_head	queued_entry;
285*4882a593Smuzhiyun 	struct list_head	done_entry;
286*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * Counters for how often these buffer-related ops are
289*4882a593Smuzhiyun 	 * called. Used to check for unbalanced ops.
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	u32		cnt_mem_alloc;
292*4882a593Smuzhiyun 	u32		cnt_mem_put;
293*4882a593Smuzhiyun 	u32		cnt_mem_get_dmabuf;
294*4882a593Smuzhiyun 	u32		cnt_mem_get_userptr;
295*4882a593Smuzhiyun 	u32		cnt_mem_put_userptr;
296*4882a593Smuzhiyun 	u32		cnt_mem_prepare;
297*4882a593Smuzhiyun 	u32		cnt_mem_finish;
298*4882a593Smuzhiyun 	u32		cnt_mem_attach_dmabuf;
299*4882a593Smuzhiyun 	u32		cnt_mem_detach_dmabuf;
300*4882a593Smuzhiyun 	u32		cnt_mem_map_dmabuf;
301*4882a593Smuzhiyun 	u32		cnt_mem_unmap_dmabuf;
302*4882a593Smuzhiyun 	u32		cnt_mem_vaddr;
303*4882a593Smuzhiyun 	u32		cnt_mem_cookie;
304*4882a593Smuzhiyun 	u32		cnt_mem_num_users;
305*4882a593Smuzhiyun 	u32		cnt_mem_mmap;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	u32		cnt_buf_out_validate;
308*4882a593Smuzhiyun 	u32		cnt_buf_init;
309*4882a593Smuzhiyun 	u32		cnt_buf_prepare;
310*4882a593Smuzhiyun 	u32		cnt_buf_finish;
311*4882a593Smuzhiyun 	u32		cnt_buf_cleanup;
312*4882a593Smuzhiyun 	u32		cnt_buf_queue;
313*4882a593Smuzhiyun 	u32		cnt_buf_request_complete;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	/* This counts the number of calls to vb2_buffer_done() */
316*4882a593Smuzhiyun 	u32		cnt_buf_done;
317*4882a593Smuzhiyun #endif
318*4882a593Smuzhiyun };
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /**
321*4882a593Smuzhiyun  * struct vb2_ops - driver-specific callbacks.
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  * These operations are not called from interrupt context except where
324*4882a593Smuzhiyun  * mentioned specifically.
325*4882a593Smuzhiyun  *
326*4882a593Smuzhiyun  * @queue_setup:	called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
327*4882a593Smuzhiyun  *			handlers before memory allocation. It can be called
328*4882a593Smuzhiyun  *			twice: if the original number of requested buffers
329*4882a593Smuzhiyun  *			could not be allocated, then it will be called a
330*4882a593Smuzhiyun  *			second time with the actually allocated number of
331*4882a593Smuzhiyun  *			buffers to verify if that is OK.
332*4882a593Smuzhiyun  *			The driver should return the required number of buffers
333*4882a593Smuzhiyun  *			in \*num_buffers, the required number of planes per
334*4882a593Smuzhiyun  *			buffer in \*num_planes, the size of each plane should be
335*4882a593Smuzhiyun  *			set in the sizes\[\] array and optional per-plane
336*4882a593Smuzhiyun  *			allocator specific device in the alloc_devs\[\] array.
337*4882a593Smuzhiyun  *			When called from VIDIOC_REQBUFS(), \*num_planes == 0,
338*4882a593Smuzhiyun  *			the driver has to use the currently configured format to
339*4882a593Smuzhiyun  *			determine the plane sizes and \*num_buffers is the total
340*4882a593Smuzhiyun  *			number of buffers that are being allocated. When called
341*4882a593Smuzhiyun  *			from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
342*4882a593Smuzhiyun  *			describes the requested number of planes and sizes\[\]
343*4882a593Smuzhiyun  *			contains the requested plane sizes. In this case
344*4882a593Smuzhiyun  *			\*num_buffers are being allocated additionally to
345*4882a593Smuzhiyun  *			q->num_buffers. If either \*num_planes or the requested
346*4882a593Smuzhiyun  *			sizes are invalid callback must return %-EINVAL.
347*4882a593Smuzhiyun  * @wait_prepare:	release any locks taken while calling vb2 functions;
348*4882a593Smuzhiyun  *			it is called before an ioctl needs to wait for a new
349*4882a593Smuzhiyun  *			buffer to arrive; required to avoid a deadlock in
350*4882a593Smuzhiyun  *			blocking access type.
351*4882a593Smuzhiyun  * @wait_finish:	reacquire all locks released in the previous callback;
352*4882a593Smuzhiyun  *			required to continue operation after sleeping while
353*4882a593Smuzhiyun  *			waiting for a new buffer to arrive.
354*4882a593Smuzhiyun  * @buf_out_validate:	called when the output buffer is prepared or queued
355*4882a593Smuzhiyun  *			to a request; drivers can use this to validate
356*4882a593Smuzhiyun  *			userspace-provided information; this is required only
357*4882a593Smuzhiyun  *			for OUTPUT queues.
358*4882a593Smuzhiyun  * @buf_init:		called once after allocating a buffer (in MMAP case)
359*4882a593Smuzhiyun  *			or after acquiring a new USERPTR buffer; drivers may
360*4882a593Smuzhiyun  *			perform additional buffer-related initialization;
361*4882a593Smuzhiyun  *			initialization failure (return != 0) will prevent
362*4882a593Smuzhiyun  *			queue setup from completing successfully; optional.
363*4882a593Smuzhiyun  * @buf_prepare:	called every time the buffer is queued from userspace
364*4882a593Smuzhiyun  *			and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
365*4882a593Smuzhiyun  *			perform any initialization required before each
366*4882a593Smuzhiyun  *			hardware operation in this callback; drivers can
367*4882a593Smuzhiyun  *			access/modify the buffer here as it is still synced for
368*4882a593Smuzhiyun  *			the CPU; drivers that support VIDIOC_CREATE_BUFS() must
369*4882a593Smuzhiyun  *			also validate the buffer size; if an error is returned,
370*4882a593Smuzhiyun  *			the buffer will not be queued in driver; optional.
371*4882a593Smuzhiyun  * @buf_finish:		called before every dequeue of the buffer back to
372*4882a593Smuzhiyun  *			userspace; the buffer is synced for the CPU, so drivers
373*4882a593Smuzhiyun  *			can access/modify the buffer contents; drivers may
374*4882a593Smuzhiyun  *			perform any operations required before userspace
375*4882a593Smuzhiyun  *			accesses the buffer; optional. The buffer state can be
376*4882a593Smuzhiyun  *			one of the following: %DONE and %ERROR occur while
377*4882a593Smuzhiyun  *			streaming is in progress, and the %PREPARED state occurs
378*4882a593Smuzhiyun  *			when the queue has been canceled and all pending
379*4882a593Smuzhiyun  *			buffers are being returned to their default %DEQUEUED
380*4882a593Smuzhiyun  *			state. Typically you only have to do something if the
381*4882a593Smuzhiyun  *			state is %VB2_BUF_STATE_DONE, since in all other cases
382*4882a593Smuzhiyun  *			the buffer contents will be ignored anyway.
383*4882a593Smuzhiyun  * @buf_cleanup:	called once before the buffer is freed; drivers may
384*4882a593Smuzhiyun  *			perform any additional cleanup; optional.
385*4882a593Smuzhiyun  * @start_streaming:	called once to enter 'streaming' state; the driver may
386*4882a593Smuzhiyun  *			receive buffers with @buf_queue callback
387*4882a593Smuzhiyun  *			before @start_streaming is called; the driver gets the
388*4882a593Smuzhiyun  *			number of already queued buffers in count parameter;
389*4882a593Smuzhiyun  *			driver can return an error if hardware fails, in that
390*4882a593Smuzhiyun  *			case all buffers that have been already given by
391*4882a593Smuzhiyun  *			the @buf_queue callback are to be returned by the driver
392*4882a593Smuzhiyun  *			by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
393*4882a593Smuzhiyun  *			If you need a minimum number of buffers before you can
394*4882a593Smuzhiyun  *			start streaming, then set
395*4882a593Smuzhiyun  *			&vb2_queue->min_buffers_needed. If that is non-zero
396*4882a593Smuzhiyun  *			then @start_streaming won't be called until at least
397*4882a593Smuzhiyun  *			that many buffers have been queued up by userspace.
398*4882a593Smuzhiyun  * @stop_streaming:	called when 'streaming' state must be disabled; driver
399*4882a593Smuzhiyun  *			should stop any DMA transactions or wait until they
400*4882a593Smuzhiyun  *			finish and give back all buffers it got from &buf_queue
401*4882a593Smuzhiyun  *			callback by calling vb2_buffer_done() with either
402*4882a593Smuzhiyun  *			%VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
403*4882a593Smuzhiyun  *			vb2_wait_for_all_buffers() function
404*4882a593Smuzhiyun  * @buf_queue:		passes buffer vb to the driver; driver may start
405*4882a593Smuzhiyun  *			hardware operation on this buffer; driver should give
406*4882a593Smuzhiyun  *			the buffer back by calling vb2_buffer_done() function;
407*4882a593Smuzhiyun  *			it is always called after calling VIDIOC_STREAMON()
408*4882a593Smuzhiyun  *			ioctl; might be called before @start_streaming callback
409*4882a593Smuzhiyun  *			if user pre-queued buffers before calling
410*4882a593Smuzhiyun  *			VIDIOC_STREAMON().
411*4882a593Smuzhiyun  * @buf_request_complete: a buffer that was never queued to the driver but is
412*4882a593Smuzhiyun  *			associated with a queued request was canceled.
413*4882a593Smuzhiyun  *			The driver will have to mark associated objects in the
414*4882a593Smuzhiyun  *			request as completed; required if requests are
415*4882a593Smuzhiyun  *			supported.
416*4882a593Smuzhiyun  */
417*4882a593Smuzhiyun struct vb2_ops {
418*4882a593Smuzhiyun 	int (*queue_setup)(struct vb2_queue *q,
419*4882a593Smuzhiyun 			   unsigned int *num_buffers, unsigned int *num_planes,
420*4882a593Smuzhiyun 			   unsigned int sizes[], struct device *alloc_devs[]);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	void (*wait_prepare)(struct vb2_queue *q);
423*4882a593Smuzhiyun 	void (*wait_finish)(struct vb2_queue *q);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	int (*buf_out_validate)(struct vb2_buffer *vb);
426*4882a593Smuzhiyun 	int (*buf_init)(struct vb2_buffer *vb);
427*4882a593Smuzhiyun 	int (*buf_prepare)(struct vb2_buffer *vb);
428*4882a593Smuzhiyun 	void (*buf_finish)(struct vb2_buffer *vb);
429*4882a593Smuzhiyun 	void (*buf_cleanup)(struct vb2_buffer *vb);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	int (*start_streaming)(struct vb2_queue *q, unsigned int count);
432*4882a593Smuzhiyun 	void (*stop_streaming)(struct vb2_queue *q);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	void (*buf_queue)(struct vb2_buffer *vb);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	void (*buf_request_complete)(struct vb2_buffer *vb);
437*4882a593Smuzhiyun };
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun  * struct vb2_buf_ops - driver-specific callbacks.
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  * @verify_planes_array: Verify that a given user space structure contains
443*4882a593Smuzhiyun  *			enough planes for the buffer. This is called
444*4882a593Smuzhiyun  *			for each dequeued buffer.
445*4882a593Smuzhiyun  * @init_buffer:	given a &vb2_buffer initialize the extra data after
446*4882a593Smuzhiyun  *			struct vb2_buffer.
447*4882a593Smuzhiyun  *			For V4L2 this is a &struct vb2_v4l2_buffer.
448*4882a593Smuzhiyun  * @fill_user_buffer:	given a &vb2_buffer fill in the userspace structure.
449*4882a593Smuzhiyun  *			For V4L2 this is a &struct v4l2_buffer.
450*4882a593Smuzhiyun  * @fill_vb2_buffer:	given a userspace structure, fill in the &vb2_buffer.
451*4882a593Smuzhiyun  *			If the userspace structure is invalid, then this op
452*4882a593Smuzhiyun  *			will return an error.
453*4882a593Smuzhiyun  * @copy_timestamp:	copy the timestamp from a userspace structure to
454*4882a593Smuzhiyun  *			the &struct vb2_buffer.
455*4882a593Smuzhiyun  */
456*4882a593Smuzhiyun struct vb2_buf_ops {
457*4882a593Smuzhiyun 	int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
458*4882a593Smuzhiyun 	void (*init_buffer)(struct vb2_buffer *vb);
459*4882a593Smuzhiyun 	void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
460*4882a593Smuzhiyun 	int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes);
461*4882a593Smuzhiyun 	void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
462*4882a593Smuzhiyun };
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /**
465*4882a593Smuzhiyun  * struct vb2_queue - a videobuf queue.
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * @type:	private buffer type whose content is defined by the vb2-core
468*4882a593Smuzhiyun  *		caller. For example, for V4L2, it should match
469*4882a593Smuzhiyun  *		the types defined on &enum v4l2_buf_type.
470*4882a593Smuzhiyun  * @io_modes:	supported io methods (see &enum vb2_io_modes).
471*4882a593Smuzhiyun  * @alloc_devs:	&struct device memory type/allocator-specific per-plane device
472*4882a593Smuzhiyun  * @dev:	device to use for the default allocation context if the driver
473*4882a593Smuzhiyun  *		doesn't fill in the @alloc_devs array.
474*4882a593Smuzhiyun  * @dma_attrs:	DMA attributes to use for the DMA.
475*4882a593Smuzhiyun  * @bidirectional: when this flag is set the DMA direction for the buffers of
476*4882a593Smuzhiyun  *		this queue will be overridden with %DMA_BIDIRECTIONAL direction.
477*4882a593Smuzhiyun  *		This is useful in cases where the hardware (firmware) writes to
478*4882a593Smuzhiyun  *		a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from
479*4882a593Smuzhiyun  *		buffer which is mapped for write (%DMA_FROM_DEVICE) in order
480*4882a593Smuzhiyun  *		to satisfy some internal hardware restrictions or adds a padding
481*4882a593Smuzhiyun  *		needed by the processing algorithm. In case the DMA mapping is
482*4882a593Smuzhiyun  *		not bidirectional but the hardware (firmware) trying to access
483*4882a593Smuzhiyun  *		the buffer (in the opposite direction) this could lead to an
484*4882a593Smuzhiyun  *		IOMMU protection faults.
485*4882a593Smuzhiyun  * @fileio_read_once:		report EOF after reading the first buffer
486*4882a593Smuzhiyun  * @fileio_write_immediately:	queue buffer after each write() call
487*4882a593Smuzhiyun  * @allow_zero_bytesused:	allow bytesused == 0 to be passed to the driver
488*4882a593Smuzhiyun  * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF
489*4882a593Smuzhiyun  *              has not been called. This is a vb1 idiom that has been adopted
490*4882a593Smuzhiyun  *              also by vb2.
491*4882a593Smuzhiyun  * @supports_requests: this queue supports the Request API.
492*4882a593Smuzhiyun  * @requires_requests: this queue requires the Request API. If this is set to 1,
493*4882a593Smuzhiyun  *		then supports_requests must be set to 1 as well.
494*4882a593Smuzhiyun  * @uses_qbuf:	qbuf was used directly for this queue. Set to 1 the first
495*4882a593Smuzhiyun  *		time this is called. Set to 0 when the queue is canceled.
496*4882a593Smuzhiyun  *		If this is 1, then you cannot queue buffers from a request.
497*4882a593Smuzhiyun  * @uses_requests: requests are used for this queue. Set to 1 the first time
498*4882a593Smuzhiyun  *		a request is queued. Set to 0 when the queue is canceled.
499*4882a593Smuzhiyun  *		If this is 1, then you cannot queue buffers directly.
500*4882a593Smuzhiyun  * @allow_cache_hints: when set user-space can pass cache management hints in
501*4882a593Smuzhiyun  *		order to skip cache flush/invalidation on ->prepare() or/and
502*4882a593Smuzhiyun  *		->finish().
503*4882a593Smuzhiyun  * @lock:	pointer to a mutex that protects the &struct vb2_queue. The
504*4882a593Smuzhiyun  *		driver can set this to a mutex to let the v4l2 core serialize
505*4882a593Smuzhiyun  *		the queuing ioctls. If the driver wants to handle locking
506*4882a593Smuzhiyun  *		itself, then this should be set to NULL. This lock is not used
507*4882a593Smuzhiyun  *		by the videobuf2 core API.
508*4882a593Smuzhiyun  * @owner:	The filehandle that 'owns' the buffers, i.e. the filehandle
509*4882a593Smuzhiyun  *		that called reqbufs, create_buffers or started fileio.
510*4882a593Smuzhiyun  *		This field is not used by the videobuf2 core API, but it allows
511*4882a593Smuzhiyun  *		drivers to easily associate an owner filehandle with the queue.
512*4882a593Smuzhiyun  * @ops:	driver-specific callbacks
513*4882a593Smuzhiyun  * @mem_ops:	memory allocator specific callbacks
514*4882a593Smuzhiyun  * @buf_ops:	callbacks to deliver buffer information.
515*4882a593Smuzhiyun  *		between user-space and kernel-space.
516*4882a593Smuzhiyun  * @drv_priv:	driver private data.
517*4882a593Smuzhiyun  * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used
518*4882a593Smuzhiyun  *		by the vb2 core.
519*4882a593Smuzhiyun  * @buf_struct_size: size of the driver-specific buffer structure;
520*4882a593Smuzhiyun  *		"0" indicates the driver doesn't want to use a custom buffer
521*4882a593Smuzhiyun  *		structure type. In that case a subsystem-specific struct
522*4882a593Smuzhiyun  *		will be used (in the case of V4L2 that is
523*4882a593Smuzhiyun  *		``sizeof(struct vb2_v4l2_buffer)``). The first field of the
524*4882a593Smuzhiyun  *		driver-specific buffer structure must be the subsystem-specific
525*4882a593Smuzhiyun  *		struct (vb2_v4l2_buffer in the case of V4L2).
526*4882a593Smuzhiyun  * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and
527*4882a593Smuzhiyun  *		``V4L2_BUF_FLAG_TSTAMP_SRC_*``
528*4882a593Smuzhiyun  * @gfp_flags:	additional gfp flags used when allocating the buffers.
529*4882a593Smuzhiyun  *		Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32
530*4882a593Smuzhiyun  *		to force the buffer allocation to a specific memory zone.
531*4882a593Smuzhiyun  * @min_buffers_needed: the minimum number of buffers needed before
532*4882a593Smuzhiyun  *		@start_streaming can be called. Used when a DMA engine
533*4882a593Smuzhiyun  *		cannot be started unless at least this number of buffers
534*4882a593Smuzhiyun  *		have been queued into the driver.
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun /*
537*4882a593Smuzhiyun  * Private elements (won't appear at the uAPI book):
538*4882a593Smuzhiyun  * @mmap_lock:	private mutex used when buffers are allocated/freed/mmapped
539*4882a593Smuzhiyun  * @memory:	current memory type used
540*4882a593Smuzhiyun  * @dma_dir:	DMA mapping direction.
541*4882a593Smuzhiyun  * @bufs:	videobuf buffer structures
542*4882a593Smuzhiyun  * @num_buffers: number of allocated/used buffers
543*4882a593Smuzhiyun  * @queued_list: list of buffers currently queued from userspace
544*4882a593Smuzhiyun  * @queued_count: number of buffers queued and ready for streaming.
545*4882a593Smuzhiyun  * @owned_by_drv_count: number of buffers owned by the driver
546*4882a593Smuzhiyun  * @done_list:	list of buffers ready to be dequeued to userspace
547*4882a593Smuzhiyun  * @done_lock:	lock to protect done_list list
548*4882a593Smuzhiyun  * @done_wq:	waitqueue for processes waiting for buffers ready to be dequeued
549*4882a593Smuzhiyun  * @streaming:	current streaming state
550*4882a593Smuzhiyun  * @start_streaming_called: @start_streaming was called successfully and we
551*4882a593Smuzhiyun  *		started streaming.
552*4882a593Smuzhiyun  * @error:	a fatal error occurred on the queue
553*4882a593Smuzhiyun  * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
554*4882a593Smuzhiyun  *		buffers. Only set for capture queues if qbuf has not yet been
555*4882a593Smuzhiyun  *		called since poll() needs to return %EPOLLERR in that situation.
556*4882a593Smuzhiyun  * @is_multiplanar: set if buffer type is multiplanar
557*4882a593Smuzhiyun  * @is_output:	set if buffer type is output
558*4882a593Smuzhiyun  * @copy_timestamp: set if vb2-core should set timestamps
559*4882a593Smuzhiyun  * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
560*4882a593Smuzhiyun  *		last decoded buffer was already dequeued. Set for capture queues
561*4882a593Smuzhiyun  *		when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued.
562*4882a593Smuzhiyun  * @fileio:	file io emulator internal data, used only if emulator is active
563*4882a593Smuzhiyun  * @threadio:	thread io internal data, used only if thread is active
564*4882a593Smuzhiyun  * @name:	queue name, used for logging purpose. Initialized automatically
565*4882a593Smuzhiyun  *		if left empty by drivers.
566*4882a593Smuzhiyun  */
567*4882a593Smuzhiyun struct vb2_queue {
568*4882a593Smuzhiyun 	unsigned int			type;
569*4882a593Smuzhiyun 	unsigned int			io_modes;
570*4882a593Smuzhiyun 	struct device			*dev;
571*4882a593Smuzhiyun 	unsigned long			dma_attrs;
572*4882a593Smuzhiyun 	unsigned int			bidirectional:1;
573*4882a593Smuzhiyun 	unsigned int			fileio_read_once:1;
574*4882a593Smuzhiyun 	unsigned int			fileio_write_immediately:1;
575*4882a593Smuzhiyun 	unsigned int			allow_zero_bytesused:1;
576*4882a593Smuzhiyun 	unsigned int		   quirk_poll_must_check_waiting_for_buffers:1;
577*4882a593Smuzhiyun 	unsigned int			supports_requests:1;
578*4882a593Smuzhiyun 	unsigned int			requires_requests:1;
579*4882a593Smuzhiyun 	unsigned int			uses_qbuf:1;
580*4882a593Smuzhiyun 	unsigned int			uses_requests:1;
581*4882a593Smuzhiyun 	unsigned int			allow_cache_hints:1;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	struct mutex			*lock;
584*4882a593Smuzhiyun 	void				*owner;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	const struct vb2_ops		*ops;
587*4882a593Smuzhiyun 	const struct vb2_mem_ops	*mem_ops;
588*4882a593Smuzhiyun 	const struct vb2_buf_ops	*buf_ops;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	void				*drv_priv;
591*4882a593Smuzhiyun 	u32				subsystem_flags;
592*4882a593Smuzhiyun 	unsigned int			buf_struct_size;
593*4882a593Smuzhiyun 	u32				timestamp_flags;
594*4882a593Smuzhiyun 	gfp_t				gfp_flags;
595*4882a593Smuzhiyun 	u32				min_buffers_needed;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	struct device			*alloc_devs[VB2_MAX_PLANES];
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/* private: internal use only */
600*4882a593Smuzhiyun 	struct mutex			mmap_lock;
601*4882a593Smuzhiyun 	unsigned int			memory;
602*4882a593Smuzhiyun 	enum dma_data_direction		dma_dir;
603*4882a593Smuzhiyun 	struct vb2_buffer		*bufs[VB2_MAX_FRAME];
604*4882a593Smuzhiyun 	unsigned int			num_buffers;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	struct list_head		queued_list;
607*4882a593Smuzhiyun 	unsigned int			queued_count;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	atomic_t			owned_by_drv_count;
610*4882a593Smuzhiyun 	struct list_head		done_list;
611*4882a593Smuzhiyun 	spinlock_t			done_lock;
612*4882a593Smuzhiyun 	wait_queue_head_t		done_wq;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	unsigned int			streaming:1;
615*4882a593Smuzhiyun 	unsigned int			start_streaming_called:1;
616*4882a593Smuzhiyun 	unsigned int			error:1;
617*4882a593Smuzhiyun 	unsigned int			waiting_for_buffers:1;
618*4882a593Smuzhiyun 	unsigned int			waiting_in_dqbuf:1;
619*4882a593Smuzhiyun 	unsigned int			is_multiplanar:1;
620*4882a593Smuzhiyun 	unsigned int			is_output:1;
621*4882a593Smuzhiyun 	unsigned int			copy_timestamp:1;
622*4882a593Smuzhiyun 	unsigned int			last_buffer_dequeued:1;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	struct vb2_fileio_data		*fileio;
625*4882a593Smuzhiyun 	struct vb2_threadio_data	*threadio;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	char				name[32];
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
630*4882a593Smuzhiyun 	/*
631*4882a593Smuzhiyun 	 * Counters for how often these queue-related ops are
632*4882a593Smuzhiyun 	 * called. Used to check for unbalanced ops.
633*4882a593Smuzhiyun 	 */
634*4882a593Smuzhiyun 	u32				cnt_queue_setup;
635*4882a593Smuzhiyun 	u32				cnt_wait_prepare;
636*4882a593Smuzhiyun 	u32				cnt_wait_finish;
637*4882a593Smuzhiyun 	u32				cnt_start_streaming;
638*4882a593Smuzhiyun 	u32				cnt_stop_streaming;
639*4882a593Smuzhiyun #endif
640*4882a593Smuzhiyun };
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun  * vb2_queue_allows_cache_hints() - Return true if the queue allows cache
644*4882a593Smuzhiyun  * and memory consistency hints.
645*4882a593Smuzhiyun  *
646*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue
647*4882a593Smuzhiyun  */
vb2_queue_allows_cache_hints(struct vb2_queue * q)648*4882a593Smuzhiyun static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun /**
654*4882a593Smuzhiyun  * vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
655*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
656*4882a593Smuzhiyun  *		question belongs to.
657*4882a593Smuzhiyun  * @plane_no:	plane number for which the address is to be returned.
658*4882a593Smuzhiyun  *
659*4882a593Smuzhiyun  * This function returns a kernel virtual address of a given plane if
660*4882a593Smuzhiyun  * such a mapping exist, NULL otherwise.
661*4882a593Smuzhiyun  */
662*4882a593Smuzhiyun void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun /**
665*4882a593Smuzhiyun  * vb2_plane_cookie() - Return allocator specific cookie for the given plane.
666*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
667*4882a593Smuzhiyun  *		question belongs to.
668*4882a593Smuzhiyun  * @plane_no:	plane number for which the cookie is to be returned.
669*4882a593Smuzhiyun  *
670*4882a593Smuzhiyun  * This function returns an allocator specific cookie for a given plane if
671*4882a593Smuzhiyun  * available, NULL otherwise. The allocator should provide some simple static
672*4882a593Smuzhiyun  * inline function, which would convert this cookie to the allocator specific
673*4882a593Smuzhiyun  * type that can be used directly by the driver to access the buffer. This can
674*4882a593Smuzhiyun  * be for example physical address, pointer to scatter list or IOMMU mapping.
675*4882a593Smuzhiyun  */
676*4882a593Smuzhiyun void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /**
679*4882a593Smuzhiyun  * vb2_buffer_done() - inform videobuf that an operation on a buffer
680*4882a593Smuzhiyun  *	is finished.
681*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to be used.
682*4882a593Smuzhiyun  * @state:	state of the buffer, as defined by &enum vb2_buffer_state.
683*4882a593Smuzhiyun  *		Either %VB2_BUF_STATE_DONE if the operation finished
684*4882a593Smuzhiyun  *		successfully, %VB2_BUF_STATE_ERROR if the operation finished
685*4882a593Smuzhiyun  *		with an error or %VB2_BUF_STATE_QUEUED.
686*4882a593Smuzhiyun  *
687*4882a593Smuzhiyun  * This function should be called by the driver after a hardware operation on
688*4882a593Smuzhiyun  * a buffer is finished and the buffer may be returned to userspace. The driver
689*4882a593Smuzhiyun  * cannot use this buffer anymore until it is queued back to it by videobuf
690*4882a593Smuzhiyun  * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
691*4882a593Smuzhiyun  * to the driver by &vb2_ops->buf_queue can be passed to this function.
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * While streaming a buffer can only be returned in state DONE or ERROR.
694*4882a593Smuzhiyun  * The &vb2_ops->start_streaming op can also return them in case the DMA engine
695*4882a593Smuzhiyun  * cannot be started for some reason. In that case the buffers should be
696*4882a593Smuzhiyun  * returned with state QUEUED to put them back into the queue.
697*4882a593Smuzhiyun  */
698*4882a593Smuzhiyun void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun /**
701*4882a593Smuzhiyun  * vb2_discard_done() - discard all buffers marked as DONE.
702*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
703*4882a593Smuzhiyun  *
704*4882a593Smuzhiyun  * This function is intended to be used with suspend/resume operations. It
705*4882a593Smuzhiyun  * discards all 'done' buffers as they would be too old to be requested after
706*4882a593Smuzhiyun  * resume.
707*4882a593Smuzhiyun  *
708*4882a593Smuzhiyun  * Drivers must stop the hardware and synchronize with interrupt handlers and/or
709*4882a593Smuzhiyun  * delayed works before calling this function to make sure no buffer will be
710*4882a593Smuzhiyun  * touched by the driver and/or hardware.
711*4882a593Smuzhiyun  */
712*4882a593Smuzhiyun void vb2_discard_done(struct vb2_queue *q);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /**
715*4882a593Smuzhiyun  * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2.
716*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
717*4882a593Smuzhiyun  *
718*4882a593Smuzhiyun  * This function will wait until all buffers that have been given to the driver
719*4882a593Smuzhiyun  * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
720*4882a593Smuzhiyun  * doesn't call &vb2_ops->wait_prepare/&vb2_ops->wait_finish pair.
721*4882a593Smuzhiyun  * It is intended to be called with all locks taken, for example from
722*4882a593Smuzhiyun  * &vb2_ops->stop_streaming callback.
723*4882a593Smuzhiyun  */
724*4882a593Smuzhiyun int vb2_wait_for_all_buffers(struct vb2_queue *q);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun /**
727*4882a593Smuzhiyun  * vb2_core_querybuf() - query video buffer information.
728*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
729*4882a593Smuzhiyun  * @index:	id number of the buffer.
730*4882a593Smuzhiyun  * @pb:		buffer struct passed from userspace.
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
733*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
734*4882a593Smuzhiyun  *
735*4882a593Smuzhiyun  * The passed buffer should have been verified.
736*4882a593Smuzhiyun  *
737*4882a593Smuzhiyun  * This function fills the relevant information for the userspace.
738*4882a593Smuzhiyun  *
739*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
740*4882a593Smuzhiyun  */
741*4882a593Smuzhiyun void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun  * vb2_core_reqbufs() - Initiate streaming.
745*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
746*4882a593Smuzhiyun  * @memory:	memory type, as defined by &enum vb2_memory.
747*4882a593Smuzhiyun  * @count:	requested buffer count.
748*4882a593Smuzhiyun  *
749*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
750*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
751*4882a593Smuzhiyun  *
752*4882a593Smuzhiyun  * This function:
753*4882a593Smuzhiyun  *
754*4882a593Smuzhiyun  * #) verifies streaming parameters passed from the userspace;
755*4882a593Smuzhiyun  * #) sets up the queue;
756*4882a593Smuzhiyun  * #) negotiates number of buffers and planes per buffer with the driver
757*4882a593Smuzhiyun  *    to be used during streaming;
758*4882a593Smuzhiyun  * #) allocates internal buffer structures (&struct vb2_buffer), according to
759*4882a593Smuzhiyun  *    the agreed parameters;
760*4882a593Smuzhiyun  * #) for MMAP memory type, allocates actual video memory, using the
761*4882a593Smuzhiyun  *    memory handling/allocation routines provided during queue initialization.
762*4882a593Smuzhiyun  *
763*4882a593Smuzhiyun  * If req->count is 0, all the memory will be freed instead.
764*4882a593Smuzhiyun  *
765*4882a593Smuzhiyun  * If the queue has been allocated previously by a previous vb2_core_reqbufs()
766*4882a593Smuzhiyun  * call and the queue is not busy, memory will be reallocated.
767*4882a593Smuzhiyun  *
768*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
769*4882a593Smuzhiyun  */
770*4882a593Smuzhiyun int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
771*4882a593Smuzhiyun 		    unsigned int *count);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun /**
774*4882a593Smuzhiyun  * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
775*4882a593Smuzhiyun  * @q: pointer to &struct vb2_queue with videobuf2 queue.
776*4882a593Smuzhiyun  * @memory: memory type, as defined by &enum vb2_memory.
777*4882a593Smuzhiyun  * @count: requested buffer count.
778*4882a593Smuzhiyun  * @requested_planes: number of planes requested.
779*4882a593Smuzhiyun  * @requested_sizes: array with the size of the planes.
780*4882a593Smuzhiyun  *
781*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
782*4882a593Smuzhiyun  * called internally by VB2 by an API-specific handler, like
783*4882a593Smuzhiyun  * ``videobuf2-v4l2.h``.
784*4882a593Smuzhiyun  *
785*4882a593Smuzhiyun  * This function:
786*4882a593Smuzhiyun  *
787*4882a593Smuzhiyun  * #) verifies parameter sanity;
788*4882a593Smuzhiyun  * #) calls the &vb2_ops->queue_setup queue operation;
789*4882a593Smuzhiyun  * #) performs any necessary memory allocations.
790*4882a593Smuzhiyun  *
791*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
792*4882a593Smuzhiyun  */
793*4882a593Smuzhiyun int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
794*4882a593Smuzhiyun 			 unsigned int *count,
795*4882a593Smuzhiyun 			 unsigned int requested_planes,
796*4882a593Smuzhiyun 			 const unsigned int requested_sizes[]);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /**
799*4882a593Smuzhiyun  * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
800*4882a593Smuzhiyun  *			to the kernel.
801*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
802*4882a593Smuzhiyun  * @index:	id number of the buffer.
803*4882a593Smuzhiyun  * @pb:		buffer structure passed from userspace to
804*4882a593Smuzhiyun  *		&v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
805*4882a593Smuzhiyun  *
806*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is
807*4882a593Smuzhiyun  * called internally by VB2 by an API-specific handler, like
808*4882a593Smuzhiyun  * ``videobuf2-v4l2.h``.
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * The passed buffer should have been verified.
811*4882a593Smuzhiyun  *
812*4882a593Smuzhiyun  * This function calls vb2_ops->buf_prepare callback in the driver
813*4882a593Smuzhiyun  * (if provided), in which driver-specific buffer initialization can
814*4882a593Smuzhiyun  * be performed.
815*4882a593Smuzhiyun  *
816*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
817*4882a593Smuzhiyun  */
818*4882a593Smuzhiyun int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun /**
821*4882a593Smuzhiyun  * vb2_core_qbuf() - Queue a buffer from userspace
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
824*4882a593Smuzhiyun  * @index:	id number of the buffer
825*4882a593Smuzhiyun  * @pb:		buffer structure passed from userspace to
826*4882a593Smuzhiyun  *		v4l2_ioctl_ops->vidioc_qbuf handler in driver
827*4882a593Smuzhiyun  * @req:	pointer to &struct media_request, may be NULL.
828*4882a593Smuzhiyun  *
829*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called
830*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
831*4882a593Smuzhiyun  *
832*4882a593Smuzhiyun  * This function:
833*4882a593Smuzhiyun  *
834*4882a593Smuzhiyun  * #) If @req is non-NULL, then the buffer will be bound to this
835*4882a593Smuzhiyun  *    media request and it returns. The buffer will be prepared and
836*4882a593Smuzhiyun  *    queued to the driver (i.e. the next two steps) when the request
837*4882a593Smuzhiyun  *    itself is queued.
838*4882a593Smuzhiyun  * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
839*4882a593Smuzhiyun  *    (if provided), in which driver-specific buffer initialization can
840*4882a593Smuzhiyun  *    be performed;
841*4882a593Smuzhiyun  * #) if streaming is on, queues the buffer in driver by the means of
842*4882a593Smuzhiyun  *    &vb2_ops->buf_queue callback for processing.
843*4882a593Smuzhiyun  *
844*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
845*4882a593Smuzhiyun  */
846*4882a593Smuzhiyun int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
847*4882a593Smuzhiyun 		  struct media_request *req);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun /**
850*4882a593Smuzhiyun  * vb2_core_dqbuf() - Dequeue a buffer to the userspace
851*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue
852*4882a593Smuzhiyun  * @pindex:	pointer to the buffer index. May be NULL
853*4882a593Smuzhiyun  * @pb:		buffer structure passed from userspace to
854*4882a593Smuzhiyun  *		v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
855*4882a593Smuzhiyun  * @nonblocking: if true, this call will not sleep waiting for a buffer if no
856*4882a593Smuzhiyun  *		 buffers ready for dequeuing are present. Normally the driver
857*4882a593Smuzhiyun  *		 would be passing (file->f_flags & O_NONBLOCK) here.
858*4882a593Smuzhiyun  *
859*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called
860*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
861*4882a593Smuzhiyun  *
862*4882a593Smuzhiyun  * This function:
863*4882a593Smuzhiyun  *
864*4882a593Smuzhiyun  * #) calls buf_finish callback in the driver (if provided), in which
865*4882a593Smuzhiyun  *    driver can perform any additional operations that may be required before
866*4882a593Smuzhiyun  *    returning the buffer to userspace, such as cache sync,
867*4882a593Smuzhiyun  * #) the buffer struct members are filled with relevant information for
868*4882a593Smuzhiyun  *    the userspace.
869*4882a593Smuzhiyun  *
870*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
871*4882a593Smuzhiyun  */
872*4882a593Smuzhiyun int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
873*4882a593Smuzhiyun 		   bool nonblocking);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun /**
876*4882a593Smuzhiyun  * vb2_core_streamon() - Implements VB2 stream ON logic
877*4882a593Smuzhiyun  *
878*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue
879*4882a593Smuzhiyun  * @type:	type of the queue to be started.
880*4882a593Smuzhiyun  *		For V4L2, this is defined by &enum v4l2_buf_type type.
881*4882a593Smuzhiyun  *
882*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called
883*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
884*4882a593Smuzhiyun  *
885*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
886*4882a593Smuzhiyun  */
887*4882a593Smuzhiyun int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun /**
890*4882a593Smuzhiyun  * vb2_core_streamoff() - Implements VB2 stream OFF logic
891*4882a593Smuzhiyun  *
892*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue
893*4882a593Smuzhiyun  * @type:	type of the queue to be started.
894*4882a593Smuzhiyun  *		For V4L2, this is defined by &enum v4l2_buf_type type.
895*4882a593Smuzhiyun  *
896*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is
897*4882a593Smuzhiyun  * called internally by VB2 by an API-specific handler, like
898*4882a593Smuzhiyun  * ``videobuf2-v4l2.h``.
899*4882a593Smuzhiyun  *
900*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
901*4882a593Smuzhiyun  */
902*4882a593Smuzhiyun int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun /**
905*4882a593Smuzhiyun  * vb2_core_expbuf() - Export a buffer as a file descriptor.
906*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
907*4882a593Smuzhiyun  * @fd:		pointer to the file descriptor associated with DMABUF
908*4882a593Smuzhiyun  *		(set by driver).
909*4882a593Smuzhiyun  * @type:	buffer type.
910*4882a593Smuzhiyun  * @index:	id number of the buffer.
911*4882a593Smuzhiyun  * @plane:	index of the plane to be exported, 0 for single plane queues
912*4882a593Smuzhiyun  * @flags:	file flags for newly created file, as defined at
913*4882a593Smuzhiyun  *		include/uapi/asm-generic/fcntl.h.
914*4882a593Smuzhiyun  *		Currently, the only used flag is %O_CLOEXEC.
915*4882a593Smuzhiyun  *		is supported, refer to manual of open syscall for more details.
916*4882a593Smuzhiyun  *
917*4882a593Smuzhiyun  *
918*4882a593Smuzhiyun  * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called
919*4882a593Smuzhiyun  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
920*4882a593Smuzhiyun  *
921*4882a593Smuzhiyun  * Return: returns zero on success; an error code otherwise.
922*4882a593Smuzhiyun  */
923*4882a593Smuzhiyun int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
924*4882a593Smuzhiyun 		unsigned int index, unsigned int plane, unsigned int flags);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun /**
927*4882a593Smuzhiyun  * vb2_core_queue_init() - initialize a videobuf2 queue
928*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
929*4882a593Smuzhiyun  *		This structure should be allocated in driver
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  * The &vb2_queue structure should be allocated by the driver. The driver is
932*4882a593Smuzhiyun  * responsible of clearing it's content and setting initial values for some
933*4882a593Smuzhiyun  * required entries before calling this function.
934*4882a593Smuzhiyun  *
935*4882a593Smuzhiyun  * .. note::
936*4882a593Smuzhiyun  *
937*4882a593Smuzhiyun  *    The following fields at @q should be set before calling this function:
938*4882a593Smuzhiyun  *    &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type.
939*4882a593Smuzhiyun  */
940*4882a593Smuzhiyun int vb2_core_queue_init(struct vb2_queue *q);
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun /**
943*4882a593Smuzhiyun  * vb2_core_queue_release() - stop streaming, release the queue and free memory
944*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
945*4882a593Smuzhiyun  *
946*4882a593Smuzhiyun  * This function stops streaming and performs necessary clean ups, including
947*4882a593Smuzhiyun  * freeing video buffer memory. The driver is responsible for freeing
948*4882a593Smuzhiyun  * the &struct vb2_queue itself.
949*4882a593Smuzhiyun  */
950*4882a593Smuzhiyun void vb2_core_queue_release(struct vb2_queue *q);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun /**
953*4882a593Smuzhiyun  * vb2_queue_error() - signal a fatal error on the queue
954*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
955*4882a593Smuzhiyun  *
956*4882a593Smuzhiyun  * Flag that a fatal unrecoverable error has occurred and wake up all processes
957*4882a593Smuzhiyun  * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing
958*4882a593Smuzhiyun  * buffers will return %-EIO.
959*4882a593Smuzhiyun  *
960*4882a593Smuzhiyun  * The error flag will be cleared when canceling the queue, either from
961*4882a593Smuzhiyun  * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this
962*4882a593Smuzhiyun  * function before starting the stream, otherwise the error flag will remain set
963*4882a593Smuzhiyun  * until the queue is released when closing the device node.
964*4882a593Smuzhiyun  */
965*4882a593Smuzhiyun void vb2_queue_error(struct vb2_queue *q);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun /**
968*4882a593Smuzhiyun  * vb2_mmap() - map video buffers into application address space.
969*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
970*4882a593Smuzhiyun  * @vma:	pointer to &struct vm_area_struct with the vma passed
971*4882a593Smuzhiyun  *		to the mmap file operation handler in the driver.
972*4882a593Smuzhiyun  *
973*4882a593Smuzhiyun  * Should be called from mmap file operation handler of a driver.
974*4882a593Smuzhiyun  * This function maps one plane of one of the available video buffers to
975*4882a593Smuzhiyun  * userspace. To map whole video memory allocated on reqbufs, this function
976*4882a593Smuzhiyun  * has to be called once per each plane per each buffer previously allocated.
977*4882a593Smuzhiyun  *
978*4882a593Smuzhiyun  * When the userspace application calls mmap, it passes to it an offset returned
979*4882a593Smuzhiyun  * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler.
980*4882a593Smuzhiyun  * That offset acts as a "cookie", which is then used to identify the plane
981*4882a593Smuzhiyun  * to be mapped.
982*4882a593Smuzhiyun  *
983*4882a593Smuzhiyun  * This function finds a plane with a matching offset and a mapping is performed
984*4882a593Smuzhiyun  * by the means of a provided memory operation.
985*4882a593Smuzhiyun  *
986*4882a593Smuzhiyun  * The return values from this function are intended to be directly returned
987*4882a593Smuzhiyun  * from the mmap handler in driver.
988*4882a593Smuzhiyun  */
989*4882a593Smuzhiyun int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun #ifndef CONFIG_MMU
992*4882a593Smuzhiyun /**
993*4882a593Smuzhiyun  * vb2_get_unmapped_area - map video buffers into application address space.
994*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
995*4882a593Smuzhiyun  * @addr:	memory address.
996*4882a593Smuzhiyun  * @len:	buffer size.
997*4882a593Smuzhiyun  * @pgoff:	page offset.
998*4882a593Smuzhiyun  * @flags:	memory flags.
999*4882a593Smuzhiyun  *
1000*4882a593Smuzhiyun  * This function is used in noMMU platforms to propose address mapping
1001*4882a593Smuzhiyun  * for a given buffer. It's intended to be used as a handler for the
1002*4882a593Smuzhiyun  * &file_operations->get_unmapped_area operation.
1003*4882a593Smuzhiyun  *
1004*4882a593Smuzhiyun  * This is called by the mmap() syscall routines will call this
1005*4882a593Smuzhiyun  * to get a proposed address for the mapping, when ``!CONFIG_MMU``.
1006*4882a593Smuzhiyun  */
1007*4882a593Smuzhiyun unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1008*4882a593Smuzhiyun 				    unsigned long addr,
1009*4882a593Smuzhiyun 				    unsigned long len,
1010*4882a593Smuzhiyun 				    unsigned long pgoff,
1011*4882a593Smuzhiyun 				    unsigned long flags);
1012*4882a593Smuzhiyun #endif
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun /**
1015*4882a593Smuzhiyun  * vb2_core_poll() - implements poll syscall() logic.
1016*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1017*4882a593Smuzhiyun  * @file:	&struct file argument passed to the poll
1018*4882a593Smuzhiyun  *		file operation handler.
1019*4882a593Smuzhiyun  * @wait:	&poll_table wait argument passed to the poll
1020*4882a593Smuzhiyun  *		file operation handler.
1021*4882a593Smuzhiyun  *
1022*4882a593Smuzhiyun  * This function implements poll file operation handler for a driver.
1023*4882a593Smuzhiyun  * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1024*4882a593Smuzhiyun  * be informed that the file descriptor of a video device is available for
1025*4882a593Smuzhiyun  * reading.
1026*4882a593Smuzhiyun  * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1027*4882a593Smuzhiyun  * will be reported as available for writing.
1028*4882a593Smuzhiyun  *
1029*4882a593Smuzhiyun  * The return values from this function are intended to be directly returned
1030*4882a593Smuzhiyun  * from poll handler in driver.
1031*4882a593Smuzhiyun  */
1032*4882a593Smuzhiyun __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
1033*4882a593Smuzhiyun 			   poll_table *wait);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun /**
1036*4882a593Smuzhiyun  * vb2_read() - implements read() syscall logic.
1037*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1038*4882a593Smuzhiyun  * @data:	pointed to target userspace buffer
1039*4882a593Smuzhiyun  * @count:	number of bytes to read
1040*4882a593Smuzhiyun  * @ppos:	file handle position tracking pointer
1041*4882a593Smuzhiyun  * @nonblock:	mode selector (1 means blocking calls, 0 means nonblocking)
1042*4882a593Smuzhiyun  */
1043*4882a593Smuzhiyun size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1044*4882a593Smuzhiyun 		loff_t *ppos, int nonblock);
1045*4882a593Smuzhiyun /**
1046*4882a593Smuzhiyun  * vb2_read() - implements write() syscall logic.
1047*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1048*4882a593Smuzhiyun  * @data:	pointed to target userspace buffer
1049*4882a593Smuzhiyun  * @count:	number of bytes to write
1050*4882a593Smuzhiyun  * @ppos:	file handle position tracking pointer
1051*4882a593Smuzhiyun  * @nonblock:	mode selector (1 means blocking calls, 0 means nonblocking)
1052*4882a593Smuzhiyun  */
1053*4882a593Smuzhiyun size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1054*4882a593Smuzhiyun 		loff_t *ppos, int nonblock);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun /**
1057*4882a593Smuzhiyun  * typedef vb2_thread_fnc - callback function for use with vb2_thread.
1058*4882a593Smuzhiyun  *
1059*4882a593Smuzhiyun  * @vb: pointer to struct &vb2_buffer.
1060*4882a593Smuzhiyun  * @priv: pointer to a private data.
1061*4882a593Smuzhiyun  *
1062*4882a593Smuzhiyun  * This is called whenever a buffer is dequeued in the thread.
1063*4882a593Smuzhiyun  */
1064*4882a593Smuzhiyun typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun /**
1067*4882a593Smuzhiyun  * vb2_thread_start() - start a thread for the given queue.
1068*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1069*4882a593Smuzhiyun  * @fnc:	&vb2_thread_fnc callback function.
1070*4882a593Smuzhiyun  * @priv:	priv pointer passed to the callback function.
1071*4882a593Smuzhiyun  * @thread_name:the name of the thread. This will be prefixed with "vb2-".
1072*4882a593Smuzhiyun  *
1073*4882a593Smuzhiyun  * This starts a thread that will queue and dequeue until an error occurs
1074*4882a593Smuzhiyun  * or vb2_thread_stop() is called.
1075*4882a593Smuzhiyun  *
1076*4882a593Smuzhiyun  * .. attention::
1077*4882a593Smuzhiyun  *
1078*4882a593Smuzhiyun  *   This function should not be used for anything else but the videobuf2-dvb
1079*4882a593Smuzhiyun  *   support. If you think you have another good use-case for this, then please
1080*4882a593Smuzhiyun  *   contact the linux-media mailing list first.
1081*4882a593Smuzhiyun  */
1082*4882a593Smuzhiyun int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1083*4882a593Smuzhiyun 		     const char *thread_name);
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun /**
1086*4882a593Smuzhiyun  * vb2_thread_stop() - stop the thread for the given queue.
1087*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1088*4882a593Smuzhiyun  */
1089*4882a593Smuzhiyun int vb2_thread_stop(struct vb2_queue *q);
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun /**
1092*4882a593Smuzhiyun  * vb2_is_streaming() - return streaming status of the queue.
1093*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1094*4882a593Smuzhiyun  */
vb2_is_streaming(struct vb2_queue * q)1095*4882a593Smuzhiyun static inline bool vb2_is_streaming(struct vb2_queue *q)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun 	return q->streaming;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun /**
1101*4882a593Smuzhiyun  * vb2_fileio_is_active() - return true if fileio is active.
1102*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1103*4882a593Smuzhiyun  *
1104*4882a593Smuzhiyun  * This returns true if read() or write() is used to stream the data
1105*4882a593Smuzhiyun  * as opposed to stream I/O. This is almost never an important distinction,
1106*4882a593Smuzhiyun  * except in rare cases. One such case is that using read() or write() to
1107*4882a593Smuzhiyun  * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there
1108*4882a593Smuzhiyun  * is no way you can pass the field information of each buffer to/from
1109*4882a593Smuzhiyun  * userspace. A driver that supports this field format should check for
1110*4882a593Smuzhiyun  * this in the &vb2_ops->queue_setup op and reject it if this function returns
1111*4882a593Smuzhiyun  * true.
1112*4882a593Smuzhiyun  */
vb2_fileio_is_active(struct vb2_queue * q)1113*4882a593Smuzhiyun static inline bool vb2_fileio_is_active(struct vb2_queue *q)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	return q->fileio;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun /**
1119*4882a593Smuzhiyun  * vb2_is_busy() - return busy status of the queue.
1120*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1121*4882a593Smuzhiyun  *
1122*4882a593Smuzhiyun  * This function checks if queue has any buffers allocated.
1123*4882a593Smuzhiyun  */
vb2_is_busy(struct vb2_queue * q)1124*4882a593Smuzhiyun static inline bool vb2_is_busy(struct vb2_queue *q)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun 	return (q->num_buffers > 0);
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun /**
1130*4882a593Smuzhiyun  * vb2_get_drv_priv() - return driver private data associated with the queue.
1131*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1132*4882a593Smuzhiyun  */
vb2_get_drv_priv(struct vb2_queue * q)1133*4882a593Smuzhiyun static inline void *vb2_get_drv_priv(struct vb2_queue *q)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun 	return q->drv_priv;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun /**
1139*4882a593Smuzhiyun  * vb2_set_plane_payload() - set bytesused for the plane @plane_no.
1140*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
1141*4882a593Smuzhiyun  *		question belongs to.
1142*4882a593Smuzhiyun  * @plane_no:	plane number for which payload should be set.
1143*4882a593Smuzhiyun  * @size:	payload in bytes.
1144*4882a593Smuzhiyun  */
vb2_set_plane_payload(struct vb2_buffer * vb,unsigned int plane_no,unsigned long size)1145*4882a593Smuzhiyun static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
1146*4882a593Smuzhiyun 				 unsigned int plane_no, unsigned long size)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	if (plane_no < vb->num_planes)
1149*4882a593Smuzhiyun 		vb->planes[plane_no].bytesused = size;
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun /**
1153*4882a593Smuzhiyun  * vb2_get_plane_payload() - get bytesused for the plane plane_no
1154*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
1155*4882a593Smuzhiyun  *		question belongs to.
1156*4882a593Smuzhiyun  * @plane_no:	plane number for which payload should be set.
1157*4882a593Smuzhiyun  */
vb2_get_plane_payload(struct vb2_buffer * vb,unsigned int plane_no)1158*4882a593Smuzhiyun static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
1159*4882a593Smuzhiyun 				 unsigned int plane_no)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	if (plane_no < vb->num_planes)
1162*4882a593Smuzhiyun 		return vb->planes[plane_no].bytesused;
1163*4882a593Smuzhiyun 	return 0;
1164*4882a593Smuzhiyun }
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun /**
1167*4882a593Smuzhiyun  * vb2_plane_size() - return plane size in bytes.
1168*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
1169*4882a593Smuzhiyun  *		question belongs to.
1170*4882a593Smuzhiyun  * @plane_no:	plane number for which size should be returned.
1171*4882a593Smuzhiyun  */
1172*4882a593Smuzhiyun static inline unsigned long
vb2_plane_size(struct vb2_buffer * vb,unsigned int plane_no)1173*4882a593Smuzhiyun vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
1174*4882a593Smuzhiyun {
1175*4882a593Smuzhiyun 	if (plane_no < vb->num_planes)
1176*4882a593Smuzhiyun 		return vb->planes[plane_no].length;
1177*4882a593Smuzhiyun 	return 0;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun #if defined(CONFIG_ARCH_ROCKCHIP) && IS_ENABLED(CONFIG_USB_F_UVC)
1181*4882a593Smuzhiyun /**
1182*4882a593Smuzhiyun  * vb2_plane_data_offset() - return plane data_offset in bytes.
1183*4882a593Smuzhiyun  * @vb:		pointer to &struct vb2_buffer to which the plane in
1184*4882a593Smuzhiyun  *		question belongs to.
1185*4882a593Smuzhiyun  * @plane_no:	plane number for which size should be returned.
1186*4882a593Smuzhiyun  */
1187*4882a593Smuzhiyun static inline unsigned long
vb2_plane_data_offset(struct vb2_buffer * vb,unsigned int plane_no)1188*4882a593Smuzhiyun vb2_plane_data_offset(struct vb2_buffer *vb, unsigned int plane_no)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun 	if (plane_no < vb->num_planes)
1191*4882a593Smuzhiyun 		return vb->planes[plane_no].data_offset;
1192*4882a593Smuzhiyun 	return 0;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun #endif
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun /**
1197*4882a593Smuzhiyun  * vb2_start_streaming_called() - return streaming status of driver.
1198*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1199*4882a593Smuzhiyun  */
vb2_start_streaming_called(struct vb2_queue * q)1200*4882a593Smuzhiyun static inline bool vb2_start_streaming_called(struct vb2_queue *q)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	return q->start_streaming_called;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun /**
1206*4882a593Smuzhiyun  * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue.
1207*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1208*4882a593Smuzhiyun  */
vb2_clear_last_buffer_dequeued(struct vb2_queue * q)1209*4882a593Smuzhiyun static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
1210*4882a593Smuzhiyun {
1211*4882a593Smuzhiyun 	q->last_buffer_dequeued = false;
1212*4882a593Smuzhiyun }
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun /**
1215*4882a593Smuzhiyun  * vb2_get_buffer() - get a buffer from a queue
1216*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1217*4882a593Smuzhiyun  * @index:	buffer index
1218*4882a593Smuzhiyun  *
1219*4882a593Smuzhiyun  * This function obtains a buffer from a queue, by its index.
1220*4882a593Smuzhiyun  * Keep in mind that there is no refcounting involved in this
1221*4882a593Smuzhiyun  * operation, so the buffer lifetime should be taken into
1222*4882a593Smuzhiyun  * consideration.
1223*4882a593Smuzhiyun  */
vb2_get_buffer(struct vb2_queue * q,unsigned int index)1224*4882a593Smuzhiyun static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
1225*4882a593Smuzhiyun 						unsigned int index)
1226*4882a593Smuzhiyun {
1227*4882a593Smuzhiyun 	if (index < q->num_buffers)
1228*4882a593Smuzhiyun 		return q->bufs[index];
1229*4882a593Smuzhiyun 	return NULL;
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun /*
1233*4882a593Smuzhiyun  * The following functions are not part of the vb2 core API, but are useful
1234*4882a593Smuzhiyun  * functions for videobuf2-*.
1235*4882a593Smuzhiyun  */
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun /**
1238*4882a593Smuzhiyun  * vb2_buffer_in_use() - return true if the buffer is in use and
1239*4882a593Smuzhiyun  * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call.
1240*4882a593Smuzhiyun  *
1241*4882a593Smuzhiyun  * @vb:		buffer for which plane size should be returned.
1242*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1243*4882a593Smuzhiyun  */
1244*4882a593Smuzhiyun bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun /**
1247*4882a593Smuzhiyun  * vb2_verify_memory_type() - Check whether the memory type and buffer type
1248*4882a593Smuzhiyun  * passed to a buffer operation are compatible with the queue.
1249*4882a593Smuzhiyun  *
1250*4882a593Smuzhiyun  * @q:		pointer to &struct vb2_queue with videobuf2 queue.
1251*4882a593Smuzhiyun  * @memory:	memory model, as defined by enum &vb2_memory.
1252*4882a593Smuzhiyun  * @type:	private buffer type whose content is defined by the vb2-core
1253*4882a593Smuzhiyun  *		caller. For example, for V4L2, it should match
1254*4882a593Smuzhiyun  *		the types defined on enum &v4l2_buf_type.
1255*4882a593Smuzhiyun  */
1256*4882a593Smuzhiyun int vb2_verify_memory_type(struct vb2_queue *q,
1257*4882a593Smuzhiyun 		enum vb2_memory memory, unsigned int type);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun /**
1260*4882a593Smuzhiyun  * vb2_request_object_is_buffer() - return true if the object is a buffer
1261*4882a593Smuzhiyun  *
1262*4882a593Smuzhiyun  * @obj:	the request object.
1263*4882a593Smuzhiyun  */
1264*4882a593Smuzhiyun bool vb2_request_object_is_buffer(struct media_request_object *obj);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun /**
1267*4882a593Smuzhiyun  * vb2_request_buffer_cnt() - return the number of buffers in the request
1268*4882a593Smuzhiyun  *
1269*4882a593Smuzhiyun  * @req:	the request.
1270*4882a593Smuzhiyun  */
1271*4882a593Smuzhiyun unsigned int vb2_request_buffer_cnt(struct media_request *req);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun #endif /* _MEDIA_VIDEOBUF2_CORE_H */
1274