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>
32 module_param(debug
, int, 0644);
34 #define dprintk(level, fmt, arg...) \
37 pr_info("vb2-core: %s: " fmt, __func__, ## arg); \
40 #ifdef CONFIG_VIDEO_ADV_DEBUG
43 * If advanced debugging is on, then count how often each op is called
44 * successfully, which can either be per-buffer or per-queue.
46 * This makes it easy to check that the 'init' and 'cleanup'
47 * (and variations thereof) stay balanced.
50 #define log_memop(vb, op) \
51 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
52 (vb)->vb2_queue, (vb)->index, #op, \
53 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
55 #define call_memop(vb, op, args...) \
57 struct vb2_queue *_q = (vb)->vb2_queue; \
61 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
63 (vb)->cnt_mem_ ## op++; \
67 #define call_ptr_memop(vb, op, args...) \
69 struct vb2_queue *_q = (vb)->vb2_queue; \
73 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
74 if (!IS_ERR_OR_NULL(ptr)) \
75 (vb)->cnt_mem_ ## op++; \
79 #define call_void_memop(vb, op, args...) \
81 struct vb2_queue *_q = (vb)->vb2_queue; \
84 if (_q->mem_ops->op) \
85 _q->mem_ops->op(args); \
86 (vb)->cnt_mem_ ## op++; \
89 #define log_qop(q, op) \
90 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
91 (q)->ops->op ? "" : " (nop)")
93 #define call_qop(q, op, args...) \
98 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
104 #define call_void_qop(q, op, args...) \
108 (q)->ops->op(args); \
112 #define log_vb_qop(vb, op, args...) \
113 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
114 (vb)->vb2_queue, (vb)->index, #op, \
115 (vb)->vb2_queue->ops->op ? "" : " (nop)")
117 #define call_vb_qop(vb, op, args...) \
121 log_vb_qop(vb, op); \
122 err = (vb)->vb2_queue->ops->op ? \
123 (vb)->vb2_queue->ops->op(args) : 0; \
125 (vb)->cnt_ ## op++; \
129 #define call_void_vb_qop(vb, op, args...) \
131 log_vb_qop(vb, op); \
132 if ((vb)->vb2_queue->ops->op) \
133 (vb)->vb2_queue->ops->op(args); \
134 (vb)->cnt_ ## op++; \
139 #define call_memop(vb, op, args...) \
140 ((vb)->vb2_queue->mem_ops->op ? \
141 (vb)->vb2_queue->mem_ops->op(args) : 0)
143 #define call_ptr_memop(vb, op, args...) \
144 ((vb)->vb2_queue->mem_ops->op ? \
145 (vb)->vb2_queue->mem_ops->op(args) : NULL)
147 #define call_void_memop(vb, op, args...) \
149 if ((vb)->vb2_queue->mem_ops->op) \
150 (vb)->vb2_queue->mem_ops->op(args); \
153 #define call_qop(q, op, args...) \
154 ((q)->ops->op ? (q)->ops->op(args) : 0)
156 #define call_void_qop(q, op, args...) \
159 (q)->ops->op(args); \
162 #define call_vb_qop(vb, op, args...) \
163 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
165 #define call_void_vb_qop(vb, op, args...) \
167 if ((vb)->vb2_queue->ops->op) \
168 (vb)->vb2_queue->ops->op(args); \
173 #define call_bufop(q, op, args...) \
176 if (q && q->buf_ops && q->buf_ops->op) \
177 ret = q->buf_ops->op(args); \
181 static void __vb2_queue_cancel(struct vb2_queue
*q
);
182 static void __enqueue_in_driver(struct vb2_buffer
*vb
);
185 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
187 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
189 struct vb2_queue
*q
= vb
->vb2_queue
;
190 enum dma_data_direction dma_dir
=
191 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
196 * Allocate memory for all planes in this buffer
197 * NOTE: mmapped areas should be page aligned
199 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
200 unsigned long size
= PAGE_ALIGN(q
->plane_sizes
[plane
]);
202 mem_priv
= call_ptr_memop(vb
, alloc
, q
->alloc_ctx
[plane
],
203 size
, dma_dir
, q
->gfp_flags
);
204 if (IS_ERR_OR_NULL(mem_priv
))
207 /* Associate allocator private data with this plane */
208 vb
->planes
[plane
].mem_priv
= mem_priv
;
209 vb
->planes
[plane
].length
= q
->plane_sizes
[plane
];
214 /* Free already allocated memory if one of the allocations failed */
215 for (; plane
> 0; --plane
) {
216 call_void_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
217 vb
->planes
[plane
- 1].mem_priv
= NULL
;
224 * __vb2_buf_mem_free() - free memory of the given buffer
226 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
230 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
231 call_void_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
232 vb
->planes
[plane
].mem_priv
= NULL
;
233 dprintk(3, "freed plane %d of buffer %d\n", plane
, vb
->index
);
238 * __vb2_buf_userptr_put() - release userspace memory associated with
241 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
245 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
246 if (vb
->planes
[plane
].mem_priv
)
247 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
248 vb
->planes
[plane
].mem_priv
= NULL
;
253 * __vb2_plane_dmabuf_put() - release memory associated with
254 * a DMABUF shared plane
256 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
262 call_void_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
264 call_void_memop(vb
, detach_dmabuf
, p
->mem_priv
);
265 dma_buf_put(p
->dbuf
);
272 * __vb2_buf_dmabuf_put() - release memory associated with
273 * a DMABUF shared buffer
275 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
279 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
280 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
284 * __setup_lengths() - setup initial lengths for every plane in
285 * every buffer on the queue
287 static void __setup_lengths(struct vb2_queue
*q
, unsigned int n
)
289 unsigned int buffer
, plane
;
290 struct vb2_buffer
*vb
;
292 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
293 vb
= q
->bufs
[buffer
];
297 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
298 vb
->planes
[plane
].length
= q
->plane_sizes
[plane
];
303 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
304 * every buffer on the queue
306 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
308 unsigned int buffer
, plane
;
309 struct vb2_buffer
*vb
;
312 if (q
->num_buffers
) {
314 vb
= q
->bufs
[q
->num_buffers
- 1];
315 p
= &vb
->planes
[vb
->num_planes
- 1];
316 off
= PAGE_ALIGN(p
->m
.offset
+ p
->length
);
321 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
322 vb
= q
->bufs
[buffer
];
326 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
327 vb
->planes
[plane
].m
.offset
= off
;
329 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
332 off
+= vb
->planes
[plane
].length
;
333 off
= PAGE_ALIGN(off
);
339 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
340 * video buffer memory for all buffers/planes on the queue and initializes the
343 * Returns the number of buffers successfully allocated.
345 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum vb2_memory memory
,
346 unsigned int num_buffers
, unsigned int num_planes
)
349 struct vb2_buffer
*vb
;
352 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
353 /* Allocate videobuf buffer structures */
354 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
356 dprintk(1, "memory alloc for buffer struct failed\n");
360 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
362 vb
->num_planes
= num_planes
;
363 vb
->index
= q
->num_buffers
+ buffer
;
367 /* Allocate video buffer memory for the MMAP type */
368 if (memory
== VB2_MEMORY_MMAP
) {
369 ret
= __vb2_buf_mem_alloc(vb
);
371 dprintk(1, "failed allocating memory for "
372 "buffer %d\n", buffer
);
377 * Call the driver-provided buffer initialization
378 * callback, if given. An error in initialization
379 * results in queue setup failure.
381 ret
= call_vb_qop(vb
, buf_init
, vb
);
383 dprintk(1, "buffer %d %p initialization"
384 " failed\n", buffer
, vb
);
385 __vb2_buf_mem_free(vb
);
391 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
394 __setup_lengths(q
, buffer
);
395 if (memory
== VB2_MEMORY_MMAP
)
396 __setup_offsets(q
, buffer
);
398 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
405 * __vb2_free_mem() - release all video buffer memory for a given queue
407 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
410 struct vb2_buffer
*vb
;
412 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
414 vb
= q
->bufs
[buffer
];
418 /* Free MMAP buffers or release USERPTR buffers */
419 if (q
->memory
== VB2_MEMORY_MMAP
)
420 __vb2_buf_mem_free(vb
);
421 else if (q
->memory
== VB2_MEMORY_DMABUF
)
422 __vb2_buf_dmabuf_put(vb
);
424 __vb2_buf_userptr_put(vb
);
429 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
430 * related information, if no buffers are left return the queue to an
431 * uninitialized state. Might be called even if the queue has already been freed.
433 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
438 * Sanity check: when preparing a buffer the queue lock is released for
439 * a short while (see __buf_prepare for the details), which would allow
440 * a race with a reqbufs which can call this function. Removing the
441 * buffers from underneath __buf_prepare is obviously a bad idea, so we
442 * check if any of the buffers is in the state PREPARING, and if so we
443 * just return -EAGAIN.
445 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
447 if (q
->bufs
[buffer
] == NULL
)
449 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
450 dprintk(1, "preparing buffers, cannot free\n");
455 /* Call driver-provided cleanup function for each buffer, if provided */
456 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
458 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
460 if (vb
&& vb
->planes
[0].mem_priv
)
461 call_void_vb_qop(vb
, buf_cleanup
, vb
);
464 /* Release video buffer memory */
465 __vb2_free_mem(q
, buffers
);
467 #ifdef CONFIG_VIDEO_ADV_DEBUG
469 * Check that all the calls were balances during the life-time of this
470 * queue. If not (or if the debug level is 1 or up), then dump the
471 * counters to the kernel log.
473 if (q
->num_buffers
) {
474 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
475 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
477 if (unbalanced
|| debug
) {
478 pr_info("vb2: counters for queue %p:%s\n", q
,
479 unbalanced
? " UNBALANCED!" : "");
480 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
481 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
482 q
->cnt_stop_streaming
);
483 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
484 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
486 q
->cnt_queue_setup
= 0;
487 q
->cnt_wait_prepare
= 0;
488 q
->cnt_wait_finish
= 0;
489 q
->cnt_start_streaming
= 0;
490 q
->cnt_stop_streaming
= 0;
492 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
493 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
494 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
495 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
496 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
497 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
498 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
499 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
500 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
501 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
503 if (unbalanced
|| debug
) {
504 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
505 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
506 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
507 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
508 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
509 pr_info("vb2: buf_queue: %u buf_done: %u\n",
510 vb
->cnt_buf_queue
, vb
->cnt_buf_done
);
511 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
512 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
513 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
515 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
516 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
517 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
518 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
519 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
520 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
521 vb
->cnt_mem_get_dmabuf
,
522 vb
->cnt_mem_num_users
,
529 /* Free videobuf buffers */
530 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
532 kfree(q
->bufs
[buffer
]);
533 q
->bufs
[buffer
] = NULL
;
536 q
->num_buffers
-= buffers
;
537 if (!q
->num_buffers
) {
539 INIT_LIST_HEAD(&q
->queued_list
);
545 * vb2_buffer_in_use() - return true if the buffer is in use and
546 * the queue cannot be freed (by the means of REQBUFS(0)) call
548 bool vb2_buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
551 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
552 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
554 * If num_users() has not been provided, call_memop
555 * will return 0, apparently nobody cares about this
556 * case anyway. If num_users() returns more than 1,
557 * we are not the only user of the plane's memory.
559 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
564 EXPORT_SYMBOL(vb2_buffer_in_use
);
567 * __buffers_in_use() - return true if any buffers on the queue are in use and
568 * the queue cannot be freed (by the means of REQBUFS(0)) call
570 static bool __buffers_in_use(struct vb2_queue
*q
)
573 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
574 if (vb2_buffer_in_use(q
, q
->bufs
[buffer
]))
581 * vb2_core_querybuf() - query video buffer information
583 * @index: id number of the buffer
584 * @pb: buffer struct passed from userspace
586 * Should be called from vidioc_querybuf ioctl handler in driver.
587 * The passed buffer should have been verified.
588 * This function fills the relevant information for the userspace.
590 * The return values from this function are intended to be directly returned
591 * from vidioc_querybuf handler in driver.
593 int vb2_core_querybuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
595 return call_bufop(q
, fill_user_buffer
, q
->bufs
[index
], pb
);
597 EXPORT_SYMBOL_GPL(vb2_core_querybuf
);
600 * __verify_userptr_ops() - verify that all memory operations required for
601 * USERPTR queue type have been provided
603 static int __verify_userptr_ops(struct vb2_queue
*q
)
605 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
606 !q
->mem_ops
->put_userptr
)
613 * __verify_mmap_ops() - verify that all memory operations required for
614 * MMAP queue type have been provided
616 static int __verify_mmap_ops(struct vb2_queue
*q
)
618 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
619 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
626 * __verify_dmabuf_ops() - verify that all memory operations required for
627 * DMABUF queue type have been provided
629 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
631 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
632 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
633 !q
->mem_ops
->unmap_dmabuf
)
640 * vb2_verify_memory_type() - Check whether the memory type and buffer type
641 * passed to a buffer operation are compatible with the queue.
643 int vb2_verify_memory_type(struct vb2_queue
*q
,
644 enum vb2_memory memory
, unsigned int type
)
646 if (memory
!= VB2_MEMORY_MMAP
&& memory
!= VB2_MEMORY_USERPTR
&&
647 memory
!= VB2_MEMORY_DMABUF
) {
648 dprintk(1, "unsupported memory type\n");
652 if (type
!= q
->type
) {
653 dprintk(1, "requested type is incorrect\n");
658 * Make sure all the required memory ops for given memory type
661 if (memory
== VB2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
662 dprintk(1, "MMAP for current setup unsupported\n");
666 if (memory
== VB2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
667 dprintk(1, "USERPTR for current setup unsupported\n");
671 if (memory
== VB2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
672 dprintk(1, "DMABUF for current setup unsupported\n");
677 * Place the busy tests at the end: -EBUSY can be ignored when
678 * create_bufs is called with count == 0, but count == 0 should still
679 * do the memory and type validation.
681 if (vb2_fileio_is_active(q
)) {
682 dprintk(1, "file io in progress\n");
687 EXPORT_SYMBOL(vb2_verify_memory_type
);
690 * vb2_core_reqbufs() - Initiate streaming
691 * @q: videobuf2 queue
692 * @memory: memory type
693 * @count: requested buffer count
695 * Should be called from vidioc_reqbufs ioctl handler of a driver.
697 * 1) verifies streaming parameters passed from the userspace,
698 * 2) sets up the queue,
699 * 3) negotiates number of buffers and planes per buffer with the driver
700 * to be used during streaming,
701 * 4) allocates internal buffer structures (struct vb2_buffer), according to
702 * the agreed parameters,
703 * 5) for MMAP memory type, allocates actual video memory, using the
704 * memory handling/allocation routines provided during queue initialization
706 * If req->count is 0, all the memory will be freed instead.
707 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
708 * and the queue is not busy, memory will be reallocated.
710 * The return values from this function are intended to be directly returned
711 * from vidioc_reqbufs handler in driver.
713 int vb2_core_reqbufs(struct vb2_queue
*q
, enum vb2_memory memory
,
716 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
720 dprintk(1, "streaming active\n");
724 if (*count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= memory
) {
726 * We already have buffers allocated, so first check if they
727 * are not in use and can be freed.
729 mutex_lock(&q
->mmap_lock
);
730 if (q
->memory
== VB2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
731 mutex_unlock(&q
->mmap_lock
);
732 dprintk(1, "memory in use, cannot free\n");
737 * Call queue_cancel to clean up any buffers in the PREPARED or
738 * QUEUED state which is possible if buffers were prepared or
739 * queued without ever calling STREAMON.
741 __vb2_queue_cancel(q
);
742 ret
= __vb2_queue_free(q
, q
->num_buffers
);
743 mutex_unlock(&q
->mmap_lock
);
748 * In case of REQBUFS(0) return immediately without calling
749 * driver's queue_setup() callback and allocating resources.
756 * Make sure the requested values and current defaults are sane.
758 num_buffers
= min_t(unsigned int, *count
, VB2_MAX_FRAME
);
759 num_buffers
= max_t(unsigned int, num_buffers
, q
->min_buffers_needed
);
760 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
761 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
765 * Ask the driver how many buffers and planes per buffer it requires.
766 * Driver also sets the size and allocator context for each plane.
768 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
769 q
->plane_sizes
, q
->alloc_ctx
);
773 /* Finally, allocate buffers and video memory */
775 __vb2_queue_alloc(q
, memory
, num_buffers
, num_planes
);
776 if (allocated_buffers
== 0) {
777 dprintk(1, "memory allocation failed\n");
782 * There is no point in continuing if we can't allocate the minimum
783 * number of buffers needed by this vb2_queue.
785 if (allocated_buffers
< q
->min_buffers_needed
)
789 * Check if driver can handle the allocated number of buffers.
791 if (!ret
&& allocated_buffers
< num_buffers
) {
792 num_buffers
= allocated_buffers
;
794 * num_planes is set by the previous queue_setup(), but since it
795 * signals to queue_setup() whether it is called from create_bufs()
796 * vs reqbufs() we zero it here to signal that queue_setup() is
797 * called for the reqbufs() case.
801 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
802 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
804 if (!ret
&& allocated_buffers
< num_buffers
)
808 * Either the driver has accepted a smaller number of buffers,
809 * or .queue_setup() returned an error
813 mutex_lock(&q
->mmap_lock
);
814 q
->num_buffers
= allocated_buffers
;
818 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
819 * from q->num_buffers.
821 __vb2_queue_free(q
, allocated_buffers
);
822 mutex_unlock(&q
->mmap_lock
);
825 mutex_unlock(&q
->mmap_lock
);
828 * Return the number of successfully allocated buffers
831 *count
= allocated_buffers
;
832 q
->waiting_for_buffers
= !q
->is_output
;
836 EXPORT_SYMBOL_GPL(vb2_core_reqbufs
);
839 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
840 * @q: videobuf2 queue
841 * @memory: memory type
842 * @count: requested buffer count
843 * @parg: parameter passed to device driver
845 * Should be called from vidioc_create_bufs ioctl handler of a driver.
847 * 1) verifies parameter sanity
848 * 2) calls the .queue_setup() queue operation
849 * 3) performs any necessary memory allocations
851 * The return values from this function are intended to be directly returned
852 * from vidioc_create_bufs handler in driver.
854 int vb2_core_create_bufs(struct vb2_queue
*q
, enum vb2_memory memory
,
855 unsigned int *count
, unsigned requested_planes
,
856 const unsigned requested_sizes
[])
858 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
861 if (q
->num_buffers
== VB2_MAX_FRAME
) {
862 dprintk(1, "maximum number of buffers already allocated\n");
866 if (!q
->num_buffers
) {
867 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
868 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
870 q
->waiting_for_buffers
= !q
->is_output
;
873 num_buffers
= min(*count
, VB2_MAX_FRAME
- q
->num_buffers
);
875 if (requested_planes
&& requested_sizes
) {
876 num_planes
= requested_planes
;
877 memcpy(q
->plane_sizes
, requested_sizes
, sizeof(q
->plane_sizes
));
881 * Ask the driver, whether the requested number of buffers, planes per
882 * buffer and their sizes are acceptable
884 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
885 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
889 /* Finally, allocate buffers and video memory */
890 allocated_buffers
= __vb2_queue_alloc(q
, memory
, num_buffers
,
892 if (allocated_buffers
== 0) {
893 dprintk(1, "memory allocation failed\n");
898 * Check if driver can handle the so far allocated number of buffers.
900 if (allocated_buffers
< num_buffers
) {
901 num_buffers
= allocated_buffers
;
904 * q->num_buffers contains the total number of buffers, that the
905 * queue driver has set up
907 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
908 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
910 if (!ret
&& allocated_buffers
< num_buffers
)
914 * Either the driver has accepted a smaller number of buffers,
915 * or .queue_setup() returned an error
919 mutex_lock(&q
->mmap_lock
);
920 q
->num_buffers
+= allocated_buffers
;
924 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
925 * from q->num_buffers.
927 __vb2_queue_free(q
, allocated_buffers
);
928 mutex_unlock(&q
->mmap_lock
);
931 mutex_unlock(&q
->mmap_lock
);
934 * Return the number of successfully allocated buffers
937 *count
= allocated_buffers
;
941 EXPORT_SYMBOL_GPL(vb2_core_create_bufs
);
944 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
945 * @vb: vb2_buffer to which the plane in question belongs to
946 * @plane_no: plane number for which the address is to be returned
948 * This function returns a kernel virtual address of a given plane if
949 * such a mapping exist, NULL otherwise.
951 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
953 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
956 return call_ptr_memop(vb
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
959 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
962 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
963 * @vb: vb2_buffer to which the plane in question belongs to
964 * @plane_no: plane number for which the cookie is to be returned
966 * This function returns an allocator specific cookie for a given plane if
967 * available, NULL otherwise. The allocator should provide some simple static
968 * inline function, which would convert this cookie to the allocator specific
969 * type that can be used directly by the driver to access the buffer. This can
970 * be for example physical address, pointer to scatter list or IOMMU mapping.
972 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
974 if (plane_no
>= vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
977 return call_ptr_memop(vb
, cookie
, vb
->planes
[plane_no
].mem_priv
);
979 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
982 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
983 * @vb: vb2_buffer returned from the driver
984 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
985 * VB2_BUF_STATE_ERROR if the operation finished with an error or
986 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
987 * If start_streaming fails then it should return buffers with state
988 * VB2_BUF_STATE_QUEUED to put them back into the queue.
990 * This function should be called by the driver after a hardware operation on
991 * a buffer is finished and the buffer may be returned to userspace. The driver
992 * cannot use this buffer anymore until it is queued back to it by videobuf
993 * by the means of buf_queue callback. Only buffers previously queued to the
994 * driver by buf_queue can be passed to this function.
996 * While streaming a buffer can only be returned in state DONE or ERROR.
997 * The start_streaming op can also return them in case the DMA engine cannot
998 * be started for some reason. In that case the buffers should be returned with
1001 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
1003 struct vb2_queue
*q
= vb
->vb2_queue
;
1004 unsigned long flags
;
1007 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_ACTIVE
))
1010 if (WARN_ON(state
!= VB2_BUF_STATE_DONE
&&
1011 state
!= VB2_BUF_STATE_ERROR
&&
1012 state
!= VB2_BUF_STATE_QUEUED
&&
1013 state
!= VB2_BUF_STATE_REQUEUEING
))
1014 state
= VB2_BUF_STATE_ERROR
;
1016 #ifdef CONFIG_VIDEO_ADV_DEBUG
1018 * Although this is not a callback, it still does have to balance
1019 * with the buf_queue op. So update this counter manually.
1023 dprintk(4, "done processing on buffer %d, state: %d\n",
1027 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1028 call_void_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
1030 spin_lock_irqsave(&q
->done_lock
, flags
);
1031 if (state
== VB2_BUF_STATE_QUEUED
||
1032 state
== VB2_BUF_STATE_REQUEUEING
) {
1033 vb
->state
= VB2_BUF_STATE_QUEUED
;
1035 /* Add the buffer to the done buffers list */
1036 list_add_tail(&vb
->done_entry
, &q
->done_list
);
1039 atomic_dec(&q
->owned_by_drv_count
);
1040 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1042 trace_vb2_buf_done(q
, vb
);
1045 case VB2_BUF_STATE_QUEUED
:
1047 case VB2_BUF_STATE_REQUEUEING
:
1048 if (q
->start_streaming_called
)
1049 __enqueue_in_driver(vb
);
1052 /* Inform any processes that may be waiting for buffers */
1053 wake_up(&q
->done_wq
);
1057 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
1060 * vb2_discard_done() - discard all buffers marked as DONE
1061 * @q: videobuf2 queue
1063 * This function is intended to be used with suspend/resume operations. It
1064 * discards all 'done' buffers as they would be too old to be requested after
1067 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1068 * delayed works before calling this function to make sure no buffer will be
1069 * touched by the driver and/or hardware.
1071 void vb2_discard_done(struct vb2_queue
*q
)
1073 struct vb2_buffer
*vb
;
1074 unsigned long flags
;
1076 spin_lock_irqsave(&q
->done_lock
, flags
);
1077 list_for_each_entry(vb
, &q
->done_list
, done_entry
)
1078 vb
->state
= VB2_BUF_STATE_ERROR
;
1079 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1081 EXPORT_SYMBOL_GPL(vb2_discard_done
);
1084 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1086 static int __qbuf_mmap(struct vb2_buffer
*vb
, const void *pb
)
1088 int ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1089 vb
, pb
, vb
->planes
);
1090 return ret
? ret
: call_vb_qop(vb
, buf_prepare
, vb
);
1094 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1096 static int __qbuf_userptr(struct vb2_buffer
*vb
, const void *pb
)
1098 struct vb2_plane planes
[VB2_MAX_PLANES
];
1099 struct vb2_queue
*q
= vb
->vb2_queue
;
1103 enum dma_data_direction dma_dir
=
1104 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1105 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1107 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1108 /* Copy relevant information provided by the userspace */
1109 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
, vb
, pb
, planes
);
1113 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1114 /* Skip the plane if already verified */
1115 if (vb
->planes
[plane
].m
.userptr
&&
1116 vb
->planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1117 && vb
->planes
[plane
].length
== planes
[plane
].length
)
1120 dprintk(3, "userspace address for plane %d changed, "
1121 "reacquiring memory\n", plane
);
1123 /* Check if the provided plane buffer is large enough */
1124 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
1125 dprintk(1, "provided buffer size %u is less than "
1126 "setup size %u for plane %d\n",
1127 planes
[plane
].length
,
1128 q
->plane_sizes
[plane
], plane
);
1133 /* Release previously acquired memory if present */
1134 if (vb
->planes
[plane
].mem_priv
) {
1137 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1139 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1142 vb
->planes
[plane
].mem_priv
= NULL
;
1143 vb
->planes
[plane
].bytesused
= 0;
1144 vb
->planes
[plane
].length
= 0;
1145 vb
->planes
[plane
].m
.userptr
= 0;
1146 vb
->planes
[plane
].data_offset
= 0;
1148 /* Acquire each plane's memory */
1149 mem_priv
= call_ptr_memop(vb
, get_userptr
, q
->alloc_ctx
[plane
],
1150 planes
[plane
].m
.userptr
,
1151 planes
[plane
].length
, dma_dir
);
1152 if (IS_ERR_OR_NULL(mem_priv
)) {
1153 dprintk(1, "failed acquiring userspace "
1154 "memory for plane %d\n", plane
);
1155 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
1158 vb
->planes
[plane
].mem_priv
= mem_priv
;
1162 * Now that everything is in order, copy relevant information
1163 * provided by userspace.
1165 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1166 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1167 vb
->planes
[plane
].length
= planes
[plane
].length
;
1168 vb
->planes
[plane
].m
.userptr
= planes
[plane
].m
.userptr
;
1169 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1174 * One or more planes changed, so we must call buf_init to do
1175 * the driver-specific initialization on the newly acquired
1176 * buffer, if provided.
1178 ret
= call_vb_qop(vb
, buf_init
, vb
);
1180 dprintk(1, "buffer initialization failed\n");
1185 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1187 dprintk(1, "buffer preparation failed\n");
1188 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1194 /* In case of errors, release planes that were already acquired */
1195 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1196 if (vb
->planes
[plane
].mem_priv
)
1197 call_void_memop(vb
, put_userptr
,
1198 vb
->planes
[plane
].mem_priv
);
1199 vb
->planes
[plane
].mem_priv
= NULL
;
1200 vb
->planes
[plane
].m
.userptr
= 0;
1201 vb
->planes
[plane
].length
= 0;
1208 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1210 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const void *pb
)
1212 struct vb2_plane planes
[VB2_MAX_PLANES
];
1213 struct vb2_queue
*q
= vb
->vb2_queue
;
1217 enum dma_data_direction dma_dir
=
1218 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1219 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1221 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1222 /* Copy relevant information provided by the userspace */
1223 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
, vb
, pb
, planes
);
1227 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1228 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1230 if (IS_ERR_OR_NULL(dbuf
)) {
1231 dprintk(1, "invalid dmabuf fd for plane %d\n",
1237 /* use DMABUF size if length is not provided */
1238 if (planes
[plane
].length
== 0)
1239 planes
[plane
].length
= dbuf
->size
;
1241 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
1242 dprintk(1, "invalid dmabuf length for plane %d\n",
1248 /* Skip the plane if already verified */
1249 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1250 vb
->planes
[plane
].length
== planes
[plane
].length
) {
1255 dprintk(1, "buffer for plane %d changed\n", plane
);
1259 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1262 /* Release previously acquired memory if present */
1263 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1264 vb
->planes
[plane
].bytesused
= 0;
1265 vb
->planes
[plane
].length
= 0;
1266 vb
->planes
[plane
].m
.fd
= 0;
1267 vb
->planes
[plane
].data_offset
= 0;
1269 /* Acquire each plane's memory */
1270 mem_priv
= call_ptr_memop(vb
, attach_dmabuf
,
1271 q
->alloc_ctx
[plane
], dbuf
, planes
[plane
].length
,
1273 if (IS_ERR(mem_priv
)) {
1274 dprintk(1, "failed to attach dmabuf\n");
1275 ret
= PTR_ERR(mem_priv
);
1280 vb
->planes
[plane
].dbuf
= dbuf
;
1281 vb
->planes
[plane
].mem_priv
= mem_priv
;
1284 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1285 * really we want to do this just before the DMA, not while queueing
1288 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1289 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1291 dprintk(1, "failed to map dmabuf for plane %d\n",
1295 vb
->planes
[plane
].dbuf_mapped
= 1;
1299 * Now that everything is in order, copy relevant information
1300 * provided by userspace.
1302 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1303 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1304 vb
->planes
[plane
].length
= planes
[plane
].length
;
1305 vb
->planes
[plane
].m
.fd
= planes
[plane
].m
.fd
;
1306 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1311 * Call driver-specific initialization on the newly acquired buffer,
1314 ret
= call_vb_qop(vb
, buf_init
, vb
);
1316 dprintk(1, "buffer initialization failed\n");
1321 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1323 dprintk(1, "buffer preparation failed\n");
1324 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1330 /* In case of errors, release planes that were already acquired */
1331 __vb2_buf_dmabuf_put(vb
);
1337 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1339 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1341 struct vb2_queue
*q
= vb
->vb2_queue
;
1344 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1345 atomic_inc(&q
->owned_by_drv_count
);
1347 trace_vb2_buf_queue(q
, vb
);
1350 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1351 call_void_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1353 call_void_vb_qop(vb
, buf_queue
, vb
);
1356 static int __buf_prepare(struct vb2_buffer
*vb
, const void *pb
)
1358 struct vb2_queue
*q
= vb
->vb2_queue
;
1362 dprintk(1, "fatal error occurred on queue\n");
1366 vb
->state
= VB2_BUF_STATE_PREPARING
;
1368 switch (q
->memory
) {
1369 case VB2_MEMORY_MMAP
:
1370 ret
= __qbuf_mmap(vb
, pb
);
1372 case VB2_MEMORY_USERPTR
:
1373 ret
= __qbuf_userptr(vb
, pb
);
1375 case VB2_MEMORY_DMABUF
:
1376 ret
= __qbuf_dmabuf(vb
, pb
);
1379 WARN(1, "Invalid queue type\n");
1384 dprintk(1, "buffer preparation failed: %d\n", ret
);
1385 vb
->state
= ret
? VB2_BUF_STATE_DEQUEUED
: VB2_BUF_STATE_PREPARED
;
1391 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1393 * @q: videobuf2 queue
1394 * @index: id number of the buffer
1395 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1398 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1399 * The passed buffer should have been verified.
1400 * This function calls buf_prepare callback in the driver (if provided),
1401 * in which driver-specific buffer initialization can be performed,
1403 * The return values from this function are intended to be directly returned
1404 * from vidioc_prepare_buf handler in driver.
1406 int vb2_core_prepare_buf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1408 struct vb2_buffer
*vb
;
1411 vb
= q
->bufs
[index
];
1412 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1413 dprintk(1, "invalid buffer state %d\n",
1418 ret
= __buf_prepare(vb
, pb
);
1422 /* Fill buffer information for the userspace */
1423 ret
= call_bufop(q
, fill_user_buffer
, vb
, pb
);
1427 dprintk(1, "prepare of buffer %d succeeded\n", vb
->index
);
1431 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf
);
1434 * vb2_start_streaming() - Attempt to start streaming.
1435 * @q: videobuf2 queue
1437 * Attempt to start streaming. When this function is called there must be
1438 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1439 * number of buffers required for the DMA engine to function). If the
1440 * @start_streaming op fails it is supposed to return all the driver-owned
1441 * buffers back to vb2 in state QUEUED. Check if that happened and if
1442 * not warn and reclaim them forcefully.
1444 static int vb2_start_streaming(struct vb2_queue
*q
)
1446 struct vb2_buffer
*vb
;
1450 * If any buffers were queued before streamon,
1451 * we can now pass them to driver for processing.
1453 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1454 __enqueue_in_driver(vb
);
1456 /* Tell the driver to start streaming */
1457 q
->start_streaming_called
= 1;
1458 ret
= call_qop(q
, start_streaming
, q
,
1459 atomic_read(&q
->owned_by_drv_count
));
1463 q
->start_streaming_called
= 0;
1465 dprintk(1, "driver refused to start streaming\n");
1467 * If you see this warning, then the driver isn't cleaning up properly
1468 * after a failed start_streaming(). See the start_streaming()
1469 * documentation in videobuf2-core.h for more information how buffers
1470 * should be returned to vb2 in start_streaming().
1472 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1476 * Forcefully reclaim buffers if the driver did not
1477 * correctly return them to vb2.
1479 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1481 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1482 vb2_buffer_done(vb
, VB2_BUF_STATE_QUEUED
);
1484 /* Must be zero now */
1485 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1488 * If done_list is not empty, then start_streaming() didn't call
1489 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1492 WARN_ON(!list_empty(&q
->done_list
));
1497 * vb2_core_qbuf() - Queue a buffer from userspace
1498 * @q: videobuf2 queue
1499 * @index: id number of the buffer
1500 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1503 * Should be called from vidioc_qbuf ioctl handler of a driver.
1504 * The passed buffer should have been verified.
1506 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1507 * which driver-specific buffer initialization can be performed,
1508 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1509 * callback for processing.
1511 * The return values from this function are intended to be directly returned
1512 * from vidioc_qbuf handler in driver.
1514 int vb2_core_qbuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1516 struct vb2_buffer
*vb
;
1519 vb
= q
->bufs
[index
];
1521 switch (vb
->state
) {
1522 case VB2_BUF_STATE_DEQUEUED
:
1523 ret
= __buf_prepare(vb
, pb
);
1527 case VB2_BUF_STATE_PREPARED
:
1529 case VB2_BUF_STATE_PREPARING
:
1530 dprintk(1, "buffer still being prepared\n");
1533 dprintk(1, "invalid buffer state %d\n", vb
->state
);
1538 * Add to the queued buffers list, a buffer will stay on it until
1539 * dequeued in dqbuf.
1541 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1543 q
->waiting_for_buffers
= false;
1544 vb
->state
= VB2_BUF_STATE_QUEUED
;
1546 call_bufop(q
, copy_timestamp
, vb
, pb
);
1548 trace_vb2_qbuf(q
, vb
);
1551 * If already streaming, give the buffer to driver for processing.
1552 * If not, the buffer will be given to driver on next streamon.
1554 if (q
->start_streaming_called
)
1555 __enqueue_in_driver(vb
);
1557 /* Fill buffer information for the userspace */
1558 ret
= call_bufop(q
, fill_user_buffer
, vb
, pb
);
1563 * If streamon has been called, and we haven't yet called
1564 * start_streaming() since not enough buffers were queued, and
1565 * we now have reached the minimum number of queued buffers,
1566 * then we can finally call start_streaming().
1568 if (q
->streaming
&& !q
->start_streaming_called
&&
1569 q
->queued_count
>= q
->min_buffers_needed
) {
1570 ret
= vb2_start_streaming(q
);
1575 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->index
);
1578 EXPORT_SYMBOL_GPL(vb2_core_qbuf
);
1581 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1584 * Will sleep if required for nonblocking == false.
1586 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1589 * All operations on vb_done_list are performed under done_lock
1590 * spinlock protection. However, buffers may be removed from
1591 * it and returned to userspace only while holding both driver's
1592 * lock and the done_lock spinlock. Thus we can be sure that as
1593 * long as we hold the driver's lock, the list will remain not
1594 * empty if list_empty() check succeeds.
1600 if (!q
->streaming
) {
1601 dprintk(1, "streaming off, will not wait for buffers\n");
1606 dprintk(1, "Queue in error state, will not wait for buffers\n");
1610 if (q
->last_buffer_dequeued
) {
1611 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1615 if (!list_empty(&q
->done_list
)) {
1617 * Found a buffer that we were waiting for.
1623 dprintk(1, "nonblocking and no buffers to dequeue, "
1629 * We are streaming and blocking, wait for another buffer to
1630 * become ready or for streamoff. Driver's lock is released to
1631 * allow streamoff or qbuf to be called while waiting.
1633 call_void_qop(q
, wait_prepare
, q
);
1636 * All locks have been released, it is safe to sleep now.
1638 dprintk(3, "will sleep waiting for buffers\n");
1639 ret
= wait_event_interruptible(q
->done_wq
,
1640 !list_empty(&q
->done_list
) || !q
->streaming
||
1644 * We need to reevaluate both conditions again after reacquiring
1645 * the locks or return an error if one occurred.
1647 call_void_qop(q
, wait_finish
, q
);
1649 dprintk(1, "sleep was interrupted\n");
1657 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1659 * Will sleep if required for nonblocking == false.
1661 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1664 unsigned long flags
;
1668 * Wait for at least one buffer to become available on the done_list.
1670 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1675 * Driver's lock has been held since we last verified that done_list
1676 * is not empty, so no need for another list_empty(done_list) check.
1678 spin_lock_irqsave(&q
->done_lock
, flags
);
1679 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1681 * Only remove the buffer from done_list if v4l2_buffer can handle all
1683 * Verifying planes is NOT necessary since it already has been checked
1684 * before the buffer is queued/prepared. So it can never fail.
1686 list_del(&(*vb
)->done_entry
);
1687 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1693 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1694 * @q: videobuf2 queue
1696 * This function will wait until all buffers that have been given to the driver
1697 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1698 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1699 * taken, for example from stop_streaming() callback.
1701 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1703 if (!q
->streaming
) {
1704 dprintk(1, "streaming off, will not wait for buffers\n");
1708 if (q
->start_streaming_called
)
1709 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1712 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1715 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1717 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1719 struct vb2_queue
*q
= vb
->vb2_queue
;
1722 /* nothing to do if the buffer is already dequeued */
1723 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1726 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1728 /* unmap DMABUF buffer */
1729 if (q
->memory
== VB2_MEMORY_DMABUF
)
1730 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1731 if (!vb
->planes
[i
].dbuf_mapped
)
1733 call_void_memop(vb
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1734 vb
->planes
[i
].dbuf_mapped
= 0;
1739 * vb2_dqbuf() - Dequeue a buffer to the userspace
1740 * @q: videobuf2 queue
1741 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1743 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1744 * buffers ready for dequeuing are present. Normally the driver
1745 * would be passing (file->f_flags & O_NONBLOCK) here
1747 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1748 * The passed buffer should have been verified.
1750 * 1) calls buf_finish callback in the driver (if provided), in which
1751 * driver can perform any additional operations that may be required before
1752 * returning the buffer to userspace, such as cache sync,
1753 * 2) the buffer struct members are filled with relevant information for
1756 * The return values from this function are intended to be directly returned
1757 * from vidioc_dqbuf handler in driver.
1759 int vb2_core_dqbuf(struct vb2_queue
*q
, void *pb
, bool nonblocking
)
1761 struct vb2_buffer
*vb
= NULL
;
1764 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1768 switch (vb
->state
) {
1769 case VB2_BUF_STATE_DONE
:
1770 dprintk(3, "returning done buffer\n");
1772 case VB2_BUF_STATE_ERROR
:
1773 dprintk(3, "returning done buffer with errors\n");
1776 dprintk(1, "invalid buffer state\n");
1780 call_void_vb_qop(vb
, buf_finish
, vb
);
1782 /* Fill buffer information for the userspace */
1783 ret
= call_bufop(q
, fill_user_buffer
, vb
, pb
);
1787 /* Remove from videobuf queue */
1788 list_del(&vb
->queued_entry
);
1791 trace_vb2_dqbuf(q
, vb
);
1793 /* go back to dequeued state */
1796 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1797 vb
->index
, vb
->state
);
1802 EXPORT_SYMBOL_GPL(vb2_core_dqbuf
);
1805 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1807 * Removes all queued buffers from driver's queue and all buffers queued by
1808 * userspace from videobuf's queue. Returns to state after reqbufs.
1810 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1815 * Tell driver to stop all transactions and release all queued
1818 if (q
->start_streaming_called
)
1819 call_void_qop(q
, stop_streaming
, q
);
1822 * If you see this warning, then the driver isn't cleaning up properly
1823 * in stop_streaming(). See the stop_streaming() documentation in
1824 * videobuf2-core.h for more information how buffers should be returned
1825 * to vb2 in stop_streaming().
1827 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1828 for (i
= 0; i
< q
->num_buffers
; ++i
)
1829 if (q
->bufs
[i
]->state
== VB2_BUF_STATE_ACTIVE
)
1830 vb2_buffer_done(q
->bufs
[i
], VB2_BUF_STATE_ERROR
);
1831 /* Must be zero now */
1832 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1836 q
->start_streaming_called
= 0;
1837 q
->queued_count
= 0;
1841 * Remove all buffers from videobuf's list...
1843 INIT_LIST_HEAD(&q
->queued_list
);
1845 * ...and done list; userspace will not receive any buffers it
1846 * has not already dequeued before initiating cancel.
1848 INIT_LIST_HEAD(&q
->done_list
);
1849 atomic_set(&q
->owned_by_drv_count
, 0);
1850 wake_up_all(&q
->done_wq
);
1853 * Reinitialize all buffers for next use.
1854 * Make sure to call buf_finish for any queued buffers. Normally
1855 * that's done in dqbuf, but that's not going to happen when we
1856 * cancel the whole queue. Note: this code belongs here, not in
1857 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1858 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1859 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1861 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1862 struct vb2_buffer
*vb
= q
->bufs
[i
];
1864 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1865 vb
->state
= VB2_BUF_STATE_PREPARED
;
1866 call_void_vb_qop(vb
, buf_finish
, vb
);
1872 int vb2_core_streamon(struct vb2_queue
*q
, unsigned int type
)
1876 if (type
!= q
->type
) {
1877 dprintk(1, "invalid stream type\n");
1882 dprintk(3, "already streaming\n");
1886 if (!q
->num_buffers
) {
1887 dprintk(1, "no buffers have been allocated\n");
1891 if (q
->num_buffers
< q
->min_buffers_needed
) {
1892 dprintk(1, "need at least %u allocated buffers\n",
1893 q
->min_buffers_needed
);
1898 * Tell driver to start streaming provided sufficient buffers
1901 if (q
->queued_count
>= q
->min_buffers_needed
) {
1902 ret
= vb2_start_streaming(q
);
1904 __vb2_queue_cancel(q
);
1911 dprintk(3, "successful\n");
1914 EXPORT_SYMBOL_GPL(vb2_core_streamon
);
1917 * vb2_queue_error() - signal a fatal error on the queue
1918 * @q: videobuf2 queue
1920 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1921 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1922 * buffers will return -EIO.
1924 * The error flag will be cleared when cancelling the queue, either from
1925 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1926 * function before starting the stream, otherwise the error flag will remain set
1927 * until the queue is released when closing the device node.
1929 void vb2_queue_error(struct vb2_queue
*q
)
1933 wake_up_all(&q
->done_wq
);
1935 EXPORT_SYMBOL_GPL(vb2_queue_error
);
1937 int vb2_core_streamoff(struct vb2_queue
*q
, unsigned int type
)
1939 if (type
!= q
->type
) {
1940 dprintk(1, "invalid stream type\n");
1945 * Cancel will pause streaming and remove all buffers from the driver
1946 * and videobuf, effectively returning control over them to userspace.
1948 * Note that we do this even if q->streaming == 0: if you prepare or
1949 * queue buffers, and then call streamoff without ever having called
1950 * streamon, you would still expect those buffers to be returned to
1951 * their normal dequeued state.
1953 __vb2_queue_cancel(q
);
1954 q
->waiting_for_buffers
= !q
->is_output
;
1955 q
->last_buffer_dequeued
= false;
1957 dprintk(3, "successful\n");
1960 EXPORT_SYMBOL_GPL(vb2_core_streamoff
);
1963 * __find_plane_by_offset() - find plane associated with the given offset off
1965 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1966 unsigned int *_buffer
, unsigned int *_plane
)
1968 struct vb2_buffer
*vb
;
1969 unsigned int buffer
, plane
;
1972 * Go over all buffers and their planes, comparing the given offset
1973 * with an offset assigned to each plane. If a match is found,
1974 * return its buffer and plane numbers.
1976 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1977 vb
= q
->bufs
[buffer
];
1979 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1980 if (vb
->planes
[plane
].m
.offset
== off
) {
1992 * vb2_core_expbuf() - Export a buffer as a file descriptor
1993 * @q: videobuf2 queue
1994 * @fd: file descriptor associated with DMABUF (set by driver) *
1995 * @type: buffer type
1996 * @index: id number of the buffer
1997 * @plane: index of the plane to be exported, 0 for single plane queues
1998 * @flags: flags for newly created file, currently only O_CLOEXEC is
1999 * supported, refer to manual of open syscall for more details
2001 * The return values from this function are intended to be directly returned
2002 * from vidioc_expbuf handler in driver.
2004 int vb2_core_expbuf(struct vb2_queue
*q
, int *fd
, unsigned int type
,
2005 unsigned int index
, unsigned int plane
, unsigned int flags
)
2007 struct vb2_buffer
*vb
= NULL
;
2008 struct vb2_plane
*vb_plane
;
2010 struct dma_buf
*dbuf
;
2012 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2013 dprintk(1, "queue is not currently set up for mmap\n");
2017 if (!q
->mem_ops
->get_dmabuf
) {
2018 dprintk(1, "queue does not support DMA buffer exporting\n");
2022 if (flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2023 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2027 if (type
!= q
->type
) {
2028 dprintk(1, "invalid buffer type\n");
2032 if (index
>= q
->num_buffers
) {
2033 dprintk(1, "buffer index out of range\n");
2037 vb
= q
->bufs
[index
];
2039 if (plane
>= vb
->num_planes
) {
2040 dprintk(1, "buffer plane out of range\n");
2044 if (vb2_fileio_is_active(q
)) {
2045 dprintk(1, "expbuf: file io in progress\n");
2049 vb_plane
= &vb
->planes
[plane
];
2051 dbuf
= call_ptr_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
,
2053 if (IS_ERR_OR_NULL(dbuf
)) {
2054 dprintk(1, "failed to export buffer %d, plane %d\n",
2059 ret
= dma_buf_fd(dbuf
, flags
& ~O_ACCMODE
);
2061 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2067 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2073 EXPORT_SYMBOL_GPL(vb2_core_expbuf
);
2076 * vb2_mmap() - map video buffers into application address space
2077 * @q: videobuf2 queue
2078 * @vma: vma passed to the mmap file operation handler in the driver
2080 * Should be called from mmap file operation handler of a driver.
2081 * This function maps one plane of one of the available video buffers to
2082 * userspace. To map whole video memory allocated on reqbufs, this function
2083 * has to be called once per each plane per each buffer previously allocated.
2085 * When the userspace application calls mmap, it passes to it an offset returned
2086 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2087 * a "cookie", which is then used to identify the plane to be mapped.
2088 * This function finds a plane with a matching offset and a mapping is performed
2089 * by the means of a provided memory operation.
2091 * The return values from this function are intended to be directly returned
2092 * from the mmap handler in driver.
2094 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2096 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2097 struct vb2_buffer
*vb
;
2098 unsigned int buffer
= 0, plane
= 0;
2100 unsigned long length
;
2102 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2103 dprintk(1, "queue is not currently set up for mmap\n");
2108 * Check memory area access mode.
2110 if (!(vma
->vm_flags
& VM_SHARED
)) {
2111 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2115 if (!(vma
->vm_flags
& VM_WRITE
)) {
2116 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2120 if (!(vma
->vm_flags
& VM_READ
)) {
2121 dprintk(1, "invalid vma flags, VM_READ needed\n");
2125 if (vb2_fileio_is_active(q
)) {
2126 dprintk(1, "mmap: file io in progress\n");
2131 * Find the plane corresponding to the offset passed by userspace.
2133 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2137 vb
= q
->bufs
[buffer
];
2140 * MMAP requires page_aligned buffers.
2141 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2142 * so, we need to do the same here.
2144 length
= PAGE_ALIGN(vb
->planes
[plane
].length
);
2145 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2147 "MMAP invalid, as it would overflow buffer length\n");
2151 mutex_lock(&q
->mmap_lock
);
2152 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2153 mutex_unlock(&q
->mmap_lock
);
2157 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2160 EXPORT_SYMBOL_GPL(vb2_mmap
);
2163 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2166 unsigned long pgoff
,
2167 unsigned long flags
)
2169 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2170 struct vb2_buffer
*vb
;
2171 unsigned int buffer
, plane
;
2175 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2176 dprintk(1, "queue is not currently set up for mmap\n");
2181 * Find the plane corresponding to the offset passed by userspace.
2183 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2187 vb
= q
->bufs
[buffer
];
2189 vaddr
= vb2_plane_vaddr(vb
, plane
);
2190 return vaddr
? (unsigned long)vaddr
: -EINVAL
;
2192 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2196 * vb2_core_queue_init() - initialize a videobuf2 queue
2197 * @q: videobuf2 queue; this structure should be allocated in driver
2199 * The vb2_queue structure should be allocated by the driver. The driver is
2200 * responsible of clearing it's content and setting initial values for some
2201 * required entries before calling this function.
2202 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2203 * to the struct vb2_queue description in include/media/videobuf2-core.h
2204 * for more information.
2206 int vb2_core_queue_init(struct vb2_queue
*q
)
2213 WARN_ON(!q
->mem_ops
) ||
2214 WARN_ON(!q
->type
) ||
2215 WARN_ON(!q
->io_modes
) ||
2216 WARN_ON(!q
->ops
->queue_setup
) ||
2217 WARN_ON(!q
->ops
->buf_queue
))
2220 INIT_LIST_HEAD(&q
->queued_list
);
2221 INIT_LIST_HEAD(&q
->done_list
);
2222 spin_lock_init(&q
->done_lock
);
2223 mutex_init(&q
->mmap_lock
);
2224 init_waitqueue_head(&q
->done_wq
);
2226 if (q
->buf_struct_size
== 0)
2227 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2231 EXPORT_SYMBOL_GPL(vb2_core_queue_init
);
2233 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2234 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2236 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2237 * @q: videobuf2 queue
2239 * This function stops streaming and performs necessary clean ups, including
2240 * freeing video buffer memory. The driver is responsible for freeing
2241 * the vb2_queue structure itself.
2243 void vb2_core_queue_release(struct vb2_queue
*q
)
2245 __vb2_cleanup_fileio(q
);
2246 __vb2_queue_cancel(q
);
2247 mutex_lock(&q
->mmap_lock
);
2248 __vb2_queue_free(q
, q
->num_buffers
);
2249 mutex_unlock(&q
->mmap_lock
);
2251 EXPORT_SYMBOL_GPL(vb2_core_queue_release
);
2254 * vb2_core_poll() - implements poll userspace operation
2255 * @q: videobuf2 queue
2256 * @file: file argument passed to the poll file operation handler
2257 * @wait: wait argument passed to the poll file operation handler
2259 * This function implements poll file operation handler for a driver.
2260 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2261 * be informed that the file descriptor of a video device is available for
2263 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2264 * will be reported as available for writing.
2266 * The return values from this function are intended to be directly returned
2267 * from poll handler in driver.
2269 unsigned int vb2_core_poll(struct vb2_queue
*q
, struct file
*file
,
2272 unsigned long req_events
= poll_requested_events(wait
);
2273 struct vb2_buffer
*vb
= NULL
;
2274 unsigned long flags
;
2276 if (!q
->is_output
&& !(req_events
& (POLLIN
| POLLRDNORM
)))
2278 if (q
->is_output
&& !(req_events
& (POLLOUT
| POLLWRNORM
)))
2282 * Start file I/O emulator only if streaming API has not been used yet.
2284 if (q
->num_buffers
== 0 && !vb2_fileio_is_active(q
)) {
2285 if (!q
->is_output
&& (q
->io_modes
& VB2_READ
) &&
2286 (req_events
& (POLLIN
| POLLRDNORM
))) {
2287 if (__vb2_init_fileio(q
, 1))
2290 if (q
->is_output
&& (q
->io_modes
& VB2_WRITE
) &&
2291 (req_events
& (POLLOUT
| POLLWRNORM
))) {
2292 if (__vb2_init_fileio(q
, 0))
2295 * Write to OUTPUT queue can be done immediately.
2297 return POLLOUT
| POLLWRNORM
;
2302 * There is nothing to wait for if the queue isn't streaming, or if the
2303 * error flag is set.
2305 if (!vb2_is_streaming(q
) || q
->error
)
2309 * For output streams you can call write() as long as there are fewer
2310 * buffers queued than there are buffers available.
2312 if (q
->is_output
&& q
->fileio
&& q
->queued_count
< q
->num_buffers
)
2313 return POLLOUT
| POLLWRNORM
;
2315 if (list_empty(&q
->done_list
)) {
2317 * If the last buffer was dequeued from a capture queue,
2318 * return immediately. DQBUF will return -EPIPE.
2320 if (q
->last_buffer_dequeued
)
2321 return POLLIN
| POLLRDNORM
;
2323 poll_wait(file
, &q
->done_wq
, wait
);
2327 * Take first buffer available for dequeuing.
2329 spin_lock_irqsave(&q
->done_lock
, flags
);
2330 if (!list_empty(&q
->done_list
))
2331 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2333 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2335 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2336 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2337 return (q
->is_output
) ?
2338 POLLOUT
| POLLWRNORM
:
2339 POLLIN
| POLLRDNORM
;
2343 EXPORT_SYMBOL_GPL(vb2_core_poll
);
2346 * struct vb2_fileio_buf - buffer context used by file io emulator
2348 * vb2 provides a compatibility layer and emulator of file io (read and
2349 * write) calls on top of streaming API. This structure is used for
2350 * tracking context related to the buffers.
2352 struct vb2_fileio_buf
{
2356 unsigned int queued
:1;
2360 * struct vb2_fileio_data - queue context used by file io emulator
2362 * @cur_index: the index of the buffer currently being read from or
2363 * written to. If equal to q->num_buffers then a new buffer
2365 * @initial_index: in the read() case all buffers are queued up immediately
2366 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2367 * buffers. However, in the write() case no buffers are initially
2368 * queued, instead whenever a buffer is full it is queued up by
2369 * __vb2_perform_fileio(). Only once all available buffers have
2370 * been queued up will __vb2_perform_fileio() start to dequeue
2371 * buffers. This means that initially __vb2_perform_fileio()
2372 * needs to know what buffer index to use when it is queuing up
2373 * the buffers for the first time. That initial index is stored
2374 * in this field. Once it is equal to q->num_buffers all
2375 * available buffers have been queued and __vb2_perform_fileio()
2376 * should start the normal dequeue/queue cycle.
2378 * vb2 provides a compatibility layer and emulator of file io (read and
2379 * write) calls on top of streaming API. For proper operation it required
2380 * this structure to save the driver state between each call of the read
2381 * or write function.
2383 struct vb2_fileio_data
{
2386 unsigned int memory
;
2387 struct vb2_buffer
*b
;
2388 struct vb2_fileio_buf bufs
[VB2_MAX_FRAME
];
2389 unsigned int cur_index
;
2390 unsigned int initial_index
;
2391 unsigned int q_count
;
2392 unsigned int dq_count
;
2393 unsigned read_once
:1;
2394 unsigned write_immediately
:1;
2398 * __vb2_init_fileio() - initialize file io emulator
2399 * @q: videobuf2 queue
2400 * @read: mode selector (1 means read, 0 means write)
2402 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2404 struct vb2_fileio_data
*fileio
;
2406 unsigned int count
= 0;
2411 if (WARN_ON((read
&& !(q
->io_modes
& VB2_READ
)) ||
2412 (!read
&& !(q
->io_modes
& VB2_WRITE
))))
2416 * Check if device supports mapping buffers to kernel virtual space.
2418 if (!q
->mem_ops
->vaddr
)
2422 * Check if streaming api has not been already activated.
2424 if (q
->streaming
|| q
->num_buffers
> 0)
2428 * Start with count 1, driver can increase it in queue_setup()
2432 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2433 (read
) ? "read" : "write", count
, q
->fileio_read_once
,
2434 q
->fileio_write_immediately
);
2436 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
2440 fileio
->b
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
2441 if (fileio
->b
== NULL
)
2444 fileio
->read_once
= q
->fileio_read_once
;
2445 fileio
->write_immediately
= q
->fileio_write_immediately
;
2448 * Request buffers and use MMAP type to force driver
2449 * to allocate buffers by itself.
2451 fileio
->count
= count
;
2452 fileio
->memory
= VB2_MEMORY_MMAP
;
2453 fileio
->type
= q
->type
;
2455 ret
= vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2460 * Check if plane_count is correct
2461 * (multiplane buffers are not supported).
2463 if (q
->bufs
[0]->num_planes
!= 1) {
2469 * Get kernel address of each buffer.
2471 for (i
= 0; i
< q
->num_buffers
; i
++) {
2472 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2473 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2477 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2481 * Read mode requires pre queuing of all buffers.
2485 * Queue all buffers.
2487 for (i
= 0; i
< q
->num_buffers
; i
++) {
2488 struct vb2_buffer
*b
= fileio
->b
;
2490 memset(b
, 0, q
->buf_struct_size
);
2492 b
->memory
= q
->memory
;
2494 ret
= vb2_core_qbuf(q
, i
, b
);
2497 fileio
->bufs
[i
].queued
= 1;
2500 * All buffers have been queued, so mark that by setting
2501 * initial_index to q->num_buffers
2503 fileio
->initial_index
= q
->num_buffers
;
2504 fileio
->cur_index
= q
->num_buffers
;
2510 ret
= vb2_core_streamon(q
, q
->type
);
2518 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2527 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2528 * @q: videobuf2 queue
2530 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2532 struct vb2_fileio_data
*fileio
= q
->fileio
;
2535 vb2_core_streamoff(q
, q
->type
);
2538 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2541 dprintk(3, "file io emulator closed\n");
2547 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2548 * @q: videobuf2 queue
2549 * @data: pointed to target userspace buffer
2550 * @count: number of bytes to read or write
2551 * @ppos: file handle position tracking pointer
2552 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2553 * @read: access mode selector (1 means read, 0 means write)
2555 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2556 loff_t
*ppos
, int nonblock
, int read
)
2558 struct vb2_fileio_data
*fileio
;
2559 struct vb2_fileio_buf
*buf
;
2560 bool is_multiplanar
= q
->is_multiplanar
;
2562 * When using write() to write data to an output video node the vb2 core
2563 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2564 * else is able to provide this information with the write() operation.
2566 bool copy_timestamp
= !read
&& q
->copy_timestamp
;
2569 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2570 read
? "read" : "write", (long)*ppos
, count
,
2571 nonblock
? "non" : "");
2577 * Initialize emulator on first call.
2579 if (!vb2_fileio_is_active(q
)) {
2580 ret
= __vb2_init_fileio(q
, read
);
2581 dprintk(3, "vb2_init_fileio result: %d\n", ret
);
2588 * Check if we need to dequeue the buffer.
2590 index
= fileio
->cur_index
;
2591 if (index
>= q
->num_buffers
) {
2592 struct vb2_buffer
*b
= fileio
->b
;
2595 * Call vb2_dqbuf to get buffer back.
2597 memset(b
, 0, q
->buf_struct_size
);
2599 b
->memory
= q
->memory
;
2600 ret
= vb2_core_dqbuf(q
, b
, nonblock
);
2601 dprintk(5, "vb2_dqbuf result: %d\n", ret
);
2604 fileio
->dq_count
+= 1;
2606 fileio
->cur_index
= index
= b
->index
;
2607 buf
= &fileio
->bufs
[index
];
2610 * Get number of bytes filled by the driver
2614 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2615 : vb2_plane_size(q
->bufs
[index
], 0);
2616 /* Compensate for data_offset on read in the multiplanar case. */
2617 if (is_multiplanar
&& read
&&
2618 b
->planes
[0].data_offset
< buf
->size
) {
2619 buf
->pos
= b
->planes
[0].data_offset
;
2620 buf
->size
-= buf
->pos
;
2623 buf
= &fileio
->bufs
[index
];
2627 * Limit count on last few bytes of the buffer.
2629 if (buf
->pos
+ count
> buf
->size
) {
2630 count
= buf
->size
- buf
->pos
;
2631 dprintk(5, "reducing read count: %zd\n", count
);
2635 * Transfer data to userspace.
2637 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2638 count
, index
, buf
->pos
);
2640 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2642 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2644 dprintk(3, "error copying data\n");
2655 * Queue next buffer if required.
2657 if (buf
->pos
== buf
->size
|| (!read
&& fileio
->write_immediately
)) {
2658 struct vb2_buffer
*b
= fileio
->b
;
2661 * Check if this is the last buffer to read.
2663 if (read
&& fileio
->read_once
&& fileio
->dq_count
== 1) {
2664 dprintk(3, "read limit reached\n");
2665 return __vb2_cleanup_fileio(q
);
2669 * Call vb2_qbuf and give buffer to the driver.
2671 memset(b
, 0, q
->buf_struct_size
);
2673 b
->memory
= q
->memory
;
2675 b
->planes
[0].bytesused
= buf
->pos
;
2678 b
->timestamp
= ktime_get_ns();
2679 ret
= vb2_core_qbuf(q
, index
, b
);
2680 dprintk(5, "vb2_dbuf result: %d\n", ret
);
2685 * Buffer has been queued, update the status
2689 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2690 fileio
->q_count
+= 1;
2692 * If we are queuing up buffers for the first time, then
2693 * increase initial_index by one.
2695 if (fileio
->initial_index
< q
->num_buffers
)
2696 fileio
->initial_index
++;
2698 * The next buffer to use is either a buffer that's going to be
2699 * queued for the first time (initial_index < q->num_buffers)
2700 * or it is equal to q->num_buffers, meaning that the next
2701 * time we need to dequeue a buffer since we've now queued up
2702 * all the 'first time' buffers.
2704 fileio
->cur_index
= fileio
->initial_index
;
2708 * Return proper number of bytes processed.
2715 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2716 loff_t
*ppos
, int nonblocking
)
2718 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2720 EXPORT_SYMBOL_GPL(vb2_read
);
2722 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2723 loff_t
*ppos
, int nonblocking
)
2725 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2726 ppos
, nonblocking
, 0);
2728 EXPORT_SYMBOL_GPL(vb2_write
);
2730 struct vb2_threadio_data
{
2731 struct task_struct
*thread
;
2737 static int vb2_thread(void *data
)
2739 struct vb2_queue
*q
= data
;
2740 struct vb2_threadio_data
*threadio
= q
->threadio
;
2741 struct vb2_fileio_data
*fileio
= q
->fileio
;
2742 bool copy_timestamp
= false;
2748 prequeue
= q
->num_buffers
;
2749 copy_timestamp
= q
->copy_timestamp
;
2755 struct vb2_buffer
*vb
;
2756 struct vb2_buffer
*b
= fileio
->b
;
2759 * Call vb2_dqbuf to get buffer back.
2761 memset(b
, 0, q
->buf_struct_size
);
2763 b
->memory
= q
->memory
;
2768 call_void_qop(q
, wait_finish
, q
);
2769 if (!threadio
->stop
)
2770 ret
= vb2_core_dqbuf(q
, b
, 0);
2771 call_void_qop(q
, wait_prepare
, q
);
2772 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2774 if (ret
|| threadio
->stop
)
2778 vb
= q
->bufs
[b
->index
];
2779 if (b
->state
== VB2_BUF_STATE_DONE
)
2780 if (threadio
->fnc(vb
, threadio
->priv
))
2782 call_void_qop(q
, wait_finish
, q
);
2784 b
->timestamp
= ktime_get_ns();;
2785 if (!threadio
->stop
)
2786 ret
= vb2_core_qbuf(q
, b
->index
, b
);
2787 call_void_qop(q
, wait_prepare
, q
);
2788 if (ret
|| threadio
->stop
)
2792 /* Hmm, linux becomes *very* unhappy without this ... */
2793 while (!kthread_should_stop()) {
2794 set_current_state(TASK_INTERRUPTIBLE
);
2801 * This function should not be used for anything else but the videobuf2-dvb
2802 * support. If you think you have another good use-case for this, then please
2803 * contact the linux-media mailinglist first.
2805 int vb2_thread_start(struct vb2_queue
*q
, vb2_thread_fnc fnc
, void *priv
,
2806 const char *thread_name
)
2808 struct vb2_threadio_data
*threadio
;
2815 if (WARN_ON(q
->fileio
))
2818 threadio
= kzalloc(sizeof(*threadio
), GFP_KERNEL
);
2819 if (threadio
== NULL
)
2821 threadio
->fnc
= fnc
;
2822 threadio
->priv
= priv
;
2824 ret
= __vb2_init_fileio(q
, !q
->is_output
);
2825 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2828 q
->threadio
= threadio
;
2829 threadio
->thread
= kthread_run(vb2_thread
, q
, "vb2-%s", thread_name
);
2830 if (IS_ERR(threadio
->thread
)) {
2831 ret
= PTR_ERR(threadio
->thread
);
2832 threadio
->thread
= NULL
;
2838 __vb2_cleanup_fileio(q
);
2843 EXPORT_SYMBOL_GPL(vb2_thread_start
);
2845 int vb2_thread_stop(struct vb2_queue
*q
)
2847 struct vb2_threadio_data
*threadio
= q
->threadio
;
2850 if (threadio
== NULL
)
2852 threadio
->stop
= true;
2853 /* Wake up all pending sleeps in the thread */
2855 err
= kthread_stop(threadio
->thread
);
2856 __vb2_cleanup_fileio(q
);
2857 threadio
->thread
= NULL
;
2862 EXPORT_SYMBOL_GPL(vb2_thread_stop
);
2864 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2865 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2866 MODULE_LICENSE("GPL");