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 |
| 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 Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 31 | * @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 | */ |
| 36 | struct vb2_v4l2_buffer { |
| 37 | struct vb2_buffer vb2_buf; |
| 38 | |
| 39 | __u32 flags; |
| 40 | __u32 field; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 41 | struct v4l2_timecode timecode; |
| 42 | __u32 sequence; |
| 43 | }; |
| 44 | |
Mauro Carvalho Chehab | d383b57 | 2015-10-01 14:23:35 -0300 | [diff] [blame] | 45 | /* |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 46 | * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer * |
| 47 | */ |
| 48 | #define to_vb2_v4l2_buffer(vb) \ |
Mauro Carvalho Chehab | d383b57 | 2015-10-01 14:23:35 -0300 | [diff] [blame] | 49 | container_of(vb, struct vb2_v4l2_buffer, vb2_buf) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 50 | |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 51 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 52 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 60 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req); |
| 61 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 62 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 69 | 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^] | 70 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 86 | int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b); |
| 87 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 88 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 105 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 106 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 116 | int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 117 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 139 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking); |
| 140 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 141 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 154 | 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^] | 155 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 171 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type); |
| 172 | |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 173 | /** |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 184 | int __must_check vb2_queue_init(struct vb2_queue *q); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 185 | |
| 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 Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 194 | void vb2_queue_release(struct vb2_queue *q); |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 195 | |
| 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 Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 215 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, |
Mauro Carvalho Chehab | 24ade5b | 2016-09-08 14:22:00 -0300 | [diff] [blame^] | 216 | poll_table *wait); |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 217 | |
| 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 | |
| 228 | int vb2_ioctl_reqbufs(struct file *file, void *priv, |
| 229 | struct v4l2_requestbuffers *p); |
| 230 | int vb2_ioctl_create_bufs(struct file *file, void *priv, |
| 231 | struct v4l2_create_buffers *p); |
| 232 | int vb2_ioctl_prepare_buf(struct file *file, void *priv, |
| 233 | struct v4l2_buffer *p); |
| 234 | int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 235 | int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 236 | int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p); |
| 237 | int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i); |
| 238 | int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i); |
| 239 | int vb2_ioctl_expbuf(struct file *file, void *priv, |
| 240 | struct v4l2_exportbuffer *p); |
| 241 | |
| 242 | /* struct v4l2_file_operations helpers */ |
| 243 | |
| 244 | int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma); |
| 245 | int vb2_fop_release(struct file *file); |
| 246 | int _vb2_fop_release(struct file *file, struct mutex *lock); |
| 247 | ssize_t vb2_fop_write(struct file *file, const char __user *buf, |
| 248 | size_t count, loff_t *ppos); |
| 249 | ssize_t vb2_fop_read(struct file *file, char __user *buf, |
| 250 | size_t count, loff_t *ppos); |
| 251 | unsigned int vb2_fop_poll(struct file *file, poll_table *wait); |
| 252 | #ifndef CONFIG_MMU |
| 253 | unsigned 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 | |
| 259 | void vb2_ops_wait_prepare(struct vb2_queue *vq); |
| 260 | void vb2_ops_wait_finish(struct vb2_queue *vq); |
| 261 | |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 262 | #endif /* _MEDIA_VIDEOBUF2_V4L2_H */ |