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(q, level, fmt, arg...) \
40 pr_info("[%s] %s: " fmt, (q)->name, __func__, \
44 #ifdef CONFIG_VIDEO_ADV_DEBUG
47 * If advanced debugging is on, then count how often each op is called
48 * successfully, which can either be per-buffer or per-queue.
50 * This makes it easy to check that the 'init' and 'cleanup'
51 * (and variations thereof) stay balanced.
54 #define log_memop(vb, op) \
55 dprintk((vb)->vb2_queue, 2, "call_memop(%d, %s)%s\n", \
57 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
59 #define call_memop(vb, op, args...) \
61 struct vb2_queue *_q = (vb)->vb2_queue; \
65 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
67 (vb)->cnt_mem_ ## op++; \
71 #define call_ptr_memop(op, vb, args...) \
73 struct vb2_queue *_q = (vb)->vb2_queue; \
77 ptr = _q->mem_ops->op ? _q->mem_ops->op(vb, args) : NULL; \
78 if (!IS_ERR_OR_NULL(ptr)) \
79 (vb)->cnt_mem_ ## op++; \
83 #define call_void_memop(vb, op, args...) \
85 struct vb2_queue *_q = (vb)->vb2_queue; \
88 if (_q->mem_ops->op) \
89 _q->mem_ops->op(args); \
90 (vb)->cnt_mem_ ## op++; \
93 #define log_qop(q, op) \
94 dprintk(q, 2, "call_qop(%s)%s\n", #op, \
95 (q)->ops->op ? "" : " (nop)")
97 #define call_qop(q, op, args...) \
102 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
108 #define call_void_qop(q, op, args...) \
112 (q)->ops->op(args); \
116 #define log_vb_qop(vb, op, args...) \
117 dprintk((vb)->vb2_queue, 2, "call_vb_qop(%d, %s)%s\n", \
119 (vb)->vb2_queue->ops->op ? "" : " (nop)")
121 #define call_vb_qop(vb, op, args...) \
125 log_vb_qop(vb, op); \
126 err = (vb)->vb2_queue->ops->op ? \
127 (vb)->vb2_queue->ops->op(args) : 0; \
129 (vb)->cnt_ ## op++; \
133 #define call_void_vb_qop(vb, op, args...) \
135 log_vb_qop(vb, op); \
136 if ((vb)->vb2_queue->ops->op) \
137 (vb)->vb2_queue->ops->op(args); \
138 (vb)->cnt_ ## op++; \
143 #define call_memop(vb, op, args...) \
144 ((vb)->vb2_queue->mem_ops->op ? \
145 (vb)->vb2_queue->mem_ops->op(args) : 0)
147 #define call_ptr_memop(op, vb, args...) \
148 ((vb)->vb2_queue->mem_ops->op ? \
149 (vb)->vb2_queue->mem_ops->op(vb, args) : NULL)
151 #define call_void_memop(vb, op, args...) \
153 if ((vb)->vb2_queue->mem_ops->op) \
154 (vb)->vb2_queue->mem_ops->op(args); \
157 #define call_qop(q, op, args...) \
158 ((q)->ops->op ? (q)->ops->op(args) : 0)
160 #define call_void_qop(q, op, args...) \
163 (q)->ops->op(args); \
166 #define call_vb_qop(vb, op, args...) \
167 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
169 #define call_void_vb_qop(vb, op, args...) \
171 if ((vb)->vb2_queue->ops->op) \
172 (vb)->vb2_queue->ops->op(args); \
177 #define call_bufop(q, op, args...) \
180 if (q && q->buf_ops && q->buf_ops->op) \
181 ret = q->buf_ops->op(args); \
185 #define call_void_bufop(q, op, args...) \
187 if (q && q->buf_ops && q->buf_ops->op) \
188 q->buf_ops->op(args); \
191 static void __vb2_queue_cancel(struct vb2_queue *q);
192 static void __enqueue_in_driver(struct vb2_buffer *vb);
194 static const char *vb2_state_name(enum vb2_buffer_state s)
196 static const char * const state_names[] = {
197 [VB2_BUF_STATE_DEQUEUED] = "dequeued",
198 [VB2_BUF_STATE_IN_REQUEST] = "in request",
199 [VB2_BUF_STATE_PREPARING] = "preparing",
200 [VB2_BUF_STATE_QUEUED] = "queued",
201 [VB2_BUF_STATE_ACTIVE] = "active",
202 [VB2_BUF_STATE_DONE] = "done",
203 [VB2_BUF_STATE_ERROR] = "error",
206 if ((unsigned int)(s) < ARRAY_SIZE(state_names))
207 return state_names[s];
212 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
214 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
216 struct vb2_queue *q = vb->vb2_queue;
222 * Allocate memory for all planes in this buffer
223 * NOTE: mmapped areas should be page aligned
225 for (plane = 0; plane < vb->num_planes; ++plane) {
226 /* Memops alloc requires size to be page aligned. */
227 unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
229 /* Did it wrap around? */
230 if (size < vb->planes[plane].length)
233 mem_priv = call_ptr_memop(alloc,
235 q->alloc_devs[plane] ? : q->dev,
237 if (IS_ERR_OR_NULL(mem_priv)) {
239 ret = PTR_ERR(mem_priv);
243 /* Associate allocator private data with this plane */
244 vb->planes[plane].mem_priv = mem_priv;
249 /* Free already allocated memory if one of the allocations failed */
250 for (; plane > 0; --plane) {
251 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
252 vb->planes[plane - 1].mem_priv = NULL;
259 * __vb2_buf_mem_free() - free memory of the given buffer
261 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
265 for (plane = 0; plane < vb->num_planes; ++plane) {
266 call_void_memop(vb, put, vb->planes[plane].mem_priv);
267 vb->planes[plane].mem_priv = NULL;
268 dprintk(vb->vb2_queue, 3, "freed plane %d of buffer %d\n",
274 * __vb2_buf_userptr_put() - release userspace memory associated with
277 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
281 for (plane = 0; plane < vb->num_planes; ++plane) {
282 if (vb->planes[plane].mem_priv)
283 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
284 vb->planes[plane].mem_priv = NULL;
289 * __vb2_plane_dmabuf_put() - release memory associated with
290 * a DMABUF shared plane
292 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
298 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
300 call_void_memop(vb, detach_dmabuf, p->mem_priv);
301 dma_buf_put(p->dbuf);
308 * __vb2_buf_dmabuf_put() - release memory associated with
309 * a DMABUF shared buffer
311 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
315 for (plane = 0; plane < vb->num_planes; ++plane)
316 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
320 * __vb2_buf_mem_prepare() - call ->prepare() on buffer's private memory
323 static void __vb2_buf_mem_prepare(struct vb2_buffer *vb)
330 if (vb->need_cache_sync_on_prepare) {
331 for (plane = 0; plane < vb->num_planes; ++plane)
332 call_void_memop(vb, prepare,
333 vb->planes[plane].mem_priv);
339 * __vb2_buf_mem_finish() - call ->finish on buffer's private memory
342 static void __vb2_buf_mem_finish(struct vb2_buffer *vb)
349 if (vb->need_cache_sync_on_finish) {
350 for (plane = 0; plane < vb->num_planes; ++plane)
351 call_void_memop(vb, finish,
352 vb->planes[plane].mem_priv);
358 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
361 static void __setup_offsets(struct vb2_buffer *vb)
363 struct vb2_queue *q = vb->vb2_queue;
365 unsigned long off = 0;
368 struct vb2_buffer *prev = q->bufs[vb->index - 1];
369 struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
371 off = PAGE_ALIGN(p->m.offset + p->length);
374 for (plane = 0; plane < vb->num_planes; ++plane) {
375 vb->planes[plane].m.offset = off;
377 dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n",
378 vb->index, plane, off);
380 off += vb->planes[plane].length;
381 off = PAGE_ALIGN(off);
386 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
387 * video buffer memory for all buffers/planes on the queue and initializes the
390 * Returns the number of buffers successfully allocated.
392 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
393 unsigned int num_buffers, unsigned int num_planes,
394 const unsigned plane_sizes[VB2_MAX_PLANES])
396 unsigned int buffer, plane;
397 struct vb2_buffer *vb;
400 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
401 num_buffers = min_t(unsigned int, num_buffers,
402 VB2_MAX_FRAME - q->num_buffers);
404 for (buffer = 0; buffer < num_buffers; ++buffer) {
405 /* Allocate videobuf buffer structures */
406 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
408 dprintk(q, 1, "memory alloc for buffer struct failed\n");
412 vb->state = VB2_BUF_STATE_DEQUEUED;
414 vb->num_planes = num_planes;
415 vb->index = q->num_buffers + buffer;
419 * We need to set these flags here so that the videobuf2 core
420 * will call ->prepare()/->finish() cache sync/flush on vb2
421 * buffers when appropriate. However, we can avoid explicit
422 * ->prepare() and ->finish() cache sync for DMABUF buffers,
423 * because DMA exporter takes care of it.
425 if (q->memory != VB2_MEMORY_DMABUF) {
426 vb->need_cache_sync_on_prepare = 1;
427 vb->need_cache_sync_on_finish = 1;
429 for (plane = 0; plane < num_planes; ++plane) {
430 vb->planes[plane].length = plane_sizes[plane];
431 vb->planes[plane].min_length = plane_sizes[plane];
433 call_void_bufop(q, init_buffer, vb);
435 q->bufs[vb->index] = vb;
437 /* Allocate video buffer memory for the MMAP type */
438 if (memory == VB2_MEMORY_MMAP) {
439 ret = __vb2_buf_mem_alloc(vb);
441 dprintk(q, 1, "failed allocating memory for buffer %d\n",
443 q->bufs[vb->index] = NULL;
449 * Call the driver-provided buffer initialization
450 * callback, if given. An error in initialization
451 * results in queue setup failure.
453 ret = call_vb_qop(vb, buf_init, vb);
455 dprintk(q, 1, "buffer %d %p initialization failed\n",
457 __vb2_buf_mem_free(vb);
458 q->bufs[vb->index] = NULL;
465 dprintk(q, 3, "allocated %d buffers, %d plane(s) each\n",
472 * __vb2_free_mem() - release all video buffer memory for a given queue
474 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
477 struct vb2_buffer *vb;
479 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
481 vb = q->bufs[buffer];
485 /* Free MMAP buffers or release USERPTR buffers */
486 if (q->memory == VB2_MEMORY_MMAP)
487 __vb2_buf_mem_free(vb);
488 else if (q->memory == VB2_MEMORY_DMABUF)
489 __vb2_buf_dmabuf_put(vb);
491 __vb2_buf_userptr_put(vb);
496 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
497 * related information, if no buffers are left return the queue to an
498 * uninitialized state. Might be called even if the queue has already been freed.
500 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
505 * Sanity check: when preparing a buffer the queue lock is released for
506 * a short while (see __buf_prepare for the details), which would allow
507 * a race with a reqbufs which can call this function. Removing the
508 * buffers from underneath __buf_prepare is obviously a bad idea, so we
509 * check if any of the buffers is in the state PREPARING, and if so we
510 * just return -EAGAIN.
512 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
514 if (q->bufs[buffer] == NULL)
516 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
517 dprintk(q, 1, "preparing buffers, cannot free\n");
522 /* Call driver-provided cleanup function for each buffer, if provided */
523 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
525 struct vb2_buffer *vb = q->bufs[buffer];
527 if (vb && vb->planes[0].mem_priv)
528 call_void_vb_qop(vb, buf_cleanup, vb);
531 /* Release video buffer memory */
532 __vb2_free_mem(q, buffers);
534 #ifdef CONFIG_VIDEO_ADV_DEBUG
536 * Check that all the calls were balances during the life-time of this
537 * queue. If not (or if the debug level is 1 or up), then dump the
538 * counters to the kernel log.
540 if (q->num_buffers) {
541 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
542 q->cnt_wait_prepare != q->cnt_wait_finish;
544 if (unbalanced || debug) {
545 pr_info("counters for queue %p:%s\n", q,
546 unbalanced ? " UNBALANCED!" : "");
547 pr_info(" setup: %u start_streaming: %u stop_streaming: %u\n",
548 q->cnt_queue_setup, q->cnt_start_streaming,
549 q->cnt_stop_streaming);
550 pr_info(" wait_prepare: %u wait_finish: %u\n",
551 q->cnt_wait_prepare, q->cnt_wait_finish);
553 q->cnt_queue_setup = 0;
554 q->cnt_wait_prepare = 0;
555 q->cnt_wait_finish = 0;
556 q->cnt_start_streaming = 0;
557 q->cnt_stop_streaming = 0;
559 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
560 struct vb2_buffer *vb = q->bufs[buffer];
561 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
562 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
563 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
564 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
565 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
566 vb->cnt_buf_queue != vb->cnt_buf_done ||
567 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
568 vb->cnt_buf_init != vb->cnt_buf_cleanup;
570 if (unbalanced || debug) {
571 pr_info(" counters for queue %p, buffer %d:%s\n",
572 q, buffer, unbalanced ? " UNBALANCED!" : "");
573 pr_info(" buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
574 vb->cnt_buf_init, vb->cnt_buf_cleanup,
575 vb->cnt_buf_prepare, vb->cnt_buf_finish);
576 pr_info(" buf_out_validate: %u buf_queue: %u buf_done: %u buf_request_complete: %u\n",
577 vb->cnt_buf_out_validate, vb->cnt_buf_queue,
578 vb->cnt_buf_done, vb->cnt_buf_request_complete);
579 pr_info(" alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
580 vb->cnt_mem_alloc, vb->cnt_mem_put,
581 vb->cnt_mem_prepare, vb->cnt_mem_finish,
583 pr_info(" get_userptr: %u put_userptr: %u\n",
584 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
585 pr_info(" attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
586 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
587 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
588 pr_info(" get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
589 vb->cnt_mem_get_dmabuf,
590 vb->cnt_mem_num_users,
597 /* Free videobuf buffers */
598 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
600 kfree(q->bufs[buffer]);
601 q->bufs[buffer] = NULL;
604 q->num_buffers -= buffers;
605 if (!q->num_buffers) {
606 q->memory = VB2_MEMORY_UNKNOWN;
607 INIT_LIST_HEAD(&q->queued_list);
612 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
615 for (plane = 0; plane < vb->num_planes; ++plane) {
616 void *mem_priv = vb->planes[plane].mem_priv;
618 * If num_users() has not been provided, call_memop
619 * will return 0, apparently nobody cares about this
620 * case anyway. If num_users() returns more than 1,
621 * we are not the only user of the plane's memory.
623 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
628 EXPORT_SYMBOL(vb2_buffer_in_use);
631 * __buffers_in_use() - return true if any buffers on the queue are in use and
632 * the queue cannot be freed (by the means of REQBUFS(0)) call
634 static bool __buffers_in_use(struct vb2_queue *q)
637 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
638 if (vb2_buffer_in_use(q, q->bufs[buffer]))
644 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
646 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
648 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
651 * __verify_userptr_ops() - verify that all memory operations required for
652 * USERPTR queue type have been provided
654 static int __verify_userptr_ops(struct vb2_queue *q)
656 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
657 !q->mem_ops->put_userptr)
664 * __verify_mmap_ops() - verify that all memory operations required for
665 * MMAP queue type have been provided
667 static int __verify_mmap_ops(struct vb2_queue *q)
669 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
670 !q->mem_ops->put || !q->mem_ops->mmap)
677 * __verify_dmabuf_ops() - verify that all memory operations required for
678 * DMABUF queue type have been provided
680 static int __verify_dmabuf_ops(struct vb2_queue *q)
682 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
683 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
684 !q->mem_ops->unmap_dmabuf)
690 int vb2_verify_memory_type(struct vb2_queue *q,
691 enum vb2_memory memory, unsigned int type)
693 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
694 memory != VB2_MEMORY_DMABUF) {
695 dprintk(q, 1, "unsupported memory type\n");
699 if (type != q->type) {
700 dprintk(q, 1, "requested type is incorrect\n");
705 * Make sure all the required memory ops for given memory type
708 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
709 dprintk(q, 1, "MMAP for current setup unsupported\n");
713 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
714 dprintk(q, 1, "USERPTR for current setup unsupported\n");
718 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
719 dprintk(q, 1, "DMABUF for current setup unsupported\n");
724 * Place the busy tests at the end: -EBUSY can be ignored when
725 * create_bufs is called with count == 0, but count == 0 should still
726 * do the memory and type validation.
728 if (vb2_fileio_is_active(q)) {
729 dprintk(q, 1, "file io in progress\n");
734 EXPORT_SYMBOL(vb2_verify_memory_type);
736 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
739 unsigned int num_buffers, allocated_buffers, num_planes = 0;
740 unsigned plane_sizes[VB2_MAX_PLANES] = { };
745 dprintk(q, 1, "streaming active\n");
749 if (q->waiting_in_dqbuf && *count) {
750 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
754 if (*count == 0 || q->num_buffers != 0 ||
755 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
757 * We already have buffers allocated, so first check if they
758 * are not in use and can be freed.
760 mutex_lock(&q->mmap_lock);
761 if (debug && q->memory == VB2_MEMORY_MMAP &&
763 dprintk(q, 1, "memory in use, orphaning buffers\n");
766 * Call queue_cancel to clean up any buffers in the
767 * QUEUED state which is possible if buffers were prepared or
768 * queued without ever calling STREAMON.
770 __vb2_queue_cancel(q);
771 ret = __vb2_queue_free(q, q->num_buffers);
772 mutex_unlock(&q->mmap_lock);
777 * In case of REQBUFS(0) return immediately without calling
778 * driver's queue_setup() callback and allocating resources.
785 * Make sure the requested values and current defaults are sane.
787 WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME);
788 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed);
789 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
790 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
794 * Ask the driver how many buffers and planes per buffer it requires.
795 * Driver also sets the size and allocator context for each plane.
797 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
798 plane_sizes, q->alloc_devs);
802 /* Check that driver has set sane values */
803 if (WARN_ON(!num_planes))
806 for (i = 0; i < num_planes; i++)
807 if (WARN_ON(!plane_sizes[i]))
810 /* Finally, allocate buffers and video memory */
812 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
813 if (allocated_buffers == 0) {
814 dprintk(q, 1, "memory allocation failed\n");
819 * There is no point in continuing if we can't allocate the minimum
820 * number of buffers needed by this vb2_queue.
822 if (allocated_buffers < q->min_buffers_needed)
826 * Check if driver can handle the allocated number of buffers.
828 if (!ret && allocated_buffers < num_buffers) {
829 num_buffers = allocated_buffers;
831 * num_planes is set by the previous queue_setup(), but since it
832 * signals to queue_setup() whether it is called from create_bufs()
833 * vs reqbufs() we zero it here to signal that queue_setup() is
834 * called for the reqbufs() case.
838 ret = call_qop(q, queue_setup, q, &num_buffers,
839 &num_planes, plane_sizes, q->alloc_devs);
841 if (!ret && allocated_buffers < num_buffers)
845 * Either the driver has accepted a smaller number of buffers,
846 * or .queue_setup() returned an error
850 mutex_lock(&q->mmap_lock);
851 q->num_buffers = allocated_buffers;
855 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
856 * from q->num_buffers.
858 __vb2_queue_free(q, allocated_buffers);
859 mutex_unlock(&q->mmap_lock);
862 mutex_unlock(&q->mmap_lock);
865 * Return the number of successfully allocated buffers
868 *count = allocated_buffers;
869 q->waiting_for_buffers = !q->is_output;
873 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
875 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
877 unsigned int requested_planes,
878 const unsigned int requested_sizes[])
880 unsigned int num_planes = 0, num_buffers, allocated_buffers;
881 unsigned plane_sizes[VB2_MAX_PLANES] = { };
884 if (q->num_buffers == VB2_MAX_FRAME) {
885 dprintk(q, 1, "maximum number of buffers already allocated\n");
889 if (!q->num_buffers) {
890 if (q->waiting_in_dqbuf && *count) {
891 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
894 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
896 q->waiting_for_buffers = !q->is_output;
898 if (q->memory != memory) {
899 dprintk(q, 1, "memory model mismatch\n");
904 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
906 if (requested_planes && requested_sizes) {
907 num_planes = requested_planes;
908 memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
912 * Ask the driver, whether the requested number of buffers, planes per
913 * buffer and their sizes are acceptable
915 ret = call_qop(q, queue_setup, q, &num_buffers,
916 &num_planes, plane_sizes, q->alloc_devs);
920 /* Finally, allocate buffers and video memory */
921 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
922 num_planes, plane_sizes);
923 if (allocated_buffers == 0) {
924 dprintk(q, 1, "memory allocation failed\n");
929 * Check if driver can handle the so far allocated number of buffers.
931 if (allocated_buffers < num_buffers) {
932 num_buffers = allocated_buffers;
935 * q->num_buffers contains the total number of buffers, that the
936 * queue driver has set up
938 ret = call_qop(q, queue_setup, q, &num_buffers,
939 &num_planes, plane_sizes, q->alloc_devs);
941 if (!ret && allocated_buffers < num_buffers)
945 * Either the driver has accepted a smaller number of buffers,
946 * or .queue_setup() returned an error
950 mutex_lock(&q->mmap_lock);
951 q->num_buffers += allocated_buffers;
955 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
956 * from q->num_buffers.
958 __vb2_queue_free(q, allocated_buffers);
959 mutex_unlock(&q->mmap_lock);
962 mutex_unlock(&q->mmap_lock);
965 * Return the number of successfully allocated buffers
968 *count = allocated_buffers;
972 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
974 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
976 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
979 return call_ptr_memop(vaddr, vb, vb->planes[plane_no].mem_priv);
982 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
984 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
986 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
989 return call_ptr_memop(cookie, vb, vb->planes[plane_no].mem_priv);
991 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
993 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
995 struct vb2_queue *q = vb->vb2_queue;
998 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
1001 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
1002 state != VB2_BUF_STATE_ERROR &&
1003 state != VB2_BUF_STATE_QUEUED))
1004 state = VB2_BUF_STATE_ERROR;
1006 #ifdef CONFIG_VIDEO_ADV_DEBUG
1008 * Although this is not a callback, it still does have to balance
1009 * with the buf_queue op. So update this counter manually.
1013 dprintk(q, 4, "done processing on buffer %d, state: %s\n",
1014 vb->index, vb2_state_name(state));
1016 if (state != VB2_BUF_STATE_QUEUED)
1017 __vb2_buf_mem_finish(vb);
1019 spin_lock_irqsave(&q->done_lock, flags);
1020 if (state == VB2_BUF_STATE_QUEUED) {
1021 vb->state = VB2_BUF_STATE_QUEUED;
1023 /* Add the buffer to the done buffers list */
1024 list_add_tail(&vb->done_entry, &q->done_list);
1027 atomic_dec(&q->owned_by_drv_count);
1029 if (state != VB2_BUF_STATE_QUEUED && vb->req_obj.req) {
1030 media_request_object_unbind(&vb->req_obj);
1031 media_request_object_put(&vb->req_obj);
1034 spin_unlock_irqrestore(&q->done_lock, flags);
1036 trace_vb2_buf_done(q, vb);
1039 case VB2_BUF_STATE_QUEUED:
1042 /* Inform any processes that may be waiting for buffers */
1043 wake_up(&q->done_wq);
1047 EXPORT_SYMBOL_GPL(vb2_buffer_done);
1049 void vb2_discard_done(struct vb2_queue *q)
1051 struct vb2_buffer *vb;
1052 unsigned long flags;
1054 spin_lock_irqsave(&q->done_lock, flags);
1055 list_for_each_entry(vb, &q->done_list, done_entry)
1056 vb->state = VB2_BUF_STATE_ERROR;
1057 spin_unlock_irqrestore(&q->done_lock, flags);
1059 EXPORT_SYMBOL_GPL(vb2_discard_done);
1062 * __prepare_mmap() - prepare an MMAP buffer
1064 static int __prepare_mmap(struct vb2_buffer *vb)
1068 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1070 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
1074 * __prepare_userptr() - prepare a USERPTR buffer
1076 static int __prepare_userptr(struct vb2_buffer *vb)
1078 struct vb2_plane planes[VB2_MAX_PLANES];
1079 struct vb2_queue *q = vb->vb2_queue;
1083 bool reacquired = vb->planes[0].mem_priv == NULL;
1085 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1086 /* Copy relevant information provided by the userspace */
1087 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1092 for (plane = 0; plane < vb->num_planes; ++plane) {
1093 /* Skip the plane if already verified */
1094 if (vb->planes[plane].m.userptr &&
1095 vb->planes[plane].m.userptr == planes[plane].m.userptr
1096 && vb->planes[plane].length == planes[plane].length)
1099 dprintk(q, 3, "userspace address for plane %d changed, reacquiring memory\n",
1102 /* Check if the provided plane buffer is large enough */
1103 if (planes[plane].length < vb->planes[plane].min_length) {
1104 dprintk(q, 1, "provided buffer size %u is less than setup size %u for plane %d\n",
1105 planes[plane].length,
1106 vb->planes[plane].min_length,
1112 /* Release previously acquired memory if present */
1113 if (vb->planes[plane].mem_priv) {
1116 vb->copied_timestamp = 0;
1117 call_void_vb_qop(vb, buf_cleanup, vb);
1119 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
1122 vb->planes[plane].mem_priv = NULL;
1123 vb->planes[plane].bytesused = 0;
1124 vb->planes[plane].length = 0;
1125 vb->planes[plane].m.userptr = 0;
1126 vb->planes[plane].data_offset = 0;
1128 /* Acquire each plane's memory */
1129 mem_priv = call_ptr_memop(get_userptr,
1131 q->alloc_devs[plane] ? : q->dev,
1132 planes[plane].m.userptr,
1133 planes[plane].length);
1134 if (IS_ERR(mem_priv)) {
1135 dprintk(q, 1, "failed acquiring userspace memory for plane %d\n",
1137 ret = PTR_ERR(mem_priv);
1140 vb->planes[plane].mem_priv = mem_priv;
1144 * Now that everything is in order, copy relevant information
1145 * provided by userspace.
1147 for (plane = 0; plane < vb->num_planes; ++plane) {
1148 vb->planes[plane].bytesused = planes[plane].bytesused;
1149 vb->planes[plane].length = planes[plane].length;
1150 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1151 vb->planes[plane].data_offset = planes[plane].data_offset;
1156 * One or more planes changed, so we must call buf_init to do
1157 * the driver-specific initialization on the newly acquired
1158 * buffer, if provided.
1160 ret = call_vb_qop(vb, buf_init, vb);
1162 dprintk(q, 1, "buffer initialization failed\n");
1167 ret = call_vb_qop(vb, buf_prepare, vb);
1169 dprintk(q, 1, "buffer preparation failed\n");
1170 call_void_vb_qop(vb, buf_cleanup, vb);
1176 /* In case of errors, release planes that were already acquired */
1177 for (plane = 0; plane < vb->num_planes; ++plane) {
1178 if (vb->planes[plane].mem_priv)
1179 call_void_memop(vb, put_userptr,
1180 vb->planes[plane].mem_priv);
1181 vb->planes[plane].mem_priv = NULL;
1182 vb->planes[plane].m.userptr = 0;
1183 vb->planes[plane].length = 0;
1190 * __prepare_dmabuf() - prepare a DMABUF buffer
1192 static int __prepare_dmabuf(struct vb2_buffer *vb)
1194 struct vb2_plane planes[VB2_MAX_PLANES];
1195 struct vb2_queue *q = vb->vb2_queue;
1199 bool reacquired = vb->planes[0].mem_priv == NULL;
1201 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1202 /* Copy relevant information provided by the userspace */
1203 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1208 for (plane = 0; plane < vb->num_planes; ++plane) {
1209 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1211 if (IS_ERR_OR_NULL(dbuf)) {
1212 dprintk(q, 1, "invalid dmabuf fd for plane %d\n",
1218 /* use DMABUF size if length is not provided */
1219 if (planes[plane].length == 0)
1220 planes[plane].length = dbuf->size;
1222 if (planes[plane].length < vb->planes[plane].min_length) {
1223 dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1224 planes[plane].length, plane,
1225 vb->planes[plane].min_length);
1231 /* Skip the plane if already verified */
1232 if (dbuf == vb->planes[plane].dbuf &&
1233 vb->planes[plane].length == planes[plane].length) {
1238 dprintk(q, 3, "buffer for plane %d changed\n", plane);
1242 vb->copied_timestamp = 0;
1243 call_void_vb_qop(vb, buf_cleanup, vb);
1246 /* Release previously acquired memory if present */
1247 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1248 vb->planes[plane].bytesused = 0;
1249 vb->planes[plane].length = 0;
1250 vb->planes[plane].m.fd = 0;
1251 vb->planes[plane].data_offset = 0;
1253 /* Acquire each plane's memory */
1254 mem_priv = call_ptr_memop(attach_dmabuf,
1256 q->alloc_devs[plane] ? : q->dev,
1258 planes[plane].length);
1259 if (IS_ERR(mem_priv)) {
1260 dprintk(q, 1, "failed to attach dmabuf\n");
1261 ret = PTR_ERR(mem_priv);
1266 vb->planes[plane].dbuf = dbuf;
1267 vb->planes[plane].mem_priv = mem_priv;
1271 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1272 * here instead just before the DMA, while queueing the buffer(s) so
1273 * userspace knows sooner rather than later if the dma-buf map fails.
1275 for (plane = 0; plane < vb->num_planes; ++plane) {
1276 if (vb->planes[plane].dbuf_mapped)
1279 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1281 dprintk(q, 1, "failed to map dmabuf for plane %d\n",
1285 vb->planes[plane].dbuf_mapped = 1;
1289 * Now that everything is in order, copy relevant information
1290 * provided by userspace.
1292 for (plane = 0; plane < vb->num_planes; ++plane) {
1293 vb->planes[plane].bytesused = planes[plane].bytesused;
1294 vb->planes[plane].length = planes[plane].length;
1295 vb->planes[plane].m.fd = planes[plane].m.fd;
1296 vb->planes[plane].data_offset = planes[plane].data_offset;
1301 * Call driver-specific initialization on the newly acquired buffer,
1304 ret = call_vb_qop(vb, buf_init, vb);
1306 dprintk(q, 1, "buffer initialization failed\n");
1311 ret = call_vb_qop(vb, buf_prepare, vb);
1313 dprintk(q, 1, "buffer preparation failed\n");
1314 call_void_vb_qop(vb, buf_cleanup, vb);
1320 /* In case of errors, release planes that were already acquired */
1321 __vb2_buf_dmabuf_put(vb);
1327 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1329 static void __enqueue_in_driver(struct vb2_buffer *vb)
1331 struct vb2_queue *q = vb->vb2_queue;
1333 vb->state = VB2_BUF_STATE_ACTIVE;
1334 atomic_inc(&q->owned_by_drv_count);
1336 trace_vb2_buf_queue(q, vb);
1338 call_void_vb_qop(vb, buf_queue, vb);
1341 static int __buf_prepare(struct vb2_buffer *vb)
1343 struct vb2_queue *q = vb->vb2_queue;
1344 enum vb2_buffer_state orig_state = vb->state;
1348 dprintk(q, 1, "fatal error occurred on queue\n");
1354 WARN_ON(vb->synced);
1357 ret = call_vb_qop(vb, buf_out_validate, vb);
1359 dprintk(q, 1, "buffer validation failed\n");
1364 vb->state = VB2_BUF_STATE_PREPARING;
1366 switch (q->memory) {
1367 case VB2_MEMORY_MMAP:
1368 ret = __prepare_mmap(vb);
1370 case VB2_MEMORY_USERPTR:
1371 ret = __prepare_userptr(vb);
1373 case VB2_MEMORY_DMABUF:
1374 ret = __prepare_dmabuf(vb);
1377 WARN(1, "Invalid queue type\n");
1383 dprintk(q, 1, "buffer preparation failed: %d\n", ret);
1384 vb->state = orig_state;
1388 __vb2_buf_mem_prepare(vb);
1390 vb->state = orig_state;
1395 static int vb2_req_prepare(struct media_request_object *obj)
1397 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1400 if (WARN_ON(vb->state != VB2_BUF_STATE_IN_REQUEST))
1403 mutex_lock(vb->vb2_queue->lock);
1404 ret = __buf_prepare(vb);
1405 mutex_unlock(vb->vb2_queue->lock);
1409 static void __vb2_dqbuf(struct vb2_buffer *vb);
1411 static void vb2_req_unprepare(struct media_request_object *obj)
1413 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1415 mutex_lock(vb->vb2_queue->lock);
1417 vb->state = VB2_BUF_STATE_IN_REQUEST;
1418 mutex_unlock(vb->vb2_queue->lock);
1419 WARN_ON(!vb->req_obj.req);
1422 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1423 struct media_request *req);
1425 static void vb2_req_queue(struct media_request_object *obj)
1427 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1429 mutex_lock(vb->vb2_queue->lock);
1430 vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
1431 mutex_unlock(vb->vb2_queue->lock);
1434 static void vb2_req_unbind(struct media_request_object *obj)
1436 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1438 if (vb->state == VB2_BUF_STATE_IN_REQUEST)
1439 call_void_bufop(vb->vb2_queue, init_buffer, vb);
1442 static void vb2_req_release(struct media_request_object *obj)
1444 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1446 if (vb->state == VB2_BUF_STATE_IN_REQUEST) {
1447 vb->state = VB2_BUF_STATE_DEQUEUED;
1449 media_request_put(vb->request);
1454 static const struct media_request_object_ops vb2_core_req_ops = {
1455 .prepare = vb2_req_prepare,
1456 .unprepare = vb2_req_unprepare,
1457 .queue = vb2_req_queue,
1458 .unbind = vb2_req_unbind,
1459 .release = vb2_req_release,
1462 bool vb2_request_object_is_buffer(struct media_request_object *obj)
1464 return obj->ops == &vb2_core_req_ops;
1466 EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer);
1468 unsigned int vb2_request_buffer_cnt(struct media_request *req)
1470 struct media_request_object *obj;
1471 unsigned long flags;
1472 unsigned int buffer_cnt = 0;
1474 spin_lock_irqsave(&req->lock, flags);
1475 list_for_each_entry(obj, &req->objects, list)
1476 if (vb2_request_object_is_buffer(obj))
1478 spin_unlock_irqrestore(&req->lock, flags);
1482 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
1484 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1486 struct vb2_buffer *vb;
1489 vb = q->bufs[index];
1490 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1491 dprintk(q, 1, "invalid buffer state %s\n",
1492 vb2_state_name(vb->state));
1496 dprintk(q, 1, "buffer already prepared\n");
1500 ret = __buf_prepare(vb);
1504 /* Fill buffer information for the userspace */
1505 call_void_bufop(q, fill_user_buffer, vb, pb);
1507 dprintk(q, 2, "prepare of buffer %d succeeded\n", vb->index);
1511 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1514 * vb2_start_streaming() - Attempt to start streaming.
1515 * @q: videobuf2 queue
1517 * Attempt to start streaming. When this function is called there must be
1518 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1519 * number of buffers required for the DMA engine to function). If the
1520 * @start_streaming op fails it is supposed to return all the driver-owned
1521 * buffers back to vb2 in state QUEUED. Check if that happened and if
1522 * not warn and reclaim them forcefully.
1524 static int vb2_start_streaming(struct vb2_queue *q)
1526 struct vb2_buffer *vb;
1530 * If any buffers were queued before streamon,
1531 * we can now pass them to driver for processing.
1533 list_for_each_entry(vb, &q->queued_list, queued_entry)
1534 __enqueue_in_driver(vb);
1536 /* Tell the driver to start streaming */
1537 q->start_streaming_called = 1;
1538 ret = call_qop(q, start_streaming, q,
1539 atomic_read(&q->owned_by_drv_count));
1543 q->start_streaming_called = 0;
1545 dprintk(q, 1, "driver refused to start streaming\n");
1547 * If you see this warning, then the driver isn't cleaning up properly
1548 * after a failed start_streaming(). See the start_streaming()
1549 * documentation in videobuf2-core.h for more information how buffers
1550 * should be returned to vb2 in start_streaming().
1552 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1556 * Forcefully reclaim buffers if the driver did not
1557 * correctly return them to vb2.
1559 for (i = 0; i < q->num_buffers; ++i) {
1561 if (vb->state == VB2_BUF_STATE_ACTIVE)
1562 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1564 /* Must be zero now */
1565 WARN_ON(atomic_read(&q->owned_by_drv_count));
1568 * If done_list is not empty, then start_streaming() didn't call
1569 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1572 WARN_ON(!list_empty(&q->done_list));
1576 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1577 struct media_request *req)
1579 struct vb2_buffer *vb;
1580 enum vb2_buffer_state orig_state;
1584 dprintk(q, 1, "fatal error occurred on queue\n");
1588 vb = q->bufs[index];
1590 if (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1591 q->requires_requests) {
1592 dprintk(q, 1, "qbuf requires a request\n");
1596 if ((req && q->uses_qbuf) ||
1597 (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1598 q->uses_requests)) {
1599 dprintk(q, 1, "queue in wrong mode (qbuf vs requests)\n");
1606 q->uses_requests = 1;
1607 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1608 dprintk(q, 1, "buffer %d not in dequeued state\n",
1613 if (q->is_output && !vb->prepared) {
1614 ret = call_vb_qop(vb, buf_out_validate, vb);
1616 dprintk(q, 1, "buffer validation failed\n");
1621 media_request_object_init(&vb->req_obj);
1623 /* Make sure the request is in a safe state for updating. */
1624 ret = media_request_lock_for_update(req);
1627 ret = media_request_object_bind(req, &vb2_core_req_ops,
1628 q, true, &vb->req_obj);
1629 media_request_unlock_for_update(req);
1633 vb->state = VB2_BUF_STATE_IN_REQUEST;
1636 * Increment the refcount and store the request.
1637 * The request refcount is decremented again when the
1638 * buffer is dequeued. This is to prevent vb2_buffer_done()
1639 * from freeing the request from interrupt context, which can
1640 * happen if the application closed the request fd after
1641 * queueing the request.
1643 media_request_get(req);
1646 /* Fill buffer information for the userspace */
1648 call_void_bufop(q, copy_timestamp, vb, pb);
1649 call_void_bufop(q, fill_user_buffer, vb, pb);
1652 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
1656 if (vb->state != VB2_BUF_STATE_IN_REQUEST)
1659 switch (vb->state) {
1660 case VB2_BUF_STATE_DEQUEUED:
1661 case VB2_BUF_STATE_IN_REQUEST:
1662 if (!vb->prepared) {
1663 ret = __buf_prepare(vb);
1668 case VB2_BUF_STATE_PREPARING:
1669 dprintk(q, 1, "buffer still being prepared\n");
1672 dprintk(q, 1, "invalid buffer state %s\n",
1673 vb2_state_name(vb->state));
1678 * Add to the queued buffers list, a buffer will stay on it until
1679 * dequeued in dqbuf.
1681 orig_state = vb->state;
1682 list_add_tail(&vb->queued_entry, &q->queued_list);
1684 q->waiting_for_buffers = false;
1685 vb->state = VB2_BUF_STATE_QUEUED;
1688 call_void_bufop(q, copy_timestamp, vb, pb);
1690 trace_vb2_qbuf(q, vb);
1693 * If already streaming, give the buffer to driver for processing.
1694 * If not, the buffer will be given to driver on next streamon.
1696 if (q->start_streaming_called)
1697 __enqueue_in_driver(vb);
1699 /* Fill buffer information for the userspace */
1701 call_void_bufop(q, fill_user_buffer, vb, pb);
1704 * If streamon has been called, and we haven't yet called
1705 * start_streaming() since not enough buffers were queued, and
1706 * we now have reached the minimum number of queued buffers,
1707 * then we can finally call start_streaming().
1709 if (q->streaming && !q->start_streaming_called &&
1710 q->queued_count >= q->min_buffers_needed) {
1711 ret = vb2_start_streaming(q);
1714 * Since vb2_core_qbuf will return with an error,
1715 * we should return it to state DEQUEUED since
1716 * the error indicates that the buffer wasn't queued.
1718 list_del(&vb->queued_entry);
1720 vb->state = orig_state;
1725 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
1728 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1731 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1734 * Will sleep if required for nonblocking == false.
1736 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1739 * All operations on vb_done_list are performed under done_lock
1740 * spinlock protection. However, buffers may be removed from
1741 * it and returned to userspace only while holding both driver's
1742 * lock and the done_lock spinlock. Thus we can be sure that as
1743 * long as we hold the driver's lock, the list will remain not
1744 * empty if list_empty() check succeeds.
1750 if (q->waiting_in_dqbuf) {
1751 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
1755 if (!q->streaming) {
1756 dprintk(q, 1, "streaming off, will not wait for buffers\n");
1761 dprintk(q, 1, "Queue in error state, will not wait for buffers\n");
1765 if (q->last_buffer_dequeued) {
1766 dprintk(q, 3, "last buffer dequeued already, will not wait for buffers\n");
1770 if (!list_empty(&q->done_list)) {
1772 * Found a buffer that we were waiting for.
1778 dprintk(q, 3, "nonblocking and no buffers to dequeue, will not wait\n");
1782 q->waiting_in_dqbuf = 1;
1784 * We are streaming and blocking, wait for another buffer to
1785 * become ready or for streamoff. Driver's lock is released to
1786 * allow streamoff or qbuf to be called while waiting.
1788 call_void_qop(q, wait_prepare, q);
1791 * All locks have been released, it is safe to sleep now.
1793 dprintk(q, 3, "will sleep waiting for buffers\n");
1794 ret = wait_event_interruptible(q->done_wq,
1795 !list_empty(&q->done_list) || !q->streaming ||
1799 * We need to reevaluate both conditions again after reacquiring
1800 * the locks or return an error if one occurred.
1802 call_void_qop(q, wait_finish, q);
1803 q->waiting_in_dqbuf = 0;
1805 dprintk(q, 1, "sleep was interrupted\n");
1813 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1815 * Will sleep if required for nonblocking == false.
1817 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1818 void *pb, int nonblocking)
1820 unsigned long flags;
1824 * Wait for at least one buffer to become available on the done_list.
1826 ret = __vb2_wait_for_done_vb(q, nonblocking);
1831 * Driver's lock has been held since we last verified that done_list
1832 * is not empty, so no need for another list_empty(done_list) check.
1834 spin_lock_irqsave(&q->done_lock, flags);
1835 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1837 * Only remove the buffer from done_list if all planes can be
1838 * handled. Some cases such as V4L2 file I/O and DVB have pb
1839 * == NULL; skip the check then as there's nothing to verify.
1842 ret = call_bufop(q, verify_planes_array, *vb, pb);
1844 list_del(&(*vb)->done_entry);
1845 spin_unlock_irqrestore(&q->done_lock, flags);
1850 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1852 if (!q->streaming) {
1853 dprintk(q, 1, "streaming off, will not wait for buffers\n");
1857 if (q->start_streaming_called)
1858 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1861 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1864 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1866 static void __vb2_dqbuf(struct vb2_buffer *vb)
1868 struct vb2_queue *q = vb->vb2_queue;
1870 /* nothing to do if the buffer is already dequeued */
1871 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1874 vb->state = VB2_BUF_STATE_DEQUEUED;
1876 call_void_bufop(q, init_buffer, vb);
1879 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
1882 struct vb2_buffer *vb = NULL;
1885 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1889 switch (vb->state) {
1890 case VB2_BUF_STATE_DONE:
1891 dprintk(q, 3, "returning done buffer\n");
1893 case VB2_BUF_STATE_ERROR:
1894 dprintk(q, 3, "returning done buffer with errors\n");
1897 dprintk(q, 1, "invalid buffer state %s\n",
1898 vb2_state_name(vb->state));
1902 call_void_vb_qop(vb, buf_finish, vb);
1906 *pindex = vb->index;
1908 /* Fill buffer information for the userspace */
1910 call_void_bufop(q, fill_user_buffer, vb, pb);
1912 /* Remove from videobuf queue */
1913 list_del(&vb->queued_entry);
1916 trace_vb2_dqbuf(q, vb);
1918 /* go back to dequeued state */
1921 if (WARN_ON(vb->req_obj.req)) {
1922 media_request_object_unbind(&vb->req_obj);
1923 media_request_object_put(&vb->req_obj);
1926 media_request_put(vb->request);
1929 dprintk(q, 2, "dqbuf of buffer %d, state: %s\n",
1930 vb->index, vb2_state_name(vb->state));
1935 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1938 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1940 * Removes all queued buffers from driver's queue and all buffers queued by
1941 * userspace from videobuf's queue. Returns to state after reqbufs.
1943 static void __vb2_queue_cancel(struct vb2_queue *q)
1948 * Tell driver to stop all transactions and release all queued
1951 if (q->start_streaming_called)
1952 call_void_qop(q, stop_streaming, q);
1955 * If you see this warning, then the driver isn't cleaning up properly
1956 * in stop_streaming(). See the stop_streaming() documentation in
1957 * videobuf2-core.h for more information how buffers should be returned
1958 * to vb2 in stop_streaming().
1960 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1961 for (i = 0; i < q->num_buffers; ++i)
1962 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
1963 pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
1965 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1967 /* Must be zero now */
1968 WARN_ON(atomic_read(&q->owned_by_drv_count));
1972 q->start_streaming_called = 0;
1973 q->queued_count = 0;
1975 q->uses_requests = 0;
1979 * Remove all buffers from videobuf's list...
1981 INIT_LIST_HEAD(&q->queued_list);
1983 * ...and done list; userspace will not receive any buffers it
1984 * has not already dequeued before initiating cancel.
1986 INIT_LIST_HEAD(&q->done_list);
1987 atomic_set(&q->owned_by_drv_count, 0);
1988 wake_up_all(&q->done_wq);
1991 * Reinitialize all buffers for next use.
1992 * Make sure to call buf_finish for any queued buffers. Normally
1993 * that's done in dqbuf, but that's not going to happen when we
1994 * cancel the whole queue. Note: this code belongs here, not in
1995 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1996 * call to __fill_user_buffer() after buf_finish(). That order can't
1997 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1999 for (i = 0; i < q->num_buffers; ++i) {
2000 struct vb2_buffer *vb = q->bufs[i];
2001 struct media_request *req = vb->req_obj.req;
2004 * If a request is associated with this buffer, then
2005 * call buf_request_cancel() to give the driver to complete()
2006 * related request objects. Otherwise those objects would
2010 enum media_request_state state;
2011 unsigned long flags;
2013 spin_lock_irqsave(&req->lock, flags);
2015 spin_unlock_irqrestore(&req->lock, flags);
2017 if (state == MEDIA_REQUEST_STATE_QUEUED)
2018 call_void_vb_qop(vb, buf_request_complete, vb);
2021 __vb2_buf_mem_finish(vb);
2024 call_void_vb_qop(vb, buf_finish, vb);
2029 if (vb->req_obj.req) {
2030 media_request_object_unbind(&vb->req_obj);
2031 media_request_object_put(&vb->req_obj);
2034 media_request_put(vb->request);
2036 vb->copied_timestamp = 0;
2040 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
2044 if (type != q->type) {
2045 dprintk(q, 1, "invalid stream type\n");
2050 dprintk(q, 3, "already streaming\n");
2054 if (!q->num_buffers) {
2055 dprintk(q, 1, "no buffers have been allocated\n");
2059 if (q->num_buffers < q->min_buffers_needed) {
2060 dprintk(q, 1, "need at least %u allocated buffers\n",
2061 q->min_buffers_needed);
2066 * Tell driver to start streaming provided sufficient buffers
2069 if (q->queued_count >= q->min_buffers_needed) {
2070 ret = v4l_vb2q_enable_media_source(q);
2073 ret = vb2_start_streaming(q);
2080 dprintk(q, 3, "successful\n");
2083 EXPORT_SYMBOL_GPL(vb2_core_streamon);
2085 void vb2_queue_error(struct vb2_queue *q)
2089 wake_up_all(&q->done_wq);
2091 EXPORT_SYMBOL_GPL(vb2_queue_error);
2093 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2095 if (type != q->type) {
2096 dprintk(q, 1, "invalid stream type\n");
2101 * Cancel will pause streaming and remove all buffers from the driver
2102 * and videobuf, effectively returning control over them to userspace.
2104 * Note that we do this even if q->streaming == 0: if you prepare or
2105 * queue buffers, and then call streamoff without ever having called
2106 * streamon, you would still expect those buffers to be returned to
2107 * their normal dequeued state.
2109 __vb2_queue_cancel(q);
2110 q->waiting_for_buffers = !q->is_output;
2111 q->last_buffer_dequeued = false;
2113 dprintk(q, 3, "successful\n");
2116 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
2119 * __find_plane_by_offset() - find plane associated with the given offset off
2121 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2122 unsigned int *_buffer, unsigned int *_plane)
2124 struct vb2_buffer *vb;
2125 unsigned int buffer, plane;
2128 * Go over all buffers and their planes, comparing the given offset
2129 * with an offset assigned to each plane. If a match is found,
2130 * return its buffer and plane numbers.
2132 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2133 vb = q->bufs[buffer];
2135 for (plane = 0; plane < vb->num_planes; ++plane) {
2136 if (vb->planes[plane].m.offset == off) {
2147 int vb2_core_expbuf_dmabuf(struct vb2_queue *q, unsigned int type,
2148 unsigned int index, unsigned int plane,
2149 unsigned int flags, struct dma_buf **dmabuf)
2151 struct vb2_buffer *vb = NULL;
2152 struct vb2_plane *vb_plane;
2153 struct dma_buf *dbuf;
2155 if (q->memory != VB2_MEMORY_MMAP) {
2156 dprintk(q, 1, "queue is not currently set up for mmap\n");
2160 if (!q->mem_ops->get_dmabuf) {
2161 dprintk(q, 1, "queue does not support DMA buffer exporting\n");
2165 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
2166 dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n");
2170 if (type != q->type) {
2171 dprintk(q, 1, "invalid buffer type\n");
2175 if (index >= q->num_buffers) {
2176 dprintk(q, 1, "buffer index out of range\n");
2180 vb = q->bufs[index];
2182 if (plane >= vb->num_planes) {
2183 dprintk(q, 1, "buffer plane out of range\n");
2187 if (vb2_fileio_is_active(q)) {
2188 dprintk(q, 1, "expbuf: file io in progress\n");
2192 vb_plane = &vb->planes[plane];
2194 dbuf = call_ptr_memop(get_dmabuf,
2198 if (IS_ERR_OR_NULL(dbuf)) {
2199 dprintk(q, 1, "failed to export buffer %d, plane %d\n",
2207 EXPORT_SYMBOL_GPL(vb2_core_expbuf_dmabuf);
2209 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2210 unsigned int index, unsigned int plane, unsigned int flags)
2212 struct dma_buf *dbuf;
2215 ret = vb2_core_expbuf_dmabuf(q, type, index, plane, flags, &dbuf);
2219 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
2221 dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n",
2227 dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n",
2233 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
2235 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2237 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
2238 struct vb2_buffer *vb;
2239 unsigned int buffer = 0, plane = 0;
2241 unsigned long length;
2243 if (q->memory != VB2_MEMORY_MMAP) {
2244 dprintk(q, 1, "queue is not currently set up for mmap\n");
2249 * Check memory area access mode.
2251 if (!(vma->vm_flags & VM_SHARED)) {
2252 dprintk(q, 1, "invalid vma flags, VM_SHARED needed\n");
2256 if (!(vma->vm_flags & VM_WRITE)) {
2257 dprintk(q, 1, "invalid vma flags, VM_WRITE needed\n");
2261 if (!(vma->vm_flags & VM_READ)) {
2262 dprintk(q, 1, "invalid vma flags, VM_READ needed\n");
2267 mutex_lock(&q->mmap_lock);
2269 if (vb2_fileio_is_active(q)) {
2270 dprintk(q, 1, "mmap: file io in progress\n");
2276 * Find the plane corresponding to the offset passed by userspace.
2278 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2282 vb = q->bufs[buffer];
2285 * MMAP requires page_aligned buffers.
2286 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2287 * so, we need to do the same here.
2289 length = PAGE_ALIGN(vb->planes[plane].length);
2290 if (length < (vma->vm_end - vma->vm_start)) {
2292 "MMAP invalid, as it would overflow buffer length\n");
2298 * vm_pgoff is treated in V4L2 API as a 'cookie' to select a buffer,
2299 * not as a in-buffer offset. We always want to mmap a whole buffer
2300 * from its beginning.
2304 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2307 mutex_unlock(&q->mmap_lock);
2311 dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2314 EXPORT_SYMBOL_GPL(vb2_mmap);
2317 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2320 unsigned long pgoff,
2321 unsigned long flags)
2323 unsigned long off = pgoff << PAGE_SHIFT;
2324 struct vb2_buffer *vb;
2325 unsigned int buffer, plane;
2329 if (q->memory != VB2_MEMORY_MMAP) {
2330 dprintk(q, 1, "queue is not currently set up for mmap\n");
2335 * Find the plane corresponding to the offset passed by userspace.
2337 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2341 vb = q->bufs[buffer];
2343 vaddr = vb2_plane_vaddr(vb, plane);
2344 return vaddr ? (unsigned long)vaddr : -EINVAL;
2346 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2349 int vb2_core_queue_init(struct vb2_queue *q)
2356 WARN_ON(!q->mem_ops) ||
2357 WARN_ON(!q->type) ||
2358 WARN_ON(!q->io_modes) ||
2359 WARN_ON(!q->ops->queue_setup) ||
2360 WARN_ON(!q->ops->buf_queue))
2363 if (WARN_ON(q->requires_requests && !q->supports_requests))
2366 INIT_LIST_HEAD(&q->queued_list);
2367 INIT_LIST_HEAD(&q->done_list);
2368 spin_lock_init(&q->done_lock);
2369 mutex_init(&q->mmap_lock);
2370 init_waitqueue_head(&q->done_wq);
2372 q->memory = VB2_MEMORY_UNKNOWN;
2374 if (q->buf_struct_size == 0)
2375 q->buf_struct_size = sizeof(struct vb2_buffer);
2377 if (q->bidirectional)
2378 q->dma_dir = DMA_BIDIRECTIONAL;
2380 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2382 if (q->name[0] == '\0')
2383 snprintf(q->name, sizeof(q->name), "%s-%p",
2384 q->is_output ? "out" : "cap", q);
2388 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2390 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2391 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2392 void vb2_core_queue_release(struct vb2_queue *q)
2394 __vb2_cleanup_fileio(q);
2395 __vb2_queue_cancel(q);
2396 mutex_lock(&q->mmap_lock);
2397 __vb2_queue_free(q, q->num_buffers);
2398 mutex_unlock(&q->mmap_lock);
2400 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2402 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
2405 __poll_t req_events = poll_requested_events(wait);
2406 struct vb2_buffer *vb = NULL;
2407 unsigned long flags;
2410 * poll_wait() MUST be called on the first invocation on all the
2411 * potential queues of interest, even if we are not interested in their
2412 * events during this first call. Failure to do so will result in
2413 * queue's events to be ignored because the poll_table won't be capable
2414 * of adding new wait queues thereafter.
2416 poll_wait(file, &q->done_wq, wait);
2418 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM)))
2420 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
2424 * Start file I/O emulator only if streaming API has not been used yet.
2426 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
2427 if (!q->is_output && (q->io_modes & VB2_READ) &&
2428 (req_events & (EPOLLIN | EPOLLRDNORM))) {
2429 if (__vb2_init_fileio(q, 1))
2432 if (q->is_output && (q->io_modes & VB2_WRITE) &&
2433 (req_events & (EPOLLOUT | EPOLLWRNORM))) {
2434 if (__vb2_init_fileio(q, 0))
2437 * Write to OUTPUT queue can be done immediately.
2439 return EPOLLOUT | EPOLLWRNORM;
2444 * There is nothing to wait for if the queue isn't streaming, or if the
2445 * error flag is set.
2447 if (!vb2_is_streaming(q) || q->error)
2451 * If this quirk is set and QBUF hasn't been called yet then
2452 * return EPOLLERR as well. This only affects capture queues, output
2453 * queues will always initialize waiting_for_buffers to false.
2454 * This quirk is set by V4L2 for backwards compatibility reasons.
2456 if (q->quirk_poll_must_check_waiting_for_buffers &&
2457 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM)))
2461 * For output streams you can call write() as long as there are fewer
2462 * buffers queued than there are buffers available.
2464 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
2465 return EPOLLOUT | EPOLLWRNORM;
2467 if (list_empty(&q->done_list)) {
2469 * If the last buffer was dequeued from a capture queue,
2470 * return immediately. DQBUF will return -EPIPE.
2472 if (q->last_buffer_dequeued)
2473 return EPOLLIN | EPOLLRDNORM;
2477 * Take first buffer available for dequeuing.
2479 spin_lock_irqsave(&q->done_lock, flags);
2480 if (!list_empty(&q->done_list))
2481 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2483 spin_unlock_irqrestore(&q->done_lock, flags);
2485 if (vb && (vb->state == VB2_BUF_STATE_DONE
2486 || vb->state == VB2_BUF_STATE_ERROR)) {
2487 return (q->is_output) ?
2488 EPOLLOUT | EPOLLWRNORM :
2489 EPOLLIN | EPOLLRDNORM;
2493 EXPORT_SYMBOL_GPL(vb2_core_poll);
2496 * struct vb2_fileio_buf - buffer context used by file io emulator
2498 * vb2 provides a compatibility layer and emulator of file io (read and
2499 * write) calls on top of streaming API. This structure is used for
2500 * tracking context related to the buffers.
2502 struct vb2_fileio_buf {
2506 unsigned int queued:1;
2510 * struct vb2_fileio_data - queue context used by file io emulator
2512 * @cur_index: the index of the buffer currently being read from or
2513 * written to. If equal to q->num_buffers then a new buffer
2515 * @initial_index: in the read() case all buffers are queued up immediately
2516 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2517 * buffers. However, in the write() case no buffers are initially
2518 * queued, instead whenever a buffer is full it is queued up by
2519 * __vb2_perform_fileio(). Only once all available buffers have
2520 * been queued up will __vb2_perform_fileio() start to dequeue
2521 * buffers. This means that initially __vb2_perform_fileio()
2522 * needs to know what buffer index to use when it is queuing up
2523 * the buffers for the first time. That initial index is stored
2524 * in this field. Once it is equal to q->num_buffers all
2525 * available buffers have been queued and __vb2_perform_fileio()
2526 * should start the normal dequeue/queue cycle.
2528 * vb2 provides a compatibility layer and emulator of file io (read and
2529 * write) calls on top of streaming API. For proper operation it required
2530 * this structure to save the driver state between each call of the read
2531 * or write function.
2533 struct vb2_fileio_data {
2536 unsigned int memory;
2537 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
2538 unsigned int cur_index;
2539 unsigned int initial_index;
2540 unsigned int q_count;
2541 unsigned int dq_count;
2542 unsigned read_once:1;
2543 unsigned write_immediately:1;
2547 * __vb2_init_fileio() - initialize file io emulator
2548 * @q: videobuf2 queue
2549 * @read: mode selector (1 means read, 0 means write)
2551 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2553 struct vb2_fileio_data *fileio;
2555 unsigned int count = 0;
2560 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
2561 (!read && !(q->io_modes & VB2_WRITE))))
2565 * Check if device supports mapping buffers to kernel virtual space.
2567 if (!q->mem_ops->vaddr)
2571 * Check if streaming api has not been already activated.
2573 if (q->streaming || q->num_buffers > 0)
2577 * Start with count 1, driver can increase it in queue_setup()
2581 dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2582 (read) ? "read" : "write", count, q->fileio_read_once,
2583 q->fileio_write_immediately);
2585 fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
2589 fileio->read_once = q->fileio_read_once;
2590 fileio->write_immediately = q->fileio_write_immediately;
2593 * Request buffers and use MMAP type to force driver
2594 * to allocate buffers by itself.
2596 fileio->count = count;
2597 fileio->memory = VB2_MEMORY_MMAP;
2598 fileio->type = q->type;
2600 ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2605 * Check if plane_count is correct
2606 * (multiplane buffers are not supported).
2608 if (q->bufs[0]->num_planes != 1) {
2614 * Get kernel address of each buffer.
2616 for (i = 0; i < q->num_buffers; i++) {
2617 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2618 if (fileio->bufs[i].vaddr == NULL) {
2622 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2626 * Read mode requires pre queuing of all buffers.
2630 * Queue all buffers.
2632 for (i = 0; i < q->num_buffers; i++) {
2633 ret = vb2_core_qbuf(q, i, NULL, NULL);
2636 fileio->bufs[i].queued = 1;
2639 * All buffers have been queued, so mark that by setting
2640 * initial_index to q->num_buffers
2642 fileio->initial_index = q->num_buffers;
2643 fileio->cur_index = q->num_buffers;
2649 ret = vb2_core_streamon(q, q->type);
2657 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2666 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2667 * @q: videobuf2 queue
2669 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2671 struct vb2_fileio_data *fileio = q->fileio;
2674 vb2_core_streamoff(q, q->type);
2677 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2679 dprintk(q, 3, "file io emulator closed\n");
2685 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2686 * @q: videobuf2 queue
2687 * @data: pointed to target userspace buffer
2688 * @count: number of bytes to read or write
2689 * @ppos: file handle position tracking pointer
2690 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2691 * @read: access mode selector (1 means read, 0 means write)
2693 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2694 loff_t *ppos, int nonblock, int read)
2696 struct vb2_fileio_data *fileio;
2697 struct vb2_fileio_buf *buf;
2698 bool is_multiplanar = q->is_multiplanar;
2700 * When using write() to write data to an output video node the vb2 core
2701 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2702 * else is able to provide this information with the write() operation.
2704 bool copy_timestamp = !read && q->copy_timestamp;
2708 dprintk(q, 3, "mode %s, offset %ld, count %zd, %sblocking\n",
2709 read ? "read" : "write", (long)*ppos, count,
2710 nonblock ? "non" : "");
2715 if (q->waiting_in_dqbuf) {
2716 dprintk(q, 3, "another dup()ped fd is %s\n",
2717 read ? "reading" : "writing");
2722 * Initialize emulator on first call.
2724 if (!vb2_fileio_is_active(q)) {
2725 ret = __vb2_init_fileio(q, read);
2726 dprintk(q, 3, "vb2_init_fileio result: %d\n", ret);
2733 * Check if we need to dequeue the buffer.
2735 index = fileio->cur_index;
2736 if (index >= q->num_buffers) {
2737 struct vb2_buffer *b;
2740 * Call vb2_dqbuf to get buffer back.
2742 ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2743 dprintk(q, 5, "vb2_dqbuf result: %d\n", ret);
2746 fileio->dq_count += 1;
2748 fileio->cur_index = index;
2749 buf = &fileio->bufs[index];
2753 * Get number of bytes filled by the driver
2757 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2758 : vb2_plane_size(q->bufs[index], 0);
2759 /* Compensate for data_offset on read in the multiplanar case. */
2760 if (is_multiplanar && read &&
2761 b->planes[0].data_offset < buf->size) {
2762 buf->pos = b->planes[0].data_offset;
2763 buf->size -= buf->pos;
2766 buf = &fileio->bufs[index];
2770 * Limit count on last few bytes of the buffer.
2772 if (buf->pos + count > buf->size) {
2773 count = buf->size - buf->pos;
2774 dprintk(q, 5, "reducing read count: %zd\n", count);
2778 * Transfer data to userspace.
2780 dprintk(q, 3, "copying %zd bytes - buffer %d, offset %u\n",
2781 count, index, buf->pos);
2783 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2785 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2787 dprintk(q, 3, "error copying data\n");
2798 * Queue next buffer if required.
2800 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
2801 struct vb2_buffer *b = q->bufs[index];
2804 * Check if this is the last buffer to read.
2806 if (read && fileio->read_once && fileio->dq_count == 1) {
2807 dprintk(q, 3, "read limit reached\n");
2808 return __vb2_cleanup_fileio(q);
2812 * Call vb2_qbuf and give buffer to the driver.
2814 b->planes[0].bytesused = buf->pos;
2817 b->timestamp = ktime_get_ns();
2818 ret = vb2_core_qbuf(q, index, NULL, NULL);
2819 dprintk(q, 5, "vb2_dbuf result: %d\n", ret);
2824 * Buffer has been queued, update the status
2828 buf->size = vb2_plane_size(q->bufs[index], 0);
2829 fileio->q_count += 1;
2831 * If we are queuing up buffers for the first time, then
2832 * increase initial_index by one.
2834 if (fileio->initial_index < q->num_buffers)
2835 fileio->initial_index++;
2837 * The next buffer to use is either a buffer that's going to be
2838 * queued for the first time (initial_index < q->num_buffers)
2839 * or it is equal to q->num_buffers, meaning that the next
2840 * time we need to dequeue a buffer since we've now queued up
2841 * all the 'first time' buffers.
2843 fileio->cur_index = fileio->initial_index;
2847 * Return proper number of bytes processed.
2854 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2855 loff_t *ppos, int nonblocking)
2857 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2859 EXPORT_SYMBOL_GPL(vb2_read);
2861 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
2862 loff_t *ppos, int nonblocking)
2864 return __vb2_perform_fileio(q, (char __user *) data, count,
2865 ppos, nonblocking, 0);
2867 EXPORT_SYMBOL_GPL(vb2_write);
2869 struct vb2_threadio_data {
2870 struct task_struct *thread;
2876 static int vb2_thread(void *data)
2878 struct vb2_queue *q = data;
2879 struct vb2_threadio_data *threadio = q->threadio;
2880 bool copy_timestamp = false;
2881 unsigned prequeue = 0;
2886 prequeue = q->num_buffers;
2887 copy_timestamp = q->copy_timestamp;
2893 struct vb2_buffer *vb;
2896 * Call vb2_dqbuf to get buffer back.
2899 vb = q->bufs[index++];
2902 call_void_qop(q, wait_finish, q);
2903 if (!threadio->stop)
2904 ret = vb2_core_dqbuf(q, &index, NULL, 0);
2905 call_void_qop(q, wait_prepare, q);
2906 dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret);
2908 vb = q->bufs[index];
2910 if (ret || threadio->stop)
2914 if (vb->state != VB2_BUF_STATE_ERROR)
2915 if (threadio->fnc(vb, threadio->priv))
2917 call_void_qop(q, wait_finish, q);
2919 vb->timestamp = ktime_get_ns();
2920 if (!threadio->stop)
2921 ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
2922 call_void_qop(q, wait_prepare, q);
2923 if (ret || threadio->stop)
2927 /* Hmm, linux becomes *very* unhappy without this ... */
2928 while (!kthread_should_stop()) {
2929 set_current_state(TASK_INTERRUPTIBLE);
2936 * This function should not be used for anything else but the videobuf2-dvb
2937 * support. If you think you have another good use-case for this, then please
2938 * contact the linux-media mailinglist first.
2940 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
2941 const char *thread_name)
2943 struct vb2_threadio_data *threadio;
2950 if (WARN_ON(q->fileio))
2953 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
2954 if (threadio == NULL)
2956 threadio->fnc = fnc;
2957 threadio->priv = priv;
2959 ret = __vb2_init_fileio(q, !q->is_output);
2960 dprintk(q, 3, "file io: vb2_init_fileio result: %d\n", ret);
2963 q->threadio = threadio;
2964 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
2965 if (IS_ERR(threadio->thread)) {
2966 ret = PTR_ERR(threadio->thread);
2967 threadio->thread = NULL;
2973 __vb2_cleanup_fileio(q);
2978 EXPORT_SYMBOL_GPL(vb2_thread_start);
2980 int vb2_thread_stop(struct vb2_queue *q)
2982 struct vb2_threadio_data *threadio = q->threadio;
2985 if (threadio == NULL)
2987 threadio->stop = true;
2988 /* Wake up all pending sleeps in the thread */
2990 err = kthread_stop(threadio->thread);
2991 __vb2_cleanup_fileio(q);
2992 threadio->thread = NULL;
2997 EXPORT_SYMBOL_GPL(vb2_thread_stop);
2999 MODULE_DESCRIPTION("Media buffer core framework");
3000 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
3001 MODULE_LICENSE("GPL");