Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | * videobuf2-core.c - 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 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/sched.h> |
| 21 | |
| 22 | #include <media/videobuf2-core.h> |
| 23 | |
| 24 | static int debug; |
| 25 | module_param(debug, int, 0644); |
| 26 | |
| 27 | #define dprintk(level, fmt, arg...) \ |
| 28 | do { \ |
| 29 | if (debug >= level) \ |
| 30 | printk(KERN_DEBUG "vb2: " fmt, ## arg); \ |
| 31 | } while (0) |
| 32 | |
| 33 | #define call_memop(q, plane, op, args...) \ |
| 34 | (((q)->mem_ops->op) ? \ |
| 35 | ((q)->mem_ops->op(args)) : 0) |
| 36 | |
| 37 | #define call_qop(q, op, args...) \ |
| 38 | (((q)->ops->op) ? ((q)->ops->op(args)) : 0) |
| 39 | |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 40 | #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 41 | V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \ |
| 42 | V4L2_BUF_FLAG_PREPARED) |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 43 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 44 | /** |
| 45 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer |
| 46 | */ |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 47 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 48 | { |
| 49 | struct vb2_queue *q = vb->vb2_queue; |
| 50 | void *mem_priv; |
| 51 | int plane; |
| 52 | |
| 53 | /* Allocate memory for all planes in this buffer */ |
| 54 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 55 | mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane], |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 56 | q->plane_sizes[plane]); |
Guennadi Liakhovetski | 62a7943 | 2011-03-22 09:24:58 -0300 | [diff] [blame] | 57 | if (IS_ERR_OR_NULL(mem_priv)) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 58 | goto free; |
| 59 | |
| 60 | /* Associate allocator private data with this plane */ |
| 61 | vb->planes[plane].mem_priv = mem_priv; |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 62 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | free: |
| 67 | /* Free already allocated memory if one of the allocations failed */ |
| 68 | for (; plane > 0; --plane) |
| 69 | call_memop(q, plane, put, vb->planes[plane - 1].mem_priv); |
| 70 | |
| 71 | return -ENOMEM; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * __vb2_buf_mem_free() - free memory of the given buffer |
| 76 | */ |
| 77 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) |
| 78 | { |
| 79 | struct vb2_queue *q = vb->vb2_queue; |
| 80 | unsigned int plane; |
| 81 | |
| 82 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 83 | call_memop(q, plane, put, vb->planes[plane].mem_priv); |
| 84 | vb->planes[plane].mem_priv = NULL; |
| 85 | dprintk(3, "Freed plane %d of buffer %d\n", |
| 86 | plane, vb->v4l2_buf.index); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * __vb2_buf_userptr_put() - release userspace memory associated with |
| 92 | * a USERPTR buffer |
| 93 | */ |
| 94 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) |
| 95 | { |
| 96 | struct vb2_queue *q = vb->vb2_queue; |
| 97 | unsigned int plane; |
| 98 | |
| 99 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 100 | void *mem_priv = vb->planes[plane].mem_priv; |
| 101 | |
| 102 | if (mem_priv) { |
| 103 | call_memop(q, plane, put_userptr, mem_priv); |
| 104 | vb->planes[plane].mem_priv = NULL; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in |
| 111 | * every buffer on the queue |
| 112 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 113 | static void __setup_offsets(struct vb2_queue *q, unsigned int n) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 114 | { |
| 115 | unsigned int buffer, plane; |
| 116 | struct vb2_buffer *vb; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 117 | unsigned long off; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 118 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 119 | if (q->num_buffers) { |
| 120 | struct v4l2_plane *p; |
| 121 | vb = q->bufs[q->num_buffers - 1]; |
| 122 | p = &vb->v4l2_planes[vb->num_planes - 1]; |
| 123 | off = PAGE_ALIGN(p->m.mem_offset + p->length); |
| 124 | } else { |
| 125 | off = 0; |
| 126 | } |
| 127 | |
| 128 | for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 129 | vb = q->bufs[buffer]; |
| 130 | if (!vb) |
| 131 | continue; |
| 132 | |
| 133 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Marek Szyprowski | 4907602 | 2011-10-13 07:07:24 -0300 | [diff] [blame] | 134 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 135 | vb->v4l2_planes[plane].m.mem_offset = off; |
| 136 | |
| 137 | dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n", |
| 138 | buffer, plane, off); |
| 139 | |
| 140 | off += vb->v4l2_planes[plane].length; |
| 141 | off = PAGE_ALIGN(off); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) |
| 148 | * video buffer memory for all buffers/planes on the queue and initializes the |
| 149 | * queue |
| 150 | * |
| 151 | * Returns the number of buffers successfully allocated. |
| 152 | */ |
| 153 | static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory, |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 154 | unsigned int num_buffers, unsigned int num_planes) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 155 | { |
| 156 | unsigned int buffer; |
| 157 | struct vb2_buffer *vb; |
| 158 | int ret; |
| 159 | |
| 160 | for (buffer = 0; buffer < num_buffers; ++buffer) { |
| 161 | /* Allocate videobuf buffer structures */ |
| 162 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); |
| 163 | if (!vb) { |
| 164 | dprintk(1, "Memory alloc for buffer struct failed\n"); |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | /* Length stores number of planes for multiplanar buffers */ |
| 169 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) |
| 170 | vb->v4l2_buf.length = num_planes; |
| 171 | |
| 172 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 173 | vb->vb2_queue = q; |
| 174 | vb->num_planes = num_planes; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 175 | vb->v4l2_buf.index = q->num_buffers + buffer; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 176 | vb->v4l2_buf.type = q->type; |
| 177 | vb->v4l2_buf.memory = memory; |
| 178 | |
| 179 | /* Allocate video buffer memory for the MMAP type */ |
| 180 | if (memory == V4L2_MEMORY_MMAP) { |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 181 | ret = __vb2_buf_mem_alloc(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 182 | if (ret) { |
| 183 | dprintk(1, "Failed allocating memory for " |
| 184 | "buffer %d\n", buffer); |
| 185 | kfree(vb); |
| 186 | break; |
| 187 | } |
| 188 | /* |
| 189 | * Call the driver-provided buffer initialization |
| 190 | * callback, if given. An error in initialization |
| 191 | * results in queue setup failure. |
| 192 | */ |
| 193 | ret = call_qop(q, buf_init, vb); |
| 194 | if (ret) { |
| 195 | dprintk(1, "Buffer %d %p initialization" |
| 196 | " failed\n", buffer, vb); |
| 197 | __vb2_buf_mem_free(vb); |
| 198 | kfree(vb); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 203 | q->bufs[q->num_buffers + buffer] = vb; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 204 | } |
| 205 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 206 | __setup_offsets(q, buffer); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 207 | |
| 208 | dprintk(1, "Allocated %d buffers, %d plane(s) each\n", |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 209 | buffer, num_planes); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 210 | |
| 211 | return buffer; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * __vb2_free_mem() - release all video buffer memory for a given queue |
| 216 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 217 | static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 218 | { |
| 219 | unsigned int buffer; |
| 220 | struct vb2_buffer *vb; |
| 221 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 222 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 223 | ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 224 | vb = q->bufs[buffer]; |
| 225 | if (!vb) |
| 226 | continue; |
| 227 | |
| 228 | /* Free MMAP buffers or release USERPTR buffers */ |
| 229 | if (q->memory == V4L2_MEMORY_MMAP) |
| 230 | __vb2_buf_mem_free(vb); |
| 231 | else |
| 232 | __vb2_buf_userptr_put(vb); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 237 | * __vb2_queue_free() - free buffers at the end of the queue - video memory and |
| 238 | * related information, if no buffers are left return the queue to an |
| 239 | * uninitialized state. Might be called even if the queue has already been freed. |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 240 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 241 | static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 242 | { |
| 243 | unsigned int buffer; |
| 244 | |
| 245 | /* Call driver-provided cleanup function for each buffer, if provided */ |
| 246 | if (q->ops->buf_cleanup) { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 247 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 248 | ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 249 | if (NULL == q->bufs[buffer]) |
| 250 | continue; |
| 251 | q->ops->buf_cleanup(q->bufs[buffer]); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* Release video buffer memory */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 256 | __vb2_free_mem(q, buffers); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 257 | |
| 258 | /* Free videobuf buffers */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 259 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 260 | ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 261 | kfree(q->bufs[buffer]); |
| 262 | q->bufs[buffer] = NULL; |
| 263 | } |
| 264 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 265 | q->num_buffers -= buffers; |
| 266 | if (!q->num_buffers) |
| 267 | q->memory = 0; |
Marek Szyprowski | bd50d99 | 2011-10-25 03:07:59 -0300 | [diff] [blame] | 268 | INIT_LIST_HEAD(&q->queued_list); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * __verify_planes_array() - verify that the planes array passed in struct |
| 273 | * v4l2_buffer from userspace can be safely used |
| 274 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 275 | static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 276 | { |
| 277 | /* Is memory for copying plane information present? */ |
| 278 | if (NULL == b->m.planes) { |
| 279 | dprintk(1, "Multi-planar buffer passed but " |
| 280 | "planes array not provided\n"); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
| 284 | if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) { |
| 285 | dprintk(1, "Incorrect planes array length, " |
| 286 | "expected %d, got %d\n", vb->num_planes, b->length); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /** |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 294 | * __buffer_in_use() - return true if the buffer is in use and |
| 295 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 296 | */ |
| 297 | static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) |
| 298 | { |
| 299 | unsigned int plane; |
| 300 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Marek Szyprowski | 2c2dd6ac | 2011-10-12 13:09:53 -0300 | [diff] [blame] | 301 | void *mem_priv = vb->planes[plane].mem_priv; |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 302 | /* |
| 303 | * If num_users() has not been provided, call_memop |
| 304 | * will return 0, apparently nobody cares about this |
| 305 | * case anyway. If num_users() returns more than 1, |
| 306 | * we are not the only user of the plane's memory. |
| 307 | */ |
Marek Szyprowski | 2c2dd6ac | 2011-10-12 13:09:53 -0300 | [diff] [blame] | 308 | if (mem_priv && call_memop(q, plane, num_users, mem_priv) > 1) |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * __buffers_in_use() - return true if any buffers on the queue are in use and |
| 316 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 317 | */ |
| 318 | static bool __buffers_in_use(struct vb2_queue *q) |
| 319 | { |
| 320 | unsigned int buffer; |
| 321 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 322 | if (__buffer_in_use(q, q->bufs[buffer])) |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 329 | * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be |
| 330 | * returned to userspace |
| 331 | */ |
| 332 | static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 333 | { |
| 334 | struct vb2_queue *q = vb->vb2_queue; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 335 | int ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 336 | |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 337 | /* Copy back data such as timestamp, flags, input, etc. */ |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 338 | memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m)); |
| 339 | b->input = vb->v4l2_buf.input; |
| 340 | b->reserved = vb->v4l2_buf.reserved; |
| 341 | |
| 342 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) { |
| 343 | ret = __verify_planes_array(vb, b); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | /* |
| 348 | * Fill in plane-related data if userspace provided an array |
| 349 | * for it. The memory and size is verified above. |
| 350 | */ |
| 351 | memcpy(b->m.planes, vb->v4l2_planes, |
| 352 | b->length * sizeof(struct v4l2_plane)); |
| 353 | } else { |
| 354 | /* |
| 355 | * We use length and offset in v4l2_planes array even for |
| 356 | * single-planar buffers, but userspace does not. |
| 357 | */ |
| 358 | b->length = vb->v4l2_planes[0].length; |
| 359 | b->bytesused = vb->v4l2_planes[0].bytesused; |
| 360 | if (q->memory == V4L2_MEMORY_MMAP) |
| 361 | b->m.offset = vb->v4l2_planes[0].m.mem_offset; |
| 362 | else if (q->memory == V4L2_MEMORY_USERPTR) |
| 363 | b->m.userptr = vb->v4l2_planes[0].m.userptr; |
| 364 | } |
| 365 | |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 366 | /* |
| 367 | * Clear any buffer state related flags. |
| 368 | */ |
| 369 | b->flags &= ~V4L2_BUFFER_STATE_FLAGS; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 370 | |
| 371 | switch (vb->state) { |
| 372 | case VB2_BUF_STATE_QUEUED: |
| 373 | case VB2_BUF_STATE_ACTIVE: |
| 374 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 375 | break; |
| 376 | case VB2_BUF_STATE_ERROR: |
| 377 | b->flags |= V4L2_BUF_FLAG_ERROR; |
| 378 | /* fall through */ |
| 379 | case VB2_BUF_STATE_DONE: |
| 380 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 381 | break; |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 382 | case VB2_BUF_STATE_PREPARED: |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 383 | b->flags |= V4L2_BUF_FLAG_PREPARED; |
| 384 | break; |
| 385 | case VB2_BUF_STATE_DEQUEUED: |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 386 | /* nothing */ |
| 387 | break; |
| 388 | } |
| 389 | |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 390 | if (__buffer_in_use(q, vb)) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 391 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 392 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 393 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
| 397 | * vb2_querybuf() - query video buffer information |
| 398 | * @q: videobuf queue |
| 399 | * @b: buffer struct passed from userspace to vidioc_querybuf handler |
| 400 | * in driver |
| 401 | * |
| 402 | * Should be called from vidioc_querybuf ioctl handler in driver. |
| 403 | * This function will verify the passed v4l2_buffer structure and fill the |
| 404 | * relevant information for the userspace. |
| 405 | * |
| 406 | * The return values from this function are intended to be directly returned |
| 407 | * from vidioc_querybuf handler in driver. |
| 408 | */ |
| 409 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 410 | { |
| 411 | struct vb2_buffer *vb; |
| 412 | |
| 413 | if (b->type != q->type) { |
| 414 | dprintk(1, "querybuf: wrong buffer type\n"); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | if (b->index >= q->num_buffers) { |
| 419 | dprintk(1, "querybuf: buffer index out of range\n"); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | vb = q->bufs[b->index]; |
| 423 | |
| 424 | return __fill_v4l2_buffer(vb, b); |
| 425 | } |
| 426 | EXPORT_SYMBOL(vb2_querybuf); |
| 427 | |
| 428 | /** |
| 429 | * __verify_userptr_ops() - verify that all memory operations required for |
| 430 | * USERPTR queue type have been provided |
| 431 | */ |
| 432 | static int __verify_userptr_ops(struct vb2_queue *q) |
| 433 | { |
| 434 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || |
| 435 | !q->mem_ops->put_userptr) |
| 436 | return -EINVAL; |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * __verify_mmap_ops() - verify that all memory operations required for |
| 443 | * MMAP queue type have been provided |
| 444 | */ |
| 445 | static int __verify_mmap_ops(struct vb2_queue *q) |
| 446 | { |
| 447 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || |
| 448 | !q->mem_ops->put || !q->mem_ops->mmap) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 455 | * vb2_reqbufs() - Initiate streaming |
| 456 | * @q: videobuf2 queue |
| 457 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver |
| 458 | * |
| 459 | * Should be called from vidioc_reqbufs ioctl handler of a driver. |
| 460 | * This function: |
| 461 | * 1) verifies streaming parameters passed from the userspace, |
| 462 | * 2) sets up the queue, |
| 463 | * 3) negotiates number of buffers and planes per buffer with the driver |
| 464 | * to be used during streaming, |
| 465 | * 4) allocates internal buffer structures (struct vb2_buffer), according to |
| 466 | * the agreed parameters, |
| 467 | * 5) for MMAP memory type, allocates actual video memory, using the |
| 468 | * memory handling/allocation routines provided during queue initialization |
| 469 | * |
| 470 | * If req->count is 0, all the memory will be freed instead. |
| 471 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call |
| 472 | * and the queue is not busy, memory will be reallocated. |
| 473 | * |
| 474 | * The return values from this function are intended to be directly returned |
| 475 | * from vidioc_reqbufs handler in driver. |
| 476 | */ |
| 477 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) |
| 478 | { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 479 | unsigned int num_buffers, allocated_buffers, num_planes = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 480 | int ret = 0; |
| 481 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 482 | if (q->fileio) { |
| 483 | dprintk(1, "reqbufs: file io in progress\n"); |
| 484 | return -EBUSY; |
| 485 | } |
| 486 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 487 | if (req->memory != V4L2_MEMORY_MMAP |
| 488 | && req->memory != V4L2_MEMORY_USERPTR) { |
| 489 | dprintk(1, "reqbufs: unsupported memory type\n"); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | if (req->type != q->type) { |
| 494 | dprintk(1, "reqbufs: requested type is incorrect\n"); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | if (q->streaming) { |
| 499 | dprintk(1, "reqbufs: streaming active\n"); |
| 500 | return -EBUSY; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Make sure all the required memory ops for given memory type |
| 505 | * are available. |
| 506 | */ |
| 507 | if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { |
| 508 | dprintk(1, "reqbufs: MMAP for current setup unsupported\n"); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { |
| 513 | dprintk(1, "reqbufs: USERPTR for current setup unsupported\n"); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
Marek Szyprowski | 29e3fbd | 2011-03-09 14:03:24 -0300 | [diff] [blame] | 517 | if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 518 | /* |
| 519 | * We already have buffers allocated, so first check if they |
| 520 | * are not in use and can be freed. |
| 521 | */ |
| 522 | if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { |
| 523 | dprintk(1, "reqbufs: memory in use, cannot free\n"); |
| 524 | return -EBUSY; |
| 525 | } |
| 526 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 527 | __vb2_queue_free(q, q->num_buffers); |
Marek Szyprowski | 29e3fbd | 2011-03-09 14:03:24 -0300 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * In case of REQBUFS(0) return immediately without calling |
| 531 | * driver's queue_setup() callback and allocating resources. |
| 532 | */ |
| 533 | if (req->count == 0) |
| 534 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Make sure the requested values and current defaults are sane. |
| 539 | */ |
| 540 | num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 541 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 542 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
Marek Szyprowski | 13b1409 | 2011-04-14 07:17:44 -0300 | [diff] [blame] | 543 | q->memory = req->memory; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Ask the driver how many buffers and planes per buffer it requires. |
| 547 | * Driver also sets the size and allocator context for each plane. |
| 548 | */ |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 549 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes, |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 550 | q->plane_sizes, q->alloc_ctx); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 551 | if (ret) |
| 552 | return ret; |
| 553 | |
| 554 | /* Finally, allocate buffers and video memory */ |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 555 | ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes); |
Marek Szyprowski | 66072d4 | 2011-06-28 08:29:02 -0300 | [diff] [blame] | 556 | if (ret == 0) { |
| 557 | dprintk(1, "Memory allocation failed\n"); |
| 558 | return -ENOMEM; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 559 | } |
| 560 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 561 | allocated_buffers = ret; |
| 562 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 563 | /* |
| 564 | * Check if driver can handle the allocated number of buffers. |
| 565 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 566 | if (allocated_buffers < num_buffers) { |
| 567 | num_buffers = allocated_buffers; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 568 | |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 569 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, |
| 570 | &num_planes, q->plane_sizes, q->alloc_ctx); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 571 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 572 | if (!ret && allocated_buffers < num_buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 573 | ret = -ENOMEM; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 574 | |
| 575 | /* |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 576 | * Either the driver has accepted a smaller number of buffers, |
| 577 | * or .queue_setup() returned an error |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 578 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | q->num_buffers = allocated_buffers; |
| 582 | |
| 583 | if (ret < 0) { |
| 584 | __vb2_queue_free(q, allocated_buffers); |
| 585 | return ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 586 | } |
| 587 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 588 | /* |
| 589 | * Return the number of successfully allocated buffers |
| 590 | * to the userspace. |
| 591 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 592 | req->count = allocated_buffers; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 593 | |
| 594 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 595 | } |
| 596 | EXPORT_SYMBOL_GPL(vb2_reqbufs); |
| 597 | |
| 598 | /** |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 599 | * vb2_create_bufs() - Allocate buffers and any required auxiliary structs |
| 600 | * @q: videobuf2 queue |
| 601 | * @create: creation parameters, passed from userspace to vidioc_create_bufs |
| 602 | * handler in driver |
| 603 | * |
| 604 | * Should be called from vidioc_create_bufs ioctl handler of a driver. |
| 605 | * This function: |
| 606 | * 1) verifies parameter sanity |
| 607 | * 2) calls the .queue_setup() queue operation |
| 608 | * 3) performs any necessary memory allocations |
| 609 | * |
| 610 | * The return values from this function are intended to be directly returned |
| 611 | * from vidioc_create_bufs handler in driver. |
| 612 | */ |
| 613 | int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) |
| 614 | { |
| 615 | unsigned int num_planes = 0, num_buffers, allocated_buffers; |
| 616 | int ret = 0; |
| 617 | |
| 618 | if (q->fileio) { |
| 619 | dprintk(1, "%s(): file io in progress\n", __func__); |
| 620 | return -EBUSY; |
| 621 | } |
| 622 | |
| 623 | if (create->memory != V4L2_MEMORY_MMAP |
| 624 | && create->memory != V4L2_MEMORY_USERPTR) { |
| 625 | dprintk(1, "%s(): unsupported memory type\n", __func__); |
| 626 | return -EINVAL; |
| 627 | } |
| 628 | |
| 629 | if (create->format.type != q->type) { |
| 630 | dprintk(1, "%s(): requested type is incorrect\n", __func__); |
| 631 | return -EINVAL; |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Make sure all the required memory ops for given memory type |
| 636 | * are available. |
| 637 | */ |
| 638 | if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { |
| 639 | dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { |
| 644 | dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__); |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | if (q->num_buffers == VIDEO_MAX_FRAME) { |
| 649 | dprintk(1, "%s(): maximum number of buffers already allocated\n", |
| 650 | __func__); |
| 651 | return -ENOBUFS; |
| 652 | } |
| 653 | |
| 654 | create->index = q->num_buffers; |
| 655 | |
| 656 | if (!q->num_buffers) { |
| 657 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); |
| 658 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
| 659 | q->memory = create->memory; |
| 660 | } |
| 661 | |
| 662 | num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers); |
| 663 | |
| 664 | /* |
| 665 | * Ask the driver, whether the requested number of buffers, planes per |
| 666 | * buffer and their sizes are acceptable |
| 667 | */ |
| 668 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, |
| 669 | &num_planes, q->plane_sizes, q->alloc_ctx); |
| 670 | if (ret) |
| 671 | return ret; |
| 672 | |
| 673 | /* Finally, allocate buffers and video memory */ |
| 674 | ret = __vb2_queue_alloc(q, create->memory, num_buffers, |
| 675 | num_planes); |
| 676 | if (ret < 0) { |
| 677 | dprintk(1, "Memory allocation failed with error: %d\n", ret); |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | allocated_buffers = ret; |
| 682 | |
| 683 | /* |
| 684 | * Check if driver can handle the so far allocated number of buffers. |
| 685 | */ |
| 686 | if (ret < num_buffers) { |
| 687 | num_buffers = ret; |
| 688 | |
| 689 | /* |
| 690 | * q->num_buffers contains the total number of buffers, that the |
| 691 | * queue driver has set up |
| 692 | */ |
| 693 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, |
| 694 | &num_planes, q->plane_sizes, q->alloc_ctx); |
| 695 | |
| 696 | if (!ret && allocated_buffers < num_buffers) |
| 697 | ret = -ENOMEM; |
| 698 | |
| 699 | /* |
| 700 | * Either the driver has accepted a smaller number of buffers, |
| 701 | * or .queue_setup() returned an error |
| 702 | */ |
| 703 | } |
| 704 | |
| 705 | q->num_buffers += allocated_buffers; |
| 706 | |
| 707 | if (ret < 0) { |
| 708 | __vb2_queue_free(q, allocated_buffers); |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Return the number of successfully allocated buffers |
| 714 | * to the userspace. |
| 715 | */ |
| 716 | create->count = allocated_buffers; |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(vb2_create_bufs); |
| 721 | |
| 722 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 723 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane |
| 724 | * @vb: vb2_buffer to which the plane in question belongs to |
| 725 | * @plane_no: plane number for which the address is to be returned |
| 726 | * |
| 727 | * This function returns a kernel virtual address of a given plane if |
| 728 | * such a mapping exist, NULL otherwise. |
| 729 | */ |
| 730 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) |
| 731 | { |
| 732 | struct vb2_queue *q = vb->vb2_queue; |
| 733 | |
| 734 | if (plane_no > vb->num_planes) |
| 735 | return NULL; |
| 736 | |
| 737 | return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv); |
| 738 | |
| 739 | } |
| 740 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); |
| 741 | |
| 742 | /** |
| 743 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane |
| 744 | * @vb: vb2_buffer to which the plane in question belongs to |
| 745 | * @plane_no: plane number for which the cookie is to be returned |
| 746 | * |
| 747 | * This function returns an allocator specific cookie for a given plane if |
| 748 | * available, NULL otherwise. The allocator should provide some simple static |
| 749 | * inline function, which would convert this cookie to the allocator specific |
| 750 | * type that can be used directly by the driver to access the buffer. This can |
| 751 | * be for example physical address, pointer to scatter list or IOMMU mapping. |
| 752 | */ |
| 753 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) |
| 754 | { |
| 755 | struct vb2_queue *q = vb->vb2_queue; |
| 756 | |
| 757 | if (plane_no > vb->num_planes) |
| 758 | return NULL; |
| 759 | |
| 760 | return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv); |
| 761 | } |
| 762 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); |
| 763 | |
| 764 | /** |
| 765 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished |
| 766 | * @vb: vb2_buffer returned from the driver |
| 767 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully |
| 768 | * or VB2_BUF_STATE_ERROR if the operation finished with an error |
| 769 | * |
| 770 | * This function should be called by the driver after a hardware operation on |
| 771 | * a buffer is finished and the buffer may be returned to userspace. The driver |
| 772 | * cannot use this buffer anymore until it is queued back to it by videobuf |
| 773 | * by the means of buf_queue callback. Only buffers previously queued to the |
| 774 | * driver by buf_queue can be passed to this function. |
| 775 | */ |
| 776 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) |
| 777 | { |
| 778 | struct vb2_queue *q = vb->vb2_queue; |
| 779 | unsigned long flags; |
| 780 | |
| 781 | if (vb->state != VB2_BUF_STATE_ACTIVE) |
| 782 | return; |
| 783 | |
| 784 | if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) |
| 785 | return; |
| 786 | |
| 787 | dprintk(4, "Done processing on buffer %d, state: %d\n", |
| 788 | vb->v4l2_buf.index, vb->state); |
| 789 | |
| 790 | /* Add the buffer to the done buffers list */ |
| 791 | spin_lock_irqsave(&q->done_lock, flags); |
| 792 | vb->state = state; |
| 793 | list_add_tail(&vb->done_entry, &q->done_list); |
| 794 | atomic_dec(&q->queued_count); |
| 795 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 796 | |
| 797 | /* Inform any processes that may be waiting for buffers */ |
| 798 | wake_up(&q->done_wq); |
| 799 | } |
| 800 | EXPORT_SYMBOL_GPL(vb2_buffer_done); |
| 801 | |
| 802 | /** |
| 803 | * __fill_vb2_buffer() - fill a vb2_buffer with information provided in |
| 804 | * a v4l2_buffer by the userspace |
| 805 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 806 | static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b, |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 807 | struct v4l2_plane *v4l2_planes) |
| 808 | { |
| 809 | unsigned int plane; |
| 810 | int ret; |
| 811 | |
| 812 | if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { |
| 813 | /* |
| 814 | * Verify that the userspace gave us a valid array for |
| 815 | * plane information. |
| 816 | */ |
| 817 | ret = __verify_planes_array(vb, b); |
| 818 | if (ret) |
| 819 | return ret; |
| 820 | |
| 821 | /* Fill in driver-provided information for OUTPUT types */ |
| 822 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { |
| 823 | /* |
| 824 | * Will have to go up to b->length when API starts |
| 825 | * accepting variable number of planes. |
| 826 | */ |
| 827 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 828 | v4l2_planes[plane].bytesused = |
| 829 | b->m.planes[plane].bytesused; |
| 830 | v4l2_planes[plane].data_offset = |
| 831 | b->m.planes[plane].data_offset; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 836 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 837 | v4l2_planes[plane].m.userptr = |
| 838 | b->m.planes[plane].m.userptr; |
| 839 | v4l2_planes[plane].length = |
| 840 | b->m.planes[plane].length; |
| 841 | } |
| 842 | } |
| 843 | } else { |
| 844 | /* |
| 845 | * Single-planar buffers do not use planes array, |
| 846 | * so fill in relevant v4l2_buffer struct fields instead. |
| 847 | * In videobuf we use our internal V4l2_planes struct for |
| 848 | * single-planar buffers as well, for simplicity. |
| 849 | */ |
| 850 | if (V4L2_TYPE_IS_OUTPUT(b->type)) |
| 851 | v4l2_planes[0].bytesused = b->bytesused; |
| 852 | |
| 853 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 854 | v4l2_planes[0].m.userptr = b->m.userptr; |
| 855 | v4l2_planes[0].length = b->length; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | vb->v4l2_buf.field = b->field; |
| 860 | vb->v4l2_buf.timestamp = b->timestamp; |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 861 | vb->v4l2_buf.input = b->input; |
| 862 | vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer |
| 869 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 870 | static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 871 | { |
| 872 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 873 | struct vb2_queue *q = vb->vb2_queue; |
| 874 | void *mem_priv; |
| 875 | unsigned int plane; |
| 876 | int ret; |
| 877 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); |
| 878 | |
| 879 | /* Verify and copy relevant information provided by the userspace */ |
| 880 | ret = __fill_vb2_buffer(vb, b, planes); |
| 881 | if (ret) |
| 882 | return ret; |
| 883 | |
| 884 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 885 | /* Skip the plane if already verified */ |
Marek Szyprowski | f0b7c7f | 2011-11-16 15:09:40 -0300 | [diff] [blame^] | 886 | if (vb->v4l2_planes[plane].m.userptr && |
| 887 | vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 888 | && vb->v4l2_planes[plane].length == planes[plane].length) |
| 889 | continue; |
| 890 | |
| 891 | dprintk(3, "qbuf: userspace address for plane %d changed, " |
| 892 | "reacquiring memory\n", plane); |
| 893 | |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 894 | /* Check if the provided plane buffer is large enough */ |
| 895 | if (planes[plane].length < q->plane_sizes[plane]) { |
Marek Szyprowski | 4c2625d | 2011-10-03 03:21:45 -0300 | [diff] [blame] | 896 | ret = -EINVAL; |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 897 | goto err; |
| 898 | } |
| 899 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 900 | /* Release previously acquired memory if present */ |
| 901 | if (vb->planes[plane].mem_priv) |
| 902 | call_memop(q, plane, put_userptr, |
| 903 | vb->planes[plane].mem_priv); |
| 904 | |
| 905 | vb->planes[plane].mem_priv = NULL; |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 906 | vb->v4l2_planes[plane].m.userptr = 0; |
| 907 | vb->v4l2_planes[plane].length = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 908 | |
| 909 | /* Acquire each plane's memory */ |
| 910 | if (q->mem_ops->get_userptr) { |
| 911 | mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane], |
| 912 | planes[plane].m.userptr, |
| 913 | planes[plane].length, |
| 914 | write); |
| 915 | if (IS_ERR(mem_priv)) { |
| 916 | dprintk(1, "qbuf: failed acquiring userspace " |
| 917 | "memory for plane %d\n", plane); |
| 918 | ret = PTR_ERR(mem_priv); |
| 919 | goto err; |
| 920 | } |
| 921 | vb->planes[plane].mem_priv = mem_priv; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Call driver-specific initialization on the newly acquired buffer, |
| 927 | * if provided. |
| 928 | */ |
| 929 | ret = call_qop(q, buf_init, vb); |
| 930 | if (ret) { |
| 931 | dprintk(1, "qbuf: buffer initialization failed\n"); |
| 932 | goto err; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Now that everything is in order, copy relevant information |
| 937 | * provided by userspace. |
| 938 | */ |
| 939 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 940 | vb->v4l2_planes[plane] = planes[plane]; |
| 941 | |
| 942 | return 0; |
| 943 | err: |
| 944 | /* In case of errors, release planes that were already acquired */ |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 945 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 946 | if (vb->planes[plane].mem_priv) |
| 947 | call_memop(q, plane, put_userptr, |
| 948 | vb->planes[plane].mem_priv); |
| 949 | vb->planes[plane].mem_priv = NULL; |
| 950 | vb->v4l2_planes[plane].m.userptr = 0; |
| 951 | vb->v4l2_planes[plane].length = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | return ret; |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * __qbuf_mmap() - handle qbuf of an MMAP buffer |
| 959 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 960 | static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 961 | { |
| 962 | return __fill_vb2_buffer(vb, b, vb->v4l2_planes); |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing |
| 967 | */ |
| 968 | static void __enqueue_in_driver(struct vb2_buffer *vb) |
| 969 | { |
| 970 | struct vb2_queue *q = vb->vb2_queue; |
| 971 | |
| 972 | vb->state = VB2_BUF_STATE_ACTIVE; |
| 973 | atomic_inc(&q->queued_count); |
| 974 | q->ops->buf_queue(vb); |
| 975 | } |
| 976 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 977 | static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 978 | { |
| 979 | struct vb2_queue *q = vb->vb2_queue; |
| 980 | int ret; |
| 981 | |
| 982 | switch (q->memory) { |
| 983 | case V4L2_MEMORY_MMAP: |
| 984 | ret = __qbuf_mmap(vb, b); |
| 985 | break; |
| 986 | case V4L2_MEMORY_USERPTR: |
| 987 | ret = __qbuf_userptr(vb, b); |
| 988 | break; |
| 989 | default: |
| 990 | WARN(1, "Invalid queue type\n"); |
| 991 | ret = -EINVAL; |
| 992 | } |
| 993 | |
| 994 | if (!ret) |
| 995 | ret = call_qop(q, buf_prepare, vb); |
| 996 | if (ret) |
| 997 | dprintk(1, "qbuf: buffer preparation failed: %d\n", ret); |
| 998 | else |
| 999 | vb->state = VB2_BUF_STATE_PREPARED; |
| 1000 | |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1004 | /** |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 1005 | * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel |
| 1006 | * @q: videobuf2 queue |
| 1007 | * @b: buffer structure passed from userspace to vidioc_prepare_buf |
| 1008 | * handler in driver |
| 1009 | * |
| 1010 | * Should be called from vidioc_prepare_buf ioctl handler of a driver. |
| 1011 | * This function: |
| 1012 | * 1) verifies the passed buffer, |
| 1013 | * 2) calls buf_prepare callback in the driver (if provided), in which |
| 1014 | * driver-specific buffer initialization can be performed, |
| 1015 | * |
| 1016 | * The return values from this function are intended to be directly returned |
| 1017 | * from vidioc_prepare_buf handler in driver. |
| 1018 | */ |
| 1019 | int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 1020 | { |
| 1021 | struct vb2_buffer *vb; |
| 1022 | int ret; |
| 1023 | |
| 1024 | if (q->fileio) { |
| 1025 | dprintk(1, "%s(): file io in progress\n", __func__); |
| 1026 | return -EBUSY; |
| 1027 | } |
| 1028 | |
| 1029 | if (b->type != q->type) { |
| 1030 | dprintk(1, "%s(): invalid buffer type\n", __func__); |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | |
| 1034 | if (b->index >= q->num_buffers) { |
| 1035 | dprintk(1, "%s(): buffer index out of range\n", __func__); |
| 1036 | return -EINVAL; |
| 1037 | } |
| 1038 | |
| 1039 | vb = q->bufs[b->index]; |
| 1040 | if (NULL == vb) { |
| 1041 | /* Should never happen */ |
| 1042 | dprintk(1, "%s(): buffer is NULL\n", __func__); |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | |
| 1046 | if (b->memory != q->memory) { |
| 1047 | dprintk(1, "%s(): invalid memory type\n", __func__); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { |
| 1052 | dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | ret = __buf_prepare(vb, b); |
| 1057 | if (ret < 0) |
| 1058 | return ret; |
| 1059 | |
| 1060 | __fill_v4l2_buffer(vb, b); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | EXPORT_SYMBOL_GPL(vb2_prepare_buf); |
| 1065 | |
| 1066 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1067 | * vb2_qbuf() - Queue a buffer from userspace |
| 1068 | * @q: videobuf2 queue |
| 1069 | * @b: buffer structure passed from userspace to vidioc_qbuf handler |
| 1070 | * in driver |
| 1071 | * |
| 1072 | * Should be called from vidioc_qbuf ioctl handler of a driver. |
| 1073 | * This function: |
| 1074 | * 1) verifies the passed buffer, |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1075 | * 2) if necessary, calls buf_prepare callback in the driver (if provided), in |
| 1076 | * which driver-specific buffer initialization can be performed, |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1077 | * 3) if streaming is on, queues the buffer in driver by the means of buf_queue |
| 1078 | * callback for processing. |
| 1079 | * |
| 1080 | * The return values from this function are intended to be directly returned |
| 1081 | * from vidioc_qbuf handler in driver. |
| 1082 | */ |
| 1083 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 1084 | { |
| 1085 | struct vb2_buffer *vb; |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1086 | int ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1087 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1088 | if (q->fileio) { |
| 1089 | dprintk(1, "qbuf: file io in progress\n"); |
| 1090 | return -EBUSY; |
| 1091 | } |
| 1092 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1093 | if (b->type != q->type) { |
| 1094 | dprintk(1, "qbuf: invalid buffer type\n"); |
| 1095 | return -EINVAL; |
| 1096 | } |
| 1097 | |
| 1098 | if (b->index >= q->num_buffers) { |
| 1099 | dprintk(1, "qbuf: buffer index out of range\n"); |
| 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
| 1103 | vb = q->bufs[b->index]; |
| 1104 | if (NULL == vb) { |
| 1105 | /* Should never happen */ |
| 1106 | dprintk(1, "qbuf: buffer is NULL\n"); |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
| 1110 | if (b->memory != q->memory) { |
| 1111 | dprintk(1, "qbuf: invalid memory type\n"); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1115 | switch (vb->state) { |
| 1116 | case VB2_BUF_STATE_DEQUEUED: |
| 1117 | ret = __buf_prepare(vb, b); |
| 1118 | if (ret) |
| 1119 | return ret; |
| 1120 | case VB2_BUF_STATE_PREPARED: |
| 1121 | break; |
| 1122 | default: |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1123 | dprintk(1, "qbuf: buffer already in use\n"); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1127 | /* |
| 1128 | * Add to the queued buffers list, a buffer will stay on it until |
| 1129 | * dequeued in dqbuf. |
| 1130 | */ |
| 1131 | list_add_tail(&vb->queued_entry, &q->queued_list); |
| 1132 | vb->state = VB2_BUF_STATE_QUEUED; |
| 1133 | |
| 1134 | /* |
| 1135 | * If already streaming, give the buffer to driver for processing. |
| 1136 | * If not, the buffer will be given to driver on next streamon. |
| 1137 | */ |
| 1138 | if (q->streaming) |
| 1139 | __enqueue_in_driver(vb); |
| 1140 | |
Guennadi Liakhovetski | 21db3e0 | 2011-09-28 07:23:27 -0300 | [diff] [blame] | 1141 | /* Fill buffer information for the userspace */ |
| 1142 | __fill_v4l2_buffer(vb, b); |
| 1143 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1144 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index); |
| 1145 | return 0; |
| 1146 | } |
| 1147 | EXPORT_SYMBOL_GPL(vb2_qbuf); |
| 1148 | |
| 1149 | /** |
| 1150 | * __vb2_wait_for_done_vb() - wait for a buffer to become available |
| 1151 | * for dequeuing |
| 1152 | * |
| 1153 | * Will sleep if required for nonblocking == false. |
| 1154 | */ |
| 1155 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) |
| 1156 | { |
| 1157 | /* |
| 1158 | * All operations on vb_done_list are performed under done_lock |
| 1159 | * spinlock protection. However, buffers may be removed from |
| 1160 | * it and returned to userspace only while holding both driver's |
| 1161 | * lock and the done_lock spinlock. Thus we can be sure that as |
| 1162 | * long as we hold the driver's lock, the list will remain not |
| 1163 | * empty if list_empty() check succeeds. |
| 1164 | */ |
| 1165 | |
| 1166 | for (;;) { |
| 1167 | int ret; |
| 1168 | |
| 1169 | if (!q->streaming) { |
| 1170 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 1171 | return -EINVAL; |
| 1172 | } |
| 1173 | |
| 1174 | if (!list_empty(&q->done_list)) { |
| 1175 | /* |
| 1176 | * Found a buffer that we were waiting for. |
| 1177 | */ |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | if (nonblocking) { |
| 1182 | dprintk(1, "Nonblocking and no buffers to dequeue, " |
| 1183 | "will not wait\n"); |
| 1184 | return -EAGAIN; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * We are streaming and blocking, wait for another buffer to |
| 1189 | * become ready or for streamoff. Driver's lock is released to |
| 1190 | * allow streamoff or qbuf to be called while waiting. |
| 1191 | */ |
| 1192 | call_qop(q, wait_prepare, q); |
| 1193 | |
| 1194 | /* |
| 1195 | * All locks have been released, it is safe to sleep now. |
| 1196 | */ |
| 1197 | dprintk(3, "Will sleep waiting for buffers\n"); |
| 1198 | ret = wait_event_interruptible(q->done_wq, |
| 1199 | !list_empty(&q->done_list) || !q->streaming); |
| 1200 | |
| 1201 | /* |
| 1202 | * We need to reevaluate both conditions again after reacquiring |
| 1203 | * the locks or return an error if one occurred. |
| 1204 | */ |
| 1205 | call_qop(q, wait_finish, q); |
| 1206 | if (ret) |
| 1207 | return ret; |
| 1208 | } |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * __vb2_get_done_vb() - get a buffer ready for dequeuing |
| 1214 | * |
| 1215 | * Will sleep if required for nonblocking == false. |
| 1216 | */ |
| 1217 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, |
| 1218 | int nonblocking) |
| 1219 | { |
| 1220 | unsigned long flags; |
| 1221 | int ret; |
| 1222 | |
| 1223 | /* |
| 1224 | * Wait for at least one buffer to become available on the done_list. |
| 1225 | */ |
| 1226 | ret = __vb2_wait_for_done_vb(q, nonblocking); |
| 1227 | if (ret) |
| 1228 | return ret; |
| 1229 | |
| 1230 | /* |
| 1231 | * Driver's lock has been held since we last verified that done_list |
| 1232 | * is not empty, so no need for another list_empty(done_list) check. |
| 1233 | */ |
| 1234 | spin_lock_irqsave(&q->done_lock, flags); |
| 1235 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); |
| 1236 | list_del(&(*vb)->done_entry); |
| 1237 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 |
| 1244 | * @q: videobuf2 queue |
| 1245 | * |
| 1246 | * This function will wait until all buffers that have been given to the driver |
| 1247 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call |
| 1248 | * wait_prepare, wait_finish pair. It is intended to be called with all locks |
| 1249 | * taken, for example from stop_streaming() callback. |
| 1250 | */ |
| 1251 | int vb2_wait_for_all_buffers(struct vb2_queue *q) |
| 1252 | { |
| 1253 | if (!q->streaming) { |
| 1254 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | |
| 1258 | wait_event(q->done_wq, !atomic_read(&q->queued_count)); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); |
| 1262 | |
| 1263 | /** |
| 1264 | * vb2_dqbuf() - Dequeue a buffer to the userspace |
| 1265 | * @q: videobuf2 queue |
| 1266 | * @b: buffer structure passed from userspace to vidioc_dqbuf handler |
| 1267 | * in driver |
| 1268 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no |
| 1269 | * buffers ready for dequeuing are present. Normally the driver |
| 1270 | * would be passing (file->f_flags & O_NONBLOCK) here |
| 1271 | * |
| 1272 | * Should be called from vidioc_dqbuf ioctl handler of a driver. |
| 1273 | * This function: |
| 1274 | * 1) verifies the passed buffer, |
| 1275 | * 2) calls buf_finish callback in the driver (if provided), in which |
| 1276 | * driver can perform any additional operations that may be required before |
| 1277 | * returning the buffer to userspace, such as cache sync, |
| 1278 | * 3) the buffer struct members are filled with relevant information for |
| 1279 | * the userspace. |
| 1280 | * |
| 1281 | * The return values from this function are intended to be directly returned |
| 1282 | * from vidioc_dqbuf handler in driver. |
| 1283 | */ |
| 1284 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) |
| 1285 | { |
| 1286 | struct vb2_buffer *vb = NULL; |
| 1287 | int ret; |
| 1288 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1289 | if (q->fileio) { |
| 1290 | dprintk(1, "dqbuf: file io in progress\n"); |
| 1291 | return -EBUSY; |
| 1292 | } |
| 1293 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1294 | if (b->type != q->type) { |
| 1295 | dprintk(1, "dqbuf: invalid buffer type\n"); |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
| 1299 | ret = __vb2_get_done_vb(q, &vb, nonblocking); |
| 1300 | if (ret < 0) { |
| 1301 | dprintk(1, "dqbuf: error getting next done buffer\n"); |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | ret = call_qop(q, buf_finish, vb); |
| 1306 | if (ret) { |
| 1307 | dprintk(1, "dqbuf: buffer finish failed\n"); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | switch (vb->state) { |
| 1312 | case VB2_BUF_STATE_DONE: |
| 1313 | dprintk(3, "dqbuf: Returning done buffer\n"); |
| 1314 | break; |
| 1315 | case VB2_BUF_STATE_ERROR: |
| 1316 | dprintk(3, "dqbuf: Returning done buffer with errors\n"); |
| 1317 | break; |
| 1318 | default: |
| 1319 | dprintk(1, "dqbuf: Invalid buffer state\n"); |
| 1320 | return -EINVAL; |
| 1321 | } |
| 1322 | |
| 1323 | /* Fill buffer information for the userspace */ |
| 1324 | __fill_v4l2_buffer(vb, b); |
| 1325 | /* Remove from videobuf queue */ |
| 1326 | list_del(&vb->queued_entry); |
| 1327 | |
| 1328 | dprintk(1, "dqbuf of buffer %d, with state %d\n", |
| 1329 | vb->v4l2_buf.index, vb->state); |
| 1330 | |
| 1331 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 1332 | return 0; |
| 1333 | } |
| 1334 | EXPORT_SYMBOL_GPL(vb2_dqbuf); |
| 1335 | |
| 1336 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1337 | * __vb2_queue_cancel() - cancel and stop (pause) streaming |
| 1338 | * |
| 1339 | * Removes all queued buffers from driver's queue and all buffers queued by |
| 1340 | * userspace from videobuf's queue. Returns to state after reqbufs. |
| 1341 | */ |
| 1342 | static void __vb2_queue_cancel(struct vb2_queue *q) |
| 1343 | { |
| 1344 | unsigned int i; |
| 1345 | |
| 1346 | /* |
| 1347 | * Tell driver to stop all transactions and release all queued |
| 1348 | * buffers. |
| 1349 | */ |
| 1350 | if (q->streaming) |
| 1351 | call_qop(q, stop_streaming, q); |
| 1352 | q->streaming = 0; |
| 1353 | |
| 1354 | /* |
| 1355 | * Remove all buffers from videobuf's list... |
| 1356 | */ |
| 1357 | INIT_LIST_HEAD(&q->queued_list); |
| 1358 | /* |
| 1359 | * ...and done list; userspace will not receive any buffers it |
| 1360 | * has not already dequeued before initiating cancel. |
| 1361 | */ |
| 1362 | INIT_LIST_HEAD(&q->done_list); |
Marek Szyprowski | afdea8b | 2011-06-10 08:58:42 -0300 | [diff] [blame] | 1363 | atomic_set(&q->queued_count, 0); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1364 | wake_up_all(&q->done_wq); |
| 1365 | |
| 1366 | /* |
| 1367 | * Reinitialize all buffers for next use. |
| 1368 | */ |
| 1369 | for (i = 0; i < q->num_buffers; ++i) |
| 1370 | q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED; |
| 1371 | } |
| 1372 | |
| 1373 | /** |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1374 | * vb2_streamon - start streaming |
| 1375 | * @q: videobuf2 queue |
| 1376 | * @type: type argument passed from userspace to vidioc_streamon handler |
| 1377 | * |
| 1378 | * Should be called from vidioc_streamon handler of a driver. |
| 1379 | * This function: |
| 1380 | * 1) verifies current state |
| 1381 | * 2) passes any previously queued buffers to the driver and starts streaming |
| 1382 | * |
| 1383 | * The return values from this function are intended to be directly returned |
| 1384 | * from vidioc_streamon handler in the driver. |
| 1385 | */ |
| 1386 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1387 | { |
| 1388 | struct vb2_buffer *vb; |
| 1389 | int ret; |
| 1390 | |
| 1391 | if (q->fileio) { |
| 1392 | dprintk(1, "streamon: file io in progress\n"); |
| 1393 | return -EBUSY; |
| 1394 | } |
| 1395 | |
| 1396 | if (type != q->type) { |
| 1397 | dprintk(1, "streamon: invalid stream type\n"); |
| 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | |
| 1401 | if (q->streaming) { |
| 1402 | dprintk(1, "streamon: already streaming\n"); |
| 1403 | return -EBUSY; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * If any buffers were queued before streamon, |
| 1408 | * we can now pass them to driver for processing. |
| 1409 | */ |
| 1410 | list_for_each_entry(vb, &q->queued_list, queued_entry) |
| 1411 | __enqueue_in_driver(vb); |
| 1412 | |
| 1413 | /* |
| 1414 | * Let driver notice that streaming state has been enabled. |
| 1415 | */ |
| 1416 | ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count)); |
| 1417 | if (ret) { |
| 1418 | dprintk(1, "streamon: driver refused to start streaming\n"); |
| 1419 | __vb2_queue_cancel(q); |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
| 1423 | q->streaming = 1; |
| 1424 | |
| 1425 | dprintk(3, "Streamon successful\n"); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | EXPORT_SYMBOL_GPL(vb2_streamon); |
| 1429 | |
| 1430 | |
| 1431 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1432 | * vb2_streamoff - stop streaming |
| 1433 | * @q: videobuf2 queue |
| 1434 | * @type: type argument passed from userspace to vidioc_streamoff handler |
| 1435 | * |
| 1436 | * Should be called from vidioc_streamoff handler of a driver. |
| 1437 | * This function: |
| 1438 | * 1) verifies current state, |
| 1439 | * 2) stop streaming and dequeues any queued buffers, including those previously |
| 1440 | * passed to the driver (after waiting for the driver to finish). |
| 1441 | * |
| 1442 | * This call can be used for pausing playback. |
| 1443 | * The return values from this function are intended to be directly returned |
| 1444 | * from vidioc_streamoff handler in the driver |
| 1445 | */ |
| 1446 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1447 | { |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1448 | if (q->fileio) { |
| 1449 | dprintk(1, "streamoff: file io in progress\n"); |
| 1450 | return -EBUSY; |
| 1451 | } |
| 1452 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1453 | if (type != q->type) { |
| 1454 | dprintk(1, "streamoff: invalid stream type\n"); |
| 1455 | return -EINVAL; |
| 1456 | } |
| 1457 | |
| 1458 | if (!q->streaming) { |
| 1459 | dprintk(1, "streamoff: not streaming\n"); |
| 1460 | return -EINVAL; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Cancel will pause streaming and remove all buffers from the driver |
| 1465 | * and videobuf, effectively returning control over them to userspace. |
| 1466 | */ |
| 1467 | __vb2_queue_cancel(q); |
| 1468 | |
| 1469 | dprintk(3, "Streamoff successful\n"); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | EXPORT_SYMBOL_GPL(vb2_streamoff); |
| 1473 | |
| 1474 | /** |
| 1475 | * __find_plane_by_offset() - find plane associated with the given offset off |
| 1476 | */ |
| 1477 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, |
| 1478 | unsigned int *_buffer, unsigned int *_plane) |
| 1479 | { |
| 1480 | struct vb2_buffer *vb; |
| 1481 | unsigned int buffer, plane; |
| 1482 | |
| 1483 | /* |
| 1484 | * Go over all buffers and their planes, comparing the given offset |
| 1485 | * with an offset assigned to each plane. If a match is found, |
| 1486 | * return its buffer and plane numbers. |
| 1487 | */ |
| 1488 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 1489 | vb = q->bufs[buffer]; |
| 1490 | |
| 1491 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1492 | if (vb->v4l2_planes[plane].m.mem_offset == off) { |
| 1493 | *_buffer = buffer; |
| 1494 | *_plane = plane; |
| 1495 | return 0; |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | return -EINVAL; |
| 1501 | } |
| 1502 | |
| 1503 | /** |
| 1504 | * vb2_mmap() - map video buffers into application address space |
| 1505 | * @q: videobuf2 queue |
| 1506 | * @vma: vma passed to the mmap file operation handler in the driver |
| 1507 | * |
| 1508 | * Should be called from mmap file operation handler of a driver. |
| 1509 | * This function maps one plane of one of the available video buffers to |
| 1510 | * userspace. To map whole video memory allocated on reqbufs, this function |
| 1511 | * has to be called once per each plane per each buffer previously allocated. |
| 1512 | * |
| 1513 | * When the userspace application calls mmap, it passes to it an offset returned |
| 1514 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as |
| 1515 | * a "cookie", which is then used to identify the plane to be mapped. |
| 1516 | * This function finds a plane with a matching offset and a mapping is performed |
| 1517 | * by the means of a provided memory operation. |
| 1518 | * |
| 1519 | * The return values from this function are intended to be directly returned |
| 1520 | * from the mmap handler in driver. |
| 1521 | */ |
| 1522 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) |
| 1523 | { |
| 1524 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 1525 | struct vb2_plane *vb_plane; |
| 1526 | struct vb2_buffer *vb; |
| 1527 | unsigned int buffer, plane; |
| 1528 | int ret; |
| 1529 | |
| 1530 | if (q->memory != V4L2_MEMORY_MMAP) { |
| 1531 | dprintk(1, "Queue is not currently set up for mmap\n"); |
| 1532 | return -EINVAL; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * Check memory area access mode. |
| 1537 | */ |
| 1538 | if (!(vma->vm_flags & VM_SHARED)) { |
| 1539 | dprintk(1, "Invalid vma flags, VM_SHARED needed\n"); |
| 1540 | return -EINVAL; |
| 1541 | } |
| 1542 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { |
| 1543 | if (!(vma->vm_flags & VM_WRITE)) { |
| 1544 | dprintk(1, "Invalid vma flags, VM_WRITE needed\n"); |
| 1545 | return -EINVAL; |
| 1546 | } |
| 1547 | } else { |
| 1548 | if (!(vma->vm_flags & VM_READ)) { |
| 1549 | dprintk(1, "Invalid vma flags, VM_READ needed\n"); |
| 1550 | return -EINVAL; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * Find the plane corresponding to the offset passed by userspace. |
| 1556 | */ |
| 1557 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 1558 | if (ret) |
| 1559 | return ret; |
| 1560 | |
| 1561 | vb = q->bufs[buffer]; |
| 1562 | vb_plane = &vb->planes[plane]; |
| 1563 | |
| 1564 | ret = q->mem_ops->mmap(vb_plane->mem_priv, vma); |
| 1565 | if (ret) |
| 1566 | return ret; |
| 1567 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1568 | dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | EXPORT_SYMBOL_GPL(vb2_mmap); |
| 1572 | |
Scott Jiang | 6f524ec | 2011-09-21 09:25:23 -0300 | [diff] [blame] | 1573 | #ifndef CONFIG_MMU |
| 1574 | unsigned long vb2_get_unmapped_area(struct vb2_queue *q, |
| 1575 | unsigned long addr, |
| 1576 | unsigned long len, |
| 1577 | unsigned long pgoff, |
| 1578 | unsigned long flags) |
| 1579 | { |
| 1580 | unsigned long off = pgoff << PAGE_SHIFT; |
| 1581 | struct vb2_buffer *vb; |
| 1582 | unsigned int buffer, plane; |
| 1583 | int ret; |
| 1584 | |
| 1585 | if (q->memory != V4L2_MEMORY_MMAP) { |
| 1586 | dprintk(1, "Queue is not currently set up for mmap\n"); |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | * Find the plane corresponding to the offset passed by userspace. |
| 1592 | */ |
| 1593 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 1594 | if (ret) |
| 1595 | return ret; |
| 1596 | |
| 1597 | vb = q->bufs[buffer]; |
| 1598 | |
| 1599 | return (unsigned long)vb2_plane_vaddr(vb, plane); |
| 1600 | } |
| 1601 | EXPORT_SYMBOL_GPL(vb2_get_unmapped_area); |
| 1602 | #endif |
| 1603 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1604 | static int __vb2_init_fileio(struct vb2_queue *q, int read); |
| 1605 | static int __vb2_cleanup_fileio(struct vb2_queue *q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1606 | |
| 1607 | /** |
| 1608 | * vb2_poll() - implements poll userspace operation |
| 1609 | * @q: videobuf2 queue |
| 1610 | * @file: file argument passed to the poll file operation handler |
| 1611 | * @wait: wait argument passed to the poll file operation handler |
| 1612 | * |
| 1613 | * This function implements poll file operation handler for a driver. |
| 1614 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will |
| 1615 | * be informed that the file descriptor of a video device is available for |
| 1616 | * reading. |
| 1617 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor |
| 1618 | * will be reported as available for writing. |
| 1619 | * |
| 1620 | * The return values from this function are intended to be directly returned |
| 1621 | * from poll handler in driver. |
| 1622 | */ |
| 1623 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) |
| 1624 | { |
| 1625 | unsigned long flags; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1626 | unsigned int ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1627 | struct vb2_buffer *vb = NULL; |
| 1628 | |
| 1629 | /* |
Pawel Osciak | 4ffabdb | 2011-03-20 18:17:34 -0300 | [diff] [blame] | 1630 | * Start file I/O emulator only if streaming API has not been used yet. |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1631 | */ |
| 1632 | if (q->num_buffers == 0 && q->fileio == NULL) { |
| 1633 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) { |
| 1634 | ret = __vb2_init_fileio(q, 1); |
| 1635 | if (ret) |
Pawel Osciak | 4ffabdb | 2011-03-20 18:17:34 -0300 | [diff] [blame] | 1636 | return POLLERR; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1637 | } |
| 1638 | if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) { |
| 1639 | ret = __vb2_init_fileio(q, 0); |
| 1640 | if (ret) |
Pawel Osciak | 4ffabdb | 2011-03-20 18:17:34 -0300 | [diff] [blame] | 1641 | return POLLERR; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1642 | /* |
| 1643 | * Write to OUTPUT queue can be done immediately. |
| 1644 | */ |
| 1645 | return POLLOUT | POLLWRNORM; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | /* |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1650 | * There is nothing to wait for if no buffers have already been queued. |
| 1651 | */ |
| 1652 | if (list_empty(&q->queued_list)) |
| 1653 | return POLLERR; |
| 1654 | |
| 1655 | poll_wait(file, &q->done_wq, wait); |
| 1656 | |
| 1657 | /* |
| 1658 | * Take first buffer available for dequeuing. |
| 1659 | */ |
| 1660 | spin_lock_irqsave(&q->done_lock, flags); |
| 1661 | if (!list_empty(&q->done_list)) |
| 1662 | vb = list_first_entry(&q->done_list, struct vb2_buffer, |
| 1663 | done_entry); |
| 1664 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1665 | |
| 1666 | if (vb && (vb->state == VB2_BUF_STATE_DONE |
| 1667 | || vb->state == VB2_BUF_STATE_ERROR)) { |
| 1668 | return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM : |
| 1669 | POLLIN | POLLRDNORM; |
| 1670 | } |
| 1671 | return 0; |
| 1672 | } |
| 1673 | EXPORT_SYMBOL_GPL(vb2_poll); |
| 1674 | |
| 1675 | /** |
| 1676 | * vb2_queue_init() - initialize a videobuf2 queue |
| 1677 | * @q: videobuf2 queue; this structure should be allocated in driver |
| 1678 | * |
| 1679 | * The vb2_queue structure should be allocated by the driver. The driver is |
| 1680 | * responsible of clearing it's content and setting initial values for some |
| 1681 | * required entries before calling this function. |
| 1682 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer |
| 1683 | * to the struct vb2_queue description in include/media/videobuf2-core.h |
| 1684 | * for more information. |
| 1685 | */ |
| 1686 | int vb2_queue_init(struct vb2_queue *q) |
| 1687 | { |
| 1688 | BUG_ON(!q); |
| 1689 | BUG_ON(!q->ops); |
| 1690 | BUG_ON(!q->mem_ops); |
| 1691 | BUG_ON(!q->type); |
| 1692 | BUG_ON(!q->io_modes); |
| 1693 | |
| 1694 | BUG_ON(!q->ops->queue_setup); |
| 1695 | BUG_ON(!q->ops->buf_queue); |
| 1696 | |
| 1697 | INIT_LIST_HEAD(&q->queued_list); |
| 1698 | INIT_LIST_HEAD(&q->done_list); |
| 1699 | spin_lock_init(&q->done_lock); |
| 1700 | init_waitqueue_head(&q->done_wq); |
| 1701 | |
| 1702 | if (q->buf_struct_size == 0) |
| 1703 | q->buf_struct_size = sizeof(struct vb2_buffer); |
| 1704 | |
| 1705 | return 0; |
| 1706 | } |
| 1707 | EXPORT_SYMBOL_GPL(vb2_queue_init); |
| 1708 | |
| 1709 | /** |
| 1710 | * vb2_queue_release() - stop streaming, release the queue and free memory |
| 1711 | * @q: videobuf2 queue |
| 1712 | * |
| 1713 | * This function stops streaming and performs necessary clean ups, including |
| 1714 | * freeing video buffer memory. The driver is responsible for freeing |
| 1715 | * the vb2_queue structure itself. |
| 1716 | */ |
| 1717 | void vb2_queue_release(struct vb2_queue *q) |
| 1718 | { |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1719 | __vb2_cleanup_fileio(q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1720 | __vb2_queue_cancel(q); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 1721 | __vb2_queue_free(q, q->num_buffers); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1722 | } |
| 1723 | EXPORT_SYMBOL_GPL(vb2_queue_release); |
| 1724 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1725 | /** |
| 1726 | * struct vb2_fileio_buf - buffer context used by file io emulator |
| 1727 | * |
| 1728 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 1729 | * write) calls on top of streaming API. This structure is used for |
| 1730 | * tracking context related to the buffers. |
| 1731 | */ |
| 1732 | struct vb2_fileio_buf { |
| 1733 | void *vaddr; |
| 1734 | unsigned int size; |
| 1735 | unsigned int pos; |
| 1736 | unsigned int queued:1; |
| 1737 | }; |
| 1738 | |
| 1739 | /** |
| 1740 | * struct vb2_fileio_data - queue context used by file io emulator |
| 1741 | * |
| 1742 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 1743 | * write) calls on top of streaming API. For proper operation it required |
| 1744 | * this structure to save the driver state between each call of the read |
| 1745 | * or write function. |
| 1746 | */ |
| 1747 | struct vb2_fileio_data { |
| 1748 | struct v4l2_requestbuffers req; |
| 1749 | struct v4l2_buffer b; |
| 1750 | struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME]; |
| 1751 | unsigned int index; |
| 1752 | unsigned int q_count; |
| 1753 | unsigned int dq_count; |
| 1754 | unsigned int flags; |
| 1755 | }; |
| 1756 | |
| 1757 | /** |
| 1758 | * __vb2_init_fileio() - initialize file io emulator |
| 1759 | * @q: videobuf2 queue |
| 1760 | * @read: mode selector (1 means read, 0 means write) |
| 1761 | */ |
| 1762 | static int __vb2_init_fileio(struct vb2_queue *q, int read) |
| 1763 | { |
| 1764 | struct vb2_fileio_data *fileio; |
| 1765 | int i, ret; |
| 1766 | unsigned int count = 0; |
| 1767 | |
| 1768 | /* |
| 1769 | * Sanity check |
| 1770 | */ |
| 1771 | if ((read && !(q->io_modes & VB2_READ)) || |
| 1772 | (!read && !(q->io_modes & VB2_WRITE))) |
| 1773 | BUG(); |
| 1774 | |
| 1775 | /* |
| 1776 | * Check if device supports mapping buffers to kernel virtual space. |
| 1777 | */ |
| 1778 | if (!q->mem_ops->vaddr) |
| 1779 | return -EBUSY; |
| 1780 | |
| 1781 | /* |
| 1782 | * Check if streaming api has not been already activated. |
| 1783 | */ |
| 1784 | if (q->streaming || q->num_buffers > 0) |
| 1785 | return -EBUSY; |
| 1786 | |
| 1787 | /* |
| 1788 | * Start with count 1, driver can increase it in queue_setup() |
| 1789 | */ |
| 1790 | count = 1; |
| 1791 | |
| 1792 | dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n", |
| 1793 | (read) ? "read" : "write", count, q->io_flags); |
| 1794 | |
| 1795 | fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL); |
| 1796 | if (fileio == NULL) |
| 1797 | return -ENOMEM; |
| 1798 | |
| 1799 | fileio->flags = q->io_flags; |
| 1800 | |
| 1801 | /* |
| 1802 | * Request buffers and use MMAP type to force driver |
| 1803 | * to allocate buffers by itself. |
| 1804 | */ |
| 1805 | fileio->req.count = count; |
| 1806 | fileio->req.memory = V4L2_MEMORY_MMAP; |
| 1807 | fileio->req.type = q->type; |
| 1808 | ret = vb2_reqbufs(q, &fileio->req); |
| 1809 | if (ret) |
| 1810 | goto err_kfree; |
| 1811 | |
| 1812 | /* |
| 1813 | * Check if plane_count is correct |
| 1814 | * (multiplane buffers are not supported). |
| 1815 | */ |
| 1816 | if (q->bufs[0]->num_planes != 1) { |
| 1817 | fileio->req.count = 0; |
| 1818 | ret = -EBUSY; |
| 1819 | goto err_reqbufs; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * Get kernel address of each buffer. |
| 1824 | */ |
| 1825 | for (i = 0; i < q->num_buffers; i++) { |
| 1826 | fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); |
| 1827 | if (fileio->bufs[i].vaddr == NULL) |
| 1828 | goto err_reqbufs; |
| 1829 | fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Read mode requires pre queuing of all buffers. |
| 1834 | */ |
| 1835 | if (read) { |
| 1836 | /* |
| 1837 | * Queue all buffers. |
| 1838 | */ |
| 1839 | for (i = 0; i < q->num_buffers; i++) { |
| 1840 | struct v4l2_buffer *b = &fileio->b; |
| 1841 | memset(b, 0, sizeof(*b)); |
| 1842 | b->type = q->type; |
| 1843 | b->memory = q->memory; |
| 1844 | b->index = i; |
| 1845 | ret = vb2_qbuf(q, b); |
| 1846 | if (ret) |
| 1847 | goto err_reqbufs; |
| 1848 | fileio->bufs[i].queued = 1; |
| 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | * Start streaming. |
| 1853 | */ |
| 1854 | ret = vb2_streamon(q, q->type); |
| 1855 | if (ret) |
| 1856 | goto err_reqbufs; |
| 1857 | } |
| 1858 | |
| 1859 | q->fileio = fileio; |
| 1860 | |
| 1861 | return ret; |
| 1862 | |
| 1863 | err_reqbufs: |
| 1864 | vb2_reqbufs(q, &fileio->req); |
| 1865 | |
| 1866 | err_kfree: |
| 1867 | kfree(fileio); |
| 1868 | return ret; |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * __vb2_cleanup_fileio() - free resourced used by file io emulator |
| 1873 | * @q: videobuf2 queue |
| 1874 | */ |
| 1875 | static int __vb2_cleanup_fileio(struct vb2_queue *q) |
| 1876 | { |
| 1877 | struct vb2_fileio_data *fileio = q->fileio; |
| 1878 | |
| 1879 | if (fileio) { |
| 1880 | /* |
| 1881 | * Hack fileio context to enable direct calls to vb2 ioctl |
| 1882 | * interface. |
| 1883 | */ |
| 1884 | q->fileio = NULL; |
| 1885 | |
| 1886 | vb2_streamoff(q, q->type); |
| 1887 | fileio->req.count = 0; |
| 1888 | vb2_reqbufs(q, &fileio->req); |
| 1889 | kfree(fileio); |
| 1890 | dprintk(3, "file io emulator closed\n"); |
| 1891 | } |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * __vb2_perform_fileio() - perform a single file io (read or write) operation |
| 1897 | * @q: videobuf2 queue |
| 1898 | * @data: pointed to target userspace buffer |
| 1899 | * @count: number of bytes to read or write |
| 1900 | * @ppos: file handle position tracking pointer |
| 1901 | * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) |
| 1902 | * @read: access mode selector (1 means read, 0 means write) |
| 1903 | */ |
| 1904 | static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, |
| 1905 | loff_t *ppos, int nonblock, int read) |
| 1906 | { |
| 1907 | struct vb2_fileio_data *fileio; |
| 1908 | struct vb2_fileio_buf *buf; |
| 1909 | int ret, index; |
| 1910 | |
Mauro Carvalho Chehab | 08b99e2 | 2011-01-11 17:12:34 -0300 | [diff] [blame] | 1911 | dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n", |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1912 | read ? "read" : "write", (long)*ppos, count, |
| 1913 | nonblock ? "non" : ""); |
| 1914 | |
| 1915 | if (!data) |
| 1916 | return -EINVAL; |
| 1917 | |
| 1918 | /* |
| 1919 | * Initialize emulator on first call. |
| 1920 | */ |
| 1921 | if (!q->fileio) { |
| 1922 | ret = __vb2_init_fileio(q, read); |
| 1923 | dprintk(3, "file io: vb2_init_fileio result: %d\n", ret); |
| 1924 | if (ret) |
| 1925 | return ret; |
| 1926 | } |
| 1927 | fileio = q->fileio; |
| 1928 | |
| 1929 | /* |
| 1930 | * Hack fileio context to enable direct calls to vb2 ioctl interface. |
| 1931 | * The pointer will be restored before returning from this function. |
| 1932 | */ |
| 1933 | q->fileio = NULL; |
| 1934 | |
| 1935 | index = fileio->index; |
| 1936 | buf = &fileio->bufs[index]; |
| 1937 | |
| 1938 | /* |
| 1939 | * Check if we need to dequeue the buffer. |
| 1940 | */ |
| 1941 | if (buf->queued) { |
| 1942 | struct vb2_buffer *vb; |
| 1943 | |
| 1944 | /* |
| 1945 | * Call vb2_dqbuf to get buffer back. |
| 1946 | */ |
| 1947 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 1948 | fileio->b.type = q->type; |
| 1949 | fileio->b.memory = q->memory; |
| 1950 | fileio->b.index = index; |
| 1951 | ret = vb2_dqbuf(q, &fileio->b, nonblock); |
| 1952 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); |
| 1953 | if (ret) |
| 1954 | goto end; |
| 1955 | fileio->dq_count += 1; |
| 1956 | |
| 1957 | /* |
| 1958 | * Get number of bytes filled by the driver |
| 1959 | */ |
| 1960 | vb = q->bufs[index]; |
| 1961 | buf->size = vb2_get_plane_payload(vb, 0); |
| 1962 | buf->queued = 0; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * Limit count on last few bytes of the buffer. |
| 1967 | */ |
| 1968 | if (buf->pos + count > buf->size) { |
| 1969 | count = buf->size - buf->pos; |
Mauro Carvalho Chehab | 08b99e2 | 2011-01-11 17:12:34 -0300 | [diff] [blame] | 1970 | dprintk(5, "reducing read count: %zd\n", count); |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * Transfer data to userspace. |
| 1975 | */ |
Mauro Carvalho Chehab | 08b99e2 | 2011-01-11 17:12:34 -0300 | [diff] [blame] | 1976 | dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n", |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame] | 1977 | count, index, buf->pos); |
| 1978 | if (read) |
| 1979 | ret = copy_to_user(data, buf->vaddr + buf->pos, count); |
| 1980 | else |
| 1981 | ret = copy_from_user(buf->vaddr + buf->pos, data, count); |
| 1982 | if (ret) { |
| 1983 | dprintk(3, "file io: error copying data\n"); |
| 1984 | ret = -EFAULT; |
| 1985 | goto end; |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * Update counters. |
| 1990 | */ |
| 1991 | buf->pos += count; |
| 1992 | *ppos += count; |
| 1993 | |
| 1994 | /* |
| 1995 | * Queue next buffer if required. |
| 1996 | */ |
| 1997 | if (buf->pos == buf->size || |
| 1998 | (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) { |
| 1999 | /* |
| 2000 | * Check if this is the last buffer to read. |
| 2001 | */ |
| 2002 | if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) && |
| 2003 | fileio->dq_count == 1) { |
| 2004 | dprintk(3, "file io: read limit reached\n"); |
| 2005 | /* |
| 2006 | * Restore fileio pointer and release the context. |
| 2007 | */ |
| 2008 | q->fileio = fileio; |
| 2009 | return __vb2_cleanup_fileio(q); |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * Call vb2_qbuf and give buffer to the driver. |
| 2014 | */ |
| 2015 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 2016 | fileio->b.type = q->type; |
| 2017 | fileio->b.memory = q->memory; |
| 2018 | fileio->b.index = index; |
| 2019 | fileio->b.bytesused = buf->pos; |
| 2020 | ret = vb2_qbuf(q, &fileio->b); |
| 2021 | dprintk(5, "file io: vb2_dbuf result: %d\n", ret); |
| 2022 | if (ret) |
| 2023 | goto end; |
| 2024 | |
| 2025 | /* |
| 2026 | * Buffer has been queued, update the status |
| 2027 | */ |
| 2028 | buf->pos = 0; |
| 2029 | buf->queued = 1; |
| 2030 | buf->size = q->bufs[0]->v4l2_planes[0].length; |
| 2031 | fileio->q_count += 1; |
| 2032 | |
| 2033 | /* |
| 2034 | * Switch to the next buffer |
| 2035 | */ |
| 2036 | fileio->index = (index + 1) % q->num_buffers; |
| 2037 | |
| 2038 | /* |
| 2039 | * Start streaming if required. |
| 2040 | */ |
| 2041 | if (!read && !q->streaming) { |
| 2042 | ret = vb2_streamon(q, q->type); |
| 2043 | if (ret) |
| 2044 | goto end; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * Return proper number of bytes processed. |
| 2050 | */ |
| 2051 | if (ret == 0) |
| 2052 | ret = count; |
| 2053 | end: |
| 2054 | /* |
| 2055 | * Restore the fileio context and block vb2 ioctl interface. |
| 2056 | */ |
| 2057 | q->fileio = fileio; |
| 2058 | return ret; |
| 2059 | } |
| 2060 | |
| 2061 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, |
| 2062 | loff_t *ppos, int nonblocking) |
| 2063 | { |
| 2064 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); |
| 2065 | } |
| 2066 | EXPORT_SYMBOL_GPL(vb2_read); |
| 2067 | |
| 2068 | size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count, |
| 2069 | loff_t *ppos, int nonblocking) |
| 2070 | { |
| 2071 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0); |
| 2072 | } |
| 2073 | EXPORT_SYMBOL_GPL(vb2_write); |
| 2074 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2075 | MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 2076 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2077 | MODULE_LICENSE("GPL"); |