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, 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
, 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
, put
, vb
->planes
[plane
- 1].mem_priv
);
70 vb
->planes
[plane
- 1].mem_priv
= NULL
;
77 * __vb2_buf_mem_free() - free memory of the given buffer
79 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
81 struct vb2_queue
*q
= vb
->vb2_queue
;
84 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
85 call_memop(q
, put
, vb
->planes
[plane
].mem_priv
);
86 vb
->planes
[plane
].mem_priv
= NULL
;
87 dprintk(3, "Freed plane %d of buffer %d\n", plane
,
93 * __vb2_buf_userptr_put() - release userspace memory associated with
96 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
98 struct vb2_queue
*q
= vb
->vb2_queue
;
101 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
102 if (vb
->planes
[plane
].mem_priv
)
103 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
104 vb
->planes
[plane
].mem_priv
= NULL
;
109 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
110 * every buffer on the queue
112 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
114 unsigned int buffer
, plane
;
115 struct vb2_buffer
*vb
;
118 if (q
->num_buffers
) {
119 struct v4l2_plane
*p
;
120 vb
= q
->bufs
[q
->num_buffers
- 1];
121 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
122 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
127 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
128 vb
= q
->bufs
[buffer
];
132 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
133 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
134 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
136 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
139 off
+= vb
->v4l2_planes
[plane
].length
;
140 off
= PAGE_ALIGN(off
);
146 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
147 * video buffer memory for all buffers/planes on the queue and initializes the
150 * Returns the number of buffers successfully allocated.
152 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
153 unsigned int num_buffers
, unsigned int num_planes
)
156 struct vb2_buffer
*vb
;
159 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
160 /* Allocate videobuf buffer structures */
161 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
163 dprintk(1, "Memory alloc for buffer struct failed\n");
167 /* Length stores number of planes for multiplanar buffers */
168 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
169 vb
->v4l2_buf
.length
= num_planes
;
171 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
173 vb
->num_planes
= num_planes
;
174 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
175 vb
->v4l2_buf
.type
= q
->type
;
176 vb
->v4l2_buf
.memory
= memory
;
178 /* Allocate video buffer memory for the MMAP type */
179 if (memory
== V4L2_MEMORY_MMAP
) {
180 ret
= __vb2_buf_mem_alloc(vb
);
182 dprintk(1, "Failed allocating memory for "
183 "buffer %d\n", buffer
);
188 * Call the driver-provided buffer initialization
189 * callback, if given. An error in initialization
190 * results in queue setup failure.
192 ret
= call_qop(q
, buf_init
, vb
);
194 dprintk(1, "Buffer %d %p initialization"
195 " failed\n", buffer
, vb
);
196 __vb2_buf_mem_free(vb
);
202 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
205 __setup_offsets(q
, buffer
);
207 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
214 * __vb2_free_mem() - release all video buffer memory for a given queue
216 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
219 struct vb2_buffer
*vb
;
221 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
223 vb
= q
->bufs
[buffer
];
227 /* Free MMAP buffers or release USERPTR buffers */
228 if (q
->memory
== V4L2_MEMORY_MMAP
)
229 __vb2_buf_mem_free(vb
);
231 __vb2_buf_userptr_put(vb
);
236 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
237 * related information, if no buffers are left return the queue to an
238 * uninitialized state. Might be called even if the queue has already been freed.
240 static void __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
244 /* Call driver-provided cleanup function for each buffer, if provided */
245 if (q
->ops
->buf_cleanup
) {
246 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
248 if (NULL
== q
->bufs
[buffer
])
250 q
->ops
->buf_cleanup(q
->bufs
[buffer
]);
254 /* Release video buffer memory */
255 __vb2_free_mem(q
, buffers
);
257 /* Free videobuf buffers */
258 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
260 kfree(q
->bufs
[buffer
]);
261 q
->bufs
[buffer
] = NULL
;
264 q
->num_buffers
-= buffers
;
267 INIT_LIST_HEAD(&q
->queued_list
);
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
, 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
|| !vb
->planes
[plane_no
].mem_priv
)
736 return call_memop(q
, 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
|| !vb
->planes
[plane_no
].mem_priv
)
759 return call_memop(q
, 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
&&
886 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
887 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
890 dprintk(3, "qbuf: userspace address for plane %d changed, "
891 "reacquiring memory\n", plane
);
893 /* Check if the provided plane buffer is large enough */
894 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
899 /* Release previously acquired memory if present */
900 if (vb
->planes
[plane
].mem_priv
)
901 call_memop(q
, put_userptr
, 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 mem_priv
= call_memop(q
, get_userptr
, q
->alloc_ctx
[plane
],
909 planes
[plane
].m
.userptr
,
910 planes
[plane
].length
, write
);
911 if (IS_ERR_OR_NULL(mem_priv
)) {
912 dprintk(1, "qbuf: failed acquiring userspace "
913 "memory for plane %d\n", plane
);
914 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
917 vb
->planes
[plane
].mem_priv
= mem_priv
;
921 * Call driver-specific initialization on the newly acquired buffer,
924 ret
= call_qop(q
, buf_init
, vb
);
926 dprintk(1, "qbuf: buffer initialization failed\n");
931 * Now that everything is in order, copy relevant information
932 * provided by userspace.
934 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
935 vb
->v4l2_planes
[plane
] = planes
[plane
];
939 /* In case of errors, release planes that were already acquired */
940 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
941 if (vb
->planes
[plane
].mem_priv
)
942 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
943 vb
->planes
[plane
].mem_priv
= NULL
;
944 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
945 vb
->v4l2_planes
[plane
].length
= 0;
952 * __qbuf_mmap() - handle qbuf of an MMAP buffer
954 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
956 return __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
960 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
962 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
964 struct vb2_queue
*q
= vb
->vb2_queue
;
966 vb
->state
= VB2_BUF_STATE_ACTIVE
;
967 atomic_inc(&q
->queued_count
);
968 q
->ops
->buf_queue(vb
);
971 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
973 struct vb2_queue
*q
= vb
->vb2_queue
;
977 case V4L2_MEMORY_MMAP
:
978 ret
= __qbuf_mmap(vb
, b
);
980 case V4L2_MEMORY_USERPTR
:
981 ret
= __qbuf_userptr(vb
, b
);
984 WARN(1, "Invalid queue type\n");
989 ret
= call_qop(q
, buf_prepare
, vb
);
991 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
993 vb
->state
= VB2_BUF_STATE_PREPARED
;
999 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1000 * @q: videobuf2 queue
1001 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1004 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1006 * 1) verifies the passed buffer,
1007 * 2) calls buf_prepare callback in the driver (if provided), in which
1008 * driver-specific buffer initialization can be performed,
1010 * The return values from this function are intended to be directly returned
1011 * from vidioc_prepare_buf handler in driver.
1013 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1015 struct vb2_buffer
*vb
;
1019 dprintk(1, "%s(): file io in progress\n", __func__
);
1023 if (b
->type
!= q
->type
) {
1024 dprintk(1, "%s(): invalid buffer type\n", __func__
);
1028 if (b
->index
>= q
->num_buffers
) {
1029 dprintk(1, "%s(): buffer index out of range\n", __func__
);
1033 vb
= q
->bufs
[b
->index
];
1035 /* Should never happen */
1036 dprintk(1, "%s(): buffer is NULL\n", __func__
);
1040 if (b
->memory
!= q
->memory
) {
1041 dprintk(1, "%s(): invalid memory type\n", __func__
);
1045 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1046 dprintk(1, "%s(): invalid buffer state %d\n", __func__
, vb
->state
);
1050 ret
= __buf_prepare(vb
, b
);
1054 __fill_v4l2_buffer(vb
, b
);
1058 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1061 * vb2_qbuf() - Queue a buffer from userspace
1062 * @q: videobuf2 queue
1063 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1066 * Should be called from vidioc_qbuf ioctl handler of a driver.
1068 * 1) verifies the passed buffer,
1069 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1070 * which driver-specific buffer initialization can be performed,
1071 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1072 * callback for processing.
1074 * The return values from this function are intended to be directly returned
1075 * from vidioc_qbuf handler in driver.
1077 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1079 struct rw_semaphore
*mmap_sem
= NULL
;
1080 struct vb2_buffer
*vb
;
1084 * In case of user pointer buffers vb2 allocator needs to get direct
1085 * access to userspace pages. This requires getting read access on
1086 * mmap semaphore in the current process structure. The same
1087 * semaphore is taken before calling mmap operation, while both mmap
1088 * and qbuf are called by the driver or v4l2 core with driver's lock
1089 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1090 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1091 * release driver's lock, takes mmap_sem and then takes again driver's
1094 * To avoid race with other vb2 calls, which might be called after
1095 * releasing driver's lock, this operation is performed at the
1096 * beggining of qbuf processing. This way the queue status is
1097 * consistent after getting driver's lock back.
1099 if (q
->memory
== V4L2_MEMORY_USERPTR
) {
1100 mmap_sem
= ¤t
->mm
->mmap_sem
;
1101 call_qop(q
, wait_prepare
, q
);
1102 down_read(mmap_sem
);
1103 call_qop(q
, wait_finish
, q
);
1107 dprintk(1, "qbuf: file io in progress\n");
1112 if (b
->type
!= q
->type
) {
1113 dprintk(1, "qbuf: invalid buffer type\n");
1118 if (b
->index
>= q
->num_buffers
) {
1119 dprintk(1, "qbuf: buffer index out of range\n");
1124 vb
= q
->bufs
[b
->index
];
1126 /* Should never happen */
1127 dprintk(1, "qbuf: buffer is NULL\n");
1132 if (b
->memory
!= q
->memory
) {
1133 dprintk(1, "qbuf: invalid memory type\n");
1138 switch (vb
->state
) {
1139 case VB2_BUF_STATE_DEQUEUED
:
1140 ret
= __buf_prepare(vb
, b
);
1143 case VB2_BUF_STATE_PREPARED
:
1146 dprintk(1, "qbuf: buffer already in use\n");
1152 * Add to the queued buffers list, a buffer will stay on it until
1153 * dequeued in dqbuf.
1155 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1156 vb
->state
= VB2_BUF_STATE_QUEUED
;
1159 * If already streaming, give the buffer to driver for processing.
1160 * If not, the buffer will be given to driver on next streamon.
1163 __enqueue_in_driver(vb
);
1165 /* Fill buffer information for the userspace */
1166 __fill_v4l2_buffer(vb
, b
);
1168 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
1174 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1177 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1180 * Will sleep if required for nonblocking == false.
1182 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1185 * All operations on vb_done_list are performed under done_lock
1186 * spinlock protection. However, buffers may be removed from
1187 * it and returned to userspace only while holding both driver's
1188 * lock and the done_lock spinlock. Thus we can be sure that as
1189 * long as we hold the driver's lock, the list will remain not
1190 * empty if list_empty() check succeeds.
1196 if (!q
->streaming
) {
1197 dprintk(1, "Streaming off, will not wait for buffers\n");
1201 if (!list_empty(&q
->done_list
)) {
1203 * Found a buffer that we were waiting for.
1209 dprintk(1, "Nonblocking and no buffers to dequeue, "
1215 * We are streaming and blocking, wait for another buffer to
1216 * become ready or for streamoff. Driver's lock is released to
1217 * allow streamoff or qbuf to be called while waiting.
1219 call_qop(q
, wait_prepare
, q
);
1222 * All locks have been released, it is safe to sleep now.
1224 dprintk(3, "Will sleep waiting for buffers\n");
1225 ret
= wait_event_interruptible(q
->done_wq
,
1226 !list_empty(&q
->done_list
) || !q
->streaming
);
1229 * We need to reevaluate both conditions again after reacquiring
1230 * the locks or return an error if one occurred.
1232 call_qop(q
, wait_finish
, q
);
1240 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1242 * Will sleep if required for nonblocking == false.
1244 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1247 unsigned long flags
;
1251 * Wait for at least one buffer to become available on the done_list.
1253 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1258 * Driver's lock has been held since we last verified that done_list
1259 * is not empty, so no need for another list_empty(done_list) check.
1261 spin_lock_irqsave(&q
->done_lock
, flags
);
1262 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1263 list_del(&(*vb
)->done_entry
);
1264 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1270 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1271 * @q: videobuf2 queue
1273 * This function will wait until all buffers that have been given to the driver
1274 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1275 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1276 * taken, for example from stop_streaming() callback.
1278 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1280 if (!q
->streaming
) {
1281 dprintk(1, "Streaming off, will not wait for buffers\n");
1285 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1288 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1291 * vb2_dqbuf() - Dequeue a buffer to the userspace
1292 * @q: videobuf2 queue
1293 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1295 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1296 * buffers ready for dequeuing are present. Normally the driver
1297 * would be passing (file->f_flags & O_NONBLOCK) here
1299 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1301 * 1) verifies the passed buffer,
1302 * 2) calls buf_finish callback in the driver (if provided), in which
1303 * driver can perform any additional operations that may be required before
1304 * returning the buffer to userspace, such as cache sync,
1305 * 3) the buffer struct members are filled with relevant information for
1308 * The return values from this function are intended to be directly returned
1309 * from vidioc_dqbuf handler in driver.
1311 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1313 struct vb2_buffer
*vb
= NULL
;
1317 dprintk(1, "dqbuf: file io in progress\n");
1321 if (b
->type
!= q
->type
) {
1322 dprintk(1, "dqbuf: invalid buffer type\n");
1326 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1328 dprintk(1, "dqbuf: error getting next done buffer\n");
1332 ret
= call_qop(q
, buf_finish
, vb
);
1334 dprintk(1, "dqbuf: buffer finish failed\n");
1338 switch (vb
->state
) {
1339 case VB2_BUF_STATE_DONE
:
1340 dprintk(3, "dqbuf: Returning done buffer\n");
1342 case VB2_BUF_STATE_ERROR
:
1343 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1346 dprintk(1, "dqbuf: Invalid buffer state\n");
1350 /* Fill buffer information for the userspace */
1351 __fill_v4l2_buffer(vb
, b
);
1352 /* Remove from videobuf queue */
1353 list_del(&vb
->queued_entry
);
1355 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1356 vb
->v4l2_buf
.index
, vb
->state
);
1358 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1361 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1364 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1366 * Removes all queued buffers from driver's queue and all buffers queued by
1367 * userspace from videobuf's queue. Returns to state after reqbufs.
1369 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1374 * Tell driver to stop all transactions and release all queued
1378 call_qop(q
, stop_streaming
, q
);
1382 * Remove all buffers from videobuf's list...
1384 INIT_LIST_HEAD(&q
->queued_list
);
1386 * ...and done list; userspace will not receive any buffers it
1387 * has not already dequeued before initiating cancel.
1389 INIT_LIST_HEAD(&q
->done_list
);
1390 atomic_set(&q
->queued_count
, 0);
1391 wake_up_all(&q
->done_wq
);
1394 * Reinitialize all buffers for next use.
1396 for (i
= 0; i
< q
->num_buffers
; ++i
)
1397 q
->bufs
[i
]->state
= VB2_BUF_STATE_DEQUEUED
;
1401 * vb2_streamon - start streaming
1402 * @q: videobuf2 queue
1403 * @type: type argument passed from userspace to vidioc_streamon handler
1405 * Should be called from vidioc_streamon handler of a driver.
1407 * 1) verifies current state
1408 * 2) passes any previously queued buffers to the driver and starts streaming
1410 * The return values from this function are intended to be directly returned
1411 * from vidioc_streamon handler in the driver.
1413 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1415 struct vb2_buffer
*vb
;
1419 dprintk(1, "streamon: file io in progress\n");
1423 if (type
!= q
->type
) {
1424 dprintk(1, "streamon: invalid stream type\n");
1429 dprintk(1, "streamon: already streaming\n");
1434 * If any buffers were queued before streamon,
1435 * we can now pass them to driver for processing.
1437 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1438 __enqueue_in_driver(vb
);
1441 * Let driver notice that streaming state has been enabled.
1443 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->queued_count
));
1445 dprintk(1, "streamon: driver refused to start streaming\n");
1446 __vb2_queue_cancel(q
);
1452 dprintk(3, "Streamon successful\n");
1455 EXPORT_SYMBOL_GPL(vb2_streamon
);
1459 * vb2_streamoff - stop streaming
1460 * @q: videobuf2 queue
1461 * @type: type argument passed from userspace to vidioc_streamoff handler
1463 * Should be called from vidioc_streamoff handler of a driver.
1465 * 1) verifies current state,
1466 * 2) stop streaming and dequeues any queued buffers, including those previously
1467 * passed to the driver (after waiting for the driver to finish).
1469 * This call can be used for pausing playback.
1470 * The return values from this function are intended to be directly returned
1471 * from vidioc_streamoff handler in the driver
1473 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1476 dprintk(1, "streamoff: file io in progress\n");
1480 if (type
!= q
->type
) {
1481 dprintk(1, "streamoff: invalid stream type\n");
1485 if (!q
->streaming
) {
1486 dprintk(1, "streamoff: not streaming\n");
1491 * Cancel will pause streaming and remove all buffers from the driver
1492 * and videobuf, effectively returning control over them to userspace.
1494 __vb2_queue_cancel(q
);
1496 dprintk(3, "Streamoff successful\n");
1499 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1502 * __find_plane_by_offset() - find plane associated with the given offset off
1504 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1505 unsigned int *_buffer
, unsigned int *_plane
)
1507 struct vb2_buffer
*vb
;
1508 unsigned int buffer
, plane
;
1511 * Go over all buffers and their planes, comparing the given offset
1512 * with an offset assigned to each plane. If a match is found,
1513 * return its buffer and plane numbers.
1515 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1516 vb
= q
->bufs
[buffer
];
1518 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1519 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1531 * vb2_mmap() - map video buffers into application address space
1532 * @q: videobuf2 queue
1533 * @vma: vma passed to the mmap file operation handler in the driver
1535 * Should be called from mmap file operation handler of a driver.
1536 * This function maps one plane of one of the available video buffers to
1537 * userspace. To map whole video memory allocated on reqbufs, this function
1538 * has to be called once per each plane per each buffer previously allocated.
1540 * When the userspace application calls mmap, it passes to it an offset returned
1541 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1542 * a "cookie", which is then used to identify the plane to be mapped.
1543 * This function finds a plane with a matching offset and a mapping is performed
1544 * by the means of a provided memory operation.
1546 * The return values from this function are intended to be directly returned
1547 * from the mmap handler in driver.
1549 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1551 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1552 struct vb2_buffer
*vb
;
1553 unsigned int buffer
, plane
;
1556 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1557 dprintk(1, "Queue is not currently set up for mmap\n");
1562 * Check memory area access mode.
1564 if (!(vma
->vm_flags
& VM_SHARED
)) {
1565 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1568 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1569 if (!(vma
->vm_flags
& VM_WRITE
)) {
1570 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1574 if (!(vma
->vm_flags
& VM_READ
)) {
1575 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1581 * Find the plane corresponding to the offset passed by userspace.
1583 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1587 vb
= q
->bufs
[buffer
];
1589 ret
= call_memop(q
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
1593 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1596 EXPORT_SYMBOL_GPL(vb2_mmap
);
1599 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
1602 unsigned long pgoff
,
1603 unsigned long flags
)
1605 unsigned long off
= pgoff
<< PAGE_SHIFT
;
1606 struct vb2_buffer
*vb
;
1607 unsigned int buffer
, plane
;
1610 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1611 dprintk(1, "Queue is not currently set up for mmap\n");
1616 * Find the plane corresponding to the offset passed by userspace.
1618 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1622 vb
= q
->bufs
[buffer
];
1624 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
1626 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
1629 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1630 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1633 * vb2_poll() - implements poll userspace operation
1634 * @q: videobuf2 queue
1635 * @file: file argument passed to the poll file operation handler
1636 * @wait: wait argument passed to the poll file operation handler
1638 * This function implements poll file operation handler for a driver.
1639 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1640 * be informed that the file descriptor of a video device is available for
1642 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1643 * will be reported as available for writing.
1645 * The return values from this function are intended to be directly returned
1646 * from poll handler in driver.
1648 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1650 unsigned long flags
;
1652 struct vb2_buffer
*vb
= NULL
;
1655 * Start file I/O emulator only if streaming API has not been used yet.
1657 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1658 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
)) {
1659 ret
= __vb2_init_fileio(q
, 1);
1663 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
)) {
1664 ret
= __vb2_init_fileio(q
, 0);
1668 * Write to OUTPUT queue can be done immediately.
1670 return POLLOUT
| POLLWRNORM
;
1675 * There is nothing to wait for if no buffers have already been queued.
1677 if (list_empty(&q
->queued_list
))
1680 poll_wait(file
, &q
->done_wq
, wait
);
1683 * Take first buffer available for dequeuing.
1685 spin_lock_irqsave(&q
->done_lock
, flags
);
1686 if (!list_empty(&q
->done_list
))
1687 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
1689 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1691 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
1692 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
1693 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ? POLLOUT
| POLLWRNORM
:
1694 POLLIN
| POLLRDNORM
;
1698 EXPORT_SYMBOL_GPL(vb2_poll
);
1701 * vb2_queue_init() - initialize a videobuf2 queue
1702 * @q: videobuf2 queue; this structure should be allocated in driver
1704 * The vb2_queue structure should be allocated by the driver. The driver is
1705 * responsible of clearing it's content and setting initial values for some
1706 * required entries before calling this function.
1707 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1708 * to the struct vb2_queue description in include/media/videobuf2-core.h
1709 * for more information.
1711 int vb2_queue_init(struct vb2_queue
*q
)
1715 BUG_ON(!q
->mem_ops
);
1717 BUG_ON(!q
->io_modes
);
1719 BUG_ON(!q
->ops
->queue_setup
);
1720 BUG_ON(!q
->ops
->buf_queue
);
1722 INIT_LIST_HEAD(&q
->queued_list
);
1723 INIT_LIST_HEAD(&q
->done_list
);
1724 spin_lock_init(&q
->done_lock
);
1725 init_waitqueue_head(&q
->done_wq
);
1727 if (q
->buf_struct_size
== 0)
1728 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
1732 EXPORT_SYMBOL_GPL(vb2_queue_init
);
1735 * vb2_queue_release() - stop streaming, release the queue and free memory
1736 * @q: videobuf2 queue
1738 * This function stops streaming and performs necessary clean ups, including
1739 * freeing video buffer memory. The driver is responsible for freeing
1740 * the vb2_queue structure itself.
1742 void vb2_queue_release(struct vb2_queue
*q
)
1744 __vb2_cleanup_fileio(q
);
1745 __vb2_queue_cancel(q
);
1746 __vb2_queue_free(q
, q
->num_buffers
);
1748 EXPORT_SYMBOL_GPL(vb2_queue_release
);
1751 * struct vb2_fileio_buf - buffer context used by file io emulator
1753 * vb2 provides a compatibility layer and emulator of file io (read and
1754 * write) calls on top of streaming API. This structure is used for
1755 * tracking context related to the buffers.
1757 struct vb2_fileio_buf
{
1761 unsigned int queued
:1;
1765 * struct vb2_fileio_data - queue context used by file io emulator
1767 * vb2 provides a compatibility layer and emulator of file io (read and
1768 * write) calls on top of streaming API. For proper operation it required
1769 * this structure to save the driver state between each call of the read
1770 * or write function.
1772 struct vb2_fileio_data
{
1773 struct v4l2_requestbuffers req
;
1774 struct v4l2_buffer b
;
1775 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
1777 unsigned int q_count
;
1778 unsigned int dq_count
;
1783 * __vb2_init_fileio() - initialize file io emulator
1784 * @q: videobuf2 queue
1785 * @read: mode selector (1 means read, 0 means write)
1787 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
1789 struct vb2_fileio_data
*fileio
;
1791 unsigned int count
= 0;
1796 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
1797 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
1801 * Check if device supports mapping buffers to kernel virtual space.
1803 if (!q
->mem_ops
->vaddr
)
1807 * Check if streaming api has not been already activated.
1809 if (q
->streaming
|| q
->num_buffers
> 0)
1813 * Start with count 1, driver can increase it in queue_setup()
1817 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1818 (read
) ? "read" : "write", count
, q
->io_flags
);
1820 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
1824 fileio
->flags
= q
->io_flags
;
1827 * Request buffers and use MMAP type to force driver
1828 * to allocate buffers by itself.
1830 fileio
->req
.count
= count
;
1831 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
1832 fileio
->req
.type
= q
->type
;
1833 ret
= vb2_reqbufs(q
, &fileio
->req
);
1838 * Check if plane_count is correct
1839 * (multiplane buffers are not supported).
1841 if (q
->bufs
[0]->num_planes
!= 1) {
1842 fileio
->req
.count
= 0;
1848 * Get kernel address of each buffer.
1850 for (i
= 0; i
< q
->num_buffers
; i
++) {
1851 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
1852 if (fileio
->bufs
[i
].vaddr
== NULL
)
1854 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
1858 * Read mode requires pre queuing of all buffers.
1862 * Queue all buffers.
1864 for (i
= 0; i
< q
->num_buffers
; i
++) {
1865 struct v4l2_buffer
*b
= &fileio
->b
;
1866 memset(b
, 0, sizeof(*b
));
1868 b
->memory
= q
->memory
;
1870 ret
= vb2_qbuf(q
, b
);
1873 fileio
->bufs
[i
].queued
= 1;
1879 ret
= vb2_streamon(q
, q
->type
);
1889 vb2_reqbufs(q
, &fileio
->req
);
1897 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1898 * @q: videobuf2 queue
1900 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
1902 struct vb2_fileio_data
*fileio
= q
->fileio
;
1906 * Hack fileio context to enable direct calls to vb2 ioctl
1911 vb2_streamoff(q
, q
->type
);
1912 fileio
->req
.count
= 0;
1913 vb2_reqbufs(q
, &fileio
->req
);
1915 dprintk(3, "file io emulator closed\n");
1921 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1922 * @q: videobuf2 queue
1923 * @data: pointed to target userspace buffer
1924 * @count: number of bytes to read or write
1925 * @ppos: file handle position tracking pointer
1926 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1927 * @read: access mode selector (1 means read, 0 means write)
1929 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1930 loff_t
*ppos
, int nonblock
, int read
)
1932 struct vb2_fileio_data
*fileio
;
1933 struct vb2_fileio_buf
*buf
;
1936 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1937 read
? "read" : "write", (long)*ppos
, count
,
1938 nonblock
? "non" : "");
1944 * Initialize emulator on first call.
1947 ret
= __vb2_init_fileio(q
, read
);
1948 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
1955 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1956 * The pointer will be restored before returning from this function.
1960 index
= fileio
->index
;
1961 buf
= &fileio
->bufs
[index
];
1964 * Check if we need to dequeue the buffer.
1967 struct vb2_buffer
*vb
;
1970 * Call vb2_dqbuf to get buffer back.
1972 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
1973 fileio
->b
.type
= q
->type
;
1974 fileio
->b
.memory
= q
->memory
;
1975 fileio
->b
.index
= index
;
1976 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
1977 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
1980 fileio
->dq_count
+= 1;
1983 * Get number of bytes filled by the driver
1985 vb
= q
->bufs
[index
];
1986 buf
->size
= vb2_get_plane_payload(vb
, 0);
1991 * Limit count on last few bytes of the buffer.
1993 if (buf
->pos
+ count
> buf
->size
) {
1994 count
= buf
->size
- buf
->pos
;
1995 dprintk(5, "reducing read count: %zd\n", count
);
1999 * Transfer data to userspace.
2001 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2002 count
, index
, buf
->pos
);
2004 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2006 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2008 dprintk(3, "file io: error copying data\n");
2020 * Queue next buffer if required.
2022 if (buf
->pos
== buf
->size
||
2023 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2025 * Check if this is the last buffer to read.
2027 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2028 fileio
->dq_count
== 1) {
2029 dprintk(3, "file io: read limit reached\n");
2031 * Restore fileio pointer and release the context.
2034 return __vb2_cleanup_fileio(q
);
2038 * Call vb2_qbuf and give buffer to the driver.
2040 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2041 fileio
->b
.type
= q
->type
;
2042 fileio
->b
.memory
= q
->memory
;
2043 fileio
->b
.index
= index
;
2044 fileio
->b
.bytesused
= buf
->pos
;
2045 ret
= vb2_qbuf(q
, &fileio
->b
);
2046 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2051 * Buffer has been queued, update the status
2055 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
2056 fileio
->q_count
+= 1;
2059 * Switch to the next buffer
2061 fileio
->index
= (index
+ 1) % q
->num_buffers
;
2064 * Start streaming if required.
2066 if (!read
&& !q
->streaming
) {
2067 ret
= vb2_streamon(q
, q
->type
);
2074 * Return proper number of bytes processed.
2080 * Restore the fileio context and block vb2 ioctl interface.
2086 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2087 loff_t
*ppos
, int nonblocking
)
2089 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2091 EXPORT_SYMBOL_GPL(vb2_read
);
2093 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2094 loff_t
*ppos
, int nonblocking
)
2096 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
2098 EXPORT_SYMBOL_GPL(vb2_write
);
2100 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2101 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2102 MODULE_LICENSE("GPL");