2 * videobuf2-core.h - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
12 #ifndef _MEDIA_VIDEOBUF2_CORE_H
13 #define _MEDIA_VIDEOBUF2_CORE_H
15 #include <linux/mm_types.h>
16 #include <linux/mutex.h>
17 #include <linux/poll.h>
18 #include <linux/videodev2.h>
21 struct vb2_fileio_data
;
24 * struct vb2_mem_ops - memory handling/memory allocator operations
25 * @alloc: allocate video memory and, optionally, allocator private data,
26 * return NULL on failure or a pointer to allocator private,
27 * per-buffer data on success; the returned private structure
28 * will then be passed as buf_priv argument to other ops in this
30 * @put: inform the allocator that the buffer will no longer be used;
31 * usually will result in the allocator freeing the buffer (if
32 * no other users of this buffer are present); the buf_priv
33 * argument is the allocator private per-buffer structure
34 * previously returned from the alloc callback
35 * @get_userptr: acquire userspace memory for a hardware operation; used for
36 * USERPTR memory types; vaddr is the address passed to the
37 * videobuf layer when queuing a video buffer of USERPTR type;
38 * should return an allocator private per-buffer structure
39 * associated with the buffer on success, NULL on failure;
40 * the returned private structure will then be passed as buf_priv
41 * argument to other ops in this structure
42 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
44 * @vaddr: return a kernel virtual address to a given memory buffer
45 * associated with the passed private structure or NULL if no
47 * @cookie: return allocator specific cookie for a given memory buffer
48 * associated with the passed private structure or NULL if not
50 * @num_users: return the current number of users of a memory buffer;
51 * return 1 if the videobuf layer (or actually the driver using
52 * it) is the only user
53 * @mmap: setup a userspace mapping for a given memory buffer under
54 * the provided virtual memory region
56 * Required ops for USERPTR types: get_userptr, put_userptr.
57 * Required ops for MMAP types: alloc, put, num_users, mmap.
58 * Required ops for read/write access types: alloc, put, num_users, vaddr
61 void *(*alloc
)(void *alloc_ctx
, unsigned long size
);
62 void (*put
)(void *buf_priv
);
64 void *(*get_userptr
)(void *alloc_ctx
, unsigned long vaddr
,
65 unsigned long size
, int write
);
66 void (*put_userptr
)(void *buf_priv
);
68 void *(*vaddr
)(void *buf_priv
);
69 void *(*cookie
)(void *buf_priv
);
71 unsigned int (*num_users
)(void *buf_priv
);
73 int (*mmap
)(void *buf_priv
, struct vm_area_struct
*vma
);
81 * enum vb2_io_modes - queue access methods
82 * @VB2_MMAP: driver supports MMAP with streaming API
83 * @VB2_USERPTR: driver supports USERPTR with streaming API
84 * @VB2_READ: driver supports read() style access
85 * @VB2_WRITE: driver supports write() style access
89 VB2_USERPTR
= (1 << 1),
95 * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator,
96 * by default the 'streaming' style is used by the file io emulator
97 * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer
98 * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call
100 enum vb2_fileio_flags
{
101 VB2_FILEIO_READ_ONCE
= (1 << 0),
102 VB2_FILEIO_WRITE_IMMEDIATELY
= (1 << 1),
106 * enum vb2_buffer_state - current video buffer state
107 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
108 * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
109 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
110 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
111 * in a hardware operation
112 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
113 * not yet dequeued to userspace
114 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
115 * has ended with an error, which will be reported
116 * to the userspace when it is dequeued
118 enum vb2_buffer_state
{
119 VB2_BUF_STATE_DEQUEUED
,
120 VB2_BUF_STATE_PREPARED
,
121 VB2_BUF_STATE_QUEUED
,
122 VB2_BUF_STATE_ACTIVE
,
130 * struct vb2_buffer - represents a video buffer
131 * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
132 * be read by the driver and relevant entries can be
133 * changed by the driver in case of CAPTURE types
134 * (such as timestamp)
135 * @v4l2_planes: struct v4l2_planes associated with this buffer; can
136 * be read by the driver and relevant entries can be
137 * changed by the driver in case of CAPTURE types
138 * (such as bytesused); NOTE that even for single-planar
139 * types, the v4l2_planes[0] struct should be used
140 * instead of v4l2_buf for filling bytesused - drivers
141 * should use the vb2_set_plane_payload() function for that
142 * @vb2_queue: the queue to which this driver belongs
143 * @num_planes: number of planes in the buffer
144 * on an internal driver queue
145 * @state: current buffer state; do not change
146 * @queued_entry: entry on the queued buffers list, which holds all
147 * buffers queued from userspace
148 * @done_entry: entry on the list that stores all buffers ready to
149 * be dequeued to userspace
150 * @planes: private per-plane information; do not change
153 struct v4l2_buffer v4l2_buf
;
154 struct v4l2_plane v4l2_planes
[VIDEO_MAX_PLANES
];
156 struct vb2_queue
*vb2_queue
;
158 unsigned int num_planes
;
160 /* Private: internal use only */
161 enum vb2_buffer_state state
;
163 struct list_head queued_entry
;
164 struct list_head done_entry
;
166 struct vb2_plane planes
[VIDEO_MAX_PLANES
];
170 * struct vb2_ops - driver-specific callbacks
172 * @queue_setup: called from VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS
173 * handlers before memory allocation, or, if
174 * *num_planes != 0, after the allocation to verify a
175 * smaller number of buffers. Driver should return
176 * the required number of buffers in *num_buffers, the
177 * required number of planes per buffer in *num_planes; the
178 * size of each plane should be set in the sizes[] array
179 * and optional per-plane allocator specific context in the
180 * alloc_ctxs[] array. When called from VIDIOC_REQBUFS,
181 * fmt == NULL, the driver has to use the currently
182 * configured format and *num_buffers is the total number
183 * of buffers, that are being allocated. When called from
184 * VIDIOC_CREATE_BUFS, fmt != NULL and it describes the
185 * target frame format. In this case *num_buffers are being
186 * allocated additionally to q->num_buffers.
187 * @wait_prepare: release any locks taken while calling vb2 functions;
188 * it is called before an ioctl needs to wait for a new
189 * buffer to arrive; required to avoid a deadlock in
190 * blocking access type
191 * @wait_finish: reacquire all locks released in the previous callback;
192 * required to continue operation after sleeping while
193 * waiting for a new buffer to arrive
194 * @buf_init: called once after allocating a buffer (in MMAP case)
195 * or after acquiring a new USERPTR buffer; drivers may
196 * perform additional buffer-related initialization;
197 * initialization failure (return != 0) will prevent
198 * queue setup from completing successfully; optional
199 * @buf_prepare: called every time the buffer is queued from userspace
200 * and from the VIDIOC_PREPARE_BUF ioctl; drivers may
201 * perform any initialization required before each hardware
202 * operation in this callback; if an error is returned, the
203 * buffer will not be queued in driver; optional
204 * @buf_finish: called before every dequeue of the buffer back to
205 * userspace; drivers may perform any operations required
206 * before userspace accesses the buffer; optional
207 * @buf_cleanup: called once before the buffer is freed; drivers may
208 * perform any additional cleanup; optional
209 * @start_streaming: called once to enter 'streaming' state; the driver may
210 * receive buffers with @buf_queue callback before
211 * @start_streaming is called; the driver gets the number
212 * of already queued buffers in count parameter; driver
213 * can return an error if hardware fails or not enough
214 * buffers has been queued, in such case all buffers that
215 * have been already given by the @buf_queue callback are
217 * @stop_streaming: called when 'streaming' state must be disabled; driver
218 * should stop any DMA transactions or wait until they
219 * finish and give back all buffers it got from buf_queue()
220 * callback; may use vb2_wait_for_all_buffers() function
221 * @buf_queue: passes buffer vb to the driver; driver may start
222 * hardware operation on this buffer; driver should give
223 * the buffer back by calling vb2_buffer_done() function;
224 * it is allways called after calling STREAMON ioctl;
225 * might be called before start_streaming callback if user
226 * pre-queued buffers before calling STREAMON
229 int (*queue_setup
)(struct vb2_queue
*q
, const struct v4l2_format
*fmt
,
230 unsigned int *num_buffers
, unsigned int *num_planes
,
231 unsigned int sizes
[], void *alloc_ctxs
[]);
233 void (*wait_prepare
)(struct vb2_queue
*q
);
234 void (*wait_finish
)(struct vb2_queue
*q
);
236 int (*buf_init
)(struct vb2_buffer
*vb
);
237 int (*buf_prepare
)(struct vb2_buffer
*vb
);
238 int (*buf_finish
)(struct vb2_buffer
*vb
);
239 void (*buf_cleanup
)(struct vb2_buffer
*vb
);
241 int (*start_streaming
)(struct vb2_queue
*q
, unsigned int count
);
242 int (*stop_streaming
)(struct vb2_queue
*q
);
244 void (*buf_queue
)(struct vb2_buffer
*vb
);
248 * struct vb2_queue - a videobuf queue
250 * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
251 * @io_modes: supported io methods (see vb2_io_modes enum)
252 * @io_flags: additional io flags (see vb2_fileio_flags enum)
253 * @ops: driver-specific callbacks
254 * @mem_ops: memory allocator specific callbacks
255 * @drv_priv: driver private data
256 * @buf_struct_size: size of the driver-specific buffer structure;
257 * "0" indicates the driver doesn't want to use a custom buffer
258 * structure type, so sizeof(struct vb2_buffer) will is used
260 * @memory: current memory type used
261 * @bufs: videobuf buffer structures
262 * @num_buffers: number of allocated/used buffers
263 * @queued_list: list of buffers currently queued from userspace
264 * @queued_count: number of buffers owned by the driver
265 * @done_list: list of buffers ready to be dequeued to userspace
266 * @done_lock: lock to protect done_list list
267 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
268 * @alloc_ctx: memory type/allocator-specific contexts for each plane
269 * @streaming: current streaming state
270 * @fileio: file io emulator internal data, used only if emulator is active
273 enum v4l2_buf_type type
;
274 unsigned int io_modes
;
275 unsigned int io_flags
;
277 const struct vb2_ops
*ops
;
278 const struct vb2_mem_ops
*mem_ops
;
280 unsigned int buf_struct_size
;
282 /* private: internal use only */
283 enum v4l2_memory memory
;
284 struct vb2_buffer
*bufs
[VIDEO_MAX_FRAME
];
285 unsigned int num_buffers
;
287 struct list_head queued_list
;
289 atomic_t queued_count
;
290 struct list_head done_list
;
291 spinlock_t done_lock
;
292 wait_queue_head_t done_wq
;
294 void *alloc_ctx
[VIDEO_MAX_PLANES
];
295 unsigned int plane_sizes
[VIDEO_MAX_PLANES
];
297 unsigned int streaming
:1;
299 struct vb2_fileio_data
*fileio
;
302 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
);
303 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
);
305 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
);
306 int vb2_wait_for_all_buffers(struct vb2_queue
*q
);
308 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
);
309 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
);
311 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
);
312 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
);
314 int vb2_queue_init(struct vb2_queue
*q
);
316 void vb2_queue_release(struct vb2_queue
*q
);
318 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
);
319 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
);
321 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
);
322 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
);
324 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
);
326 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
330 unsigned long flags
);
332 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
);
333 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
334 loff_t
*ppos
, int nonblock
);
335 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
336 loff_t
*ppos
, int nonblock
);
339 * vb2_is_streaming() - return streaming status of the queue
342 static inline bool vb2_is_streaming(struct vb2_queue
*q
)
348 * vb2_is_busy() - return busy status of the queue
351 * This function checks if queue has any buffers allocated.
353 static inline bool vb2_is_busy(struct vb2_queue
*q
)
355 return (q
->num_buffers
> 0);
359 * vb2_get_drv_priv() - return driver private data associated with the queue
362 static inline void *vb2_get_drv_priv(struct vb2_queue
*q
)
368 * vb2_set_plane_payload() - set bytesused for the plane plane_no
369 * @vb: buffer for which plane payload should be set
370 * @plane_no: plane number for which payload should be set
371 * @size: payload in bytes
373 static inline void vb2_set_plane_payload(struct vb2_buffer
*vb
,
374 unsigned int plane_no
, unsigned long size
)
376 if (plane_no
< vb
->num_planes
)
377 vb
->v4l2_planes
[plane_no
].bytesused
= size
;
381 * vb2_get_plane_payload() - get bytesused for the plane plane_no
382 * @vb: buffer for which plane payload should be set
383 * @plane_no: plane number for which payload should be set
384 * @size: payload in bytes
386 static inline unsigned long vb2_get_plane_payload(struct vb2_buffer
*vb
,
387 unsigned int plane_no
)
389 if (plane_no
< vb
->num_planes
)
390 return vb
->v4l2_planes
[plane_no
].bytesused
;
395 * vb2_plane_size() - return plane size in bytes
396 * @vb: buffer for which plane size should be returned
397 * @plane_no: plane number for which size should be returned
399 static inline unsigned long
400 vb2_plane_size(struct vb2_buffer
*vb
, unsigned int plane_no
)
402 if (plane_no
< vb
->num_planes
)
403 return vb
->v4l2_planes
[plane_no
].length
;
407 #endif /* _MEDIA_VIDEOBUF2_CORE_H */