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;
50 * Allocate memory for all planes in this buffer
51 * NOTE: mmapped areas should be page aligned
53 for (plane = 0; plane < vb->num_planes; ++plane) {
54 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
56 mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
57 size, q->dma_dir, q->gfp_flags);
58 if (IS_ERR_OR_NULL(mem_priv))
61 /* Associate allocator private data with this plane */
62 vb->planes[plane].mem_priv = mem_priv;
63 vb->planes[plane].length = q->plane_sizes[plane];
68 /* Free already allocated memory if one of the allocations failed */
69 for (; plane > 0; --plane) {
70 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
71 vb->planes[plane - 1].mem_priv = NULL;
78 * __vb2_buf_mem_free() - free memory of the given buffer
80 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
84 for (plane = 0; plane < vb->num_planes; ++plane) {
85 call_void_memop(vb, put, vb->planes[plane].mem_priv);
86 vb->planes[plane].mem_priv = NULL;
87 dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
92 * __vb2_buf_userptr_put() - release userspace memory associated with
95 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
99 for (plane = 0; plane < vb->num_planes; ++plane) {
100 if (vb->planes[plane].mem_priv)
101 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
102 vb->planes[plane].mem_priv = NULL;
107 * __vb2_plane_dmabuf_put() - release memory associated with
108 * a DMABUF shared plane
110 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
116 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
118 call_void_memop(vb, detach_dmabuf, p->mem_priv);
119 dma_buf_put(p->dbuf);
126 * __vb2_buf_dmabuf_put() - release memory associated with
127 * a DMABUF shared buffer
129 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
133 for (plane = 0; plane < vb->num_planes; ++plane)
134 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
138 * __setup_lengths() - setup initial lengths for every plane in
139 * every buffer on the queue
141 static void __setup_lengths(struct vb2_queue *q, unsigned int n)
143 unsigned int buffer, plane;
144 struct vb2_buffer *vb;
146 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
147 vb = q->bufs[buffer];
151 for (plane = 0; plane < vb->num_planes; ++plane)
152 vb->planes[plane].length = q->plane_sizes[plane];
157 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
158 * every buffer on the queue
160 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
162 unsigned int buffer, plane;
163 struct vb2_buffer *vb;
166 if (q->num_buffers) {
168 vb = q->bufs[q->num_buffers - 1];
169 p = &vb->planes[vb->num_planes - 1];
170 off = PAGE_ALIGN(p->m.offset + p->length);
175 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
176 vb = q->bufs[buffer];
180 for (plane = 0; plane < vb->num_planes; ++plane) {
181 vb->planes[plane].m.offset = off;
183 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
186 off += vb->planes[plane].length;
187 off = PAGE_ALIGN(off);
193 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
194 * video buffer memory for all buffers/planes on the queue and initializes the
197 * Returns the number of buffers successfully allocated.
199 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
200 unsigned int num_buffers, unsigned int num_planes)
203 struct vb2_buffer *vb;
206 for (buffer = 0; buffer < num_buffers; ++buffer) {
207 /* Allocate videobuf buffer structures */
208 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
210 dprintk(1, "memory alloc for buffer struct failed\n");
214 vb->state = VB2_BUF_STATE_DEQUEUED;
216 vb->num_planes = num_planes;
217 vb->index = q->num_buffers + buffer;
221 /* Allocate video buffer memory for the MMAP type */
222 if (memory == VB2_MEMORY_MMAP) {
223 ret = __vb2_buf_mem_alloc(vb);
225 dprintk(1, "failed allocating memory for "
226 "buffer %d\n", buffer);
231 * Call the driver-provided buffer initialization
232 * callback, if given. An error in initialization
233 * results in queue setup failure.
235 ret = call_vb_qop(vb, buf_init, vb);
237 dprintk(1, "buffer %d %p initialization"
238 " failed\n", buffer, vb);
239 __vb2_buf_mem_free(vb);
245 q->bufs[q->num_buffers + buffer] = vb;
248 __setup_lengths(q, buffer);
249 if (memory == VB2_MEMORY_MMAP)
250 __setup_offsets(q, buffer);
252 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
259 * __vb2_free_mem() - release all video buffer memory for a given queue
261 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
264 struct vb2_buffer *vb;
266 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
268 vb = q->bufs[buffer];
272 /* Free MMAP buffers or release USERPTR buffers */
273 if (q->memory == VB2_MEMORY_MMAP)
274 __vb2_buf_mem_free(vb);
275 else if (q->memory == VB2_MEMORY_DMABUF)
276 __vb2_buf_dmabuf_put(vb);
278 __vb2_buf_userptr_put(vb);
283 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
284 * related information, if no buffers are left return the queue to an
285 * uninitialized state. Might be called even if the queue has already been freed.
287 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
292 * Sanity check: when preparing a buffer the queue lock is released for
293 * a short while (see __buf_prepare for the details), which would allow
294 * a race with a reqbufs which can call this function. Removing the
295 * buffers from underneath __buf_prepare is obviously a bad idea, so we
296 * check if any of the buffers is in the state PREPARING, and if so we
297 * just return -EAGAIN.
299 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
301 if (q->bufs[buffer] == NULL)
303 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
304 dprintk(1, "preparing buffers, cannot free\n");
309 /* Call driver-provided cleanup function for each buffer, if provided */
310 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
312 struct vb2_buffer *vb = q->bufs[buffer];
314 if (vb && vb->planes[0].mem_priv)
315 call_void_vb_qop(vb, buf_cleanup, vb);
318 /* Release video buffer memory */
319 __vb2_free_mem(q, buffers);
321 #ifdef CONFIG_VIDEO_ADV_DEBUG
323 * Check that all the calls were balances during the life-time of this
324 * queue. If not (or if the debug level is 1 or up), then dump the
325 * counters to the kernel log.
327 if (q->num_buffers) {
328 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
329 q->cnt_wait_prepare != q->cnt_wait_finish;
331 if (unbalanced || vb2_debug) {
332 pr_info("vb2: counters for queue %p:%s\n", q,
333 unbalanced ? " UNBALANCED!" : "");
334 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
335 q->cnt_queue_setup, q->cnt_start_streaming,
336 q->cnt_stop_streaming);
337 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
338 q->cnt_wait_prepare, q->cnt_wait_finish);
340 q->cnt_queue_setup = 0;
341 q->cnt_wait_prepare = 0;
342 q->cnt_wait_finish = 0;
343 q->cnt_start_streaming = 0;
344 q->cnt_stop_streaming = 0;
346 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
347 struct vb2_buffer *vb = q->bufs[buffer];
348 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
349 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
350 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
351 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
352 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
353 vb->cnt_buf_queue != vb->cnt_buf_done ||
354 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
355 vb->cnt_buf_init != vb->cnt_buf_cleanup;
357 if (unbalanced || vb2_debug) {
358 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
359 q, buffer, unbalanced ? " UNBALANCED!" : "");
360 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
361 vb->cnt_buf_init, vb->cnt_buf_cleanup,
362 vb->cnt_buf_prepare, vb->cnt_buf_finish);
363 pr_info("vb2: buf_queue: %u buf_done: %u\n",
364 vb->cnt_buf_queue, vb->cnt_buf_done);
365 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
366 vb->cnt_mem_alloc, vb->cnt_mem_put,
367 vb->cnt_mem_prepare, vb->cnt_mem_finish,
369 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
370 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
371 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
372 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
373 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
374 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
375 vb->cnt_mem_get_dmabuf,
376 vb->cnt_mem_num_users,
383 /* Free videobuf buffers */
384 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
386 kfree(q->bufs[buffer]);
387 q->bufs[buffer] = NULL;
390 q->num_buffers -= buffers;
391 if (!q->num_buffers) {
393 INIT_LIST_HEAD(&q->queued_list);
399 * vb2_buffer_in_use() - return true if the buffer is in use and
400 * the queue cannot be freed (by the means of REQBUFS(0)) call
402 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
405 for (plane = 0; plane < vb->num_planes; ++plane) {
406 void *mem_priv = vb->planes[plane].mem_priv;
408 * If num_users() has not been provided, call_memop
409 * will return 0, apparently nobody cares about this
410 * case anyway. If num_users() returns more than 1,
411 * we are not the only user of the plane's memory.
413 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
418 EXPORT_SYMBOL(vb2_buffer_in_use);
421 * __buffers_in_use() - return true if any buffers on the queue are in use and
422 * the queue cannot be freed (by the means of REQBUFS(0)) call
424 static bool __buffers_in_use(struct vb2_queue *q)
427 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
428 if (vb2_buffer_in_use(q, q->bufs[buffer]))
435 * vb2_core_querybuf() - query video buffer information
437 * @index: id number of the buffer
438 * @pb: buffer struct passed from userspace
440 * Should be called from vidioc_querybuf ioctl handler in driver.
441 * The passed buffer should have been verified.
442 * This function fills the relevant information for the userspace.
444 * The return values from this function are intended to be directly returned
445 * from vidioc_querybuf handler in driver.
447 int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
449 return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
451 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
454 * __verify_userptr_ops() - verify that all memory operations required for
455 * USERPTR queue type have been provided
457 static int __verify_userptr_ops(struct vb2_queue *q)
459 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
460 !q->mem_ops->put_userptr)
467 * __verify_mmap_ops() - verify that all memory operations required for
468 * MMAP queue type have been provided
470 static int __verify_mmap_ops(struct vb2_queue *q)
472 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
473 !q->mem_ops->put || !q->mem_ops->mmap)
480 * __verify_dmabuf_ops() - verify that all memory operations required for
481 * DMABUF queue type have been provided
483 static int __verify_dmabuf_ops(struct vb2_queue *q)
485 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
486 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
487 !q->mem_ops->unmap_dmabuf)
494 * vb2_verify_memory_type() - Check whether the memory type and buffer type
495 * passed to a buffer operation are compatible with the queue.
497 int vb2_verify_memory_type(struct vb2_queue *q,
498 enum vb2_memory memory, unsigned int type)
500 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
501 memory != VB2_MEMORY_DMABUF) {
502 dprintk(1, "unsupported memory type\n");
506 if (type != q->type) {
507 dprintk(1, "requested type is incorrect\n");
512 * Make sure all the required memory ops for given memory type
515 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
516 dprintk(1, "MMAP for current setup unsupported\n");
520 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
521 dprintk(1, "USERPTR for current setup unsupported\n");
525 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
526 dprintk(1, "DMABUF for current setup unsupported\n");
531 * Place the busy tests at the end: -EBUSY can be ignored when
532 * create_bufs is called with count == 0, but count == 0 should still
533 * do the memory and type validation.
535 if (vb2_fileio_is_active(q)) {
536 dprintk(1, "file io in progress\n");
541 EXPORT_SYMBOL(vb2_verify_memory_type);
544 * vb2_core_reqbufs() - Initiate streaming
545 * @q: videobuf2 queue
546 * @memory: memory type
547 * @count: requested buffer count
549 * Should be called from vidioc_reqbufs ioctl handler of a driver.
551 * 1) verifies streaming parameters passed from the userspace,
552 * 2) sets up the queue,
553 * 3) negotiates number of buffers and planes per buffer with the driver
554 * to be used during streaming,
555 * 4) allocates internal buffer structures (struct vb2_buffer), according to
556 * the agreed parameters,
557 * 5) for MMAP memory type, allocates actual video memory, using the
558 * memory handling/allocation routines provided during queue initialization
560 * If req->count is 0, all the memory will be freed instead.
561 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
562 * and the queue is not busy, memory will be reallocated.
564 * The return values from this function are intended to be directly returned
565 * from vidioc_reqbufs handler in driver.
567 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
570 unsigned int num_buffers, allocated_buffers, num_planes = 0;
574 dprintk(1, "streaming active\n");
578 if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
580 * We already have buffers allocated, so first check if they
581 * are not in use and can be freed.
583 mutex_lock(&q->mmap_lock);
584 if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
585 mutex_unlock(&q->mmap_lock);
586 dprintk(1, "memory in use, cannot free\n");
591 * Call queue_cancel to clean up any buffers in the PREPARED or
592 * QUEUED state which is possible if buffers were prepared or
593 * queued without ever calling STREAMON.
595 __vb2_queue_cancel(q);
596 ret = __vb2_queue_free(q, q->num_buffers);
597 mutex_unlock(&q->mmap_lock);
602 * In case of REQBUFS(0) return immediately without calling
603 * driver's queue_setup() callback and allocating resources.
610 * Make sure the requested values and current defaults are sane.
612 num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
613 num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
614 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
615 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
619 * Ask the driver how many buffers and planes per buffer it requires.
620 * Driver also sets the size and allocator context for each plane.
622 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
623 q->plane_sizes, q->alloc_ctx);
627 /* Finally, allocate buffers and video memory */
629 __vb2_queue_alloc(q, memory, num_buffers, num_planes);
630 if (allocated_buffers == 0) {
631 dprintk(1, "memory allocation failed\n");
636 * There is no point in continuing if we can't allocate the minimum
637 * number of buffers needed by this vb2_queue.
639 if (allocated_buffers < q->min_buffers_needed)
643 * Check if driver can handle the allocated number of buffers.
645 if (!ret && allocated_buffers < num_buffers) {
646 num_buffers = allocated_buffers;
648 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
649 &num_planes, q->plane_sizes, q->alloc_ctx);
651 if (!ret && allocated_buffers < num_buffers)
655 * Either the driver has accepted a smaller number of buffers,
656 * or .queue_setup() returned an error
660 mutex_lock(&q->mmap_lock);
661 q->num_buffers = allocated_buffers;
665 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
666 * from q->num_buffers.
668 __vb2_queue_free(q, allocated_buffers);
669 mutex_unlock(&q->mmap_lock);
672 mutex_unlock(&q->mmap_lock);
675 * Return the number of successfully allocated buffers
678 *count = allocated_buffers;
679 q->waiting_for_buffers = !q->is_output;
683 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
686 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
687 * @q: videobuf2 queue
688 * @memory: memory type
689 * @count: requested buffer count
690 * @parg: parameter passed to device driver
692 * Should be called from vidioc_create_bufs ioctl handler of a driver.
694 * 1) verifies parameter sanity
695 * 2) calls the .queue_setup() queue operation
696 * 3) performs any necessary memory allocations
698 * The return values from this function are intended to be directly returned
699 * from vidioc_create_bufs handler in driver.
701 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
702 unsigned int *count, const void *parg)
704 unsigned int num_planes = 0, num_buffers, allocated_buffers;
707 if (q->num_buffers == VB2_MAX_FRAME) {
708 dprintk(1, "maximum number of buffers already allocated\n");
712 if (!q->num_buffers) {
713 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
714 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
716 q->waiting_for_buffers = !q->is_output;
719 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
722 * Ask the driver, whether the requested number of buffers, planes per
723 * buffer and their sizes are acceptable
725 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
726 &num_planes, q->plane_sizes, q->alloc_ctx);
730 /* Finally, allocate buffers and video memory */
731 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
733 if (allocated_buffers == 0) {
734 dprintk(1, "memory allocation failed\n");
739 * Check if driver can handle the so far allocated number of buffers.
741 if (allocated_buffers < num_buffers) {
742 num_buffers = allocated_buffers;
745 * q->num_buffers contains the total number of buffers, that the
746 * queue driver has set up
748 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
749 &num_planes, q->plane_sizes, q->alloc_ctx);
751 if (!ret && allocated_buffers < num_buffers)
755 * Either the driver has accepted a smaller number of buffers,
756 * or .queue_setup() returned an error
760 mutex_lock(&q->mmap_lock);
761 q->num_buffers += allocated_buffers;
765 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
766 * from q->num_buffers.
768 __vb2_queue_free(q, allocated_buffers);
769 mutex_unlock(&q->mmap_lock);
772 mutex_unlock(&q->mmap_lock);
775 * Return the number of successfully allocated buffers
778 *count = allocated_buffers;
782 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
785 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
786 * @vb: vb2_buffer to which the plane in question belongs to
787 * @plane_no: plane number for which the address is to be returned
789 * This function returns a kernel virtual address of a given plane if
790 * such a mapping exist, NULL otherwise.
792 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
794 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
797 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
800 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
803 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
804 * @vb: vb2_buffer to which the plane in question belongs to
805 * @plane_no: plane number for which the cookie is to be returned
807 * This function returns an allocator specific cookie for a given plane if
808 * available, NULL otherwise. The allocator should provide some simple static
809 * inline function, which would convert this cookie to the allocator specific
810 * type that can be used directly by the driver to access the buffer. This can
811 * be for example physical address, pointer to scatter list or IOMMU mapping.
813 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
815 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
818 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
820 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
823 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
824 * @vb: vb2_buffer returned from the driver
825 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
826 * VB2_BUF_STATE_ERROR if the operation finished with an error or
827 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
828 * If start_streaming fails then it should return buffers with state
829 * VB2_BUF_STATE_QUEUED to put them back into the queue.
831 * This function should be called by the driver after a hardware operation on
832 * a buffer is finished and the buffer may be returned to userspace. The driver
833 * cannot use this buffer anymore until it is queued back to it by videobuf
834 * by the means of buf_queue callback. Only buffers previously queued to the
835 * driver by buf_queue can be passed to this function.
837 * While streaming a buffer can only be returned in state DONE or ERROR.
838 * The start_streaming op can also return them in case the DMA engine cannot
839 * be started for some reason. In that case the buffers should be returned with
842 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
844 struct vb2_queue *q = vb->vb2_queue;
848 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
851 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
852 state != VB2_BUF_STATE_ERROR &&
853 state != VB2_BUF_STATE_QUEUED &&
854 state != VB2_BUF_STATE_REQUEUEING))
855 state = VB2_BUF_STATE_ERROR;
857 #ifdef CONFIG_VIDEO_ADV_DEBUG
859 * Although this is not a callback, it still does have to balance
860 * with the buf_queue op. So update this counter manually.
864 dprintk(4, "done processing on buffer %d, state: %d\n",
868 for (plane = 0; plane < vb->num_planes; ++plane)
869 call_void_memop(vb, finish, vb->planes[plane].mem_priv);
871 spin_lock_irqsave(&q->done_lock, flags);
872 if (state == VB2_BUF_STATE_QUEUED ||
873 state == VB2_BUF_STATE_REQUEUEING) {
874 vb->state = VB2_BUF_STATE_QUEUED;
876 /* Add the buffer to the done buffers list */
877 list_add_tail(&vb->done_entry, &q->done_list);
880 atomic_dec(&q->owned_by_drv_count);
881 spin_unlock_irqrestore(&q->done_lock, flags);
883 trace_vb2_buf_done(q, vb);
886 case VB2_BUF_STATE_QUEUED:
888 case VB2_BUF_STATE_REQUEUEING:
889 if (q->start_streaming_called)
890 __enqueue_in_driver(vb);
893 /* Inform any processes that may be waiting for buffers */
894 wake_up(&q->done_wq);
898 EXPORT_SYMBOL_GPL(vb2_buffer_done);
901 * vb2_discard_done() - discard all buffers marked as DONE
902 * @q: videobuf2 queue
904 * This function is intended to be used with suspend/resume operations. It
905 * discards all 'done' buffers as they would be too old to be requested after
908 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
909 * delayed works before calling this function to make sure no buffer will be
910 * touched by the driver and/or hardware.
912 void vb2_discard_done(struct vb2_queue *q)
914 struct vb2_buffer *vb;
917 spin_lock_irqsave(&q->done_lock, flags);
918 list_for_each_entry(vb, &q->done_list, done_entry)
919 vb->state = VB2_BUF_STATE_ERROR;
920 spin_unlock_irqrestore(&q->done_lock, flags);
922 EXPORT_SYMBOL_GPL(vb2_discard_done);
925 * __qbuf_mmap() - handle qbuf of an MMAP buffer
927 static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
929 int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
931 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
935 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
937 static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
939 struct vb2_plane planes[VB2_MAX_PLANES];
940 struct vb2_queue *q = vb->vb2_queue;
944 bool reacquired = vb->planes[0].mem_priv == NULL;
946 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
947 /* Copy relevant information provided by the userspace */
948 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
952 for (plane = 0; plane < vb->num_planes; ++plane) {
953 /* Skip the plane if already verified */
954 if (vb->planes[plane].m.userptr &&
955 vb->planes[plane].m.userptr == planes[plane].m.userptr
956 && vb->planes[plane].length == planes[plane].length)
959 dprintk(3, "userspace address for plane %d changed, "
960 "reacquiring memory\n", plane);
962 /* Check if the provided plane buffer is large enough */
963 if (planes[plane].length < q->plane_sizes[plane]) {
964 dprintk(1, "provided buffer size %u is less than "
965 "setup size %u for plane %d\n",
966 planes[plane].length,
967 q->plane_sizes[plane], plane);
972 /* Release previously acquired memory if present */
973 if (vb->planes[plane].mem_priv) {
976 call_void_vb_qop(vb, buf_cleanup, vb);
978 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
981 vb->planes[plane].mem_priv = NULL;
982 vb->planes[plane].bytesused = 0;
983 vb->planes[plane].length = 0;
984 vb->planes[plane].m.userptr = 0;
985 vb->planes[plane].data_offset = 0;
987 /* Acquire each plane's memory */
988 mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
989 planes[plane].m.userptr,
990 planes[plane].length, q->dma_dir);
991 if (IS_ERR_OR_NULL(mem_priv)) {
992 dprintk(1, "failed acquiring userspace "
993 "memory for plane %d\n", plane);
994 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
997 vb->planes[plane].mem_priv = mem_priv;
1001 * Now that everything is in order, copy relevant information
1002 * provided by userspace.
1004 for (plane = 0; plane < vb->num_planes; ++plane) {
1005 vb->planes[plane].bytesused = planes[plane].bytesused;
1006 vb->planes[plane].length = planes[plane].length;
1007 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1008 vb->planes[plane].data_offset = planes[plane].data_offset;
1013 * One or more planes changed, so we must call buf_init to do
1014 * the driver-specific initialization on the newly acquired
1015 * buffer, if provided.
1017 ret = call_vb_qop(vb, buf_init, vb);
1019 dprintk(1, "buffer initialization failed\n");
1024 ret = call_vb_qop(vb, buf_prepare, vb);
1026 dprintk(1, "buffer preparation failed\n");
1027 call_void_vb_qop(vb, buf_cleanup, vb);
1033 /* In case of errors, release planes that were already acquired */
1034 for (plane = 0; plane < vb->num_planes; ++plane) {
1035 if (vb->planes[plane].mem_priv)
1036 call_void_memop(vb, put_userptr,
1037 vb->planes[plane].mem_priv);
1038 vb->planes[plane].mem_priv = NULL;
1039 vb->planes[plane].m.userptr = 0;
1040 vb->planes[plane].length = 0;
1047 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1049 static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
1051 struct vb2_plane planes[VB2_MAX_PLANES];
1052 struct vb2_queue *q = vb->vb2_queue;
1056 bool reacquired = vb->planes[0].mem_priv == NULL;
1058 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1059 /* Copy relevant information provided by the userspace */
1060 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
1064 for (plane = 0; plane < vb->num_planes; ++plane) {
1065 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1067 if (IS_ERR_OR_NULL(dbuf)) {
1068 dprintk(1, "invalid dmabuf fd for plane %d\n",
1074 /* use DMABUF size if length is not provided */
1075 if (planes[plane].length == 0)
1076 planes[plane].length = dbuf->size;
1078 if (planes[plane].length < q->plane_sizes[plane]) {
1079 dprintk(1, "invalid dmabuf length for plane %d\n",
1085 /* Skip the plane if already verified */
1086 if (dbuf == vb->planes[plane].dbuf &&
1087 vb->planes[plane].length == planes[plane].length) {
1092 dprintk(1, "buffer for plane %d changed\n", plane);
1096 call_void_vb_qop(vb, buf_cleanup, vb);
1099 /* Release previously acquired memory if present */
1100 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1101 vb->planes[plane].bytesused = 0;
1102 vb->planes[plane].length = 0;
1103 vb->planes[plane].m.fd = 0;
1104 vb->planes[plane].data_offset = 0;
1106 /* Acquire each plane's memory */
1107 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1108 q->alloc_ctx[plane], dbuf, planes[plane].length,
1110 if (IS_ERR(mem_priv)) {
1111 dprintk(1, "failed to attach dmabuf\n");
1112 ret = PTR_ERR(mem_priv);
1117 vb->planes[plane].dbuf = dbuf;
1118 vb->planes[plane].mem_priv = mem_priv;
1121 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1122 * really we want to do this just before the DMA, not while queueing
1125 for (plane = 0; plane < vb->num_planes; ++plane) {
1126 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1128 dprintk(1, "failed to map dmabuf for plane %d\n",
1132 vb->planes[plane].dbuf_mapped = 1;
1136 * Now that everything is in order, copy relevant information
1137 * provided by userspace.
1139 for (plane = 0; plane < vb->num_planes; ++plane) {
1140 vb->planes[plane].bytesused = planes[plane].bytesused;
1141 vb->planes[plane].length = planes[plane].length;
1142 vb->planes[plane].m.fd = planes[plane].m.fd;
1143 vb->planes[plane].data_offset = planes[plane].data_offset;
1148 * Call driver-specific initialization on the newly acquired buffer,
1151 ret = call_vb_qop(vb, buf_init, vb);
1153 dprintk(1, "buffer initialization failed\n");
1158 ret = call_vb_qop(vb, buf_prepare, vb);
1160 dprintk(1, "buffer preparation failed\n");
1161 call_void_vb_qop(vb, buf_cleanup, vb);
1167 /* In case of errors, release planes that were already acquired */
1168 __vb2_buf_dmabuf_put(vb);
1174 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1176 static void __enqueue_in_driver(struct vb2_buffer *vb)
1178 struct vb2_queue *q = vb->vb2_queue;
1181 vb->state = VB2_BUF_STATE_ACTIVE;
1182 atomic_inc(&q->owned_by_drv_count);
1184 trace_vb2_buf_queue(q, vb);
1187 for (plane = 0; plane < vb->num_planes; ++plane)
1188 call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
1190 call_void_vb_qop(vb, buf_queue, vb);
1193 static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1195 struct vb2_queue *q = vb->vb2_queue;
1199 dprintk(1, "fatal error occurred on queue\n");
1203 vb->state = VB2_BUF_STATE_PREPARING;
1205 switch (q->memory) {
1206 case VB2_MEMORY_MMAP:
1207 ret = __qbuf_mmap(vb, pb);
1209 case VB2_MEMORY_USERPTR:
1210 ret = __qbuf_userptr(vb, pb);
1212 case VB2_MEMORY_DMABUF:
1213 ret = __qbuf_dmabuf(vb, pb);
1216 WARN(1, "Invalid queue type\n");
1221 dprintk(1, "buffer preparation failed: %d\n", ret);
1222 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
1228 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1230 * @q: videobuf2 queue
1231 * @index: id number of the buffer
1232 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1235 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1236 * The passed buffer should have been verified.
1237 * This function calls buf_prepare callback in the driver (if provided),
1238 * in which driver-specific buffer initialization can be performed,
1240 * The return values from this function are intended to be directly returned
1241 * from vidioc_prepare_buf handler in driver.
1243 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1245 struct vb2_buffer *vb;
1248 vb = q->bufs[index];
1249 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1250 dprintk(1, "invalid buffer state %d\n",
1255 ret = __buf_prepare(vb, pb);
1259 /* Fill buffer information for the userspace */
1260 ret = call_bufop(q, fill_user_buffer, vb, pb);
1264 dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
1268 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1271 * vb2_start_streaming() - Attempt to start streaming.
1272 * @q: videobuf2 queue
1274 * Attempt to start streaming. When this function is called there must be
1275 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1276 * number of buffers required for the DMA engine to function). If the
1277 * @start_streaming op fails it is supposed to return all the driver-owned
1278 * buffers back to vb2 in state QUEUED. Check if that happened and if
1279 * not warn and reclaim them forcefully.
1281 static int vb2_start_streaming(struct vb2_queue *q)
1283 struct vb2_buffer *vb;
1287 * If any buffers were queued before streamon,
1288 * we can now pass them to driver for processing.
1290 list_for_each_entry(vb, &q->queued_list, queued_entry)
1291 __enqueue_in_driver(vb);
1293 /* Tell the driver to start streaming */
1294 q->start_streaming_called = 1;
1295 ret = call_qop(q, start_streaming, q,
1296 atomic_read(&q->owned_by_drv_count));
1300 q->start_streaming_called = 0;
1302 dprintk(1, "driver refused to start streaming\n");
1304 * If you see this warning, then the driver isn't cleaning up properly
1305 * after a failed start_streaming(). See the start_streaming()
1306 * documentation in videobuf2-core.h for more information how buffers
1307 * should be returned to vb2 in start_streaming().
1309 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1313 * Forcefully reclaim buffers if the driver did not
1314 * correctly return them to vb2.
1316 for (i = 0; i < q->num_buffers; ++i) {
1318 if (vb->state == VB2_BUF_STATE_ACTIVE)
1319 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1321 /* Must be zero now */
1322 WARN_ON(atomic_read(&q->owned_by_drv_count));
1325 * If done_list is not empty, then start_streaming() didn't call
1326 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1329 WARN_ON(!list_empty(&q->done_list));
1334 * vb2_core_qbuf() - Queue a buffer from userspace
1335 * @q: videobuf2 queue
1336 * @index: id number of the buffer
1337 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1340 * Should be called from vidioc_qbuf ioctl handler of a driver.
1341 * The passed buffer should have been verified.
1343 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1344 * which driver-specific buffer initialization can be performed,
1345 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1346 * callback for processing.
1348 * The return values from this function are intended to be directly returned
1349 * from vidioc_qbuf handler in driver.
1351 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1353 struct vb2_buffer *vb;
1356 vb = q->bufs[index];
1358 switch (vb->state) {
1359 case VB2_BUF_STATE_DEQUEUED:
1360 ret = __buf_prepare(vb, pb);
1364 case VB2_BUF_STATE_PREPARED:
1366 case VB2_BUF_STATE_PREPARING:
1367 dprintk(1, "buffer still being prepared\n");
1370 dprintk(1, "invalid buffer state %d\n", vb->state);
1375 * Add to the queued buffers list, a buffer will stay on it until
1376 * dequeued in dqbuf.
1378 list_add_tail(&vb->queued_entry, &q->queued_list);
1380 q->waiting_for_buffers = false;
1381 vb->state = VB2_BUF_STATE_QUEUED;
1383 call_bufop(q, set_timestamp, vb, pb);
1385 trace_vb2_qbuf(q, vb);
1388 * If already streaming, give the buffer to driver for processing.
1389 * If not, the buffer will be given to driver on next streamon.
1391 if (q->start_streaming_called)
1392 __enqueue_in_driver(vb);
1394 /* Fill buffer information for the userspace */
1395 ret = call_bufop(q, fill_user_buffer, vb, pb);
1400 * If streamon has been called, and we haven't yet called
1401 * start_streaming() since not enough buffers were queued, and
1402 * we now have reached the minimum number of queued buffers,
1403 * then we can finally call start_streaming().
1405 if (q->streaming && !q->start_streaming_called &&
1406 q->queued_count >= q->min_buffers_needed) {
1407 ret = vb2_start_streaming(q);
1412 dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
1415 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1418 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1421 * Will sleep if required for nonblocking == false.
1423 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1426 * All operations on vb_done_list are performed under done_lock
1427 * spinlock protection. However, buffers may be removed from
1428 * it and returned to userspace only while holding both driver's
1429 * lock and the done_lock spinlock. Thus we can be sure that as
1430 * long as we hold the driver's lock, the list will remain not
1431 * empty if list_empty() check succeeds.
1437 if (!q->streaming) {
1438 dprintk(1, "streaming off, will not wait for buffers\n");
1443 dprintk(1, "Queue in error state, will not wait for buffers\n");
1447 if (q->last_buffer_dequeued) {
1448 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1452 if (!list_empty(&q->done_list)) {
1454 * Found a buffer that we were waiting for.
1460 dprintk(1, "nonblocking and no buffers to dequeue, "
1466 * We are streaming and blocking, wait for another buffer to
1467 * become ready or for streamoff. Driver's lock is released to
1468 * allow streamoff or qbuf to be called while waiting.
1470 call_void_qop(q, wait_prepare, q);
1473 * All locks have been released, it is safe to sleep now.
1475 dprintk(3, "will sleep waiting for buffers\n");
1476 ret = wait_event_interruptible(q->done_wq,
1477 !list_empty(&q->done_list) || !q->streaming ||
1481 * We need to reevaluate both conditions again after reacquiring
1482 * the locks or return an error if one occurred.
1484 call_void_qop(q, wait_finish, q);
1486 dprintk(1, "sleep was interrupted\n");
1494 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1496 * Will sleep if required for nonblocking == false.
1498 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1499 void *pb, int nonblocking)
1501 unsigned long flags;
1505 * Wait for at least one buffer to become available on the done_list.
1507 ret = __vb2_wait_for_done_vb(q, nonblocking);
1512 * Driver's lock has been held since we last verified that done_list
1513 * is not empty, so no need for another list_empty(done_list) check.
1515 spin_lock_irqsave(&q->done_lock, flags);
1516 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1518 * Only remove the buffer from done_list if all planes can be
1519 * handled. Some cases such as V4L2 file I/O and DVB have pb
1520 * == NULL; skip the check then as there's nothing to verify.
1523 ret = call_bufop(q, verify_planes_array, *vb, pb);
1525 list_del(&(*vb)->done_entry);
1526 spin_unlock_irqrestore(&q->done_lock, flags);
1532 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1533 * @q: videobuf2 queue
1535 * This function will wait until all buffers that have been given to the driver
1536 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1537 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1538 * taken, for example from stop_streaming() callback.
1540 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1542 if (!q->streaming) {
1543 dprintk(1, "streaming off, will not wait for buffers\n");
1547 if (q->start_streaming_called)
1548 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1551 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1554 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1556 static void __vb2_dqbuf(struct vb2_buffer *vb)
1558 struct vb2_queue *q = vb->vb2_queue;
1561 /* nothing to do if the buffer is already dequeued */
1562 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1565 vb->state = VB2_BUF_STATE_DEQUEUED;
1567 /* unmap DMABUF buffer */
1568 if (q->memory == VB2_MEMORY_DMABUF)
1569 for (i = 0; i < vb->num_planes; ++i) {
1570 if (!vb->planes[i].dbuf_mapped)
1572 call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1573 vb->planes[i].dbuf_mapped = 0;
1578 * vb2_dqbuf() - Dequeue a buffer to the userspace
1579 * @q: videobuf2 queue
1580 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1582 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1583 * buffers ready for dequeuing are present. Normally the driver
1584 * would be passing (file->f_flags & O_NONBLOCK) here
1586 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1587 * The passed buffer should have been verified.
1589 * 1) calls buf_finish callback in the driver (if provided), in which
1590 * driver can perform any additional operations that may be required before
1591 * returning the buffer to userspace, such as cache sync,
1592 * 2) the buffer struct members are filled with relevant information for
1595 * The return values from this function are intended to be directly returned
1596 * from vidioc_dqbuf handler in driver.
1598 int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
1600 struct vb2_buffer *vb = NULL;
1603 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1607 switch (vb->state) {
1608 case VB2_BUF_STATE_DONE:
1609 dprintk(3, "returning done buffer\n");
1611 case VB2_BUF_STATE_ERROR:
1612 dprintk(3, "returning done buffer with errors\n");
1615 dprintk(1, "invalid buffer state\n");
1619 call_void_vb_qop(vb, buf_finish, vb);
1621 /* Fill buffer information for the userspace */
1622 ret = call_bufop(q, fill_user_buffer, vb, pb);
1626 /* Remove from videobuf queue */
1627 list_del(&vb->queued_entry);
1630 trace_vb2_dqbuf(q, vb);
1632 /* go back to dequeued state */
1635 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1636 vb->index, vb->state);
1641 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1644 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1646 * Removes all queued buffers from driver's queue and all buffers queued by
1647 * userspace from videobuf's queue. Returns to state after reqbufs.
1649 static void __vb2_queue_cancel(struct vb2_queue *q)
1654 * Tell driver to stop all transactions and release all queued
1657 if (q->start_streaming_called)
1658 call_void_qop(q, stop_streaming, q);
1661 * If you see this warning, then the driver isn't cleaning up properly
1662 * in stop_streaming(). See the stop_streaming() documentation in
1663 * videobuf2-core.h for more information how buffers should be returned
1664 * to vb2 in stop_streaming().
1666 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1667 for (i = 0; i < q->num_buffers; ++i)
1668 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
1669 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1670 /* Must be zero now */
1671 WARN_ON(atomic_read(&q->owned_by_drv_count));
1675 q->start_streaming_called = 0;
1676 q->queued_count = 0;
1680 * Remove all buffers from videobuf's list...
1682 INIT_LIST_HEAD(&q->queued_list);
1684 * ...and done list; userspace will not receive any buffers it
1685 * has not already dequeued before initiating cancel.
1687 INIT_LIST_HEAD(&q->done_list);
1688 atomic_set(&q->owned_by_drv_count, 0);
1689 wake_up_all(&q->done_wq);
1692 * Reinitialize all buffers for next use.
1693 * Make sure to call buf_finish for any queued buffers. Normally
1694 * that's done in dqbuf, but that's not going to happen when we
1695 * cancel the whole queue. Note: this code belongs here, not in
1696 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1697 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1698 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1700 for (i = 0; i < q->num_buffers; ++i) {
1701 struct vb2_buffer *vb = q->bufs[i];
1703 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1704 vb->state = VB2_BUF_STATE_PREPARED;
1705 call_void_vb_qop(vb, buf_finish, vb);
1711 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1715 if (type != q->type) {
1716 dprintk(1, "invalid stream type\n");
1721 dprintk(3, "already streaming\n");
1725 if (!q->num_buffers) {
1726 dprintk(1, "no buffers have been allocated\n");
1730 if (q->num_buffers < q->min_buffers_needed) {
1731 dprintk(1, "need at least %u allocated buffers\n",
1732 q->min_buffers_needed);
1737 * Tell driver to start streaming provided sufficient buffers
1740 if (q->queued_count >= q->min_buffers_needed) {
1741 ret = vb2_start_streaming(q);
1743 __vb2_queue_cancel(q);
1750 dprintk(3, "successful\n");
1753 EXPORT_SYMBOL_GPL(vb2_core_streamon);
1756 * vb2_queue_error() - signal a fatal error on the queue
1757 * @q: videobuf2 queue
1759 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1760 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1761 * buffers will return -EIO.
1763 * The error flag will be cleared when cancelling the queue, either from
1764 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1765 * function before starting the stream, otherwise the error flag will remain set
1766 * until the queue is released when closing the device node.
1768 void vb2_queue_error(struct vb2_queue *q)
1772 wake_up_all(&q->done_wq);
1774 EXPORT_SYMBOL_GPL(vb2_queue_error);
1776 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
1778 if (type != q->type) {
1779 dprintk(1, "invalid stream type\n");
1784 * Cancel will pause streaming and remove all buffers from the driver
1785 * and videobuf, effectively returning control over them to userspace.
1787 * Note that we do this even if q->streaming == 0: if you prepare or
1788 * queue buffers, and then call streamoff without ever having called
1789 * streamon, you would still expect those buffers to be returned to
1790 * their normal dequeued state.
1792 __vb2_queue_cancel(q);
1793 q->waiting_for_buffers = !q->is_output;
1794 q->last_buffer_dequeued = false;
1796 dprintk(3, "successful\n");
1799 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
1802 * __find_plane_by_offset() - find plane associated with the given offset off
1804 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1805 unsigned int *_buffer, unsigned int *_plane)
1807 struct vb2_buffer *vb;
1808 unsigned int buffer, plane;
1811 * Go over all buffers and their planes, comparing the given offset
1812 * with an offset assigned to each plane. If a match is found,
1813 * return its buffer and plane numbers.
1815 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1816 vb = q->bufs[buffer];
1818 for (plane = 0; plane < vb->num_planes; ++plane) {
1819 if (vb->planes[plane].m.offset == off) {
1831 * vb2_core_expbuf() - Export a buffer as a file descriptor
1832 * @q: videobuf2 queue
1833 * @fd: file descriptor associated with DMABUF (set by driver) *
1834 * @type: buffer type
1835 * @index: id number of the buffer
1836 * @plane: index of the plane to be exported, 0 for single plane queues
1837 * @flags: flags for newly created file, currently only O_CLOEXEC is
1838 * supported, refer to manual of open syscall for more details
1840 * The return values from this function are intended to be directly returned
1841 * from vidioc_expbuf handler in driver.
1843 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
1844 unsigned int index, unsigned int plane, unsigned int flags)
1846 struct vb2_buffer *vb = NULL;
1847 struct vb2_plane *vb_plane;
1849 struct dma_buf *dbuf;
1851 if (q->memory != VB2_MEMORY_MMAP) {
1852 dprintk(1, "queue is not currently set up for mmap\n");
1856 if (!q->mem_ops->get_dmabuf) {
1857 dprintk(1, "queue does not support DMA buffer exporting\n");
1861 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
1862 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
1866 if (type != q->type) {
1867 dprintk(1, "invalid buffer type\n");
1871 if (index >= q->num_buffers) {
1872 dprintk(1, "buffer index out of range\n");
1876 vb = q->bufs[index];
1878 if (plane >= vb->num_planes) {
1879 dprintk(1, "buffer plane out of range\n");
1883 if (vb2_fileio_is_active(q)) {
1884 dprintk(1, "expbuf: file io in progress\n");
1888 vb_plane = &vb->planes[plane];
1890 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
1892 if (IS_ERR_OR_NULL(dbuf)) {
1893 dprintk(1, "failed to export buffer %d, plane %d\n",
1898 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
1900 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1906 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1912 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
1915 * vb2_mmap() - map video buffers into application address space
1916 * @q: videobuf2 queue
1917 * @vma: vma passed to the mmap file operation handler in the driver
1919 * Should be called from mmap file operation handler of a driver.
1920 * This function maps one plane of one of the available video buffers to
1921 * userspace. To map whole video memory allocated on reqbufs, this function
1922 * has to be called once per each plane per each buffer previously allocated.
1924 * When the userspace application calls mmap, it passes to it an offset returned
1925 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1926 * a "cookie", which is then used to identify the plane to be mapped.
1927 * This function finds a plane with a matching offset and a mapping is performed
1928 * by the means of a provided memory operation.
1930 * The return values from this function are intended to be directly returned
1931 * from the mmap handler in driver.
1933 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1935 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1936 struct vb2_buffer *vb;
1937 unsigned int buffer = 0, plane = 0;
1939 unsigned long length;
1941 if (q->memory != VB2_MEMORY_MMAP) {
1942 dprintk(1, "queue is not currently set up for mmap\n");
1947 * Check memory area access mode.
1949 if (!(vma->vm_flags & VM_SHARED)) {
1950 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
1954 if (!(vma->vm_flags & VM_WRITE)) {
1955 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
1959 if (!(vma->vm_flags & VM_READ)) {
1960 dprintk(1, "invalid vma flags, VM_READ needed\n");
1964 if (vb2_fileio_is_active(q)) {
1965 dprintk(1, "mmap: file io in progress\n");
1970 * Find the plane corresponding to the offset passed by userspace.
1972 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1976 vb = q->bufs[buffer];
1979 * MMAP requires page_aligned buffers.
1980 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1981 * so, we need to do the same here.
1983 length = PAGE_ALIGN(vb->planes[plane].length);
1984 if (length < (vma->vm_end - vma->vm_start)) {
1986 "MMAP invalid, as it would overflow buffer length\n");
1990 mutex_lock(&q->mmap_lock);
1991 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
1992 mutex_unlock(&q->mmap_lock);
1996 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
1999 EXPORT_SYMBOL_GPL(vb2_mmap);
2002 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2005 unsigned long pgoff,
2006 unsigned long flags)
2008 unsigned long off = pgoff << PAGE_SHIFT;
2009 struct vb2_buffer *vb;
2010 unsigned int buffer, plane;
2014 if (q->memory != VB2_MEMORY_MMAP) {
2015 dprintk(1, "queue is not currently set up for mmap\n");
2020 * Find the plane corresponding to the offset passed by userspace.
2022 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2026 vb = q->bufs[buffer];
2028 vaddr = vb2_plane_vaddr(vb, plane);
2029 return vaddr ? (unsigned long)vaddr : -EINVAL;
2031 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2035 * vb2_core_queue_init() - initialize a videobuf2 queue
2036 * @q: videobuf2 queue; this structure should be allocated in driver
2038 * The vb2_queue structure should be allocated by the driver. The driver is
2039 * responsible of clearing it's content and setting initial values for some
2040 * required entries before calling this function.
2041 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2042 * to the struct vb2_queue description in include/media/videobuf2-core.h
2043 * for more information.
2045 int vb2_core_queue_init(struct vb2_queue *q)
2052 WARN_ON(!q->mem_ops) ||
2053 WARN_ON(!q->type) ||
2054 WARN_ON(!q->io_modes) ||
2055 WARN_ON(!q->ops->queue_setup) ||
2056 WARN_ON(!q->ops->buf_queue))
2059 INIT_LIST_HEAD(&q->queued_list);
2060 INIT_LIST_HEAD(&q->done_list);
2061 spin_lock_init(&q->done_lock);
2062 mutex_init(&q->mmap_lock);
2063 init_waitqueue_head(&q->done_wq);
2065 if (q->buf_struct_size == 0)
2066 q->buf_struct_size = sizeof(struct vb2_buffer);
2069 q->dma_dir = DMA_TO_DEVICE;
2071 q->dma_dir = q->use_dma_bidirectional
2072 ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2076 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2079 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2080 * @q: videobuf2 queue
2082 * This function stops streaming and performs necessary clean ups, including
2083 * freeing video buffer memory. The driver is responsible for freeing
2084 * the vb2_queue structure itself.
2086 void vb2_core_queue_release(struct vb2_queue *q)
2088 __vb2_queue_cancel(q);
2089 mutex_lock(&q->mmap_lock);
2090 __vb2_queue_free(q, q->num_buffers);
2091 mutex_unlock(&q->mmap_lock);
2093 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2095 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2096 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2097 MODULE_LICENSE("GPL");