blob: 01b1b71fc6fdc35e4572ee8b1497e4782a389108 [file] [log] [blame]
Junghak Sungc1399902015-09-22 10:30:29 -03001/*
2 * videobuf2-v4l2.h - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Pawel Osciak <pawel@osciak.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_V4L2_H
13#define _MEDIA_VIDEOBUF2_V4L2_H
14
Junghak Sung2d700712015-09-22 10:30:30 -030015#include <linux/videodev2.h>
Junghak Sungc1399902015-09-22 10:30:29 -030016#include <media/videobuf2-core.h>
17
Junghak Sungbed04f92015-10-06 06:37:47 -030018#if VB2_MAX_FRAME != VIDEO_MAX_FRAME
19#error VB2_MAX_FRAME != VIDEO_MAX_FRAME
20#endif
21
22#if VB2_MAX_PLANES != VIDEO_MAX_PLANES
23#error VB2_MAX_PLANES != VIDEO_MAX_PLANES
24#endif
25
Junghak Sung2d700712015-09-22 10:30:30 -030026/**
27 * struct vb2_v4l2_buffer - video buffer information for v4l2
28 * @vb2_buf: video buffer 2
29 * @flags: buffer informational flags
30 * @field: enum v4l2_field; field order of the image in the buffer
Junghak Sung2d700712015-09-22 10:30:30 -030031 * @timecode: frame timecode
32 * @sequence: sequence count of this frame
33 * Should contain enough information to be able to cover all the fields
34 * of struct v4l2_buffer at videodev2.h
35 */
36struct vb2_v4l2_buffer {
37 struct vb2_buffer vb2_buf;
38
39 __u32 flags;
40 __u32 field;
Junghak Sung2d700712015-09-22 10:30:30 -030041 struct v4l2_timecode timecode;
42 __u32 sequence;
43};
44
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030045/*
Junghak Sung2d700712015-09-22 10:30:30 -030046 * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
47 */
48#define to_vb2_v4l2_buffer(vb) \
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030049 container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
Junghak Sung2d700712015-09-22 10:30:30 -030050
Junghak Sung3c5be982015-10-06 06:37:49 -030051int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030052
53/**
54 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
55 * the memory and type values.
56 * @q: videobuf2 queue
57 * @req: struct passed from userspace to vidioc_reqbufs handler
58 * in driver
59 */
Junghak Sung3c5be982015-10-06 06:37:49 -030060int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
61
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030062/**
63 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
64 * the memory and type values.
65 * @q: videobuf2 queue
66 * @create: creation parameters, passed from userspace to vidioc_create_bufs
67 * handler in driver
68 */
Junghak Sung3c5be982015-10-06 06:37:49 -030069int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030070
71/**
72 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
73 * @q: videobuf2 queue
74 * @b: buffer structure passed from userspace to vidioc_prepare_buf
75 * handler in driver
76 *
77 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
78 * This function:
79 * 1) verifies the passed buffer,
80 * 2) calls buf_prepare callback in the driver (if provided), in which
81 * driver-specific buffer initialization can be performed,
82 *
83 * The return values from this function are intended to be directly returned
84 * from vidioc_prepare_buf handler in driver.
85 */
Junghak Sung3c5be982015-10-06 06:37:49 -030086int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
87
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030088/**
89 * vb2_qbuf() - Queue a buffer from userspace
90 * @q: videobuf2 queue
91 * @b: buffer structure passed from userspace to vidioc_qbuf handler
92 * in driver
93 *
94 * Should be called from vidioc_qbuf ioctl handler of a driver.
95 * This function:
96 * 1) verifies the passed buffer,
97 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
98 * which driver-specific buffer initialization can be performed,
99 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
100 * callback for processing.
101 *
102 * The return values from this function are intended to be directly returned
103 * from vidioc_qbuf handler in driver.
104 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300105int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300106
107/**
108 * vb2_expbuf() - Export a buffer as a file descriptor
109 * @q: videobuf2 queue
110 * @eb: export buffer structure passed from userspace to vidioc_expbuf
111 * handler in driver
112 *
113 * The return values from this function are intended to be directly returned
114 * from vidioc_expbuf handler in driver.
115 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300116int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300117
118/**
119 * vb2_dqbuf() - Dequeue a buffer to the userspace
120 * @q: videobuf2 queue
121 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
122 * in driver
123 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
124 * buffers ready for dequeuing are present. Normally the driver
125 * would be passing (file->f_flags & O_NONBLOCK) here
126 *
127 * Should be called from vidioc_dqbuf ioctl handler of a driver.
128 * This function:
129 * 1) verifies the passed buffer,
130 * 2) calls buf_finish callback in the driver (if provided), in which
131 * driver can perform any additional operations that may be required before
132 * returning the buffer to userspace, such as cache sync,
133 * 3) the buffer struct members are filled with relevant information for
134 * the userspace.
135 *
136 * The return values from this function are intended to be directly returned
137 * from vidioc_dqbuf handler in driver.
138 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300139int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
140
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300141/**
142 * vb2_streamon - start streaming
143 * @q: videobuf2 queue
144 * @type: type argument passed from userspace to vidioc_streamon handler
145 *
146 * Should be called from vidioc_streamon handler of a driver.
147 * This function:
148 * 1) verifies current state
149 * 2) passes any previously queued buffers to the driver and starts streaming
150 *
151 * The return values from this function are intended to be directly returned
152 * from vidioc_streamon handler in the driver.
153 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300154int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300155
156/**
157 * vb2_streamoff - stop streaming
158 * @q: videobuf2 queue
159 * @type: type argument passed from userspace to vidioc_streamoff handler
160 *
161 * Should be called from vidioc_streamoff handler of a driver.
162 * This function:
163 * 1) verifies current state,
164 * 2) stop streaming and dequeues any queued buffers, including those previously
165 * passed to the driver (after waiting for the driver to finish).
166 *
167 * This call can be used for pausing playback.
168 * The return values from this function are intended to be directly returned
169 * from vidioc_streamoff handler in the driver
170 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300171int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
172
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300173/**
174 * vb2_queue_init() - initialize a videobuf2 queue
175 * @q: videobuf2 queue; this structure should be allocated in driver
176 *
177 * The vb2_queue structure should be allocated by the driver. The driver is
178 * responsible of clearing it's content and setting initial values for some
179 * required entries before calling this function.
180 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
181 * to the struct vb2_queue description in include/media/videobuf2-core.h
182 * for more information.
183 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300184int __must_check vb2_queue_init(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300185
186/**
187 * vb2_queue_release() - stop streaming, release the queue and free memory
188 * @q: videobuf2 queue
189 *
190 * This function stops streaming and performs necessary clean ups, including
191 * freeing video buffer memory. The driver is responsible for freeing
192 * the vb2_queue structure itself.
193 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300194void vb2_queue_release(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300195
196/**
197 * vb2_poll() - implements poll userspace operation
198 * @q: videobuf2 queue
199 * @file: file argument passed to the poll file operation handler
200 * @wait: wait argument passed to the poll file operation handler
201 *
202 * This function implements poll file operation handler for a driver.
203 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
204 * be informed that the file descriptor of a video device is available for
205 * reading.
206 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
207 * will be reported as available for writing.
208 *
209 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
210 * pending events.
211 *
212 * The return values from this function are intended to be directly returned
213 * from poll handler in driver.
214 */
Junghak Sungaf3bac12015-11-03 08:16:42 -0200215unsigned int vb2_poll(struct vb2_queue *q, struct file *file,
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300216 poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300217
218/*
219 * The following functions are not part of the vb2 core API, but are simple
220 * helper functions that you can use in your struct v4l2_file_operations,
221 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
222 * or video_device->lock is set, and they will set and test vb2_queue->owner
223 * to check if the calling filehandle is permitted to do the queuing operation.
224 */
225
226/* struct v4l2_ioctl_ops helpers */
227
228int vb2_ioctl_reqbufs(struct file *file, void *priv,
229 struct v4l2_requestbuffers *p);
230int vb2_ioctl_create_bufs(struct file *file, void *priv,
231 struct v4l2_create_buffers *p);
232int vb2_ioctl_prepare_buf(struct file *file, void *priv,
233 struct v4l2_buffer *p);
234int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
235int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
236int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
237int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
238int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
239int vb2_ioctl_expbuf(struct file *file, void *priv,
240 struct v4l2_exportbuffer *p);
241
242/* struct v4l2_file_operations helpers */
243
244int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
245int vb2_fop_release(struct file *file);
246int _vb2_fop_release(struct file *file, struct mutex *lock);
247ssize_t vb2_fop_write(struct file *file, const char __user *buf,
248 size_t count, loff_t *ppos);
249ssize_t vb2_fop_read(struct file *file, char __user *buf,
250 size_t count, loff_t *ppos);
251unsigned int vb2_fop_poll(struct file *file, poll_table *wait);
252#ifndef CONFIG_MMU
253unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
254 unsigned long len, unsigned long pgoff, unsigned long flags);
255#endif
256
257/* struct vb2_ops helpers, only use if vq->lock is non-NULL. */
258
259void vb2_ops_wait_prepare(struct vb2_queue *vq);
260void vb2_ops_wait_finish(struct vb2_queue *vq);
261
Junghak Sungc1399902015-09-22 10:30:29 -0300262#endif /* _MEDIA_VIDEOBUF2_V4L2_H */