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
,
210 q
->alloc_devs
[plane
] ? : q
->dev
,
211 q
->dma_attrs
, size
, dma_dir
, q
->gfp_flags
);
212 if (IS_ERR_OR_NULL(mem_priv
))
215 /* Associate allocator private data with this plane */
216 vb
->planes
[plane
].mem_priv
= mem_priv
;
221 /* Free already allocated memory if one of the allocations failed */
222 for (; plane
> 0; --plane
) {
223 call_void_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
224 vb
->planes
[plane
- 1].mem_priv
= NULL
;
231 * __vb2_buf_mem_free() - free memory of the given buffer
233 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
237 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
238 call_void_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
239 vb
->planes
[plane
].mem_priv
= NULL
;
240 dprintk(3, "freed plane %d of buffer %d\n", plane
, vb
->index
);
245 * __vb2_buf_userptr_put() - release userspace memory associated with
248 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
252 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
253 if (vb
->planes
[plane
].mem_priv
)
254 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
255 vb
->planes
[plane
].mem_priv
= NULL
;
260 * __vb2_plane_dmabuf_put() - release memory associated with
261 * a DMABUF shared plane
263 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
269 call_void_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
271 call_void_memop(vb
, detach_dmabuf
, p
->mem_priv
);
272 dma_buf_put(p
->dbuf
);
279 * __vb2_buf_dmabuf_put() - release memory associated with
280 * a DMABUF shared buffer
282 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
286 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
287 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
291 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
294 static void __setup_offsets(struct vb2_buffer
*vb
)
296 struct vb2_queue
*q
= vb
->vb2_queue
;
298 unsigned long off
= 0;
301 struct vb2_buffer
*prev
= q
->bufs
[vb
->index
- 1];
302 struct vb2_plane
*p
= &prev
->planes
[prev
->num_planes
- 1];
304 off
= PAGE_ALIGN(p
->m
.offset
+ p
->length
);
307 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
308 vb
->planes
[plane
].m
.offset
= off
;
310 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
311 vb
->index
, plane
, off
);
313 off
+= vb
->planes
[plane
].length
;
314 off
= PAGE_ALIGN(off
);
319 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
320 * video buffer memory for all buffers/planes on the queue and initializes the
323 * Returns the number of buffers successfully allocated.
325 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum vb2_memory memory
,
326 unsigned int num_buffers
, unsigned int num_planes
,
327 const unsigned plane_sizes
[VB2_MAX_PLANES
])
329 unsigned int buffer
, plane
;
330 struct vb2_buffer
*vb
;
333 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
334 /* Allocate videobuf buffer structures */
335 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
337 dprintk(1, "memory alloc for buffer struct failed\n");
341 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
343 vb
->num_planes
= num_planes
;
344 vb
->index
= q
->num_buffers
+ buffer
;
347 for (plane
= 0; plane
< num_planes
; ++plane
) {
348 vb
->planes
[plane
].length
= plane_sizes
[plane
];
349 vb
->planes
[plane
].min_length
= plane_sizes
[plane
];
351 q
->bufs
[vb
->index
] = vb
;
353 /* Allocate video buffer memory for the MMAP type */
354 if (memory
== VB2_MEMORY_MMAP
) {
355 ret
= __vb2_buf_mem_alloc(vb
);
357 dprintk(1, "failed allocating memory for "
358 "buffer %d\n", buffer
);
359 q
->bufs
[vb
->index
] = NULL
;
365 * Call the driver-provided buffer initialization
366 * callback, if given. An error in initialization
367 * results in queue setup failure.
369 ret
= call_vb_qop(vb
, buf_init
, vb
);
371 dprintk(1, "buffer %d %p initialization"
372 " failed\n", buffer
, vb
);
373 __vb2_buf_mem_free(vb
);
374 q
->bufs
[vb
->index
] = NULL
;
381 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
388 * __vb2_free_mem() - release all video buffer memory for a given queue
390 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
393 struct vb2_buffer
*vb
;
395 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
397 vb
= q
->bufs
[buffer
];
401 /* Free MMAP buffers or release USERPTR buffers */
402 if (q
->memory
== VB2_MEMORY_MMAP
)
403 __vb2_buf_mem_free(vb
);
404 else if (q
->memory
== VB2_MEMORY_DMABUF
)
405 __vb2_buf_dmabuf_put(vb
);
407 __vb2_buf_userptr_put(vb
);
412 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
413 * related information, if no buffers are left return the queue to an
414 * uninitialized state. Might be called even if the queue has already been freed.
416 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
421 * Sanity check: when preparing a buffer the queue lock is released for
422 * a short while (see __buf_prepare for the details), which would allow
423 * a race with a reqbufs which can call this function. Removing the
424 * buffers from underneath __buf_prepare is obviously a bad idea, so we
425 * check if any of the buffers is in the state PREPARING, and if so we
426 * just return -EAGAIN.
428 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
430 if (q
->bufs
[buffer
] == NULL
)
432 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
433 dprintk(1, "preparing buffers, cannot free\n");
438 /* Call driver-provided cleanup function for each buffer, if provided */
439 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
441 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
443 if (vb
&& vb
->planes
[0].mem_priv
)
444 call_void_vb_qop(vb
, buf_cleanup
, vb
);
447 /* Release video buffer memory */
448 __vb2_free_mem(q
, buffers
);
450 #ifdef CONFIG_VIDEO_ADV_DEBUG
452 * Check that all the calls were balances during the life-time of this
453 * queue. If not (or if the debug level is 1 or up), then dump the
454 * counters to the kernel log.
456 if (q
->num_buffers
) {
457 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
458 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
460 if (unbalanced
|| debug
) {
461 pr_info("vb2: counters for queue %p:%s\n", q
,
462 unbalanced
? " UNBALANCED!" : "");
463 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
464 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
465 q
->cnt_stop_streaming
);
466 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
467 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
469 q
->cnt_queue_setup
= 0;
470 q
->cnt_wait_prepare
= 0;
471 q
->cnt_wait_finish
= 0;
472 q
->cnt_start_streaming
= 0;
473 q
->cnt_stop_streaming
= 0;
475 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
476 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
477 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
478 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
479 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
480 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
481 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
482 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
483 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
484 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
486 if (unbalanced
|| debug
) {
487 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
488 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
489 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
490 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
491 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
492 pr_info("vb2: buf_queue: %u buf_done: %u\n",
493 vb
->cnt_buf_queue
, vb
->cnt_buf_done
);
494 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
495 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
496 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
498 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
499 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
500 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
501 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
502 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
503 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
504 vb
->cnt_mem_get_dmabuf
,
505 vb
->cnt_mem_num_users
,
512 /* Free videobuf buffers */
513 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
515 kfree(q
->bufs
[buffer
]);
516 q
->bufs
[buffer
] = NULL
;
519 q
->num_buffers
-= buffers
;
520 if (!q
->num_buffers
) {
522 INIT_LIST_HEAD(&q
->queued_list
);
528 * vb2_buffer_in_use() - return true if the buffer is in use and
529 * the queue cannot be freed (by the means of REQBUFS(0)) call
531 bool vb2_buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
534 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
535 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
537 * If num_users() has not been provided, call_memop
538 * will return 0, apparently nobody cares about this
539 * case anyway. If num_users() returns more than 1,
540 * we are not the only user of the plane's memory.
542 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
547 EXPORT_SYMBOL(vb2_buffer_in_use
);
550 * __buffers_in_use() - return true if any buffers on the queue are in use and
551 * the queue cannot be freed (by the means of REQBUFS(0)) call
553 static bool __buffers_in_use(struct vb2_queue
*q
)
556 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
557 if (vb2_buffer_in_use(q
, q
->bufs
[buffer
]))
564 * vb2_core_querybuf() - query video buffer information
566 * @index: id number of the buffer
567 * @pb: buffer struct passed from userspace
569 * Should be called from vidioc_querybuf ioctl handler in driver.
570 * The passed buffer should have been verified.
571 * This function fills the relevant information for the userspace.
573 void vb2_core_querybuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
575 call_void_bufop(q
, fill_user_buffer
, q
->bufs
[index
], pb
);
577 EXPORT_SYMBOL_GPL(vb2_core_querybuf
);
580 * __verify_userptr_ops() - verify that all memory operations required for
581 * USERPTR queue type have been provided
583 static int __verify_userptr_ops(struct vb2_queue
*q
)
585 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
586 !q
->mem_ops
->put_userptr
)
593 * __verify_mmap_ops() - verify that all memory operations required for
594 * MMAP queue type have been provided
596 static int __verify_mmap_ops(struct vb2_queue
*q
)
598 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
599 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
606 * __verify_dmabuf_ops() - verify that all memory operations required for
607 * DMABUF queue type have been provided
609 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
611 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
612 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
613 !q
->mem_ops
->unmap_dmabuf
)
620 * vb2_verify_memory_type() - Check whether the memory type and buffer type
621 * passed to a buffer operation are compatible with the queue.
623 int vb2_verify_memory_type(struct vb2_queue
*q
,
624 enum vb2_memory memory
, unsigned int type
)
626 if (memory
!= VB2_MEMORY_MMAP
&& memory
!= VB2_MEMORY_USERPTR
&&
627 memory
!= VB2_MEMORY_DMABUF
) {
628 dprintk(1, "unsupported memory type\n");
632 if (type
!= q
->type
) {
633 dprintk(1, "requested type is incorrect\n");
638 * Make sure all the required memory ops for given memory type
641 if (memory
== VB2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
642 dprintk(1, "MMAP for current setup unsupported\n");
646 if (memory
== VB2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
647 dprintk(1, "USERPTR for current setup unsupported\n");
651 if (memory
== VB2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
652 dprintk(1, "DMABUF for current setup unsupported\n");
657 * Place the busy tests at the end: -EBUSY can be ignored when
658 * create_bufs is called with count == 0, but count == 0 should still
659 * do the memory and type validation.
661 if (vb2_fileio_is_active(q
)) {
662 dprintk(1, "file io in progress\n");
667 EXPORT_SYMBOL(vb2_verify_memory_type
);
670 * vb2_core_reqbufs() - Initiate streaming
671 * @q: videobuf2 queue
672 * @memory: memory type
673 * @count: requested buffer count
675 * Should be called from vidioc_reqbufs ioctl handler of a driver.
677 * 1) verifies streaming parameters passed from the userspace,
678 * 2) sets up the queue,
679 * 3) negotiates number of buffers and planes per buffer with the driver
680 * to be used during streaming,
681 * 4) allocates internal buffer structures (struct vb2_buffer), according to
682 * the agreed parameters,
683 * 5) for MMAP memory type, allocates actual video memory, using the
684 * memory handling/allocation routines provided during queue initialization
686 * If req->count is 0, all the memory will be freed instead.
687 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
688 * and the queue is not busy, memory will be reallocated.
690 * The return values from this function are intended to be directly returned
691 * from vidioc_reqbufs handler in driver.
693 int vb2_core_reqbufs(struct vb2_queue
*q
, enum vb2_memory memory
,
696 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
697 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
701 dprintk(1, "streaming active\n");
705 if (*count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= memory
) {
707 * We already have buffers allocated, so first check if they
708 * are not in use and can be freed.
710 mutex_lock(&q
->mmap_lock
);
711 if (q
->memory
== VB2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
712 mutex_unlock(&q
->mmap_lock
);
713 dprintk(1, "memory in use, cannot free\n");
718 * Call queue_cancel to clean up any buffers in the PREPARED or
719 * QUEUED state which is possible if buffers were prepared or
720 * queued without ever calling STREAMON.
722 __vb2_queue_cancel(q
);
723 ret
= __vb2_queue_free(q
, q
->num_buffers
);
724 mutex_unlock(&q
->mmap_lock
);
729 * In case of REQBUFS(0) return immediately without calling
730 * driver's queue_setup() callback and allocating resources.
737 * Make sure the requested values and current defaults are sane.
739 num_buffers
= min_t(unsigned int, *count
, VB2_MAX_FRAME
);
740 num_buffers
= max_t(unsigned int, num_buffers
, q
->min_buffers_needed
);
741 memset(q
->alloc_devs
, 0, sizeof(q
->alloc_devs
));
745 * Ask the driver how many buffers and planes per buffer it requires.
746 * Driver also sets the size and allocator context for each plane.
748 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
749 plane_sizes
, q
->alloc_devs
);
753 /* Finally, allocate buffers and video memory */
755 __vb2_queue_alloc(q
, memory
, num_buffers
, num_planes
, plane_sizes
);
756 if (allocated_buffers
== 0) {
757 dprintk(1, "memory allocation failed\n");
762 * There is no point in continuing if we can't allocate the minimum
763 * number of buffers needed by this vb2_queue.
765 if (allocated_buffers
< q
->min_buffers_needed
)
769 * Check if driver can handle the allocated number of buffers.
771 if (!ret
&& allocated_buffers
< num_buffers
) {
772 num_buffers
= allocated_buffers
;
774 * num_planes is set by the previous queue_setup(), but since it
775 * signals to queue_setup() whether it is called from create_bufs()
776 * vs reqbufs() we zero it here to signal that queue_setup() is
777 * called for the reqbufs() case.
781 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
782 &num_planes
, plane_sizes
, q
->alloc_devs
);
784 if (!ret
&& allocated_buffers
< num_buffers
)
788 * Either the driver has accepted a smaller number of buffers,
789 * or .queue_setup() returned an error
793 mutex_lock(&q
->mmap_lock
);
794 q
->num_buffers
= allocated_buffers
;
798 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
799 * from q->num_buffers.
801 __vb2_queue_free(q
, allocated_buffers
);
802 mutex_unlock(&q
->mmap_lock
);
805 mutex_unlock(&q
->mmap_lock
);
808 * Return the number of successfully allocated buffers
811 *count
= allocated_buffers
;
812 q
->waiting_for_buffers
= !q
->is_output
;
816 EXPORT_SYMBOL_GPL(vb2_core_reqbufs
);
819 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
820 * @q: videobuf2 queue
821 * @memory: memory type
822 * @count: requested buffer count
823 * @parg: parameter passed to device driver
825 * Should be called from vidioc_create_bufs ioctl handler of a driver.
827 * 1) verifies parameter sanity
828 * 2) calls the .queue_setup() queue operation
829 * 3) performs any necessary memory allocations
831 * The return values from this function are intended to be directly returned
832 * from vidioc_create_bufs handler in driver.
834 int vb2_core_create_bufs(struct vb2_queue
*q
, enum vb2_memory memory
,
835 unsigned int *count
, unsigned requested_planes
,
836 const unsigned requested_sizes
[])
838 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
839 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
842 if (q
->num_buffers
== VB2_MAX_FRAME
) {
843 dprintk(1, "maximum number of buffers already allocated\n");
847 if (!q
->num_buffers
) {
848 memset(q
->alloc_devs
, 0, sizeof(q
->alloc_devs
));
850 q
->waiting_for_buffers
= !q
->is_output
;
853 num_buffers
= min(*count
, VB2_MAX_FRAME
- q
->num_buffers
);
855 if (requested_planes
&& requested_sizes
) {
856 num_planes
= requested_planes
;
857 memcpy(plane_sizes
, requested_sizes
, sizeof(plane_sizes
));
861 * Ask the driver, whether the requested number of buffers, planes per
862 * buffer and their sizes are acceptable
864 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
865 &num_planes
, plane_sizes
, q
->alloc_devs
);
869 /* Finally, allocate buffers and video memory */
870 allocated_buffers
= __vb2_queue_alloc(q
, memory
, num_buffers
,
871 num_planes
, plane_sizes
);
872 if (allocated_buffers
== 0) {
873 dprintk(1, "memory allocation failed\n");
878 * Check if driver can handle the so far allocated number of buffers.
880 if (allocated_buffers
< num_buffers
) {
881 num_buffers
= allocated_buffers
;
884 * q->num_buffers contains the total number of buffers, that the
885 * queue driver has set up
887 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
888 &num_planes
, plane_sizes
, q
->alloc_devs
);
890 if (!ret
&& allocated_buffers
< num_buffers
)
894 * Either the driver has accepted a smaller number of buffers,
895 * or .queue_setup() returned an error
899 mutex_lock(&q
->mmap_lock
);
900 q
->num_buffers
+= allocated_buffers
;
904 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
905 * from q->num_buffers.
907 __vb2_queue_free(q
, allocated_buffers
);
908 mutex_unlock(&q
->mmap_lock
);
911 mutex_unlock(&q
->mmap_lock
);
914 * Return the number of successfully allocated buffers
917 *count
= allocated_buffers
;
921 EXPORT_SYMBOL_GPL(vb2_core_create_bufs
);
924 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
925 * @vb: vb2_buffer to which the plane in question belongs to
926 * @plane_no: plane number for which the address is to be returned
928 * This function returns a kernel virtual address of a given plane if
929 * such a mapping exist, NULL otherwise.
931 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
933 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
936 return call_ptr_memop(vb
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
939 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
942 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
943 * @vb: vb2_buffer to which the plane in question belongs to
944 * @plane_no: plane number for which the cookie is to be returned
946 * This function returns an allocator specific cookie for a given plane if
947 * available, NULL otherwise. The allocator should provide some simple static
948 * inline function, which would convert this cookie to the allocator specific
949 * type that can be used directly by the driver to access the buffer. This can
950 * be for example physical address, pointer to scatter list or IOMMU mapping.
952 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
954 if (plane_no
>= vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
957 return call_ptr_memop(vb
, cookie
, vb
->planes
[plane_no
].mem_priv
);
959 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
962 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
963 * @vb: vb2_buffer returned from the driver
964 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
965 * VB2_BUF_STATE_ERROR if the operation finished with an error or
966 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
967 * If start_streaming fails then it should return buffers with state
968 * VB2_BUF_STATE_QUEUED to put them back into the queue.
970 * This function should be called by the driver after a hardware operation on
971 * a buffer is finished and the buffer may be returned to userspace. The driver
972 * cannot use this buffer anymore until it is queued back to it by videobuf
973 * by the means of buf_queue callback. Only buffers previously queued to the
974 * driver by buf_queue can be passed to this function.
976 * While streaming a buffer can only be returned in state DONE or ERROR.
977 * The start_streaming op can also return them in case the DMA engine cannot
978 * be started for some reason. In that case the buffers should be returned with
981 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
983 struct vb2_queue
*q
= vb
->vb2_queue
;
987 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_ACTIVE
))
990 if (WARN_ON(state
!= VB2_BUF_STATE_DONE
&&
991 state
!= VB2_BUF_STATE_ERROR
&&
992 state
!= VB2_BUF_STATE_QUEUED
&&
993 state
!= VB2_BUF_STATE_REQUEUEING
))
994 state
= VB2_BUF_STATE_ERROR
;
996 #ifdef CONFIG_VIDEO_ADV_DEBUG
998 * Although this is not a callback, it still does have to balance
999 * with the buf_queue op. So update this counter manually.
1003 dprintk(4, "done processing on buffer %d, state: %d\n",
1007 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1008 call_void_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
1010 spin_lock_irqsave(&q
->done_lock
, flags
);
1011 if (state
== VB2_BUF_STATE_QUEUED
||
1012 state
== VB2_BUF_STATE_REQUEUEING
) {
1013 vb
->state
= VB2_BUF_STATE_QUEUED
;
1015 /* Add the buffer to the done buffers list */
1016 list_add_tail(&vb
->done_entry
, &q
->done_list
);
1019 atomic_dec(&q
->owned_by_drv_count
);
1020 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1022 trace_vb2_buf_done(q
, vb
);
1025 case VB2_BUF_STATE_QUEUED
:
1027 case VB2_BUF_STATE_REQUEUEING
:
1028 if (q
->start_streaming_called
)
1029 __enqueue_in_driver(vb
);
1032 /* Inform any processes that may be waiting for buffers */
1033 wake_up(&q
->done_wq
);
1037 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
1040 * vb2_discard_done() - discard all buffers marked as DONE
1041 * @q: videobuf2 queue
1043 * This function is intended to be used with suspend/resume operations. It
1044 * discards all 'done' buffers as they would be too old to be requested after
1047 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1048 * delayed works before calling this function to make sure no buffer will be
1049 * touched by the driver and/or hardware.
1051 void vb2_discard_done(struct vb2_queue
*q
)
1053 struct vb2_buffer
*vb
;
1054 unsigned long flags
;
1056 spin_lock_irqsave(&q
->done_lock
, flags
);
1057 list_for_each_entry(vb
, &q
->done_list
, done_entry
)
1058 vb
->state
= VB2_BUF_STATE_ERROR
;
1059 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1061 EXPORT_SYMBOL_GPL(vb2_discard_done
);
1064 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1066 static int __qbuf_mmap(struct vb2_buffer
*vb
, const void *pb
)
1071 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1072 vb
, pb
, vb
->planes
);
1073 return ret
? ret
: call_vb_qop(vb
, buf_prepare
, vb
);
1077 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1079 static int __qbuf_userptr(struct vb2_buffer
*vb
, const void *pb
)
1081 struct vb2_plane planes
[VB2_MAX_PLANES
];
1082 struct vb2_queue
*q
= vb
->vb2_queue
;
1086 enum dma_data_direction dma_dir
=
1087 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1088 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1090 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1091 /* Copy relevant information provided by the userspace */
1093 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1098 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1099 /* Skip the plane if already verified */
1100 if (vb
->planes
[plane
].m
.userptr
&&
1101 vb
->planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1102 && vb
->planes
[plane
].length
== planes
[plane
].length
)
1105 dprintk(3, "userspace address for plane %d changed, "
1106 "reacquiring memory\n", plane
);
1108 /* Check if the provided plane buffer is large enough */
1109 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1110 dprintk(1, "provided buffer size %u is less than "
1111 "setup size %u for plane %d\n",
1112 planes
[plane
].length
,
1113 vb
->planes
[plane
].min_length
,
1119 /* Release previously acquired memory if present */
1120 if (vb
->planes
[plane
].mem_priv
) {
1123 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1125 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1128 vb
->planes
[plane
].mem_priv
= NULL
;
1129 vb
->planes
[plane
].bytesused
= 0;
1130 vb
->planes
[plane
].length
= 0;
1131 vb
->planes
[plane
].m
.userptr
= 0;
1132 vb
->planes
[plane
].data_offset
= 0;
1134 /* Acquire each plane's memory */
1135 mem_priv
= call_ptr_memop(vb
, get_userptr
,
1136 q
->alloc_devs
[plane
] ? : q
->dev
,
1137 planes
[plane
].m
.userptr
,
1138 planes
[plane
].length
, dma_dir
);
1139 if (IS_ERR_OR_NULL(mem_priv
)) {
1140 dprintk(1, "failed acquiring userspace "
1141 "memory for plane %d\n", plane
);
1142 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
1145 vb
->planes
[plane
].mem_priv
= mem_priv
;
1149 * Now that everything is in order, copy relevant information
1150 * provided by userspace.
1152 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1153 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1154 vb
->planes
[plane
].length
= planes
[plane
].length
;
1155 vb
->planes
[plane
].m
.userptr
= planes
[plane
].m
.userptr
;
1156 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1161 * One or more planes changed, so we must call buf_init to do
1162 * the driver-specific initialization on the newly acquired
1163 * buffer, if provided.
1165 ret
= call_vb_qop(vb
, buf_init
, vb
);
1167 dprintk(1, "buffer initialization failed\n");
1172 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1174 dprintk(1, "buffer preparation failed\n");
1175 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1181 /* In case of errors, release planes that were already acquired */
1182 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1183 if (vb
->planes
[plane
].mem_priv
)
1184 call_void_memop(vb
, put_userptr
,
1185 vb
->planes
[plane
].mem_priv
);
1186 vb
->planes
[plane
].mem_priv
= NULL
;
1187 vb
->planes
[plane
].m
.userptr
= 0;
1188 vb
->planes
[plane
].length
= 0;
1195 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1197 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const void *pb
)
1199 struct vb2_plane planes
[VB2_MAX_PLANES
];
1200 struct vb2_queue
*q
= vb
->vb2_queue
;
1204 enum dma_data_direction dma_dir
=
1205 q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1206 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1208 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1209 /* Copy relevant information provided by the userspace */
1211 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1216 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1217 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1219 if (IS_ERR_OR_NULL(dbuf
)) {
1220 dprintk(1, "invalid dmabuf fd for plane %d\n",
1226 /* use DMABUF size if length is not provided */
1227 if (planes
[plane
].length
== 0)
1228 planes
[plane
].length
= dbuf
->size
;
1230 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1231 dprintk(1, "invalid dmabuf length for plane %d\n",
1238 /* Skip the plane if already verified */
1239 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1240 vb
->planes
[plane
].length
== planes
[plane
].length
) {
1245 dprintk(1, "buffer for plane %d changed\n", plane
);
1249 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1252 /* Release previously acquired memory if present */
1253 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1254 vb
->planes
[plane
].bytesused
= 0;
1255 vb
->planes
[plane
].length
= 0;
1256 vb
->planes
[plane
].m
.fd
= 0;
1257 vb
->planes
[plane
].data_offset
= 0;
1259 /* Acquire each plane's memory */
1260 mem_priv
= call_ptr_memop(vb
, attach_dmabuf
,
1261 q
->alloc_devs
[plane
] ? : q
->dev
,
1262 dbuf
, planes
[plane
].length
, dma_dir
);
1263 if (IS_ERR(mem_priv
)) {
1264 dprintk(1, "failed to attach dmabuf\n");
1265 ret
= PTR_ERR(mem_priv
);
1270 vb
->planes
[plane
].dbuf
= dbuf
;
1271 vb
->planes
[plane
].mem_priv
= mem_priv
;
1274 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1275 * really we want to do this just before the DMA, not while queueing
1278 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1279 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1281 dprintk(1, "failed to map dmabuf for plane %d\n",
1285 vb
->planes
[plane
].dbuf_mapped
= 1;
1289 * Now that everything is in order, copy relevant information
1290 * provided by userspace.
1292 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1293 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1294 vb
->planes
[plane
].length
= planes
[plane
].length
;
1295 vb
->planes
[plane
].m
.fd
= planes
[plane
].m
.fd
;
1296 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1301 * Call driver-specific initialization on the newly acquired buffer,
1304 ret
= call_vb_qop(vb
, buf_init
, vb
);
1306 dprintk(1, "buffer initialization failed\n");
1311 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1313 dprintk(1, "buffer preparation failed\n");
1314 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1320 /* In case of errors, release planes that were already acquired */
1321 __vb2_buf_dmabuf_put(vb
);
1327 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1329 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1331 struct vb2_queue
*q
= vb
->vb2_queue
;
1334 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1335 atomic_inc(&q
->owned_by_drv_count
);
1337 trace_vb2_buf_queue(q
, vb
);
1340 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1341 call_void_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1343 call_void_vb_qop(vb
, buf_queue
, vb
);
1346 static int __buf_prepare(struct vb2_buffer
*vb
, const void *pb
)
1348 struct vb2_queue
*q
= vb
->vb2_queue
;
1352 dprintk(1, "fatal error occurred on queue\n");
1356 vb
->state
= VB2_BUF_STATE_PREPARING
;
1358 switch (q
->memory
) {
1359 case VB2_MEMORY_MMAP
:
1360 ret
= __qbuf_mmap(vb
, pb
);
1362 case VB2_MEMORY_USERPTR
:
1363 ret
= __qbuf_userptr(vb
, pb
);
1365 case VB2_MEMORY_DMABUF
:
1366 ret
= __qbuf_dmabuf(vb
, pb
);
1369 WARN(1, "Invalid queue type\n");
1374 dprintk(1, "buffer preparation failed: %d\n", ret
);
1375 vb
->state
= ret
? VB2_BUF_STATE_DEQUEUED
: VB2_BUF_STATE_PREPARED
;
1381 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1383 * @q: videobuf2 queue
1384 * @index: id number of the buffer
1385 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1388 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1389 * The passed buffer should have been verified.
1390 * This function calls buf_prepare callback in the driver (if provided),
1391 * in which driver-specific buffer initialization can be performed,
1393 * The return values from this function are intended to be directly returned
1394 * from vidioc_prepare_buf handler in driver.
1396 int vb2_core_prepare_buf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1398 struct vb2_buffer
*vb
;
1401 vb
= q
->bufs
[index
];
1402 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1403 dprintk(1, "invalid buffer state %d\n",
1408 ret
= __buf_prepare(vb
, pb
);
1412 /* Fill buffer information for the userspace */
1413 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1415 dprintk(1, "prepare of buffer %d succeeded\n", vb
->index
);
1419 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf
);
1422 * vb2_start_streaming() - Attempt to start streaming.
1423 * @q: videobuf2 queue
1425 * Attempt to start streaming. When this function is called there must be
1426 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1427 * number of buffers required for the DMA engine to function). If the
1428 * @start_streaming op fails it is supposed to return all the driver-owned
1429 * buffers back to vb2 in state QUEUED. Check if that happened and if
1430 * not warn and reclaim them forcefully.
1432 static int vb2_start_streaming(struct vb2_queue
*q
)
1434 struct vb2_buffer
*vb
;
1438 * If any buffers were queued before streamon,
1439 * we can now pass them to driver for processing.
1441 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1442 __enqueue_in_driver(vb
);
1444 /* Tell the driver to start streaming */
1445 q
->start_streaming_called
= 1;
1446 ret
= call_qop(q
, start_streaming
, q
,
1447 atomic_read(&q
->owned_by_drv_count
));
1451 q
->start_streaming_called
= 0;
1453 dprintk(1, "driver refused to start streaming\n");
1455 * If you see this warning, then the driver isn't cleaning up properly
1456 * after a failed start_streaming(). See the start_streaming()
1457 * documentation in videobuf2-core.h for more information how buffers
1458 * should be returned to vb2 in start_streaming().
1460 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1464 * Forcefully reclaim buffers if the driver did not
1465 * correctly return them to vb2.
1467 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1469 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1470 vb2_buffer_done(vb
, VB2_BUF_STATE_QUEUED
);
1472 /* Must be zero now */
1473 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1476 * If done_list is not empty, then start_streaming() didn't call
1477 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1480 WARN_ON(!list_empty(&q
->done_list
));
1485 * vb2_core_qbuf() - Queue a buffer from userspace
1486 * @q: videobuf2 queue
1487 * @index: id number of the buffer
1488 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1491 * Should be called from vidioc_qbuf ioctl handler of a driver.
1492 * The passed buffer should have been verified.
1494 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1495 * which driver-specific buffer initialization can be performed,
1496 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1497 * callback for processing.
1499 * The return values from this function are intended to be directly returned
1500 * from vidioc_qbuf handler in driver.
1502 int vb2_core_qbuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1504 struct vb2_buffer
*vb
;
1507 vb
= q
->bufs
[index
];
1509 switch (vb
->state
) {
1510 case VB2_BUF_STATE_DEQUEUED
:
1511 ret
= __buf_prepare(vb
, pb
);
1515 case VB2_BUF_STATE_PREPARED
:
1517 case VB2_BUF_STATE_PREPARING
:
1518 dprintk(1, "buffer still being prepared\n");
1521 dprintk(1, "invalid buffer state %d\n", vb
->state
);
1526 * Add to the queued buffers list, a buffer will stay on it until
1527 * dequeued in dqbuf.
1529 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1531 q
->waiting_for_buffers
= false;
1532 vb
->state
= VB2_BUF_STATE_QUEUED
;
1535 call_void_bufop(q
, copy_timestamp
, vb
, pb
);
1537 trace_vb2_qbuf(q
, vb
);
1540 * If already streaming, give the buffer to driver for processing.
1541 * If not, the buffer will be given to driver on next streamon.
1543 if (q
->start_streaming_called
)
1544 __enqueue_in_driver(vb
);
1546 /* Fill buffer information for the userspace */
1548 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1551 * If streamon has been called, and we haven't yet called
1552 * start_streaming() since not enough buffers were queued, and
1553 * we now have reached the minimum number of queued buffers,
1554 * then we can finally call start_streaming().
1556 if (q
->streaming
&& !q
->start_streaming_called
&&
1557 q
->queued_count
>= q
->min_buffers_needed
) {
1558 ret
= vb2_start_streaming(q
);
1563 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->index
);
1566 EXPORT_SYMBOL_GPL(vb2_core_qbuf
);
1569 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1572 * Will sleep if required for nonblocking == false.
1574 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1577 * All operations on vb_done_list are performed under done_lock
1578 * spinlock protection. However, buffers may be removed from
1579 * it and returned to userspace only while holding both driver's
1580 * lock and the done_lock spinlock. Thus we can be sure that as
1581 * long as we hold the driver's lock, the list will remain not
1582 * empty if list_empty() check succeeds.
1588 if (!q
->streaming
) {
1589 dprintk(1, "streaming off, will not wait for buffers\n");
1594 dprintk(1, "Queue in error state, will not wait for buffers\n");
1598 if (q
->last_buffer_dequeued
) {
1599 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1603 if (!list_empty(&q
->done_list
)) {
1605 * Found a buffer that we were waiting for.
1611 dprintk(1, "nonblocking and no buffers to dequeue, "
1617 * We are streaming and blocking, wait for another buffer to
1618 * become ready or for streamoff. Driver's lock is released to
1619 * allow streamoff or qbuf to be called while waiting.
1621 call_void_qop(q
, wait_prepare
, q
);
1624 * All locks have been released, it is safe to sleep now.
1626 dprintk(3, "will sleep waiting for buffers\n");
1627 ret
= wait_event_interruptible(q
->done_wq
,
1628 !list_empty(&q
->done_list
) || !q
->streaming
||
1632 * We need to reevaluate both conditions again after reacquiring
1633 * the locks or return an error if one occurred.
1635 call_void_qop(q
, wait_finish
, q
);
1637 dprintk(1, "sleep was interrupted\n");
1645 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1647 * Will sleep if required for nonblocking == false.
1649 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1650 void *pb
, int nonblocking
)
1652 unsigned long flags
;
1656 * Wait for at least one buffer to become available on the done_list.
1658 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1663 * Driver's lock has been held since we last verified that done_list
1664 * is not empty, so no need for another list_empty(done_list) check.
1666 spin_lock_irqsave(&q
->done_lock
, flags
);
1667 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1669 * Only remove the buffer from done_list if all planes can be
1670 * handled. Some cases such as V4L2 file I/O and DVB have pb
1671 * == NULL; skip the check then as there's nothing to verify.
1674 ret
= call_bufop(q
, verify_planes_array
, *vb
, pb
);
1676 list_del(&(*vb
)->done_entry
);
1677 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1683 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1684 * @q: videobuf2 queue
1686 * This function will wait until all buffers that have been given to the driver
1687 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1688 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1689 * taken, for example from stop_streaming() callback.
1691 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1693 if (!q
->streaming
) {
1694 dprintk(1, "streaming off, will not wait for buffers\n");
1698 if (q
->start_streaming_called
)
1699 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1702 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1705 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1707 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1709 struct vb2_queue
*q
= vb
->vb2_queue
;
1712 /* nothing to do if the buffer is already dequeued */
1713 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1716 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1718 /* unmap DMABUF buffer */
1719 if (q
->memory
== VB2_MEMORY_DMABUF
)
1720 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1721 if (!vb
->planes
[i
].dbuf_mapped
)
1723 call_void_memop(vb
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1724 vb
->planes
[i
].dbuf_mapped
= 0;
1729 * vb2_dqbuf() - Dequeue a buffer to the userspace
1730 * @q: videobuf2 queue
1731 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1733 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1734 * buffers ready for dequeuing are present. Normally the driver
1735 * would be passing (file->f_flags & O_NONBLOCK) here
1737 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1738 * The passed buffer should have been verified.
1740 * 1) calls buf_finish callback in the driver (if provided), in which
1741 * driver can perform any additional operations that may be required before
1742 * returning the buffer to userspace, such as cache sync,
1743 * 2) the buffer struct members are filled with relevant information for
1746 * The return values from this function are intended to be directly returned
1747 * from vidioc_dqbuf handler in driver.
1749 int vb2_core_dqbuf(struct vb2_queue
*q
, unsigned int *pindex
, void *pb
,
1752 struct vb2_buffer
*vb
= NULL
;
1755 ret
= __vb2_get_done_vb(q
, &vb
, pb
, nonblocking
);
1759 switch (vb
->state
) {
1760 case VB2_BUF_STATE_DONE
:
1761 dprintk(3, "returning done buffer\n");
1763 case VB2_BUF_STATE_ERROR
:
1764 dprintk(3, "returning done buffer with errors\n");
1767 dprintk(1, "invalid buffer state\n");
1771 call_void_vb_qop(vb
, buf_finish
, vb
);
1774 *pindex
= vb
->index
;
1776 /* Fill buffer information for the userspace */
1778 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1780 /* Remove from videobuf queue */
1781 list_del(&vb
->queued_entry
);
1784 trace_vb2_dqbuf(q
, vb
);
1786 /* go back to dequeued state */
1789 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1790 vb
->index
, vb
->state
);
1795 EXPORT_SYMBOL_GPL(vb2_core_dqbuf
);
1798 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1800 * Removes all queued buffers from driver's queue and all buffers queued by
1801 * userspace from videobuf's queue. Returns to state after reqbufs.
1803 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1808 * Tell driver to stop all transactions and release all queued
1811 if (q
->start_streaming_called
)
1812 call_void_qop(q
, stop_streaming
, q
);
1815 * If you see this warning, then the driver isn't cleaning up properly
1816 * in stop_streaming(). See the stop_streaming() documentation in
1817 * videobuf2-core.h for more information how buffers should be returned
1818 * to vb2 in stop_streaming().
1820 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1821 for (i
= 0; i
< q
->num_buffers
; ++i
)
1822 if (q
->bufs
[i
]->state
== VB2_BUF_STATE_ACTIVE
)
1823 vb2_buffer_done(q
->bufs
[i
], VB2_BUF_STATE_ERROR
);
1824 /* Must be zero now */
1825 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1829 q
->start_streaming_called
= 0;
1830 q
->queued_count
= 0;
1834 * Remove all buffers from videobuf's list...
1836 INIT_LIST_HEAD(&q
->queued_list
);
1838 * ...and done list; userspace will not receive any buffers it
1839 * has not already dequeued before initiating cancel.
1841 INIT_LIST_HEAD(&q
->done_list
);
1842 atomic_set(&q
->owned_by_drv_count
, 0);
1843 wake_up_all(&q
->done_wq
);
1846 * Reinitialize all buffers for next use.
1847 * Make sure to call buf_finish for any queued buffers. Normally
1848 * that's done in dqbuf, but that's not going to happen when we
1849 * cancel the whole queue. Note: this code belongs here, not in
1850 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1851 * call to __fill_user_buffer() after buf_finish(). That order can't
1852 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1854 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1855 struct vb2_buffer
*vb
= q
->bufs
[i
];
1857 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1858 vb
->state
= VB2_BUF_STATE_PREPARED
;
1859 call_void_vb_qop(vb
, buf_finish
, vb
);
1865 int vb2_core_streamon(struct vb2_queue
*q
, unsigned int type
)
1869 if (type
!= q
->type
) {
1870 dprintk(1, "invalid stream type\n");
1875 dprintk(3, "already streaming\n");
1879 if (!q
->num_buffers
) {
1880 dprintk(1, "no buffers have been allocated\n");
1884 if (q
->num_buffers
< q
->min_buffers_needed
) {
1885 dprintk(1, "need at least %u allocated buffers\n",
1886 q
->min_buffers_needed
);
1891 * Tell driver to start streaming provided sufficient buffers
1894 if (q
->queued_count
>= q
->min_buffers_needed
) {
1895 ret
= v4l_vb2q_enable_media_source(q
);
1898 ret
= vb2_start_streaming(q
);
1900 __vb2_queue_cancel(q
);
1907 dprintk(3, "successful\n");
1910 EXPORT_SYMBOL_GPL(vb2_core_streamon
);
1913 * vb2_queue_error() - signal a fatal error on the queue
1914 * @q: videobuf2 queue
1916 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1917 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1918 * buffers will return -EIO.
1920 * The error flag will be cleared when cancelling the queue, either from
1921 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1922 * function before starting the stream, otherwise the error flag will remain set
1923 * until the queue is released when closing the device node.
1925 void vb2_queue_error(struct vb2_queue
*q
)
1929 wake_up_all(&q
->done_wq
);
1931 EXPORT_SYMBOL_GPL(vb2_queue_error
);
1933 int vb2_core_streamoff(struct vb2_queue
*q
, unsigned int type
)
1935 if (type
!= q
->type
) {
1936 dprintk(1, "invalid stream type\n");
1941 * Cancel will pause streaming and remove all buffers from the driver
1942 * and videobuf, effectively returning control over them to userspace.
1944 * Note that we do this even if q->streaming == 0: if you prepare or
1945 * queue buffers, and then call streamoff without ever having called
1946 * streamon, you would still expect those buffers to be returned to
1947 * their normal dequeued state.
1949 __vb2_queue_cancel(q
);
1950 q
->waiting_for_buffers
= !q
->is_output
;
1951 q
->last_buffer_dequeued
= false;
1953 dprintk(3, "successful\n");
1956 EXPORT_SYMBOL_GPL(vb2_core_streamoff
);
1959 * __find_plane_by_offset() - find plane associated with the given offset off
1961 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1962 unsigned int *_buffer
, unsigned int *_plane
)
1964 struct vb2_buffer
*vb
;
1965 unsigned int buffer
, plane
;
1968 * Go over all buffers and their planes, comparing the given offset
1969 * with an offset assigned to each plane. If a match is found,
1970 * return its buffer and plane numbers.
1972 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1973 vb
= q
->bufs
[buffer
];
1975 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1976 if (vb
->planes
[plane
].m
.offset
== off
) {
1988 * vb2_core_expbuf() - Export a buffer as a file descriptor
1989 * @q: videobuf2 queue
1990 * @fd: file descriptor associated with DMABUF (set by driver) *
1991 * @type: buffer type
1992 * @index: id number of the buffer
1993 * @plane: index of the plane to be exported, 0 for single plane queues
1994 * @flags: flags for newly created file, currently only O_CLOEXEC is
1995 * supported, refer to manual of open syscall for more details
1997 * The return values from this function are intended to be directly returned
1998 * from vidioc_expbuf handler in driver.
2000 int vb2_core_expbuf(struct vb2_queue
*q
, int *fd
, unsigned int type
,
2001 unsigned int index
, unsigned int plane
, unsigned int flags
)
2003 struct vb2_buffer
*vb
= NULL
;
2004 struct vb2_plane
*vb_plane
;
2006 struct dma_buf
*dbuf
;
2008 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2009 dprintk(1, "queue is not currently set up for mmap\n");
2013 if (!q
->mem_ops
->get_dmabuf
) {
2014 dprintk(1, "queue does not support DMA buffer exporting\n");
2018 if (flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2019 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2023 if (type
!= q
->type
) {
2024 dprintk(1, "invalid buffer type\n");
2028 if (index
>= q
->num_buffers
) {
2029 dprintk(1, "buffer index out of range\n");
2033 vb
= q
->bufs
[index
];
2035 if (plane
>= vb
->num_planes
) {
2036 dprintk(1, "buffer plane out of range\n");
2040 if (vb2_fileio_is_active(q
)) {
2041 dprintk(1, "expbuf: file io in progress\n");
2045 vb_plane
= &vb
->planes
[plane
];
2047 dbuf
= call_ptr_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
,
2049 if (IS_ERR_OR_NULL(dbuf
)) {
2050 dprintk(1, "failed to export buffer %d, plane %d\n",
2055 ret
= dma_buf_fd(dbuf
, flags
& ~O_ACCMODE
);
2057 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2063 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2069 EXPORT_SYMBOL_GPL(vb2_core_expbuf
);
2072 * vb2_mmap() - map video buffers into application address space
2073 * @q: videobuf2 queue
2074 * @vma: vma passed to the mmap file operation handler in the driver
2076 * Should be called from mmap file operation handler of a driver.
2077 * This function maps one plane of one of the available video buffers to
2078 * userspace. To map whole video memory allocated on reqbufs, this function
2079 * has to be called once per each plane per each buffer previously allocated.
2081 * When the userspace application calls mmap, it passes to it an offset returned
2082 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2083 * a "cookie", which is then used to identify the plane to be mapped.
2084 * This function finds a plane with a matching offset and a mapping is performed
2085 * by the means of a provided memory operation.
2087 * The return values from this function are intended to be directly returned
2088 * from the mmap handler in driver.
2090 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2092 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2093 struct vb2_buffer
*vb
;
2094 unsigned int buffer
= 0, plane
= 0;
2096 unsigned long length
;
2098 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2099 dprintk(1, "queue is not currently set up for mmap\n");
2104 * Check memory area access mode.
2106 if (!(vma
->vm_flags
& VM_SHARED
)) {
2107 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2111 if (!(vma
->vm_flags
& VM_WRITE
)) {
2112 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2116 if (!(vma
->vm_flags
& VM_READ
)) {
2117 dprintk(1, "invalid vma flags, VM_READ needed\n");
2121 if (vb2_fileio_is_active(q
)) {
2122 dprintk(1, "mmap: file io in progress\n");
2127 * Find the plane corresponding to the offset passed by userspace.
2129 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2133 vb
= q
->bufs
[buffer
];
2136 * MMAP requires page_aligned buffers.
2137 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2138 * so, we need to do the same here.
2140 length
= PAGE_ALIGN(vb
->planes
[plane
].length
);
2141 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2143 "MMAP invalid, as it would overflow buffer length\n");
2147 mutex_lock(&q
->mmap_lock
);
2148 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2149 mutex_unlock(&q
->mmap_lock
);
2153 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2156 EXPORT_SYMBOL_GPL(vb2_mmap
);
2159 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2162 unsigned long pgoff
,
2163 unsigned long flags
)
2165 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2166 struct vb2_buffer
*vb
;
2167 unsigned int buffer
, plane
;
2171 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2172 dprintk(1, "queue is not currently set up for mmap\n");
2177 * Find the plane corresponding to the offset passed by userspace.
2179 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2183 vb
= q
->bufs
[buffer
];
2185 vaddr
= vb2_plane_vaddr(vb
, plane
);
2186 return vaddr
? (unsigned long)vaddr
: -EINVAL
;
2188 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2192 * vb2_core_queue_init() - initialize a videobuf2 queue
2193 * @q: videobuf2 queue; this structure should be allocated in driver
2195 * The vb2_queue structure should be allocated by the driver. The driver is
2196 * responsible of clearing it's content and setting initial values for some
2197 * required entries before calling this function.
2198 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2199 * to the struct vb2_queue description in include/media/videobuf2-core.h
2200 * for more information.
2202 int vb2_core_queue_init(struct vb2_queue
*q
)
2209 WARN_ON(!q
->mem_ops
) ||
2210 WARN_ON(!q
->type
) ||
2211 WARN_ON(!q
->io_modes
) ||
2212 WARN_ON(!q
->ops
->queue_setup
) ||
2213 WARN_ON(!q
->ops
->buf_queue
))
2216 INIT_LIST_HEAD(&q
->queued_list
);
2217 INIT_LIST_HEAD(&q
->done_list
);
2218 spin_lock_init(&q
->done_lock
);
2219 mutex_init(&q
->mmap_lock
);
2220 init_waitqueue_head(&q
->done_wq
);
2222 if (q
->buf_struct_size
== 0)
2223 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2227 EXPORT_SYMBOL_GPL(vb2_core_queue_init
);
2229 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2230 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2232 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2233 * @q: videobuf2 queue
2235 * This function stops streaming and performs necessary clean ups, including
2236 * freeing video buffer memory. The driver is responsible for freeing
2237 * the vb2_queue structure itself.
2239 void vb2_core_queue_release(struct vb2_queue
*q
)
2241 __vb2_cleanup_fileio(q
);
2242 __vb2_queue_cancel(q
);
2243 mutex_lock(&q
->mmap_lock
);
2244 __vb2_queue_free(q
, q
->num_buffers
);
2245 mutex_unlock(&q
->mmap_lock
);
2247 EXPORT_SYMBOL_GPL(vb2_core_queue_release
);
2250 * vb2_core_poll() - implements poll userspace operation
2251 * @q: videobuf2 queue
2252 * @file: file argument passed to the poll file operation handler
2253 * @wait: wait argument passed to the poll file operation handler
2255 * This function implements poll file operation handler for a driver.
2256 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2257 * be informed that the file descriptor of a video device is available for
2259 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2260 * will be reported as available for writing.
2262 * The return values from this function are intended to be directly returned
2263 * from poll handler in driver.
2265 unsigned int vb2_core_poll(struct vb2_queue
*q
, struct file
*file
,
2268 unsigned long req_events
= poll_requested_events(wait
);
2269 struct vb2_buffer
*vb
= NULL
;
2270 unsigned long flags
;
2272 if (!q
->is_output
&& !(req_events
& (POLLIN
| POLLRDNORM
)))
2274 if (q
->is_output
&& !(req_events
& (POLLOUT
| POLLWRNORM
)))
2278 * Start file I/O emulator only if streaming API has not been used yet.
2280 if (q
->num_buffers
== 0 && !vb2_fileio_is_active(q
)) {
2281 if (!q
->is_output
&& (q
->io_modes
& VB2_READ
) &&
2282 (req_events
& (POLLIN
| POLLRDNORM
))) {
2283 if (__vb2_init_fileio(q
, 1))
2286 if (q
->is_output
&& (q
->io_modes
& VB2_WRITE
) &&
2287 (req_events
& (POLLOUT
| POLLWRNORM
))) {
2288 if (__vb2_init_fileio(q
, 0))
2291 * Write to OUTPUT queue can be done immediately.
2293 return POLLOUT
| POLLWRNORM
;
2298 * There is nothing to wait for if the queue isn't streaming, or if the
2299 * error flag is set.
2301 if (!vb2_is_streaming(q
) || q
->error
)
2305 * If this quirk is set and QBUF hasn't been called yet then
2306 * return POLLERR as well. This only affects capture queues, output
2307 * queues will always initialize waiting_for_buffers to false.
2308 * This quirk is set by V4L2 for backwards compatibility reasons.
2310 if (q
->quirk_poll_must_check_waiting_for_buffers
&&
2311 q
->waiting_for_buffers
&& (req_events
& (POLLIN
| POLLRDNORM
)))
2315 * For output streams you can call write() as long as there are fewer
2316 * buffers queued than there are buffers available.
2318 if (q
->is_output
&& q
->fileio
&& q
->queued_count
< q
->num_buffers
)
2319 return POLLOUT
| POLLWRNORM
;
2321 if (list_empty(&q
->done_list
)) {
2323 * If the last buffer was dequeued from a capture queue,
2324 * return immediately. DQBUF will return -EPIPE.
2326 if (q
->last_buffer_dequeued
)
2327 return POLLIN
| POLLRDNORM
;
2329 poll_wait(file
, &q
->done_wq
, wait
);
2333 * Take first buffer available for dequeuing.
2335 spin_lock_irqsave(&q
->done_lock
, flags
);
2336 if (!list_empty(&q
->done_list
))
2337 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2339 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2341 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2342 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2343 return (q
->is_output
) ?
2344 POLLOUT
| POLLWRNORM
:
2345 POLLIN
| POLLRDNORM
;
2349 EXPORT_SYMBOL_GPL(vb2_core_poll
);
2352 * struct vb2_fileio_buf - buffer context used by file io emulator
2354 * vb2 provides a compatibility layer and emulator of file io (read and
2355 * write) calls on top of streaming API. This structure is used for
2356 * tracking context related to the buffers.
2358 struct vb2_fileio_buf
{
2362 unsigned int queued
:1;
2366 * struct vb2_fileio_data - queue context used by file io emulator
2368 * @cur_index: the index of the buffer currently being read from or
2369 * written to. If equal to q->num_buffers then a new buffer
2371 * @initial_index: in the read() case all buffers are queued up immediately
2372 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2373 * buffers. However, in the write() case no buffers are initially
2374 * queued, instead whenever a buffer is full it is queued up by
2375 * __vb2_perform_fileio(). Only once all available buffers have
2376 * been queued up will __vb2_perform_fileio() start to dequeue
2377 * buffers. This means that initially __vb2_perform_fileio()
2378 * needs to know what buffer index to use when it is queuing up
2379 * the buffers for the first time. That initial index is stored
2380 * in this field. Once it is equal to q->num_buffers all
2381 * available buffers have been queued and __vb2_perform_fileio()
2382 * should start the normal dequeue/queue cycle.
2384 * vb2 provides a compatibility layer and emulator of file io (read and
2385 * write) calls on top of streaming API. For proper operation it required
2386 * this structure to save the driver state between each call of the read
2387 * or write function.
2389 struct vb2_fileio_data
{
2392 unsigned int memory
;
2393 struct vb2_fileio_buf bufs
[VB2_MAX_FRAME
];
2394 unsigned int cur_index
;
2395 unsigned int initial_index
;
2396 unsigned int q_count
;
2397 unsigned int dq_count
;
2398 unsigned read_once
:1;
2399 unsigned write_immediately
:1;
2403 * __vb2_init_fileio() - initialize file io emulator
2404 * @q: videobuf2 queue
2405 * @read: mode selector (1 means read, 0 means write)
2407 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2409 struct vb2_fileio_data
*fileio
;
2411 unsigned int count
= 0;
2416 if (WARN_ON((read
&& !(q
->io_modes
& VB2_READ
)) ||
2417 (!read
&& !(q
->io_modes
& VB2_WRITE
))))
2421 * Check if device supports mapping buffers to kernel virtual space.
2423 if (!q
->mem_ops
->vaddr
)
2427 * Check if streaming api has not been already activated.
2429 if (q
->streaming
|| q
->num_buffers
> 0)
2433 * Start with count 1, driver can increase it in queue_setup()
2437 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2438 (read
) ? "read" : "write", count
, q
->fileio_read_once
,
2439 q
->fileio_write_immediately
);
2441 fileio
= kzalloc(sizeof(*fileio
), GFP_KERNEL
);
2445 fileio
->read_once
= q
->fileio_read_once
;
2446 fileio
->write_immediately
= q
->fileio_write_immediately
;
2449 * Request buffers and use MMAP type to force driver
2450 * to allocate buffers by itself.
2452 fileio
->count
= count
;
2453 fileio
->memory
= VB2_MEMORY_MMAP
;
2454 fileio
->type
= q
->type
;
2456 ret
= vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2461 * Check if plane_count is correct
2462 * (multiplane buffers are not supported).
2464 if (q
->bufs
[0]->num_planes
!= 1) {
2470 * Get kernel address of each buffer.
2472 for (i
= 0; i
< q
->num_buffers
; i
++) {
2473 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2474 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2478 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2482 * Read mode requires pre queuing of all buffers.
2486 * Queue all buffers.
2488 for (i
= 0; i
< q
->num_buffers
; i
++) {
2489 ret
= vb2_core_qbuf(q
, i
, NULL
);
2492 fileio
->bufs
[i
].queued
= 1;
2495 * All buffers have been queued, so mark that by setting
2496 * initial_index to q->num_buffers
2498 fileio
->initial_index
= q
->num_buffers
;
2499 fileio
->cur_index
= q
->num_buffers
;
2505 ret
= vb2_core_streamon(q
, q
->type
);
2513 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2522 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2523 * @q: videobuf2 queue
2525 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2527 struct vb2_fileio_data
*fileio
= q
->fileio
;
2530 vb2_core_streamoff(q
, q
->type
);
2533 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2535 dprintk(3, "file io emulator closed\n");
2541 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2542 * @q: videobuf2 queue
2543 * @data: pointed to target userspace buffer
2544 * @count: number of bytes to read or write
2545 * @ppos: file handle position tracking pointer
2546 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2547 * @read: access mode selector (1 means read, 0 means write)
2549 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2550 loff_t
*ppos
, int nonblock
, int read
)
2552 struct vb2_fileio_data
*fileio
;
2553 struct vb2_fileio_buf
*buf
;
2554 bool is_multiplanar
= q
->is_multiplanar
;
2556 * When using write() to write data to an output video node the vb2 core
2557 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2558 * else is able to provide this information with the write() operation.
2560 bool copy_timestamp
= !read
&& q
->copy_timestamp
;
2564 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2565 read
? "read" : "write", (long)*ppos
, count
,
2566 nonblock
? "non" : "");
2572 * Initialize emulator on first call.
2574 if (!vb2_fileio_is_active(q
)) {
2575 ret
= __vb2_init_fileio(q
, read
);
2576 dprintk(3, "vb2_init_fileio result: %d\n", ret
);
2583 * Check if we need to dequeue the buffer.
2585 index
= fileio
->cur_index
;
2586 if (index
>= q
->num_buffers
) {
2587 struct vb2_buffer
*b
;
2590 * Call vb2_dqbuf to get buffer back.
2592 ret
= vb2_core_dqbuf(q
, &index
, NULL
, nonblock
);
2593 dprintk(5, "vb2_dqbuf result: %d\n", ret
);
2596 fileio
->dq_count
+= 1;
2598 fileio
->cur_index
= index
;
2599 buf
= &fileio
->bufs
[index
];
2603 * Get number of bytes filled by the driver
2607 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2608 : vb2_plane_size(q
->bufs
[index
], 0);
2609 /* Compensate for data_offset on read in the multiplanar case. */
2610 if (is_multiplanar
&& read
&&
2611 b
->planes
[0].data_offset
< buf
->size
) {
2612 buf
->pos
= b
->planes
[0].data_offset
;
2613 buf
->size
-= buf
->pos
;
2616 buf
= &fileio
->bufs
[index
];
2620 * Limit count on last few bytes of the buffer.
2622 if (buf
->pos
+ count
> buf
->size
) {
2623 count
= buf
->size
- buf
->pos
;
2624 dprintk(5, "reducing read count: %zd\n", count
);
2628 * Transfer data to userspace.
2630 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2631 count
, index
, buf
->pos
);
2633 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2635 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2637 dprintk(3, "error copying data\n");
2648 * Queue next buffer if required.
2650 if (buf
->pos
== buf
->size
|| (!read
&& fileio
->write_immediately
)) {
2651 struct vb2_buffer
*b
= q
->bufs
[index
];
2654 * Check if this is the last buffer to read.
2656 if (read
&& fileio
->read_once
&& fileio
->dq_count
== 1) {
2657 dprintk(3, "read limit reached\n");
2658 return __vb2_cleanup_fileio(q
);
2662 * Call vb2_qbuf and give buffer to the driver.
2664 b
->planes
[0].bytesused
= buf
->pos
;
2667 b
->timestamp
= ktime_get_ns();
2668 ret
= vb2_core_qbuf(q
, index
, NULL
);
2669 dprintk(5, "vb2_dbuf result: %d\n", ret
);
2674 * Buffer has been queued, update the status
2678 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2679 fileio
->q_count
+= 1;
2681 * If we are queuing up buffers for the first time, then
2682 * increase initial_index by one.
2684 if (fileio
->initial_index
< q
->num_buffers
)
2685 fileio
->initial_index
++;
2687 * The next buffer to use is either a buffer that's going to be
2688 * queued for the first time (initial_index < q->num_buffers)
2689 * or it is equal to q->num_buffers, meaning that the next
2690 * time we need to dequeue a buffer since we've now queued up
2691 * all the 'first time' buffers.
2693 fileio
->cur_index
= fileio
->initial_index
;
2697 * Return proper number of bytes processed.
2704 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2705 loff_t
*ppos
, int nonblocking
)
2707 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2709 EXPORT_SYMBOL_GPL(vb2_read
);
2711 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2712 loff_t
*ppos
, int nonblocking
)
2714 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2715 ppos
, nonblocking
, 0);
2717 EXPORT_SYMBOL_GPL(vb2_write
);
2719 struct vb2_threadio_data
{
2720 struct task_struct
*thread
;
2726 static int vb2_thread(void *data
)
2728 struct vb2_queue
*q
= data
;
2729 struct vb2_threadio_data
*threadio
= q
->threadio
;
2730 bool copy_timestamp
= false;
2731 unsigned prequeue
= 0;
2736 prequeue
= q
->num_buffers
;
2737 copy_timestamp
= q
->copy_timestamp
;
2743 struct vb2_buffer
*vb
;
2746 * Call vb2_dqbuf to get buffer back.
2749 vb
= q
->bufs
[index
++];
2752 call_void_qop(q
, wait_finish
, q
);
2753 if (!threadio
->stop
)
2754 ret
= vb2_core_dqbuf(q
, &index
, NULL
, 0);
2755 call_void_qop(q
, wait_prepare
, q
);
2756 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2758 vb
= q
->bufs
[index
];
2760 if (ret
|| threadio
->stop
)
2764 if (vb
->state
!= VB2_BUF_STATE_ERROR
)
2765 if (threadio
->fnc(vb
, threadio
->priv
))
2767 call_void_qop(q
, wait_finish
, q
);
2769 vb
->timestamp
= ktime_get_ns();;
2770 if (!threadio
->stop
)
2771 ret
= vb2_core_qbuf(q
, vb
->index
, NULL
);
2772 call_void_qop(q
, wait_prepare
, q
);
2773 if (ret
|| threadio
->stop
)
2777 /* Hmm, linux becomes *very* unhappy without this ... */
2778 while (!kthread_should_stop()) {
2779 set_current_state(TASK_INTERRUPTIBLE
);
2786 * This function should not be used for anything else but the videobuf2-dvb
2787 * support. If you think you have another good use-case for this, then please
2788 * contact the linux-media mailinglist first.
2790 int vb2_thread_start(struct vb2_queue
*q
, vb2_thread_fnc fnc
, void *priv
,
2791 const char *thread_name
)
2793 struct vb2_threadio_data
*threadio
;
2800 if (WARN_ON(q
->fileio
))
2803 threadio
= kzalloc(sizeof(*threadio
), GFP_KERNEL
);
2804 if (threadio
== NULL
)
2806 threadio
->fnc
= fnc
;
2807 threadio
->priv
= priv
;
2809 ret
= __vb2_init_fileio(q
, !q
->is_output
);
2810 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2813 q
->threadio
= threadio
;
2814 threadio
->thread
= kthread_run(vb2_thread
, q
, "vb2-%s", thread_name
);
2815 if (IS_ERR(threadio
->thread
)) {
2816 ret
= PTR_ERR(threadio
->thread
);
2817 threadio
->thread
= NULL
;
2823 __vb2_cleanup_fileio(q
);
2828 EXPORT_SYMBOL_GPL(vb2_thread_start
);
2830 int vb2_thread_stop(struct vb2_queue
*q
)
2832 struct vb2_threadio_data
*threadio
= q
->threadio
;
2835 if (threadio
== NULL
)
2837 threadio
->stop
= true;
2838 /* Wake up all pending sleeps in the thread */
2840 err
= kthread_stop(threadio
->thread
);
2841 __vb2_cleanup_fileio(q
);
2842 threadio
->thread
= NULL
;
2847 EXPORT_SYMBOL_GPL(vb2_thread_stop
);
2849 MODULE_DESCRIPTION("Media buffer core framework");
2850 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2851 MODULE_LICENSE("GPL");