[media] V4L: omap1-camera: fix Oops with NULL platform data
[deliverable/linux.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 *
95072084 6 * Author: Pawel Osciak <pawel@osciak.com>
e23ccc0a
PO
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;
b25748fe 21struct vb2_fileio_data;
e23ccc0a
PO
22
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.
b25748fe 58 * Required ops for read/write access types: alloc, put, num_users, vaddr
e23ccc0a
PO
59 */
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;
e23ccc0a
PO
78};
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
108 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
109 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
110 * in a hardware operation
111 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
112 * not yet dequeued to userspace
113 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
114 * has ended with an error, which will be reported
115 * to the userspace when it is dequeued
116 */
117enum vb2_buffer_state {
118 VB2_BUF_STATE_DEQUEUED,
119 VB2_BUF_STATE_QUEUED,
120 VB2_BUF_STATE_ACTIVE,
121 VB2_BUF_STATE_DONE,
122 VB2_BUF_STATE_ERROR,
123};
124
125struct vb2_queue;
126
127/**
128 * struct vb2_buffer - represents a video buffer
129 * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
130 * be read by the driver and relevant entries can be
131 * changed by the driver in case of CAPTURE types
132 * (such as timestamp)
133 * @v4l2_planes: struct v4l2_planes 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 bytesused); NOTE that even for single-planar
137 * types, the v4l2_planes[0] struct should be used
138 * instead of v4l2_buf for filling bytesused - drivers
139 * should use the vb2_set_plane_payload() function for that
140 * @vb2_queue: the queue to which this driver belongs
141 * @num_planes: number of planes in the buffer
142 * on an internal driver queue
143 * @state: current buffer state; do not change
144 * @queued_entry: entry on the queued buffers list, which holds all
145 * buffers queued from userspace
146 * @done_entry: entry on the list that stores all buffers ready to
147 * be dequeued to userspace
148 * @planes: private per-plane information; do not change
e23ccc0a
PO
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];
e23ccc0a
PO
165};
166
167/**
168 * struct vb2_ops - driver-specific callbacks
169 *
170 * @queue_setup: called from a VIDIOC_REQBUFS handler, before
171 * memory allocation; driver should return the required
172 * number of buffers in num_buffers, the required number
173 * of planes per buffer in num_planes; the size of each
174 * plane should be set in the sizes[] array and optional
175 * per-plane allocator specific context in alloc_ctxs[]
176 * array
177 * @wait_prepare: release any locks taken while calling vb2 functions;
178 * it is called before an ioctl needs to wait for a new
179 * buffer to arrive; required to avoid a deadlock in
180 * blocking access type
181 * @wait_finish: reacquire all locks released in the previous callback;
182 * required to continue operation after sleeping while
183 * waiting for a new buffer to arrive
184 * @buf_init: called once after allocating a buffer (in MMAP case)
185 * or after acquiring a new USERPTR buffer; drivers may
186 * perform additional buffer-related initialization;
187 * initialization failure (return != 0) will prevent
188 * queue setup from completing successfully; optional
189 * @buf_prepare: called every time the buffer is queued from userspace;
190 * drivers may perform any initialization required before
191 * each hardware operation in this callback;
192 * if an error is returned, the buffer will not be queued
193 * in driver; optional
194 * @buf_finish: called before every dequeue of the buffer back to
195 * userspace; drivers may perform any operations required
196 * before userspace accesses the buffer; optional
197 * @buf_cleanup: called once before the buffer is freed; drivers may
198 * perform any additional cleanup; optional
bd323e28
MS
199 * @start_streaming: called once to enter 'streaming' state; the driver may
200 * receive buffers with @buf_queue callback before
201 * @start_streaming is called; the driver gets the number
202 * of already queued buffers in count parameter; driver
203 * can return an error if hardware fails or not enough
204 * buffers has been queued, in such case all buffers that
205 * have been already given by the @buf_queue callback are
206 * invalidated.
e23ccc0a
PO
207 * @stop_streaming: called when 'streaming' state must be disabled; driver
208 * should stop any DMA transactions or wait until they
209 * finish and give back all buffers it got from buf_queue()
210 * callback; may use vb2_wait_for_all_buffers() function
211 * @buf_queue: passes buffer vb to the driver; driver may start
212 * hardware operation on this buffer; driver should give
bd323e28
MS
213 * the buffer back by calling vb2_buffer_done() function;
214 * it is allways called after calling STREAMON ioctl;
215 * might be called before start_streaming callback if user
216 * pre-queued buffers before calling STREAMON
e23ccc0a
PO
217 */
218struct vb2_ops {
219 int (*queue_setup)(struct vb2_queue *q, unsigned int *num_buffers,
035aa147 220 unsigned int *num_planes, unsigned int sizes[],
e23ccc0a
PO
221 void *alloc_ctxs[]);
222
223 void (*wait_prepare)(struct vb2_queue *q);
224 void (*wait_finish)(struct vb2_queue *q);
225
226 int (*buf_init)(struct vb2_buffer *vb);
227 int (*buf_prepare)(struct vb2_buffer *vb);
228 int (*buf_finish)(struct vb2_buffer *vb);
229 void (*buf_cleanup)(struct vb2_buffer *vb);
230
bd323e28 231 int (*start_streaming)(struct vb2_queue *q, unsigned int count);
e23ccc0a
PO
232 int (*stop_streaming)(struct vb2_queue *q);
233
234 void (*buf_queue)(struct vb2_buffer *vb);
235};
236
237/**
238 * struct vb2_queue - a videobuf queue
239 *
240 * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
241 * @io_modes: supported io methods (see vb2_io_modes enum)
242 * @io_flags: additional io flags (see vb2_fileio_flags enum)
243 * @ops: driver-specific callbacks
244 * @mem_ops: memory allocator specific callbacks
245 * @drv_priv: driver private data
246 * @buf_struct_size: size of the driver-specific buffer structure;
247 * "0" indicates the driver doesn't want to use a custom buffer
248 * structure type, so sizeof(struct vb2_buffer) will is used
249 *
250 * @memory: current memory type used
251 * @bufs: videobuf buffer structures
252 * @num_buffers: number of allocated/used buffers
253 * @queued_list: list of buffers currently queued from userspace
254 * @queued_count: number of buffers owned by the driver
255 * @done_list: list of buffers ready to be dequeued to userspace
256 * @done_lock: lock to protect done_list list
257 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
258 * @alloc_ctx: memory type/allocator-specific contexts for each plane
259 * @streaming: current streaming state
b25748fe 260 * @fileio: file io emulator internal data, used only if emulator is active
e23ccc0a
PO
261 */
262struct vb2_queue {
263 enum v4l2_buf_type type;
264 unsigned int io_modes;
265 unsigned int io_flags;
266
267 const struct vb2_ops *ops;
268 const struct vb2_mem_ops *mem_ops;
269 void *drv_priv;
270 unsigned int buf_struct_size;
271
272/* private: internal use only */
273 enum v4l2_memory memory;
274 struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
275 unsigned int num_buffers;
276
277 struct list_head queued_list;
278
279 atomic_t queued_count;
280 struct list_head done_list;
281 spinlock_t done_lock;
282 wait_queue_head_t done_wq;
283
284 void *alloc_ctx[VIDEO_MAX_PLANES];
035aa147 285 unsigned int plane_sizes[VIDEO_MAX_PLANES];
e23ccc0a
PO
286
287 unsigned int streaming:1;
b25748fe
MS
288
289 struct vb2_fileio_data *fileio;
e23ccc0a
PO
290};
291
292void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
293void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
294
295void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
296int vb2_wait_for_all_buffers(struct vb2_queue *q);
297
298int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
299int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
300
301int vb2_queue_init(struct vb2_queue *q);
302
303void vb2_queue_release(struct vb2_queue *q);
304
305int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
306int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
307
308int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
309int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
310
311int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
312unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
b25748fe
MS
313size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
314 loff_t *ppos, int nonblock);
315size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
316 loff_t *ppos, int nonblock);
e23ccc0a
PO
317
318/**
319 * vb2_is_streaming() - return streaming status of the queue
320 * @q: videobuf queue
321 */
322static inline bool vb2_is_streaming(struct vb2_queue *q)
323{
324 return q->streaming;
325}
326
327/**
328 * vb2_is_busy() - return busy status of the queue
329 * @q: videobuf queue
330 *
331 * This function checks if queue has any buffers allocated.
332 */
333static inline bool vb2_is_busy(struct vb2_queue *q)
334{
335 return (q->num_buffers > 0);
336}
337
338/**
339 * vb2_get_drv_priv() - return driver private data associated with the queue
340 * @q: videobuf queue
341 */
342static inline void *vb2_get_drv_priv(struct vb2_queue *q)
343{
344 return q->drv_priv;
345}
346
347/**
348 * vb2_set_plane_payload() - set bytesused for the plane plane_no
349 * @vb: buffer for which plane payload should be set
350 * @plane_no: plane number for which payload should be set
351 * @size: payload in bytes
352 */
353static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
354 unsigned int plane_no, unsigned long size)
355{
356 if (plane_no < vb->num_planes)
357 vb->v4l2_planes[plane_no].bytesused = size;
358}
359
360/**
9f00edae 361 * vb2_get_plane_payload() - get bytesused for the plane plane_no
e23ccc0a
PO
362 * @vb: buffer for which plane payload should be set
363 * @plane_no: plane number for which payload should be set
364 * @size: payload in bytes
365 */
366static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
367 unsigned int plane_no)
368{
369 if (plane_no < vb->num_planes)
370 return vb->v4l2_planes[plane_no].bytesused;
371 return 0;
372}
373
374/**
375 * vb2_plane_size() - return plane size in bytes
376 * @vb: buffer for which plane size should be returned
377 * @plane_no: plane number for which size should be returned
378 */
379static inline unsigned long
380vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
381{
382 if (plane_no < vb->num_planes)
383 return vb->v4l2_planes[plane_no].length;
384 return 0;
385}
386
387#endif /* _MEDIA_VIDEOBUF2_CORE_H */
This page took 0.074201 seconds and 5 git commands to generate.