Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 1 | /* |
| 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 Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 15 | #include <linux/videodev2.h> |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 16 | #include <media/videobuf2-core.h> |
| 17 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 18 | #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 Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 26 | /** |
| 27 | * struct vb2_v4l2_buffer - video buffer information for v4l2 |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 28 | * |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 29 | * @vb2_buf: video buffer 2 |
| 30 | * @flags: buffer informational flags |
| 31 | * @field: enum v4l2_field; field order of the image in the buffer |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 32 | * @timecode: frame timecode |
| 33 | * @sequence: sequence count of this frame |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 34 | * |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 35 | * Should contain enough information to be able to cover all the fields |
| 36 | * of struct v4l2_buffer at videodev2.h |
| 37 | */ |
| 38 | struct vb2_v4l2_buffer { |
| 39 | struct vb2_buffer vb2_buf; |
| 40 | |
| 41 | __u32 flags; |
| 42 | __u32 field; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 43 | struct v4l2_timecode timecode; |
| 44 | __u32 sequence; |
| 45 | }; |
| 46 | |
Mauro Carvalho Chehab | d383b57 | 2015-10-01 14:23:35 -0300 | [diff] [blame] | 47 | /* |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 48 | * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer * |
| 49 | */ |
| 50 | #define to_vb2_v4l2_buffer(vb) \ |
Mauro Carvalho Chehab | d383b57 | 2015-10-01 14:23:35 -0300 | [diff] [blame] | 51 | container_of(vb, struct vb2_v4l2_buffer, vb2_buf) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 52 | |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 53 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies |
| 57 | * the memory and type values. |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 58 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 59 | * @q: videobuf2 queue |
| 60 | * @req: struct passed from userspace to vidioc_reqbufs handler |
| 61 | * in driver |
| 62 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 63 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req); |
| 64 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 65 | /** |
| 66 | * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies |
| 67 | * the memory and type values. |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 68 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 69 | * @q: videobuf2 queue |
| 70 | * @create: creation parameters, passed from userspace to vidioc_create_bufs |
| 71 | * handler in driver |
| 72 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 73 | int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 77 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 78 | * @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 Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 84 | * |
| 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 Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 88 | * |
| 89 | * The return values from this function are intended to be directly returned |
| 90 | * from vidioc_prepare_buf handler in driver. |
| 91 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 92 | int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b); |
| 93 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 94 | /** |
| 95 | * vb2_qbuf() - Queue a buffer from userspace |
| 96 | * @q: videobuf2 queue |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 97 | * @b: buffer structure passed from userspace to VIDIOC_QBUF() handler |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 98 | * in driver |
| 99 | * |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 100 | * Should be called from VIDIOC_QBUF() ioctl handler of a driver. |
| 101 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 102 | * This function: |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 103 | * |
| 104 | * #) verifies the passed buffer, |
| 105 | * #) if necessary, calls buf_prepare callback in the driver (if provided), in |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 106 | * which driver-specific buffer initialization can be performed, |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 107 | * #) if streaming is on, queues the buffer in driver by the means of buf_queue |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 108 | * callback for processing. |
| 109 | * |
| 110 | * The return values from this function are intended to be directly returned |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 111 | * from VIDIOC_QBUF() handler in driver. |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 112 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 113 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * vb2_expbuf() - Export a buffer as a file descriptor |
| 117 | * @q: videobuf2 queue |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 118 | * @eb: export buffer structure passed from userspace to VIDIOC_EXPBUF() |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 119 | * handler in driver |
| 120 | * |
| 121 | * The return values from this function are intended to be directly returned |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 122 | * from VIDIOC_EXPBUF() handler in driver. |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 123 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 124 | int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * vb2_dqbuf() - Dequeue a buffer to the userspace |
| 128 | * @q: videobuf2 queue |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 129 | * @b: buffer structure passed from userspace to VIDIOC_DQBUF() handler |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 130 | * 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 Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 135 | * Should be called from VIDIOC_DQBUF() ioctl handler of a driver. |
| 136 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 137 | * This function: |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 138 | * |
| 139 | * #) verifies the passed buffer, |
| 140 | * #) calls buf_finish callback in the driver (if provided), in which |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 141 | * driver can perform any additional operations that may be required before |
| 142 | * returning the buffer to userspace, such as cache sync, |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 143 | * #) the buffer struct members are filled with relevant information for |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 144 | * the userspace. |
| 145 | * |
| 146 | * The return values from this function are intended to be directly returned |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 147 | * from VIDIOC_DQBUF() handler in driver. |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 148 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 149 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking); |
| 150 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 151 | /** |
| 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 Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 157 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 158 | * This function: |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 159 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 160 | * 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 166 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 167 | |
| 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 Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 174 | * |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 175 | * This function: |
Mauro Carvalho Chehab | bf4404b | 2016-09-08 18:01:44 -0300 | [diff] [blame] | 176 | * |
| 177 | * #) verifies current state, |
| 178 | * #) stop streaming and dequeues any queued buffers, including those previously |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 179 | * 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 185 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type); |
| 186 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 187 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 198 | int __must_check vb2_queue_init(struct vb2_queue *q); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 199 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 208 | void vb2_queue_release(struct vb2_queue *q); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 209 | |
| 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 Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 229 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame] | 230 | poll_table *wait); |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 231 | |
| 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 | |
| 242 | int vb2_ioctl_reqbufs(struct file *file, void *priv, |
| 243 | struct v4l2_requestbuffers *p); |
| 244 | int vb2_ioctl_create_bufs(struct file *file, void *priv, |
| 245 | struct v4l2_create_buffers *p); |
| 246 | int vb2_ioctl_prepare_buf(struct file *file, void *priv, |
| 247 | struct v4l2_buffer *p); |
| 248 | int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 249 | int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 250 | int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 251 | int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i); |
| 252 | int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i); |
| 253 | int vb2_ioctl_expbuf(struct file *file, void *priv, |
| 254 | struct v4l2_exportbuffer *p); |
| 255 | |
| 256 | /* struct v4l2_file_operations helpers */ |
| 257 | |
| 258 | int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma); |
| 259 | int vb2_fop_release(struct file *file); |
| 260 | int _vb2_fop_release(struct file *file, struct mutex *lock); |
| 261 | ssize_t vb2_fop_write(struct file *file, const char __user *buf, |
| 262 | size_t count, loff_t *ppos); |
| 263 | ssize_t vb2_fop_read(struct file *file, char __user *buf, |
| 264 | size_t count, loff_t *ppos); |
| 265 | unsigned int vb2_fop_poll(struct file *file, poll_table *wait); |
| 266 | #ifndef CONFIG_MMU |
| 267 | unsigned 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 Chehab | dba2d12 | 2016-09-08 18:12:18 -0300 | [diff] [blame] | 271 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 278 | void vb2_ops_wait_prepare(struct vb2_queue *vq); |
Mauro Carvalho Chehab | dba2d12 | 2016-09-08 18:12:18 -0300 | [diff] [blame] | 279 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 287 | void vb2_ops_wait_finish(struct vb2_queue *vq); |
| 288 | |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 289 | #endif /* _MEDIA_VIDEOBUF2_V4L2_H */ |