2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/videobuf2-core.h>
25 module_param(debug, int, 0644);
27 #define dprintk(level, fmt, arg...) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
33 #define call_memop(q, plane, op, args...) \
34 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
37 #define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
40 #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
41 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
42 V4L2_BUF_FLAG_PREPARED)
45 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
47 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
49 struct vb2_queue *q = vb->vb2_queue;
53 /* Allocate memory for all planes in this buffer */
54 for (plane = 0; plane < vb->num_planes; ++plane) {
55 mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
56 q->plane_sizes[plane]);
57 if (IS_ERR_OR_NULL(mem_priv))
60 /* Associate allocator private data with this plane */
61 vb->planes[plane].mem_priv = mem_priv;
62 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
67 /* Free already allocated memory if one of the allocations failed */
68 for (; plane > 0; --plane)
69 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
75 * __vb2_buf_mem_free() - free memory of the given buffer
77 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
79 struct vb2_queue *q = vb->vb2_queue;
82 for (plane = 0; plane < vb->num_planes; ++plane) {
83 call_memop(q, plane, put, vb->planes[plane].mem_priv);
84 vb->planes[plane].mem_priv = NULL;
85 dprintk(3, "Freed plane %d of buffer %d\n",
86 plane, vb->v4l2_buf.index);
91 * __vb2_buf_userptr_put() - release userspace memory associated with
94 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
96 struct vb2_queue *q = vb->vb2_queue;
99 for (plane = 0; plane < vb->num_planes; ++plane) {
100 void *mem_priv = vb->planes[plane].mem_priv;
103 call_memop(q, plane, put_userptr, mem_priv);
104 vb->planes[plane].mem_priv = NULL;
110 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
111 * every buffer on the queue
113 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
115 unsigned int buffer, plane;
116 struct vb2_buffer *vb;
119 if (q->num_buffers) {
120 struct v4l2_plane *p;
121 vb = q->bufs[q->num_buffers - 1];
122 p = &vb->v4l2_planes[vb->num_planes - 1];
123 off = PAGE_ALIGN(p->m.mem_offset + p->length);
128 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
129 vb = q->bufs[buffer];
133 for (plane = 0; plane < vb->num_planes; ++plane) {
134 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
135 vb->v4l2_planes[plane].m.mem_offset = off;
137 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
140 off += vb->v4l2_planes[plane].length;
141 off = PAGE_ALIGN(off);
147 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
148 * video buffer memory for all buffers/planes on the queue and initializes the
151 * Returns the number of buffers successfully allocated.
153 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
154 unsigned int num_buffers, unsigned int num_planes)
157 struct vb2_buffer *vb;
160 for (buffer = 0; buffer < num_buffers; ++buffer) {
161 /* Allocate videobuf buffer structures */
162 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
164 dprintk(1, "Memory alloc for buffer struct failed\n");
168 /* Length stores number of planes for multiplanar buffers */
169 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
170 vb->v4l2_buf.length = num_planes;
172 vb->state = VB2_BUF_STATE_DEQUEUED;
174 vb->num_planes = num_planes;
175 vb->v4l2_buf.index = q->num_buffers + buffer;
176 vb->v4l2_buf.type = q->type;
177 vb->v4l2_buf.memory = memory;
179 /* Allocate video buffer memory for the MMAP type */
180 if (memory == V4L2_MEMORY_MMAP) {
181 ret = __vb2_buf_mem_alloc(vb);
183 dprintk(1, "Failed allocating memory for "
184 "buffer %d\n", buffer);
189 * Call the driver-provided buffer initialization
190 * callback, if given. An error in initialization
191 * results in queue setup failure.
193 ret = call_qop(q, buf_init, vb);
195 dprintk(1, "Buffer %d %p initialization"
196 " failed\n", buffer, vb);
197 __vb2_buf_mem_free(vb);
203 q->bufs[q->num_buffers + buffer] = vb;
206 __setup_offsets(q, buffer);
208 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
215 * __vb2_free_mem() - release all video buffer memory for a given queue
217 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
220 struct vb2_buffer *vb;
222 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
224 vb = q->bufs[buffer];
228 /* Free MMAP buffers or release USERPTR buffers */
229 if (q->memory == V4L2_MEMORY_MMAP)
230 __vb2_buf_mem_free(vb);
232 __vb2_buf_userptr_put(vb);
237 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
238 * related information, if no buffers are left return the queue to an
239 * uninitialized state. Might be called even if the queue has already been freed.
241 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
245 /* Call driver-provided cleanup function for each buffer, if provided */
246 if (q->ops->buf_cleanup) {
247 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
249 if (NULL == q->bufs[buffer])
251 q->ops->buf_cleanup(q->bufs[buffer]);
255 /* Release video buffer memory */
256 __vb2_free_mem(q, buffers);
258 /* Free videobuf buffers */
259 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
261 kfree(q->bufs[buffer]);
262 q->bufs[buffer] = NULL;
265 q->num_buffers -= buffers;
268 INIT_LIST_HEAD(&q->queued_list);
272 * __verify_planes_array() - verify that the planes array passed in struct
273 * v4l2_buffer from userspace can be safely used
275 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
277 /* Is memory for copying plane information present? */
278 if (NULL == b->m.planes) {
279 dprintk(1, "Multi-planar buffer passed but "
280 "planes array not provided\n");
284 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
285 dprintk(1, "Incorrect planes array length, "
286 "expected %d, got %d\n", vb->num_planes, b->length);
294 * __buffer_in_use() - return true if the buffer is in use and
295 * the queue cannot be freed (by the means of REQBUFS(0)) call
297 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
300 for (plane = 0; plane < vb->num_planes; ++plane) {
301 void *mem_priv = vb->planes[plane].mem_priv;
303 * If num_users() has not been provided, call_memop
304 * will return 0, apparently nobody cares about this
305 * case anyway. If num_users() returns more than 1,
306 * we are not the only user of the plane's memory.
308 if (mem_priv && call_memop(q, plane, num_users, mem_priv) > 1)
315 * __buffers_in_use() - return true if any buffers on the queue are in use and
316 * the queue cannot be freed (by the means of REQBUFS(0)) call
318 static bool __buffers_in_use(struct vb2_queue *q)
321 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
322 if (__buffer_in_use(q, q->bufs[buffer]))
329 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
330 * returned to userspace
332 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
334 struct vb2_queue *q = vb->vb2_queue;
337 /* Copy back data such as timestamp, flags, input, etc. */
338 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
339 b->input = vb->v4l2_buf.input;
340 b->reserved = vb->v4l2_buf.reserved;
342 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
343 ret = __verify_planes_array(vb, b);
348 * Fill in plane-related data if userspace provided an array
349 * for it. The memory and size is verified above.
351 memcpy(b->m.planes, vb->v4l2_planes,
352 b->length * sizeof(struct v4l2_plane));
355 * We use length and offset in v4l2_planes array even for
356 * single-planar buffers, but userspace does not.
358 b->length = vb->v4l2_planes[0].length;
359 b->bytesused = vb->v4l2_planes[0].bytesused;
360 if (q->memory == V4L2_MEMORY_MMAP)
361 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
362 else if (q->memory == V4L2_MEMORY_USERPTR)
363 b->m.userptr = vb->v4l2_planes[0].m.userptr;
367 * Clear any buffer state related flags.
369 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
372 case VB2_BUF_STATE_QUEUED:
373 case VB2_BUF_STATE_ACTIVE:
374 b->flags |= V4L2_BUF_FLAG_QUEUED;
376 case VB2_BUF_STATE_ERROR:
377 b->flags |= V4L2_BUF_FLAG_ERROR;
379 case VB2_BUF_STATE_DONE:
380 b->flags |= V4L2_BUF_FLAG_DONE;
382 case VB2_BUF_STATE_PREPARED:
383 b->flags |= V4L2_BUF_FLAG_PREPARED;
385 case VB2_BUF_STATE_DEQUEUED:
390 if (__buffer_in_use(q, vb))
391 b->flags |= V4L2_BUF_FLAG_MAPPED;
397 * vb2_querybuf() - query video buffer information
399 * @b: buffer struct passed from userspace to vidioc_querybuf handler
402 * Should be called from vidioc_querybuf ioctl handler in driver.
403 * This function will verify the passed v4l2_buffer structure and fill the
404 * relevant information for the userspace.
406 * The return values from this function are intended to be directly returned
407 * from vidioc_querybuf handler in driver.
409 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
411 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];
424 return __fill_v4l2_buffer(vb, b);
426 EXPORT_SYMBOL(vb2_querybuf);
429 * __verify_userptr_ops() - verify that all memory operations required for
430 * USERPTR queue type have been provided
432 static int __verify_userptr_ops(struct vb2_queue *q)
434 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
435 !q->mem_ops->put_userptr)
442 * __verify_mmap_ops() - verify that all memory operations required for
443 * MMAP queue type have been provided
445 static int __verify_mmap_ops(struct vb2_queue *q)
447 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
448 !q->mem_ops->put || !q->mem_ops->mmap)
455 * vb2_reqbufs() - Initiate streaming
456 * @q: videobuf2 queue
457 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
459 * Should be called from vidioc_reqbufs ioctl handler of a driver.
461 * 1) verifies streaming parameters passed from the userspace,
462 * 2) sets up the queue,
463 * 3) negotiates number of buffers and planes per buffer with the driver
464 * to be used during streaming,
465 * 4) allocates internal buffer structures (struct vb2_buffer), according to
466 * the agreed parameters,
467 * 5) for MMAP memory type, allocates actual video memory, using the
468 * memory handling/allocation routines provided during queue initialization
470 * If req->count is 0, all the memory will be freed instead.
471 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
472 * and the queue is not busy, memory will be reallocated.
474 * The return values from this function are intended to be directly returned
475 * from vidioc_reqbufs handler in driver.
477 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
479 unsigned int num_buffers, allocated_buffers, num_planes = 0;
483 dprintk(1, "reqbufs: file io in progress\n");
487 if (req->memory != V4L2_MEMORY_MMAP
488 && req->memory != V4L2_MEMORY_USERPTR) {
489 dprintk(1, "reqbufs: unsupported memory type\n");
493 if (req->type != q->type) {
494 dprintk(1, "reqbufs: requested type is incorrect\n");
499 dprintk(1, "reqbufs: streaming active\n");
504 * Make sure all the required memory ops for given memory type
507 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
508 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
512 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
513 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
517 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
519 * We already have buffers allocated, so first check if they
520 * are not in use and can be freed.
522 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
523 dprintk(1, "reqbufs: memory in use, cannot free\n");
527 __vb2_queue_free(q, q->num_buffers);
530 * In case of REQBUFS(0) return immediately without calling
531 * driver's queue_setup() callback and allocating resources.
538 * Make sure the requested values and current defaults are sane.
540 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
541 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
542 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
543 q->memory = req->memory;
546 * Ask the driver how many buffers and planes per buffer it requires.
547 * Driver also sets the size and allocator context for each plane.
549 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
550 q->plane_sizes, q->alloc_ctx);
554 /* Finally, allocate buffers and video memory */
555 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
557 dprintk(1, "Memory allocation failed\n");
561 allocated_buffers = ret;
564 * Check if driver can handle the allocated number of buffers.
566 if (allocated_buffers < num_buffers) {
567 num_buffers = allocated_buffers;
569 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
570 &num_planes, q->plane_sizes, q->alloc_ctx);
572 if (!ret && allocated_buffers < num_buffers)
576 * Either the driver has accepted a smaller number of buffers,
577 * or .queue_setup() returned an error
581 q->num_buffers = allocated_buffers;
584 __vb2_queue_free(q, allocated_buffers);
589 * Return the number of successfully allocated buffers
592 req->count = allocated_buffers;
596 EXPORT_SYMBOL_GPL(vb2_reqbufs);
599 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
600 * @q: videobuf2 queue
601 * @create: creation parameters, passed from userspace to vidioc_create_bufs
604 * Should be called from vidioc_create_bufs ioctl handler of a driver.
606 * 1) verifies parameter sanity
607 * 2) calls the .queue_setup() queue operation
608 * 3) performs any necessary memory allocations
610 * The return values from this function are intended to be directly returned
611 * from vidioc_create_bufs handler in driver.
613 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
615 unsigned int num_planes = 0, num_buffers, allocated_buffers;
619 dprintk(1, "%s(): file io in progress\n", __func__);
623 if (create->memory != V4L2_MEMORY_MMAP
624 && create->memory != V4L2_MEMORY_USERPTR) {
625 dprintk(1, "%s(): unsupported memory type\n", __func__);
629 if (create->format.type != q->type) {
630 dprintk(1, "%s(): requested type is incorrect\n", __func__);
635 * Make sure all the required memory ops for given memory type
638 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
639 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
643 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
644 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
648 if (q->num_buffers == VIDEO_MAX_FRAME) {
649 dprintk(1, "%s(): maximum number of buffers already allocated\n",
654 create->index = q->num_buffers;
656 if (!q->num_buffers) {
657 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
658 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
659 q->memory = create->memory;
662 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
665 * Ask the driver, whether the requested number of buffers, planes per
666 * buffer and their sizes are acceptable
668 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
669 &num_planes, q->plane_sizes, q->alloc_ctx);
673 /* Finally, allocate buffers and video memory */
674 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
677 dprintk(1, "Memory allocation failed with error: %d\n", ret);
681 allocated_buffers = ret;
684 * Check if driver can handle the so far allocated number of buffers.
686 if (ret < num_buffers) {
690 * q->num_buffers contains the total number of buffers, that the
691 * queue driver has set up
693 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
694 &num_planes, q->plane_sizes, q->alloc_ctx);
696 if (!ret && allocated_buffers < num_buffers)
700 * Either the driver has accepted a smaller number of buffers,
701 * or .queue_setup() returned an error
705 q->num_buffers += allocated_buffers;
708 __vb2_queue_free(q, allocated_buffers);
713 * Return the number of successfully allocated buffers
716 create->count = allocated_buffers;
720 EXPORT_SYMBOL_GPL(vb2_create_bufs);
723 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
724 * @vb: vb2_buffer to which the plane in question belongs to
725 * @plane_no: plane number for which the address is to be returned
727 * This function returns a kernel virtual address of a given plane if
728 * such a mapping exist, NULL otherwise.
730 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
732 struct vb2_queue *q = vb->vb2_queue;
734 if (plane_no > vb->num_planes)
737 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
740 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
743 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
744 * @vb: vb2_buffer to which the plane in question belongs to
745 * @plane_no: plane number for which the cookie is to be returned
747 * This function returns an allocator specific cookie for a given plane if
748 * available, NULL otherwise. The allocator should provide some simple static
749 * inline function, which would convert this cookie to the allocator specific
750 * type that can be used directly by the driver to access the buffer. This can
751 * be for example physical address, pointer to scatter list or IOMMU mapping.
753 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
755 struct vb2_queue *q = vb->vb2_queue;
757 if (plane_no > vb->num_planes)
760 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
762 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
765 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
766 * @vb: vb2_buffer returned from the driver
767 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
768 * or VB2_BUF_STATE_ERROR if the operation finished with an error
770 * This function should be called by the driver after a hardware operation on
771 * a buffer is finished and the buffer may be returned to userspace. The driver
772 * cannot use this buffer anymore until it is queued back to it by videobuf
773 * by the means of buf_queue callback. Only buffers previously queued to the
774 * driver by buf_queue can be passed to this function.
776 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
778 struct vb2_queue *q = vb->vb2_queue;
781 if (vb->state != VB2_BUF_STATE_ACTIVE)
784 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
787 dprintk(4, "Done processing on buffer %d, state: %d\n",
788 vb->v4l2_buf.index, vb->state);
790 /* Add the buffer to the done buffers list */
791 spin_lock_irqsave(&q->done_lock, flags);
793 list_add_tail(&vb->done_entry, &q->done_list);
794 atomic_dec(&q->queued_count);
795 spin_unlock_irqrestore(&q->done_lock, flags);
797 /* Inform any processes that may be waiting for buffers */
798 wake_up(&q->done_wq);
800 EXPORT_SYMBOL_GPL(vb2_buffer_done);
803 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
804 * a v4l2_buffer by the userspace
806 static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
807 struct v4l2_plane *v4l2_planes)
812 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
814 * Verify that the userspace gave us a valid array for
817 ret = __verify_planes_array(vb, b);
821 /* Fill in driver-provided information for OUTPUT types */
822 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
824 * Will have to go up to b->length when API starts
825 * accepting variable number of planes.
827 for (plane = 0; plane < vb->num_planes; ++plane) {
828 v4l2_planes[plane].bytesused =
829 b->m.planes[plane].bytesused;
830 v4l2_planes[plane].data_offset =
831 b->m.planes[plane].data_offset;
835 if (b->memory == V4L2_MEMORY_USERPTR) {
836 for (plane = 0; plane < vb->num_planes; ++plane) {
837 v4l2_planes[plane].m.userptr =
838 b->m.planes[plane].m.userptr;
839 v4l2_planes[plane].length =
840 b->m.planes[plane].length;
845 * Single-planar buffers do not use planes array,
846 * so fill in relevant v4l2_buffer struct fields instead.
847 * In videobuf we use our internal V4l2_planes struct for
848 * single-planar buffers as well, for simplicity.
850 if (V4L2_TYPE_IS_OUTPUT(b->type))
851 v4l2_planes[0].bytesused = b->bytesused;
853 if (b->memory == V4L2_MEMORY_USERPTR) {
854 v4l2_planes[0].m.userptr = b->m.userptr;
855 v4l2_planes[0].length = b->length;
859 vb->v4l2_buf.field = b->field;
860 vb->v4l2_buf.timestamp = b->timestamp;
861 vb->v4l2_buf.input = b->input;
862 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
868 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
870 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
872 struct v4l2_plane planes[VIDEO_MAX_PLANES];
873 struct vb2_queue *q = vb->vb2_queue;
877 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
879 /* Verify and copy relevant information provided by the userspace */
880 ret = __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 == planes[plane].m.userptr
887 && vb->v4l2_planes[plane].length == planes[plane].length)
890 dprintk(3, "qbuf: userspace address for plane %d changed, "
891 "reacquiring memory\n", plane);
893 /* Check if the provided plane buffer is large enough */
894 if (planes[plane].length < q->plane_sizes[plane]) {
899 /* Release previously acquired memory if present */
900 if (vb->planes[plane].mem_priv)
901 call_memop(q, plane, put_userptr,
902 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 if (q->mem_ops->get_userptr) {
910 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
911 planes[plane].m.userptr,
912 planes[plane].length,
914 if (IS_ERR(mem_priv)) {
915 dprintk(1, "qbuf: failed acquiring userspace "
916 "memory for plane %d\n", plane);
917 ret = PTR_ERR(mem_priv);
920 vb->planes[plane].mem_priv = mem_priv;
925 * Call driver-specific initialization on the newly acquired buffer,
928 ret = call_qop(q, buf_init, vb);
930 dprintk(1, "qbuf: buffer initialization failed\n");
935 * Now that everything is in order, copy relevant information
936 * provided by userspace.
938 for (plane = 0; plane < vb->num_planes; ++plane)
939 vb->v4l2_planes[plane] = planes[plane];
943 /* In case of errors, release planes that were already acquired */
944 for (plane = 0; plane < vb->num_planes; ++plane) {
945 if (vb->planes[plane].mem_priv)
946 call_memop(q, plane, put_userptr,
947 vb->planes[plane].mem_priv);
948 vb->planes[plane].mem_priv = NULL;
949 vb->v4l2_planes[plane].m.userptr = 0;
950 vb->v4l2_planes[plane].length = 0;
957 * __qbuf_mmap() - handle qbuf of an MMAP buffer
959 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
961 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
965 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
967 static void __enqueue_in_driver(struct vb2_buffer *vb)
969 struct vb2_queue *q = vb->vb2_queue;
971 vb->state = VB2_BUF_STATE_ACTIVE;
972 atomic_inc(&q->queued_count);
973 q->ops->buf_queue(vb);
976 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
978 struct vb2_queue *q = vb->vb2_queue;
982 case V4L2_MEMORY_MMAP:
983 ret = __qbuf_mmap(vb, b);
985 case V4L2_MEMORY_USERPTR:
986 ret = __qbuf_userptr(vb, b);
989 WARN(1, "Invalid queue type\n");
994 ret = call_qop(q, buf_prepare, vb);
996 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
998 vb->state = VB2_BUF_STATE_PREPARED;
1004 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1005 * @q: videobuf2 queue
1006 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1009 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1011 * 1) verifies the passed buffer,
1012 * 2) calls buf_prepare callback in the driver (if provided), in which
1013 * driver-specific buffer initialization can be performed,
1015 * The return values from this function are intended to be directly returned
1016 * from vidioc_prepare_buf handler in driver.
1018 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1020 struct vb2_buffer *vb;
1024 dprintk(1, "%s(): file io in progress\n", __func__);
1028 if (b->type != q->type) {
1029 dprintk(1, "%s(): invalid buffer type\n", __func__);
1033 if (b->index >= q->num_buffers) {
1034 dprintk(1, "%s(): buffer index out of range\n", __func__);
1038 vb = q->bufs[b->index];
1040 /* Should never happen */
1041 dprintk(1, "%s(): buffer is NULL\n", __func__);
1045 if (b->memory != q->memory) {
1046 dprintk(1, "%s(): invalid memory type\n", __func__);
1050 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1051 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1055 ret = __buf_prepare(vb, b);
1059 __fill_v4l2_buffer(vb, b);
1063 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1066 * vb2_qbuf() - Queue a buffer from userspace
1067 * @q: videobuf2 queue
1068 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1071 * Should be called from vidioc_qbuf ioctl handler of a driver.
1073 * 1) verifies the passed buffer,
1074 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1075 * which driver-specific buffer initialization can be performed,
1076 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1077 * callback for processing.
1079 * The return values from this function are intended to be directly returned
1080 * from vidioc_qbuf handler in driver.
1082 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1084 struct vb2_buffer *vb;
1088 dprintk(1, "qbuf: file io in progress\n");
1092 if (b->type != q->type) {
1093 dprintk(1, "qbuf: invalid buffer type\n");
1097 if (b->index >= q->num_buffers) {
1098 dprintk(1, "qbuf: buffer index out of range\n");
1102 vb = q->bufs[b->index];
1104 /* Should never happen */
1105 dprintk(1, "qbuf: buffer is NULL\n");
1109 if (b->memory != q->memory) {
1110 dprintk(1, "qbuf: invalid memory type\n");
1114 switch (vb->state) {
1115 case VB2_BUF_STATE_DEQUEUED:
1116 ret = __buf_prepare(vb, b);
1119 case VB2_BUF_STATE_PREPARED:
1122 dprintk(1, "qbuf: buffer already in use\n");
1127 * Add to the queued buffers list, a buffer will stay on it until
1128 * dequeued in dqbuf.
1130 list_add_tail(&vb->queued_entry, &q->queued_list);
1131 vb->state = VB2_BUF_STATE_QUEUED;
1134 * If already streaming, give the buffer to driver for processing.
1135 * If not, the buffer will be given to driver on next streamon.
1138 __enqueue_in_driver(vb);
1140 /* Fill buffer information for the userspace */
1141 __fill_v4l2_buffer(vb, b);
1143 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1146 EXPORT_SYMBOL_GPL(vb2_qbuf);
1149 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1152 * Will sleep if required for nonblocking == false.
1154 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1157 * All operations on vb_done_list are performed under done_lock
1158 * spinlock protection. However, buffers may be removed from
1159 * it and returned to userspace only while holding both driver's
1160 * lock and the done_lock spinlock. Thus we can be sure that as
1161 * long as we hold the driver's lock, the list will remain not
1162 * empty if list_empty() check succeeds.
1168 if (!q->streaming) {
1169 dprintk(1, "Streaming off, will not wait for buffers\n");
1173 if (!list_empty(&q->done_list)) {
1175 * Found a buffer that we were waiting for.
1181 dprintk(1, "Nonblocking and no buffers to dequeue, "
1187 * We are streaming and blocking, wait for another buffer to
1188 * become ready or for streamoff. Driver's lock is released to
1189 * allow streamoff or qbuf to be called while waiting.
1191 call_qop(q, wait_prepare, q);
1194 * All locks have been released, it is safe to sleep now.
1196 dprintk(3, "Will sleep waiting for buffers\n");
1197 ret = wait_event_interruptible(q->done_wq,
1198 !list_empty(&q->done_list) || !q->streaming);
1201 * We need to reevaluate both conditions again after reacquiring
1202 * the locks or return an error if one occurred.
1204 call_qop(q, wait_finish, q);
1212 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1214 * Will sleep if required for nonblocking == false.
1216 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1219 unsigned long flags;
1223 * Wait for at least one buffer to become available on the done_list.
1225 ret = __vb2_wait_for_done_vb(q, nonblocking);
1230 * Driver's lock has been held since we last verified that done_list
1231 * is not empty, so no need for another list_empty(done_list) check.
1233 spin_lock_irqsave(&q->done_lock, flags);
1234 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1235 list_del(&(*vb)->done_entry);
1236 spin_unlock_irqrestore(&q->done_lock, flags);
1242 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1243 * @q: videobuf2 queue
1245 * This function will wait until all buffers that have been given to the driver
1246 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1247 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1248 * taken, for example from stop_streaming() callback.
1250 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1252 if (!q->streaming) {
1253 dprintk(1, "Streaming off, will not wait for buffers\n");
1257 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1260 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1263 * vb2_dqbuf() - Dequeue a buffer to the userspace
1264 * @q: videobuf2 queue
1265 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1267 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1268 * buffers ready for dequeuing are present. Normally the driver
1269 * would be passing (file->f_flags & O_NONBLOCK) here
1271 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1273 * 1) verifies the passed buffer,
1274 * 2) calls buf_finish callback in the driver (if provided), in which
1275 * driver can perform any additional operations that may be required before
1276 * returning the buffer to userspace, such as cache sync,
1277 * 3) the buffer struct members are filled with relevant information for
1280 * The return values from this function are intended to be directly returned
1281 * from vidioc_dqbuf handler in driver.
1283 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1285 struct vb2_buffer *vb = NULL;
1289 dprintk(1, "dqbuf: file io in progress\n");
1293 if (b->type != q->type) {
1294 dprintk(1, "dqbuf: invalid buffer type\n");
1298 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1300 dprintk(1, "dqbuf: error getting next done buffer\n");
1304 ret = call_qop(q, buf_finish, vb);
1306 dprintk(1, "dqbuf: buffer finish failed\n");
1310 switch (vb->state) {
1311 case VB2_BUF_STATE_DONE:
1312 dprintk(3, "dqbuf: Returning done buffer\n");
1314 case VB2_BUF_STATE_ERROR:
1315 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1318 dprintk(1, "dqbuf: Invalid buffer state\n");
1322 /* Fill buffer information for the userspace */
1323 __fill_v4l2_buffer(vb, b);
1324 /* Remove from videobuf queue */
1325 list_del(&vb->queued_entry);
1327 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1328 vb->v4l2_buf.index, vb->state);
1330 vb->state = VB2_BUF_STATE_DEQUEUED;
1333 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1336 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1338 * Removes all queued buffers from driver's queue and all buffers queued by
1339 * userspace from videobuf's queue. Returns to state after reqbufs.
1341 static void __vb2_queue_cancel(struct vb2_queue *q)
1346 * Tell driver to stop all transactions and release all queued
1350 call_qop(q, stop_streaming, q);
1354 * Remove all buffers from videobuf's list...
1356 INIT_LIST_HEAD(&q->queued_list);
1358 * ...and done list; userspace will not receive any buffers it
1359 * has not already dequeued before initiating cancel.
1361 INIT_LIST_HEAD(&q->done_list);
1362 atomic_set(&q->queued_count, 0);
1363 wake_up_all(&q->done_wq);
1366 * Reinitialize all buffers for next use.
1368 for (i = 0; i < q->num_buffers; ++i)
1369 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1373 * vb2_streamon - start streaming
1374 * @q: videobuf2 queue
1375 * @type: type argument passed from userspace to vidioc_streamon handler
1377 * Should be called from vidioc_streamon handler of a driver.
1379 * 1) verifies current state
1380 * 2) passes any previously queued buffers to the driver and starts streaming
1382 * The return values from this function are intended to be directly returned
1383 * from vidioc_streamon handler in the driver.
1385 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1387 struct vb2_buffer *vb;
1391 dprintk(1, "streamon: file io in progress\n");
1395 if (type != q->type) {
1396 dprintk(1, "streamon: invalid stream type\n");
1401 dprintk(1, "streamon: already streaming\n");
1406 * If any buffers were queued before streamon,
1407 * we can now pass them to driver for processing.
1409 list_for_each_entry(vb, &q->queued_list, queued_entry)
1410 __enqueue_in_driver(vb);
1413 * Let driver notice that streaming state has been enabled.
1415 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1417 dprintk(1, "streamon: driver refused to start streaming\n");
1418 __vb2_queue_cancel(q);
1424 dprintk(3, "Streamon successful\n");
1427 EXPORT_SYMBOL_GPL(vb2_streamon);
1431 * vb2_streamoff - stop streaming
1432 * @q: videobuf2 queue
1433 * @type: type argument passed from userspace to vidioc_streamoff handler
1435 * Should be called from vidioc_streamoff handler of a driver.
1437 * 1) verifies current state,
1438 * 2) stop streaming and dequeues any queued buffers, including those previously
1439 * passed to the driver (after waiting for the driver to finish).
1441 * This call can be used for pausing playback.
1442 * The return values from this function are intended to be directly returned
1443 * from vidioc_streamoff handler in the driver
1445 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1448 dprintk(1, "streamoff: file io in progress\n");
1452 if (type != q->type) {
1453 dprintk(1, "streamoff: invalid stream type\n");
1457 if (!q->streaming) {
1458 dprintk(1, "streamoff: not streaming\n");
1463 * Cancel will pause streaming and remove all buffers from the driver
1464 * and videobuf, effectively returning control over them to userspace.
1466 __vb2_queue_cancel(q);
1468 dprintk(3, "Streamoff successful\n");
1471 EXPORT_SYMBOL_GPL(vb2_streamoff);
1474 * __find_plane_by_offset() - find plane associated with the given offset off
1476 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1477 unsigned int *_buffer, unsigned int *_plane)
1479 struct vb2_buffer *vb;
1480 unsigned int buffer, plane;
1483 * Go over all buffers and their planes, comparing the given offset
1484 * with an offset assigned to each plane. If a match is found,
1485 * return its buffer and plane numbers.
1487 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1488 vb = q->bufs[buffer];
1490 for (plane = 0; plane < vb->num_planes; ++plane) {
1491 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1503 * vb2_mmap() - map video buffers into application address space
1504 * @q: videobuf2 queue
1505 * @vma: vma passed to the mmap file operation handler in the driver
1507 * Should be called from mmap file operation handler of a driver.
1508 * This function maps one plane of one of the available video buffers to
1509 * userspace. To map whole video memory allocated on reqbufs, this function
1510 * has to be called once per each plane per each buffer previously allocated.
1512 * When the userspace application calls mmap, it passes to it an offset returned
1513 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1514 * a "cookie", which is then used to identify the plane to be mapped.
1515 * This function finds a plane with a matching offset and a mapping is performed
1516 * by the means of a provided memory operation.
1518 * The return values from this function are intended to be directly returned
1519 * from the mmap handler in driver.
1521 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1523 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1524 struct vb2_plane *vb_plane;
1525 struct vb2_buffer *vb;
1526 unsigned int buffer, plane;
1529 if (q->memory != V4L2_MEMORY_MMAP) {
1530 dprintk(1, "Queue is not currently set up for mmap\n");
1535 * Check memory area access mode.
1537 if (!(vma->vm_flags & VM_SHARED)) {
1538 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1541 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1542 if (!(vma->vm_flags & VM_WRITE)) {
1543 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1547 if (!(vma->vm_flags & VM_READ)) {
1548 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1554 * Find the plane corresponding to the offset passed by userspace.
1556 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1560 vb = q->bufs[buffer];
1561 vb_plane = &vb->planes[plane];
1563 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1567 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1570 EXPORT_SYMBOL_GPL(vb2_mmap);
1573 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1576 unsigned long pgoff,
1577 unsigned long flags)
1579 unsigned long off = pgoff << PAGE_SHIFT;
1580 struct vb2_buffer *vb;
1581 unsigned int buffer, plane;
1584 if (q->memory != V4L2_MEMORY_MMAP) {
1585 dprintk(1, "Queue is not currently set up for mmap\n");
1590 * Find the plane corresponding to the offset passed by userspace.
1592 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1596 vb = q->bufs[buffer];
1598 return (unsigned long)vb2_plane_vaddr(vb, plane);
1600 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1603 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1604 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1607 * vb2_poll() - implements poll userspace operation
1608 * @q: videobuf2 queue
1609 * @file: file argument passed to the poll file operation handler
1610 * @wait: wait argument passed to the poll file operation handler
1612 * This function implements poll file operation handler for a driver.
1613 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1614 * be informed that the file descriptor of a video device is available for
1616 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1617 * will be reported as available for writing.
1619 * The return values from this function are intended to be directly returned
1620 * from poll handler in driver.
1622 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1624 unsigned long flags;
1626 struct vb2_buffer *vb = NULL;
1629 * Start file I/O emulator only if streaming API has not been used yet.
1631 if (q->num_buffers == 0 && q->fileio == NULL) {
1632 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1633 ret = __vb2_init_fileio(q, 1);
1637 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1638 ret = __vb2_init_fileio(q, 0);
1642 * Write to OUTPUT queue can be done immediately.
1644 return POLLOUT | POLLWRNORM;
1649 * There is nothing to wait for if no buffers have already been queued.
1651 if (list_empty(&q->queued_list))
1654 poll_wait(file, &q->done_wq, wait);
1657 * Take first buffer available for dequeuing.
1659 spin_lock_irqsave(&q->done_lock, flags);
1660 if (!list_empty(&q->done_list))
1661 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1663 spin_unlock_irqrestore(&q->done_lock, flags);
1665 if (vb && (vb->state == VB2_BUF_STATE_DONE
1666 || vb->state == VB2_BUF_STATE_ERROR)) {
1667 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1668 POLLIN | POLLRDNORM;
1672 EXPORT_SYMBOL_GPL(vb2_poll);
1675 * vb2_queue_init() - initialize a videobuf2 queue
1676 * @q: videobuf2 queue; this structure should be allocated in driver
1678 * The vb2_queue structure should be allocated by the driver. The driver is
1679 * responsible of clearing it's content and setting initial values for some
1680 * required entries before calling this function.
1681 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1682 * to the struct vb2_queue description in include/media/videobuf2-core.h
1683 * for more information.
1685 int vb2_queue_init(struct vb2_queue *q)
1689 BUG_ON(!q->mem_ops);
1691 BUG_ON(!q->io_modes);
1693 BUG_ON(!q->ops->queue_setup);
1694 BUG_ON(!q->ops->buf_queue);
1696 INIT_LIST_HEAD(&q->queued_list);
1697 INIT_LIST_HEAD(&q->done_list);
1698 spin_lock_init(&q->done_lock);
1699 init_waitqueue_head(&q->done_wq);
1701 if (q->buf_struct_size == 0)
1702 q->buf_struct_size = sizeof(struct vb2_buffer);
1706 EXPORT_SYMBOL_GPL(vb2_queue_init);
1709 * vb2_queue_release() - stop streaming, release the queue and free memory
1710 * @q: videobuf2 queue
1712 * This function stops streaming and performs necessary clean ups, including
1713 * freeing video buffer memory. The driver is responsible for freeing
1714 * the vb2_queue structure itself.
1716 void vb2_queue_release(struct vb2_queue *q)
1718 __vb2_cleanup_fileio(q);
1719 __vb2_queue_cancel(q);
1720 __vb2_queue_free(q, q->num_buffers);
1722 EXPORT_SYMBOL_GPL(vb2_queue_release);
1725 * struct vb2_fileio_buf - buffer context used by file io emulator
1727 * vb2 provides a compatibility layer and emulator of file io (read and
1728 * write) calls on top of streaming API. This structure is used for
1729 * tracking context related to the buffers.
1731 struct vb2_fileio_buf {
1735 unsigned int queued:1;
1739 * struct vb2_fileio_data - queue context used by file io emulator
1741 * vb2 provides a compatibility layer and emulator of file io (read and
1742 * write) calls on top of streaming API. For proper operation it required
1743 * this structure to save the driver state between each call of the read
1744 * or write function.
1746 struct vb2_fileio_data {
1747 struct v4l2_requestbuffers req;
1748 struct v4l2_buffer b;
1749 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1751 unsigned int q_count;
1752 unsigned int dq_count;
1757 * __vb2_init_fileio() - initialize file io emulator
1758 * @q: videobuf2 queue
1759 * @read: mode selector (1 means read, 0 means write)
1761 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1763 struct vb2_fileio_data *fileio;
1765 unsigned int count = 0;
1770 if ((read && !(q->io_modes & VB2_READ)) ||
1771 (!read && !(q->io_modes & VB2_WRITE)))
1775 * Check if device supports mapping buffers to kernel virtual space.
1777 if (!q->mem_ops->vaddr)
1781 * Check if streaming api has not been already activated.
1783 if (q->streaming || q->num_buffers > 0)
1787 * Start with count 1, driver can increase it in queue_setup()
1791 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1792 (read) ? "read" : "write", count, q->io_flags);
1794 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1798 fileio->flags = q->io_flags;
1801 * Request buffers and use MMAP type to force driver
1802 * to allocate buffers by itself.
1804 fileio->req.count = count;
1805 fileio->req.memory = V4L2_MEMORY_MMAP;
1806 fileio->req.type = q->type;
1807 ret = vb2_reqbufs(q, &fileio->req);
1812 * Check if plane_count is correct
1813 * (multiplane buffers are not supported).
1815 if (q->bufs[0]->num_planes != 1) {
1816 fileio->req.count = 0;
1822 * Get kernel address of each buffer.
1824 for (i = 0; i < q->num_buffers; i++) {
1825 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1826 if (fileio->bufs[i].vaddr == NULL)
1828 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1832 * Read mode requires pre queuing of all buffers.
1836 * Queue all buffers.
1838 for (i = 0; i < q->num_buffers; i++) {
1839 struct v4l2_buffer *b = &fileio->b;
1840 memset(b, 0, sizeof(*b));
1842 b->memory = q->memory;
1844 ret = vb2_qbuf(q, b);
1847 fileio->bufs[i].queued = 1;
1853 ret = vb2_streamon(q, q->type);
1863 vb2_reqbufs(q, &fileio->req);
1871 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1872 * @q: videobuf2 queue
1874 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1876 struct vb2_fileio_data *fileio = q->fileio;
1880 * Hack fileio context to enable direct calls to vb2 ioctl
1885 vb2_streamoff(q, q->type);
1886 fileio->req.count = 0;
1887 vb2_reqbufs(q, &fileio->req);
1889 dprintk(3, "file io emulator closed\n");
1895 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1896 * @q: videobuf2 queue
1897 * @data: pointed to target userspace buffer
1898 * @count: number of bytes to read or write
1899 * @ppos: file handle position tracking pointer
1900 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1901 * @read: access mode selector (1 means read, 0 means write)
1903 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1904 loff_t *ppos, int nonblock, int read)
1906 struct vb2_fileio_data *fileio;
1907 struct vb2_fileio_buf *buf;
1910 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1911 read ? "read" : "write", (long)*ppos, count,
1912 nonblock ? "non" : "");
1918 * Initialize emulator on first call.
1921 ret = __vb2_init_fileio(q, read);
1922 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1929 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1930 * The pointer will be restored before returning from this function.
1934 index = fileio->index;
1935 buf = &fileio->bufs[index];
1938 * Check if we need to dequeue the buffer.
1941 struct vb2_buffer *vb;
1944 * Call vb2_dqbuf to get buffer back.
1946 memset(&fileio->b, 0, sizeof(fileio->b));
1947 fileio->b.type = q->type;
1948 fileio->b.memory = q->memory;
1949 fileio->b.index = index;
1950 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1951 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1954 fileio->dq_count += 1;
1957 * Get number of bytes filled by the driver
1959 vb = q->bufs[index];
1960 buf->size = vb2_get_plane_payload(vb, 0);
1965 * Limit count on last few bytes of the buffer.
1967 if (buf->pos + count > buf->size) {
1968 count = buf->size - buf->pos;
1969 dprintk(5, "reducing read count: %zd\n", count);
1973 * Transfer data to userspace.
1975 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1976 count, index, buf->pos);
1978 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1980 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1982 dprintk(3, "file io: error copying data\n");
1994 * Queue next buffer if required.
1996 if (buf->pos == buf->size ||
1997 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1999 * Check if this is the last buffer to read.
2001 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2002 fileio->dq_count == 1) {
2003 dprintk(3, "file io: read limit reached\n");
2005 * Restore fileio pointer and release the context.
2008 return __vb2_cleanup_fileio(q);
2012 * Call vb2_qbuf and give buffer to the driver.
2014 memset(&fileio->b, 0, sizeof(fileio->b));
2015 fileio->b.type = q->type;
2016 fileio->b.memory = q->memory;
2017 fileio->b.index = index;
2018 fileio->b.bytesused = buf->pos;
2019 ret = vb2_qbuf(q, &fileio->b);
2020 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2025 * Buffer has been queued, update the status
2029 buf->size = q->bufs[0]->v4l2_planes[0].length;
2030 fileio->q_count += 1;
2033 * Switch to the next buffer
2035 fileio->index = (index + 1) % q->num_buffers;
2038 * Start streaming if required.
2040 if (!read && !q->streaming) {
2041 ret = vb2_streamon(q, q->type);
2048 * Return proper number of bytes processed.
2054 * Restore the fileio context and block vb2 ioctl interface.
2060 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2061 loff_t *ppos, int nonblocking)
2063 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2065 EXPORT_SYMBOL_GPL(vb2_read);
2067 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2068 loff_t *ppos, int nonblocking)
2070 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2072 EXPORT_SYMBOL_GPL(vb2_write);
2074 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2075 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2076 MODULE_LICENSE("GPL");