[media] v4l: videobuf2: add DMA scatter/gather allocator
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / media / videobuf2-core.h
CommitLineData
e23ccc0a
PO
1/*
2 * videobuf2-core.h - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Pawel Osciak <p.osciak@samsung.com>
7 *
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;
21
22/**
23 * struct vb2_mem_ops - memory handling/memory allocator operations
24 * @alloc: allocate video memory and, optionally, allocator private data,
25 * return NULL on failure or a pointer to allocator private,
26 * per-buffer data on success; the returned private structure
27 * will then be passed as buf_priv argument to other ops in this
28 * structure
29 * @put: inform the allocator that the buffer will no longer be used;
30 * usually will result in the allocator freeing the buffer (if
31 * no other users of this buffer are present); the buf_priv
32 * argument is the allocator private per-buffer structure
33 * previously returned from the alloc callback
34 * @get_userptr: acquire userspace memory for a hardware operation; used for
35 * USERPTR memory types; vaddr is the address passed to the
36 * videobuf layer when queuing a video buffer of USERPTR type;
37 * should return an allocator private per-buffer structure
38 * associated with the buffer on success, NULL on failure;
39 * the returned private structure will then be passed as buf_priv
40 * argument to other ops in this structure
41 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
42 * be used
43 * @vaddr: return a kernel virtual address to a given memory buffer
44 * associated with the passed private structure or NULL if no
45 * such mapping exists
46 * @cookie: return allocator specific cookie for a given memory buffer
47 * associated with the passed private structure or NULL if not
48 * available
49 * @num_users: return the current number of users of a memory buffer;
50 * return 1 if the videobuf layer (or actually the driver using
51 * it) is the only user
52 * @mmap: setup a userspace mapping for a given memory buffer under
53 * the provided virtual memory region
54 *
55 * Required ops for USERPTR types: get_userptr, put_userptr.
56 * Required ops for MMAP types: alloc, put, num_users, mmap.
57 */
58struct vb2_mem_ops {
59 void *(*alloc)(void *alloc_ctx, unsigned long size);
60 void (*put)(void *buf_priv);
61
62 void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
63 unsigned long size, int write);
64 void (*put_userptr)(void *buf_priv);
65
66 void *(*vaddr)(void *buf_priv);
67 void *(*cookie)(void *buf_priv);
68
69 unsigned int (*num_users)(void *buf_priv);
70
71 int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
72};
73
74struct vb2_plane {
75 void *mem_priv;
76 int mapped:1;
77};
78
79/**
80 * enum vb2_io_modes - queue access methods
81 * @VB2_MMAP: driver supports MMAP with streaming API
82 * @VB2_USERPTR: driver supports USERPTR with streaming API
83 * @VB2_READ: driver supports read() style access
84 * @VB2_WRITE: driver supports write() style access
85 */
86enum vb2_io_modes {
87 VB2_MMAP = (1 << 0),
88 VB2_USERPTR = (1 << 1),
89 VB2_READ = (1 << 2),
90 VB2_WRITE = (1 << 3),
91};
92
93/**
94 * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator,
95 * by default the 'streaming' style is used by the file io emulator
96 * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer
97 * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call
98 */
99enum vb2_fileio_flags {
100 VB2_FILEIO_READ_ONCE = (1 << 0),
101 VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1),
102};
103
104/**
105 * enum vb2_buffer_state - current video buffer state
106 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
107 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
108 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
109 * in a hardware operation
110 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
111 * not yet dequeued to userspace
112 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
113 * has ended with an error, which will be reported
114 * to the userspace when it is dequeued
115 */
116enum vb2_buffer_state {
117 VB2_BUF_STATE_DEQUEUED,
118 VB2_BUF_STATE_QUEUED,
119 VB2_BUF_STATE_ACTIVE,
120 VB2_BUF_STATE_DONE,
121 VB2_BUF_STATE_ERROR,
122};
123
124struct vb2_queue;
125
126/**
127 * struct vb2_buffer - represents a video buffer
128 * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
129 * be read by the driver and relevant entries can be
130 * changed by the driver in case of CAPTURE types
131 * (such as timestamp)
132 * @v4l2_planes: struct v4l2_planes associated with this buffer; can
133 * be read by the driver and relevant entries can be
134 * changed by the driver in case of CAPTURE types
135 * (such as bytesused); NOTE that even for single-planar
136 * types, the v4l2_planes[0] struct should be used
137 * instead of v4l2_buf for filling bytesused - drivers
138 * should use the vb2_set_plane_payload() function for that
139 * @vb2_queue: the queue to which this driver belongs
140 * @num_planes: number of planes in the buffer
141 * on an internal driver queue
142 * @state: current buffer state; do not change
143 * @queued_entry: entry on the queued buffers list, which holds all
144 * buffers queued from userspace
145 * @done_entry: entry on the list that stores all buffers ready to
146 * be dequeued to userspace
147 * @planes: private per-plane information; do not change
148 * @num_planes_mapped: number of mapped planes; do not change
149 */
150struct vb2_buffer {
151 struct v4l2_buffer v4l2_buf;
152 struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES];
153
154 struct vb2_queue *vb2_queue;
155
156 unsigned int num_planes;
157
158/* Private: internal use only */
159 enum vb2_buffer_state state;
160
161 struct list_head queued_entry;
162 struct list_head done_entry;
163
164 struct vb2_plane planes[VIDEO_MAX_PLANES];
165 unsigned int num_planes_mapped;
166};
167
168/**
169 * struct vb2_ops - driver-specific callbacks
170 *
171 * @queue_setup: called from a VIDIOC_REQBUFS handler, before
172 * memory allocation; driver should return the required
173 * number of buffers in num_buffers, the required number
174 * of planes per buffer in num_planes; the size of each
175 * plane should be set in the sizes[] array and optional
176 * per-plane allocator specific context in alloc_ctxs[]
177 * array
178 * @wait_prepare: release any locks taken while calling vb2 functions;
179 * it is called before an ioctl needs to wait for a new
180 * buffer to arrive; required to avoid a deadlock in
181 * blocking access type
182 * @wait_finish: reacquire all locks released in the previous callback;
183 * required to continue operation after sleeping while
184 * waiting for a new buffer to arrive
185 * @buf_init: called once after allocating a buffer (in MMAP case)
186 * or after acquiring a new USERPTR buffer; drivers may
187 * perform additional buffer-related initialization;
188 * initialization failure (return != 0) will prevent
189 * queue setup from completing successfully; optional
190 * @buf_prepare: called every time the buffer is queued from userspace;
191 * drivers may perform any initialization required before
192 * each hardware operation in this callback;
193 * if an error is returned, the buffer will not be queued
194 * in driver; optional
195 * @buf_finish: called before every dequeue of the buffer back to
196 * userspace; drivers may perform any operations required
197 * before userspace accesses the buffer; optional
198 * @buf_cleanup: called once before the buffer is freed; drivers may
199 * perform any additional cleanup; optional
200 * @start_streaming: called once before entering 'streaming' state; enables
201 * driver to receive buffers over buf_queue() callback
202 * @stop_streaming: called when 'streaming' state must be disabled; driver
203 * should stop any DMA transactions or wait until they
204 * finish and give back all buffers it got from buf_queue()
205 * callback; may use vb2_wait_for_all_buffers() function
206 * @buf_queue: passes buffer vb to the driver; driver may start
207 * hardware operation on this buffer; driver should give
208 * the buffer back by calling vb2_buffer_done() function
209 */
210struct vb2_ops {
211 int (*queue_setup)(struct vb2_queue *q, unsigned int *num_buffers,
212 unsigned int *num_planes, unsigned long sizes[],
213 void *alloc_ctxs[]);
214
215 void (*wait_prepare)(struct vb2_queue *q);
216 void (*wait_finish)(struct vb2_queue *q);
217
218 int (*buf_init)(struct vb2_buffer *vb);
219 int (*buf_prepare)(struct vb2_buffer *vb);
220 int (*buf_finish)(struct vb2_buffer *vb);
221 void (*buf_cleanup)(struct vb2_buffer *vb);
222
223 int (*start_streaming)(struct vb2_queue *q);
224 int (*stop_streaming)(struct vb2_queue *q);
225
226 void (*buf_queue)(struct vb2_buffer *vb);
227};
228
229/**
230 * struct vb2_queue - a videobuf queue
231 *
232 * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
233 * @io_modes: supported io methods (see vb2_io_modes enum)
234 * @io_flags: additional io flags (see vb2_fileio_flags enum)
235 * @ops: driver-specific callbacks
236 * @mem_ops: memory allocator specific callbacks
237 * @drv_priv: driver private data
238 * @buf_struct_size: size of the driver-specific buffer structure;
239 * "0" indicates the driver doesn't want to use a custom buffer
240 * structure type, so sizeof(struct vb2_buffer) will is used
241 *
242 * @memory: current memory type used
243 * @bufs: videobuf buffer structures
244 * @num_buffers: number of allocated/used buffers
245 * @queued_list: list of buffers currently queued from userspace
246 * @queued_count: number of buffers owned by the driver
247 * @done_list: list of buffers ready to be dequeued to userspace
248 * @done_lock: lock to protect done_list list
249 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
250 * @alloc_ctx: memory type/allocator-specific contexts for each plane
251 * @streaming: current streaming state
252 */
253struct vb2_queue {
254 enum v4l2_buf_type type;
255 unsigned int io_modes;
256 unsigned int io_flags;
257
258 const struct vb2_ops *ops;
259 const struct vb2_mem_ops *mem_ops;
260 void *drv_priv;
261 unsigned int buf_struct_size;
262
263/* private: internal use only */
264 enum v4l2_memory memory;
265 struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
266 unsigned int num_buffers;
267
268 struct list_head queued_list;
269
270 atomic_t queued_count;
271 struct list_head done_list;
272 spinlock_t done_lock;
273 wait_queue_head_t done_wq;
274
275 void *alloc_ctx[VIDEO_MAX_PLANES];
276
277 unsigned int streaming:1;
278};
279
280void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
281void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
282
283void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
284int vb2_wait_for_all_buffers(struct vb2_queue *q);
285
286int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
287int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
288
289int vb2_queue_init(struct vb2_queue *q);
290
291void vb2_queue_release(struct vb2_queue *q);
292
293int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
294int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
295
296int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
297int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
298
299int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
300unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
301
302/**
303 * vb2_is_streaming() - return streaming status of the queue
304 * @q: videobuf queue
305 */
306static inline bool vb2_is_streaming(struct vb2_queue *q)
307{
308 return q->streaming;
309}
310
311/**
312 * vb2_is_busy() - return busy status of the queue
313 * @q: videobuf queue
314 *
315 * This function checks if queue has any buffers allocated.
316 */
317static inline bool vb2_is_busy(struct vb2_queue *q)
318{
319 return (q->num_buffers > 0);
320}
321
322/**
323 * vb2_get_drv_priv() - return driver private data associated with the queue
324 * @q: videobuf queue
325 */
326static inline void *vb2_get_drv_priv(struct vb2_queue *q)
327{
328 return q->drv_priv;
329}
330
331/**
332 * vb2_set_plane_payload() - set bytesused for the plane plane_no
333 * @vb: buffer for which plane payload should be set
334 * @plane_no: plane number for which payload should be set
335 * @size: payload in bytes
336 */
337static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
338 unsigned int plane_no, unsigned long size)
339{
340 if (plane_no < vb->num_planes)
341 vb->v4l2_planes[plane_no].bytesused = size;
342}
343
344/**
345 * vb2_get_plane_payload() - set bytesused for the plane plane_no
346 * @vb: buffer for which plane payload should be set
347 * @plane_no: plane number for which payload should be set
348 * @size: payload in bytes
349 */
350static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
351 unsigned int plane_no)
352{
353 if (plane_no < vb->num_planes)
354 return vb->v4l2_planes[plane_no].bytesused;
355 return 0;
356}
357
358/**
359 * vb2_plane_size() - return plane size in bytes
360 * @vb: buffer for which plane size should be returned
361 * @plane_no: plane number for which size should be returned
362 */
363static inline unsigned long
364vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
365{
366 if (plane_no < vb->num_planes)
367 return vb->v4l2_planes[plane_no].length;
368 return 0;
369}
370
371#endif /* _MEDIA_VIDEOBUF2_CORE_H */