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