[media] vivi: Add return code check at vb2_queue_init()
[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
ebc087d0 108 * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
e23ccc0a
PO
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
117 */
118enum vb2_buffer_state {
119 VB2_BUF_STATE_DEQUEUED,
ebc087d0 120 VB2_BUF_STATE_PREPARED,
e23ccc0a
PO
121 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
e23ccc0a
PO
151 */
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];
e23ccc0a
PO
167};
168
169/**
170 * struct vb2_ops - driver-specific callbacks
171 *
2d86401c
GL
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.
e23ccc0a
PO
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
2d86401c
GL
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
e23ccc0a
PO
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
bd323e28
MS
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
216 * invalidated.
e23ccc0a
PO
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
bd323e28
MS
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
e23ccc0a
PO
227 */
228struct vb2_ops {
fc714e70
GL
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[]);
e23ccc0a
PO
232
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
bd323e28 241 int (*start_streaming)(struct vb2_queue *q, unsigned int count);
e23ccc0a
PO
242 int (*stop_streaming)(struct vb2_queue *q);
243
244 void (*buf_queue)(struct vb2_buffer *vb);
245};
246
5a5adf6b
HV
247struct v4l2_fh;
248
e23ccc0a
PO
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)
5a5adf6b
HV
255 * @lock: pointer to a mutex that protects the vb2_queue struct. The
256 * driver can set this to a mutex to let the v4l2 core serialize
257 * the queuing ioctls. If the driver wants to handle locking
258 * itself, then this should be set to NULL. This lock is not used
259 * by the videobuf2 core API.
260 * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
261 * that called reqbufs, create_buffers or started fileio.
262 * This field is not used by the videobuf2 core API, but it allows
263 * drivers to easily associate an owner filehandle with the queue.
e23ccc0a
PO
264 * @ops: driver-specific callbacks
265 * @mem_ops: memory allocator specific callbacks
266 * @drv_priv: driver private data
267 * @buf_struct_size: size of the driver-specific buffer structure;
268 * "0" indicates the driver doesn't want to use a custom buffer
269 * structure type, so sizeof(struct vb2_buffer) will is used
270 *
271 * @memory: current memory type used
272 * @bufs: videobuf buffer structures
273 * @num_buffers: number of allocated/used buffers
274 * @queued_list: list of buffers currently queued from userspace
275 * @queued_count: number of buffers owned by the driver
276 * @done_list: list of buffers ready to be dequeued to userspace
277 * @done_lock: lock to protect done_list list
278 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
279 * @alloc_ctx: memory type/allocator-specific contexts for each plane
280 * @streaming: current streaming state
b25748fe 281 * @fileio: file io emulator internal data, used only if emulator is active
e23ccc0a
PO
282 */
283struct vb2_queue {
284 enum v4l2_buf_type type;
285 unsigned int io_modes;
286 unsigned int io_flags;
5a5adf6b
HV
287 struct mutex *lock;
288 struct v4l2_fh *owner;
e23ccc0a
PO
289
290 const struct vb2_ops *ops;
291 const struct vb2_mem_ops *mem_ops;
292 void *drv_priv;
293 unsigned int buf_struct_size;
294
295/* private: internal use only */
296 enum v4l2_memory memory;
297 struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
298 unsigned int num_buffers;
299
300 struct list_head queued_list;
301
302 atomic_t queued_count;
303 struct list_head done_list;
304 spinlock_t done_lock;
305 wait_queue_head_t done_wq;
306
307 void *alloc_ctx[VIDEO_MAX_PLANES];
035aa147 308 unsigned int plane_sizes[VIDEO_MAX_PLANES];
e23ccc0a
PO
309
310 unsigned int streaming:1;
b25748fe
MS
311
312 struct vb2_fileio_data *fileio;
e23ccc0a
PO
313};
314
315void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
316void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
317
318void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
319int vb2_wait_for_all_buffers(struct vb2_queue *q);
320
321int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
322int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
323
2d86401c
GL
324int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
325int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
326
e23ccc0a
PO
327int vb2_queue_init(struct vb2_queue *q);
328
329void vb2_queue_release(struct vb2_queue *q);
330
331int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
332int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
333
334int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
335int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
336
337int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
6f524ec1
SJ
338#ifndef CONFIG_MMU
339unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
340 unsigned long addr,
341 unsigned long len,
342 unsigned long pgoff,
343 unsigned long flags);
344#endif
e23ccc0a 345unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
b25748fe
MS
346size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
347 loff_t *ppos, int nonblock);
348size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
349 loff_t *ppos, int nonblock);
e23ccc0a
PO
350
351/**
352 * vb2_is_streaming() - return streaming status of the queue
353 * @q: videobuf queue
354 */
355static inline bool vb2_is_streaming(struct vb2_queue *q)
356{
357 return q->streaming;
358}
359
360/**
361 * vb2_is_busy() - return busy status of the queue
362 * @q: videobuf queue
363 *
364 * This function checks if queue has any buffers allocated.
365 */
366static inline bool vb2_is_busy(struct vb2_queue *q)
367{
368 return (q->num_buffers > 0);
369}
370
371/**
372 * vb2_get_drv_priv() - return driver private data associated with the queue
373 * @q: videobuf queue
374 */
375static inline void *vb2_get_drv_priv(struct vb2_queue *q)
376{
377 return q->drv_priv;
378}
379
380/**
381 * vb2_set_plane_payload() - set 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
385 */
386static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
387 unsigned int plane_no, unsigned long size)
388{
389 if (plane_no < vb->num_planes)
390 vb->v4l2_planes[plane_no].bytesused = size;
391}
392
393/**
9f00edae 394 * vb2_get_plane_payload() - get bytesused for the plane plane_no
e23ccc0a
PO
395 * @vb: buffer for which plane payload should be set
396 * @plane_no: plane number for which payload should be set
397 * @size: payload in bytes
398 */
399static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
400 unsigned int plane_no)
401{
402 if (plane_no < vb->num_planes)
403 return vb->v4l2_planes[plane_no].bytesused;
404 return 0;
405}
406
407/**
408 * vb2_plane_size() - return plane size in bytes
409 * @vb: buffer for which plane size should be returned
410 * @plane_no: plane number for which size should be returned
411 */
412static inline unsigned long
413vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
414{
415 if (plane_no < vb->num_planes)
416 return vb->v4l2_planes[plane_no].length;
417 return 0;
418}
419
4c1ffcaa
HV
420/*
421 * The following functions are not part of the vb2 core API, but are simple
422 * helper functions that you can use in your struct v4l2_file_operations,
423 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
424 * or video_device->lock is set, and they will set and test vb2_queue->owner
425 * to check if the calling filehandle is permitted to do the queuing operation.
426 */
427
428/* struct v4l2_ioctl_ops helpers */
429
430int vb2_ioctl_reqbufs(struct file *file, void *priv,
431 struct v4l2_requestbuffers *p);
432int vb2_ioctl_create_bufs(struct file *file, void *priv,
433 struct v4l2_create_buffers *p);
434int vb2_ioctl_prepare_buf(struct file *file, void *priv,
435 struct v4l2_buffer *p);
436int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
437int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
438int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
439int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
440int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
441
442/* struct v4l2_file_operations helpers */
443
444int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
445int vb2_fop_release(struct file *file);
446ssize_t vb2_fop_write(struct file *file, char __user *buf,
447 size_t count, loff_t *ppos);
448ssize_t vb2_fop_read(struct file *file, char __user *buf,
449 size_t count, loff_t *ppos);
450unsigned int vb2_fop_poll(struct file *file, poll_table *wait);
451#ifndef CONFIG_MMU
452unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
453 unsigned long len, unsigned long pgoff, unsigned long flags);
454#endif
455
456/* struct vb2_ops helpers, only use if vq->lock is non-NULL. */
457
458void vb2_ops_wait_prepare(struct vb2_queue *vq);
459void vb2_ops_wait_finish(struct vb2_queue *vq);
460
e23ccc0a 461#endif /* _MEDIA_VIDEOBUF2_CORE_H */
This page took 0.167976 seconds and 5 git commands to generate.