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