[media] media: vb2: set buffer length correctly for all buffer types
[linux-2.6.git] / drivers / media / video / videobuf2-core.c
blobe7c52ff3c76138eb94601e9304bd85c2402b1fb1
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 | \
42 V4L2_BUF_FLAG_PREPARED)
44 /**
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;
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 q->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 = q->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, unsigned int n)
115 unsigned int buffer, plane;
116 struct vb2_buffer *vb;
117 unsigned long off;
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);
124 } else {
125 off = 0;
128 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
129 vb = q->bufs[buffer];
130 if (!vb)
131 continue;
133 for (plane = 0; plane < vb->num_planes; ++plane) {
134 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
135 vb->v4l2_planes[plane].m.mem_offset = off;
137 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
138 buffer, plane, off);
140 off += vb->v4l2_planes[plane].length;
141 off = PAGE_ALIGN(off);
147 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
148 * video buffer memory for all buffers/planes on the queue and initializes the
149 * queue
151 * Returns the number of buffers successfully allocated.
153 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
154 unsigned int num_buffers, unsigned int num_planes)
156 unsigned int buffer;
157 struct vb2_buffer *vb;
158 int ret;
160 for (buffer = 0; buffer < num_buffers; ++buffer) {
161 /* Allocate videobuf buffer structures */
162 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
163 if (!vb) {
164 dprintk(1, "Memory alloc for buffer struct failed\n");
165 break;
168 /* Length stores number of planes for multiplanar buffers */
169 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
170 vb->v4l2_buf.length = num_planes;
172 vb->state = VB2_BUF_STATE_DEQUEUED;
173 vb->vb2_queue = q;
174 vb->num_planes = num_planes;
175 vb->v4l2_buf.index = q->num_buffers + buffer;
176 vb->v4l2_buf.type = q->type;
177 vb->v4l2_buf.memory = memory;
179 /* Allocate video buffer memory for the MMAP type */
180 if (memory == V4L2_MEMORY_MMAP) {
181 ret = __vb2_buf_mem_alloc(vb);
182 if (ret) {
183 dprintk(1, "Failed allocating memory for "
184 "buffer %d\n", buffer);
185 kfree(vb);
186 break;
189 * Call the driver-provided buffer initialization
190 * callback, if given. An error in initialization
191 * results in queue setup failure.
193 ret = call_qop(q, buf_init, vb);
194 if (ret) {
195 dprintk(1, "Buffer %d %p initialization"
196 " failed\n", buffer, vb);
197 __vb2_buf_mem_free(vb);
198 kfree(vb);
199 break;
203 q->bufs[q->num_buffers + buffer] = vb;
206 __setup_offsets(q, buffer);
208 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
209 buffer, num_planes);
211 return buffer;
215 * __vb2_free_mem() - release all video buffer memory for a given queue
217 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
219 unsigned int buffer;
220 struct vb2_buffer *vb;
222 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
223 ++buffer) {
224 vb = q->bufs[buffer];
225 if (!vb)
226 continue;
228 /* Free MMAP buffers or release USERPTR buffers */
229 if (q->memory == V4L2_MEMORY_MMAP)
230 __vb2_buf_mem_free(vb);
231 else
232 __vb2_buf_userptr_put(vb);
237 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
238 * related information, if no buffers are left return the queue to an
239 * uninitialized state. Might be called even if the queue has already been freed.
241 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
243 unsigned int buffer;
245 /* Call driver-provided cleanup function for each buffer, if provided */
246 if (q->ops->buf_cleanup) {
247 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
248 ++buffer) {
249 if (NULL == q->bufs[buffer])
250 continue;
251 q->ops->buf_cleanup(q->bufs[buffer]);
255 /* Release video buffer memory */
256 __vb2_free_mem(q, buffers);
258 /* Free videobuf buffers */
259 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
260 ++buffer) {
261 kfree(q->bufs[buffer]);
262 q->bufs[buffer] = NULL;
265 q->num_buffers -= buffers;
266 if (!q->num_buffers)
267 q->memory = 0;
271 * __verify_planes_array() - verify that the planes array passed in struct
272 * v4l2_buffer from userspace can be safely used
274 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
276 /* Is memory for copying plane information present? */
277 if (NULL == b->m.planes) {
278 dprintk(1, "Multi-planar buffer passed but "
279 "planes array not provided\n");
280 return -EINVAL;
283 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
284 dprintk(1, "Incorrect planes array length, "
285 "expected %d, got %d\n", vb->num_planes, b->length);
286 return -EINVAL;
289 return 0;
293 * __buffer_in_use() - return true if the buffer is in use and
294 * the queue cannot be freed (by the means of REQBUFS(0)) call
296 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
298 unsigned int plane;
299 for (plane = 0; plane < vb->num_planes; ++plane) {
300 void *mem_priv = vb->planes[plane].mem_priv;
302 * If num_users() has not been provided, call_memop
303 * will return 0, apparently nobody cares about this
304 * case anyway. If num_users() returns more than 1,
305 * we are not the only user of the plane's memory.
307 if (mem_priv && call_memop(q, plane, num_users, mem_priv) > 1)
308 return true;
310 return false;
314 * __buffers_in_use() - return true if any buffers on the queue are in use and
315 * the queue cannot be freed (by the means of REQBUFS(0)) call
317 static bool __buffers_in_use(struct vb2_queue *q)
319 unsigned int buffer;
320 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
321 if (__buffer_in_use(q, q->bufs[buffer]))
322 return true;
324 return false;
328 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
329 * returned to userspace
331 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
333 struct vb2_queue *q = vb->vb2_queue;
334 int ret;
336 /* Copy back data such as timestamp, flags, input, etc. */
337 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
338 b->input = vb->v4l2_buf.input;
339 b->reserved = vb->v4l2_buf.reserved;
341 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
342 ret = __verify_planes_array(vb, b);
343 if (ret)
344 return ret;
347 * Fill in plane-related data if userspace provided an array
348 * for it. The memory and size is verified above.
350 memcpy(b->m.planes, vb->v4l2_planes,
351 b->length * sizeof(struct v4l2_plane));
352 } else {
354 * We use length and offset in v4l2_planes array even for
355 * single-planar buffers, but userspace does not.
357 b->length = vb->v4l2_planes[0].length;
358 b->bytesused = vb->v4l2_planes[0].bytesused;
359 if (q->memory == V4L2_MEMORY_MMAP)
360 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
361 else if (q->memory == V4L2_MEMORY_USERPTR)
362 b->m.userptr = vb->v4l2_planes[0].m.userptr;
366 * Clear any buffer state related flags.
368 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
370 switch (vb->state) {
371 case VB2_BUF_STATE_QUEUED:
372 case VB2_BUF_STATE_ACTIVE:
373 b->flags |= V4L2_BUF_FLAG_QUEUED;
374 break;
375 case VB2_BUF_STATE_ERROR:
376 b->flags |= V4L2_BUF_FLAG_ERROR;
377 /* fall through */
378 case VB2_BUF_STATE_DONE:
379 b->flags |= V4L2_BUF_FLAG_DONE;
380 break;
381 case VB2_BUF_STATE_PREPARED:
382 b->flags |= V4L2_BUF_FLAG_PREPARED;
383 break;
384 case VB2_BUF_STATE_DEQUEUED:
385 /* nothing */
386 break;
389 if (__buffer_in_use(q, vb))
390 b->flags |= V4L2_BUF_FLAG_MAPPED;
392 return 0;
396 * vb2_querybuf() - query video buffer information
397 * @q: videobuf queue
398 * @b: buffer struct passed from userspace to vidioc_querybuf handler
399 * in driver
401 * Should be called from vidioc_querybuf ioctl handler in driver.
402 * This function will verify the passed v4l2_buffer structure and fill the
403 * relevant information for the userspace.
405 * The return values from this function are intended to be directly returned
406 * from vidioc_querybuf handler in driver.
408 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
410 struct vb2_buffer *vb;
412 if (b->type != q->type) {
413 dprintk(1, "querybuf: wrong buffer type\n");
414 return -EINVAL;
417 if (b->index >= q->num_buffers) {
418 dprintk(1, "querybuf: buffer index out of range\n");
419 return -EINVAL;
421 vb = q->bufs[b->index];
423 return __fill_v4l2_buffer(vb, b);
425 EXPORT_SYMBOL(vb2_querybuf);
428 * __verify_userptr_ops() - verify that all memory operations required for
429 * USERPTR queue type have been provided
431 static int __verify_userptr_ops(struct vb2_queue *q)
433 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
434 !q->mem_ops->put_userptr)
435 return -EINVAL;
437 return 0;
441 * __verify_mmap_ops() - verify that all memory operations required for
442 * MMAP queue type have been provided
444 static int __verify_mmap_ops(struct vb2_queue *q)
446 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
447 !q->mem_ops->put || !q->mem_ops->mmap)
448 return -EINVAL;
450 return 0;
454 * vb2_reqbufs() - Initiate streaming
455 * @q: videobuf2 queue
456 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
458 * Should be called from vidioc_reqbufs ioctl handler of a driver.
459 * This function:
460 * 1) verifies streaming parameters passed from the userspace,
461 * 2) sets up the queue,
462 * 3) negotiates number of buffers and planes per buffer with the driver
463 * to be used during streaming,
464 * 4) allocates internal buffer structures (struct vb2_buffer), according to
465 * the agreed parameters,
466 * 5) for MMAP memory type, allocates actual video memory, using the
467 * memory handling/allocation routines provided during queue initialization
469 * If req->count is 0, all the memory will be freed instead.
470 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
471 * and the queue is not busy, memory will be reallocated.
473 * The return values from this function are intended to be directly returned
474 * from vidioc_reqbufs handler in driver.
476 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
478 unsigned int num_buffers, allocated_buffers, num_planes = 0;
479 int ret = 0;
481 if (q->fileio) {
482 dprintk(1, "reqbufs: file io in progress\n");
483 return -EBUSY;
486 if (req->memory != V4L2_MEMORY_MMAP
487 && req->memory != V4L2_MEMORY_USERPTR) {
488 dprintk(1, "reqbufs: unsupported memory type\n");
489 return -EINVAL;
492 if (req->type != q->type) {
493 dprintk(1, "reqbufs: requested type is incorrect\n");
494 return -EINVAL;
497 if (q->streaming) {
498 dprintk(1, "reqbufs: streaming active\n");
499 return -EBUSY;
503 * Make sure all the required memory ops for given memory type
504 * are available.
506 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
507 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
508 return -EINVAL;
511 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
512 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
513 return -EINVAL;
516 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
518 * We already have buffers allocated, so first check if they
519 * are not in use and can be freed.
521 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
522 dprintk(1, "reqbufs: memory in use, cannot free\n");
523 return -EBUSY;
526 __vb2_queue_free(q, q->num_buffers);
529 * In case of REQBUFS(0) return immediately without calling
530 * driver's queue_setup() callback and allocating resources.
532 if (req->count == 0)
533 return 0;
537 * Make sure the requested values and current defaults are sane.
539 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
540 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
541 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
542 q->memory = req->memory;
545 * Ask the driver how many buffers and planes per buffer it requires.
546 * Driver also sets the size and allocator context for each plane.
548 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
549 q->plane_sizes, q->alloc_ctx);
550 if (ret)
551 return ret;
553 /* Finally, allocate buffers and video memory */
554 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
555 if (ret == 0) {
556 dprintk(1, "Memory allocation failed\n");
557 return -ENOMEM;
560 allocated_buffers = ret;
563 * Check if driver can handle the allocated number of buffers.
565 if (allocated_buffers < num_buffers) {
566 num_buffers = allocated_buffers;
568 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
569 &num_planes, q->plane_sizes, q->alloc_ctx);
571 if (!ret && allocated_buffers < num_buffers)
572 ret = -ENOMEM;
575 * Either the driver has accepted a smaller number of buffers,
576 * or .queue_setup() returned an error
580 q->num_buffers = allocated_buffers;
582 if (ret < 0) {
583 __vb2_queue_free(q, allocated_buffers);
584 return ret;
588 * Return the number of successfully allocated buffers
589 * to the userspace.
591 req->count = allocated_buffers;
593 return 0;
595 EXPORT_SYMBOL_GPL(vb2_reqbufs);
598 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
599 * @q: videobuf2 queue
600 * @create: creation parameters, passed from userspace to vidioc_create_bufs
601 * handler in driver
603 * Should be called from vidioc_create_bufs ioctl handler of a driver.
604 * This function:
605 * 1) verifies parameter sanity
606 * 2) calls the .queue_setup() queue operation
607 * 3) performs any necessary memory allocations
609 * The return values from this function are intended to be directly returned
610 * from vidioc_create_bufs handler in driver.
612 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
614 unsigned int num_planes = 0, num_buffers, allocated_buffers;
615 int ret = 0;
617 if (q->fileio) {
618 dprintk(1, "%s(): file io in progress\n", __func__);
619 return -EBUSY;
622 if (create->memory != V4L2_MEMORY_MMAP
623 && create->memory != V4L2_MEMORY_USERPTR) {
624 dprintk(1, "%s(): unsupported memory type\n", __func__);
625 return -EINVAL;
628 if (create->format.type != q->type) {
629 dprintk(1, "%s(): requested type is incorrect\n", __func__);
630 return -EINVAL;
634 * Make sure all the required memory ops for given memory type
635 * are available.
637 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
638 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
639 return -EINVAL;
642 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
643 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
644 return -EINVAL;
647 if (q->num_buffers == VIDEO_MAX_FRAME) {
648 dprintk(1, "%s(): maximum number of buffers already allocated\n",
649 __func__);
650 return -ENOBUFS;
653 create->index = q->num_buffers;
655 if (!q->num_buffers) {
656 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
657 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
658 q->memory = create->memory;
661 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
664 * Ask the driver, whether the requested number of buffers, planes per
665 * buffer and their sizes are acceptable
667 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
668 &num_planes, q->plane_sizes, q->alloc_ctx);
669 if (ret)
670 return ret;
672 /* Finally, allocate buffers and video memory */
673 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
674 num_planes);
675 if (ret < 0) {
676 dprintk(1, "Memory allocation failed with error: %d\n", ret);
677 return ret;
680 allocated_buffers = ret;
683 * Check if driver can handle the so far allocated number of buffers.
685 if (ret < num_buffers) {
686 num_buffers = ret;
689 * q->num_buffers contains the total number of buffers, that the
690 * queue driver has set up
692 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
693 &num_planes, q->plane_sizes, q->alloc_ctx);
695 if (!ret && allocated_buffers < num_buffers)
696 ret = -ENOMEM;
699 * Either the driver has accepted a smaller number of buffers,
700 * or .queue_setup() returned an error
704 q->num_buffers += allocated_buffers;
706 if (ret < 0) {
707 __vb2_queue_free(q, allocated_buffers);
708 return ret;
712 * Return the number of successfully allocated buffers
713 * to the userspace.
715 create->count = allocated_buffers;
717 return 0;
719 EXPORT_SYMBOL_GPL(vb2_create_bufs);
722 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
723 * @vb: vb2_buffer to which the plane in question belongs to
724 * @plane_no: plane number for which the address is to be returned
726 * This function returns a kernel virtual address of a given plane if
727 * such a mapping exist, NULL otherwise.
729 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
731 struct vb2_queue *q = vb->vb2_queue;
733 if (plane_no > vb->num_planes)
734 return NULL;
736 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
739 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
742 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
743 * @vb: vb2_buffer to which the plane in question belongs to
744 * @plane_no: plane number for which the cookie is to be returned
746 * This function returns an allocator specific cookie for a given plane if
747 * available, NULL otherwise. The allocator should provide some simple static
748 * inline function, which would convert this cookie to the allocator specific
749 * type that can be used directly by the driver to access the buffer. This can
750 * be for example physical address, pointer to scatter list or IOMMU mapping.
752 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
754 struct vb2_queue *q = vb->vb2_queue;
756 if (plane_no > vb->num_planes)
757 return NULL;
759 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
761 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
764 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
765 * @vb: vb2_buffer returned from the driver
766 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
767 * or VB2_BUF_STATE_ERROR if the operation finished with an error
769 * This function should be called by the driver after a hardware operation on
770 * a buffer is finished and the buffer may be returned to userspace. The driver
771 * cannot use this buffer anymore until it is queued back to it by videobuf
772 * by the means of buf_queue callback. Only buffers previously queued to the
773 * driver by buf_queue can be passed to this function.
775 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
777 struct vb2_queue *q = vb->vb2_queue;
778 unsigned long flags;
780 if (vb->state != VB2_BUF_STATE_ACTIVE)
781 return;
783 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
784 return;
786 dprintk(4, "Done processing on buffer %d, state: %d\n",
787 vb->v4l2_buf.index, vb->state);
789 /* Add the buffer to the done buffers list */
790 spin_lock_irqsave(&q->done_lock, flags);
791 vb->state = state;
792 list_add_tail(&vb->done_entry, &q->done_list);
793 atomic_dec(&q->queued_count);
794 spin_unlock_irqrestore(&q->done_lock, flags);
796 /* Inform any processes that may be waiting for buffers */
797 wake_up(&q->done_wq);
799 EXPORT_SYMBOL_GPL(vb2_buffer_done);
802 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
803 * a v4l2_buffer by the userspace
805 static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
806 struct v4l2_plane *v4l2_planes)
808 unsigned int plane;
809 int ret;
811 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
813 * Verify that the userspace gave us a valid array for
814 * plane information.
816 ret = __verify_planes_array(vb, b);
817 if (ret)
818 return ret;
820 /* Fill in driver-provided information for OUTPUT types */
821 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
823 * Will have to go up to b->length when API starts
824 * accepting variable number of planes.
826 for (plane = 0; plane < vb->num_planes; ++plane) {
827 v4l2_planes[plane].bytesused =
828 b->m.planes[plane].bytesused;
829 v4l2_planes[plane].data_offset =
830 b->m.planes[plane].data_offset;
834 if (b->memory == V4L2_MEMORY_USERPTR) {
835 for (plane = 0; plane < vb->num_planes; ++plane) {
836 v4l2_planes[plane].m.userptr =
837 b->m.planes[plane].m.userptr;
838 v4l2_planes[plane].length =
839 b->m.planes[plane].length;
842 } else {
844 * Single-planar buffers do not use planes array,
845 * so fill in relevant v4l2_buffer struct fields instead.
846 * In videobuf we use our internal V4l2_planes struct for
847 * single-planar buffers as well, for simplicity.
849 if (V4L2_TYPE_IS_OUTPUT(b->type))
850 v4l2_planes[0].bytesused = b->bytesused;
852 if (b->memory == V4L2_MEMORY_USERPTR) {
853 v4l2_planes[0].m.userptr = b->m.userptr;
854 v4l2_planes[0].length = b->length;
858 vb->v4l2_buf.field = b->field;
859 vb->v4l2_buf.timestamp = b->timestamp;
860 vb->v4l2_buf.input = b->input;
861 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
863 return 0;
867 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
869 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
871 struct v4l2_plane planes[VIDEO_MAX_PLANES];
872 struct vb2_queue *q = vb->vb2_queue;
873 void *mem_priv;
874 unsigned int plane;
875 int ret;
876 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
878 /* Verify and copy relevant information provided by the userspace */
879 ret = __fill_vb2_buffer(vb, b, planes);
880 if (ret)
881 return ret;
883 for (plane = 0; plane < vb->num_planes; ++plane) {
884 /* Skip the plane if already verified */
885 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
886 && vb->v4l2_planes[plane].length == planes[plane].length)
887 continue;
889 dprintk(3, "qbuf: userspace address for plane %d changed, "
890 "reacquiring memory\n", plane);
892 /* Check if the provided plane buffer is large enough */
893 if (planes[plane].length < q->plane_sizes[plane]) {
894 ret = -EINVAL;
895 goto err;
898 /* Release previously acquired memory if present */
899 if (vb->planes[plane].mem_priv)
900 call_memop(q, plane, put_userptr,
901 vb->planes[plane].mem_priv);
903 vb->planes[plane].mem_priv = NULL;
904 vb->v4l2_planes[plane].m.userptr = 0;
905 vb->v4l2_planes[plane].length = 0;
907 /* Acquire each plane's memory */
908 if (q->mem_ops->get_userptr) {
909 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
910 planes[plane].m.userptr,
911 planes[plane].length,
912 write);
913 if (IS_ERR(mem_priv)) {
914 dprintk(1, "qbuf: failed acquiring userspace "
915 "memory for plane %d\n", plane);
916 ret = PTR_ERR(mem_priv);
917 goto err;
919 vb->planes[plane].mem_priv = mem_priv;
924 * Call driver-specific initialization on the newly acquired buffer,
925 * if provided.
927 ret = call_qop(q, buf_init, vb);
928 if (ret) {
929 dprintk(1, "qbuf: buffer initialization failed\n");
930 goto err;
934 * Now that everything is in order, copy relevant information
935 * provided by userspace.
937 for (plane = 0; plane < vb->num_planes; ++plane)
938 vb->v4l2_planes[plane] = planes[plane];
940 return 0;
941 err:
942 /* In case of errors, release planes that were already acquired */
943 for (plane = 0; plane < vb->num_planes; ++plane) {
944 if (vb->planes[plane].mem_priv)
945 call_memop(q, plane, put_userptr,
946 vb->planes[plane].mem_priv);
947 vb->planes[plane].mem_priv = NULL;
948 vb->v4l2_planes[plane].m.userptr = 0;
949 vb->v4l2_planes[plane].length = 0;
952 return ret;
956 * __qbuf_mmap() - handle qbuf of an MMAP buffer
958 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
960 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
964 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
966 static void __enqueue_in_driver(struct vb2_buffer *vb)
968 struct vb2_queue *q = vb->vb2_queue;
970 vb->state = VB2_BUF_STATE_ACTIVE;
971 atomic_inc(&q->queued_count);
972 q->ops->buf_queue(vb);
975 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
977 struct vb2_queue *q = vb->vb2_queue;
978 int ret;
980 switch (q->memory) {
981 case V4L2_MEMORY_MMAP:
982 ret = __qbuf_mmap(vb, b);
983 break;
984 case V4L2_MEMORY_USERPTR:
985 ret = __qbuf_userptr(vb, b);
986 break;
987 default:
988 WARN(1, "Invalid queue type\n");
989 ret = -EINVAL;
992 if (!ret)
993 ret = call_qop(q, buf_prepare, vb);
994 if (ret)
995 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
996 else
997 vb->state = VB2_BUF_STATE_PREPARED;
999 return ret;
1003 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1004 * @q: videobuf2 queue
1005 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1006 * handler in driver
1008 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1009 * This function:
1010 * 1) verifies the passed buffer,
1011 * 2) calls buf_prepare callback in the driver (if provided), in which
1012 * driver-specific buffer initialization can be performed,
1014 * The return values from this function are intended to be directly returned
1015 * from vidioc_prepare_buf handler in driver.
1017 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1019 struct vb2_buffer *vb;
1020 int ret;
1022 if (q->fileio) {
1023 dprintk(1, "%s(): file io in progress\n", __func__);
1024 return -EBUSY;
1027 if (b->type != q->type) {
1028 dprintk(1, "%s(): invalid buffer type\n", __func__);
1029 return -EINVAL;
1032 if (b->index >= q->num_buffers) {
1033 dprintk(1, "%s(): buffer index out of range\n", __func__);
1034 return -EINVAL;
1037 vb = q->bufs[b->index];
1038 if (NULL == vb) {
1039 /* Should never happen */
1040 dprintk(1, "%s(): buffer is NULL\n", __func__);
1041 return -EINVAL;
1044 if (b->memory != q->memory) {
1045 dprintk(1, "%s(): invalid memory type\n", __func__);
1046 return -EINVAL;
1049 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1050 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1051 return -EINVAL;
1054 ret = __buf_prepare(vb, b);
1055 if (ret < 0)
1056 return ret;
1058 __fill_v4l2_buffer(vb, b);
1060 return 0;
1062 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1065 * vb2_qbuf() - Queue a buffer from userspace
1066 * @q: videobuf2 queue
1067 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1068 * in driver
1070 * Should be called from vidioc_qbuf ioctl handler of a driver.
1071 * This function:
1072 * 1) verifies the passed buffer,
1073 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1074 * which driver-specific buffer initialization can be performed,
1075 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1076 * callback for processing.
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_qbuf handler in driver.
1081 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1083 struct vb2_buffer *vb;
1084 int ret;
1086 if (q->fileio) {
1087 dprintk(1, "qbuf: file io in progress\n");
1088 return -EBUSY;
1091 if (b->type != q->type) {
1092 dprintk(1, "qbuf: invalid buffer type\n");
1093 return -EINVAL;
1096 if (b->index >= q->num_buffers) {
1097 dprintk(1, "qbuf: buffer index out of range\n");
1098 return -EINVAL;
1101 vb = q->bufs[b->index];
1102 if (NULL == vb) {
1103 /* Should never happen */
1104 dprintk(1, "qbuf: buffer is NULL\n");
1105 return -EINVAL;
1108 if (b->memory != q->memory) {
1109 dprintk(1, "qbuf: invalid memory type\n");
1110 return -EINVAL;
1113 switch (vb->state) {
1114 case VB2_BUF_STATE_DEQUEUED:
1115 ret = __buf_prepare(vb, b);
1116 if (ret)
1117 return ret;
1118 case VB2_BUF_STATE_PREPARED:
1119 break;
1120 default:
1121 dprintk(1, "qbuf: buffer already in use\n");
1122 return -EINVAL;
1126 * Add to the queued buffers list, a buffer will stay on it until
1127 * dequeued in dqbuf.
1129 list_add_tail(&vb->queued_entry, &q->queued_list);
1130 vb->state = VB2_BUF_STATE_QUEUED;
1133 * If already streaming, give the buffer to driver for processing.
1134 * If not, the buffer will be given to driver on next streamon.
1136 if (q->streaming)
1137 __enqueue_in_driver(vb);
1139 /* Fill buffer information for the userspace */
1140 __fill_v4l2_buffer(vb, b);
1142 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1143 return 0;
1145 EXPORT_SYMBOL_GPL(vb2_qbuf);
1148 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1149 * for dequeuing
1151 * Will sleep if required for nonblocking == false.
1153 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1156 * All operations on vb_done_list are performed under done_lock
1157 * spinlock protection. However, buffers may be removed from
1158 * it and returned to userspace only while holding both driver's
1159 * lock and the done_lock spinlock. Thus we can be sure that as
1160 * long as we hold the driver's lock, the list will remain not
1161 * empty if list_empty() check succeeds.
1164 for (;;) {
1165 int ret;
1167 if (!q->streaming) {
1168 dprintk(1, "Streaming off, will not wait for buffers\n");
1169 return -EINVAL;
1172 if (!list_empty(&q->done_list)) {
1174 * Found a buffer that we were waiting for.
1176 break;
1179 if (nonblocking) {
1180 dprintk(1, "Nonblocking and no buffers to dequeue, "
1181 "will not wait\n");
1182 return -EAGAIN;
1186 * We are streaming and blocking, wait for another buffer to
1187 * become ready or for streamoff. Driver's lock is released to
1188 * allow streamoff or qbuf to be called while waiting.
1190 call_qop(q, wait_prepare, q);
1193 * All locks have been released, it is safe to sleep now.
1195 dprintk(3, "Will sleep waiting for buffers\n");
1196 ret = wait_event_interruptible(q->done_wq,
1197 !list_empty(&q->done_list) || !q->streaming);
1200 * We need to reevaluate both conditions again after reacquiring
1201 * the locks or return an error if one occurred.
1203 call_qop(q, wait_finish, q);
1204 if (ret)
1205 return ret;
1207 return 0;
1211 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1213 * Will sleep if required for nonblocking == false.
1215 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1216 int nonblocking)
1218 unsigned long flags;
1219 int ret;
1222 * Wait for at least one buffer to become available on the done_list.
1224 ret = __vb2_wait_for_done_vb(q, nonblocking);
1225 if (ret)
1226 return ret;
1229 * Driver's lock has been held since we last verified that done_list
1230 * is not empty, so no need for another list_empty(done_list) check.
1232 spin_lock_irqsave(&q->done_lock, flags);
1233 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1234 list_del(&(*vb)->done_entry);
1235 spin_unlock_irqrestore(&q->done_lock, flags);
1237 return 0;
1241 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1242 * @q: videobuf2 queue
1244 * This function will wait until all buffers that have been given to the driver
1245 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1246 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1247 * taken, for example from stop_streaming() callback.
1249 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1251 if (!q->streaming) {
1252 dprintk(1, "Streaming off, will not wait for buffers\n");
1253 return -EINVAL;
1256 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1257 return 0;
1259 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1262 * vb2_dqbuf() - Dequeue a buffer to the userspace
1263 * @q: videobuf2 queue
1264 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1265 * in driver
1266 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1267 * buffers ready for dequeuing are present. Normally the driver
1268 * would be passing (file->f_flags & O_NONBLOCK) here
1270 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1271 * This function:
1272 * 1) verifies the passed buffer,
1273 * 2) calls buf_finish callback in the driver (if provided), in which
1274 * driver can perform any additional operations that may be required before
1275 * returning the buffer to userspace, such as cache sync,
1276 * 3) the buffer struct members are filled with relevant information for
1277 * the userspace.
1279 * The return values from this function are intended to be directly returned
1280 * from vidioc_dqbuf handler in driver.
1282 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1284 struct vb2_buffer *vb = NULL;
1285 int ret;
1287 if (q->fileio) {
1288 dprintk(1, "dqbuf: file io in progress\n");
1289 return -EBUSY;
1292 if (b->type != q->type) {
1293 dprintk(1, "dqbuf: invalid buffer type\n");
1294 return -EINVAL;
1297 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1298 if (ret < 0) {
1299 dprintk(1, "dqbuf: error getting next done buffer\n");
1300 return ret;
1303 ret = call_qop(q, buf_finish, vb);
1304 if (ret) {
1305 dprintk(1, "dqbuf: buffer finish failed\n");
1306 return ret;
1309 switch (vb->state) {
1310 case VB2_BUF_STATE_DONE:
1311 dprintk(3, "dqbuf: Returning done buffer\n");
1312 break;
1313 case VB2_BUF_STATE_ERROR:
1314 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1315 break;
1316 default:
1317 dprintk(1, "dqbuf: Invalid buffer state\n");
1318 return -EINVAL;
1321 /* Fill buffer information for the userspace */
1322 __fill_v4l2_buffer(vb, b);
1323 /* Remove from videobuf queue */
1324 list_del(&vb->queued_entry);
1326 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1327 vb->v4l2_buf.index, vb->state);
1329 vb->state = VB2_BUF_STATE_DEQUEUED;
1330 return 0;
1332 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1335 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1337 * Removes all queued buffers from driver's queue and all buffers queued by
1338 * userspace from videobuf's queue. Returns to state after reqbufs.
1340 static void __vb2_queue_cancel(struct vb2_queue *q)
1342 unsigned int i;
1345 * Tell driver to stop all transactions and release all queued
1346 * buffers.
1348 if (q->streaming)
1349 call_qop(q, stop_streaming, q);
1350 q->streaming = 0;
1353 * Remove all buffers from videobuf's list...
1355 INIT_LIST_HEAD(&q->queued_list);
1357 * ...and done list; userspace will not receive any buffers it
1358 * has not already dequeued before initiating cancel.
1360 INIT_LIST_HEAD(&q->done_list);
1361 atomic_set(&q->queued_count, 0);
1362 wake_up_all(&q->done_wq);
1365 * Reinitialize all buffers for next use.
1367 for (i = 0; i < q->num_buffers; ++i)
1368 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1372 * vb2_streamon - start streaming
1373 * @q: videobuf2 queue
1374 * @type: type argument passed from userspace to vidioc_streamon handler
1376 * Should be called from vidioc_streamon handler of a driver.
1377 * This function:
1378 * 1) verifies current state
1379 * 2) passes any previously queued buffers to the driver and starts streaming
1381 * The return values from this function are intended to be directly returned
1382 * from vidioc_streamon handler in the driver.
1384 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1386 struct vb2_buffer *vb;
1387 int ret;
1389 if (q->fileio) {
1390 dprintk(1, "streamon: file io in progress\n");
1391 return -EBUSY;
1394 if (type != q->type) {
1395 dprintk(1, "streamon: invalid stream type\n");
1396 return -EINVAL;
1399 if (q->streaming) {
1400 dprintk(1, "streamon: already streaming\n");
1401 return -EBUSY;
1405 * If any buffers were queued before streamon,
1406 * we can now pass them to driver for processing.
1408 list_for_each_entry(vb, &q->queued_list, queued_entry)
1409 __enqueue_in_driver(vb);
1412 * Let driver notice that streaming state has been enabled.
1414 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1415 if (ret) {
1416 dprintk(1, "streamon: driver refused to start streaming\n");
1417 __vb2_queue_cancel(q);
1418 return ret;
1421 q->streaming = 1;
1423 dprintk(3, "Streamon successful\n");
1424 return 0;
1426 EXPORT_SYMBOL_GPL(vb2_streamon);
1430 * vb2_streamoff - stop streaming
1431 * @q: videobuf2 queue
1432 * @type: type argument passed from userspace to vidioc_streamoff handler
1434 * Should be called from vidioc_streamoff handler of a driver.
1435 * This function:
1436 * 1) verifies current state,
1437 * 2) stop streaming and dequeues any queued buffers, including those previously
1438 * passed to the driver (after waiting for the driver to finish).
1440 * This call can be used for pausing playback.
1441 * The return values from this function are intended to be directly returned
1442 * from vidioc_streamoff handler in the driver
1444 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1446 if (q->fileio) {
1447 dprintk(1, "streamoff: file io in progress\n");
1448 return -EBUSY;
1451 if (type != q->type) {
1452 dprintk(1, "streamoff: invalid stream type\n");
1453 return -EINVAL;
1456 if (!q->streaming) {
1457 dprintk(1, "streamoff: not streaming\n");
1458 return -EINVAL;
1462 * Cancel will pause streaming and remove all buffers from the driver
1463 * and videobuf, effectively returning control over them to userspace.
1465 __vb2_queue_cancel(q);
1467 dprintk(3, "Streamoff successful\n");
1468 return 0;
1470 EXPORT_SYMBOL_GPL(vb2_streamoff);
1473 * __find_plane_by_offset() - find plane associated with the given offset off
1475 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1476 unsigned int *_buffer, unsigned int *_plane)
1478 struct vb2_buffer *vb;
1479 unsigned int buffer, plane;
1482 * Go over all buffers and their planes, comparing the given offset
1483 * with an offset assigned to each plane. If a match is found,
1484 * return its buffer and plane numbers.
1486 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1487 vb = q->bufs[buffer];
1489 for (plane = 0; plane < vb->num_planes; ++plane) {
1490 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1491 *_buffer = buffer;
1492 *_plane = plane;
1493 return 0;
1498 return -EINVAL;
1502 * vb2_mmap() - map video buffers into application address space
1503 * @q: videobuf2 queue
1504 * @vma: vma passed to the mmap file operation handler in the driver
1506 * Should be called from mmap file operation handler of a driver.
1507 * This function maps one plane of one of the available video buffers to
1508 * userspace. To map whole video memory allocated on reqbufs, this function
1509 * has to be called once per each plane per each buffer previously allocated.
1511 * When the userspace application calls mmap, it passes to it an offset returned
1512 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1513 * a "cookie", which is then used to identify the plane to be mapped.
1514 * This function finds a plane with a matching offset and a mapping is performed
1515 * by the means of a provided memory operation.
1517 * The return values from this function are intended to be directly returned
1518 * from the mmap handler in driver.
1520 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1522 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1523 struct vb2_plane *vb_plane;
1524 struct vb2_buffer *vb;
1525 unsigned int buffer, plane;
1526 int ret;
1528 if (q->memory != V4L2_MEMORY_MMAP) {
1529 dprintk(1, "Queue is not currently set up for mmap\n");
1530 return -EINVAL;
1534 * Check memory area access mode.
1536 if (!(vma->vm_flags & VM_SHARED)) {
1537 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1538 return -EINVAL;
1540 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1541 if (!(vma->vm_flags & VM_WRITE)) {
1542 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1543 return -EINVAL;
1545 } else {
1546 if (!(vma->vm_flags & VM_READ)) {
1547 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1548 return -EINVAL;
1553 * Find the plane corresponding to the offset passed by userspace.
1555 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1556 if (ret)
1557 return ret;
1559 vb = q->bufs[buffer];
1560 vb_plane = &vb->planes[plane];
1562 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1563 if (ret)
1564 return ret;
1566 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1567 return 0;
1569 EXPORT_SYMBOL_GPL(vb2_mmap);
1571 #ifndef CONFIG_MMU
1572 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1573 unsigned long addr,
1574 unsigned long len,
1575 unsigned long pgoff,
1576 unsigned long flags)
1578 unsigned long off = pgoff << PAGE_SHIFT;
1579 struct vb2_buffer *vb;
1580 unsigned int buffer, plane;
1581 int ret;
1583 if (q->memory != V4L2_MEMORY_MMAP) {
1584 dprintk(1, "Queue is not currently set up for mmap\n");
1585 return -EINVAL;
1589 * Find the plane corresponding to the offset passed by userspace.
1591 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1592 if (ret)
1593 return ret;
1595 vb = q->bufs[buffer];
1597 return (unsigned long)vb2_plane_vaddr(vb, plane);
1599 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1600 #endif
1602 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1603 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1606 * vb2_poll() - implements poll userspace operation
1607 * @q: videobuf2 queue
1608 * @file: file argument passed to the poll file operation handler
1609 * @wait: wait argument passed to the poll file operation handler
1611 * This function implements poll file operation handler for a driver.
1612 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1613 * be informed that the file descriptor of a video device is available for
1614 * reading.
1615 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1616 * will be reported as available for writing.
1618 * The return values from this function are intended to be directly returned
1619 * from poll handler in driver.
1621 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1623 unsigned long flags;
1624 unsigned int ret;
1625 struct vb2_buffer *vb = NULL;
1628 * Start file I/O emulator only if streaming API has not been used yet.
1630 if (q->num_buffers == 0 && q->fileio == NULL) {
1631 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1632 ret = __vb2_init_fileio(q, 1);
1633 if (ret)
1634 return POLLERR;
1636 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1637 ret = __vb2_init_fileio(q, 0);
1638 if (ret)
1639 return POLLERR;
1641 * Write to OUTPUT queue can be done immediately.
1643 return POLLOUT | POLLWRNORM;
1648 * There is nothing to wait for if no buffers have already been queued.
1650 if (list_empty(&q->queued_list))
1651 return POLLERR;
1653 poll_wait(file, &q->done_wq, wait);
1656 * Take first buffer available for dequeuing.
1658 spin_lock_irqsave(&q->done_lock, flags);
1659 if (!list_empty(&q->done_list))
1660 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1661 done_entry);
1662 spin_unlock_irqrestore(&q->done_lock, flags);
1664 if (vb && (vb->state == VB2_BUF_STATE_DONE
1665 || vb->state == VB2_BUF_STATE_ERROR)) {
1666 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1667 POLLIN | POLLRDNORM;
1669 return 0;
1671 EXPORT_SYMBOL_GPL(vb2_poll);
1674 * vb2_queue_init() - initialize a videobuf2 queue
1675 * @q: videobuf2 queue; this structure should be allocated in driver
1677 * The vb2_queue structure should be allocated by the driver. The driver is
1678 * responsible of clearing it's content and setting initial values for some
1679 * required entries before calling this function.
1680 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1681 * to the struct vb2_queue description in include/media/videobuf2-core.h
1682 * for more information.
1684 int vb2_queue_init(struct vb2_queue *q)
1686 BUG_ON(!q);
1687 BUG_ON(!q->ops);
1688 BUG_ON(!q->mem_ops);
1689 BUG_ON(!q->type);
1690 BUG_ON(!q->io_modes);
1692 BUG_ON(!q->ops->queue_setup);
1693 BUG_ON(!q->ops->buf_queue);
1695 INIT_LIST_HEAD(&q->queued_list);
1696 INIT_LIST_HEAD(&q->done_list);
1697 spin_lock_init(&q->done_lock);
1698 init_waitqueue_head(&q->done_wq);
1700 if (q->buf_struct_size == 0)
1701 q->buf_struct_size = sizeof(struct vb2_buffer);
1703 return 0;
1705 EXPORT_SYMBOL_GPL(vb2_queue_init);
1708 * vb2_queue_release() - stop streaming, release the queue and free memory
1709 * @q: videobuf2 queue
1711 * This function stops streaming and performs necessary clean ups, including
1712 * freeing video buffer memory. The driver is responsible for freeing
1713 * the vb2_queue structure itself.
1715 void vb2_queue_release(struct vb2_queue *q)
1717 __vb2_cleanup_fileio(q);
1718 __vb2_queue_cancel(q);
1719 __vb2_queue_free(q, q->num_buffers);
1721 EXPORT_SYMBOL_GPL(vb2_queue_release);
1724 * struct vb2_fileio_buf - buffer context used by file io emulator
1726 * vb2 provides a compatibility layer and emulator of file io (read and
1727 * write) calls on top of streaming API. This structure is used for
1728 * tracking context related to the buffers.
1730 struct vb2_fileio_buf {
1731 void *vaddr;
1732 unsigned int size;
1733 unsigned int pos;
1734 unsigned int queued:1;
1738 * struct vb2_fileio_data - queue context used by file io emulator
1740 * vb2 provides a compatibility layer and emulator of file io (read and
1741 * write) calls on top of streaming API. For proper operation it required
1742 * this structure to save the driver state between each call of the read
1743 * or write function.
1745 struct vb2_fileio_data {
1746 struct v4l2_requestbuffers req;
1747 struct v4l2_buffer b;
1748 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1749 unsigned int index;
1750 unsigned int q_count;
1751 unsigned int dq_count;
1752 unsigned int flags;
1756 * __vb2_init_fileio() - initialize file io emulator
1757 * @q: videobuf2 queue
1758 * @read: mode selector (1 means read, 0 means write)
1760 static int __vb2_init_fileio(struct vb2_queue *q, int read)
1762 struct vb2_fileio_data *fileio;
1763 int i, ret;
1764 unsigned int count = 0;
1767 * Sanity check
1769 if ((read && !(q->io_modes & VB2_READ)) ||
1770 (!read && !(q->io_modes & VB2_WRITE)))
1771 BUG();
1774 * Check if device supports mapping buffers to kernel virtual space.
1776 if (!q->mem_ops->vaddr)
1777 return -EBUSY;
1780 * Check if streaming api has not been already activated.
1782 if (q->streaming || q->num_buffers > 0)
1783 return -EBUSY;
1786 * Start with count 1, driver can increase it in queue_setup()
1788 count = 1;
1790 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1791 (read) ? "read" : "write", count, q->io_flags);
1793 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1794 if (fileio == NULL)
1795 return -ENOMEM;
1797 fileio->flags = q->io_flags;
1800 * Request buffers and use MMAP type to force driver
1801 * to allocate buffers by itself.
1803 fileio->req.count = count;
1804 fileio->req.memory = V4L2_MEMORY_MMAP;
1805 fileio->req.type = q->type;
1806 ret = vb2_reqbufs(q, &fileio->req);
1807 if (ret)
1808 goto err_kfree;
1811 * Check if plane_count is correct
1812 * (multiplane buffers are not supported).
1814 if (q->bufs[0]->num_planes != 1) {
1815 fileio->req.count = 0;
1816 ret = -EBUSY;
1817 goto err_reqbufs;
1821 * Get kernel address of each buffer.
1823 for (i = 0; i < q->num_buffers; i++) {
1824 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1825 if (fileio->bufs[i].vaddr == NULL)
1826 goto err_reqbufs;
1827 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1831 * Read mode requires pre queuing of all buffers.
1833 if (read) {
1835 * Queue all buffers.
1837 for (i = 0; i < q->num_buffers; i++) {
1838 struct v4l2_buffer *b = &fileio->b;
1839 memset(b, 0, sizeof(*b));
1840 b->type = q->type;
1841 b->memory = q->memory;
1842 b->index = i;
1843 ret = vb2_qbuf(q, b);
1844 if (ret)
1845 goto err_reqbufs;
1846 fileio->bufs[i].queued = 1;
1850 * Start streaming.
1852 ret = vb2_streamon(q, q->type);
1853 if (ret)
1854 goto err_reqbufs;
1857 q->fileio = fileio;
1859 return ret;
1861 err_reqbufs:
1862 vb2_reqbufs(q, &fileio->req);
1864 err_kfree:
1865 kfree(fileio);
1866 return ret;
1870 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1871 * @q: videobuf2 queue
1873 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1875 struct vb2_fileio_data *fileio = q->fileio;
1877 if (fileio) {
1879 * Hack fileio context to enable direct calls to vb2 ioctl
1880 * interface.
1882 q->fileio = NULL;
1884 vb2_streamoff(q, q->type);
1885 fileio->req.count = 0;
1886 vb2_reqbufs(q, &fileio->req);
1887 kfree(fileio);
1888 dprintk(3, "file io emulator closed\n");
1890 return 0;
1894 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1895 * @q: videobuf2 queue
1896 * @data: pointed to target userspace buffer
1897 * @count: number of bytes to read or write
1898 * @ppos: file handle position tracking pointer
1899 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1900 * @read: access mode selector (1 means read, 0 means write)
1902 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1903 loff_t *ppos, int nonblock, int read)
1905 struct vb2_fileio_data *fileio;
1906 struct vb2_fileio_buf *buf;
1907 int ret, index;
1909 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1910 read ? "read" : "write", (long)*ppos, count,
1911 nonblock ? "non" : "");
1913 if (!data)
1914 return -EINVAL;
1917 * Initialize emulator on first call.
1919 if (!q->fileio) {
1920 ret = __vb2_init_fileio(q, read);
1921 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1922 if (ret)
1923 return ret;
1925 fileio = q->fileio;
1928 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1929 * The pointer will be restored before returning from this function.
1931 q->fileio = NULL;
1933 index = fileio->index;
1934 buf = &fileio->bufs[index];
1937 * Check if we need to dequeue the buffer.
1939 if (buf->queued) {
1940 struct vb2_buffer *vb;
1943 * Call vb2_dqbuf to get buffer back.
1945 memset(&fileio->b, 0, sizeof(fileio->b));
1946 fileio->b.type = q->type;
1947 fileio->b.memory = q->memory;
1948 fileio->b.index = index;
1949 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1950 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1951 if (ret)
1952 goto end;
1953 fileio->dq_count += 1;
1956 * Get number of bytes filled by the driver
1958 vb = q->bufs[index];
1959 buf->size = vb2_get_plane_payload(vb, 0);
1960 buf->queued = 0;
1964 * Limit count on last few bytes of the buffer.
1966 if (buf->pos + count > buf->size) {
1967 count = buf->size - buf->pos;
1968 dprintk(5, "reducing read count: %zd\n", count);
1972 * Transfer data to userspace.
1974 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1975 count, index, buf->pos);
1976 if (read)
1977 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1978 else
1979 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1980 if (ret) {
1981 dprintk(3, "file io: error copying data\n");
1982 ret = -EFAULT;
1983 goto end;
1987 * Update counters.
1989 buf->pos += count;
1990 *ppos += count;
1993 * Queue next buffer if required.
1995 if (buf->pos == buf->size ||
1996 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1998 * Check if this is the last buffer to read.
2000 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2001 fileio->dq_count == 1) {
2002 dprintk(3, "file io: read limit reached\n");
2004 * Restore fileio pointer and release the context.
2006 q->fileio = fileio;
2007 return __vb2_cleanup_fileio(q);
2011 * Call vb2_qbuf and give buffer to the driver.
2013 memset(&fileio->b, 0, sizeof(fileio->b));
2014 fileio->b.type = q->type;
2015 fileio->b.memory = q->memory;
2016 fileio->b.index = index;
2017 fileio->b.bytesused = buf->pos;
2018 ret = vb2_qbuf(q, &fileio->b);
2019 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2020 if (ret)
2021 goto end;
2024 * Buffer has been queued, update the status
2026 buf->pos = 0;
2027 buf->queued = 1;
2028 buf->size = q->bufs[0]->v4l2_planes[0].length;
2029 fileio->q_count += 1;
2032 * Switch to the next buffer
2034 fileio->index = (index + 1) % q->num_buffers;
2037 * Start streaming if required.
2039 if (!read && !q->streaming) {
2040 ret = vb2_streamon(q, q->type);
2041 if (ret)
2042 goto end;
2047 * Return proper number of bytes processed.
2049 if (ret == 0)
2050 ret = count;
2051 end:
2053 * Restore the fileio context and block vb2 ioctl interface.
2055 q->fileio = fileio;
2056 return ret;
2059 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2060 loff_t *ppos, int nonblocking)
2062 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2064 EXPORT_SYMBOL_GPL(vb2_read);
2066 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2067 loff_t *ppos, int nonblocking)
2069 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2071 EXPORT_SYMBOL_GPL(vb2_write);
2073 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2074 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2075 MODULE_LICENSE("GPL");