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].m.mem_offset = off;
136 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
139 off += vb->v4l2_planes[plane].length;
140 off = PAGE_ALIGN(off);
146 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
147 * video buffer memory for all buffers/planes on the queue and initializes the
150 * Returns the number of buffers successfully allocated.
152 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
153 unsigned int num_buffers, unsigned int num_planes)
156 struct vb2_buffer *vb;
159 for (buffer = 0; buffer < num_buffers; ++buffer) {
160 /* Allocate videobuf buffer structures */
161 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
163 dprintk(1, "Memory alloc for buffer struct failed\n");
167 /* Length stores number of planes for multiplanar buffers */
168 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
169 vb->v4l2_buf.length = num_planes;
171 vb->state = VB2_BUF_STATE_DEQUEUED;
173 vb->num_planes = num_planes;
174 vb->v4l2_buf.index = q->num_buffers + buffer;
175 vb->v4l2_buf.type = q->type;
176 vb->v4l2_buf.memory = memory;
178 /* Allocate video buffer memory for the MMAP type */
179 if (memory == V4L2_MEMORY_MMAP) {
180 ret = __vb2_buf_mem_alloc(vb);
182 dprintk(1, "Failed allocating memory for "
183 "buffer %d\n", buffer);
188 * Call the driver-provided buffer initialization
189 * callback, if given. An error in initialization
190 * results in queue setup failure.
192 ret = call_qop(q, buf_init, vb);
194 dprintk(1, "Buffer %d %p initialization"
195 " failed\n", buffer, vb);
196 __vb2_buf_mem_free(vb);
202 q->bufs[q->num_buffers + buffer] = vb;
205 __setup_offsets(q, buffer);
207 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
214 * __vb2_free_mem() - release all video buffer memory for a given queue
216 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
219 struct vb2_buffer *vb;
221 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
223 vb = q->bufs[buffer];
227 /* Free MMAP buffers or release USERPTR buffers */
228 if (q->memory == V4L2_MEMORY_MMAP)
229 __vb2_buf_mem_free(vb);
231 __vb2_buf_userptr_put(vb);
236 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
237 * related information, if no buffers are left return the queue to an
238 * uninitialized state. Might be called even if the queue has already been freed.
240 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
244 /* Call driver-provided cleanup function for each buffer, if provided */
245 if (q->ops->buf_cleanup) {
246 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
248 if (NULL == q->bufs[buffer])
250 q->ops->buf_cleanup(q->bufs[buffer]);
254 /* Release video buffer memory */
255 __vb2_free_mem(q, buffers);
257 /* Free videobuf buffers */
258 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
260 kfree(q->bufs[buffer]);
261 q->bufs[buffer] = NULL;
264 q->num_buffers -= buffers;
270 * __verify_planes_array() - verify that the planes array passed in struct
271 * v4l2_buffer from userspace can be safely used
273 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
275 /* Is memory for copying plane information present? */
276 if (NULL == b->m.planes) {
277 dprintk(1, "Multi-planar buffer passed but "
278 "planes array not provided\n");
282 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
283 dprintk(1, "Incorrect planes array length, "
284 "expected %d, got %d\n", vb->num_planes, b->length);
292 * __buffer_in_use() - return true if the buffer is in use and
293 * the queue cannot be freed (by the means of REQBUFS(0)) call
295 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
298 for (plane = 0; plane < vb->num_planes; ++plane) {
300 * If num_users() has not been provided, call_memop
301 * will return 0, apparently nobody cares about this
302 * case anyway. If num_users() returns more than 1,
303 * we are not the only user of the plane's memory.
305 if (call_memop(q, plane, num_users,
306 vb->planes[plane].mem_priv) > 1)
313 * __buffers_in_use() - return true if any buffers on the queue are in use and
314 * the queue cannot be freed (by the means of REQBUFS(0)) call
316 static bool __buffers_in_use(struct vb2_queue *q)
319 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
320 if (__buffer_in_use(q, q->bufs[buffer]))
327 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
328 * returned to userspace
330 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
332 struct vb2_queue *q = vb->vb2_queue;
335 /* Copy back data such as timestamp, flags, input, etc. */
336 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
337 b->input = vb->v4l2_buf.input;
338 b->reserved = vb->v4l2_buf.reserved;
340 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
341 ret = __verify_planes_array(vb, b);
346 * Fill in plane-related data if userspace provided an array
347 * for it. The memory and size is verified above.
349 memcpy(b->m.planes, vb->v4l2_planes,
350 b->length * sizeof(struct v4l2_plane));
353 * We use length and offset in v4l2_planes array even for
354 * single-planar buffers, but userspace does not.
356 b->length = vb->v4l2_planes[0].length;
357 b->bytesused = vb->v4l2_planes[0].bytesused;
358 if (q->memory == V4L2_MEMORY_MMAP)
359 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
360 else if (q->memory == V4L2_MEMORY_USERPTR)
361 b->m.userptr = vb->v4l2_planes[0].m.userptr;
365 * Clear any buffer state related flags.
367 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
370 case VB2_BUF_STATE_QUEUED:
371 case VB2_BUF_STATE_ACTIVE:
372 b->flags |= V4L2_BUF_FLAG_QUEUED;
374 case VB2_BUF_STATE_ERROR:
375 b->flags |= V4L2_BUF_FLAG_ERROR;
377 case VB2_BUF_STATE_DONE:
378 b->flags |= V4L2_BUF_FLAG_DONE;
380 case VB2_BUF_STATE_PREPARED:
381 b->flags |= V4L2_BUF_FLAG_PREPARED;
383 case VB2_BUF_STATE_DEQUEUED:
388 if (__buffer_in_use(q, vb))
389 b->flags |= V4L2_BUF_FLAG_MAPPED;
395 * vb2_querybuf() - query video buffer information
397 * @b: buffer struct passed from userspace to vidioc_querybuf handler
400 * Should be called from vidioc_querybuf ioctl handler in driver.
401 * This function will verify the passed v4l2_buffer structure and fill the
402 * relevant information for the userspace.
404 * The return values from this function are intended to be directly returned
405 * from vidioc_querybuf handler in driver.
407 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
409 struct vb2_buffer *vb;
411 if (b->type != q->type) {
412 dprintk(1, "querybuf: wrong buffer type\n");
416 if (b->index >= q->num_buffers) {
417 dprintk(1, "querybuf: buffer index out of range\n");
420 vb = q->bufs[b->index];
422 return __fill_v4l2_buffer(vb, b);
424 EXPORT_SYMBOL(vb2_querybuf);
427 * __verify_userptr_ops() - verify that all memory operations required for
428 * USERPTR queue type have been provided
430 static int __verify_userptr_ops(struct vb2_queue *q)
432 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
433 !q->mem_ops->put_userptr)
440 * __verify_mmap_ops() - verify that all memory operations required for
441 * MMAP queue type have been provided
443 static int __verify_mmap_ops(struct vb2_queue *q)
445 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
446 !q->mem_ops->put || !q->mem_ops->mmap)
453 * vb2_reqbufs() - Initiate streaming
454 * @q: videobuf2 queue
455 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
457 * Should be called from vidioc_reqbufs ioctl handler of a driver.
459 * 1) verifies streaming parameters passed from the userspace,
460 * 2) sets up the queue,
461 * 3) negotiates number of buffers and planes per buffer with the driver
462 * to be used during streaming,
463 * 4) allocates internal buffer structures (struct vb2_buffer), according to
464 * the agreed parameters,
465 * 5) for MMAP memory type, allocates actual video memory, using the
466 * memory handling/allocation routines provided during queue initialization
468 * If req->count is 0, all the memory will be freed instead.
469 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
470 * and the queue is not busy, memory will be reallocated.
472 * The return values from this function are intended to be directly returned
473 * from vidioc_reqbufs handler in driver.
475 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
477 unsigned int num_buffers, allocated_buffers, num_planes = 0;
481 dprintk(1, "reqbufs: file io in progress\n");
485 if (req->memory != V4L2_MEMORY_MMAP
486 && req->memory != V4L2_MEMORY_USERPTR) {
487 dprintk(1, "reqbufs: unsupported memory type\n");
491 if (req->type != q->type) {
492 dprintk(1, "reqbufs: requested type is incorrect\n");
497 dprintk(1, "reqbufs: streaming active\n");
502 * Make sure all the required memory ops for given memory type
505 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
506 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
510 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
511 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
515 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
517 * We already have buffers allocated, so first check if they
518 * are not in use and can be freed.
520 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
521 dprintk(1, "reqbufs: memory in use, cannot free\n");
525 __vb2_queue_free(q, q->num_buffers);
528 * In case of REQBUFS(0) return immediately without calling
529 * driver's queue_setup() callback and allocating resources.
536 * Make sure the requested values and current defaults are sane.
538 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
539 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
540 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
541 q->memory = req->memory;
544 * Ask the driver how many buffers and planes per buffer it requires.
545 * Driver also sets the size and allocator context for each plane.
547 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
548 q->plane_sizes, q->alloc_ctx);
552 /* Finally, allocate buffers and video memory */
553 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
555 dprintk(1, "Memory allocation failed\n");
559 allocated_buffers = ret;
562 * Check if driver can handle the allocated number of buffers.
564 if (allocated_buffers < num_buffers) {
565 num_buffers = allocated_buffers;
567 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
568 &num_planes, q->plane_sizes, q->alloc_ctx);
570 if (!ret && allocated_buffers < num_buffers)
574 * Either the driver has accepted a smaller number of buffers,
575 * or .queue_setup() returned an error
579 q->num_buffers = allocated_buffers;
582 __vb2_queue_free(q, allocated_buffers);
587 * Return the number of successfully allocated buffers
590 req->count = allocated_buffers;
594 EXPORT_SYMBOL_GPL(vb2_reqbufs);
597 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
598 * @q: videobuf2 queue
599 * @create: creation parameters, passed from userspace to vidioc_create_bufs
602 * Should be called from vidioc_create_bufs ioctl handler of a driver.
604 * 1) verifies parameter sanity
605 * 2) calls the .queue_setup() queue operation
606 * 3) performs any necessary memory allocations
608 * The return values from this function are intended to be directly returned
609 * from vidioc_create_bufs handler in driver.
611 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
613 unsigned int num_planes = 0, num_buffers, allocated_buffers;
617 dprintk(1, "%s(): file io in progress\n", __func__);
621 if (create->memory != V4L2_MEMORY_MMAP
622 && create->memory != V4L2_MEMORY_USERPTR) {
623 dprintk(1, "%s(): unsupported memory type\n", __func__);
627 if (create->format.type != q->type) {
628 dprintk(1, "%s(): requested type is incorrect\n", __func__);
633 * Make sure all the required memory ops for given memory type
636 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
637 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
641 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
642 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
646 if (q->num_buffers == VIDEO_MAX_FRAME) {
647 dprintk(1, "%s(): maximum number of buffers already allocated\n",
652 create->index = q->num_buffers;
654 if (!q->num_buffers) {
655 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
656 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
657 q->memory = create->memory;
660 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
663 * Ask the driver, whether the requested number of buffers, planes per
664 * buffer and their sizes are acceptable
666 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
667 &num_planes, q->plane_sizes, q->alloc_ctx);
671 /* Finally, allocate buffers and video memory */
672 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
675 dprintk(1, "Memory allocation failed with error: %d\n", ret);
679 allocated_buffers = ret;
682 * Check if driver can handle the so far allocated number of buffers.
684 if (ret < num_buffers) {
688 * q->num_buffers contains the total number of buffers, that the
689 * queue driver has set up
691 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
692 &num_planes, q->plane_sizes, q->alloc_ctx);
694 if (!ret && allocated_buffers < num_buffers)
698 * Either the driver has accepted a smaller number of buffers,
699 * or .queue_setup() returned an error
703 q->num_buffers += allocated_buffers;
706 __vb2_queue_free(q, allocated_buffers);
711 * Return the number of successfully allocated buffers
714 create->count = allocated_buffers;
718 EXPORT_SYMBOL_GPL(vb2_create_bufs);
721 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
722 * @vb: vb2_buffer to which the plane in question belongs to
723 * @plane_no: plane number for which the address is to be returned
725 * This function returns a kernel virtual address of a given plane if
726 * such a mapping exist, NULL otherwise.
728 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
730 struct vb2_queue *q = vb->vb2_queue;
732 if (plane_no > vb->num_planes)
735 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
738 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
741 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
742 * @vb: vb2_buffer to which the plane in question belongs to
743 * @plane_no: plane number for which the cookie is to be returned
745 * This function returns an allocator specific cookie for a given plane if
746 * available, NULL otherwise. The allocator should provide some simple static
747 * inline function, which would convert this cookie to the allocator specific
748 * type that can be used directly by the driver to access the buffer. This can
749 * be for example physical address, pointer to scatter list or IOMMU mapping.
751 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
753 struct vb2_queue *q = vb->vb2_queue;
755 if (plane_no > vb->num_planes)
758 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
760 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
763 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
764 * @vb: vb2_buffer returned from the driver
765 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
766 * or VB2_BUF_STATE_ERROR if the operation finished with an error
768 * This function should be called by the driver after a hardware operation on
769 * a buffer is finished and the buffer may be returned to userspace. The driver
770 * cannot use this buffer anymore until it is queued back to it by videobuf
771 * by the means of buf_queue callback. Only buffers previously queued to the
772 * driver by buf_queue can be passed to this function.
774 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
776 struct vb2_queue *q = vb->vb2_queue;
779 if (vb->state != VB2_BUF_STATE_ACTIVE)
782 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
785 dprintk(4, "Done processing on buffer %d, state: %d\n",
786 vb->v4l2_buf.index, vb->state);
788 /* Add the buffer to the done buffers list */
789 spin_lock_irqsave(&q->done_lock, flags);
791 list_add_tail(&vb->done_entry, &q->done_list);
792 atomic_dec(&q->queued_count);
793 spin_unlock_irqrestore(&q->done_lock, flags);
795 /* Inform any processes that may be waiting for buffers */
796 wake_up(&q->done_wq);
798 EXPORT_SYMBOL_GPL(vb2_buffer_done);
801 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
802 * a v4l2_buffer by the userspace
804 static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
805 struct v4l2_plane *v4l2_planes)
810 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
812 * Verify that the userspace gave us a valid array for
815 ret = __verify_planes_array(vb, b);
819 /* Fill in driver-provided information for OUTPUT types */
820 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
822 * Will have to go up to b->length when API starts
823 * accepting variable number of planes.
825 for (plane = 0; plane < vb->num_planes; ++plane) {
826 v4l2_planes[plane].bytesused =
827 b->m.planes[plane].bytesused;
828 v4l2_planes[plane].data_offset =
829 b->m.planes[plane].data_offset;
833 if (b->memory == V4L2_MEMORY_USERPTR) {
834 for (plane = 0; plane < vb->num_planes; ++plane) {
835 v4l2_planes[plane].m.userptr =
836 b->m.planes[plane].m.userptr;
837 v4l2_planes[plane].length =
838 b->m.planes[plane].length;
843 * Single-planar buffers do not use planes array,
844 * so fill in relevant v4l2_buffer struct fields instead.
845 * In videobuf we use our internal V4l2_planes struct for
846 * single-planar buffers as well, for simplicity.
848 if (V4L2_TYPE_IS_OUTPUT(b->type))
849 v4l2_planes[0].bytesused = b->bytesused;
851 if (b->memory == V4L2_MEMORY_USERPTR) {
852 v4l2_planes[0].m.userptr = b->m.userptr;
853 v4l2_planes[0].length = b->length;
857 vb->v4l2_buf.field = b->field;
858 vb->v4l2_buf.timestamp = b->timestamp;
859 vb->v4l2_buf.input = b->input;
860 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
866 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
868 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
870 struct v4l2_plane planes[VIDEO_MAX_PLANES];
871 struct vb2_queue *q = vb->vb2_queue;
875 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
877 /* Verify and copy relevant information provided by the userspace */
878 ret = __fill_vb2_buffer(vb, b, planes);
882 for (plane = 0; plane < vb->num_planes; ++plane) {
883 /* Skip the plane if already verified */
884 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
885 && vb->v4l2_planes[plane].length == planes[plane].length)
888 dprintk(3, "qbuf: userspace address for plane %d changed, "
889 "reacquiring memory\n", plane);
891 /* Check if the provided plane buffer is large enough */
892 if (planes[plane].length < q->plane_sizes[plane]) {
897 /* Release previously acquired memory if present */
898 if (vb->planes[plane].mem_priv)
899 call_memop(q, plane, put_userptr,
900 vb->planes[plane].mem_priv);
902 vb->planes[plane].mem_priv = NULL;
903 vb->v4l2_planes[plane].m.userptr = 0;
904 vb->v4l2_planes[plane].length = 0;
906 /* Acquire each plane's memory */
907 if (q->mem_ops->get_userptr) {
908 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
909 planes[plane].m.userptr,
910 planes[plane].length,
912 if (IS_ERR(mem_priv)) {
913 dprintk(1, "qbuf: failed acquiring userspace "
914 "memory for plane %d\n", plane);
915 ret = PTR_ERR(mem_priv);
918 vb->planes[plane].mem_priv = mem_priv;
923 * Call driver-specific initialization on the newly acquired buffer,
926 ret = call_qop(q, buf_init, vb);
928 dprintk(1, "qbuf: buffer initialization failed\n");
933 * Now that everything is in order, copy relevant information
934 * provided by userspace.
936 for (plane = 0; plane < vb->num_planes; ++plane)
937 vb->v4l2_planes[plane] = planes[plane];
941 /* In case of errors, release planes that were already acquired */
942 for (plane = 0; plane < vb->num_planes; ++plane) {
943 if (vb->planes[plane].mem_priv)
944 call_memop(q, plane, put_userptr,
945 vb->planes[plane].mem_priv);
946 vb->planes[plane].mem_priv = NULL;
947 vb->v4l2_planes[plane].m.userptr = 0;
948 vb->v4l2_planes[plane].length = 0;
955 * __qbuf_mmap() - handle qbuf of an MMAP buffer
957 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
959 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
963 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
965 static void __enqueue_in_driver(struct vb2_buffer *vb)
967 struct vb2_queue *q = vb->vb2_queue;
969 vb->state = VB2_BUF_STATE_ACTIVE;
970 atomic_inc(&q->queued_count);
971 q->ops->buf_queue(vb);
974 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
976 struct vb2_queue *q = vb->vb2_queue;
980 case V4L2_MEMORY_MMAP:
981 ret = __qbuf_mmap(vb, b);
983 case V4L2_MEMORY_USERPTR:
984 ret = __qbuf_userptr(vb, b);
987 WARN(1, "Invalid queue type\n");
992 ret = call_qop(q, buf_prepare, vb);
994 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
996 vb->state = VB2_BUF_STATE_PREPARED;
1002 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1003 * @q: videobuf2 queue
1004 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1007 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1009 * 1) verifies the passed buffer,
1010 * 2) calls buf_prepare callback in the driver (if provided), in which
1011 * driver-specific buffer initialization can be performed,
1013 * The return values from this function are intended to be directly returned
1014 * from vidioc_prepare_buf handler in driver.
1016 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1018 struct vb2_buffer *vb;
1022 dprintk(1, "%s(): file io in progress\n", __func__);
1026 if (b->type != q->type) {
1027 dprintk(1, "%s(): invalid buffer type\n", __func__);
1031 if (b->index >= q->num_buffers) {
1032 dprintk(1, "%s(): buffer index out of range\n", __func__);
1036 vb = q->bufs[b->index];
1038 /* Should never happen */
1039 dprintk(1, "%s(): buffer is NULL\n", __func__);
1043 if (b->memory != q->memory) {
1044 dprintk(1, "%s(): invalid memory type\n", __func__);
1048 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1049 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1053 ret = __buf_prepare(vb, b);
1057 __fill_v4l2_buffer(vb, b);
1061 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1064 * vb2_qbuf() - Queue a buffer from userspace
1065 * @q: videobuf2 queue
1066 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1069 * Should be called from vidioc_qbuf ioctl handler of a driver.
1071 * 1) verifies the passed buffer,
1072 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1073 * which driver-specific buffer initialization can be performed,
1074 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1075 * callback for processing.
1077 * The return values from this function are intended to be directly returned
1078 * from vidioc_qbuf handler in driver.
1080 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1082 struct vb2_buffer *vb;
1086 dprintk(1, "qbuf: file io in progress\n");
1090 if (b->type != q->type) {
1091 dprintk(1, "qbuf: invalid buffer type\n");
1095 if (b->index >= q->num_buffers) {
1096 dprintk(1, "qbuf: buffer index out of range\n");
1100 vb = q->bufs[b->index];
1102 /* Should never happen */
1103 dprintk(1, "qbuf: buffer is NULL\n");
1107 if (b->memory != q->memory) {
1108 dprintk(1, "qbuf: invalid memory type\n");
1112 switch (vb->state) {
1113 case VB2_BUF_STATE_DEQUEUED:
1114 ret = __buf_prepare(vb, b);
1117 case VB2_BUF_STATE_PREPARED:
1120 dprintk(1, "qbuf: buffer already in use\n");
1125 * Add to the queued buffers list, a buffer will stay on it until
1126 * dequeued in dqbuf.
1128 list_add_tail(&vb->queued_entry, &q->queued_list);
1129 vb->state = VB2_BUF_STATE_QUEUED;
1132 * If already streaming, give the buffer to driver for processing.
1133 * If not, the buffer will be given to driver on next streamon.
1136 __enqueue_in_driver(vb);
1138 /* Fill buffer information for the userspace */
1139 __fill_v4l2_buffer(vb, b);
1141 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1144 EXPORT_SYMBOL_GPL(vb2_qbuf);
1147 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1150 * Will sleep if required for nonblocking == false.
1152 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1155 * All operations on vb_done_list are performed under done_lock
1156 * spinlock protection. However, buffers may be removed from
1157 * it and returned to userspace only while holding both driver's
1158 * lock and the done_lock spinlock. Thus we can be sure that as
1159 * long as we hold the driver's lock, the list will remain not
1160 * empty if list_empty() check succeeds.
1166 if (!q->streaming) {
1167 dprintk(1, "Streaming off, will not wait for buffers\n");
1171 if (!list_empty(&q->done_list)) {
1173 * Found a buffer that we were waiting for.
1179 dprintk(1, "Nonblocking and no buffers to dequeue, "
1185 * We are streaming and blocking, wait for another buffer to
1186 * become ready or for streamoff. Driver's lock is released to
1187 * allow streamoff or qbuf to be called while waiting.
1189 call_qop(q, wait_prepare, q);
1192 * All locks have been released, it is safe to sleep now.
1194 dprintk(3, "Will sleep waiting for buffers\n");
1195 ret = wait_event_interruptible(q->done_wq,
1196 !list_empty(&q->done_list) || !q->streaming);
1199 * We need to reevaluate both conditions again after reacquiring
1200 * the locks or return an error if one occurred.
1202 call_qop(q, wait_finish, q);
1210 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1212 * Will sleep if required for nonblocking == false.
1214 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1217 unsigned long flags;
1221 * Wait for at least one buffer to become available on the done_list.
1223 ret = __vb2_wait_for_done_vb(q, nonblocking);
1228 * Driver's lock has been held since we last verified that done_list
1229 * is not empty, so no need for another list_empty(done_list) check.
1231 spin_lock_irqsave(&q->done_lock, flags);
1232 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1233 list_del(&(*vb)->done_entry);
1234 spin_unlock_irqrestore(&q->done_lock, flags);
1240 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1241 * @q: videobuf2 queue
1243 * This function will wait until all buffers that have been given to the driver
1244 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1245 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1246 * taken, for example from stop_streaming() callback.
1248 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1250 if (!q->streaming) {
1251 dprintk(1, "Streaming off, will not wait for buffers\n");
1255 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1258 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1261 * vb2_dqbuf() - Dequeue a buffer to the userspace
1262 * @q: videobuf2 queue
1263 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1265 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1266 * buffers ready for dequeuing are present. Normally the driver
1267 * would be passing (file->f_flags & O_NONBLOCK) here
1269 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1271 * 1) verifies the passed buffer,
1272 * 2) calls buf_finish callback in the driver (if provided), in which
1273 * driver can perform any additional operations that may be required before
1274 * returning the buffer to userspace, such as cache sync,
1275 * 3) the buffer struct members are filled with relevant information for
1278 * The return values from this function are intended to be directly returned
1279 * from vidioc_dqbuf handler in driver.
1281 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1283 struct vb2_buffer *vb = NULL;
1287 dprintk(1, "dqbuf: file io in progress\n");
1291 if (b->type != q->type) {
1292 dprintk(1, "dqbuf: invalid buffer type\n");
1296 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1298 dprintk(1, "dqbuf: error getting next done buffer\n");
1302 ret = call_qop(q, buf_finish, vb);
1304 dprintk(1, "dqbuf: buffer finish failed\n");
1308 switch (vb->state) {
1309 case VB2_BUF_STATE_DONE:
1310 dprintk(3, "dqbuf: Returning done buffer\n");
1312 case VB2_BUF_STATE_ERROR:
1313 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1316 dprintk(1, "dqbuf: Invalid buffer state\n");
1320 /* Fill buffer information for the userspace */
1321 __fill_v4l2_buffer(vb, b);
1322 /* Remove from videobuf queue */
1323 list_del(&vb->queued_entry);
1325 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1326 vb->v4l2_buf.index, vb->state);
1328 vb->state = VB2_BUF_STATE_DEQUEUED;
1331 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1334 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1336 * Removes all queued buffers from driver's queue and all buffers queued by
1337 * userspace from videobuf's queue. Returns to state after reqbufs.
1339 static void __vb2_queue_cancel(struct vb2_queue *q)
1344 * Tell driver to stop all transactions and release all queued
1348 call_qop(q, stop_streaming, q);
1352 * Remove all buffers from videobuf's list...
1354 INIT_LIST_HEAD(&q->queued_list);
1356 * ...and done list; userspace will not receive any buffers it
1357 * has not already dequeued before initiating cancel.
1359 INIT_LIST_HEAD(&q->done_list);
1360 atomic_set(&q->queued_count, 0);
1361 wake_up_all(&q->done_wq);
1364 * Reinitialize all buffers for next use.
1366 for (i = 0; i < q->num_buffers; ++i)
1367 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1371 * vb2_streamon - start streaming
1372 * @q: videobuf2 queue
1373 * @type: type argument passed from userspace to vidioc_streamon handler
1375 * Should be called from vidioc_streamon handler of a driver.
1377 * 1) verifies current state
1378 * 2) passes any previously queued buffers to the driver and starts streaming
1380 * The return values from this function are intended to be directly returned
1381 * from vidioc_streamon handler in the driver.
1383 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1385 struct vb2_buffer *vb;
1389 dprintk(1, "streamon: file io in progress\n");
1393 if (type != q->type) {
1394 dprintk(1, "streamon: invalid stream type\n");
1399 dprintk(1, "streamon: already streaming\n");
1404 * If any buffers were queued before streamon,
1405 * we can now pass them to driver for processing.
1407 list_for_each_entry(vb, &q->queued_list, queued_entry)
1408 __enqueue_in_driver(vb);
1411 * Let driver notice that streaming state has been enabled.
1413 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1415 dprintk(1, "streamon: driver refused to start streaming\n");
1416 __vb2_queue_cancel(q);
1422 dprintk(3, "Streamon successful\n");
1425 EXPORT_SYMBOL_GPL(vb2_streamon);
1429 * vb2_streamoff - stop streaming
1430 * @q: videobuf2 queue
1431 * @type: type argument passed from userspace to vidioc_streamoff handler
1433 * Should be called from vidioc_streamoff handler of a driver.
1435 * 1) verifies current state,
1436 * 2) stop streaming and dequeues any queued buffers, including those previously
1437 * passed to the driver (after waiting for the driver to finish).
1439 * This call can be used for pausing playback.
1440 * The return values from this function are intended to be directly returned
1441 * from vidioc_streamoff handler in the driver
1443 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1446 dprintk(1, "streamoff: file io in progress\n");
1450 if (type != q->type) {
1451 dprintk(1, "streamoff: invalid stream type\n");
1455 if (!q->streaming) {
1456 dprintk(1, "streamoff: not streaming\n");
1461 * Cancel will pause streaming and remove all buffers from the driver
1462 * and videobuf, effectively returning control over them to userspace.
1464 __vb2_queue_cancel(q);
1466 dprintk(3, "Streamoff successful\n");
1469 EXPORT_SYMBOL_GPL(vb2_streamoff);
1472 * __find_plane_by_offset() - find plane associated with the given offset off
1474 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1475 unsigned int *_buffer, unsigned int *_plane)
1477 struct vb2_buffer *vb;
1478 unsigned int buffer, plane;
1481 * Go over all buffers and their planes, comparing the given offset
1482 * with an offset assigned to each plane. If a match is found,
1483 * return its buffer and plane numbers.
1485 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1486 vb = q->bufs[buffer];
1488 for (plane = 0; plane < vb->num_planes; ++plane) {
1489 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1501 * vb2_mmap() - map video buffers into application address space
1502 * @q: videobuf2 queue
1503 * @vma: vma passed to the mmap file operation handler in the driver
1505 * Should be called from mmap file operation handler of a driver.
1506 * This function maps one plane of one of the available video buffers to
1507 * userspace. To map whole video memory allocated on reqbufs, this function
1508 * has to be called once per each plane per each buffer previously allocated.
1510 * When the userspace application calls mmap, it passes to it an offset returned
1511 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1512 * a "cookie", which is then used to identify the plane to be mapped.
1513 * This function finds a plane with a matching offset and a mapping is performed
1514 * by the means of a provided memory operation.
1516 * The return values from this function are intended to be directly returned
1517 * from the mmap handler in driver.
1519 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1521 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1522 struct vb2_plane *vb_plane;
1523 struct vb2_buffer *vb;
1524 unsigned int buffer, plane;
1527 if (q->memory != V4L2_MEMORY_MMAP) {
1528 dprintk(1, "Queue is not currently set up for mmap\n");
1533 * Check memory area access mode.
1535 if (!(vma->vm_flags & VM_SHARED)) {
1536 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1539 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1540 if (!(vma->vm_flags & VM_WRITE)) {
1541 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1545 if (!(vma->vm_flags & VM_READ)) {
1546 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1552 * Find the plane corresponding to the offset passed by userspace.
1554 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1558 vb = q->bufs[buffer];
1559 vb_plane = &vb->planes[plane];
1561 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1565 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1568 EXPORT_SYMBOL_GPL(vb2_mmap);
1571 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1574 unsigned long pgoff,
1575 unsigned long flags)
1577 unsigned long off = pgoff << PAGE_SHIFT;
1578 struct vb2_buffer *vb;
1579 unsigned int buffer, plane;
1582 if (q->memory != V4L2_MEMORY_MMAP) {
1583 dprintk(1, "Queue is not currently set up for mmap\n");
1588 * Find the plane corresponding to the offset passed by userspace.
1590 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1594 vb = q->bufs[buffer];
1596 return (unsigned long)vb2_plane_vaddr(vb, plane);
1598 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1601 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1602 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1605 * vb2_poll() - implements poll userspace operation
1606 * @q: videobuf2 queue
1607 * @file: file argument passed to the poll file operation handler
1608 * @wait: wait argument passed to the poll file operation handler
1610 * This function implements poll file operation handler for a driver.
1611 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1612 * be informed that the file descriptor of a video device is available for
1614 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1615 * will be reported as available for writing.
1617 * The return values from this function are intended to be directly returned
1618 * from poll handler in driver.
1620 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1622 unsigned long flags;
1624 struct vb2_buffer *vb = NULL;
1627 * Start file I/O emulator only if streaming API has not been used yet.
1629 if (q->num_buffers == 0 && q->fileio == NULL) {
1630 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1631 ret = __vb2_init_fileio(q, 1);
1635 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1636 ret = __vb2_init_fileio(q, 0);
1640 * Write to OUTPUT queue can be done immediately.
1642 return POLLOUT | POLLWRNORM;
1647 * There is nothing to wait for if no buffers have already been queued.
1649 if (list_empty(&q->queued_list))
1652 poll_wait(file, &q->done_wq, wait);
1655 * Take first buffer available for dequeuing.
1657 spin_lock_irqsave(&q->done_lock, flags);
1658 if (!list_empty(&q->done_list))
1659 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1661 spin_unlock_irqrestore(&q->done_lock, flags);
1663 if (vb && (vb->state == VB2_BUF_STATE_DONE
1664 || vb->state == VB2_BUF_STATE_ERROR)) {
1665 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1666 POLLIN | POLLRDNORM;
1670 EXPORT_SYMBOL_GPL(vb2_poll);
1673 * vb2_queue_init() - initialize a videobuf2 queue
1674 * @q: videobuf2 queue; this structure should be allocated in driver
1676 * The vb2_queue structure should be allocated by the driver. The driver is
1677 * responsible of clearing it's content and setting initial values for some
1678 * required entries before calling this function.
1679 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1680 * to the struct vb2_queue description in include/media/videobuf2-core.h
1681 * for more information.
1683 int vb2_queue_init(struct vb2_queue *q)
1687 BUG_ON(!q->mem_ops);
1689 BUG_ON(!q->io_modes);
1691 BUG_ON(!q->ops->queue_setup);
1692 BUG_ON(!q->ops->buf_queue);
1694 INIT_LIST_HEAD(&q->queued_list);
1695 INIT_LIST_HEAD(&q->done_list);
1696 spin_lock_init(&q->done_lock);
1697 init_waitqueue_head(&q->done_wq);
1699 if (q->buf_struct_size == 0)
1700 q->buf_struct_size = sizeof(struct vb2_buffer);
1704 EXPORT_SYMBOL_GPL(vb2_queue_init);
1707 * vb2_queue_release() - stop streaming, release the queue and free memory
1708 * @q: videobuf2 queue
1710 * This function stops streaming and performs necessary clean ups, including
1711 * freeing video buffer memory. The driver is responsible for freeing
1712 * the vb2_queue structure itself.
1714 void vb2_queue_release(struct vb2_queue *q)
1716 __vb2_cleanup_fileio(q);
1717 __vb2_queue_cancel(q);
1718 __vb2_queue_free(q, q->num_buffers);
1720 EXPORT_SYMBOL_GPL(vb2_queue_release);
1723 * struct vb2_fileio_buf - buffer context used by file io emulator
1725 * vb2 provides a compatibility layer and emulator of file io (read and
1726 * write) calls on top of streaming API. This structure is used for
1727 * tracking context related to the buffers.
1729 struct vb2_fileio_buf {
1733 unsigned int queued:1;
1737 * struct vb2_fileio_data - queue context used by file io emulator
1739 * vb2 provides a compatibility layer and emulator of file io (read and
1740 * write) calls on top of streaming API. For proper operation it required
1741 * this structure to save the driver state between each call of the read
1742 * or write function.
1744 struct vb2_fileio_data {
1745 struct v4l2_requestbuffers req;
1746 struct v4l2_buffer b;
1747 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1749 unsigned int q_count;
1750 unsigned int dq_count;
1755 * __vb2_init_fileio() - initialize file io emulator
1756 * @q: videobuf2 queue
1757 * @read: mode selector (1 means read, 0 means write)
1759 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1761 struct vb2_fileio_data *fileio;
1763 unsigned int count = 0;
1768 if ((read && !(q->io_modes & VB2_READ)) ||
1769 (!read && !(q->io_modes & VB2_WRITE)))
1773 * Check if device supports mapping buffers to kernel virtual space.
1775 if (!q->mem_ops->vaddr)
1779 * Check if streaming api has not been already activated.
1781 if (q->streaming || q->num_buffers > 0)
1785 * Start with count 1, driver can increase it in queue_setup()
1789 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1790 (read) ? "read" : "write", count, q->io_flags);
1792 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1796 fileio->flags = q->io_flags;
1799 * Request buffers and use MMAP type to force driver
1800 * to allocate buffers by itself.
1802 fileio->req.count = count;
1803 fileio->req.memory = V4L2_MEMORY_MMAP;
1804 fileio->req.type = q->type;
1805 ret = vb2_reqbufs(q, &fileio->req);
1810 * Check if plane_count is correct
1811 * (multiplane buffers are not supported).
1813 if (q->bufs[0]->num_planes != 1) {
1814 fileio->req.count = 0;
1820 * Get kernel address of each buffer.
1822 for (i = 0; i < q->num_buffers; i++) {
1823 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1824 if (fileio->bufs[i].vaddr == NULL)
1826 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1830 * Read mode requires pre queuing of all buffers.
1834 * Queue all buffers.
1836 for (i = 0; i < q->num_buffers; i++) {
1837 struct v4l2_buffer *b = &fileio->b;
1838 memset(b, 0, sizeof(*b));
1840 b->memory = q->memory;
1842 ret = vb2_qbuf(q, b);
1845 fileio->bufs[i].queued = 1;
1851 ret = vb2_streamon(q, q->type);
1861 vb2_reqbufs(q, &fileio->req);
1869 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1870 * @q: videobuf2 queue
1872 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1874 struct vb2_fileio_data *fileio = q->fileio;
1878 * Hack fileio context to enable direct calls to vb2 ioctl
1883 vb2_streamoff(q, q->type);
1884 fileio->req.count = 0;
1885 vb2_reqbufs(q, &fileio->req);
1887 dprintk(3, "file io emulator closed\n");
1893 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1894 * @q: videobuf2 queue
1895 * @data: pointed to target userspace buffer
1896 * @count: number of bytes to read or write
1897 * @ppos: file handle position tracking pointer
1898 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1899 * @read: access mode selector (1 means read, 0 means write)
1901 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1902 loff_t *ppos, int nonblock, int read)
1904 struct vb2_fileio_data *fileio;
1905 struct vb2_fileio_buf *buf;
1908 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1909 read ? "read" : "write", (long)*ppos, count,
1910 nonblock ? "non" : "");
1916 * Initialize emulator on first call.
1919 ret = __vb2_init_fileio(q, read);
1920 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1927 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1928 * The pointer will be restored before returning from this function.
1932 index = fileio->index;
1933 buf = &fileio->bufs[index];
1936 * Check if we need to dequeue the buffer.
1939 struct vb2_buffer *vb;
1942 * Call vb2_dqbuf to get buffer back.
1944 memset(&fileio->b, 0, sizeof(fileio->b));
1945 fileio->b.type = q->type;
1946 fileio->b.memory = q->memory;
1947 fileio->b.index = index;
1948 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1949 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1952 fileio->dq_count += 1;
1955 * Get number of bytes filled by the driver
1957 vb = q->bufs[index];
1958 buf->size = vb2_get_plane_payload(vb, 0);
1963 * Limit count on last few bytes of the buffer.
1965 if (buf->pos + count > buf->size) {
1966 count = buf->size - buf->pos;
1967 dprintk(5, "reducing read count: %zd\n", count);
1971 * Transfer data to userspace.
1973 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1974 count, index, buf->pos);
1976 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1978 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1980 dprintk(3, "file io: error copying data\n");
1992 * Queue next buffer if required.
1994 if (buf->pos == buf->size ||
1995 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1997 * Check if this is the last buffer to read.
1999 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2000 fileio->dq_count == 1) {
2001 dprintk(3, "file io: read limit reached\n");
2003 * Restore fileio pointer and release the context.
2006 return __vb2_cleanup_fileio(q);
2010 * Call vb2_qbuf and give buffer to the driver.
2012 memset(&fileio->b, 0, sizeof(fileio->b));
2013 fileio->b.type = q->type;
2014 fileio->b.memory = q->memory;
2015 fileio->b.index = index;
2016 fileio->b.bytesused = buf->pos;
2017 ret = vb2_qbuf(q, &fileio->b);
2018 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2023 * Buffer has been queued, update the status
2027 buf->size = q->bufs[0]->v4l2_planes[0].length;
2028 fileio->q_count += 1;
2031 * Switch to the next buffer
2033 fileio->index = (index + 1) % q->num_buffers;
2036 * Start streaming if required.
2038 if (!read && !q->streaming) {
2039 ret = vb2_streamon(q, q->type);
2046 * Return proper number of bytes processed.
2052 * Restore the fileio context and block vb2 ioctl interface.
2058 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2059 loff_t *ppos, int nonblocking)
2061 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2063 EXPORT_SYMBOL_GPL(vb2_read);
2065 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2066 loff_t *ppos, int nonblocking)
2068 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2070 EXPORT_SYMBOL_GPL(vb2_write);
2072 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2073 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2074 MODULE_LICENSE("GPL");