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)
44 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
46 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
48 struct vb2_queue *q = vb->vb2_queue;
52 /* Allocate memory for all planes in this buffer */
53 for (plane = 0; plane < vb->num_planes; ++plane) {
54 mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
55 q->plane_sizes[plane]);
56 if (IS_ERR_OR_NULL(mem_priv))
59 /* Associate allocator private data with this plane */
60 vb->planes[plane].mem_priv = mem_priv;
61 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
66 /* Free already allocated memory if one of the allocations failed */
67 for (; plane > 0; --plane)
68 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
74 * __vb2_buf_mem_free() - free memory of the given buffer
76 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
78 struct vb2_queue *q = vb->vb2_queue;
81 for (plane = 0; plane < vb->num_planes; ++plane) {
82 call_memop(q, plane, put, vb->planes[plane].mem_priv);
83 vb->planes[plane].mem_priv = NULL;
84 dprintk(3, "Freed plane %d of buffer %d\n",
85 plane, vb->v4l2_buf.index);
90 * __vb2_buf_userptr_put() - release userspace memory associated with
93 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
95 struct vb2_queue *q = vb->vb2_queue;
98 for (plane = 0; plane < vb->num_planes; ++plane) {
99 void *mem_priv = vb->planes[plane].mem_priv;
102 call_memop(q, plane, put_userptr, mem_priv);
103 vb->planes[plane].mem_priv = NULL;
109 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
110 * every buffer on the queue
112 static void __setup_offsets(struct vb2_queue *q)
114 unsigned int buffer, plane;
115 struct vb2_buffer *vb;
116 unsigned long off = 0;
118 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
119 vb = q->bufs[buffer];
123 for (plane = 0; plane < vb->num_planes; ++plane) {
124 vb->v4l2_planes[plane].m.mem_offset = off;
126 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
129 off += vb->v4l2_planes[plane].length;
130 off = PAGE_ALIGN(off);
136 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
137 * video buffer memory for all buffers/planes on the queue and initializes the
140 * Returns the number of buffers successfully allocated.
142 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
143 unsigned int num_buffers, unsigned int num_planes)
146 struct vb2_buffer *vb;
149 for (buffer = 0; buffer < num_buffers; ++buffer) {
150 /* Allocate videobuf buffer structures */
151 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
153 dprintk(1, "Memory alloc for buffer struct failed\n");
157 /* Length stores number of planes for multiplanar buffers */
158 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
159 vb->v4l2_buf.length = num_planes;
161 vb->state = VB2_BUF_STATE_DEQUEUED;
163 vb->num_planes = num_planes;
164 vb->v4l2_buf.index = buffer;
165 vb->v4l2_buf.type = q->type;
166 vb->v4l2_buf.memory = memory;
168 /* Allocate video buffer memory for the MMAP type */
169 if (memory == V4L2_MEMORY_MMAP) {
170 ret = __vb2_buf_mem_alloc(vb);
172 dprintk(1, "Failed allocating memory for "
173 "buffer %d\n", buffer);
178 * Call the driver-provided buffer initialization
179 * callback, if given. An error in initialization
180 * results in queue setup failure.
182 ret = call_qop(q, buf_init, vb);
184 dprintk(1, "Buffer %d %p initialization"
185 " failed\n", buffer, vb);
186 __vb2_buf_mem_free(vb);
192 q->bufs[buffer] = vb;
195 q->num_buffers = buffer;
199 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
200 q->num_buffers, num_planes);
206 * __vb2_free_mem() - release all video buffer memory for a given queue
208 static void __vb2_free_mem(struct vb2_queue *q)
211 struct vb2_buffer *vb;
213 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
214 vb = q->bufs[buffer];
218 /* Free MMAP buffers or release USERPTR buffers */
219 if (q->memory == V4L2_MEMORY_MMAP)
220 __vb2_buf_mem_free(vb);
222 __vb2_buf_userptr_put(vb);
227 * __vb2_queue_free() - free the queue - video memory and related information
228 * and return the queue to an uninitialized state. Might be called even if the
229 * queue has already been freed.
231 static void __vb2_queue_free(struct vb2_queue *q)
235 /* Call driver-provided cleanup function for each buffer, if provided */
236 if (q->ops->buf_cleanup) {
237 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
238 if (NULL == q->bufs[buffer])
240 q->ops->buf_cleanup(q->bufs[buffer]);
244 /* Release video buffer memory */
247 /* Free videobuf buffers */
248 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
249 kfree(q->bufs[buffer]);
250 q->bufs[buffer] = NULL;
258 * __verify_planes_array() - verify that the planes array passed in struct
259 * v4l2_buffer from userspace can be safely used
261 static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b)
263 /* Is memory for copying plane information present? */
264 if (NULL == b->m.planes) {
265 dprintk(1, "Multi-planar buffer passed but "
266 "planes array not provided\n");
270 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
271 dprintk(1, "Incorrect planes array length, "
272 "expected %d, got %d\n", vb->num_planes, b->length);
280 * __buffer_in_use() - return true if the buffer is in use and
281 * the queue cannot be freed (by the means of REQBUFS(0)) call
283 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
286 for (plane = 0; plane < vb->num_planes; ++plane) {
288 * If num_users() has not been provided, call_memop
289 * will return 0, apparently nobody cares about this
290 * case anyway. If num_users() returns more than 1,
291 * we are not the only user of the plane's memory.
293 if (call_memop(q, plane, num_users,
294 vb->planes[plane].mem_priv) > 1)
301 * __buffers_in_use() - return true if any buffers on the queue are in use and
302 * the queue cannot be freed (by the means of REQBUFS(0)) call
304 static bool __buffers_in_use(struct vb2_queue *q)
307 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
308 if (__buffer_in_use(q, q->bufs[buffer]))
315 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
316 * returned to userspace
318 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
320 struct vb2_queue *q = vb->vb2_queue;
323 /* Copy back data such as timestamp, flags, input, etc. */
324 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
325 b->input = vb->v4l2_buf.input;
326 b->reserved = vb->v4l2_buf.reserved;
328 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
329 ret = __verify_planes_array(vb, b);
334 * Fill in plane-related data if userspace provided an array
335 * for it. The memory and size is verified above.
337 memcpy(b->m.planes, vb->v4l2_planes,
338 b->length * sizeof(struct v4l2_plane));
341 * We use length and offset in v4l2_planes array even for
342 * single-planar buffers, but userspace does not.
344 b->length = vb->v4l2_planes[0].length;
345 b->bytesused = vb->v4l2_planes[0].bytesused;
346 if (q->memory == V4L2_MEMORY_MMAP)
347 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
348 else if (q->memory == V4L2_MEMORY_USERPTR)
349 b->m.userptr = vb->v4l2_planes[0].m.userptr;
353 * Clear any buffer state related flags.
355 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
358 case VB2_BUF_STATE_QUEUED:
359 case VB2_BUF_STATE_ACTIVE:
360 b->flags |= V4L2_BUF_FLAG_QUEUED;
362 case VB2_BUF_STATE_ERROR:
363 b->flags |= V4L2_BUF_FLAG_ERROR;
365 case VB2_BUF_STATE_DONE:
366 b->flags |= V4L2_BUF_FLAG_DONE;
368 case VB2_BUF_STATE_DEQUEUED:
369 case VB2_BUF_STATE_PREPARED:
374 if (__buffer_in_use(q, vb))
375 b->flags |= V4L2_BUF_FLAG_MAPPED;
381 * vb2_querybuf() - query video buffer information
383 * @b: buffer struct passed from userspace to vidioc_querybuf handler
386 * Should be called from vidioc_querybuf ioctl handler in driver.
387 * This function will verify the passed v4l2_buffer structure and fill the
388 * relevant information for the userspace.
390 * The return values from this function are intended to be directly returned
391 * from vidioc_querybuf handler in driver.
393 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
395 struct vb2_buffer *vb;
397 if (b->type != q->type) {
398 dprintk(1, "querybuf: wrong buffer type\n");
402 if (b->index >= q->num_buffers) {
403 dprintk(1, "querybuf: buffer index out of range\n");
406 vb = q->bufs[b->index];
408 return __fill_v4l2_buffer(vb, b);
410 EXPORT_SYMBOL(vb2_querybuf);
413 * __verify_userptr_ops() - verify that all memory operations required for
414 * USERPTR queue type have been provided
416 static int __verify_userptr_ops(struct vb2_queue *q)
418 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
419 !q->mem_ops->put_userptr)
426 * __verify_mmap_ops() - verify that all memory operations required for
427 * MMAP queue type have been provided
429 static int __verify_mmap_ops(struct vb2_queue *q)
431 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
432 !q->mem_ops->put || !q->mem_ops->mmap)
439 * vb2_reqbufs() - Initiate streaming
440 * @q: videobuf2 queue
441 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
443 * Should be called from vidioc_reqbufs ioctl handler of a driver.
445 * 1) verifies streaming parameters passed from the userspace,
446 * 2) sets up the queue,
447 * 3) negotiates number of buffers and planes per buffer with the driver
448 * to be used during streaming,
449 * 4) allocates internal buffer structures (struct vb2_buffer), according to
450 * the agreed parameters,
451 * 5) for MMAP memory type, allocates actual video memory, using the
452 * memory handling/allocation routines provided during queue initialization
454 * If req->count is 0, all the memory will be freed instead.
455 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
456 * and the queue is not busy, memory will be reallocated.
458 * The return values from this function are intended to be directly returned
459 * from vidioc_reqbufs handler in driver.
461 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
463 unsigned int num_buffers, num_planes;
467 dprintk(1, "reqbufs: file io in progress\n");
471 if (req->memory != V4L2_MEMORY_MMAP
472 && req->memory != V4L2_MEMORY_USERPTR) {
473 dprintk(1, "reqbufs: unsupported memory type\n");
477 if (req->type != q->type) {
478 dprintk(1, "reqbufs: requested type is incorrect\n");
483 dprintk(1, "reqbufs: streaming active\n");
488 * Make sure all the required memory ops for given memory type
491 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
492 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
496 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
497 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
501 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
503 * We already have buffers allocated, so first check if they
504 * are not in use and can be freed.
506 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
507 dprintk(1, "reqbufs: memory in use, cannot free\n");
514 * In case of REQBUFS(0) return immediately without calling
515 * driver's queue_setup() callback and allocating resources.
522 * Make sure the requested values and current defaults are sane.
524 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
525 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
526 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
527 q->memory = req->memory;
530 * Ask the driver how many buffers and planes per buffer it requires.
531 * Driver also sets the size and allocator context for each plane.
533 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
534 q->plane_sizes, q->alloc_ctx);
538 /* Finally, allocate buffers and video memory */
539 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
541 dprintk(1, "Memory allocation failed\n");
546 * Check if driver can handle the allocated number of buffers.
548 if (ret < num_buffers) {
549 unsigned int orig_num_buffers;
551 orig_num_buffers = num_buffers = ret;
552 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
553 q->plane_sizes, q->alloc_ctx);
557 if (orig_num_buffers < num_buffers) {
563 * Ok, driver accepted smaller number of buffers.
569 * Return the number of successfully allocated buffers
580 EXPORT_SYMBOL_GPL(vb2_reqbufs);
583 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
584 * @vb: vb2_buffer to which the plane in question belongs to
585 * @plane_no: plane number for which the address is to be returned
587 * This function returns a kernel virtual address of a given plane if
588 * such a mapping exist, NULL otherwise.
590 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
592 struct vb2_queue *q = vb->vb2_queue;
594 if (plane_no > vb->num_planes)
597 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
600 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
603 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
604 * @vb: vb2_buffer to which the plane in question belongs to
605 * @plane_no: plane number for which the cookie is to be returned
607 * This function returns an allocator specific cookie for a given plane if
608 * available, NULL otherwise. The allocator should provide some simple static
609 * inline function, which would convert this cookie to the allocator specific
610 * type that can be used directly by the driver to access the buffer. This can
611 * be for example physical address, pointer to scatter list or IOMMU mapping.
613 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
615 struct vb2_queue *q = vb->vb2_queue;
617 if (plane_no > vb->num_planes)
620 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
622 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
625 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
626 * @vb: vb2_buffer returned from the driver
627 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
628 * or VB2_BUF_STATE_ERROR if the operation finished with an error
630 * This function should be called by the driver after a hardware operation on
631 * a buffer is finished and the buffer may be returned to userspace. The driver
632 * cannot use this buffer anymore until it is queued back to it by videobuf
633 * by the means of buf_queue callback. Only buffers previously queued to the
634 * driver by buf_queue can be passed to this function.
636 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
638 struct vb2_queue *q = vb->vb2_queue;
641 if (vb->state != VB2_BUF_STATE_ACTIVE)
644 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
647 dprintk(4, "Done processing on buffer %d, state: %d\n",
648 vb->v4l2_buf.index, vb->state);
650 /* Add the buffer to the done buffers list */
651 spin_lock_irqsave(&q->done_lock, flags);
653 list_add_tail(&vb->done_entry, &q->done_list);
654 atomic_dec(&q->queued_count);
655 spin_unlock_irqrestore(&q->done_lock, flags);
657 /* Inform any processes that may be waiting for buffers */
658 wake_up(&q->done_wq);
660 EXPORT_SYMBOL_GPL(vb2_buffer_done);
663 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
664 * a v4l2_buffer by the userspace
666 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
667 struct v4l2_plane *v4l2_planes)
672 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
674 * Verify that the userspace gave us a valid array for
677 ret = __verify_planes_array(vb, b);
681 /* Fill in driver-provided information for OUTPUT types */
682 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
684 * Will have to go up to b->length when API starts
685 * accepting variable number of planes.
687 for (plane = 0; plane < vb->num_planes; ++plane) {
688 v4l2_planes[plane].bytesused =
689 b->m.planes[plane].bytesused;
690 v4l2_planes[plane].data_offset =
691 b->m.planes[plane].data_offset;
695 if (b->memory == V4L2_MEMORY_USERPTR) {
696 for (plane = 0; plane < vb->num_planes; ++plane) {
697 v4l2_planes[plane].m.userptr =
698 b->m.planes[plane].m.userptr;
699 v4l2_planes[plane].length =
700 b->m.planes[plane].length;
705 * Single-planar buffers do not use planes array,
706 * so fill in relevant v4l2_buffer struct fields instead.
707 * In videobuf we use our internal V4l2_planes struct for
708 * single-planar buffers as well, for simplicity.
710 if (V4L2_TYPE_IS_OUTPUT(b->type))
711 v4l2_planes[0].bytesused = b->bytesused;
713 if (b->memory == V4L2_MEMORY_USERPTR) {
714 v4l2_planes[0].m.userptr = b->m.userptr;
715 v4l2_planes[0].length = b->length;
719 vb->v4l2_buf.field = b->field;
720 vb->v4l2_buf.timestamp = b->timestamp;
721 vb->v4l2_buf.input = b->input;
722 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
728 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
730 static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
732 struct v4l2_plane planes[VIDEO_MAX_PLANES];
733 struct vb2_queue *q = vb->vb2_queue;
737 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
739 /* Verify and copy relevant information provided by the userspace */
740 ret = __fill_vb2_buffer(vb, b, planes);
744 for (plane = 0; plane < vb->num_planes; ++plane) {
745 /* Skip the plane if already verified */
746 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
747 && vb->v4l2_planes[plane].length == planes[plane].length)
750 dprintk(3, "qbuf: userspace address for plane %d changed, "
751 "reacquiring memory\n", plane);
753 /* Check if the provided plane buffer is large enough */
754 if (planes[plane].length < q->plane_sizes[plane]) {
759 /* Release previously acquired memory if present */
760 if (vb->planes[plane].mem_priv)
761 call_memop(q, plane, put_userptr,
762 vb->planes[plane].mem_priv);
764 vb->planes[plane].mem_priv = NULL;
765 vb->v4l2_planes[plane].m.userptr = 0;
766 vb->v4l2_planes[plane].length = 0;
768 /* Acquire each plane's memory */
769 if (q->mem_ops->get_userptr) {
770 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
771 planes[plane].m.userptr,
772 planes[plane].length,
774 if (IS_ERR(mem_priv)) {
775 dprintk(1, "qbuf: failed acquiring userspace "
776 "memory for plane %d\n", plane);
777 ret = PTR_ERR(mem_priv);
780 vb->planes[plane].mem_priv = mem_priv;
785 * Call driver-specific initialization on the newly acquired buffer,
788 ret = call_qop(q, buf_init, vb);
790 dprintk(1, "qbuf: buffer initialization failed\n");
795 * Now that everything is in order, copy relevant information
796 * provided by userspace.
798 for (plane = 0; plane < vb->num_planes; ++plane)
799 vb->v4l2_planes[plane] = planes[plane];
803 /* In case of errors, release planes that were already acquired */
804 for (plane = 0; plane < vb->num_planes; ++plane) {
805 if (vb->planes[plane].mem_priv)
806 call_memop(q, plane, put_userptr,
807 vb->planes[plane].mem_priv);
808 vb->planes[plane].mem_priv = NULL;
809 vb->v4l2_planes[plane].m.userptr = 0;
810 vb->v4l2_planes[plane].length = 0;
817 * __qbuf_mmap() - handle qbuf of an MMAP buffer
819 static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
821 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
825 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
827 static void __enqueue_in_driver(struct vb2_buffer *vb)
829 struct vb2_queue *q = vb->vb2_queue;
831 vb->state = VB2_BUF_STATE_ACTIVE;
832 atomic_inc(&q->queued_count);
833 q->ops->buf_queue(vb);
836 static int __buf_prepare(struct vb2_buffer *vb, struct v4l2_buffer *b)
838 struct vb2_queue *q = vb->vb2_queue;
842 case V4L2_MEMORY_MMAP:
843 ret = __qbuf_mmap(vb, b);
845 case V4L2_MEMORY_USERPTR:
846 ret = __qbuf_userptr(vb, b);
849 WARN(1, "Invalid queue type\n");
854 ret = call_qop(q, buf_prepare, vb);
856 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
858 vb->state = VB2_BUF_STATE_PREPARED;
864 * vb2_qbuf() - Queue a buffer from userspace
865 * @q: videobuf2 queue
866 * @b: buffer structure passed from userspace to vidioc_qbuf handler
869 * Should be called from vidioc_qbuf ioctl handler of a driver.
871 * 1) verifies the passed buffer,
872 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
873 * which driver-specific buffer initialization can be performed,
874 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
875 * callback for processing.
877 * The return values from this function are intended to be directly returned
878 * from vidioc_qbuf handler in driver.
880 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
882 struct vb2_buffer *vb;
886 dprintk(1, "qbuf: file io in progress\n");
890 if (b->type != q->type) {
891 dprintk(1, "qbuf: invalid buffer type\n");
895 if (b->index >= q->num_buffers) {
896 dprintk(1, "qbuf: buffer index out of range\n");
900 vb = q->bufs[b->index];
902 /* Should never happen */
903 dprintk(1, "qbuf: buffer is NULL\n");
907 if (b->memory != q->memory) {
908 dprintk(1, "qbuf: invalid memory type\n");
913 case VB2_BUF_STATE_DEQUEUED:
914 ret = __buf_prepare(vb, b);
917 case VB2_BUF_STATE_PREPARED:
920 dprintk(1, "qbuf: buffer already in use\n");
925 * Add to the queued buffers list, a buffer will stay on it until
928 list_add_tail(&vb->queued_entry, &q->queued_list);
929 vb->state = VB2_BUF_STATE_QUEUED;
932 * If already streaming, give the buffer to driver for processing.
933 * If not, the buffer will be given to driver on next streamon.
936 __enqueue_in_driver(vb);
938 /* Fill buffer information for the userspace */
939 __fill_v4l2_buffer(vb, b);
941 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
944 EXPORT_SYMBOL_GPL(vb2_qbuf);
947 * __vb2_wait_for_done_vb() - wait for a buffer to become available
950 * Will sleep if required for nonblocking == false.
952 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
955 * All operations on vb_done_list are performed under done_lock
956 * spinlock protection. However, buffers may be removed from
957 * it and returned to userspace only while holding both driver's
958 * lock and the done_lock spinlock. Thus we can be sure that as
959 * long as we hold the driver's lock, the list will remain not
960 * empty if list_empty() check succeeds.
967 dprintk(1, "Streaming off, will not wait for buffers\n");
971 if (!list_empty(&q->done_list)) {
973 * Found a buffer that we were waiting for.
979 dprintk(1, "Nonblocking and no buffers to dequeue, "
985 * We are streaming and blocking, wait for another buffer to
986 * become ready or for streamoff. Driver's lock is released to
987 * allow streamoff or qbuf to be called while waiting.
989 call_qop(q, wait_prepare, q);
992 * All locks have been released, it is safe to sleep now.
994 dprintk(3, "Will sleep waiting for buffers\n");
995 ret = wait_event_interruptible(q->done_wq,
996 !list_empty(&q->done_list) || !q->streaming);
999 * We need to reevaluate both conditions again after reacquiring
1000 * the locks or return an error if one occurred.
1002 call_qop(q, wait_finish, q);
1010 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1012 * Will sleep if required for nonblocking == false.
1014 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1017 unsigned long flags;
1021 * Wait for at least one buffer to become available on the done_list.
1023 ret = __vb2_wait_for_done_vb(q, nonblocking);
1028 * Driver's lock has been held since we last verified that done_list
1029 * is not empty, so no need for another list_empty(done_list) check.
1031 spin_lock_irqsave(&q->done_lock, flags);
1032 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1033 list_del(&(*vb)->done_entry);
1034 spin_unlock_irqrestore(&q->done_lock, flags);
1040 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1041 * @q: videobuf2 queue
1043 * This function will wait until all buffers that have been given to the driver
1044 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1045 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1046 * taken, for example from stop_streaming() callback.
1048 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1050 if (!q->streaming) {
1051 dprintk(1, "Streaming off, will not wait for buffers\n");
1055 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1058 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1061 * vb2_dqbuf() - Dequeue a buffer to the userspace
1062 * @q: videobuf2 queue
1063 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1065 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1066 * buffers ready for dequeuing are present. Normally the driver
1067 * would be passing (file->f_flags & O_NONBLOCK) here
1069 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1071 * 1) verifies the passed buffer,
1072 * 2) calls buf_finish callback in the driver (if provided), in which
1073 * driver can perform any additional operations that may be required before
1074 * returning the buffer to userspace, such as cache sync,
1075 * 3) the buffer struct members are filled with relevant information for
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_dqbuf handler in driver.
1081 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1083 struct vb2_buffer *vb = NULL;
1087 dprintk(1, "dqbuf: file io in progress\n");
1091 if (b->type != q->type) {
1092 dprintk(1, "dqbuf: invalid buffer type\n");
1096 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1098 dprintk(1, "dqbuf: error getting next done buffer\n");
1102 ret = call_qop(q, buf_finish, vb);
1104 dprintk(1, "dqbuf: buffer finish failed\n");
1108 switch (vb->state) {
1109 case VB2_BUF_STATE_DONE:
1110 dprintk(3, "dqbuf: Returning done buffer\n");
1112 case VB2_BUF_STATE_ERROR:
1113 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1116 dprintk(1, "dqbuf: Invalid buffer state\n");
1120 /* Fill buffer information for the userspace */
1121 __fill_v4l2_buffer(vb, b);
1122 /* Remove from videobuf queue */
1123 list_del(&vb->queued_entry);
1125 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1126 vb->v4l2_buf.index, vb->state);
1128 vb->state = VB2_BUF_STATE_DEQUEUED;
1131 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1134 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1136 * Removes all queued buffers from driver's queue and all buffers queued by
1137 * userspace from videobuf's queue. Returns to state after reqbufs.
1139 static void __vb2_queue_cancel(struct vb2_queue *q)
1144 * Tell driver to stop all transactions and release all queued
1148 call_qop(q, stop_streaming, q);
1152 * Remove all buffers from videobuf's list...
1154 INIT_LIST_HEAD(&q->queued_list);
1156 * ...and done list; userspace will not receive any buffers it
1157 * has not already dequeued before initiating cancel.
1159 INIT_LIST_HEAD(&q->done_list);
1160 atomic_set(&q->queued_count, 0);
1161 wake_up_all(&q->done_wq);
1164 * Reinitialize all buffers for next use.
1166 for (i = 0; i < q->num_buffers; ++i)
1167 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1171 * vb2_streamon - start streaming
1172 * @q: videobuf2 queue
1173 * @type: type argument passed from userspace to vidioc_streamon handler
1175 * Should be called from vidioc_streamon handler of a driver.
1177 * 1) verifies current state
1178 * 2) passes any previously queued buffers to the driver and starts streaming
1180 * The return values from this function are intended to be directly returned
1181 * from vidioc_streamon handler in the driver.
1183 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1185 struct vb2_buffer *vb;
1189 dprintk(1, "streamon: file io in progress\n");
1193 if (type != q->type) {
1194 dprintk(1, "streamon: invalid stream type\n");
1199 dprintk(1, "streamon: already streaming\n");
1204 * If any buffers were queued before streamon,
1205 * we can now pass them to driver for processing.
1207 list_for_each_entry(vb, &q->queued_list, queued_entry)
1208 __enqueue_in_driver(vb);
1211 * Let driver notice that streaming state has been enabled.
1213 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1215 dprintk(1, "streamon: driver refused to start streaming\n");
1216 __vb2_queue_cancel(q);
1222 dprintk(3, "Streamon successful\n");
1225 EXPORT_SYMBOL_GPL(vb2_streamon);
1229 * vb2_streamoff - stop streaming
1230 * @q: videobuf2 queue
1231 * @type: type argument passed from userspace to vidioc_streamoff handler
1233 * Should be called from vidioc_streamoff handler of a driver.
1235 * 1) verifies current state,
1236 * 2) stop streaming and dequeues any queued buffers, including those previously
1237 * passed to the driver (after waiting for the driver to finish).
1239 * This call can be used for pausing playback.
1240 * The return values from this function are intended to be directly returned
1241 * from vidioc_streamoff handler in the driver
1243 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1246 dprintk(1, "streamoff: file io in progress\n");
1250 if (type != q->type) {
1251 dprintk(1, "streamoff: invalid stream type\n");
1255 if (!q->streaming) {
1256 dprintk(1, "streamoff: not streaming\n");
1261 * Cancel will pause streaming and remove all buffers from the driver
1262 * and videobuf, effectively returning control over them to userspace.
1264 __vb2_queue_cancel(q);
1266 dprintk(3, "Streamoff successful\n");
1269 EXPORT_SYMBOL_GPL(vb2_streamoff);
1272 * __find_plane_by_offset() - find plane associated with the given offset off
1274 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1275 unsigned int *_buffer, unsigned int *_plane)
1277 struct vb2_buffer *vb;
1278 unsigned int buffer, plane;
1281 * Go over all buffers and their planes, comparing the given offset
1282 * with an offset assigned to each plane. If a match is found,
1283 * return its buffer and plane numbers.
1285 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1286 vb = q->bufs[buffer];
1288 for (plane = 0; plane < vb->num_planes; ++plane) {
1289 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1301 * vb2_mmap() - map video buffers into application address space
1302 * @q: videobuf2 queue
1303 * @vma: vma passed to the mmap file operation handler in the driver
1305 * Should be called from mmap file operation handler of a driver.
1306 * This function maps one plane of one of the available video buffers to
1307 * userspace. To map whole video memory allocated on reqbufs, this function
1308 * has to be called once per each plane per each buffer previously allocated.
1310 * When the userspace application calls mmap, it passes to it an offset returned
1311 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1312 * a "cookie", which is then used to identify the plane to be mapped.
1313 * This function finds a plane with a matching offset and a mapping is performed
1314 * by the means of a provided memory operation.
1316 * The return values from this function are intended to be directly returned
1317 * from the mmap handler in driver.
1319 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1321 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1322 struct vb2_plane *vb_plane;
1323 struct vb2_buffer *vb;
1324 unsigned int buffer, plane;
1327 if (q->memory != V4L2_MEMORY_MMAP) {
1328 dprintk(1, "Queue is not currently set up for mmap\n");
1333 * Check memory area access mode.
1335 if (!(vma->vm_flags & VM_SHARED)) {
1336 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1339 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1340 if (!(vma->vm_flags & VM_WRITE)) {
1341 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1345 if (!(vma->vm_flags & VM_READ)) {
1346 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1352 * Find the plane corresponding to the offset passed by userspace.
1354 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1358 vb = q->bufs[buffer];
1359 vb_plane = &vb->planes[plane];
1361 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1365 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1368 EXPORT_SYMBOL_GPL(vb2_mmap);
1370 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1371 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1374 * vb2_poll() - implements poll userspace operation
1375 * @q: videobuf2 queue
1376 * @file: file argument passed to the poll file operation handler
1377 * @wait: wait argument passed to the poll file operation handler
1379 * This function implements poll file operation handler for a driver.
1380 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1381 * be informed that the file descriptor of a video device is available for
1383 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1384 * will be reported as available for writing.
1386 * The return values from this function are intended to be directly returned
1387 * from poll handler in driver.
1389 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1391 unsigned long flags;
1393 struct vb2_buffer *vb = NULL;
1396 * Start file I/O emulator only if streaming API has not been used yet.
1398 if (q->num_buffers == 0 && q->fileio == NULL) {
1399 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1400 ret = __vb2_init_fileio(q, 1);
1404 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1405 ret = __vb2_init_fileio(q, 0);
1409 * Write to OUTPUT queue can be done immediately.
1411 return POLLOUT | POLLWRNORM;
1416 * There is nothing to wait for if no buffers have already been queued.
1418 if (list_empty(&q->queued_list))
1421 poll_wait(file, &q->done_wq, wait);
1424 * Take first buffer available for dequeuing.
1426 spin_lock_irqsave(&q->done_lock, flags);
1427 if (!list_empty(&q->done_list))
1428 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1430 spin_unlock_irqrestore(&q->done_lock, flags);
1432 if (vb && (vb->state == VB2_BUF_STATE_DONE
1433 || vb->state == VB2_BUF_STATE_ERROR)) {
1434 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1435 POLLIN | POLLRDNORM;
1439 EXPORT_SYMBOL_GPL(vb2_poll);
1442 * vb2_queue_init() - initialize a videobuf2 queue
1443 * @q: videobuf2 queue; this structure should be allocated in driver
1445 * The vb2_queue structure should be allocated by the driver. The driver is
1446 * responsible of clearing it's content and setting initial values for some
1447 * required entries before calling this function.
1448 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1449 * to the struct vb2_queue description in include/media/videobuf2-core.h
1450 * for more information.
1452 int vb2_queue_init(struct vb2_queue *q)
1456 BUG_ON(!q->mem_ops);
1458 BUG_ON(!q->io_modes);
1460 BUG_ON(!q->ops->queue_setup);
1461 BUG_ON(!q->ops->buf_queue);
1463 INIT_LIST_HEAD(&q->queued_list);
1464 INIT_LIST_HEAD(&q->done_list);
1465 spin_lock_init(&q->done_lock);
1466 init_waitqueue_head(&q->done_wq);
1468 if (q->buf_struct_size == 0)
1469 q->buf_struct_size = sizeof(struct vb2_buffer);
1473 EXPORT_SYMBOL_GPL(vb2_queue_init);
1476 * vb2_queue_release() - stop streaming, release the queue and free memory
1477 * @q: videobuf2 queue
1479 * This function stops streaming and performs necessary clean ups, including
1480 * freeing video buffer memory. The driver is responsible for freeing
1481 * the vb2_queue structure itself.
1483 void vb2_queue_release(struct vb2_queue *q)
1485 __vb2_cleanup_fileio(q);
1486 __vb2_queue_cancel(q);
1487 __vb2_queue_free(q);
1489 EXPORT_SYMBOL_GPL(vb2_queue_release);
1492 * struct vb2_fileio_buf - buffer context used by file io emulator
1494 * vb2 provides a compatibility layer and emulator of file io (read and
1495 * write) calls on top of streaming API. This structure is used for
1496 * tracking context related to the buffers.
1498 struct vb2_fileio_buf {
1502 unsigned int queued:1;
1506 * struct vb2_fileio_data - queue context used by file io emulator
1508 * vb2 provides a compatibility layer and emulator of file io (read and
1509 * write) calls on top of streaming API. For proper operation it required
1510 * this structure to save the driver state between each call of the read
1511 * or write function.
1513 struct vb2_fileio_data {
1514 struct v4l2_requestbuffers req;
1515 struct v4l2_buffer b;
1516 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1518 unsigned int q_count;
1519 unsigned int dq_count;
1524 * __vb2_init_fileio() - initialize file io emulator
1525 * @q: videobuf2 queue
1526 * @read: mode selector (1 means read, 0 means write)
1528 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1530 struct vb2_fileio_data *fileio;
1532 unsigned int count = 0;
1537 if ((read && !(q->io_modes & VB2_READ)) ||
1538 (!read && !(q->io_modes & VB2_WRITE)))
1542 * Check if device supports mapping buffers to kernel virtual space.
1544 if (!q->mem_ops->vaddr)
1548 * Check if streaming api has not been already activated.
1550 if (q->streaming || q->num_buffers > 0)
1554 * Start with count 1, driver can increase it in queue_setup()
1558 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1559 (read) ? "read" : "write", count, q->io_flags);
1561 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1565 fileio->flags = q->io_flags;
1568 * Request buffers and use MMAP type to force driver
1569 * to allocate buffers by itself.
1571 fileio->req.count = count;
1572 fileio->req.memory = V4L2_MEMORY_MMAP;
1573 fileio->req.type = q->type;
1574 ret = vb2_reqbufs(q, &fileio->req);
1579 * Check if plane_count is correct
1580 * (multiplane buffers are not supported).
1582 if (q->bufs[0]->num_planes != 1) {
1583 fileio->req.count = 0;
1589 * Get kernel address of each buffer.
1591 for (i = 0; i < q->num_buffers; i++) {
1592 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1593 if (fileio->bufs[i].vaddr == NULL)
1595 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1599 * Read mode requires pre queuing of all buffers.
1603 * Queue all buffers.
1605 for (i = 0; i < q->num_buffers; i++) {
1606 struct v4l2_buffer *b = &fileio->b;
1607 memset(b, 0, sizeof(*b));
1609 b->memory = q->memory;
1611 ret = vb2_qbuf(q, b);
1614 fileio->bufs[i].queued = 1;
1620 ret = vb2_streamon(q, q->type);
1630 vb2_reqbufs(q, &fileio->req);
1638 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1639 * @q: videobuf2 queue
1641 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1643 struct vb2_fileio_data *fileio = q->fileio;
1647 * Hack fileio context to enable direct calls to vb2 ioctl
1652 vb2_streamoff(q, q->type);
1653 fileio->req.count = 0;
1654 vb2_reqbufs(q, &fileio->req);
1656 dprintk(3, "file io emulator closed\n");
1662 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1663 * @q: videobuf2 queue
1664 * @data: pointed to target userspace buffer
1665 * @count: number of bytes to read or write
1666 * @ppos: file handle position tracking pointer
1667 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1668 * @read: access mode selector (1 means read, 0 means write)
1670 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1671 loff_t *ppos, int nonblock, int read)
1673 struct vb2_fileio_data *fileio;
1674 struct vb2_fileio_buf *buf;
1677 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1678 read ? "read" : "write", (long)*ppos, count,
1679 nonblock ? "non" : "");
1685 * Initialize emulator on first call.
1688 ret = __vb2_init_fileio(q, read);
1689 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1696 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1697 * The pointer will be restored before returning from this function.
1701 index = fileio->index;
1702 buf = &fileio->bufs[index];
1705 * Check if we need to dequeue the buffer.
1708 struct vb2_buffer *vb;
1711 * Call vb2_dqbuf to get buffer back.
1713 memset(&fileio->b, 0, sizeof(fileio->b));
1714 fileio->b.type = q->type;
1715 fileio->b.memory = q->memory;
1716 fileio->b.index = index;
1717 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1718 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1721 fileio->dq_count += 1;
1724 * Get number of bytes filled by the driver
1726 vb = q->bufs[index];
1727 buf->size = vb2_get_plane_payload(vb, 0);
1732 * Limit count on last few bytes of the buffer.
1734 if (buf->pos + count > buf->size) {
1735 count = buf->size - buf->pos;
1736 dprintk(5, "reducing read count: %zd\n", count);
1740 * Transfer data to userspace.
1742 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1743 count, index, buf->pos);
1745 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1747 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1749 dprintk(3, "file io: error copying data\n");
1761 * Queue next buffer if required.
1763 if (buf->pos == buf->size ||
1764 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1766 * Check if this is the last buffer to read.
1768 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
1769 fileio->dq_count == 1) {
1770 dprintk(3, "file io: read limit reached\n");
1772 * Restore fileio pointer and release the context.
1775 return __vb2_cleanup_fileio(q);
1779 * Call vb2_qbuf and give buffer to the driver.
1781 memset(&fileio->b, 0, sizeof(fileio->b));
1782 fileio->b.type = q->type;
1783 fileio->b.memory = q->memory;
1784 fileio->b.index = index;
1785 fileio->b.bytesused = buf->pos;
1786 ret = vb2_qbuf(q, &fileio->b);
1787 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
1792 * Buffer has been queued, update the status
1796 buf->size = q->bufs[0]->v4l2_planes[0].length;
1797 fileio->q_count += 1;
1800 * Switch to the next buffer
1802 fileio->index = (index + 1) % q->num_buffers;
1805 * Start streaming if required.
1807 if (!read && !q->streaming) {
1808 ret = vb2_streamon(q, q->type);
1815 * Return proper number of bytes processed.
1821 * Restore the fileio context and block vb2 ioctl interface.
1827 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1828 loff_t *ppos, int nonblocking)
1830 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1832 EXPORT_SYMBOL_GPL(vb2_read);
1834 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
1835 loff_t *ppos, int nonblocking)
1837 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
1839 EXPORT_SYMBOL_GPL(vb2_write);
1841 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1842 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1843 MODULE_LICENSE("GPL");