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