| 1 | /* |
| 2 | * videobuf2-core.c - V4L2 driver helper framework |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
| 5 | * |
| 6 | * Author: Pawel Osciak <pawel@osciak.com> |
| 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/v4l2-dev.h> |
| 23 | #include <media/v4l2-fh.h> |
| 24 | #include <media/v4l2-event.h> |
| 25 | #include <media/videobuf2-core.h> |
| 26 | |
| 27 | static int debug; |
| 28 | module_param(debug, int, 0644); |
| 29 | |
| 30 | #define dprintk(level, fmt, arg...) \ |
| 31 | do { \ |
| 32 | if (debug >= level) \ |
| 33 | printk(KERN_DEBUG "vb2: " fmt, ## arg); \ |
| 34 | } while (0) |
| 35 | |
| 36 | #define call_memop(q, op, args...) \ |
| 37 | (((q)->mem_ops->op) ? \ |
| 38 | ((q)->mem_ops->op(args)) : 0) |
| 39 | |
| 40 | #define call_qop(q, op, args...) \ |
| 41 | (((q)->ops->op) ? ((q)->ops->op(args)) : 0) |
| 42 | |
| 43 | #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \ |
| 44 | V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \ |
| 45 | V4L2_BUF_FLAG_PREPARED) |
| 46 | |
| 47 | /** |
| 48 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer |
| 49 | */ |
| 50 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) |
| 51 | { |
| 52 | struct vb2_queue *q = vb->vb2_queue; |
| 53 | void *mem_priv; |
| 54 | int plane; |
| 55 | |
| 56 | /* Allocate memory for all planes in this buffer */ |
| 57 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 58 | mem_priv = call_memop(q, alloc, q->alloc_ctx[plane], |
| 59 | q->plane_sizes[plane]); |
| 60 | if (IS_ERR_OR_NULL(mem_priv)) |
| 61 | goto free; |
| 62 | |
| 63 | /* Associate allocator private data with this plane */ |
| 64 | vb->planes[plane].mem_priv = mem_priv; |
| 65 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | free: |
| 70 | /* Free already allocated memory if one of the allocations failed */ |
| 71 | for (; plane > 0; --plane) { |
| 72 | call_memop(q, put, vb->planes[plane - 1].mem_priv); |
| 73 | vb->planes[plane - 1].mem_priv = NULL; |
| 74 | } |
| 75 | |
| 76 | return -ENOMEM; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * __vb2_buf_mem_free() - free memory of the given buffer |
| 81 | */ |
| 82 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) |
| 83 | { |
| 84 | struct vb2_queue *q = vb->vb2_queue; |
| 85 | unsigned int plane; |
| 86 | |
| 87 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 88 | call_memop(q, put, vb->planes[plane].mem_priv); |
| 89 | vb->planes[plane].mem_priv = NULL; |
| 90 | dprintk(3, "Freed plane %d of buffer %d\n", plane, |
| 91 | vb->v4l2_buf.index); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * __vb2_buf_userptr_put() - release userspace memory associated with |
| 97 | * a USERPTR buffer |
| 98 | */ |
| 99 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) |
| 100 | { |
| 101 | struct vb2_queue *q = vb->vb2_queue; |
| 102 | unsigned int plane; |
| 103 | |
| 104 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 105 | if (vb->planes[plane].mem_priv) |
| 106 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); |
| 107 | vb->planes[plane].mem_priv = NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * __vb2_plane_dmabuf_put() - release memory associated with |
| 113 | * a DMABUF shared plane |
| 114 | */ |
| 115 | static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p) |
| 116 | { |
| 117 | if (!p->mem_priv) |
| 118 | return; |
| 119 | |
| 120 | if (p->dbuf_mapped) |
| 121 | call_memop(q, unmap_dmabuf, p->mem_priv); |
| 122 | |
| 123 | call_memop(q, detach_dmabuf, p->mem_priv); |
| 124 | dma_buf_put(p->dbuf); |
| 125 | memset(p, 0, sizeof(*p)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * __vb2_buf_dmabuf_put() - release memory associated with |
| 130 | * a DMABUF shared buffer |
| 131 | */ |
| 132 | static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb) |
| 133 | { |
| 134 | struct vb2_queue *q = vb->vb2_queue; |
| 135 | unsigned int plane; |
| 136 | |
| 137 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 138 | __vb2_plane_dmabuf_put(q, &vb->planes[plane]); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in |
| 143 | * every buffer on the queue |
| 144 | */ |
| 145 | static void __setup_offsets(struct vb2_queue *q, unsigned int n) |
| 146 | { |
| 147 | unsigned int buffer, plane; |
| 148 | struct vb2_buffer *vb; |
| 149 | unsigned long off; |
| 150 | |
| 151 | if (q->num_buffers) { |
| 152 | struct v4l2_plane *p; |
| 153 | vb = q->bufs[q->num_buffers - 1]; |
| 154 | p = &vb->v4l2_planes[vb->num_planes - 1]; |
| 155 | off = PAGE_ALIGN(p->m.mem_offset + p->length); |
| 156 | } else { |
| 157 | off = 0; |
| 158 | } |
| 159 | |
| 160 | for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) { |
| 161 | vb = q->bufs[buffer]; |
| 162 | if (!vb) |
| 163 | continue; |
| 164 | |
| 165 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 166 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
| 167 | vb->v4l2_planes[plane].m.mem_offset = off; |
| 168 | |
| 169 | dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n", |
| 170 | buffer, plane, off); |
| 171 | |
| 172 | off += vb->v4l2_planes[plane].length; |
| 173 | off = PAGE_ALIGN(off); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) |
| 180 | * video buffer memory for all buffers/planes on the queue and initializes the |
| 181 | * queue |
| 182 | * |
| 183 | * Returns the number of buffers successfully allocated. |
| 184 | */ |
| 185 | static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory, |
| 186 | unsigned int num_buffers, unsigned int num_planes) |
| 187 | { |
| 188 | unsigned int buffer; |
| 189 | struct vb2_buffer *vb; |
| 190 | int ret; |
| 191 | |
| 192 | for (buffer = 0; buffer < num_buffers; ++buffer) { |
| 193 | /* Allocate videobuf buffer structures */ |
| 194 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); |
| 195 | if (!vb) { |
| 196 | dprintk(1, "Memory alloc for buffer struct failed\n"); |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | /* Length stores number of planes for multiplanar buffers */ |
| 201 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) |
| 202 | vb->v4l2_buf.length = num_planes; |
| 203 | |
| 204 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 205 | vb->vb2_queue = q; |
| 206 | vb->num_planes = num_planes; |
| 207 | vb->v4l2_buf.index = q->num_buffers + buffer; |
| 208 | vb->v4l2_buf.type = q->type; |
| 209 | vb->v4l2_buf.memory = memory; |
| 210 | |
| 211 | /* Allocate video buffer memory for the MMAP type */ |
| 212 | if (memory == V4L2_MEMORY_MMAP) { |
| 213 | ret = __vb2_buf_mem_alloc(vb); |
| 214 | if (ret) { |
| 215 | dprintk(1, "Failed allocating memory for " |
| 216 | "buffer %d\n", buffer); |
| 217 | kfree(vb); |
| 218 | break; |
| 219 | } |
| 220 | /* |
| 221 | * Call the driver-provided buffer initialization |
| 222 | * callback, if given. An error in initialization |
| 223 | * results in queue setup failure. |
| 224 | */ |
| 225 | ret = call_qop(q, buf_init, vb); |
| 226 | if (ret) { |
| 227 | dprintk(1, "Buffer %d %p initialization" |
| 228 | " failed\n", buffer, vb); |
| 229 | __vb2_buf_mem_free(vb); |
| 230 | kfree(vb); |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | q->bufs[q->num_buffers + buffer] = vb; |
| 236 | } |
| 237 | |
| 238 | __setup_offsets(q, buffer); |
| 239 | |
| 240 | dprintk(1, "Allocated %d buffers, %d plane(s) each\n", |
| 241 | buffer, num_planes); |
| 242 | |
| 243 | return buffer; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * __vb2_free_mem() - release all video buffer memory for a given queue |
| 248 | */ |
| 249 | static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) |
| 250 | { |
| 251 | unsigned int buffer; |
| 252 | struct vb2_buffer *vb; |
| 253 | |
| 254 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 255 | ++buffer) { |
| 256 | vb = q->bufs[buffer]; |
| 257 | if (!vb) |
| 258 | continue; |
| 259 | |
| 260 | /* Free MMAP buffers or release USERPTR buffers */ |
| 261 | if (q->memory == V4L2_MEMORY_MMAP) |
| 262 | __vb2_buf_mem_free(vb); |
| 263 | else if (q->memory == V4L2_MEMORY_DMABUF) |
| 264 | __vb2_buf_dmabuf_put(vb); |
| 265 | else |
| 266 | __vb2_buf_userptr_put(vb); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * __vb2_queue_free() - free buffers at the end of the queue - video memory and |
| 272 | * related information, if no buffers are left return the queue to an |
| 273 | * uninitialized state. Might be called even if the queue has already been freed. |
| 274 | */ |
| 275 | static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) |
| 276 | { |
| 277 | unsigned int buffer; |
| 278 | |
| 279 | /* Call driver-provided cleanup function for each buffer, if provided */ |
| 280 | if (q->ops->buf_cleanup) { |
| 281 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 282 | ++buffer) { |
| 283 | if (NULL == q->bufs[buffer]) |
| 284 | continue; |
| 285 | q->ops->buf_cleanup(q->bufs[buffer]); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* Release video buffer memory */ |
| 290 | __vb2_free_mem(q, buffers); |
| 291 | |
| 292 | /* Free videobuf buffers */ |
| 293 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 294 | ++buffer) { |
| 295 | kfree(q->bufs[buffer]); |
| 296 | q->bufs[buffer] = NULL; |
| 297 | } |
| 298 | |
| 299 | q->num_buffers -= buffers; |
| 300 | if (!q->num_buffers) |
| 301 | q->memory = 0; |
| 302 | INIT_LIST_HEAD(&q->queued_list); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * __verify_planes_array() - verify that the planes array passed in struct |
| 307 | * v4l2_buffer from userspace can be safely used |
| 308 | */ |
| 309 | static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
| 310 | { |
| 311 | if (!V4L2_TYPE_IS_MULTIPLANAR(b->type)) |
| 312 | return 0; |
| 313 | |
| 314 | /* Is memory for copying plane information present? */ |
| 315 | if (NULL == b->m.planes) { |
| 316 | dprintk(1, "Multi-planar buffer passed but " |
| 317 | "planes array not provided\n"); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) { |
| 322 | dprintk(1, "Incorrect planes array length, " |
| 323 | "expected %d, got %d\n", vb->num_planes, b->length); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * __buffer_in_use() - return true if the buffer is in use and |
| 332 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 333 | */ |
| 334 | static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) |
| 335 | { |
| 336 | unsigned int plane; |
| 337 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 338 | void *mem_priv = vb->planes[plane].mem_priv; |
| 339 | /* |
| 340 | * If num_users() has not been provided, call_memop |
| 341 | * will return 0, apparently nobody cares about this |
| 342 | * case anyway. If num_users() returns more than 1, |
| 343 | * we are not the only user of the plane's memory. |
| 344 | */ |
| 345 | if (mem_priv && call_memop(q, num_users, mem_priv) > 1) |
| 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * __buffers_in_use() - return true if any buffers on the queue are in use and |
| 353 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 354 | */ |
| 355 | static bool __buffers_in_use(struct vb2_queue *q) |
| 356 | { |
| 357 | unsigned int buffer; |
| 358 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 359 | if (__buffer_in_use(q, q->bufs[buffer])) |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be |
| 367 | * returned to userspace |
| 368 | */ |
| 369 | static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 370 | { |
| 371 | struct vb2_queue *q = vb->vb2_queue; |
| 372 | |
| 373 | /* Copy back data such as timestamp, flags, etc. */ |
| 374 | memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m)); |
| 375 | b->reserved2 = vb->v4l2_buf.reserved2; |
| 376 | b->reserved = vb->v4l2_buf.reserved; |
| 377 | |
| 378 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) { |
| 379 | /* |
| 380 | * Fill in plane-related data if userspace provided an array |
| 381 | * for it. The caller has already verified memory and size. |
| 382 | */ |
| 383 | b->length = vb->num_planes; |
| 384 | memcpy(b->m.planes, vb->v4l2_planes, |
| 385 | b->length * sizeof(struct v4l2_plane)); |
| 386 | } else { |
| 387 | /* |
| 388 | * We use length and offset in v4l2_planes array even for |
| 389 | * single-planar buffers, but userspace does not. |
| 390 | */ |
| 391 | b->length = vb->v4l2_planes[0].length; |
| 392 | b->bytesused = vb->v4l2_planes[0].bytesused; |
| 393 | if (q->memory == V4L2_MEMORY_MMAP) |
| 394 | b->m.offset = vb->v4l2_planes[0].m.mem_offset; |
| 395 | else if (q->memory == V4L2_MEMORY_USERPTR) |
| 396 | b->m.userptr = vb->v4l2_planes[0].m.userptr; |
| 397 | else if (q->memory == V4L2_MEMORY_DMABUF) |
| 398 | b->m.fd = vb->v4l2_planes[0].m.fd; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Clear any buffer state related flags. |
| 403 | */ |
| 404 | b->flags &= ~V4L2_BUFFER_STATE_FLAGS; |
| 405 | |
| 406 | switch (vb->state) { |
| 407 | case VB2_BUF_STATE_QUEUED: |
| 408 | case VB2_BUF_STATE_ACTIVE: |
| 409 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 410 | break; |
| 411 | case VB2_BUF_STATE_ERROR: |
| 412 | b->flags |= V4L2_BUF_FLAG_ERROR; |
| 413 | /* fall through */ |
| 414 | case VB2_BUF_STATE_DONE: |
| 415 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 416 | break; |
| 417 | case VB2_BUF_STATE_PREPARED: |
| 418 | b->flags |= V4L2_BUF_FLAG_PREPARED; |
| 419 | break; |
| 420 | case VB2_BUF_STATE_DEQUEUED: |
| 421 | /* nothing */ |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if (__buffer_in_use(q, vb)) |
| 426 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * vb2_querybuf() - query video buffer information |
| 431 | * @q: videobuf queue |
| 432 | * @b: buffer struct passed from userspace to vidioc_querybuf handler |
| 433 | * in driver |
| 434 | * |
| 435 | * Should be called from vidioc_querybuf ioctl handler in driver. |
| 436 | * This function will verify the passed v4l2_buffer structure and fill the |
| 437 | * relevant information for the userspace. |
| 438 | * |
| 439 | * The return values from this function are intended to be directly returned |
| 440 | * from vidioc_querybuf handler in driver. |
| 441 | */ |
| 442 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 443 | { |
| 444 | struct vb2_buffer *vb; |
| 445 | int ret; |
| 446 | |
| 447 | if (b->type != q->type) { |
| 448 | dprintk(1, "querybuf: wrong buffer type\n"); |
| 449 | return -EINVAL; |
| 450 | } |
| 451 | |
| 452 | if (b->index >= q->num_buffers) { |
| 453 | dprintk(1, "querybuf: buffer index out of range\n"); |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | vb = q->bufs[b->index]; |
| 457 | ret = __verify_planes_array(vb, b); |
| 458 | if (!ret) |
| 459 | __fill_v4l2_buffer(vb, b); |
| 460 | return ret; |
| 461 | } |
| 462 | EXPORT_SYMBOL(vb2_querybuf); |
| 463 | |
| 464 | /** |
| 465 | * __verify_userptr_ops() - verify that all memory operations required for |
| 466 | * USERPTR queue type have been provided |
| 467 | */ |
| 468 | static int __verify_userptr_ops(struct vb2_queue *q) |
| 469 | { |
| 470 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || |
| 471 | !q->mem_ops->put_userptr) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * __verify_mmap_ops() - verify that all memory operations required for |
| 479 | * MMAP queue type have been provided |
| 480 | */ |
| 481 | static int __verify_mmap_ops(struct vb2_queue *q) |
| 482 | { |
| 483 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || |
| 484 | !q->mem_ops->put || !q->mem_ops->mmap) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * __verify_dmabuf_ops() - verify that all memory operations required for |
| 492 | * DMABUF queue type have been provided |
| 493 | */ |
| 494 | static int __verify_dmabuf_ops(struct vb2_queue *q) |
| 495 | { |
| 496 | if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf || |
| 497 | !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf || |
| 498 | !q->mem_ops->unmap_dmabuf) |
| 499 | return -EINVAL; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * __verify_memory_type() - Check whether the memory type and buffer type |
| 506 | * passed to a buffer operation are compatible with the queue. |
| 507 | */ |
| 508 | static int __verify_memory_type(struct vb2_queue *q, |
| 509 | enum v4l2_memory memory, enum v4l2_buf_type type) |
| 510 | { |
| 511 | if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR && |
| 512 | memory != V4L2_MEMORY_DMABUF) { |
| 513 | dprintk(1, "reqbufs: unsupported memory type\n"); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | if (type != q->type) { |
| 518 | dprintk(1, "reqbufs: requested type is incorrect\n"); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Make sure all the required memory ops for given memory type |
| 524 | * are available. |
| 525 | */ |
| 526 | if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { |
| 527 | dprintk(1, "reqbufs: MMAP for current setup unsupported\n"); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { |
| 532 | dprintk(1, "reqbufs: USERPTR for current setup unsupported\n"); |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) { |
| 537 | dprintk(1, "reqbufs: DMABUF for current setup unsupported\n"); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Place the busy tests at the end: -EBUSY can be ignored when |
| 543 | * create_bufs is called with count == 0, but count == 0 should still |
| 544 | * do the memory and type validation. |
| 545 | */ |
| 546 | if (q->fileio) { |
| 547 | dprintk(1, "reqbufs: file io in progress\n"); |
| 548 | return -EBUSY; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * __reqbufs() - Initiate streaming |
| 555 | * @q: videobuf2 queue |
| 556 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver |
| 557 | * |
| 558 | * Should be called from vidioc_reqbufs ioctl handler of a driver. |
| 559 | * This function: |
| 560 | * 1) verifies streaming parameters passed from the userspace, |
| 561 | * 2) sets up the queue, |
| 562 | * 3) negotiates number of buffers and planes per buffer with the driver |
| 563 | * to be used during streaming, |
| 564 | * 4) allocates internal buffer structures (struct vb2_buffer), according to |
| 565 | * the agreed parameters, |
| 566 | * 5) for MMAP memory type, allocates actual video memory, using the |
| 567 | * memory handling/allocation routines provided during queue initialization |
| 568 | * |
| 569 | * If req->count is 0, all the memory will be freed instead. |
| 570 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call |
| 571 | * and the queue is not busy, memory will be reallocated. |
| 572 | * |
| 573 | * The return values from this function are intended to be directly returned |
| 574 | * from vidioc_reqbufs handler in driver. |
| 575 | */ |
| 576 | static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) |
| 577 | { |
| 578 | unsigned int num_buffers, allocated_buffers, num_planes = 0; |
| 579 | int ret; |
| 580 | |
| 581 | if (q->streaming) { |
| 582 | dprintk(1, "reqbufs: streaming active\n"); |
| 583 | return -EBUSY; |
| 584 | } |
| 585 | |
| 586 | if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) { |
| 587 | /* |
| 588 | * We already have buffers allocated, so first check if they |
| 589 | * are not in use and can be freed. |
| 590 | */ |
| 591 | if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { |
| 592 | dprintk(1, "reqbufs: memory in use, cannot free\n"); |
| 593 | return -EBUSY; |
| 594 | } |
| 595 | |
| 596 | __vb2_queue_free(q, q->num_buffers); |
| 597 | |
| 598 | /* |
| 599 | * In case of REQBUFS(0) return immediately without calling |
| 600 | * driver's queue_setup() callback and allocating resources. |
| 601 | */ |
| 602 | if (req->count == 0) |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Make sure the requested values and current defaults are sane. |
| 608 | */ |
| 609 | num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); |
| 610 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); |
| 611 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
| 612 | q->memory = req->memory; |
| 613 | |
| 614 | /* |
| 615 | * Ask the driver how many buffers and planes per buffer it requires. |
| 616 | * Driver also sets the size and allocator context for each plane. |
| 617 | */ |
| 618 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes, |
| 619 | q->plane_sizes, q->alloc_ctx); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | /* Finally, allocate buffers and video memory */ |
| 624 | ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes); |
| 625 | if (ret == 0) { |
| 626 | dprintk(1, "Memory allocation failed\n"); |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | |
| 630 | allocated_buffers = ret; |
| 631 | |
| 632 | /* |
| 633 | * Check if driver can handle the allocated number of buffers. |
| 634 | */ |
| 635 | if (allocated_buffers < num_buffers) { |
| 636 | num_buffers = allocated_buffers; |
| 637 | |
| 638 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, |
| 639 | &num_planes, q->plane_sizes, q->alloc_ctx); |
| 640 | |
| 641 | if (!ret && allocated_buffers < num_buffers) |
| 642 | ret = -ENOMEM; |
| 643 | |
| 644 | /* |
| 645 | * Either the driver has accepted a smaller number of buffers, |
| 646 | * or .queue_setup() returned an error |
| 647 | */ |
| 648 | } |
| 649 | |
| 650 | q->num_buffers = allocated_buffers; |
| 651 | |
| 652 | if (ret < 0) { |
| 653 | __vb2_queue_free(q, allocated_buffers); |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Return the number of successfully allocated buffers |
| 659 | * to the userspace. |
| 660 | */ |
| 661 | req->count = allocated_buffers; |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and |
| 668 | * type values. |
| 669 | * @q: videobuf2 queue |
| 670 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver |
| 671 | */ |
| 672 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) |
| 673 | { |
| 674 | int ret = __verify_memory_type(q, req->memory, req->type); |
| 675 | |
| 676 | return ret ? ret : __reqbufs(q, req); |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(vb2_reqbufs); |
| 679 | |
| 680 | /** |
| 681 | * __create_bufs() - Allocate buffers and any required auxiliary structs |
| 682 | * @q: videobuf2 queue |
| 683 | * @create: creation parameters, passed from userspace to vidioc_create_bufs |
| 684 | * handler in driver |
| 685 | * |
| 686 | * Should be called from vidioc_create_bufs ioctl handler of a driver. |
| 687 | * This function: |
| 688 | * 1) verifies parameter sanity |
| 689 | * 2) calls the .queue_setup() queue operation |
| 690 | * 3) performs any necessary memory allocations |
| 691 | * |
| 692 | * The return values from this function are intended to be directly returned |
| 693 | * from vidioc_create_bufs handler in driver. |
| 694 | */ |
| 695 | static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) |
| 696 | { |
| 697 | unsigned int num_planes = 0, num_buffers, allocated_buffers; |
| 698 | int ret; |
| 699 | |
| 700 | if (q->num_buffers == VIDEO_MAX_FRAME) { |
| 701 | dprintk(1, "%s(): maximum number of buffers already allocated\n", |
| 702 | __func__); |
| 703 | return -ENOBUFS; |
| 704 | } |
| 705 | |
| 706 | if (!q->num_buffers) { |
| 707 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); |
| 708 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
| 709 | q->memory = create->memory; |
| 710 | } |
| 711 | |
| 712 | num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers); |
| 713 | |
| 714 | /* |
| 715 | * Ask the driver, whether the requested number of buffers, planes per |
| 716 | * buffer and their sizes are acceptable |
| 717 | */ |
| 718 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, |
| 719 | &num_planes, q->plane_sizes, q->alloc_ctx); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | |
| 723 | /* Finally, allocate buffers and video memory */ |
| 724 | ret = __vb2_queue_alloc(q, create->memory, num_buffers, |
| 725 | num_planes); |
| 726 | if (ret == 0) { |
| 727 | dprintk(1, "Memory allocation failed\n"); |
| 728 | return -ENOMEM; |
| 729 | } |
| 730 | |
| 731 | allocated_buffers = ret; |
| 732 | |
| 733 | /* |
| 734 | * Check if driver can handle the so far allocated number of buffers. |
| 735 | */ |
| 736 | if (ret < num_buffers) { |
| 737 | num_buffers = ret; |
| 738 | |
| 739 | /* |
| 740 | * q->num_buffers contains the total number of buffers, that the |
| 741 | * queue driver has set up |
| 742 | */ |
| 743 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, |
| 744 | &num_planes, q->plane_sizes, q->alloc_ctx); |
| 745 | |
| 746 | if (!ret && allocated_buffers < num_buffers) |
| 747 | ret = -ENOMEM; |
| 748 | |
| 749 | /* |
| 750 | * Either the driver has accepted a smaller number of buffers, |
| 751 | * or .queue_setup() returned an error |
| 752 | */ |
| 753 | } |
| 754 | |
| 755 | q->num_buffers += allocated_buffers; |
| 756 | |
| 757 | if (ret < 0) { |
| 758 | __vb2_queue_free(q, allocated_buffers); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Return the number of successfully allocated buffers |
| 764 | * to the userspace. |
| 765 | */ |
| 766 | create->count = allocated_buffers; |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the |
| 773 | * memory and type values. |
| 774 | * @q: videobuf2 queue |
| 775 | * @create: creation parameters, passed from userspace to vidioc_create_bufs |
| 776 | * handler in driver |
| 777 | */ |
| 778 | int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) |
| 779 | { |
| 780 | int ret = __verify_memory_type(q, create->memory, create->format.type); |
| 781 | |
| 782 | create->index = q->num_buffers; |
| 783 | if (create->count == 0) |
| 784 | return ret != -EBUSY ? ret : 0; |
| 785 | return ret ? ret : __create_bufs(q, create); |
| 786 | } |
| 787 | EXPORT_SYMBOL_GPL(vb2_create_bufs); |
| 788 | |
| 789 | /** |
| 790 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane |
| 791 | * @vb: vb2_buffer to which the plane in question belongs to |
| 792 | * @plane_no: plane number for which the address is to be returned |
| 793 | * |
| 794 | * This function returns a kernel virtual address of a given plane if |
| 795 | * such a mapping exist, NULL otherwise. |
| 796 | */ |
| 797 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) |
| 798 | { |
| 799 | struct vb2_queue *q = vb->vb2_queue; |
| 800 | |
| 801 | if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv) |
| 802 | return NULL; |
| 803 | |
| 804 | return call_memop(q, vaddr, vb->planes[plane_no].mem_priv); |
| 805 | |
| 806 | } |
| 807 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); |
| 808 | |
| 809 | /** |
| 810 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane |
| 811 | * @vb: vb2_buffer to which the plane in question belongs to |
| 812 | * @plane_no: plane number for which the cookie is to be returned |
| 813 | * |
| 814 | * This function returns an allocator specific cookie for a given plane if |
| 815 | * available, NULL otherwise. The allocator should provide some simple static |
| 816 | * inline function, which would convert this cookie to the allocator specific |
| 817 | * type that can be used directly by the driver to access the buffer. This can |
| 818 | * be for example physical address, pointer to scatter list or IOMMU mapping. |
| 819 | */ |
| 820 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) |
| 821 | { |
| 822 | struct vb2_queue *q = vb->vb2_queue; |
| 823 | |
| 824 | if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv) |
| 825 | return NULL; |
| 826 | |
| 827 | return call_memop(q, cookie, vb->planes[plane_no].mem_priv); |
| 828 | } |
| 829 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); |
| 830 | |
| 831 | /** |
| 832 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished |
| 833 | * @vb: vb2_buffer returned from the driver |
| 834 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully |
| 835 | * or VB2_BUF_STATE_ERROR if the operation finished with an error |
| 836 | * |
| 837 | * This function should be called by the driver after a hardware operation on |
| 838 | * a buffer is finished and the buffer may be returned to userspace. The driver |
| 839 | * cannot use this buffer anymore until it is queued back to it by videobuf |
| 840 | * by the means of buf_queue callback. Only buffers previously queued to the |
| 841 | * driver by buf_queue can be passed to this function. |
| 842 | */ |
| 843 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) |
| 844 | { |
| 845 | struct vb2_queue *q = vb->vb2_queue; |
| 846 | unsigned long flags; |
| 847 | unsigned int plane; |
| 848 | |
| 849 | if (vb->state != VB2_BUF_STATE_ACTIVE) |
| 850 | return; |
| 851 | |
| 852 | if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) |
| 853 | return; |
| 854 | |
| 855 | dprintk(4, "Done processing on buffer %d, state: %d\n", |
| 856 | vb->v4l2_buf.index, vb->state); |
| 857 | |
| 858 | /* sync buffers */ |
| 859 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 860 | call_memop(q, finish, vb->planes[plane].mem_priv); |
| 861 | |
| 862 | /* Add the buffer to the done buffers list */ |
| 863 | spin_lock_irqsave(&q->done_lock, flags); |
| 864 | vb->state = state; |
| 865 | list_add_tail(&vb->done_entry, &q->done_list); |
| 866 | atomic_dec(&q->queued_count); |
| 867 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 868 | |
| 869 | /* Inform any processes that may be waiting for buffers */ |
| 870 | wake_up(&q->done_wq); |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(vb2_buffer_done); |
| 873 | |
| 874 | /** |
| 875 | * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a |
| 876 | * v4l2_buffer by the userspace. The caller has already verified that struct |
| 877 | * v4l2_buffer has a valid number of planes. |
| 878 | */ |
| 879 | static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b, |
| 880 | struct v4l2_plane *v4l2_planes) |
| 881 | { |
| 882 | unsigned int plane; |
| 883 | |
| 884 | if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { |
| 885 | /* Fill in driver-provided information for OUTPUT types */ |
| 886 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { |
| 887 | /* |
| 888 | * Will have to go up to b->length when API starts |
| 889 | * accepting variable number of planes. |
| 890 | */ |
| 891 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 892 | v4l2_planes[plane].bytesused = |
| 893 | b->m.planes[plane].bytesused; |
| 894 | v4l2_planes[plane].data_offset = |
| 895 | b->m.planes[plane].data_offset; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 900 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 901 | v4l2_planes[plane].m.userptr = |
| 902 | b->m.planes[plane].m.userptr; |
| 903 | v4l2_planes[plane].length = |
| 904 | b->m.planes[plane].length; |
| 905 | } |
| 906 | } |
| 907 | if (b->memory == V4L2_MEMORY_DMABUF) { |
| 908 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 909 | v4l2_planes[plane].m.fd = |
| 910 | b->m.planes[plane].m.fd; |
| 911 | v4l2_planes[plane].length = |
| 912 | b->m.planes[plane].length; |
| 913 | v4l2_planes[plane].data_offset = |
| 914 | b->m.planes[plane].data_offset; |
| 915 | } |
| 916 | } |
| 917 | } else { |
| 918 | /* |
| 919 | * Single-planar buffers do not use planes array, |
| 920 | * so fill in relevant v4l2_buffer struct fields instead. |
| 921 | * In videobuf we use our internal V4l2_planes struct for |
| 922 | * single-planar buffers as well, for simplicity. |
| 923 | */ |
| 924 | if (V4L2_TYPE_IS_OUTPUT(b->type)) |
| 925 | v4l2_planes[0].bytesused = b->bytesused; |
| 926 | |
| 927 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 928 | v4l2_planes[0].m.userptr = b->m.userptr; |
| 929 | v4l2_planes[0].length = b->length; |
| 930 | } |
| 931 | |
| 932 | if (b->memory == V4L2_MEMORY_DMABUF) { |
| 933 | v4l2_planes[0].m.fd = b->m.fd; |
| 934 | v4l2_planes[0].length = b->length; |
| 935 | v4l2_planes[0].data_offset = 0; |
| 936 | } |
| 937 | |
| 938 | } |
| 939 | |
| 940 | vb->v4l2_buf.field = b->field; |
| 941 | vb->v4l2_buf.timestamp = b->timestamp; |
| 942 | vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS; |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer |
| 947 | */ |
| 948 | static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
| 949 | { |
| 950 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 951 | struct vb2_queue *q = vb->vb2_queue; |
| 952 | void *mem_priv; |
| 953 | unsigned int plane; |
| 954 | int ret; |
| 955 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); |
| 956 | |
| 957 | /* Copy relevant information provided by the userspace */ |
| 958 | __fill_vb2_buffer(vb, b, planes); |
| 959 | |
| 960 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 961 | /* Skip the plane if already verified */ |
| 962 | if (vb->v4l2_planes[plane].m.userptr && |
| 963 | vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr |
| 964 | && vb->v4l2_planes[plane].length == planes[plane].length) |
| 965 | continue; |
| 966 | |
| 967 | dprintk(3, "qbuf: userspace address for plane %d changed, " |
| 968 | "reacquiring memory\n", plane); |
| 969 | |
| 970 | /* Check if the provided plane buffer is large enough */ |
| 971 | if (planes[plane].length < q->plane_sizes[plane]) { |
| 972 | ret = -EINVAL; |
| 973 | goto err; |
| 974 | } |
| 975 | |
| 976 | /* Release previously acquired memory if present */ |
| 977 | if (vb->planes[plane].mem_priv) |
| 978 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); |
| 979 | |
| 980 | vb->planes[plane].mem_priv = NULL; |
| 981 | vb->v4l2_planes[plane].m.userptr = 0; |
| 982 | vb->v4l2_planes[plane].length = 0; |
| 983 | |
| 984 | /* Acquire each plane's memory */ |
| 985 | mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane], |
| 986 | planes[plane].m.userptr, |
| 987 | planes[plane].length, write); |
| 988 | if (IS_ERR_OR_NULL(mem_priv)) { |
| 989 | dprintk(1, "qbuf: failed acquiring userspace " |
| 990 | "memory for plane %d\n", plane); |
| 991 | ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL; |
| 992 | goto err; |
| 993 | } |
| 994 | vb->planes[plane].mem_priv = mem_priv; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Call driver-specific initialization on the newly acquired buffer, |
| 999 | * if provided. |
| 1000 | */ |
| 1001 | ret = call_qop(q, buf_init, vb); |
| 1002 | if (ret) { |
| 1003 | dprintk(1, "qbuf: buffer initialization failed\n"); |
| 1004 | goto err; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Now that everything is in order, copy relevant information |
| 1009 | * provided by userspace. |
| 1010 | */ |
| 1011 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 1012 | vb->v4l2_planes[plane] = planes[plane]; |
| 1013 | |
| 1014 | return 0; |
| 1015 | err: |
| 1016 | /* In case of errors, release planes that were already acquired */ |
| 1017 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1018 | if (vb->planes[plane].mem_priv) |
| 1019 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); |
| 1020 | vb->planes[plane].mem_priv = NULL; |
| 1021 | vb->v4l2_planes[plane].m.userptr = 0; |
| 1022 | vb->v4l2_planes[plane].length = 0; |
| 1023 | } |
| 1024 | |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * __qbuf_mmap() - handle qbuf of an MMAP buffer |
| 1030 | */ |
| 1031 | static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
| 1032 | { |
| 1033 | __fill_vb2_buffer(vb, b, vb->v4l2_planes); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer |
| 1039 | */ |
| 1040 | static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
| 1041 | { |
| 1042 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 1043 | struct vb2_queue *q = vb->vb2_queue; |
| 1044 | void *mem_priv; |
| 1045 | unsigned int plane; |
| 1046 | int ret; |
| 1047 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); |
| 1048 | |
| 1049 | /* Verify and copy relevant information provided by the userspace */ |
| 1050 | __fill_vb2_buffer(vb, b, planes); |
| 1051 | |
| 1052 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1053 | struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd); |
| 1054 | |
| 1055 | if (IS_ERR_OR_NULL(dbuf)) { |
| 1056 | dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n", |
| 1057 | plane); |
| 1058 | ret = -EINVAL; |
| 1059 | goto err; |
| 1060 | } |
| 1061 | |
| 1062 | /* use DMABUF size if length is not provided */ |
| 1063 | if (planes[plane].length == 0) |
| 1064 | planes[plane].length = dbuf->size; |
| 1065 | |
| 1066 | if (planes[plane].length < planes[plane].data_offset + |
| 1067 | q->plane_sizes[plane]) { |
| 1068 | ret = -EINVAL; |
| 1069 | goto err; |
| 1070 | } |
| 1071 | |
| 1072 | /* Skip the plane if already verified */ |
| 1073 | if (dbuf == vb->planes[plane].dbuf && |
| 1074 | vb->v4l2_planes[plane].length == planes[plane].length) { |
| 1075 | dma_buf_put(dbuf); |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | dprintk(1, "qbuf: buffer for plane %d changed\n", plane); |
| 1080 | |
| 1081 | /* Release previously acquired memory if present */ |
| 1082 | __vb2_plane_dmabuf_put(q, &vb->planes[plane]); |
| 1083 | memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane)); |
| 1084 | |
| 1085 | /* Acquire each plane's memory */ |
| 1086 | mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane], |
| 1087 | dbuf, planes[plane].length, write); |
| 1088 | if (IS_ERR(mem_priv)) { |
| 1089 | dprintk(1, "qbuf: failed to attach dmabuf\n"); |
| 1090 | ret = PTR_ERR(mem_priv); |
| 1091 | dma_buf_put(dbuf); |
| 1092 | goto err; |
| 1093 | } |
| 1094 | |
| 1095 | vb->planes[plane].dbuf = dbuf; |
| 1096 | vb->planes[plane].mem_priv = mem_priv; |
| 1097 | } |
| 1098 | |
| 1099 | /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but |
| 1100 | * really we want to do this just before the DMA, not while queueing |
| 1101 | * the buffer(s).. |
| 1102 | */ |
| 1103 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1104 | ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv); |
| 1105 | if (ret) { |
| 1106 | dprintk(1, "qbuf: failed to map dmabuf for plane %d\n", |
| 1107 | plane); |
| 1108 | goto err; |
| 1109 | } |
| 1110 | vb->planes[plane].dbuf_mapped = 1; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Call driver-specific initialization on the newly acquired buffer, |
| 1115 | * if provided. |
| 1116 | */ |
| 1117 | ret = call_qop(q, buf_init, vb); |
| 1118 | if (ret) { |
| 1119 | dprintk(1, "qbuf: buffer initialization failed\n"); |
| 1120 | goto err; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Now that everything is in order, copy relevant information |
| 1125 | * provided by userspace. |
| 1126 | */ |
| 1127 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 1128 | vb->v4l2_planes[plane] = planes[plane]; |
| 1129 | |
| 1130 | return 0; |
| 1131 | err: |
| 1132 | /* In case of errors, release planes that were already acquired */ |
| 1133 | __vb2_buf_dmabuf_put(vb); |
| 1134 | |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing |
| 1140 | */ |
| 1141 | static void __enqueue_in_driver(struct vb2_buffer *vb) |
| 1142 | { |
| 1143 | struct vb2_queue *q = vb->vb2_queue; |
| 1144 | unsigned int plane; |
| 1145 | |
| 1146 | vb->state = VB2_BUF_STATE_ACTIVE; |
| 1147 | atomic_inc(&q->queued_count); |
| 1148 | |
| 1149 | /* sync buffers */ |
| 1150 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 1151 | call_memop(q, prepare, vb->planes[plane].mem_priv); |
| 1152 | |
| 1153 | q->ops->buf_queue(vb); |
| 1154 | } |
| 1155 | |
| 1156 | static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
| 1157 | { |
| 1158 | struct vb2_queue *q = vb->vb2_queue; |
| 1159 | int ret; |
| 1160 | |
| 1161 | switch (q->memory) { |
| 1162 | case V4L2_MEMORY_MMAP: |
| 1163 | ret = __qbuf_mmap(vb, b); |
| 1164 | break; |
| 1165 | case V4L2_MEMORY_USERPTR: |
| 1166 | ret = __qbuf_userptr(vb, b); |
| 1167 | break; |
| 1168 | case V4L2_MEMORY_DMABUF: |
| 1169 | ret = __qbuf_dmabuf(vb, b); |
| 1170 | break; |
| 1171 | default: |
| 1172 | WARN(1, "Invalid queue type\n"); |
| 1173 | ret = -EINVAL; |
| 1174 | } |
| 1175 | |
| 1176 | if (!ret) |
| 1177 | ret = call_qop(q, buf_prepare, vb); |
| 1178 | if (ret) |
| 1179 | dprintk(1, "qbuf: buffer preparation failed: %d\n", ret); |
| 1180 | else |
| 1181 | vb->state = VB2_BUF_STATE_PREPARED; |
| 1182 | |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel |
| 1188 | * @q: videobuf2 queue |
| 1189 | * @b: buffer structure passed from userspace to vidioc_prepare_buf |
| 1190 | * handler in driver |
| 1191 | * |
| 1192 | * Should be called from vidioc_prepare_buf ioctl handler of a driver. |
| 1193 | * This function: |
| 1194 | * 1) verifies the passed buffer, |
| 1195 | * 2) calls buf_prepare callback in the driver (if provided), in which |
| 1196 | * driver-specific buffer initialization can be performed, |
| 1197 | * |
| 1198 | * The return values from this function are intended to be directly returned |
| 1199 | * from vidioc_prepare_buf handler in driver. |
| 1200 | */ |
| 1201 | int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 1202 | { |
| 1203 | struct vb2_buffer *vb; |
| 1204 | int ret; |
| 1205 | |
| 1206 | if (q->fileio) { |
| 1207 | dprintk(1, "%s(): file io in progress\n", __func__); |
| 1208 | return -EBUSY; |
| 1209 | } |
| 1210 | |
| 1211 | if (b->type != q->type) { |
| 1212 | dprintk(1, "%s(): invalid buffer type\n", __func__); |
| 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | |
| 1216 | if (b->index >= q->num_buffers) { |
| 1217 | dprintk(1, "%s(): buffer index out of range\n", __func__); |
| 1218 | return -EINVAL; |
| 1219 | } |
| 1220 | |
| 1221 | vb = q->bufs[b->index]; |
| 1222 | if (NULL == vb) { |
| 1223 | /* Should never happen */ |
| 1224 | dprintk(1, "%s(): buffer is NULL\n", __func__); |
| 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | |
| 1228 | if (b->memory != q->memory) { |
| 1229 | dprintk(1, "%s(): invalid memory type\n", __func__); |
| 1230 | return -EINVAL; |
| 1231 | } |
| 1232 | |
| 1233 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { |
| 1234 | dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state); |
| 1235 | return -EINVAL; |
| 1236 | } |
| 1237 | ret = __verify_planes_array(vb, b); |
| 1238 | if (ret < 0) |
| 1239 | return ret; |
| 1240 | ret = __buf_prepare(vb, b); |
| 1241 | if (ret < 0) |
| 1242 | return ret; |
| 1243 | |
| 1244 | __fill_v4l2_buffer(vb, b); |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | EXPORT_SYMBOL_GPL(vb2_prepare_buf); |
| 1249 | |
| 1250 | /** |
| 1251 | * vb2_qbuf() - Queue a buffer from userspace |
| 1252 | * @q: videobuf2 queue |
| 1253 | * @b: buffer structure passed from userspace to vidioc_qbuf handler |
| 1254 | * in driver |
| 1255 | * |
| 1256 | * Should be called from vidioc_qbuf ioctl handler of a driver. |
| 1257 | * This function: |
| 1258 | * 1) verifies the passed buffer, |
| 1259 | * 2) if necessary, calls buf_prepare callback in the driver (if provided), in |
| 1260 | * which driver-specific buffer initialization can be performed, |
| 1261 | * 3) if streaming is on, queues the buffer in driver by the means of buf_queue |
| 1262 | * callback for processing. |
| 1263 | * |
| 1264 | * The return values from this function are intended to be directly returned |
| 1265 | * from vidioc_qbuf handler in driver. |
| 1266 | */ |
| 1267 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 1268 | { |
| 1269 | struct rw_semaphore *mmap_sem = NULL; |
| 1270 | struct vb2_buffer *vb; |
| 1271 | int ret = 0; |
| 1272 | |
| 1273 | /* |
| 1274 | * In case of user pointer buffers vb2 allocator needs to get direct |
| 1275 | * access to userspace pages. This requires getting read access on |
| 1276 | * mmap semaphore in the current process structure. The same |
| 1277 | * semaphore is taken before calling mmap operation, while both mmap |
| 1278 | * and qbuf are called by the driver or v4l2 core with driver's lock |
| 1279 | * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in |
| 1280 | * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core |
| 1281 | * release driver's lock, takes mmap_sem and then takes again driver's |
| 1282 | * lock. |
| 1283 | * |
| 1284 | * To avoid race with other vb2 calls, which might be called after |
| 1285 | * releasing driver's lock, this operation is performed at the |
| 1286 | * beggining of qbuf processing. This way the queue status is |
| 1287 | * consistent after getting driver's lock back. |
| 1288 | */ |
| 1289 | if (q->memory == V4L2_MEMORY_USERPTR) { |
| 1290 | mmap_sem = ¤t->mm->mmap_sem; |
| 1291 | call_qop(q, wait_prepare, q); |
| 1292 | down_read(mmap_sem); |
| 1293 | call_qop(q, wait_finish, q); |
| 1294 | } |
| 1295 | |
| 1296 | if (q->fileio) { |
| 1297 | dprintk(1, "qbuf: file io in progress\n"); |
| 1298 | ret = -EBUSY; |
| 1299 | goto unlock; |
| 1300 | } |
| 1301 | |
| 1302 | if (b->type != q->type) { |
| 1303 | dprintk(1, "qbuf: invalid buffer type\n"); |
| 1304 | ret = -EINVAL; |
| 1305 | goto unlock; |
| 1306 | } |
| 1307 | |
| 1308 | if (b->index >= q->num_buffers) { |
| 1309 | dprintk(1, "qbuf: buffer index out of range\n"); |
| 1310 | ret = -EINVAL; |
| 1311 | goto unlock; |
| 1312 | } |
| 1313 | |
| 1314 | vb = q->bufs[b->index]; |
| 1315 | if (NULL == vb) { |
| 1316 | /* Should never happen */ |
| 1317 | dprintk(1, "qbuf: buffer is NULL\n"); |
| 1318 | ret = -EINVAL; |
| 1319 | goto unlock; |
| 1320 | } |
| 1321 | |
| 1322 | if (b->memory != q->memory) { |
| 1323 | dprintk(1, "qbuf: invalid memory type\n"); |
| 1324 | ret = -EINVAL; |
| 1325 | goto unlock; |
| 1326 | } |
| 1327 | ret = __verify_planes_array(vb, b); |
| 1328 | if (ret) |
| 1329 | goto unlock; |
| 1330 | |
| 1331 | switch (vb->state) { |
| 1332 | case VB2_BUF_STATE_DEQUEUED: |
| 1333 | ret = __buf_prepare(vb, b); |
| 1334 | if (ret) |
| 1335 | goto unlock; |
| 1336 | case VB2_BUF_STATE_PREPARED: |
| 1337 | break; |
| 1338 | default: |
| 1339 | dprintk(1, "qbuf: buffer already in use\n"); |
| 1340 | ret = -EINVAL; |
| 1341 | goto unlock; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Add to the queued buffers list, a buffer will stay on it until |
| 1346 | * dequeued in dqbuf. |
| 1347 | */ |
| 1348 | list_add_tail(&vb->queued_entry, &q->queued_list); |
| 1349 | vb->state = VB2_BUF_STATE_QUEUED; |
| 1350 | |
| 1351 | /* |
| 1352 | * If already streaming, give the buffer to driver for processing. |
| 1353 | * If not, the buffer will be given to driver on next streamon. |
| 1354 | */ |
| 1355 | if (q->streaming) |
| 1356 | __enqueue_in_driver(vb); |
| 1357 | |
| 1358 | /* Fill buffer information for the userspace */ |
| 1359 | __fill_v4l2_buffer(vb, b); |
| 1360 | |
| 1361 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index); |
| 1362 | unlock: |
| 1363 | if (mmap_sem) |
| 1364 | up_read(mmap_sem); |
| 1365 | return ret; |
| 1366 | } |
| 1367 | EXPORT_SYMBOL_GPL(vb2_qbuf); |
| 1368 | |
| 1369 | /** |
| 1370 | * __vb2_wait_for_done_vb() - wait for a buffer to become available |
| 1371 | * for dequeuing |
| 1372 | * |
| 1373 | * Will sleep if required for nonblocking == false. |
| 1374 | */ |
| 1375 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) |
| 1376 | { |
| 1377 | /* |
| 1378 | * All operations on vb_done_list are performed under done_lock |
| 1379 | * spinlock protection. However, buffers may be removed from |
| 1380 | * it and returned to userspace only while holding both driver's |
| 1381 | * lock and the done_lock spinlock. Thus we can be sure that as |
| 1382 | * long as we hold the driver's lock, the list will remain not |
| 1383 | * empty if list_empty() check succeeds. |
| 1384 | */ |
| 1385 | |
| 1386 | for (;;) { |
| 1387 | int ret; |
| 1388 | |
| 1389 | if (!q->streaming) { |
| 1390 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | |
| 1394 | if (!list_empty(&q->done_list)) { |
| 1395 | /* |
| 1396 | * Found a buffer that we were waiting for. |
| 1397 | */ |
| 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | if (nonblocking) { |
| 1402 | dprintk(1, "Nonblocking and no buffers to dequeue, " |
| 1403 | "will not wait\n"); |
| 1404 | return -EAGAIN; |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * We are streaming and blocking, wait for another buffer to |
| 1409 | * become ready or for streamoff. Driver's lock is released to |
| 1410 | * allow streamoff or qbuf to be called while waiting. |
| 1411 | */ |
| 1412 | call_qop(q, wait_prepare, q); |
| 1413 | |
| 1414 | /* |
| 1415 | * All locks have been released, it is safe to sleep now. |
| 1416 | */ |
| 1417 | dprintk(3, "Will sleep waiting for buffers\n"); |
| 1418 | ret = wait_event_interruptible(q->done_wq, |
| 1419 | !list_empty(&q->done_list) || !q->streaming); |
| 1420 | |
| 1421 | /* |
| 1422 | * We need to reevaluate both conditions again after reacquiring |
| 1423 | * the locks or return an error if one occurred. |
| 1424 | */ |
| 1425 | call_qop(q, wait_finish, q); |
| 1426 | if (ret) { |
| 1427 | dprintk(1, "Sleep was interrupted\n"); |
| 1428 | return ret; |
| 1429 | } |
| 1430 | } |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * __vb2_get_done_vb() - get a buffer ready for dequeuing |
| 1436 | * |
| 1437 | * Will sleep if required for nonblocking == false. |
| 1438 | */ |
| 1439 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, |
| 1440 | struct v4l2_buffer *b, int nonblocking) |
| 1441 | { |
| 1442 | unsigned long flags; |
| 1443 | int ret; |
| 1444 | |
| 1445 | /* |
| 1446 | * Wait for at least one buffer to become available on the done_list. |
| 1447 | */ |
| 1448 | ret = __vb2_wait_for_done_vb(q, nonblocking); |
| 1449 | if (ret) |
| 1450 | return ret; |
| 1451 | |
| 1452 | /* |
| 1453 | * Driver's lock has been held since we last verified that done_list |
| 1454 | * is not empty, so no need for another list_empty(done_list) check. |
| 1455 | */ |
| 1456 | spin_lock_irqsave(&q->done_lock, flags); |
| 1457 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); |
| 1458 | /* |
| 1459 | * Only remove the buffer from done_list if v4l2_buffer can handle all |
| 1460 | * the planes. |
| 1461 | */ |
| 1462 | ret = __verify_planes_array(*vb, b); |
| 1463 | if (!ret) |
| 1464 | list_del(&(*vb)->done_entry); |
| 1465 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1466 | |
| 1467 | return ret; |
| 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 |
| 1472 | * @q: videobuf2 queue |
| 1473 | * |
| 1474 | * This function will wait until all buffers that have been given to the driver |
| 1475 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call |
| 1476 | * wait_prepare, wait_finish pair. It is intended to be called with all locks |
| 1477 | * taken, for example from stop_streaming() callback. |
| 1478 | */ |
| 1479 | int vb2_wait_for_all_buffers(struct vb2_queue *q) |
| 1480 | { |
| 1481 | if (!q->streaming) { |
| 1482 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 1483 | return -EINVAL; |
| 1484 | } |
| 1485 | |
| 1486 | wait_event(q->done_wq, !atomic_read(&q->queued_count)); |
| 1487 | return 0; |
| 1488 | } |
| 1489 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); |
| 1490 | |
| 1491 | /** |
| 1492 | * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state |
| 1493 | */ |
| 1494 | static void __vb2_dqbuf(struct vb2_buffer *vb) |
| 1495 | { |
| 1496 | struct vb2_queue *q = vb->vb2_queue; |
| 1497 | unsigned int i; |
| 1498 | |
| 1499 | /* nothing to do if the buffer is already dequeued */ |
| 1500 | if (vb->state == VB2_BUF_STATE_DEQUEUED) |
| 1501 | return; |
| 1502 | |
| 1503 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 1504 | |
| 1505 | /* unmap DMABUF buffer */ |
| 1506 | if (q->memory == V4L2_MEMORY_DMABUF) |
| 1507 | for (i = 0; i < vb->num_planes; ++i) { |
| 1508 | if (!vb->planes[i].dbuf_mapped) |
| 1509 | continue; |
| 1510 | call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv); |
| 1511 | vb->planes[i].dbuf_mapped = 0; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /** |
| 1516 | * vb2_dqbuf() - Dequeue a buffer to the userspace |
| 1517 | * @q: videobuf2 queue |
| 1518 | * @b: buffer structure passed from userspace to vidioc_dqbuf handler |
| 1519 | * in driver |
| 1520 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no |
| 1521 | * buffers ready for dequeuing are present. Normally the driver |
| 1522 | * would be passing (file->f_flags & O_NONBLOCK) here |
| 1523 | * |
| 1524 | * Should be called from vidioc_dqbuf ioctl handler of a driver. |
| 1525 | * This function: |
| 1526 | * 1) verifies the passed buffer, |
| 1527 | * 2) calls buf_finish callback in the driver (if provided), in which |
| 1528 | * driver can perform any additional operations that may be required before |
| 1529 | * returning the buffer to userspace, such as cache sync, |
| 1530 | * 3) the buffer struct members are filled with relevant information for |
| 1531 | * the userspace. |
| 1532 | * |
| 1533 | * The return values from this function are intended to be directly returned |
| 1534 | * from vidioc_dqbuf handler in driver. |
| 1535 | */ |
| 1536 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) |
| 1537 | { |
| 1538 | struct vb2_buffer *vb = NULL; |
| 1539 | int ret; |
| 1540 | |
| 1541 | if (q->fileio) { |
| 1542 | dprintk(1, "dqbuf: file io in progress\n"); |
| 1543 | return -EBUSY; |
| 1544 | } |
| 1545 | |
| 1546 | if (b->type != q->type) { |
| 1547 | dprintk(1, "dqbuf: invalid buffer type\n"); |
| 1548 | return -EINVAL; |
| 1549 | } |
| 1550 | ret = __vb2_get_done_vb(q, &vb, b, nonblocking); |
| 1551 | if (ret < 0) |
| 1552 | return ret; |
| 1553 | |
| 1554 | ret = call_qop(q, buf_finish, vb); |
| 1555 | if (ret) { |
| 1556 | dprintk(1, "dqbuf: buffer finish failed\n"); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | switch (vb->state) { |
| 1561 | case VB2_BUF_STATE_DONE: |
| 1562 | dprintk(3, "dqbuf: Returning done buffer\n"); |
| 1563 | break; |
| 1564 | case VB2_BUF_STATE_ERROR: |
| 1565 | dprintk(3, "dqbuf: Returning done buffer with errors\n"); |
| 1566 | break; |
| 1567 | default: |
| 1568 | dprintk(1, "dqbuf: Invalid buffer state\n"); |
| 1569 | return -EINVAL; |
| 1570 | } |
| 1571 | |
| 1572 | /* Fill buffer information for the userspace */ |
| 1573 | __fill_v4l2_buffer(vb, b); |
| 1574 | /* Remove from videobuf queue */ |
| 1575 | list_del(&vb->queued_entry); |
| 1576 | /* go back to dequeued state */ |
| 1577 | __vb2_dqbuf(vb); |
| 1578 | |
| 1579 | dprintk(1, "dqbuf of buffer %d, with state %d\n", |
| 1580 | vb->v4l2_buf.index, vb->state); |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | EXPORT_SYMBOL_GPL(vb2_dqbuf); |
| 1585 | |
| 1586 | /** |
| 1587 | * __vb2_queue_cancel() - cancel and stop (pause) streaming |
| 1588 | * |
| 1589 | * Removes all queued buffers from driver's queue and all buffers queued by |
| 1590 | * userspace from videobuf's queue. Returns to state after reqbufs. |
| 1591 | */ |
| 1592 | static void __vb2_queue_cancel(struct vb2_queue *q) |
| 1593 | { |
| 1594 | unsigned int i; |
| 1595 | |
| 1596 | /* |
| 1597 | * Tell driver to stop all transactions and release all queued |
| 1598 | * buffers. |
| 1599 | */ |
| 1600 | if (q->streaming) |
| 1601 | call_qop(q, stop_streaming, q); |
| 1602 | q->streaming = 0; |
| 1603 | |
| 1604 | /* |
| 1605 | * Remove all buffers from videobuf's list... |
| 1606 | */ |
| 1607 | INIT_LIST_HEAD(&q->queued_list); |
| 1608 | /* |
| 1609 | * ...and done list; userspace will not receive any buffers it |
| 1610 | * has not already dequeued before initiating cancel. |
| 1611 | */ |
| 1612 | INIT_LIST_HEAD(&q->done_list); |
| 1613 | atomic_set(&q->queued_count, 0); |
| 1614 | wake_up_all(&q->done_wq); |
| 1615 | |
| 1616 | /* |
| 1617 | * Reinitialize all buffers for next use. |
| 1618 | */ |
| 1619 | for (i = 0; i < q->num_buffers; ++i) |
| 1620 | __vb2_dqbuf(q->bufs[i]); |
| 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * vb2_streamon - start streaming |
| 1625 | * @q: videobuf2 queue |
| 1626 | * @type: type argument passed from userspace to vidioc_streamon handler |
| 1627 | * |
| 1628 | * Should be called from vidioc_streamon handler of a driver. |
| 1629 | * This function: |
| 1630 | * 1) verifies current state |
| 1631 | * 2) passes any previously queued buffers to the driver and starts streaming |
| 1632 | * |
| 1633 | * The return values from this function are intended to be directly returned |
| 1634 | * from vidioc_streamon handler in the driver. |
| 1635 | */ |
| 1636 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1637 | { |
| 1638 | struct vb2_buffer *vb; |
| 1639 | int ret; |
| 1640 | |
| 1641 | if (q->fileio) { |
| 1642 | dprintk(1, "streamon: file io in progress\n"); |
| 1643 | return -EBUSY; |
| 1644 | } |
| 1645 | |
| 1646 | if (type != q->type) { |
| 1647 | dprintk(1, "streamon: invalid stream type\n"); |
| 1648 | return -EINVAL; |
| 1649 | } |
| 1650 | |
| 1651 | if (q->streaming) { |
| 1652 | dprintk(1, "streamon: already streaming\n"); |
| 1653 | return -EBUSY; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * If any buffers were queued before streamon, |
| 1658 | * we can now pass them to driver for processing. |
| 1659 | */ |
| 1660 | list_for_each_entry(vb, &q->queued_list, queued_entry) |
| 1661 | __enqueue_in_driver(vb); |
| 1662 | |
| 1663 | /* |
| 1664 | * Let driver notice that streaming state has been enabled. |
| 1665 | */ |
| 1666 | ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count)); |
| 1667 | if (ret) { |
| 1668 | dprintk(1, "streamon: driver refused to start streaming\n"); |
| 1669 | __vb2_queue_cancel(q); |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | q->streaming = 1; |
| 1674 | |
| 1675 | dprintk(3, "Streamon successful\n"); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | EXPORT_SYMBOL_GPL(vb2_streamon); |
| 1679 | |
| 1680 | |
| 1681 | /** |
| 1682 | * vb2_streamoff - stop streaming |
| 1683 | * @q: videobuf2 queue |
| 1684 | * @type: type argument passed from userspace to vidioc_streamoff handler |
| 1685 | * |
| 1686 | * Should be called from vidioc_streamoff handler of a driver. |
| 1687 | * This function: |
| 1688 | * 1) verifies current state, |
| 1689 | * 2) stop streaming and dequeues any queued buffers, including those previously |
| 1690 | * passed to the driver (after waiting for the driver to finish). |
| 1691 | * |
| 1692 | * This call can be used for pausing playback. |
| 1693 | * The return values from this function are intended to be directly returned |
| 1694 | * from vidioc_streamoff handler in the driver |
| 1695 | */ |
| 1696 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1697 | { |
| 1698 | if (q->fileio) { |
| 1699 | dprintk(1, "streamoff: file io in progress\n"); |
| 1700 | return -EBUSY; |
| 1701 | } |
| 1702 | |
| 1703 | if (type != q->type) { |
| 1704 | dprintk(1, "streamoff: invalid stream type\n"); |
| 1705 | return -EINVAL; |
| 1706 | } |
| 1707 | |
| 1708 | if (!q->streaming) { |
| 1709 | dprintk(1, "streamoff: not streaming\n"); |
| 1710 | return -EINVAL; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
| 1714 | * Cancel will pause streaming and remove all buffers from the driver |
| 1715 | * and videobuf, effectively returning control over them to userspace. |
| 1716 | */ |
| 1717 | __vb2_queue_cancel(q); |
| 1718 | |
| 1719 | dprintk(3, "Streamoff successful\n"); |
| 1720 | return 0; |
| 1721 | } |
| 1722 | EXPORT_SYMBOL_GPL(vb2_streamoff); |
| 1723 | |
| 1724 | /** |
| 1725 | * __find_plane_by_offset() - find plane associated with the given offset off |
| 1726 | */ |
| 1727 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, |
| 1728 | unsigned int *_buffer, unsigned int *_plane) |
| 1729 | { |
| 1730 | struct vb2_buffer *vb; |
| 1731 | unsigned int buffer, plane; |
| 1732 | |
| 1733 | /* |
| 1734 | * Go over all buffers and their planes, comparing the given offset |
| 1735 | * with an offset assigned to each plane. If a match is found, |
| 1736 | * return its buffer and plane numbers. |
| 1737 | */ |
| 1738 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 1739 | vb = q->bufs[buffer]; |
| 1740 | |
| 1741 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1742 | if (vb->v4l2_planes[plane].m.mem_offset == off) { |
| 1743 | *_buffer = buffer; |
| 1744 | *_plane = plane; |
| 1745 | return 0; |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | return -EINVAL; |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * vb2_mmap() - map video buffers into application address space |
| 1755 | * @q: videobuf2 queue |
| 1756 | * @vma: vma passed to the mmap file operation handler in the driver |
| 1757 | * |
| 1758 | * Should be called from mmap file operation handler of a driver. |
| 1759 | * This function maps one plane of one of the available video buffers to |
| 1760 | * userspace. To map whole video memory allocated on reqbufs, this function |
| 1761 | * has to be called once per each plane per each buffer previously allocated. |
| 1762 | * |
| 1763 | * When the userspace application calls mmap, it passes to it an offset returned |
| 1764 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as |
| 1765 | * a "cookie", which is then used to identify the plane to be mapped. |
| 1766 | * This function finds a plane with a matching offset and a mapping is performed |
| 1767 | * by the means of a provided memory operation. |
| 1768 | * |
| 1769 | * The return values from this function are intended to be directly returned |
| 1770 | * from the mmap handler in driver. |
| 1771 | */ |
| 1772 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) |
| 1773 | { |
| 1774 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 1775 | struct vb2_buffer *vb; |
| 1776 | unsigned int buffer, plane; |
| 1777 | int ret; |
| 1778 | |
| 1779 | if (q->memory != V4L2_MEMORY_MMAP) { |
| 1780 | dprintk(1, "Queue is not currently set up for mmap\n"); |
| 1781 | return -EINVAL; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * Check memory area access mode. |
| 1786 | */ |
| 1787 | if (!(vma->vm_flags & VM_SHARED)) { |
| 1788 | dprintk(1, "Invalid vma flags, VM_SHARED needed\n"); |
| 1789 | return -EINVAL; |
| 1790 | } |
| 1791 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { |
| 1792 | if (!(vma->vm_flags & VM_WRITE)) { |
| 1793 | dprintk(1, "Invalid vma flags, VM_WRITE needed\n"); |
| 1794 | return -EINVAL; |
| 1795 | } |
| 1796 | } else { |
| 1797 | if (!(vma->vm_flags & VM_READ)) { |
| 1798 | dprintk(1, "Invalid vma flags, VM_READ needed\n"); |
| 1799 | return -EINVAL; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * Find the plane corresponding to the offset passed by userspace. |
| 1805 | */ |
| 1806 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 1807 | if (ret) |
| 1808 | return ret; |
| 1809 | |
| 1810 | vb = q->bufs[buffer]; |
| 1811 | |
| 1812 | ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma); |
| 1813 | if (ret) |
| 1814 | return ret; |
| 1815 | |
| 1816 | dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane); |
| 1817 | return 0; |
| 1818 | } |
| 1819 | EXPORT_SYMBOL_GPL(vb2_mmap); |
| 1820 | |
| 1821 | #ifndef CONFIG_MMU |
| 1822 | unsigned long vb2_get_unmapped_area(struct vb2_queue *q, |
| 1823 | unsigned long addr, |
| 1824 | unsigned long len, |
| 1825 | unsigned long pgoff, |
| 1826 | unsigned long flags) |
| 1827 | { |
| 1828 | unsigned long off = pgoff << PAGE_SHIFT; |
| 1829 | struct vb2_buffer *vb; |
| 1830 | unsigned int buffer, plane; |
| 1831 | int ret; |
| 1832 | |
| 1833 | if (q->memory != V4L2_MEMORY_MMAP) { |
| 1834 | dprintk(1, "Queue is not currently set up for mmap\n"); |
| 1835 | return -EINVAL; |
| 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * Find the plane corresponding to the offset passed by userspace. |
| 1840 | */ |
| 1841 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 1842 | if (ret) |
| 1843 | return ret; |
| 1844 | |
| 1845 | vb = q->bufs[buffer]; |
| 1846 | |
| 1847 | return (unsigned long)vb2_plane_vaddr(vb, plane); |
| 1848 | } |
| 1849 | EXPORT_SYMBOL_GPL(vb2_get_unmapped_area); |
| 1850 | #endif |
| 1851 | |
| 1852 | static int __vb2_init_fileio(struct vb2_queue *q, int read); |
| 1853 | static int __vb2_cleanup_fileio(struct vb2_queue *q); |
| 1854 | |
| 1855 | /** |
| 1856 | * vb2_poll() - implements poll userspace operation |
| 1857 | * @q: videobuf2 queue |
| 1858 | * @file: file argument passed to the poll file operation handler |
| 1859 | * @wait: wait argument passed to the poll file operation handler |
| 1860 | * |
| 1861 | * This function implements poll file operation handler for a driver. |
| 1862 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will |
| 1863 | * be informed that the file descriptor of a video device is available for |
| 1864 | * reading. |
| 1865 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor |
| 1866 | * will be reported as available for writing. |
| 1867 | * |
| 1868 | * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any |
| 1869 | * pending events. |
| 1870 | * |
| 1871 | * The return values from this function are intended to be directly returned |
| 1872 | * from poll handler in driver. |
| 1873 | */ |
| 1874 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) |
| 1875 | { |
| 1876 | struct video_device *vfd = video_devdata(file); |
| 1877 | unsigned long req_events = poll_requested_events(wait); |
| 1878 | struct vb2_buffer *vb = NULL; |
| 1879 | unsigned int res = 0; |
| 1880 | unsigned long flags; |
| 1881 | |
| 1882 | if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { |
| 1883 | struct v4l2_fh *fh = file->private_data; |
| 1884 | |
| 1885 | if (v4l2_event_pending(fh)) |
| 1886 | res = POLLPRI; |
| 1887 | else if (req_events & POLLPRI) |
| 1888 | poll_wait(file, &fh->wait, wait); |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | * Start file I/O emulator only if streaming API has not been used yet. |
| 1893 | */ |
| 1894 | if (q->num_buffers == 0 && q->fileio == NULL) { |
| 1895 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) && |
| 1896 | (req_events & (POLLIN | POLLRDNORM))) { |
| 1897 | if (__vb2_init_fileio(q, 1)) |
| 1898 | return res | POLLERR; |
| 1899 | } |
| 1900 | if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) && |
| 1901 | (req_events & (POLLOUT | POLLWRNORM))) { |
| 1902 | if (__vb2_init_fileio(q, 0)) |
| 1903 | return res | POLLERR; |
| 1904 | /* |
| 1905 | * Write to OUTPUT queue can be done immediately. |
| 1906 | */ |
| 1907 | return res | POLLOUT | POLLWRNORM; |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * There is nothing to wait for if no buffers have already been queued. |
| 1913 | */ |
| 1914 | if (list_empty(&q->queued_list)) |
| 1915 | return res | POLLERR; |
| 1916 | |
| 1917 | poll_wait(file, &q->done_wq, wait); |
| 1918 | |
| 1919 | /* |
| 1920 | * Take first buffer available for dequeuing. |
| 1921 | */ |
| 1922 | spin_lock_irqsave(&q->done_lock, flags); |
| 1923 | if (!list_empty(&q->done_list)) |
| 1924 | vb = list_first_entry(&q->done_list, struct vb2_buffer, |
| 1925 | done_entry); |
| 1926 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1927 | |
| 1928 | if (vb && (vb->state == VB2_BUF_STATE_DONE |
| 1929 | || vb->state == VB2_BUF_STATE_ERROR)) { |
| 1930 | return (V4L2_TYPE_IS_OUTPUT(q->type)) ? |
| 1931 | res | POLLOUT | POLLWRNORM : |
| 1932 | res | POLLIN | POLLRDNORM; |
| 1933 | } |
| 1934 | return res; |
| 1935 | } |
| 1936 | EXPORT_SYMBOL_GPL(vb2_poll); |
| 1937 | |
| 1938 | /** |
| 1939 | * vb2_queue_init() - initialize a videobuf2 queue |
| 1940 | * @q: videobuf2 queue; this structure should be allocated in driver |
| 1941 | * |
| 1942 | * The vb2_queue structure should be allocated by the driver. The driver is |
| 1943 | * responsible of clearing it's content and setting initial values for some |
| 1944 | * required entries before calling this function. |
| 1945 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer |
| 1946 | * to the struct vb2_queue description in include/media/videobuf2-core.h |
| 1947 | * for more information. |
| 1948 | */ |
| 1949 | int vb2_queue_init(struct vb2_queue *q) |
| 1950 | { |
| 1951 | /* |
| 1952 | * Sanity check |
| 1953 | */ |
| 1954 | if (WARN_ON(!q) || |
| 1955 | WARN_ON(!q->ops) || |
| 1956 | WARN_ON(!q->mem_ops) || |
| 1957 | WARN_ON(!q->type) || |
| 1958 | WARN_ON(!q->io_modes) || |
| 1959 | WARN_ON(!q->ops->queue_setup) || |
| 1960 | WARN_ON(!q->ops->buf_queue)) |
| 1961 | return -EINVAL; |
| 1962 | |
| 1963 | INIT_LIST_HEAD(&q->queued_list); |
| 1964 | INIT_LIST_HEAD(&q->done_list); |
| 1965 | spin_lock_init(&q->done_lock); |
| 1966 | init_waitqueue_head(&q->done_wq); |
| 1967 | |
| 1968 | if (q->buf_struct_size == 0) |
| 1969 | q->buf_struct_size = sizeof(struct vb2_buffer); |
| 1970 | |
| 1971 | return 0; |
| 1972 | } |
| 1973 | EXPORT_SYMBOL_GPL(vb2_queue_init); |
| 1974 | |
| 1975 | /** |
| 1976 | * vb2_queue_release() - stop streaming, release the queue and free memory |
| 1977 | * @q: videobuf2 queue |
| 1978 | * |
| 1979 | * This function stops streaming and performs necessary clean ups, including |
| 1980 | * freeing video buffer memory. The driver is responsible for freeing |
| 1981 | * the vb2_queue structure itself. |
| 1982 | */ |
| 1983 | void vb2_queue_release(struct vb2_queue *q) |
| 1984 | { |
| 1985 | __vb2_cleanup_fileio(q); |
| 1986 | __vb2_queue_cancel(q); |
| 1987 | __vb2_queue_free(q, q->num_buffers); |
| 1988 | } |
| 1989 | EXPORT_SYMBOL_GPL(vb2_queue_release); |
| 1990 | |
| 1991 | /** |
| 1992 | * struct vb2_fileio_buf - buffer context used by file io emulator |
| 1993 | * |
| 1994 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 1995 | * write) calls on top of streaming API. This structure is used for |
| 1996 | * tracking context related to the buffers. |
| 1997 | */ |
| 1998 | struct vb2_fileio_buf { |
| 1999 | void *vaddr; |
| 2000 | unsigned int size; |
| 2001 | unsigned int pos; |
| 2002 | unsigned int queued:1; |
| 2003 | }; |
| 2004 | |
| 2005 | /** |
| 2006 | * struct vb2_fileio_data - queue context used by file io emulator |
| 2007 | * |
| 2008 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 2009 | * write) calls on top of streaming API. For proper operation it required |
| 2010 | * this structure to save the driver state between each call of the read |
| 2011 | * or write function. |
| 2012 | */ |
| 2013 | struct vb2_fileio_data { |
| 2014 | struct v4l2_requestbuffers req; |
| 2015 | struct v4l2_buffer b; |
| 2016 | struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME]; |
| 2017 | unsigned int index; |
| 2018 | unsigned int q_count; |
| 2019 | unsigned int dq_count; |
| 2020 | unsigned int flags; |
| 2021 | }; |
| 2022 | |
| 2023 | /** |
| 2024 | * __vb2_init_fileio() - initialize file io emulator |
| 2025 | * @q: videobuf2 queue |
| 2026 | * @read: mode selector (1 means read, 0 means write) |
| 2027 | */ |
| 2028 | static int __vb2_init_fileio(struct vb2_queue *q, int read) |
| 2029 | { |
| 2030 | struct vb2_fileio_data *fileio; |
| 2031 | int i, ret; |
| 2032 | unsigned int count = 0; |
| 2033 | |
| 2034 | /* |
| 2035 | * Sanity check |
| 2036 | */ |
| 2037 | if ((read && !(q->io_modes & VB2_READ)) || |
| 2038 | (!read && !(q->io_modes & VB2_WRITE))) |
| 2039 | BUG(); |
| 2040 | |
| 2041 | /* |
| 2042 | * Check if device supports mapping buffers to kernel virtual space. |
| 2043 | */ |
| 2044 | if (!q->mem_ops->vaddr) |
| 2045 | return -EBUSY; |
| 2046 | |
| 2047 | /* |
| 2048 | * Check if streaming api has not been already activated. |
| 2049 | */ |
| 2050 | if (q->streaming || q->num_buffers > 0) |
| 2051 | return -EBUSY; |
| 2052 | |
| 2053 | /* |
| 2054 | * Start with count 1, driver can increase it in queue_setup() |
| 2055 | */ |
| 2056 | count = 1; |
| 2057 | |
| 2058 | dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n", |
| 2059 | (read) ? "read" : "write", count, q->io_flags); |
| 2060 | |
| 2061 | fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL); |
| 2062 | if (fileio == NULL) |
| 2063 | return -ENOMEM; |
| 2064 | |
| 2065 | fileio->flags = q->io_flags; |
| 2066 | |
| 2067 | /* |
| 2068 | * Request buffers and use MMAP type to force driver |
| 2069 | * to allocate buffers by itself. |
| 2070 | */ |
| 2071 | fileio->req.count = count; |
| 2072 | fileio->req.memory = V4L2_MEMORY_MMAP; |
| 2073 | fileio->req.type = q->type; |
| 2074 | ret = vb2_reqbufs(q, &fileio->req); |
| 2075 | if (ret) |
| 2076 | goto err_kfree; |
| 2077 | |
| 2078 | /* |
| 2079 | * Check if plane_count is correct |
| 2080 | * (multiplane buffers are not supported). |
| 2081 | */ |
| 2082 | if (q->bufs[0]->num_planes != 1) { |
| 2083 | ret = -EBUSY; |
| 2084 | goto err_reqbufs; |
| 2085 | } |
| 2086 | |
| 2087 | /* |
| 2088 | * Get kernel address of each buffer. |
| 2089 | */ |
| 2090 | for (i = 0; i < q->num_buffers; i++) { |
| 2091 | fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); |
| 2092 | if (fileio->bufs[i].vaddr == NULL) |
| 2093 | goto err_reqbufs; |
| 2094 | fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * Read mode requires pre queuing of all buffers. |
| 2099 | */ |
| 2100 | if (read) { |
| 2101 | /* |
| 2102 | * Queue all buffers. |
| 2103 | */ |
| 2104 | for (i = 0; i < q->num_buffers; i++) { |
| 2105 | struct v4l2_buffer *b = &fileio->b; |
| 2106 | memset(b, 0, sizeof(*b)); |
| 2107 | b->type = q->type; |
| 2108 | b->memory = q->memory; |
| 2109 | b->index = i; |
| 2110 | ret = vb2_qbuf(q, b); |
| 2111 | if (ret) |
| 2112 | goto err_reqbufs; |
| 2113 | fileio->bufs[i].queued = 1; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Start streaming. |
| 2118 | */ |
| 2119 | ret = vb2_streamon(q, q->type); |
| 2120 | if (ret) |
| 2121 | goto err_reqbufs; |
| 2122 | } |
| 2123 | |
| 2124 | q->fileio = fileio; |
| 2125 | |
| 2126 | return ret; |
| 2127 | |
| 2128 | err_reqbufs: |
| 2129 | fileio->req.count = 0; |
| 2130 | vb2_reqbufs(q, &fileio->req); |
| 2131 | |
| 2132 | err_kfree: |
| 2133 | kfree(fileio); |
| 2134 | return ret; |
| 2135 | } |
| 2136 | |
| 2137 | /** |
| 2138 | * __vb2_cleanup_fileio() - free resourced used by file io emulator |
| 2139 | * @q: videobuf2 queue |
| 2140 | */ |
| 2141 | static int __vb2_cleanup_fileio(struct vb2_queue *q) |
| 2142 | { |
| 2143 | struct vb2_fileio_data *fileio = q->fileio; |
| 2144 | |
| 2145 | if (fileio) { |
| 2146 | /* |
| 2147 | * Hack fileio context to enable direct calls to vb2 ioctl |
| 2148 | * interface. |
| 2149 | */ |
| 2150 | q->fileio = NULL; |
| 2151 | |
| 2152 | vb2_streamoff(q, q->type); |
| 2153 | fileio->req.count = 0; |
| 2154 | vb2_reqbufs(q, &fileio->req); |
| 2155 | kfree(fileio); |
| 2156 | dprintk(3, "file io emulator closed\n"); |
| 2157 | } |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | /** |
| 2162 | * __vb2_perform_fileio() - perform a single file io (read or write) operation |
| 2163 | * @q: videobuf2 queue |
| 2164 | * @data: pointed to target userspace buffer |
| 2165 | * @count: number of bytes to read or write |
| 2166 | * @ppos: file handle position tracking pointer |
| 2167 | * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) |
| 2168 | * @read: access mode selector (1 means read, 0 means write) |
| 2169 | */ |
| 2170 | static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, |
| 2171 | loff_t *ppos, int nonblock, int read) |
| 2172 | { |
| 2173 | struct vb2_fileio_data *fileio; |
| 2174 | struct vb2_fileio_buf *buf; |
| 2175 | int ret, index; |
| 2176 | |
| 2177 | dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n", |
| 2178 | read ? "read" : "write", (long)*ppos, count, |
| 2179 | nonblock ? "non" : ""); |
| 2180 | |
| 2181 | if (!data) |
| 2182 | return -EINVAL; |
| 2183 | |
| 2184 | /* |
| 2185 | * Initialize emulator on first call. |
| 2186 | */ |
| 2187 | if (!q->fileio) { |
| 2188 | ret = __vb2_init_fileio(q, read); |
| 2189 | dprintk(3, "file io: vb2_init_fileio result: %d\n", ret); |
| 2190 | if (ret) |
| 2191 | return ret; |
| 2192 | } |
| 2193 | fileio = q->fileio; |
| 2194 | |
| 2195 | /* |
| 2196 | * Hack fileio context to enable direct calls to vb2 ioctl interface. |
| 2197 | * The pointer will be restored before returning from this function. |
| 2198 | */ |
| 2199 | q->fileio = NULL; |
| 2200 | |
| 2201 | index = fileio->index; |
| 2202 | buf = &fileio->bufs[index]; |
| 2203 | |
| 2204 | /* |
| 2205 | * Check if we need to dequeue the buffer. |
| 2206 | */ |
| 2207 | if (buf->queued) { |
| 2208 | struct vb2_buffer *vb; |
| 2209 | |
| 2210 | /* |
| 2211 | * Call vb2_dqbuf to get buffer back. |
| 2212 | */ |
| 2213 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 2214 | fileio->b.type = q->type; |
| 2215 | fileio->b.memory = q->memory; |
| 2216 | fileio->b.index = index; |
| 2217 | ret = vb2_dqbuf(q, &fileio->b, nonblock); |
| 2218 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); |
| 2219 | if (ret) |
| 2220 | goto end; |
| 2221 | fileio->dq_count += 1; |
| 2222 | |
| 2223 | /* |
| 2224 | * Get number of bytes filled by the driver |
| 2225 | */ |
| 2226 | vb = q->bufs[index]; |
| 2227 | buf->size = vb2_get_plane_payload(vb, 0); |
| 2228 | buf->queued = 0; |
| 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * Limit count on last few bytes of the buffer. |
| 2233 | */ |
| 2234 | if (buf->pos + count > buf->size) { |
| 2235 | count = buf->size - buf->pos; |
| 2236 | dprintk(5, "reducing read count: %zd\n", count); |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * Transfer data to userspace. |
| 2241 | */ |
| 2242 | dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n", |
| 2243 | count, index, buf->pos); |
| 2244 | if (read) |
| 2245 | ret = copy_to_user(data, buf->vaddr + buf->pos, count); |
| 2246 | else |
| 2247 | ret = copy_from_user(buf->vaddr + buf->pos, data, count); |
| 2248 | if (ret) { |
| 2249 | dprintk(3, "file io: error copying data\n"); |
| 2250 | ret = -EFAULT; |
| 2251 | goto end; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Update counters. |
| 2256 | */ |
| 2257 | buf->pos += count; |
| 2258 | *ppos += count; |
| 2259 | |
| 2260 | /* |
| 2261 | * Queue next buffer if required. |
| 2262 | */ |
| 2263 | if (buf->pos == buf->size || |
| 2264 | (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) { |
| 2265 | /* |
| 2266 | * Check if this is the last buffer to read. |
| 2267 | */ |
| 2268 | if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) && |
| 2269 | fileio->dq_count == 1) { |
| 2270 | dprintk(3, "file io: read limit reached\n"); |
| 2271 | /* |
| 2272 | * Restore fileio pointer and release the context. |
| 2273 | */ |
| 2274 | q->fileio = fileio; |
| 2275 | return __vb2_cleanup_fileio(q); |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Call vb2_qbuf and give buffer to the driver. |
| 2280 | */ |
| 2281 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 2282 | fileio->b.type = q->type; |
| 2283 | fileio->b.memory = q->memory; |
| 2284 | fileio->b.index = index; |
| 2285 | fileio->b.bytesused = buf->pos; |
| 2286 | ret = vb2_qbuf(q, &fileio->b); |
| 2287 | dprintk(5, "file io: vb2_dbuf result: %d\n", ret); |
| 2288 | if (ret) |
| 2289 | goto end; |
| 2290 | |
| 2291 | /* |
| 2292 | * Buffer has been queued, update the status |
| 2293 | */ |
| 2294 | buf->pos = 0; |
| 2295 | buf->queued = 1; |
| 2296 | buf->size = q->bufs[0]->v4l2_planes[0].length; |
| 2297 | fileio->q_count += 1; |
| 2298 | |
| 2299 | /* |
| 2300 | * Switch to the next buffer |
| 2301 | */ |
| 2302 | fileio->index = (index + 1) % q->num_buffers; |
| 2303 | |
| 2304 | /* |
| 2305 | * Start streaming if required. |
| 2306 | */ |
| 2307 | if (!read && !q->streaming) { |
| 2308 | ret = vb2_streamon(q, q->type); |
| 2309 | if (ret) |
| 2310 | goto end; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * Return proper number of bytes processed. |
| 2316 | */ |
| 2317 | if (ret == 0) |
| 2318 | ret = count; |
| 2319 | end: |
| 2320 | /* |
| 2321 | * Restore the fileio context and block vb2 ioctl interface. |
| 2322 | */ |
| 2323 | q->fileio = fileio; |
| 2324 | return ret; |
| 2325 | } |
| 2326 | |
| 2327 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, |
| 2328 | loff_t *ppos, int nonblocking) |
| 2329 | { |
| 2330 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); |
| 2331 | } |
| 2332 | EXPORT_SYMBOL_GPL(vb2_read); |
| 2333 | |
| 2334 | size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count, |
| 2335 | loff_t *ppos, int nonblocking) |
| 2336 | { |
| 2337 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0); |
| 2338 | } |
| 2339 | EXPORT_SYMBOL_GPL(vb2_write); |
| 2340 | |
| 2341 | |
| 2342 | /* |
| 2343 | * The following functions are not part of the vb2 core API, but are helper |
| 2344 | * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations |
| 2345 | * and struct vb2_ops. |
| 2346 | * They contain boilerplate code that most if not all drivers have to do |
| 2347 | * and so they simplify the driver code. |
| 2348 | */ |
| 2349 | |
| 2350 | /* The queue is busy if there is a owner and you are not that owner. */ |
| 2351 | static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file) |
| 2352 | { |
| 2353 | return vdev->queue->owner && vdev->queue->owner != file->private_data; |
| 2354 | } |
| 2355 | |
| 2356 | /* vb2 ioctl helpers */ |
| 2357 | |
| 2358 | int vb2_ioctl_reqbufs(struct file *file, void *priv, |
| 2359 | struct v4l2_requestbuffers *p) |
| 2360 | { |
| 2361 | struct video_device *vdev = video_devdata(file); |
| 2362 | int res = __verify_memory_type(vdev->queue, p->memory, p->type); |
| 2363 | |
| 2364 | if (res) |
| 2365 | return res; |
| 2366 | if (vb2_queue_is_busy(vdev, file)) |
| 2367 | return -EBUSY; |
| 2368 | res = __reqbufs(vdev->queue, p); |
| 2369 | /* If count == 0, then the owner has released all buffers and he |
| 2370 | is no longer owner of the queue. Otherwise we have a new owner. */ |
| 2371 | if (res == 0) |
| 2372 | vdev->queue->owner = p->count ? file->private_data : NULL; |
| 2373 | return res; |
| 2374 | } |
| 2375 | EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs); |
| 2376 | |
| 2377 | int vb2_ioctl_create_bufs(struct file *file, void *priv, |
| 2378 | struct v4l2_create_buffers *p) |
| 2379 | { |
| 2380 | struct video_device *vdev = video_devdata(file); |
| 2381 | int res = __verify_memory_type(vdev->queue, p->memory, p->format.type); |
| 2382 | |
| 2383 | p->index = vdev->queue->num_buffers; |
| 2384 | /* If count == 0, then just check if memory and type are valid. |
| 2385 | Any -EBUSY result from __verify_memory_type can be mapped to 0. */ |
| 2386 | if (p->count == 0) |
| 2387 | return res != -EBUSY ? res : 0; |
| 2388 | if (res) |
| 2389 | return res; |
| 2390 | if (vb2_queue_is_busy(vdev, file)) |
| 2391 | return -EBUSY; |
| 2392 | res = __create_bufs(vdev->queue, p); |
| 2393 | if (res == 0) |
| 2394 | vdev->queue->owner = file->private_data; |
| 2395 | return res; |
| 2396 | } |
| 2397 | EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs); |
| 2398 | |
| 2399 | int vb2_ioctl_prepare_buf(struct file *file, void *priv, |
| 2400 | struct v4l2_buffer *p) |
| 2401 | { |
| 2402 | struct video_device *vdev = video_devdata(file); |
| 2403 | |
| 2404 | if (vb2_queue_is_busy(vdev, file)) |
| 2405 | return -EBUSY; |
| 2406 | return vb2_prepare_buf(vdev->queue, p); |
| 2407 | } |
| 2408 | EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf); |
| 2409 | |
| 2410 | int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 2411 | { |
| 2412 | struct video_device *vdev = video_devdata(file); |
| 2413 | |
| 2414 | /* No need to call vb2_queue_is_busy(), anyone can query buffers. */ |
| 2415 | return vb2_querybuf(vdev->queue, p); |
| 2416 | } |
| 2417 | EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf); |
| 2418 | |
| 2419 | int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 2420 | { |
| 2421 | struct video_device *vdev = video_devdata(file); |
| 2422 | |
| 2423 | if (vb2_queue_is_busy(vdev, file)) |
| 2424 | return -EBUSY; |
| 2425 | return vb2_qbuf(vdev->queue, p); |
| 2426 | } |
| 2427 | EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf); |
| 2428 | |
| 2429 | int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 2430 | { |
| 2431 | struct video_device *vdev = video_devdata(file); |
| 2432 | |
| 2433 | if (vb2_queue_is_busy(vdev, file)) |
| 2434 | return -EBUSY; |
| 2435 | return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK); |
| 2436 | } |
| 2437 | EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf); |
| 2438 | |
| 2439 | int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
| 2440 | { |
| 2441 | struct video_device *vdev = video_devdata(file); |
| 2442 | |
| 2443 | if (vb2_queue_is_busy(vdev, file)) |
| 2444 | return -EBUSY; |
| 2445 | return vb2_streamon(vdev->queue, i); |
| 2446 | } |
| 2447 | EXPORT_SYMBOL_GPL(vb2_ioctl_streamon); |
| 2448 | |
| 2449 | int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 2450 | { |
| 2451 | struct video_device *vdev = video_devdata(file); |
| 2452 | |
| 2453 | if (vb2_queue_is_busy(vdev, file)) |
| 2454 | return -EBUSY; |
| 2455 | return vb2_streamoff(vdev->queue, i); |
| 2456 | } |
| 2457 | EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff); |
| 2458 | |
| 2459 | /* v4l2_file_operations helpers */ |
| 2460 | |
| 2461 | int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) |
| 2462 | { |
| 2463 | struct video_device *vdev = video_devdata(file); |
| 2464 | |
| 2465 | return vb2_mmap(vdev->queue, vma); |
| 2466 | } |
| 2467 | EXPORT_SYMBOL_GPL(vb2_fop_mmap); |
| 2468 | |
| 2469 | int vb2_fop_release(struct file *file) |
| 2470 | { |
| 2471 | struct video_device *vdev = video_devdata(file); |
| 2472 | |
| 2473 | if (file->private_data == vdev->queue->owner) { |
| 2474 | vb2_queue_release(vdev->queue); |
| 2475 | vdev->queue->owner = NULL; |
| 2476 | } |
| 2477 | return v4l2_fh_release(file); |
| 2478 | } |
| 2479 | EXPORT_SYMBOL_GPL(vb2_fop_release); |
| 2480 | |
| 2481 | ssize_t vb2_fop_write(struct file *file, char __user *buf, |
| 2482 | size_t count, loff_t *ppos) |
| 2483 | { |
| 2484 | struct video_device *vdev = video_devdata(file); |
| 2485 | struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; |
| 2486 | int err = -EBUSY; |
| 2487 | |
| 2488 | if (lock && mutex_lock_interruptible(lock)) |
| 2489 | return -ERESTARTSYS; |
| 2490 | if (vb2_queue_is_busy(vdev, file)) |
| 2491 | goto exit; |
| 2492 | err = vb2_write(vdev->queue, buf, count, ppos, |
| 2493 | file->f_flags & O_NONBLOCK); |
| 2494 | if (vdev->queue->fileio) |
| 2495 | vdev->queue->owner = file->private_data; |
| 2496 | exit: |
| 2497 | if (lock) |
| 2498 | mutex_unlock(lock); |
| 2499 | return err; |
| 2500 | } |
| 2501 | EXPORT_SYMBOL_GPL(vb2_fop_write); |
| 2502 | |
| 2503 | ssize_t vb2_fop_read(struct file *file, char __user *buf, |
| 2504 | size_t count, loff_t *ppos) |
| 2505 | { |
| 2506 | struct video_device *vdev = video_devdata(file); |
| 2507 | struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; |
| 2508 | int err = -EBUSY; |
| 2509 | |
| 2510 | if (lock && mutex_lock_interruptible(lock)) |
| 2511 | return -ERESTARTSYS; |
| 2512 | if (vb2_queue_is_busy(vdev, file)) |
| 2513 | goto exit; |
| 2514 | err = vb2_read(vdev->queue, buf, count, ppos, |
| 2515 | file->f_flags & O_NONBLOCK); |
| 2516 | if (vdev->queue->fileio) |
| 2517 | vdev->queue->owner = file->private_data; |
| 2518 | exit: |
| 2519 | if (lock) |
| 2520 | mutex_unlock(lock); |
| 2521 | return err; |
| 2522 | } |
| 2523 | EXPORT_SYMBOL_GPL(vb2_fop_read); |
| 2524 | |
| 2525 | unsigned int vb2_fop_poll(struct file *file, poll_table *wait) |
| 2526 | { |
| 2527 | struct video_device *vdev = video_devdata(file); |
| 2528 | struct vb2_queue *q = vdev->queue; |
| 2529 | struct mutex *lock = q->lock ? q->lock : vdev->lock; |
| 2530 | unsigned long req_events = poll_requested_events(wait); |
| 2531 | unsigned res; |
| 2532 | void *fileio; |
| 2533 | bool must_lock = false; |
| 2534 | |
| 2535 | /* Try to be smart: only lock if polling might start fileio, |
| 2536 | otherwise locking will only introduce unwanted delays. */ |
| 2537 | if (q->num_buffers == 0 && q->fileio == NULL) { |
| 2538 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) && |
| 2539 | (req_events & (POLLIN | POLLRDNORM))) |
| 2540 | must_lock = true; |
| 2541 | else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) && |
| 2542 | (req_events & (POLLOUT | POLLWRNORM))) |
| 2543 | must_lock = true; |
| 2544 | } |
| 2545 | |
| 2546 | /* If locking is needed, but this helper doesn't know how, then you |
| 2547 | shouldn't be using this helper but you should write your own. */ |
| 2548 | WARN_ON(must_lock && !lock); |
| 2549 | |
| 2550 | if (must_lock && lock && mutex_lock_interruptible(lock)) |
| 2551 | return POLLERR; |
| 2552 | |
| 2553 | fileio = q->fileio; |
| 2554 | |
| 2555 | res = vb2_poll(vdev->queue, file, wait); |
| 2556 | |
| 2557 | /* If fileio was started, then we have a new queue owner. */ |
| 2558 | if (must_lock && !fileio && q->fileio) |
| 2559 | q->owner = file->private_data; |
| 2560 | if (must_lock && lock) |
| 2561 | mutex_unlock(lock); |
| 2562 | return res; |
| 2563 | } |
| 2564 | EXPORT_SYMBOL_GPL(vb2_fop_poll); |
| 2565 | |
| 2566 | #ifndef CONFIG_MMU |
| 2567 | unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, |
| 2568 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 2569 | { |
| 2570 | struct video_device *vdev = video_devdata(file); |
| 2571 | |
| 2572 | return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags); |
| 2573 | } |
| 2574 | EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area); |
| 2575 | #endif |
| 2576 | |
| 2577 | /* vb2_ops helpers. Only use if vq->lock is non-NULL. */ |
| 2578 | |
| 2579 | void vb2_ops_wait_prepare(struct vb2_queue *vq) |
| 2580 | { |
| 2581 | mutex_unlock(vq->lock); |
| 2582 | } |
| 2583 | EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare); |
| 2584 | |
| 2585 | void vb2_ops_wait_finish(struct vb2_queue *vq) |
| 2586 | { |
| 2587 | mutex_lock(vq->lock); |
| 2588 | } |
| 2589 | EXPORT_SYMBOL_GPL(vb2_ops_wait_finish); |
| 2590 | |
| 2591 | MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); |
| 2592 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); |
| 2593 | MODULE_LICENSE("GPL"); |