blob: a15d1f1b319effd43a404af98bcff323bfa798e5 [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
20struct vb2_alloc_ctx;
Marek Szyprowskib25748f2010-12-06 05:56:55 -030021struct vb2_fileio_data;
Pawel Osciake23ccc02010-10-11 10:56:41 -030022
23/**
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
29 * structure
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
43 * be used
44 * @vaddr: return a kernel virtual address to a given memory buffer
45 * associated with the passed private structure or NULL if no
46 * such mapping exists
47 * @cookie: return allocator specific cookie for a given memory buffer
48 * associated with the passed private structure or NULL if not
49 * available
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
55 *
56 * Required ops for USERPTR types: get_userptr, put_userptr.
57 * Required ops for MMAP types: alloc, put, num_users, mmap.
Marek Szyprowskib25748f2010-12-06 05:56:55 -030058 * Required ops for read/write access types: alloc, put, num_users, vaddr
Pawel Osciake23ccc02010-10-11 10:56:41 -030059 */
60struct vb2_mem_ops {
61 void *(*alloc)(void *alloc_ctx, unsigned long size);
62 void (*put)(void *buf_priv);
63
64 void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
65 unsigned long size, int write);
66 void (*put_userptr)(void *buf_priv);
67
68 void *(*vaddr)(void *buf_priv);
69 void *(*cookie)(void *buf_priv);
70
71 unsigned int (*num_users)(void *buf_priv);
72
73 int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
74};
75
76struct vb2_plane {
77 void *mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -030078};
79
80/**
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
86 */
87enum vb2_io_modes {
88 VB2_MMAP = (1 << 0),
89 VB2_USERPTR = (1 << 1),
90 VB2_READ = (1 << 2),
91 VB2_WRITE = (1 << 3),
92};
93
94/**
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
99 */
100enum vb2_fileio_flags {
101 VB2_FILEIO_READ_ONCE = (1 << 0),
102 VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1),
103};
104
105/**
106 * enum vb2_buffer_state - current video buffer state
107 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300108 * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
Pawel Osciake23ccc02010-10-11 10:56:41 -0300109 * @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
117 */
118enum vb2_buffer_state {
119 VB2_BUF_STATE_DEQUEUED,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300120 VB2_BUF_STATE_PREPARED,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300121 VB2_BUF_STATE_QUEUED,
122 VB2_BUF_STATE_ACTIVE,
123 VB2_BUF_STATE_DONE,
124 VB2_BUF_STATE_ERROR,
125};
126
127struct vb2_queue;
128
129/**
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
Pawel Osciake23ccc02010-10-11 10:56:41 -0300151 */
152struct vb2_buffer {
153 struct v4l2_buffer v4l2_buf;
154 struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES];
155
156 struct vb2_queue *vb2_queue;
157
158 unsigned int num_planes;
159
160/* Private: internal use only */
161 enum vb2_buffer_state state;
162
163 struct list_head queued_entry;
164 struct list_head done_entry;
165
166 struct vb2_plane planes[VIDEO_MAX_PLANES];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167};
168
169/**
170 * struct vb2_ops - driver-specific callbacks
171 *
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300172 * @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.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300187 * @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
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300199 * @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
Pawel Osciake23ccc02010-10-11 10:56:41 -0300204 * @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
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300209 * @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
216 * invalidated.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300217 * @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
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300223 * 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
Pawel Osciake23ccc02010-10-11 10:56:41 -0300227 */
228struct vb2_ops {
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300229 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[]);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300232
233 void (*wait_prepare)(struct vb2_queue *q);
234 void (*wait_finish)(struct vb2_queue *q);
235
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);
240
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300241 int (*start_streaming)(struct vb2_queue *q, unsigned int count);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 int (*stop_streaming)(struct vb2_queue *q);
243
244 void (*buf_queue)(struct vb2_buffer *vb);
245};
246
247/**
248 * struct vb2_queue - a videobuf queue
249 *
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
259 *
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
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300270 * @fileio: file io emulator internal data, used only if emulator is active
Pawel Osciake23ccc02010-10-11 10:56:41 -0300271 */
272struct vb2_queue {
273 enum v4l2_buf_type type;
274 unsigned int io_modes;
275 unsigned int io_flags;
276
277 const struct vb2_ops *ops;
278 const struct vb2_mem_ops *mem_ops;
279 void *drv_priv;
280 unsigned int buf_struct_size;
281
282/* private: internal use only */
283 enum v4l2_memory memory;
284 struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
285 unsigned int num_buffers;
286
287 struct list_head queued_list;
288
289 atomic_t queued_count;
290 struct list_head done_list;
291 spinlock_t done_lock;
292 wait_queue_head_t done_wq;
293
294 void *alloc_ctx[VIDEO_MAX_PLANES];
Marek Szyprowski035aa142011-08-24 06:43:36 -0300295 unsigned int plane_sizes[VIDEO_MAX_PLANES];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300296
297 unsigned int streaming:1;
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300298
299 struct vb2_fileio_data *fileio;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300300};
301
302void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
303void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
304
305void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
306int vb2_wait_for_all_buffers(struct vb2_queue *q);
307
308int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
309int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
310
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300311int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
312int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
313
Pawel Osciake23ccc02010-10-11 10:56:41 -0300314int vb2_queue_init(struct vb2_queue *q);
315
316void vb2_queue_release(struct vb2_queue *q);
317
318int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
319int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
320
321int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
322int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
323
324int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
Scott Jiang6f524ec2011-09-21 09:25:23 -0300325#ifndef CONFIG_MMU
326unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
327 unsigned long addr,
328 unsigned long len,
329 unsigned long pgoff,
330 unsigned long flags);
331#endif
Pawel Osciake23ccc02010-10-11 10:56:41 -0300332unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300333size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
334 loff_t *ppos, int nonblock);
335size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
336 loff_t *ppos, int nonblock);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300337
338/**
339 * vb2_is_streaming() - return streaming status of the queue
340 * @q: videobuf queue
341 */
342static inline bool vb2_is_streaming(struct vb2_queue *q)
343{
344 return q->streaming;
345}
346
347/**
348 * vb2_is_busy() - return busy status of the queue
349 * @q: videobuf queue
350 *
351 * This function checks if queue has any buffers allocated.
352 */
353static inline bool vb2_is_busy(struct vb2_queue *q)
354{
355 return (q->num_buffers > 0);
356}
357
358/**
359 * vb2_get_drv_priv() - return driver private data associated with the queue
360 * @q: videobuf queue
361 */
362static inline void *vb2_get_drv_priv(struct vb2_queue *q)
363{
364 return q->drv_priv;
365}
366
367/**
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
372 */
373static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
374 unsigned int plane_no, unsigned long size)
375{
376 if (plane_no < vb->num_planes)
377 vb->v4l2_planes[plane_no].bytesused = size;
378}
379
380/**
Laurent Pinchart9f00eda2011-02-27 14:38:19 -0300381 * vb2_get_plane_payload() - get bytesused for the plane plane_no
Pawel Osciake23ccc02010-10-11 10:56:41 -0300382 * @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
385 */
386static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
387 unsigned int plane_no)
388{
389 if (plane_no < vb->num_planes)
390 return vb->v4l2_planes[plane_no].bytesused;
391 return 0;
392}
393
394/**
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
398 */
399static inline unsigned long
400vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
401{
402 if (plane_no < vb->num_planes)
403 return vb->v4l2_planes[plane_no].length;
404 return 0;
405}
406
407#endif /* _MEDIA_VIDEOBUF2_CORE_H */