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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/err.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/freezer.h>
27 #include <linux/kthread.h>
29 #include <media/videobuf2-core.h>
30 #include <media/v4l2-mc.h>
32 #include <trace/events/vb2.h>
35 module_param(debug
, int, 0644);
37 #define dprintk(level, fmt, arg...) \
40 pr_info("%s: " fmt, __func__, ## arg); \
43 #ifdef CONFIG_VIDEO_ADV_DEBUG
46 * If advanced debugging is on, then count how often each op is called
47 * successfully, which can either be per-buffer or per-queue.
49 * This makes it easy to check that the 'init' and 'cleanup'
50 * (and variations thereof) stay balanced.
53 #define log_memop(vb, op) \
54 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
55 (vb)->vb2_queue, (vb)->index, #op, \
56 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
58 #define call_memop(vb, op, args...) \
60 struct vb2_queue *_q = (vb)->vb2_queue; \
64 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
66 (vb)->cnt_mem_ ## op++; \
70 #define call_ptr_memop(vb, op, args...) \
72 struct vb2_queue *_q = (vb)->vb2_queue; \
76 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
77 if (!IS_ERR_OR_NULL(ptr)) \
78 (vb)->cnt_mem_ ## op++; \
82 #define call_void_memop(vb, op, args...) \
84 struct vb2_queue *_q = (vb)->vb2_queue; \
87 if (_q->mem_ops->op) \
88 _q->mem_ops->op(args); \
89 (vb)->cnt_mem_ ## op++; \
92 #define log_qop(q, op) \
93 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
94 (q)->ops->op ? "" : " (nop)")
96 #define call_qop(q, op, args...) \
101 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
107 #define call_void_qop(q, op, args...) \
111 (q)->ops->op(args); \
115 #define log_vb_qop(vb, op, args...) \
116 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
117 (vb)->vb2_queue, (vb)->index, #op, \
118 (vb)->vb2_queue->ops->op ? "" : " (nop)")
120 #define call_vb_qop(vb, op, args...) \
124 log_vb_qop(vb, op); \
125 err = (vb)->vb2_queue->ops->op ? \
126 (vb)->vb2_queue->ops->op(args) : 0; \
128 (vb)->cnt_ ## op++; \
132 #define call_void_vb_qop(vb, op, args...) \
134 log_vb_qop(vb, op); \
135 if ((vb)->vb2_queue->ops->op) \
136 (vb)->vb2_queue->ops->op(args); \
137 (vb)->cnt_ ## op++; \
142 #define call_memop(vb, op, args...) \
143 ((vb)->vb2_queue->mem_ops->op ? \
144 (vb)->vb2_queue->mem_ops->op(args) : 0)
146 #define call_ptr_memop(vb, op, args...) \
147 ((vb)->vb2_queue->mem_ops->op ? \
148 (vb)->vb2_queue->mem_ops->op(args) : NULL)
150 #define call_void_memop(vb, op, args...) \
152 if ((vb)->vb2_queue->mem_ops->op) \
153 (vb)->vb2_queue->mem_ops->op(args); \
156 #define call_qop(q, op, args...) \
157 ((q)->ops->op ? (q)->ops->op(args) : 0)
159 #define call_void_qop(q, op, args...) \
162 (q)->ops->op(args); \
165 #define call_vb_qop(vb, op, args...) \
166 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
168 #define call_void_vb_qop(vb, op, args...) \
170 if ((vb)->vb2_queue->ops->op) \
171 (vb)->vb2_queue->ops->op(args); \
176 #define call_bufop(q, op, args...) \
179 if (q && q->buf_ops && q->buf_ops->op) \
180 ret = q->buf_ops->op(args); \
184 #define call_void_bufop(q, op, args...) \
186 if (q && q->buf_ops && q->buf_ops->op) \
187 q->buf_ops->op(args); \
190 static void __vb2_queue_cancel(struct vb2_queue
*q
);
191 static void __enqueue_in_driver(struct vb2_buffer
*vb
);
194 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
196 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
198 struct vb2_queue
*q
= vb
->vb2_queue
;
204 * Allocate memory for all planes in this buffer
205 * NOTE: mmapped areas should be page aligned
207 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
208 /* Memops alloc requires size to be page aligned. */
209 unsigned long size
= PAGE_ALIGN(vb
->planes
[plane
].length
);
211 /* Did it wrap around? */
212 if (size
< vb
->planes
[plane
].length
)
215 mem_priv
= call_ptr_memop(vb
, alloc
,
216 q
->alloc_devs
[plane
] ? : q
->dev
,
217 q
->dma_attrs
, size
, q
->dma_dir
, q
->gfp_flags
);
218 if (IS_ERR_OR_NULL(mem_priv
)) {
220 ret
= PTR_ERR(mem_priv
);
224 /* Associate allocator private data with this plane */
225 vb
->planes
[plane
].mem_priv
= mem_priv
;
230 /* Free already allocated memory if one of the allocations failed */
231 for (; plane
> 0; --plane
) {
232 call_void_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
233 vb
->planes
[plane
- 1].mem_priv
= NULL
;
240 * __vb2_buf_mem_free() - free memory of the given buffer
242 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
246 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
247 call_void_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
248 vb
->planes
[plane
].mem_priv
= NULL
;
249 dprintk(3, "freed plane %d of buffer %d\n", plane
, vb
->index
);
254 * __vb2_buf_userptr_put() - release userspace memory associated with
257 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
261 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
262 if (vb
->planes
[plane
].mem_priv
)
263 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
264 vb
->planes
[plane
].mem_priv
= NULL
;
269 * __vb2_plane_dmabuf_put() - release memory associated with
270 * a DMABUF shared plane
272 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
278 call_void_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
280 call_void_memop(vb
, detach_dmabuf
, p
->mem_priv
);
281 dma_buf_put(p
->dbuf
);
288 * __vb2_buf_dmabuf_put() - release memory associated with
289 * a DMABUF shared buffer
291 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
295 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
296 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
300 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
303 static void __setup_offsets(struct vb2_buffer
*vb
)
305 struct vb2_queue
*q
= vb
->vb2_queue
;
307 unsigned long off
= 0;
310 struct vb2_buffer
*prev
= q
->bufs
[vb
->index
- 1];
311 struct vb2_plane
*p
= &prev
->planes
[prev
->num_planes
- 1];
313 off
= PAGE_ALIGN(p
->m
.offset
+ p
->length
);
316 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
317 vb
->planes
[plane
].m
.offset
= off
;
319 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
320 vb
->index
, plane
, off
);
322 off
+= vb
->planes
[plane
].length
;
323 off
= PAGE_ALIGN(off
);
328 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
329 * video buffer memory for all buffers/planes on the queue and initializes the
332 * Returns the number of buffers successfully allocated.
334 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum vb2_memory memory
,
335 unsigned int num_buffers
, unsigned int num_planes
,
336 const unsigned plane_sizes
[VB2_MAX_PLANES
])
338 unsigned int buffer
, plane
;
339 struct vb2_buffer
*vb
;
342 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
343 num_buffers
= min_t(unsigned int, num_buffers
,
344 VB2_MAX_FRAME
- q
->num_buffers
);
346 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
347 /* Allocate videobuf buffer structures */
348 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
350 dprintk(1, "memory alloc for buffer struct failed\n");
354 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
356 vb
->num_planes
= num_planes
;
357 vb
->index
= q
->num_buffers
+ buffer
;
360 for (plane
= 0; plane
< num_planes
; ++plane
) {
361 vb
->planes
[plane
].length
= plane_sizes
[plane
];
362 vb
->planes
[plane
].min_length
= plane_sizes
[plane
];
364 call_void_bufop(q
, init_buffer
, vb
);
366 q
->bufs
[vb
->index
] = vb
;
368 /* Allocate video buffer memory for the MMAP type */
369 if (memory
== VB2_MEMORY_MMAP
) {
370 ret
= __vb2_buf_mem_alloc(vb
);
372 dprintk(1, "failed allocating memory for buffer %d\n",
374 q
->bufs
[vb
->index
] = NULL
;
380 * Call the driver-provided buffer initialization
381 * callback, if given. An error in initialization
382 * results in queue setup failure.
384 ret
= call_vb_qop(vb
, buf_init
, vb
);
386 dprintk(1, "buffer %d %p initialization failed\n",
388 __vb2_buf_mem_free(vb
);
389 q
->bufs
[vb
->index
] = NULL
;
396 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
403 * __vb2_free_mem() - release all video buffer memory for a given queue
405 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
408 struct vb2_buffer
*vb
;
410 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
412 vb
= q
->bufs
[buffer
];
416 /* Free MMAP buffers or release USERPTR buffers */
417 if (q
->memory
== VB2_MEMORY_MMAP
)
418 __vb2_buf_mem_free(vb
);
419 else if (q
->memory
== VB2_MEMORY_DMABUF
)
420 __vb2_buf_dmabuf_put(vb
);
422 __vb2_buf_userptr_put(vb
);
427 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
428 * related information, if no buffers are left return the queue to an
429 * uninitialized state. Might be called even if the queue has already been freed.
431 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
436 * Sanity check: when preparing a buffer the queue lock is released for
437 * a short while (see __buf_prepare for the details), which would allow
438 * a race with a reqbufs which can call this function. Removing the
439 * buffers from underneath __buf_prepare is obviously a bad idea, so we
440 * check if any of the buffers is in the state PREPARING, and if so we
441 * just return -EAGAIN.
443 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
445 if (q
->bufs
[buffer
] == NULL
)
447 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
448 dprintk(1, "preparing buffers, cannot free\n");
453 /* Call driver-provided cleanup function for each buffer, if provided */
454 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
456 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
458 if (vb
&& vb
->planes
[0].mem_priv
)
459 call_void_vb_qop(vb
, buf_cleanup
, vb
);
462 /* Release video buffer memory */
463 __vb2_free_mem(q
, buffers
);
465 #ifdef CONFIG_VIDEO_ADV_DEBUG
467 * Check that all the calls were balances during the life-time of this
468 * queue. If not (or if the debug level is 1 or up), then dump the
469 * counters to the kernel log.
471 if (q
->num_buffers
) {
472 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
473 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
475 if (unbalanced
|| debug
) {
476 pr_info("counters for queue %p:%s\n", q
,
477 unbalanced
? " UNBALANCED!" : "");
478 pr_info(" setup: %u start_streaming: %u stop_streaming: %u\n",
479 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
480 q
->cnt_stop_streaming
);
481 pr_info(" wait_prepare: %u wait_finish: %u\n",
482 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
484 q
->cnt_queue_setup
= 0;
485 q
->cnt_wait_prepare
= 0;
486 q
->cnt_wait_finish
= 0;
487 q
->cnt_start_streaming
= 0;
488 q
->cnt_stop_streaming
= 0;
490 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
491 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
492 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
493 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
494 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
495 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
496 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
497 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
498 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
499 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
501 if (unbalanced
|| debug
) {
502 pr_info(" counters for queue %p, buffer %d:%s\n",
503 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
504 pr_info(" buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
505 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
506 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
507 pr_info(" buf_out_validate: %u buf_queue: %u buf_done: %u buf_request_complete: %u\n",
508 vb
->cnt_buf_out_validate
, vb
->cnt_buf_queue
,
509 vb
->cnt_buf_done
, vb
->cnt_buf_request_complete
);
510 pr_info(" alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
511 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
512 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
514 pr_info(" get_userptr: %u put_userptr: %u\n",
515 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
516 pr_info(" attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
517 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
518 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
519 pr_info(" get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
520 vb
->cnt_mem_get_dmabuf
,
521 vb
->cnt_mem_num_users
,
528 /* Free videobuf buffers */
529 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
531 kfree(q
->bufs
[buffer
]);
532 q
->bufs
[buffer
] = NULL
;
535 q
->num_buffers
-= buffers
;
536 if (!q
->num_buffers
) {
537 q
->memory
= VB2_MEMORY_UNKNOWN
;
538 INIT_LIST_HEAD(&q
->queued_list
);
543 bool vb2_buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
546 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
547 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
549 * If num_users() has not been provided, call_memop
550 * will return 0, apparently nobody cares about this
551 * case anyway. If num_users() returns more than 1,
552 * we are not the only user of the plane's memory.
554 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
559 EXPORT_SYMBOL(vb2_buffer_in_use
);
562 * __buffers_in_use() - return true if any buffers on the queue are in use and
563 * the queue cannot be freed (by the means of REQBUFS(0)) call
565 static bool __buffers_in_use(struct vb2_queue
*q
)
568 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
569 if (vb2_buffer_in_use(q
, q
->bufs
[buffer
]))
575 void vb2_core_querybuf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
577 call_void_bufop(q
, fill_user_buffer
, q
->bufs
[index
], pb
);
579 EXPORT_SYMBOL_GPL(vb2_core_querybuf
);
582 * __verify_userptr_ops() - verify that all memory operations required for
583 * USERPTR queue type have been provided
585 static int __verify_userptr_ops(struct vb2_queue
*q
)
587 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
588 !q
->mem_ops
->put_userptr
)
595 * __verify_mmap_ops() - verify that all memory operations required for
596 * MMAP queue type have been provided
598 static int __verify_mmap_ops(struct vb2_queue
*q
)
600 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
601 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
608 * __verify_dmabuf_ops() - verify that all memory operations required for
609 * DMABUF queue type have been provided
611 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
613 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
614 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
615 !q
->mem_ops
->unmap_dmabuf
)
621 int vb2_verify_memory_type(struct vb2_queue
*q
,
622 enum vb2_memory memory
, unsigned int type
)
624 if (memory
!= VB2_MEMORY_MMAP
&& memory
!= VB2_MEMORY_USERPTR
&&
625 memory
!= VB2_MEMORY_DMABUF
) {
626 dprintk(1, "unsupported memory type\n");
630 if (type
!= q
->type
) {
631 dprintk(1, "requested type is incorrect\n");
636 * Make sure all the required memory ops for given memory type
639 if (memory
== VB2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
640 dprintk(1, "MMAP for current setup unsupported\n");
644 if (memory
== VB2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
645 dprintk(1, "USERPTR for current setup unsupported\n");
649 if (memory
== VB2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
650 dprintk(1, "DMABUF for current setup unsupported\n");
655 * Place the busy tests at the end: -EBUSY can be ignored when
656 * create_bufs is called with count == 0, but count == 0 should still
657 * do the memory and type validation.
659 if (vb2_fileio_is_active(q
)) {
660 dprintk(1, "file io in progress\n");
665 EXPORT_SYMBOL(vb2_verify_memory_type
);
667 int vb2_core_reqbufs(struct vb2_queue
*q
, enum vb2_memory memory
,
670 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
671 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
676 dprintk(1, "streaming active\n");
680 if (q
->waiting_in_dqbuf
&& *count
) {
681 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
685 if (*count
== 0 || q
->num_buffers
!= 0 ||
686 (q
->memory
!= VB2_MEMORY_UNKNOWN
&& q
->memory
!= memory
)) {
688 * We already have buffers allocated, so first check if they
689 * are not in use and can be freed.
691 mutex_lock(&q
->mmap_lock
);
692 if (debug
&& q
->memory
== VB2_MEMORY_MMAP
&&
694 dprintk(1, "memory in use, orphaning buffers\n");
697 * Call queue_cancel to clean up any buffers in the
698 * QUEUED state which is possible if buffers were prepared or
699 * queued without ever calling STREAMON.
701 __vb2_queue_cancel(q
);
702 ret
= __vb2_queue_free(q
, q
->num_buffers
);
703 mutex_unlock(&q
->mmap_lock
);
708 * In case of REQBUFS(0) return immediately without calling
709 * driver's queue_setup() callback and allocating resources.
716 * Make sure the requested values and current defaults are sane.
718 WARN_ON(q
->min_buffers_needed
> VB2_MAX_FRAME
);
719 num_buffers
= max_t(unsigned int, *count
, q
->min_buffers_needed
);
720 num_buffers
= min_t(unsigned int, num_buffers
, VB2_MAX_FRAME
);
721 memset(q
->alloc_devs
, 0, sizeof(q
->alloc_devs
));
725 * Ask the driver how many buffers and planes per buffer it requires.
726 * Driver also sets the size and allocator context for each plane.
728 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
, &num_planes
,
729 plane_sizes
, q
->alloc_devs
);
733 /* Check that driver has set sane values */
734 if (WARN_ON(!num_planes
))
737 for (i
= 0; i
< num_planes
; i
++)
738 if (WARN_ON(!plane_sizes
[i
]))
741 /* Finally, allocate buffers and video memory */
743 __vb2_queue_alloc(q
, memory
, num_buffers
, num_planes
, plane_sizes
);
744 if (allocated_buffers
== 0) {
745 dprintk(1, "memory allocation failed\n");
750 * There is no point in continuing if we can't allocate the minimum
751 * number of buffers needed by this vb2_queue.
753 if (allocated_buffers
< q
->min_buffers_needed
)
757 * Check if driver can handle the allocated number of buffers.
759 if (!ret
&& allocated_buffers
< num_buffers
) {
760 num_buffers
= allocated_buffers
;
762 * num_planes is set by the previous queue_setup(), but since it
763 * signals to queue_setup() whether it is called from create_bufs()
764 * vs reqbufs() we zero it here to signal that queue_setup() is
765 * called for the reqbufs() case.
769 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
770 &num_planes
, plane_sizes
, q
->alloc_devs
);
772 if (!ret
&& allocated_buffers
< num_buffers
)
776 * Either the driver has accepted a smaller number of buffers,
777 * or .queue_setup() returned an error
781 mutex_lock(&q
->mmap_lock
);
782 q
->num_buffers
= allocated_buffers
;
786 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
787 * from q->num_buffers.
789 __vb2_queue_free(q
, allocated_buffers
);
790 mutex_unlock(&q
->mmap_lock
);
793 mutex_unlock(&q
->mmap_lock
);
796 * Return the number of successfully allocated buffers
799 *count
= allocated_buffers
;
800 q
->waiting_for_buffers
= !q
->is_output
;
804 EXPORT_SYMBOL_GPL(vb2_core_reqbufs
);
806 int vb2_core_create_bufs(struct vb2_queue
*q
, enum vb2_memory memory
,
807 unsigned int *count
, unsigned requested_planes
,
808 const unsigned requested_sizes
[])
810 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
811 unsigned plane_sizes
[VB2_MAX_PLANES
] = { };
814 if (q
->num_buffers
== VB2_MAX_FRAME
) {
815 dprintk(1, "maximum number of buffers already allocated\n");
819 if (!q
->num_buffers
) {
820 if (q
->waiting_in_dqbuf
&& *count
) {
821 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
824 memset(q
->alloc_devs
, 0, sizeof(q
->alloc_devs
));
826 q
->waiting_for_buffers
= !q
->is_output
;
827 } else if (q
->memory
!= memory
) {
828 dprintk(1, "memory model mismatch\n");
832 num_buffers
= min(*count
, VB2_MAX_FRAME
- q
->num_buffers
);
834 if (requested_planes
&& requested_sizes
) {
835 num_planes
= requested_planes
;
836 memcpy(plane_sizes
, requested_sizes
, sizeof(plane_sizes
));
840 * Ask the driver, whether the requested number of buffers, planes per
841 * buffer and their sizes are acceptable
843 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
844 &num_planes
, plane_sizes
, q
->alloc_devs
);
848 /* Finally, allocate buffers and video memory */
849 allocated_buffers
= __vb2_queue_alloc(q
, memory
, num_buffers
,
850 num_planes
, plane_sizes
);
851 if (allocated_buffers
== 0) {
852 dprintk(1, "memory allocation failed\n");
857 * Check if driver can handle the so far allocated number of buffers.
859 if (allocated_buffers
< num_buffers
) {
860 num_buffers
= allocated_buffers
;
863 * q->num_buffers contains the total number of buffers, that the
864 * queue driver has set up
866 ret
= call_qop(q
, queue_setup
, q
, &num_buffers
,
867 &num_planes
, plane_sizes
, q
->alloc_devs
);
869 if (!ret
&& allocated_buffers
< num_buffers
)
873 * Either the driver has accepted a smaller number of buffers,
874 * or .queue_setup() returned an error
878 mutex_lock(&q
->mmap_lock
);
879 q
->num_buffers
+= allocated_buffers
;
883 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
884 * from q->num_buffers.
886 __vb2_queue_free(q
, allocated_buffers
);
887 mutex_unlock(&q
->mmap_lock
);
890 mutex_unlock(&q
->mmap_lock
);
893 * Return the number of successfully allocated buffers
896 *count
= allocated_buffers
;
900 EXPORT_SYMBOL_GPL(vb2_core_create_bufs
);
902 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
904 if (plane_no
>= vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
907 return call_ptr_memop(vb
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
910 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
912 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
914 if (plane_no
>= vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
917 return call_ptr_memop(vb
, cookie
, vb
->planes
[plane_no
].mem_priv
);
919 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
921 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
923 struct vb2_queue
*q
= vb
->vb2_queue
;
927 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_ACTIVE
))
930 if (WARN_ON(state
!= VB2_BUF_STATE_DONE
&&
931 state
!= VB2_BUF_STATE_ERROR
&&
932 state
!= VB2_BUF_STATE_QUEUED
))
933 state
= VB2_BUF_STATE_ERROR
;
935 #ifdef CONFIG_VIDEO_ADV_DEBUG
937 * Although this is not a callback, it still does have to balance
938 * with the buf_queue op. So update this counter manually.
942 dprintk(4, "done processing on buffer %d, state: %d\n",
945 if (state
!= VB2_BUF_STATE_QUEUED
) {
947 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
948 call_void_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
952 spin_lock_irqsave(&q
->done_lock
, flags
);
953 if (state
== VB2_BUF_STATE_QUEUED
) {
954 vb
->state
= VB2_BUF_STATE_QUEUED
;
956 /* Add the buffer to the done buffers list */
957 list_add_tail(&vb
->done_entry
, &q
->done_list
);
960 atomic_dec(&q
->owned_by_drv_count
);
962 if (state
!= VB2_BUF_STATE_QUEUED
&& vb
->req_obj
.req
) {
963 media_request_object_unbind(&vb
->req_obj
);
964 media_request_object_put(&vb
->req_obj
);
967 spin_unlock_irqrestore(&q
->done_lock
, flags
);
969 trace_vb2_buf_done(q
, vb
);
972 case VB2_BUF_STATE_QUEUED
:
975 /* Inform any processes that may be waiting for buffers */
976 wake_up(&q
->done_wq
);
980 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
982 void vb2_discard_done(struct vb2_queue
*q
)
984 struct vb2_buffer
*vb
;
987 spin_lock_irqsave(&q
->done_lock
, flags
);
988 list_for_each_entry(vb
, &q
->done_list
, done_entry
)
989 vb
->state
= VB2_BUF_STATE_ERROR
;
990 spin_unlock_irqrestore(&q
->done_lock
, flags
);
992 EXPORT_SYMBOL_GPL(vb2_discard_done
);
995 * __prepare_mmap() - prepare an MMAP buffer
997 static int __prepare_mmap(struct vb2_buffer
*vb
)
1001 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1003 return ret
? ret
: call_vb_qop(vb
, buf_prepare
, vb
);
1007 * __prepare_userptr() - prepare a USERPTR buffer
1009 static int __prepare_userptr(struct vb2_buffer
*vb
)
1011 struct vb2_plane planes
[VB2_MAX_PLANES
];
1012 struct vb2_queue
*q
= vb
->vb2_queue
;
1016 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1018 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1019 /* Copy relevant information provided by the userspace */
1020 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1025 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1026 /* Skip the plane if already verified */
1027 if (vb
->planes
[plane
].m
.userptr
&&
1028 vb
->planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1029 && vb
->planes
[plane
].length
== planes
[plane
].length
)
1032 dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
1035 /* Check if the provided plane buffer is large enough */
1036 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1037 dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
1038 planes
[plane
].length
,
1039 vb
->planes
[plane
].min_length
,
1045 /* Release previously acquired memory if present */
1046 if (vb
->planes
[plane
].mem_priv
) {
1049 vb
->copied_timestamp
= 0;
1050 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1052 call_void_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1055 vb
->planes
[plane
].mem_priv
= NULL
;
1056 vb
->planes
[plane
].bytesused
= 0;
1057 vb
->planes
[plane
].length
= 0;
1058 vb
->planes
[plane
].m
.userptr
= 0;
1059 vb
->planes
[plane
].data_offset
= 0;
1061 /* Acquire each plane's memory */
1062 mem_priv
= call_ptr_memop(vb
, get_userptr
,
1063 q
->alloc_devs
[plane
] ? : q
->dev
,
1064 planes
[plane
].m
.userptr
,
1065 planes
[plane
].length
, q
->dma_dir
);
1066 if (IS_ERR(mem_priv
)) {
1067 dprintk(1, "failed acquiring userspace memory for plane %d\n",
1069 ret
= PTR_ERR(mem_priv
);
1072 vb
->planes
[plane
].mem_priv
= mem_priv
;
1076 * Now that everything is in order, copy relevant information
1077 * provided by userspace.
1079 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1080 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1081 vb
->planes
[plane
].length
= planes
[plane
].length
;
1082 vb
->planes
[plane
].m
.userptr
= planes
[plane
].m
.userptr
;
1083 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1088 * One or more planes changed, so we must call buf_init to do
1089 * the driver-specific initialization on the newly acquired
1090 * buffer, if provided.
1092 ret
= call_vb_qop(vb
, buf_init
, vb
);
1094 dprintk(1, "buffer initialization failed\n");
1099 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1101 dprintk(1, "buffer preparation failed\n");
1102 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1108 /* In case of errors, release planes that were already acquired */
1109 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1110 if (vb
->planes
[plane
].mem_priv
)
1111 call_void_memop(vb
, put_userptr
,
1112 vb
->planes
[plane
].mem_priv
);
1113 vb
->planes
[plane
].mem_priv
= NULL
;
1114 vb
->planes
[plane
].m
.userptr
= 0;
1115 vb
->planes
[plane
].length
= 0;
1122 * __prepare_dmabuf() - prepare a DMABUF buffer
1124 static int __prepare_dmabuf(struct vb2_buffer
*vb
)
1126 struct vb2_plane planes
[VB2_MAX_PLANES
];
1127 struct vb2_queue
*q
= vb
->vb2_queue
;
1131 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1133 memset(planes
, 0, sizeof(planes
[0]) * vb
->num_planes
);
1134 /* Copy relevant information provided by the userspace */
1135 ret
= call_bufop(vb
->vb2_queue
, fill_vb2_buffer
,
1140 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1141 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1143 if (IS_ERR_OR_NULL(dbuf
)) {
1144 dprintk(1, "invalid dmabuf fd for plane %d\n",
1150 /* use DMABUF size if length is not provided */
1151 if (planes
[plane
].length
== 0)
1152 planes
[plane
].length
= dbuf
->size
;
1154 if (planes
[plane
].length
< vb
->planes
[plane
].min_length
) {
1155 dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1156 planes
[plane
].length
, plane
,
1157 vb
->planes
[plane
].min_length
);
1163 /* Skip the plane if already verified */
1164 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1165 vb
->planes
[plane
].length
== planes
[plane
].length
) {
1170 dprintk(3, "buffer for plane %d changed\n", plane
);
1174 vb
->copied_timestamp
= 0;
1175 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1178 /* Release previously acquired memory if present */
1179 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1180 vb
->planes
[plane
].bytesused
= 0;
1181 vb
->planes
[plane
].length
= 0;
1182 vb
->planes
[plane
].m
.fd
= 0;
1183 vb
->planes
[plane
].data_offset
= 0;
1185 /* Acquire each plane's memory */
1186 mem_priv
= call_ptr_memop(vb
, attach_dmabuf
,
1187 q
->alloc_devs
[plane
] ? : q
->dev
,
1188 dbuf
, planes
[plane
].length
, q
->dma_dir
);
1189 if (IS_ERR(mem_priv
)) {
1190 dprintk(1, "failed to attach dmabuf\n");
1191 ret
= PTR_ERR(mem_priv
);
1196 vb
->planes
[plane
].dbuf
= dbuf
;
1197 vb
->planes
[plane
].mem_priv
= mem_priv
;
1201 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1202 * here instead just before the DMA, while queueing the buffer(s) so
1203 * userspace knows sooner rather than later if the dma-buf map fails.
1205 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1206 if (vb
->planes
[plane
].dbuf_mapped
)
1209 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1211 dprintk(1, "failed to map dmabuf for plane %d\n",
1215 vb
->planes
[plane
].dbuf_mapped
= 1;
1219 * Now that everything is in order, copy relevant information
1220 * provided by userspace.
1222 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1223 vb
->planes
[plane
].bytesused
= planes
[plane
].bytesused
;
1224 vb
->planes
[plane
].length
= planes
[plane
].length
;
1225 vb
->planes
[plane
].m
.fd
= planes
[plane
].m
.fd
;
1226 vb
->planes
[plane
].data_offset
= planes
[plane
].data_offset
;
1231 * Call driver-specific initialization on the newly acquired buffer,
1234 ret
= call_vb_qop(vb
, buf_init
, vb
);
1236 dprintk(1, "buffer initialization failed\n");
1241 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1243 dprintk(1, "buffer preparation failed\n");
1244 call_void_vb_qop(vb
, buf_cleanup
, vb
);
1250 /* In case of errors, release planes that were already acquired */
1251 __vb2_buf_dmabuf_put(vb
);
1257 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1259 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1261 struct vb2_queue
*q
= vb
->vb2_queue
;
1263 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1264 atomic_inc(&q
->owned_by_drv_count
);
1266 trace_vb2_buf_queue(q
, vb
);
1268 call_void_vb_qop(vb
, buf_queue
, vb
);
1271 static int __buf_prepare(struct vb2_buffer
*vb
)
1273 struct vb2_queue
*q
= vb
->vb2_queue
;
1274 enum vb2_buffer_state orig_state
= vb
->state
;
1279 dprintk(1, "fatal error occurred on queue\n");
1285 WARN_ON(vb
->synced
);
1288 ret
= call_vb_qop(vb
, buf_out_validate
, vb
);
1290 dprintk(1, "buffer validation failed\n");
1295 vb
->state
= VB2_BUF_STATE_PREPARING
;
1297 switch (q
->memory
) {
1298 case VB2_MEMORY_MMAP
:
1299 ret
= __prepare_mmap(vb
);
1301 case VB2_MEMORY_USERPTR
:
1302 ret
= __prepare_userptr(vb
);
1304 case VB2_MEMORY_DMABUF
:
1305 ret
= __prepare_dmabuf(vb
);
1308 WARN(1, "Invalid queue type\n");
1314 dprintk(1, "buffer preparation failed: %d\n", ret
);
1315 vb
->state
= orig_state
;
1320 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1321 call_void_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1325 vb
->state
= orig_state
;
1330 static int vb2_req_prepare(struct media_request_object
*obj
)
1332 struct vb2_buffer
*vb
= container_of(obj
, struct vb2_buffer
, req_obj
);
1335 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_IN_REQUEST
))
1338 mutex_lock(vb
->vb2_queue
->lock
);
1339 ret
= __buf_prepare(vb
);
1340 mutex_unlock(vb
->vb2_queue
->lock
);
1344 static void __vb2_dqbuf(struct vb2_buffer
*vb
);
1346 static void vb2_req_unprepare(struct media_request_object
*obj
)
1348 struct vb2_buffer
*vb
= container_of(obj
, struct vb2_buffer
, req_obj
);
1350 mutex_lock(vb
->vb2_queue
->lock
);
1352 vb
->state
= VB2_BUF_STATE_IN_REQUEST
;
1353 mutex_unlock(vb
->vb2_queue
->lock
);
1354 WARN_ON(!vb
->req_obj
.req
);
1357 int vb2_core_qbuf(struct vb2_queue
*q
, unsigned int index
, void *pb
,
1358 struct media_request
*req
);
1360 static void vb2_req_queue(struct media_request_object
*obj
)
1362 struct vb2_buffer
*vb
= container_of(obj
, struct vb2_buffer
, req_obj
);
1364 mutex_lock(vb
->vb2_queue
->lock
);
1365 vb2_core_qbuf(vb
->vb2_queue
, vb
->index
, NULL
, NULL
);
1366 mutex_unlock(vb
->vb2_queue
->lock
);
1369 static void vb2_req_unbind(struct media_request_object
*obj
)
1371 struct vb2_buffer
*vb
= container_of(obj
, struct vb2_buffer
, req_obj
);
1373 if (vb
->state
== VB2_BUF_STATE_IN_REQUEST
)
1374 call_void_bufop(vb
->vb2_queue
, init_buffer
, vb
);
1377 static void vb2_req_release(struct media_request_object
*obj
)
1379 struct vb2_buffer
*vb
= container_of(obj
, struct vb2_buffer
, req_obj
);
1381 if (vb
->state
== VB2_BUF_STATE_IN_REQUEST
) {
1382 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1384 media_request_put(vb
->request
);
1389 static const struct media_request_object_ops vb2_core_req_ops
= {
1390 .prepare
= vb2_req_prepare
,
1391 .unprepare
= vb2_req_unprepare
,
1392 .queue
= vb2_req_queue
,
1393 .unbind
= vb2_req_unbind
,
1394 .release
= vb2_req_release
,
1397 bool vb2_request_object_is_buffer(struct media_request_object
*obj
)
1399 return obj
->ops
== &vb2_core_req_ops
;
1401 EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer
);
1403 unsigned int vb2_request_buffer_cnt(struct media_request
*req
)
1405 struct media_request_object
*obj
;
1406 unsigned long flags
;
1407 unsigned int buffer_cnt
= 0;
1409 spin_lock_irqsave(&req
->lock
, flags
);
1410 list_for_each_entry(obj
, &req
->objects
, list
)
1411 if (vb2_request_object_is_buffer(obj
))
1413 spin_unlock_irqrestore(&req
->lock
, flags
);
1417 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt
);
1419 int vb2_core_prepare_buf(struct vb2_queue
*q
, unsigned int index
, void *pb
)
1421 struct vb2_buffer
*vb
;
1424 vb
= q
->bufs
[index
];
1425 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1426 dprintk(1, "invalid buffer state %d\n",
1431 dprintk(1, "buffer already prepared\n");
1435 ret
= __buf_prepare(vb
);
1439 /* Fill buffer information for the userspace */
1440 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1442 dprintk(2, "prepare of buffer %d succeeded\n", vb
->index
);
1446 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf
);
1449 * vb2_start_streaming() - Attempt to start streaming.
1450 * @q: videobuf2 queue
1452 * Attempt to start streaming. When this function is called there must be
1453 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1454 * number of buffers required for the DMA engine to function). If the
1455 * @start_streaming op fails it is supposed to return all the driver-owned
1456 * buffers back to vb2 in state QUEUED. Check if that happened and if
1457 * not warn and reclaim them forcefully.
1459 static int vb2_start_streaming(struct vb2_queue
*q
)
1461 struct vb2_buffer
*vb
;
1465 * If any buffers were queued before streamon,
1466 * we can now pass them to driver for processing.
1468 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1469 __enqueue_in_driver(vb
);
1471 /* Tell the driver to start streaming */
1472 q
->start_streaming_called
= 1;
1473 ret
= call_qop(q
, start_streaming
, q
,
1474 atomic_read(&q
->owned_by_drv_count
));
1478 q
->start_streaming_called
= 0;
1480 dprintk(1, "driver refused to start streaming\n");
1482 * If you see this warning, then the driver isn't cleaning up properly
1483 * after a failed start_streaming(). See the start_streaming()
1484 * documentation in videobuf2-core.h for more information how buffers
1485 * should be returned to vb2 in start_streaming().
1487 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1491 * Forcefully reclaim buffers if the driver did not
1492 * correctly return them to vb2.
1494 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1496 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1497 vb2_buffer_done(vb
, VB2_BUF_STATE_QUEUED
);
1499 /* Must be zero now */
1500 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1503 * If done_list is not empty, then start_streaming() didn't call
1504 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1507 WARN_ON(!list_empty(&q
->done_list
));
1511 int vb2_core_qbuf(struct vb2_queue
*q
, unsigned int index
, void *pb
,
1512 struct media_request
*req
)
1514 struct vb2_buffer
*vb
;
1518 dprintk(1, "fatal error occurred on queue\n");
1522 vb
= q
->bufs
[index
];
1524 if (!req
&& vb
->state
!= VB2_BUF_STATE_IN_REQUEST
&&
1525 q
->requires_requests
) {
1526 dprintk(1, "qbuf requires a request\n");
1530 if ((req
&& q
->uses_qbuf
) ||
1531 (!req
&& vb
->state
!= VB2_BUF_STATE_IN_REQUEST
&&
1532 q
->uses_requests
)) {
1533 dprintk(1, "queue in wrong mode (qbuf vs requests)\n");
1540 q
->uses_requests
= 1;
1541 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1542 dprintk(1, "buffer %d not in dequeued state\n",
1547 if (q
->is_output
&& !vb
->prepared
) {
1548 ret
= call_vb_qop(vb
, buf_out_validate
, vb
);
1550 dprintk(1, "buffer validation failed\n");
1555 media_request_object_init(&vb
->req_obj
);
1557 /* Make sure the request is in a safe state for updating. */
1558 ret
= media_request_lock_for_update(req
);
1561 ret
= media_request_object_bind(req
, &vb2_core_req_ops
,
1562 q
, true, &vb
->req_obj
);
1563 media_request_unlock_for_update(req
);
1567 vb
->state
= VB2_BUF_STATE_IN_REQUEST
;
1570 * Increment the refcount and store the request.
1571 * The request refcount is decremented again when the
1572 * buffer is dequeued. This is to prevent vb2_buffer_done()
1573 * from freeing the request from interrupt context, which can
1574 * happen if the application closed the request fd after
1575 * queueing the request.
1577 media_request_get(req
);
1580 /* Fill buffer information for the userspace */
1582 call_void_bufop(q
, copy_timestamp
, vb
, pb
);
1583 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1586 dprintk(2, "qbuf of buffer %d succeeded\n", vb
->index
);
1590 if (vb
->state
!= VB2_BUF_STATE_IN_REQUEST
)
1593 switch (vb
->state
) {
1594 case VB2_BUF_STATE_DEQUEUED
:
1595 case VB2_BUF_STATE_IN_REQUEST
:
1596 if (!vb
->prepared
) {
1597 ret
= __buf_prepare(vb
);
1602 case VB2_BUF_STATE_PREPARING
:
1603 dprintk(1, "buffer still being prepared\n");
1606 dprintk(1, "invalid buffer state %d\n", vb
->state
);
1611 * Add to the queued buffers list, a buffer will stay on it until
1612 * dequeued in dqbuf.
1614 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1616 q
->waiting_for_buffers
= false;
1617 vb
->state
= VB2_BUF_STATE_QUEUED
;
1620 call_void_bufop(q
, copy_timestamp
, vb
, pb
);
1622 trace_vb2_qbuf(q
, vb
);
1625 * If already streaming, give the buffer to driver for processing.
1626 * If not, the buffer will be given to driver on next streamon.
1628 if (q
->start_streaming_called
)
1629 __enqueue_in_driver(vb
);
1631 /* Fill buffer information for the userspace */
1633 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1636 * If streamon has been called, and we haven't yet called
1637 * start_streaming() since not enough buffers were queued, and
1638 * we now have reached the minimum number of queued buffers,
1639 * then we can finally call start_streaming().
1641 if (q
->streaming
&& !q
->start_streaming_called
&&
1642 q
->queued_count
>= q
->min_buffers_needed
) {
1643 ret
= vb2_start_streaming(q
);
1648 dprintk(2, "qbuf of buffer %d succeeded\n", vb
->index
);
1651 EXPORT_SYMBOL_GPL(vb2_core_qbuf
);
1654 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1657 * Will sleep if required for nonblocking == false.
1659 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1662 * All operations on vb_done_list are performed under done_lock
1663 * spinlock protection. However, buffers may be removed from
1664 * it and returned to userspace only while holding both driver's
1665 * lock and the done_lock spinlock. Thus we can be sure that as
1666 * long as we hold the driver's lock, the list will remain not
1667 * empty if list_empty() check succeeds.
1673 if (q
->waiting_in_dqbuf
) {
1674 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
1678 if (!q
->streaming
) {
1679 dprintk(1, "streaming off, will not wait for buffers\n");
1684 dprintk(1, "Queue in error state, will not wait for buffers\n");
1688 if (q
->last_buffer_dequeued
) {
1689 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1693 if (!list_empty(&q
->done_list
)) {
1695 * Found a buffer that we were waiting for.
1701 dprintk(3, "nonblocking and no buffers to dequeue, will not wait\n");
1705 q
->waiting_in_dqbuf
= 1;
1707 * We are streaming and blocking, wait for another buffer to
1708 * become ready or for streamoff. Driver's lock is released to
1709 * allow streamoff or qbuf to be called while waiting.
1711 call_void_qop(q
, wait_prepare
, q
);
1714 * All locks have been released, it is safe to sleep now.
1716 dprintk(3, "will sleep waiting for buffers\n");
1717 ret
= wait_event_interruptible(q
->done_wq
,
1718 !list_empty(&q
->done_list
) || !q
->streaming
||
1722 * We need to reevaluate both conditions again after reacquiring
1723 * the locks or return an error if one occurred.
1725 call_void_qop(q
, wait_finish
, q
);
1726 q
->waiting_in_dqbuf
= 0;
1728 dprintk(1, "sleep was interrupted\n");
1736 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1738 * Will sleep if required for nonblocking == false.
1740 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1741 void *pb
, int nonblocking
)
1743 unsigned long flags
;
1747 * Wait for at least one buffer to become available on the done_list.
1749 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1754 * Driver's lock has been held since we last verified that done_list
1755 * is not empty, so no need for another list_empty(done_list) check.
1757 spin_lock_irqsave(&q
->done_lock
, flags
);
1758 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1760 * Only remove the buffer from done_list if all planes can be
1761 * handled. Some cases such as V4L2 file I/O and DVB have pb
1762 * == NULL; skip the check then as there's nothing to verify.
1765 ret
= call_bufop(q
, verify_planes_array
, *vb
, pb
);
1767 list_del(&(*vb
)->done_entry
);
1768 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1773 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1775 if (!q
->streaming
) {
1776 dprintk(1, "streaming off, will not wait for buffers\n");
1780 if (q
->start_streaming_called
)
1781 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1784 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1787 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1789 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1791 struct vb2_queue
*q
= vb
->vb2_queue
;
1793 /* nothing to do if the buffer is already dequeued */
1794 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1797 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1799 call_void_bufop(q
, init_buffer
, vb
);
1802 int vb2_core_dqbuf(struct vb2_queue
*q
, unsigned int *pindex
, void *pb
,
1805 struct vb2_buffer
*vb
= NULL
;
1808 ret
= __vb2_get_done_vb(q
, &vb
, pb
, nonblocking
);
1812 switch (vb
->state
) {
1813 case VB2_BUF_STATE_DONE
:
1814 dprintk(3, "returning done buffer\n");
1816 case VB2_BUF_STATE_ERROR
:
1817 dprintk(3, "returning done buffer with errors\n");
1820 dprintk(1, "invalid buffer state\n");
1824 call_void_vb_qop(vb
, buf_finish
, vb
);
1828 *pindex
= vb
->index
;
1830 /* Fill buffer information for the userspace */
1832 call_void_bufop(q
, fill_user_buffer
, vb
, pb
);
1834 /* Remove from videobuf queue */
1835 list_del(&vb
->queued_entry
);
1838 trace_vb2_dqbuf(q
, vb
);
1840 /* go back to dequeued state */
1843 if (WARN_ON(vb
->req_obj
.req
)) {
1844 media_request_object_unbind(&vb
->req_obj
);
1845 media_request_object_put(&vb
->req_obj
);
1848 media_request_put(vb
->request
);
1851 dprintk(2, "dqbuf of buffer %d, with state %d\n",
1852 vb
->index
, vb
->state
);
1857 EXPORT_SYMBOL_GPL(vb2_core_dqbuf
);
1860 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1862 * Removes all queued buffers from driver's queue and all buffers queued by
1863 * userspace from videobuf's queue. Returns to state after reqbufs.
1865 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1870 * Tell driver to stop all transactions and release all queued
1873 if (q
->start_streaming_called
)
1874 call_void_qop(q
, stop_streaming
, q
);
1877 * If you see this warning, then the driver isn't cleaning up properly
1878 * in stop_streaming(). See the stop_streaming() documentation in
1879 * videobuf2-core.h for more information how buffers should be returned
1880 * to vb2 in stop_streaming().
1882 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1883 for (i
= 0; i
< q
->num_buffers
; ++i
)
1884 if (q
->bufs
[i
]->state
== VB2_BUF_STATE_ACTIVE
) {
1885 pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
1887 vb2_buffer_done(q
->bufs
[i
], VB2_BUF_STATE_ERROR
);
1889 /* Must be zero now */
1890 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1894 q
->start_streaming_called
= 0;
1895 q
->queued_count
= 0;
1897 q
->uses_requests
= 0;
1901 * Remove all buffers from videobuf's list...
1903 INIT_LIST_HEAD(&q
->queued_list
);
1905 * ...and done list; userspace will not receive any buffers it
1906 * has not already dequeued before initiating cancel.
1908 INIT_LIST_HEAD(&q
->done_list
);
1909 atomic_set(&q
->owned_by_drv_count
, 0);
1910 wake_up_all(&q
->done_wq
);
1913 * Reinitialize all buffers for next use.
1914 * Make sure to call buf_finish for any queued buffers. Normally
1915 * that's done in dqbuf, but that's not going to happen when we
1916 * cancel the whole queue. Note: this code belongs here, not in
1917 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1918 * call to __fill_user_buffer() after buf_finish(). That order can't
1919 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1921 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1922 struct vb2_buffer
*vb
= q
->bufs
[i
];
1923 struct media_request
*req
= vb
->req_obj
.req
;
1926 * If a request is associated with this buffer, then
1927 * call buf_request_cancel() to give the driver to complete()
1928 * related request objects. Otherwise those objects would
1932 enum media_request_state state
;
1933 unsigned long flags
;
1935 spin_lock_irqsave(&req
->lock
, flags
);
1937 spin_unlock_irqrestore(&req
->lock
, flags
);
1939 if (state
== MEDIA_REQUEST_STATE_QUEUED
)
1940 call_void_vb_qop(vb
, buf_request_complete
, vb
);
1946 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1947 call_void_memop(vb
, finish
,
1948 vb
->planes
[plane
].mem_priv
);
1953 call_void_vb_qop(vb
, buf_finish
, vb
);
1958 if (vb
->req_obj
.req
) {
1959 media_request_object_unbind(&vb
->req_obj
);
1960 media_request_object_put(&vb
->req_obj
);
1963 media_request_put(vb
->request
);
1965 vb
->copied_timestamp
= 0;
1969 int vb2_core_streamon(struct vb2_queue
*q
, unsigned int type
)
1973 if (type
!= q
->type
) {
1974 dprintk(1, "invalid stream type\n");
1979 dprintk(3, "already streaming\n");
1983 if (!q
->num_buffers
) {
1984 dprintk(1, "no buffers have been allocated\n");
1988 if (q
->num_buffers
< q
->min_buffers_needed
) {
1989 dprintk(1, "need at least %u allocated buffers\n",
1990 q
->min_buffers_needed
);
1995 * Tell driver to start streaming provided sufficient buffers
1998 if (q
->queued_count
>= q
->min_buffers_needed
) {
1999 ret
= v4l_vb2q_enable_media_source(q
);
2002 ret
= vb2_start_streaming(q
);
2009 dprintk(3, "successful\n");
2012 EXPORT_SYMBOL_GPL(vb2_core_streamon
);
2014 void vb2_queue_error(struct vb2_queue
*q
)
2018 wake_up_all(&q
->done_wq
);
2020 EXPORT_SYMBOL_GPL(vb2_queue_error
);
2022 int vb2_core_streamoff(struct vb2_queue
*q
, unsigned int type
)
2024 if (type
!= q
->type
) {
2025 dprintk(1, "invalid stream type\n");
2030 * Cancel will pause streaming and remove all buffers from the driver
2031 * and videobuf, effectively returning control over them to userspace.
2033 * Note that we do this even if q->streaming == 0: if you prepare or
2034 * queue buffers, and then call streamoff without ever having called
2035 * streamon, you would still expect those buffers to be returned to
2036 * their normal dequeued state.
2038 __vb2_queue_cancel(q
);
2039 q
->waiting_for_buffers
= !q
->is_output
;
2040 q
->last_buffer_dequeued
= false;
2042 dprintk(3, "successful\n");
2045 EXPORT_SYMBOL_GPL(vb2_core_streamoff
);
2048 * __find_plane_by_offset() - find plane associated with the given offset off
2050 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
2051 unsigned int *_buffer
, unsigned int *_plane
)
2053 struct vb2_buffer
*vb
;
2054 unsigned int buffer
, plane
;
2057 * Go over all buffers and their planes, comparing the given offset
2058 * with an offset assigned to each plane. If a match is found,
2059 * return its buffer and plane numbers.
2061 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
2062 vb
= q
->bufs
[buffer
];
2064 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
2065 if (vb
->planes
[plane
].m
.offset
== off
) {
2076 int vb2_core_expbuf(struct vb2_queue
*q
, int *fd
, unsigned int type
,
2077 unsigned int index
, unsigned int plane
, unsigned int flags
)
2079 struct vb2_buffer
*vb
= NULL
;
2080 struct vb2_plane
*vb_plane
;
2082 struct dma_buf
*dbuf
;
2084 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2085 dprintk(1, "queue is not currently set up for mmap\n");
2089 if (!q
->mem_ops
->get_dmabuf
) {
2090 dprintk(1, "queue does not support DMA buffer exporting\n");
2094 if (flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2095 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2099 if (type
!= q
->type
) {
2100 dprintk(1, "invalid buffer type\n");
2104 if (index
>= q
->num_buffers
) {
2105 dprintk(1, "buffer index out of range\n");
2109 vb
= q
->bufs
[index
];
2111 if (plane
>= vb
->num_planes
) {
2112 dprintk(1, "buffer plane out of range\n");
2116 if (vb2_fileio_is_active(q
)) {
2117 dprintk(1, "expbuf: file io in progress\n");
2121 vb_plane
= &vb
->planes
[plane
];
2123 dbuf
= call_ptr_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
,
2125 if (IS_ERR_OR_NULL(dbuf
)) {
2126 dprintk(1, "failed to export buffer %d, plane %d\n",
2131 ret
= dma_buf_fd(dbuf
, flags
& ~O_ACCMODE
);
2133 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2139 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2145 EXPORT_SYMBOL_GPL(vb2_core_expbuf
);
2147 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2149 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2150 struct vb2_buffer
*vb
;
2151 unsigned int buffer
= 0, plane
= 0;
2153 unsigned long length
;
2155 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2156 dprintk(1, "queue is not currently set up for mmap\n");
2161 * Check memory area access mode.
2163 if (!(vma
->vm_flags
& VM_SHARED
)) {
2164 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2168 if (!(vma
->vm_flags
& VM_WRITE
)) {
2169 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2173 if (!(vma
->vm_flags
& VM_READ
)) {
2174 dprintk(1, "invalid vma flags, VM_READ needed\n");
2179 mutex_lock(&q
->mmap_lock
);
2181 if (vb2_fileio_is_active(q
)) {
2182 dprintk(1, "mmap: file io in progress\n");
2188 * Find the plane corresponding to the offset passed by userspace.
2190 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2194 vb
= q
->bufs
[buffer
];
2197 * MMAP requires page_aligned buffers.
2198 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2199 * so, we need to do the same here.
2201 length
= PAGE_ALIGN(vb
->planes
[plane
].length
);
2202 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2204 "MMAP invalid, as it would overflow buffer length\n");
2210 * vm_pgoff is treated in V4L2 API as a 'cookie' to select a buffer,
2211 * not as a in-buffer offset. We always want to mmap a whole buffer
2212 * from its beginning.
2216 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2219 mutex_unlock(&q
->mmap_lock
);
2223 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2226 EXPORT_SYMBOL_GPL(vb2_mmap
);
2229 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2232 unsigned long pgoff
,
2233 unsigned long flags
)
2235 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2236 struct vb2_buffer
*vb
;
2237 unsigned int buffer
, plane
;
2241 if (q
->memory
!= VB2_MEMORY_MMAP
) {
2242 dprintk(1, "queue is not currently set up for mmap\n");
2247 * Find the plane corresponding to the offset passed by userspace.
2249 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2253 vb
= q
->bufs
[buffer
];
2255 vaddr
= vb2_plane_vaddr(vb
, plane
);
2256 return vaddr
? (unsigned long)vaddr
: -EINVAL
;
2258 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2261 int vb2_core_queue_init(struct vb2_queue
*q
)
2268 WARN_ON(!q
->mem_ops
) ||
2269 WARN_ON(!q
->type
) ||
2270 WARN_ON(!q
->io_modes
) ||
2271 WARN_ON(!q
->ops
->queue_setup
) ||
2272 WARN_ON(!q
->ops
->buf_queue
))
2275 if (WARN_ON(q
->requires_requests
&& !q
->supports_requests
))
2278 INIT_LIST_HEAD(&q
->queued_list
);
2279 INIT_LIST_HEAD(&q
->done_list
);
2280 spin_lock_init(&q
->done_lock
);
2281 mutex_init(&q
->mmap_lock
);
2282 init_waitqueue_head(&q
->done_wq
);
2284 q
->memory
= VB2_MEMORY_UNKNOWN
;
2286 if (q
->buf_struct_size
== 0)
2287 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2289 if (q
->bidirectional
)
2290 q
->dma_dir
= DMA_BIDIRECTIONAL
;
2292 q
->dma_dir
= q
->is_output
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
2296 EXPORT_SYMBOL_GPL(vb2_core_queue_init
);
2298 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2299 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2300 void vb2_core_queue_release(struct vb2_queue
*q
)
2302 __vb2_cleanup_fileio(q
);
2303 __vb2_queue_cancel(q
);
2304 mutex_lock(&q
->mmap_lock
);
2305 __vb2_queue_free(q
, q
->num_buffers
);
2306 mutex_unlock(&q
->mmap_lock
);
2308 EXPORT_SYMBOL_GPL(vb2_core_queue_release
);
2310 __poll_t
vb2_core_poll(struct vb2_queue
*q
, struct file
*file
,
2313 __poll_t req_events
= poll_requested_events(wait
);
2314 struct vb2_buffer
*vb
= NULL
;
2315 unsigned long flags
;
2317 if (!q
->is_output
&& !(req_events
& (EPOLLIN
| EPOLLRDNORM
)))
2319 if (q
->is_output
&& !(req_events
& (EPOLLOUT
| EPOLLWRNORM
)))
2322 poll_wait(file
, &q
->done_wq
, wait
);
2325 * Start file I/O emulator only if streaming API has not been used yet.
2327 if (q
->num_buffers
== 0 && !vb2_fileio_is_active(q
)) {
2328 if (!q
->is_output
&& (q
->io_modes
& VB2_READ
) &&
2329 (req_events
& (EPOLLIN
| EPOLLRDNORM
))) {
2330 if (__vb2_init_fileio(q
, 1))
2333 if (q
->is_output
&& (q
->io_modes
& VB2_WRITE
) &&
2334 (req_events
& (EPOLLOUT
| EPOLLWRNORM
))) {
2335 if (__vb2_init_fileio(q
, 0))
2338 * Write to OUTPUT queue can be done immediately.
2340 return EPOLLOUT
| EPOLLWRNORM
;
2345 * There is nothing to wait for if the queue isn't streaming, or if the
2346 * error flag is set.
2348 if (!vb2_is_streaming(q
) || q
->error
)
2352 * If this quirk is set and QBUF hasn't been called yet then
2353 * return EPOLLERR as well. This only affects capture queues, output
2354 * queues will always initialize waiting_for_buffers to false.
2355 * This quirk is set by V4L2 for backwards compatibility reasons.
2357 if (q
->quirk_poll_must_check_waiting_for_buffers
&&
2358 q
->waiting_for_buffers
&& (req_events
& (EPOLLIN
| EPOLLRDNORM
)))
2362 * For output streams you can call write() as long as there are fewer
2363 * buffers queued than there are buffers available.
2365 if (q
->is_output
&& q
->fileio
&& q
->queued_count
< q
->num_buffers
)
2366 return EPOLLOUT
| EPOLLWRNORM
;
2368 if (list_empty(&q
->done_list
)) {
2370 * If the last buffer was dequeued from a capture queue,
2371 * return immediately. DQBUF will return -EPIPE.
2373 if (q
->last_buffer_dequeued
)
2374 return EPOLLIN
| EPOLLRDNORM
;
2378 * Take first buffer available for dequeuing.
2380 spin_lock_irqsave(&q
->done_lock
, flags
);
2381 if (!list_empty(&q
->done_list
))
2382 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2384 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2386 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2387 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2388 return (q
->is_output
) ?
2389 EPOLLOUT
| EPOLLWRNORM
:
2390 EPOLLIN
| EPOLLRDNORM
;
2394 EXPORT_SYMBOL_GPL(vb2_core_poll
);
2397 * struct vb2_fileio_buf - buffer context used by file io emulator
2399 * vb2 provides a compatibility layer and emulator of file io (read and
2400 * write) calls on top of streaming API. This structure is used for
2401 * tracking context related to the buffers.
2403 struct vb2_fileio_buf
{
2407 unsigned int queued
:1;
2411 * struct vb2_fileio_data - queue context used by file io emulator
2413 * @cur_index: the index of the buffer currently being read from or
2414 * written to. If equal to q->num_buffers then a new buffer
2416 * @initial_index: in the read() case all buffers are queued up immediately
2417 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2418 * buffers. However, in the write() case no buffers are initially
2419 * queued, instead whenever a buffer is full it is queued up by
2420 * __vb2_perform_fileio(). Only once all available buffers have
2421 * been queued up will __vb2_perform_fileio() start to dequeue
2422 * buffers. This means that initially __vb2_perform_fileio()
2423 * needs to know what buffer index to use when it is queuing up
2424 * the buffers for the first time. That initial index is stored
2425 * in this field. Once it is equal to q->num_buffers all
2426 * available buffers have been queued and __vb2_perform_fileio()
2427 * should start the normal dequeue/queue cycle.
2429 * vb2 provides a compatibility layer and emulator of file io (read and
2430 * write) calls on top of streaming API. For proper operation it required
2431 * this structure to save the driver state between each call of the read
2432 * or write function.
2434 struct vb2_fileio_data
{
2437 unsigned int memory
;
2438 struct vb2_fileio_buf bufs
[VB2_MAX_FRAME
];
2439 unsigned int cur_index
;
2440 unsigned int initial_index
;
2441 unsigned int q_count
;
2442 unsigned int dq_count
;
2443 unsigned read_once
:1;
2444 unsigned write_immediately
:1;
2448 * __vb2_init_fileio() - initialize file io emulator
2449 * @q: videobuf2 queue
2450 * @read: mode selector (1 means read, 0 means write)
2452 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2454 struct vb2_fileio_data
*fileio
;
2456 unsigned int count
= 0;
2461 if (WARN_ON((read
&& !(q
->io_modes
& VB2_READ
)) ||
2462 (!read
&& !(q
->io_modes
& VB2_WRITE
))))
2466 * Check if device supports mapping buffers to kernel virtual space.
2468 if (!q
->mem_ops
->vaddr
)
2472 * Check if streaming api has not been already activated.
2474 if (q
->streaming
|| q
->num_buffers
> 0)
2478 * Start with count 1, driver can increase it in queue_setup()
2482 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2483 (read
) ? "read" : "write", count
, q
->fileio_read_once
,
2484 q
->fileio_write_immediately
);
2486 fileio
= kzalloc(sizeof(*fileio
), GFP_KERNEL
);
2490 fileio
->read_once
= q
->fileio_read_once
;
2491 fileio
->write_immediately
= q
->fileio_write_immediately
;
2494 * Request buffers and use MMAP type to force driver
2495 * to allocate buffers by itself.
2497 fileio
->count
= count
;
2498 fileio
->memory
= VB2_MEMORY_MMAP
;
2499 fileio
->type
= q
->type
;
2501 ret
= vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2506 * Check if plane_count is correct
2507 * (multiplane buffers are not supported).
2509 if (q
->bufs
[0]->num_planes
!= 1) {
2515 * Get kernel address of each buffer.
2517 for (i
= 0; i
< q
->num_buffers
; i
++) {
2518 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2519 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2523 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2527 * Read mode requires pre queuing of all buffers.
2531 * Queue all buffers.
2533 for (i
= 0; i
< q
->num_buffers
; i
++) {
2534 ret
= vb2_core_qbuf(q
, i
, NULL
, NULL
);
2537 fileio
->bufs
[i
].queued
= 1;
2540 * All buffers have been queued, so mark that by setting
2541 * initial_index to q->num_buffers
2543 fileio
->initial_index
= q
->num_buffers
;
2544 fileio
->cur_index
= q
->num_buffers
;
2550 ret
= vb2_core_streamon(q
, q
->type
);
2558 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2567 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2568 * @q: videobuf2 queue
2570 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2572 struct vb2_fileio_data
*fileio
= q
->fileio
;
2575 vb2_core_streamoff(q
, q
->type
);
2578 vb2_core_reqbufs(q
, fileio
->memory
, &fileio
->count
);
2580 dprintk(3, "file io emulator closed\n");
2586 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2587 * @q: videobuf2 queue
2588 * @data: pointed to target userspace buffer
2589 * @count: number of bytes to read or write
2590 * @ppos: file handle position tracking pointer
2591 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2592 * @read: access mode selector (1 means read, 0 means write)
2594 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2595 loff_t
*ppos
, int nonblock
, int read
)
2597 struct vb2_fileio_data
*fileio
;
2598 struct vb2_fileio_buf
*buf
;
2599 bool is_multiplanar
= q
->is_multiplanar
;
2601 * When using write() to write data to an output video node the vb2 core
2602 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
2603 * else is able to provide this information with the write() operation.
2605 bool copy_timestamp
= !read
&& q
->copy_timestamp
;
2609 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2610 read
? "read" : "write", (long)*ppos
, count
,
2611 nonblock
? "non" : "");
2616 if (q
->waiting_in_dqbuf
) {
2617 dprintk(3, "another dup()ped fd is %s\n",
2618 read
? "reading" : "writing");
2623 * Initialize emulator on first call.
2625 if (!vb2_fileio_is_active(q
)) {
2626 ret
= __vb2_init_fileio(q
, read
);
2627 dprintk(3, "vb2_init_fileio result: %d\n", ret
);
2634 * Check if we need to dequeue the buffer.
2636 index
= fileio
->cur_index
;
2637 if (index
>= q
->num_buffers
) {
2638 struct vb2_buffer
*b
;
2641 * Call vb2_dqbuf to get buffer back.
2643 ret
= vb2_core_dqbuf(q
, &index
, NULL
, nonblock
);
2644 dprintk(5, "vb2_dqbuf result: %d\n", ret
);
2647 fileio
->dq_count
+= 1;
2649 fileio
->cur_index
= index
;
2650 buf
= &fileio
->bufs
[index
];
2654 * Get number of bytes filled by the driver
2658 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2659 : vb2_plane_size(q
->bufs
[index
], 0);
2660 /* Compensate for data_offset on read in the multiplanar case. */
2661 if (is_multiplanar
&& read
&&
2662 b
->planes
[0].data_offset
< buf
->size
) {
2663 buf
->pos
= b
->planes
[0].data_offset
;
2664 buf
->size
-= buf
->pos
;
2667 buf
= &fileio
->bufs
[index
];
2671 * Limit count on last few bytes of the buffer.
2673 if (buf
->pos
+ count
> buf
->size
) {
2674 count
= buf
->size
- buf
->pos
;
2675 dprintk(5, "reducing read count: %zd\n", count
);
2679 * Transfer data to userspace.
2681 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2682 count
, index
, buf
->pos
);
2684 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2686 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2688 dprintk(3, "error copying data\n");
2699 * Queue next buffer if required.
2701 if (buf
->pos
== buf
->size
|| (!read
&& fileio
->write_immediately
)) {
2702 struct vb2_buffer
*b
= q
->bufs
[index
];
2705 * Check if this is the last buffer to read.
2707 if (read
&& fileio
->read_once
&& fileio
->dq_count
== 1) {
2708 dprintk(3, "read limit reached\n");
2709 return __vb2_cleanup_fileio(q
);
2713 * Call vb2_qbuf and give buffer to the driver.
2715 b
->planes
[0].bytesused
= buf
->pos
;
2718 b
->timestamp
= ktime_get_ns();
2719 ret
= vb2_core_qbuf(q
, index
, NULL
, NULL
);
2720 dprintk(5, "vb2_dbuf result: %d\n", ret
);
2725 * Buffer has been queued, update the status
2729 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2730 fileio
->q_count
+= 1;
2732 * If we are queuing up buffers for the first time, then
2733 * increase initial_index by one.
2735 if (fileio
->initial_index
< q
->num_buffers
)
2736 fileio
->initial_index
++;
2738 * The next buffer to use is either a buffer that's going to be
2739 * queued for the first time (initial_index < q->num_buffers)
2740 * or it is equal to q->num_buffers, meaning that the next
2741 * time we need to dequeue a buffer since we've now queued up
2742 * all the 'first time' buffers.
2744 fileio
->cur_index
= fileio
->initial_index
;
2748 * Return proper number of bytes processed.
2755 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2756 loff_t
*ppos
, int nonblocking
)
2758 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2760 EXPORT_SYMBOL_GPL(vb2_read
);
2762 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2763 loff_t
*ppos
, int nonblocking
)
2765 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2766 ppos
, nonblocking
, 0);
2768 EXPORT_SYMBOL_GPL(vb2_write
);
2770 struct vb2_threadio_data
{
2771 struct task_struct
*thread
;
2777 static int vb2_thread(void *data
)
2779 struct vb2_queue
*q
= data
;
2780 struct vb2_threadio_data
*threadio
= q
->threadio
;
2781 bool copy_timestamp
= false;
2782 unsigned prequeue
= 0;
2787 prequeue
= q
->num_buffers
;
2788 copy_timestamp
= q
->copy_timestamp
;
2794 struct vb2_buffer
*vb
;
2797 * Call vb2_dqbuf to get buffer back.
2800 vb
= q
->bufs
[index
++];
2803 call_void_qop(q
, wait_finish
, q
);
2804 if (!threadio
->stop
)
2805 ret
= vb2_core_dqbuf(q
, &index
, NULL
, 0);
2806 call_void_qop(q
, wait_prepare
, q
);
2807 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2809 vb
= q
->bufs
[index
];
2811 if (ret
|| threadio
->stop
)
2815 if (vb
->state
!= VB2_BUF_STATE_ERROR
)
2816 if (threadio
->fnc(vb
, threadio
->priv
))
2818 call_void_qop(q
, wait_finish
, q
);
2820 vb
->timestamp
= ktime_get_ns();
2821 if (!threadio
->stop
)
2822 ret
= vb2_core_qbuf(q
, vb
->index
, NULL
, NULL
);
2823 call_void_qop(q
, wait_prepare
, q
);
2824 if (ret
|| threadio
->stop
)
2828 /* Hmm, linux becomes *very* unhappy without this ... */
2829 while (!kthread_should_stop()) {
2830 set_current_state(TASK_INTERRUPTIBLE
);
2837 * This function should not be used for anything else but the videobuf2-dvb
2838 * support. If you think you have another good use-case for this, then please
2839 * contact the linux-media mailinglist first.
2841 int vb2_thread_start(struct vb2_queue
*q
, vb2_thread_fnc fnc
, void *priv
,
2842 const char *thread_name
)
2844 struct vb2_threadio_data
*threadio
;
2851 if (WARN_ON(q
->fileio
))
2854 threadio
= kzalloc(sizeof(*threadio
), GFP_KERNEL
);
2855 if (threadio
== NULL
)
2857 threadio
->fnc
= fnc
;
2858 threadio
->priv
= priv
;
2860 ret
= __vb2_init_fileio(q
, !q
->is_output
);
2861 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2864 q
->threadio
= threadio
;
2865 threadio
->thread
= kthread_run(vb2_thread
, q
, "vb2-%s", thread_name
);
2866 if (IS_ERR(threadio
->thread
)) {
2867 ret
= PTR_ERR(threadio
->thread
);
2868 threadio
->thread
= NULL
;
2874 __vb2_cleanup_fileio(q
);
2879 EXPORT_SYMBOL_GPL(vb2_thread_start
);
2881 int vb2_thread_stop(struct vb2_queue
*q
)
2883 struct vb2_threadio_data
*threadio
= q
->threadio
;
2886 if (threadio
== NULL
)
2888 threadio
->stop
= true;
2889 /* Wake up all pending sleeps in the thread */
2891 err
= kthread_stop(threadio
->thread
);
2892 __vb2_cleanup_fileio(q
);
2893 threadio
->thread
= NULL
;
2898 EXPORT_SYMBOL_GPL(vb2_thread_stop
);
2900 MODULE_DESCRIPTION("Media buffer core framework");
2901 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2902 MODULE_LICENSE("GPL");