blob: 3d5e2d739f057679ad2176e458b77b445e01d266 [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/**
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040027 * struct vb2_v4l2_buffer - video buffer information for v4l2.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030028 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040029 * @vb2_buf: embedded struct &vb2_buffer.
30 * @flags: buffer informational flags.
31 * @field: field order of the image in the buffer, as defined by
32 * &enum v4l2_field.
33 * @timecode: frame timecode.
34 * @sequence: sequence count of this frame.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030035 *
Junghak Sung2d700712015-09-22 10:30:30 -030036 * Should contain enough information to be able to cover all the fields
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040037 * of &struct v4l2_buffer at ``videodev2.h``.
Junghak Sung2d700712015-09-22 10:30:30 -030038 */
39struct vb2_v4l2_buffer {
40 struct vb2_buffer vb2_buf;
41
42 __u32 flags;
43 __u32 field;
Junghak Sung2d700712015-09-22 10:30:30 -030044 struct v4l2_timecode timecode;
45 __u32 sequence;
46};
47
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030048/*
Junghak Sung2d700712015-09-22 10:30:30 -030049 * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
50 */
51#define to_vb2_v4l2_buffer(vb) \
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030052 container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
Junghak Sung2d700712015-09-22 10:30:30 -030053
Junghak Sung3c5be982015-10-06 06:37:49 -030054int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030055
56/**
57 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
58 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030059 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040060 * @q: pointer to &struct vb2_queue with videobuf2 queue.
61 * @req: &struct v4l2_requestbuffers passed from userspace to
62 * &v4l2_ioctl_ops->vidioc_reqbufs handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030063 */
Junghak Sung3c5be982015-10-06 06:37:49 -030064int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
65
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030066/**
67 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
68 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030069 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040070 * @q: pointer to &struct vb2_queue with videobuf2 queue.
71 * @create: creation parameters, passed from userspace to
72 * &v4l2_ioctl_ops->vidioc_create_bufs handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030073 */
Junghak Sung3c5be982015-10-06 06:37:49 -030074int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030075
76/**
77 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030078 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040079 * @q: pointer to &struct vb2_queue with videobuf2 queue.
80 * @b: buffer structure passed from userspace to
81 * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030082 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040083 * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler
84 * of a driver.
85 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030086 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030087 *
88 * #) verifies the passed buffer,
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040089 * #) calls &vb2_ops->buf_prepare callback in the driver (if provided),
90 * in which driver-specific buffer initialization can be performed.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030091 *
92 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040093 * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030094 */
Junghak Sung3c5be982015-10-06 06:37:49 -030095int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
96
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030097/**
98 * vb2_qbuf() - Queue a buffer from userspace
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040099 * @q: pointer to &struct vb2_queue with videobuf2 queue.
100 * @b: buffer structure passed from userspace to
101 * &v4l2_ioctl_ops->vidioc_qbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300102 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400103 * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300104 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300105 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300106 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400107 * #) verifies the passed buffer;
108 * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
109 * (if provided), in which driver-specific buffer initialization can
110 * be performed;
111 * #) if streaming is on, queues the buffer in driver by the means of
112 * &vb2_ops->buf_queue callback for processing.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300113 *
114 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400115 * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300116 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300117int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300118
119/**
120 * vb2_expbuf() - Export a buffer as a file descriptor
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400121 * @q: pointer to &struct vb2_queue with videobuf2 queue.
122 * @eb: export buffer structure passed from userspace to
123 * &v4l2_ioctl_ops->vidioc_expbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300124 *
125 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400126 * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300127 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300128int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300129
130/**
131 * vb2_dqbuf() - Dequeue a buffer to the userspace
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400132 * @q: pointer to &struct vb2_queue with videobuf2 queue.
133 * @b: buffer structure passed from userspace to
134 * &v4l2_ioctl_ops->vidioc_dqbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300135 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
136 * buffers ready for dequeuing are present. Normally the driver
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400137 * would be passing (&file->f_flags & %O_NONBLOCK) here
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300138 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400139 * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler
140 * of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300141 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300142 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300143 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400144 * #) verifies the passed buffer;
145 * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300146 * driver can perform any additional operations that may be required before
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400147 * returning the buffer to userspace, such as cache sync;
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300148 * #) the buffer struct members are filled with relevant information for
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300149 * the userspace.
150 *
151 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400152 * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300153 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300154int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
155
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300156/**
157 * vb2_streamon - start streaming
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400158 * @q: pointer to &struct vb2_queue with videobuf2 queue.
159 * @type: type argument passed from userspace to vidioc_streamon handler,
160 * as defined by &enum v4l2_buf_type.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300161 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400162 * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300163 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300164 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300165 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300166 * 1) verifies current state
167 * 2) passes any previously queued buffers to the driver and starts streaming
168 *
169 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400170 * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300171 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300172int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300173
174/**
175 * vb2_streamoff - stop streaming
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400176 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300177 * @type: type argument passed from userspace to vidioc_streamoff handler
178 *
179 * Should be called from vidioc_streamoff handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300180 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300181 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300182 *
183 * #) verifies current state,
184 * #) stop streaming and dequeues any queued buffers, including those previously
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300185 * passed to the driver (after waiting for the driver to finish).
186 *
187 * This call can be used for pausing playback.
188 * The return values from this function are intended to be directly returned
189 * from vidioc_streamoff handler in the driver
190 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300191int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
192
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300193/**
194 * vb2_queue_init() - initialize a videobuf2 queue
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400195 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300196 *
197 * The vb2_queue structure should be allocated by the driver. The driver is
198 * responsible of clearing it's content and setting initial values for some
199 * required entries before calling this function.
200 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
201 * to the struct vb2_queue description in include/media/videobuf2-core.h
202 * for more information.
203 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300204int __must_check vb2_queue_init(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300205
206/**
207 * vb2_queue_release() - stop streaming, release the queue and free memory
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400208 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300209 *
210 * This function stops streaming and performs necessary clean ups, including
211 * freeing video buffer memory. The driver is responsible for freeing
212 * the vb2_queue structure itself.
213 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300214void vb2_queue_release(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300215
216/**
217 * vb2_poll() - implements poll userspace operation
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400218 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300219 * @file: file argument passed to the poll file operation handler
220 * @wait: wait argument passed to the poll file operation handler
221 *
222 * This function implements poll file operation handler for a driver.
223 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
224 * be informed that the file descriptor of a video device is available for
225 * reading.
226 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
227 * will be reported as available for writing.
228 *
229 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
230 * pending events.
231 *
232 * The return values from this function are intended to be directly returned
233 * from poll handler in driver.
234 */
Al Viroc23e0cb2017-07-03 03:02:56 -0400235__poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300236
237/*
238 * The following functions are not part of the vb2 core API, but are simple
239 * helper functions that you can use in your struct v4l2_file_operations,
240 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
241 * or video_device->lock is set, and they will set and test vb2_queue->owner
242 * to check if the calling filehandle is permitted to do the queuing operation.
243 */
244
245/* struct v4l2_ioctl_ops helpers */
246
247int vb2_ioctl_reqbufs(struct file *file, void *priv,
248 struct v4l2_requestbuffers *p);
249int vb2_ioctl_create_bufs(struct file *file, void *priv,
250 struct v4l2_create_buffers *p);
251int vb2_ioctl_prepare_buf(struct file *file, void *priv,
252 struct v4l2_buffer *p);
253int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
254int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
255int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
256int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
257int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
258int vb2_ioctl_expbuf(struct file *file, void *priv,
259 struct v4l2_exportbuffer *p);
260
261/* struct v4l2_file_operations helpers */
262
263int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
264int vb2_fop_release(struct file *file);
265int _vb2_fop_release(struct file *file, struct mutex *lock);
266ssize_t vb2_fop_write(struct file *file, const char __user *buf,
267 size_t count, loff_t *ppos);
268ssize_t vb2_fop_read(struct file *file, char __user *buf,
269 size_t count, loff_t *ppos);
Al Viroc23e0cb2017-07-03 03:02:56 -0400270__poll_t vb2_fop_poll(struct file *file, poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300271#ifndef CONFIG_MMU
272unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
273 unsigned long len, unsigned long pgoff, unsigned long flags);
274#endif
275
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300276/**
277 * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue
278 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400279 * @vq: pointer to &struct vb2_queue
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300280 *
281 * ..note:: only use if vq->lock is non-NULL.
282 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300283void vb2_ops_wait_prepare(struct vb2_queue *vq);
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300284
285/**
286 * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue
287 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400288 * @vq: pointer to &struct vb2_queue
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300289 *
290 * ..note:: only use if vq->lock is non-NULL.
291 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300292void vb2_ops_wait_finish(struct vb2_queue *vq);
293
Junghak Sungc1399902015-09-22 10:30:29 -0300294#endif /* _MEDIA_VIDEOBUF2_V4L2_H */