162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * videobuf2-core.h - Video Buffer 2 Core Framework
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 2010 Samsung Electronics
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Author: Pawel Osciak <pawel@osciak.com>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
962306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
1062306a36Sopenharmony_ci * the Free Software Foundation.
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci#ifndef _MEDIA_VIDEOBUF2_CORE_H
1362306a36Sopenharmony_ci#define _MEDIA_VIDEOBUF2_CORE_H
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/mm_types.h>
1662306a36Sopenharmony_ci#include <linux/mutex.h>
1762306a36Sopenharmony_ci#include <linux/poll.h>
1862306a36Sopenharmony_ci#include <linux/dma-buf.h>
1962306a36Sopenharmony_ci#include <linux/bitops.h>
2062306a36Sopenharmony_ci#include <media/media-request.h>
2162306a36Sopenharmony_ci#include <media/frame_vector.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define VB2_MAX_FRAME	(32)
2462306a36Sopenharmony_ci#define VB2_MAX_PLANES	(8)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/**
2762306a36Sopenharmony_ci * enum vb2_memory - type of memory model used to make the buffers visible
2862306a36Sopenharmony_ci *	on userspace.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * @VB2_MEMORY_UNKNOWN:	Buffer status is unknown or it is not used yet on
3162306a36Sopenharmony_ci *			userspace.
3262306a36Sopenharmony_ci * @VB2_MEMORY_MMAP:	The buffers are allocated by the Kernel and it is
3362306a36Sopenharmony_ci *			memory mapped via mmap() ioctl. This model is
3462306a36Sopenharmony_ci *			also used when the user is using the buffers via
3562306a36Sopenharmony_ci *			read() or write() system calls.
3662306a36Sopenharmony_ci * @VB2_MEMORY_USERPTR:	The buffers was allocated in userspace and it is
3762306a36Sopenharmony_ci *			memory mapped via mmap() ioctl.
3862306a36Sopenharmony_ci * @VB2_MEMORY_DMABUF:	The buffers are passed to userspace via DMA buffer.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_cienum vb2_memory {
4162306a36Sopenharmony_ci	VB2_MEMORY_UNKNOWN	= 0,
4262306a36Sopenharmony_ci	VB2_MEMORY_MMAP		= 1,
4362306a36Sopenharmony_ci	VB2_MEMORY_USERPTR	= 2,
4462306a36Sopenharmony_ci	VB2_MEMORY_DMABUF	= 4,
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct vb2_fileio_data;
4862306a36Sopenharmony_cistruct vb2_threadio_data;
4962306a36Sopenharmony_cistruct vb2_buffer;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/**
5262306a36Sopenharmony_ci * struct vb2_mem_ops - memory handling/memory allocator operations.
5362306a36Sopenharmony_ci * @alloc:	allocate video memory and, optionally, allocator private data,
5462306a36Sopenharmony_ci *		return ERR_PTR() on failure or a pointer to allocator private,
5562306a36Sopenharmony_ci *		per-buffer data on success; the returned private structure
5662306a36Sopenharmony_ci *		will then be passed as @buf_priv argument to other ops in this
5762306a36Sopenharmony_ci *		structure. The size argument to this function shall be
5862306a36Sopenharmony_ci *		*page aligned*.
5962306a36Sopenharmony_ci * @put:	inform the allocator that the buffer will no longer be used;
6062306a36Sopenharmony_ci *		usually will result in the allocator freeing the buffer (if
6162306a36Sopenharmony_ci *		no other users of this buffer are present); the @buf_priv
6262306a36Sopenharmony_ci *		argument is the allocator private per-buffer structure
6362306a36Sopenharmony_ci *		previously returned from the alloc callback.
6462306a36Sopenharmony_ci * @get_dmabuf: acquire userspace memory for a hardware operation; used for
6562306a36Sopenharmony_ci *		 DMABUF memory types.
6662306a36Sopenharmony_ci * @get_userptr: acquire userspace memory for a hardware operation; used for
6762306a36Sopenharmony_ci *		 USERPTR memory types; vaddr is the address passed to the
6862306a36Sopenharmony_ci *		 videobuf2 layer when queuing a video buffer of USERPTR type;
6962306a36Sopenharmony_ci *		 should return an allocator private per-buffer structure
7062306a36Sopenharmony_ci *		 associated with the buffer on success, ERR_PTR() on failure;
7162306a36Sopenharmony_ci *		 the returned private structure will then be passed as @buf_priv
7262306a36Sopenharmony_ci *		 argument to other ops in this structure.
7362306a36Sopenharmony_ci * @put_userptr: inform the allocator that a USERPTR buffer will no longer
7462306a36Sopenharmony_ci *		 be used.
7562306a36Sopenharmony_ci * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation;
7662306a36Sopenharmony_ci *		   used for DMABUF memory types; dev is the alloc device
7762306a36Sopenharmony_ci *		   dbuf is the shared dma_buf; returns ERR_PTR() on failure;
7862306a36Sopenharmony_ci *		   allocator private per-buffer structure on success;
7962306a36Sopenharmony_ci *		   this needs to be used for further accesses to the buffer.
8062306a36Sopenharmony_ci * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
8162306a36Sopenharmony_ci *		   buffer is no longer used; the @buf_priv argument is the
8262306a36Sopenharmony_ci *		   allocator private per-buffer structure previously returned
8362306a36Sopenharmony_ci *		   from the attach_dmabuf callback.
8462306a36Sopenharmony_ci * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
8562306a36Sopenharmony_ci *		of dmabuf is informed that this driver is going to use the
8662306a36Sopenharmony_ci *		dmabuf.
8762306a36Sopenharmony_ci * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
8862306a36Sopenharmony_ci *		  that this driver is done using the dmabuf for now.
8962306a36Sopenharmony_ci * @prepare:	called every time the buffer is passed from userspace to the
9062306a36Sopenharmony_ci *		driver, useful for cache synchronisation, optional.
9162306a36Sopenharmony_ci * @finish:	called every time the buffer is passed back from the driver
9262306a36Sopenharmony_ci *		to the userspace, also optional.
9362306a36Sopenharmony_ci * @vaddr:	return a kernel virtual address to a given memory buffer
9462306a36Sopenharmony_ci *		associated with the passed private structure or NULL if no
9562306a36Sopenharmony_ci *		such mapping exists.
9662306a36Sopenharmony_ci * @cookie:	return allocator specific cookie for a given memory buffer
9762306a36Sopenharmony_ci *		associated with the passed private structure or NULL if not
9862306a36Sopenharmony_ci *		available.
9962306a36Sopenharmony_ci * @num_users:	return the current number of users of a memory buffer;
10062306a36Sopenharmony_ci *		return 1 if the videobuf2 layer (or actually the driver using
10162306a36Sopenharmony_ci *		it) is the only user.
10262306a36Sopenharmony_ci * @mmap:	setup a userspace mapping for a given memory buffer under
10362306a36Sopenharmony_ci *		the provided virtual memory region.
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci * Those operations are used by the videobuf2 core to implement the memory
10662306a36Sopenharmony_ci * handling/memory allocators for each type of supported streaming I/O method.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * .. note::
10962306a36Sopenharmony_ci *    #) Required ops for USERPTR types: get_userptr, put_userptr.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci *    #) Required ops for MMAP types: alloc, put, num_users, mmap.
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci *    #) Required ops for read/write access types: alloc, put, num_users, vaddr.
11462306a36Sopenharmony_ci *
11562306a36Sopenharmony_ci *    #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
11662306a36Sopenharmony_ci *       map_dmabuf, unmap_dmabuf.
11762306a36Sopenharmony_ci */
11862306a36Sopenharmony_cistruct vb2_mem_ops {
11962306a36Sopenharmony_ci	void		*(*alloc)(struct vb2_buffer *vb,
12062306a36Sopenharmony_ci				  struct device *dev,
12162306a36Sopenharmony_ci				  unsigned long size);
12262306a36Sopenharmony_ci	void		(*put)(void *buf_priv);
12362306a36Sopenharmony_ci	struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb,
12462306a36Sopenharmony_ci				      void *buf_priv,
12562306a36Sopenharmony_ci				      unsigned long flags);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	void		*(*get_userptr)(struct vb2_buffer *vb,
12862306a36Sopenharmony_ci					struct device *dev,
12962306a36Sopenharmony_ci					unsigned long vaddr,
13062306a36Sopenharmony_ci					unsigned long size);
13162306a36Sopenharmony_ci	void		(*put_userptr)(void *buf_priv);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	void		(*prepare)(void *buf_priv);
13462306a36Sopenharmony_ci	void		(*finish)(void *buf_priv);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	void		*(*attach_dmabuf)(struct vb2_buffer *vb,
13762306a36Sopenharmony_ci					  struct device *dev,
13862306a36Sopenharmony_ci					  struct dma_buf *dbuf,
13962306a36Sopenharmony_ci					  unsigned long size);
14062306a36Sopenharmony_ci	void		(*detach_dmabuf)(void *buf_priv);
14162306a36Sopenharmony_ci	int		(*map_dmabuf)(void *buf_priv);
14262306a36Sopenharmony_ci	void		(*unmap_dmabuf)(void *buf_priv);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	void		*(*vaddr)(struct vb2_buffer *vb, void *buf_priv);
14562306a36Sopenharmony_ci	void		*(*cookie)(struct vb2_buffer *vb, void *buf_priv);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	unsigned int	(*num_users)(void *buf_priv);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	int		(*mmap)(void *buf_priv, struct vm_area_struct *vma);
15062306a36Sopenharmony_ci};
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci/**
15362306a36Sopenharmony_ci * struct vb2_plane - plane information.
15462306a36Sopenharmony_ci * @mem_priv:	private data with this plane.
15562306a36Sopenharmony_ci * @dbuf:	dma_buf - shared buffer object.
15662306a36Sopenharmony_ci * @dbuf_mapped:	flag to show whether dbuf is mapped or not
15762306a36Sopenharmony_ci * @bytesused:	number of bytes occupied by data in the plane (payload).
15862306a36Sopenharmony_ci * @length:	size of this plane (NOT the payload) in bytes. The maximum
15962306a36Sopenharmony_ci *		valid size is MAX_UINT - PAGE_SIZE.
16062306a36Sopenharmony_ci * @min_length:	minimum required size of this plane (NOT the payload) in bytes.
16162306a36Sopenharmony_ci *		@length is always greater or equal to @min_length, and like
16262306a36Sopenharmony_ci *		@length, it is limited to MAX_UINT - PAGE_SIZE.
16362306a36Sopenharmony_ci * @m:		Union with memtype-specific data.
16462306a36Sopenharmony_ci * @m.offset:	when memory in the associated struct vb2_buffer is
16562306a36Sopenharmony_ci *		%VB2_MEMORY_MMAP, equals the offset from the start of
16662306a36Sopenharmony_ci *		the device memory for this plane (or is a "cookie" that
16762306a36Sopenharmony_ci *		should be passed to mmap() called on the video node).
16862306a36Sopenharmony_ci * @m.userptr:	when memory is %VB2_MEMORY_USERPTR, a userspace pointer
16962306a36Sopenharmony_ci *		pointing to this plane.
17062306a36Sopenharmony_ci * @m.fd:	when memory is %VB2_MEMORY_DMABUF, a userspace file
17162306a36Sopenharmony_ci *		descriptor associated with this plane.
17262306a36Sopenharmony_ci * @data_offset:	offset in the plane to the start of data; usually 0,
17362306a36Sopenharmony_ci *		unless there is a header in front of the data.
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * Should contain enough information to be able to cover all the fields
17662306a36Sopenharmony_ci * of &struct v4l2_plane at videodev2.h.
17762306a36Sopenharmony_ci */
17862306a36Sopenharmony_cistruct vb2_plane {
17962306a36Sopenharmony_ci	void			*mem_priv;
18062306a36Sopenharmony_ci	struct dma_buf		*dbuf;
18162306a36Sopenharmony_ci	unsigned int		dbuf_mapped;
18262306a36Sopenharmony_ci	unsigned int		bytesused;
18362306a36Sopenharmony_ci	unsigned int		length;
18462306a36Sopenharmony_ci	unsigned int		min_length;
18562306a36Sopenharmony_ci	union {
18662306a36Sopenharmony_ci		unsigned int	offset;
18762306a36Sopenharmony_ci		unsigned long	userptr;
18862306a36Sopenharmony_ci		int		fd;
18962306a36Sopenharmony_ci	} m;
19062306a36Sopenharmony_ci	unsigned int		data_offset;
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci/**
19462306a36Sopenharmony_ci * enum vb2_io_modes - queue access methods.
19562306a36Sopenharmony_ci * @VB2_MMAP:		driver supports MMAP with streaming API.
19662306a36Sopenharmony_ci * @VB2_USERPTR:	driver supports USERPTR with streaming API.
19762306a36Sopenharmony_ci * @VB2_READ:		driver supports read() style access.
19862306a36Sopenharmony_ci * @VB2_WRITE:		driver supports write() style access.
19962306a36Sopenharmony_ci * @VB2_DMABUF:		driver supports DMABUF with streaming API.
20062306a36Sopenharmony_ci */
20162306a36Sopenharmony_cienum vb2_io_modes {
20262306a36Sopenharmony_ci	VB2_MMAP	= BIT(0),
20362306a36Sopenharmony_ci	VB2_USERPTR	= BIT(1),
20462306a36Sopenharmony_ci	VB2_READ	= BIT(2),
20562306a36Sopenharmony_ci	VB2_WRITE	= BIT(3),
20662306a36Sopenharmony_ci	VB2_DMABUF	= BIT(4),
20762306a36Sopenharmony_ci};
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/**
21062306a36Sopenharmony_ci * enum vb2_buffer_state - current video buffer state.
21162306a36Sopenharmony_ci * @VB2_BUF_STATE_DEQUEUED:	buffer under userspace control.
21262306a36Sopenharmony_ci * @VB2_BUF_STATE_IN_REQUEST:	buffer is queued in media request.
21362306a36Sopenharmony_ci * @VB2_BUF_STATE_PREPARING:	buffer is being prepared in videobuf2.
21462306a36Sopenharmony_ci * @VB2_BUF_STATE_QUEUED:	buffer queued in videobuf2, but not in driver.
21562306a36Sopenharmony_ci * @VB2_BUF_STATE_ACTIVE:	buffer queued in driver and possibly used
21662306a36Sopenharmony_ci *				in a hardware operation.
21762306a36Sopenharmony_ci * @VB2_BUF_STATE_DONE:		buffer returned from driver to videobuf2, but
21862306a36Sopenharmony_ci *				not yet dequeued to userspace.
21962306a36Sopenharmony_ci * @VB2_BUF_STATE_ERROR:	same as above, but the operation on the buffer
22062306a36Sopenharmony_ci *				has ended with an error, which will be reported
22162306a36Sopenharmony_ci *				to the userspace when it is dequeued.
22262306a36Sopenharmony_ci */
22362306a36Sopenharmony_cienum vb2_buffer_state {
22462306a36Sopenharmony_ci	VB2_BUF_STATE_DEQUEUED,
22562306a36Sopenharmony_ci	VB2_BUF_STATE_IN_REQUEST,
22662306a36Sopenharmony_ci	VB2_BUF_STATE_PREPARING,
22762306a36Sopenharmony_ci	VB2_BUF_STATE_QUEUED,
22862306a36Sopenharmony_ci	VB2_BUF_STATE_ACTIVE,
22962306a36Sopenharmony_ci	VB2_BUF_STATE_DONE,
23062306a36Sopenharmony_ci	VB2_BUF_STATE_ERROR,
23162306a36Sopenharmony_ci};
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistruct vb2_queue;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/**
23662306a36Sopenharmony_ci * struct vb2_buffer - represents a video buffer.
23762306a36Sopenharmony_ci * @vb2_queue:		pointer to &struct vb2_queue with the queue to
23862306a36Sopenharmony_ci *			which this driver belongs.
23962306a36Sopenharmony_ci * @index:		id number of the buffer.
24062306a36Sopenharmony_ci * @type:		buffer type.
24162306a36Sopenharmony_ci * @memory:		the method, in which the actual data is passed.
24262306a36Sopenharmony_ci * @num_planes:		number of planes in the buffer
24362306a36Sopenharmony_ci *			on an internal driver queue.
24462306a36Sopenharmony_ci * @timestamp:		frame timestamp in ns.
24562306a36Sopenharmony_ci * @request:		the request this buffer is associated with.
24662306a36Sopenharmony_ci * @req_obj:		used to bind this buffer to a request. This
24762306a36Sopenharmony_ci *			request object has a refcount.
24862306a36Sopenharmony_ci */
24962306a36Sopenharmony_cistruct vb2_buffer {
25062306a36Sopenharmony_ci	struct vb2_queue	*vb2_queue;
25162306a36Sopenharmony_ci	unsigned int		index;
25262306a36Sopenharmony_ci	unsigned int		type;
25362306a36Sopenharmony_ci	unsigned int		memory;
25462306a36Sopenharmony_ci	unsigned int		num_planes;
25562306a36Sopenharmony_ci	u64			timestamp;
25662306a36Sopenharmony_ci	struct media_request	*request;
25762306a36Sopenharmony_ci	struct media_request_object	req_obj;
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	/* private: internal use only
26062306a36Sopenharmony_ci	 *
26162306a36Sopenharmony_ci	 * state:		current buffer state; do not change
26262306a36Sopenharmony_ci	 * synced:		this buffer has been synced for DMA, i.e. the
26362306a36Sopenharmony_ci	 *			'prepare' memop was called. It is cleared again
26462306a36Sopenharmony_ci	 *			after the 'finish' memop is called.
26562306a36Sopenharmony_ci	 * prepared:		this buffer has been prepared, i.e. the
26662306a36Sopenharmony_ci	 *			buf_prepare op was called. It is cleared again
26762306a36Sopenharmony_ci	 *			after the 'buf_finish' op is called.
26862306a36Sopenharmony_ci	 * copied_timestamp:	the timestamp of this capture buffer was copied
26962306a36Sopenharmony_ci	 *			from an output buffer.
27062306a36Sopenharmony_ci	 * skip_cache_sync_on_prepare: when set buffer's ->prepare() function
27162306a36Sopenharmony_ci	 *			skips cache sync/invalidation.
27262306a36Sopenharmony_ci	 * skip_cache_sync_on_finish: when set buffer's ->finish() function
27362306a36Sopenharmony_ci	 *			skips cache sync/invalidation.
27462306a36Sopenharmony_ci	 * queued_entry:	entry on the queued buffers list, which holds
27562306a36Sopenharmony_ci	 *			all buffers queued from userspace
27662306a36Sopenharmony_ci	 * done_entry:		entry on the list that stores all buffers ready
27762306a36Sopenharmony_ci	 *			to be dequeued to userspace
27862306a36Sopenharmony_ci	 * vb2_plane:		per-plane information; do not change
27962306a36Sopenharmony_ci	 */
28062306a36Sopenharmony_ci	enum vb2_buffer_state	state;
28162306a36Sopenharmony_ci	unsigned int		synced:1;
28262306a36Sopenharmony_ci	unsigned int		prepared:1;
28362306a36Sopenharmony_ci	unsigned int		copied_timestamp:1;
28462306a36Sopenharmony_ci	unsigned int		skip_cache_sync_on_prepare:1;
28562306a36Sopenharmony_ci	unsigned int		skip_cache_sync_on_finish:1;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	struct vb2_plane	planes[VB2_MAX_PLANES];
28862306a36Sopenharmony_ci	struct list_head	queued_entry;
28962306a36Sopenharmony_ci	struct list_head	done_entry;
29062306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
29162306a36Sopenharmony_ci	/*
29262306a36Sopenharmony_ci	 * Counters for how often these buffer-related ops are
29362306a36Sopenharmony_ci	 * called. Used to check for unbalanced ops.
29462306a36Sopenharmony_ci	 */
29562306a36Sopenharmony_ci	u32		cnt_mem_alloc;
29662306a36Sopenharmony_ci	u32		cnt_mem_put;
29762306a36Sopenharmony_ci	u32		cnt_mem_get_dmabuf;
29862306a36Sopenharmony_ci	u32		cnt_mem_get_userptr;
29962306a36Sopenharmony_ci	u32		cnt_mem_put_userptr;
30062306a36Sopenharmony_ci	u32		cnt_mem_prepare;
30162306a36Sopenharmony_ci	u32		cnt_mem_finish;
30262306a36Sopenharmony_ci	u32		cnt_mem_attach_dmabuf;
30362306a36Sopenharmony_ci	u32		cnt_mem_detach_dmabuf;
30462306a36Sopenharmony_ci	u32		cnt_mem_map_dmabuf;
30562306a36Sopenharmony_ci	u32		cnt_mem_unmap_dmabuf;
30662306a36Sopenharmony_ci	u32		cnt_mem_vaddr;
30762306a36Sopenharmony_ci	u32		cnt_mem_cookie;
30862306a36Sopenharmony_ci	u32		cnt_mem_num_users;
30962306a36Sopenharmony_ci	u32		cnt_mem_mmap;
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	u32		cnt_buf_out_validate;
31262306a36Sopenharmony_ci	u32		cnt_buf_init;
31362306a36Sopenharmony_ci	u32		cnt_buf_prepare;
31462306a36Sopenharmony_ci	u32		cnt_buf_finish;
31562306a36Sopenharmony_ci	u32		cnt_buf_cleanup;
31662306a36Sopenharmony_ci	u32		cnt_buf_queue;
31762306a36Sopenharmony_ci	u32		cnt_buf_request_complete;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	/* This counts the number of calls to vb2_buffer_done() */
32062306a36Sopenharmony_ci	u32		cnt_buf_done;
32162306a36Sopenharmony_ci#endif
32262306a36Sopenharmony_ci};
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci/**
32562306a36Sopenharmony_ci * struct vb2_ops - driver-specific callbacks.
32662306a36Sopenharmony_ci *
32762306a36Sopenharmony_ci * These operations are not called from interrupt context except where
32862306a36Sopenharmony_ci * mentioned specifically.
32962306a36Sopenharmony_ci *
33062306a36Sopenharmony_ci * @queue_setup:	called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
33162306a36Sopenharmony_ci *			handlers before memory allocation. It can be called
33262306a36Sopenharmony_ci *			twice: if the original number of requested buffers
33362306a36Sopenharmony_ci *			could not be allocated, then it will be called a
33462306a36Sopenharmony_ci *			second time with the actually allocated number of
33562306a36Sopenharmony_ci *			buffers to verify if that is OK.
33662306a36Sopenharmony_ci *			The driver should return the required number of buffers
33762306a36Sopenharmony_ci *			in \*num_buffers, the required number of planes per
33862306a36Sopenharmony_ci *			buffer in \*num_planes, the size of each plane should be
33962306a36Sopenharmony_ci *			set in the sizes\[\] array and optional per-plane
34062306a36Sopenharmony_ci *			allocator specific device in the alloc_devs\[\] array.
34162306a36Sopenharmony_ci *			When called from VIDIOC_REQBUFS(), \*num_planes == 0,
34262306a36Sopenharmony_ci *			the driver has to use the currently configured format to
34362306a36Sopenharmony_ci *			determine the plane sizes and \*num_buffers is the total
34462306a36Sopenharmony_ci *			number of buffers that are being allocated. When called
34562306a36Sopenharmony_ci *			from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
34662306a36Sopenharmony_ci *			describes the requested number of planes and sizes\[\]
34762306a36Sopenharmony_ci *			contains the requested plane sizes. In this case
34862306a36Sopenharmony_ci *			\*num_buffers are being allocated additionally to
34962306a36Sopenharmony_ci *			q->num_buffers. If either \*num_planes or the requested
35062306a36Sopenharmony_ci *			sizes are invalid callback must return %-EINVAL.
35162306a36Sopenharmony_ci * @wait_prepare:	release any locks taken while calling vb2 functions;
35262306a36Sopenharmony_ci *			it is called before an ioctl needs to wait for a new
35362306a36Sopenharmony_ci *			buffer to arrive; required to avoid a deadlock in
35462306a36Sopenharmony_ci *			blocking access type.
35562306a36Sopenharmony_ci * @wait_finish:	reacquire all locks released in the previous callback;
35662306a36Sopenharmony_ci *			required to continue operation after sleeping while
35762306a36Sopenharmony_ci *			waiting for a new buffer to arrive.
35862306a36Sopenharmony_ci * @buf_out_validate:	called when the output buffer is prepared or queued
35962306a36Sopenharmony_ci *			to a request; drivers can use this to validate
36062306a36Sopenharmony_ci *			userspace-provided information; this is required only
36162306a36Sopenharmony_ci *			for OUTPUT queues.
36262306a36Sopenharmony_ci * @buf_init:		called once after allocating a buffer (in MMAP case)
36362306a36Sopenharmony_ci *			or after acquiring a new USERPTR buffer; drivers may
36462306a36Sopenharmony_ci *			perform additional buffer-related initialization;
36562306a36Sopenharmony_ci *			initialization failure (return != 0) will prevent
36662306a36Sopenharmony_ci *			queue setup from completing successfully; optional.
36762306a36Sopenharmony_ci * @buf_prepare:	called every time the buffer is queued from userspace
36862306a36Sopenharmony_ci *			and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
36962306a36Sopenharmony_ci *			perform any initialization required before each
37062306a36Sopenharmony_ci *			hardware operation in this callback; drivers can
37162306a36Sopenharmony_ci *			access/modify the buffer here as it is still synced for
37262306a36Sopenharmony_ci *			the CPU; drivers that support VIDIOC_CREATE_BUFS() must
37362306a36Sopenharmony_ci *			also validate the buffer size; if an error is returned,
37462306a36Sopenharmony_ci *			the buffer will not be queued in driver; optional.
37562306a36Sopenharmony_ci * @buf_finish:		called before every dequeue of the buffer back to
37662306a36Sopenharmony_ci *			userspace; the buffer is synced for the CPU, so drivers
37762306a36Sopenharmony_ci *			can access/modify the buffer contents; drivers may
37862306a36Sopenharmony_ci *			perform any operations required before userspace
37962306a36Sopenharmony_ci *			accesses the buffer; optional. The buffer state can be
38062306a36Sopenharmony_ci *			one of the following: %DONE and %ERROR occur while
38162306a36Sopenharmony_ci *			streaming is in progress, and the %PREPARED state occurs
38262306a36Sopenharmony_ci *			when the queue has been canceled and all pending
38362306a36Sopenharmony_ci *			buffers are being returned to their default %DEQUEUED
38462306a36Sopenharmony_ci *			state. Typically you only have to do something if the
38562306a36Sopenharmony_ci *			state is %VB2_BUF_STATE_DONE, since in all other cases
38662306a36Sopenharmony_ci *			the buffer contents will be ignored anyway.
38762306a36Sopenharmony_ci * @buf_cleanup:	called once before the buffer is freed; drivers may
38862306a36Sopenharmony_ci *			perform any additional cleanup; optional.
38962306a36Sopenharmony_ci * @prepare_streaming:	called once to prepare for 'streaming' state; this is
39062306a36Sopenharmony_ci *			where validation can be done to verify everything is
39162306a36Sopenharmony_ci *			okay and streaming resources can be claimed. It is
39262306a36Sopenharmony_ci *			called when the VIDIOC_STREAMON ioctl is called. The
39362306a36Sopenharmony_ci *			actual streaming starts when @start_streaming is called.
39462306a36Sopenharmony_ci *			Optional.
39562306a36Sopenharmony_ci * @start_streaming:	called once to enter 'streaming' state; the driver may
39662306a36Sopenharmony_ci *			receive buffers with @buf_queue callback
39762306a36Sopenharmony_ci *			before @start_streaming is called; the driver gets the
39862306a36Sopenharmony_ci *			number of already queued buffers in count parameter;
39962306a36Sopenharmony_ci *			driver can return an error if hardware fails, in that
40062306a36Sopenharmony_ci *			case all buffers that have been already given by
40162306a36Sopenharmony_ci *			the @buf_queue callback are to be returned by the driver
40262306a36Sopenharmony_ci *			by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
40362306a36Sopenharmony_ci *			If you need a minimum number of buffers before you can
40462306a36Sopenharmony_ci *			start streaming, then set
40562306a36Sopenharmony_ci *			&vb2_queue->min_buffers_needed. If that is non-zero
40662306a36Sopenharmony_ci *			then @start_streaming won't be called until at least
40762306a36Sopenharmony_ci *			that many buffers have been queued up by userspace.
40862306a36Sopenharmony_ci * @stop_streaming:	called when 'streaming' state must be disabled; driver
40962306a36Sopenharmony_ci *			should stop any DMA transactions or wait until they
41062306a36Sopenharmony_ci *			finish and give back all buffers it got from &buf_queue
41162306a36Sopenharmony_ci *			callback by calling vb2_buffer_done() with either
41262306a36Sopenharmony_ci *			%VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
41362306a36Sopenharmony_ci *			vb2_wait_for_all_buffers() function
41462306a36Sopenharmony_ci * @unprepare_streaming:called as counterpart to @prepare_streaming; any claimed
41562306a36Sopenharmony_ci *			streaming resources can be released here. It is
41662306a36Sopenharmony_ci *			called when the VIDIOC_STREAMOFF ioctls is called or
41762306a36Sopenharmony_ci *			when the streaming filehandle is closed. Optional.
41862306a36Sopenharmony_ci * @buf_queue:		passes buffer vb to the driver; driver may start
41962306a36Sopenharmony_ci *			hardware operation on this buffer; driver should give
42062306a36Sopenharmony_ci *			the buffer back by calling vb2_buffer_done() function;
42162306a36Sopenharmony_ci *			it is always called after calling VIDIOC_STREAMON()
42262306a36Sopenharmony_ci *			ioctl; might be called before @start_streaming callback
42362306a36Sopenharmony_ci *			if user pre-queued buffers before calling
42462306a36Sopenharmony_ci *			VIDIOC_STREAMON().
42562306a36Sopenharmony_ci * @buf_request_complete: a buffer that was never queued to the driver but is
42662306a36Sopenharmony_ci *			associated with a queued request was canceled.
42762306a36Sopenharmony_ci *			The driver will have to mark associated objects in the
42862306a36Sopenharmony_ci *			request as completed; required if requests are
42962306a36Sopenharmony_ci *			supported.
43062306a36Sopenharmony_ci */
43162306a36Sopenharmony_cistruct vb2_ops {
43262306a36Sopenharmony_ci	int (*queue_setup)(struct vb2_queue *q,
43362306a36Sopenharmony_ci			   unsigned int *num_buffers, unsigned int *num_planes,
43462306a36Sopenharmony_ci			   unsigned int sizes[], struct device *alloc_devs[]);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	void (*wait_prepare)(struct vb2_queue *q);
43762306a36Sopenharmony_ci	void (*wait_finish)(struct vb2_queue *q);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	int (*buf_out_validate)(struct vb2_buffer *vb);
44062306a36Sopenharmony_ci	int (*buf_init)(struct vb2_buffer *vb);
44162306a36Sopenharmony_ci	int (*buf_prepare)(struct vb2_buffer *vb);
44262306a36Sopenharmony_ci	void (*buf_finish)(struct vb2_buffer *vb);
44362306a36Sopenharmony_ci	void (*buf_cleanup)(struct vb2_buffer *vb);
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	int (*prepare_streaming)(struct vb2_queue *q);
44662306a36Sopenharmony_ci	int (*start_streaming)(struct vb2_queue *q, unsigned int count);
44762306a36Sopenharmony_ci	void (*stop_streaming)(struct vb2_queue *q);
44862306a36Sopenharmony_ci	void (*unprepare_streaming)(struct vb2_queue *q);
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	void (*buf_queue)(struct vb2_buffer *vb);
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	void (*buf_request_complete)(struct vb2_buffer *vb);
45362306a36Sopenharmony_ci};
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci/**
45662306a36Sopenharmony_ci * struct vb2_buf_ops - driver-specific callbacks.
45762306a36Sopenharmony_ci *
45862306a36Sopenharmony_ci * @verify_planes_array: Verify that a given user space structure contains
45962306a36Sopenharmony_ci *			enough planes for the buffer. This is called
46062306a36Sopenharmony_ci *			for each dequeued buffer.
46162306a36Sopenharmony_ci * @init_buffer:	given a &vb2_buffer initialize the extra data after
46262306a36Sopenharmony_ci *			struct vb2_buffer.
46362306a36Sopenharmony_ci *			For V4L2 this is a &struct vb2_v4l2_buffer.
46462306a36Sopenharmony_ci * @fill_user_buffer:	given a &vb2_buffer fill in the userspace structure.
46562306a36Sopenharmony_ci *			For V4L2 this is a &struct v4l2_buffer.
46662306a36Sopenharmony_ci * @fill_vb2_buffer:	given a userspace structure, fill in the &vb2_buffer.
46762306a36Sopenharmony_ci *			If the userspace structure is invalid, then this op
46862306a36Sopenharmony_ci *			will return an error.
46962306a36Sopenharmony_ci * @copy_timestamp:	copy the timestamp from a userspace structure to
47062306a36Sopenharmony_ci *			the &struct vb2_buffer.
47162306a36Sopenharmony_ci */
47262306a36Sopenharmony_cistruct vb2_buf_ops {
47362306a36Sopenharmony_ci	int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
47462306a36Sopenharmony_ci	void (*init_buffer)(struct vb2_buffer *vb);
47562306a36Sopenharmony_ci	void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
47662306a36Sopenharmony_ci	int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes);
47762306a36Sopenharmony_ci	void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
47862306a36Sopenharmony_ci};
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/**
48162306a36Sopenharmony_ci * struct vb2_queue - a videobuf2 queue.
48262306a36Sopenharmony_ci *
48362306a36Sopenharmony_ci * @type:	private buffer type whose content is defined by the vb2-core
48462306a36Sopenharmony_ci *		caller. For example, for V4L2, it should match
48562306a36Sopenharmony_ci *		the types defined on &enum v4l2_buf_type.
48662306a36Sopenharmony_ci * @io_modes:	supported io methods (see &enum vb2_io_modes).
48762306a36Sopenharmony_ci * @alloc_devs:	&struct device memory type/allocator-specific per-plane device
48862306a36Sopenharmony_ci * @dev:	device to use for the default allocation context if the driver
48962306a36Sopenharmony_ci *		doesn't fill in the @alloc_devs array.
49062306a36Sopenharmony_ci * @dma_attrs:	DMA attributes to use for the DMA.
49162306a36Sopenharmony_ci * @bidirectional: when this flag is set the DMA direction for the buffers of
49262306a36Sopenharmony_ci *		this queue will be overridden with %DMA_BIDIRECTIONAL direction.
49362306a36Sopenharmony_ci *		This is useful in cases where the hardware (firmware) writes to
49462306a36Sopenharmony_ci *		a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from
49562306a36Sopenharmony_ci *		buffer which is mapped for write (%DMA_FROM_DEVICE) in order
49662306a36Sopenharmony_ci *		to satisfy some internal hardware restrictions or adds a padding
49762306a36Sopenharmony_ci *		needed by the processing algorithm. In case the DMA mapping is
49862306a36Sopenharmony_ci *		not bidirectional but the hardware (firmware) trying to access
49962306a36Sopenharmony_ci *		the buffer (in the opposite direction) this could lead to an
50062306a36Sopenharmony_ci *		IOMMU protection faults.
50162306a36Sopenharmony_ci * @fileio_read_once:		report EOF after reading the first buffer
50262306a36Sopenharmony_ci * @fileio_write_immediately:	queue buffer after each write() call
50362306a36Sopenharmony_ci * @allow_zero_bytesused:	allow bytesused == 0 to be passed to the driver
50462306a36Sopenharmony_ci * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF
50562306a36Sopenharmony_ci *              has not been called. This is a vb1 idiom that has been adopted
50662306a36Sopenharmony_ci *              also by vb2.
50762306a36Sopenharmony_ci * @supports_requests: this queue supports the Request API.
50862306a36Sopenharmony_ci * @requires_requests: this queue requires the Request API. If this is set to 1,
50962306a36Sopenharmony_ci *		then supports_requests must be set to 1 as well.
51062306a36Sopenharmony_ci * @uses_qbuf:	qbuf was used directly for this queue. Set to 1 the first
51162306a36Sopenharmony_ci *		time this is called. Set to 0 when the queue is canceled.
51262306a36Sopenharmony_ci *		If this is 1, then you cannot queue buffers from a request.
51362306a36Sopenharmony_ci * @uses_requests: requests are used for this queue. Set to 1 the first time
51462306a36Sopenharmony_ci *		a request is queued. Set to 0 when the queue is canceled.
51562306a36Sopenharmony_ci *		If this is 1, then you cannot queue buffers directly.
51662306a36Sopenharmony_ci * @allow_cache_hints: when set user-space can pass cache management hints in
51762306a36Sopenharmony_ci *		order to skip cache flush/invalidation on ->prepare() or/and
51862306a36Sopenharmony_ci *		->finish().
51962306a36Sopenharmony_ci * @non_coherent_mem: when set queue will attempt to allocate buffers using
52062306a36Sopenharmony_ci *		non-coherent memory.
52162306a36Sopenharmony_ci * @lock:	pointer to a mutex that protects the &struct vb2_queue. The
52262306a36Sopenharmony_ci *		driver can set this to a mutex to let the v4l2 core serialize
52362306a36Sopenharmony_ci *		the queuing ioctls. If the driver wants to handle locking
52462306a36Sopenharmony_ci *		itself, then this should be set to NULL. This lock is not used
52562306a36Sopenharmony_ci *		by the videobuf2 core API.
52662306a36Sopenharmony_ci * @owner:	The filehandle that 'owns' the buffers, i.e. the filehandle
52762306a36Sopenharmony_ci *		that called reqbufs, create_buffers or started fileio.
52862306a36Sopenharmony_ci *		This field is not used by the videobuf2 core API, but it allows
52962306a36Sopenharmony_ci *		drivers to easily associate an owner filehandle with the queue.
53062306a36Sopenharmony_ci * @ops:	driver-specific callbacks
53162306a36Sopenharmony_ci * @mem_ops:	memory allocator specific callbacks
53262306a36Sopenharmony_ci * @buf_ops:	callbacks to deliver buffer information.
53362306a36Sopenharmony_ci *		between user-space and kernel-space.
53462306a36Sopenharmony_ci * @drv_priv:	driver private data.
53562306a36Sopenharmony_ci * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used
53662306a36Sopenharmony_ci *		by the vb2 core.
53762306a36Sopenharmony_ci * @buf_struct_size: size of the driver-specific buffer structure;
53862306a36Sopenharmony_ci *		"0" indicates the driver doesn't want to use a custom buffer
53962306a36Sopenharmony_ci *		structure type. In that case a subsystem-specific struct
54062306a36Sopenharmony_ci *		will be used (in the case of V4L2 that is
54162306a36Sopenharmony_ci *		``sizeof(struct vb2_v4l2_buffer)``). The first field of the
54262306a36Sopenharmony_ci *		driver-specific buffer structure must be the subsystem-specific
54362306a36Sopenharmony_ci *		struct (vb2_v4l2_buffer in the case of V4L2).
54462306a36Sopenharmony_ci * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and
54562306a36Sopenharmony_ci *		``V4L2_BUF_FLAG_TSTAMP_SRC_*``
54662306a36Sopenharmony_ci * @gfp_flags:	additional gfp flags used when allocating the buffers.
54762306a36Sopenharmony_ci *		Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32
54862306a36Sopenharmony_ci *		to force the buffer allocation to a specific memory zone.
54962306a36Sopenharmony_ci * @min_buffers_needed: the minimum number of buffers needed before
55062306a36Sopenharmony_ci *		@start_streaming can be called. Used when a DMA engine
55162306a36Sopenharmony_ci *		cannot be started unless at least this number of buffers
55262306a36Sopenharmony_ci *		have been queued into the driver.
55362306a36Sopenharmony_ci */
55462306a36Sopenharmony_ci/*
55562306a36Sopenharmony_ci * Private elements (won't appear at the uAPI book):
55662306a36Sopenharmony_ci * @mmap_lock:	private mutex used when buffers are allocated/freed/mmapped
55762306a36Sopenharmony_ci * @memory:	current memory type used
55862306a36Sopenharmony_ci * @dma_dir:	DMA mapping direction.
55962306a36Sopenharmony_ci * @bufs:	videobuf2 buffer structures
56062306a36Sopenharmony_ci * @num_buffers: number of allocated/used buffers
56162306a36Sopenharmony_ci * @queued_list: list of buffers currently queued from userspace
56262306a36Sopenharmony_ci * @queued_count: number of buffers queued and ready for streaming.
56362306a36Sopenharmony_ci * @owned_by_drv_count: number of buffers owned by the driver
56462306a36Sopenharmony_ci * @done_list:	list of buffers ready to be dequeued to userspace
56562306a36Sopenharmony_ci * @done_lock:	lock to protect done_list list
56662306a36Sopenharmony_ci * @done_wq:	waitqueue for processes waiting for buffers ready to be dequeued
56762306a36Sopenharmony_ci * @streaming:	current streaming state
56862306a36Sopenharmony_ci * @start_streaming_called: @start_streaming was called successfully and we
56962306a36Sopenharmony_ci *		started streaming.
57062306a36Sopenharmony_ci * @error:	a fatal error occurred on the queue
57162306a36Sopenharmony_ci * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
57262306a36Sopenharmony_ci *		buffers. Only set for capture queues if qbuf has not yet been
57362306a36Sopenharmony_ci *		called since poll() needs to return %EPOLLERR in that situation.
57462306a36Sopenharmony_ci * @is_multiplanar: set if buffer type is multiplanar
57562306a36Sopenharmony_ci * @is_output:	set if buffer type is output
57662306a36Sopenharmony_ci * @copy_timestamp: set if vb2-core should set timestamps
57762306a36Sopenharmony_ci * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
57862306a36Sopenharmony_ci *		last decoded buffer was already dequeued. Set for capture queues
57962306a36Sopenharmony_ci *		when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued.
58062306a36Sopenharmony_ci * @fileio:	file io emulator internal data, used only if emulator is active
58162306a36Sopenharmony_ci * @threadio:	thread io internal data, used only if thread is active
58262306a36Sopenharmony_ci * @name:	queue name, used for logging purpose. Initialized automatically
58362306a36Sopenharmony_ci *		if left empty by drivers.
58462306a36Sopenharmony_ci */
58562306a36Sopenharmony_cistruct vb2_queue {
58662306a36Sopenharmony_ci	unsigned int			type;
58762306a36Sopenharmony_ci	unsigned int			io_modes;
58862306a36Sopenharmony_ci	struct device			*dev;
58962306a36Sopenharmony_ci	unsigned long			dma_attrs;
59062306a36Sopenharmony_ci	unsigned int			bidirectional:1;
59162306a36Sopenharmony_ci	unsigned int			fileio_read_once:1;
59262306a36Sopenharmony_ci	unsigned int			fileio_write_immediately:1;
59362306a36Sopenharmony_ci	unsigned int			allow_zero_bytesused:1;
59462306a36Sopenharmony_ci	unsigned int		   quirk_poll_must_check_waiting_for_buffers:1;
59562306a36Sopenharmony_ci	unsigned int			supports_requests:1;
59662306a36Sopenharmony_ci	unsigned int			requires_requests:1;
59762306a36Sopenharmony_ci	unsigned int			uses_qbuf:1;
59862306a36Sopenharmony_ci	unsigned int			uses_requests:1;
59962306a36Sopenharmony_ci	unsigned int			allow_cache_hints:1;
60062306a36Sopenharmony_ci	unsigned int			non_coherent_mem:1;
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	struct mutex			*lock;
60362306a36Sopenharmony_ci	void				*owner;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	const struct vb2_ops		*ops;
60662306a36Sopenharmony_ci	const struct vb2_mem_ops	*mem_ops;
60762306a36Sopenharmony_ci	const struct vb2_buf_ops	*buf_ops;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	void				*drv_priv;
61062306a36Sopenharmony_ci	u32				subsystem_flags;
61162306a36Sopenharmony_ci	unsigned int			buf_struct_size;
61262306a36Sopenharmony_ci	u32				timestamp_flags;
61362306a36Sopenharmony_ci	gfp_t				gfp_flags;
61462306a36Sopenharmony_ci	u32				min_buffers_needed;
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci	struct device			*alloc_devs[VB2_MAX_PLANES];
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	/* private: internal use only */
61962306a36Sopenharmony_ci	struct mutex			mmap_lock;
62062306a36Sopenharmony_ci	unsigned int			memory;
62162306a36Sopenharmony_ci	enum dma_data_direction		dma_dir;
62262306a36Sopenharmony_ci	struct vb2_buffer		*bufs[VB2_MAX_FRAME];
62362306a36Sopenharmony_ci	unsigned int			num_buffers;
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ci	struct list_head		queued_list;
62662306a36Sopenharmony_ci	unsigned int			queued_count;
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci	atomic_t			owned_by_drv_count;
62962306a36Sopenharmony_ci	struct list_head		done_list;
63062306a36Sopenharmony_ci	spinlock_t			done_lock;
63162306a36Sopenharmony_ci	wait_queue_head_t		done_wq;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	unsigned int			streaming:1;
63462306a36Sopenharmony_ci	unsigned int			start_streaming_called:1;
63562306a36Sopenharmony_ci	unsigned int			error:1;
63662306a36Sopenharmony_ci	unsigned int			waiting_for_buffers:1;
63762306a36Sopenharmony_ci	unsigned int			waiting_in_dqbuf:1;
63862306a36Sopenharmony_ci	unsigned int			is_multiplanar:1;
63962306a36Sopenharmony_ci	unsigned int			is_output:1;
64062306a36Sopenharmony_ci	unsigned int			copy_timestamp:1;
64162306a36Sopenharmony_ci	unsigned int			last_buffer_dequeued:1;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	struct vb2_fileio_data		*fileio;
64462306a36Sopenharmony_ci	struct vb2_threadio_data	*threadio;
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	char				name[32];
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
64962306a36Sopenharmony_ci	/*
65062306a36Sopenharmony_ci	 * Counters for how often these queue-related ops are
65162306a36Sopenharmony_ci	 * called. Used to check for unbalanced ops.
65262306a36Sopenharmony_ci	 */
65362306a36Sopenharmony_ci	u32				cnt_queue_setup;
65462306a36Sopenharmony_ci	u32				cnt_wait_prepare;
65562306a36Sopenharmony_ci	u32				cnt_wait_finish;
65662306a36Sopenharmony_ci	u32				cnt_prepare_streaming;
65762306a36Sopenharmony_ci	u32				cnt_start_streaming;
65862306a36Sopenharmony_ci	u32				cnt_stop_streaming;
65962306a36Sopenharmony_ci	u32				cnt_unprepare_streaming;
66062306a36Sopenharmony_ci#endif
66162306a36Sopenharmony_ci};
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci/**
66462306a36Sopenharmony_ci * vb2_queue_allows_cache_hints() - Return true if the queue allows cache
66562306a36Sopenharmony_ci * and memory consistency hints.
66662306a36Sopenharmony_ci *
66762306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue
66862306a36Sopenharmony_ci */
66962306a36Sopenharmony_cistatic inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
67062306a36Sopenharmony_ci{
67162306a36Sopenharmony_ci	return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
67262306a36Sopenharmony_ci}
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci/**
67562306a36Sopenharmony_ci * vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
67662306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to which the plane in
67762306a36Sopenharmony_ci *		question belongs to.
67862306a36Sopenharmony_ci * @plane_no:	plane number for which the address is to be returned.
67962306a36Sopenharmony_ci *
68062306a36Sopenharmony_ci * This function returns a kernel virtual address of a given plane if
68162306a36Sopenharmony_ci * such a mapping exist, NULL otherwise.
68262306a36Sopenharmony_ci */
68362306a36Sopenharmony_civoid *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci/**
68662306a36Sopenharmony_ci * vb2_plane_cookie() - Return allocator specific cookie for the given plane.
68762306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to which the plane in
68862306a36Sopenharmony_ci *		question belongs to.
68962306a36Sopenharmony_ci * @plane_no:	plane number for which the cookie is to be returned.
69062306a36Sopenharmony_ci *
69162306a36Sopenharmony_ci * This function returns an allocator specific cookie for a given plane if
69262306a36Sopenharmony_ci * available, NULL otherwise. The allocator should provide some simple static
69362306a36Sopenharmony_ci * inline function, which would convert this cookie to the allocator specific
69462306a36Sopenharmony_ci * type that can be used directly by the driver to access the buffer. This can
69562306a36Sopenharmony_ci * be for example physical address, pointer to scatter list or IOMMU mapping.
69662306a36Sopenharmony_ci */
69762306a36Sopenharmony_civoid *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci/**
70062306a36Sopenharmony_ci * vb2_buffer_done() - inform videobuf2 that an operation on a buffer
70162306a36Sopenharmony_ci *	is finished.
70262306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to be used.
70362306a36Sopenharmony_ci * @state:	state of the buffer, as defined by &enum vb2_buffer_state.
70462306a36Sopenharmony_ci *		Either %VB2_BUF_STATE_DONE if the operation finished
70562306a36Sopenharmony_ci *		successfully, %VB2_BUF_STATE_ERROR if the operation finished
70662306a36Sopenharmony_ci *		with an error or %VB2_BUF_STATE_QUEUED.
70762306a36Sopenharmony_ci *
70862306a36Sopenharmony_ci * This function should be called by the driver after a hardware operation on
70962306a36Sopenharmony_ci * a buffer is finished and the buffer may be returned to userspace. The driver
71062306a36Sopenharmony_ci * cannot use this buffer anymore until it is queued back to it by videobuf
71162306a36Sopenharmony_ci * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
71262306a36Sopenharmony_ci * to the driver by &vb2_ops->buf_queue can be passed to this function.
71362306a36Sopenharmony_ci *
71462306a36Sopenharmony_ci * While streaming a buffer can only be returned in state DONE or ERROR.
71562306a36Sopenharmony_ci * The &vb2_ops->start_streaming op can also return them in case the DMA engine
71662306a36Sopenharmony_ci * cannot be started for some reason. In that case the buffers should be
71762306a36Sopenharmony_ci * returned with state QUEUED to put them back into the queue.
71862306a36Sopenharmony_ci */
71962306a36Sopenharmony_civoid vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci/**
72262306a36Sopenharmony_ci * vb2_discard_done() - discard all buffers marked as DONE.
72362306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
72462306a36Sopenharmony_ci *
72562306a36Sopenharmony_ci * This function is intended to be used with suspend/resume operations. It
72662306a36Sopenharmony_ci * discards all 'done' buffers as they would be too old to be requested after
72762306a36Sopenharmony_ci * resume.
72862306a36Sopenharmony_ci *
72962306a36Sopenharmony_ci * Drivers must stop the hardware and synchronize with interrupt handlers and/or
73062306a36Sopenharmony_ci * delayed works before calling this function to make sure no buffer will be
73162306a36Sopenharmony_ci * touched by the driver and/or hardware.
73262306a36Sopenharmony_ci */
73362306a36Sopenharmony_civoid vb2_discard_done(struct vb2_queue *q);
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci/**
73662306a36Sopenharmony_ci * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2.
73762306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
73862306a36Sopenharmony_ci *
73962306a36Sopenharmony_ci * This function will wait until all buffers that have been given to the driver
74062306a36Sopenharmony_ci * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
74162306a36Sopenharmony_ci * doesn't call &vb2_ops->wait_prepare/&vb2_ops->wait_finish pair.
74262306a36Sopenharmony_ci * It is intended to be called with all locks taken, for example from
74362306a36Sopenharmony_ci * &vb2_ops->stop_streaming callback.
74462306a36Sopenharmony_ci */
74562306a36Sopenharmony_ciint vb2_wait_for_all_buffers(struct vb2_queue *q);
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci/**
74862306a36Sopenharmony_ci * vb2_core_querybuf() - query video buffer information.
74962306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
75062306a36Sopenharmony_ci * @index:	id number of the buffer.
75162306a36Sopenharmony_ci * @pb:		buffer struct passed from userspace.
75262306a36Sopenharmony_ci *
75362306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
75462306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
75562306a36Sopenharmony_ci *
75662306a36Sopenharmony_ci * The passed buffer should have been verified.
75762306a36Sopenharmony_ci *
75862306a36Sopenharmony_ci * This function fills the relevant information for the userspace.
75962306a36Sopenharmony_ci *
76062306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
76162306a36Sopenharmony_ci */
76262306a36Sopenharmony_civoid vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci/**
76562306a36Sopenharmony_ci * vb2_core_reqbufs() - Initiate streaming.
76662306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
76762306a36Sopenharmony_ci * @memory:	memory type, as defined by &enum vb2_memory.
76862306a36Sopenharmony_ci * @flags:	auxiliary queue/buffer management flags. Currently, the only
76962306a36Sopenharmony_ci *		used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
77062306a36Sopenharmony_ci * @count:	requested buffer count.
77162306a36Sopenharmony_ci *
77262306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
77362306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
77462306a36Sopenharmony_ci *
77562306a36Sopenharmony_ci * This function:
77662306a36Sopenharmony_ci *
77762306a36Sopenharmony_ci * #) verifies streaming parameters passed from the userspace;
77862306a36Sopenharmony_ci * #) sets up the queue;
77962306a36Sopenharmony_ci * #) negotiates number of buffers and planes per buffer with the driver
78062306a36Sopenharmony_ci *    to be used during streaming;
78162306a36Sopenharmony_ci * #) allocates internal buffer structures (&struct vb2_buffer), according to
78262306a36Sopenharmony_ci *    the agreed parameters;
78362306a36Sopenharmony_ci * #) for MMAP memory type, allocates actual video memory, using the
78462306a36Sopenharmony_ci *    memory handling/allocation routines provided during queue initialization.
78562306a36Sopenharmony_ci *
78662306a36Sopenharmony_ci * If req->count is 0, all the memory will be freed instead.
78762306a36Sopenharmony_ci *
78862306a36Sopenharmony_ci * If the queue has been allocated previously by a previous vb2_core_reqbufs()
78962306a36Sopenharmony_ci * call and the queue is not busy, memory will be reallocated.
79062306a36Sopenharmony_ci *
79162306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
79262306a36Sopenharmony_ci */
79362306a36Sopenharmony_ciint vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
79462306a36Sopenharmony_ci		     unsigned int flags, unsigned int *count);
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci/**
79762306a36Sopenharmony_ci * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
79862306a36Sopenharmony_ci * @q: pointer to &struct vb2_queue with videobuf2 queue.
79962306a36Sopenharmony_ci * @memory: memory type, as defined by &enum vb2_memory.
80062306a36Sopenharmony_ci * @flags: auxiliary queue/buffer management flags.
80162306a36Sopenharmony_ci * @count: requested buffer count.
80262306a36Sopenharmony_ci * @requested_planes: number of planes requested.
80362306a36Sopenharmony_ci * @requested_sizes: array with the size of the planes.
80462306a36Sopenharmony_ci *
80562306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
80662306a36Sopenharmony_ci * called internally by VB2 by an API-specific handler, like
80762306a36Sopenharmony_ci * ``videobuf2-v4l2.h``.
80862306a36Sopenharmony_ci *
80962306a36Sopenharmony_ci * This function:
81062306a36Sopenharmony_ci *
81162306a36Sopenharmony_ci * #) verifies parameter sanity;
81262306a36Sopenharmony_ci * #) calls the &vb2_ops->queue_setup queue operation;
81362306a36Sopenharmony_ci * #) performs any necessary memory allocations.
81462306a36Sopenharmony_ci *
81562306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
81662306a36Sopenharmony_ci */
81762306a36Sopenharmony_ciint vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
81862306a36Sopenharmony_ci			 unsigned int flags, unsigned int *count,
81962306a36Sopenharmony_ci			 unsigned int requested_planes,
82062306a36Sopenharmony_ci			 const unsigned int requested_sizes[]);
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci/**
82362306a36Sopenharmony_ci * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
82462306a36Sopenharmony_ci *			to the kernel.
82562306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
82662306a36Sopenharmony_ci * @index:	id number of the buffer.
82762306a36Sopenharmony_ci * @pb:		buffer structure passed from userspace to
82862306a36Sopenharmony_ci *		&v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
82962306a36Sopenharmony_ci *
83062306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is
83162306a36Sopenharmony_ci * called internally by VB2 by an API-specific handler, like
83262306a36Sopenharmony_ci * ``videobuf2-v4l2.h``.
83362306a36Sopenharmony_ci *
83462306a36Sopenharmony_ci * The passed buffer should have been verified.
83562306a36Sopenharmony_ci *
83662306a36Sopenharmony_ci * This function calls vb2_ops->buf_prepare callback in the driver
83762306a36Sopenharmony_ci * (if provided), in which driver-specific buffer initialization can
83862306a36Sopenharmony_ci * be performed.
83962306a36Sopenharmony_ci *
84062306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
84162306a36Sopenharmony_ci */
84262306a36Sopenharmony_ciint vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci/**
84562306a36Sopenharmony_ci * vb2_core_qbuf() - Queue a buffer from userspace
84662306a36Sopenharmony_ci *
84762306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
84862306a36Sopenharmony_ci * @index:	id number of the buffer
84962306a36Sopenharmony_ci * @pb:		buffer structure passed from userspace to
85062306a36Sopenharmony_ci *		v4l2_ioctl_ops->vidioc_qbuf handler in driver
85162306a36Sopenharmony_ci * @req:	pointer to &struct media_request, may be NULL.
85262306a36Sopenharmony_ci *
85362306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called
85462306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
85562306a36Sopenharmony_ci *
85662306a36Sopenharmony_ci * This function:
85762306a36Sopenharmony_ci *
85862306a36Sopenharmony_ci * #) If @req is non-NULL, then the buffer will be bound to this
85962306a36Sopenharmony_ci *    media request and it returns. The buffer will be prepared and
86062306a36Sopenharmony_ci *    queued to the driver (i.e. the next two steps) when the request
86162306a36Sopenharmony_ci *    itself is queued.
86262306a36Sopenharmony_ci * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
86362306a36Sopenharmony_ci *    (if provided), in which driver-specific buffer initialization can
86462306a36Sopenharmony_ci *    be performed;
86562306a36Sopenharmony_ci * #) if streaming is on, queues the buffer in driver by the means of
86662306a36Sopenharmony_ci *    &vb2_ops->buf_queue callback for processing.
86762306a36Sopenharmony_ci *
86862306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
86962306a36Sopenharmony_ci */
87062306a36Sopenharmony_ciint vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
87162306a36Sopenharmony_ci		  struct media_request *req);
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci/**
87462306a36Sopenharmony_ci * vb2_core_dqbuf() - Dequeue a buffer to the userspace
87562306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue
87662306a36Sopenharmony_ci * @pindex:	pointer to the buffer index. May be NULL
87762306a36Sopenharmony_ci * @pb:		buffer structure passed from userspace to
87862306a36Sopenharmony_ci *		v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
87962306a36Sopenharmony_ci * @nonblocking: if true, this call will not sleep waiting for a buffer if no
88062306a36Sopenharmony_ci *		 buffers ready for dequeuing are present. Normally the driver
88162306a36Sopenharmony_ci *		 would be passing (file->f_flags & O_NONBLOCK) here.
88262306a36Sopenharmony_ci *
88362306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called
88462306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
88562306a36Sopenharmony_ci *
88662306a36Sopenharmony_ci * This function:
88762306a36Sopenharmony_ci *
88862306a36Sopenharmony_ci * #) calls buf_finish callback in the driver (if provided), in which
88962306a36Sopenharmony_ci *    driver can perform any additional operations that may be required before
89062306a36Sopenharmony_ci *    returning the buffer to userspace, such as cache sync,
89162306a36Sopenharmony_ci * #) the buffer struct members are filled with relevant information for
89262306a36Sopenharmony_ci *    the userspace.
89362306a36Sopenharmony_ci *
89462306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
89562306a36Sopenharmony_ci */
89662306a36Sopenharmony_ciint vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
89762306a36Sopenharmony_ci		   bool nonblocking);
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci/**
90062306a36Sopenharmony_ci * vb2_core_streamon() - Implements VB2 stream ON logic
90162306a36Sopenharmony_ci *
90262306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue
90362306a36Sopenharmony_ci * @type:	type of the queue to be started.
90462306a36Sopenharmony_ci *		For V4L2, this is defined by &enum v4l2_buf_type type.
90562306a36Sopenharmony_ci *
90662306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called
90762306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
90862306a36Sopenharmony_ci *
90962306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
91062306a36Sopenharmony_ci */
91162306a36Sopenharmony_ciint vb2_core_streamon(struct vb2_queue *q, unsigned int type);
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci/**
91462306a36Sopenharmony_ci * vb2_core_streamoff() - Implements VB2 stream OFF logic
91562306a36Sopenharmony_ci *
91662306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue
91762306a36Sopenharmony_ci * @type:	type of the queue to be started.
91862306a36Sopenharmony_ci *		For V4L2, this is defined by &enum v4l2_buf_type type.
91962306a36Sopenharmony_ci *
92062306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is
92162306a36Sopenharmony_ci * called internally by VB2 by an API-specific handler, like
92262306a36Sopenharmony_ci * ``videobuf2-v4l2.h``.
92362306a36Sopenharmony_ci *
92462306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
92562306a36Sopenharmony_ci */
92662306a36Sopenharmony_ciint vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci/**
92962306a36Sopenharmony_ci * vb2_core_expbuf() - Export a buffer as a file descriptor.
93062306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
93162306a36Sopenharmony_ci * @fd:		pointer to the file descriptor associated with DMABUF
93262306a36Sopenharmony_ci *		(set by driver).
93362306a36Sopenharmony_ci * @type:	buffer type.
93462306a36Sopenharmony_ci * @index:	id number of the buffer.
93562306a36Sopenharmony_ci * @plane:	index of the plane to be exported, 0 for single plane queues
93662306a36Sopenharmony_ci * @flags:	file flags for newly created file, as defined at
93762306a36Sopenharmony_ci *		include/uapi/asm-generic/fcntl.h.
93862306a36Sopenharmony_ci *		Currently, the only used flag is %O_CLOEXEC.
93962306a36Sopenharmony_ci *		is supported, refer to manual of open syscall for more details.
94062306a36Sopenharmony_ci *
94162306a36Sopenharmony_ci *
94262306a36Sopenharmony_ci * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called
94362306a36Sopenharmony_ci * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
94462306a36Sopenharmony_ci *
94562306a36Sopenharmony_ci * Return: returns zero on success; an error code otherwise.
94662306a36Sopenharmony_ci */
94762306a36Sopenharmony_ciint vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
94862306a36Sopenharmony_ci		unsigned int index, unsigned int plane, unsigned int flags);
94962306a36Sopenharmony_ci
95062306a36Sopenharmony_ci/**
95162306a36Sopenharmony_ci * vb2_core_queue_init() - initialize a videobuf2 queue
95262306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
95362306a36Sopenharmony_ci *		This structure should be allocated in driver
95462306a36Sopenharmony_ci *
95562306a36Sopenharmony_ci * The &vb2_queue structure should be allocated by the driver. The driver is
95662306a36Sopenharmony_ci * responsible of clearing it's content and setting initial values for some
95762306a36Sopenharmony_ci * required entries before calling this function.
95862306a36Sopenharmony_ci *
95962306a36Sopenharmony_ci * .. note::
96062306a36Sopenharmony_ci *
96162306a36Sopenharmony_ci *    The following fields at @q should be set before calling this function:
96262306a36Sopenharmony_ci *    &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type.
96362306a36Sopenharmony_ci */
96462306a36Sopenharmony_ciint vb2_core_queue_init(struct vb2_queue *q);
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_ci/**
96762306a36Sopenharmony_ci * vb2_core_queue_release() - stop streaming, release the queue and free memory
96862306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
96962306a36Sopenharmony_ci *
97062306a36Sopenharmony_ci * This function stops streaming and performs necessary clean ups, including
97162306a36Sopenharmony_ci * freeing video buffer memory. The driver is responsible for freeing
97262306a36Sopenharmony_ci * the &struct vb2_queue itself.
97362306a36Sopenharmony_ci */
97462306a36Sopenharmony_civoid vb2_core_queue_release(struct vb2_queue *q);
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci/**
97762306a36Sopenharmony_ci * vb2_queue_error() - signal a fatal error on the queue
97862306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
97962306a36Sopenharmony_ci *
98062306a36Sopenharmony_ci * Flag that a fatal unrecoverable error has occurred and wake up all processes
98162306a36Sopenharmony_ci * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing
98262306a36Sopenharmony_ci * buffers will return %-EIO.
98362306a36Sopenharmony_ci *
98462306a36Sopenharmony_ci * The error flag will be cleared when canceling the queue, either from
98562306a36Sopenharmony_ci * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this
98662306a36Sopenharmony_ci * function before starting the stream, otherwise the error flag will remain set
98762306a36Sopenharmony_ci * until the queue is released when closing the device node.
98862306a36Sopenharmony_ci */
98962306a36Sopenharmony_civoid vb2_queue_error(struct vb2_queue *q);
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_ci/**
99262306a36Sopenharmony_ci * vb2_mmap() - map video buffers into application address space.
99362306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
99462306a36Sopenharmony_ci * @vma:	pointer to &struct vm_area_struct with the vma passed
99562306a36Sopenharmony_ci *		to the mmap file operation handler in the driver.
99662306a36Sopenharmony_ci *
99762306a36Sopenharmony_ci * Should be called from mmap file operation handler of a driver.
99862306a36Sopenharmony_ci * This function maps one plane of one of the available video buffers to
99962306a36Sopenharmony_ci * userspace. To map whole video memory allocated on reqbufs, this function
100062306a36Sopenharmony_ci * has to be called once per each plane per each buffer previously allocated.
100162306a36Sopenharmony_ci *
100262306a36Sopenharmony_ci * When the userspace application calls mmap, it passes to it an offset returned
100362306a36Sopenharmony_ci * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler.
100462306a36Sopenharmony_ci * That offset acts as a "cookie", which is then used to identify the plane
100562306a36Sopenharmony_ci * to be mapped.
100662306a36Sopenharmony_ci *
100762306a36Sopenharmony_ci * This function finds a plane with a matching offset and a mapping is performed
100862306a36Sopenharmony_ci * by the means of a provided memory operation.
100962306a36Sopenharmony_ci *
101062306a36Sopenharmony_ci * The return values from this function are intended to be directly returned
101162306a36Sopenharmony_ci * from the mmap handler in driver.
101262306a36Sopenharmony_ci */
101362306a36Sopenharmony_ciint vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci#ifndef CONFIG_MMU
101662306a36Sopenharmony_ci/**
101762306a36Sopenharmony_ci * vb2_get_unmapped_area - map video buffers into application address space.
101862306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
101962306a36Sopenharmony_ci * @addr:	memory address.
102062306a36Sopenharmony_ci * @len:	buffer size.
102162306a36Sopenharmony_ci * @pgoff:	page offset.
102262306a36Sopenharmony_ci * @flags:	memory flags.
102362306a36Sopenharmony_ci *
102462306a36Sopenharmony_ci * This function is used in noMMU platforms to propose address mapping
102562306a36Sopenharmony_ci * for a given buffer. It's intended to be used as a handler for the
102662306a36Sopenharmony_ci * &file_operations->get_unmapped_area operation.
102762306a36Sopenharmony_ci *
102862306a36Sopenharmony_ci * This is called by the mmap() syscall routines will call this
102962306a36Sopenharmony_ci * to get a proposed address for the mapping, when ``!CONFIG_MMU``.
103062306a36Sopenharmony_ci */
103162306a36Sopenharmony_ciunsigned long vb2_get_unmapped_area(struct vb2_queue *q,
103262306a36Sopenharmony_ci				    unsigned long addr,
103362306a36Sopenharmony_ci				    unsigned long len,
103462306a36Sopenharmony_ci				    unsigned long pgoff,
103562306a36Sopenharmony_ci				    unsigned long flags);
103662306a36Sopenharmony_ci#endif
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci/**
103962306a36Sopenharmony_ci * vb2_core_poll() - implements poll syscall() logic.
104062306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
104162306a36Sopenharmony_ci * @file:	&struct file argument passed to the poll
104262306a36Sopenharmony_ci *		file operation handler.
104362306a36Sopenharmony_ci * @wait:	&poll_table wait argument passed to the poll
104462306a36Sopenharmony_ci *		file operation handler.
104562306a36Sopenharmony_ci *
104662306a36Sopenharmony_ci * This function implements poll file operation handler for a driver.
104762306a36Sopenharmony_ci * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
104862306a36Sopenharmony_ci * be informed that the file descriptor of a video device is available for
104962306a36Sopenharmony_ci * reading.
105062306a36Sopenharmony_ci * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
105162306a36Sopenharmony_ci * will be reported as available for writing.
105262306a36Sopenharmony_ci *
105362306a36Sopenharmony_ci * The return values from this function are intended to be directly returned
105462306a36Sopenharmony_ci * from poll handler in driver.
105562306a36Sopenharmony_ci */
105662306a36Sopenharmony_ci__poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
105762306a36Sopenharmony_ci			   poll_table *wait);
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci/**
106062306a36Sopenharmony_ci * vb2_read() - implements read() syscall logic.
106162306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
106262306a36Sopenharmony_ci * @data:	pointed to target userspace buffer
106362306a36Sopenharmony_ci * @count:	number of bytes to read
106462306a36Sopenharmony_ci * @ppos:	file handle position tracking pointer
106562306a36Sopenharmony_ci * @nonblock:	mode selector (1 means blocking calls, 0 means nonblocking)
106662306a36Sopenharmony_ci */
106762306a36Sopenharmony_cisize_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
106862306a36Sopenharmony_ci		loff_t *ppos, int nonblock);
106962306a36Sopenharmony_ci/**
107062306a36Sopenharmony_ci * vb2_write() - implements write() syscall logic.
107162306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
107262306a36Sopenharmony_ci * @data:	pointed to target userspace buffer
107362306a36Sopenharmony_ci * @count:	number of bytes to write
107462306a36Sopenharmony_ci * @ppos:	file handle position tracking pointer
107562306a36Sopenharmony_ci * @nonblock:	mode selector (1 means blocking calls, 0 means nonblocking)
107662306a36Sopenharmony_ci */
107762306a36Sopenharmony_cisize_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
107862306a36Sopenharmony_ci		loff_t *ppos, int nonblock);
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci/**
108162306a36Sopenharmony_ci * typedef vb2_thread_fnc - callback function for use with vb2_thread.
108262306a36Sopenharmony_ci *
108362306a36Sopenharmony_ci * @vb: pointer to struct &vb2_buffer.
108462306a36Sopenharmony_ci * @priv: pointer to a private data.
108562306a36Sopenharmony_ci *
108662306a36Sopenharmony_ci * This is called whenever a buffer is dequeued in the thread.
108762306a36Sopenharmony_ci */
108862306a36Sopenharmony_citypedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci/**
109162306a36Sopenharmony_ci * vb2_thread_start() - start a thread for the given queue.
109262306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
109362306a36Sopenharmony_ci * @fnc:	&vb2_thread_fnc callback function.
109462306a36Sopenharmony_ci * @priv:	priv pointer passed to the callback function.
109562306a36Sopenharmony_ci * @thread_name:the name of the thread. This will be prefixed with "vb2-".
109662306a36Sopenharmony_ci *
109762306a36Sopenharmony_ci * This starts a thread that will queue and dequeue until an error occurs
109862306a36Sopenharmony_ci * or vb2_thread_stop() is called.
109962306a36Sopenharmony_ci *
110062306a36Sopenharmony_ci * .. attention::
110162306a36Sopenharmony_ci *
110262306a36Sopenharmony_ci *   This function should not be used for anything else but the videobuf2-dvb
110362306a36Sopenharmony_ci *   support. If you think you have another good use-case for this, then please
110462306a36Sopenharmony_ci *   contact the linux-media mailing list first.
110562306a36Sopenharmony_ci */
110662306a36Sopenharmony_ciint vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
110762306a36Sopenharmony_ci		     const char *thread_name);
110862306a36Sopenharmony_ci
110962306a36Sopenharmony_ci/**
111062306a36Sopenharmony_ci * vb2_thread_stop() - stop the thread for the given queue.
111162306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
111262306a36Sopenharmony_ci */
111362306a36Sopenharmony_ciint vb2_thread_stop(struct vb2_queue *q);
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci/**
111662306a36Sopenharmony_ci * vb2_is_streaming() - return streaming status of the queue.
111762306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
111862306a36Sopenharmony_ci */
111962306a36Sopenharmony_cistatic inline bool vb2_is_streaming(struct vb2_queue *q)
112062306a36Sopenharmony_ci{
112162306a36Sopenharmony_ci	return q->streaming;
112262306a36Sopenharmony_ci}
112362306a36Sopenharmony_ci
112462306a36Sopenharmony_ci/**
112562306a36Sopenharmony_ci * vb2_fileio_is_active() - return true if fileio is active.
112662306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
112762306a36Sopenharmony_ci *
112862306a36Sopenharmony_ci * This returns true if read() or write() is used to stream the data
112962306a36Sopenharmony_ci * as opposed to stream I/O. This is almost never an important distinction,
113062306a36Sopenharmony_ci * except in rare cases. One such case is that using read() or write() to
113162306a36Sopenharmony_ci * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there
113262306a36Sopenharmony_ci * is no way you can pass the field information of each buffer to/from
113362306a36Sopenharmony_ci * userspace. A driver that supports this field format should check for
113462306a36Sopenharmony_ci * this in the &vb2_ops->queue_setup op and reject it if this function returns
113562306a36Sopenharmony_ci * true.
113662306a36Sopenharmony_ci */
113762306a36Sopenharmony_cistatic inline bool vb2_fileio_is_active(struct vb2_queue *q)
113862306a36Sopenharmony_ci{
113962306a36Sopenharmony_ci	return q->fileio;
114062306a36Sopenharmony_ci}
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci/**
114362306a36Sopenharmony_ci * vb2_is_busy() - return busy status of the queue.
114462306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
114562306a36Sopenharmony_ci *
114662306a36Sopenharmony_ci * This function checks if queue has any buffers allocated.
114762306a36Sopenharmony_ci */
114862306a36Sopenharmony_cistatic inline bool vb2_is_busy(struct vb2_queue *q)
114962306a36Sopenharmony_ci{
115062306a36Sopenharmony_ci	return (q->num_buffers > 0);
115162306a36Sopenharmony_ci}
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci/**
115462306a36Sopenharmony_ci * vb2_get_drv_priv() - return driver private data associated with the queue.
115562306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
115662306a36Sopenharmony_ci */
115762306a36Sopenharmony_cistatic inline void *vb2_get_drv_priv(struct vb2_queue *q)
115862306a36Sopenharmony_ci{
115962306a36Sopenharmony_ci	return q->drv_priv;
116062306a36Sopenharmony_ci}
116162306a36Sopenharmony_ci
116262306a36Sopenharmony_ci/**
116362306a36Sopenharmony_ci * vb2_set_plane_payload() - set bytesused for the plane @plane_no.
116462306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to which the plane in
116562306a36Sopenharmony_ci *		question belongs to.
116662306a36Sopenharmony_ci * @plane_no:	plane number for which payload should be set.
116762306a36Sopenharmony_ci * @size:	payload in bytes.
116862306a36Sopenharmony_ci */
116962306a36Sopenharmony_cistatic inline void vb2_set_plane_payload(struct vb2_buffer *vb,
117062306a36Sopenharmony_ci				 unsigned int plane_no, unsigned long size)
117162306a36Sopenharmony_ci{
117262306a36Sopenharmony_ci	/*
117362306a36Sopenharmony_ci	 * size must never be larger than the buffer length, so
117462306a36Sopenharmony_ci	 * warn and clamp to the buffer length if that's the case.
117562306a36Sopenharmony_ci	 */
117662306a36Sopenharmony_ci	if (plane_no < vb->num_planes) {
117762306a36Sopenharmony_ci		if (WARN_ON_ONCE(size > vb->planes[plane_no].length))
117862306a36Sopenharmony_ci			size = vb->planes[plane_no].length;
117962306a36Sopenharmony_ci		vb->planes[plane_no].bytesused = size;
118062306a36Sopenharmony_ci	}
118162306a36Sopenharmony_ci}
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci/**
118462306a36Sopenharmony_ci * vb2_get_plane_payload() - get bytesused for the plane plane_no
118562306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to which the plane in
118662306a36Sopenharmony_ci *		question belongs to.
118762306a36Sopenharmony_ci * @plane_no:	plane number for which payload should be set.
118862306a36Sopenharmony_ci */
118962306a36Sopenharmony_cistatic inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
119062306a36Sopenharmony_ci				 unsigned int plane_no)
119162306a36Sopenharmony_ci{
119262306a36Sopenharmony_ci	if (plane_no < vb->num_planes)
119362306a36Sopenharmony_ci		return vb->planes[plane_no].bytesused;
119462306a36Sopenharmony_ci	return 0;
119562306a36Sopenharmony_ci}
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci/**
119862306a36Sopenharmony_ci * vb2_plane_size() - return plane size in bytes.
119962306a36Sopenharmony_ci * @vb:		pointer to &struct vb2_buffer to which the plane in
120062306a36Sopenharmony_ci *		question belongs to.
120162306a36Sopenharmony_ci * @plane_no:	plane number for which size should be returned.
120262306a36Sopenharmony_ci */
120362306a36Sopenharmony_cistatic inline unsigned long
120462306a36Sopenharmony_civb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
120562306a36Sopenharmony_ci{
120662306a36Sopenharmony_ci	if (plane_no < vb->num_planes)
120762306a36Sopenharmony_ci		return vb->planes[plane_no].length;
120862306a36Sopenharmony_ci	return 0;
120962306a36Sopenharmony_ci}
121062306a36Sopenharmony_ci
121162306a36Sopenharmony_ci/**
121262306a36Sopenharmony_ci * vb2_start_streaming_called() - return streaming status of driver.
121362306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
121462306a36Sopenharmony_ci */
121562306a36Sopenharmony_cistatic inline bool vb2_start_streaming_called(struct vb2_queue *q)
121662306a36Sopenharmony_ci{
121762306a36Sopenharmony_ci	return q->start_streaming_called;
121862306a36Sopenharmony_ci}
121962306a36Sopenharmony_ci
122062306a36Sopenharmony_ci/**
122162306a36Sopenharmony_ci * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue.
122262306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
122362306a36Sopenharmony_ci */
122462306a36Sopenharmony_cistatic inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
122562306a36Sopenharmony_ci{
122662306a36Sopenharmony_ci	q->last_buffer_dequeued = false;
122762306a36Sopenharmony_ci}
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci/**
123062306a36Sopenharmony_ci * vb2_get_buffer() - get a buffer from a queue
123162306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
123262306a36Sopenharmony_ci * @index:	buffer index
123362306a36Sopenharmony_ci *
123462306a36Sopenharmony_ci * This function obtains a buffer from a queue, by its index.
123562306a36Sopenharmony_ci * Keep in mind that there is no refcounting involved in this
123662306a36Sopenharmony_ci * operation, so the buffer lifetime should be taken into
123762306a36Sopenharmony_ci * consideration.
123862306a36Sopenharmony_ci */
123962306a36Sopenharmony_cistatic inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
124062306a36Sopenharmony_ci						unsigned int index)
124162306a36Sopenharmony_ci{
124262306a36Sopenharmony_ci	if (index < q->num_buffers)
124362306a36Sopenharmony_ci		return q->bufs[index];
124462306a36Sopenharmony_ci	return NULL;
124562306a36Sopenharmony_ci}
124662306a36Sopenharmony_ci
124762306a36Sopenharmony_ci/*
124862306a36Sopenharmony_ci * The following functions are not part of the vb2 core API, but are useful
124962306a36Sopenharmony_ci * functions for videobuf2-*.
125062306a36Sopenharmony_ci */
125162306a36Sopenharmony_ci
125262306a36Sopenharmony_ci/**
125362306a36Sopenharmony_ci * vb2_buffer_in_use() - return true if the buffer is in use and
125462306a36Sopenharmony_ci * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call.
125562306a36Sopenharmony_ci *
125662306a36Sopenharmony_ci * @vb:		buffer for which plane size should be returned.
125762306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
125862306a36Sopenharmony_ci */
125962306a36Sopenharmony_cibool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci/**
126262306a36Sopenharmony_ci * vb2_verify_memory_type() - Check whether the memory type and buffer type
126362306a36Sopenharmony_ci * passed to a buffer operation are compatible with the queue.
126462306a36Sopenharmony_ci *
126562306a36Sopenharmony_ci * @q:		pointer to &struct vb2_queue with videobuf2 queue.
126662306a36Sopenharmony_ci * @memory:	memory model, as defined by enum &vb2_memory.
126762306a36Sopenharmony_ci * @type:	private buffer type whose content is defined by the vb2-core
126862306a36Sopenharmony_ci *		caller. For example, for V4L2, it should match
126962306a36Sopenharmony_ci *		the types defined on enum &v4l2_buf_type.
127062306a36Sopenharmony_ci */
127162306a36Sopenharmony_ciint vb2_verify_memory_type(struct vb2_queue *q,
127262306a36Sopenharmony_ci		enum vb2_memory memory, unsigned int type);
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci/**
127562306a36Sopenharmony_ci * vb2_request_object_is_buffer() - return true if the object is a buffer
127662306a36Sopenharmony_ci *
127762306a36Sopenharmony_ci * @obj:	the request object.
127862306a36Sopenharmony_ci */
127962306a36Sopenharmony_cibool vb2_request_object_is_buffer(struct media_request_object *obj);
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ci/**
128262306a36Sopenharmony_ci * vb2_request_buffer_cnt() - return the number of buffers in the request
128362306a36Sopenharmony_ci *
128462306a36Sopenharmony_ci * @req:	the request.
128562306a36Sopenharmony_ci */
128662306a36Sopenharmony_ciunsigned int vb2_request_buffer_cnt(struct media_request *req);
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci#endif /* _MEDIA_VIDEOBUF2_CORE_H */
1289