2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/videobuf2-core.h>
25 module_param(debug
, int, 0644);
27 #define dprintk(level, fmt, arg...) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
33 #define call_memop(q, plane, op, args...) \
34 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
37 #define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
40 #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
41 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
42 V4L2_BUF_FLAG_PREPARED)
45 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
47 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
49 struct vb2_queue
*q
= vb
->vb2_queue
;
53 /* Allocate memory for all planes in this buffer */
54 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
55 mem_priv
= call_memop(q
, plane
, alloc
, q
->alloc_ctx
[plane
],
56 q
->plane_sizes
[plane
]);
57 if (IS_ERR_OR_NULL(mem_priv
))
60 /* Associate allocator private data with this plane */
61 vb
->planes
[plane
].mem_priv
= mem_priv
;
62 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
67 /* Free already allocated memory if one of the allocations failed */
68 for (; plane
> 0; --plane
)
69 call_memop(q
, plane
, put
, vb
->planes
[plane
- 1].mem_priv
);
75 * __vb2_buf_mem_free() - free memory of the given buffer
77 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
79 struct vb2_queue
*q
= vb
->vb2_queue
;
82 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
83 call_memop(q
, plane
, put
, vb
->planes
[plane
].mem_priv
);
84 vb
->planes
[plane
].mem_priv
= NULL
;
85 dprintk(3, "Freed plane %d of buffer %d\n",
86 plane
, vb
->v4l2_buf
.index
);
91 * __vb2_buf_userptr_put() - release userspace memory associated with
94 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
96 struct vb2_queue
*q
= vb
->vb2_queue
;
99 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
100 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
103 call_memop(q
, plane
, put_userptr
, mem_priv
);
104 vb
->planes
[plane
].mem_priv
= NULL
;
110 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
111 * every buffer on the queue
113 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
115 unsigned int buffer
, plane
;
116 struct vb2_buffer
*vb
;
119 if (q
->num_buffers
) {
120 struct v4l2_plane
*p
;
121 vb
= q
->bufs
[q
->num_buffers
- 1];
122 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
123 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
128 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
129 vb
= q
->bufs
[buffer
];
133 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
134 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
135 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
137 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
140 off
+= vb
->v4l2_planes
[plane
].length
;
141 off
= PAGE_ALIGN(off
);
147 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
148 * video buffer memory for all buffers/planes on the queue and initializes the
151 * Returns the number of buffers successfully allocated.
153 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
154 unsigned int num_buffers
, unsigned int num_planes
)
157 struct vb2_buffer
*vb
;
160 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
161 /* Allocate videobuf buffer structures */
162 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
164 dprintk(1, "Memory alloc for buffer struct failed\n");
168 /* Length stores number of planes for multiplanar buffers */
169 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
170 vb
->v4l2_buf
.length
= num_planes
;
172 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
174 vb
->num_planes
= num_planes
;
175 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
176 vb
->v4l2_buf
.type
= q
->type
;
177 vb
->v4l2_buf
.memory
= memory
;
179 /* Allocate video buffer memory for the MMAP type */
180 if (memory
== V4L2_MEMORY_MMAP
) {
181 ret
= __vb2_buf_mem_alloc(vb
);
183 dprintk(1, "Failed allocating memory for "
184 "buffer %d\n", buffer
);
189 * Call the driver-provided buffer initialization
190 * callback, if given. An error in initialization
191 * results in queue setup failure.
193 ret
= call_qop(q
, buf_init
, vb
);
195 dprintk(1, "Buffer %d %p initialization"
196 " failed\n", buffer
, vb
);
197 __vb2_buf_mem_free(vb
);
203 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
206 __setup_offsets(q
, buffer
);
208 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
215 * __vb2_free_mem() - release all video buffer memory for a given queue
217 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
220 struct vb2_buffer
*vb
;
222 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
224 vb
= q
->bufs
[buffer
];
228 /* Free MMAP buffers or release USERPTR buffers */
229 if (q
->memory
== V4L2_MEMORY_MMAP
)
230 __vb2_buf_mem_free(vb
);
232 __vb2_buf_userptr_put(vb
);
237 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
238 * related information, if no buffers are left return the queue to an
239 * uninitialized state. Might be called even if the queue has already been freed.
241 static void __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
245 /* Call driver-provided cleanup function for each buffer, if provided */
246 if (q
->ops
->buf_cleanup
) {
247 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
249 if (NULL
== q
->bufs
[buffer
])
251 q
->ops
->buf_cleanup(q
->bufs
[buffer
]);
255 /* Release video buffer memory */
256 __vb2_free_mem(q
, buffers
);
258 /* Free videobuf buffers */
259 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
261 kfree(q
->bufs
[buffer
]);
262 q
->bufs
[buffer
] = NULL
;
265 q
->num_buffers
-= buffers
;
271 * __verify_planes_array() - verify that the planes array passed in struct
272 * v4l2_buffer from userspace can be safely used
274 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
276 /* Is memory for copying plane information present? */
277 if (NULL
== b
->m
.planes
) {
278 dprintk(1, "Multi-planar buffer passed but "
279 "planes array not provided\n");
283 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
284 dprintk(1, "Incorrect planes array length, "
285 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
293 * __buffer_in_use() - return true if the buffer is in use and
294 * the queue cannot be freed (by the means of REQBUFS(0)) call
296 static bool __buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
299 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
300 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
302 * If num_users() has not been provided, call_memop
303 * will return 0, apparently nobody cares about this
304 * case anyway. If num_users() returns more than 1,
305 * we are not the only user of the plane's memory.
307 if (mem_priv
&& call_memop(q
, plane
, num_users
, mem_priv
) > 1)
314 * __buffers_in_use() - return true if any buffers on the queue are in use and
315 * the queue cannot be freed (by the means of REQBUFS(0)) call
317 static bool __buffers_in_use(struct vb2_queue
*q
)
320 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
321 if (__buffer_in_use(q
, q
->bufs
[buffer
]))
328 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
329 * returned to userspace
331 static int __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
333 struct vb2_queue
*q
= vb
->vb2_queue
;
336 /* Copy back data such as timestamp, flags, input, etc. */
337 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
338 b
->input
= vb
->v4l2_buf
.input
;
339 b
->reserved
= vb
->v4l2_buf
.reserved
;
341 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
342 ret
= __verify_planes_array(vb
, b
);
347 * Fill in plane-related data if userspace provided an array
348 * for it. The memory and size is verified above.
350 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
351 b
->length
* sizeof(struct v4l2_plane
));
354 * We use length and offset in v4l2_planes array even for
355 * single-planar buffers, but userspace does not.
357 b
->length
= vb
->v4l2_planes
[0].length
;
358 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
359 if (q
->memory
== V4L2_MEMORY_MMAP
)
360 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
361 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
362 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
366 * Clear any buffer state related flags.
368 b
->flags
&= ~V4L2_BUFFER_STATE_FLAGS
;
371 case VB2_BUF_STATE_QUEUED
:
372 case VB2_BUF_STATE_ACTIVE
:
373 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
375 case VB2_BUF_STATE_ERROR
:
376 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
378 case VB2_BUF_STATE_DONE
:
379 b
->flags
|= V4L2_BUF_FLAG_DONE
;
381 case VB2_BUF_STATE_PREPARED
:
382 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
384 case VB2_BUF_STATE_DEQUEUED
:
389 if (__buffer_in_use(q
, vb
))
390 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
396 * vb2_querybuf() - query video buffer information
398 * @b: buffer struct passed from userspace to vidioc_querybuf handler
401 * Should be called from vidioc_querybuf ioctl handler in driver.
402 * This function will verify the passed v4l2_buffer structure and fill the
403 * relevant information for the userspace.
405 * The return values from this function are intended to be directly returned
406 * from vidioc_querybuf handler in driver.
408 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
410 struct vb2_buffer
*vb
;
412 if (b
->type
!= q
->type
) {
413 dprintk(1, "querybuf: wrong buffer type\n");
417 if (b
->index
>= q
->num_buffers
) {
418 dprintk(1, "querybuf: buffer index out of range\n");
421 vb
= q
->bufs
[b
->index
];
423 return __fill_v4l2_buffer(vb
, b
);
425 EXPORT_SYMBOL(vb2_querybuf
);
428 * __verify_userptr_ops() - verify that all memory operations required for
429 * USERPTR queue type have been provided
431 static int __verify_userptr_ops(struct vb2_queue
*q
)
433 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
434 !q
->mem_ops
->put_userptr
)
441 * __verify_mmap_ops() - verify that all memory operations required for
442 * MMAP queue type have been provided
444 static int __verify_mmap_ops(struct vb2_queue
*q
)
446 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
447 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
454 * vb2_reqbufs() - Initiate streaming
455 * @q: videobuf2 queue
456 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
458 * Should be called from vidioc_reqbufs ioctl handler of a driver.
460 * 1) verifies streaming parameters passed from the userspace,
461 * 2) sets up the queue,
462 * 3) negotiates number of buffers and planes per buffer with the driver
463 * to be used during streaming,
464 * 4) allocates internal buffer structures (struct vb2_buffer), according to
465 * the agreed parameters,
466 * 5) for MMAP memory type, allocates actual video memory, using the
467 * memory handling/allocation routines provided during queue initialization
469 * If req->count is 0, all the memory will be freed instead.
470 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
471 * and the queue is not busy, memory will be reallocated.
473 * The return values from this function are intended to be directly returned
474 * from vidioc_reqbufs handler in driver.
476 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
478 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
482 dprintk(1, "reqbufs: file io in progress\n");
486 if (req
->memory
!= V4L2_MEMORY_MMAP
487 && req
->memory
!= V4L2_MEMORY_USERPTR
) {
488 dprintk(1, "reqbufs: unsupported memory type\n");
492 if (req
->type
!= q
->type
) {
493 dprintk(1, "reqbufs: requested type is incorrect\n");
498 dprintk(1, "reqbufs: streaming active\n");
503 * Make sure all the required memory ops for given memory type
506 if (req
->memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
507 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
511 if (req
->memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
512 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
516 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
518 * We already have buffers allocated, so first check if they
519 * are not in use and can be freed.
521 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
522 dprintk(1, "reqbufs: memory in use, cannot free\n");
526 __vb2_queue_free(q
, q
->num_buffers
);
529 * In case of REQBUFS(0) return immediately without calling
530 * driver's queue_setup() callback and allocating resources.
537 * Make sure the requested values and current defaults are sane.
539 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
540 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
541 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
542 q
->memory
= req
->memory
;
545 * Ask the driver how many buffers and planes per buffer it requires.
546 * Driver also sets the size and allocator context for each plane.
548 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
549 q
->plane_sizes
, q
->alloc_ctx
);
553 /* Finally, allocate buffers and video memory */
554 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
556 dprintk(1, "Memory allocation failed\n");
560 allocated_buffers
= ret
;
563 * Check if driver can handle the allocated number of buffers.
565 if (allocated_buffers
< num_buffers
) {
566 num_buffers
= allocated_buffers
;
568 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
569 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
571 if (!ret
&& allocated_buffers
< num_buffers
)
575 * Either the driver has accepted a smaller number of buffers,
576 * or .queue_setup() returned an error
580 q
->num_buffers
= allocated_buffers
;
583 __vb2_queue_free(q
, allocated_buffers
);
588 * Return the number of successfully allocated buffers
591 req
->count
= allocated_buffers
;
595 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
598 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
599 * @q: videobuf2 queue
600 * @create: creation parameters, passed from userspace to vidioc_create_bufs
603 * Should be called from vidioc_create_bufs ioctl handler of a driver.
605 * 1) verifies parameter sanity
606 * 2) calls the .queue_setup() queue operation
607 * 3) performs any necessary memory allocations
609 * The return values from this function are intended to be directly returned
610 * from vidioc_create_bufs handler in driver.
612 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
614 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
618 dprintk(1, "%s(): file io in progress\n", __func__
);
622 if (create
->memory
!= V4L2_MEMORY_MMAP
623 && create
->memory
!= V4L2_MEMORY_USERPTR
) {
624 dprintk(1, "%s(): unsupported memory type\n", __func__
);
628 if (create
->format
.type
!= q
->type
) {
629 dprintk(1, "%s(): requested type is incorrect\n", __func__
);
634 * Make sure all the required memory ops for given memory type
637 if (create
->memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
638 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__
);
642 if (create
->memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
643 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__
);
647 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
648 dprintk(1, "%s(): maximum number of buffers already allocated\n",
653 create
->index
= q
->num_buffers
;
655 if (!q
->num_buffers
) {
656 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
657 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
658 q
->memory
= create
->memory
;
661 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
664 * Ask the driver, whether the requested number of buffers, planes per
665 * buffer and their sizes are acceptable
667 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
668 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
672 /* Finally, allocate buffers and video memory */
673 ret
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
676 dprintk(1, "Memory allocation failed with error: %d\n", ret
);
680 allocated_buffers
= ret
;
683 * Check if driver can handle the so far allocated number of buffers.
685 if (ret
< num_buffers
) {
689 * q->num_buffers contains the total number of buffers, that the
690 * queue driver has set up
692 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
693 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
695 if (!ret
&& allocated_buffers
< num_buffers
)
699 * Either the driver has accepted a smaller number of buffers,
700 * or .queue_setup() returned an error
704 q
->num_buffers
+= allocated_buffers
;
707 __vb2_queue_free(q
, allocated_buffers
);
712 * Return the number of successfully allocated buffers
715 create
->count
= allocated_buffers
;
719 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
722 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
723 * @vb: vb2_buffer to which the plane in question belongs to
724 * @plane_no: plane number for which the address is to be returned
726 * This function returns a kernel virtual address of a given plane if
727 * such a mapping exist, NULL otherwise.
729 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
731 struct vb2_queue
*q
= vb
->vb2_queue
;
733 if (plane_no
> vb
->num_planes
)
736 return call_memop(q
, plane_no
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
739 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
742 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
743 * @vb: vb2_buffer to which the plane in question belongs to
744 * @plane_no: plane number for which the cookie is to be returned
746 * This function returns an allocator specific cookie for a given plane if
747 * available, NULL otherwise. The allocator should provide some simple static
748 * inline function, which would convert this cookie to the allocator specific
749 * type that can be used directly by the driver to access the buffer. This can
750 * be for example physical address, pointer to scatter list or IOMMU mapping.
752 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
754 struct vb2_queue
*q
= vb
->vb2_queue
;
756 if (plane_no
> vb
->num_planes
)
759 return call_memop(q
, plane_no
, cookie
, vb
->planes
[plane_no
].mem_priv
);
761 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
764 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
765 * @vb: vb2_buffer returned from the driver
766 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
767 * or VB2_BUF_STATE_ERROR if the operation finished with an error
769 * This function should be called by the driver after a hardware operation on
770 * a buffer is finished and the buffer may be returned to userspace. The driver
771 * cannot use this buffer anymore until it is queued back to it by videobuf
772 * by the means of buf_queue callback. Only buffers previously queued to the
773 * driver by buf_queue can be passed to this function.
775 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
777 struct vb2_queue
*q
= vb
->vb2_queue
;
780 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
783 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
786 dprintk(4, "Done processing on buffer %d, state: %d\n",
787 vb
->v4l2_buf
.index
, vb
->state
);
789 /* Add the buffer to the done buffers list */
790 spin_lock_irqsave(&q
->done_lock
, flags
);
792 list_add_tail(&vb
->done_entry
, &q
->done_list
);
793 atomic_dec(&q
->queued_count
);
794 spin_unlock_irqrestore(&q
->done_lock
, flags
);
796 /* Inform any processes that may be waiting for buffers */
797 wake_up(&q
->done_wq
);
799 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
802 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
803 * a v4l2_buffer by the userspace
805 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
806 struct v4l2_plane
*v4l2_planes
)
811 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
813 * Verify that the userspace gave us a valid array for
816 ret
= __verify_planes_array(vb
, b
);
820 /* Fill in driver-provided information for OUTPUT types */
821 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
823 * Will have to go up to b->length when API starts
824 * accepting variable number of planes.
826 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
827 v4l2_planes
[plane
].bytesused
=
828 b
->m
.planes
[plane
].bytesused
;
829 v4l2_planes
[plane
].data_offset
=
830 b
->m
.planes
[plane
].data_offset
;
834 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
835 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
836 v4l2_planes
[plane
].m
.userptr
=
837 b
->m
.planes
[plane
].m
.userptr
;
838 v4l2_planes
[plane
].length
=
839 b
->m
.planes
[plane
].length
;
844 * Single-planar buffers do not use planes array,
845 * so fill in relevant v4l2_buffer struct fields instead.
846 * In videobuf we use our internal V4l2_planes struct for
847 * single-planar buffers as well, for simplicity.
849 if (V4L2_TYPE_IS_OUTPUT(b
->type
))
850 v4l2_planes
[0].bytesused
= b
->bytesused
;
852 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
853 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
854 v4l2_planes
[0].length
= b
->length
;
858 vb
->v4l2_buf
.field
= b
->field
;
859 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
860 vb
->v4l2_buf
.input
= b
->input
;
861 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_STATE_FLAGS
;
867 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
869 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
871 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
872 struct vb2_queue
*q
= vb
->vb2_queue
;
876 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
878 /* Verify and copy relevant information provided by the userspace */
879 ret
= __fill_vb2_buffer(vb
, b
, planes
);
883 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
884 /* Skip the plane if already verified */
885 if (vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
886 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
889 dprintk(3, "qbuf: userspace address for plane %d changed, "
890 "reacquiring memory\n", plane
);
892 /* Check if the provided plane buffer is large enough */
893 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
898 /* Release previously acquired memory if present */
899 if (vb
->planes
[plane
].mem_priv
)
900 call_memop(q
, plane
, put_userptr
,
901 vb
->planes
[plane
].mem_priv
);
903 vb
->planes
[plane
].mem_priv
= NULL
;
904 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
905 vb
->v4l2_planes
[plane
].length
= 0;
907 /* Acquire each plane's memory */
908 if (q
->mem_ops
->get_userptr
) {
909 mem_priv
= q
->mem_ops
->get_userptr(q
->alloc_ctx
[plane
],
910 planes
[plane
].m
.userptr
,
911 planes
[plane
].length
,
913 if (IS_ERR(mem_priv
)) {
914 dprintk(1, "qbuf: failed acquiring userspace "
915 "memory for plane %d\n", plane
);
916 ret
= PTR_ERR(mem_priv
);
919 vb
->planes
[plane
].mem_priv
= mem_priv
;
924 * Call driver-specific initialization on the newly acquired buffer,
927 ret
= call_qop(q
, buf_init
, vb
);
929 dprintk(1, "qbuf: buffer initialization failed\n");
934 * Now that everything is in order, copy relevant information
935 * provided by userspace.
937 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
938 vb
->v4l2_planes
[plane
] = planes
[plane
];
942 /* In case of errors, release planes that were already acquired */
943 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
944 if (vb
->planes
[plane
].mem_priv
)
945 call_memop(q
, plane
, put_userptr
,
946 vb
->planes
[plane
].mem_priv
);
947 vb
->planes
[plane
].mem_priv
= NULL
;
948 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
949 vb
->v4l2_planes
[plane
].length
= 0;
956 * __qbuf_mmap() - handle qbuf of an MMAP buffer
958 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
960 return __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
964 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
966 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
968 struct vb2_queue
*q
= vb
->vb2_queue
;
970 vb
->state
= VB2_BUF_STATE_ACTIVE
;
971 atomic_inc(&q
->queued_count
);
972 q
->ops
->buf_queue(vb
);
975 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
977 struct vb2_queue
*q
= vb
->vb2_queue
;
981 case V4L2_MEMORY_MMAP
:
982 ret
= __qbuf_mmap(vb
, b
);
984 case V4L2_MEMORY_USERPTR
:
985 ret
= __qbuf_userptr(vb
, b
);
988 WARN(1, "Invalid queue type\n");
993 ret
= call_qop(q
, buf_prepare
, vb
);
995 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
997 vb
->state
= VB2_BUF_STATE_PREPARED
;
1003 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1004 * @q: videobuf2 queue
1005 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1008 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1010 * 1) verifies the passed buffer,
1011 * 2) calls buf_prepare callback in the driver (if provided), in which
1012 * driver-specific buffer initialization can be performed,
1014 * The return values from this function are intended to be directly returned
1015 * from vidioc_prepare_buf handler in driver.
1017 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1019 struct vb2_buffer
*vb
;
1023 dprintk(1, "%s(): file io in progress\n", __func__
);
1027 if (b
->type
!= q
->type
) {
1028 dprintk(1, "%s(): invalid buffer type\n", __func__
);
1032 if (b
->index
>= q
->num_buffers
) {
1033 dprintk(1, "%s(): buffer index out of range\n", __func__
);
1037 vb
= q
->bufs
[b
->index
];
1039 /* Should never happen */
1040 dprintk(1, "%s(): buffer is NULL\n", __func__
);
1044 if (b
->memory
!= q
->memory
) {
1045 dprintk(1, "%s(): invalid memory type\n", __func__
);
1049 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1050 dprintk(1, "%s(): invalid buffer state %d\n", __func__
, vb
->state
);
1054 ret
= __buf_prepare(vb
, b
);
1058 __fill_v4l2_buffer(vb
, b
);
1062 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1065 * vb2_qbuf() - Queue a buffer from userspace
1066 * @q: videobuf2 queue
1067 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1070 * Should be called from vidioc_qbuf ioctl handler of a driver.
1072 * 1) verifies the passed buffer,
1073 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1074 * which driver-specific buffer initialization can be performed,
1075 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1076 * callback for processing.
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_qbuf handler in driver.
1081 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1083 struct vb2_buffer
*vb
;
1087 dprintk(1, "qbuf: file io in progress\n");
1091 if (b
->type
!= q
->type
) {
1092 dprintk(1, "qbuf: invalid buffer type\n");
1096 if (b
->index
>= q
->num_buffers
) {
1097 dprintk(1, "qbuf: buffer index out of range\n");
1101 vb
= q
->bufs
[b
->index
];
1103 /* Should never happen */
1104 dprintk(1, "qbuf: buffer is NULL\n");
1108 if (b
->memory
!= q
->memory
) {
1109 dprintk(1, "qbuf: invalid memory type\n");
1113 switch (vb
->state
) {
1114 case VB2_BUF_STATE_DEQUEUED
:
1115 ret
= __buf_prepare(vb
, b
);
1118 case VB2_BUF_STATE_PREPARED
:
1121 dprintk(1, "qbuf: buffer already in use\n");
1126 * Add to the queued buffers list, a buffer will stay on it until
1127 * dequeued in dqbuf.
1129 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1130 vb
->state
= VB2_BUF_STATE_QUEUED
;
1133 * If already streaming, give the buffer to driver for processing.
1134 * If not, the buffer will be given to driver on next streamon.
1137 __enqueue_in_driver(vb
);
1139 /* Fill buffer information for the userspace */
1140 __fill_v4l2_buffer(vb
, b
);
1142 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
1145 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1148 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1151 * Will sleep if required for nonblocking == false.
1153 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1156 * All operations on vb_done_list are performed under done_lock
1157 * spinlock protection. However, buffers may be removed from
1158 * it and returned to userspace only while holding both driver's
1159 * lock and the done_lock spinlock. Thus we can be sure that as
1160 * long as we hold the driver's lock, the list will remain not
1161 * empty if list_empty() check succeeds.
1167 if (!q
->streaming
) {
1168 dprintk(1, "Streaming off, will not wait for buffers\n");
1172 if (!list_empty(&q
->done_list
)) {
1174 * Found a buffer that we were waiting for.
1180 dprintk(1, "Nonblocking and no buffers to dequeue, "
1186 * We are streaming and blocking, wait for another buffer to
1187 * become ready or for streamoff. Driver's lock is released to
1188 * allow streamoff or qbuf to be called while waiting.
1190 call_qop(q
, wait_prepare
, q
);
1193 * All locks have been released, it is safe to sleep now.
1195 dprintk(3, "Will sleep waiting for buffers\n");
1196 ret
= wait_event_interruptible(q
->done_wq
,
1197 !list_empty(&q
->done_list
) || !q
->streaming
);
1200 * We need to reevaluate both conditions again after reacquiring
1201 * the locks or return an error if one occurred.
1203 call_qop(q
, wait_finish
, q
);
1211 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1213 * Will sleep if required for nonblocking == false.
1215 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1218 unsigned long flags
;
1222 * Wait for at least one buffer to become available on the done_list.
1224 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1229 * Driver's lock has been held since we last verified that done_list
1230 * is not empty, so no need for another list_empty(done_list) check.
1232 spin_lock_irqsave(&q
->done_lock
, flags
);
1233 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1234 list_del(&(*vb
)->done_entry
);
1235 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1241 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1242 * @q: videobuf2 queue
1244 * This function will wait until all buffers that have been given to the driver
1245 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1246 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1247 * taken, for example from stop_streaming() callback.
1249 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1251 if (!q
->streaming
) {
1252 dprintk(1, "Streaming off, will not wait for buffers\n");
1256 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1259 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1262 * vb2_dqbuf() - Dequeue a buffer to the userspace
1263 * @q: videobuf2 queue
1264 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1266 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1267 * buffers ready for dequeuing are present. Normally the driver
1268 * would be passing (file->f_flags & O_NONBLOCK) here
1270 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1272 * 1) verifies the passed buffer,
1273 * 2) calls buf_finish callback in the driver (if provided), in which
1274 * driver can perform any additional operations that may be required before
1275 * returning the buffer to userspace, such as cache sync,
1276 * 3) the buffer struct members are filled with relevant information for
1279 * The return values from this function are intended to be directly returned
1280 * from vidioc_dqbuf handler in driver.
1282 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1284 struct vb2_buffer
*vb
= NULL
;
1288 dprintk(1, "dqbuf: file io in progress\n");
1292 if (b
->type
!= q
->type
) {
1293 dprintk(1, "dqbuf: invalid buffer type\n");
1297 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1299 dprintk(1, "dqbuf: error getting next done buffer\n");
1303 ret
= call_qop(q
, buf_finish
, vb
);
1305 dprintk(1, "dqbuf: buffer finish failed\n");
1309 switch (vb
->state
) {
1310 case VB2_BUF_STATE_DONE
:
1311 dprintk(3, "dqbuf: Returning done buffer\n");
1313 case VB2_BUF_STATE_ERROR
:
1314 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1317 dprintk(1, "dqbuf: Invalid buffer state\n");
1321 /* Fill buffer information for the userspace */
1322 __fill_v4l2_buffer(vb
, b
);
1323 /* Remove from videobuf queue */
1324 list_del(&vb
->queued_entry
);
1326 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1327 vb
->v4l2_buf
.index
, vb
->state
);
1329 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1332 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1335 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1337 * Removes all queued buffers from driver's queue and all buffers queued by
1338 * userspace from videobuf's queue. Returns to state after reqbufs.
1340 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1345 * Tell driver to stop all transactions and release all queued
1349 call_qop(q
, stop_streaming
, q
);
1353 * Remove all buffers from videobuf's list...
1355 INIT_LIST_HEAD(&q
->queued_list
);
1357 * ...and done list; userspace will not receive any buffers it
1358 * has not already dequeued before initiating cancel.
1360 INIT_LIST_HEAD(&q
->done_list
);
1361 atomic_set(&q
->queued_count
, 0);
1362 wake_up_all(&q
->done_wq
);
1365 * Reinitialize all buffers for next use.
1367 for (i
= 0; i
< q
->num_buffers
; ++i
)
1368 q
->bufs
[i
]->state
= VB2_BUF_STATE_DEQUEUED
;
1372 * vb2_streamon - start streaming
1373 * @q: videobuf2 queue
1374 * @type: type argument passed from userspace to vidioc_streamon handler
1376 * Should be called from vidioc_streamon handler of a driver.
1378 * 1) verifies current state
1379 * 2) passes any previously queued buffers to the driver and starts streaming
1381 * The return values from this function are intended to be directly returned
1382 * from vidioc_streamon handler in the driver.
1384 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1386 struct vb2_buffer
*vb
;
1390 dprintk(1, "streamon: file io in progress\n");
1394 if (type
!= q
->type
) {
1395 dprintk(1, "streamon: invalid stream type\n");
1400 dprintk(1, "streamon: already streaming\n");
1405 * If any buffers were queued before streamon,
1406 * we can now pass them to driver for processing.
1408 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1409 __enqueue_in_driver(vb
);
1412 * Let driver notice that streaming state has been enabled.
1414 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->queued_count
));
1416 dprintk(1, "streamon: driver refused to start streaming\n");
1417 __vb2_queue_cancel(q
);
1423 dprintk(3, "Streamon successful\n");
1426 EXPORT_SYMBOL_GPL(vb2_streamon
);
1430 * vb2_streamoff - stop streaming
1431 * @q: videobuf2 queue
1432 * @type: type argument passed from userspace to vidioc_streamoff handler
1434 * Should be called from vidioc_streamoff handler of a driver.
1436 * 1) verifies current state,
1437 * 2) stop streaming and dequeues any queued buffers, including those previously
1438 * passed to the driver (after waiting for the driver to finish).
1440 * This call can be used for pausing playback.
1441 * The return values from this function are intended to be directly returned
1442 * from vidioc_streamoff handler in the driver
1444 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1447 dprintk(1, "streamoff: file io in progress\n");
1451 if (type
!= q
->type
) {
1452 dprintk(1, "streamoff: invalid stream type\n");
1456 if (!q
->streaming
) {
1457 dprintk(1, "streamoff: not streaming\n");
1462 * Cancel will pause streaming and remove all buffers from the driver
1463 * and videobuf, effectively returning control over them to userspace.
1465 __vb2_queue_cancel(q
);
1467 dprintk(3, "Streamoff successful\n");
1470 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1473 * __find_plane_by_offset() - find plane associated with the given offset off
1475 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1476 unsigned int *_buffer
, unsigned int *_plane
)
1478 struct vb2_buffer
*vb
;
1479 unsigned int buffer
, plane
;
1482 * Go over all buffers and their planes, comparing the given offset
1483 * with an offset assigned to each plane. If a match is found,
1484 * return its buffer and plane numbers.
1486 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1487 vb
= q
->bufs
[buffer
];
1489 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1490 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1502 * vb2_mmap() - map video buffers into application address space
1503 * @q: videobuf2 queue
1504 * @vma: vma passed to the mmap file operation handler in the driver
1506 * Should be called from mmap file operation handler of a driver.
1507 * This function maps one plane of one of the available video buffers to
1508 * userspace. To map whole video memory allocated on reqbufs, this function
1509 * has to be called once per each plane per each buffer previously allocated.
1511 * When the userspace application calls mmap, it passes to it an offset returned
1512 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1513 * a "cookie", which is then used to identify the plane to be mapped.
1514 * This function finds a plane with a matching offset and a mapping is performed
1515 * by the means of a provided memory operation.
1517 * The return values from this function are intended to be directly returned
1518 * from the mmap handler in driver.
1520 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1522 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1523 struct vb2_plane
*vb_plane
;
1524 struct vb2_buffer
*vb
;
1525 unsigned int buffer
, plane
;
1528 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1529 dprintk(1, "Queue is not currently set up for mmap\n");
1534 * Check memory area access mode.
1536 if (!(vma
->vm_flags
& VM_SHARED
)) {
1537 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1540 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1541 if (!(vma
->vm_flags
& VM_WRITE
)) {
1542 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1546 if (!(vma
->vm_flags
& VM_READ
)) {
1547 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1553 * Find the plane corresponding to the offset passed by userspace.
1555 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1559 vb
= q
->bufs
[buffer
];
1560 vb_plane
= &vb
->planes
[plane
];
1562 ret
= q
->mem_ops
->mmap(vb_plane
->mem_priv
, vma
);
1566 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1569 EXPORT_SYMBOL_GPL(vb2_mmap
);
1572 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
1575 unsigned long pgoff
,
1576 unsigned long flags
)
1578 unsigned long off
= pgoff
<< PAGE_SHIFT
;
1579 struct vb2_buffer
*vb
;
1580 unsigned int buffer
, plane
;
1583 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1584 dprintk(1, "Queue is not currently set up for mmap\n");
1589 * Find the plane corresponding to the offset passed by userspace.
1591 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1595 vb
= q
->bufs
[buffer
];
1597 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
1599 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
1602 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1603 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1606 * vb2_poll() - implements poll userspace operation
1607 * @q: videobuf2 queue
1608 * @file: file argument passed to the poll file operation handler
1609 * @wait: wait argument passed to the poll file operation handler
1611 * This function implements poll file operation handler for a driver.
1612 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1613 * be informed that the file descriptor of a video device is available for
1615 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1616 * will be reported as available for writing.
1618 * The return values from this function are intended to be directly returned
1619 * from poll handler in driver.
1621 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1623 unsigned long flags
;
1625 struct vb2_buffer
*vb
= NULL
;
1628 * Start file I/O emulator only if streaming API has not been used yet.
1630 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1631 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
)) {
1632 ret
= __vb2_init_fileio(q
, 1);
1636 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
)) {
1637 ret
= __vb2_init_fileio(q
, 0);
1641 * Write to OUTPUT queue can be done immediately.
1643 return POLLOUT
| POLLWRNORM
;
1648 * There is nothing to wait for if no buffers have already been queued.
1650 if (list_empty(&q
->queued_list
))
1653 poll_wait(file
, &q
->done_wq
, wait
);
1656 * Take first buffer available for dequeuing.
1658 spin_lock_irqsave(&q
->done_lock
, flags
);
1659 if (!list_empty(&q
->done_list
))
1660 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
1662 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1664 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
1665 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
1666 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ? POLLOUT
| POLLWRNORM
:
1667 POLLIN
| POLLRDNORM
;
1671 EXPORT_SYMBOL_GPL(vb2_poll
);
1674 * vb2_queue_init() - initialize a videobuf2 queue
1675 * @q: videobuf2 queue; this structure should be allocated in driver
1677 * The vb2_queue structure should be allocated by the driver. The driver is
1678 * responsible of clearing it's content and setting initial values for some
1679 * required entries before calling this function.
1680 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1681 * to the struct vb2_queue description in include/media/videobuf2-core.h
1682 * for more information.
1684 int vb2_queue_init(struct vb2_queue
*q
)
1688 BUG_ON(!q
->mem_ops
);
1690 BUG_ON(!q
->io_modes
);
1692 BUG_ON(!q
->ops
->queue_setup
);
1693 BUG_ON(!q
->ops
->buf_queue
);
1695 INIT_LIST_HEAD(&q
->queued_list
);
1696 INIT_LIST_HEAD(&q
->done_list
);
1697 spin_lock_init(&q
->done_lock
);
1698 init_waitqueue_head(&q
->done_wq
);
1700 if (q
->buf_struct_size
== 0)
1701 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
1705 EXPORT_SYMBOL_GPL(vb2_queue_init
);
1708 * vb2_queue_release() - stop streaming, release the queue and free memory
1709 * @q: videobuf2 queue
1711 * This function stops streaming and performs necessary clean ups, including
1712 * freeing video buffer memory. The driver is responsible for freeing
1713 * the vb2_queue structure itself.
1715 void vb2_queue_release(struct vb2_queue
*q
)
1717 __vb2_cleanup_fileio(q
);
1718 __vb2_queue_cancel(q
);
1719 __vb2_queue_free(q
, q
->num_buffers
);
1721 EXPORT_SYMBOL_GPL(vb2_queue_release
);
1724 * struct vb2_fileio_buf - buffer context used by file io emulator
1726 * vb2 provides a compatibility layer and emulator of file io (read and
1727 * write) calls on top of streaming API. This structure is used for
1728 * tracking context related to the buffers.
1730 struct vb2_fileio_buf
{
1734 unsigned int queued
:1;
1738 * struct vb2_fileio_data - queue context used by file io emulator
1740 * vb2 provides a compatibility layer and emulator of file io (read and
1741 * write) calls on top of streaming API. For proper operation it required
1742 * this structure to save the driver state between each call of the read
1743 * or write function.
1745 struct vb2_fileio_data
{
1746 struct v4l2_requestbuffers req
;
1747 struct v4l2_buffer b
;
1748 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
1750 unsigned int q_count
;
1751 unsigned int dq_count
;
1756 * __vb2_init_fileio() - initialize file io emulator
1757 * @q: videobuf2 queue
1758 * @read: mode selector (1 means read, 0 means write)
1760 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
1762 struct vb2_fileio_data
*fileio
;
1764 unsigned int count
= 0;
1769 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
1770 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
1774 * Check if device supports mapping buffers to kernel virtual space.
1776 if (!q
->mem_ops
->vaddr
)
1780 * Check if streaming api has not been already activated.
1782 if (q
->streaming
|| q
->num_buffers
> 0)
1786 * Start with count 1, driver can increase it in queue_setup()
1790 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1791 (read
) ? "read" : "write", count
, q
->io_flags
);
1793 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
1797 fileio
->flags
= q
->io_flags
;
1800 * Request buffers and use MMAP type to force driver
1801 * to allocate buffers by itself.
1803 fileio
->req
.count
= count
;
1804 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
1805 fileio
->req
.type
= q
->type
;
1806 ret
= vb2_reqbufs(q
, &fileio
->req
);
1811 * Check if plane_count is correct
1812 * (multiplane buffers are not supported).
1814 if (q
->bufs
[0]->num_planes
!= 1) {
1815 fileio
->req
.count
= 0;
1821 * Get kernel address of each buffer.
1823 for (i
= 0; i
< q
->num_buffers
; i
++) {
1824 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
1825 if (fileio
->bufs
[i
].vaddr
== NULL
)
1827 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
1831 * Read mode requires pre queuing of all buffers.
1835 * Queue all buffers.
1837 for (i
= 0; i
< q
->num_buffers
; i
++) {
1838 struct v4l2_buffer
*b
= &fileio
->b
;
1839 memset(b
, 0, sizeof(*b
));
1841 b
->memory
= q
->memory
;
1843 ret
= vb2_qbuf(q
, b
);
1846 fileio
->bufs
[i
].queued
= 1;
1852 ret
= vb2_streamon(q
, q
->type
);
1862 vb2_reqbufs(q
, &fileio
->req
);
1870 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1871 * @q: videobuf2 queue
1873 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
1875 struct vb2_fileio_data
*fileio
= q
->fileio
;
1879 * Hack fileio context to enable direct calls to vb2 ioctl
1884 vb2_streamoff(q
, q
->type
);
1885 fileio
->req
.count
= 0;
1886 vb2_reqbufs(q
, &fileio
->req
);
1888 dprintk(3, "file io emulator closed\n");
1894 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1895 * @q: videobuf2 queue
1896 * @data: pointed to target userspace buffer
1897 * @count: number of bytes to read or write
1898 * @ppos: file handle position tracking pointer
1899 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1900 * @read: access mode selector (1 means read, 0 means write)
1902 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1903 loff_t
*ppos
, int nonblock
, int read
)
1905 struct vb2_fileio_data
*fileio
;
1906 struct vb2_fileio_buf
*buf
;
1909 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1910 read
? "read" : "write", (long)*ppos
, count
,
1911 nonblock
? "non" : "");
1917 * Initialize emulator on first call.
1920 ret
= __vb2_init_fileio(q
, read
);
1921 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
1928 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1929 * The pointer will be restored before returning from this function.
1933 index
= fileio
->index
;
1934 buf
= &fileio
->bufs
[index
];
1937 * Check if we need to dequeue the buffer.
1940 struct vb2_buffer
*vb
;
1943 * Call vb2_dqbuf to get buffer back.
1945 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
1946 fileio
->b
.type
= q
->type
;
1947 fileio
->b
.memory
= q
->memory
;
1948 fileio
->b
.index
= index
;
1949 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
1950 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
1953 fileio
->dq_count
+= 1;
1956 * Get number of bytes filled by the driver
1958 vb
= q
->bufs
[index
];
1959 buf
->size
= vb2_get_plane_payload(vb
, 0);
1964 * Limit count on last few bytes of the buffer.
1966 if (buf
->pos
+ count
> buf
->size
) {
1967 count
= buf
->size
- buf
->pos
;
1968 dprintk(5, "reducing read count: %zd\n", count
);
1972 * Transfer data to userspace.
1974 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1975 count
, index
, buf
->pos
);
1977 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
1979 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
1981 dprintk(3, "file io: error copying data\n");
1993 * Queue next buffer if required.
1995 if (buf
->pos
== buf
->size
||
1996 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
1998 * Check if this is the last buffer to read.
2000 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2001 fileio
->dq_count
== 1) {
2002 dprintk(3, "file io: read limit reached\n");
2004 * Restore fileio pointer and release the context.
2007 return __vb2_cleanup_fileio(q
);
2011 * Call vb2_qbuf and give buffer to the driver.
2013 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2014 fileio
->b
.type
= q
->type
;
2015 fileio
->b
.memory
= q
->memory
;
2016 fileio
->b
.index
= index
;
2017 fileio
->b
.bytesused
= buf
->pos
;
2018 ret
= vb2_qbuf(q
, &fileio
->b
);
2019 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2024 * Buffer has been queued, update the status
2028 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
2029 fileio
->q_count
+= 1;
2032 * Switch to the next buffer
2034 fileio
->index
= (index
+ 1) % q
->num_buffers
;
2037 * Start streaming if required.
2039 if (!read
&& !q
->streaming
) {
2040 ret
= vb2_streamon(q
, q
->type
);
2047 * Return proper number of bytes processed.
2053 * Restore the fileio context and block vb2 ioctl interface.
2059 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2060 loff_t
*ppos
, int nonblocking
)
2062 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2064 EXPORT_SYMBOL_GPL(vb2_read
);
2066 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2067 loff_t
*ppos
, int nonblocking
)
2069 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
2071 EXPORT_SYMBOL_GPL(vb2_write
);
2073 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2074 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2075 MODULE_LICENSE("GPL");