blob: b0b6b04f61d9445ffa000bd1e6096d9b09cbedbc [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.h - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 */
12#ifndef _MEDIA_VIDEOBUF2_CORE_H
13#define _MEDIA_VIDEOBUF2_CORE_H
14
15#include <linux/mm_types.h>
16#include <linux/mutex.h>
17#include <linux/poll.h>
18#include <linux/videodev2.h>
19
Balamurugan Alagarsamy0a5b6ee2014-08-14 16:33:19 +053020#define VB2_MAX_FRAME 64
21
Pawel Osciake23ccc02010-10-11 10:56:41 -030022struct vb2_alloc_ctx;
Marek Szyprowskib25748f2010-12-06 05:56:55 -030023struct vb2_fileio_data;
Pawel Osciake23ccc02010-10-11 10:56:41 -030024
25/**
26 * struct vb2_mem_ops - memory handling/memory allocator operations
27 * @alloc: allocate video memory and, optionally, allocator private data,
28 * return NULL on failure or a pointer to allocator private,
29 * per-buffer data on success; the returned private structure
30 * will then be passed as buf_priv argument to other ops in this
31 * structure
32 * @put: inform the allocator that the buffer will no longer be used;
33 * usually will result in the allocator freeing the buffer (if
34 * no other users of this buffer are present); the buf_priv
35 * argument is the allocator private per-buffer structure
36 * previously returned from the alloc callback
37 * @get_userptr: acquire userspace memory for a hardware operation; used for
38 * USERPTR memory types; vaddr is the address passed to the
39 * videobuf layer when queuing a video buffer of USERPTR type;
40 * should return an allocator private per-buffer structure
41 * associated with the buffer on success, NULL on failure;
42 * the returned private structure will then be passed as buf_priv
43 * argument to other ops in this structure
44 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
45 * be used
46 * @vaddr: return a kernel virtual address to a given memory buffer
47 * associated with the passed private structure or NULL if no
48 * such mapping exists
49 * @cookie: return allocator specific cookie for a given memory buffer
50 * associated with the passed private structure or NULL if not
51 * available
52 * @num_users: return the current number of users of a memory buffer;
53 * return 1 if the videobuf layer (or actually the driver using
54 * it) is the only user
55 * @mmap: setup a userspace mapping for a given memory buffer under
56 * the provided virtual memory region
57 *
58 * Required ops for USERPTR types: get_userptr, put_userptr.
59 * Required ops for MMAP types: alloc, put, num_users, mmap.
Marek Szyprowskib25748f2010-12-06 05:56:55 -030060 * Required ops for read/write access types: alloc, put, num_users, vaddr
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 */
62struct vb2_mem_ops {
63 void *(*alloc)(void *alloc_ctx, unsigned long size);
64 void (*put)(void *buf_priv);
65
66 void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
67 unsigned long size, int write);
68 void (*put_userptr)(void *buf_priv);
69
70 void *(*vaddr)(void *buf_priv);
71 void *(*cookie)(void *buf_priv);
72
73 unsigned int (*num_users)(void *buf_priv);
74
75 int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
76};
77
78struct vb2_plane {
79 void *mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -030080};
81
82/**
83 * enum vb2_io_modes - queue access methods
84 * @VB2_MMAP: driver supports MMAP with streaming API
85 * @VB2_USERPTR: driver supports USERPTR with streaming API
86 * @VB2_READ: driver supports read() style access
87 * @VB2_WRITE: driver supports write() style access
88 */
89enum vb2_io_modes {
90 VB2_MMAP = (1 << 0),
91 VB2_USERPTR = (1 << 1),
92 VB2_READ = (1 << 2),
93 VB2_WRITE = (1 << 3),
94};
95
96/**
97 * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator,
98 * by default the 'streaming' style is used by the file io emulator
99 * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer
100 * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call
101 */
102enum vb2_fileio_flags {
103 VB2_FILEIO_READ_ONCE = (1 << 0),
104 VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1),
105};
106
107/**
108 * enum vb2_buffer_state - current video buffer state
109 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300110 * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
Pawel Osciake23ccc02010-10-11 10:56:41 -0300111 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
112 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
113 * in a hardware operation
114 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
115 * not yet dequeued to userspace
116 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
117 * has ended with an error, which will be reported
118 * to the userspace when it is dequeued
119 */
120enum vb2_buffer_state {
121 VB2_BUF_STATE_DEQUEUED,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300122 VB2_BUF_STATE_PREPARED,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300123 VB2_BUF_STATE_QUEUED,
124 VB2_BUF_STATE_ACTIVE,
125 VB2_BUF_STATE_DONE,
126 VB2_BUF_STATE_ERROR,
127};
128
129struct vb2_queue;
130
131/**
132 * struct vb2_buffer - represents a video buffer
133 * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
134 * be read by the driver and relevant entries can be
135 * changed by the driver in case of CAPTURE types
136 * (such as timestamp)
137 * @v4l2_planes: struct v4l2_planes associated with this buffer; can
138 * be read by the driver and relevant entries can be
139 * changed by the driver in case of CAPTURE types
140 * (such as bytesused); NOTE that even for single-planar
141 * types, the v4l2_planes[0] struct should be used
142 * instead of v4l2_buf for filling bytesused - drivers
143 * should use the vb2_set_plane_payload() function for that
144 * @vb2_queue: the queue to which this driver belongs
145 * @num_planes: number of planes in the buffer
146 * on an internal driver queue
147 * @state: current buffer state; do not change
148 * @queued_entry: entry on the queued buffers list, which holds all
149 * buffers queued from userspace
150 * @done_entry: entry on the list that stores all buffers ready to
151 * be dequeued to userspace
152 * @planes: private per-plane information; do not change
Pawel Osciake23ccc02010-10-11 10:56:41 -0300153 */
154struct vb2_buffer {
155 struct v4l2_buffer v4l2_buf;
156 struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES];
157
158 struct vb2_queue *vb2_queue;
159
160 unsigned int num_planes;
161
162/* Private: internal use only */
163 enum vb2_buffer_state state;
164
165 struct list_head queued_entry;
166 struct list_head done_entry;
167
168 struct vb2_plane planes[VIDEO_MAX_PLANES];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300169};
170
171/**
172 * struct vb2_ops - driver-specific callbacks
173 *
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300174 * @queue_setup: called from VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS
175 * handlers before memory allocation, or, if
176 * *num_planes != 0, after the allocation to verify a
177 * smaller number of buffers. Driver should return
178 * the required number of buffers in *num_buffers, the
179 * required number of planes per buffer in *num_planes; the
180 * size of each plane should be set in the sizes[] array
181 * and optional per-plane allocator specific context in the
182 * alloc_ctxs[] array. When called from VIDIOC_REQBUFS,
183 * fmt == NULL, the driver has to use the currently
184 * configured format and *num_buffers is the total number
185 * of buffers, that are being allocated. When called from
186 * VIDIOC_CREATE_BUFS, fmt != NULL and it describes the
187 * target frame format. In this case *num_buffers are being
188 * allocated additionally to q->num_buffers.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300189 * @wait_prepare: release any locks taken while calling vb2 functions;
190 * it is called before an ioctl needs to wait for a new
191 * buffer to arrive; required to avoid a deadlock in
192 * blocking access type
193 * @wait_finish: reacquire all locks released in the previous callback;
194 * required to continue operation after sleeping while
195 * waiting for a new buffer to arrive
196 * @buf_init: called once after allocating a buffer (in MMAP case)
197 * or after acquiring a new USERPTR buffer; drivers may
198 * perform additional buffer-related initialization;
199 * initialization failure (return != 0) will prevent
200 * queue setup from completing successfully; optional
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300201 * @buf_prepare: called every time the buffer is queued from userspace
202 * and from the VIDIOC_PREPARE_BUF ioctl; drivers may
203 * perform any initialization required before each hardware
204 * operation in this callback; if an error is returned, the
205 * buffer will not be queued in driver; optional
Pawel Osciake23ccc02010-10-11 10:56:41 -0300206 * @buf_finish: called before every dequeue of the buffer back to
207 * userspace; drivers may perform any operations required
208 * before userspace accesses the buffer; optional
209 * @buf_cleanup: called once before the buffer is freed; drivers may
210 * perform any additional cleanup; optional
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300211 * @start_streaming: called once to enter 'streaming' state; the driver may
212 * receive buffers with @buf_queue callback before
213 * @start_streaming is called; the driver gets the number
214 * of already queued buffers in count parameter; driver
215 * can return an error if hardware fails or not enough
216 * buffers has been queued, in such case all buffers that
217 * have been already given by the @buf_queue callback are
218 * invalidated.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300219 * @stop_streaming: called when 'streaming' state must be disabled; driver
220 * should stop any DMA transactions or wait until they
221 * finish and give back all buffers it got from buf_queue()
222 * callback; may use vb2_wait_for_all_buffers() function
223 * @buf_queue: passes buffer vb to the driver; driver may start
224 * hardware operation on this buffer; driver should give
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300225 * the buffer back by calling vb2_buffer_done() function;
226 * it is allways called after calling STREAMON ioctl;
227 * might be called before start_streaming callback if user
228 * pre-queued buffers before calling STREAMON
Pawel Osciake23ccc02010-10-11 10:56:41 -0300229 */
230struct vb2_ops {
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300231 int (*queue_setup)(struct vb2_queue *q, const struct v4l2_format *fmt,
232 unsigned int *num_buffers, unsigned int *num_planes,
233 unsigned int sizes[], void *alloc_ctxs[]);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300234
235 void (*wait_prepare)(struct vb2_queue *q);
236 void (*wait_finish)(struct vb2_queue *q);
237
238 int (*buf_init)(struct vb2_buffer *vb);
239 int (*buf_prepare)(struct vb2_buffer *vb);
240 int (*buf_finish)(struct vb2_buffer *vb);
241 void (*buf_cleanup)(struct vb2_buffer *vb);
242
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300243 int (*start_streaming)(struct vb2_queue *q, unsigned int count);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300244 int (*stop_streaming)(struct vb2_queue *q);
245
246 void (*buf_queue)(struct vb2_buffer *vb);
247};
248
249/**
250 * struct vb2_queue - a videobuf queue
251 *
252 * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
253 * @io_modes: supported io methods (see vb2_io_modes enum)
254 * @io_flags: additional io flags (see vb2_fileio_flags enum)
255 * @ops: driver-specific callbacks
256 * @mem_ops: memory allocator specific callbacks
257 * @drv_priv: driver private data
258 * @buf_struct_size: size of the driver-specific buffer structure;
259 * "0" indicates the driver doesn't want to use a custom buffer
260 * structure type, so sizeof(struct vb2_buffer) will is used
261 *
262 * @memory: current memory type used
263 * @bufs: videobuf buffer structures
264 * @num_buffers: number of allocated/used buffers
265 * @queued_list: list of buffers currently queued from userspace
266 * @queued_count: number of buffers owned by the driver
267 * @done_list: list of buffers ready to be dequeued to userspace
268 * @done_lock: lock to protect done_list list
269 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
270 * @alloc_ctx: memory type/allocator-specific contexts for each plane
271 * @streaming: current streaming state
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300272 * @fileio: file io emulator internal data, used only if emulator is active
Pawel Osciake23ccc02010-10-11 10:56:41 -0300273 */
274struct vb2_queue {
275 enum v4l2_buf_type type;
276 unsigned int io_modes;
277 unsigned int io_flags;
278
279 const struct vb2_ops *ops;
280 const struct vb2_mem_ops *mem_ops;
281 void *drv_priv;
282 unsigned int buf_struct_size;
283
284/* private: internal use only */
285 enum v4l2_memory memory;
Balamurugan Alagarsamy0a5b6ee2014-08-14 16:33:19 +0530286 struct vb2_buffer *bufs[VB2_MAX_FRAME];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300287 unsigned int num_buffers;
288
289 struct list_head queued_list;
290
291 atomic_t queued_count;
292 struct list_head done_list;
293 spinlock_t done_lock;
294 wait_queue_head_t done_wq;
295
296 void *alloc_ctx[VIDEO_MAX_PLANES];
Marek Szyprowski035aa142011-08-24 06:43:36 -0300297 unsigned int plane_sizes[VIDEO_MAX_PLANES];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300298
299 unsigned int streaming:1;
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300300
301 struct vb2_fileio_data *fileio;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300302};
303
304void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
305void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
306
307void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
308int vb2_wait_for_all_buffers(struct vb2_queue *q);
309
310int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
311int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
312
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300313int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
314int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
315
Pawel Osciake23ccc02010-10-11 10:56:41 -0300316int vb2_queue_init(struct vb2_queue *q);
317
318void vb2_queue_release(struct vb2_queue *q);
319
320int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
321int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
322
323int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
324int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
325
326int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
Scott Jiang6f524ec2011-09-21 09:25:23 -0300327#ifndef CONFIG_MMU
328unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
329 unsigned long addr,
330 unsigned long len,
331 unsigned long pgoff,
332 unsigned long flags);
333#endif
Pawel Osciake23ccc02010-10-11 10:56:41 -0300334unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300335size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
336 loff_t *ppos, int nonblock);
337size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
338 loff_t *ppos, int nonblock);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300339
340/**
341 * vb2_is_streaming() - return streaming status of the queue
342 * @q: videobuf queue
343 */
344static inline bool vb2_is_streaming(struct vb2_queue *q)
345{
346 return q->streaming;
347}
348
349/**
350 * vb2_is_busy() - return busy status of the queue
351 * @q: videobuf queue
352 *
353 * This function checks if queue has any buffers allocated.
354 */
355static inline bool vb2_is_busy(struct vb2_queue *q)
356{
357 return (q->num_buffers > 0);
358}
359
360/**
361 * vb2_get_drv_priv() - return driver private data associated with the queue
362 * @q: videobuf queue
363 */
364static inline void *vb2_get_drv_priv(struct vb2_queue *q)
365{
366 return q->drv_priv;
367}
368
369/**
370 * vb2_set_plane_payload() - set bytesused for the plane plane_no
371 * @vb: buffer for which plane payload should be set
372 * @plane_no: plane number for which payload should be set
373 * @size: payload in bytes
374 */
375static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
376 unsigned int plane_no, unsigned long size)
377{
378 if (plane_no < vb->num_planes)
379 vb->v4l2_planes[plane_no].bytesused = size;
380}
381
382/**
Laurent Pinchart9f00eda2011-02-27 14:38:19 -0300383 * vb2_get_plane_payload() - get bytesused for the plane plane_no
Pawel Osciake23ccc02010-10-11 10:56:41 -0300384 * @vb: buffer for which plane payload should be set
385 * @plane_no: plane number for which payload should be set
386 * @size: payload in bytes
387 */
388static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
389 unsigned int plane_no)
390{
391 if (plane_no < vb->num_planes)
392 return vb->v4l2_planes[plane_no].bytesused;
393 return 0;
394}
395
396/**
397 * vb2_plane_size() - return plane size in bytes
398 * @vb: buffer for which plane size should be returned
399 * @plane_no: plane number for which size should be returned
400 */
401static inline unsigned long
402vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
403{
404 if (plane_no < vb->num_planes)
405 return vb->v4l2_planes[plane_no].length;
406 return 0;
407}
408
409#endif /* _MEDIA_VIDEOBUF2_CORE_H */