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