blob: a9961bc776dcbe0d8d2f2a81b13a6c14bf7db3e3 [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.
Hans Verkuil394dc582018-05-30 02:46:22 -040035 * @request_fd: the request_fd associated with this buffer
Hans Verkuildb6e8d52018-05-21 04:54:45 -040036 * @planes: plane information (userptr/fd, length, bytesused, data_offset).
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030037 *
Junghak Sung2d700712015-09-22 10:30:30 -030038 * Should contain enough information to be able to cover all the fields
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040039 * of &struct v4l2_buffer at ``videodev2.h``.
Junghak Sung2d700712015-09-22 10:30:30 -030040 */
41struct vb2_v4l2_buffer {
42 struct vb2_buffer vb2_buf;
43
44 __u32 flags;
45 __u32 field;
Junghak Sung2d700712015-09-22 10:30:30 -030046 struct v4l2_timecode timecode;
47 __u32 sequence;
Hans Verkuil394dc582018-05-30 02:46:22 -040048 __s32 request_fd;
Hans Verkuildb6e8d52018-05-21 04:54:45 -040049 struct vb2_plane planes[VB2_MAX_PLANES];
Junghak Sung2d700712015-09-22 10:30:30 -030050};
51
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030052/*
Junghak Sung2d700712015-09-22 10:30:30 -030053 * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
54 */
55#define to_vb2_v4l2_buffer(vb) \
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030056 container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
Junghak Sung2d700712015-09-22 10:30:30 -030057
Hans Verkuil245ede42018-10-24 06:51:01 -040058/**
59 * vb2_find_timestamp() - Find buffer with given timestamp in the queue
60 *
61 * @q: pointer to &struct vb2_queue with videobuf2 queue.
62 * @timestamp: the timestamp to find. Only buffers in state DEQUEUED or DONE
63 * are considered.
64 * @start_idx: the start index (usually 0) in the buffer array to start
65 * searching from. Note that there may be multiple buffers
66 * with the same timestamp value, so you can restart the search
67 * by setting @start_idx to the previously found index + 1.
68 *
69 * Returns the buffer index of the buffer with the given @timestamp, or
70 * -1 if no buffer with @timestamp was found.
71 */
72int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
73 unsigned int start_idx);
74
Junghak Sung3c5be982015-10-06 06:37:49 -030075int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030076
77/**
78 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
79 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030080 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040081 * @q: pointer to &struct vb2_queue with videobuf2 queue.
82 * @req: &struct v4l2_requestbuffers passed from userspace to
83 * &v4l2_ioctl_ops->vidioc_reqbufs handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030084 */
Junghak Sung3c5be982015-10-06 06:37:49 -030085int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
86
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030087/**
88 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
89 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030090 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -040091 * @q: pointer to &struct vb2_queue with videobuf2 queue.
92 * @create: creation parameters, passed from userspace to
93 * &v4l2_ioctl_ops->vidioc_create_bufs handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030094 */
Junghak Sung3c5be982015-10-06 06:37:49 -030095int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030096
97/**
98 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030099 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400100 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Hans Verkuil394dc582018-05-30 02:46:22 -0400101 * @mdev: pointer to &struct media_device, may be NULL.
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400102 * @b: buffer structure passed from userspace to
103 * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300104 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400105 * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler
106 * of a driver.
107 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300108 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300109 *
110 * #) verifies the passed buffer,
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400111 * #) calls &vb2_ops->buf_prepare callback in the driver (if provided),
112 * in which driver-specific buffer initialization can be performed.
Hans Verkuil394dc582018-05-30 02:46:22 -0400113 * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
114 * then bind the prepared buffer to the request.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300115 *
116 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400117 * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300118 */
Hans Verkuil394dc582018-05-30 02:46:22 -0400119int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
120 struct v4l2_buffer *b);
Junghak Sung3c5be982015-10-06 06:37:49 -0300121
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300122/**
123 * vb2_qbuf() - Queue a buffer from userspace
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400124 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Hans Verkuil394dc582018-05-30 02:46:22 -0400125 * @mdev: pointer to &struct media_device, may be NULL.
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400126 * @b: buffer structure passed from userspace to
127 * &v4l2_ioctl_ops->vidioc_qbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300128 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400129 * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300130 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300131 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300132 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400133 * #) verifies the passed buffer;
Hans Verkuil394dc582018-05-30 02:46:22 -0400134 * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
135 * then bind the buffer to the request.
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400136 * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
137 * (if provided), in which driver-specific buffer initialization can
138 * be performed;
139 * #) if streaming is on, queues the buffer in driver by the means of
140 * &vb2_ops->buf_queue callback for processing.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300141 *
142 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400143 * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300144 */
Hans Verkuil394dc582018-05-30 02:46:22 -0400145int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
146 struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300147
148/**
149 * vb2_expbuf() - Export a buffer as a file descriptor
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400150 * @q: pointer to &struct vb2_queue with videobuf2 queue.
151 * @eb: export buffer structure passed from userspace to
152 * &v4l2_ioctl_ops->vidioc_expbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300153 *
154 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400155 * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300156 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300157int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300158
159/**
160 * vb2_dqbuf() - Dequeue a buffer to the userspace
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400161 * @q: pointer to &struct vb2_queue with videobuf2 queue.
162 * @b: buffer structure passed from userspace to
163 * &v4l2_ioctl_ops->vidioc_dqbuf handler in driver
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300164 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
165 * buffers ready for dequeuing are present. Normally the driver
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400166 * would be passing (&file->f_flags & %O_NONBLOCK) here
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300167 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400168 * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler
169 * of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300170 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300171 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300172 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400173 * #) verifies the passed buffer;
174 * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300175 * driver can perform any additional operations that may be required before
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400176 * returning the buffer to userspace, such as cache sync;
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300177 * #) the buffer struct members are filled with relevant information for
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300178 * the userspace.
179 *
180 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400181 * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300182 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300183int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
184
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300185/**
186 * vb2_streamon - start streaming
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400187 * @q: pointer to &struct vb2_queue with videobuf2 queue.
188 * @type: type argument passed from userspace to vidioc_streamon handler,
189 * as defined by &enum v4l2_buf_type.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300190 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400191 * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300192 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300193 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300194 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300195 * 1) verifies current state
196 * 2) passes any previously queued buffers to the driver and starts streaming
197 *
198 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400199 * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300200 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300201int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300202
203/**
204 * vb2_streamoff - stop streaming
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400205 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300206 * @type: type argument passed from userspace to vidioc_streamoff handler
207 *
208 * Should be called from vidioc_streamoff handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300209 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300210 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300211 *
212 * #) verifies current state,
213 * #) stop streaming and dequeues any queued buffers, including those previously
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300214 * passed to the driver (after waiting for the driver to finish).
215 *
216 * This call can be used for pausing playback.
217 * The return values from this function are intended to be directly returned
218 * from vidioc_streamoff handler in the driver
219 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300220int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
221
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300222/**
223 * vb2_queue_init() - initialize a videobuf2 queue
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400224 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300225 *
226 * The vb2_queue structure should be allocated by the driver. The driver is
227 * responsible of clearing it's content and setting initial values for some
228 * required entries before calling this function.
229 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
230 * to the struct vb2_queue description in include/media/videobuf2-core.h
231 * for more information.
232 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300233int __must_check vb2_queue_init(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300234
235/**
236 * vb2_queue_release() - stop streaming, release the queue and free memory
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400237 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300238 *
239 * This function stops streaming and performs necessary clean ups, including
240 * freeing video buffer memory. The driver is responsible for freeing
241 * the vb2_queue structure itself.
242 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300243void vb2_queue_release(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300244
245/**
246 * vb2_poll() - implements poll userspace operation
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400247 * @q: pointer to &struct vb2_queue with videobuf2 queue.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300248 * @file: file argument passed to the poll file operation handler
249 * @wait: wait argument passed to the poll file operation handler
250 *
251 * This function implements poll file operation handler for a driver.
252 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
253 * be informed that the file descriptor of a video device is available for
254 * reading.
255 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
256 * will be reported as available for writing.
257 *
258 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
259 * pending events.
260 *
261 * The return values from this function are intended to be directly returned
262 * from poll handler in driver.
263 */
Al Viroc23e0cb2017-07-03 03:02:56 -0400264__poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300265
266/*
267 * The following functions are not part of the vb2 core API, but are simple
268 * helper functions that you can use in your struct v4l2_file_operations,
269 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
270 * or video_device->lock is set, and they will set and test vb2_queue->owner
271 * to check if the calling filehandle is permitted to do the queuing operation.
272 */
273
274/* struct v4l2_ioctl_ops helpers */
275
276int vb2_ioctl_reqbufs(struct file *file, void *priv,
277 struct v4l2_requestbuffers *p);
278int vb2_ioctl_create_bufs(struct file *file, void *priv,
279 struct v4l2_create_buffers *p);
280int vb2_ioctl_prepare_buf(struct file *file, void *priv,
281 struct v4l2_buffer *p);
282int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
283int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
284int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
285int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
286int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
287int vb2_ioctl_expbuf(struct file *file, void *priv,
288 struct v4l2_exportbuffer *p);
289
290/* struct v4l2_file_operations helpers */
291
292int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
293int vb2_fop_release(struct file *file);
294int _vb2_fop_release(struct file *file, struct mutex *lock);
295ssize_t vb2_fop_write(struct file *file, const char __user *buf,
296 size_t count, loff_t *ppos);
297ssize_t vb2_fop_read(struct file *file, char __user *buf,
298 size_t count, loff_t *ppos);
Al Viroc23e0cb2017-07-03 03:02:56 -0400299__poll_t vb2_fop_poll(struct file *file, poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300300#ifndef CONFIG_MMU
301unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
302 unsigned long len, unsigned long pgoff, unsigned long flags);
303#endif
304
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300305/**
306 * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue
307 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400308 * @vq: pointer to &struct vb2_queue
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300309 *
310 * ..note:: only use if vq->lock is non-NULL.
311 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300312void vb2_ops_wait_prepare(struct vb2_queue *vq);
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300313
314/**
315 * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue
316 *
Mauro Carvalho Chehab9fbe71b2017-10-09 05:36:52 -0400317 * @vq: pointer to &struct vb2_queue
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300318 *
319 * ..note:: only use if vq->lock is non-NULL.
320 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300321void vb2_ops_wait_finish(struct vb2_queue *vq);
322
Hans Verkuil86f6bd32018-05-21 04:54:52 -0400323struct media_request;
324int vb2_request_validate(struct media_request *req);
325void vb2_request_queue(struct media_request *req);
326
Junghak Sungc1399902015-09-22 10:30:29 -0300327#endif /* _MEDIA_VIDEOBUF2_V4L2_H */