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