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>
28 #include <media/v4l2-mc.h>
30 #include <trace/events/vb2.h>
33 module_param(debug
, int, 0644);
35 #define dprintk(level, fmt, arg...) \
38 pr_info("vb2-core: %s: " fmt, __func__, ## arg); \
41 #ifdef CONFIG_VIDEO_ADV_DEBUG
44 * If advanced debugging is on, then count how often each op is called
45 * successfully, which can either be per-buffer or per-queue.
47 * This makes it easy to check that the 'init' and 'cleanup'
48 * (and variations thereof) stay balanced.
51 #define log_memop(vb, op) \
52 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
53 (vb)->vb2_queue, (vb)->index, #op, \
54 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
56 #define call_memop(vb, op, args...) \
58 struct vb2_queue *_q = (vb)->vb2_queue; \
62 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
64 (vb)->cnt_mem_ ## op++; \
68 #define call_ptr_memop(vb, op, args...) \
70 struct vb2_queue *_q = (vb)->vb2_queue; \
74 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
75 if (!IS_ERR_OR_NULL(ptr)) \
76 (vb)->cnt_mem_ ## op++; \
80 #define call_void_memop(vb, op, args...) \
82 struct vb2_queue *_q = (vb)->vb2_queue; \
85 if (_q->mem_ops->op) \
86 _q->mem_ops->op(args); \
87 (vb)->cnt_mem_ ## op++; \
90 #define log_qop(q, op) \
91 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
92 (q)->ops->op ? "" : " (nop)")
94 #define call_qop(q, op, args...) \
99 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
105 #define call_void_qop(q, op, args...) \
109 (q)->ops->op(args); \
113 #define log_vb_qop(vb, op, args...) \
114 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
115 (vb)->vb2_queue, (vb)->index, #op, \
116 (vb)->vb2_queue->ops->op ? "" : " (nop)")
118 #define call_vb_qop(vb, op, args...) \
122 log_vb_qop(vb, op); \
123 err = (vb)->vb2_queue->ops->op ? \
124 (vb)->vb2_queue->ops->op(args) : 0; \
126 (vb)->cnt_ ## op++; \
130 #define call_void_vb_qop(vb, op, args...) \
132 log_vb_qop(vb, op); \
133 if ((vb)->vb2_queue->ops->op) \
134 (vb)->vb2_queue->ops->op(args); \
135 (vb)->cnt_ ## op++; \
140 #define call_memop(vb, op, args...) \
141 ((vb)->vb2_queue->mem_ops->op ? \
142 (vb)->vb2_queue->mem_ops->op(args) : 0)
144 #define call_ptr_memop(vb, op, args...) \
145 ((vb)->vb2_queue->mem_ops->op ? \
146 (vb)->vb2_queue->mem_ops->op(args) : NULL)
148 #define call_void_memop(vb, op, args...) \
150 if ((vb)->vb2_queue->mem_ops->op) \
151 (vb)->vb2_queue->mem_ops->op(args); \
154 #define call_qop(q, op, args...) \
155 ((q)->ops->op ? (q)->ops->op(args) : 0)
157 #define call_void_qop(q, op, args...) \
160 (q)->ops->op(args); \
163 #define call_vb_qop(vb, op, args...) \
164 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
166 #define call_void_vb_qop(vb, op, args...) \
168 if ((vb)->vb2_queue->ops->op) \
169 (vb)->vb2_queue->ops->op(args); \
174 #define call_bufop(q, op, args...) \
177 if (q && q->buf_ops && q->buf_ops->op) \
178 ret = q->buf_ops->op(args); \
182 #define call_void_bufop(q, op, args...) \
184 if (q && q->buf_ops && q->buf_ops->op) \
185 q->buf_ops->op(args); \
188 static void __vb2_queue_cancel(struct vb2_queue
*q
);
189 static void __enqueue_in_driver(struct vb2_buffer
*vb
);
192 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
194 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
196 struct vb2_queue
*q
= vb
->vb2_queue
;
197 enum dma_data_direction dma_dir
=
198 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
203 * Allocate memory for all planes in this buffer
204 * NOTE: mmapped areas should be page aligned
206 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
207 unsigned long size
= PAGE_ALIGN(vb
->planes
[plane
].length
);
209 mem_priv
= call_ptr_memop(vb
, alloc
, q
->alloc_ctx
[plane
],
210 size
, dma_dir
, q
->gfp_flags
);
211 if (IS_ERR_OR_NULL(mem_priv
))
214 /* Associate allocator private data with this plane */
215 vb
->planes
[plane
].mem_priv
= mem_priv
;
220 /* Free already allocated memory if one of the allocations failed */
221 for (; plane
> 0; --plane
) {
222 call_void_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
223 vb
->planes
[plane
- 1].mem_priv
= NULL
;
230 * __vb2_buf_mem_free() - free memory of the given buffer
232 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
236 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
237 call_void_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
238 vb
->planes
[plane
].mem_priv
= NULL
;
239 dprintk(3, "freed plane %d of buffer %d\n", plane
, vb
->index
);
244 * __vb2_buf_userptr_put() - release userspace memory associated with
247 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
251 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
252 if (vb
->planes
[plane
].mem_priv
)
253 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
254 vb
->planes
[plane
].mem_priv
= NULL
;
259 * __vb2_plane_dmabuf_put() - release memory associated with
260 * a DMABUF shared plane
262 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
268 call_void_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
270 call_void_memop(vb
, detach_dmabuf
, p
->mem_priv
);
271 dma_buf_put(p
->dbuf
);
278 * __vb2_buf_dmabuf_put() - release memory associated with
279 * a DMABUF shared buffer
281 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
285 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
286 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
290 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
293 static void __setup_offsets(struct vb2_buffer
*vb
)
295 struct vb2_queue
*q
= vb
->vb2_queue
;
297 unsigned long off
= 0;
300 struct vb2_buffer
*prev
= q
->bufs
[vb
->index
- 1];
301 struct vb2_plane
*p
= &prev
->planes
[prev
->num_planes
- 1];
303 off
= PAGE_ALIGN(p
->m
.offset
+ p
->length
);
306 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
307 vb
->planes
[plane
].m
.offset
= off
;
309 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
310 vb
->index
, plane
, off
);
312 off
+= vb
->planes
[plane
].length
;
313 off
= PAGE_ALIGN(off
);
318 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
319 * video buffer memory for all buffers/planes on the queue and initializes the
322 * Returns the number of buffers successfully allocated.
324 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum vb2_memory memory
,
325 unsigned int num_buffers
, unsigned int num_planes
,
326 const unsigned plane_sizes
[VB2_MAX_PLANES
])
328 unsigned int buffer
, plane
;
329 struct vb2_buffer
*vb
;
332 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
333 /* Allocate videobuf buffer structures */
334 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
336 dprintk(1, "memory alloc for buffer struct failed\n");
340 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
342 vb
->num_planes
= num_planes
;
343 vb
->index
= q
->num_buffers
+ buffer
;
346 for (plane
= 0; plane
< num_planes
; ++plane
) {
347 vb
->planes
[plane
].length
= plane_sizes
[plane
];
348 vb
->planes
[plane
].min_length
= plane_sizes
[plane
];
350 q
->bufs
[vb
->index
] = vb
;
352 /* Allocate video buffer memory for the MMAP type */
353 if (memory
== VB2_MEMORY_MMAP
) {
354 ret
= __vb2_buf_mem_alloc(vb
);
356 dprintk(1, "failed allocating memory for "
357 "buffer %d\n", buffer
);
358 q
->bufs
[vb
->index
] = NULL
;
364 * Call the driver-provided buffer initialization
365 * callback, if given. An error in initialization
366 * results in queue setup failure.
368 ret
= call_vb_qop(vb
, buf_init
, vb
);
370 dprintk(1, "buffer %d %p initialization"
371 " failed\n", buffer
, vb
);
372 __vb2_buf_mem_free(vb
);
373 q
->bufs
[vb
->index
] = NULL
;
380 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
387 * __vb2_free_mem() - release all video buffer memory for a given queue
389 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
392 struct vb2_buffer
*vb
;
394 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
396 vb
= q
->bufs
[buffer
];
400 /* Free MMAP buffers or release USERPTR buffers */
401 if (q
->memory
== VB2_MEMORY_MMAP
)
402 __vb2_buf_mem_free(vb
);
403 else if (q
->memory
== VB2_MEMORY_DMABUF
)
404 __vb2_buf_dmabuf_put(vb
);
406 __vb2_buf_userptr_put(vb
);
411 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
412 * related information, if no buffers are left return the queue to an
413 * uninitialized state. Might be called even if the queue has already been freed.
415 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
420 * Sanity check: when preparing a buffer the queue lock is released for
421 * a short while (see __buf_prepare for the details), which would allow
422 * a race with a reqbufs which can call this function. Removing the
423 * buffers from underneath __buf_prepare is obviously a bad idea, so we
424 * check if any of the buffers is in the state PREPARING, and if so we
425 * just return -EAGAIN.
427 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
429 if (q
->bufs
[buffer
] == NULL
)
431 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
432 dprintk(1, "preparing buffers, cannot free\n");
437 /* Call driver-provided cleanup function for each buffer, if provided */
438 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
440 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
442 if (vb
&& vb
->planes
[0].mem_priv
)
443 call_void_vb_qop(vb
, buf_cleanup
, vb
);
446 /* Release video buffer memory */
447 __vb2_free_mem(q
, buffers
);
449 #ifdef CONFIG_VIDEO_ADV_DEBUG
451 * Check that all the calls were balances during the life-time of this
452 * queue. If not (or if the debug level is 1 or up), then dump the
453 * counters to the kernel log.
455 if (q
->num_buffers
) {
456 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
457 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
459 if (unbalanced
|| debug
) {
460 pr_info("vb2: counters for queue %p:%s\n", q
,
461 unbalanced
? " UNBALANCED!" : "");
462 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
463 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
464 q
->cnt_stop_streaming
);
465 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
466 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
468 q
->cnt_queue_setup
= 0;
469 q
->cnt_wait_prepare
= 0;
470 q
->cnt_wait_finish
= 0;
471 q
->cnt_start_streaming
= 0;
472 q
->cnt_stop_streaming
= 0;
474 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
475 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
476 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
477 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
478 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
479 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
480 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
481 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
482 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
483 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
485 if (unbalanced
|| debug
) {
486 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
487 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
488 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
489 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
490 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
491 pr_info("vb2: buf_queue: %u buf_done: %u\n",
492 vb
->cnt_buf_queue
, vb
->cnt_buf_done
);
493 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
494 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
495 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
497 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
498 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
499 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
500 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
501 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
502 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
503 vb
->cnt_mem_get_dmabuf
,
504 vb
->cnt_mem_num_users
,
511 /* Free videobuf buffers */
512 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
514 kfree(q
->bufs
[buffer
]);
515 q
->bufs
[buffer
] = NULL
;
518 q
->num_buffers
-= buffers
;
519 if (!q
->num_buffers
) {
521 INIT_LIST_HEAD(&q
->queued_list
);
527 * vb2_buffer_in_use() - return true if the buffer is in use and
528 * the queue cannot be freed (by the means of REQBUFS(0)) call
530 bool vb2_buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
533 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
534 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
536 * If num_users() has not been provided, call_memop
537 * will return 0, apparently nobody cares about this
538 * case anyway. If num_users() returns more than 1,
539 * we are not the only user of the plane's memory.
541 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
546 EXPORT_SYMBOL(vb2_buffer_in_use
);
549 * __buffers_in_use() - return true if any buffers on the queue are in use and
550 * the queue cannot be freed (by the means of REQBUFS(0)) call
552 static bool __buffers_in_use(struct vb2_queue
*q
)
555 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
556 if (vb2_buffer_in_use(q
, q
->bufs
[buffer
]))
563 * vb2_core_querybuf() - query video buffer information
565 * @index: id number of the buffer
566 * @pb: buffer struct passed from userspace
568 * Should be called from vidioc_querybuf ioctl handler in driver.
569 * The passed buffer should have been verified.
570 * This function fills the relevant information for the userspace.
572 void vb2_core_querybuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
574 call_void_bufop(q
, fill_user_buffer
, q
->bufs
[index
], pb
);
576 EXPORT_SYMBOL_GPL(vb2_core_querybuf
);
579 * __verify_userptr_ops() - verify that all memory operations required for
580 * USERPTR queue type have been provided
582 static int __verify_userptr_ops(struct vb2_queue
*q
)
584 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
585 !q
->mem_ops
->put_userptr
)
592 * __verify_mmap_ops() - verify that all memory operations required for
593 * MMAP queue type have been provided
595 static int __verify_mmap_ops(struct vb2_queue
*q
)
597 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
598 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
605 * __verify_dmabuf_ops() - verify that all memory operations required for
606 * DMABUF queue type have been provided
608 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
610 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
611 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
612 !q
->mem_ops
->unmap_dmabuf
)
619 * vb2_verify_memory_type() - Check whether the memory type and buffer type
620 * passed to a buffer operation are compatible with the queue.
622 int vb2_verify_memory_type(struct vb2_queue
*q
,
623 enum vb2_memory memory
, unsigned int type
)
625 if (memory
!= VB2_MEMORY_MMAP
&& memory
!= VB2_MEMORY_USERPTR
&&
626 memory
!= VB2_MEMORY_DMABUF
) {
627 dprintk(1, "unsupported memory type\n");
631 if (type
!= q
->type
) {
632 dprintk(1, "requested type is incorrect\n");
637 * Make sure all the required memory ops for given memory type
640 if (memory
== VB2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
641 dprintk(1, "MMAP for current setup unsupported\n");
645 if (memory
== VB2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
646 dprintk(1, "USERPTR for current setup unsupported\n");
650 if (memory
== VB2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
651 dprintk(1, "DMABUF for current setup unsupported\n");
656 * Place the busy tests at the end: -EBUSY can be ignored when
657 * create_bufs is called with count == 0, but count == 0 should still
658 * do the memory and type validation.
660 if (vb2_fileio_is_active(q
)) {
661 dprintk(1, "file io in progress\n");
666 EXPORT_SYMBOL(vb2_verify_memory_type
);
669 * vb2_core_reqbufs() - Initiate streaming
670 * @q: videobuf2 queue
671 * @memory: memory type
672 * @count: requested buffer count
674 * Should be called from vidioc_reqbufs ioctl handler of a driver.
676 * 1) verifies streaming parameters passed from the userspace,
677 * 2) sets up the queue,
678 * 3) negotiates number of buffers and planes per buffer with the driver
679 * to be used during streaming,
680 * 4) allocates internal buffer structures (struct vb2_buffer), according to
681 * the agreed parameters,
682 * 5) for MMAP memory type, allocates actual video memory, using the
683 * memory handling/allocation routines provided during queue initialization
685 * If req->count is 0, all the memory will be freed instead.
686 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
687 * and the queue is not busy, memory will be reallocated.
689 * The return values from this function are intended to be directly returned
690 * from vidioc_reqbufs handler in driver.
692 int vb2_core_reqbufs(struct vb2_queue
*q
, enum vb2_memory memory
,
695 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
696 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
700 dprintk(1, "streaming active\n");
704 if (*count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= memory
) {
706 * We already have buffers allocated, so first check if they
707 * are not in use and can be freed.
709 mutex_lock(&q
->mmap_lock
);
710 if (q
->memory
== VB2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
711 mutex_unlock(&q
->mmap_lock
);
712 dprintk(1, "memory in use, cannot free\n");
717 * Call queue_cancel to clean up any buffers in the PREPARED or
718 * QUEUED state which is possible if buffers were prepared or
719 * queued without ever calling STREAMON.
721 __vb2_queue_cancel(q
);
722 ret
= __vb2_queue_free(q
, q
->num_buffers
);
723 mutex_unlock(&q
->mmap_lock
);
728 * In case of REQBUFS(0) return immediately without calling
729 * driver's queue_setup() callback and allocating resources.
736 * Make sure the requested values and current defaults are sane.
738 num_buffers
= min_t(unsigned int, *count
, VB2_MAX_FRAME
);
739 num_buffers
= max_t(unsigned int, num_buffers
, q
->min_buffers_needed
);
740 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
744 * Ask the driver how many buffers and planes per buffer it requires.
745 * Driver also sets the size and allocator context for each plane.
747 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
748 plane_sizes
, q
->alloc_ctx
);
752 /* Finally, allocate buffers and video memory */
754 __vb2_queue_alloc(q
, memory
, num_buffers
, num_planes
, plane_sizes
);
755 if (allocated_buffers
== 0) {
756 dprintk(1, "memory allocation failed\n");
761 * There is no point in continuing if we can't allocate the minimum
762 * number of buffers needed by this vb2_queue.
764 if (allocated_buffers
< q
->min_buffers_needed
)
768 * Check if driver can handle the allocated number of buffers.
770 if (!ret
&& allocated_buffers
< num_buffers
) {
771 num_buffers
= allocated_buffers
;
773 * num_planes is set by the previous queue_setup(), but since it
774 * signals to queue_setup() whether it is called from create_bufs()
775 * vs reqbufs() we zero it here to signal that queue_setup() is
776 * called for the reqbufs() case.
780 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
781 &num_planes
, plane_sizes
, q
->alloc_ctx
);
783 if (!ret
&& allocated_buffers
< num_buffers
)
787 * Either the driver has accepted a smaller number of buffers,
788 * or .queue_setup() returned an error
792 mutex_lock(&q
->mmap_lock
);
793 q
->num_buffers
= allocated_buffers
;
797 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
798 * from q->num_buffers.
800 __vb2_queue_free(q
, allocated_buffers
);
801 mutex_unlock(&q
->mmap_lock
);
804 mutex_unlock(&q
->mmap_lock
);
807 * Return the number of successfully allocated buffers
810 *count
= allocated_buffers
;
811 q
->waiting_for_buffers
= !q
->is_output
;
815 EXPORT_SYMBOL_GPL(vb2_core_reqbufs
);
818 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
819 * @q: videobuf2 queue
820 * @memory: memory type
821 * @count: requested buffer count
822 * @parg: parameter passed to device driver
824 * Should be called from vidioc_create_bufs ioctl handler of a driver.
826 * 1) verifies parameter sanity
827 * 2) calls the .queue_setup() queue operation
828 * 3) performs any necessary memory allocations
830 * The return values from this function are intended to be directly returned
831 * from vidioc_create_bufs handler in driver.
833 int vb2_core_create_bufs(struct vb2_queue
*q
, enum vb2_memory memory
,
834 unsigned int *count
, unsigned requested_planes
,
835 const unsigned requested_sizes
[])
837 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
838 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
841 if (q
->num_buffers
== VB2_MAX_FRAME
) {
842 dprintk(1, "maximum number of buffers already allocated\n");
846 if (!q
->num_buffers
) {
847 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
849 q
->waiting_for_buffers
= !q
->is_output
;
852 num_buffers
= min(*count
, VB2_MAX_FRAME
- q
->num_buffers
);
854 if (requested_planes
&& requested_sizes
) {
855 num_planes
= requested_planes
;
856 memcpy(plane_sizes
, requested_sizes
, sizeof(plane_sizes
));
860 * Ask the driver, whether the requested number of buffers, planes per
861 * buffer and their sizes are acceptable
863 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
864 &num_planes
, plane_sizes
, q
->alloc_ctx
);
868 /* Finally, allocate buffers and video memory */
869 allocated_buffers
= __vb2_queue_alloc(q
, memory
, num_buffers
,
870 num_planes
, plane_sizes
);
871 if (allocated_buffers
== 0) {
872 dprintk(1, "memory allocation failed\n");
877 * Check if driver can handle the so far allocated number of buffers.
879 if (allocated_buffers
< num_buffers
) {
880 num_buffers
= allocated_buffers
;
883 * q->num_buffers contains the total number of buffers, that the
884 * queue driver has set up
886 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
887 &num_planes
, plane_sizes
, q
->alloc_ctx
);
889 if (!ret
&& allocated_buffers
< num_buffers
)
893 * Either the driver has accepted a smaller number of buffers,
894 * or .queue_setup() returned an error
898 mutex_lock(&q
->mmap_lock
);
899 q
->num_buffers
+= allocated_buffers
;
903 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
904 * from q->num_buffers.
906 __vb2_queue_free(q
, allocated_buffers
);
907 mutex_unlock(&q
->mmap_lock
);
910 mutex_unlock(&q
->mmap_lock
);
913 * Return the number of successfully allocated buffers
916 *count
= allocated_buffers
;
920 EXPORT_SYMBOL_GPL(vb2_core_create_bufs
);
923 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
924 * @vb: vb2_buffer to which the plane in question belongs to
925 * @plane_no: plane number for which the address is to be returned
927 * This function returns a kernel virtual address of a given plane if
928 * such a mapping exist, NULL otherwise.
930 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
932 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
935 return call_ptr_memop(vb
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
938 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
941 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
942 * @vb: vb2_buffer to which the plane in question belongs to
943 * @plane_no: plane number for which the cookie is to be returned
945 * This function returns an allocator specific cookie for a given plane if
946 * available, NULL otherwise. The allocator should provide some simple static
947 * inline function, which would convert this cookie to the allocator specific
948 * type that can be used directly by the driver to access the buffer. This can
949 * be for example physical address, pointer to scatter list or IOMMU mapping.
951 void *vb2_plane_cookie(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
, cookie
, vb
->planes
[plane_no
].mem_priv
);
958 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
961 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
962 * @vb: vb2_buffer returned from the driver
963 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
964 * VB2_BUF_STATE_ERROR if the operation finished with an error or
965 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
966 * If start_streaming fails then it should return buffers with state
967 * VB2_BUF_STATE_QUEUED to put them back into the queue.
969 * This function should be called by the driver after a hardware operation on
970 * a buffer is finished and the buffer may be returned to userspace. The driver
971 * cannot use this buffer anymore until it is queued back to it by videobuf
972 * by the means of buf_queue callback. Only buffers previously queued to the
973 * driver by buf_queue can be passed to this function.
975 * While streaming a buffer can only be returned in state DONE or ERROR.
976 * The start_streaming op can also return them in case the DMA engine cannot
977 * be started for some reason. In that case the buffers should be returned with
980 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
982 struct vb2_queue
*q
= vb
->vb2_queue
;
986 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_ACTIVE
))
989 if (WARN_ON(state
!= VB2_BUF_STATE_DONE
&&
990 state
!= VB2_BUF_STATE_ERROR
&&
991 state
!= VB2_BUF_STATE_QUEUED
&&
992 state
!= VB2_BUF_STATE_REQUEUEING
))
993 state
= VB2_BUF_STATE_ERROR
;
995 #ifdef CONFIG_VIDEO_ADV_DEBUG
997 * Although this is not a callback, it still does have to balance
998 * with the buf_queue op. So update this counter manually.
1002 dprintk(4, "done processing on buffer %d, state: %d\n",
1006 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1007 call_void_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
1009 spin_lock_irqsave(&q
->done_lock
, flags
);
1010 if (state
== VB2_BUF_STATE_QUEUED
||
1011 state
== VB2_BUF_STATE_REQUEUEING
) {
1012 vb
->state
= VB2_BUF_STATE_QUEUED
;
1014 /* Add the buffer to the done buffers list */
1015 list_add_tail(&vb
->done_entry
, &q
->done_list
);
1018 atomic_dec(&q
->owned_by_drv_count
);
1019 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1021 trace_vb2_buf_done(q
, vb
);
1024 case VB2_BUF_STATE_QUEUED
:
1026 case VB2_BUF_STATE_REQUEUEING
:
1027 if (q
->start_streaming_called
)
1028 __enqueue_in_driver(vb
);
1031 /* Inform any processes that may be waiting for buffers */
1032 wake_up(&q
->done_wq
);
1036 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
1039 * vb2_discard_done() - discard all buffers marked as DONE
1040 * @q: videobuf2 queue
1042 * This function is intended to be used with suspend/resume operations. It
1043 * discards all 'done' buffers as they would be too old to be requested after
1046 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1047 * delayed works before calling this function to make sure no buffer will be
1048 * touched by the driver and/or hardware.
1050 void vb2_discard_done(struct vb2_queue
*q
)
1052 struct vb2_buffer
*vb
;
1053 unsigned long flags
;
1055 spin_lock_irqsave(&q
->done_lock
, flags
);
1056 list_for_each_entry(vb
, &q
->done_list
, done_entry
)
1057 vb
->state
= VB2_BUF_STATE_ERROR
;
1058 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1060 EXPORT_SYMBOL_GPL(vb2_discard_done
);
1063 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1065 static int __qbuf_mmap(struct vb2_buffer
*vb
, const void *pb
)
1070 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1071 vb
, pb
, vb
->planes
);
1072 return ret
? ret
: call_vb_qop(vb
, buf_prepare
, vb
);
1076 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1078 static int __qbuf_userptr(struct vb2_buffer
*vb
, const void *pb
)
1080 struct vb2_plane planes
[VB2_MAX_PLANES
];
1081 struct vb2_queue
*q
= vb
->vb2_queue
;
1085 enum dma_data_direction dma_dir
=
1086 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1087 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1089 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1090 /* Copy relevant information provided by the userspace */
1092 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1097 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1098 /* Skip the plane if already verified */
1099 if (vb
->planes
[plane
].m
.userptr
&&
1100 vb
->planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1101 && vb
->planes
[plane
].length
== planes
[plane
].length
)
1104 dprintk(3, "userspace address for plane %d changed, "
1105 "reacquiring memory\n", plane
);
1107 /* Check if the provided plane buffer is large enough */
1108 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1109 dprintk(1, "provided buffer size %u is less than "
1110 "setup size %u for plane %d\n",
1111 planes
[plane
].length
,
1112 vb
->planes
[plane
].min_length
,
1118 /* Release previously acquired memory if present */
1119 if (vb
->planes
[plane
].mem_priv
) {
1122 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1124 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1127 vb
->planes
[plane
].mem_priv
= NULL
;
1128 vb
->planes
[plane
].bytesused
= 0;
1129 vb
->planes
[plane
].length
= 0;
1130 vb
->planes
[plane
].m
.userptr
= 0;
1131 vb
->planes
[plane
].data_offset
= 0;
1133 /* Acquire each plane's memory */
1134 mem_priv
= call_ptr_memop(vb
, get_userptr
, q
->alloc_ctx
[plane
],
1135 planes
[plane
].m
.userptr
,
1136 planes
[plane
].length
, dma_dir
);
1137 if (IS_ERR_OR_NULL(mem_priv
)) {
1138 dprintk(1, "failed acquiring userspace "
1139 "memory for plane %d\n", plane
);
1140 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
1143 vb
->planes
[plane
].mem_priv
= mem_priv
;
1147 * Now that everything is in order, copy relevant information
1148 * provided by userspace.
1150 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1151 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1152 vb
->planes
[plane
].length
= planes
[plane
].length
;
1153 vb
->planes
[plane
].m
.userptr
= planes
[plane
].m
.userptr
;
1154 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1159 * One or more planes changed, so we must call buf_init to do
1160 * the driver-specific initialization on the newly acquired
1161 * buffer, if provided.
1163 ret
= call_vb_qop(vb
, buf_init
, vb
);
1165 dprintk(1, "buffer initialization failed\n");
1170 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1172 dprintk(1, "buffer preparation failed\n");
1173 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1179 /* In case of errors, release planes that were already acquired */
1180 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1181 if (vb
->planes
[plane
].mem_priv
)
1182 call_void_memop(vb
, put_userptr
,
1183 vb
->planes
[plane
].mem_priv
);
1184 vb
->planes
[plane
].mem_priv
= NULL
;
1185 vb
->planes
[plane
].m
.userptr
= 0;
1186 vb
->planes
[plane
].length
= 0;
1193 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1195 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const void *pb
)
1197 struct vb2_plane planes
[VB2_MAX_PLANES
];
1198 struct vb2_queue
*q
= vb
->vb2_queue
;
1202 enum dma_data_direction dma_dir
=
1203 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1204 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1206 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1207 /* Copy relevant information provided by the userspace */
1209 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1214 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1215 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1217 if (IS_ERR_OR_NULL(dbuf
)) {
1218 dprintk(1, "invalid dmabuf fd for plane %d\n",
1224 /* use DMABUF size if length is not provided */
1225 if (planes
[plane
].length
== 0)
1226 planes
[plane
].length
= dbuf
->size
;
1228 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1229 dprintk(1, "invalid dmabuf length for plane %d\n",
1236 /* Skip the plane if already verified */
1237 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1238 vb
->planes
[plane
].length
== planes
[plane
].length
) {
1243 dprintk(1, "buffer for plane %d changed\n", plane
);
1247 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1250 /* Release previously acquired memory if present */
1251 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1252 vb
->planes
[plane
].bytesused
= 0;
1253 vb
->planes
[plane
].length
= 0;
1254 vb
->planes
[plane
].m
.fd
= 0;
1255 vb
->planes
[plane
].data_offset
= 0;
1257 /* Acquire each plane's memory */
1258 mem_priv
= call_ptr_memop(vb
, attach_dmabuf
,
1259 q
->alloc_ctx
[plane
], dbuf
, planes
[plane
].length
,
1261 if (IS_ERR(mem_priv
)) {
1262 dprintk(1, "failed to attach dmabuf\n");
1263 ret
= PTR_ERR(mem_priv
);
1268 vb
->planes
[plane
].dbuf
= dbuf
;
1269 vb
->planes
[plane
].mem_priv
= mem_priv
;
1272 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1273 * really we want to do this just before the DMA, not while queueing
1276 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1277 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1279 dprintk(1, "failed to map dmabuf for plane %d\n",
1283 vb
->planes
[plane
].dbuf_mapped
= 1;
1287 * Now that everything is in order, copy relevant information
1288 * provided by userspace.
1290 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1291 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1292 vb
->planes
[plane
].length
= planes
[plane
].length
;
1293 vb
->planes
[plane
].m
.fd
= planes
[plane
].m
.fd
;
1294 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1299 * Call driver-specific initialization on the newly acquired buffer,
1302 ret
= call_vb_qop(vb
, buf_init
, vb
);
1304 dprintk(1, "buffer initialization failed\n");
1309 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1311 dprintk(1, "buffer preparation failed\n");
1312 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1318 /* In case of errors, release planes that were already acquired */
1319 __vb2_buf_dmabuf_put(vb
);
1325 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1327 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1329 struct vb2_queue
*q
= vb
->vb2_queue
;
1332 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1333 atomic_inc(&q
->owned_by_drv_count
);
1335 trace_vb2_buf_queue(q
, vb
);
1338 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1339 call_void_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1341 call_void_vb_qop(vb
, buf_queue
, vb
);
1344 static int __buf_prepare(struct vb2_buffer
*vb
, const void *pb
)
1346 struct vb2_queue
*q
= vb
->vb2_queue
;
1350 dprintk(1, "fatal error occurred on queue\n");
1354 vb
->state
= VB2_BUF_STATE_PREPARING
;
1356 switch (q
->memory
) {
1357 case VB2_MEMORY_MMAP
:
1358 ret
= __qbuf_mmap(vb
, pb
);
1360 case VB2_MEMORY_USERPTR
:
1361 ret
= __qbuf_userptr(vb
, pb
);
1363 case VB2_MEMORY_DMABUF
:
1364 ret
= __qbuf_dmabuf(vb
, pb
);
1367 WARN(1, "Invalid queue type\n");
1372 dprintk(1, "buffer preparation failed: %d\n", ret
);
1373 vb
->state
= ret
? VB2_BUF_STATE_DEQUEUED
: VB2_BUF_STATE_PREPARED
;
1379 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1381 * @q: videobuf2 queue
1382 * @index: id number of the buffer
1383 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1386 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1387 * The passed buffer should have been verified.
1388 * This function calls buf_prepare callback in the driver (if provided),
1389 * in which driver-specific buffer initialization can be performed,
1391 * The return values from this function are intended to be directly returned
1392 * from vidioc_prepare_buf handler in driver.
1394 int vb2_core_prepare_buf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1396 struct vb2_buffer
*vb
;
1399 vb
= q
->bufs
[index
];
1400 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1401 dprintk(1, "invalid buffer state %d\n",
1406 ret
= __buf_prepare(vb
, pb
);
1410 /* Fill buffer information for the userspace */
1411 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1413 dprintk(1, "prepare of buffer %d succeeded\n", vb
->index
);
1417 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf
);
1420 * vb2_start_streaming() - Attempt to start streaming.
1421 * @q: videobuf2 queue
1423 * Attempt to start streaming. When this function is called there must be
1424 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1425 * number of buffers required for the DMA engine to function). If the
1426 * @start_streaming op fails it is supposed to return all the driver-owned
1427 * buffers back to vb2 in state QUEUED. Check if that happened and if
1428 * not warn and reclaim them forcefully.
1430 static int vb2_start_streaming(struct vb2_queue
*q
)
1432 struct vb2_buffer
*vb
;
1436 * If any buffers were queued before streamon,
1437 * we can now pass them to driver for processing.
1439 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1440 __enqueue_in_driver(vb
);
1442 /* Tell the driver to start streaming */
1443 q
->start_streaming_called
= 1;
1444 ret
= call_qop(q
, start_streaming
, q
,
1445 atomic_read(&q
->owned_by_drv_count
));
1449 q
->start_streaming_called
= 0;
1451 dprintk(1, "driver refused to start streaming\n");
1453 * If you see this warning, then the driver isn't cleaning up properly
1454 * after a failed start_streaming(). See the start_streaming()
1455 * documentation in videobuf2-core.h for more information how buffers
1456 * should be returned to vb2 in start_streaming().
1458 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1462 * Forcefully reclaim buffers if the driver did not
1463 * correctly return them to vb2.
1465 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1467 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1468 vb2_buffer_done(vb
, VB2_BUF_STATE_QUEUED
);
1470 /* Must be zero now */
1471 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1474 * If done_list is not empty, then start_streaming() didn't call
1475 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1478 WARN_ON(!list_empty(&q
->done_list
));
1483 * vb2_core_qbuf() - Queue a buffer from userspace
1484 * @q: videobuf2 queue
1485 * @index: id number of the buffer
1486 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1489 * Should be called from vidioc_qbuf ioctl handler of a driver.
1490 * The passed buffer should have been verified.
1492 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1493 * which driver-specific buffer initialization can be performed,
1494 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1495 * callback for processing.
1497 * The return values from this function are intended to be directly returned
1498 * from vidioc_qbuf handler in driver.
1500 int vb2_core_qbuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1502 struct vb2_buffer
*vb
;
1505 vb
= q
->bufs
[index
];
1507 switch (vb
->state
) {
1508 case VB2_BUF_STATE_DEQUEUED
:
1509 ret
= __buf_prepare(vb
, pb
);
1513 case VB2_BUF_STATE_PREPARED
:
1515 case VB2_BUF_STATE_PREPARING
:
1516 dprintk(1, "buffer still being prepared\n");
1519 dprintk(1, "invalid buffer state %d\n", vb
->state
);
1524 * Add to the queued buffers list, a buffer will stay on it until
1525 * dequeued in dqbuf.
1527 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1529 q
->waiting_for_buffers
= false;
1530 vb
->state
= VB2_BUF_STATE_QUEUED
;
1533 call_void_bufop(q
, copy_timestamp
, vb
, pb
);
1535 trace_vb2_qbuf(q
, vb
);
1538 * If already streaming, give the buffer to driver for processing.
1539 * If not, the buffer will be given to driver on next streamon.
1541 if (q
->start_streaming_called
)
1542 __enqueue_in_driver(vb
);
1544 /* Fill buffer information for the userspace */
1546 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1549 * If streamon has been called, and we haven't yet called
1550 * start_streaming() since not enough buffers were queued, and
1551 * we now have reached the minimum number of queued buffers,
1552 * then we can finally call start_streaming().
1554 if (q
->streaming
&& !q
->start_streaming_called
&&
1555 q
->queued_count
>= q
->min_buffers_needed
) {
1556 ret
= vb2_start_streaming(q
);
1561 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->index
);
1564 EXPORT_SYMBOL_GPL(vb2_core_qbuf
);
1567 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1570 * Will sleep if required for nonblocking == false.
1572 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1575 * All operations on vb_done_list are performed under done_lock
1576 * spinlock protection. However, buffers may be removed from
1577 * it and returned to userspace only while holding both driver's
1578 * lock and the done_lock spinlock. Thus we can be sure that as
1579 * long as we hold the driver's lock, the list will remain not
1580 * empty if list_empty() check succeeds.
1586 if (!q
->streaming
) {
1587 dprintk(1, "streaming off, will not wait for buffers\n");
1592 dprintk(1, "Queue in error state, will not wait for buffers\n");
1596 if (q
->last_buffer_dequeued
) {
1597 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1601 if (!list_empty(&q
->done_list
)) {
1603 * Found a buffer that we were waiting for.
1609 dprintk(1, "nonblocking and no buffers to dequeue, "
1615 * We are streaming and blocking, wait for another buffer to
1616 * become ready or for streamoff. Driver's lock is released to
1617 * allow streamoff or qbuf to be called while waiting.
1619 call_void_qop(q
, wait_prepare
, q
);
1622 * All locks have been released, it is safe to sleep now.
1624 dprintk(3, "will sleep waiting for buffers\n");
1625 ret
= wait_event_interruptible(q
->done_wq
,
1626 !list_empty(&q
->done_list
) || !q
->streaming
||
1630 * We need to reevaluate both conditions again after reacquiring
1631 * the locks or return an error if one occurred.
1633 call_void_qop(q
, wait_finish
, q
);
1635 dprintk(1, "sleep was interrupted\n");
1643 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1645 * Will sleep if required for nonblocking == false.
1647 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1650 unsigned long flags
;
1654 * Wait for at least one buffer to become available on the done_list.
1656 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1661 * Driver's lock has been held since we last verified that done_list
1662 * is not empty, so no need for another list_empty(done_list) check.
1664 spin_lock_irqsave(&q
->done_lock
, flags
);
1665 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1667 * Only remove the buffer from done_list if v4l2_buffer can handle all
1669 * Verifying planes is NOT necessary since it already has been checked
1670 * before the buffer is queued/prepared. So it can never fail.
1672 list_del(&(*vb
)->done_entry
);
1673 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1679 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1680 * @q: videobuf2 queue
1682 * This function will wait until all buffers that have been given to the driver
1683 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1684 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1685 * taken, for example from stop_streaming() callback.
1687 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1689 if (!q
->streaming
) {
1690 dprintk(1, "streaming off, will not wait for buffers\n");
1694 if (q
->start_streaming_called
)
1695 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1698 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1701 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1703 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1705 struct vb2_queue
*q
= vb
->vb2_queue
;
1708 /* nothing to do if the buffer is already dequeued */
1709 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1712 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1714 /* unmap DMABUF buffer */
1715 if (q
->memory
== VB2_MEMORY_DMABUF
)
1716 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1717 if (!vb
->planes
[i
].dbuf_mapped
)
1719 call_void_memop(vb
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1720 vb
->planes
[i
].dbuf_mapped
= 0;
1725 * vb2_dqbuf() - Dequeue a buffer to the userspace
1726 * @q: videobuf2 queue
1727 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1729 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1730 * buffers ready for dequeuing are present. Normally the driver
1731 * would be passing (file->f_flags & O_NONBLOCK) here
1733 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1734 * The passed buffer should have been verified.
1736 * 1) calls buf_finish callback in the driver (if provided), in which
1737 * driver can perform any additional operations that may be required before
1738 * returning the buffer to userspace, such as cache sync,
1739 * 2) the buffer struct members are filled with relevant information for
1742 * The return values from this function are intended to be directly returned
1743 * from vidioc_dqbuf handler in driver.
1745 int vb2_core_dqbuf(struct vb2_queue
*q
, unsigned int *pindex
, void *pb
,
1748 struct vb2_buffer
*vb
= NULL
;
1751 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1755 switch (vb
->state
) {
1756 case VB2_BUF_STATE_DONE
:
1757 dprintk(3, "returning done buffer\n");
1759 case VB2_BUF_STATE_ERROR
:
1760 dprintk(3, "returning done buffer with errors\n");
1763 dprintk(1, "invalid buffer state\n");
1767 call_void_vb_qop(vb
, buf_finish
, vb
);
1770 *pindex
= vb
->index
;
1772 /* Fill buffer information for the userspace */
1774 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1776 /* Remove from videobuf queue */
1777 list_del(&vb
->queued_entry
);
1780 trace_vb2_dqbuf(q
, vb
);
1782 /* go back to dequeued state */
1785 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1786 vb
->index
, vb
->state
);
1791 EXPORT_SYMBOL_GPL(vb2_core_dqbuf
);
1794 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1796 * Removes all queued buffers from driver's queue and all buffers queued by
1797 * userspace from videobuf's queue. Returns to state after reqbufs.
1799 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1804 * Tell driver to stop all transactions and release all queued
1807 if (q
->start_streaming_called
)
1808 call_void_qop(q
, stop_streaming
, q
);
1811 * If you see this warning, then the driver isn't cleaning up properly
1812 * in stop_streaming(). See the stop_streaming() documentation in
1813 * videobuf2-core.h for more information how buffers should be returned
1814 * to vb2 in stop_streaming().
1816 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1817 for (i
= 0; i
< q
->num_buffers
; ++i
)
1818 if (q
->bufs
[i
]->state
== VB2_BUF_STATE_ACTIVE
)
1819 vb2_buffer_done(q
->bufs
[i
], VB2_BUF_STATE_ERROR
);
1820 /* Must be zero now */
1821 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1825 q
->start_streaming_called
= 0;
1826 q
->queued_count
= 0;
1830 * Remove all buffers from videobuf's list...
1832 INIT_LIST_HEAD(&q
->queued_list
);
1834 * ...and done list; userspace will not receive any buffers it
1835 * has not already dequeued before initiating cancel.
1837 INIT_LIST_HEAD(&q
->done_list
);
1838 atomic_set(&q
->owned_by_drv_count
, 0);
1839 wake_up_all(&q
->done_wq
);
1842 * Reinitialize all buffers for next use.
1843 * Make sure to call buf_finish for any queued buffers. Normally
1844 * that's done in dqbuf, but that's not going to happen when we
1845 * cancel the whole queue. Note: this code belongs here, not in
1846 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1847 * call to __fill_user_buffer() after buf_finish(). That order can't
1848 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1850 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1851 struct vb2_buffer
*vb
= q
->bufs
[i
];
1853 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1854 vb
->state
= VB2_BUF_STATE_PREPARED
;
1855 call_void_vb_qop(vb
, buf_finish
, vb
);
1861 int vb2_core_streamon(struct vb2_queue
*q
, unsigned int type
)
1865 if (type
!= q
->type
) {
1866 dprintk(1, "invalid stream type\n");
1871 dprintk(3, "already streaming\n");
1875 if (!q
->num_buffers
) {
1876 dprintk(1, "no buffers have been allocated\n");
1880 if (q
->num_buffers
< q
->min_buffers_needed
) {
1881 dprintk(1, "need at least %u allocated buffers\n",
1882 q
->min_buffers_needed
);
1887 * Tell driver to start streaming provided sufficient buffers
1890 if (q
->queued_count
>= q
->min_buffers_needed
) {
1891 ret
= v4l_vb2q_enable_media_source(q
);
1894 ret
= vb2_start_streaming(q
);
1896 __vb2_queue_cancel(q
);
1903 dprintk(3, "successful\n");
1906 EXPORT_SYMBOL_GPL(vb2_core_streamon
);
1909 * vb2_queue_error() - signal a fatal error on the queue
1910 * @q: videobuf2 queue
1912 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1913 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1914 * buffers will return -EIO.
1916 * The error flag will be cleared when cancelling the queue, either from
1917 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1918 * function before starting the stream, otherwise the error flag will remain set
1919 * until the queue is released when closing the device node.
1921 void vb2_queue_error(struct vb2_queue
*q
)
1925 wake_up_all(&q
->done_wq
);
1927 EXPORT_SYMBOL_GPL(vb2_queue_error
);
1929 int vb2_core_streamoff(struct vb2_queue
*q
, unsigned int type
)
1931 if (type
!= q
->type
) {
1932 dprintk(1, "invalid stream type\n");
1937 * Cancel will pause streaming and remove all buffers from the driver
1938 * and videobuf, effectively returning control over them to userspace.
1940 * Note that we do this even if q->streaming == 0: if you prepare or
1941 * queue buffers, and then call streamoff without ever having called
1942 * streamon, you would still expect those buffers to be returned to
1943 * their normal dequeued state.
1945 __vb2_queue_cancel(q
);
1946 q
->waiting_for_buffers
= !q
->is_output
;
1947 q
->last_buffer_dequeued
= false;
1949 dprintk(3, "successful\n");
1952 EXPORT_SYMBOL_GPL(vb2_core_streamoff
);
1955 * __find_plane_by_offset() - find plane associated with the given offset off
1957 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1958 unsigned int *_buffer
, unsigned int *_plane
)
1960 struct vb2_buffer
*vb
;
1961 unsigned int buffer
, plane
;
1964 * Go over all buffers and their planes, comparing the given offset
1965 * with an offset assigned to each plane. If a match is found,
1966 * return its buffer and plane numbers.
1968 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1969 vb
= q
->bufs
[buffer
];
1971 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1972 if (vb
->planes
[plane
].m
.offset
== off
) {
1984 * vb2_core_expbuf() - Export a buffer as a file descriptor
1985 * @q: videobuf2 queue
1986 * @fd: file descriptor associated with DMABUF (set by driver) *
1987 * @type: buffer type
1988 * @index: id number of the buffer
1989 * @plane: index of the plane to be exported, 0 for single plane queues
1990 * @flags: flags for newly created file, currently only O_CLOEXEC is
1991 * supported, refer to manual of open syscall for more details
1993 * The return values from this function are intended to be directly returned
1994 * from vidioc_expbuf handler in driver.
1996 int vb2_core_expbuf(struct vb2_queue
*q
, int *fd
, unsigned int type
,
1997 unsigned int index
, unsigned int plane
, unsigned int flags
)
1999 struct vb2_buffer
*vb
= NULL
;
2000 struct vb2_plane
*vb_plane
;
2002 struct dma_buf
*dbuf
;
2004 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2005 dprintk(1, "queue is not currently set up for mmap\n");
2009 if (!q
->mem_ops
->get_dmabuf
) {
2010 dprintk(1, "queue does not support DMA buffer exporting\n");
2014 if (flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2015 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2019 if (type
!= q
->type
) {
2020 dprintk(1, "invalid buffer type\n");
2024 if (index
>= q
->num_buffers
) {
2025 dprintk(1, "buffer index out of range\n");
2029 vb
= q
->bufs
[index
];
2031 if (plane
>= vb
->num_planes
) {
2032 dprintk(1, "buffer plane out of range\n");
2036 if (vb2_fileio_is_active(q
)) {
2037 dprintk(1, "expbuf: file io in progress\n");
2041 vb_plane
= &vb
->planes
[plane
];
2043 dbuf
= call_ptr_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
,
2045 if (IS_ERR_OR_NULL(dbuf
)) {
2046 dprintk(1, "failed to export buffer %d, plane %d\n",
2051 ret
= dma_buf_fd(dbuf
, flags
& ~O_ACCMODE
);
2053 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2059 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2065 EXPORT_SYMBOL_GPL(vb2_core_expbuf
);
2068 * vb2_mmap() - map video buffers into application address space
2069 * @q: videobuf2 queue
2070 * @vma: vma passed to the mmap file operation handler in the driver
2072 * Should be called from mmap file operation handler of a driver.
2073 * This function maps one plane of one of the available video buffers to
2074 * userspace. To map whole video memory allocated on reqbufs, this function
2075 * has to be called once per each plane per each buffer previously allocated.
2077 * When the userspace application calls mmap, it passes to it an offset returned
2078 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2079 * a "cookie", which is then used to identify the plane to be mapped.
2080 * This function finds a plane with a matching offset and a mapping is performed
2081 * by the means of a provided memory operation.
2083 * The return values from this function are intended to be directly returned
2084 * from the mmap handler in driver.
2086 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2088 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2089 struct vb2_buffer
*vb
;
2090 unsigned int buffer
= 0, plane
= 0;
2092 unsigned long length
;
2094 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2095 dprintk(1, "queue is not currently set up for mmap\n");
2100 * Check memory area access mode.
2102 if (!(vma
->vm_flags
& VM_SHARED
)) {
2103 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2107 if (!(vma
->vm_flags
& VM_WRITE
)) {
2108 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2112 if (!(vma
->vm_flags
& VM_READ
)) {
2113 dprintk(1, "invalid vma flags, VM_READ needed\n");
2117 if (vb2_fileio_is_active(q
)) {
2118 dprintk(1, "mmap: file io in progress\n");
2123 * Find the plane corresponding to the offset passed by userspace.
2125 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2129 vb
= q
->bufs
[buffer
];
2132 * MMAP requires page_aligned buffers.
2133 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2134 * so, we need to do the same here.
2136 length
= PAGE_ALIGN(vb
->planes
[plane
].length
);
2137 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2139 "MMAP invalid, as it would overflow buffer length\n");
2143 mutex_lock(&q
->mmap_lock
);
2144 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2145 mutex_unlock(&q
->mmap_lock
);
2149 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2152 EXPORT_SYMBOL_GPL(vb2_mmap
);
2155 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2158 unsigned long pgoff
,
2159 unsigned long flags
)
2161 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2162 struct vb2_buffer
*vb
;
2163 unsigned int buffer
, plane
;
2167 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2168 dprintk(1, "queue is not currently set up for mmap\n");
2173 * Find the plane corresponding to the offset passed by userspace.
2175 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2179 vb
= q
->bufs
[buffer
];
2181 vaddr
= vb2_plane_vaddr(vb
, plane
);
2182 return vaddr
? (unsigned long)vaddr
: -EINVAL
;
2184 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2188 * vb2_core_queue_init() - initialize a videobuf2 queue
2189 * @q: videobuf2 queue; this structure should be allocated in driver
2191 * The vb2_queue structure should be allocated by the driver. The driver is
2192 * responsible of clearing it's content and setting initial values for some
2193 * required entries before calling this function.
2194 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2195 * to the struct vb2_queue description in include/media/videobuf2-core.h
2196 * for more information.
2198 int vb2_core_queue_init(struct vb2_queue
*q
)
2205 WARN_ON(!q
->mem_ops
) ||
2206 WARN_ON(!q
->type
) ||
2207 WARN_ON(!q
->io_modes
) ||
2208 WARN_ON(!q
->ops
->queue_setup
) ||
2209 WARN_ON(!q
->ops
->buf_queue
))
2212 INIT_LIST_HEAD(&q
->queued_list
);
2213 INIT_LIST_HEAD(&q
->done_list
);
2214 spin_lock_init(&q
->done_lock
);
2215 mutex_init(&q
->mmap_lock
);
2216 init_waitqueue_head(&q
->done_wq
);
2218 if (q
->buf_struct_size
== 0)
2219 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2223 EXPORT_SYMBOL_GPL(vb2_core_queue_init
);
2225 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2226 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2228 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2229 * @q: videobuf2 queue
2231 * This function stops streaming and performs necessary clean ups, including
2232 * freeing video buffer memory. The driver is responsible for freeing
2233 * the vb2_queue structure itself.
2235 void vb2_core_queue_release(struct vb2_queue
*q
)
2237 __vb2_cleanup_fileio(q
);
2238 __vb2_queue_cancel(q
);
2239 mutex_lock(&q
->mmap_lock
);
2240 __vb2_queue_free(q
, q
->num_buffers
);
2241 mutex_unlock(&q
->mmap_lock
);
2243 EXPORT_SYMBOL_GPL(vb2_core_queue_release
);
2246 * vb2_core_poll() - implements poll userspace operation
2247 * @q: videobuf2 queue
2248 * @file: file argument passed to the poll file operation handler
2249 * @wait: wait argument passed to the poll file operation handler
2251 * This function implements poll file operation handler for a driver.
2252 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2253 * be informed that the file descriptor of a video device is available for
2255 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2256 * will be reported as available for writing.
2258 * The return values from this function are intended to be directly returned
2259 * from poll handler in driver.
2261 unsigned int vb2_core_poll(struct vb2_queue
*q
, struct file
*file
,
2264 unsigned long req_events
= poll_requested_events(wait
);
2265 struct vb2_buffer
*vb
= NULL
;
2266 unsigned long flags
;
2268 if (!q
->is_output
&& !(req_events
& (POLLIN
| POLLRDNORM
)))
2270 if (q
->is_output
&& !(req_events
& (POLLOUT
| POLLWRNORM
)))
2274 * Start file I/O emulator only if streaming API has not been used yet.
2276 if (q
->num_buffers
== 0 && !vb2_fileio_is_active(q
)) {
2277 if (!q
->is_output
&& (q
->io_modes
& VB2_READ
) &&
2278 (req_events
& (POLLIN
| POLLRDNORM
))) {
2279 if (__vb2_init_fileio(q
, 1))
2282 if (q
->is_output
&& (q
->io_modes
& VB2_WRITE
) &&
2283 (req_events
& (POLLOUT
| POLLWRNORM
))) {
2284 if (__vb2_init_fileio(q
, 0))
2287 * Write to OUTPUT queue can be done immediately.
2289 return POLLOUT
| POLLWRNORM
;
2294 * There is nothing to wait for if the queue isn't streaming, or if the
2295 * error flag is set.
2297 if (!vb2_is_streaming(q
) || q
->error
)
2301 * For output streams you can call write() as long as there are fewer
2302 * buffers queued than there are buffers available.
2304 if (q
->is_output
&& q
->fileio
&& q
->queued_count
< q
->num_buffers
)
2305 return POLLOUT
| POLLWRNORM
;
2307 if (list_empty(&q
->done_list
)) {
2309 * If the last buffer was dequeued from a capture queue,
2310 * return immediately. DQBUF will return -EPIPE.
2312 if (q
->last_buffer_dequeued
)
2313 return POLLIN
| POLLRDNORM
;
2315 poll_wait(file
, &q
->done_wq
, wait
);
2319 * Take first buffer available for dequeuing.
2321 spin_lock_irqsave(&q
->done_lock
, flags
);
2322 if (!list_empty(&q
->done_list
))
2323 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2325 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2327 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2328 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2329 return (q
->is_output
) ?
2330 POLLOUT
| POLLWRNORM
:
2331 POLLIN
| POLLRDNORM
;
2335 EXPORT_SYMBOL_GPL(vb2_core_poll
);
2338 * struct vb2_fileio_buf - buffer context used by file io emulator
2340 * vb2 provides a compatibility layer and emulator of file io (read and
2341 * write) calls on top of streaming API. This structure is used for
2342 * tracking context related to the buffers.
2344 struct vb2_fileio_buf
{
2348 unsigned int queued
:1;
2352 * struct vb2_fileio_data - queue context used by file io emulator
2354 * @cur_index: the index of the buffer currently being read from or
2355 * written to. If equal to q->num_buffers then a new buffer
2357 * @initial_index: in the read() case all buffers are queued up immediately
2358 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2359 * buffers. However, in the write() case no buffers are initially
2360 * queued, instead whenever a buffer is full it is queued up by
2361 * __vb2_perform_fileio(). Only once all available buffers have
2362 * been queued up will __vb2_perform_fileio() start to dequeue
2363 * buffers. This means that initially __vb2_perform_fileio()
2364 * needs to know what buffer index to use when it is queuing up
2365 * the buffers for the first time. That initial index is stored
2366 * in this field. Once it is equal to q->num_buffers all
2367 * available buffers have been queued and __vb2_perform_fileio()
2368 * should start the normal dequeue/queue cycle.
2370 * vb2 provides a compatibility layer and emulator of file io (read and
2371 * write) calls on top of streaming API. For proper operation it required
2372 * this structure to save the driver state between each call of the read
2373 * or write function.
2375 struct vb2_fileio_data
{
2378 unsigned int memory
;
2379 struct vb2_fileio_buf bufs
[VB2_MAX_FRAME
];
2380 unsigned int cur_index
;
2381 unsigned int initial_index
;
2382 unsigned int q_count
;
2383 unsigned int dq_count
;
2384 unsigned read_once
:1;
2385 unsigned write_immediately
:1;
2389 * __vb2_init_fileio() - initialize file io emulator
2390 * @q: videobuf2 queue
2391 * @read: mode selector (1 means read, 0 means write)
2393 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2395 struct vb2_fileio_data
*fileio
;
2397 unsigned int count
= 0;
2402 if (WARN_ON((read
&& !(q
->io_modes
& VB2_READ
)) ||
2403 (!read
&& !(q
->io_modes
& VB2_WRITE
))))
2407 * Check if device supports mapping buffers to kernel virtual space.
2409 if (!q
->mem_ops
->vaddr
)
2413 * Check if streaming api has not been already activated.
2415 if (q
->streaming
|| q
->num_buffers
> 0)
2419 * Start with count 1, driver can increase it in queue_setup()
2423 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2424 (read
) ? "read" : "write", count
, q
->fileio_read_once
,
2425 q
->fileio_write_immediately
);
2427 fileio
= kzalloc(sizeof(*fileio
), GFP_KERNEL
);
2431 fileio
->read_once
= q
->fileio_read_once
;
2432 fileio
->write_immediately
= q
->fileio_write_immediately
;
2435 * Request buffers and use MMAP type to force driver
2436 * to allocate buffers by itself.
2438 fileio
->count
= count
;
2439 fileio
->memory
= VB2_MEMORY_MMAP
;
2440 fileio
->type
= q
->type
;
2442 ret
= vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2447 * Check if plane_count is correct
2448 * (multiplane buffers are not supported).
2450 if (q
->bufs
[0]->num_planes
!= 1) {
2456 * Get kernel address of each buffer.
2458 for (i
= 0; i
< q
->num_buffers
; i
++) {
2459 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2460 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2464 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2468 * Read mode requires pre queuing of all buffers.
2472 * Queue all buffers.
2474 for (i
= 0; i
< q
->num_buffers
; i
++) {
2475 ret
= vb2_core_qbuf(q
, i
, NULL
);
2478 fileio
->bufs
[i
].queued
= 1;
2481 * All buffers have been queued, so mark that by setting
2482 * initial_index to q->num_buffers
2484 fileio
->initial_index
= q
->num_buffers
;
2485 fileio
->cur_index
= q
->num_buffers
;
2491 ret
= vb2_core_streamon(q
, q
->type
);
2499 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2508 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2509 * @q: videobuf2 queue
2511 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2513 struct vb2_fileio_data
*fileio
= q
->fileio
;
2516 vb2_core_streamoff(q
, q
->type
);
2519 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2521 dprintk(3, "file io emulator closed\n");
2527 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2528 * @q: videobuf2 queue
2529 * @data: pointed to target userspace buffer
2530 * @count: number of bytes to read or write
2531 * @ppos: file handle position tracking pointer
2532 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2533 * @read: access mode selector (1 means read, 0 means write)
2535 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2536 loff_t
*ppos
, int nonblock
, int read
)
2538 struct vb2_fileio_data
*fileio
;
2539 struct vb2_fileio_buf
*buf
;
2540 bool is_multiplanar
= q
->is_multiplanar
;
2542 * When using write() to write data to an output video node the vb2 core
2543 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2544 * else is able to provide this information with the write() operation.
2546 bool copy_timestamp
= !read
&& q
->copy_timestamp
;
2550 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2551 read
? "read" : "write", (long)*ppos
, count
,
2552 nonblock
? "non" : "");
2558 * Initialize emulator on first call.
2560 if (!vb2_fileio_is_active(q
)) {
2561 ret
= __vb2_init_fileio(q
, read
);
2562 dprintk(3, "vb2_init_fileio result: %d\n", ret
);
2569 * Check if we need to dequeue the buffer.
2571 index
= fileio
->cur_index
;
2572 if (index
>= q
->num_buffers
) {
2573 struct vb2_buffer
*b
;
2576 * Call vb2_dqbuf to get buffer back.
2578 ret
= vb2_core_dqbuf(q
, &index
, NULL
, nonblock
);
2579 dprintk(5, "vb2_dqbuf result: %d\n", ret
);
2582 fileio
->dq_count
+= 1;
2584 fileio
->cur_index
= index
;
2585 buf
= &fileio
->bufs
[index
];
2589 * Get number of bytes filled by the driver
2593 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2594 : vb2_plane_size(q
->bufs
[index
], 0);
2595 /* Compensate for data_offset on read in the multiplanar case. */
2596 if (is_multiplanar
&& read
&&
2597 b
->planes
[0].data_offset
< buf
->size
) {
2598 buf
->pos
= b
->planes
[0].data_offset
;
2599 buf
->size
-= buf
->pos
;
2602 buf
= &fileio
->bufs
[index
];
2606 * Limit count on last few bytes of the buffer.
2608 if (buf
->pos
+ count
> buf
->size
) {
2609 count
= buf
->size
- buf
->pos
;
2610 dprintk(5, "reducing read count: %zd\n", count
);
2614 * Transfer data to userspace.
2616 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2617 count
, index
, buf
->pos
);
2619 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2621 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2623 dprintk(3, "error copying data\n");
2634 * Queue next buffer if required.
2636 if (buf
->pos
== buf
->size
|| (!read
&& fileio
->write_immediately
)) {
2637 struct vb2_buffer
*b
= q
->bufs
[index
];
2640 * Check if this is the last buffer to read.
2642 if (read
&& fileio
->read_once
&& fileio
->dq_count
== 1) {
2643 dprintk(3, "read limit reached\n");
2644 return __vb2_cleanup_fileio(q
);
2648 * Call vb2_qbuf and give buffer to the driver.
2650 b
->planes
[0].bytesused
= buf
->pos
;
2653 b
->timestamp
= ktime_get_ns();
2654 ret
= vb2_core_qbuf(q
, index
, NULL
);
2655 dprintk(5, "vb2_dbuf result: %d\n", ret
);
2660 * Buffer has been queued, update the status
2664 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2665 fileio
->q_count
+= 1;
2667 * If we are queuing up buffers for the first time, then
2668 * increase initial_index by one.
2670 if (fileio
->initial_index
< q
->num_buffers
)
2671 fileio
->initial_index
++;
2673 * The next buffer to use is either a buffer that's going to be
2674 * queued for the first time (initial_index < q->num_buffers)
2675 * or it is equal to q->num_buffers, meaning that the next
2676 * time we need to dequeue a buffer since we've now queued up
2677 * all the 'first time' buffers.
2679 fileio
->cur_index
= fileio
->initial_index
;
2683 * Return proper number of bytes processed.
2690 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2691 loff_t
*ppos
, int nonblocking
)
2693 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2695 EXPORT_SYMBOL_GPL(vb2_read
);
2697 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2698 loff_t
*ppos
, int nonblocking
)
2700 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2701 ppos
, nonblocking
, 0);
2703 EXPORT_SYMBOL_GPL(vb2_write
);
2705 struct vb2_threadio_data
{
2706 struct task_struct
*thread
;
2712 static int vb2_thread(void *data
)
2714 struct vb2_queue
*q
= data
;
2715 struct vb2_threadio_data
*threadio
= q
->threadio
;
2716 bool copy_timestamp
= false;
2717 unsigned prequeue
= 0;
2722 prequeue
= q
->num_buffers
;
2723 copy_timestamp
= q
->copy_timestamp
;
2729 struct vb2_buffer
*vb
;
2732 * Call vb2_dqbuf to get buffer back.
2735 vb
= q
->bufs
[index
++];
2738 call_void_qop(q
, wait_finish
, q
);
2739 if (!threadio
->stop
)
2740 ret
= vb2_core_dqbuf(q
, &index
, NULL
, 0);
2741 call_void_qop(q
, wait_prepare
, q
);
2742 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2744 vb
= q
->bufs
[index
];
2746 if (ret
|| threadio
->stop
)
2750 if (vb
->state
!= VB2_BUF_STATE_ERROR
)
2751 if (threadio
->fnc(vb
, threadio
->priv
))
2753 call_void_qop(q
, wait_finish
, q
);
2755 vb
->timestamp
= ktime_get_ns();;
2756 if (!threadio
->stop
)
2757 ret
= vb2_core_qbuf(q
, vb
->index
, NULL
);
2758 call_void_qop(q
, wait_prepare
, q
);
2759 if (ret
|| threadio
->stop
)
2763 /* Hmm, linux becomes *very* unhappy without this ... */
2764 while (!kthread_should_stop()) {
2765 set_current_state(TASK_INTERRUPTIBLE
);
2772 * This function should not be used for anything else but the videobuf2-dvb
2773 * support. If you think you have another good use-case for this, then please
2774 * contact the linux-media mailinglist first.
2776 int vb2_thread_start(struct vb2_queue
*q
, vb2_thread_fnc fnc
, void *priv
,
2777 const char *thread_name
)
2779 struct vb2_threadio_data
*threadio
;
2786 if (WARN_ON(q
->fileio
))
2789 threadio
= kzalloc(sizeof(*threadio
), GFP_KERNEL
);
2790 if (threadio
== NULL
)
2792 threadio
->fnc
= fnc
;
2793 threadio
->priv
= priv
;
2795 ret
= __vb2_init_fileio(q
, !q
->is_output
);
2796 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2799 q
->threadio
= threadio
;
2800 threadio
->thread
= kthread_run(vb2_thread
, q
, "vb2-%s", thread_name
);
2801 if (IS_ERR(threadio
->thread
)) {
2802 ret
= PTR_ERR(threadio
->thread
);
2803 threadio
->thread
= NULL
;
2809 __vb2_cleanup_fileio(q
);
2814 EXPORT_SYMBOL_GPL(vb2_thread_start
);
2816 int vb2_thread_stop(struct vb2_queue
*q
)
2818 struct vb2_threadio_data
*threadio
= q
->threadio
;
2821 if (threadio
== NULL
)
2823 threadio
->stop
= true;
2824 /* Wake up all pending sleeps in the thread */
2826 err
= kthread_stop(threadio
->thread
);
2827 __vb2_cleanup_fileio(q
);
2828 threadio
->thread
= NULL
;
2833 EXPORT_SYMBOL_GPL(vb2_thread_stop
);
2835 MODULE_DESCRIPTION("Media buffer core framework");
2836 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2837 MODULE_LICENSE("GPL");