blob: 036127c54bbf2d051fd99d08758da389b4722c11 [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
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030028 *
Junghak Sung2d700712015-09-22 10:30:30 -030029 * @vb2_buf: video buffer 2
30 * @flags: buffer informational flags
31 * @field: enum v4l2_field; field order of the image in the buffer
Junghak Sung2d700712015-09-22 10:30:30 -030032 * @timecode: frame timecode
33 * @sequence: sequence count of this frame
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030034 *
Junghak Sung2d700712015-09-22 10:30:30 -030035 * Should contain enough information to be able to cover all the fields
36 * of struct v4l2_buffer at videodev2.h
37 */
38struct vb2_v4l2_buffer {
39 struct vb2_buffer vb2_buf;
40
41 __u32 flags;
42 __u32 field;
Junghak Sung2d700712015-09-22 10:30:30 -030043 struct v4l2_timecode timecode;
44 __u32 sequence;
45};
46
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030047/*
Junghak Sung2d700712015-09-22 10:30:30 -030048 * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
49 */
50#define to_vb2_v4l2_buffer(vb) \
Mauro Carvalho Chehabd383b572015-10-01 14:23:35 -030051 container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
Junghak Sung2d700712015-09-22 10:30:30 -030052
Junghak Sung3c5be982015-10-06 06:37:49 -030053int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030054
55/**
56 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
57 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030058 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030059 * @q: videobuf2 queue
60 * @req: struct passed from userspace to vidioc_reqbufs handler
61 * in driver
62 */
Junghak Sung3c5be982015-10-06 06:37:49 -030063int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
64
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030065/**
66 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
67 * the memory and type values.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030068 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030069 * @q: videobuf2 queue
70 * @create: creation parameters, passed from userspace to vidioc_create_bufs
71 * handler in driver
72 */
Junghak Sung3c5be982015-10-06 06:37:49 -030073int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030074
75/**
76 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030077 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030078 * @q: videobuf2 queue
79 * @b: buffer structure passed from userspace to vidioc_prepare_buf
80 * handler in driver
81 *
82 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
83 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030084 *
85 * #) verifies the passed buffer,
86 * #) calls buf_prepare callback in the driver (if provided), in which
87 * driver-specific buffer initialization can be performed.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030088 *
89 * The return values from this function are intended to be directly returned
90 * from vidioc_prepare_buf handler in driver.
91 */
Junghak Sung3c5be982015-10-06 06:37:49 -030092int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
93
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030094/**
95 * vb2_qbuf() - Queue a buffer from userspace
96 * @q: videobuf2 queue
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -030097 * @b: buffer structure passed from userspace to VIDIOC_QBUF() handler
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -030098 * in driver
99 *
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300100 * Should be called from VIDIOC_QBUF() ioctl handler of a driver.
101 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300102 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300103 *
104 * #) verifies the passed buffer,
105 * #) if necessary, calls buf_prepare callback in the driver (if provided), in
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300106 * which driver-specific buffer initialization can be performed,
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300107 * #) if streaming is on, queues the buffer in driver by the means of buf_queue
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300108 * callback for processing.
109 *
110 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300111 * from VIDIOC_QBUF() handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300112 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300113int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300114
115/**
116 * vb2_expbuf() - Export a buffer as a file descriptor
117 * @q: videobuf2 queue
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300118 * @eb: export buffer structure passed from userspace to VIDIOC_EXPBUF()
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300119 * handler in driver
120 *
121 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300122 * from VIDIOC_EXPBUF() handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300123 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300124int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300125
126/**
127 * vb2_dqbuf() - Dequeue a buffer to the userspace
128 * @q: videobuf2 queue
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300129 * @b: buffer structure passed from userspace to VIDIOC_DQBUF() handler
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300130 * in driver
131 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
132 * buffers ready for dequeuing are present. Normally the driver
133 * would be passing (file->f_flags & O_NONBLOCK) here
134 *
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300135 * Should be called from VIDIOC_DQBUF() ioctl handler of a driver.
136 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300137 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300138 *
139 * #) verifies the passed buffer,
140 * #) calls buf_finish callback in the driver (if provided), in which
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300141 * driver can perform any additional operations that may be required before
142 * returning the buffer to userspace, such as cache sync,
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300143 * #) the buffer struct members are filled with relevant information for
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300144 * the userspace.
145 *
146 * The return values from this function are intended to be directly returned
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300147 * from VIDIOC_DQBUF() handler in driver.
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300148 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300149int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
150
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300151/**
152 * vb2_streamon - start streaming
153 * @q: videobuf2 queue
154 * @type: type argument passed from userspace to vidioc_streamon handler
155 *
156 * Should be called from vidioc_streamon handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300157 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300158 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300159 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300160 * 1) verifies current state
161 * 2) passes any previously queued buffers to the driver and starts streaming
162 *
163 * The return values from this function are intended to be directly returned
164 * from vidioc_streamon handler in the driver.
165 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300166int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300167
168/**
169 * vb2_streamoff - stop streaming
170 * @q: videobuf2 queue
171 * @type: type argument passed from userspace to vidioc_streamoff handler
172 *
173 * Should be called from vidioc_streamoff handler of a driver.
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300174 *
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300175 * This function:
Mauro Carvalho Chehabbf4404b2016-09-08 18:01:44 -0300176 *
177 * #) verifies current state,
178 * #) stop streaming and dequeues any queued buffers, including those previously
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300179 * passed to the driver (after waiting for the driver to finish).
180 *
181 * This call can be used for pausing playback.
182 * The return values from this function are intended to be directly returned
183 * from vidioc_streamoff handler in the driver
184 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300185int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
186
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300187/**
188 * vb2_queue_init() - initialize a videobuf2 queue
189 * @q: videobuf2 queue; this structure should be allocated in driver
190 *
191 * The vb2_queue structure should be allocated by the driver. The driver is
192 * responsible of clearing it's content and setting initial values for some
193 * required entries before calling this function.
194 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
195 * to the struct vb2_queue description in include/media/videobuf2-core.h
196 * for more information.
197 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300198int __must_check vb2_queue_init(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300199
200/**
201 * vb2_queue_release() - stop streaming, release the queue and free memory
202 * @q: videobuf2 queue
203 *
204 * This function stops streaming and performs necessary clean ups, including
205 * freeing video buffer memory. The driver is responsible for freeing
206 * the vb2_queue structure itself.
207 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300208void vb2_queue_release(struct vb2_queue *q);
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300209
210/**
211 * vb2_poll() - implements poll userspace operation
212 * @q: videobuf2 queue
213 * @file: file argument passed to the poll file operation handler
214 * @wait: wait argument passed to the poll file operation handler
215 *
216 * This function implements poll file operation handler for a driver.
217 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
218 * be informed that the file descriptor of a video device is available for
219 * reading.
220 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
221 * will be reported as available for writing.
222 *
223 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
224 * pending events.
225 *
226 * The return values from this function are intended to be directly returned
227 * from poll handler in driver.
228 */
Junghak Sungaf3bac12015-11-03 08:16:42 -0200229unsigned int vb2_poll(struct vb2_queue *q, struct file *file,
Mauro Carvalho Chehab24ade5b2016-09-08 14:22:00 -0300230 poll_table *wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300231
232/*
233 * The following functions are not part of the vb2 core API, but are simple
234 * helper functions that you can use in your struct v4l2_file_operations,
235 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
236 * or video_device->lock is set, and they will set and test vb2_queue->owner
237 * to check if the calling filehandle is permitted to do the queuing operation.
238 */
239
240/* struct v4l2_ioctl_ops helpers */
241
242int vb2_ioctl_reqbufs(struct file *file, void *priv,
243 struct v4l2_requestbuffers *p);
244int vb2_ioctl_create_bufs(struct file *file, void *priv,
245 struct v4l2_create_buffers *p);
246int vb2_ioctl_prepare_buf(struct file *file, void *priv,
247 struct v4l2_buffer *p);
248int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
249int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
250int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
251int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
252int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
253int vb2_ioctl_expbuf(struct file *file, void *priv,
254 struct v4l2_exportbuffer *p);
255
256/* struct v4l2_file_operations helpers */
257
258int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
259int vb2_fop_release(struct file *file);
260int _vb2_fop_release(struct file *file, struct mutex *lock);
261ssize_t vb2_fop_write(struct file *file, const char __user *buf,
262 size_t count, loff_t *ppos);
263ssize_t vb2_fop_read(struct file *file, char __user *buf,
264 size_t count, loff_t *ppos);
265unsigned int vb2_fop_poll(struct file *file, poll_table *wait);
266#ifndef CONFIG_MMU
267unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
268 unsigned long len, unsigned long pgoff, unsigned long flags);
269#endif
270
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300271/**
272 * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue
273 *
274 * @vq: pointer to struct vb2_queue
275 *
276 * ..note:: only use if vq->lock is non-NULL.
277 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300278void vb2_ops_wait_prepare(struct vb2_queue *vq);
Mauro Carvalho Chehabdba2d122016-09-08 18:12:18 -0300279
280/**
281 * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue
282 *
283 * @vq: pointer to struct vb2_queue
284 *
285 * ..note:: only use if vq->lock is non-NULL.
286 */
Junghak Sung3c5be982015-10-06 06:37:49 -0300287void vb2_ops_wait_finish(struct vb2_queue *vq);
288
Junghak Sungc1399902015-09-22 10:30:29 -0300289#endif /* _MEDIA_VIDEOBUF2_V4L2_H */