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