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/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-core.h>
28 module_param(debug
, int, 0644);
30 #define dprintk(level, fmt, arg...) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
36 #define call_memop(q, op, args...) \
37 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
40 #define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
43 #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
44 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
45 V4L2_BUF_FLAG_PREPARED)
48 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
52 struct vb2_queue
*q
= vb
->vb2_queue
;
56 /* Allocate memory for all planes in this buffer */
57 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
58 mem_priv
= call_memop(q
, alloc
, q
->alloc_ctx
[plane
],
59 q
->plane_sizes
[plane
]);
60 if (IS_ERR_OR_NULL(mem_priv
))
63 /* Associate allocator private data with this plane */
64 vb
->planes
[plane
].mem_priv
= mem_priv
;
65 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
70 /* Free already allocated memory if one of the allocations failed */
71 for (; plane
> 0; --plane
) {
72 call_memop(q
, put
, vb
->planes
[plane
- 1].mem_priv
);
73 vb
->planes
[plane
- 1].mem_priv
= NULL
;
80 * __vb2_buf_mem_free() - free memory of the given buffer
82 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
84 struct vb2_queue
*q
= vb
->vb2_queue
;
87 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
88 call_memop(q
, put
, vb
->planes
[plane
].mem_priv
);
89 vb
->planes
[plane
].mem_priv
= NULL
;
90 dprintk(3, "Freed plane %d of buffer %d\n", plane
,
96 * __vb2_buf_userptr_put() - release userspace memory associated with
99 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
101 struct vb2_queue
*q
= vb
->vb2_queue
;
104 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
105 if (vb
->planes
[plane
].mem_priv
)
106 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
107 vb
->planes
[plane
].mem_priv
= NULL
;
112 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
113 * every buffer on the queue
115 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
117 unsigned int buffer
, plane
;
118 struct vb2_buffer
*vb
;
121 if (q
->num_buffers
) {
122 struct v4l2_plane
*p
;
123 vb
= q
->bufs
[q
->num_buffers
- 1];
124 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
125 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
130 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
131 vb
= q
->bufs
[buffer
];
135 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
136 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
137 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
139 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
142 off
+= vb
->v4l2_planes
[plane
].length
;
143 off
= PAGE_ALIGN(off
);
149 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
150 * video buffer memory for all buffers/planes on the queue and initializes the
153 * Returns the number of buffers successfully allocated.
155 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
156 unsigned int num_buffers
, unsigned int num_planes
)
159 struct vb2_buffer
*vb
;
162 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
163 /* Allocate videobuf buffer structures */
164 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
166 dprintk(1, "Memory alloc for buffer struct failed\n");
170 /* Length stores number of planes for multiplanar buffers */
171 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
172 vb
->v4l2_buf
.length
= num_planes
;
174 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
176 vb
->num_planes
= num_planes
;
177 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
178 vb
->v4l2_buf
.type
= q
->type
;
179 vb
->v4l2_buf
.memory
= memory
;
181 /* Allocate video buffer memory for the MMAP type */
182 if (memory
== V4L2_MEMORY_MMAP
) {
183 ret
= __vb2_buf_mem_alloc(vb
);
185 dprintk(1, "Failed allocating memory for "
186 "buffer %d\n", buffer
);
191 * Call the driver-provided buffer initialization
192 * callback, if given. An error in initialization
193 * results in queue setup failure.
195 ret
= call_qop(q
, buf_init
, vb
);
197 dprintk(1, "Buffer %d %p initialization"
198 " failed\n", buffer
, vb
);
199 __vb2_buf_mem_free(vb
);
205 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
208 __setup_offsets(q
, buffer
);
210 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
217 * __vb2_free_mem() - release all video buffer memory for a given queue
219 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
222 struct vb2_buffer
*vb
;
224 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
226 vb
= q
->bufs
[buffer
];
230 /* Free MMAP buffers or release USERPTR buffers */
231 if (q
->memory
== V4L2_MEMORY_MMAP
)
232 __vb2_buf_mem_free(vb
);
234 __vb2_buf_userptr_put(vb
);
239 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
240 * related information, if no buffers are left return the queue to an
241 * uninitialized state. Might be called even if the queue has already been freed.
243 static void __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
247 /* Call driver-provided cleanup function for each buffer, if provided */
248 if (q
->ops
->buf_cleanup
) {
249 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
251 if (NULL
== q
->bufs
[buffer
])
253 q
->ops
->buf_cleanup(q
->bufs
[buffer
]);
257 /* Release video buffer memory */
258 __vb2_free_mem(q
, buffers
);
260 /* Free videobuf buffers */
261 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
263 kfree(q
->bufs
[buffer
]);
264 q
->bufs
[buffer
] = NULL
;
267 q
->num_buffers
-= buffers
;
270 INIT_LIST_HEAD(&q
->queued_list
);
274 * __verify_planes_array() - verify that the planes array passed in struct
275 * v4l2_buffer from userspace can be safely used
277 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
279 /* Is memory for copying plane information present? */
280 if (NULL
== b
->m
.planes
) {
281 dprintk(1, "Multi-planar buffer passed but "
282 "planes array not provided\n");
286 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
287 dprintk(1, "Incorrect planes array length, "
288 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
296 * __buffer_in_use() - return true if the buffer is in use and
297 * the queue cannot be freed (by the means of REQBUFS(0)) call
299 static bool __buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
302 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
303 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
305 * If num_users() has not been provided, call_memop
306 * will return 0, apparently nobody cares about this
307 * case anyway. If num_users() returns more than 1,
308 * we are not the only user of the plane's memory.
310 if (mem_priv
&& call_memop(q
, num_users
, mem_priv
) > 1)
317 * __buffers_in_use() - return true if any buffers on the queue are in use and
318 * the queue cannot be freed (by the means of REQBUFS(0)) call
320 static bool __buffers_in_use(struct vb2_queue
*q
)
323 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
324 if (__buffer_in_use(q
, q
->bufs
[buffer
]))
331 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
332 * returned to userspace
334 static int __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
336 struct vb2_queue
*q
= vb
->vb2_queue
;
339 /* Copy back data such as timestamp, flags, input, etc. */
340 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
341 b
->input
= vb
->v4l2_buf
.input
;
342 b
->reserved
= vb
->v4l2_buf
.reserved
;
344 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
345 ret
= __verify_planes_array(vb
, b
);
350 * Fill in plane-related data if userspace provided an array
351 * for it. The memory and size is verified above.
353 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
354 b
->length
* sizeof(struct v4l2_plane
));
357 * We use length and offset in v4l2_planes array even for
358 * single-planar buffers, but userspace does not.
360 b
->length
= vb
->v4l2_planes
[0].length
;
361 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
362 if (q
->memory
== V4L2_MEMORY_MMAP
)
363 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
364 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
365 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
369 * Clear any buffer state related flags.
371 b
->flags
&= ~V4L2_BUFFER_STATE_FLAGS
;
374 case VB2_BUF_STATE_QUEUED
:
375 case VB2_BUF_STATE_ACTIVE
:
376 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
378 case VB2_BUF_STATE_ERROR
:
379 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
381 case VB2_BUF_STATE_DONE
:
382 b
->flags
|= V4L2_BUF_FLAG_DONE
;
384 case VB2_BUF_STATE_PREPARED
:
385 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
387 case VB2_BUF_STATE_DEQUEUED
:
392 if (__buffer_in_use(q
, vb
))
393 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
399 * vb2_querybuf() - query video buffer information
401 * @b: buffer struct passed from userspace to vidioc_querybuf handler
404 * Should be called from vidioc_querybuf ioctl handler in driver.
405 * This function will verify the passed v4l2_buffer structure and fill the
406 * relevant information for the userspace.
408 * The return values from this function are intended to be directly returned
409 * from vidioc_querybuf handler in driver.
411 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
413 struct vb2_buffer
*vb
;
415 if (b
->type
!= q
->type
) {
416 dprintk(1, "querybuf: wrong buffer type\n");
420 if (b
->index
>= q
->num_buffers
) {
421 dprintk(1, "querybuf: buffer index out of range\n");
424 vb
= q
->bufs
[b
->index
];
426 return __fill_v4l2_buffer(vb
, b
);
428 EXPORT_SYMBOL(vb2_querybuf
);
431 * __verify_userptr_ops() - verify that all memory operations required for
432 * USERPTR queue type have been provided
434 static int __verify_userptr_ops(struct vb2_queue
*q
)
436 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
437 !q
->mem_ops
->put_userptr
)
444 * __verify_mmap_ops() - verify that all memory operations required for
445 * MMAP queue type have been provided
447 static int __verify_mmap_ops(struct vb2_queue
*q
)
449 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
450 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
457 * vb2_reqbufs() - Initiate streaming
458 * @q: videobuf2 queue
459 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
461 * Should be called from vidioc_reqbufs ioctl handler of a driver.
463 * 1) verifies streaming parameters passed from the userspace,
464 * 2) sets up the queue,
465 * 3) negotiates number of buffers and planes per buffer with the driver
466 * to be used during streaming,
467 * 4) allocates internal buffer structures (struct vb2_buffer), according to
468 * the agreed parameters,
469 * 5) for MMAP memory type, allocates actual video memory, using the
470 * memory handling/allocation routines provided during queue initialization
472 * If req->count is 0, all the memory will be freed instead.
473 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
474 * and the queue is not busy, memory will be reallocated.
476 * The return values from this function are intended to be directly returned
477 * from vidioc_reqbufs handler in driver.
479 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
481 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
485 dprintk(1, "reqbufs: file io in progress\n");
489 if (req
->memory
!= V4L2_MEMORY_MMAP
490 && req
->memory
!= V4L2_MEMORY_USERPTR
) {
491 dprintk(1, "reqbufs: unsupported memory type\n");
495 if (req
->type
!= q
->type
) {
496 dprintk(1, "reqbufs: requested type is incorrect\n");
501 dprintk(1, "reqbufs: streaming active\n");
506 * Make sure all the required memory ops for given memory type
509 if (req
->memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
510 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
514 if (req
->memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
515 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
519 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
521 * We already have buffers allocated, so first check if they
522 * are not in use and can be freed.
524 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
525 dprintk(1, "reqbufs: memory in use, cannot free\n");
529 __vb2_queue_free(q
, q
->num_buffers
);
532 * In case of REQBUFS(0) return immediately without calling
533 * driver's queue_setup() callback and allocating resources.
540 * Make sure the requested values and current defaults are sane.
542 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
543 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
544 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
545 q
->memory
= req
->memory
;
548 * Ask the driver how many buffers and planes per buffer it requires.
549 * Driver also sets the size and allocator context for each plane.
551 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
552 q
->plane_sizes
, q
->alloc_ctx
);
556 /* Finally, allocate buffers and video memory */
557 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
559 dprintk(1, "Memory allocation failed\n");
563 allocated_buffers
= ret
;
566 * Check if driver can handle the allocated number of buffers.
568 if (allocated_buffers
< num_buffers
) {
569 num_buffers
= allocated_buffers
;
571 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
572 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
574 if (!ret
&& allocated_buffers
< num_buffers
)
578 * Either the driver has accepted a smaller number of buffers,
579 * or .queue_setup() returned an error
583 q
->num_buffers
= allocated_buffers
;
586 __vb2_queue_free(q
, allocated_buffers
);
591 * Return the number of successfully allocated buffers
594 req
->count
= allocated_buffers
;
598 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
601 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
602 * @q: videobuf2 queue
603 * @create: creation parameters, passed from userspace to vidioc_create_bufs
606 * Should be called from vidioc_create_bufs ioctl handler of a driver.
608 * 1) verifies parameter sanity
609 * 2) calls the .queue_setup() queue operation
610 * 3) performs any necessary memory allocations
612 * The return values from this function are intended to be directly returned
613 * from vidioc_create_bufs handler in driver.
615 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
617 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
621 dprintk(1, "%s(): file io in progress\n", __func__
);
625 if (create
->memory
!= V4L2_MEMORY_MMAP
626 && create
->memory
!= V4L2_MEMORY_USERPTR
) {
627 dprintk(1, "%s(): unsupported memory type\n", __func__
);
631 if (create
->format
.type
!= q
->type
) {
632 dprintk(1, "%s(): requested type is incorrect\n", __func__
);
637 * Make sure all the required memory ops for given memory type
640 if (create
->memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
641 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__
);
645 if (create
->memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
646 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__
);
650 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
651 dprintk(1, "%s(): maximum number of buffers already allocated\n",
656 create
->index
= q
->num_buffers
;
658 if (!q
->num_buffers
) {
659 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
660 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
661 q
->memory
= create
->memory
;
664 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
667 * Ask the driver, whether the requested number of buffers, planes per
668 * buffer and their sizes are acceptable
670 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
671 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
675 /* Finally, allocate buffers and video memory */
676 ret
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
679 dprintk(1, "Memory allocation failed with error: %d\n", ret
);
683 allocated_buffers
= ret
;
686 * Check if driver can handle the so far allocated number of buffers.
688 if (ret
< num_buffers
) {
692 * q->num_buffers contains the total number of buffers, that the
693 * queue driver has set up
695 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
696 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
698 if (!ret
&& allocated_buffers
< num_buffers
)
702 * Either the driver has accepted a smaller number of buffers,
703 * or .queue_setup() returned an error
707 q
->num_buffers
+= allocated_buffers
;
710 __vb2_queue_free(q
, allocated_buffers
);
715 * Return the number of successfully allocated buffers
718 create
->count
= allocated_buffers
;
722 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
725 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
726 * @vb: vb2_buffer to which the plane in question belongs to
727 * @plane_no: plane number for which the address is to be returned
729 * This function returns a kernel virtual address of a given plane if
730 * such a mapping exist, NULL otherwise.
732 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
734 struct vb2_queue
*q
= vb
->vb2_queue
;
736 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
739 return call_memop(q
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
742 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
745 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
746 * @vb: vb2_buffer to which the plane in question belongs to
747 * @plane_no: plane number for which the cookie is to be returned
749 * This function returns an allocator specific cookie for a given plane if
750 * available, NULL otherwise. The allocator should provide some simple static
751 * inline function, which would convert this cookie to the allocator specific
752 * type that can be used directly by the driver to access the buffer. This can
753 * be for example physical address, pointer to scatter list or IOMMU mapping.
755 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
757 struct vb2_queue
*q
= vb
->vb2_queue
;
759 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
762 return call_memop(q
, cookie
, vb
->planes
[plane_no
].mem_priv
);
764 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
767 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
768 * @vb: vb2_buffer returned from the driver
769 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
770 * or VB2_BUF_STATE_ERROR if the operation finished with an error
772 * This function should be called by the driver after a hardware operation on
773 * a buffer is finished and the buffer may be returned to userspace. The driver
774 * cannot use this buffer anymore until it is queued back to it by videobuf
775 * by the means of buf_queue callback. Only buffers previously queued to the
776 * driver by buf_queue can be passed to this function.
778 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
780 struct vb2_queue
*q
= vb
->vb2_queue
;
783 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
786 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
789 dprintk(4, "Done processing on buffer %d, state: %d\n",
790 vb
->v4l2_buf
.index
, vb
->state
);
792 /* Add the buffer to the done buffers list */
793 spin_lock_irqsave(&q
->done_lock
, flags
);
795 list_add_tail(&vb
->done_entry
, &q
->done_list
);
796 atomic_dec(&q
->queued_count
);
797 spin_unlock_irqrestore(&q
->done_lock
, flags
);
799 /* Inform any processes that may be waiting for buffers */
800 wake_up(&q
->done_wq
);
802 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
805 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
806 * a v4l2_buffer by the userspace
808 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
809 struct v4l2_plane
*v4l2_planes
)
814 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
816 * Verify that the userspace gave us a valid array for
819 ret
= __verify_planes_array(vb
, b
);
823 /* Fill in driver-provided information for OUTPUT types */
824 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
826 * Will have to go up to b->length when API starts
827 * accepting variable number of planes.
829 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
830 v4l2_planes
[plane
].bytesused
=
831 b
->m
.planes
[plane
].bytesused
;
832 v4l2_planes
[plane
].data_offset
=
833 b
->m
.planes
[plane
].data_offset
;
837 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
838 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
839 v4l2_planes
[plane
].m
.userptr
=
840 b
->m
.planes
[plane
].m
.userptr
;
841 v4l2_planes
[plane
].length
=
842 b
->m
.planes
[plane
].length
;
847 * Single-planar buffers do not use planes array,
848 * so fill in relevant v4l2_buffer struct fields instead.
849 * In videobuf we use our internal V4l2_planes struct for
850 * single-planar buffers as well, for simplicity.
852 if (V4L2_TYPE_IS_OUTPUT(b
->type
))
853 v4l2_planes
[0].bytesused
= b
->bytesused
;
855 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
856 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
857 v4l2_planes
[0].length
= b
->length
;
861 vb
->v4l2_buf
.field
= b
->field
;
862 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
863 vb
->v4l2_buf
.input
= b
->input
;
864 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_STATE_FLAGS
;
870 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
872 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
874 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
875 struct vb2_queue
*q
= vb
->vb2_queue
;
879 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
881 /* Verify and copy relevant information provided by the userspace */
882 ret
= __fill_vb2_buffer(vb
, b
, planes
);
886 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
887 /* Skip the plane if already verified */
888 if (vb
->v4l2_planes
[plane
].m
.userptr
&&
889 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
890 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
893 dprintk(3, "qbuf: userspace address for plane %d changed, "
894 "reacquiring memory\n", plane
);
896 /* Check if the provided plane buffer is large enough */
897 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
902 /* Release previously acquired memory if present */
903 if (vb
->planes
[plane
].mem_priv
)
904 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
906 vb
->planes
[plane
].mem_priv
= NULL
;
907 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
908 vb
->v4l2_planes
[plane
].length
= 0;
910 /* Acquire each plane's memory */
911 mem_priv
= call_memop(q
, get_userptr
, q
->alloc_ctx
[plane
],
912 planes
[plane
].m
.userptr
,
913 planes
[plane
].length
, write
);
914 if (IS_ERR_OR_NULL(mem_priv
)) {
915 dprintk(1, "qbuf: failed acquiring userspace "
916 "memory for plane %d\n", plane
);
917 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
920 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
, put_userptr
, vb
->planes
[plane
].mem_priv
);
946 vb
->planes
[plane
].mem_priv
= NULL
;
947 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
948 vb
->v4l2_planes
[plane
].length
= 0;
955 * __qbuf_mmap() - handle qbuf of an MMAP buffer
957 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
959 return __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
963 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
965 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
967 struct vb2_queue
*q
= vb
->vb2_queue
;
969 vb
->state
= VB2_BUF_STATE_ACTIVE
;
970 atomic_inc(&q
->queued_count
);
971 q
->ops
->buf_queue(vb
);
974 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
976 struct vb2_queue
*q
= vb
->vb2_queue
;
980 case V4L2_MEMORY_MMAP
:
981 ret
= __qbuf_mmap(vb
, b
);
983 case V4L2_MEMORY_USERPTR
:
984 ret
= __qbuf_userptr(vb
, b
);
987 WARN(1, "Invalid queue type\n");
992 ret
= call_qop(q
, buf_prepare
, vb
);
994 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
996 vb
->state
= VB2_BUF_STATE_PREPARED
;
1002 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1003 * @q: videobuf2 queue
1004 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1007 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1009 * 1) verifies the passed buffer,
1010 * 2) calls buf_prepare callback in the driver (if provided), in which
1011 * driver-specific buffer initialization can be performed,
1013 * The return values from this function are intended to be directly returned
1014 * from vidioc_prepare_buf handler in driver.
1016 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1018 struct vb2_buffer
*vb
;
1022 dprintk(1, "%s(): file io in progress\n", __func__
);
1026 if (b
->type
!= q
->type
) {
1027 dprintk(1, "%s(): invalid buffer type\n", __func__
);
1031 if (b
->index
>= q
->num_buffers
) {
1032 dprintk(1, "%s(): buffer index out of range\n", __func__
);
1036 vb
= q
->bufs
[b
->index
];
1038 /* Should never happen */
1039 dprintk(1, "%s(): buffer is NULL\n", __func__
);
1043 if (b
->memory
!= q
->memory
) {
1044 dprintk(1, "%s(): invalid memory type\n", __func__
);
1048 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1049 dprintk(1, "%s(): invalid buffer state %d\n", __func__
, vb
->state
);
1053 ret
= __buf_prepare(vb
, b
);
1057 __fill_v4l2_buffer(vb
, b
);
1061 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1064 * vb2_qbuf() - Queue a buffer from userspace
1065 * @q: videobuf2 queue
1066 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1069 * Should be called from vidioc_qbuf ioctl handler of a driver.
1071 * 1) verifies the passed buffer,
1072 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1073 * which driver-specific buffer initialization can be performed,
1074 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1075 * callback for processing.
1077 * The return values from this function are intended to be directly returned
1078 * from vidioc_qbuf handler in driver.
1080 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1082 struct rw_semaphore
*mmap_sem
= NULL
;
1083 struct vb2_buffer
*vb
;
1087 * In case of user pointer buffers vb2 allocator needs to get direct
1088 * access to userspace pages. This requires getting read access on
1089 * mmap semaphore in the current process structure. The same
1090 * semaphore is taken before calling mmap operation, while both mmap
1091 * and qbuf are called by the driver or v4l2 core with driver's lock
1092 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1093 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1094 * release driver's lock, takes mmap_sem and then takes again driver's
1097 * To avoid race with other vb2 calls, which might be called after
1098 * releasing driver's lock, this operation is performed at the
1099 * beggining of qbuf processing. This way the queue status is
1100 * consistent after getting driver's lock back.
1102 if (q
->memory
== V4L2_MEMORY_USERPTR
) {
1103 mmap_sem
= ¤t
->mm
->mmap_sem
;
1104 call_qop(q
, wait_prepare
, q
);
1105 down_read(mmap_sem
);
1106 call_qop(q
, wait_finish
, q
);
1110 dprintk(1, "qbuf: file io in progress\n");
1115 if (b
->type
!= q
->type
) {
1116 dprintk(1, "qbuf: invalid buffer type\n");
1121 if (b
->index
>= q
->num_buffers
) {
1122 dprintk(1, "qbuf: buffer index out of range\n");
1127 vb
= q
->bufs
[b
->index
];
1129 /* Should never happen */
1130 dprintk(1, "qbuf: buffer is NULL\n");
1135 if (b
->memory
!= q
->memory
) {
1136 dprintk(1, "qbuf: invalid memory type\n");
1141 switch (vb
->state
) {
1142 case VB2_BUF_STATE_DEQUEUED
:
1143 ret
= __buf_prepare(vb
, b
);
1146 case VB2_BUF_STATE_PREPARED
:
1149 dprintk(1, "qbuf: buffer already in use\n");
1155 * Add to the queued buffers list, a buffer will stay on it until
1156 * dequeued in dqbuf.
1158 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1159 vb
->state
= VB2_BUF_STATE_QUEUED
;
1162 * If already streaming, give the buffer to driver for processing.
1163 * If not, the buffer will be given to driver on next streamon.
1166 __enqueue_in_driver(vb
);
1168 /* Fill buffer information for the userspace */
1169 __fill_v4l2_buffer(vb
, b
);
1171 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
1177 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1180 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1183 * Will sleep if required for nonblocking == false.
1185 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1188 * All operations on vb_done_list are performed under done_lock
1189 * spinlock protection. However, buffers may be removed from
1190 * it and returned to userspace only while holding both driver's
1191 * lock and the done_lock spinlock. Thus we can be sure that as
1192 * long as we hold the driver's lock, the list will remain not
1193 * empty if list_empty() check succeeds.
1199 if (!q
->streaming
) {
1200 dprintk(1, "Streaming off, will not wait for buffers\n");
1204 if (!list_empty(&q
->done_list
)) {
1206 * Found a buffer that we were waiting for.
1212 dprintk(1, "Nonblocking and no buffers to dequeue, "
1218 * We are streaming and blocking, wait for another buffer to
1219 * become ready or for streamoff. Driver's lock is released to
1220 * allow streamoff or qbuf to be called while waiting.
1222 call_qop(q
, wait_prepare
, q
);
1225 * All locks have been released, it is safe to sleep now.
1227 dprintk(3, "Will sleep waiting for buffers\n");
1228 ret
= wait_event_interruptible(q
->done_wq
,
1229 !list_empty(&q
->done_list
) || !q
->streaming
);
1232 * We need to reevaluate both conditions again after reacquiring
1233 * the locks or return an error if one occurred.
1235 call_qop(q
, wait_finish
, q
);
1243 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1245 * Will sleep if required for nonblocking == false.
1247 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1250 unsigned long flags
;
1254 * Wait for at least one buffer to become available on the done_list.
1256 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1261 * Driver's lock has been held since we last verified that done_list
1262 * is not empty, so no need for another list_empty(done_list) check.
1264 spin_lock_irqsave(&q
->done_lock
, flags
);
1265 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1266 list_del(&(*vb
)->done_entry
);
1267 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1273 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1274 * @q: videobuf2 queue
1276 * This function will wait until all buffers that have been given to the driver
1277 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1278 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1279 * taken, for example from stop_streaming() callback.
1281 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1283 if (!q
->streaming
) {
1284 dprintk(1, "Streaming off, will not wait for buffers\n");
1288 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1291 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1294 * vb2_dqbuf() - Dequeue a buffer to the userspace
1295 * @q: videobuf2 queue
1296 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1298 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1299 * buffers ready for dequeuing are present. Normally the driver
1300 * would be passing (file->f_flags & O_NONBLOCK) here
1302 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1304 * 1) verifies the passed buffer,
1305 * 2) calls buf_finish callback in the driver (if provided), in which
1306 * driver can perform any additional operations that may be required before
1307 * returning the buffer to userspace, such as cache sync,
1308 * 3) the buffer struct members are filled with relevant information for
1311 * The return values from this function are intended to be directly returned
1312 * from vidioc_dqbuf handler in driver.
1314 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1316 struct vb2_buffer
*vb
= NULL
;
1320 dprintk(1, "dqbuf: file io in progress\n");
1324 if (b
->type
!= q
->type
) {
1325 dprintk(1, "dqbuf: invalid buffer type\n");
1329 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1331 dprintk(1, "dqbuf: error getting next done buffer\n");
1335 ret
= call_qop(q
, buf_finish
, vb
);
1337 dprintk(1, "dqbuf: buffer finish failed\n");
1341 switch (vb
->state
) {
1342 case VB2_BUF_STATE_DONE
:
1343 dprintk(3, "dqbuf: Returning done buffer\n");
1345 case VB2_BUF_STATE_ERROR
:
1346 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1349 dprintk(1, "dqbuf: Invalid buffer state\n");
1353 /* Fill buffer information for the userspace */
1354 __fill_v4l2_buffer(vb
, b
);
1355 /* Remove from videobuf queue */
1356 list_del(&vb
->queued_entry
);
1358 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1359 vb
->v4l2_buf
.index
, vb
->state
);
1361 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1364 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1367 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1369 * Removes all queued buffers from driver's queue and all buffers queued by
1370 * userspace from videobuf's queue. Returns to state after reqbufs.
1372 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1377 * Tell driver to stop all transactions and release all queued
1381 call_qop(q
, stop_streaming
, q
);
1385 * Remove all buffers from videobuf's list...
1387 INIT_LIST_HEAD(&q
->queued_list
);
1389 * ...and done list; userspace will not receive any buffers it
1390 * has not already dequeued before initiating cancel.
1392 INIT_LIST_HEAD(&q
->done_list
);
1393 atomic_set(&q
->queued_count
, 0);
1394 wake_up_all(&q
->done_wq
);
1397 * Reinitialize all buffers for next use.
1399 for (i
= 0; i
< q
->num_buffers
; ++i
)
1400 q
->bufs
[i
]->state
= VB2_BUF_STATE_DEQUEUED
;
1404 * vb2_streamon - start streaming
1405 * @q: videobuf2 queue
1406 * @type: type argument passed from userspace to vidioc_streamon handler
1408 * Should be called from vidioc_streamon handler of a driver.
1410 * 1) verifies current state
1411 * 2) passes any previously queued buffers to the driver and starts streaming
1413 * The return values from this function are intended to be directly returned
1414 * from vidioc_streamon handler in the driver.
1416 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1418 struct vb2_buffer
*vb
;
1422 dprintk(1, "streamon: file io in progress\n");
1426 if (type
!= q
->type
) {
1427 dprintk(1, "streamon: invalid stream type\n");
1432 dprintk(1, "streamon: already streaming\n");
1437 * If any buffers were queued before streamon,
1438 * we can now pass them to driver for processing.
1440 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1441 __enqueue_in_driver(vb
);
1444 * Let driver notice that streaming state has been enabled.
1446 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->queued_count
));
1448 dprintk(1, "streamon: driver refused to start streaming\n");
1449 __vb2_queue_cancel(q
);
1455 dprintk(3, "Streamon successful\n");
1458 EXPORT_SYMBOL_GPL(vb2_streamon
);
1462 * vb2_streamoff - stop streaming
1463 * @q: videobuf2 queue
1464 * @type: type argument passed from userspace to vidioc_streamoff handler
1466 * Should be called from vidioc_streamoff handler of a driver.
1468 * 1) verifies current state,
1469 * 2) stop streaming and dequeues any queued buffers, including those previously
1470 * passed to the driver (after waiting for the driver to finish).
1472 * This call can be used for pausing playback.
1473 * The return values from this function are intended to be directly returned
1474 * from vidioc_streamoff handler in the driver
1476 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1479 dprintk(1, "streamoff: file io in progress\n");
1483 if (type
!= q
->type
) {
1484 dprintk(1, "streamoff: invalid stream type\n");
1488 if (!q
->streaming
) {
1489 dprintk(1, "streamoff: not streaming\n");
1494 * Cancel will pause streaming and remove all buffers from the driver
1495 * and videobuf, effectively returning control over them to userspace.
1497 __vb2_queue_cancel(q
);
1499 dprintk(3, "Streamoff successful\n");
1502 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1505 * __find_plane_by_offset() - find plane associated with the given offset off
1507 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1508 unsigned int *_buffer
, unsigned int *_plane
)
1510 struct vb2_buffer
*vb
;
1511 unsigned int buffer
, plane
;
1514 * Go over all buffers and their planes, comparing the given offset
1515 * with an offset assigned to each plane. If a match is found,
1516 * return its buffer and plane numbers.
1518 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1519 vb
= q
->bufs
[buffer
];
1521 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1522 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1534 * vb2_mmap() - map video buffers into application address space
1535 * @q: videobuf2 queue
1536 * @vma: vma passed to the mmap file operation handler in the driver
1538 * Should be called from mmap file operation handler of a driver.
1539 * This function maps one plane of one of the available video buffers to
1540 * userspace. To map whole video memory allocated on reqbufs, this function
1541 * has to be called once per each plane per each buffer previously allocated.
1543 * When the userspace application calls mmap, it passes to it an offset returned
1544 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1545 * a "cookie", which is then used to identify the plane to be mapped.
1546 * This function finds a plane with a matching offset and a mapping is performed
1547 * by the means of a provided memory operation.
1549 * The return values from this function are intended to be directly returned
1550 * from the mmap handler in driver.
1552 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1554 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1555 struct vb2_buffer
*vb
;
1556 unsigned int buffer
, plane
;
1559 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1560 dprintk(1, "Queue is not currently set up for mmap\n");
1565 * Check memory area access mode.
1567 if (!(vma
->vm_flags
& VM_SHARED
)) {
1568 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1571 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1572 if (!(vma
->vm_flags
& VM_WRITE
)) {
1573 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1577 if (!(vma
->vm_flags
& VM_READ
)) {
1578 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1584 * Find the plane corresponding to the offset passed by userspace.
1586 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1590 vb
= q
->bufs
[buffer
];
1592 ret
= call_memop(q
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
1596 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1599 EXPORT_SYMBOL_GPL(vb2_mmap
);
1602 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
1605 unsigned long pgoff
,
1606 unsigned long flags
)
1608 unsigned long off
= pgoff
<< PAGE_SHIFT
;
1609 struct vb2_buffer
*vb
;
1610 unsigned int buffer
, plane
;
1613 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1614 dprintk(1, "Queue is not currently set up for mmap\n");
1619 * Find the plane corresponding to the offset passed by userspace.
1621 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1625 vb
= q
->bufs
[buffer
];
1627 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
1629 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
1632 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1633 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1636 * vb2_poll() - implements poll userspace operation
1637 * @q: videobuf2 queue
1638 * @file: file argument passed to the poll file operation handler
1639 * @wait: wait argument passed to the poll file operation handler
1641 * This function implements poll file operation handler for a driver.
1642 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1643 * be informed that the file descriptor of a video device is available for
1645 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1646 * will be reported as available for writing.
1648 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1651 * The return values from this function are intended to be directly returned
1652 * from poll handler in driver.
1654 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1656 struct video_device
*vfd
= video_devdata(file
);
1657 unsigned long req_events
= poll_requested_events(wait
);
1658 struct vb2_buffer
*vb
= NULL
;
1659 unsigned int res
= 0;
1660 unsigned long flags
;
1662 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
1663 struct v4l2_fh
*fh
= file
->private_data
;
1665 if (v4l2_event_pending(fh
))
1667 else if (req_events
& POLLPRI
)
1668 poll_wait(file
, &fh
->wait
, wait
);
1672 * Start file I/O emulator only if streaming API has not been used yet.
1674 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1675 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
1676 (req_events
& (POLLIN
| POLLRDNORM
))) {
1677 if (__vb2_init_fileio(q
, 1))
1678 return res
| POLLERR
;
1680 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
1681 (req_events
& (POLLOUT
| POLLWRNORM
))) {
1682 if (__vb2_init_fileio(q
, 0))
1683 return res
| POLLERR
;
1685 * Write to OUTPUT queue can be done immediately.
1687 return res
| POLLOUT
| POLLWRNORM
;
1692 * There is nothing to wait for if no buffers have already been queued.
1694 if (list_empty(&q
->queued_list
))
1695 return res
| POLLERR
;
1697 poll_wait(file
, &q
->done_wq
, wait
);
1700 * Take first buffer available for dequeuing.
1702 spin_lock_irqsave(&q
->done_lock
, flags
);
1703 if (!list_empty(&q
->done_list
))
1704 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
1706 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1708 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
1709 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
1710 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ?
1711 res
| POLLOUT
| POLLWRNORM
:
1712 res
| POLLIN
| POLLRDNORM
;
1716 EXPORT_SYMBOL_GPL(vb2_poll
);
1719 * vb2_queue_init() - initialize a videobuf2 queue
1720 * @q: videobuf2 queue; this structure should be allocated in driver
1722 * The vb2_queue structure should be allocated by the driver. The driver is
1723 * responsible of clearing it's content and setting initial values for some
1724 * required entries before calling this function.
1725 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1726 * to the struct vb2_queue description in include/media/videobuf2-core.h
1727 * for more information.
1729 int vb2_queue_init(struct vb2_queue
*q
)
1733 BUG_ON(!q
->mem_ops
);
1735 BUG_ON(!q
->io_modes
);
1737 BUG_ON(!q
->ops
->queue_setup
);
1738 BUG_ON(!q
->ops
->buf_queue
);
1740 INIT_LIST_HEAD(&q
->queued_list
);
1741 INIT_LIST_HEAD(&q
->done_list
);
1742 spin_lock_init(&q
->done_lock
);
1743 init_waitqueue_head(&q
->done_wq
);
1745 if (q
->buf_struct_size
== 0)
1746 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
1750 EXPORT_SYMBOL_GPL(vb2_queue_init
);
1753 * vb2_queue_release() - stop streaming, release the queue and free memory
1754 * @q: videobuf2 queue
1756 * This function stops streaming and performs necessary clean ups, including
1757 * freeing video buffer memory. The driver is responsible for freeing
1758 * the vb2_queue structure itself.
1760 void vb2_queue_release(struct vb2_queue
*q
)
1762 __vb2_cleanup_fileio(q
);
1763 __vb2_queue_cancel(q
);
1764 __vb2_queue_free(q
, q
->num_buffers
);
1766 EXPORT_SYMBOL_GPL(vb2_queue_release
);
1769 * struct vb2_fileio_buf - buffer context used by file io emulator
1771 * vb2 provides a compatibility layer and emulator of file io (read and
1772 * write) calls on top of streaming API. This structure is used for
1773 * tracking context related to the buffers.
1775 struct vb2_fileio_buf
{
1779 unsigned int queued
:1;
1783 * struct vb2_fileio_data - queue context used by file io emulator
1785 * vb2 provides a compatibility layer and emulator of file io (read and
1786 * write) calls on top of streaming API. For proper operation it required
1787 * this structure to save the driver state between each call of the read
1788 * or write function.
1790 struct vb2_fileio_data
{
1791 struct v4l2_requestbuffers req
;
1792 struct v4l2_buffer b
;
1793 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
1795 unsigned int q_count
;
1796 unsigned int dq_count
;
1801 * __vb2_init_fileio() - initialize file io emulator
1802 * @q: videobuf2 queue
1803 * @read: mode selector (1 means read, 0 means write)
1805 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
1807 struct vb2_fileio_data
*fileio
;
1809 unsigned int count
= 0;
1814 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
1815 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
1819 * Check if device supports mapping buffers to kernel virtual space.
1821 if (!q
->mem_ops
->vaddr
)
1825 * Check if streaming api has not been already activated.
1827 if (q
->streaming
|| q
->num_buffers
> 0)
1831 * Start with count 1, driver can increase it in queue_setup()
1835 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1836 (read
) ? "read" : "write", count
, q
->io_flags
);
1838 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
1842 fileio
->flags
= q
->io_flags
;
1845 * Request buffers and use MMAP type to force driver
1846 * to allocate buffers by itself.
1848 fileio
->req
.count
= count
;
1849 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
1850 fileio
->req
.type
= q
->type
;
1851 ret
= vb2_reqbufs(q
, &fileio
->req
);
1856 * Check if plane_count is correct
1857 * (multiplane buffers are not supported).
1859 if (q
->bufs
[0]->num_planes
!= 1) {
1865 * Get kernel address of each buffer.
1867 for (i
= 0; i
< q
->num_buffers
; i
++) {
1868 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
1869 if (fileio
->bufs
[i
].vaddr
== NULL
)
1871 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
1875 * Read mode requires pre queuing of all buffers.
1879 * Queue all buffers.
1881 for (i
= 0; i
< q
->num_buffers
; i
++) {
1882 struct v4l2_buffer
*b
= &fileio
->b
;
1883 memset(b
, 0, sizeof(*b
));
1885 b
->memory
= q
->memory
;
1887 ret
= vb2_qbuf(q
, b
);
1890 fileio
->bufs
[i
].queued
= 1;
1896 ret
= vb2_streamon(q
, q
->type
);
1906 fileio
->req
.count
= 0;
1907 vb2_reqbufs(q
, &fileio
->req
);
1915 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1916 * @q: videobuf2 queue
1918 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
1920 struct vb2_fileio_data
*fileio
= q
->fileio
;
1924 * Hack fileio context to enable direct calls to vb2 ioctl
1929 vb2_streamoff(q
, q
->type
);
1930 fileio
->req
.count
= 0;
1931 vb2_reqbufs(q
, &fileio
->req
);
1933 dprintk(3, "file io emulator closed\n");
1939 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1940 * @q: videobuf2 queue
1941 * @data: pointed to target userspace buffer
1942 * @count: number of bytes to read or write
1943 * @ppos: file handle position tracking pointer
1944 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1945 * @read: access mode selector (1 means read, 0 means write)
1947 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1948 loff_t
*ppos
, int nonblock
, int read
)
1950 struct vb2_fileio_data
*fileio
;
1951 struct vb2_fileio_buf
*buf
;
1954 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1955 read
? "read" : "write", (long)*ppos
, count
,
1956 nonblock
? "non" : "");
1962 * Initialize emulator on first call.
1965 ret
= __vb2_init_fileio(q
, read
);
1966 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
1973 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1974 * The pointer will be restored before returning from this function.
1978 index
= fileio
->index
;
1979 buf
= &fileio
->bufs
[index
];
1982 * Check if we need to dequeue the buffer.
1985 struct vb2_buffer
*vb
;
1988 * Call vb2_dqbuf to get buffer back.
1990 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
1991 fileio
->b
.type
= q
->type
;
1992 fileio
->b
.memory
= q
->memory
;
1993 fileio
->b
.index
= index
;
1994 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
1995 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
1998 fileio
->dq_count
+= 1;
2001 * Get number of bytes filled by the driver
2003 vb
= q
->bufs
[index
];
2004 buf
->size
= vb2_get_plane_payload(vb
, 0);
2009 * Limit count on last few bytes of the buffer.
2011 if (buf
->pos
+ count
> buf
->size
) {
2012 count
= buf
->size
- buf
->pos
;
2013 dprintk(5, "reducing read count: %zd\n", count
);
2017 * Transfer data to userspace.
2019 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2020 count
, index
, buf
->pos
);
2022 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2024 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2026 dprintk(3, "file io: error copying data\n");
2038 * Queue next buffer if required.
2040 if (buf
->pos
== buf
->size
||
2041 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2043 * Check if this is the last buffer to read.
2045 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2046 fileio
->dq_count
== 1) {
2047 dprintk(3, "file io: read limit reached\n");
2049 * Restore fileio pointer and release the context.
2052 return __vb2_cleanup_fileio(q
);
2056 * Call vb2_qbuf and give buffer to the driver.
2058 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2059 fileio
->b
.type
= q
->type
;
2060 fileio
->b
.memory
= q
->memory
;
2061 fileio
->b
.index
= index
;
2062 fileio
->b
.bytesused
= buf
->pos
;
2063 ret
= vb2_qbuf(q
, &fileio
->b
);
2064 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2069 * Buffer has been queued, update the status
2073 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
2074 fileio
->q_count
+= 1;
2077 * Switch to the next buffer
2079 fileio
->index
= (index
+ 1) % q
->num_buffers
;
2082 * Start streaming if required.
2084 if (!read
&& !q
->streaming
) {
2085 ret
= vb2_streamon(q
, q
->type
);
2092 * Return proper number of bytes processed.
2098 * Restore the fileio context and block vb2 ioctl interface.
2104 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2105 loff_t
*ppos
, int nonblocking
)
2107 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2109 EXPORT_SYMBOL_GPL(vb2_read
);
2111 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2112 loff_t
*ppos
, int nonblocking
)
2114 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
2116 EXPORT_SYMBOL_GPL(vb2_write
);
2118 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2119 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2120 MODULE_LICENSE("GPL");