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 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
282 /* Is memory for copying plane information present? */
283 if (NULL == b->m.planes) {
284 dprintk(1, "Multi-planar buffer passed but "
285 "planes array not provided\n");
289 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
290 dprintk(1, "Incorrect planes array length, "
291 "expected %d, got %d\n", vb->num_planes, b->length);
299 * __buffer_in_use() - return true if the buffer is in use and
300 * the queue cannot be freed (by the means of REQBUFS(0)) call
302 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
305 for (plane = 0; plane < vb->num_planes; ++plane) {
306 void *mem_priv = vb->planes[plane].mem_priv;
308 * If num_users() has not been provided, call_memop
309 * will return 0, apparently nobody cares about this
310 * case anyway. If num_users() returns more than 1,
311 * we are not the only user of the plane's memory.
313 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
320 * __buffers_in_use() - return true if any buffers on the queue are in use and
321 * the queue cannot be freed (by the means of REQBUFS(0)) call
323 static bool __buffers_in_use(struct vb2_queue *q)
326 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
327 if (__buffer_in_use(q, q->bufs[buffer]))
334 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
335 * returned to userspace
337 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
339 struct vb2_queue *q = vb->vb2_queue;
341 /* Copy back data such as timestamp, flags, etc. */
342 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
343 b->reserved2 = vb->v4l2_buf.reserved2;
344 b->reserved = vb->v4l2_buf.reserved;
346 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
348 * Fill in plane-related data if userspace provided an array
349 * for it. The caller has already verified memory and size.
351 b->length = vb->num_planes;
352 memcpy(b->m.planes, vb->v4l2_planes,
353 b->length * sizeof(struct v4l2_plane));
356 * We use length and offset in v4l2_planes array even for
357 * single-planar buffers, but userspace does not.
359 b->length = vb->v4l2_planes[0].length;
360 b->bytesused = vb->v4l2_planes[0].bytesused;
361 if (q->memory == V4L2_MEMORY_MMAP)
362 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
363 else if (q->memory == V4L2_MEMORY_USERPTR)
364 b->m.userptr = vb->v4l2_planes[0].m.userptr;
368 * Clear any buffer state related flags.
370 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
373 case VB2_BUF_STATE_QUEUED:
374 case VB2_BUF_STATE_ACTIVE:
375 b->flags |= V4L2_BUF_FLAG_QUEUED;
377 case VB2_BUF_STATE_ERROR:
378 b->flags |= V4L2_BUF_FLAG_ERROR;
380 case VB2_BUF_STATE_DONE:
381 b->flags |= V4L2_BUF_FLAG_DONE;
383 case VB2_BUF_STATE_PREPARED:
384 b->flags |= V4L2_BUF_FLAG_PREPARED;
386 case VB2_BUF_STATE_DEQUEUED:
391 if (__buffer_in_use(q, vb))
392 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;
413 if (b->type != q->type) {
414 dprintk(1, "querybuf: wrong buffer type\n");
418 if (b->index >= q->num_buffers) {
419 dprintk(1, "querybuf: buffer index out of range\n");
422 vb = q->bufs[b->index];
423 ret = __verify_planes_array(vb, b);
425 __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_create_bufs() - Wrapper for __create_bufs() that also verifies the
719 * memory and type values.
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 a
817 * v4l2_buffer by the userspace. The caller has already verified that struct
818 * v4l2_buffer has a valid number of planes.
820 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
821 struct v4l2_plane *v4l2_planes)
825 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
826 /* Fill in driver-provided information for OUTPUT types */
827 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
829 * Will have to go up to b->length when API starts
830 * accepting variable number of planes.
832 for (plane = 0; plane < vb->num_planes; ++plane) {
833 v4l2_planes[plane].bytesused =
834 b->m.planes[plane].bytesused;
835 v4l2_planes[plane].data_offset =
836 b->m.planes[plane].data_offset;
840 if (b->memory == V4L2_MEMORY_USERPTR) {
841 for (plane = 0; plane < vb->num_planes; ++plane) {
842 v4l2_planes[plane].m.userptr =
843 b->m.planes[plane].m.userptr;
844 v4l2_planes[plane].length =
845 b->m.planes[plane].length;
850 * Single-planar buffers do not use planes array,
851 * so fill in relevant v4l2_buffer struct fields instead.
852 * In videobuf we use our internal V4l2_planes struct for
853 * single-planar buffers as well, for simplicity.
855 if (V4L2_TYPE_IS_OUTPUT(b->type))
856 v4l2_planes[0].bytesused = b->bytesused;
858 if (b->memory == V4L2_MEMORY_USERPTR) {
859 v4l2_planes[0].m.userptr = b->m.userptr;
860 v4l2_planes[0].length = b->length;
864 vb->v4l2_buf.field = b->field;
865 vb->v4l2_buf.timestamp = b->timestamp;
866 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
870 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
872 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
874 struct v4l2_plane planes[VIDEO_MAX_PLANES];
875 struct vb2_queue *q = vb->vb2_queue;
879 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
881 /* Copy relevant information provided by the userspace */
882 __fill_vb2_buffer(vb, b, planes);
884 for (plane = 0; plane < vb->num_planes; ++plane) {
885 /* Skip the plane if already verified */
886 if (vb->v4l2_planes[plane].m.userptr &&
887 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
888 && vb->v4l2_planes[plane].length == planes[plane].length)
891 dprintk(3, "qbuf: userspace address for plane %d changed, "
892 "reacquiring memory\n", plane);
894 /* Check if the provided plane buffer is large enough */
895 if (planes[plane].length < q->plane_sizes[plane]) {
900 /* Release previously acquired memory if present */
901 if (vb->planes[plane].mem_priv)
902 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
904 vb->planes[plane].mem_priv = NULL;
905 vb->v4l2_planes[plane].m.userptr = 0;
906 vb->v4l2_planes[plane].length = 0;
908 /* Acquire each plane's memory */
909 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
910 planes[plane].m.userptr,
911 planes[plane].length, write);
912 if (IS_ERR_OR_NULL(mem_priv)) {
913 dprintk(1, "qbuf: failed acquiring userspace "
914 "memory for plane %d\n", plane);
915 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
918 vb->planes[plane].mem_priv = mem_priv;
922 * Call driver-specific initialization on the newly acquired buffer,
925 ret = call_qop(q, buf_init, vb);
927 dprintk(1, "qbuf: buffer initialization failed\n");
932 * Now that everything is in order, copy relevant information
933 * provided by userspace.
935 for (plane = 0; plane < vb->num_planes; ++plane)
936 vb->v4l2_planes[plane] = planes[plane];
940 /* In case of errors, release planes that were already acquired */
941 for (plane = 0; plane < vb->num_planes; ++plane) {
942 if (vb->planes[plane].mem_priv)
943 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
944 vb->planes[plane].mem_priv = NULL;
945 vb->v4l2_planes[plane].m.userptr = 0;
946 vb->v4l2_planes[plane].length = 0;
953 * __qbuf_mmap() - handle qbuf of an MMAP buffer
955 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
957 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
962 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
964 static void __enqueue_in_driver(struct vb2_buffer *vb)
966 struct vb2_queue *q = vb->vb2_queue;
968 vb->state = VB2_BUF_STATE_ACTIVE;
969 atomic_inc(&q->queued_count);
970 q->ops->buf_queue(vb);
973 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
975 struct vb2_queue *q = vb->vb2_queue;
979 case V4L2_MEMORY_MMAP:
980 ret = __qbuf_mmap(vb, b);
982 case V4L2_MEMORY_USERPTR:
983 ret = __qbuf_userptr(vb, b);
986 WARN(1, "Invalid queue type\n");
991 ret = call_qop(q, buf_prepare, vb);
993 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
995 vb->state = VB2_BUF_STATE_PREPARED;
1001 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1002 * @q: videobuf2 queue
1003 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1006 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1008 * 1) verifies the passed buffer,
1009 * 2) calls buf_prepare callback in the driver (if provided), in which
1010 * driver-specific buffer initialization can be performed,
1012 * The return values from this function are intended to be directly returned
1013 * from vidioc_prepare_buf handler in driver.
1015 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1017 struct vb2_buffer *vb;
1021 dprintk(1, "%s(): file io in progress\n", __func__);
1025 if (b->type != q->type) {
1026 dprintk(1, "%s(): invalid buffer type\n", __func__);
1030 if (b->index >= q->num_buffers) {
1031 dprintk(1, "%s(): buffer index out of range\n", __func__);
1035 vb = q->bufs[b->index];
1037 /* Should never happen */
1038 dprintk(1, "%s(): buffer is NULL\n", __func__);
1042 if (b->memory != q->memory) {
1043 dprintk(1, "%s(): invalid memory type\n", __func__);
1047 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1048 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1051 ret = __verify_planes_array(vb, b);
1054 ret = __buf_prepare(vb, b);
1058 __fill_v4l2_buffer(vb, b);
1062 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1065 * vb2_qbuf() - Queue a buffer from userspace
1066 * @q: videobuf2 queue
1067 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1070 * Should be called from vidioc_qbuf ioctl handler of a driver.
1072 * 1) verifies the passed buffer,
1073 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1074 * which driver-specific buffer initialization can be performed,
1075 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1076 * callback for processing.
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_qbuf handler in driver.
1081 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1083 struct rw_semaphore *mmap_sem = NULL;
1084 struct vb2_buffer *vb;
1088 * In case of user pointer buffers vb2 allocator needs to get direct
1089 * access to userspace pages. This requires getting read access on
1090 * mmap semaphore in the current process structure. The same
1091 * semaphore is taken before calling mmap operation, while both mmap
1092 * and qbuf are called by the driver or v4l2 core with driver's lock
1093 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1094 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1095 * release driver's lock, takes mmap_sem and then takes again driver's
1098 * To avoid race with other vb2 calls, which might be called after
1099 * releasing driver's lock, this operation is performed at the
1100 * beggining of qbuf processing. This way the queue status is
1101 * consistent after getting driver's lock back.
1103 if (q->memory == V4L2_MEMORY_USERPTR) {
1104 mmap_sem = ¤t->mm->mmap_sem;
1105 call_qop(q, wait_prepare, q);
1106 down_read(mmap_sem);
1107 call_qop(q, wait_finish, q);
1111 dprintk(1, "qbuf: file io in progress\n");
1116 if (b->type != q->type) {
1117 dprintk(1, "qbuf: invalid buffer type\n");
1122 if (b->index >= q->num_buffers) {
1123 dprintk(1, "qbuf: buffer index out of range\n");
1128 vb = q->bufs[b->index];
1130 /* Should never happen */
1131 dprintk(1, "qbuf: buffer is NULL\n");
1136 if (b->memory != q->memory) {
1137 dprintk(1, "qbuf: invalid memory type\n");
1141 ret = __verify_planes_array(vb, b);
1145 switch (vb->state) {
1146 case VB2_BUF_STATE_DEQUEUED:
1147 ret = __buf_prepare(vb, b);
1150 case VB2_BUF_STATE_PREPARED:
1153 dprintk(1, "qbuf: buffer already in use\n");
1159 * Add to the queued buffers list, a buffer will stay on it until
1160 * dequeued in dqbuf.
1162 list_add_tail(&vb->queued_entry, &q->queued_list);
1163 vb->state = VB2_BUF_STATE_QUEUED;
1166 * If already streaming, give the buffer to driver for processing.
1167 * If not, the buffer will be given to driver on next streamon.
1170 __enqueue_in_driver(vb);
1172 /* Fill buffer information for the userspace */
1173 __fill_v4l2_buffer(vb, b);
1175 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1181 EXPORT_SYMBOL_GPL(vb2_qbuf);
1184 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1187 * Will sleep if required for nonblocking == false.
1189 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1192 * All operations on vb_done_list are performed under done_lock
1193 * spinlock protection. However, buffers may be removed from
1194 * it and returned to userspace only while holding both driver's
1195 * lock and the done_lock spinlock. Thus we can be sure that as
1196 * long as we hold the driver's lock, the list will remain not
1197 * empty if list_empty() check succeeds.
1203 if (!q->streaming) {
1204 dprintk(1, "Streaming off, will not wait for buffers\n");
1208 if (!list_empty(&q->done_list)) {
1210 * Found a buffer that we were waiting for.
1216 dprintk(1, "Nonblocking and no buffers to dequeue, "
1222 * We are streaming and blocking, wait for another buffer to
1223 * become ready or for streamoff. Driver's lock is released to
1224 * allow streamoff or qbuf to be called while waiting.
1226 call_qop(q, wait_prepare, q);
1229 * All locks have been released, it is safe to sleep now.
1231 dprintk(3, "Will sleep waiting for buffers\n");
1232 ret = wait_event_interruptible(q->done_wq,
1233 !list_empty(&q->done_list) || !q->streaming);
1236 * We need to reevaluate both conditions again after reacquiring
1237 * the locks or return an error if one occurred.
1239 call_qop(q, wait_finish, q);
1241 dprintk(1, "Sleep was interrupted\n");
1249 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1251 * Will sleep if required for nonblocking == false.
1253 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1254 struct v4l2_buffer *b, int nonblocking)
1256 unsigned long flags;
1260 * Wait for at least one buffer to become available on the done_list.
1262 ret = __vb2_wait_for_done_vb(q, nonblocking);
1267 * Driver's lock has been held since we last verified that done_list
1268 * is not empty, so no need for another list_empty(done_list) check.
1270 spin_lock_irqsave(&q->done_lock, flags);
1271 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1273 * Only remove the buffer from done_list if v4l2_buffer can handle all
1276 ret = __verify_planes_array(*vb, b);
1278 list_del(&(*vb)->done_entry);
1279 spin_unlock_irqrestore(&q->done_lock, flags);
1285 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1286 * @q: videobuf2 queue
1288 * This function will wait until all buffers that have been given to the driver
1289 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1290 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1291 * taken, for example from stop_streaming() callback.
1293 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1295 if (!q->streaming) {
1296 dprintk(1, "Streaming off, will not wait for buffers\n");
1300 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1303 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1306 * vb2_dqbuf() - Dequeue a buffer to the userspace
1307 * @q: videobuf2 queue
1308 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1310 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1311 * buffers ready for dequeuing are present. Normally the driver
1312 * would be passing (file->f_flags & O_NONBLOCK) here
1314 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1316 * 1) verifies the passed buffer,
1317 * 2) calls buf_finish callback in the driver (if provided), in which
1318 * driver can perform any additional operations that may be required before
1319 * returning the buffer to userspace, such as cache sync,
1320 * 3) the buffer struct members are filled with relevant information for
1323 * The return values from this function are intended to be directly returned
1324 * from vidioc_dqbuf handler in driver.
1326 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1328 struct vb2_buffer *vb = NULL;
1332 dprintk(1, "dqbuf: file io in progress\n");
1336 if (b->type != q->type) {
1337 dprintk(1, "dqbuf: invalid buffer type\n");
1340 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1344 ret = call_qop(q, buf_finish, vb);
1346 dprintk(1, "dqbuf: buffer finish failed\n");
1350 switch (vb->state) {
1351 case VB2_BUF_STATE_DONE:
1352 dprintk(3, "dqbuf: Returning done buffer\n");
1354 case VB2_BUF_STATE_ERROR:
1355 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1358 dprintk(1, "dqbuf: Invalid buffer state\n");
1362 /* Fill buffer information for the userspace */
1363 __fill_v4l2_buffer(vb, b);
1364 /* Remove from videobuf queue */
1365 list_del(&vb->queued_entry);
1367 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1368 vb->v4l2_buf.index, vb->state);
1370 vb->state = VB2_BUF_STATE_DEQUEUED;
1373 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1376 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1378 * Removes all queued buffers from driver's queue and all buffers queued by
1379 * userspace from videobuf's queue. Returns to state after reqbufs.
1381 static void __vb2_queue_cancel(struct vb2_queue *q)
1386 * Tell driver to stop all transactions and release all queued
1390 call_qop(q, stop_streaming, q);
1394 * Remove all buffers from videobuf's list...
1396 INIT_LIST_HEAD(&q->queued_list);
1398 * ...and done list; userspace will not receive any buffers it
1399 * has not already dequeued before initiating cancel.
1401 INIT_LIST_HEAD(&q->done_list);
1402 atomic_set(&q->queued_count, 0);
1403 wake_up_all(&q->done_wq);
1406 * Reinitialize all buffers for next use.
1408 for (i = 0; i < q->num_buffers; ++i)
1409 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1413 * vb2_streamon - start streaming
1414 * @q: videobuf2 queue
1415 * @type: type argument passed from userspace to vidioc_streamon handler
1417 * Should be called from vidioc_streamon handler of a driver.
1419 * 1) verifies current state
1420 * 2) passes any previously queued buffers to the driver and starts streaming
1422 * The return values from this function are intended to be directly returned
1423 * from vidioc_streamon handler in the driver.
1425 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1427 struct vb2_buffer *vb;
1431 dprintk(1, "streamon: file io in progress\n");
1435 if (type != q->type) {
1436 dprintk(1, "streamon: invalid stream type\n");
1441 dprintk(1, "streamon: already streaming\n");
1446 * If any buffers were queued before streamon,
1447 * we can now pass them to driver for processing.
1449 list_for_each_entry(vb, &q->queued_list, queued_entry)
1450 __enqueue_in_driver(vb);
1453 * Let driver notice that streaming state has been enabled.
1455 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1457 dprintk(1, "streamon: driver refused to start streaming\n");
1458 __vb2_queue_cancel(q);
1464 dprintk(3, "Streamon successful\n");
1467 EXPORT_SYMBOL_GPL(vb2_streamon);
1471 * vb2_streamoff - stop streaming
1472 * @q: videobuf2 queue
1473 * @type: type argument passed from userspace to vidioc_streamoff handler
1475 * Should be called from vidioc_streamoff handler of a driver.
1477 * 1) verifies current state,
1478 * 2) stop streaming and dequeues any queued buffers, including those previously
1479 * passed to the driver (after waiting for the driver to finish).
1481 * This call can be used for pausing playback.
1482 * The return values from this function are intended to be directly returned
1483 * from vidioc_streamoff handler in the driver
1485 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1488 dprintk(1, "streamoff: file io in progress\n");
1492 if (type != q->type) {
1493 dprintk(1, "streamoff: invalid stream type\n");
1497 if (!q->streaming) {
1498 dprintk(1, "streamoff: not streaming\n");
1503 * Cancel will pause streaming and remove all buffers from the driver
1504 * and videobuf, effectively returning control over them to userspace.
1506 __vb2_queue_cancel(q);
1508 dprintk(3, "Streamoff successful\n");
1511 EXPORT_SYMBOL_GPL(vb2_streamoff);
1514 * __find_plane_by_offset() - find plane associated with the given offset off
1516 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1517 unsigned int *_buffer, unsigned int *_plane)
1519 struct vb2_buffer *vb;
1520 unsigned int buffer, plane;
1523 * Go over all buffers and their planes, comparing the given offset
1524 * with an offset assigned to each plane. If a match is found,
1525 * return its buffer and plane numbers.
1527 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1528 vb = q->bufs[buffer];
1530 for (plane = 0; plane < vb->num_planes; ++plane) {
1531 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1543 * vb2_mmap() - map video buffers into application address space
1544 * @q: videobuf2 queue
1545 * @vma: vma passed to the mmap file operation handler in the driver
1547 * Should be called from mmap file operation handler of a driver.
1548 * This function maps one plane of one of the available video buffers to
1549 * userspace. To map whole video memory allocated on reqbufs, this function
1550 * has to be called once per each plane per each buffer previously allocated.
1552 * When the userspace application calls mmap, it passes to it an offset returned
1553 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1554 * a "cookie", which is then used to identify the plane to be mapped.
1555 * This function finds a plane with a matching offset and a mapping is performed
1556 * by the means of a provided memory operation.
1558 * The return values from this function are intended to be directly returned
1559 * from the mmap handler in driver.
1561 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1563 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1564 struct vb2_buffer *vb;
1565 unsigned int buffer, plane;
1568 if (q->memory != V4L2_MEMORY_MMAP) {
1569 dprintk(1, "Queue is not currently set up for mmap\n");
1574 * Check memory area access mode.
1576 if (!(vma->vm_flags & VM_SHARED)) {
1577 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1580 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1581 if (!(vma->vm_flags & VM_WRITE)) {
1582 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1586 if (!(vma->vm_flags & VM_READ)) {
1587 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1593 * Find the plane corresponding to the offset passed by userspace.
1595 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1599 vb = q->bufs[buffer];
1601 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
1605 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1608 EXPORT_SYMBOL_GPL(vb2_mmap);
1611 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1614 unsigned long pgoff,
1615 unsigned long flags)
1617 unsigned long off = pgoff << PAGE_SHIFT;
1618 struct vb2_buffer *vb;
1619 unsigned int buffer, plane;
1622 if (q->memory != V4L2_MEMORY_MMAP) {
1623 dprintk(1, "Queue is not currently set up for mmap\n");
1628 * Find the plane corresponding to the offset passed by userspace.
1630 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1634 vb = q->bufs[buffer];
1636 return (unsigned long)vb2_plane_vaddr(vb, plane);
1638 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1641 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1642 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1645 * vb2_poll() - implements poll userspace operation
1646 * @q: videobuf2 queue
1647 * @file: file argument passed to the poll file operation handler
1648 * @wait: wait argument passed to the poll file operation handler
1650 * This function implements poll file operation handler for a driver.
1651 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1652 * be informed that the file descriptor of a video device is available for
1654 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1655 * will be reported as available for writing.
1657 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1660 * The return values from this function are intended to be directly returned
1661 * from poll handler in driver.
1663 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1665 struct video_device *vfd = video_devdata(file);
1666 unsigned long req_events = poll_requested_events(wait);
1667 struct vb2_buffer *vb = NULL;
1668 unsigned int res = 0;
1669 unsigned long flags;
1671 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1672 struct v4l2_fh *fh = file->private_data;
1674 if (v4l2_event_pending(fh))
1676 else if (req_events & POLLPRI)
1677 poll_wait(file, &fh->wait, wait);
1681 * Start file I/O emulator only if streaming API has not been used yet.
1683 if (q->num_buffers == 0 && q->fileio == NULL) {
1684 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1685 (req_events & (POLLIN | POLLRDNORM))) {
1686 if (__vb2_init_fileio(q, 1))
1687 return res | POLLERR;
1689 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1690 (req_events & (POLLOUT | POLLWRNORM))) {
1691 if (__vb2_init_fileio(q, 0))
1692 return res | POLLERR;
1694 * Write to OUTPUT queue can be done immediately.
1696 return res | POLLOUT | POLLWRNORM;
1701 * There is nothing to wait for if no buffers have already been queued.
1703 if (list_empty(&q->queued_list))
1704 return res | POLLERR;
1706 poll_wait(file, &q->done_wq, wait);
1709 * Take first buffer available for dequeuing.
1711 spin_lock_irqsave(&q->done_lock, flags);
1712 if (!list_empty(&q->done_list))
1713 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1715 spin_unlock_irqrestore(&q->done_lock, flags);
1717 if (vb && (vb->state == VB2_BUF_STATE_DONE
1718 || vb->state == VB2_BUF_STATE_ERROR)) {
1719 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
1720 res | POLLOUT | POLLWRNORM :
1721 res | POLLIN | POLLRDNORM;
1725 EXPORT_SYMBOL_GPL(vb2_poll);
1728 * vb2_queue_init() - initialize a videobuf2 queue
1729 * @q: videobuf2 queue; this structure should be allocated in driver
1731 * The vb2_queue structure should be allocated by the driver. The driver is
1732 * responsible of clearing it's content and setting initial values for some
1733 * required entries before calling this function.
1734 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1735 * to the struct vb2_queue description in include/media/videobuf2-core.h
1736 * for more information.
1738 int vb2_queue_init(struct vb2_queue *q)
1745 WARN_ON(!q->mem_ops) ||
1746 WARN_ON(!q->type) ||
1747 WARN_ON(!q->io_modes) ||
1748 WARN_ON(!q->ops->queue_setup) ||
1749 WARN_ON(!q->ops->buf_queue))
1752 INIT_LIST_HEAD(&q->queued_list);
1753 INIT_LIST_HEAD(&q->done_list);
1754 spin_lock_init(&q->done_lock);
1755 init_waitqueue_head(&q->done_wq);
1757 if (q->buf_struct_size == 0)
1758 q->buf_struct_size = sizeof(struct vb2_buffer);
1762 EXPORT_SYMBOL_GPL(vb2_queue_init);
1765 * vb2_queue_release() - stop streaming, release the queue and free memory
1766 * @q: videobuf2 queue
1768 * This function stops streaming and performs necessary clean ups, including
1769 * freeing video buffer memory. The driver is responsible for freeing
1770 * the vb2_queue structure itself.
1772 void vb2_queue_release(struct vb2_queue *q)
1774 __vb2_cleanup_fileio(q);
1775 __vb2_queue_cancel(q);
1776 __vb2_queue_free(q, q->num_buffers);
1778 EXPORT_SYMBOL_GPL(vb2_queue_release);
1781 * struct vb2_fileio_buf - buffer context used by file io emulator
1783 * vb2 provides a compatibility layer and emulator of file io (read and
1784 * write) calls on top of streaming API. This structure is used for
1785 * tracking context related to the buffers.
1787 struct vb2_fileio_buf {
1791 unsigned int queued:1;
1795 * struct vb2_fileio_data - queue context used by file io emulator
1797 * vb2 provides a compatibility layer and emulator of file io (read and
1798 * write) calls on top of streaming API. For proper operation it required
1799 * this structure to save the driver state between each call of the read
1800 * or write function.
1802 struct vb2_fileio_data {
1803 struct v4l2_requestbuffers req;
1804 struct v4l2_buffer b;
1805 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1807 unsigned int q_count;
1808 unsigned int dq_count;
1813 * __vb2_init_fileio() - initialize file io emulator
1814 * @q: videobuf2 queue
1815 * @read: mode selector (1 means read, 0 means write)
1817 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1819 struct vb2_fileio_data *fileio;
1821 unsigned int count = 0;
1826 if ((read && !(q->io_modes & VB2_READ)) ||
1827 (!read && !(q->io_modes & VB2_WRITE)))
1831 * Check if device supports mapping buffers to kernel virtual space.
1833 if (!q->mem_ops->vaddr)
1837 * Check if streaming api has not been already activated.
1839 if (q->streaming || q->num_buffers > 0)
1843 * Start with count 1, driver can increase it in queue_setup()
1847 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1848 (read) ? "read" : "write", count, q->io_flags);
1850 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1854 fileio->flags = q->io_flags;
1857 * Request buffers and use MMAP type to force driver
1858 * to allocate buffers by itself.
1860 fileio->req.count = count;
1861 fileio->req.memory = V4L2_MEMORY_MMAP;
1862 fileio->req.type = q->type;
1863 ret = vb2_reqbufs(q, &fileio->req);
1868 * Check if plane_count is correct
1869 * (multiplane buffers are not supported).
1871 if (q->bufs[0]->num_planes != 1) {
1877 * Get kernel address of each buffer.
1879 for (i = 0; i < q->num_buffers; i++) {
1880 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1881 if (fileio->bufs[i].vaddr == NULL)
1883 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1887 * Read mode requires pre queuing of all buffers.
1891 * Queue all buffers.
1893 for (i = 0; i < q->num_buffers; i++) {
1894 struct v4l2_buffer *b = &fileio->b;
1895 memset(b, 0, sizeof(*b));
1897 b->memory = q->memory;
1899 ret = vb2_qbuf(q, b);
1902 fileio->bufs[i].queued = 1;
1908 ret = vb2_streamon(q, q->type);
1918 fileio->req.count = 0;
1919 vb2_reqbufs(q, &fileio->req);
1927 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1928 * @q: videobuf2 queue
1930 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1932 struct vb2_fileio_data *fileio = q->fileio;
1936 * Hack fileio context to enable direct calls to vb2 ioctl
1941 vb2_streamoff(q, q->type);
1942 fileio->req.count = 0;
1943 vb2_reqbufs(q, &fileio->req);
1945 dprintk(3, "file io emulator closed\n");
1951 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1952 * @q: videobuf2 queue
1953 * @data: pointed to target userspace buffer
1954 * @count: number of bytes to read or write
1955 * @ppos: file handle position tracking pointer
1956 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1957 * @read: access mode selector (1 means read, 0 means write)
1959 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1960 loff_t *ppos, int nonblock, int read)
1962 struct vb2_fileio_data *fileio;
1963 struct vb2_fileio_buf *buf;
1966 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1967 read ? "read" : "write", (long)*ppos, count,
1968 nonblock ? "non" : "");
1974 * Initialize emulator on first call.
1977 ret = __vb2_init_fileio(q, read);
1978 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1985 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1986 * The pointer will be restored before returning from this function.
1990 index = fileio->index;
1991 buf = &fileio->bufs[index];
1994 * Check if we need to dequeue the buffer.
1997 struct vb2_buffer *vb;
2000 * Call vb2_dqbuf to get buffer back.
2002 memset(&fileio->b, 0, sizeof(fileio->b));
2003 fileio->b.type = q->type;
2004 fileio->b.memory = q->memory;
2005 fileio->b.index = index;
2006 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2007 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2010 fileio->dq_count += 1;
2013 * Get number of bytes filled by the driver
2015 vb = q->bufs[index];
2016 buf->size = vb2_get_plane_payload(vb, 0);
2021 * Limit count on last few bytes of the buffer.
2023 if (buf->pos + count > buf->size) {
2024 count = buf->size - buf->pos;
2025 dprintk(5, "reducing read count: %zd\n", count);
2029 * Transfer data to userspace.
2031 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2032 count, index, buf->pos);
2034 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2036 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2038 dprintk(3, "file io: error copying data\n");
2050 * Queue next buffer if required.
2052 if (buf->pos == buf->size ||
2053 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2055 * Check if this is the last buffer to read.
2057 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2058 fileio->dq_count == 1) {
2059 dprintk(3, "file io: read limit reached\n");
2061 * Restore fileio pointer and release the context.
2064 return __vb2_cleanup_fileio(q);
2068 * Call vb2_qbuf and give buffer to the driver.
2070 memset(&fileio->b, 0, sizeof(fileio->b));
2071 fileio->b.type = q->type;
2072 fileio->b.memory = q->memory;
2073 fileio->b.index = index;
2074 fileio->b.bytesused = buf->pos;
2075 ret = vb2_qbuf(q, &fileio->b);
2076 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2081 * Buffer has been queued, update the status
2085 buf->size = q->bufs[0]->v4l2_planes[0].length;
2086 fileio->q_count += 1;
2089 * Switch to the next buffer
2091 fileio->index = (index + 1) % q->num_buffers;
2094 * Start streaming if required.
2096 if (!read && !q->streaming) {
2097 ret = vb2_streamon(q, q->type);
2104 * Return proper number of bytes processed.
2110 * Restore the fileio context and block vb2 ioctl interface.
2116 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2117 loff_t *ppos, int nonblocking)
2119 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2121 EXPORT_SYMBOL_GPL(vb2_read);
2123 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2124 loff_t *ppos, int nonblocking)
2126 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2128 EXPORT_SYMBOL_GPL(vb2_write);
2132 * The following functions are not part of the vb2 core API, but are helper
2133 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2134 * and struct vb2_ops.
2135 * They contain boilerplate code that most if not all drivers have to do
2136 * and so they simplify the driver code.
2139 /* The queue is busy if there is a owner and you are not that owner. */
2140 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2142 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2145 /* vb2 ioctl helpers */
2147 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2148 struct v4l2_requestbuffers *p)
2150 struct video_device *vdev = video_devdata(file);
2151 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2155 if (vb2_queue_is_busy(vdev, file))
2157 res = __reqbufs(vdev->queue, p);
2158 /* If count == 0, then the owner has released all buffers and he
2159 is no longer owner of the queue. Otherwise we have a new owner. */
2161 vdev->queue->owner = p->count ? file->private_data : NULL;
2164 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2166 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2167 struct v4l2_create_buffers *p)
2169 struct video_device *vdev = video_devdata(file);
2170 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2172 p->index = vdev->queue->num_buffers;
2173 /* If count == 0, then just check if memory and type are valid.
2174 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2176 return res != -EBUSY ? res : 0;
2179 if (vb2_queue_is_busy(vdev, file))
2181 res = __create_bufs(vdev->queue, p);
2183 vdev->queue->owner = file->private_data;
2186 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2188 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2189 struct v4l2_buffer *p)
2191 struct video_device *vdev = video_devdata(file);
2193 if (vb2_queue_is_busy(vdev, file))
2195 return vb2_prepare_buf(vdev->queue, p);
2197 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2199 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2201 struct video_device *vdev = video_devdata(file);
2203 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2204 return vb2_querybuf(vdev->queue, p);
2206 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2208 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2210 struct video_device *vdev = video_devdata(file);
2212 if (vb2_queue_is_busy(vdev, file))
2214 return vb2_qbuf(vdev->queue, p);
2216 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2218 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2220 struct video_device *vdev = video_devdata(file);
2222 if (vb2_queue_is_busy(vdev, file))
2224 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2226 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2228 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2230 struct video_device *vdev = video_devdata(file);
2232 if (vb2_queue_is_busy(vdev, file))
2234 return vb2_streamon(vdev->queue, i);
2236 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2238 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2240 struct video_device *vdev = video_devdata(file);
2242 if (vb2_queue_is_busy(vdev, file))
2244 return vb2_streamoff(vdev->queue, i);
2246 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2248 /* v4l2_file_operations helpers */
2250 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2252 struct video_device *vdev = video_devdata(file);
2254 return vb2_mmap(vdev->queue, vma);
2256 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2258 int vb2_fop_release(struct file *file)
2260 struct video_device *vdev = video_devdata(file);
2262 if (file->private_data == vdev->queue->owner) {
2263 vb2_queue_release(vdev->queue);
2264 vdev->queue->owner = NULL;
2266 return v4l2_fh_release(file);
2268 EXPORT_SYMBOL_GPL(vb2_fop_release);
2270 ssize_t vb2_fop_write(struct file *file, char __user *buf,
2271 size_t count, loff_t *ppos)
2273 struct video_device *vdev = video_devdata(file);
2274 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2277 if (lock && mutex_lock_interruptible(lock))
2278 return -ERESTARTSYS;
2279 if (vb2_queue_is_busy(vdev, file))
2281 err = vb2_write(vdev->queue, buf, count, ppos,
2282 file->f_flags & O_NONBLOCK);
2283 if (vdev->queue->fileio)
2284 vdev->queue->owner = file->private_data;
2290 EXPORT_SYMBOL_GPL(vb2_fop_write);
2292 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2293 size_t count, loff_t *ppos)
2295 struct video_device *vdev = video_devdata(file);
2296 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2299 if (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);
2305 if (vdev->queue->fileio)
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 bool must_lock = false;
2324 /* Try to be smart: only lock if polling might start fileio,
2325 otherwise locking will only introduce unwanted delays. */
2326 if (q->num_buffers == 0 && q->fileio == NULL) {
2327 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2328 (req_events & (POLLIN | POLLRDNORM)))
2330 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2331 (req_events & (POLLOUT | POLLWRNORM)))
2335 /* If locking is needed, but this helper doesn't know how, then you
2336 shouldn't be using this helper but you should write your own. */
2337 WARN_ON(must_lock && !lock);
2339 if (must_lock && lock && mutex_lock_interruptible(lock))
2344 res = vb2_poll(vdev->queue, file, wait);
2346 /* If fileio was started, then we have a new queue owner. */
2347 if (must_lock && !fileio && q->fileio)
2348 q->owner = file->private_data;
2349 if (must_lock && lock)
2353 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2356 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2357 unsigned long len, unsigned long pgoff, unsigned long flags)
2359 struct video_device *vdev = video_devdata(file);
2361 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2363 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2366 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2368 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2370 mutex_unlock(vq->lock);
2372 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2374 void vb2_ops_wait_finish(struct vb2_queue *vq)
2376 mutex_lock(vq->lock);
2378 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2380 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2381 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2382 MODULE_LICENSE("GPL");