Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | * videobuf2-core.h - V4L2 driver helper framework |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
| 5 | * |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 6 | * Author: Pawel Osciak <pawel@osciak.com> |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 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_CORE_H |
| 13 | #define _MEDIA_VIDEOBUF2_CORE_H |
| 14 | |
| 15 | #include <linux/mm_types.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/poll.h> |
| 18 | #include <linux/videodev2.h> |
| 19 | |
| 20 | struct vb2_alloc_ctx; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 21 | struct vb2_fileio_data; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * struct vb2_mem_ops - memory handling/memory allocator operations |
| 25 | * @alloc: allocate video memory and, optionally, allocator private data, |
| 26 | * return NULL on failure or a pointer to allocator private, |
| 27 | * per-buffer data on success; the returned private structure |
| 28 | * will then be passed as buf_priv argument to other ops in this |
| 29 | * structure |
| 30 | * @put: inform the allocator that the buffer will no longer be used; |
| 31 | * usually will result in the allocator freeing the buffer (if |
| 32 | * no other users of this buffer are present); the buf_priv |
| 33 | * argument is the allocator private per-buffer structure |
| 34 | * previously returned from the alloc callback |
| 35 | * @get_userptr: acquire userspace memory for a hardware operation; used for |
| 36 | * USERPTR memory types; vaddr is the address passed to the |
| 37 | * videobuf layer when queuing a video buffer of USERPTR type; |
| 38 | * should return an allocator private per-buffer structure |
| 39 | * associated with the buffer on success, NULL on failure; |
| 40 | * the returned private structure will then be passed as buf_priv |
| 41 | * argument to other ops in this structure |
| 42 | * @put_userptr: inform the allocator that a USERPTR buffer will no longer |
| 43 | * be used |
| 44 | * @vaddr: return a kernel virtual address to a given memory buffer |
| 45 | * associated with the passed private structure or NULL if no |
| 46 | * such mapping exists |
| 47 | * @cookie: return allocator specific cookie for a given memory buffer |
| 48 | * associated with the passed private structure or NULL if not |
| 49 | * available |
| 50 | * @num_users: return the current number of users of a memory buffer; |
| 51 | * return 1 if the videobuf layer (or actually the driver using |
| 52 | * it) is the only user |
| 53 | * @mmap: setup a userspace mapping for a given memory buffer under |
| 54 | * the provided virtual memory region |
| 55 | * |
| 56 | * Required ops for USERPTR types: get_userptr, put_userptr. |
| 57 | * Required ops for MMAP types: alloc, put, num_users, mmap. |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 58 | * Required ops for read/write access types: alloc, put, num_users, vaddr |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 59 | */ |
| 60 | struct vb2_mem_ops { |
| 61 | void *(*alloc)(void *alloc_ctx, unsigned long size); |
| 62 | void (*put)(void *buf_priv); |
| 63 | |
| 64 | void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr, |
| 65 | unsigned long size, int write); |
| 66 | void (*put_userptr)(void *buf_priv); |
| 67 | |
| 68 | void *(*vaddr)(void *buf_priv); |
| 69 | void *(*cookie)(void *buf_priv); |
| 70 | |
| 71 | unsigned int (*num_users)(void *buf_priv); |
| 72 | |
| 73 | int (*mmap)(void *buf_priv, struct vm_area_struct *vma); |
| 74 | }; |
| 75 | |
| 76 | struct vb2_plane { |
| 77 | void *mem_priv; |
| 78 | int mapped:1; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * enum vb2_io_modes - queue access methods |
| 83 | * @VB2_MMAP: driver supports MMAP with streaming API |
| 84 | * @VB2_USERPTR: driver supports USERPTR with streaming API |
| 85 | * @VB2_READ: driver supports read() style access |
| 86 | * @VB2_WRITE: driver supports write() style access |
| 87 | */ |
| 88 | enum vb2_io_modes { |
| 89 | VB2_MMAP = (1 << 0), |
| 90 | VB2_USERPTR = (1 << 1), |
| 91 | VB2_READ = (1 << 2), |
| 92 | VB2_WRITE = (1 << 3), |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator, |
| 97 | * by default the 'streaming' style is used by the file io emulator |
| 98 | * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer |
| 99 | * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call |
| 100 | */ |
| 101 | enum vb2_fileio_flags { |
| 102 | VB2_FILEIO_READ_ONCE = (1 << 0), |
| 103 | VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1), |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * enum vb2_buffer_state - current video buffer state |
| 108 | * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control |
| 109 | * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver |
| 110 | * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used |
| 111 | * in a hardware operation |
| 112 | * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but |
| 113 | * not yet dequeued to userspace |
| 114 | * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer |
| 115 | * has ended with an error, which will be reported |
| 116 | * to the userspace when it is dequeued |
| 117 | */ |
| 118 | enum vb2_buffer_state { |
| 119 | VB2_BUF_STATE_DEQUEUED, |
| 120 | VB2_BUF_STATE_QUEUED, |
| 121 | VB2_BUF_STATE_ACTIVE, |
| 122 | VB2_BUF_STATE_DONE, |
| 123 | VB2_BUF_STATE_ERROR, |
| 124 | }; |
| 125 | |
| 126 | struct vb2_queue; |
| 127 | |
| 128 | /** |
| 129 | * struct vb2_buffer - represents a video buffer |
| 130 | * @v4l2_buf: struct v4l2_buffer associated with this buffer; can |
| 131 | * be read by the driver and relevant entries can be |
| 132 | * changed by the driver in case of CAPTURE types |
| 133 | * (such as timestamp) |
| 134 | * @v4l2_planes: struct v4l2_planes associated with this buffer; can |
| 135 | * be read by the driver and relevant entries can be |
| 136 | * changed by the driver in case of CAPTURE types |
| 137 | * (such as bytesused); NOTE that even for single-planar |
| 138 | * types, the v4l2_planes[0] struct should be used |
| 139 | * instead of v4l2_buf for filling bytesused - drivers |
| 140 | * should use the vb2_set_plane_payload() function for that |
| 141 | * @vb2_queue: the queue to which this driver belongs |
| 142 | * @num_planes: number of planes in the buffer |
| 143 | * on an internal driver queue |
| 144 | * @state: current buffer state; do not change |
| 145 | * @queued_entry: entry on the queued buffers list, which holds all |
| 146 | * buffers queued from userspace |
| 147 | * @done_entry: entry on the list that stores all buffers ready to |
| 148 | * be dequeued to userspace |
| 149 | * @planes: private per-plane information; do not change |
| 150 | * @num_planes_mapped: number of mapped planes; do not change |
| 151 | */ |
| 152 | struct vb2_buffer { |
| 153 | struct v4l2_buffer v4l2_buf; |
| 154 | struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES]; |
| 155 | |
| 156 | struct vb2_queue *vb2_queue; |
| 157 | |
| 158 | unsigned int num_planes; |
| 159 | |
| 160 | /* Private: internal use only */ |
| 161 | enum vb2_buffer_state state; |
| 162 | |
| 163 | struct list_head queued_entry; |
| 164 | struct list_head done_entry; |
| 165 | |
| 166 | struct vb2_plane planes[VIDEO_MAX_PLANES]; |
| 167 | unsigned int num_planes_mapped; |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * struct vb2_ops - driver-specific callbacks |
| 172 | * |
| 173 | * @queue_setup: called from a VIDIOC_REQBUFS handler, before |
| 174 | * memory allocation; driver should return the required |
| 175 | * number of buffers in num_buffers, the required number |
| 176 | * of planes per buffer in num_planes; the size of each |
| 177 | * plane should be set in the sizes[] array and optional |
| 178 | * per-plane allocator specific context in alloc_ctxs[] |
| 179 | * array |
| 180 | * @wait_prepare: release any locks taken while calling vb2 functions; |
| 181 | * it is called before an ioctl needs to wait for a new |
| 182 | * buffer to arrive; required to avoid a deadlock in |
| 183 | * blocking access type |
| 184 | * @wait_finish: reacquire all locks released in the previous callback; |
| 185 | * required to continue operation after sleeping while |
| 186 | * waiting for a new buffer to arrive |
| 187 | * @buf_init: called once after allocating a buffer (in MMAP case) |
| 188 | * or after acquiring a new USERPTR buffer; drivers may |
| 189 | * perform additional buffer-related initialization; |
| 190 | * initialization failure (return != 0) will prevent |
| 191 | * queue setup from completing successfully; optional |
| 192 | * @buf_prepare: called every time the buffer is queued from userspace; |
| 193 | * drivers may perform any initialization required before |
| 194 | * each hardware operation in this callback; |
| 195 | * if an error is returned, the buffer will not be queued |
| 196 | * in driver; optional |
| 197 | * @buf_finish: called before every dequeue of the buffer back to |
| 198 | * userspace; drivers may perform any operations required |
| 199 | * before userspace accesses the buffer; optional |
| 200 | * @buf_cleanup: called once before the buffer is freed; drivers may |
| 201 | * perform any additional cleanup; optional |
| 202 | * @start_streaming: called once before entering 'streaming' state; enables |
| 203 | * driver to receive buffers over buf_queue() callback |
| 204 | * @stop_streaming: called when 'streaming' state must be disabled; driver |
| 205 | * should stop any DMA transactions or wait until they |
| 206 | * finish and give back all buffers it got from buf_queue() |
| 207 | * callback; may use vb2_wait_for_all_buffers() function |
| 208 | * @buf_queue: passes buffer vb to the driver; driver may start |
| 209 | * hardware operation on this buffer; driver should give |
| 210 | * the buffer back by calling vb2_buffer_done() function |
| 211 | */ |
| 212 | struct vb2_ops { |
| 213 | int (*queue_setup)(struct vb2_queue *q, unsigned int *num_buffers, |
| 214 | unsigned int *num_planes, unsigned long sizes[], |
| 215 | void *alloc_ctxs[]); |
| 216 | |
| 217 | void (*wait_prepare)(struct vb2_queue *q); |
| 218 | void (*wait_finish)(struct vb2_queue *q); |
| 219 | |
| 220 | int (*buf_init)(struct vb2_buffer *vb); |
| 221 | int (*buf_prepare)(struct vb2_buffer *vb); |
| 222 | int (*buf_finish)(struct vb2_buffer *vb); |
| 223 | void (*buf_cleanup)(struct vb2_buffer *vb); |
| 224 | |
| 225 | int (*start_streaming)(struct vb2_queue *q); |
| 226 | int (*stop_streaming)(struct vb2_queue *q); |
| 227 | |
| 228 | void (*buf_queue)(struct vb2_buffer *vb); |
| 229 | }; |
| 230 | |
| 231 | /** |
| 232 | * struct vb2_queue - a videobuf queue |
| 233 | * |
| 234 | * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h |
| 235 | * @io_modes: supported io methods (see vb2_io_modes enum) |
| 236 | * @io_flags: additional io flags (see vb2_fileio_flags enum) |
| 237 | * @ops: driver-specific callbacks |
| 238 | * @mem_ops: memory allocator specific callbacks |
| 239 | * @drv_priv: driver private data |
| 240 | * @buf_struct_size: size of the driver-specific buffer structure; |
| 241 | * "0" indicates the driver doesn't want to use a custom buffer |
| 242 | * structure type, so sizeof(struct vb2_buffer) will is used |
| 243 | * |
| 244 | * @memory: current memory type used |
| 245 | * @bufs: videobuf buffer structures |
| 246 | * @num_buffers: number of allocated/used buffers |
| 247 | * @queued_list: list of buffers currently queued from userspace |
| 248 | * @queued_count: number of buffers owned by the driver |
| 249 | * @done_list: list of buffers ready to be dequeued to userspace |
| 250 | * @done_lock: lock to protect done_list list |
| 251 | * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued |
| 252 | * @alloc_ctx: memory type/allocator-specific contexts for each plane |
| 253 | * @streaming: current streaming state |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 254 | * @fileio: file io emulator internal data, used only if emulator is active |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 255 | */ |
| 256 | struct vb2_queue { |
| 257 | enum v4l2_buf_type type; |
| 258 | unsigned int io_modes; |
| 259 | unsigned int io_flags; |
| 260 | |
| 261 | const struct vb2_ops *ops; |
| 262 | const struct vb2_mem_ops *mem_ops; |
| 263 | void *drv_priv; |
| 264 | unsigned int buf_struct_size; |
| 265 | |
| 266 | /* private: internal use only */ |
| 267 | enum v4l2_memory memory; |
| 268 | struct vb2_buffer *bufs[VIDEO_MAX_FRAME]; |
| 269 | unsigned int num_buffers; |
| 270 | |
| 271 | struct list_head queued_list; |
| 272 | |
| 273 | atomic_t queued_count; |
| 274 | struct list_head done_list; |
| 275 | spinlock_t done_lock; |
| 276 | wait_queue_head_t done_wq; |
| 277 | |
| 278 | void *alloc_ctx[VIDEO_MAX_PLANES]; |
| 279 | |
| 280 | unsigned int streaming:1; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 281 | |
| 282 | struct vb2_fileio_data *fileio; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no); |
| 286 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no); |
| 287 | |
| 288 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state); |
| 289 | int vb2_wait_for_all_buffers(struct vb2_queue *q); |
| 290 | |
| 291 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b); |
| 292 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req); |
| 293 | |
| 294 | int vb2_queue_init(struct vb2_queue *q); |
| 295 | |
| 296 | void vb2_queue_release(struct vb2_queue *q); |
| 297 | |
| 298 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b); |
| 299 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking); |
| 300 | |
| 301 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type); |
| 302 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type); |
| 303 | |
| 304 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma); |
| 305 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait); |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 306 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, |
| 307 | loff_t *ppos, int nonblock); |
| 308 | size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count, |
| 309 | loff_t *ppos, int nonblock); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * vb2_is_streaming() - return streaming status of the queue |
| 313 | * @q: videobuf queue |
| 314 | */ |
| 315 | static inline bool vb2_is_streaming(struct vb2_queue *q) |
| 316 | { |
| 317 | return q->streaming; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * vb2_is_busy() - return busy status of the queue |
| 322 | * @q: videobuf queue |
| 323 | * |
| 324 | * This function checks if queue has any buffers allocated. |
| 325 | */ |
| 326 | static inline bool vb2_is_busy(struct vb2_queue *q) |
| 327 | { |
| 328 | return (q->num_buffers > 0); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * vb2_get_drv_priv() - return driver private data associated with the queue |
| 333 | * @q: videobuf queue |
| 334 | */ |
| 335 | static inline void *vb2_get_drv_priv(struct vb2_queue *q) |
| 336 | { |
| 337 | return q->drv_priv; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * vb2_set_plane_payload() - set bytesused for the plane plane_no |
| 342 | * @vb: buffer for which plane payload should be set |
| 343 | * @plane_no: plane number for which payload should be set |
| 344 | * @size: payload in bytes |
| 345 | */ |
| 346 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb, |
| 347 | unsigned int plane_no, unsigned long size) |
| 348 | { |
| 349 | if (plane_no < vb->num_planes) |
| 350 | vb->v4l2_planes[plane_no].bytesused = size; |
| 351 | } |
| 352 | |
| 353 | /** |
Laurent Pinchart | 9f00eda | 2011-02-27 14:38:19 -0300 | [diff] [blame] | 354 | * vb2_get_plane_payload() - get bytesused for the plane plane_no |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 355 | * @vb: buffer for which plane payload should be set |
| 356 | * @plane_no: plane number for which payload should be set |
| 357 | * @size: payload in bytes |
| 358 | */ |
| 359 | static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb, |
| 360 | unsigned int plane_no) |
| 361 | { |
| 362 | if (plane_no < vb->num_planes) |
| 363 | return vb->v4l2_planes[plane_no].bytesused; |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * vb2_plane_size() - return plane size in bytes |
| 369 | * @vb: buffer for which plane size should be returned |
| 370 | * @plane_no: plane number for which size should be returned |
| 371 | */ |
| 372 | static inline unsigned long |
| 373 | vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no) |
| 374 | { |
| 375 | if (plane_no < vb->num_planes) |
| 376 | return vb->v4l2_planes[plane_no].length; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | #endif /* _MEDIA_VIDEOBUF2_CORE_H */ |