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, etc. */
340 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
341 b
->reserved2
= vb
->v4l2_buf
.reserved2
;
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 * __verify_memory_type() - Check whether the memory type and buffer type
458 * passed to a buffer operation are compatible with the queue.
460 static int __verify_memory_type(struct vb2_queue
*q
,
461 enum v4l2_memory memory
, enum v4l2_buf_type type
)
463 if (memory
!= V4L2_MEMORY_MMAP
&& memory
!= V4L2_MEMORY_USERPTR
) {
464 dprintk(1, "reqbufs: unsupported memory type\n");
468 if (type
!= q
->type
) {
469 dprintk(1, "reqbufs: requested type is incorrect\n");
474 * Make sure all the required memory ops for given memory type
477 if (memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
478 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
482 if (memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
483 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
488 * Place the busy tests at the end: -EBUSY can be ignored when
489 * create_bufs is called with count == 0, but count == 0 should still
490 * do the memory and type validation.
493 dprintk(1, "reqbufs: file io in progress\n");
500 * __reqbufs() - Initiate streaming
501 * @q: videobuf2 queue
502 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
504 * Should be called from vidioc_reqbufs ioctl handler of a driver.
506 * 1) verifies streaming parameters passed from the userspace,
507 * 2) sets up the queue,
508 * 3) negotiates number of buffers and planes per buffer with the driver
509 * to be used during streaming,
510 * 4) allocates internal buffer structures (struct vb2_buffer), according to
511 * the agreed parameters,
512 * 5) for MMAP memory type, allocates actual video memory, using the
513 * memory handling/allocation routines provided during queue initialization
515 * If req->count is 0, all the memory will be freed instead.
516 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
517 * and the queue is not busy, memory will be reallocated.
519 * The return values from this function are intended to be directly returned
520 * from vidioc_reqbufs handler in driver.
522 static int __reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
524 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
528 dprintk(1, "reqbufs: streaming active\n");
532 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
534 * We already have buffers allocated, so first check if they
535 * are not in use and can be freed.
537 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
538 dprintk(1, "reqbufs: memory in use, cannot free\n");
542 __vb2_queue_free(q
, q
->num_buffers
);
545 * In case of REQBUFS(0) return immediately without calling
546 * driver's queue_setup() callback and allocating resources.
553 * Make sure the requested values and current defaults are sane.
555 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
556 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
557 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
558 q
->memory
= req
->memory
;
561 * Ask the driver how many buffers and planes per buffer it requires.
562 * Driver also sets the size and allocator context for each plane.
564 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
565 q
->plane_sizes
, q
->alloc_ctx
);
569 /* Finally, allocate buffers and video memory */
570 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
572 dprintk(1, "Memory allocation failed\n");
576 allocated_buffers
= ret
;
579 * Check if driver can handle the allocated number of buffers.
581 if (allocated_buffers
< num_buffers
) {
582 num_buffers
= allocated_buffers
;
584 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
585 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
587 if (!ret
&& allocated_buffers
< num_buffers
)
591 * Either the driver has accepted a smaller number of buffers,
592 * or .queue_setup() returned an error
596 q
->num_buffers
= allocated_buffers
;
599 __vb2_queue_free(q
, allocated_buffers
);
604 * Return the number of successfully allocated buffers
607 req
->count
= allocated_buffers
;
613 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
615 * @q: videobuf2 queue
616 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
618 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
620 int ret
= __verify_memory_type(q
, req
->memory
, req
->type
);
622 return ret
? ret
: __reqbufs(q
, req
);
624 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
627 * __create_bufs() - Allocate buffers and any required auxiliary structs
628 * @q: videobuf2 queue
629 * @create: creation parameters, passed from userspace to vidioc_create_bufs
632 * Should be called from vidioc_create_bufs ioctl handler of a driver.
634 * 1) verifies parameter sanity
635 * 2) calls the .queue_setup() queue operation
636 * 3) performs any necessary memory allocations
638 * The return values from this function are intended to be directly returned
639 * from vidioc_create_bufs handler in driver.
641 static int __create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
643 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
646 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
647 dprintk(1, "%s(): maximum number of buffers already allocated\n",
652 if (!q
->num_buffers
) {
653 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
654 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
655 q
->memory
= create
->memory
;
658 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
661 * Ask the driver, whether the requested number of buffers, planes per
662 * buffer and their sizes are acceptable
664 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
665 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
669 /* Finally, allocate buffers and video memory */
670 ret
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
673 dprintk(1, "Memory allocation failed\n");
677 allocated_buffers
= ret
;
680 * Check if driver can handle the so far allocated number of buffers.
682 if (ret
< num_buffers
) {
686 * q->num_buffers contains the total number of buffers, that the
687 * queue driver has set up
689 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
690 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
692 if (!ret
&& allocated_buffers
< num_buffers
)
696 * Either the driver has accepted a smaller number of buffers,
697 * or .queue_setup() returned an error
701 q
->num_buffers
+= allocated_buffers
;
704 __vb2_queue_free(q
, allocated_buffers
);
709 * Return the number of successfully allocated buffers
712 create
->count
= allocated_buffers
;
718 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
720 * @q: videobuf2 queue
721 * @create: creation parameters, passed from userspace to vidioc_create_bufs
724 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
726 int ret
= __verify_memory_type(q
, create
->memory
, create
->format
.type
);
728 create
->index
= q
->num_buffers
;
729 if (create
->count
== 0)
730 return ret
!= -EBUSY
? ret
: 0;
731 return ret
? ret
: __create_bufs(q
, create
);
733 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
736 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
737 * @vb: vb2_buffer to which the plane in question belongs to
738 * @plane_no: plane number for which the address is to be returned
740 * This function returns a kernel virtual address of a given plane if
741 * such a mapping exist, NULL otherwise.
743 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
745 struct vb2_queue
*q
= vb
->vb2_queue
;
747 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
750 return call_memop(q
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
753 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
756 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
757 * @vb: vb2_buffer to which the plane in question belongs to
758 * @plane_no: plane number for which the cookie is to be returned
760 * This function returns an allocator specific cookie for a given plane if
761 * available, NULL otherwise. The allocator should provide some simple static
762 * inline function, which would convert this cookie to the allocator specific
763 * type that can be used directly by the driver to access the buffer. This can
764 * be for example physical address, pointer to scatter list or IOMMU mapping.
766 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
768 struct vb2_queue
*q
= vb
->vb2_queue
;
770 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
773 return call_memop(q
, cookie
, vb
->planes
[plane_no
].mem_priv
);
775 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
778 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
779 * @vb: vb2_buffer returned from the driver
780 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
781 * or VB2_BUF_STATE_ERROR if the operation finished with an error
783 * This function should be called by the driver after a hardware operation on
784 * a buffer is finished and the buffer may be returned to userspace. The driver
785 * cannot use this buffer anymore until it is queued back to it by videobuf
786 * by the means of buf_queue callback. Only buffers previously queued to the
787 * driver by buf_queue can be passed to this function.
789 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
791 struct vb2_queue
*q
= vb
->vb2_queue
;
794 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
797 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
800 dprintk(4, "Done processing on buffer %d, state: %d\n",
801 vb
->v4l2_buf
.index
, vb
->state
);
803 /* Add the buffer to the done buffers list */
804 spin_lock_irqsave(&q
->done_lock
, flags
);
806 list_add_tail(&vb
->done_entry
, &q
->done_list
);
807 atomic_dec(&q
->queued_count
);
808 spin_unlock_irqrestore(&q
->done_lock
, flags
);
810 /* Inform any processes that may be waiting for buffers */
811 wake_up(&q
->done_wq
);
813 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
816 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
817 * a v4l2_buffer by the userspace
819 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
820 struct v4l2_plane
*v4l2_planes
)
825 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
827 * Verify that the userspace gave us a valid array for
830 ret
= __verify_planes_array(vb
, b
);
834 /* Fill in driver-provided information for OUTPUT types */
835 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
837 * Will have to go up to b->length when API starts
838 * accepting variable number of planes.
840 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
841 v4l2_planes
[plane
].bytesused
=
842 b
->m
.planes
[plane
].bytesused
;
843 v4l2_planes
[plane
].data_offset
=
844 b
->m
.planes
[plane
].data_offset
;
848 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
849 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
850 v4l2_planes
[plane
].m
.userptr
=
851 b
->m
.planes
[plane
].m
.userptr
;
852 v4l2_planes
[plane
].length
=
853 b
->m
.planes
[plane
].length
;
858 * Single-planar buffers do not use planes array,
859 * so fill in relevant v4l2_buffer struct fields instead.
860 * In videobuf we use our internal V4l2_planes struct for
861 * single-planar buffers as well, for simplicity.
863 if (V4L2_TYPE_IS_OUTPUT(b
->type
))
864 v4l2_planes
[0].bytesused
= b
->bytesused
;
866 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
867 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
868 v4l2_planes
[0].length
= b
->length
;
872 vb
->v4l2_buf
.field
= b
->field
;
873 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
874 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_STATE_FLAGS
;
880 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
882 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
884 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
885 struct vb2_queue
*q
= vb
->vb2_queue
;
889 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
891 /* Verify and copy relevant information provided by the userspace */
892 ret
= __fill_vb2_buffer(vb
, b
, planes
);
896 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
897 /* Skip the plane if already verified */
898 if (vb
->v4l2_planes
[plane
].m
.userptr
&&
899 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
900 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
903 dprintk(3, "qbuf: userspace address for plane %d changed, "
904 "reacquiring memory\n", plane
);
906 /* Check if the provided plane buffer is large enough */
907 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
912 /* Release previously acquired memory if present */
913 if (vb
->planes
[plane
].mem_priv
)
914 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
916 vb
->planes
[plane
].mem_priv
= NULL
;
917 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
918 vb
->v4l2_planes
[plane
].length
= 0;
920 /* Acquire each plane's memory */
921 mem_priv
= call_memop(q
, get_userptr
, q
->alloc_ctx
[plane
],
922 planes
[plane
].m
.userptr
,
923 planes
[plane
].length
, write
);
924 if (IS_ERR_OR_NULL(mem_priv
)) {
925 dprintk(1, "qbuf: failed acquiring userspace "
926 "memory for plane %d\n", plane
);
927 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
930 vb
->planes
[plane
].mem_priv
= mem_priv
;
934 * Call driver-specific initialization on the newly acquired buffer,
937 ret
= call_qop(q
, buf_init
, vb
);
939 dprintk(1, "qbuf: buffer initialization failed\n");
944 * Now that everything is in order, copy relevant information
945 * provided by userspace.
947 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
948 vb
->v4l2_planes
[plane
] = planes
[plane
];
952 /* In case of errors, release planes that were already acquired */
953 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
954 if (vb
->planes
[plane
].mem_priv
)
955 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
956 vb
->planes
[plane
].mem_priv
= NULL
;
957 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
958 vb
->v4l2_planes
[plane
].length
= 0;
965 * __qbuf_mmap() - handle qbuf of an MMAP buffer
967 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
969 return __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
973 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
975 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
977 struct vb2_queue
*q
= vb
->vb2_queue
;
979 vb
->state
= VB2_BUF_STATE_ACTIVE
;
980 atomic_inc(&q
->queued_count
);
981 q
->ops
->buf_queue(vb
);
984 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
986 struct vb2_queue
*q
= vb
->vb2_queue
;
990 case V4L2_MEMORY_MMAP
:
991 ret
= __qbuf_mmap(vb
, b
);
993 case V4L2_MEMORY_USERPTR
:
994 ret
= __qbuf_userptr(vb
, b
);
997 WARN(1, "Invalid queue type\n");
1002 ret
= call_qop(q
, buf_prepare
, vb
);
1004 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
1006 vb
->state
= VB2_BUF_STATE_PREPARED
;
1012 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1013 * @q: videobuf2 queue
1014 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1017 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1019 * 1) verifies the passed buffer,
1020 * 2) calls buf_prepare callback in the driver (if provided), in which
1021 * driver-specific buffer initialization can be performed,
1023 * The return values from this function are intended to be directly returned
1024 * from vidioc_prepare_buf handler in driver.
1026 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1028 struct vb2_buffer
*vb
;
1032 dprintk(1, "%s(): file io in progress\n", __func__
);
1036 if (b
->type
!= q
->type
) {
1037 dprintk(1, "%s(): invalid buffer type\n", __func__
);
1041 if (b
->index
>= q
->num_buffers
) {
1042 dprintk(1, "%s(): buffer index out of range\n", __func__
);
1046 vb
= q
->bufs
[b
->index
];
1048 /* Should never happen */
1049 dprintk(1, "%s(): buffer is NULL\n", __func__
);
1053 if (b
->memory
!= q
->memory
) {
1054 dprintk(1, "%s(): invalid memory type\n", __func__
);
1058 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1059 dprintk(1, "%s(): invalid buffer state %d\n", __func__
, vb
->state
);
1063 ret
= __buf_prepare(vb
, b
);
1067 __fill_v4l2_buffer(vb
, b
);
1071 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1074 * vb2_qbuf() - Queue a buffer from userspace
1075 * @q: videobuf2 queue
1076 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1079 * Should be called from vidioc_qbuf ioctl handler of a driver.
1081 * 1) verifies the passed buffer,
1082 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1083 * which driver-specific buffer initialization can be performed,
1084 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1085 * callback for processing.
1087 * The return values from this function are intended to be directly returned
1088 * from vidioc_qbuf handler in driver.
1090 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1092 struct rw_semaphore
*mmap_sem
= NULL
;
1093 struct vb2_buffer
*vb
;
1097 * In case of user pointer buffers vb2 allocator needs to get direct
1098 * access to userspace pages. This requires getting read access on
1099 * mmap semaphore in the current process structure. The same
1100 * semaphore is taken before calling mmap operation, while both mmap
1101 * and qbuf are called by the driver or v4l2 core with driver's lock
1102 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1103 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1104 * release driver's lock, takes mmap_sem and then takes again driver's
1107 * To avoid race with other vb2 calls, which might be called after
1108 * releasing driver's lock, this operation is performed at the
1109 * beggining of qbuf processing. This way the queue status is
1110 * consistent after getting driver's lock back.
1112 if (q
->memory
== V4L2_MEMORY_USERPTR
) {
1113 mmap_sem
= ¤t
->mm
->mmap_sem
;
1114 call_qop(q
, wait_prepare
, q
);
1115 down_read(mmap_sem
);
1116 call_qop(q
, wait_finish
, q
);
1120 dprintk(1, "qbuf: file io in progress\n");
1125 if (b
->type
!= q
->type
) {
1126 dprintk(1, "qbuf: invalid buffer type\n");
1131 if (b
->index
>= q
->num_buffers
) {
1132 dprintk(1, "qbuf: buffer index out of range\n");
1137 vb
= q
->bufs
[b
->index
];
1139 /* Should never happen */
1140 dprintk(1, "qbuf: buffer is NULL\n");
1145 if (b
->memory
!= q
->memory
) {
1146 dprintk(1, "qbuf: invalid memory type\n");
1151 switch (vb
->state
) {
1152 case VB2_BUF_STATE_DEQUEUED
:
1153 ret
= __buf_prepare(vb
, b
);
1156 case VB2_BUF_STATE_PREPARED
:
1159 dprintk(1, "qbuf: buffer already in use\n");
1165 * Add to the queued buffers list, a buffer will stay on it until
1166 * dequeued in dqbuf.
1168 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1169 vb
->state
= VB2_BUF_STATE_QUEUED
;
1172 * If already streaming, give the buffer to driver for processing.
1173 * If not, the buffer will be given to driver on next streamon.
1176 __enqueue_in_driver(vb
);
1178 /* Fill buffer information for the userspace */
1179 __fill_v4l2_buffer(vb
, b
);
1181 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
1187 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1190 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1193 * Will sleep if required for nonblocking == false.
1195 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1198 * All operations on vb_done_list are performed under done_lock
1199 * spinlock protection. However, buffers may be removed from
1200 * it and returned to userspace only while holding both driver's
1201 * lock and the done_lock spinlock. Thus we can be sure that as
1202 * long as we hold the driver's lock, the list will remain not
1203 * empty if list_empty() check succeeds.
1209 if (!q
->streaming
) {
1210 dprintk(1, "Streaming off, will not wait for buffers\n");
1214 if (!list_empty(&q
->done_list
)) {
1216 * Found a buffer that we were waiting for.
1222 dprintk(1, "Nonblocking and no buffers to dequeue, "
1228 * We are streaming and blocking, wait for another buffer to
1229 * become ready or for streamoff. Driver's lock is released to
1230 * allow streamoff or qbuf to be called while waiting.
1232 call_qop(q
, wait_prepare
, q
);
1235 * All locks have been released, it is safe to sleep now.
1237 dprintk(3, "Will sleep waiting for buffers\n");
1238 ret
= wait_event_interruptible(q
->done_wq
,
1239 !list_empty(&q
->done_list
) || !q
->streaming
);
1242 * We need to reevaluate both conditions again after reacquiring
1243 * the locks or return an error if one occurred.
1245 call_qop(q
, wait_finish
, q
);
1253 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1255 * Will sleep if required for nonblocking == false.
1257 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1260 unsigned long flags
;
1264 * Wait for at least one buffer to become available on the done_list.
1266 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1271 * Driver's lock has been held since we last verified that done_list
1272 * is not empty, so no need for another list_empty(done_list) check.
1274 spin_lock_irqsave(&q
->done_lock
, flags
);
1275 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1276 list_del(&(*vb
)->done_entry
);
1277 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1283 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1284 * @q: videobuf2 queue
1286 * This function will wait until all buffers that have been given to the driver
1287 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1288 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1289 * taken, for example from stop_streaming() callback.
1291 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1293 if (!q
->streaming
) {
1294 dprintk(1, "Streaming off, will not wait for buffers\n");
1298 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1301 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1304 * vb2_dqbuf() - Dequeue a buffer to the userspace
1305 * @q: videobuf2 queue
1306 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1308 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1309 * buffers ready for dequeuing are present. Normally the driver
1310 * would be passing (file->f_flags & O_NONBLOCK) here
1312 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1314 * 1) verifies the passed buffer,
1315 * 2) calls buf_finish callback in the driver (if provided), in which
1316 * driver can perform any additional operations that may be required before
1317 * returning the buffer to userspace, such as cache sync,
1318 * 3) the buffer struct members are filled with relevant information for
1321 * The return values from this function are intended to be directly returned
1322 * from vidioc_dqbuf handler in driver.
1324 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1326 struct vb2_buffer
*vb
= NULL
;
1330 dprintk(1, "dqbuf: file io in progress\n");
1334 if (b
->type
!= q
->type
) {
1335 dprintk(1, "dqbuf: invalid buffer type\n");
1339 ret
= __vb2_get_done_vb(q
, &vb
, nonblocking
);
1341 dprintk(1, "dqbuf: error getting next done buffer\n");
1345 ret
= call_qop(q
, buf_finish
, vb
);
1347 dprintk(1, "dqbuf: buffer finish failed\n");
1351 switch (vb
->state
) {
1352 case VB2_BUF_STATE_DONE
:
1353 dprintk(3, "dqbuf: Returning done buffer\n");
1355 case VB2_BUF_STATE_ERROR
:
1356 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1359 dprintk(1, "dqbuf: Invalid buffer state\n");
1363 /* Fill buffer information for the userspace */
1364 __fill_v4l2_buffer(vb
, b
);
1365 /* Remove from videobuf queue */
1366 list_del(&vb
->queued_entry
);
1368 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1369 vb
->v4l2_buf
.index
, vb
->state
);
1371 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1374 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1377 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1379 * Removes all queued buffers from driver's queue and all buffers queued by
1380 * userspace from videobuf's queue. Returns to state after reqbufs.
1382 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1387 * Tell driver to stop all transactions and release all queued
1391 call_qop(q
, stop_streaming
, q
);
1395 * Remove all buffers from videobuf's list...
1397 INIT_LIST_HEAD(&q
->queued_list
);
1399 * ...and done list; userspace will not receive any buffers it
1400 * has not already dequeued before initiating cancel.
1402 INIT_LIST_HEAD(&q
->done_list
);
1403 atomic_set(&q
->queued_count
, 0);
1404 wake_up_all(&q
->done_wq
);
1407 * Reinitialize all buffers for next use.
1409 for (i
= 0; i
< q
->num_buffers
; ++i
)
1410 q
->bufs
[i
]->state
= VB2_BUF_STATE_DEQUEUED
;
1414 * vb2_streamon - start streaming
1415 * @q: videobuf2 queue
1416 * @type: type argument passed from userspace to vidioc_streamon handler
1418 * Should be called from vidioc_streamon handler of a driver.
1420 * 1) verifies current state
1421 * 2) passes any previously queued buffers to the driver and starts streaming
1423 * The return values from this function are intended to be directly returned
1424 * from vidioc_streamon handler in the driver.
1426 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1428 struct vb2_buffer
*vb
;
1432 dprintk(1, "streamon: file io in progress\n");
1436 if (type
!= q
->type
) {
1437 dprintk(1, "streamon: invalid stream type\n");
1442 dprintk(1, "streamon: already streaming\n");
1447 * If any buffers were queued before streamon,
1448 * we can now pass them to driver for processing.
1450 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1451 __enqueue_in_driver(vb
);
1454 * Let driver notice that streaming state has been enabled.
1456 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->queued_count
));
1458 dprintk(1, "streamon: driver refused to start streaming\n");
1459 __vb2_queue_cancel(q
);
1465 dprintk(3, "Streamon successful\n");
1468 EXPORT_SYMBOL_GPL(vb2_streamon
);
1472 * vb2_streamoff - stop streaming
1473 * @q: videobuf2 queue
1474 * @type: type argument passed from userspace to vidioc_streamoff handler
1476 * Should be called from vidioc_streamoff handler of a driver.
1478 * 1) verifies current state,
1479 * 2) stop streaming and dequeues any queued buffers, including those previously
1480 * passed to the driver (after waiting for the driver to finish).
1482 * This call can be used for pausing playback.
1483 * The return values from this function are intended to be directly returned
1484 * from vidioc_streamoff handler in the driver
1486 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1489 dprintk(1, "streamoff: file io in progress\n");
1493 if (type
!= q
->type
) {
1494 dprintk(1, "streamoff: invalid stream type\n");
1498 if (!q
->streaming
) {
1499 dprintk(1, "streamoff: not streaming\n");
1504 * Cancel will pause streaming and remove all buffers from the driver
1505 * and videobuf, effectively returning control over them to userspace.
1507 __vb2_queue_cancel(q
);
1509 dprintk(3, "Streamoff successful\n");
1512 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1515 * __find_plane_by_offset() - find plane associated with the given offset off
1517 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1518 unsigned int *_buffer
, unsigned int *_plane
)
1520 struct vb2_buffer
*vb
;
1521 unsigned int buffer
, plane
;
1524 * Go over all buffers and their planes, comparing the given offset
1525 * with an offset assigned to each plane. If a match is found,
1526 * return its buffer and plane numbers.
1528 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1529 vb
= q
->bufs
[buffer
];
1531 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1532 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1544 * vb2_mmap() - map video buffers into application address space
1545 * @q: videobuf2 queue
1546 * @vma: vma passed to the mmap file operation handler in the driver
1548 * Should be called from mmap file operation handler of a driver.
1549 * This function maps one plane of one of the available video buffers to
1550 * userspace. To map whole video memory allocated on reqbufs, this function
1551 * has to be called once per each plane per each buffer previously allocated.
1553 * When the userspace application calls mmap, it passes to it an offset returned
1554 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1555 * a "cookie", which is then used to identify the plane to be mapped.
1556 * This function finds a plane with a matching offset and a mapping is performed
1557 * by the means of a provided memory operation.
1559 * The return values from this function are intended to be directly returned
1560 * from the mmap handler in driver.
1562 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1564 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1565 struct vb2_buffer
*vb
;
1566 unsigned int buffer
, plane
;
1569 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1570 dprintk(1, "Queue is not currently set up for mmap\n");
1575 * Check memory area access mode.
1577 if (!(vma
->vm_flags
& VM_SHARED
)) {
1578 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1581 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1582 if (!(vma
->vm_flags
& VM_WRITE
)) {
1583 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1587 if (!(vma
->vm_flags
& VM_READ
)) {
1588 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1594 * Find the plane corresponding to the offset passed by userspace.
1596 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1600 vb
= q
->bufs
[buffer
];
1602 ret
= call_memop(q
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
1606 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1609 EXPORT_SYMBOL_GPL(vb2_mmap
);
1612 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
1615 unsigned long pgoff
,
1616 unsigned long flags
)
1618 unsigned long off
= pgoff
<< PAGE_SHIFT
;
1619 struct vb2_buffer
*vb
;
1620 unsigned int buffer
, plane
;
1623 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1624 dprintk(1, "Queue is not currently set up for mmap\n");
1629 * Find the plane corresponding to the offset passed by userspace.
1631 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1635 vb
= q
->bufs
[buffer
];
1637 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
1639 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
1642 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1643 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1646 * vb2_poll() - implements poll userspace operation
1647 * @q: videobuf2 queue
1648 * @file: file argument passed to the poll file operation handler
1649 * @wait: wait argument passed to the poll file operation handler
1651 * This function implements poll file operation handler for a driver.
1652 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1653 * be informed that the file descriptor of a video device is available for
1655 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1656 * will be reported as available for writing.
1658 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1661 * The return values from this function are intended to be directly returned
1662 * from poll handler in driver.
1664 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1666 struct video_device
*vfd
= video_devdata(file
);
1667 unsigned long req_events
= poll_requested_events(wait
);
1668 struct vb2_buffer
*vb
= NULL
;
1669 unsigned int res
= 0;
1670 unsigned long flags
;
1672 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
1673 struct v4l2_fh
*fh
= file
->private_data
;
1675 if (v4l2_event_pending(fh
))
1677 else if (req_events
& POLLPRI
)
1678 poll_wait(file
, &fh
->wait
, wait
);
1682 * Start file I/O emulator only if streaming API has not been used yet.
1684 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1685 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
1686 (req_events
& (POLLIN
| POLLRDNORM
))) {
1687 if (__vb2_init_fileio(q
, 1))
1688 return res
| POLLERR
;
1690 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
1691 (req_events
& (POLLOUT
| POLLWRNORM
))) {
1692 if (__vb2_init_fileio(q
, 0))
1693 return res
| POLLERR
;
1695 * Write to OUTPUT queue can be done immediately.
1697 return res
| POLLOUT
| POLLWRNORM
;
1702 * There is nothing to wait for if no buffers have already been queued.
1704 if (list_empty(&q
->queued_list
))
1705 return res
| POLLERR
;
1707 poll_wait(file
, &q
->done_wq
, wait
);
1710 * Take first buffer available for dequeuing.
1712 spin_lock_irqsave(&q
->done_lock
, flags
);
1713 if (!list_empty(&q
->done_list
))
1714 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
1716 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1718 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
1719 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
1720 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ?
1721 res
| POLLOUT
| POLLWRNORM
:
1722 res
| POLLIN
| POLLRDNORM
;
1726 EXPORT_SYMBOL_GPL(vb2_poll
);
1729 * vb2_queue_init() - initialize a videobuf2 queue
1730 * @q: videobuf2 queue; this structure should be allocated in driver
1732 * The vb2_queue structure should be allocated by the driver. The driver is
1733 * responsible of clearing it's content and setting initial values for some
1734 * required entries before calling this function.
1735 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1736 * to the struct vb2_queue description in include/media/videobuf2-core.h
1737 * for more information.
1739 int vb2_queue_init(struct vb2_queue
*q
)
1743 BUG_ON(!q
->mem_ops
);
1745 BUG_ON(!q
->io_modes
);
1747 BUG_ON(!q
->ops
->queue_setup
);
1748 BUG_ON(!q
->ops
->buf_queue
);
1750 INIT_LIST_HEAD(&q
->queued_list
);
1751 INIT_LIST_HEAD(&q
->done_list
);
1752 spin_lock_init(&q
->done_lock
);
1753 init_waitqueue_head(&q
->done_wq
);
1755 if (q
->buf_struct_size
== 0)
1756 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
1760 EXPORT_SYMBOL_GPL(vb2_queue_init
);
1763 * vb2_queue_release() - stop streaming, release the queue and free memory
1764 * @q: videobuf2 queue
1766 * This function stops streaming and performs necessary clean ups, including
1767 * freeing video buffer memory. The driver is responsible for freeing
1768 * the vb2_queue structure itself.
1770 void vb2_queue_release(struct vb2_queue
*q
)
1772 __vb2_cleanup_fileio(q
);
1773 __vb2_queue_cancel(q
);
1774 __vb2_queue_free(q
, q
->num_buffers
);
1776 EXPORT_SYMBOL_GPL(vb2_queue_release
);
1779 * struct vb2_fileio_buf - buffer context used by file io emulator
1781 * vb2 provides a compatibility layer and emulator of file io (read and
1782 * write) calls on top of streaming API. This structure is used for
1783 * tracking context related to the buffers.
1785 struct vb2_fileio_buf
{
1789 unsigned int queued
:1;
1793 * struct vb2_fileio_data - queue context used by file io emulator
1795 * vb2 provides a compatibility layer and emulator of file io (read and
1796 * write) calls on top of streaming API. For proper operation it required
1797 * this structure to save the driver state between each call of the read
1798 * or write function.
1800 struct vb2_fileio_data
{
1801 struct v4l2_requestbuffers req
;
1802 struct v4l2_buffer b
;
1803 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
1805 unsigned int q_count
;
1806 unsigned int dq_count
;
1811 * __vb2_init_fileio() - initialize file io emulator
1812 * @q: videobuf2 queue
1813 * @read: mode selector (1 means read, 0 means write)
1815 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
1817 struct vb2_fileio_data
*fileio
;
1819 unsigned int count
= 0;
1824 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
1825 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
1829 * Check if device supports mapping buffers to kernel virtual space.
1831 if (!q
->mem_ops
->vaddr
)
1835 * Check if streaming api has not been already activated.
1837 if (q
->streaming
|| q
->num_buffers
> 0)
1841 * Start with count 1, driver can increase it in queue_setup()
1845 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1846 (read
) ? "read" : "write", count
, q
->io_flags
);
1848 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
1852 fileio
->flags
= q
->io_flags
;
1855 * Request buffers and use MMAP type to force driver
1856 * to allocate buffers by itself.
1858 fileio
->req
.count
= count
;
1859 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
1860 fileio
->req
.type
= q
->type
;
1861 ret
= vb2_reqbufs(q
, &fileio
->req
);
1866 * Check if plane_count is correct
1867 * (multiplane buffers are not supported).
1869 if (q
->bufs
[0]->num_planes
!= 1) {
1875 * Get kernel address of each buffer.
1877 for (i
= 0; i
< q
->num_buffers
; i
++) {
1878 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
1879 if (fileio
->bufs
[i
].vaddr
== NULL
)
1881 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
1885 * Read mode requires pre queuing of all buffers.
1889 * Queue all buffers.
1891 for (i
= 0; i
< q
->num_buffers
; i
++) {
1892 struct v4l2_buffer
*b
= &fileio
->b
;
1893 memset(b
, 0, sizeof(*b
));
1895 b
->memory
= q
->memory
;
1897 ret
= vb2_qbuf(q
, b
);
1900 fileio
->bufs
[i
].queued
= 1;
1906 ret
= vb2_streamon(q
, q
->type
);
1916 fileio
->req
.count
= 0;
1917 vb2_reqbufs(q
, &fileio
->req
);
1925 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1926 * @q: videobuf2 queue
1928 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
1930 struct vb2_fileio_data
*fileio
= q
->fileio
;
1934 * Hack fileio context to enable direct calls to vb2 ioctl
1939 vb2_streamoff(q
, q
->type
);
1940 fileio
->req
.count
= 0;
1941 vb2_reqbufs(q
, &fileio
->req
);
1943 dprintk(3, "file io emulator closed\n");
1949 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1950 * @q: videobuf2 queue
1951 * @data: pointed to target userspace buffer
1952 * @count: number of bytes to read or write
1953 * @ppos: file handle position tracking pointer
1954 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1955 * @read: access mode selector (1 means read, 0 means write)
1957 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
1958 loff_t
*ppos
, int nonblock
, int read
)
1960 struct vb2_fileio_data
*fileio
;
1961 struct vb2_fileio_buf
*buf
;
1964 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1965 read
? "read" : "write", (long)*ppos
, count
,
1966 nonblock
? "non" : "");
1972 * Initialize emulator on first call.
1975 ret
= __vb2_init_fileio(q
, read
);
1976 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
1983 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1984 * The pointer will be restored before returning from this function.
1988 index
= fileio
->index
;
1989 buf
= &fileio
->bufs
[index
];
1992 * Check if we need to dequeue the buffer.
1995 struct vb2_buffer
*vb
;
1998 * Call vb2_dqbuf to get buffer back.
2000 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2001 fileio
->b
.type
= q
->type
;
2002 fileio
->b
.memory
= q
->memory
;
2003 fileio
->b
.index
= index
;
2004 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
2005 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2008 fileio
->dq_count
+= 1;
2011 * Get number of bytes filled by the driver
2013 vb
= q
->bufs
[index
];
2014 buf
->size
= vb2_get_plane_payload(vb
, 0);
2019 * Limit count on last few bytes of the buffer.
2021 if (buf
->pos
+ count
> buf
->size
) {
2022 count
= buf
->size
- buf
->pos
;
2023 dprintk(5, "reducing read count: %zd\n", count
);
2027 * Transfer data to userspace.
2029 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2030 count
, index
, buf
->pos
);
2032 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2034 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2036 dprintk(3, "file io: error copying data\n");
2048 * Queue next buffer if required.
2050 if (buf
->pos
== buf
->size
||
2051 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2053 * Check if this is the last buffer to read.
2055 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2056 fileio
->dq_count
== 1) {
2057 dprintk(3, "file io: read limit reached\n");
2059 * Restore fileio pointer and release the context.
2062 return __vb2_cleanup_fileio(q
);
2066 * Call vb2_qbuf and give buffer to the driver.
2068 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2069 fileio
->b
.type
= q
->type
;
2070 fileio
->b
.memory
= q
->memory
;
2071 fileio
->b
.index
= index
;
2072 fileio
->b
.bytesused
= buf
->pos
;
2073 ret
= vb2_qbuf(q
, &fileio
->b
);
2074 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2079 * Buffer has been queued, update the status
2083 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
2084 fileio
->q_count
+= 1;
2087 * Switch to the next buffer
2089 fileio
->index
= (index
+ 1) % q
->num_buffers
;
2092 * Start streaming if required.
2094 if (!read
&& !q
->streaming
) {
2095 ret
= vb2_streamon(q
, q
->type
);
2102 * Return proper number of bytes processed.
2108 * Restore the fileio context and block vb2 ioctl interface.
2114 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2115 loff_t
*ppos
, int nonblocking
)
2117 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2119 EXPORT_SYMBOL_GPL(vb2_read
);
2121 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2122 loff_t
*ppos
, int nonblocking
)
2124 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
2126 EXPORT_SYMBOL_GPL(vb2_write
);
2130 * The following functions are not part of the vb2 core API, but are helper
2131 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2132 * and struct vb2_ops.
2133 * They contain boilerplate code that most if not all drivers have to do
2134 * and so they simplify the driver code.
2137 /* The queue is busy if there is a owner and you are not that owner. */
2138 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
2140 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
2143 /* vb2 ioctl helpers */
2145 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
2146 struct v4l2_requestbuffers
*p
)
2148 struct video_device
*vdev
= video_devdata(file
);
2149 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
2153 if (vb2_queue_is_busy(vdev
, file
))
2155 res
= __reqbufs(vdev
->queue
, p
);
2156 /* If count == 0, then the owner has released all buffers and he
2157 is no longer owner of the queue. Otherwise we have a new owner. */
2159 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
2162 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
2164 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
2165 struct v4l2_create_buffers
*p
)
2167 struct video_device
*vdev
= video_devdata(file
);
2168 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->format
.type
);
2170 p
->index
= vdev
->queue
->num_buffers
;
2171 /* If count == 0, then just check if memory and type are valid.
2172 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2174 return res
!= -EBUSY
? res
: 0;
2177 if (vb2_queue_is_busy(vdev
, file
))
2179 res
= __create_bufs(vdev
->queue
, p
);
2181 vdev
->queue
->owner
= file
->private_data
;
2184 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
2186 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
2187 struct v4l2_buffer
*p
)
2189 struct video_device
*vdev
= video_devdata(file
);
2191 if (vb2_queue_is_busy(vdev
, file
))
2193 return vb2_prepare_buf(vdev
->queue
, p
);
2195 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
2197 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2199 struct video_device
*vdev
= video_devdata(file
);
2201 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2202 return vb2_querybuf(vdev
->queue
, p
);
2204 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
2206 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2208 struct video_device
*vdev
= video_devdata(file
);
2210 if (vb2_queue_is_busy(vdev
, file
))
2212 return vb2_qbuf(vdev
->queue
, p
);
2214 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
2216 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2218 struct video_device
*vdev
= video_devdata(file
);
2220 if (vb2_queue_is_busy(vdev
, file
))
2222 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
2224 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
2226 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2228 struct video_device
*vdev
= video_devdata(file
);
2230 if (vb2_queue_is_busy(vdev
, file
))
2232 return vb2_streamon(vdev
->queue
, i
);
2234 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
2236 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2238 struct video_device
*vdev
= video_devdata(file
);
2240 if (vb2_queue_is_busy(vdev
, file
))
2242 return vb2_streamoff(vdev
->queue
, i
);
2244 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
2246 /* v4l2_file_operations helpers */
2248 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2250 struct video_device
*vdev
= video_devdata(file
);
2252 return vb2_mmap(vdev
->queue
, vma
);
2254 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
2256 int vb2_fop_release(struct file
*file
)
2258 struct video_device
*vdev
= video_devdata(file
);
2260 if (file
->private_data
== vdev
->queue
->owner
) {
2261 vb2_queue_release(vdev
->queue
);
2262 vdev
->queue
->owner
= NULL
;
2264 return v4l2_fh_release(file
);
2266 EXPORT_SYMBOL_GPL(vb2_fop_release
);
2268 ssize_t
vb2_fop_write(struct file
*file
, char __user
*buf
,
2269 size_t count
, loff_t
*ppos
)
2271 struct video_device
*vdev
= video_devdata(file
);
2272 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2273 bool must_lock
= !test_bit(V4L2_FL_LOCK_ALL_FOPS
, &vdev
->flags
) && lock
;
2276 if (must_lock
&& mutex_lock_interruptible(lock
))
2277 return -ERESTARTSYS
;
2278 if (vb2_queue_is_busy(vdev
, file
))
2280 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
2281 file
->f_flags
& O_NONBLOCK
);
2283 vdev
->queue
->owner
= file
->private_data
;
2289 EXPORT_SYMBOL_GPL(vb2_fop_write
);
2291 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
2292 size_t count
, loff_t
*ppos
)
2294 struct video_device
*vdev
= video_devdata(file
);
2295 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2296 bool must_lock
= !test_bit(V4L2_FL_LOCK_ALL_FOPS
, &vdev
->flags
) && vdev
->lock
;
2299 if (must_lock
&& mutex_lock_interruptible(lock
))
2300 return -ERESTARTSYS
;
2301 if (vb2_queue_is_busy(vdev
, file
))
2303 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
2304 file
->f_flags
& O_NONBLOCK
);
2306 vdev
->queue
->owner
= file
->private_data
;
2312 EXPORT_SYMBOL_GPL(vb2_fop_read
);
2314 unsigned int vb2_fop_poll(struct file
*file
, poll_table
*wait
)
2316 struct video_device
*vdev
= video_devdata(file
);
2317 struct vb2_queue
*q
= vdev
->queue
;
2318 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
2319 unsigned long req_events
= poll_requested_events(wait
);
2322 /* Yuck. We really need to get rid of this flag asap. If it is
2323 set, then the core took the serialization lock before calling
2324 poll(). This is being phased out, but for now we have to handle
2326 bool locked
= test_bit(V4L2_FL_LOCK_ALL_FOPS
, &vdev
->flags
);
2327 bool must_lock
= false;
2329 /* Try to be smart: only lock if polling might start fileio,
2330 otherwise locking will only introduce unwanted delays. */
2331 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
2332 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
2333 (req_events
& (POLLIN
| POLLRDNORM
)))
2335 else if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
2336 (req_events
& (POLLOUT
| POLLWRNORM
)))
2340 /* If locking is needed, but this helper doesn't know how, then you
2341 shouldn't be using this helper but you should write your own. */
2342 WARN_ON(must_lock
&& !locked
&& !lock
);
2344 if (must_lock
&& !locked
&& lock
&& mutex_lock_interruptible(lock
))
2349 res
= vb2_poll(vdev
->queue
, file
, wait
);
2351 /* If fileio was started, then we have a new queue owner. */
2352 if (must_lock
&& !fileio
&& q
->fileio
)
2353 q
->owner
= file
->private_data
;
2354 if (must_lock
&& !locked
&& lock
)
2358 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
2361 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
2362 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
2364 struct video_device
*vdev
= video_devdata(file
);
2366 return vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
2368 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
2371 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2373 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
2375 mutex_unlock(vq
->lock
);
2377 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
2379 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
2381 mutex_lock(vq
->lock
);
2383 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
2385 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2386 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2387 MODULE_LICENSE("GPL");