2 * videobuf2-core.c - video buffer 2 core framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
10 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/err.h>
20 #include <linux/kernel.h>
21 #include <linux/module.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>
29 #include <media/videobuf2-core.h>
30 #include <media/v4l2-mc.h>
32 #include <trace/events/vb2.h>
35 module_param(debug, int, 0644);
37 #define dprintk(level, fmt, arg...) \
40 pr_info("%s: " fmt, __func__, ## arg); \
43 #ifdef CONFIG_VIDEO_ADV_DEBUG
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.
49 * This makes it easy to check that the 'init' and 'cleanup'
50 * (and variations thereof) stay balanced.
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)")
58 #define call_memop(vb, op, args...) \
60 struct vb2_queue *_q = (vb)->vb2_queue; \
64 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
66 (vb)->cnt_mem_ ## op++; \
70 #define call_ptr_memop(vb, op, args...) \
72 struct vb2_queue *_q = (vb)->vb2_queue; \
76 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
77 if (!IS_ERR_OR_NULL(ptr)) \
78 (vb)->cnt_mem_ ## op++; \
82 #define call_void_memop(vb, op, args...) \
84 struct vb2_queue *_q = (vb)->vb2_queue; \
87 if (_q->mem_ops->op) \
88 _q->mem_ops->op(args); \
89 (vb)->cnt_mem_ ## op++; \
92 #define log_qop(q, op) \
93 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
94 (q)->ops->op ? "" : " (nop)")
96 #define call_qop(q, op, args...) \
101 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
107 #define call_void_qop(q, op, args...) \
111 (q)->ops->op(args); \
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)")
120 #define call_vb_qop(vb, op, args...) \
124 log_vb_qop(vb, op); \
125 err = (vb)->vb2_queue->ops->op ? \
126 (vb)->vb2_queue->ops->op(args) : 0; \
128 (vb)->cnt_ ## op++; \
132 #define call_void_vb_qop(vb, op, args...) \
134 log_vb_qop(vb, op); \
135 if ((vb)->vb2_queue->ops->op) \
136 (vb)->vb2_queue->ops->op(args); \
137 (vb)->cnt_ ## op++; \
142 #define call_memop(vb, op, args...) \
143 ((vb)->vb2_queue->mem_ops->op ? \
144 (vb)->vb2_queue->mem_ops->op(args) : 0)
146 #define call_ptr_memop(vb, op, args...) \
147 ((vb)->vb2_queue->mem_ops->op ? \
148 (vb)->vb2_queue->mem_ops->op(args) : NULL)
150 #define call_void_memop(vb, op, args...) \
152 if ((vb)->vb2_queue->mem_ops->op) \
153 (vb)->vb2_queue->mem_ops->op(args); \
156 #define call_qop(q, op, args...) \
157 ((q)->ops->op ? (q)->ops->op(args) : 0)
159 #define call_void_qop(q, op, args...) \
162 (q)->ops->op(args); \
165 #define call_vb_qop(vb, op, args...) \
166 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
168 #define call_void_vb_qop(vb, op, args...) \
170 if ((vb)->vb2_queue->ops->op) \
171 (vb)->vb2_queue->ops->op(args); \
176 #define call_bufop(q, op, args...) \
179 if (q && q->buf_ops && q->buf_ops->op) \
180 ret = q->buf_ops->op(args); \
184 #define call_void_bufop(q, op, args...) \
186 if (q && q->buf_ops && q->buf_ops->op) \
187 q->buf_ops->op(args); \
190 static void __vb2_queue_cancel(struct vb2_queue *q);
191 static void __enqueue_in_driver(struct vb2_buffer *vb);
194 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
196 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
198 struct vb2_queue *q = vb->vb2_queue;
204 * Allocate memory for all planes in this buffer
205 * NOTE: mmapped areas should be page aligned
207 for (plane = 0; plane < vb->num_planes; ++plane) {
208 unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
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)) {
215 ret = PTR_ERR(mem_priv);
219 /* Associate allocator private data with this plane */
220 vb->planes[plane].mem_priv = mem_priv;
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;
235 * __vb2_buf_mem_free() - free memory of the given buffer
237 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
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);
249 * __vb2_buf_userptr_put() - release userspace memory associated with
252 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
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;
264 * __vb2_plane_dmabuf_put() - release memory associated with
265 * a DMABUF shared plane
267 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
273 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
275 call_void_memop(vb, detach_dmabuf, p->mem_priv);
276 dma_buf_put(p->dbuf);
283 * __vb2_buf_dmabuf_put() - release memory associated with
284 * a DMABUF shared buffer
286 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
290 for (plane = 0; plane < vb->num_planes; ++plane)
291 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
295 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
298 static void __setup_offsets(struct vb2_buffer *vb)
300 struct vb2_queue *q = vb->vb2_queue;
302 unsigned long off = 0;
305 struct vb2_buffer *prev = q->bufs[vb->index - 1];
306 struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
308 off = PAGE_ALIGN(p->m.offset + p->length);
311 for (plane = 0; plane < vb->num_planes; ++plane) {
312 vb->planes[plane].m.offset = off;
314 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
315 vb->index, plane, off);
317 off += vb->planes[plane].length;
318 off = PAGE_ALIGN(off);
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
327 * Returns the number of buffers successfully allocated.
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])
333 unsigned int buffer, plane;
334 struct vb2_buffer *vb;
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);
341 for (buffer = 0; buffer < num_buffers; ++buffer) {
342 /* Allocate videobuf buffer structures */
343 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
345 dprintk(1, "memory alloc for buffer struct failed\n");
349 vb->state = VB2_BUF_STATE_DEQUEUED;
351 vb->num_planes = num_planes;
352 vb->index = q->num_buffers + buffer;
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];
359 call_void_bufop(q, init_buffer, vb);
361 q->bufs[vb->index] = vb;
363 /* Allocate video buffer memory for the MMAP type */
364 if (memory == VB2_MEMORY_MMAP) {
365 ret = __vb2_buf_mem_alloc(vb);
367 dprintk(1, "failed allocating memory for buffer %d\n",
369 q->bufs[vb->index] = NULL;
375 * Call the driver-provided buffer initialization
376 * callback, if given. An error in initialization
377 * results in queue setup failure.
379 ret = call_vb_qop(vb, buf_init, vb);
381 dprintk(1, "buffer %d %p initialization failed\n",
383 __vb2_buf_mem_free(vb);
384 q->bufs[vb->index] = NULL;
391 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
398 * __vb2_free_mem() - release all video buffer memory for a given queue
400 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
403 struct vb2_buffer *vb;
405 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
407 vb = q->bufs[buffer];
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);
417 __vb2_buf_userptr_put(vb);
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.
426 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
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.
438 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
440 if (q->bufs[buffer] == NULL)
442 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
443 dprintk(1, "preparing buffers, cannot free\n");
448 /* Call driver-provided cleanup function for each buffer, if provided */
449 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
451 struct vb2_buffer *vb = q->bufs[buffer];
453 if (vb && vb->planes[0].mem_priv)
454 call_void_vb_qop(vb, buf_cleanup, vb);
457 /* Release video buffer memory */
458 __vb2_free_mem(q, buffers);
460 #ifdef CONFIG_VIDEO_ADV_DEBUG
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.
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;
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);
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;
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;
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_queue: %u buf_done: %u buf_request_complete: %u\n",
503 vb->cnt_buf_queue, vb->cnt_buf_done,
504 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,
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,
523 /* Free videobuf buffers */
524 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
526 kfree(q->bufs[buffer]);
527 q->bufs[buffer] = NULL;
530 q->num_buffers -= buffers;
531 if (!q->num_buffers) {
532 q->memory = VB2_MEMORY_UNKNOWN;
533 INIT_LIST_HEAD(&q->queued_list);
538 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
541 for (plane = 0; plane < vb->num_planes; ++plane) {
542 void *mem_priv = vb->planes[plane].mem_priv;
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.
549 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
554 EXPORT_SYMBOL(vb2_buffer_in_use);
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
560 static bool __buffers_in_use(struct vb2_queue *q)
563 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
564 if (vb2_buffer_in_use(q, q->bufs[buffer]))
570 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
572 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
574 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
577 * __verify_userptr_ops() - verify that all memory operations required for
578 * USERPTR queue type have been provided
580 static int __verify_userptr_ops(struct vb2_queue *q)
582 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
583 !q->mem_ops->put_userptr)
590 * __verify_mmap_ops() - verify that all memory operations required for
591 * MMAP queue type have been provided
593 static int __verify_mmap_ops(struct vb2_queue *q)
595 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
596 !q->mem_ops->put || !q->mem_ops->mmap)
603 * __verify_dmabuf_ops() - verify that all memory operations required for
604 * DMABUF queue type have been provided
606 static int __verify_dmabuf_ops(struct vb2_queue *q)
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)
616 int vb2_verify_memory_type(struct vb2_queue *q,
617 enum vb2_memory memory, unsigned int type)
619 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
620 memory != VB2_MEMORY_DMABUF) {
621 dprintk(1, "unsupported memory type\n");
625 if (type != q->type) {
626 dprintk(1, "requested type is incorrect\n");
631 * Make sure all the required memory ops for given memory type
634 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
635 dprintk(1, "MMAP for current setup unsupported\n");
639 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
640 dprintk(1, "USERPTR for current setup unsupported\n");
644 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
645 dprintk(1, "DMABUF for current setup unsupported\n");
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.
654 if (vb2_fileio_is_active(q)) {
655 dprintk(1, "file io in progress\n");
660 EXPORT_SYMBOL(vb2_verify_memory_type);
662 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
665 unsigned int num_buffers, allocated_buffers, num_planes = 0;
666 unsigned plane_sizes[VB2_MAX_PLANES] = { };
671 dprintk(1, "streaming active\n");
675 if (*count == 0 || q->num_buffers != 0 ||
676 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
678 * We already have buffers allocated, so first check if they
679 * are not in use and can be freed.
681 mutex_lock(&q->mmap_lock);
682 if (debug && q->memory == VB2_MEMORY_MMAP &&
684 dprintk(1, "memory in use, orphaning buffers\n");
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.
691 __vb2_queue_cancel(q);
692 ret = __vb2_queue_free(q, q->num_buffers);
693 mutex_unlock(&q->mmap_lock);
698 * In case of REQBUFS(0) return immediately without calling
699 * driver's queue_setup() callback and allocating resources.
706 * Make sure the requested values and current defaults are sane.
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));
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.
718 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
719 plane_sizes, q->alloc_devs);
723 /* Check that driver has set sane values */
724 if (WARN_ON(!num_planes))
727 for (i = 0; i < num_planes; i++)
728 if (WARN_ON(!plane_sizes[i]))
731 /* Finally, allocate buffers and video memory */
733 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
734 if (allocated_buffers == 0) {
735 dprintk(1, "memory allocation failed\n");
740 * There is no point in continuing if we can't allocate the minimum
741 * number of buffers needed by this vb2_queue.
743 if (allocated_buffers < q->min_buffers_needed)
747 * Check if driver can handle the allocated number of buffers.
749 if (!ret && allocated_buffers < num_buffers) {
750 num_buffers = allocated_buffers;
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.
759 ret = call_qop(q, queue_setup, q, &num_buffers,
760 &num_planes, plane_sizes, q->alloc_devs);
762 if (!ret && allocated_buffers < num_buffers)
766 * Either the driver has accepted a smaller number of buffers,
767 * or .queue_setup() returned an error
771 mutex_lock(&q->mmap_lock);
772 q->num_buffers = allocated_buffers;
776 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
777 * from q->num_buffers.
779 __vb2_queue_free(q, allocated_buffers);
780 mutex_unlock(&q->mmap_lock);
783 mutex_unlock(&q->mmap_lock);
786 * Return the number of successfully allocated buffers
789 *count = allocated_buffers;
790 q->waiting_for_buffers = !q->is_output;
794 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
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[])
800 unsigned int num_planes = 0, num_buffers, allocated_buffers;
801 unsigned plane_sizes[VB2_MAX_PLANES] = { };
804 if (q->num_buffers == VB2_MAX_FRAME) {
805 dprintk(1, "maximum number of buffers already allocated\n");
809 if (!q->num_buffers) {
810 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
812 q->waiting_for_buffers = !q->is_output;
813 } else if (q->memory != memory) {
814 dprintk(1, "memory model mismatch\n");
818 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
820 if (requested_planes && requested_sizes) {
821 num_planes = requested_planes;
822 memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
826 * Ask the driver, whether the requested number of buffers, planes per
827 * buffer and their sizes are acceptable
829 ret = call_qop(q, queue_setup, q, &num_buffers,
830 &num_planes, plane_sizes, q->alloc_devs);
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");
843 * Check if driver can handle the so far allocated number of buffers.
845 if (allocated_buffers < num_buffers) {
846 num_buffers = allocated_buffers;
849 * q->num_buffers contains the total number of buffers, that the
850 * queue driver has set up
852 ret = call_qop(q, queue_setup, q, &num_buffers,
853 &num_planes, plane_sizes, q->alloc_devs);
855 if (!ret && allocated_buffers < num_buffers)
859 * Either the driver has accepted a smaller number of buffers,
860 * or .queue_setup() returned an error
864 mutex_lock(&q->mmap_lock);
865 q->num_buffers += allocated_buffers;
869 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
870 * from q->num_buffers.
872 __vb2_queue_free(q, allocated_buffers);
873 mutex_unlock(&q->mmap_lock);
876 mutex_unlock(&q->mmap_lock);
879 * Return the number of successfully allocated buffers
882 *count = allocated_buffers;
886 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
888 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
890 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
893 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
896 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
898 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
900 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
903 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
905 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
907 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
909 struct vb2_queue *q = vb->vb2_queue;
913 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
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_REQUEUEING))
920 state = VB2_BUF_STATE_ERROR;
922 #ifdef CONFIG_VIDEO_ADV_DEBUG
924 * Although this is not a callback, it still does have to balance
925 * with the buf_queue op. So update this counter manually.
929 dprintk(4, "done processing on buffer %d, state: %d\n",
932 if (state != VB2_BUF_STATE_QUEUED &&
933 state != VB2_BUF_STATE_REQUEUEING) {
935 for (plane = 0; plane < vb->num_planes; ++plane)
936 call_void_memop(vb, finish, vb->planes[plane].mem_priv);
940 spin_lock_irqsave(&q->done_lock, flags);
941 if (state == VB2_BUF_STATE_QUEUED ||
942 state == VB2_BUF_STATE_REQUEUEING) {
943 vb->state = VB2_BUF_STATE_QUEUED;
945 /* Add the buffer to the done buffers list */
946 list_add_tail(&vb->done_entry, &q->done_list);
949 atomic_dec(&q->owned_by_drv_count);
951 if (state != VB2_BUF_STATE_QUEUED && vb->req_obj.req) {
952 /* This is not supported at the moment */
953 WARN_ON(state == VB2_BUF_STATE_REQUEUEING);
954 media_request_object_unbind(&vb->req_obj);
955 media_request_object_put(&vb->req_obj);
958 spin_unlock_irqrestore(&q->done_lock, flags);
960 trace_vb2_buf_done(q, vb);
963 case VB2_BUF_STATE_QUEUED:
965 case VB2_BUF_STATE_REQUEUEING:
966 if (q->start_streaming_called)
967 __enqueue_in_driver(vb);
970 /* Inform any processes that may be waiting for buffers */
971 wake_up(&q->done_wq);
975 EXPORT_SYMBOL_GPL(vb2_buffer_done);
977 void vb2_discard_done(struct vb2_queue *q)
979 struct vb2_buffer *vb;
982 spin_lock_irqsave(&q->done_lock, flags);
983 list_for_each_entry(vb, &q->done_list, done_entry)
984 vb->state = VB2_BUF_STATE_ERROR;
985 spin_unlock_irqrestore(&q->done_lock, flags);
987 EXPORT_SYMBOL_GPL(vb2_discard_done);
990 * __prepare_mmap() - prepare an MMAP buffer
992 static int __prepare_mmap(struct vb2_buffer *vb)
996 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
998 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
1002 * __prepare_userptr() - prepare a USERPTR buffer
1004 static int __prepare_userptr(struct vb2_buffer *vb)
1006 struct vb2_plane planes[VB2_MAX_PLANES];
1007 struct vb2_queue *q = vb->vb2_queue;
1011 bool reacquired = vb->planes[0].mem_priv == NULL;
1013 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1014 /* Copy relevant information provided by the userspace */
1015 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1020 for (plane = 0; plane < vb->num_planes; ++plane) {
1021 /* Skip the plane if already verified */
1022 if (vb->planes[plane].m.userptr &&
1023 vb->planes[plane].m.userptr == planes[plane].m.userptr
1024 && vb->planes[plane].length == planes[plane].length)
1027 dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
1030 /* Check if the provided plane buffer is large enough */
1031 if (planes[plane].length < vb->planes[plane].min_length) {
1032 dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
1033 planes[plane].length,
1034 vb->planes[plane].min_length,
1040 /* Release previously acquired memory if present */
1041 if (vb->planes[plane].mem_priv) {
1044 call_void_vb_qop(vb, buf_cleanup, vb);
1046 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
1049 vb->planes[plane].mem_priv = NULL;
1050 vb->planes[plane].bytesused = 0;
1051 vb->planes[plane].length = 0;
1052 vb->planes[plane].m.userptr = 0;
1053 vb->planes[plane].data_offset = 0;
1055 /* Acquire each plane's memory */
1056 mem_priv = call_ptr_memop(vb, get_userptr,
1057 q->alloc_devs[plane] ? : q->dev,
1058 planes[plane].m.userptr,
1059 planes[plane].length, q->dma_dir);
1060 if (IS_ERR(mem_priv)) {
1061 dprintk(1, "failed acquiring userspace memory for plane %d\n",
1063 ret = PTR_ERR(mem_priv);
1066 vb->planes[plane].mem_priv = mem_priv;
1070 * Now that everything is in order, copy relevant information
1071 * provided by userspace.
1073 for (plane = 0; plane < vb->num_planes; ++plane) {
1074 vb->planes[plane].bytesused = planes[plane].bytesused;
1075 vb->planes[plane].length = planes[plane].length;
1076 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1077 vb->planes[plane].data_offset = planes[plane].data_offset;
1082 * One or more planes changed, so we must call buf_init to do
1083 * the driver-specific initialization on the newly acquired
1084 * buffer, if provided.
1086 ret = call_vb_qop(vb, buf_init, vb);
1088 dprintk(1, "buffer initialization failed\n");
1093 ret = call_vb_qop(vb, buf_prepare, vb);
1095 dprintk(1, "buffer preparation failed\n");
1096 call_void_vb_qop(vb, buf_cleanup, vb);
1102 /* In case of errors, release planes that were already acquired */
1103 for (plane = 0; plane < vb->num_planes; ++plane) {
1104 if (vb->planes[plane].mem_priv)
1105 call_void_memop(vb, put_userptr,
1106 vb->planes[plane].mem_priv);
1107 vb->planes[plane].mem_priv = NULL;
1108 vb->planes[plane].m.userptr = 0;
1109 vb->planes[plane].length = 0;
1116 * __prepare_dmabuf() - prepare a DMABUF buffer
1118 static int __prepare_dmabuf(struct vb2_buffer *vb)
1120 struct vb2_plane planes[VB2_MAX_PLANES];
1121 struct vb2_queue *q = vb->vb2_queue;
1125 bool reacquired = vb->planes[0].mem_priv == NULL;
1127 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1128 /* Copy relevant information provided by the userspace */
1129 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1134 for (plane = 0; plane < vb->num_planes; ++plane) {
1135 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1137 if (IS_ERR_OR_NULL(dbuf)) {
1138 dprintk(1, "invalid dmabuf fd for plane %d\n",
1144 /* use DMABUF size if length is not provided */
1145 if (planes[plane].length == 0)
1146 planes[plane].length = dbuf->size;
1148 if (planes[plane].length < vb->planes[plane].min_length) {
1149 dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1150 planes[plane].length, plane,
1151 vb->planes[plane].min_length);
1157 /* Skip the plane if already verified */
1158 if (dbuf == vb->planes[plane].dbuf &&
1159 vb->planes[plane].length == planes[plane].length) {
1164 dprintk(3, "buffer for plane %d changed\n", plane);
1168 call_void_vb_qop(vb, buf_cleanup, vb);
1171 /* Release previously acquired memory if present */
1172 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1173 vb->planes[plane].bytesused = 0;
1174 vb->planes[plane].length = 0;
1175 vb->planes[plane].m.fd = 0;
1176 vb->planes[plane].data_offset = 0;
1178 /* Acquire each plane's memory */
1179 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1180 q->alloc_devs[plane] ? : q->dev,
1181 dbuf, planes[plane].length, q->dma_dir);
1182 if (IS_ERR(mem_priv)) {
1183 dprintk(1, "failed to attach dmabuf\n");
1184 ret = PTR_ERR(mem_priv);
1189 vb->planes[plane].dbuf = dbuf;
1190 vb->planes[plane].mem_priv = mem_priv;
1194 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1195 * here instead just before the DMA, while queueing the buffer(s) so
1196 * userspace knows sooner rather than later if the dma-buf map fails.
1198 for (plane = 0; plane < vb->num_planes; ++plane) {
1199 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1201 dprintk(1, "failed to map dmabuf for plane %d\n",
1205 vb->planes[plane].dbuf_mapped = 1;
1209 * Now that everything is in order, copy relevant information
1210 * provided by userspace.
1212 for (plane = 0; plane < vb->num_planes; ++plane) {
1213 vb->planes[plane].bytesused = planes[plane].bytesused;
1214 vb->planes[plane].length = planes[plane].length;
1215 vb->planes[plane].m.fd = planes[plane].m.fd;
1216 vb->planes[plane].data_offset = planes[plane].data_offset;
1221 * Call driver-specific initialization on the newly acquired buffer,
1224 ret = call_vb_qop(vb, buf_init, vb);
1226 dprintk(1, "buffer initialization failed\n");
1231 ret = call_vb_qop(vb, buf_prepare, vb);
1233 dprintk(1, "buffer preparation failed\n");
1234 call_void_vb_qop(vb, buf_cleanup, vb);
1240 /* In case of errors, release planes that were already acquired */
1241 __vb2_buf_dmabuf_put(vb);
1247 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1249 static void __enqueue_in_driver(struct vb2_buffer *vb)
1251 struct vb2_queue *q = vb->vb2_queue;
1253 vb->state = VB2_BUF_STATE_ACTIVE;
1254 atomic_inc(&q->owned_by_drv_count);
1256 trace_vb2_buf_queue(q, vb);
1258 call_void_vb_qop(vb, buf_queue, vb);
1261 static int __buf_prepare(struct vb2_buffer *vb)
1263 struct vb2_queue *q = vb->vb2_queue;
1264 enum vb2_buffer_state orig_state = vb->state;
1269 dprintk(1, "fatal error occurred on queue\n");
1275 WARN_ON(vb->synced);
1277 vb->state = VB2_BUF_STATE_PREPARING;
1279 switch (q->memory) {
1280 case VB2_MEMORY_MMAP:
1281 ret = __prepare_mmap(vb);
1283 case VB2_MEMORY_USERPTR:
1284 ret = __prepare_userptr(vb);
1286 case VB2_MEMORY_DMABUF:
1287 ret = __prepare_dmabuf(vb);
1290 WARN(1, "Invalid queue type\n");
1296 dprintk(1, "buffer preparation failed: %d\n", ret);
1297 vb->state = orig_state;
1302 for (plane = 0; plane < vb->num_planes; ++plane)
1303 call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
1306 vb->prepared = true;
1307 vb->state = orig_state;
1312 static int vb2_req_prepare(struct media_request_object *obj)
1314 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1317 if (WARN_ON(vb->state != VB2_BUF_STATE_IN_REQUEST))
1320 mutex_lock(vb->vb2_queue->lock);
1321 ret = __buf_prepare(vb);
1322 mutex_unlock(vb->vb2_queue->lock);
1326 static void __vb2_dqbuf(struct vb2_buffer *vb);
1328 static void vb2_req_unprepare(struct media_request_object *obj)
1330 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1332 mutex_lock(vb->vb2_queue->lock);
1334 vb->state = VB2_BUF_STATE_IN_REQUEST;
1335 mutex_unlock(vb->vb2_queue->lock);
1336 WARN_ON(!vb->req_obj.req);
1339 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1340 struct media_request *req);
1342 static void vb2_req_queue(struct media_request_object *obj)
1344 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1346 mutex_lock(vb->vb2_queue->lock);
1347 vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
1348 mutex_unlock(vb->vb2_queue->lock);
1351 static void vb2_req_unbind(struct media_request_object *obj)
1353 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1355 if (vb->state == VB2_BUF_STATE_IN_REQUEST)
1356 call_void_bufop(vb->vb2_queue, init_buffer, vb);
1359 static void vb2_req_release(struct media_request_object *obj)
1361 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1363 if (vb->state == VB2_BUF_STATE_IN_REQUEST) {
1364 vb->state = VB2_BUF_STATE_DEQUEUED;
1366 media_request_put(vb->request);
1371 static const struct media_request_object_ops vb2_core_req_ops = {
1372 .prepare = vb2_req_prepare,
1373 .unprepare = vb2_req_unprepare,
1374 .queue = vb2_req_queue,
1375 .unbind = vb2_req_unbind,
1376 .release = vb2_req_release,
1379 bool vb2_request_object_is_buffer(struct media_request_object *obj)
1381 return obj->ops == &vb2_core_req_ops;
1383 EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer);
1385 unsigned int vb2_request_buffer_cnt(struct media_request *req)
1387 struct media_request_object *obj;
1388 unsigned long flags;
1389 unsigned int buffer_cnt = 0;
1391 spin_lock_irqsave(&req->lock, flags);
1392 list_for_each_entry(obj, &req->objects, list)
1393 if (vb2_request_object_is_buffer(obj))
1395 spin_unlock_irqrestore(&req->lock, flags);
1399 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
1401 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1403 struct vb2_buffer *vb;
1406 vb = q->bufs[index];
1407 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1408 dprintk(1, "invalid buffer state %d\n",
1413 dprintk(1, "buffer already prepared\n");
1417 ret = __buf_prepare(vb);
1421 /* Fill buffer information for the userspace */
1422 call_void_bufop(q, fill_user_buffer, vb, pb);
1424 dprintk(2, "prepare of buffer %d succeeded\n", vb->index);
1428 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1431 * vb2_start_streaming() - Attempt to start streaming.
1432 * @q: videobuf2 queue
1434 * Attempt to start streaming. When this function is called there must be
1435 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1436 * number of buffers required for the DMA engine to function). If the
1437 * @start_streaming op fails it is supposed to return all the driver-owned
1438 * buffers back to vb2 in state QUEUED. Check if that happened and if
1439 * not warn and reclaim them forcefully.
1441 static int vb2_start_streaming(struct vb2_queue *q)
1443 struct vb2_buffer *vb;
1447 * If any buffers were queued before streamon,
1448 * we can now pass them to driver for processing.
1450 list_for_each_entry(vb, &q->queued_list, queued_entry)
1451 __enqueue_in_driver(vb);
1453 /* Tell the driver to start streaming */
1454 q->start_streaming_called = 1;
1455 ret = call_qop(q, start_streaming, q,
1456 atomic_read(&q->owned_by_drv_count));
1460 q->start_streaming_called = 0;
1462 dprintk(1, "driver refused to start streaming\n");
1464 * If you see this warning, then the driver isn't cleaning up properly
1465 * after a failed start_streaming(). See the start_streaming()
1466 * documentation in videobuf2-core.h for more information how buffers
1467 * should be returned to vb2 in start_streaming().
1469 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1473 * Forcefully reclaim buffers if the driver did not
1474 * correctly return them to vb2.
1476 for (i = 0; i < q->num_buffers; ++i) {
1478 if (vb->state == VB2_BUF_STATE_ACTIVE)
1479 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1481 /* Must be zero now */
1482 WARN_ON(atomic_read(&q->owned_by_drv_count));
1485 * If done_list is not empty, then start_streaming() didn't call
1486 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1489 WARN_ON(!list_empty(&q->done_list));
1493 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1494 struct media_request *req)
1496 struct vb2_buffer *vb;
1500 dprintk(1, "fatal error occurred on queue\n");
1504 vb = q->bufs[index];
1506 if ((req && q->uses_qbuf) ||
1507 (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1508 q->uses_requests)) {
1509 dprintk(1, "queue in wrong mode (qbuf vs requests)\n");
1516 q->uses_requests = 1;
1517 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1518 dprintk(1, "buffer %d not in dequeued state\n",
1523 media_request_object_init(&vb->req_obj);
1525 /* Make sure the request is in a safe state for updating. */
1526 ret = media_request_lock_for_update(req);
1529 ret = media_request_object_bind(req, &vb2_core_req_ops,
1530 q, true, &vb->req_obj);
1531 media_request_unlock_for_update(req);
1535 vb->state = VB2_BUF_STATE_IN_REQUEST;
1538 * Increment the refcount and store the request.
1539 * The request refcount is decremented again when the
1540 * buffer is dequeued. This is to prevent vb2_buffer_done()
1541 * from freeing the request from interrupt context, which can
1542 * happen if the application closed the request fd after
1543 * queueing the request.
1545 media_request_get(req);
1548 /* Fill buffer information for the userspace */
1550 call_void_bufop(q, copy_timestamp, vb, pb);
1551 call_void_bufop(q, fill_user_buffer, vb, pb);
1554 dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1558 if (vb->state != VB2_BUF_STATE_IN_REQUEST)
1561 switch (vb->state) {
1562 case VB2_BUF_STATE_DEQUEUED:
1563 case VB2_BUF_STATE_IN_REQUEST:
1564 if (!vb->prepared) {
1565 ret = __buf_prepare(vb);
1570 case VB2_BUF_STATE_PREPARING:
1571 dprintk(1, "buffer still being prepared\n");
1574 dprintk(1, "invalid buffer state %d\n", vb->state);
1579 * Add to the queued buffers list, a buffer will stay on it until
1580 * dequeued in dqbuf.
1582 list_add_tail(&vb->queued_entry, &q->queued_list);
1584 q->waiting_for_buffers = false;
1585 vb->state = VB2_BUF_STATE_QUEUED;
1588 call_void_bufop(q, copy_timestamp, vb, pb);
1590 trace_vb2_qbuf(q, vb);
1593 * If already streaming, give the buffer to driver for processing.
1594 * If not, the buffer will be given to driver on next streamon.
1596 if (q->start_streaming_called)
1597 __enqueue_in_driver(vb);
1599 /* Fill buffer information for the userspace */
1601 call_void_bufop(q, fill_user_buffer, vb, pb);
1604 * If streamon has been called, and we haven't yet called
1605 * start_streaming() since not enough buffers were queued, and
1606 * we now have reached the minimum number of queued buffers,
1607 * then we can finally call start_streaming().
1609 if (q->streaming && !q->start_streaming_called &&
1610 q->queued_count >= q->min_buffers_needed) {
1611 ret = vb2_start_streaming(q);
1616 dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1619 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1622 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1625 * Will sleep if required for nonblocking == false.
1627 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1630 * All operations on vb_done_list are performed under done_lock
1631 * spinlock protection. However, buffers may be removed from
1632 * it and returned to userspace only while holding both driver's
1633 * lock and the done_lock spinlock. Thus we can be sure that as
1634 * long as we hold the driver's lock, the list will remain not
1635 * empty if list_empty() check succeeds.
1641 if (!q->streaming) {
1642 dprintk(1, "streaming off, will not wait for buffers\n");
1647 dprintk(1, "Queue in error state, will not wait for buffers\n");
1651 if (q->last_buffer_dequeued) {
1652 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1656 if (!list_empty(&q->done_list)) {
1658 * Found a buffer that we were waiting for.
1664 dprintk(3, "nonblocking and no buffers to dequeue, will not wait\n");
1669 * We are streaming and blocking, wait for another buffer to
1670 * become ready or for streamoff. Driver's lock is released to
1671 * allow streamoff or qbuf to be called while waiting.
1673 call_void_qop(q, wait_prepare, q);
1676 * All locks have been released, it is safe to sleep now.
1678 dprintk(3, "will sleep waiting for buffers\n");
1679 ret = wait_event_interruptible(q->done_wq,
1680 !list_empty(&q->done_list) || !q->streaming ||
1684 * We need to reevaluate both conditions again after reacquiring
1685 * the locks or return an error if one occurred.
1687 call_void_qop(q, wait_finish, q);
1689 dprintk(1, "sleep was interrupted\n");
1697 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1699 * Will sleep if required for nonblocking == false.
1701 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1702 void *pb, int nonblocking)
1704 unsigned long flags;
1708 * Wait for at least one buffer to become available on the done_list.
1710 ret = __vb2_wait_for_done_vb(q, nonblocking);
1715 * Driver's lock has been held since we last verified that done_list
1716 * is not empty, so no need for another list_empty(done_list) check.
1718 spin_lock_irqsave(&q->done_lock, flags);
1719 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1721 * Only remove the buffer from done_list if all planes can be
1722 * handled. Some cases such as V4L2 file I/O and DVB have pb
1723 * == NULL; skip the check then as there's nothing to verify.
1726 ret = call_bufop(q, verify_planes_array, *vb, pb);
1728 list_del(&(*vb)->done_entry);
1729 spin_unlock_irqrestore(&q->done_lock, flags);
1734 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1736 if (!q->streaming) {
1737 dprintk(1, "streaming off, will not wait for buffers\n");
1741 if (q->start_streaming_called)
1742 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1745 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1748 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1750 static void __vb2_dqbuf(struct vb2_buffer *vb)
1752 struct vb2_queue *q = vb->vb2_queue;
1755 /* nothing to do if the buffer is already dequeued */
1756 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1759 vb->state = VB2_BUF_STATE_DEQUEUED;
1761 /* unmap DMABUF buffer */
1762 if (q->memory == VB2_MEMORY_DMABUF)
1763 for (i = 0; i < vb->num_planes; ++i) {
1764 if (!vb->planes[i].dbuf_mapped)
1766 call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1767 vb->planes[i].dbuf_mapped = 0;
1769 call_void_bufop(q, init_buffer, vb);
1772 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
1775 struct vb2_buffer *vb = NULL;
1778 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1782 switch (vb->state) {
1783 case VB2_BUF_STATE_DONE:
1784 dprintk(3, "returning done buffer\n");
1786 case VB2_BUF_STATE_ERROR:
1787 dprintk(3, "returning done buffer with errors\n");
1790 dprintk(1, "invalid buffer state\n");
1794 call_void_vb_qop(vb, buf_finish, vb);
1795 vb->prepared = false;
1798 *pindex = vb->index;
1800 /* Fill buffer information for the userspace */
1802 call_void_bufop(q, fill_user_buffer, vb, pb);
1804 /* Remove from videobuf queue */
1805 list_del(&vb->queued_entry);
1808 trace_vb2_dqbuf(q, vb);
1810 /* go back to dequeued state */
1813 if (WARN_ON(vb->req_obj.req)) {
1814 media_request_object_unbind(&vb->req_obj);
1815 media_request_object_put(&vb->req_obj);
1818 media_request_put(vb->request);
1821 dprintk(2, "dqbuf of buffer %d, with state %d\n",
1822 vb->index, vb->state);
1827 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1830 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1832 * Removes all queued buffers from driver's queue and all buffers queued by
1833 * userspace from videobuf's queue. Returns to state after reqbufs.
1835 static void __vb2_queue_cancel(struct vb2_queue *q)
1840 * Tell driver to stop all transactions and release all queued
1843 if (q->start_streaming_called)
1844 call_void_qop(q, stop_streaming, q);
1847 * If you see this warning, then the driver isn't cleaning up properly
1848 * in stop_streaming(). See the stop_streaming() documentation in
1849 * videobuf2-core.h for more information how buffers should be returned
1850 * to vb2 in stop_streaming().
1852 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1853 for (i = 0; i < q->num_buffers; ++i)
1854 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
1855 pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
1857 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1859 /* Must be zero now */
1860 WARN_ON(atomic_read(&q->owned_by_drv_count));
1864 q->start_streaming_called = 0;
1865 q->queued_count = 0;
1867 q->uses_requests = 0;
1871 * Remove all buffers from videobuf's list...
1873 INIT_LIST_HEAD(&q->queued_list);
1875 * ...and done list; userspace will not receive any buffers it
1876 * has not already dequeued before initiating cancel.
1878 INIT_LIST_HEAD(&q->done_list);
1879 atomic_set(&q->owned_by_drv_count, 0);
1880 wake_up_all(&q->done_wq);
1883 * Reinitialize all buffers for next use.
1884 * Make sure to call buf_finish for any queued buffers. Normally
1885 * that's done in dqbuf, but that's not going to happen when we
1886 * cancel the whole queue. Note: this code belongs here, not in
1887 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1888 * call to __fill_user_buffer() after buf_finish(). That order can't
1889 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1891 for (i = 0; i < q->num_buffers; ++i) {
1892 struct vb2_buffer *vb = q->bufs[i];
1893 struct media_request *req = vb->req_obj.req;
1896 * If a request is associated with this buffer, then
1897 * call buf_request_cancel() to give the driver to complete()
1898 * related request objects. Otherwise those objects would
1902 enum media_request_state state;
1903 unsigned long flags;
1905 spin_lock_irqsave(&req->lock, flags);
1907 spin_unlock_irqrestore(&req->lock, flags);
1909 if (state == MEDIA_REQUEST_STATE_QUEUED)
1910 call_void_vb_qop(vb, buf_request_complete, vb);
1916 for (plane = 0; plane < vb->num_planes; ++plane)
1917 call_void_memop(vb, finish,
1918 vb->planes[plane].mem_priv);
1923 call_void_vb_qop(vb, buf_finish, vb);
1924 vb->prepared = false;
1928 if (vb->req_obj.req) {
1929 media_request_object_unbind(&vb->req_obj);
1930 media_request_object_put(&vb->req_obj);
1933 media_request_put(vb->request);
1938 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1942 if (type != q->type) {
1943 dprintk(1, "invalid stream type\n");
1948 dprintk(3, "already streaming\n");
1952 if (!q->num_buffers) {
1953 dprintk(1, "no buffers have been allocated\n");
1957 if (q->num_buffers < q->min_buffers_needed) {
1958 dprintk(1, "need at least %u allocated buffers\n",
1959 q->min_buffers_needed);
1964 * Tell driver to start streaming provided sufficient buffers
1967 if (q->queued_count >= q->min_buffers_needed) {
1968 ret = v4l_vb2q_enable_media_source(q);
1971 ret = vb2_start_streaming(q);
1978 dprintk(3, "successful\n");
1981 EXPORT_SYMBOL_GPL(vb2_core_streamon);
1983 void vb2_queue_error(struct vb2_queue *q)
1987 wake_up_all(&q->done_wq);
1989 EXPORT_SYMBOL_GPL(vb2_queue_error);
1991 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
1993 if (type != q->type) {
1994 dprintk(1, "invalid stream type\n");
1999 * Cancel will pause streaming and remove all buffers from the driver
2000 * and videobuf, effectively returning control over them to userspace.
2002 * Note that we do this even if q->streaming == 0: if you prepare or
2003 * queue buffers, and then call streamoff without ever having called
2004 * streamon, you would still expect those buffers to be returned to
2005 * their normal dequeued state.
2007 __vb2_queue_cancel(q);
2008 q->waiting_for_buffers = !q->is_output;
2009 q->last_buffer_dequeued = false;
2011 dprintk(3, "successful\n");
2014 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
2017 * __find_plane_by_offset() - find plane associated with the given offset off
2019 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2020 unsigned int *_buffer, unsigned int *_plane)
2022 struct vb2_buffer *vb;
2023 unsigned int buffer, plane;
2026 * Go over all buffers and their planes, comparing the given offset
2027 * with an offset assigned to each plane. If a match is found,
2028 * return its buffer and plane numbers.
2030 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2031 vb = q->bufs[buffer];
2033 for (plane = 0; plane < vb->num_planes; ++plane) {
2034 if (vb->planes[plane].m.offset == off) {
2045 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2046 unsigned int index, unsigned int plane, unsigned int flags)
2048 struct vb2_buffer *vb = NULL;
2049 struct vb2_plane *vb_plane;
2051 struct dma_buf *dbuf;
2053 if (q->memory != VB2_MEMORY_MMAP) {
2054 dprintk(1, "queue is not currently set up for mmap\n");
2058 if (!q->mem_ops->get_dmabuf) {
2059 dprintk(1, "queue does not support DMA buffer exporting\n");
2063 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
2064 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2068 if (type != q->type) {
2069 dprintk(1, "invalid buffer type\n");
2073 if (index >= q->num_buffers) {
2074 dprintk(1, "buffer index out of range\n");
2078 vb = q->bufs[index];
2080 if (plane >= vb->num_planes) {
2081 dprintk(1, "buffer plane out of range\n");
2085 if (vb2_fileio_is_active(q)) {
2086 dprintk(1, "expbuf: file io in progress\n");
2090 vb_plane = &vb->planes[plane];
2092 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
2094 if (IS_ERR_OR_NULL(dbuf)) {
2095 dprintk(1, "failed to export buffer %d, plane %d\n",
2100 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
2102 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2108 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2114 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
2116 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2118 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
2119 struct vb2_buffer *vb;
2120 unsigned int buffer = 0, plane = 0;
2122 unsigned long length;
2124 if (q->memory != VB2_MEMORY_MMAP) {
2125 dprintk(1, "queue is not currently set up for mmap\n");
2130 * Check memory area access mode.
2132 if (!(vma->vm_flags & VM_SHARED)) {
2133 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2137 if (!(vma->vm_flags & VM_WRITE)) {
2138 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2142 if (!(vma->vm_flags & VM_READ)) {
2143 dprintk(1, "invalid vma flags, VM_READ needed\n");
2148 mutex_lock(&q->mmap_lock);
2150 if (vb2_fileio_is_active(q)) {
2151 dprintk(1, "mmap: file io in progress\n");
2157 * Find the plane corresponding to the offset passed by userspace.
2159 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2163 vb = q->bufs[buffer];
2166 * MMAP requires page_aligned buffers.
2167 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2168 * so, we need to do the same here.
2170 length = PAGE_ALIGN(vb->planes[plane].length);
2171 if (length < (vma->vm_end - vma->vm_start)) {
2173 "MMAP invalid, as it would overflow buffer length\n");
2178 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2181 mutex_unlock(&q->mmap_lock);
2185 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2188 EXPORT_SYMBOL_GPL(vb2_mmap);
2191 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2194 unsigned long pgoff,
2195 unsigned long flags)
2197 unsigned long off = pgoff << PAGE_SHIFT;
2198 struct vb2_buffer *vb;
2199 unsigned int buffer, plane;
2203 if (q->memory != VB2_MEMORY_MMAP) {
2204 dprintk(1, "queue is not currently set up for mmap\n");
2209 * Find the plane corresponding to the offset passed by userspace.
2211 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2215 vb = q->bufs[buffer];
2217 vaddr = vb2_plane_vaddr(vb, plane);
2218 return vaddr ? (unsigned long)vaddr : -EINVAL;
2220 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2223 int vb2_core_queue_init(struct vb2_queue *q)
2230 WARN_ON(!q->mem_ops) ||
2231 WARN_ON(!q->type) ||
2232 WARN_ON(!q->io_modes) ||
2233 WARN_ON(!q->ops->queue_setup) ||
2234 WARN_ON(!q->ops->buf_queue))
2237 INIT_LIST_HEAD(&q->queued_list);
2238 INIT_LIST_HEAD(&q->done_list);
2239 spin_lock_init(&q->done_lock);
2240 mutex_init(&q->mmap_lock);
2241 init_waitqueue_head(&q->done_wq);
2243 q->memory = VB2_MEMORY_UNKNOWN;
2245 if (q->buf_struct_size == 0)
2246 q->buf_struct_size = sizeof(struct vb2_buffer);
2248 if (q->bidirectional)
2249 q->dma_dir = DMA_BIDIRECTIONAL;
2251 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2255 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2257 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2258 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2259 void vb2_core_queue_release(struct vb2_queue *q)
2261 __vb2_cleanup_fileio(q);
2262 __vb2_queue_cancel(q);
2263 mutex_lock(&q->mmap_lock);
2264 __vb2_queue_free(q, q->num_buffers);
2265 mutex_unlock(&q->mmap_lock);
2267 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2269 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
2272 __poll_t req_events = poll_requested_events(wait);
2273 struct vb2_buffer *vb = NULL;
2274 unsigned long flags;
2276 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM)))
2278 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
2282 * Start file I/O emulator only if streaming API has not been used yet.
2284 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
2285 if (!q->is_output && (q->io_modes & VB2_READ) &&
2286 (req_events & (EPOLLIN | EPOLLRDNORM))) {
2287 if (__vb2_init_fileio(q, 1))
2290 if (q->is_output && (q->io_modes & VB2_WRITE) &&
2291 (req_events & (EPOLLOUT | EPOLLWRNORM))) {
2292 if (__vb2_init_fileio(q, 0))
2295 * Write to OUTPUT queue can be done immediately.
2297 return EPOLLOUT | EPOLLWRNORM;
2302 * There is nothing to wait for if the queue isn't streaming, or if the
2303 * error flag is set.
2305 if (!vb2_is_streaming(q) || q->error)
2309 * If this quirk is set and QBUF hasn't been called yet then
2310 * return EPOLLERR as well. This only affects capture queues, output
2311 * queues will always initialize waiting_for_buffers to false.
2312 * This quirk is set by V4L2 for backwards compatibility reasons.
2314 if (q->quirk_poll_must_check_waiting_for_buffers &&
2315 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM)))
2319 * For output streams you can call write() as long as there are fewer
2320 * buffers queued than there are buffers available.
2322 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
2323 return EPOLLOUT | EPOLLWRNORM;
2325 if (list_empty(&q->done_list)) {
2327 * If the last buffer was dequeued from a capture queue,
2328 * return immediately. DQBUF will return -EPIPE.
2330 if (q->last_buffer_dequeued)
2331 return EPOLLIN | EPOLLRDNORM;
2333 poll_wait(file, &q->done_wq, wait);
2337 * Take first buffer available for dequeuing.
2339 spin_lock_irqsave(&q->done_lock, flags);
2340 if (!list_empty(&q->done_list))
2341 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2343 spin_unlock_irqrestore(&q->done_lock, flags);
2345 if (vb && (vb->state == VB2_BUF_STATE_DONE
2346 || vb->state == VB2_BUF_STATE_ERROR)) {
2347 return (q->is_output) ?
2348 EPOLLOUT | EPOLLWRNORM :
2349 EPOLLIN | EPOLLRDNORM;
2353 EXPORT_SYMBOL_GPL(vb2_core_poll);
2356 * struct vb2_fileio_buf - buffer context used by file io emulator
2358 * vb2 provides a compatibility layer and emulator of file io (read and
2359 * write) calls on top of streaming API. This structure is used for
2360 * tracking context related to the buffers.
2362 struct vb2_fileio_buf {
2366 unsigned int queued:1;
2370 * struct vb2_fileio_data - queue context used by file io emulator
2372 * @cur_index: the index of the buffer currently being read from or
2373 * written to. If equal to q->num_buffers then a new buffer
2375 * @initial_index: in the read() case all buffers are queued up immediately
2376 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2377 * buffers. However, in the write() case no buffers are initially
2378 * queued, instead whenever a buffer is full it is queued up by
2379 * __vb2_perform_fileio(). Only once all available buffers have
2380 * been queued up will __vb2_perform_fileio() start to dequeue
2381 * buffers. This means that initially __vb2_perform_fileio()
2382 * needs to know what buffer index to use when it is queuing up
2383 * the buffers for the first time. That initial index is stored
2384 * in this field. Once it is equal to q->num_buffers all
2385 * available buffers have been queued and __vb2_perform_fileio()
2386 * should start the normal dequeue/queue cycle.
2388 * vb2 provides a compatibility layer and emulator of file io (read and
2389 * write) calls on top of streaming API. For proper operation it required
2390 * this structure to save the driver state between each call of the read
2391 * or write function.
2393 struct vb2_fileio_data {
2396 unsigned int memory;
2397 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
2398 unsigned int cur_index;
2399 unsigned int initial_index;
2400 unsigned int q_count;
2401 unsigned int dq_count;
2402 unsigned read_once:1;
2403 unsigned write_immediately:1;
2407 * __vb2_init_fileio() - initialize file io emulator
2408 * @q: videobuf2 queue
2409 * @read: mode selector (1 means read, 0 means write)
2411 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2413 struct vb2_fileio_data *fileio;
2415 unsigned int count = 0;
2420 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
2421 (!read && !(q->io_modes & VB2_WRITE))))
2425 * Check if device supports mapping buffers to kernel virtual space.
2427 if (!q->mem_ops->vaddr)
2431 * Check if streaming api has not been already activated.
2433 if (q->streaming || q->num_buffers > 0)
2437 * Start with count 1, driver can increase it in queue_setup()
2441 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2442 (read) ? "read" : "write", count, q->fileio_read_once,
2443 q->fileio_write_immediately);
2445 fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
2449 fileio->read_once = q->fileio_read_once;
2450 fileio->write_immediately = q->fileio_write_immediately;
2453 * Request buffers and use MMAP type to force driver
2454 * to allocate buffers by itself.
2456 fileio->count = count;
2457 fileio->memory = VB2_MEMORY_MMAP;
2458 fileio->type = q->type;
2460 ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2465 * Check if plane_count is correct
2466 * (multiplane buffers are not supported).
2468 if (q->bufs[0]->num_planes != 1) {
2474 * Get kernel address of each buffer.
2476 for (i = 0; i < q->num_buffers; i++) {
2477 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2478 if (fileio->bufs[i].vaddr == NULL) {
2482 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2486 * Read mode requires pre queuing of all buffers.
2490 * Queue all buffers.
2492 for (i = 0; i < q->num_buffers; i++) {
2493 ret = vb2_core_qbuf(q, i, NULL, NULL);
2496 fileio->bufs[i].queued = 1;
2499 * All buffers have been queued, so mark that by setting
2500 * initial_index to q->num_buffers
2502 fileio->initial_index = q->num_buffers;
2503 fileio->cur_index = q->num_buffers;
2509 ret = vb2_core_streamon(q, q->type);
2517 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2526 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2527 * @q: videobuf2 queue
2529 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2531 struct vb2_fileio_data *fileio = q->fileio;
2534 vb2_core_streamoff(q, q->type);
2537 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2539 dprintk(3, "file io emulator closed\n");
2545 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2546 * @q: videobuf2 queue
2547 * @data: pointed to target userspace buffer
2548 * @count: number of bytes to read or write
2549 * @ppos: file handle position tracking pointer
2550 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2551 * @read: access mode selector (1 means read, 0 means write)
2553 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2554 loff_t *ppos, int nonblock, int read)
2556 struct vb2_fileio_data *fileio;
2557 struct vb2_fileio_buf *buf;
2558 bool is_multiplanar = q->is_multiplanar;
2560 * When using write() to write data to an output video node the vb2 core
2561 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2562 * else is able to provide this information with the write() operation.
2564 bool copy_timestamp = !read && q->copy_timestamp;
2568 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2569 read ? "read" : "write", (long)*ppos, count,
2570 nonblock ? "non" : "");
2576 * Initialize emulator on first call.
2578 if (!vb2_fileio_is_active(q)) {
2579 ret = __vb2_init_fileio(q, read);
2580 dprintk(3, "vb2_init_fileio result: %d\n", ret);
2587 * Check if we need to dequeue the buffer.
2589 index = fileio->cur_index;
2590 if (index >= q->num_buffers) {
2591 struct vb2_buffer *b;
2594 * Call vb2_dqbuf to get buffer back.
2596 ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2597 dprintk(5, "vb2_dqbuf result: %d\n", ret);
2600 fileio->dq_count += 1;
2602 fileio->cur_index = index;
2603 buf = &fileio->bufs[index];
2607 * Get number of bytes filled by the driver
2611 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2612 : vb2_plane_size(q->bufs[index], 0);
2613 /* Compensate for data_offset on read in the multiplanar case. */
2614 if (is_multiplanar && read &&
2615 b->planes[0].data_offset < buf->size) {
2616 buf->pos = b->planes[0].data_offset;
2617 buf->size -= buf->pos;
2620 buf = &fileio->bufs[index];
2624 * Limit count on last few bytes of the buffer.
2626 if (buf->pos + count > buf->size) {
2627 count = buf->size - buf->pos;
2628 dprintk(5, "reducing read count: %zd\n", count);
2632 * Transfer data to userspace.
2634 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2635 count, index, buf->pos);
2637 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2639 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2641 dprintk(3, "error copying data\n");
2652 * Queue next buffer if required.
2654 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
2655 struct vb2_buffer *b = q->bufs[index];
2658 * Check if this is the last buffer to read.
2660 if (read && fileio->read_once && fileio->dq_count == 1) {
2661 dprintk(3, "read limit reached\n");
2662 return __vb2_cleanup_fileio(q);
2666 * Call vb2_qbuf and give buffer to the driver.
2668 b->planes[0].bytesused = buf->pos;
2671 b->timestamp = ktime_get_ns();
2672 ret = vb2_core_qbuf(q, index, NULL, NULL);
2673 dprintk(5, "vb2_dbuf result: %d\n", ret);
2678 * Buffer has been queued, update the status
2682 buf->size = vb2_plane_size(q->bufs[index], 0);
2683 fileio->q_count += 1;
2685 * If we are queuing up buffers for the first time, then
2686 * increase initial_index by one.
2688 if (fileio->initial_index < q->num_buffers)
2689 fileio->initial_index++;
2691 * The next buffer to use is either a buffer that's going to be
2692 * queued for the first time (initial_index < q->num_buffers)
2693 * or it is equal to q->num_buffers, meaning that the next
2694 * time we need to dequeue a buffer since we've now queued up
2695 * all the 'first time' buffers.
2697 fileio->cur_index = fileio->initial_index;
2701 * Return proper number of bytes processed.
2708 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2709 loff_t *ppos, int nonblocking)
2711 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2713 EXPORT_SYMBOL_GPL(vb2_read);
2715 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
2716 loff_t *ppos, int nonblocking)
2718 return __vb2_perform_fileio(q, (char __user *) data, count,
2719 ppos, nonblocking, 0);
2721 EXPORT_SYMBOL_GPL(vb2_write);
2723 struct vb2_threadio_data {
2724 struct task_struct *thread;
2730 static int vb2_thread(void *data)
2732 struct vb2_queue *q = data;
2733 struct vb2_threadio_data *threadio = q->threadio;
2734 bool copy_timestamp = false;
2735 unsigned prequeue = 0;
2740 prequeue = q->num_buffers;
2741 copy_timestamp = q->copy_timestamp;
2747 struct vb2_buffer *vb;
2750 * Call vb2_dqbuf to get buffer back.
2753 vb = q->bufs[index++];
2756 call_void_qop(q, wait_finish, q);
2757 if (!threadio->stop)
2758 ret = vb2_core_dqbuf(q, &index, NULL, 0);
2759 call_void_qop(q, wait_prepare, q);
2760 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2762 vb = q->bufs[index];
2764 if (ret || threadio->stop)
2768 if (vb->state != VB2_BUF_STATE_ERROR)
2769 if (threadio->fnc(vb, threadio->priv))
2771 call_void_qop(q, wait_finish, q);
2773 vb->timestamp = ktime_get_ns();
2774 if (!threadio->stop)
2775 ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
2776 call_void_qop(q, wait_prepare, q);
2777 if (ret || threadio->stop)
2781 /* Hmm, linux becomes *very* unhappy without this ... */
2782 while (!kthread_should_stop()) {
2783 set_current_state(TASK_INTERRUPTIBLE);
2790 * This function should not be used for anything else but the videobuf2-dvb
2791 * support. If you think you have another good use-case for this, then please
2792 * contact the linux-media mailinglist first.
2794 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
2795 const char *thread_name)
2797 struct vb2_threadio_data *threadio;
2804 if (WARN_ON(q->fileio))
2807 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
2808 if (threadio == NULL)
2810 threadio->fnc = fnc;
2811 threadio->priv = priv;
2813 ret = __vb2_init_fileio(q, !q->is_output);
2814 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2817 q->threadio = threadio;
2818 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
2819 if (IS_ERR(threadio->thread)) {
2820 ret = PTR_ERR(threadio->thread);
2821 threadio->thread = NULL;
2827 __vb2_cleanup_fileio(q);
2832 EXPORT_SYMBOL_GPL(vb2_thread_start);
2834 int vb2_thread_stop(struct vb2_queue *q)
2836 struct vb2_threadio_data *threadio = q->threadio;
2839 if (threadio == NULL)
2841 threadio->stop = true;
2842 /* Wake up all pending sleeps in the thread */
2844 err = kthread_stop(threadio->thread);
2845 __vb2_cleanup_fileio(q);
2846 threadio->thread = NULL;
2851 EXPORT_SYMBOL_GPL(vb2_thread_stop);
2853 MODULE_DESCRIPTION("Media buffer core framework");
2854 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2855 MODULE_LICENSE("GPL");