1e23ccc0aSPawel Osciak /*
2c139990eSJunghak Sung * videobuf2-core.h - Video Buffer 2 Core Framework
3e23ccc0aSPawel Osciak *
4e23ccc0aSPawel Osciak * Copyright (C) 2010 Samsung Electronics
5e23ccc0aSPawel Osciak *
695072084SPawel Osciak * Author: Pawel Osciak <pawel@osciak.com>
7e23ccc0aSPawel Osciak *
8e23ccc0aSPawel Osciak * This program is free software; you can redistribute it and/or modify
9e23ccc0aSPawel Osciak * it under the terms of the GNU General Public License as published by
10e23ccc0aSPawel Osciak * the Free Software Foundation.
11e23ccc0aSPawel Osciak */
12e23ccc0aSPawel Osciak #ifndef _MEDIA_VIDEOBUF2_CORE_H
13e23ccc0aSPawel Osciak #define _MEDIA_VIDEOBUF2_CORE_H
14e23ccc0aSPawel Osciak
15e23ccc0aSPawel Osciak #include <linux/mm_types.h>
16e23ccc0aSPawel Osciak #include <linux/mutex.h>
17e23ccc0aSPawel Osciak #include <linux/poll.h>
18c5384048SSumit Semwal #include <linux/dma-buf.h>
1937bc2d87SMauro Carvalho Chehab #include <linux/bitops.h>
201cf96dccSHans Verkuil #include <media/media-request.h>
21eb83b8e3SDaniel Vetter #include <media/frame_vector.h>
22e23ccc0aSPawel Osciak
23bed04f93SJunghak Sung #define VB2_MAX_FRAME (32)
24bed04f93SJunghak Sung #define VB2_MAX_PLANES (8)
25bed04f93SJunghak Sung
2652839f66SMauro Carvalho Chehab /**
2752839f66SMauro Carvalho Chehab * enum vb2_memory - type of memory model used to make the buffers visible
2852839f66SMauro Carvalho Chehab * on userspace.
2952839f66SMauro Carvalho Chehab *
3052839f66SMauro Carvalho Chehab * @VB2_MEMORY_UNKNOWN: Buffer status is unknown or it is not used yet on
3152839f66SMauro Carvalho Chehab * userspace.
3252839f66SMauro Carvalho Chehab * @VB2_MEMORY_MMAP: The buffers are allocated by the Kernel and it is
3352839f66SMauro Carvalho Chehab * memory mapped via mmap() ioctl. This model is
3452839f66SMauro Carvalho Chehab * also used when the user is using the buffers via
3552839f66SMauro Carvalho Chehab * read() or write() system calls.
3652839f66SMauro Carvalho Chehab * @VB2_MEMORY_USERPTR: The buffers was allocated in userspace and it is
3752839f66SMauro Carvalho Chehab * memory mapped via mmap() ioctl.
3852839f66SMauro Carvalho Chehab * @VB2_MEMORY_DMABUF: The buffers are passed to userspace via DMA buffer.
3952839f66SMauro Carvalho Chehab */
40bed04f93SJunghak Sung enum vb2_memory {
41bed04f93SJunghak Sung VB2_MEMORY_UNKNOWN = 0,
42bed04f93SJunghak Sung VB2_MEMORY_MMAP = 1,
43bed04f93SJunghak Sung VB2_MEMORY_USERPTR = 2,
44bed04f93SJunghak Sung VB2_MEMORY_DMABUF = 4,
45bed04f93SJunghak Sung };
46bed04f93SJunghak Sung
47b25748feSMarek Szyprowski struct vb2_fileio_data;
483415a89fSHans Verkuil struct vb2_threadio_data;
49a4b83debSSergey Senozhatsky struct vb2_buffer;
50e23ccc0aSPawel Osciak
51e23ccc0aSPawel Osciak /**
522b141324SMauro Carvalho Chehab * struct vb2_mem_ops - memory handling/memory allocator operations.
53e23ccc0aSPawel Osciak * @alloc: allocate video memory and, optionally, allocator private data,
540ff657b0SHans Verkuil * return ERR_PTR() on failure or a pointer to allocator private,
55e23ccc0aSPawel Osciak * per-buffer data on success; the returned private structure
56f286f4dfSMauro Carvalho Chehab * will then be passed as @buf_priv argument to other ops in this
57a4b83debSSergey Senozhatsky * structure. The size argument to this function shall be
58a4b83debSSergey Senozhatsky * *page aligned*.
59e23ccc0aSPawel Osciak * @put: inform the allocator that the buffer will no longer be used;
60e23ccc0aSPawel Osciak * usually will result in the allocator freeing the buffer (if
61f286f4dfSMauro Carvalho Chehab * no other users of this buffer are present); the @buf_priv
62e23ccc0aSPawel Osciak * argument is the allocator private per-buffer structure
633f12e6b0SHans Verkuil * previously returned from the alloc callback.
6432d81b41SMauro Carvalho Chehab * @get_dmabuf: acquire userspace memory for a hardware operation; used for
6532d81b41SMauro Carvalho Chehab * DMABUF memory types.
66e23ccc0aSPawel Osciak * @get_userptr: acquire userspace memory for a hardware operation; used for
67e23ccc0aSPawel Osciak * USERPTR memory types; vaddr is the address passed to the
683e947c36SHans Verkuil * videobuf2 layer when queuing a video buffer of USERPTR type;
69e23ccc0aSPawel Osciak * should return an allocator private per-buffer structure
700ff657b0SHans Verkuil * associated with the buffer on success, ERR_PTR() on failure;
71f286f4dfSMauro Carvalho Chehab * the returned private structure will then be passed as @buf_priv
723f12e6b0SHans Verkuil * argument to other ops in this structure.
73e23ccc0aSPawel Osciak * @put_userptr: inform the allocator that a USERPTR buffer will no longer
743f12e6b0SHans Verkuil * be used.
7560e7926bSMauro Carvalho Chehab * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation;
7636c0f8b3SHans Verkuil * used for DMABUF memory types; dev is the alloc device
770ff657b0SHans Verkuil * dbuf is the shared dma_buf; returns ERR_PTR() on failure;
78c5384048SSumit Semwal * allocator private per-buffer structure on success;
793f12e6b0SHans Verkuil * this needs to be used for further accesses to the buffer.
80c5384048SSumit Semwal * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
81f286f4dfSMauro Carvalho Chehab * buffer is no longer used; the @buf_priv argument is the
82c5384048SSumit Semwal * allocator private per-buffer structure previously returned
833f12e6b0SHans Verkuil * from the attach_dmabuf callback.
84c5384048SSumit Semwal * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
85c5384048SSumit Semwal * of dmabuf is informed that this driver is going to use the
863f12e6b0SHans Verkuil * dmabuf.
87c5384048SSumit Semwal * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
883f12e6b0SHans Verkuil * that this driver is done using the dmabuf for now.
893e0c2f20SMarek Szyprowski * @prepare: called every time the buffer is passed from userspace to the
903f12e6b0SHans Verkuil * driver, useful for cache synchronisation, optional.
913e0c2f20SMarek Szyprowski * @finish: called every time the buffer is passed back from the driver
923f12e6b0SHans Verkuil * to the userspace, also optional.
93e23ccc0aSPawel Osciak * @vaddr: return a kernel virtual address to a given memory buffer
94e23ccc0aSPawel Osciak * associated with the passed private structure or NULL if no
953f12e6b0SHans Verkuil * such mapping exists.
96e23ccc0aSPawel Osciak * @cookie: return allocator specific cookie for a given memory buffer
97e23ccc0aSPawel Osciak * associated with the passed private structure or NULL if not
983f12e6b0SHans Verkuil * available.
99e23ccc0aSPawel Osciak * @num_users: return the current number of users of a memory buffer;
1003e947c36SHans Verkuil * return 1 if the videobuf2 layer (or actually the driver using
1013f12e6b0SHans Verkuil * it) is the only user.
102e23ccc0aSPawel Osciak * @mmap: setup a userspace mapping for a given memory buffer under
1033f12e6b0SHans Verkuil * the provided virtual memory region.
104e23ccc0aSPawel Osciak *
10589cb3ddbSMauro Carvalho Chehab * Those operations are used by the videobuf2 core to implement the memory
10689cb3ddbSMauro Carvalho Chehab * handling/memory allocators for each type of supported streaming I/O method.
10789cb3ddbSMauro Carvalho Chehab *
10889cb3ddbSMauro Carvalho Chehab * .. note::
10989cb3ddbSMauro Carvalho Chehab * #) Required ops for USERPTR types: get_userptr, put_userptr.
11089cb3ddbSMauro Carvalho Chehab *
11189cb3ddbSMauro Carvalho Chehab * #) Required ops for MMAP types: alloc, put, num_users, mmap.
11289cb3ddbSMauro Carvalho Chehab *
11389cb3ddbSMauro Carvalho Chehab * #) Required ops for read/write access types: alloc, put, num_users, vaddr.
11489cb3ddbSMauro Carvalho Chehab *
115f286f4dfSMauro Carvalho Chehab * #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
116f286f4dfSMauro Carvalho Chehab * map_dmabuf, unmap_dmabuf.
117e23ccc0aSPawel Osciak */
118e23ccc0aSPawel Osciak struct vb2_mem_ops {
119a4b83debSSergey Senozhatsky void *(*alloc)(struct vb2_buffer *vb,
120a4b83debSSergey Senozhatsky struct device *dev,
121a4b83debSSergey Senozhatsky unsigned long size);
122e23ccc0aSPawel Osciak void (*put)(void *buf_priv);
123a4b83debSSergey Senozhatsky struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb,
124a4b83debSSergey Senozhatsky void *buf_priv,
125a4b83debSSergey Senozhatsky unsigned long flags);
126e23ccc0aSPawel Osciak
127a4b83debSSergey Senozhatsky void *(*get_userptr)(struct vb2_buffer *vb,
128a4b83debSSergey Senozhatsky struct device *dev,
129a4b83debSSergey Senozhatsky unsigned long vaddr,
130a4b83debSSergey Senozhatsky unsigned long size);
131e23ccc0aSPawel Osciak void (*put_userptr)(void *buf_priv);
132e23ccc0aSPawel Osciak
1333e0c2f20SMarek Szyprowski void (*prepare)(void *buf_priv);
1343e0c2f20SMarek Szyprowski void (*finish)(void *buf_priv);
1353e0c2f20SMarek Szyprowski
136a4b83debSSergey Senozhatsky void *(*attach_dmabuf)(struct vb2_buffer *vb,
137a4b83debSSergey Senozhatsky struct device *dev,
138f286f4dfSMauro Carvalho Chehab struct dma_buf *dbuf,
139a4b83debSSergey Senozhatsky unsigned long size);
140c5384048SSumit Semwal void (*detach_dmabuf)(void *buf_priv);
141c5384048SSumit Semwal int (*map_dmabuf)(void *buf_priv);
142c5384048SSumit Semwal void (*unmap_dmabuf)(void *buf_priv);
143c5384048SSumit Semwal
144a4b83debSSergey Senozhatsky void *(*vaddr)(struct vb2_buffer *vb, void *buf_priv);
145a4b83debSSergey Senozhatsky void *(*cookie)(struct vb2_buffer *vb, void *buf_priv);
146e23ccc0aSPawel Osciak
147e23ccc0aSPawel Osciak unsigned int (*num_users)(void *buf_priv);
148e23ccc0aSPawel Osciak
149e23ccc0aSPawel Osciak int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
150e23ccc0aSPawel Osciak };
151e23ccc0aSPawel Osciak
1522d700715SJunghak Sung /**
1532b141324SMauro Carvalho Chehab * struct vb2_plane - plane information.
1542b141324SMauro Carvalho Chehab * @mem_priv: private data with this plane.
1552b141324SMauro Carvalho Chehab * @dbuf: dma_buf - shared buffer object.
1562d700715SJunghak Sung * @dbuf_mapped: flag to show whether dbuf is mapped or not
1572b141324SMauro Carvalho Chehab * @bytesused: number of bytes occupied by data in the plane (payload).
1582f65fceeSRicardo Ribalda * @length: size of this plane (NOT the payload) in bytes. The maximum
1592f65fceeSRicardo Ribalda * valid size is MAX_UINT - PAGE_SIZE.
16058e1ba3cSHans Verkuil * @min_length: minimum required size of this plane (NOT the payload) in bytes.
1612f65fceeSRicardo Ribalda * @length is always greater or equal to @min_length, and like
1622f65fceeSRicardo Ribalda * @length, it is limited to MAX_UINT - PAGE_SIZE.
1632b141324SMauro Carvalho Chehab * @m: Union with memtype-specific data.
16460e7926bSMauro Carvalho Chehab * @m.offset: when memory in the associated struct vb2_buffer is
16560e7926bSMauro Carvalho Chehab * %VB2_MEMORY_MMAP, equals the offset from the start of
1662d700715SJunghak Sung * the device memory for this plane (or is a "cookie" that
1672b141324SMauro Carvalho Chehab * should be passed to mmap() called on the video node).
16860e7926bSMauro Carvalho Chehab * @m.userptr: when memory is %VB2_MEMORY_USERPTR, a userspace pointer
1692b141324SMauro Carvalho Chehab * pointing to this plane.
17060e7926bSMauro Carvalho Chehab * @m.fd: when memory is %VB2_MEMORY_DMABUF, a userspace file
1712b141324SMauro Carvalho Chehab * descriptor associated with this plane.
1722d700715SJunghak Sung * @data_offset: offset in the plane to the start of data; usually 0,
1732b141324SMauro Carvalho Chehab * unless there is a header in front of the data.
1742b141324SMauro Carvalho Chehab *
1752d700715SJunghak Sung * Should contain enough information to be able to cover all the fields
1762b141324SMauro Carvalho Chehab * of &struct v4l2_plane at videodev2.h.
1772d700715SJunghak Sung */
178e23ccc0aSPawel Osciak struct vb2_plane {
179e23ccc0aSPawel Osciak void *mem_priv;
180c5384048SSumit Semwal struct dma_buf *dbuf;
181c5384048SSumit Semwal unsigned int dbuf_mapped;
1822d700715SJunghak Sung unsigned int bytesused;
1832d700715SJunghak Sung unsigned int length;
18458e1ba3cSHans Verkuil unsigned int min_length;
1852d700715SJunghak Sung union {
1862d700715SJunghak Sung unsigned int offset;
1872d700715SJunghak Sung unsigned long userptr;
1882d700715SJunghak Sung int fd;
1892d700715SJunghak Sung } m;
1902d700715SJunghak Sung unsigned int data_offset;
191e23ccc0aSPawel Osciak };
192e23ccc0aSPawel Osciak
193e23ccc0aSPawel Osciak /**
1942b141324SMauro Carvalho Chehab * enum vb2_io_modes - queue access methods.
1952b141324SMauro Carvalho Chehab * @VB2_MMAP: driver supports MMAP with streaming API.
1962b141324SMauro Carvalho Chehab * @VB2_USERPTR: driver supports USERPTR with streaming API.
1972b141324SMauro Carvalho Chehab * @VB2_READ: driver supports read() style access.
1982b141324SMauro Carvalho Chehab * @VB2_WRITE: driver supports write() style access.
1992b141324SMauro Carvalho Chehab * @VB2_DMABUF: driver supports DMABUF with streaming API.
200e23ccc0aSPawel Osciak */
201e23ccc0aSPawel Osciak enum vb2_io_modes {
20237bc2d87SMauro Carvalho Chehab VB2_MMAP = BIT(0),
20337bc2d87SMauro Carvalho Chehab VB2_USERPTR = BIT(1),
20437bc2d87SMauro Carvalho Chehab VB2_READ = BIT(2),
20537bc2d87SMauro Carvalho Chehab VB2_WRITE = BIT(3),
20637bc2d87SMauro Carvalho Chehab VB2_DMABUF = BIT(4),
207e23ccc0aSPawel Osciak };
208e23ccc0aSPawel Osciak
209e23ccc0aSPawel Osciak /**
2102b141324SMauro Carvalho Chehab * enum vb2_buffer_state - current video buffer state.
2112b141324SMauro Carvalho Chehab * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control.
212fd89e0bbSHans Verkuil * @VB2_BUF_STATE_IN_REQUEST: buffer is queued in media request.
2133e947c36SHans Verkuil * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf2.
2143e947c36SHans Verkuil * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf2, but not in driver.
215e23ccc0aSPawel Osciak * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
2162b141324SMauro Carvalho Chehab * in a hardware operation.
2173e947c36SHans Verkuil * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf2, but
2182b141324SMauro Carvalho Chehab * not yet dequeued to userspace.
219e23ccc0aSPawel Osciak * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
220e23ccc0aSPawel Osciak * has ended with an error, which will be reported
2212b141324SMauro Carvalho Chehab * to the userspace when it is dequeued.
222e23ccc0aSPawel Osciak */
223e23ccc0aSPawel Osciak enum vb2_buffer_state {
224e23ccc0aSPawel Osciak VB2_BUF_STATE_DEQUEUED,
225fd89e0bbSHans Verkuil VB2_BUF_STATE_IN_REQUEST,
226b18a8ff2SHans Verkuil VB2_BUF_STATE_PREPARING,
227e23ccc0aSPawel Osciak VB2_BUF_STATE_QUEUED,
228e23ccc0aSPawel Osciak VB2_BUF_STATE_ACTIVE,
229e23ccc0aSPawel Osciak VB2_BUF_STATE_DONE,
230e23ccc0aSPawel Osciak VB2_BUF_STATE_ERROR,
231e23ccc0aSPawel Osciak };
232e23ccc0aSPawel Osciak
233e23ccc0aSPawel Osciak struct vb2_queue;
234e23ccc0aSPawel Osciak
235e23ccc0aSPawel Osciak /**
2362b141324SMauro Carvalho Chehab * struct vb2_buffer - represents a video buffer.
2372b141324SMauro Carvalho Chehab * @vb2_queue: pointer to &struct vb2_queue with the queue to
2382b141324SMauro Carvalho Chehab * which this driver belongs.
2392b141324SMauro Carvalho Chehab * @index: id number of the buffer.
2402b141324SMauro Carvalho Chehab * @type: buffer type.
2412b141324SMauro Carvalho Chehab * @memory: the method, in which the actual data is passed.
242e23ccc0aSPawel Osciak * @num_planes: number of planes in the buffer
2432b141324SMauro Carvalho Chehab * on an internal driver queue.
2442b141324SMauro Carvalho Chehab * @timestamp: frame timestamp in ns.
2456093d300SHans Verkuil * @request: the request this buffer is associated with.
2461cf96dccSHans Verkuil * @req_obj: used to bind this buffer to a request. This
2471cf96dccSHans Verkuil * request object has a refcount.
248e23ccc0aSPawel Osciak */
249e23ccc0aSPawel Osciak struct vb2_buffer {
250e23ccc0aSPawel Osciak struct vb2_queue *vb2_queue;
2512d700715SJunghak Sung unsigned int index;
2522d700715SJunghak Sung unsigned int type;
2532d700715SJunghak Sung unsigned int memory;
254e23ccc0aSPawel Osciak unsigned int num_planes;
255d6dd645eSJunghak Sung u64 timestamp;
2566093d300SHans Verkuil struct media_request *request;
2571cf96dccSHans Verkuil struct media_request_object req_obj;
258e23ccc0aSPawel Osciak
259efe98010SMauro Carvalho Chehab /* private: internal use only
260efe98010SMauro Carvalho Chehab *
261efe98010SMauro Carvalho Chehab * state: current buffer state; do not change
26255028695SHans Verkuil * synced: this buffer has been synced for DMA, i.e. the
26355028695SHans Verkuil * 'prepare' memop was called. It is cleared again
26455028695SHans Verkuil * after the 'finish' memop is called.
26555028695SHans Verkuil * prepared: this buffer has been prepared, i.e. the
26655028695SHans Verkuil * buf_prepare op was called. It is cleared again
26755028695SHans Verkuil * after the 'buf_finish' op is called.
2687e4e7162SHans Verkuil * copied_timestamp: the timestamp of this capture buffer was copied
2697e4e7162SHans Verkuil * from an output buffer.
2704dbe7eabSSergey Senozhatsky * skip_cache_sync_on_prepare: when set buffer's ->prepare() function
2714dbe7eabSSergey Senozhatsky * skips cache sync/invalidation.
2724dbe7eabSSergey Senozhatsky * skip_cache_sync_on_finish: when set buffer's ->finish() function
2734dbe7eabSSergey Senozhatsky * skips cache sync/invalidation.
274efe98010SMauro Carvalho Chehab * queued_entry: entry on the queued buffers list, which holds
275efe98010SMauro Carvalho Chehab * all buffers queued from userspace
276efe98010SMauro Carvalho Chehab * done_entry: entry on the list that stores all buffers ready
277efe98010SMauro Carvalho Chehab * to be dequeued to userspace
2782b141324SMauro Carvalho Chehab * vb2_plane: per-plane information; do not change
279efe98010SMauro Carvalho Chehab */
280e23ccc0aSPawel Osciak enum vb2_buffer_state state;
281cfc77408SHans Verkuil unsigned int synced:1;
282cfc77408SHans Verkuil unsigned int prepared:1;
2837e4e7162SHans Verkuil unsigned int copied_timestamp:1;
2844dbe7eabSSergey Senozhatsky unsigned int skip_cache_sync_on_prepare:1;
2854dbe7eabSSergey Senozhatsky unsigned int skip_cache_sync_on_finish:1;
286e23ccc0aSPawel Osciak
2872b141324SMauro Carvalho Chehab struct vb2_plane planes[VB2_MAX_PLANES];
288e23ccc0aSPawel Osciak struct list_head queued_entry;
289e23ccc0aSPawel Osciak struct list_head done_entry;
290b5b4541eSHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
291b5b4541eSHans Verkuil /*
292b5b4541eSHans Verkuil * Counters for how often these buffer-related ops are
293b5b4541eSHans Verkuil * called. Used to check for unbalanced ops.
294b5b4541eSHans Verkuil */
295b5b4541eSHans Verkuil u32 cnt_mem_alloc;
296b5b4541eSHans Verkuil u32 cnt_mem_put;
297b5b4541eSHans Verkuil u32 cnt_mem_get_dmabuf;
298b5b4541eSHans Verkuil u32 cnt_mem_get_userptr;
299b5b4541eSHans Verkuil u32 cnt_mem_put_userptr;
300b5b4541eSHans Verkuil u32 cnt_mem_prepare;
301b5b4541eSHans Verkuil u32 cnt_mem_finish;
302b5b4541eSHans Verkuil u32 cnt_mem_attach_dmabuf;
303b5b4541eSHans Verkuil u32 cnt_mem_detach_dmabuf;
304b5b4541eSHans Verkuil u32 cnt_mem_map_dmabuf;
305b5b4541eSHans Verkuil u32 cnt_mem_unmap_dmabuf;
306b5b4541eSHans Verkuil u32 cnt_mem_vaddr;
307b5b4541eSHans Verkuil u32 cnt_mem_cookie;
308b5b4541eSHans Verkuil u32 cnt_mem_num_users;
309b5b4541eSHans Verkuil u32 cnt_mem_mmap;
310b5b4541eSHans Verkuil
31128d77c21SHans Verkuil u32 cnt_buf_out_validate;
312b5b4541eSHans Verkuil u32 cnt_buf_init;
313b5b4541eSHans Verkuil u32 cnt_buf_prepare;
314b5b4541eSHans Verkuil u32 cnt_buf_finish;
315b5b4541eSHans Verkuil u32 cnt_buf_cleanup;
316b5b4541eSHans Verkuil u32 cnt_buf_queue;
317fd89e0bbSHans Verkuil u32 cnt_buf_request_complete;
318b5b4541eSHans Verkuil
319b5b4541eSHans Verkuil /* This counts the number of calls to vb2_buffer_done() */
320b5b4541eSHans Verkuil u32 cnt_buf_done;
321b5b4541eSHans Verkuil #endif
322e23ccc0aSPawel Osciak };
323e23ccc0aSPawel Osciak
324e23ccc0aSPawel Osciak /**
3252b141324SMauro Carvalho Chehab * struct vb2_ops - driver-specific callbacks.
326e23ccc0aSPawel Osciak *
3273f97df91SLuca Ceresoli * These operations are not called from interrupt context except where
3283f97df91SLuca Ceresoli * mentioned specifically.
3293f97df91SLuca Ceresoli *
330f286f4dfSMauro Carvalho Chehab * @queue_setup: called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
331df9ecb0cSHans Verkuil * handlers before memory allocation. It can be called
332df9ecb0cSHans Verkuil * twice: if the original number of requested buffers
333df9ecb0cSHans Verkuil * could not be allocated, then it will be called a
334df9ecb0cSHans Verkuil * second time with the actually allocated number of
335df9ecb0cSHans Verkuil * buffers to verify if that is OK.
336df9ecb0cSHans Verkuil * The driver should return the required number of buffers
33789cb3ddbSMauro Carvalho Chehab * in \*num_buffers, the required number of planes per
33889cb3ddbSMauro Carvalho Chehab * buffer in \*num_planes, the size of each plane should be
339564aaf69SMauro Carvalho Chehab * set in the sizes\[\] array and optional per-plane
340564aaf69SMauro Carvalho Chehab * allocator specific device in the alloc_devs\[\] array.
341e5c27ef3SAnton Leontiev * When called from VIDIOC_REQBUFS(), \*num_planes == 0,
342f286f4dfSMauro Carvalho Chehab * the driver has to use the currently configured format to
34389cb3ddbSMauro Carvalho Chehab * determine the plane sizes and \*num_buffers is the total
344df9ecb0cSHans Verkuil * number of buffers that are being allocated. When called
345e5c27ef3SAnton Leontiev * from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
346564aaf69SMauro Carvalho Chehab * describes the requested number of planes and sizes\[\]
347e5c27ef3SAnton Leontiev * contains the requested plane sizes. In this case
348e5c27ef3SAnton Leontiev * \*num_buffers are being allocated additionally to
349e5c27ef3SAnton Leontiev * q->num_buffers. If either \*num_planes or the requested
350e5c27ef3SAnton Leontiev * sizes are invalid callback must return %-EINVAL.
351e23ccc0aSPawel Osciak * @wait_prepare: release any locks taken while calling vb2 functions;
352e23ccc0aSPawel Osciak * it is called before an ioctl needs to wait for a new
353e23ccc0aSPawel Osciak * buffer to arrive; required to avoid a deadlock in
3543f12e6b0SHans Verkuil * blocking access type.
355e23ccc0aSPawel Osciak * @wait_finish: reacquire all locks released in the previous callback;
356e23ccc0aSPawel Osciak * required to continue operation after sleeping while
3573f12e6b0SHans Verkuil * waiting for a new buffer to arrive.
35828d77c21SHans Verkuil * @buf_out_validate: called when the output buffer is prepared or queued
35928d77c21SHans Verkuil * to a request; drivers can use this to validate
360c2d88e7dSEzequiel Garcia * userspace-provided information; this is required only
361c2d88e7dSEzequiel Garcia * for OUTPUT queues.
362e23ccc0aSPawel Osciak * @buf_init: called once after allocating a buffer (in MMAP case)
363e23ccc0aSPawel Osciak * or after acquiring a new USERPTR buffer; drivers may
364e23ccc0aSPawel Osciak * perform additional buffer-related initialization;
365e23ccc0aSPawel Osciak * initialization failure (return != 0) will prevent
3663f12e6b0SHans Verkuil * queue setup from completing successfully; optional.
3672d86401cSGuennadi Liakhovetski * @buf_prepare: called every time the buffer is queued from userspace
368f286f4dfSMauro Carvalho Chehab * and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
369cf227429SHans Verkuil * perform any initialization required before each
370cf227429SHans Verkuil * hardware operation in this callback; drivers can
371cf227429SHans Verkuil * access/modify the buffer here as it is still synced for
372f286f4dfSMauro Carvalho Chehab * the CPU; drivers that support VIDIOC_CREATE_BUFS() must
373cf227429SHans Verkuil * also validate the buffer size; if an error is returned,
374cf227429SHans Verkuil * the buffer will not be queued in driver; optional.
375e23ccc0aSPawel Osciak * @buf_finish: called before every dequeue of the buffer back to
376cf227429SHans Verkuil * userspace; the buffer is synced for the CPU, so drivers
377cf227429SHans Verkuil * can access/modify the buffer contents; drivers may
378cf227429SHans Verkuil * perform any operations required before userspace
379cf227429SHans Verkuil * accesses the buffer; optional. The buffer state can be
380564aaf69SMauro Carvalho Chehab * one of the following: %DONE and %ERROR occur while
381564aaf69SMauro Carvalho Chehab * streaming is in progress, and the %PREPARED state occurs
382cf227429SHans Verkuil * when the queue has been canceled and all pending
383564aaf69SMauro Carvalho Chehab * buffers are being returned to their default %DEQUEUED
384cf227429SHans Verkuil * state. Typically you only have to do something if the
385564aaf69SMauro Carvalho Chehab * state is %VB2_BUF_STATE_DONE, since in all other cases
386cf227429SHans Verkuil * the buffer contents will be ignored anyway.
387e23ccc0aSPawel Osciak * @buf_cleanup: called once before the buffer is freed; drivers may
3883f12e6b0SHans Verkuil * perform any additional cleanup; optional.
389*a10b2153SHans Verkuil * @prepare_streaming: called once to prepare for 'streaming' state; this is
390*a10b2153SHans Verkuil * where validation can be done to verify everything is
391*a10b2153SHans Verkuil * okay and streaming resources can be claimed. It is
392*a10b2153SHans Verkuil * called when the VIDIOC_STREAMON ioctl is called. The
393*a10b2153SHans Verkuil * actual streaming starts when @start_streaming is called.
394*a10b2153SHans Verkuil * Optional.
395bd323e28SMarek Szyprowski * @start_streaming: called once to enter 'streaming' state; the driver may
396564aaf69SMauro Carvalho Chehab * receive buffers with @buf_queue callback
397564aaf69SMauro Carvalho Chehab * before @start_streaming is called; the driver gets the
398564aaf69SMauro Carvalho Chehab * number of already queued buffers in count parameter;
399564aaf69SMauro Carvalho Chehab * driver can return an error if hardware fails, in that
400564aaf69SMauro Carvalho Chehab * case all buffers that have been already given by
401564aaf69SMauro Carvalho Chehab * the @buf_queue callback are to be returned by the driver
402c6e4e2c4SHans Verkuil * by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
403c6e4e2c4SHans Verkuil * If you need a minimum number of buffers before you can
404c6e4e2c4SHans Verkuil * start streaming, then set
405c6e4e2c4SHans Verkuil * &vb2_queue->min_buffers_needed. If that is non-zero
40668a06bd0SLuca Ceresoli * then @start_streaming won't be called until at least
40768a06bd0SLuca Ceresoli * that many buffers have been queued up by userspace.
408e23ccc0aSPawel Osciak * @stop_streaming: called when 'streaming' state must be disabled; driver
409e23ccc0aSPawel Osciak * should stop any DMA transactions or wait until they
410564aaf69SMauro Carvalho Chehab * finish and give back all buffers it got from &buf_queue
411f286f4dfSMauro Carvalho Chehab * callback by calling vb2_buffer_done() with either
412564aaf69SMauro Carvalho Chehab * %VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
413ccf58cb4SHans Verkuil * vb2_wait_for_all_buffers() function
414*a10b2153SHans Verkuil * @unprepare_streaming:called as counterpart to @prepare_streaming; any claimed
415*a10b2153SHans Verkuil * streaming resources can be released here. It is
416*a10b2153SHans Verkuil * called when the VIDIOC_STREAMOFF ioctls is called or
417*a10b2153SHans Verkuil * when the streaming filehandle is closed. Optional.
418e23ccc0aSPawel Osciak * @buf_queue: passes buffer vb to the driver; driver may start
419e23ccc0aSPawel Osciak * hardware operation on this buffer; driver should give
420bd323e28SMarek Szyprowski * the buffer back by calling vb2_buffer_done() function;
421e907bf3cSMauro Carvalho Chehab * it is always called after calling VIDIOC_STREAMON()
422f286f4dfSMauro Carvalho Chehab * ioctl; might be called before @start_streaming callback
423f286f4dfSMauro Carvalho Chehab * if user pre-queued buffers before calling
424f286f4dfSMauro Carvalho Chehab * VIDIOC_STREAMON().
425fd89e0bbSHans Verkuil * @buf_request_complete: a buffer that was never queued to the driver but is
426fd89e0bbSHans Verkuil * associated with a queued request was canceled.
427fd89e0bbSHans Verkuil * The driver will have to mark associated objects in the
428fd89e0bbSHans Verkuil * request as completed; required if requests are
429fd89e0bbSHans Verkuil * supported.
430e23ccc0aSPawel Osciak */
431e23ccc0aSPawel Osciak struct vb2_ops {
432df9ecb0cSHans Verkuil int (*queue_setup)(struct vb2_queue *q,
433fc714e70SGuennadi Liakhovetski unsigned int *num_buffers, unsigned int *num_planes,
43436c0f8b3SHans Verkuil unsigned int sizes[], struct device *alloc_devs[]);
435e23ccc0aSPawel Osciak
436e23ccc0aSPawel Osciak void (*wait_prepare)(struct vb2_queue *q);
437e23ccc0aSPawel Osciak void (*wait_finish)(struct vb2_queue *q);
438e23ccc0aSPawel Osciak
43928d77c21SHans Verkuil int (*buf_out_validate)(struct vb2_buffer *vb);
440e23ccc0aSPawel Osciak int (*buf_init)(struct vb2_buffer *vb);
441e23ccc0aSPawel Osciak int (*buf_prepare)(struct vb2_buffer *vb);
44206470642SHans Verkuil void (*buf_finish)(struct vb2_buffer *vb);
443e23ccc0aSPawel Osciak void (*buf_cleanup)(struct vb2_buffer *vb);
444e23ccc0aSPawel Osciak
445*a10b2153SHans Verkuil int (*prepare_streaming)(struct vb2_queue *q);
446bd323e28SMarek Szyprowski int (*start_streaming)(struct vb2_queue *q, unsigned int count);
447e37559b2SHans Verkuil void (*stop_streaming)(struct vb2_queue *q);
448*a10b2153SHans Verkuil void (*unprepare_streaming)(struct vb2_queue *q);
449e23ccc0aSPawel Osciak
450e23ccc0aSPawel Osciak void (*buf_queue)(struct vb2_buffer *vb);
451fd89e0bbSHans Verkuil
452fd89e0bbSHans Verkuil void (*buf_request_complete)(struct vb2_buffer *vb);
453e23ccc0aSPawel Osciak };
454e23ccc0aSPawel Osciak
45510cc3b1eSHans Verkuil /**
4562b141324SMauro Carvalho Chehab * struct vb2_buf_ops - driver-specific callbacks.
45710cc3b1eSHans Verkuil *
458e7e0c3e2SSakari Ailus * @verify_planes_array: Verify that a given user space structure contains
459e7e0c3e2SSakari Ailus * enough planes for the buffer. This is called
460e7e0c3e2SSakari Ailus * for each dequeued buffer.
4618e013700SHans Verkuil * @init_buffer: given a &vb2_buffer initialize the extra data after
4628e013700SHans Verkuil * struct vb2_buffer.
4638e013700SHans Verkuil * For V4L2 this is a &struct vb2_v4l2_buffer.
46460e7926bSMauro Carvalho Chehab * @fill_user_buffer: given a &vb2_buffer fill in the userspace structure.
4652b141324SMauro Carvalho Chehab * For V4L2 this is a &struct v4l2_buffer.
46660e7926bSMauro Carvalho Chehab * @fill_vb2_buffer: given a userspace structure, fill in the &vb2_buffer.
46710cc3b1eSHans Verkuil * If the userspace structure is invalid, then this op
46810cc3b1eSHans Verkuil * will return an error.
46910cc3b1eSHans Verkuil * @copy_timestamp: copy the timestamp from a userspace structure to
4702b141324SMauro Carvalho Chehab * the &struct vb2_buffer.
47110cc3b1eSHans Verkuil */
472b0e0e1f8SJunghak Sung struct vb2_buf_ops {
473e7e0c3e2SSakari Ailus int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
4748e013700SHans Verkuil void (*init_buffer)(struct vb2_buffer *vb);
47510cc3b1eSHans Verkuil void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
476db6e8d57SHans Verkuil int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes);
47710cc3b1eSHans Verkuil void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
478b0e0e1f8SJunghak Sung };
4795a5adf6bSHans Verkuil
480e23ccc0aSPawel Osciak /**
4813e947c36SHans Verkuil * struct vb2_queue - a videobuf2 queue.
482e23ccc0aSPawel Osciak *
483bed04f93SJunghak Sung * @type: private buffer type whose content is defined by the vb2-core
484bed04f93SJunghak Sung * caller. For example, for V4L2, it should match
4852b141324SMauro Carvalho Chehab * the types defined on &enum v4l2_buf_type.
4862b141324SMauro Carvalho Chehab * @io_modes: supported io methods (see &enum vb2_io_modes).
4872b141324SMauro Carvalho Chehab * @alloc_devs: &struct device memory type/allocator-specific per-plane device
48820be7ab8SHans Verkuil * @dev: device to use for the default allocation context if the driver
48936c0f8b3SHans Verkuil * doesn't fill in the @alloc_devs array.
49000085f1eSKrzysztof Kozlowski * @dma_attrs: DMA attributes to use for the DMA.
4915b6f9abeSStanimir Varbanov * @bidirectional: when this flag is set the DMA direction for the buffers of
49260e7926bSMauro Carvalho Chehab * this queue will be overridden with %DMA_BIDIRECTIONAL direction.
4935b6f9abeSStanimir Varbanov * This is useful in cases where the hardware (firmware) writes to
49460e7926bSMauro Carvalho Chehab * a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from
49560e7926bSMauro Carvalho Chehab * buffer which is mapped for write (%DMA_FROM_DEVICE) in order
4965b6f9abeSStanimir Varbanov * to satisfy some internal hardware restrictions or adds a padding
4975b6f9abeSStanimir Varbanov * needed by the processing algorithm. In case the DMA mapping is
4985b6f9abeSStanimir Varbanov * not bidirectional but the hardware (firmware) trying to access
4995b6f9abeSStanimir Varbanov * the buffer (in the opposite direction) this could lead to an
5005b6f9abeSStanimir Varbanov * IOMMU protection faults.
50106e7a9b6SKamil Debski * @fileio_read_once: report EOF after reading the first buffer
50206e7a9b6SKamil Debski * @fileio_write_immediately: queue buffer after each write() call
503f61bf13bSKamil Debski * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
504a9a08845SLinus Torvalds * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF
505b9387684SRicardo Ribalda * has not been called. This is a vb1 idiom that has been adopted
506b9387684SRicardo Ribalda * also by vb2.
507e5079cf1SHans Verkuil * @supports_requests: this queue supports the Request API.
50890675d39SHans Verkuil * @requires_requests: this queue requires the Request API. If this is set to 1,
50990675d39SHans Verkuil * then supports_requests must be set to 1 as well.
51061add367SHans Verkuil * @uses_qbuf: qbuf was used directly for this queue. Set to 1 the first
51161add367SHans Verkuil * time this is called. Set to 0 when the queue is canceled.
51261add367SHans Verkuil * If this is 1, then you cannot queue buffers from a request.
51361add367SHans Verkuil * @uses_requests: requests are used for this queue. Set to 1 the first time
51461add367SHans Verkuil * a request is queued. Set to 0 when the queue is canceled.
51561add367SHans Verkuil * If this is 1, then you cannot queue buffers directly.
5166d219986SSergey Senozhatsky * @allow_cache_hints: when set user-space can pass cache management hints in
5176d219986SSergey Senozhatsky * order to skip cache flush/invalidation on ->prepare() or/and
5186d219986SSergey Senozhatsky * ->finish().
519b00a9e59SSergey Senozhatsky * @non_coherent_mem: when set queue will attempt to allocate buffers using
520b00a9e59SSergey Senozhatsky * non-coherent memory.
5212b141324SMauro Carvalho Chehab * @lock: pointer to a mutex that protects the &struct vb2_queue. The
5225a5adf6bSHans Verkuil * driver can set this to a mutex to let the v4l2 core serialize
5235a5adf6bSHans Verkuil * the queuing ioctls. If the driver wants to handle locking
5245a5adf6bSHans Verkuil * itself, then this should be set to NULL. This lock is not used
5255a5adf6bSHans Verkuil * by the videobuf2 core API.
5265a5adf6bSHans Verkuil * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
5275a5adf6bSHans Verkuil * that called reqbufs, create_buffers or started fileio.
5285a5adf6bSHans Verkuil * This field is not used by the videobuf2 core API, but it allows
5295a5adf6bSHans Verkuil * drivers to easily associate an owner filehandle with the queue.
530e23ccc0aSPawel Osciak * @ops: driver-specific callbacks
531e23ccc0aSPawel Osciak * @mem_ops: memory allocator specific callbacks
5322b141324SMauro Carvalho Chehab * @buf_ops: callbacks to deliver buffer information.
5332b141324SMauro Carvalho Chehab * between user-space and kernel-space.
5342b141324SMauro Carvalho Chehab * @drv_priv: driver private data.
535137272cdSHans Verkuil * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used
536137272cdSHans Verkuil * by the vb2 core.
537e23ccc0aSPawel Osciak * @buf_struct_size: size of the driver-specific buffer structure;
538e23ccc0aSPawel Osciak * "0" indicates the driver doesn't want to use a custom buffer
5391583c793SHans Verkuil * structure type. In that case a subsystem-specific struct
5401583c793SHans Verkuil * will be used (in the case of V4L2 that is
5411583c793SHans Verkuil * ``sizeof(struct vb2_v4l2_buffer)``). The first field of the
5421583c793SHans Verkuil * driver-specific buffer structure must be the subsystem-specific
5431583c793SHans Verkuil * struct (vb2_v4l2_buffer in the case of V4L2).
5442b141324SMauro Carvalho Chehab * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and
5452b141324SMauro Carvalho Chehab * ``V4L2_BUF_FLAG_TSTAMP_SRC_*``
546b6ba2057SHans Verkuil * @gfp_flags: additional gfp flags used when allocating the buffers.
54760e7926bSMauro Carvalho Chehab * Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32
548b6ba2057SHans Verkuil * to force the buffer allocation to a specific memory zone.
549b3379c62SHans Verkuil * @min_buffers_needed: the minimum number of buffers needed before
550f286f4dfSMauro Carvalho Chehab * @start_streaming can be called. Used when a DMA engine
551b3379c62SHans Verkuil * cannot be started unless at least this number of buffers
552b3379c62SHans Verkuil * have been queued into the driver.
553d78757e7SMauro Carvalho Chehab */
554d78757e7SMauro Carvalho Chehab /*
555f286f4dfSMauro Carvalho Chehab * Private elements (won't appear at the uAPI book):
556f035eb4eSHans Verkuil * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped
557e23ccc0aSPawel Osciak * @memory: current memory type used
5585b6f9abeSStanimir Varbanov * @dma_dir: DMA mapping direction.
5593e947c36SHans Verkuil * @bufs: videobuf2 buffer structures
560e23ccc0aSPawel Osciak * @num_buffers: number of allocated/used buffers
561e23ccc0aSPawel Osciak * @queued_list: list of buffers currently queued from userspace
562b3379c62SHans Verkuil * @queued_count: number of buffers queued and ready for streaming.
5636ea3b980SHans Verkuil * @owned_by_drv_count: number of buffers owned by the driver
564e23ccc0aSPawel Osciak * @done_list: list of buffers ready to be dequeued to userspace
565e23ccc0aSPawel Osciak * @done_lock: lock to protect done_list list
566e23ccc0aSPawel Osciak * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
567e23ccc0aSPawel Osciak * @streaming: current streaming state
568f286f4dfSMauro Carvalho Chehab * @start_streaming_called: @start_streaming was called successfully and we
569b3379c62SHans Verkuil * started streaming.
5704bb7267dSLaurent Pinchart * @error: a fatal error occurred on the queue
57158d75f4bSHans Verkuil * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
57258d75f4bSHans Verkuil * buffers. Only set for capture queues if qbuf has not yet been
573a9a08845SLinus Torvalds * called since poll() needs to return %EPOLLERR in that situation.
574bed04f93SJunghak Sung * @is_multiplanar: set if buffer type is multiplanar
575bed04f93SJunghak Sung * @is_output: set if buffer type is output
576959c3ef3SJunghak Sung * @copy_timestamp: set if vb2-core should set timestamps
577c1621840SPhilipp Zabel * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
578c1621840SPhilipp Zabel * last decoded buffer was already dequeued. Set for capture queues
57960e7926bSMauro Carvalho Chehab * when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued.
580b25748feSMarek Szyprowski * @fileio: file io emulator internal data, used only if emulator is active
5813415a89fSHans Verkuil * @threadio: thread io internal data, used only if thread is active
582b820935bSLaurent Pinchart * @name: queue name, used for logging purpose. Initialized automatically
583b820935bSLaurent Pinchart * if left empty by drivers.
584e23ccc0aSPawel Osciak */
585e23ccc0aSPawel Osciak struct vb2_queue {
586bed04f93SJunghak Sung unsigned int type;
587e23ccc0aSPawel Osciak unsigned int io_modes;
58820be7ab8SHans Verkuil struct device *dev;
58900085f1eSKrzysztof Kozlowski unsigned long dma_attrs;
590830e87edSSergey Senozhatsky unsigned int bidirectional:1;
591830e87edSSergey Senozhatsky unsigned int fileio_read_once:1;
592830e87edSSergey Senozhatsky unsigned int fileio_write_immediately:1;
593830e87edSSergey Senozhatsky unsigned int allow_zero_bytesused:1;
594830e87edSSergey Senozhatsky unsigned int quirk_poll_must_check_waiting_for_buffers:1;
595830e87edSSergey Senozhatsky unsigned int supports_requests:1;
596830e87edSSergey Senozhatsky unsigned int requires_requests:1;
597830e87edSSergey Senozhatsky unsigned int uses_qbuf:1;
598830e87edSSergey Senozhatsky unsigned int uses_requests:1;
5996d219986SSergey Senozhatsky unsigned int allow_cache_hints:1;
600b00a9e59SSergey Senozhatsky unsigned int non_coherent_mem:1;
60106e7a9b6SKamil Debski
6025a5adf6bSHans Verkuil struct mutex *lock;
603bed04f93SJunghak Sung void *owner;
604e23ccc0aSPawel Osciak
605e23ccc0aSPawel Osciak const struct vb2_ops *ops;
606e23ccc0aSPawel Osciak const struct vb2_mem_ops *mem_ops;
607b0e0e1f8SJunghak Sung const struct vb2_buf_ops *buf_ops;
608b0e0e1f8SJunghak Sung
609e23ccc0aSPawel Osciak void *drv_priv;
610137272cdSHans Verkuil u32 subsystem_flags;
611e23ccc0aSPawel Osciak unsigned int buf_struct_size;
612ade48681SSakari Ailus u32 timestamp_flags;
613b6ba2057SHans Verkuil gfp_t gfp_flags;
614b3379c62SHans Verkuil u32 min_buffers_needed;
615e23ccc0aSPawel Osciak
6162b141324SMauro Carvalho Chehab struct device *alloc_devs[VB2_MAX_PLANES];
6172b141324SMauro Carvalho Chehab
618e23ccc0aSPawel Osciak /* private: internal use only */
619f035eb4eSHans Verkuil struct mutex mmap_lock;
620bed04f93SJunghak Sung unsigned int memory;
6215b6f9abeSStanimir Varbanov enum dma_data_direction dma_dir;
622bed04f93SJunghak Sung struct vb2_buffer *bufs[VB2_MAX_FRAME];
623e23ccc0aSPawel Osciak unsigned int num_buffers;
624e23ccc0aSPawel Osciak
625e23ccc0aSPawel Osciak struct list_head queued_list;
626b3379c62SHans Verkuil unsigned int queued_count;
627e23ccc0aSPawel Osciak
6286ea3b980SHans Verkuil atomic_t owned_by_drv_count;
629e23ccc0aSPawel Osciak struct list_head done_list;
630e23ccc0aSPawel Osciak spinlock_t done_lock;
631e23ccc0aSPawel Osciak wait_queue_head_t done_wq;
632e23ccc0aSPawel Osciak
633e23ccc0aSPawel Osciak unsigned int streaming:1;
634b3379c62SHans Verkuil unsigned int start_streaming_called:1;
6354bb7267dSLaurent Pinchart unsigned int error:1;
63658d75f4bSHans Verkuil unsigned int waiting_for_buffers:1;
637d65842f7SHans Verkuil unsigned int waiting_in_dqbuf:1;
638bed04f93SJunghak Sung unsigned int is_multiplanar:1;
639bed04f93SJunghak Sung unsigned int is_output:1;
640959c3ef3SJunghak Sung unsigned int copy_timestamp:1;
641c1621840SPhilipp Zabel unsigned int last_buffer_dequeued:1;
642b25748feSMarek Szyprowski
643b25748feSMarek Szyprowski struct vb2_fileio_data *fileio;
6443415a89fSHans Verkuil struct vb2_threadio_data *threadio;
645b5b4541eSHans Verkuil
646b820935bSLaurent Pinchart char name[32];
647b820935bSLaurent Pinchart
648b5b4541eSHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
649b5b4541eSHans Verkuil /*
650b5b4541eSHans Verkuil * Counters for how often these queue-related ops are
651b5b4541eSHans Verkuil * called. Used to check for unbalanced ops.
652b5b4541eSHans Verkuil */
653b5b4541eSHans Verkuil u32 cnt_queue_setup;
654b5b4541eSHans Verkuil u32 cnt_wait_prepare;
655b5b4541eSHans Verkuil u32 cnt_wait_finish;
656*a10b2153SHans Verkuil u32 cnt_prepare_streaming;
657b5b4541eSHans Verkuil u32 cnt_start_streaming;
658b5b4541eSHans Verkuil u32 cnt_stop_streaming;
659*a10b2153SHans Verkuil u32 cnt_unprepare_streaming;
660b5b4541eSHans Verkuil #endif
661e23ccc0aSPawel Osciak };
662e23ccc0aSPawel Osciak
66388b7eb09SMauro Carvalho Chehab /**
664f5f5fa73SSergey Senozhatsky * vb2_queue_allows_cache_hints() - Return true if the queue allows cache
665f5f5fa73SSergey Senozhatsky * and memory consistency hints.
666f5f5fa73SSergey Senozhatsky *
667f5f5fa73SSergey Senozhatsky * @q: pointer to &struct vb2_queue with videobuf2 queue
668f5f5fa73SSergey Senozhatsky */
vb2_queue_allows_cache_hints(struct vb2_queue * q)669f5f5fa73SSergey Senozhatsky static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
670f5f5fa73SSergey Senozhatsky {
671f5f5fa73SSergey Senozhatsky return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
672f5f5fa73SSergey Senozhatsky }
673f5f5fa73SSergey Senozhatsky
674f5f5fa73SSergey Senozhatsky /**
6752b141324SMauro Carvalho Chehab * vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
6762b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to which the plane in
6772b141324SMauro Carvalho Chehab * question belongs to.
6782b141324SMauro Carvalho Chehab * @plane_no: plane number for which the address is to be returned.
67988b7eb09SMauro Carvalho Chehab *
68088b7eb09SMauro Carvalho Chehab * This function returns a kernel virtual address of a given plane if
68188b7eb09SMauro Carvalho Chehab * such a mapping exist, NULL otherwise.
68288b7eb09SMauro Carvalho Chehab */
683e23ccc0aSPawel Osciak void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
68488b7eb09SMauro Carvalho Chehab
68588b7eb09SMauro Carvalho Chehab /**
6862b141324SMauro Carvalho Chehab * vb2_plane_cookie() - Return allocator specific cookie for the given plane.
6872b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to which the plane in
6882b141324SMauro Carvalho Chehab * question belongs to.
6892b141324SMauro Carvalho Chehab * @plane_no: plane number for which the cookie is to be returned.
69088b7eb09SMauro Carvalho Chehab *
69188b7eb09SMauro Carvalho Chehab * This function returns an allocator specific cookie for a given plane if
69288b7eb09SMauro Carvalho Chehab * available, NULL otherwise. The allocator should provide some simple static
69388b7eb09SMauro Carvalho Chehab * inline function, which would convert this cookie to the allocator specific
69488b7eb09SMauro Carvalho Chehab * type that can be used directly by the driver to access the buffer. This can
69588b7eb09SMauro Carvalho Chehab * be for example physical address, pointer to scatter list or IOMMU mapping.
69688b7eb09SMauro Carvalho Chehab */
697e23ccc0aSPawel Osciak void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
698e23ccc0aSPawel Osciak
69988b7eb09SMauro Carvalho Chehab /**
7003e947c36SHans Verkuil * vb2_buffer_done() - inform videobuf2 that an operation on a buffer
7012b141324SMauro Carvalho Chehab * is finished.
7022b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to be used.
7032b141324SMauro Carvalho Chehab * @state: state of the buffer, as defined by &enum vb2_buffer_state.
7042b141324SMauro Carvalho Chehab * Either %VB2_BUF_STATE_DONE if the operation finished
705f286f4dfSMauro Carvalho Chehab * successfully, %VB2_BUF_STATE_ERROR if the operation finished
706c6e4e2c4SHans Verkuil * with an error or %VB2_BUF_STATE_QUEUED.
70788b7eb09SMauro Carvalho Chehab *
70888b7eb09SMauro Carvalho Chehab * This function should be called by the driver after a hardware operation on
70988b7eb09SMauro Carvalho Chehab * a buffer is finished and the buffer may be returned to userspace. The driver
71088b7eb09SMauro Carvalho Chehab * cannot use this buffer anymore until it is queued back to it by videobuf
711f286f4dfSMauro Carvalho Chehab * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
712f286f4dfSMauro Carvalho Chehab * to the driver by &vb2_ops->buf_queue can be passed to this function.
71388b7eb09SMauro Carvalho Chehab *
71488b7eb09SMauro Carvalho Chehab * While streaming a buffer can only be returned in state DONE or ERROR.
71545ad3999SLuca Ceresoli * The &vb2_ops->start_streaming op can also return them in case the DMA engine
71645ad3999SLuca Ceresoli * cannot be started for some reason. In that case the buffers should be
717c6e4e2c4SHans Verkuil * returned with state QUEUED to put them back into the queue.
71888b7eb09SMauro Carvalho Chehab */
719e23ccc0aSPawel Osciak void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
72088b7eb09SMauro Carvalho Chehab
72188b7eb09SMauro Carvalho Chehab /**
7222b141324SMauro Carvalho Chehab * vb2_discard_done() - discard all buffers marked as DONE.
7232b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
72488b7eb09SMauro Carvalho Chehab *
72588b7eb09SMauro Carvalho Chehab * This function is intended to be used with suspend/resume operations. It
72688b7eb09SMauro Carvalho Chehab * discards all 'done' buffers as they would be too old to be requested after
72788b7eb09SMauro Carvalho Chehab * resume.
72888b7eb09SMauro Carvalho Chehab *
72988b7eb09SMauro Carvalho Chehab * Drivers must stop the hardware and synchronize with interrupt handlers and/or
73088b7eb09SMauro Carvalho Chehab * delayed works before calling this function to make sure no buffer will be
73188b7eb09SMauro Carvalho Chehab * touched by the driver and/or hardware.
73288b7eb09SMauro Carvalho Chehab */
73334ea4d44SLaurent Pinchart void vb2_discard_done(struct vb2_queue *q);
73488b7eb09SMauro Carvalho Chehab
73588b7eb09SMauro Carvalho Chehab /**
7362b141324SMauro Carvalho Chehab * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2.
7372b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
73888b7eb09SMauro Carvalho Chehab *
73988b7eb09SMauro Carvalho Chehab * This function will wait until all buffers that have been given to the driver
740f286f4dfSMauro Carvalho Chehab * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
7412b141324SMauro Carvalho Chehab * doesn't call &vb2_ops->wait_prepare/&vb2_ops->wait_finish pair.
7422b141324SMauro Carvalho Chehab * It is intended to be called with all locks taken, for example from
7432b141324SMauro Carvalho Chehab * &vb2_ops->stop_streaming callback.
74488b7eb09SMauro Carvalho Chehab */
745e23ccc0aSPawel Osciak int vb2_wait_for_all_buffers(struct vb2_queue *q);
746e23ccc0aSPawel Osciak
74788b7eb09SMauro Carvalho Chehab /**
7482b141324SMauro Carvalho Chehab * vb2_core_querybuf() - query video buffer information.
7492b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
7502b141324SMauro Carvalho Chehab * @index: id number of the buffer.
7512b141324SMauro Carvalho Chehab * @pb: buffer struct passed from userspace.
75288b7eb09SMauro Carvalho Chehab *
753991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
754991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
7552b141324SMauro Carvalho Chehab *
75688b7eb09SMauro Carvalho Chehab * The passed buffer should have been verified.
7572b141324SMauro Carvalho Chehab *
75888b7eb09SMauro Carvalho Chehab * This function fills the relevant information for the userspace.
759991232a9SMauro Carvalho Chehab *
760991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
76188b7eb09SMauro Carvalho Chehab */
76210cc3b1eSHans Verkuil void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
76388b7eb09SMauro Carvalho Chehab
76488b7eb09SMauro Carvalho Chehab /**
7652b141324SMauro Carvalho Chehab * vb2_core_reqbufs() - Initiate streaming.
7662b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
7672b141324SMauro Carvalho Chehab * @memory: memory type, as defined by &enum vb2_memory.
768b00a9e59SSergey Senozhatsky * @flags: auxiliary queue/buffer management flags. Currently, the only
769b00a9e59SSergey Senozhatsky * used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
7702b141324SMauro Carvalho Chehab * @count: requested buffer count.
77188b7eb09SMauro Carvalho Chehab *
772991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
773991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
774f286f4dfSMauro Carvalho Chehab *
77588b7eb09SMauro Carvalho Chehab * This function:
776f286f4dfSMauro Carvalho Chehab *
777991232a9SMauro Carvalho Chehab * #) verifies streaming parameters passed from the userspace;
778991232a9SMauro Carvalho Chehab * #) sets up the queue;
779f286f4dfSMauro Carvalho Chehab * #) negotiates number of buffers and planes per buffer with the driver
780991232a9SMauro Carvalho Chehab * to be used during streaming;
78160e7926bSMauro Carvalho Chehab * #) allocates internal buffer structures (&struct vb2_buffer), according to
782991232a9SMauro Carvalho Chehab * the agreed parameters;
783f286f4dfSMauro Carvalho Chehab * #) for MMAP memory type, allocates actual video memory, using the
784991232a9SMauro Carvalho Chehab * memory handling/allocation routines provided during queue initialization.
78588b7eb09SMauro Carvalho Chehab *
78688b7eb09SMauro Carvalho Chehab * If req->count is 0, all the memory will be freed instead.
78788b7eb09SMauro Carvalho Chehab *
7882b141324SMauro Carvalho Chehab * If the queue has been allocated previously by a previous vb2_core_reqbufs()
7892b141324SMauro Carvalho Chehab * call and the queue is not busy, memory will be reallocated.
79088b7eb09SMauro Carvalho Chehab *
791991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
79288b7eb09SMauro Carvalho Chehab */
7933c5be988SJunghak Sung int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
794b00a9e59SSergey Senozhatsky unsigned int flags, unsigned int *count);
79588b7eb09SMauro Carvalho Chehab
79688b7eb09SMauro Carvalho Chehab /**
79788b7eb09SMauro Carvalho Chehab * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
7982b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
7992b141324SMauro Carvalho Chehab * @memory: memory type, as defined by &enum vb2_memory.
800b00a9e59SSergey Senozhatsky * @flags: auxiliary queue/buffer management flags.
8012b141324SMauro Carvalho Chehab * @count: requested buffer count.
8022b141324SMauro Carvalho Chehab * @requested_planes: number of planes requested.
8032b141324SMauro Carvalho Chehab * @requested_sizes: array with the size of the planes.
80488b7eb09SMauro Carvalho Chehab *
805991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
806991232a9SMauro Carvalho Chehab * called internally by VB2 by an API-specific handler, like
807991232a9SMauro Carvalho Chehab * ``videobuf2-v4l2.h``.
8082b141324SMauro Carvalho Chehab *
80988b7eb09SMauro Carvalho Chehab * This function:
81088b7eb09SMauro Carvalho Chehab *
811991232a9SMauro Carvalho Chehab * #) verifies parameter sanity;
812991232a9SMauro Carvalho Chehab * #) calls the &vb2_ops->queue_setup queue operation;
813991232a9SMauro Carvalho Chehab * #) performs any necessary memory allocations.
814f286f4dfSMauro Carvalho Chehab *
815991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
81688b7eb09SMauro Carvalho Chehab */
8173c5be988SJunghak Sung int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
818b00a9e59SSergey Senozhatsky unsigned int flags, unsigned int *count,
8197b4b4555SSergey Senozhatsky unsigned int requested_planes,
820df9ecb0cSHans Verkuil const unsigned int requested_sizes[]);
82188b7eb09SMauro Carvalho Chehab
82288b7eb09SMauro Carvalho Chehab /**
82388b7eb09SMauro Carvalho Chehab * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
8242b141324SMauro Carvalho Chehab * to the kernel.
8252b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
8262b141324SMauro Carvalho Chehab * @index: id number of the buffer.
8272b141324SMauro Carvalho Chehab * @pb: buffer structure passed from userspace to
8282b141324SMauro Carvalho Chehab * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
82988b7eb09SMauro Carvalho Chehab *
830991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is
831991232a9SMauro Carvalho Chehab * called internally by VB2 by an API-specific handler, like
832991232a9SMauro Carvalho Chehab * ``videobuf2-v4l2.h``.
8332b141324SMauro Carvalho Chehab *
83488b7eb09SMauro Carvalho Chehab * The passed buffer should have been verified.
83588b7eb09SMauro Carvalho Chehab *
836991232a9SMauro Carvalho Chehab * This function calls vb2_ops->buf_prepare callback in the driver
837991232a9SMauro Carvalho Chehab * (if provided), in which driver-specific buffer initialization can
838991232a9SMauro Carvalho Chehab * be performed.
83988b7eb09SMauro Carvalho Chehab *
840991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
84188b7eb09SMauro Carvalho Chehab */
8423c5be988SJunghak Sung int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
84388b7eb09SMauro Carvalho Chehab
84488b7eb09SMauro Carvalho Chehab /**
84588b7eb09SMauro Carvalho Chehab * vb2_core_qbuf() - Queue a buffer from userspace
846f286f4dfSMauro Carvalho Chehab *
8472b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
84888b7eb09SMauro Carvalho Chehab * @index: id number of the buffer
8492b141324SMauro Carvalho Chehab * @pb: buffer structure passed from userspace to
8502b141324SMauro Carvalho Chehab * v4l2_ioctl_ops->vidioc_qbuf handler in driver
851fd89e0bbSHans Verkuil * @req: pointer to &struct media_request, may be NULL.
85288b7eb09SMauro Carvalho Chehab *
853991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called
854991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
855f286f4dfSMauro Carvalho Chehab *
85688b7eb09SMauro Carvalho Chehab * This function:
857f286f4dfSMauro Carvalho Chehab *
858fd89e0bbSHans Verkuil * #) If @req is non-NULL, then the buffer will be bound to this
859fd89e0bbSHans Verkuil * media request and it returns. The buffer will be prepared and
860fd89e0bbSHans Verkuil * queued to the driver (i.e. the next two steps) when the request
861fd89e0bbSHans Verkuil * itself is queued.
862991232a9SMauro Carvalho Chehab * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
863991232a9SMauro Carvalho Chehab * (if provided), in which driver-specific buffer initialization can
864991232a9SMauro Carvalho Chehab * be performed;
865f286f4dfSMauro Carvalho Chehab * #) if streaming is on, queues the buffer in driver by the means of
866f286f4dfSMauro Carvalho Chehab * &vb2_ops->buf_queue callback for processing.
86788b7eb09SMauro Carvalho Chehab *
868991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
86988b7eb09SMauro Carvalho Chehab */
870fd89e0bbSHans Verkuil int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
871fd89e0bbSHans Verkuil struct media_request *req);
87288b7eb09SMauro Carvalho Chehab
87388b7eb09SMauro Carvalho Chehab /**
87488b7eb09SMauro Carvalho Chehab * vb2_core_dqbuf() - Dequeue a buffer to the userspace
8752b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue
87688b7eb09SMauro Carvalho Chehab * @pindex: pointer to the buffer index. May be NULL
8772b141324SMauro Carvalho Chehab * @pb: buffer structure passed from userspace to
8782b141324SMauro Carvalho Chehab * v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
87988b7eb09SMauro Carvalho Chehab * @nonblocking: if true, this call will not sleep waiting for a buffer if no
88088b7eb09SMauro Carvalho Chehab * buffers ready for dequeuing are present. Normally the driver
8812b141324SMauro Carvalho Chehab * would be passing (file->f_flags & O_NONBLOCK) here.
88288b7eb09SMauro Carvalho Chehab *
883991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called
884991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
885f286f4dfSMauro Carvalho Chehab *
88688b7eb09SMauro Carvalho Chehab * This function:
887f286f4dfSMauro Carvalho Chehab *
888f286f4dfSMauro Carvalho Chehab * #) calls buf_finish callback in the driver (if provided), in which
88988b7eb09SMauro Carvalho Chehab * driver can perform any additional operations that may be required before
89088b7eb09SMauro Carvalho Chehab * returning the buffer to userspace, such as cache sync,
891f286f4dfSMauro Carvalho Chehab * #) the buffer struct members are filled with relevant information for
89288b7eb09SMauro Carvalho Chehab * the userspace.
89388b7eb09SMauro Carvalho Chehab *
894991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
89588b7eb09SMauro Carvalho Chehab */
896fac710e4SHans Verkuil int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
897fac710e4SHans Verkuil bool nonblocking);
898e23ccc0aSPawel Osciak
8998dcde47fSMauro Carvalho Chehab /**
9008dcde47fSMauro Carvalho Chehab * vb2_core_streamon() - Implements VB2 stream ON logic
9018dcde47fSMauro Carvalho Chehab *
9028dcde47fSMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue
9038dcde47fSMauro Carvalho Chehab * @type: type of the queue to be started.
9048dcde47fSMauro Carvalho Chehab * For V4L2, this is defined by &enum v4l2_buf_type type.
9058dcde47fSMauro Carvalho Chehab *
906991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called
907991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
908991232a9SMauro Carvalho Chehab *
909991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
9108dcde47fSMauro Carvalho Chehab */
9113c5be988SJunghak Sung int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
9128dcde47fSMauro Carvalho Chehab
9138dcde47fSMauro Carvalho Chehab /**
9148dcde47fSMauro Carvalho Chehab * vb2_core_streamoff() - Implements VB2 stream OFF logic
9158dcde47fSMauro Carvalho Chehab *
9168dcde47fSMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue
9178dcde47fSMauro Carvalho Chehab * @type: type of the queue to be started.
9188dcde47fSMauro Carvalho Chehab * For V4L2, this is defined by &enum v4l2_buf_type type.
9198dcde47fSMauro Carvalho Chehab *
920991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is
921991232a9SMauro Carvalho Chehab * called internally by VB2 by an API-specific handler, like
922991232a9SMauro Carvalho Chehab * ``videobuf2-v4l2.h``.
923991232a9SMauro Carvalho Chehab *
924991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
9258dcde47fSMauro Carvalho Chehab */
9263c5be988SJunghak Sung int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
9272d86401cSGuennadi Liakhovetski
92888b7eb09SMauro Carvalho Chehab /**
9292b141324SMauro Carvalho Chehab * vb2_core_expbuf() - Export a buffer as a file descriptor.
9302b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
9312b141324SMauro Carvalho Chehab * @fd: pointer to the file descriptor associated with DMABUF
9322b141324SMauro Carvalho Chehab * (set by driver).
9332b141324SMauro Carvalho Chehab * @type: buffer type.
9342b141324SMauro Carvalho Chehab * @index: id number of the buffer.
93588b7eb09SMauro Carvalho Chehab * @plane: index of the plane to be exported, 0 for single plane queues
9362b141324SMauro Carvalho Chehab * @flags: file flags for newly created file, as defined at
9372b141324SMauro Carvalho Chehab * include/uapi/asm-generic/fcntl.h.
9382b141324SMauro Carvalho Chehab * Currently, the only used flag is %O_CLOEXEC.
9392b141324SMauro Carvalho Chehab * is supported, refer to manual of open syscall for more details.
94088b7eb09SMauro Carvalho Chehab *
941991232a9SMauro Carvalho Chehab *
942991232a9SMauro Carvalho Chehab * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called
943991232a9SMauro Carvalho Chehab * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
944991232a9SMauro Carvalho Chehab *
945991232a9SMauro Carvalho Chehab * Return: returns zero on success; an error code otherwise.
94688b7eb09SMauro Carvalho Chehab */
9473c5be988SJunghak Sung int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
9483c5be988SJunghak Sung unsigned int index, unsigned int plane, unsigned int flags);
949e23ccc0aSPawel Osciak
95088b7eb09SMauro Carvalho Chehab /**
95188b7eb09SMauro Carvalho Chehab * vb2_core_queue_init() - initialize a videobuf2 queue
9522b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
9532b141324SMauro Carvalho Chehab * This structure should be allocated in driver
95488b7eb09SMauro Carvalho Chehab *
95560e7926bSMauro Carvalho Chehab * The &vb2_queue structure should be allocated by the driver. The driver is
95688b7eb09SMauro Carvalho Chehab * responsible of clearing it's content and setting initial values for some
95788b7eb09SMauro Carvalho Chehab * required entries before calling this function.
9582b141324SMauro Carvalho Chehab *
9592b141324SMauro Carvalho Chehab * .. note::
9602b141324SMauro Carvalho Chehab *
9612b141324SMauro Carvalho Chehab * The following fields at @q should be set before calling this function:
9622b141324SMauro Carvalho Chehab * &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type.
96388b7eb09SMauro Carvalho Chehab */
9643c5be988SJunghak Sung int vb2_core_queue_init(struct vb2_queue *q);
96588b7eb09SMauro Carvalho Chehab
96688b7eb09SMauro Carvalho Chehab /**
96788b7eb09SMauro Carvalho Chehab * vb2_core_queue_release() - stop streaming, release the queue and free memory
9682b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
96988b7eb09SMauro Carvalho Chehab *
97088b7eb09SMauro Carvalho Chehab * This function stops streaming and performs necessary clean ups, including
97188b7eb09SMauro Carvalho Chehab * freeing video buffer memory. The driver is responsible for freeing
9722b141324SMauro Carvalho Chehab * the &struct vb2_queue itself.
97388b7eb09SMauro Carvalho Chehab */
9743c5be988SJunghak Sung void vb2_core_queue_release(struct vb2_queue *q);
9753c5be988SJunghak Sung
97688b7eb09SMauro Carvalho Chehab /**
97788b7eb09SMauro Carvalho Chehab * vb2_queue_error() - signal a fatal error on the queue
9782b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
97988b7eb09SMauro Carvalho Chehab *
98088b7eb09SMauro Carvalho Chehab * Flag that a fatal unrecoverable error has occurred and wake up all processes
981a9a08845SLinus Torvalds * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing
9822b141324SMauro Carvalho Chehab * buffers will return %-EIO.
98388b7eb09SMauro Carvalho Chehab *
98460e7926bSMauro Carvalho Chehab * The error flag will be cleared when canceling the queue, either from
98560e7926bSMauro Carvalho Chehab * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this
98688b7eb09SMauro Carvalho Chehab * function before starting the stream, otherwise the error flag will remain set
98788b7eb09SMauro Carvalho Chehab * until the queue is released when closing the device node.
98888b7eb09SMauro Carvalho Chehab */
9894bb7267dSLaurent Pinchart void vb2_queue_error(struct vb2_queue *q);
990e23ccc0aSPawel Osciak
99188b7eb09SMauro Carvalho Chehab /**
9922b141324SMauro Carvalho Chehab * vb2_mmap() - map video buffers into application address space.
9932b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
9942b141324SMauro Carvalho Chehab * @vma: pointer to &struct vm_area_struct with the vma passed
9952b141324SMauro Carvalho Chehab * to the mmap file operation handler in the driver.
99688b7eb09SMauro Carvalho Chehab *
99788b7eb09SMauro Carvalho Chehab * Should be called from mmap file operation handler of a driver.
99888b7eb09SMauro Carvalho Chehab * This function maps one plane of one of the available video buffers to
99988b7eb09SMauro Carvalho Chehab * userspace. To map whole video memory allocated on reqbufs, this function
100088b7eb09SMauro Carvalho Chehab * has to be called once per each plane per each buffer previously allocated.
100188b7eb09SMauro Carvalho Chehab *
100288b7eb09SMauro Carvalho Chehab * When the userspace application calls mmap, it passes to it an offset returned
10032b141324SMauro Carvalho Chehab * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler.
10042b141324SMauro Carvalho Chehab * That offset acts as a "cookie", which is then used to identify the plane
10052b141324SMauro Carvalho Chehab * to be mapped.
10062b141324SMauro Carvalho Chehab *
100788b7eb09SMauro Carvalho Chehab * This function finds a plane with a matching offset and a mapping is performed
100888b7eb09SMauro Carvalho Chehab * by the means of a provided memory operation.
100988b7eb09SMauro Carvalho Chehab *
101088b7eb09SMauro Carvalho Chehab * The return values from this function are intended to be directly returned
101188b7eb09SMauro Carvalho Chehab * from the mmap handler in driver.
101288b7eb09SMauro Carvalho Chehab */
1013e23ccc0aSPawel Osciak int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
1014f286f4dfSMauro Carvalho Chehab
10156f524ec1SScott Jiang #ifndef CONFIG_MMU
10168dcde47fSMauro Carvalho Chehab /**
10178dcde47fSMauro Carvalho Chehab * vb2_get_unmapped_area - map video buffers into application address space.
10188dcde47fSMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
10198dcde47fSMauro Carvalho Chehab * @addr: memory address.
10208dcde47fSMauro Carvalho Chehab * @len: buffer size.
10218dcde47fSMauro Carvalho Chehab * @pgoff: page offset.
10228dcde47fSMauro Carvalho Chehab * @flags: memory flags.
10238dcde47fSMauro Carvalho Chehab *
10248dcde47fSMauro Carvalho Chehab * This function is used in noMMU platforms to propose address mapping
10258dcde47fSMauro Carvalho Chehab * for a given buffer. It's intended to be used as a handler for the
10268dcde47fSMauro Carvalho Chehab * &file_operations->get_unmapped_area operation.
10278dcde47fSMauro Carvalho Chehab *
10288dcde47fSMauro Carvalho Chehab * This is called by the mmap() syscall routines will call this
10298dcde47fSMauro Carvalho Chehab * to get a proposed address for the mapping, when ``!CONFIG_MMU``.
10308dcde47fSMauro Carvalho Chehab */
10316f524ec1SScott Jiang unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
10326f524ec1SScott Jiang unsigned long addr,
10336f524ec1SScott Jiang unsigned long len,
10346f524ec1SScott Jiang unsigned long pgoff,
10356f524ec1SScott Jiang unsigned long flags);
10366f524ec1SScott Jiang #endif
103788b7eb09SMauro Carvalho Chehab
103888b7eb09SMauro Carvalho Chehab /**
10398dcde47fSMauro Carvalho Chehab * vb2_core_poll() - implements poll syscall() logic.
10402b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
10412b141324SMauro Carvalho Chehab * @file: &struct file argument passed to the poll
10422b141324SMauro Carvalho Chehab * file operation handler.
10432b141324SMauro Carvalho Chehab * @wait: &poll_table wait argument passed to the poll
10442b141324SMauro Carvalho Chehab * file operation handler.
104588b7eb09SMauro Carvalho Chehab *
104688b7eb09SMauro Carvalho Chehab * This function implements poll file operation handler for a driver.
104788b7eb09SMauro Carvalho Chehab * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
104888b7eb09SMauro Carvalho Chehab * be informed that the file descriptor of a video device is available for
104988b7eb09SMauro Carvalho Chehab * reading.
105088b7eb09SMauro Carvalho Chehab * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
105188b7eb09SMauro Carvalho Chehab * will be reported as available for writing.
105288b7eb09SMauro Carvalho Chehab *
105388b7eb09SMauro Carvalho Chehab * The return values from this function are intended to be directly returned
105488b7eb09SMauro Carvalho Chehab * from poll handler in driver.
105588b7eb09SMauro Carvalho Chehab */
1056c23e0cb8SAl Viro __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
1057af3bac1aSJunghak Sung poll_table *wait);
1058f286f4dfSMauro Carvalho Chehab
10598dcde47fSMauro Carvalho Chehab /**
10608dcde47fSMauro Carvalho Chehab * vb2_read() - implements read() syscall logic.
10618dcde47fSMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
10628dcde47fSMauro Carvalho Chehab * @data: pointed to target userspace buffer
10638dcde47fSMauro Carvalho Chehab * @count: number of bytes to read
10648dcde47fSMauro Carvalho Chehab * @ppos: file handle position tracking pointer
10658dcde47fSMauro Carvalho Chehab * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
10668dcde47fSMauro Carvalho Chehab */
1067af3bac1aSJunghak Sung size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1068af3bac1aSJunghak Sung loff_t *ppos, int nonblock);
10698dcde47fSMauro Carvalho Chehab /**
1070b0649455SMauro Carvalho Chehab * vb2_write() - implements write() syscall logic.
10718dcde47fSMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
10728dcde47fSMauro Carvalho Chehab * @data: pointed to target userspace buffer
10738dcde47fSMauro Carvalho Chehab * @count: number of bytes to write
10748dcde47fSMauro Carvalho Chehab * @ppos: file handle position tracking pointer
10758dcde47fSMauro Carvalho Chehab * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
10768dcde47fSMauro Carvalho Chehab */
1077af3bac1aSJunghak Sung size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1078af3bac1aSJunghak Sung loff_t *ppos, int nonblock);
1079af3bac1aSJunghak Sung
1080f286f4dfSMauro Carvalho Chehab /**
10812b141324SMauro Carvalho Chehab * typedef vb2_thread_fnc - callback function for use with vb2_thread.
1082f286f4dfSMauro Carvalho Chehab *
10832b141324SMauro Carvalho Chehab * @vb: pointer to struct &vb2_buffer.
10842b141324SMauro Carvalho Chehab * @priv: pointer to a private data.
1085af3bac1aSJunghak Sung *
1086af3bac1aSJunghak Sung * This is called whenever a buffer is dequeued in the thread.
1087af3bac1aSJunghak Sung */
1088af3bac1aSJunghak Sung typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
1089af3bac1aSJunghak Sung
1090af3bac1aSJunghak Sung /**
1091af3bac1aSJunghak Sung * vb2_thread_start() - start a thread for the given queue.
10922b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
10932b141324SMauro Carvalho Chehab * @fnc: &vb2_thread_fnc callback function.
10942b141324SMauro Carvalho Chehab * @priv: priv pointer passed to the callback function.
1095af3bac1aSJunghak Sung * @thread_name:the name of the thread. This will be prefixed with "vb2-".
1096af3bac1aSJunghak Sung *
1097af3bac1aSJunghak Sung * This starts a thread that will queue and dequeue until an error occurs
10982b141324SMauro Carvalho Chehab * or vb2_thread_stop() is called.
1099af3bac1aSJunghak Sung *
1100f286f4dfSMauro Carvalho Chehab * .. attention::
1101f286f4dfSMauro Carvalho Chehab *
1102af3bac1aSJunghak Sung * This function should not be used for anything else but the videobuf2-dvb
1103af3bac1aSJunghak Sung * support. If you think you have another good use-case for this, then please
1104af3bac1aSJunghak Sung * contact the linux-media mailing list first.
1105af3bac1aSJunghak Sung */
1106af3bac1aSJunghak Sung int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1107af3bac1aSJunghak Sung const char *thread_name);
1108af3bac1aSJunghak Sung
1109af3bac1aSJunghak Sung /**
1110af3bac1aSJunghak Sung * vb2_thread_stop() - stop the thread for the given queue.
11112b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1112af3bac1aSJunghak Sung */
1113af3bac1aSJunghak Sung int vb2_thread_stop(struct vb2_queue *q);
1114e23ccc0aSPawel Osciak
1115e23ccc0aSPawel Osciak /**
11162b141324SMauro Carvalho Chehab * vb2_is_streaming() - return streaming status of the queue.
11172b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1118e23ccc0aSPawel Osciak */
vb2_is_streaming(struct vb2_queue * q)1119e23ccc0aSPawel Osciak static inline bool vb2_is_streaming(struct vb2_queue *q)
1120e23ccc0aSPawel Osciak {
1121e23ccc0aSPawel Osciak return q->streaming;
1122e23ccc0aSPawel Osciak }
1123e23ccc0aSPawel Osciak
1124e23ccc0aSPawel Osciak /**
112574753cffSHans Verkuil * vb2_fileio_is_active() - return true if fileio is active.
11262b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
112774753cffSHans Verkuil *
112874753cffSHans Verkuil * This returns true if read() or write() is used to stream the data
112974753cffSHans Verkuil * as opposed to stream I/O. This is almost never an important distinction,
113074753cffSHans Verkuil * except in rare cases. One such case is that using read() or write() to
11312b141324SMauro Carvalho Chehab * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there
113274753cffSHans Verkuil * is no way you can pass the field information of each buffer to/from
113374753cffSHans Verkuil * userspace. A driver that supports this field format should check for
11342b141324SMauro Carvalho Chehab * this in the &vb2_ops->queue_setup op and reject it if this function returns
11352b141324SMauro Carvalho Chehab * true.
113674753cffSHans Verkuil */
vb2_fileio_is_active(struct vb2_queue * q)113774753cffSHans Verkuil static inline bool vb2_fileio_is_active(struct vb2_queue *q)
113874753cffSHans Verkuil {
113974753cffSHans Verkuil return q->fileio;
114074753cffSHans Verkuil }
114174753cffSHans Verkuil
114274753cffSHans Verkuil /**
11432b141324SMauro Carvalho Chehab * vb2_is_busy() - return busy status of the queue.
11442b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1145e23ccc0aSPawel Osciak *
1146e23ccc0aSPawel Osciak * This function checks if queue has any buffers allocated.
1147e23ccc0aSPawel Osciak */
vb2_is_busy(struct vb2_queue * q)1148e23ccc0aSPawel Osciak static inline bool vb2_is_busy(struct vb2_queue *q)
1149e23ccc0aSPawel Osciak {
1150e23ccc0aSPawel Osciak return (q->num_buffers > 0);
1151e23ccc0aSPawel Osciak }
1152e23ccc0aSPawel Osciak
1153e23ccc0aSPawel Osciak /**
11542b141324SMauro Carvalho Chehab * vb2_get_drv_priv() - return driver private data associated with the queue.
11552b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1156e23ccc0aSPawel Osciak */
vb2_get_drv_priv(struct vb2_queue * q)1157e23ccc0aSPawel Osciak static inline void *vb2_get_drv_priv(struct vb2_queue *q)
1158e23ccc0aSPawel Osciak {
1159e23ccc0aSPawel Osciak return q->drv_priv;
1160e23ccc0aSPawel Osciak }
1161e23ccc0aSPawel Osciak
1162e23ccc0aSPawel Osciak /**
11632b141324SMauro Carvalho Chehab * vb2_set_plane_payload() - set bytesused for the plane @plane_no.
11642b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to which the plane in
11652b141324SMauro Carvalho Chehab * question belongs to.
11662b141324SMauro Carvalho Chehab * @plane_no: plane number for which payload should be set.
11672b141324SMauro Carvalho Chehab * @size: payload in bytes.
1168e23ccc0aSPawel Osciak */
vb2_set_plane_payload(struct vb2_buffer * vb,unsigned int plane_no,unsigned long size)1169e23ccc0aSPawel Osciak static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
1170e23ccc0aSPawel Osciak unsigned int plane_no, unsigned long size)
1171e23ccc0aSPawel Osciak {
1172ee1806beSDafna Hirschfeld /*
1173ee1806beSDafna Hirschfeld * size must never be larger than the buffer length, so
1174ee1806beSDafna Hirschfeld * warn and clamp to the buffer length if that's the case.
1175ee1806beSDafna Hirschfeld */
1176ee1806beSDafna Hirschfeld if (plane_no < vb->num_planes) {
1177ee1806beSDafna Hirschfeld if (WARN_ON_ONCE(size > vb->planes[plane_no].length))
1178ee1806beSDafna Hirschfeld size = vb->planes[plane_no].length;
11792d700715SJunghak Sung vb->planes[plane_no].bytesused = size;
1180e23ccc0aSPawel Osciak }
1181ee1806beSDafna Hirschfeld }
1182e23ccc0aSPawel Osciak
1183e23ccc0aSPawel Osciak /**
11849f00edaeSLaurent Pinchart * vb2_get_plane_payload() - get bytesused for the plane plane_no
11852b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to which the plane in
11862b141324SMauro Carvalho Chehab * question belongs to.
11872b141324SMauro Carvalho Chehab * @plane_no: plane number for which payload should be set.
1188e23ccc0aSPawel Osciak */
vb2_get_plane_payload(struct vb2_buffer * vb,unsigned int plane_no)1189e23ccc0aSPawel Osciak static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
1190e23ccc0aSPawel Osciak unsigned int plane_no)
1191e23ccc0aSPawel Osciak {
1192e23ccc0aSPawel Osciak if (plane_no < vb->num_planes)
11932d700715SJunghak Sung return vb->planes[plane_no].bytesused;
1194e23ccc0aSPawel Osciak return 0;
1195e23ccc0aSPawel Osciak }
1196e23ccc0aSPawel Osciak
1197e23ccc0aSPawel Osciak /**
11982b141324SMauro Carvalho Chehab * vb2_plane_size() - return plane size in bytes.
11992b141324SMauro Carvalho Chehab * @vb: pointer to &struct vb2_buffer to which the plane in
12002b141324SMauro Carvalho Chehab * question belongs to.
12012b141324SMauro Carvalho Chehab * @plane_no: plane number for which size should be returned.
1202e23ccc0aSPawel Osciak */
1203e23ccc0aSPawel Osciak static inline unsigned long
vb2_plane_size(struct vb2_buffer * vb,unsigned int plane_no)1204e23ccc0aSPawel Osciak vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
1205e23ccc0aSPawel Osciak {
1206e23ccc0aSPawel Osciak if (plane_no < vb->num_planes)
12072d700715SJunghak Sung return vb->planes[plane_no].length;
1208e23ccc0aSPawel Osciak return 0;
1209e23ccc0aSPawel Osciak }
1210e23ccc0aSPawel Osciak
1211ead13033SPrabhakar Lad /**
12122b141324SMauro Carvalho Chehab * vb2_start_streaming_called() - return streaming status of driver.
12132b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1214ead13033SPrabhakar Lad */
vb2_start_streaming_called(struct vb2_queue * q)1215ead13033SPrabhakar Lad static inline bool vb2_start_streaming_called(struct vb2_queue *q)
1216ead13033SPrabhakar Lad {
1217ead13033SPrabhakar Lad return q->start_streaming_called;
1218ead13033SPrabhakar Lad }
1219ead13033SPrabhakar Lad
1220c1621840SPhilipp Zabel /**
12212b141324SMauro Carvalho Chehab * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue.
12222b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1223c1621840SPhilipp Zabel */
vb2_clear_last_buffer_dequeued(struct vb2_queue * q)1224c1621840SPhilipp Zabel static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
1225c1621840SPhilipp Zabel {
1226c1621840SPhilipp Zabel q->last_buffer_dequeued = false;
1227c1621840SPhilipp Zabel }
1228c1621840SPhilipp Zabel
12293dd8c097SEzequiel Garcia /**
12303dd8c097SEzequiel Garcia * vb2_get_buffer() - get a buffer from a queue
12313dd8c097SEzequiel Garcia * @q: pointer to &struct vb2_queue with videobuf2 queue.
12323dd8c097SEzequiel Garcia * @index: buffer index
12333dd8c097SEzequiel Garcia *
12343dd8c097SEzequiel Garcia * This function obtains a buffer from a queue, by its index.
12353dd8c097SEzequiel Garcia * Keep in mind that there is no refcounting involved in this
12363dd8c097SEzequiel Garcia * operation, so the buffer lifetime should be taken into
12373dd8c097SEzequiel Garcia * consideration.
12383dd8c097SEzequiel Garcia */
vb2_get_buffer(struct vb2_queue * q,unsigned int index)12393dd8c097SEzequiel Garcia static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
12403dd8c097SEzequiel Garcia unsigned int index)
12413dd8c097SEzequiel Garcia {
12423dd8c097SEzequiel Garcia if (index < q->num_buffers)
12433dd8c097SEzequiel Garcia return q->bufs[index];
12443dd8c097SEzequiel Garcia return NULL;
12453dd8c097SEzequiel Garcia }
12463dd8c097SEzequiel Garcia
1247af3bac1aSJunghak Sung /*
1248af3bac1aSJunghak Sung * The following functions are not part of the vb2 core API, but are useful
1249af3bac1aSJunghak Sung * functions for videobuf2-*.
1250af3bac1aSJunghak Sung */
125188b7eb09SMauro Carvalho Chehab
125288b7eb09SMauro Carvalho Chehab /**
125388b7eb09SMauro Carvalho Chehab * vb2_buffer_in_use() - return true if the buffer is in use and
12542b141324SMauro Carvalho Chehab * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call.
125588b7eb09SMauro Carvalho Chehab *
12562b141324SMauro Carvalho Chehab * @vb: buffer for which plane size should be returned.
12572b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
125888b7eb09SMauro Carvalho Chehab */
1259af3bac1aSJunghak Sung bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
126088b7eb09SMauro Carvalho Chehab
126188b7eb09SMauro Carvalho Chehab /**
126288b7eb09SMauro Carvalho Chehab * vb2_verify_memory_type() - Check whether the memory type and buffer type
126388b7eb09SMauro Carvalho Chehab * passed to a buffer operation are compatible with the queue.
1264f286f4dfSMauro Carvalho Chehab *
12652b141324SMauro Carvalho Chehab * @q: pointer to &struct vb2_queue with videobuf2 queue.
1266f286f4dfSMauro Carvalho Chehab * @memory: memory model, as defined by enum &vb2_memory.
1267f286f4dfSMauro Carvalho Chehab * @type: private buffer type whose content is defined by the vb2-core
1268f286f4dfSMauro Carvalho Chehab * caller. For example, for V4L2, it should match
12692b141324SMauro Carvalho Chehab * the types defined on enum &v4l2_buf_type.
127088b7eb09SMauro Carvalho Chehab */
1271af3bac1aSJunghak Sung int vb2_verify_memory_type(struct vb2_queue *q,
1272af3bac1aSJunghak Sung enum vb2_memory memory, unsigned int type);
1273c07aa48eSHans Verkuil
1274c07aa48eSHans Verkuil /**
1275c07aa48eSHans Verkuil * vb2_request_object_is_buffer() - return true if the object is a buffer
1276c07aa48eSHans Verkuil *
1277c07aa48eSHans Verkuil * @obj: the request object.
1278c07aa48eSHans Verkuil */
1279c07aa48eSHans Verkuil bool vb2_request_object_is_buffer(struct media_request_object *obj);
1280c07aa48eSHans Verkuil
1281c07aa48eSHans Verkuil /**
1282515c5a73SPaul Kocialkowski * vb2_request_buffer_cnt() - return the number of buffers in the request
1283c07aa48eSHans Verkuil *
1284c07aa48eSHans Verkuil * @req: the request.
1285c07aa48eSHans Verkuil */
1286515c5a73SPaul Kocialkowski unsigned int vb2_request_buffer_cnt(struct media_request *req);
1287c07aa48eSHans Verkuil
1288e23ccc0aSPawel Osciak #endif /* _MEDIA_VIDEOBUF2_CORE_H */
1289