Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / videobuf2-core.c
blob3015e6000946b735a89d9b0a5e90f70540ab10c2
1 /*
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>
17 #include <linux/mm.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/videobuf2-core.h>
24 static int debug;
25 module_param(debug, int, 0644);
27 #define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
31 } while (0)
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)
43 /**
44 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
46 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb,
47 unsigned long *plane_sizes)
49 struct vb2_queue *q = vb->vb2_queue;
50 void *mem_priv;
51 int plane;
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 plane_sizes[plane]);
57 if (IS_ERR_OR_NULL(mem_priv))
58 goto free;
60 /* Associate allocator private data with this plane */
61 vb->planes[plane].mem_priv = mem_priv;
62 vb->v4l2_planes[plane].length = plane_sizes[plane];
65 return 0;
66 free:
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);
71 return -ENOMEM;
74 /**
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;
80 unsigned int plane;
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);
90 /**
91 * __vb2_buf_userptr_put() - release userspace memory associated with
92 * a USERPTR buffer
94 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
96 struct vb2_queue *q = vb->vb2_queue;
97 unsigned int plane;
99 for (plane = 0; plane < vb->num_planes; ++plane) {
100 void *mem_priv = vb->planes[plane].mem_priv;
102 if (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)
115 unsigned int buffer, plane;
116 struct vb2_buffer *vb;
117 unsigned long off = 0;
119 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
120 vb = q->bufs[buffer];
121 if (!vb)
122 continue;
124 for (plane = 0; plane < vb->num_planes; ++plane) {
125 vb->v4l2_planes[plane].m.mem_offset = off;
127 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
128 buffer, plane, off);
130 off += vb->v4l2_planes[plane].length;
131 off = PAGE_ALIGN(off);
137 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
138 * video buffer memory for all buffers/planes on the queue and initializes the
139 * queue
141 * Returns the number of buffers successfully allocated.
143 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
144 unsigned int num_buffers, unsigned int num_planes,
145 unsigned long plane_sizes[])
147 unsigned int buffer;
148 struct vb2_buffer *vb;
149 int ret;
151 for (buffer = 0; buffer < num_buffers; ++buffer) {
152 /* Allocate videobuf buffer structures */
153 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
154 if (!vb) {
155 dprintk(1, "Memory alloc for buffer struct failed\n");
156 break;
159 /* Length stores number of planes for multiplanar buffers */
160 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
161 vb->v4l2_buf.length = num_planes;
163 vb->state = VB2_BUF_STATE_DEQUEUED;
164 vb->vb2_queue = q;
165 vb->num_planes = num_planes;
166 vb->v4l2_buf.index = buffer;
167 vb->v4l2_buf.type = q->type;
168 vb->v4l2_buf.memory = memory;
170 /* Allocate video buffer memory for the MMAP type */
171 if (memory == V4L2_MEMORY_MMAP) {
172 ret = __vb2_buf_mem_alloc(vb, plane_sizes);
173 if (ret) {
174 dprintk(1, "Failed allocating memory for "
175 "buffer %d\n", buffer);
176 kfree(vb);
177 break;
180 * Call the driver-provided buffer initialization
181 * callback, if given. An error in initialization
182 * results in queue setup failure.
184 ret = call_qop(q, buf_init, vb);
185 if (ret) {
186 dprintk(1, "Buffer %d %p initialization"
187 " failed\n", buffer, vb);
188 __vb2_buf_mem_free(vb);
189 kfree(vb);
190 break;
194 q->bufs[buffer] = vb;
197 q->num_buffers = buffer;
199 __setup_offsets(q);
201 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
202 q->num_buffers, num_planes);
204 return buffer;
208 * __vb2_free_mem() - release all video buffer memory for a given queue
210 static void __vb2_free_mem(struct vb2_queue *q)
212 unsigned int buffer;
213 struct vb2_buffer *vb;
215 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
216 vb = q->bufs[buffer];
217 if (!vb)
218 continue;
220 /* Free MMAP buffers or release USERPTR buffers */
221 if (q->memory == V4L2_MEMORY_MMAP)
222 __vb2_buf_mem_free(vb);
223 else
224 __vb2_buf_userptr_put(vb);
229 * __vb2_queue_free() - free the queue - video memory and related information
230 * and return the queue to an uninitialized state. Might be called even if the
231 * queue has already been freed.
233 static void __vb2_queue_free(struct vb2_queue *q)
235 unsigned int buffer;
237 /* Call driver-provided cleanup function for each buffer, if provided */
238 if (q->ops->buf_cleanup) {
239 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
240 if (NULL == q->bufs[buffer])
241 continue;
242 q->ops->buf_cleanup(q->bufs[buffer]);
246 /* Release video buffer memory */
247 __vb2_free_mem(q);
249 /* Free videobuf buffers */
250 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
251 kfree(q->bufs[buffer]);
252 q->bufs[buffer] = NULL;
255 q->num_buffers = 0;
256 q->memory = 0;
260 * __verify_planes_array() - verify that the planes array passed in struct
261 * v4l2_buffer from userspace can be safely used
263 static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b)
265 /* Is memory for copying plane information present? */
266 if (NULL == b->m.planes) {
267 dprintk(1, "Multi-planar buffer passed but "
268 "planes array not provided\n");
269 return -EINVAL;
272 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
273 dprintk(1, "Incorrect planes array length, "
274 "expected %d, got %d\n", vb->num_planes, b->length);
275 return -EINVAL;
278 return 0;
282 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
283 * returned to userspace
285 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
287 struct vb2_queue *q = vb->vb2_queue;
288 int ret = 0;
290 /* Copy back data such as timestamp, flags, input, etc. */
291 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
292 b->input = vb->v4l2_buf.input;
293 b->reserved = vb->v4l2_buf.reserved;
295 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
296 ret = __verify_planes_array(vb, b);
297 if (ret)
298 return ret;
301 * Fill in plane-related data if userspace provided an array
302 * for it. The memory and size is verified above.
304 memcpy(b->m.planes, vb->v4l2_planes,
305 b->length * sizeof(struct v4l2_plane));
306 } else {
308 * We use length and offset in v4l2_planes array even for
309 * single-planar buffers, but userspace does not.
311 b->length = vb->v4l2_planes[0].length;
312 b->bytesused = vb->v4l2_planes[0].bytesused;
313 if (q->memory == V4L2_MEMORY_MMAP)
314 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
315 else if (q->memory == V4L2_MEMORY_USERPTR)
316 b->m.userptr = vb->v4l2_planes[0].m.userptr;
320 * Clear any buffer state related flags.
322 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
324 switch (vb->state) {
325 case VB2_BUF_STATE_QUEUED:
326 case VB2_BUF_STATE_ACTIVE:
327 b->flags |= V4L2_BUF_FLAG_QUEUED;
328 break;
329 case VB2_BUF_STATE_ERROR:
330 b->flags |= V4L2_BUF_FLAG_ERROR;
331 /* fall through */
332 case VB2_BUF_STATE_DONE:
333 b->flags |= V4L2_BUF_FLAG_DONE;
334 break;
335 case VB2_BUF_STATE_DEQUEUED:
336 /* nothing */
337 break;
340 if (vb->num_planes_mapped == vb->num_planes)
341 b->flags |= V4L2_BUF_FLAG_MAPPED;
343 return ret;
347 * vb2_querybuf() - query video buffer information
348 * @q: videobuf queue
349 * @b: buffer struct passed from userspace to vidioc_querybuf handler
350 * in driver
352 * Should be called from vidioc_querybuf ioctl handler in driver.
353 * This function will verify the passed v4l2_buffer structure and fill the
354 * relevant information for the userspace.
356 * The return values from this function are intended to be directly returned
357 * from vidioc_querybuf handler in driver.
359 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
361 struct vb2_buffer *vb;
363 if (b->type != q->type) {
364 dprintk(1, "querybuf: wrong buffer type\n");
365 return -EINVAL;
368 if (b->index >= q->num_buffers) {
369 dprintk(1, "querybuf: buffer index out of range\n");
370 return -EINVAL;
372 vb = q->bufs[b->index];
374 return __fill_v4l2_buffer(vb, b);
376 EXPORT_SYMBOL(vb2_querybuf);
379 * __verify_userptr_ops() - verify that all memory operations required for
380 * USERPTR queue type have been provided
382 static int __verify_userptr_ops(struct vb2_queue *q)
384 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
385 !q->mem_ops->put_userptr)
386 return -EINVAL;
388 return 0;
392 * __verify_mmap_ops() - verify that all memory operations required for
393 * MMAP queue type have been provided
395 static int __verify_mmap_ops(struct vb2_queue *q)
397 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
398 !q->mem_ops->put || !q->mem_ops->mmap)
399 return -EINVAL;
401 return 0;
405 * __buffers_in_use() - return true if any buffers on the queue are in use and
406 * the queue cannot be freed (by the means of REQBUFS(0)) call
408 static bool __buffers_in_use(struct vb2_queue *q)
410 unsigned int buffer, plane;
411 struct vb2_buffer *vb;
413 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
414 vb = q->bufs[buffer];
415 for (plane = 0; plane < vb->num_planes; ++plane) {
417 * If num_users() has not been provided, call_memop
418 * will return 0, apparently nobody cares about this
419 * case anyway. If num_users() returns more than 1,
420 * we are not the only user of the plane's memory.
422 if (call_memop(q, plane, num_users,
423 vb->planes[plane].mem_priv) > 1)
424 return true;
428 return false;
432 * vb2_reqbufs() - Initiate streaming
433 * @q: videobuf2 queue
434 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
436 * Should be called from vidioc_reqbufs ioctl handler of a driver.
437 * This function:
438 * 1) verifies streaming parameters passed from the userspace,
439 * 2) sets up the queue,
440 * 3) negotiates number of buffers and planes per buffer with the driver
441 * to be used during streaming,
442 * 4) allocates internal buffer structures (struct vb2_buffer), according to
443 * the agreed parameters,
444 * 5) for MMAP memory type, allocates actual video memory, using the
445 * memory handling/allocation routines provided during queue initialization
447 * If req->count is 0, all the memory will be freed instead.
448 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
449 * and the queue is not busy, memory will be reallocated.
451 * The return values from this function are intended to be directly returned
452 * from vidioc_reqbufs handler in driver.
454 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
456 unsigned int num_buffers, num_planes;
457 unsigned long plane_sizes[VIDEO_MAX_PLANES];
458 int ret = 0;
460 if (q->fileio) {
461 dprintk(1, "reqbufs: file io in progress\n");
462 return -EBUSY;
465 if (req->memory != V4L2_MEMORY_MMAP
466 && req->memory != V4L2_MEMORY_USERPTR) {
467 dprintk(1, "reqbufs: unsupported memory type\n");
468 return -EINVAL;
471 if (req->type != q->type) {
472 dprintk(1, "reqbufs: requested type is incorrect\n");
473 return -EINVAL;
476 if (q->streaming) {
477 dprintk(1, "reqbufs: streaming active\n");
478 return -EBUSY;
482 * Make sure all the required memory ops for given memory type
483 * are available.
485 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
486 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
487 return -EINVAL;
490 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
491 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
492 return -EINVAL;
495 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
497 * We already have buffers allocated, so first check if they
498 * are not in use and can be freed.
500 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
501 dprintk(1, "reqbufs: memory in use, cannot free\n");
502 return -EBUSY;
505 __vb2_queue_free(q);
508 * In case of REQBUFS(0) return immediately without calling
509 * driver's queue_setup() callback and allocating resources.
511 if (req->count == 0)
512 return 0;
516 * Make sure the requested values and current defaults are sane.
518 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
519 memset(plane_sizes, 0, sizeof(plane_sizes));
520 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
521 q->memory = req->memory;
524 * Ask the driver how many buffers and planes per buffer it requires.
525 * Driver also sets the size and allocator context for each plane.
527 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
528 plane_sizes, q->alloc_ctx);
529 if (ret)
530 return ret;
532 /* Finally, allocate buffers and video memory */
533 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes,
534 plane_sizes);
535 if (ret == 0) {
536 dprintk(1, "Memory allocation failed\n");
537 return -ENOMEM;
541 * Check if driver can handle the allocated number of buffers.
543 if (ret < num_buffers) {
544 unsigned int orig_num_buffers;
546 orig_num_buffers = num_buffers = ret;
547 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
548 plane_sizes, q->alloc_ctx);
549 if (ret)
550 goto free_mem;
552 if (orig_num_buffers < num_buffers) {
553 ret = -ENOMEM;
554 goto free_mem;
558 * Ok, driver accepted smaller number of buffers.
560 ret = num_buffers;
564 * Return the number of successfully allocated buffers
565 * to the userspace.
567 req->count = ret;
569 return 0;
571 free_mem:
572 __vb2_queue_free(q);
573 return ret;
575 EXPORT_SYMBOL_GPL(vb2_reqbufs);
578 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
579 * @vb: vb2_buffer to which the plane in question belongs to
580 * @plane_no: plane number for which the address is to be returned
582 * This function returns a kernel virtual address of a given plane if
583 * such a mapping exist, NULL otherwise.
585 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
587 struct vb2_queue *q = vb->vb2_queue;
589 if (plane_no > vb->num_planes)
590 return NULL;
592 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
595 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
598 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
599 * @vb: vb2_buffer to which the plane in question belongs to
600 * @plane_no: plane number for which the cookie is to be returned
602 * This function returns an allocator specific cookie for a given plane if
603 * available, NULL otherwise. The allocator should provide some simple static
604 * inline function, which would convert this cookie to the allocator specific
605 * type that can be used directly by the driver to access the buffer. This can
606 * be for example physical address, pointer to scatter list or IOMMU mapping.
608 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
610 struct vb2_queue *q = vb->vb2_queue;
612 if (plane_no > vb->num_planes)
613 return NULL;
615 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
617 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
620 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
621 * @vb: vb2_buffer returned from the driver
622 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
623 * or VB2_BUF_STATE_ERROR if the operation finished with an error
625 * This function should be called by the driver after a hardware operation on
626 * a buffer is finished and the buffer may be returned to userspace. The driver
627 * cannot use this buffer anymore until it is queued back to it by videobuf
628 * by the means of buf_queue callback. Only buffers previously queued to the
629 * driver by buf_queue can be passed to this function.
631 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
633 struct vb2_queue *q = vb->vb2_queue;
634 unsigned long flags;
636 if (vb->state != VB2_BUF_STATE_ACTIVE)
637 return;
639 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
640 return;
642 dprintk(4, "Done processing on buffer %d, state: %d\n",
643 vb->v4l2_buf.index, vb->state);
645 /* Add the buffer to the done buffers list */
646 spin_lock_irqsave(&q->done_lock, flags);
647 vb->state = state;
648 list_add_tail(&vb->done_entry, &q->done_list);
649 atomic_dec(&q->queued_count);
650 spin_unlock_irqrestore(&q->done_lock, flags);
652 /* Inform any processes that may be waiting for buffers */
653 wake_up(&q->done_wq);
655 EXPORT_SYMBOL_GPL(vb2_buffer_done);
658 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
659 * a v4l2_buffer by the userspace
661 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
662 struct v4l2_plane *v4l2_planes)
664 unsigned int plane;
665 int ret;
667 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
669 * Verify that the userspace gave us a valid array for
670 * plane information.
672 ret = __verify_planes_array(vb, b);
673 if (ret)
674 return ret;
676 /* Fill in driver-provided information for OUTPUT types */
677 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
679 * Will have to go up to b->length when API starts
680 * accepting variable number of planes.
682 for (plane = 0; plane < vb->num_planes; ++plane) {
683 v4l2_planes[plane].bytesused =
684 b->m.planes[plane].bytesused;
685 v4l2_planes[plane].data_offset =
686 b->m.planes[plane].data_offset;
690 if (b->memory == V4L2_MEMORY_USERPTR) {
691 for (plane = 0; plane < vb->num_planes; ++plane) {
692 v4l2_planes[plane].m.userptr =
693 b->m.planes[plane].m.userptr;
694 v4l2_planes[plane].length =
695 b->m.planes[plane].length;
698 } else {
700 * Single-planar buffers do not use planes array,
701 * so fill in relevant v4l2_buffer struct fields instead.
702 * In videobuf we use our internal V4l2_planes struct for
703 * single-planar buffers as well, for simplicity.
705 if (V4L2_TYPE_IS_OUTPUT(b->type))
706 v4l2_planes[0].bytesused = b->bytesused;
708 if (b->memory == V4L2_MEMORY_USERPTR) {
709 v4l2_planes[0].m.userptr = b->m.userptr;
710 v4l2_planes[0].length = b->length;
714 vb->v4l2_buf.field = b->field;
715 vb->v4l2_buf.timestamp = b->timestamp;
716 vb->v4l2_buf.input = b->input;
717 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
719 return 0;
723 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
725 static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
727 struct v4l2_plane planes[VIDEO_MAX_PLANES];
728 struct vb2_queue *q = vb->vb2_queue;
729 void *mem_priv;
730 unsigned int plane;
731 int ret;
732 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
734 /* Verify and copy relevant information provided by the userspace */
735 ret = __fill_vb2_buffer(vb, b, planes);
736 if (ret)
737 return ret;
739 for (plane = 0; plane < vb->num_planes; ++plane) {
740 /* Skip the plane if already verified */
741 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
742 && vb->v4l2_planes[plane].length == planes[plane].length)
743 continue;
745 dprintk(3, "qbuf: userspace address for plane %d changed, "
746 "reacquiring memory\n", plane);
748 /* Release previously acquired memory if present */
749 if (vb->planes[plane].mem_priv)
750 call_memop(q, plane, put_userptr,
751 vb->planes[plane].mem_priv);
753 vb->planes[plane].mem_priv = NULL;
755 /* Acquire each plane's memory */
756 if (q->mem_ops->get_userptr) {
757 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
758 planes[plane].m.userptr,
759 planes[plane].length,
760 write);
761 if (IS_ERR(mem_priv)) {
762 dprintk(1, "qbuf: failed acquiring userspace "
763 "memory for plane %d\n", plane);
764 ret = PTR_ERR(mem_priv);
765 goto err;
767 vb->planes[plane].mem_priv = mem_priv;
772 * Call driver-specific initialization on the newly acquired buffer,
773 * if provided.
775 ret = call_qop(q, buf_init, vb);
776 if (ret) {
777 dprintk(1, "qbuf: buffer initialization failed\n");
778 goto err;
782 * Now that everything is in order, copy relevant information
783 * provided by userspace.
785 for (plane = 0; plane < vb->num_planes; ++plane)
786 vb->v4l2_planes[plane] = planes[plane];
788 return 0;
789 err:
790 /* In case of errors, release planes that were already acquired */
791 for (; plane > 0; --plane) {
792 call_memop(q, plane, put_userptr,
793 vb->planes[plane - 1].mem_priv);
794 vb->planes[plane - 1].mem_priv = NULL;
797 return ret;
801 * __qbuf_mmap() - handle qbuf of an MMAP buffer
803 static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
805 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
809 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
811 static void __enqueue_in_driver(struct vb2_buffer *vb)
813 struct vb2_queue *q = vb->vb2_queue;
815 vb->state = VB2_BUF_STATE_ACTIVE;
816 atomic_inc(&q->queued_count);
817 q->ops->buf_queue(vb);
821 * vb2_qbuf() - Queue a buffer from userspace
822 * @q: videobuf2 queue
823 * @b: buffer structure passed from userspace to vidioc_qbuf handler
824 * in driver
826 * Should be called from vidioc_qbuf ioctl handler of a driver.
827 * This function:
828 * 1) verifies the passed buffer,
829 * 2) calls buf_prepare callback in the driver (if provided), in which
830 * driver-specific buffer initialization can be performed,
831 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
832 * callback for processing.
834 * The return values from this function are intended to be directly returned
835 * from vidioc_qbuf handler in driver.
837 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
839 struct vb2_buffer *vb;
840 int ret = 0;
842 if (q->fileio) {
843 dprintk(1, "qbuf: file io in progress\n");
844 return -EBUSY;
847 if (b->type != q->type) {
848 dprintk(1, "qbuf: invalid buffer type\n");
849 return -EINVAL;
852 if (b->index >= q->num_buffers) {
853 dprintk(1, "qbuf: buffer index out of range\n");
854 return -EINVAL;
857 vb = q->bufs[b->index];
858 if (NULL == vb) {
859 /* Should never happen */
860 dprintk(1, "qbuf: buffer is NULL\n");
861 return -EINVAL;
864 if (b->memory != q->memory) {
865 dprintk(1, "qbuf: invalid memory type\n");
866 return -EINVAL;
869 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
870 dprintk(1, "qbuf: buffer already in use\n");
871 return -EINVAL;
874 if (q->memory == V4L2_MEMORY_MMAP)
875 ret = __qbuf_mmap(vb, b);
876 else if (q->memory == V4L2_MEMORY_USERPTR)
877 ret = __qbuf_userptr(vb, b);
878 else {
879 WARN(1, "Invalid queue type\n");
880 return -EINVAL;
883 if (ret)
884 return ret;
886 ret = call_qop(q, buf_prepare, vb);
887 if (ret) {
888 dprintk(1, "qbuf: buffer preparation failed\n");
889 return ret;
893 * Add to the queued buffers list, a buffer will stay on it until
894 * dequeued in dqbuf.
896 list_add_tail(&vb->queued_entry, &q->queued_list);
897 vb->state = VB2_BUF_STATE_QUEUED;
900 * If already streaming, give the buffer to driver for processing.
901 * If not, the buffer will be given to driver on next streamon.
903 if (q->streaming)
904 __enqueue_in_driver(vb);
906 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
907 return 0;
909 EXPORT_SYMBOL_GPL(vb2_qbuf);
912 * __vb2_wait_for_done_vb() - wait for a buffer to become available
913 * for dequeuing
915 * Will sleep if required for nonblocking == false.
917 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
920 * All operations on vb_done_list are performed under done_lock
921 * spinlock protection. However, buffers may be removed from
922 * it and returned to userspace only while holding both driver's
923 * lock and the done_lock spinlock. Thus we can be sure that as
924 * long as we hold the driver's lock, the list will remain not
925 * empty if list_empty() check succeeds.
928 for (;;) {
929 int ret;
931 if (!q->streaming) {
932 dprintk(1, "Streaming off, will not wait for buffers\n");
933 return -EINVAL;
936 if (!list_empty(&q->done_list)) {
938 * Found a buffer that we were waiting for.
940 break;
943 if (nonblocking) {
944 dprintk(1, "Nonblocking and no buffers to dequeue, "
945 "will not wait\n");
946 return -EAGAIN;
950 * We are streaming and blocking, wait for another buffer to
951 * become ready or for streamoff. Driver's lock is released to
952 * allow streamoff or qbuf to be called while waiting.
954 call_qop(q, wait_prepare, q);
957 * All locks have been released, it is safe to sleep now.
959 dprintk(3, "Will sleep waiting for buffers\n");
960 ret = wait_event_interruptible(q->done_wq,
961 !list_empty(&q->done_list) || !q->streaming);
964 * We need to reevaluate both conditions again after reacquiring
965 * the locks or return an error if one occurred.
967 call_qop(q, wait_finish, q);
968 if (ret)
969 return ret;
971 return 0;
975 * __vb2_get_done_vb() - get a buffer ready for dequeuing
977 * Will sleep if required for nonblocking == false.
979 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
980 int nonblocking)
982 unsigned long flags;
983 int ret;
986 * Wait for at least one buffer to become available on the done_list.
988 ret = __vb2_wait_for_done_vb(q, nonblocking);
989 if (ret)
990 return ret;
993 * Driver's lock has been held since we last verified that done_list
994 * is not empty, so no need for another list_empty(done_list) check.
996 spin_lock_irqsave(&q->done_lock, flags);
997 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
998 list_del(&(*vb)->done_entry);
999 spin_unlock_irqrestore(&q->done_lock, flags);
1001 return 0;
1005 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1006 * @q: videobuf2 queue
1008 * This function will wait until all buffers that have been given to the driver
1009 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1010 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1011 * taken, for example from stop_streaming() callback.
1013 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1015 if (!q->streaming) {
1016 dprintk(1, "Streaming off, will not wait for buffers\n");
1017 return -EINVAL;
1020 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1021 return 0;
1023 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1026 * vb2_dqbuf() - Dequeue a buffer to the userspace
1027 * @q: videobuf2 queue
1028 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1029 * in driver
1030 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1031 * buffers ready for dequeuing are present. Normally the driver
1032 * would be passing (file->f_flags & O_NONBLOCK) here
1034 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1035 * This function:
1036 * 1) verifies the passed buffer,
1037 * 2) calls buf_finish callback in the driver (if provided), in which
1038 * driver can perform any additional operations that may be required before
1039 * returning the buffer to userspace, such as cache sync,
1040 * 3) the buffer struct members are filled with relevant information for
1041 * the userspace.
1043 * The return values from this function are intended to be directly returned
1044 * from vidioc_dqbuf handler in driver.
1046 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1048 struct vb2_buffer *vb = NULL;
1049 int ret;
1051 if (q->fileio) {
1052 dprintk(1, "dqbuf: file io in progress\n");
1053 return -EBUSY;
1056 if (b->type != q->type) {
1057 dprintk(1, "dqbuf: invalid buffer type\n");
1058 return -EINVAL;
1061 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1062 if (ret < 0) {
1063 dprintk(1, "dqbuf: error getting next done buffer\n");
1064 return ret;
1067 ret = call_qop(q, buf_finish, vb);
1068 if (ret) {
1069 dprintk(1, "dqbuf: buffer finish failed\n");
1070 return ret;
1073 switch (vb->state) {
1074 case VB2_BUF_STATE_DONE:
1075 dprintk(3, "dqbuf: Returning done buffer\n");
1076 break;
1077 case VB2_BUF_STATE_ERROR:
1078 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1079 break;
1080 default:
1081 dprintk(1, "dqbuf: Invalid buffer state\n");
1082 return -EINVAL;
1085 /* Fill buffer information for the userspace */
1086 __fill_v4l2_buffer(vb, b);
1087 /* Remove from videobuf queue */
1088 list_del(&vb->queued_entry);
1090 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1091 vb->v4l2_buf.index, vb->state);
1093 vb->state = VB2_BUF_STATE_DEQUEUED;
1094 return 0;
1096 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1099 * vb2_streamon - start streaming
1100 * @q: videobuf2 queue
1101 * @type: type argument passed from userspace to vidioc_streamon handler
1103 * Should be called from vidioc_streamon handler of a driver.
1104 * This function:
1105 * 1) verifies current state
1106 * 2) starts streaming and passes any previously queued buffers to the driver
1108 * The return values from this function are intended to be directly returned
1109 * from vidioc_streamon handler in the driver.
1111 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1113 struct vb2_buffer *vb;
1114 int ret;
1116 if (q->fileio) {
1117 dprintk(1, "streamon: file io in progress\n");
1118 return -EBUSY;
1121 if (type != q->type) {
1122 dprintk(1, "streamon: invalid stream type\n");
1123 return -EINVAL;
1126 if (q->streaming) {
1127 dprintk(1, "streamon: already streaming\n");
1128 return -EBUSY;
1132 * Cannot start streaming on an OUTPUT device if no buffers have
1133 * been queued yet.
1135 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1136 if (list_empty(&q->queued_list)) {
1137 dprintk(1, "streamon: no output buffers queued\n");
1138 return -EINVAL;
1143 * Let driver notice that streaming state has been enabled.
1145 ret = call_qop(q, start_streaming, q);
1146 if (ret) {
1147 dprintk(1, "streamon: driver refused to start streaming\n");
1148 return ret;
1151 q->streaming = 1;
1154 * If any buffers were queued before streamon,
1155 * we can now pass them to driver for processing.
1157 list_for_each_entry(vb, &q->queued_list, queued_entry)
1158 __enqueue_in_driver(vb);
1160 dprintk(3, "Streamon successful\n");
1161 return 0;
1163 EXPORT_SYMBOL_GPL(vb2_streamon);
1166 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1168 * Removes all queued buffers from driver's queue and all buffers queued by
1169 * userspace from videobuf's queue. Returns to state after reqbufs.
1171 static void __vb2_queue_cancel(struct vb2_queue *q)
1173 unsigned int i;
1176 * Tell driver to stop all transactions and release all queued
1177 * buffers.
1179 if (q->streaming)
1180 call_qop(q, stop_streaming, q);
1181 q->streaming = 0;
1184 * Remove all buffers from videobuf's list...
1186 INIT_LIST_HEAD(&q->queued_list);
1188 * ...and done list; userspace will not receive any buffers it
1189 * has not already dequeued before initiating cancel.
1191 INIT_LIST_HEAD(&q->done_list);
1192 atomic_set(&q->queued_count, 0);
1193 wake_up_all(&q->done_wq);
1196 * Reinitialize all buffers for next use.
1198 for (i = 0; i < q->num_buffers; ++i)
1199 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1203 * vb2_streamoff - stop streaming
1204 * @q: videobuf2 queue
1205 * @type: type argument passed from userspace to vidioc_streamoff handler
1207 * Should be called from vidioc_streamoff handler of a driver.
1208 * This function:
1209 * 1) verifies current state,
1210 * 2) stop streaming and dequeues any queued buffers, including those previously
1211 * passed to the driver (after waiting for the driver to finish).
1213 * This call can be used for pausing playback.
1214 * The return values from this function are intended to be directly returned
1215 * from vidioc_streamoff handler in the driver
1217 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1219 if (q->fileio) {
1220 dprintk(1, "streamoff: file io in progress\n");
1221 return -EBUSY;
1224 if (type != q->type) {
1225 dprintk(1, "streamoff: invalid stream type\n");
1226 return -EINVAL;
1229 if (!q->streaming) {
1230 dprintk(1, "streamoff: not streaming\n");
1231 return -EINVAL;
1235 * Cancel will pause streaming and remove all buffers from the driver
1236 * and videobuf, effectively returning control over them to userspace.
1238 __vb2_queue_cancel(q);
1240 dprintk(3, "Streamoff successful\n");
1241 return 0;
1243 EXPORT_SYMBOL_GPL(vb2_streamoff);
1246 * __find_plane_by_offset() - find plane associated with the given offset off
1248 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1249 unsigned int *_buffer, unsigned int *_plane)
1251 struct vb2_buffer *vb;
1252 unsigned int buffer, plane;
1255 * Go over all buffers and their planes, comparing the given offset
1256 * with an offset assigned to each plane. If a match is found,
1257 * return its buffer and plane numbers.
1259 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1260 vb = q->bufs[buffer];
1262 for (plane = 0; plane < vb->num_planes; ++plane) {
1263 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1264 *_buffer = buffer;
1265 *_plane = plane;
1266 return 0;
1271 return -EINVAL;
1275 * vb2_mmap() - map video buffers into application address space
1276 * @q: videobuf2 queue
1277 * @vma: vma passed to the mmap file operation handler in the driver
1279 * Should be called from mmap file operation handler of a driver.
1280 * This function maps one plane of one of the available video buffers to
1281 * userspace. To map whole video memory allocated on reqbufs, this function
1282 * has to be called once per each plane per each buffer previously allocated.
1284 * When the userspace application calls mmap, it passes to it an offset returned
1285 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1286 * a "cookie", which is then used to identify the plane to be mapped.
1287 * This function finds a plane with a matching offset and a mapping is performed
1288 * by the means of a provided memory operation.
1290 * The return values from this function are intended to be directly returned
1291 * from the mmap handler in driver.
1293 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1295 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1296 struct vb2_plane *vb_plane;
1297 struct vb2_buffer *vb;
1298 unsigned int buffer, plane;
1299 int ret;
1301 if (q->memory != V4L2_MEMORY_MMAP) {
1302 dprintk(1, "Queue is not currently set up for mmap\n");
1303 return -EINVAL;
1307 * Check memory area access mode.
1309 if (!(vma->vm_flags & VM_SHARED)) {
1310 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1311 return -EINVAL;
1313 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1314 if (!(vma->vm_flags & VM_WRITE)) {
1315 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1316 return -EINVAL;
1318 } else {
1319 if (!(vma->vm_flags & VM_READ)) {
1320 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1321 return -EINVAL;
1326 * Find the plane corresponding to the offset passed by userspace.
1328 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1329 if (ret)
1330 return ret;
1332 vb = q->bufs[buffer];
1333 vb_plane = &vb->planes[plane];
1335 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1336 if (ret)
1337 return ret;
1339 vb_plane->mapped = 1;
1340 vb->num_planes_mapped++;
1342 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1343 return 0;
1345 EXPORT_SYMBOL_GPL(vb2_mmap);
1347 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1348 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1351 * vb2_poll() - implements poll userspace operation
1352 * @q: videobuf2 queue
1353 * @file: file argument passed to the poll file operation handler
1354 * @wait: wait argument passed to the poll file operation handler
1356 * This function implements poll file operation handler for a driver.
1357 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1358 * be informed that the file descriptor of a video device is available for
1359 * reading.
1360 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1361 * will be reported as available for writing.
1363 * The return values from this function are intended to be directly returned
1364 * from poll handler in driver.
1366 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1368 unsigned long flags;
1369 unsigned int ret;
1370 struct vb2_buffer *vb = NULL;
1373 * Start file I/O emulator only if streaming API has not been used yet.
1375 if (q->num_buffers == 0 && q->fileio == NULL) {
1376 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1377 ret = __vb2_init_fileio(q, 1);
1378 if (ret)
1379 return POLLERR;
1381 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1382 ret = __vb2_init_fileio(q, 0);
1383 if (ret)
1384 return POLLERR;
1386 * Write to OUTPUT queue can be done immediately.
1388 return POLLOUT | POLLWRNORM;
1393 * There is nothing to wait for if no buffers have already been queued.
1395 if (list_empty(&q->queued_list))
1396 return POLLERR;
1398 poll_wait(file, &q->done_wq, wait);
1401 * Take first buffer available for dequeuing.
1403 spin_lock_irqsave(&q->done_lock, flags);
1404 if (!list_empty(&q->done_list))
1405 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1406 done_entry);
1407 spin_unlock_irqrestore(&q->done_lock, flags);
1409 if (vb && (vb->state == VB2_BUF_STATE_DONE
1410 || vb->state == VB2_BUF_STATE_ERROR)) {
1411 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1412 POLLIN | POLLRDNORM;
1414 return 0;
1416 EXPORT_SYMBOL_GPL(vb2_poll);
1419 * vb2_queue_init() - initialize a videobuf2 queue
1420 * @q: videobuf2 queue; this structure should be allocated in driver
1422 * The vb2_queue structure should be allocated by the driver. The driver is
1423 * responsible of clearing it's content and setting initial values for some
1424 * required entries before calling this function.
1425 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1426 * to the struct vb2_queue description in include/media/videobuf2-core.h
1427 * for more information.
1429 int vb2_queue_init(struct vb2_queue *q)
1431 BUG_ON(!q);
1432 BUG_ON(!q->ops);
1433 BUG_ON(!q->mem_ops);
1434 BUG_ON(!q->type);
1435 BUG_ON(!q->io_modes);
1437 BUG_ON(!q->ops->queue_setup);
1438 BUG_ON(!q->ops->buf_queue);
1440 INIT_LIST_HEAD(&q->queued_list);
1441 INIT_LIST_HEAD(&q->done_list);
1442 spin_lock_init(&q->done_lock);
1443 init_waitqueue_head(&q->done_wq);
1445 if (q->buf_struct_size == 0)
1446 q->buf_struct_size = sizeof(struct vb2_buffer);
1448 return 0;
1450 EXPORT_SYMBOL_GPL(vb2_queue_init);
1453 * vb2_queue_release() - stop streaming, release the queue and free memory
1454 * @q: videobuf2 queue
1456 * This function stops streaming and performs necessary clean ups, including
1457 * freeing video buffer memory. The driver is responsible for freeing
1458 * the vb2_queue structure itself.
1460 void vb2_queue_release(struct vb2_queue *q)
1462 __vb2_cleanup_fileio(q);
1463 __vb2_queue_cancel(q);
1464 __vb2_queue_free(q);
1466 EXPORT_SYMBOL_GPL(vb2_queue_release);
1469 * struct vb2_fileio_buf - buffer context used by file io emulator
1471 * vb2 provides a compatibility layer and emulator of file io (read and
1472 * write) calls on top of streaming API. This structure is used for
1473 * tracking context related to the buffers.
1475 struct vb2_fileio_buf {
1476 void *vaddr;
1477 unsigned int size;
1478 unsigned int pos;
1479 unsigned int queued:1;
1483 * struct vb2_fileio_data - queue context used by file io emulator
1485 * vb2 provides a compatibility layer and emulator of file io (read and
1486 * write) calls on top of streaming API. For proper operation it required
1487 * this structure to save the driver state between each call of the read
1488 * or write function.
1490 struct vb2_fileio_data {
1491 struct v4l2_requestbuffers req;
1492 struct v4l2_buffer b;
1493 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1494 unsigned int index;
1495 unsigned int q_count;
1496 unsigned int dq_count;
1497 unsigned int flags;
1501 * __vb2_init_fileio() - initialize file io emulator
1502 * @q: videobuf2 queue
1503 * @read: mode selector (1 means read, 0 means write)
1505 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1507 struct vb2_fileio_data *fileio;
1508 int i, ret;
1509 unsigned int count = 0;
1512 * Sanity check
1514 if ((read && !(q->io_modes & VB2_READ)) ||
1515 (!read && !(q->io_modes & VB2_WRITE)))
1516 BUG();
1519 * Check if device supports mapping buffers to kernel virtual space.
1521 if (!q->mem_ops->vaddr)
1522 return -EBUSY;
1525 * Check if streaming api has not been already activated.
1527 if (q->streaming || q->num_buffers > 0)
1528 return -EBUSY;
1531 * Start with count 1, driver can increase it in queue_setup()
1533 count = 1;
1535 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1536 (read) ? "read" : "write", count, q->io_flags);
1538 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1539 if (fileio == NULL)
1540 return -ENOMEM;
1542 fileio->flags = q->io_flags;
1545 * Request buffers and use MMAP type to force driver
1546 * to allocate buffers by itself.
1548 fileio->req.count = count;
1549 fileio->req.memory = V4L2_MEMORY_MMAP;
1550 fileio->req.type = q->type;
1551 ret = vb2_reqbufs(q, &fileio->req);
1552 if (ret)
1553 goto err_kfree;
1556 * Check if plane_count is correct
1557 * (multiplane buffers are not supported).
1559 if (q->bufs[0]->num_planes != 1) {
1560 fileio->req.count = 0;
1561 ret = -EBUSY;
1562 goto err_reqbufs;
1566 * Get kernel address of each buffer.
1568 for (i = 0; i < q->num_buffers; i++) {
1569 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1570 if (fileio->bufs[i].vaddr == NULL)
1571 goto err_reqbufs;
1572 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1576 * Read mode requires pre queuing of all buffers.
1578 if (read) {
1580 * Queue all buffers.
1582 for (i = 0; i < q->num_buffers; i++) {
1583 struct v4l2_buffer *b = &fileio->b;
1584 memset(b, 0, sizeof(*b));
1585 b->type = q->type;
1586 b->memory = q->memory;
1587 b->index = i;
1588 ret = vb2_qbuf(q, b);
1589 if (ret)
1590 goto err_reqbufs;
1591 fileio->bufs[i].queued = 1;
1595 * Start streaming.
1597 ret = vb2_streamon(q, q->type);
1598 if (ret)
1599 goto err_reqbufs;
1602 q->fileio = fileio;
1604 return ret;
1606 err_reqbufs:
1607 vb2_reqbufs(q, &fileio->req);
1609 err_kfree:
1610 kfree(fileio);
1611 return ret;
1615 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1616 * @q: videobuf2 queue
1618 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1620 struct vb2_fileio_data *fileio = q->fileio;
1622 if (fileio) {
1624 * Hack fileio context to enable direct calls to vb2 ioctl
1625 * interface.
1627 q->fileio = NULL;
1629 vb2_streamoff(q, q->type);
1630 fileio->req.count = 0;
1631 vb2_reqbufs(q, &fileio->req);
1632 kfree(fileio);
1633 dprintk(3, "file io emulator closed\n");
1635 return 0;
1639 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1640 * @q: videobuf2 queue
1641 * @data: pointed to target userspace buffer
1642 * @count: number of bytes to read or write
1643 * @ppos: file handle position tracking pointer
1644 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1645 * @read: access mode selector (1 means read, 0 means write)
1647 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1648 loff_t *ppos, int nonblock, int read)
1650 struct vb2_fileio_data *fileio;
1651 struct vb2_fileio_buf *buf;
1652 int ret, index;
1654 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1655 read ? "read" : "write", (long)*ppos, count,
1656 nonblock ? "non" : "");
1658 if (!data)
1659 return -EINVAL;
1662 * Initialize emulator on first call.
1664 if (!q->fileio) {
1665 ret = __vb2_init_fileio(q, read);
1666 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1667 if (ret)
1668 return ret;
1670 fileio = q->fileio;
1673 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1674 * The pointer will be restored before returning from this function.
1676 q->fileio = NULL;
1678 index = fileio->index;
1679 buf = &fileio->bufs[index];
1682 * Check if we need to dequeue the buffer.
1684 if (buf->queued) {
1685 struct vb2_buffer *vb;
1688 * Call vb2_dqbuf to get buffer back.
1690 memset(&fileio->b, 0, sizeof(fileio->b));
1691 fileio->b.type = q->type;
1692 fileio->b.memory = q->memory;
1693 fileio->b.index = index;
1694 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1695 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1696 if (ret)
1697 goto end;
1698 fileio->dq_count += 1;
1701 * Get number of bytes filled by the driver
1703 vb = q->bufs[index];
1704 buf->size = vb2_get_plane_payload(vb, 0);
1705 buf->queued = 0;
1709 * Limit count on last few bytes of the buffer.
1711 if (buf->pos + count > buf->size) {
1712 count = buf->size - buf->pos;
1713 dprintk(5, "reducing read count: %zd\n", count);
1717 * Transfer data to userspace.
1719 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1720 count, index, buf->pos);
1721 if (read)
1722 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1723 else
1724 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1725 if (ret) {
1726 dprintk(3, "file io: error copying data\n");
1727 ret = -EFAULT;
1728 goto end;
1732 * Update counters.
1734 buf->pos += count;
1735 *ppos += count;
1738 * Queue next buffer if required.
1740 if (buf->pos == buf->size ||
1741 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1743 * Check if this is the last buffer to read.
1745 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
1746 fileio->dq_count == 1) {
1747 dprintk(3, "file io: read limit reached\n");
1749 * Restore fileio pointer and release the context.
1751 q->fileio = fileio;
1752 return __vb2_cleanup_fileio(q);
1756 * Call vb2_qbuf and give buffer to the driver.
1758 memset(&fileio->b, 0, sizeof(fileio->b));
1759 fileio->b.type = q->type;
1760 fileio->b.memory = q->memory;
1761 fileio->b.index = index;
1762 fileio->b.bytesused = buf->pos;
1763 ret = vb2_qbuf(q, &fileio->b);
1764 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
1765 if (ret)
1766 goto end;
1769 * Buffer has been queued, update the status
1771 buf->pos = 0;
1772 buf->queued = 1;
1773 buf->size = q->bufs[0]->v4l2_planes[0].length;
1774 fileio->q_count += 1;
1777 * Switch to the next buffer
1779 fileio->index = (index + 1) % q->num_buffers;
1782 * Start streaming if required.
1784 if (!read && !q->streaming) {
1785 ret = vb2_streamon(q, q->type);
1786 if (ret)
1787 goto end;
1792 * Return proper number of bytes processed.
1794 if (ret == 0)
1795 ret = count;
1796 end:
1798 * Restore the fileio context and block vb2 ioctl interface.
1800 q->fileio = fileio;
1801 return ret;
1804 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1805 loff_t *ppos, int nonblocking)
1807 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1809 EXPORT_SYMBOL_GPL(vb2_read);
1811 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
1812 loff_t *ppos, int nonblocking)
1814 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
1816 EXPORT_SYMBOL_GPL(vb2_write);
1818 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1819 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1820 MODULE_LICENSE("GPL");