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 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
27 #include <media/videobuf2-core.h>
29 #include <trace/events/vb2.h>
31 #include "videobuf2-internal.h"
34 EXPORT_SYMBOL_GPL(vb2_debug);
35 module_param_named(debug, vb2_debug, int, 0644);
37 static void __vb2_queue_cancel(struct vb2_queue *q);
38 static void __enqueue_in_driver(struct vb2_buffer *vb);
41 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
43 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
45 struct vb2_queue *q = vb->vb2_queue;
46 enum dma_data_direction dma_dir =
47 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
52 * Allocate memory for all planes in this buffer
53 * NOTE: mmapped areas should be page aligned
55 for (plane = 0; plane < vb->num_planes; ++plane) {
56 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
58 mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
59 size, dma_dir, q->gfp_flags);
60 if (IS_ERR_OR_NULL(mem_priv))
63 /* Associate allocator private data with this plane */
64 vb->planes[plane].mem_priv = mem_priv;
65 vb->planes[plane].length = q->plane_sizes[plane];
70 /* Free already allocated memory if one of the allocations failed */
71 for (; plane > 0; --plane) {
72 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
73 vb->planes[plane - 1].mem_priv = NULL;
80 * __vb2_buf_mem_free() - free memory of the given buffer
82 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
86 for (plane = 0; plane < vb->num_planes; ++plane) {
87 call_void_memop(vb, put, vb->planes[plane].mem_priv);
88 vb->planes[plane].mem_priv = NULL;
89 dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
94 * __vb2_buf_userptr_put() - release userspace memory associated with
97 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
101 for (plane = 0; plane < vb->num_planes; ++plane) {
102 if (vb->planes[plane].mem_priv)
103 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
104 vb->planes[plane].mem_priv = NULL;
109 * __vb2_plane_dmabuf_put() - release memory associated with
110 * a DMABUF shared plane
112 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
118 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
120 call_void_memop(vb, detach_dmabuf, p->mem_priv);
121 dma_buf_put(p->dbuf);
128 * __vb2_buf_dmabuf_put() - release memory associated with
129 * a DMABUF shared buffer
131 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
135 for (plane = 0; plane < vb->num_planes; ++plane)
136 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
140 * __setup_lengths() - setup initial lengths for every plane in
141 * every buffer on the queue
143 static void __setup_lengths(struct vb2_queue *q, unsigned int n)
145 unsigned int buffer, plane;
146 struct vb2_buffer *vb;
148 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
149 vb = q->bufs[buffer];
153 for (plane = 0; plane < vb->num_planes; ++plane)
154 vb->planes[plane].length = q->plane_sizes[plane];
159 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
160 * every buffer on the queue
162 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
164 unsigned int buffer, plane;
165 struct vb2_buffer *vb;
168 if (q->num_buffers) {
170 vb = q->bufs[q->num_buffers - 1];
171 p = &vb->planes[vb->num_planes - 1];
172 off = PAGE_ALIGN(p->m.offset + p->length);
177 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
178 vb = q->bufs[buffer];
182 for (plane = 0; plane < vb->num_planes; ++plane) {
183 vb->planes[plane].m.offset = off;
185 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
188 off += vb->planes[plane].length;
189 off = PAGE_ALIGN(off);
195 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
196 * video buffer memory for all buffers/planes on the queue and initializes the
199 * Returns the number of buffers successfully allocated.
201 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
202 unsigned int num_buffers, unsigned int num_planes)
205 struct vb2_buffer *vb;
208 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
209 num_buffers = min_t(unsigned int, num_buffers,
210 VB2_MAX_FRAME - q->num_buffers);
212 for (buffer = 0; buffer < num_buffers; ++buffer) {
213 /* Allocate videobuf buffer structures */
214 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
216 dprintk(1, "memory alloc for buffer struct failed\n");
220 vb->state = VB2_BUF_STATE_DEQUEUED;
222 vb->num_planes = num_planes;
223 vb->index = q->num_buffers + buffer;
227 /* Allocate video buffer memory for the MMAP type */
228 if (memory == VB2_MEMORY_MMAP) {
229 ret = __vb2_buf_mem_alloc(vb);
231 dprintk(1, "failed allocating memory for "
232 "buffer %d\n", buffer);
237 * Call the driver-provided buffer initialization
238 * callback, if given. An error in initialization
239 * results in queue setup failure.
241 ret = call_vb_qop(vb, buf_init, vb);
243 dprintk(1, "buffer %d %p initialization"
244 " failed\n", buffer, vb);
245 __vb2_buf_mem_free(vb);
251 q->bufs[q->num_buffers + buffer] = vb;
254 __setup_lengths(q, buffer);
255 if (memory == VB2_MEMORY_MMAP)
256 __setup_offsets(q, buffer);
258 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
265 * __vb2_free_mem() - release all video buffer memory for a given queue
267 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
270 struct vb2_buffer *vb;
272 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
274 vb = q->bufs[buffer];
278 /* Free MMAP buffers or release USERPTR buffers */
279 if (q->memory == VB2_MEMORY_MMAP)
280 __vb2_buf_mem_free(vb);
281 else if (q->memory == VB2_MEMORY_DMABUF)
282 __vb2_buf_dmabuf_put(vb);
284 __vb2_buf_userptr_put(vb);
289 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
290 * related information, if no buffers are left return the queue to an
291 * uninitialized state. Might be called even if the queue has already been freed.
293 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
298 * Sanity check: when preparing a buffer the queue lock is released for
299 * a short while (see __buf_prepare for the details), which would allow
300 * a race with a reqbufs which can call this function. Removing the
301 * buffers from underneath __buf_prepare is obviously a bad idea, so we
302 * check if any of the buffers is in the state PREPARING, and if so we
303 * just return -EAGAIN.
305 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
307 if (q->bufs[buffer] == NULL)
309 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
310 dprintk(1, "preparing buffers, cannot free\n");
315 /* Call driver-provided cleanup function for each buffer, if provided */
316 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
318 struct vb2_buffer *vb = q->bufs[buffer];
320 if (vb && vb->planes[0].mem_priv)
321 call_void_vb_qop(vb, buf_cleanup, vb);
324 /* Release video buffer memory */
325 __vb2_free_mem(q, buffers);
327 #ifdef CONFIG_VIDEO_ADV_DEBUG
329 * Check that all the calls were balances during the life-time of this
330 * queue. If not (or if the debug level is 1 or up), then dump the
331 * counters to the kernel log.
333 if (q->num_buffers) {
334 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
335 q->cnt_wait_prepare != q->cnt_wait_finish;
337 if (unbalanced || vb2_debug) {
338 pr_info("vb2: counters for queue %p:%s\n", q,
339 unbalanced ? " UNBALANCED!" : "");
340 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
341 q->cnt_queue_setup, q->cnt_start_streaming,
342 q->cnt_stop_streaming);
343 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
344 q->cnt_wait_prepare, q->cnt_wait_finish);
346 q->cnt_queue_setup = 0;
347 q->cnt_wait_prepare = 0;
348 q->cnt_wait_finish = 0;
349 q->cnt_start_streaming = 0;
350 q->cnt_stop_streaming = 0;
352 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
353 struct vb2_buffer *vb = q->bufs[buffer];
354 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
355 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
356 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
357 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
358 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
359 vb->cnt_buf_queue != vb->cnt_buf_done ||
360 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
361 vb->cnt_buf_init != vb->cnt_buf_cleanup;
363 if (unbalanced || vb2_debug) {
364 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
365 q, buffer, unbalanced ? " UNBALANCED!" : "");
366 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
367 vb->cnt_buf_init, vb->cnt_buf_cleanup,
368 vb->cnt_buf_prepare, vb->cnt_buf_finish);
369 pr_info("vb2: buf_queue: %u buf_done: %u\n",
370 vb->cnt_buf_queue, vb->cnt_buf_done);
371 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
372 vb->cnt_mem_alloc, vb->cnt_mem_put,
373 vb->cnt_mem_prepare, vb->cnt_mem_finish,
375 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
376 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
377 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
378 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
379 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
380 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
381 vb->cnt_mem_get_dmabuf,
382 vb->cnt_mem_num_users,
389 /* Free videobuf buffers */
390 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
392 kfree(q->bufs[buffer]);
393 q->bufs[buffer] = NULL;
396 q->num_buffers -= buffers;
397 if (!q->num_buffers) {
399 INIT_LIST_HEAD(&q->queued_list);
405 * vb2_buffer_in_use() - return true if the buffer is in use and
406 * the queue cannot be freed (by the means of REQBUFS(0)) call
408 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
411 for (plane = 0; plane < vb->num_planes; ++plane) {
412 void *mem_priv = vb->planes[plane].mem_priv;
414 * If num_users() has not been provided, call_memop
415 * will return 0, apparently nobody cares about this
416 * case anyway. If num_users() returns more than 1,
417 * we are not the only user of the plane's memory.
419 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
424 EXPORT_SYMBOL(vb2_buffer_in_use);
427 * __buffers_in_use() - return true if any buffers on the queue are in use and
428 * the queue cannot be freed (by the means of REQBUFS(0)) call
430 static bool __buffers_in_use(struct vb2_queue *q)
433 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
434 if (vb2_buffer_in_use(q, q->bufs[buffer]))
441 * vb2_core_querybuf() - query video buffer information
443 * @index: id number of the buffer
444 * @pb: buffer struct passed from userspace
446 * Should be called from vidioc_querybuf ioctl handler in driver.
447 * The passed buffer should have been verified.
448 * This function fills the relevant information for the userspace.
450 * The return values from this function are intended to be directly returned
451 * from vidioc_querybuf handler in driver.
453 int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
455 return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
457 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
460 * __verify_userptr_ops() - verify that all memory operations required for
461 * USERPTR queue type have been provided
463 static int __verify_userptr_ops(struct vb2_queue *q)
465 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
466 !q->mem_ops->put_userptr)
473 * __verify_mmap_ops() - verify that all memory operations required for
474 * MMAP queue type have been provided
476 static int __verify_mmap_ops(struct vb2_queue *q)
478 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
479 !q->mem_ops->put || !q->mem_ops->mmap)
486 * __verify_dmabuf_ops() - verify that all memory operations required for
487 * DMABUF queue type have been provided
489 static int __verify_dmabuf_ops(struct vb2_queue *q)
491 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
492 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
493 !q->mem_ops->unmap_dmabuf)
500 * vb2_verify_memory_type() - Check whether the memory type and buffer type
501 * passed to a buffer operation are compatible with the queue.
503 int vb2_verify_memory_type(struct vb2_queue *q,
504 enum vb2_memory memory, unsigned int type)
506 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
507 memory != VB2_MEMORY_DMABUF) {
508 dprintk(1, "unsupported memory type\n");
512 if (type != q->type) {
513 dprintk(1, "requested type is incorrect\n");
518 * Make sure all the required memory ops for given memory type
521 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
522 dprintk(1, "MMAP for current setup unsupported\n");
526 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
527 dprintk(1, "USERPTR for current setup unsupported\n");
531 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
532 dprintk(1, "DMABUF for current setup unsupported\n");
537 * Place the busy tests at the end: -EBUSY can be ignored when
538 * create_bufs is called with count == 0, but count == 0 should still
539 * do the memory and type validation.
541 if (vb2_fileio_is_active(q)) {
542 dprintk(1, "file io in progress\n");
547 EXPORT_SYMBOL(vb2_verify_memory_type);
550 * vb2_core_reqbufs() - Initiate streaming
551 * @q: videobuf2 queue
552 * @memory: memory type
553 * @count: requested buffer count
555 * Should be called from vidioc_reqbufs ioctl handler of a driver.
557 * 1) verifies streaming parameters passed from the userspace,
558 * 2) sets up the queue,
559 * 3) negotiates number of buffers and planes per buffer with the driver
560 * to be used during streaming,
561 * 4) allocates internal buffer structures (struct vb2_buffer), according to
562 * the agreed parameters,
563 * 5) for MMAP memory type, allocates actual video memory, using the
564 * memory handling/allocation routines provided during queue initialization
566 * If req->count is 0, all the memory will be freed instead.
567 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
568 * and the queue is not busy, memory will be reallocated.
570 * The return values from this function are intended to be directly returned
571 * from vidioc_reqbufs handler in driver.
573 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
576 unsigned int num_buffers, allocated_buffers, num_planes = 0;
580 dprintk(1, "streaming active\n");
584 if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
586 * We already have buffers allocated, so first check if they
587 * are not in use and can be freed.
589 mutex_lock(&q->mmap_lock);
590 if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
591 mutex_unlock(&q->mmap_lock);
592 dprintk(1, "memory in use, cannot free\n");
597 * Call queue_cancel to clean up any buffers in the PREPARED or
598 * QUEUED state which is possible if buffers were prepared or
599 * queued without ever calling STREAMON.
601 __vb2_queue_cancel(q);
602 ret = __vb2_queue_free(q, q->num_buffers);
603 mutex_unlock(&q->mmap_lock);
608 * In case of REQBUFS(0) return immediately without calling
609 * driver's queue_setup() callback and allocating resources.
616 * Make sure the requested values and current defaults are sane.
618 num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
619 num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
620 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
621 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
625 * Ask the driver how many buffers and planes per buffer it requires.
626 * Driver also sets the size and allocator context for each plane.
628 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
629 q->plane_sizes, q->alloc_ctx);
633 /* Finally, allocate buffers and video memory */
635 __vb2_queue_alloc(q, memory, num_buffers, num_planes);
636 if (allocated_buffers == 0) {
637 dprintk(1, "memory allocation failed\n");
642 * There is no point in continuing if we can't allocate the minimum
643 * number of buffers needed by this vb2_queue.
645 if (allocated_buffers < q->min_buffers_needed)
649 * Check if driver can handle the allocated number of buffers.
651 if (!ret && allocated_buffers < num_buffers) {
652 num_buffers = allocated_buffers;
654 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
655 &num_planes, q->plane_sizes, q->alloc_ctx);
657 if (!ret && allocated_buffers < num_buffers)
661 * Either the driver has accepted a smaller number of buffers,
662 * or .queue_setup() returned an error
666 mutex_lock(&q->mmap_lock);
667 q->num_buffers = allocated_buffers;
671 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
672 * from q->num_buffers.
674 __vb2_queue_free(q, allocated_buffers);
675 mutex_unlock(&q->mmap_lock);
678 mutex_unlock(&q->mmap_lock);
681 * Return the number of successfully allocated buffers
684 *count = allocated_buffers;
685 q->waiting_for_buffers = !q->is_output;
689 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
692 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
693 * @q: videobuf2 queue
694 * @memory: memory type
695 * @count: requested buffer count
696 * @parg: parameter passed to device driver
698 * Should be called from vidioc_create_bufs ioctl handler of a driver.
700 * 1) verifies parameter sanity
701 * 2) calls the .queue_setup() queue operation
702 * 3) performs any necessary memory allocations
704 * The return values from this function are intended to be directly returned
705 * from vidioc_create_bufs handler in driver.
707 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
708 unsigned int *count, const void *parg)
710 unsigned int num_planes = 0, num_buffers, allocated_buffers;
713 if (q->num_buffers == VB2_MAX_FRAME) {
714 dprintk(1, "maximum number of buffers already allocated\n");
718 if (!q->num_buffers) {
719 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
720 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
722 q->waiting_for_buffers = !q->is_output;
725 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
728 * Ask the driver, whether the requested number of buffers, planes per
729 * buffer and their sizes are acceptable
731 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
732 &num_planes, q->plane_sizes, q->alloc_ctx);
736 /* Finally, allocate buffers and video memory */
737 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
739 if (allocated_buffers == 0) {
740 dprintk(1, "memory allocation failed\n");
745 * Check if driver can handle the so far allocated number of buffers.
747 if (allocated_buffers < num_buffers) {
748 num_buffers = allocated_buffers;
751 * q->num_buffers contains the total number of buffers, that the
752 * queue driver has set up
754 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
755 &num_planes, q->plane_sizes, q->alloc_ctx);
757 if (!ret && allocated_buffers < num_buffers)
761 * Either the driver has accepted a smaller number of buffers,
762 * or .queue_setup() returned an error
766 mutex_lock(&q->mmap_lock);
767 q->num_buffers += allocated_buffers;
771 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
772 * from q->num_buffers.
774 __vb2_queue_free(q, allocated_buffers);
775 mutex_unlock(&q->mmap_lock);
778 mutex_unlock(&q->mmap_lock);
781 * Return the number of successfully allocated buffers
784 *count = allocated_buffers;
788 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
791 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
792 * @vb: vb2_buffer to which the plane in question belongs to
793 * @plane_no: plane number for which the address is to be returned
795 * This function returns a kernel virtual address of a given plane if
796 * such a mapping exist, NULL otherwise.
798 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
800 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
803 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
806 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
809 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
810 * @vb: vb2_buffer to which the plane in question belongs to
811 * @plane_no: plane number for which the cookie is to be returned
813 * This function returns an allocator specific cookie for a given plane if
814 * available, NULL otherwise. The allocator should provide some simple static
815 * inline function, which would convert this cookie to the allocator specific
816 * type that can be used directly by the driver to access the buffer. This can
817 * be for example physical address, pointer to scatter list or IOMMU mapping.
819 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
821 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
824 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
826 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
829 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
830 * @vb: vb2_buffer returned from the driver
831 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
832 * VB2_BUF_STATE_ERROR if the operation finished with an error or
833 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
834 * If start_streaming fails then it should return buffers with state
835 * VB2_BUF_STATE_QUEUED to put them back into the queue.
837 * This function should be called by the driver after a hardware operation on
838 * a buffer is finished and the buffer may be returned to userspace. The driver
839 * cannot use this buffer anymore until it is queued back to it by videobuf
840 * by the means of buf_queue callback. Only buffers previously queued to the
841 * driver by buf_queue can be passed to this function.
843 * While streaming a buffer can only be returned in state DONE or ERROR.
844 * The start_streaming op can also return them in case the DMA engine cannot
845 * be started for some reason. In that case the buffers should be returned with
848 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
850 struct vb2_queue *q = vb->vb2_queue;
854 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
857 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
858 state != VB2_BUF_STATE_ERROR &&
859 state != VB2_BUF_STATE_QUEUED &&
860 state != VB2_BUF_STATE_REQUEUEING))
861 state = VB2_BUF_STATE_ERROR;
863 #ifdef CONFIG_VIDEO_ADV_DEBUG
865 * Although this is not a callback, it still does have to balance
866 * with the buf_queue op. So update this counter manually.
870 dprintk(4, "done processing on buffer %d, state: %d\n",
874 for (plane = 0; plane < vb->num_planes; ++plane)
875 call_void_memop(vb, finish, vb->planes[plane].mem_priv);
877 spin_lock_irqsave(&q->done_lock, flags);
878 if (state == VB2_BUF_STATE_QUEUED ||
879 state == VB2_BUF_STATE_REQUEUEING) {
880 vb->state = VB2_BUF_STATE_QUEUED;
882 /* Add the buffer to the done buffers list */
883 list_add_tail(&vb->done_entry, &q->done_list);
886 atomic_dec(&q->owned_by_drv_count);
887 spin_unlock_irqrestore(&q->done_lock, flags);
889 trace_vb2_buf_done(q, vb);
892 case VB2_BUF_STATE_QUEUED:
894 case VB2_BUF_STATE_REQUEUEING:
895 if (q->start_streaming_called)
896 __enqueue_in_driver(vb);
899 /* Inform any processes that may be waiting for buffers */
900 wake_up(&q->done_wq);
904 EXPORT_SYMBOL_GPL(vb2_buffer_done);
907 * vb2_discard_done() - discard all buffers marked as DONE
908 * @q: videobuf2 queue
910 * This function is intended to be used with suspend/resume operations. It
911 * discards all 'done' buffers as they would be too old to be requested after
914 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
915 * delayed works before calling this function to make sure no buffer will be
916 * touched by the driver and/or hardware.
918 void vb2_discard_done(struct vb2_queue *q)
920 struct vb2_buffer *vb;
923 spin_lock_irqsave(&q->done_lock, flags);
924 list_for_each_entry(vb, &q->done_list, done_entry)
925 vb->state = VB2_BUF_STATE_ERROR;
926 spin_unlock_irqrestore(&q->done_lock, flags);
928 EXPORT_SYMBOL_GPL(vb2_discard_done);
931 * __qbuf_mmap() - handle qbuf of an MMAP buffer
933 static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
935 int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
937 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
941 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
943 static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
945 struct vb2_plane planes[VB2_MAX_PLANES];
946 struct vb2_queue *q = vb->vb2_queue;
950 enum dma_data_direction dma_dir =
951 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
952 bool reacquired = vb->planes[0].mem_priv == NULL;
954 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
955 /* Copy relevant information provided by the userspace */
956 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
960 for (plane = 0; plane < vb->num_planes; ++plane) {
961 /* Skip the plane if already verified */
962 if (vb->planes[plane].m.userptr &&
963 vb->planes[plane].m.userptr == planes[plane].m.userptr
964 && vb->planes[plane].length == planes[plane].length)
967 dprintk(3, "userspace address for plane %d changed, "
968 "reacquiring memory\n", plane);
970 /* Check if the provided plane buffer is large enough */
971 if (planes[plane].length < q->plane_sizes[plane]) {
972 dprintk(1, "provided buffer size %u is less than "
973 "setup size %u for plane %d\n",
974 planes[plane].length,
975 q->plane_sizes[plane], plane);
980 /* Release previously acquired memory if present */
981 if (vb->planes[plane].mem_priv) {
984 call_void_vb_qop(vb, buf_cleanup, vb);
986 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
989 vb->planes[plane].mem_priv = NULL;
990 vb->planes[plane].bytesused = 0;
991 vb->planes[plane].length = 0;
992 vb->planes[plane].m.userptr = 0;
993 vb->planes[plane].data_offset = 0;
995 /* Acquire each plane's memory */
996 mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
997 planes[plane].m.userptr,
998 planes[plane].length, dma_dir);
999 if (IS_ERR_OR_NULL(mem_priv)) {
1000 dprintk(1, "failed acquiring userspace "
1001 "memory for plane %d\n", plane);
1002 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1005 vb->planes[plane].mem_priv = mem_priv;
1009 * Now that everything is in order, copy relevant information
1010 * provided by userspace.
1012 for (plane = 0; plane < vb->num_planes; ++plane) {
1013 vb->planes[plane].bytesused = planes[plane].bytesused;
1014 vb->planes[plane].length = planes[plane].length;
1015 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1016 vb->planes[plane].data_offset = planes[plane].data_offset;
1021 * One or more planes changed, so we must call buf_init to do
1022 * the driver-specific initialization on the newly acquired
1023 * buffer, if provided.
1025 ret = call_vb_qop(vb, buf_init, vb);
1027 dprintk(1, "buffer initialization failed\n");
1032 ret = call_vb_qop(vb, buf_prepare, vb);
1034 dprintk(1, "buffer preparation failed\n");
1035 call_void_vb_qop(vb, buf_cleanup, vb);
1041 /* In case of errors, release planes that were already acquired */
1042 for (plane = 0; plane < vb->num_planes; ++plane) {
1043 if (vb->planes[plane].mem_priv)
1044 call_void_memop(vb, put_userptr,
1045 vb->planes[plane].mem_priv);
1046 vb->planes[plane].mem_priv = NULL;
1047 vb->planes[plane].m.userptr = 0;
1048 vb->planes[plane].length = 0;
1055 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1057 static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
1059 struct vb2_plane planes[VB2_MAX_PLANES];
1060 struct vb2_queue *q = vb->vb2_queue;
1064 enum dma_data_direction dma_dir =
1065 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1066 bool reacquired = vb->planes[0].mem_priv == NULL;
1068 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1069 /* Copy relevant information provided by the userspace */
1070 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
1074 for (plane = 0; plane < vb->num_planes; ++plane) {
1075 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1077 if (IS_ERR_OR_NULL(dbuf)) {
1078 dprintk(1, "invalid dmabuf fd for plane %d\n",
1084 /* use DMABUF size if length is not provided */
1085 if (planes[plane].length == 0)
1086 planes[plane].length = dbuf->size;
1088 if (planes[plane].length < q->plane_sizes[plane]) {
1089 dprintk(1, "invalid dmabuf length for plane %d\n",
1095 /* Skip the plane if already verified */
1096 if (dbuf == vb->planes[plane].dbuf &&
1097 vb->planes[plane].length == planes[plane].length) {
1102 dprintk(1, "buffer for plane %d changed\n", plane);
1106 call_void_vb_qop(vb, buf_cleanup, vb);
1109 /* Release previously acquired memory if present */
1110 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1111 vb->planes[plane].bytesused = 0;
1112 vb->planes[plane].length = 0;
1113 vb->planes[plane].m.fd = 0;
1114 vb->planes[plane].data_offset = 0;
1116 /* Acquire each plane's memory */
1117 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1118 q->alloc_ctx[plane], dbuf, planes[plane].length,
1120 if (IS_ERR(mem_priv)) {
1121 dprintk(1, "failed to attach dmabuf\n");
1122 ret = PTR_ERR(mem_priv);
1127 vb->planes[plane].dbuf = dbuf;
1128 vb->planes[plane].mem_priv = mem_priv;
1131 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1132 * really we want to do this just before the DMA, not while queueing
1135 for (plane = 0; plane < vb->num_planes; ++plane) {
1136 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1138 dprintk(1, "failed to map dmabuf for plane %d\n",
1142 vb->planes[plane].dbuf_mapped = 1;
1146 * Now that everything is in order, copy relevant information
1147 * provided by userspace.
1149 for (plane = 0; plane < vb->num_planes; ++plane) {
1150 vb->planes[plane].bytesused = planes[plane].bytesused;
1151 vb->planes[plane].length = planes[plane].length;
1152 vb->planes[plane].m.fd = planes[plane].m.fd;
1153 vb->planes[plane].data_offset = planes[plane].data_offset;
1158 * Call driver-specific initialization on the newly acquired buffer,
1161 ret = call_vb_qop(vb, buf_init, vb);
1163 dprintk(1, "buffer initialization failed\n");
1168 ret = call_vb_qop(vb, buf_prepare, vb);
1170 dprintk(1, "buffer preparation failed\n");
1171 call_void_vb_qop(vb, buf_cleanup, vb);
1177 /* In case of errors, release planes that were already acquired */
1178 __vb2_buf_dmabuf_put(vb);
1184 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1186 static void __enqueue_in_driver(struct vb2_buffer *vb)
1188 struct vb2_queue *q = vb->vb2_queue;
1191 vb->state = VB2_BUF_STATE_ACTIVE;
1192 atomic_inc(&q->owned_by_drv_count);
1194 trace_vb2_buf_queue(q, vb);
1197 for (plane = 0; plane < vb->num_planes; ++plane)
1198 call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
1200 call_void_vb_qop(vb, buf_queue, vb);
1203 static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1205 struct vb2_queue *q = vb->vb2_queue;
1209 dprintk(1, "fatal error occurred on queue\n");
1213 vb->state = VB2_BUF_STATE_PREPARING;
1215 switch (q->memory) {
1216 case VB2_MEMORY_MMAP:
1217 ret = __qbuf_mmap(vb, pb);
1219 case VB2_MEMORY_USERPTR:
1220 ret = __qbuf_userptr(vb, pb);
1222 case VB2_MEMORY_DMABUF:
1223 ret = __qbuf_dmabuf(vb, pb);
1226 WARN(1, "Invalid queue type\n");
1231 dprintk(1, "buffer preparation failed: %d\n", ret);
1232 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
1238 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1240 * @q: videobuf2 queue
1241 * @index: id number of the buffer
1242 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1245 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1246 * The passed buffer should have been verified.
1247 * This function calls buf_prepare callback in the driver (if provided),
1248 * in which driver-specific buffer initialization can be performed,
1250 * The return values from this function are intended to be directly returned
1251 * from vidioc_prepare_buf handler in driver.
1253 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1255 struct vb2_buffer *vb;
1258 vb = q->bufs[index];
1259 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1260 dprintk(1, "invalid buffer state %d\n",
1265 ret = __buf_prepare(vb, pb);
1269 /* Fill buffer information for the userspace */
1270 ret = call_bufop(q, fill_user_buffer, vb, pb);
1274 dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
1278 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1281 * vb2_start_streaming() - Attempt to start streaming.
1282 * @q: videobuf2 queue
1284 * Attempt to start streaming. When this function is called there must be
1285 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1286 * number of buffers required for the DMA engine to function). If the
1287 * @start_streaming op fails it is supposed to return all the driver-owned
1288 * buffers back to vb2 in state QUEUED. Check if that happened and if
1289 * not warn and reclaim them forcefully.
1291 static int vb2_start_streaming(struct vb2_queue *q)
1293 struct vb2_buffer *vb;
1297 * If any buffers were queued before streamon,
1298 * we can now pass them to driver for processing.
1300 list_for_each_entry(vb, &q->queued_list, queued_entry)
1301 __enqueue_in_driver(vb);
1303 /* Tell the driver to start streaming */
1304 q->start_streaming_called = 1;
1305 ret = call_qop(q, start_streaming, q,
1306 atomic_read(&q->owned_by_drv_count));
1310 q->start_streaming_called = 0;
1312 dprintk(1, "driver refused to start streaming\n");
1314 * If you see this warning, then the driver isn't cleaning up properly
1315 * after a failed start_streaming(). See the start_streaming()
1316 * documentation in videobuf2-core.h for more information how buffers
1317 * should be returned to vb2 in start_streaming().
1319 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1323 * Forcefully reclaim buffers if the driver did not
1324 * correctly return them to vb2.
1326 for (i = 0; i < q->num_buffers; ++i) {
1328 if (vb->state == VB2_BUF_STATE_ACTIVE)
1329 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1331 /* Must be zero now */
1332 WARN_ON(atomic_read(&q->owned_by_drv_count));
1335 * If done_list is not empty, then start_streaming() didn't call
1336 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1339 WARN_ON(!list_empty(&q->done_list));
1344 * vb2_core_qbuf() - Queue a buffer from userspace
1345 * @q: videobuf2 queue
1346 * @index: id number of the buffer
1347 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1350 * Should be called from vidioc_qbuf ioctl handler of a driver.
1351 * The passed buffer should have been verified.
1353 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1354 * which driver-specific buffer initialization can be performed,
1355 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1356 * callback for processing.
1358 * The return values from this function are intended to be directly returned
1359 * from vidioc_qbuf handler in driver.
1361 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1363 struct vb2_buffer *vb;
1364 enum vb2_buffer_state orig_state;
1368 dprintk(1, "fatal error occurred on queue\n");
1372 vb = q->bufs[index];
1374 switch (vb->state) {
1375 case VB2_BUF_STATE_DEQUEUED:
1376 ret = __buf_prepare(vb, pb);
1380 case VB2_BUF_STATE_PREPARED:
1382 case VB2_BUF_STATE_PREPARING:
1383 dprintk(1, "buffer still being prepared\n");
1386 dprintk(1, "invalid buffer state %d\n", vb->state);
1391 * Add to the queued buffers list, a buffer will stay on it until
1392 * dequeued in dqbuf.
1394 orig_state = vb->state;
1395 list_add_tail(&vb->queued_entry, &q->queued_list);
1397 q->waiting_for_buffers = false;
1398 vb->state = VB2_BUF_STATE_QUEUED;
1400 call_bufop(q, set_timestamp, vb, pb);
1402 trace_vb2_qbuf(q, vb);
1405 * If already streaming, give the buffer to driver for processing.
1406 * If not, the buffer will be given to driver on next streamon.
1408 if (q->start_streaming_called)
1409 __enqueue_in_driver(vb);
1411 /* Fill buffer information for the userspace */
1412 ret = call_bufop(q, fill_user_buffer, vb, pb);
1417 * If streamon has been called, and we haven't yet called
1418 * start_streaming() since not enough buffers were queued, and
1419 * we now have reached the minimum number of queued buffers,
1420 * then we can finally call start_streaming().
1422 if (q->streaming && !q->start_streaming_called &&
1423 q->queued_count >= q->min_buffers_needed) {
1424 ret = vb2_start_streaming(q);
1427 * Since vb2_core_qbuf will return with an error,
1428 * we should return it to state DEQUEUED since
1429 * the error indicates that the buffer wasn't queued.
1431 list_del(&vb->queued_entry);
1433 vb->state = orig_state;
1438 dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
1441 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1444 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1447 * Will sleep if required for nonblocking == false.
1449 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1452 * All operations on vb_done_list are performed under done_lock
1453 * spinlock protection. However, buffers may be removed from
1454 * it and returned to userspace only while holding both driver's
1455 * lock and the done_lock spinlock. Thus we can be sure that as
1456 * long as we hold the driver's lock, the list will remain not
1457 * empty if list_empty() check succeeds.
1463 if (!q->streaming) {
1464 dprintk(1, "streaming off, will not wait for buffers\n");
1469 dprintk(1, "Queue in error state, will not wait for buffers\n");
1473 if (q->last_buffer_dequeued) {
1474 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1478 if (!list_empty(&q->done_list)) {
1480 * Found a buffer that we were waiting for.
1486 dprintk(1, "nonblocking and no buffers to dequeue, "
1492 * We are streaming and blocking, wait for another buffer to
1493 * become ready or for streamoff. Driver's lock is released to
1494 * allow streamoff or qbuf to be called while waiting.
1496 call_void_qop(q, wait_prepare, q);
1499 * All locks have been released, it is safe to sleep now.
1501 dprintk(3, "will sleep waiting for buffers\n");
1502 ret = wait_event_interruptible(q->done_wq,
1503 !list_empty(&q->done_list) || !q->streaming ||
1507 * We need to reevaluate both conditions again after reacquiring
1508 * the locks or return an error if one occurred.
1510 call_void_qop(q, wait_finish, q);
1512 dprintk(1, "sleep was interrupted\n");
1520 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1522 * Will sleep if required for nonblocking == false.
1524 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1525 void *pb, int nonblocking)
1527 unsigned long flags;
1531 * Wait for at least one buffer to become available on the done_list.
1533 ret = __vb2_wait_for_done_vb(q, nonblocking);
1538 * Driver's lock has been held since we last verified that done_list
1539 * is not empty, so no need for another list_empty(done_list) check.
1541 spin_lock_irqsave(&q->done_lock, flags);
1542 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1544 * Only remove the buffer from done_list if all planes can be
1545 * handled. Some cases such as V4L2 file I/O and DVB have pb
1546 * == NULL; skip the check then as there's nothing to verify.
1549 ret = call_bufop(q, verify_planes_array, *vb, pb);
1551 list_del(&(*vb)->done_entry);
1552 spin_unlock_irqrestore(&q->done_lock, flags);
1558 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1559 * @q: videobuf2 queue
1561 * This function will wait until all buffers that have been given to the driver
1562 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1563 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1564 * taken, for example from stop_streaming() callback.
1566 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1568 if (!q->streaming) {
1569 dprintk(1, "streaming off, will not wait for buffers\n");
1573 if (q->start_streaming_called)
1574 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1577 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1580 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1582 static void __vb2_dqbuf(struct vb2_buffer *vb)
1584 struct vb2_queue *q = vb->vb2_queue;
1587 /* nothing to do if the buffer is already dequeued */
1588 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1591 vb->state = VB2_BUF_STATE_DEQUEUED;
1593 /* unmap DMABUF buffer */
1594 if (q->memory == VB2_MEMORY_DMABUF)
1595 for (i = 0; i < vb->num_planes; ++i) {
1596 if (!vb->planes[i].dbuf_mapped)
1598 call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1599 vb->planes[i].dbuf_mapped = 0;
1604 * vb2_dqbuf() - Dequeue a buffer to the userspace
1605 * @q: videobuf2 queue
1606 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1608 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1609 * buffers ready for dequeuing are present. Normally the driver
1610 * would be passing (file->f_flags & O_NONBLOCK) here
1612 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1613 * The passed buffer should have been verified.
1615 * 1) calls buf_finish callback in the driver (if provided), in which
1616 * driver can perform any additional operations that may be required before
1617 * returning the buffer to userspace, such as cache sync,
1618 * 2) the buffer struct members are filled with relevant information for
1621 * The return values from this function are intended to be directly returned
1622 * from vidioc_dqbuf handler in driver.
1624 int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
1626 struct vb2_buffer *vb = NULL;
1629 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1633 switch (vb->state) {
1634 case VB2_BUF_STATE_DONE:
1635 dprintk(3, "returning done buffer\n");
1637 case VB2_BUF_STATE_ERROR:
1638 dprintk(3, "returning done buffer with errors\n");
1641 dprintk(1, "invalid buffer state\n");
1645 call_void_vb_qop(vb, buf_finish, vb);
1647 /* Fill buffer information for the userspace */
1648 ret = call_bufop(q, fill_user_buffer, vb, pb);
1652 /* Remove from videobuf queue */
1653 list_del(&vb->queued_entry);
1656 trace_vb2_dqbuf(q, vb);
1658 /* go back to dequeued state */
1661 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1662 vb->index, vb->state);
1667 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1670 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1672 * Removes all queued buffers from driver's queue and all buffers queued by
1673 * userspace from videobuf's queue. Returns to state after reqbufs.
1675 static void __vb2_queue_cancel(struct vb2_queue *q)
1680 * Tell driver to stop all transactions and release all queued
1683 if (q->start_streaming_called)
1684 call_void_qop(q, stop_streaming, q);
1687 * If you see this warning, then the driver isn't cleaning up properly
1688 * in stop_streaming(). See the stop_streaming() documentation in
1689 * videobuf2-core.h for more information how buffers should be returned
1690 * to vb2 in stop_streaming().
1692 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1693 for (i = 0; i < q->num_buffers; ++i)
1694 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
1695 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1696 /* Must be zero now */
1697 WARN_ON(atomic_read(&q->owned_by_drv_count));
1701 q->start_streaming_called = 0;
1702 q->queued_count = 0;
1706 * Remove all buffers from videobuf's list...
1708 INIT_LIST_HEAD(&q->queued_list);
1710 * ...and done list; userspace will not receive any buffers it
1711 * has not already dequeued before initiating cancel.
1713 INIT_LIST_HEAD(&q->done_list);
1714 atomic_set(&q->owned_by_drv_count, 0);
1715 wake_up_all(&q->done_wq);
1718 * Reinitialize all buffers for next use.
1719 * Make sure to call buf_finish for any queued buffers. Normally
1720 * that's done in dqbuf, but that's not going to happen when we
1721 * cancel the whole queue. Note: this code belongs here, not in
1722 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1723 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1724 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1726 for (i = 0; i < q->num_buffers; ++i) {
1727 struct vb2_buffer *vb = q->bufs[i];
1729 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1730 vb->state = VB2_BUF_STATE_PREPARED;
1731 call_void_vb_qop(vb, buf_finish, vb);
1737 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1741 if (type != q->type) {
1742 dprintk(1, "invalid stream type\n");
1747 dprintk(3, "already streaming\n");
1751 if (!q->num_buffers) {
1752 dprintk(1, "no buffers have been allocated\n");
1756 if (q->num_buffers < q->min_buffers_needed) {
1757 dprintk(1, "need at least %u allocated buffers\n",
1758 q->min_buffers_needed);
1763 * Tell driver to start streaming provided sufficient buffers
1766 if (q->queued_count >= q->min_buffers_needed) {
1767 ret = vb2_start_streaming(q);
1769 __vb2_queue_cancel(q);
1776 dprintk(3, "successful\n");
1779 EXPORT_SYMBOL_GPL(vb2_core_streamon);
1782 * vb2_queue_error() - signal a fatal error on the queue
1783 * @q: videobuf2 queue
1785 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1786 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1787 * buffers will return -EIO.
1789 * The error flag will be cleared when cancelling the queue, either from
1790 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1791 * function before starting the stream, otherwise the error flag will remain set
1792 * until the queue is released when closing the device node.
1794 void vb2_queue_error(struct vb2_queue *q)
1798 wake_up_all(&q->done_wq);
1800 EXPORT_SYMBOL_GPL(vb2_queue_error);
1802 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
1804 if (type != q->type) {
1805 dprintk(1, "invalid stream type\n");
1810 * Cancel will pause streaming and remove all buffers from the driver
1811 * and videobuf, effectively returning control over them to userspace.
1813 * Note that we do this even if q->streaming == 0: if you prepare or
1814 * queue buffers, and then call streamoff without ever having called
1815 * streamon, you would still expect those buffers to be returned to
1816 * their normal dequeued state.
1818 __vb2_queue_cancel(q);
1819 q->waiting_for_buffers = !q->is_output;
1820 q->last_buffer_dequeued = false;
1822 dprintk(3, "successful\n");
1825 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
1828 * __find_plane_by_offset() - find plane associated with the given offset off
1830 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1831 unsigned int *_buffer, unsigned int *_plane)
1833 struct vb2_buffer *vb;
1834 unsigned int buffer, plane;
1837 * Go over all buffers and their planes, comparing the given offset
1838 * with an offset assigned to each plane. If a match is found,
1839 * return its buffer and plane numbers.
1841 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1842 vb = q->bufs[buffer];
1844 for (plane = 0; plane < vb->num_planes; ++plane) {
1845 if (vb->planes[plane].m.offset == off) {
1857 * vb2_core_expbuf() - Export a buffer as a file descriptor
1858 * @q: videobuf2 queue
1859 * @fd: file descriptor associated with DMABUF (set by driver) *
1860 * @type: buffer type
1861 * @index: id number of the buffer
1862 * @plane: index of the plane to be exported, 0 for single plane queues
1863 * @flags: flags for newly created file, currently only O_CLOEXEC is
1864 * supported, refer to manual of open syscall for more details
1866 * The return values from this function are intended to be directly returned
1867 * from vidioc_expbuf handler in driver.
1869 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
1870 unsigned int index, unsigned int plane, unsigned int flags)
1872 struct vb2_buffer *vb = NULL;
1873 struct vb2_plane *vb_plane;
1875 struct dma_buf *dbuf;
1877 if (q->memory != VB2_MEMORY_MMAP) {
1878 dprintk(1, "queue is not currently set up for mmap\n");
1882 if (!q->mem_ops->get_dmabuf) {
1883 dprintk(1, "queue does not support DMA buffer exporting\n");
1887 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
1888 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
1892 if (type != q->type) {
1893 dprintk(1, "invalid buffer type\n");
1897 if (index >= q->num_buffers) {
1898 dprintk(1, "buffer index out of range\n");
1902 vb = q->bufs[index];
1904 if (plane >= vb->num_planes) {
1905 dprintk(1, "buffer plane out of range\n");
1909 if (vb2_fileio_is_active(q)) {
1910 dprintk(1, "expbuf: file io in progress\n");
1914 vb_plane = &vb->planes[plane];
1916 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
1918 if (IS_ERR_OR_NULL(dbuf)) {
1919 dprintk(1, "failed to export buffer %d, plane %d\n",
1924 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
1926 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1932 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1938 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
1941 * vb2_mmap() - map video buffers into application address space
1942 * @q: videobuf2 queue
1943 * @vma: vma passed to the mmap file operation handler in the driver
1945 * Should be called from mmap file operation handler of a driver.
1946 * This function maps one plane of one of the available video buffers to
1947 * userspace. To map whole video memory allocated on reqbufs, this function
1948 * has to be called once per each plane per each buffer previously allocated.
1950 * When the userspace application calls mmap, it passes to it an offset returned
1951 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1952 * a "cookie", which is then used to identify the plane to be mapped.
1953 * This function finds a plane with a matching offset and a mapping is performed
1954 * by the means of a provided memory operation.
1956 * The return values from this function are intended to be directly returned
1957 * from the mmap handler in driver.
1959 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1961 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1962 struct vb2_buffer *vb;
1963 unsigned int buffer = 0, plane = 0;
1965 unsigned long length;
1967 if (q->memory != VB2_MEMORY_MMAP) {
1968 dprintk(1, "queue is not currently set up for mmap\n");
1973 * Check memory area access mode.
1975 if (!(vma->vm_flags & VM_SHARED)) {
1976 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
1980 if (!(vma->vm_flags & VM_WRITE)) {
1981 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
1985 if (!(vma->vm_flags & VM_READ)) {
1986 dprintk(1, "invalid vma flags, VM_READ needed\n");
1991 mutex_lock(&q->mmap_lock);
1993 if (vb2_fileio_is_active(q)) {
1994 dprintk(1, "mmap: file io in progress\n");
2000 * Find the plane corresponding to the offset passed by userspace.
2002 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2006 vb = q->bufs[buffer];
2009 * MMAP requires page_aligned buffers.
2010 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2011 * so, we need to do the same here.
2013 length = PAGE_ALIGN(vb->planes[plane].length);
2014 if (length < (vma->vm_end - vma->vm_start)) {
2016 "MMAP invalid, as it would overflow buffer length\n");
2021 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2024 mutex_unlock(&q->mmap_lock);
2028 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2031 EXPORT_SYMBOL_GPL(vb2_mmap);
2034 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2037 unsigned long pgoff,
2038 unsigned long flags)
2040 unsigned long off = pgoff << PAGE_SHIFT;
2041 struct vb2_buffer *vb;
2042 unsigned int buffer, plane;
2046 if (q->memory != VB2_MEMORY_MMAP) {
2047 dprintk(1, "queue is not currently set up for mmap\n");
2052 * Find the plane corresponding to the offset passed by userspace.
2054 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2058 vb = q->bufs[buffer];
2060 vaddr = vb2_plane_vaddr(vb, plane);
2061 return vaddr ? (unsigned long)vaddr : -EINVAL;
2063 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2067 * vb2_core_queue_init() - initialize a videobuf2 queue
2068 * @q: videobuf2 queue; this structure should be allocated in driver
2070 * The vb2_queue structure should be allocated by the driver. The driver is
2071 * responsible of clearing it's content and setting initial values for some
2072 * required entries before calling this function.
2073 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2074 * to the struct vb2_queue description in include/media/videobuf2-core.h
2075 * for more information.
2077 int vb2_core_queue_init(struct vb2_queue *q)
2084 WARN_ON(!q->mem_ops) ||
2085 WARN_ON(!q->type) ||
2086 WARN_ON(!q->io_modes) ||
2087 WARN_ON(!q->ops->queue_setup) ||
2088 WARN_ON(!q->ops->buf_queue))
2091 INIT_LIST_HEAD(&q->queued_list);
2092 INIT_LIST_HEAD(&q->done_list);
2093 spin_lock_init(&q->done_lock);
2094 mutex_init(&q->mmap_lock);
2095 init_waitqueue_head(&q->done_wq);
2097 if (q->buf_struct_size == 0)
2098 q->buf_struct_size = sizeof(struct vb2_buffer);
2102 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2105 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2106 * @q: videobuf2 queue
2108 * This function stops streaming and performs necessary clean ups, including
2109 * freeing video buffer memory. The driver is responsible for freeing
2110 * the vb2_queue structure itself.
2112 void vb2_core_queue_release(struct vb2_queue *q)
2114 __vb2_queue_cancel(q);
2115 mutex_lock(&q->mmap_lock);
2116 __vb2_queue_free(q, q->num_buffers);
2117 mutex_unlock(&q->mmap_lock);
2119 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2121 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2122 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2123 MODULE_LICENSE("GPL");