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(vb, op, args...) \
73 struct vb2_queue *_q = (vb)->vb2_queue; \
77 ptr = _q->mem_ops->op ? _q->mem_ops->op(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(vb, op, args...) \
148 ((vb)->vb2_queue->mem_ops->op ? \
149 (vb)->vb2_queue->mem_ops->op(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(vb, alloc,
234 q->alloc_devs[plane] ? : q->dev,
235 q->dma_attrs, size, q->dma_dir, q->gfp_flags);
236 if (IS_ERR_OR_NULL(mem_priv)) {
238 ret = PTR_ERR(mem_priv);
242 /* Associate allocator private data with this plane */
243 vb->planes[plane].mem_priv = mem_priv;
248 /* Free already allocated memory if one of the allocations failed */
249 for (; plane > 0; --plane) {
250 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
251 vb->planes[plane - 1].mem_priv = NULL;
258 * __vb2_buf_mem_free() - free memory of the given buffer
260 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
264 for (plane = 0; plane < vb->num_planes; ++plane) {
265 call_void_memop(vb, put, vb->planes[plane].mem_priv);
266 vb->planes[plane].mem_priv = NULL;
267 dprintk(vb->vb2_queue, 3, "freed plane %d of buffer %d\n",
273 * __vb2_buf_userptr_put() - release userspace memory associated with
276 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
280 for (plane = 0; plane < vb->num_planes; ++plane) {
281 if (vb->planes[plane].mem_priv)
282 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
283 vb->planes[plane].mem_priv = NULL;
288 * __vb2_plane_dmabuf_put() - release memory associated with
289 * a DMABUF shared plane
291 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
297 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
299 call_void_memop(vb, detach_dmabuf, p->mem_priv);
300 dma_buf_put(p->dbuf);
307 * __vb2_buf_dmabuf_put() - release memory associated with
308 * a DMABUF shared buffer
310 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
314 for (plane = 0; plane < vb->num_planes; ++plane)
315 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
319 * __vb2_buf_mem_prepare() - call ->prepare() on buffer's private memory
322 static void __vb2_buf_mem_prepare(struct vb2_buffer *vb)
329 if (vb->need_cache_sync_on_prepare) {
330 for (plane = 0; plane < vb->num_planes; ++plane)
331 call_void_memop(vb, prepare,
332 vb->planes[plane].mem_priv);
338 * __vb2_buf_mem_finish() - call ->finish on buffer's private memory
341 static void __vb2_buf_mem_finish(struct vb2_buffer *vb)
348 if (vb->need_cache_sync_on_finish) {
349 for (plane = 0; plane < vb->num_planes; ++plane)
350 call_void_memop(vb, finish,
351 vb->planes[plane].mem_priv);
357 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
360 static void __setup_offsets(struct vb2_buffer *vb)
362 struct vb2_queue *q = vb->vb2_queue;
364 unsigned long off = 0;
367 struct vb2_buffer *prev = q->bufs[vb->index - 1];
368 struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
370 off = PAGE_ALIGN(p->m.offset + p->length);
373 for (plane = 0; plane < vb->num_planes; ++plane) {
374 vb->planes[plane].m.offset = off;
376 dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n",
377 vb->index, plane, off);
379 off += vb->planes[plane].length;
380 off = PAGE_ALIGN(off);
385 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
386 * video buffer memory for all buffers/planes on the queue and initializes the
389 * Returns the number of buffers successfully allocated.
391 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
392 unsigned int num_buffers, unsigned int num_planes,
393 const unsigned plane_sizes[VB2_MAX_PLANES])
395 unsigned int buffer, plane;
396 struct vb2_buffer *vb;
399 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
400 num_buffers = min_t(unsigned int, num_buffers,
401 VB2_MAX_FRAME - q->num_buffers);
403 for (buffer = 0; buffer < num_buffers; ++buffer) {
404 /* Allocate videobuf buffer structures */
405 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
407 dprintk(q, 1, "memory alloc for buffer struct failed\n");
411 vb->state = VB2_BUF_STATE_DEQUEUED;
413 vb->num_planes = num_planes;
414 vb->index = q->num_buffers + buffer;
418 * We need to set these flags here so that the videobuf2 core
419 * will call ->prepare()/->finish() cache sync/flush on vb2
420 * buffers when appropriate. However, we can avoid explicit
421 * ->prepare() and ->finish() cache sync for DMABUF buffers,
422 * because DMA exporter takes care of it.
424 if (q->memory != VB2_MEMORY_DMABUF) {
425 vb->need_cache_sync_on_prepare = 1;
426 vb->need_cache_sync_on_finish = 1;
428 for (plane = 0; plane < num_planes; ++plane) {
429 vb->planes[plane].length = plane_sizes[plane];
430 vb->planes[plane].min_length = plane_sizes[plane];
432 call_void_bufop(q, init_buffer, vb);
434 q->bufs[vb->index] = vb;
436 /* Allocate video buffer memory for the MMAP type */
437 if (memory == VB2_MEMORY_MMAP) {
438 ret = __vb2_buf_mem_alloc(vb);
440 dprintk(q, 1, "failed allocating memory for buffer %d\n",
442 q->bufs[vb->index] = NULL;
448 * Call the driver-provided buffer initialization
449 * callback, if given. An error in initialization
450 * results in queue setup failure.
452 ret = call_vb_qop(vb, buf_init, vb);
454 dprintk(q, 1, "buffer %d %p initialization failed\n",
456 __vb2_buf_mem_free(vb);
457 q->bufs[vb->index] = NULL;
464 dprintk(q, 3, "allocated %d buffers, %d plane(s) each\n",
471 * __vb2_free_mem() - release all video buffer memory for a given queue
473 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
476 struct vb2_buffer *vb;
478 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
480 vb = q->bufs[buffer];
484 /* Free MMAP buffers or release USERPTR buffers */
485 if (q->memory == VB2_MEMORY_MMAP)
486 __vb2_buf_mem_free(vb);
487 else if (q->memory == VB2_MEMORY_DMABUF)
488 __vb2_buf_dmabuf_put(vb);
490 __vb2_buf_userptr_put(vb);
495 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
496 * related information, if no buffers are left return the queue to an
497 * uninitialized state. Might be called even if the queue has already been freed.
499 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
504 * Sanity check: when preparing a buffer the queue lock is released for
505 * a short while (see __buf_prepare for the details), which would allow
506 * a race with a reqbufs which can call this function. Removing the
507 * buffers from underneath __buf_prepare is obviously a bad idea, so we
508 * check if any of the buffers is in the state PREPARING, and if so we
509 * just return -EAGAIN.
511 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
513 if (q->bufs[buffer] == NULL)
515 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
516 dprintk(q, 1, "preparing buffers, cannot free\n");
521 /* Call driver-provided cleanup function for each buffer, if provided */
522 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
524 struct vb2_buffer *vb = q->bufs[buffer];
526 if (vb && vb->planes[0].mem_priv)
527 call_void_vb_qop(vb, buf_cleanup, vb);
530 /* Release video buffer memory */
531 __vb2_free_mem(q, buffers);
533 #ifdef CONFIG_VIDEO_ADV_DEBUG
535 * Check that all the calls were balances during the life-time of this
536 * queue. If not (or if the debug level is 1 or up), then dump the
537 * counters to the kernel log.
539 if (q->num_buffers) {
540 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
541 q->cnt_wait_prepare != q->cnt_wait_finish;
543 if (unbalanced || debug) {
544 pr_info("counters for queue %p:%s\n", q,
545 unbalanced ? " UNBALANCED!" : "");
546 pr_info(" setup: %u start_streaming: %u stop_streaming: %u\n",
547 q->cnt_queue_setup, q->cnt_start_streaming,
548 q->cnt_stop_streaming);
549 pr_info(" wait_prepare: %u wait_finish: %u\n",
550 q->cnt_wait_prepare, q->cnt_wait_finish);
552 q->cnt_queue_setup = 0;
553 q->cnt_wait_prepare = 0;
554 q->cnt_wait_finish = 0;
555 q->cnt_start_streaming = 0;
556 q->cnt_stop_streaming = 0;
558 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
559 struct vb2_buffer *vb = q->bufs[buffer];
560 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
561 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
562 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
563 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
564 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
565 vb->cnt_buf_queue != vb->cnt_buf_done ||
566 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
567 vb->cnt_buf_init != vb->cnt_buf_cleanup;
569 if (unbalanced || debug) {
570 pr_info(" counters for queue %p, buffer %d:%s\n",
571 q, buffer, unbalanced ? " UNBALANCED!" : "");
572 pr_info(" buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
573 vb->cnt_buf_init, vb->cnt_buf_cleanup,
574 vb->cnt_buf_prepare, vb->cnt_buf_finish);
575 pr_info(" buf_out_validate: %u buf_queue: %u buf_done: %u buf_request_complete: %u\n",
576 vb->cnt_buf_out_validate, vb->cnt_buf_queue,
577 vb->cnt_buf_done, vb->cnt_buf_request_complete);
578 pr_info(" alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
579 vb->cnt_mem_alloc, vb->cnt_mem_put,
580 vb->cnt_mem_prepare, vb->cnt_mem_finish,
582 pr_info(" get_userptr: %u put_userptr: %u\n",
583 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
584 pr_info(" attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
585 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
586 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
587 pr_info(" get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
588 vb->cnt_mem_get_dmabuf,
589 vb->cnt_mem_num_users,
596 /* Free videobuf buffers */
597 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
599 kfree(q->bufs[buffer]);
600 q->bufs[buffer] = NULL;
603 q->num_buffers -= buffers;
604 if (!q->num_buffers) {
605 q->memory = VB2_MEMORY_UNKNOWN;
606 INIT_LIST_HEAD(&q->queued_list);
611 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
614 for (plane = 0; plane < vb->num_planes; ++plane) {
615 void *mem_priv = vb->planes[plane].mem_priv;
617 * If num_users() has not been provided, call_memop
618 * will return 0, apparently nobody cares about this
619 * case anyway. If num_users() returns more than 1,
620 * we are not the only user of the plane's memory.
622 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
627 EXPORT_SYMBOL(vb2_buffer_in_use);
630 * __buffers_in_use() - return true if any buffers on the queue are in use and
631 * the queue cannot be freed (by the means of REQBUFS(0)) call
633 static bool __buffers_in_use(struct vb2_queue *q)
636 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
637 if (vb2_buffer_in_use(q, q->bufs[buffer]))
643 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
645 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
647 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
650 * __verify_userptr_ops() - verify that all memory operations required for
651 * USERPTR queue type have been provided
653 static int __verify_userptr_ops(struct vb2_queue *q)
655 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
656 !q->mem_ops->put_userptr)
663 * __verify_mmap_ops() - verify that all memory operations required for
664 * MMAP queue type have been provided
666 static int __verify_mmap_ops(struct vb2_queue *q)
668 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
669 !q->mem_ops->put || !q->mem_ops->mmap)
676 * __verify_dmabuf_ops() - verify that all memory operations required for
677 * DMABUF queue type have been provided
679 static int __verify_dmabuf_ops(struct vb2_queue *q)
681 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
682 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
683 !q->mem_ops->unmap_dmabuf)
689 int vb2_verify_memory_type(struct vb2_queue *q,
690 enum vb2_memory memory, unsigned int type)
692 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
693 memory != VB2_MEMORY_DMABUF) {
694 dprintk(q, 1, "unsupported memory type\n");
698 if (type != q->type) {
699 dprintk(q, 1, "requested type is incorrect\n");
704 * Make sure all the required memory ops for given memory type
707 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
708 dprintk(q, 1, "MMAP for current setup unsupported\n");
712 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
713 dprintk(q, 1, "USERPTR for current setup unsupported\n");
717 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
718 dprintk(q, 1, "DMABUF for current setup unsupported\n");
723 * Place the busy tests at the end: -EBUSY can be ignored when
724 * create_bufs is called with count == 0, but count == 0 should still
725 * do the memory and type validation.
727 if (vb2_fileio_is_active(q)) {
728 dprintk(q, 1, "file io in progress\n");
733 EXPORT_SYMBOL(vb2_verify_memory_type);
735 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
738 unsigned int num_buffers, allocated_buffers, num_planes = 0;
739 unsigned plane_sizes[VB2_MAX_PLANES] = { };
744 dprintk(q, 1, "streaming active\n");
748 if (q->waiting_in_dqbuf && *count) {
749 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
753 if (*count == 0 || q->num_buffers != 0 ||
754 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
756 * We already have buffers allocated, so first check if they
757 * are not in use and can be freed.
759 mutex_lock(&q->mmap_lock);
760 if (debug && q->memory == VB2_MEMORY_MMAP &&
762 dprintk(q, 1, "memory in use, orphaning buffers\n");
765 * Call queue_cancel to clean up any buffers in the
766 * QUEUED state which is possible if buffers were prepared or
767 * queued without ever calling STREAMON.
769 __vb2_queue_cancel(q);
770 ret = __vb2_queue_free(q, q->num_buffers);
771 mutex_unlock(&q->mmap_lock);
776 * In case of REQBUFS(0) return immediately without calling
777 * driver's queue_setup() callback and allocating resources.
784 * Make sure the requested values and current defaults are sane.
786 WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME);
787 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed);
788 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
789 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
791 * Set this now to ensure that drivers see the correct q->memory value
792 * in the queue_setup op.
794 mutex_lock(&q->mmap_lock);
796 mutex_unlock(&q->mmap_lock);
799 * Ask the driver how many buffers and planes per buffer it requires.
800 * Driver also sets the size and allocator context for each plane.
802 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
803 plane_sizes, q->alloc_devs);
807 /* Check that driver has set sane values */
808 if (WARN_ON(!num_planes)) {
813 for (i = 0; i < num_planes; i++)
814 if (WARN_ON(!plane_sizes[i])) {
819 /* Finally, allocate buffers and video memory */
821 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
822 if (allocated_buffers == 0) {
823 dprintk(q, 1, "memory allocation failed\n");
829 * There is no point in continuing if we can't allocate the minimum
830 * number of buffers needed by this vb2_queue.
832 if (allocated_buffers < q->min_buffers_needed)
836 * Check if driver can handle the allocated number of buffers.
838 if (!ret && allocated_buffers < num_buffers) {
839 num_buffers = allocated_buffers;
841 * num_planes is set by the previous queue_setup(), but since it
842 * signals to queue_setup() whether it is called from create_bufs()
843 * vs reqbufs() we zero it here to signal that queue_setup() is
844 * called for the reqbufs() case.
848 ret = call_qop(q, queue_setup, q, &num_buffers,
849 &num_planes, plane_sizes, q->alloc_devs);
851 if (!ret && allocated_buffers < num_buffers)
855 * Either the driver has accepted a smaller number of buffers,
856 * or .queue_setup() returned an error
860 mutex_lock(&q->mmap_lock);
861 q->num_buffers = allocated_buffers;
865 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
866 * from q->num_buffers and it will reset q->memory to
867 * VB2_MEMORY_UNKNOWN.
869 __vb2_queue_free(q, allocated_buffers);
870 mutex_unlock(&q->mmap_lock);
873 mutex_unlock(&q->mmap_lock);
876 * Return the number of successfully allocated buffers
879 *count = allocated_buffers;
880 q->waiting_for_buffers = !q->is_output;
885 mutex_lock(&q->mmap_lock);
886 q->memory = VB2_MEMORY_UNKNOWN;
887 mutex_unlock(&q->mmap_lock);
890 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
892 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
894 unsigned int requested_planes,
895 const unsigned int requested_sizes[])
897 unsigned int num_planes = 0, num_buffers, allocated_buffers;
898 unsigned plane_sizes[VB2_MAX_PLANES] = { };
899 bool no_previous_buffers = !q->num_buffers;
902 if (q->num_buffers == VB2_MAX_FRAME) {
903 dprintk(q, 1, "maximum number of buffers already allocated\n");
907 if (no_previous_buffers) {
908 if (q->waiting_in_dqbuf && *count) {
909 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
912 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
914 * Set this now to ensure that drivers see the correct q->memory
915 * value in the queue_setup op.
917 mutex_lock(&q->mmap_lock);
919 mutex_unlock(&q->mmap_lock);
920 q->waiting_for_buffers = !q->is_output;
922 if (q->memory != memory) {
923 dprintk(q, 1, "memory model mismatch\n");
928 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
930 if (requested_planes && requested_sizes) {
931 num_planes = requested_planes;
932 memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
936 * Ask the driver, whether the requested number of buffers, planes per
937 * buffer and their sizes are acceptable
939 ret = call_qop(q, queue_setup, q, &num_buffers,
940 &num_planes, plane_sizes, q->alloc_devs);
944 /* Finally, allocate buffers and video memory */
945 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
946 num_planes, plane_sizes);
947 if (allocated_buffers == 0) {
948 dprintk(q, 1, "memory allocation failed\n");
954 * Check if driver can handle the so far allocated number of buffers.
956 if (allocated_buffers < num_buffers) {
957 num_buffers = allocated_buffers;
960 * q->num_buffers contains the total number of buffers, that the
961 * queue driver has set up
963 ret = call_qop(q, queue_setup, q, &num_buffers,
964 &num_planes, plane_sizes, q->alloc_devs);
966 if (!ret && allocated_buffers < num_buffers)
970 * Either the driver has accepted a smaller number of buffers,
971 * or .queue_setup() returned an error
975 mutex_lock(&q->mmap_lock);
976 q->num_buffers += allocated_buffers;
980 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
981 * from q->num_buffers and it will reset q->memory to
982 * VB2_MEMORY_UNKNOWN.
984 __vb2_queue_free(q, allocated_buffers);
985 mutex_unlock(&q->mmap_lock);
988 mutex_unlock(&q->mmap_lock);
991 * Return the number of successfully allocated buffers
994 *count = allocated_buffers;
999 if (no_previous_buffers) {
1000 mutex_lock(&q->mmap_lock);
1001 q->memory = VB2_MEMORY_UNKNOWN;
1002 mutex_unlock(&q->mmap_lock);
1006 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
1008 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
1010 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
1013 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
1016 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
1018 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
1020 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
1023 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
1025 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
1027 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1029 struct vb2_queue *q = vb->vb2_queue;
1030 unsigned long flags;
1032 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
1035 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
1036 state != VB2_BUF_STATE_ERROR &&
1037 state != VB2_BUF_STATE_QUEUED))
1038 state = VB2_BUF_STATE_ERROR;
1040 #ifdef CONFIG_VIDEO_ADV_DEBUG
1042 * Although this is not a callback, it still does have to balance
1043 * with the buf_queue op. So update this counter manually.
1047 dprintk(q, 4, "done processing on buffer %d, state: %s\n",
1048 vb->index, vb2_state_name(state));
1050 if (state != VB2_BUF_STATE_QUEUED)
1051 __vb2_buf_mem_finish(vb);
1053 spin_lock_irqsave(&q->done_lock, flags);
1054 if (state == VB2_BUF_STATE_QUEUED) {
1055 vb->state = VB2_BUF_STATE_QUEUED;
1057 /* Add the buffer to the done buffers list */
1058 list_add_tail(&vb->done_entry, &q->done_list);
1061 atomic_dec(&q->owned_by_drv_count);
1063 if (state != VB2_BUF_STATE_QUEUED && vb->req_obj.req) {
1064 media_request_object_unbind(&vb->req_obj);
1065 media_request_object_put(&vb->req_obj);
1068 spin_unlock_irqrestore(&q->done_lock, flags);
1070 trace_vb2_buf_done(q, vb);
1073 case VB2_BUF_STATE_QUEUED:
1076 /* Inform any processes that may be waiting for buffers */
1077 wake_up(&q->done_wq);
1081 EXPORT_SYMBOL_GPL(vb2_buffer_done);
1083 void vb2_discard_done(struct vb2_queue *q)
1085 struct vb2_buffer *vb;
1086 unsigned long flags;
1088 spin_lock_irqsave(&q->done_lock, flags);
1089 list_for_each_entry(vb, &q->done_list, done_entry)
1090 vb->state = VB2_BUF_STATE_ERROR;
1091 spin_unlock_irqrestore(&q->done_lock, flags);
1093 EXPORT_SYMBOL_GPL(vb2_discard_done);
1096 * __prepare_mmap() - prepare an MMAP buffer
1098 static int __prepare_mmap(struct vb2_buffer *vb)
1102 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1104 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
1108 * __prepare_userptr() - prepare a USERPTR buffer
1110 static int __prepare_userptr(struct vb2_buffer *vb)
1112 struct vb2_plane planes[VB2_MAX_PLANES];
1113 struct vb2_queue *q = vb->vb2_queue;
1117 bool reacquired = vb->planes[0].mem_priv == NULL;
1119 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1120 /* Copy relevant information provided by the userspace */
1121 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1126 for (plane = 0; plane < vb->num_planes; ++plane) {
1127 /* Skip the plane if already verified */
1128 if (vb->planes[plane].m.userptr &&
1129 vb->planes[plane].m.userptr == planes[plane].m.userptr
1130 && vb->planes[plane].length == planes[plane].length)
1133 dprintk(q, 3, "userspace address for plane %d changed, reacquiring memory\n",
1136 /* Check if the provided plane buffer is large enough */
1137 if (planes[plane].length < vb->planes[plane].min_length) {
1138 dprintk(q, 1, "provided buffer size %u is less than setup size %u for plane %d\n",
1139 planes[plane].length,
1140 vb->planes[plane].min_length,
1146 /* Release previously acquired memory if present */
1147 if (vb->planes[plane].mem_priv) {
1150 vb->copied_timestamp = 0;
1151 call_void_vb_qop(vb, buf_cleanup, vb);
1153 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
1156 vb->planes[plane].mem_priv = NULL;
1157 vb->planes[plane].bytesused = 0;
1158 vb->planes[plane].length = 0;
1159 vb->planes[plane].m.userptr = 0;
1160 vb->planes[plane].data_offset = 0;
1162 /* Acquire each plane's memory */
1163 mem_priv = call_ptr_memop(vb, get_userptr,
1164 q->alloc_devs[plane] ? : q->dev,
1165 planes[plane].m.userptr,
1166 planes[plane].length, q->dma_dir);
1167 if (IS_ERR(mem_priv)) {
1168 dprintk(q, 1, "failed acquiring userspace memory for plane %d\n",
1170 ret = PTR_ERR(mem_priv);
1173 vb->planes[plane].mem_priv = mem_priv;
1177 * Now that everything is in order, copy relevant information
1178 * provided by userspace.
1180 for (plane = 0; plane < vb->num_planes; ++plane) {
1181 vb->planes[plane].bytesused = planes[plane].bytesused;
1182 vb->planes[plane].length = planes[plane].length;
1183 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1184 vb->planes[plane].data_offset = planes[plane].data_offset;
1189 * One or more planes changed, so we must call buf_init to do
1190 * the driver-specific initialization on the newly acquired
1191 * buffer, if provided.
1193 ret = call_vb_qop(vb, buf_init, vb);
1195 dprintk(q, 1, "buffer initialization failed\n");
1200 ret = call_vb_qop(vb, buf_prepare, vb);
1202 dprintk(q, 1, "buffer preparation failed\n");
1203 call_void_vb_qop(vb, buf_cleanup, vb);
1209 /* In case of errors, release planes that were already acquired */
1210 for (plane = 0; plane < vb->num_planes; ++plane) {
1211 if (vb->planes[plane].mem_priv)
1212 call_void_memop(vb, put_userptr,
1213 vb->planes[plane].mem_priv);
1214 vb->planes[plane].mem_priv = NULL;
1215 vb->planes[plane].m.userptr = 0;
1216 vb->planes[plane].length = 0;
1223 * __prepare_dmabuf() - prepare a DMABUF buffer
1225 static int __prepare_dmabuf(struct vb2_buffer *vb)
1227 struct vb2_plane planes[VB2_MAX_PLANES];
1228 struct vb2_queue *q = vb->vb2_queue;
1232 bool reacquired = vb->planes[0].mem_priv == NULL;
1234 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1235 /* Copy relevant information provided by the userspace */
1236 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1241 for (plane = 0; plane < vb->num_planes; ++plane) {
1242 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1244 if (IS_ERR_OR_NULL(dbuf)) {
1245 dprintk(q, 1, "invalid dmabuf fd for plane %d\n",
1251 /* use DMABUF size if length is not provided */
1252 if (planes[plane].length == 0)
1253 planes[plane].length = dbuf->size;
1255 if (planes[plane].length < vb->planes[plane].min_length) {
1256 dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1257 planes[plane].length, plane,
1258 vb->planes[plane].min_length);
1264 /* Skip the plane if already verified */
1265 if (dbuf == vb->planes[plane].dbuf &&
1266 vb->planes[plane].length == planes[plane].length) {
1271 dprintk(q, 3, "buffer for plane %d changed\n", plane);
1275 vb->copied_timestamp = 0;
1276 call_void_vb_qop(vb, buf_cleanup, vb);
1279 /* Release previously acquired memory if present */
1280 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1281 vb->planes[plane].bytesused = 0;
1282 vb->planes[plane].length = 0;
1283 vb->planes[plane].m.fd = 0;
1284 vb->planes[plane].data_offset = 0;
1286 /* Acquire each plane's memory */
1287 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1288 q->alloc_devs[plane] ? : q->dev,
1289 dbuf, planes[plane].length, q->dma_dir);
1290 if (IS_ERR(mem_priv)) {
1291 dprintk(q, 1, "failed to attach dmabuf\n");
1292 ret = PTR_ERR(mem_priv);
1297 vb->planes[plane].dbuf = dbuf;
1298 vb->planes[plane].mem_priv = mem_priv;
1302 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1303 * here instead just before the DMA, while queueing the buffer(s) so
1304 * userspace knows sooner rather than later if the dma-buf map fails.
1306 for (plane = 0; plane < vb->num_planes; ++plane) {
1307 if (vb->planes[plane].dbuf_mapped)
1310 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1312 dprintk(q, 1, "failed to map dmabuf for plane %d\n",
1316 vb->planes[plane].dbuf_mapped = 1;
1320 * Now that everything is in order, copy relevant information
1321 * provided by userspace.
1323 for (plane = 0; plane < vb->num_planes; ++plane) {
1324 vb->planes[plane].bytesused = planes[plane].bytesused;
1325 vb->planes[plane].length = planes[plane].length;
1326 vb->planes[plane].m.fd = planes[plane].m.fd;
1327 vb->planes[plane].data_offset = planes[plane].data_offset;
1332 * Call driver-specific initialization on the newly acquired buffer,
1335 ret = call_vb_qop(vb, buf_init, vb);
1337 dprintk(q, 1, "buffer initialization failed\n");
1342 ret = call_vb_qop(vb, buf_prepare, vb);
1344 dprintk(q, 1, "buffer preparation failed\n");
1345 call_void_vb_qop(vb, buf_cleanup, vb);
1351 /* In case of errors, release planes that were already acquired */
1352 __vb2_buf_dmabuf_put(vb);
1358 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1360 static void __enqueue_in_driver(struct vb2_buffer *vb)
1362 struct vb2_queue *q = vb->vb2_queue;
1364 vb->state = VB2_BUF_STATE_ACTIVE;
1365 atomic_inc(&q->owned_by_drv_count);
1367 trace_vb2_buf_queue(q, vb);
1369 call_void_vb_qop(vb, buf_queue, vb);
1372 static int __buf_prepare(struct vb2_buffer *vb)
1374 struct vb2_queue *q = vb->vb2_queue;
1375 enum vb2_buffer_state orig_state = vb->state;
1379 dprintk(q, 1, "fatal error occurred on queue\n");
1385 WARN_ON(vb->synced);
1388 ret = call_vb_qop(vb, buf_out_validate, vb);
1390 dprintk(q, 1, "buffer validation failed\n");
1395 vb->state = VB2_BUF_STATE_PREPARING;
1397 switch (q->memory) {
1398 case VB2_MEMORY_MMAP:
1399 ret = __prepare_mmap(vb);
1401 case VB2_MEMORY_USERPTR:
1402 ret = __prepare_userptr(vb);
1404 case VB2_MEMORY_DMABUF:
1405 ret = __prepare_dmabuf(vb);
1408 WARN(1, "Invalid queue type\n");
1414 dprintk(q, 1, "buffer preparation failed: %d\n", ret);
1415 vb->state = orig_state;
1419 __vb2_buf_mem_prepare(vb);
1421 vb->state = orig_state;
1426 static int vb2_req_prepare(struct media_request_object *obj)
1428 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1431 if (WARN_ON(vb->state != VB2_BUF_STATE_IN_REQUEST))
1434 mutex_lock(vb->vb2_queue->lock);
1435 ret = __buf_prepare(vb);
1436 mutex_unlock(vb->vb2_queue->lock);
1440 static void __vb2_dqbuf(struct vb2_buffer *vb);
1442 static void vb2_req_unprepare(struct media_request_object *obj)
1444 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1446 mutex_lock(vb->vb2_queue->lock);
1448 vb->state = VB2_BUF_STATE_IN_REQUEST;
1449 mutex_unlock(vb->vb2_queue->lock);
1450 WARN_ON(!vb->req_obj.req);
1453 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1454 struct media_request *req);
1456 static void vb2_req_queue(struct media_request_object *obj)
1458 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1460 mutex_lock(vb->vb2_queue->lock);
1461 vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
1462 mutex_unlock(vb->vb2_queue->lock);
1465 static void vb2_req_unbind(struct media_request_object *obj)
1467 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1469 if (vb->state == VB2_BUF_STATE_IN_REQUEST)
1470 call_void_bufop(vb->vb2_queue, init_buffer, vb);
1473 static void vb2_req_release(struct media_request_object *obj)
1475 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1477 if (vb->state == VB2_BUF_STATE_IN_REQUEST) {
1478 vb->state = VB2_BUF_STATE_DEQUEUED;
1480 media_request_put(vb->request);
1485 static const struct media_request_object_ops vb2_core_req_ops = {
1486 .prepare = vb2_req_prepare,
1487 .unprepare = vb2_req_unprepare,
1488 .queue = vb2_req_queue,
1489 .unbind = vb2_req_unbind,
1490 .release = vb2_req_release,
1493 bool vb2_request_object_is_buffer(struct media_request_object *obj)
1495 return obj->ops == &vb2_core_req_ops;
1497 EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer);
1499 unsigned int vb2_request_buffer_cnt(struct media_request *req)
1501 struct media_request_object *obj;
1502 unsigned long flags;
1503 unsigned int buffer_cnt = 0;
1505 spin_lock_irqsave(&req->lock, flags);
1506 list_for_each_entry(obj, &req->objects, list)
1507 if (vb2_request_object_is_buffer(obj))
1509 spin_unlock_irqrestore(&req->lock, flags);
1513 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
1515 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1517 struct vb2_buffer *vb;
1520 vb = q->bufs[index];
1521 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1522 dprintk(q, 1, "invalid buffer state %s\n",
1523 vb2_state_name(vb->state));
1527 dprintk(q, 1, "buffer already prepared\n");
1531 ret = __buf_prepare(vb);
1535 /* Fill buffer information for the userspace */
1536 call_void_bufop(q, fill_user_buffer, vb, pb);
1538 dprintk(q, 2, "prepare of buffer %d succeeded\n", vb->index);
1542 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1545 * vb2_start_streaming() - Attempt to start streaming.
1546 * @q: videobuf2 queue
1548 * Attempt to start streaming. When this function is called there must be
1549 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1550 * number of buffers required for the DMA engine to function). If the
1551 * @start_streaming op fails it is supposed to return all the driver-owned
1552 * buffers back to vb2 in state QUEUED. Check if that happened and if
1553 * not warn and reclaim them forcefully.
1555 static int vb2_start_streaming(struct vb2_queue *q)
1557 struct vb2_buffer *vb;
1561 * If any buffers were queued before streamon,
1562 * we can now pass them to driver for processing.
1564 list_for_each_entry(vb, &q->queued_list, queued_entry)
1565 __enqueue_in_driver(vb);
1567 /* Tell the driver to start streaming */
1568 q->start_streaming_called = 1;
1569 ret = call_qop(q, start_streaming, q,
1570 atomic_read(&q->owned_by_drv_count));
1574 q->start_streaming_called = 0;
1576 dprintk(q, 1, "driver refused to start streaming\n");
1578 * If you see this warning, then the driver isn't cleaning up properly
1579 * after a failed start_streaming(). See the start_streaming()
1580 * documentation in videobuf2-core.h for more information how buffers
1581 * should be returned to vb2 in start_streaming().
1583 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1587 * Forcefully reclaim buffers if the driver did not
1588 * correctly return them to vb2.
1590 for (i = 0; i < q->num_buffers; ++i) {
1592 if (vb->state == VB2_BUF_STATE_ACTIVE)
1593 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1595 /* Must be zero now */
1596 WARN_ON(atomic_read(&q->owned_by_drv_count));
1599 * If done_list is not empty, then start_streaming() didn't call
1600 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1603 WARN_ON(!list_empty(&q->done_list));
1607 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1608 struct media_request *req)
1610 struct vb2_buffer *vb;
1611 enum vb2_buffer_state orig_state;
1615 dprintk(q, 1, "fatal error occurred on queue\n");
1619 vb = q->bufs[index];
1621 if (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1622 q->requires_requests) {
1623 dprintk(q, 1, "qbuf requires a request\n");
1627 if ((req && q->uses_qbuf) ||
1628 (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1629 q->uses_requests)) {
1630 dprintk(q, 1, "queue in wrong mode (qbuf vs requests)\n");
1637 q->uses_requests = 1;
1638 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1639 dprintk(q, 1, "buffer %d not in dequeued state\n",
1644 if (q->is_output && !vb->prepared) {
1645 ret = call_vb_qop(vb, buf_out_validate, vb);
1647 dprintk(q, 1, "buffer validation failed\n");
1652 media_request_object_init(&vb->req_obj);
1654 /* Make sure the request is in a safe state for updating. */
1655 ret = media_request_lock_for_update(req);
1658 ret = media_request_object_bind(req, &vb2_core_req_ops,
1659 q, true, &vb->req_obj);
1660 media_request_unlock_for_update(req);
1664 vb->state = VB2_BUF_STATE_IN_REQUEST;
1667 * Increment the refcount and store the request.
1668 * The request refcount is decremented again when the
1669 * buffer is dequeued. This is to prevent vb2_buffer_done()
1670 * from freeing the request from interrupt context, which can
1671 * happen if the application closed the request fd after
1672 * queueing the request.
1674 media_request_get(req);
1677 /* Fill buffer information for the userspace */
1679 call_void_bufop(q, copy_timestamp, vb, pb);
1680 call_void_bufop(q, fill_user_buffer, vb, pb);
1683 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
1687 if (vb->state != VB2_BUF_STATE_IN_REQUEST)
1690 switch (vb->state) {
1691 case VB2_BUF_STATE_DEQUEUED:
1692 case VB2_BUF_STATE_IN_REQUEST:
1693 if (!vb->prepared) {
1694 ret = __buf_prepare(vb);
1699 case VB2_BUF_STATE_PREPARING:
1700 dprintk(q, 1, "buffer still being prepared\n");
1703 dprintk(q, 1, "invalid buffer state %s\n",
1704 vb2_state_name(vb->state));
1709 * Add to the queued buffers list, a buffer will stay on it until
1710 * dequeued in dqbuf.
1712 orig_state = vb->state;
1713 list_add_tail(&vb->queued_entry, &q->queued_list);
1715 q->waiting_for_buffers = false;
1716 vb->state = VB2_BUF_STATE_QUEUED;
1719 call_void_bufop(q, copy_timestamp, vb, pb);
1721 trace_vb2_qbuf(q, vb);
1724 * If already streaming, give the buffer to driver for processing.
1725 * If not, the buffer will be given to driver on next streamon.
1727 if (q->start_streaming_called)
1728 __enqueue_in_driver(vb);
1730 /* Fill buffer information for the userspace */
1732 call_void_bufop(q, fill_user_buffer, vb, pb);
1735 * If streamon has been called, and we haven't yet called
1736 * start_streaming() since not enough buffers were queued, and
1737 * we now have reached the minimum number of queued buffers,
1738 * then we can finally call start_streaming().
1740 if (q->streaming && !q->start_streaming_called &&
1741 q->queued_count >= q->min_buffers_needed) {
1742 ret = vb2_start_streaming(q);
1745 * Since vb2_core_qbuf will return with an error,
1746 * we should return it to state DEQUEUED since
1747 * the error indicates that the buffer wasn't queued.
1749 list_del(&vb->queued_entry);
1751 vb->state = orig_state;
1756 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
1759 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1762 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1765 * Will sleep if required for nonblocking == false.
1767 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1770 * All operations on vb_done_list are performed under done_lock
1771 * spinlock protection. However, buffers may be removed from
1772 * it and returned to userspace only while holding both driver's
1773 * lock and the done_lock spinlock. Thus we can be sure that as
1774 * long as we hold the driver's lock, the list will remain not
1775 * empty if list_empty() check succeeds.
1781 if (q->waiting_in_dqbuf) {
1782 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
1786 if (!q->streaming) {
1787 dprintk(q, 1, "streaming off, will not wait for buffers\n");
1792 dprintk(q, 1, "Queue in error state, will not wait for buffers\n");
1796 if (q->last_buffer_dequeued) {
1797 dprintk(q, 3, "last buffer dequeued already, will not wait for buffers\n");
1801 if (!list_empty(&q->done_list)) {
1803 * Found a buffer that we were waiting for.
1809 dprintk(q, 3, "nonblocking and no buffers to dequeue, will not wait\n");
1813 q->waiting_in_dqbuf = 1;
1815 * We are streaming and blocking, wait for another buffer to
1816 * become ready or for streamoff. Driver's lock is released to
1817 * allow streamoff or qbuf to be called while waiting.
1819 call_void_qop(q, wait_prepare, q);
1822 * All locks have been released, it is safe to sleep now.
1824 dprintk(q, 3, "will sleep waiting for buffers\n");
1825 ret = wait_event_interruptible(q->done_wq,
1826 !list_empty(&q->done_list) || !q->streaming ||
1830 * We need to reevaluate both conditions again after reacquiring
1831 * the locks or return an error if one occurred.
1833 call_void_qop(q, wait_finish, q);
1834 q->waiting_in_dqbuf = 0;
1836 dprintk(q, 1, "sleep was interrupted\n");
1844 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1846 * Will sleep if required for nonblocking == false.
1848 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1849 void *pb, int nonblocking)
1851 unsigned long flags;
1855 * Wait for at least one buffer to become available on the done_list.
1857 ret = __vb2_wait_for_done_vb(q, nonblocking);
1862 * Driver's lock has been held since we last verified that done_list
1863 * is not empty, so no need for another list_empty(done_list) check.
1865 spin_lock_irqsave(&q->done_lock, flags);
1866 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1868 * Only remove the buffer from done_list if all planes can be
1869 * handled. Some cases such as V4L2 file I/O and DVB have pb
1870 * == NULL; skip the check then as there's nothing to verify.
1873 ret = call_bufop(q, verify_planes_array, *vb, pb);
1875 list_del(&(*vb)->done_entry);
1876 spin_unlock_irqrestore(&q->done_lock, flags);
1881 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1883 if (!q->streaming) {
1884 dprintk(q, 1, "streaming off, will not wait for buffers\n");
1888 if (q->start_streaming_called)
1889 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1892 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1895 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1897 static void __vb2_dqbuf(struct vb2_buffer *vb)
1899 struct vb2_queue *q = vb->vb2_queue;
1901 /* nothing to do if the buffer is already dequeued */
1902 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1905 vb->state = VB2_BUF_STATE_DEQUEUED;
1907 call_void_bufop(q, init_buffer, vb);
1910 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
1913 struct vb2_buffer *vb = NULL;
1916 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1920 switch (vb->state) {
1921 case VB2_BUF_STATE_DONE:
1922 dprintk(q, 3, "returning done buffer\n");
1924 case VB2_BUF_STATE_ERROR:
1925 dprintk(q, 3, "returning done buffer with errors\n");
1928 dprintk(q, 1, "invalid buffer state %s\n",
1929 vb2_state_name(vb->state));
1933 call_void_vb_qop(vb, buf_finish, vb);
1937 *pindex = vb->index;
1939 /* Fill buffer information for the userspace */
1941 call_void_bufop(q, fill_user_buffer, vb, pb);
1943 /* Remove from videobuf queue */
1944 list_del(&vb->queued_entry);
1947 trace_vb2_dqbuf(q, vb);
1949 /* go back to dequeued state */
1952 if (WARN_ON(vb->req_obj.req)) {
1953 media_request_object_unbind(&vb->req_obj);
1954 media_request_object_put(&vb->req_obj);
1957 media_request_put(vb->request);
1960 dprintk(q, 2, "dqbuf of buffer %d, state: %s\n",
1961 vb->index, vb2_state_name(vb->state));
1966 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1969 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1971 * Removes all queued buffers from driver's queue and all buffers queued by
1972 * userspace from videobuf's queue. Returns to state after reqbufs.
1974 static void __vb2_queue_cancel(struct vb2_queue *q)
1979 * Tell driver to stop all transactions and release all queued
1982 if (q->start_streaming_called)
1983 call_void_qop(q, stop_streaming, q);
1986 * If you see this warning, then the driver isn't cleaning up properly
1987 * in stop_streaming(). See the stop_streaming() documentation in
1988 * videobuf2-core.h for more information how buffers should be returned
1989 * to vb2 in stop_streaming().
1991 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1992 for (i = 0; i < q->num_buffers; ++i)
1993 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
1994 pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
1996 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1998 /* Must be zero now */
1999 WARN_ON(atomic_read(&q->owned_by_drv_count));
2003 q->start_streaming_called = 0;
2004 q->queued_count = 0;
2006 q->uses_requests = 0;
2010 * Remove all buffers from videobuf's list...
2012 INIT_LIST_HEAD(&q->queued_list);
2014 * ...and done list; userspace will not receive any buffers it
2015 * has not already dequeued before initiating cancel.
2017 INIT_LIST_HEAD(&q->done_list);
2018 atomic_set(&q->owned_by_drv_count, 0);
2019 wake_up_all(&q->done_wq);
2022 * Reinitialize all buffers for next use.
2023 * Make sure to call buf_finish for any queued buffers. Normally
2024 * that's done in dqbuf, but that's not going to happen when we
2025 * cancel the whole queue. Note: this code belongs here, not in
2026 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
2027 * call to __fill_user_buffer() after buf_finish(). That order can't
2028 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
2030 for (i = 0; i < q->num_buffers; ++i) {
2031 struct vb2_buffer *vb = q->bufs[i];
2032 struct media_request *req = vb->req_obj.req;
2035 * If a request is associated with this buffer, then
2036 * call buf_request_cancel() to give the driver to complete()
2037 * related request objects. Otherwise those objects would
2041 enum media_request_state state;
2042 unsigned long flags;
2044 spin_lock_irqsave(&req->lock, flags);
2046 spin_unlock_irqrestore(&req->lock, flags);
2048 if (state == MEDIA_REQUEST_STATE_QUEUED)
2049 call_void_vb_qop(vb, buf_request_complete, vb);
2052 __vb2_buf_mem_finish(vb);
2055 call_void_vb_qop(vb, buf_finish, vb);
2060 if (vb->req_obj.req) {
2061 media_request_object_unbind(&vb->req_obj);
2062 media_request_object_put(&vb->req_obj);
2065 media_request_put(vb->request);
2067 vb->copied_timestamp = 0;
2071 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
2075 if (type != q->type) {
2076 dprintk(q, 1, "invalid stream type\n");
2081 dprintk(q, 3, "already streaming\n");
2085 if (!q->num_buffers) {
2086 dprintk(q, 1, "no buffers have been allocated\n");
2090 if (q->num_buffers < q->min_buffers_needed) {
2091 dprintk(q, 1, "need at least %u allocated buffers\n",
2092 q->min_buffers_needed);
2097 * Tell driver to start streaming provided sufficient buffers
2100 if (q->queued_count >= q->min_buffers_needed) {
2101 ret = v4l_vb2q_enable_media_source(q);
2104 ret = vb2_start_streaming(q);
2111 dprintk(q, 3, "successful\n");
2114 EXPORT_SYMBOL_GPL(vb2_core_streamon);
2116 void vb2_queue_error(struct vb2_queue *q)
2120 wake_up_all(&q->done_wq);
2122 EXPORT_SYMBOL_GPL(vb2_queue_error);
2124 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2126 if (type != q->type) {
2127 dprintk(q, 1, "invalid stream type\n");
2132 * Cancel will pause streaming and remove all buffers from the driver
2133 * and videobuf, effectively returning control over them to userspace.
2135 * Note that we do this even if q->streaming == 0: if you prepare or
2136 * queue buffers, and then call streamoff without ever having called
2137 * streamon, you would still expect those buffers to be returned to
2138 * their normal dequeued state.
2140 __vb2_queue_cancel(q);
2141 q->waiting_for_buffers = !q->is_output;
2142 q->last_buffer_dequeued = false;
2144 dprintk(q, 3, "successful\n");
2147 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
2150 * __find_plane_by_offset() - find plane associated with the given offset off
2152 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2153 unsigned int *_buffer, unsigned int *_plane)
2155 struct vb2_buffer *vb;
2156 unsigned int buffer, plane;
2159 * Sanity checks to ensure the lock is held, MEMORY_MMAP is
2160 * used and fileio isn't active.
2162 lockdep_assert_held(&q->mmap_lock);
2164 if (q->memory != VB2_MEMORY_MMAP) {
2165 dprintk(q, 1, "queue is not currently set up for mmap\n");
2169 if (vb2_fileio_is_active(q)) {
2170 dprintk(q, 1, "file io in progress\n");
2175 * Go over all buffers and their planes, comparing the given offset
2176 * with an offset assigned to each plane. If a match is found,
2177 * return its buffer and plane numbers.
2179 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2180 vb = q->bufs[buffer];
2182 for (plane = 0; plane < vb->num_planes; ++plane) {
2183 if (vb->planes[plane].m.offset == off) {
2194 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2195 unsigned int index, unsigned int plane, unsigned int flags)
2197 struct vb2_buffer *vb = NULL;
2198 struct vb2_plane *vb_plane;
2200 struct dma_buf *dbuf;
2202 if (q->memory != VB2_MEMORY_MMAP) {
2203 dprintk(q, 1, "queue is not currently set up for mmap\n");
2207 if (!q->mem_ops->get_dmabuf) {
2208 dprintk(q, 1, "queue does not support DMA buffer exporting\n");
2212 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
2213 dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n");
2217 if (type != q->type) {
2218 dprintk(q, 1, "invalid buffer type\n");
2222 if (index >= q->num_buffers) {
2223 dprintk(q, 1, "buffer index out of range\n");
2227 vb = q->bufs[index];
2229 if (plane >= vb->num_planes) {
2230 dprintk(q, 1, "buffer plane out of range\n");
2234 if (vb2_fileio_is_active(q)) {
2235 dprintk(q, 1, "expbuf: file io in progress\n");
2239 vb_plane = &vb->planes[plane];
2241 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
2243 if (IS_ERR_OR_NULL(dbuf)) {
2244 dprintk(q, 1, "failed to export buffer %d, plane %d\n",
2249 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
2251 dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n",
2257 dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n",
2263 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
2265 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2267 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
2268 struct vb2_buffer *vb;
2269 unsigned int buffer = 0, plane = 0;
2271 unsigned long length;
2274 * Check memory area access mode.
2276 if (!(vma->vm_flags & VM_SHARED)) {
2277 dprintk(q, 1, "invalid vma flags, VM_SHARED needed\n");
2281 if (!(vma->vm_flags & VM_WRITE)) {
2282 dprintk(q, 1, "invalid vma flags, VM_WRITE needed\n");
2286 if (!(vma->vm_flags & VM_READ)) {
2287 dprintk(q, 1, "invalid vma flags, VM_READ needed\n");
2292 mutex_lock(&q->mmap_lock);
2295 * Find the plane corresponding to the offset passed by userspace. This
2296 * will return an error if not MEMORY_MMAP or file I/O is in progress.
2298 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2302 vb = q->bufs[buffer];
2305 * MMAP requires page_aligned buffers.
2306 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2307 * so, we need to do the same here.
2309 length = PAGE_ALIGN(vb->planes[plane].length);
2310 if (length < (vma->vm_end - vma->vm_start)) {
2312 "MMAP invalid, as it would overflow buffer length\n");
2318 * vm_pgoff is treated in V4L2 API as a 'cookie' to select a buffer,
2319 * not as a in-buffer offset. We always want to mmap a whole buffer
2320 * from its beginning.
2324 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2327 mutex_unlock(&q->mmap_lock);
2331 dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2334 EXPORT_SYMBOL_GPL(vb2_mmap);
2337 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2340 unsigned long pgoff,
2341 unsigned long flags)
2343 unsigned long off = pgoff << PAGE_SHIFT;
2344 struct vb2_buffer *vb;
2345 unsigned int buffer, plane;
2349 mutex_lock(&q->mmap_lock);
2352 * Find the plane corresponding to the offset passed by userspace. This
2353 * will return an error if not MEMORY_MMAP or file I/O is in progress.
2355 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2359 vb = q->bufs[buffer];
2361 vaddr = vb2_plane_vaddr(vb, plane);
2362 mutex_unlock(&q->mmap_lock);
2363 return vaddr ? (unsigned long)vaddr : -EINVAL;
2366 mutex_unlock(&q->mmap_lock);
2369 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2372 int vb2_core_queue_init(struct vb2_queue *q)
2379 WARN_ON(!q->mem_ops) ||
2380 WARN_ON(!q->type) ||
2381 WARN_ON(!q->io_modes) ||
2382 WARN_ON(!q->ops->queue_setup) ||
2383 WARN_ON(!q->ops->buf_queue))
2386 if (WARN_ON(q->requires_requests && !q->supports_requests))
2389 INIT_LIST_HEAD(&q->queued_list);
2390 INIT_LIST_HEAD(&q->done_list);
2391 spin_lock_init(&q->done_lock);
2392 mutex_init(&q->mmap_lock);
2393 init_waitqueue_head(&q->done_wq);
2395 q->memory = VB2_MEMORY_UNKNOWN;
2397 if (q->buf_struct_size == 0)
2398 q->buf_struct_size = sizeof(struct vb2_buffer);
2400 if (q->bidirectional)
2401 q->dma_dir = DMA_BIDIRECTIONAL;
2403 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2405 if (q->name[0] == '\0')
2406 snprintf(q->name, sizeof(q->name), "%s-%p",
2407 q->is_output ? "out" : "cap", q);
2411 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2413 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2414 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2415 void vb2_core_queue_release(struct vb2_queue *q)
2417 __vb2_cleanup_fileio(q);
2418 __vb2_queue_cancel(q);
2419 mutex_lock(&q->mmap_lock);
2420 __vb2_queue_free(q, q->num_buffers);
2421 mutex_unlock(&q->mmap_lock);
2423 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2425 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
2428 __poll_t req_events = poll_requested_events(wait);
2429 struct vb2_buffer *vb = NULL;
2430 unsigned long flags;
2432 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM)))
2434 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
2437 poll_wait(file, &q->done_wq, wait);
2440 * Start file I/O emulator only if streaming API has not been used yet.
2442 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
2443 if (!q->is_output && (q->io_modes & VB2_READ) &&
2444 (req_events & (EPOLLIN | EPOLLRDNORM))) {
2445 if (__vb2_init_fileio(q, 1))
2448 if (q->is_output && (q->io_modes & VB2_WRITE) &&
2449 (req_events & (EPOLLOUT | EPOLLWRNORM))) {
2450 if (__vb2_init_fileio(q, 0))
2453 * Write to OUTPUT queue can be done immediately.
2455 return EPOLLOUT | EPOLLWRNORM;
2460 * There is nothing to wait for if the queue isn't streaming, or if the
2461 * error flag is set.
2463 if (!vb2_is_streaming(q) || q->error)
2467 * If this quirk is set and QBUF hasn't been called yet then
2468 * return EPOLLERR as well. This only affects capture queues, output
2469 * queues will always initialize waiting_for_buffers to false.
2470 * This quirk is set by V4L2 for backwards compatibility reasons.
2472 if (q->quirk_poll_must_check_waiting_for_buffers &&
2473 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM)))
2477 * For output streams you can call write() as long as there are fewer
2478 * buffers queued than there are buffers available.
2480 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
2481 return EPOLLOUT | EPOLLWRNORM;
2483 if (list_empty(&q->done_list)) {
2485 * If the last buffer was dequeued from a capture queue,
2486 * return immediately. DQBUF will return -EPIPE.
2488 if (q->last_buffer_dequeued)
2489 return EPOLLIN | EPOLLRDNORM;
2493 * Take first buffer available for dequeuing.
2495 spin_lock_irqsave(&q->done_lock, flags);
2496 if (!list_empty(&q->done_list))
2497 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2499 spin_unlock_irqrestore(&q->done_lock, flags);
2501 if (vb && (vb->state == VB2_BUF_STATE_DONE
2502 || vb->state == VB2_BUF_STATE_ERROR)) {
2503 return (q->is_output) ?
2504 EPOLLOUT | EPOLLWRNORM :
2505 EPOLLIN | EPOLLRDNORM;
2509 EXPORT_SYMBOL_GPL(vb2_core_poll);
2512 * struct vb2_fileio_buf - buffer context used by file io emulator
2514 * vb2 provides a compatibility layer and emulator of file io (read and
2515 * write) calls on top of streaming API. This structure is used for
2516 * tracking context related to the buffers.
2518 struct vb2_fileio_buf {
2522 unsigned int queued:1;
2526 * struct vb2_fileio_data - queue context used by file io emulator
2528 * @cur_index: the index of the buffer currently being read from or
2529 * written to. If equal to q->num_buffers then a new buffer
2531 * @initial_index: in the read() case all buffers are queued up immediately
2532 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2533 * buffers. However, in the write() case no buffers are initially
2534 * queued, instead whenever a buffer is full it is queued up by
2535 * __vb2_perform_fileio(). Only once all available buffers have
2536 * been queued up will __vb2_perform_fileio() start to dequeue
2537 * buffers. This means that initially __vb2_perform_fileio()
2538 * needs to know what buffer index to use when it is queuing up
2539 * the buffers for the first time. That initial index is stored
2540 * in this field. Once it is equal to q->num_buffers all
2541 * available buffers have been queued and __vb2_perform_fileio()
2542 * should start the normal dequeue/queue cycle.
2544 * vb2 provides a compatibility layer and emulator of file io (read and
2545 * write) calls on top of streaming API. For proper operation it required
2546 * this structure to save the driver state between each call of the read
2547 * or write function.
2549 struct vb2_fileio_data {
2552 unsigned int memory;
2553 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
2554 unsigned int cur_index;
2555 unsigned int initial_index;
2556 unsigned int q_count;
2557 unsigned int dq_count;
2558 unsigned read_once:1;
2559 unsigned write_immediately:1;
2563 * __vb2_init_fileio() - initialize file io emulator
2564 * @q: videobuf2 queue
2565 * @read: mode selector (1 means read, 0 means write)
2567 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2569 struct vb2_fileio_data *fileio;
2571 unsigned int count = 0;
2576 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
2577 (!read && !(q->io_modes & VB2_WRITE))))
2581 * Check if device supports mapping buffers to kernel virtual space.
2583 if (!q->mem_ops->vaddr)
2587 * Check if streaming api has not been already activated.
2589 if (q->streaming || q->num_buffers > 0)
2593 * Start with count 1, driver can increase it in queue_setup()
2597 dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2598 (read) ? "read" : "write", count, q->fileio_read_once,
2599 q->fileio_write_immediately);
2601 fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
2605 fileio->read_once = q->fileio_read_once;
2606 fileio->write_immediately = q->fileio_write_immediately;
2609 * Request buffers and use MMAP type to force driver
2610 * to allocate buffers by itself.
2612 fileio->count = count;
2613 fileio->memory = VB2_MEMORY_MMAP;
2614 fileio->type = q->type;
2616 ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2621 * Check if plane_count is correct
2622 * (multiplane buffers are not supported).
2624 if (q->bufs[0]->num_planes != 1) {
2630 * Get kernel address of each buffer.
2632 for (i = 0; i < q->num_buffers; i++) {
2633 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2634 if (fileio->bufs[i].vaddr == NULL) {
2638 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2642 * Read mode requires pre queuing of all buffers.
2646 * Queue all buffers.
2648 for (i = 0; i < q->num_buffers; i++) {
2649 ret = vb2_core_qbuf(q, i, NULL, NULL);
2652 fileio->bufs[i].queued = 1;
2655 * All buffers have been queued, so mark that by setting
2656 * initial_index to q->num_buffers
2658 fileio->initial_index = q->num_buffers;
2659 fileio->cur_index = q->num_buffers;
2665 ret = vb2_core_streamon(q, q->type);
2673 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2682 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2683 * @q: videobuf2 queue
2685 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2687 struct vb2_fileio_data *fileio = q->fileio;
2690 vb2_core_streamoff(q, q->type);
2693 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2695 dprintk(q, 3, "file io emulator closed\n");
2701 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2702 * @q: videobuf2 queue
2703 * @data: pointed to target userspace buffer
2704 * @count: number of bytes to read or write
2705 * @ppos: file handle position tracking pointer
2706 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2707 * @read: access mode selector (1 means read, 0 means write)
2709 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2710 loff_t *ppos, int nonblock, int read)
2712 struct vb2_fileio_data *fileio;
2713 struct vb2_fileio_buf *buf;
2714 bool is_multiplanar = q->is_multiplanar;
2716 * When using write() to write data to an output video node the vb2 core
2717 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2718 * else is able to provide this information with the write() operation.
2720 bool copy_timestamp = !read && q->copy_timestamp;
2724 dprintk(q, 3, "mode %s, offset %ld, count %zd, %sblocking\n",
2725 read ? "read" : "write", (long)*ppos, count,
2726 nonblock ? "non" : "");
2731 if (q->waiting_in_dqbuf) {
2732 dprintk(q, 3, "another dup()ped fd is %s\n",
2733 read ? "reading" : "writing");
2738 * Initialize emulator on first call.
2740 if (!vb2_fileio_is_active(q)) {
2741 ret = __vb2_init_fileio(q, read);
2742 dprintk(q, 3, "vb2_init_fileio result: %d\n", ret);
2749 * Check if we need to dequeue the buffer.
2751 index = fileio->cur_index;
2752 if (index >= q->num_buffers) {
2753 struct vb2_buffer *b;
2756 * Call vb2_dqbuf to get buffer back.
2758 ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2759 dprintk(q, 5, "vb2_dqbuf result: %d\n", ret);
2762 fileio->dq_count += 1;
2764 fileio->cur_index = index;
2765 buf = &fileio->bufs[index];
2769 * Get number of bytes filled by the driver
2773 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2774 : vb2_plane_size(q->bufs[index], 0);
2775 /* Compensate for data_offset on read in the multiplanar case. */
2776 if (is_multiplanar && read &&
2777 b->planes[0].data_offset < buf->size) {
2778 buf->pos = b->planes[0].data_offset;
2779 buf->size -= buf->pos;
2782 buf = &fileio->bufs[index];
2786 * Limit count on last few bytes of the buffer.
2788 if (buf->pos + count > buf->size) {
2789 count = buf->size - buf->pos;
2790 dprintk(q, 5, "reducing read count: %zd\n", count);
2794 * Transfer data to userspace.
2796 dprintk(q, 3, "copying %zd bytes - buffer %d, offset %u\n",
2797 count, index, buf->pos);
2799 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2801 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2803 dprintk(q, 3, "error copying data\n");
2814 * Queue next buffer if required.
2816 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
2817 struct vb2_buffer *b = q->bufs[index];
2820 * Check if this is the last buffer to read.
2822 if (read && fileio->read_once && fileio->dq_count == 1) {
2823 dprintk(q, 3, "read limit reached\n");
2824 return __vb2_cleanup_fileio(q);
2828 * Call vb2_qbuf and give buffer to the driver.
2830 b->planes[0].bytesused = buf->pos;
2833 b->timestamp = ktime_get_ns();
2834 ret = vb2_core_qbuf(q, index, NULL, NULL);
2835 dprintk(q, 5, "vb2_dbuf result: %d\n", ret);
2840 * Buffer has been queued, update the status
2844 buf->size = vb2_plane_size(q->bufs[index], 0);
2845 fileio->q_count += 1;
2847 * If we are queuing up buffers for the first time, then
2848 * increase initial_index by one.
2850 if (fileio->initial_index < q->num_buffers)
2851 fileio->initial_index++;
2853 * The next buffer to use is either a buffer that's going to be
2854 * queued for the first time (initial_index < q->num_buffers)
2855 * or it is equal to q->num_buffers, meaning that the next
2856 * time we need to dequeue a buffer since we've now queued up
2857 * all the 'first time' buffers.
2859 fileio->cur_index = fileio->initial_index;
2863 * Return proper number of bytes processed.
2870 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2871 loff_t *ppos, int nonblocking)
2873 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2875 EXPORT_SYMBOL_GPL(vb2_read);
2877 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
2878 loff_t *ppos, int nonblocking)
2880 return __vb2_perform_fileio(q, (char __user *) data, count,
2881 ppos, nonblocking, 0);
2883 EXPORT_SYMBOL_GPL(vb2_write);
2885 struct vb2_threadio_data {
2886 struct task_struct *thread;
2892 static int vb2_thread(void *data)
2894 struct vb2_queue *q = data;
2895 struct vb2_threadio_data *threadio = q->threadio;
2896 bool copy_timestamp = false;
2897 unsigned prequeue = 0;
2902 prequeue = q->num_buffers;
2903 copy_timestamp = q->copy_timestamp;
2909 struct vb2_buffer *vb;
2912 * Call vb2_dqbuf to get buffer back.
2915 vb = q->bufs[index++];
2918 call_void_qop(q, wait_finish, q);
2919 if (!threadio->stop)
2920 ret = vb2_core_dqbuf(q, &index, NULL, 0);
2921 call_void_qop(q, wait_prepare, q);
2922 dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret);
2924 vb = q->bufs[index];
2926 if (ret || threadio->stop)
2930 if (vb->state != VB2_BUF_STATE_ERROR)
2931 if (threadio->fnc(vb, threadio->priv))
2933 call_void_qop(q, wait_finish, q);
2935 vb->timestamp = ktime_get_ns();
2936 if (!threadio->stop)
2937 ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
2938 call_void_qop(q, wait_prepare, q);
2939 if (ret || threadio->stop)
2943 /* Hmm, linux becomes *very* unhappy without this ... */
2944 while (!kthread_should_stop()) {
2945 set_current_state(TASK_INTERRUPTIBLE);
2952 * This function should not be used for anything else but the videobuf2-dvb
2953 * support. If you think you have another good use-case for this, then please
2954 * contact the linux-media mailinglist first.
2956 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
2957 const char *thread_name)
2959 struct vb2_threadio_data *threadio;
2966 if (WARN_ON(q->fileio))
2969 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
2970 if (threadio == NULL)
2972 threadio->fnc = fnc;
2973 threadio->priv = priv;
2975 ret = __vb2_init_fileio(q, !q->is_output);
2976 dprintk(q, 3, "file io: vb2_init_fileio result: %d\n", ret);
2979 q->threadio = threadio;
2980 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
2981 if (IS_ERR(threadio->thread)) {
2982 ret = PTR_ERR(threadio->thread);
2983 threadio->thread = NULL;
2989 __vb2_cleanup_fileio(q);
2994 EXPORT_SYMBOL_GPL(vb2_thread_start);
2996 int vb2_thread_stop(struct vb2_queue *q)
2998 struct vb2_threadio_data *threadio = q->threadio;
3001 if (threadio == NULL)
3003 threadio->stop = true;
3004 /* Wake up all pending sleeps in the thread */
3006 err = kthread_stop(threadio->thread);
3007 __vb2_cleanup_fileio(q);
3008 threadio->thread = NULL;
3013 EXPORT_SYMBOL_GPL(vb2_thread_stop);
3015 MODULE_DESCRIPTION("Media buffer core framework");
3016 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
3017 MODULE_LICENSE("GPL");