[media] v4l: add videobuf2 Video for Linux 2 driver framework
[linux-2.6/btrfs-unstable.git] / drivers / media / video / videobuf2-core.c
blobb856bd105d7751d8063274d2cef87bd5360c19d6
1 /*
2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <p.osciak@samsung.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 /**
41 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
43 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb,
44 unsigned long *plane_sizes)
46 struct vb2_queue *q = vb->vb2_queue;
47 void *mem_priv;
48 int plane;
50 /* Allocate memory for all planes in this buffer */
51 for (plane = 0; plane < vb->num_planes; ++plane) {
52 mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
53 plane_sizes[plane]);
54 if (!mem_priv)
55 goto free;
57 /* Associate allocator private data with this plane */
58 vb->planes[plane].mem_priv = mem_priv;
59 vb->v4l2_planes[plane].length = plane_sizes[plane];
62 return 0;
63 free:
64 /* Free already allocated memory if one of the allocations failed */
65 for (; plane > 0; --plane)
66 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
68 return -ENOMEM;
71 /**
72 * __vb2_buf_mem_free() - free memory of the given buffer
74 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
76 struct vb2_queue *q = vb->vb2_queue;
77 unsigned int plane;
79 for (plane = 0; plane < vb->num_planes; ++plane) {
80 call_memop(q, plane, put, vb->planes[plane].mem_priv);
81 vb->planes[plane].mem_priv = NULL;
82 dprintk(3, "Freed plane %d of buffer %d\n",
83 plane, vb->v4l2_buf.index);
87 /**
88 * __vb2_buf_userptr_put() - release userspace memory associated with
89 * a USERPTR buffer
91 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
93 struct vb2_queue *q = vb->vb2_queue;
94 unsigned int plane;
96 for (plane = 0; plane < vb->num_planes; ++plane) {
97 void *mem_priv = vb->planes[plane].mem_priv;
99 if (mem_priv) {
100 call_memop(q, plane, put_userptr, mem_priv);
101 vb->planes[plane].mem_priv = NULL;
107 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
108 * every buffer on the queue
110 static void __setup_offsets(struct vb2_queue *q)
112 unsigned int buffer, plane;
113 struct vb2_buffer *vb;
114 unsigned long off = 0;
116 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
117 vb = q->bufs[buffer];
118 if (!vb)
119 continue;
121 for (plane = 0; plane < vb->num_planes; ++plane) {
122 vb->v4l2_planes[plane].m.mem_offset = off;
124 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
125 buffer, plane, off);
127 off += vb->v4l2_planes[plane].length;
128 off = PAGE_ALIGN(off);
134 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
135 * video buffer memory for all buffers/planes on the queue and initializes the
136 * queue
138 * Returns the number of buffers successfully allocated.
140 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
141 unsigned int num_buffers, unsigned int num_planes,
142 unsigned long plane_sizes[])
144 unsigned int buffer;
145 struct vb2_buffer *vb;
146 int ret;
148 for (buffer = 0; buffer < num_buffers; ++buffer) {
149 /* Allocate videobuf buffer structures */
150 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
151 if (!vb) {
152 dprintk(1, "Memory alloc for buffer struct failed\n");
153 break;
156 /* Length stores number of planes for multiplanar buffers */
157 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
158 vb->v4l2_buf.length = num_planes;
160 vb->state = VB2_BUF_STATE_DEQUEUED;
161 vb->vb2_queue = q;
162 vb->num_planes = num_planes;
163 vb->v4l2_buf.index = buffer;
164 vb->v4l2_buf.type = q->type;
165 vb->v4l2_buf.memory = memory;
167 /* Allocate video buffer memory for the MMAP type */
168 if (memory == V4L2_MEMORY_MMAP) {
169 ret = __vb2_buf_mem_alloc(vb, plane_sizes);
170 if (ret) {
171 dprintk(1, "Failed allocating memory for "
172 "buffer %d\n", buffer);
173 kfree(vb);
174 break;
177 * Call the driver-provided buffer initialization
178 * callback, if given. An error in initialization
179 * results in queue setup failure.
181 ret = call_qop(q, buf_init, vb);
182 if (ret) {
183 dprintk(1, "Buffer %d %p initialization"
184 " failed\n", buffer, vb);
185 __vb2_buf_mem_free(vb);
186 kfree(vb);
187 break;
191 q->bufs[buffer] = vb;
194 q->num_buffers = buffer;
196 __setup_offsets(q);
198 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
199 q->num_buffers, num_planes);
201 return buffer;
205 * __vb2_free_mem() - release all video buffer memory for a given queue
207 static void __vb2_free_mem(struct vb2_queue *q)
209 unsigned int buffer;
210 struct vb2_buffer *vb;
212 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
213 vb = q->bufs[buffer];
214 if (!vb)
215 continue;
217 /* Free MMAP buffers or release USERPTR buffers */
218 if (q->memory == V4L2_MEMORY_MMAP)
219 __vb2_buf_mem_free(vb);
220 else
221 __vb2_buf_userptr_put(vb);
226 * __vb2_queue_free() - free the queue - video memory and related information
227 * and return the queue to an uninitialized state. Might be called even if the
228 * queue has already been freed.
230 static int __vb2_queue_free(struct vb2_queue *q)
232 unsigned int buffer;
234 /* Call driver-provided cleanup function for each buffer, if provided */
235 if (q->ops->buf_cleanup) {
236 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
237 if (NULL == q->bufs[buffer])
238 continue;
239 q->ops->buf_cleanup(q->bufs[buffer]);
243 /* Release video buffer memory */
244 __vb2_free_mem(q);
246 /* Free videobuf buffers */
247 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
248 kfree(q->bufs[buffer]);
249 q->bufs[buffer] = NULL;
252 q->num_buffers = 0;
253 q->memory = 0;
255 return 0;
259 * __verify_planes_array() - verify that the planes array passed in struct
260 * v4l2_buffer from userspace can be safely used
262 static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b)
264 /* Is memory for copying plane information present? */
265 if (NULL == b->m.planes) {
266 dprintk(1, "Multi-planar buffer passed but "
267 "planes array not provided\n");
268 return -EINVAL;
271 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
272 dprintk(1, "Incorrect planes array length, "
273 "expected %d, got %d\n", vb->num_planes, b->length);
274 return -EINVAL;
277 return 0;
281 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
282 * returned to userspace
284 static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
286 struct vb2_queue *q = vb->vb2_queue;
287 int ret = 0;
289 /* Copy back data such as timestamp, input, etc. */
290 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
291 b->input = vb->v4l2_buf.input;
292 b->reserved = vb->v4l2_buf.reserved;
294 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
295 ret = __verify_planes_array(vb, b);
296 if (ret)
297 return ret;
300 * Fill in plane-related data if userspace provided an array
301 * for it. The memory and size is verified above.
303 memcpy(b->m.planes, vb->v4l2_planes,
304 b->length * sizeof(struct v4l2_plane));
305 } else {
307 * We use length and offset in v4l2_planes array even for
308 * single-planar buffers, but userspace does not.
310 b->length = vb->v4l2_planes[0].length;
311 b->bytesused = vb->v4l2_planes[0].bytesused;
312 if (q->memory == V4L2_MEMORY_MMAP)
313 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
314 else if (q->memory == V4L2_MEMORY_USERPTR)
315 b->m.userptr = vb->v4l2_planes[0].m.userptr;
318 b->flags = 0;
320 switch (vb->state) {
321 case VB2_BUF_STATE_QUEUED:
322 case VB2_BUF_STATE_ACTIVE:
323 b->flags |= V4L2_BUF_FLAG_QUEUED;
324 break;
325 case VB2_BUF_STATE_ERROR:
326 b->flags |= V4L2_BUF_FLAG_ERROR;
327 /* fall through */
328 case VB2_BUF_STATE_DONE:
329 b->flags |= V4L2_BUF_FLAG_DONE;
330 break;
331 case VB2_BUF_STATE_DEQUEUED:
332 /* nothing */
333 break;
336 if (vb->num_planes_mapped == vb->num_planes)
337 b->flags |= V4L2_BUF_FLAG_MAPPED;
339 return ret;
343 * vb2_querybuf() - query video buffer information
344 * @q: videobuf queue
345 * @b: buffer struct passed from userspace to vidioc_querybuf handler
346 * in driver
348 * Should be called from vidioc_querybuf ioctl handler in driver.
349 * This function will verify the passed v4l2_buffer structure and fill the
350 * relevant information for the userspace.
352 * The return values from this function are intended to be directly returned
353 * from vidioc_querybuf handler in driver.
355 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
357 struct vb2_buffer *vb;
359 if (b->type != q->type) {
360 dprintk(1, "querybuf: wrong buffer type\n");
361 return -EINVAL;
364 if (b->index >= q->num_buffers) {
365 dprintk(1, "querybuf: buffer index out of range\n");
366 return -EINVAL;
368 vb = q->bufs[b->index];
370 return __fill_v4l2_buffer(vb, b);
372 EXPORT_SYMBOL(vb2_querybuf);
375 * __verify_userptr_ops() - verify that all memory operations required for
376 * USERPTR queue type have been provided
378 static int __verify_userptr_ops(struct vb2_queue *q)
380 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
381 !q->mem_ops->put_userptr)
382 return -EINVAL;
384 return 0;
388 * __verify_mmap_ops() - verify that all memory operations required for
389 * MMAP queue type have been provided
391 static int __verify_mmap_ops(struct vb2_queue *q)
393 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
394 !q->mem_ops->put || !q->mem_ops->mmap)
395 return -EINVAL;
397 return 0;
401 * __buffers_in_use() - return true if any buffers on the queue are in use and
402 * the queue cannot be freed (by the means of REQBUFS(0)) call
404 static bool __buffers_in_use(struct vb2_queue *q)
406 unsigned int buffer, plane;
407 struct vb2_buffer *vb;
409 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
410 vb = q->bufs[buffer];
411 for (plane = 0; plane < vb->num_planes; ++plane) {
413 * If num_users() has not been provided, call_memop
414 * will return 0, apparently nobody cares about this
415 * case anyway. If num_users() returns more than 1,
416 * we are not the only user of the plane's memory.
418 if (call_memop(q, plane, num_users,
419 vb->planes[plane].mem_priv) > 1)
420 return true;
424 return false;
428 * vb2_reqbufs() - Initiate streaming
429 * @q: videobuf2 queue
430 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
432 * Should be called from vidioc_reqbufs ioctl handler of a driver.
433 * This function:
434 * 1) verifies streaming parameters passed from the userspace,
435 * 2) sets up the queue,
436 * 3) negotiates number of buffers and planes per buffer with the driver
437 * to be used during streaming,
438 * 4) allocates internal buffer structures (struct vb2_buffer), according to
439 * the agreed parameters,
440 * 5) for MMAP memory type, allocates actual video memory, using the
441 * memory handling/allocation routines provided during queue initialization
443 * If req->count is 0, all the memory will be freed instead.
444 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
445 * and the queue is not busy, memory will be reallocated.
447 * The return values from this function are intended to be directly returned
448 * from vidioc_reqbufs handler in driver.
450 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
452 unsigned int num_buffers, num_planes;
453 unsigned long plane_sizes[VIDEO_MAX_PLANES];
454 int ret = 0;
456 if (req->memory != V4L2_MEMORY_MMAP
457 && req->memory != V4L2_MEMORY_USERPTR) {
458 dprintk(1, "reqbufs: unsupported memory type\n");
459 return -EINVAL;
462 if (req->type != q->type) {
463 dprintk(1, "reqbufs: requested type is incorrect\n");
464 return -EINVAL;
467 if (q->streaming) {
468 dprintk(1, "reqbufs: streaming active\n");
469 return -EBUSY;
473 * Make sure all the required memory ops for given memory type
474 * are available.
476 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
477 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
478 return -EINVAL;
481 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
482 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
483 return -EINVAL;
486 if (req->count == 0 || q->num_buffers != 0) {
488 * We already have buffers allocated, so first check if they
489 * are not in use and can be freed.
491 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
492 dprintk(1, "reqbufs: memory in use, cannot free\n");
493 return -EBUSY;
496 ret = __vb2_queue_free(q);
497 if (ret != 0)
498 return ret;
502 * Make sure the requested values and current defaults are sane.
504 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
505 memset(plane_sizes, 0, sizeof(plane_sizes));
506 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
509 * Ask the driver how many buffers and planes per buffer it requires.
510 * Driver also sets the size and allocator context for each plane.
512 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
513 plane_sizes, q->alloc_ctx);
514 if (ret)
515 return ret;
517 /* Finally, allocate buffers and video memory */
518 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes,
519 plane_sizes);
520 if (ret < 0) {
521 dprintk(1, "Memory allocation failed with error: %d\n", ret);
522 return ret;
526 * Check if driver can handle the allocated number of buffers.
528 if (ret < num_buffers) {
529 unsigned int orig_num_buffers;
531 orig_num_buffers = num_buffers = ret;
532 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
533 plane_sizes, q->alloc_ctx);
534 if (ret)
535 goto free_mem;
537 if (orig_num_buffers < num_buffers) {
538 ret = -ENOMEM;
539 goto free_mem;
543 * Ok, driver accepted smaller number of buffers.
545 ret = num_buffers;
548 q->memory = req->memory;
551 * Return the number of successfully allocated buffers
552 * to the userspace.
554 req->count = ret;
556 return 0;
558 free_mem:
559 __vb2_queue_free(q);
560 return ret;
562 EXPORT_SYMBOL_GPL(vb2_reqbufs);
565 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
566 * @vb: vb2_buffer to which the plane in question belongs to
567 * @plane_no: plane number for which the address is to be returned
569 * This function returns a kernel virtual address of a given plane if
570 * such a mapping exist, NULL otherwise.
572 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
574 struct vb2_queue *q = vb->vb2_queue;
576 if (plane_no > vb->num_planes)
577 return NULL;
579 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
582 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
585 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
586 * @vb: vb2_buffer to which the plane in question belongs to
587 * @plane_no: plane number for which the cookie is to be returned
589 * This function returns an allocator specific cookie for a given plane if
590 * available, NULL otherwise. The allocator should provide some simple static
591 * inline function, which would convert this cookie to the allocator specific
592 * type that can be used directly by the driver to access the buffer. This can
593 * be for example physical address, pointer to scatter list or IOMMU mapping.
595 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
597 struct vb2_queue *q = vb->vb2_queue;
599 if (plane_no > vb->num_planes)
600 return NULL;
602 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
604 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
607 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
608 * @vb: vb2_buffer returned from the driver
609 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
610 * or VB2_BUF_STATE_ERROR if the operation finished with an error
612 * This function should be called by the driver after a hardware operation on
613 * a buffer is finished and the buffer may be returned to userspace. The driver
614 * cannot use this buffer anymore until it is queued back to it by videobuf
615 * by the means of buf_queue callback. Only buffers previously queued to the
616 * driver by buf_queue can be passed to this function.
618 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
620 struct vb2_queue *q = vb->vb2_queue;
621 unsigned long flags;
623 if (vb->state != VB2_BUF_STATE_ACTIVE)
624 return;
626 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
627 return;
629 dprintk(4, "Done processing on buffer %d, state: %d\n",
630 vb->v4l2_buf.index, vb->state);
632 /* Add the buffer to the done buffers list */
633 spin_lock_irqsave(&q->done_lock, flags);
634 vb->state = state;
635 list_add_tail(&vb->done_entry, &q->done_list);
636 atomic_dec(&q->queued_count);
637 spin_unlock_irqrestore(&q->done_lock, flags);
639 /* Inform any processes that may be waiting for buffers */
640 wake_up(&q->done_wq);
642 EXPORT_SYMBOL_GPL(vb2_buffer_done);
645 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
646 * a v4l2_buffer by the userspace
648 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
649 struct v4l2_plane *v4l2_planes)
651 unsigned int plane;
652 int ret;
654 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
656 * Verify that the userspace gave us a valid array for
657 * plane information.
659 ret = __verify_planes_array(vb, b);
660 if (ret)
661 return ret;
663 /* Fill in driver-provided information for OUTPUT types */
664 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
666 * Will have to go up to b->length when API starts
667 * accepting variable number of planes.
669 for (plane = 0; plane < vb->num_planes; ++plane) {
670 v4l2_planes[plane].bytesused =
671 b->m.planes[plane].bytesused;
672 v4l2_planes[plane].data_offset =
673 b->m.planes[plane].data_offset;
677 if (b->memory == V4L2_MEMORY_USERPTR) {
678 for (plane = 0; plane < vb->num_planes; ++plane) {
679 v4l2_planes[plane].m.userptr =
680 b->m.planes[plane].m.userptr;
681 v4l2_planes[plane].length =
682 b->m.planes[plane].length;
685 } else {
687 * Single-planar buffers do not use planes array,
688 * so fill in relevant v4l2_buffer struct fields instead.
689 * In videobuf we use our internal V4l2_planes struct for
690 * single-planar buffers as well, for simplicity.
692 if (V4L2_TYPE_IS_OUTPUT(b->type))
693 v4l2_planes[0].bytesused = b->bytesused;
695 if (b->memory == V4L2_MEMORY_USERPTR) {
696 v4l2_planes[0].m.userptr = b->m.userptr;
697 v4l2_planes[0].length = b->length;
701 vb->v4l2_buf.field = b->field;
702 vb->v4l2_buf.timestamp = b->timestamp;
704 return 0;
708 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
710 static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
712 struct v4l2_plane planes[VIDEO_MAX_PLANES];
713 struct vb2_queue *q = vb->vb2_queue;
714 void *mem_priv;
715 unsigned int plane;
716 int ret;
717 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
719 /* Verify and copy relevant information provided by the userspace */
720 ret = __fill_vb2_buffer(vb, b, planes);
721 if (ret)
722 return ret;
724 for (plane = 0; plane < vb->num_planes; ++plane) {
725 /* Skip the plane if already verified */
726 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
727 && vb->v4l2_planes[plane].length == planes[plane].length)
728 continue;
730 dprintk(3, "qbuf: userspace address for plane %d changed, "
731 "reacquiring memory\n", plane);
733 /* Release previously acquired memory if present */
734 if (vb->planes[plane].mem_priv)
735 call_memop(q, plane, put_userptr,
736 vb->planes[plane].mem_priv);
738 vb->planes[plane].mem_priv = NULL;
740 /* Acquire each plane's memory */
741 if (q->mem_ops->get_userptr) {
742 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
743 planes[plane].m.userptr,
744 planes[plane].length,
745 write);
746 if (IS_ERR(mem_priv)) {
747 dprintk(1, "qbuf: failed acquiring userspace "
748 "memory for plane %d\n", plane);
749 ret = PTR_ERR(mem_priv);
750 goto err;
752 vb->planes[plane].mem_priv = mem_priv;
757 * Call driver-specific initialization on the newly acquired buffer,
758 * if provided.
760 ret = call_qop(q, buf_init, vb);
761 if (ret) {
762 dprintk(1, "qbuf: buffer initialization failed\n");
763 goto err;
767 * Now that everything is in order, copy relevant information
768 * provided by userspace.
770 for (plane = 0; plane < vb->num_planes; ++plane)
771 vb->v4l2_planes[plane] = planes[plane];
773 return 0;
774 err:
775 /* In case of errors, release planes that were already acquired */
776 for (; plane > 0; --plane) {
777 call_memop(q, plane, put_userptr,
778 vb->planes[plane - 1].mem_priv);
779 vb->planes[plane - 1].mem_priv = NULL;
782 return ret;
786 * __qbuf_mmap() - handle qbuf of an MMAP buffer
788 static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
790 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
794 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
796 static void __enqueue_in_driver(struct vb2_buffer *vb)
798 struct vb2_queue *q = vb->vb2_queue;
800 vb->state = VB2_BUF_STATE_ACTIVE;
801 atomic_inc(&q->queued_count);
802 q->ops->buf_queue(vb);
806 * vb2_qbuf() - Queue a buffer from userspace
807 * @q: videobuf2 queue
808 * @b: buffer structure passed from userspace to vidioc_qbuf handler
809 * in driver
811 * Should be called from vidioc_qbuf ioctl handler of a driver.
812 * This function:
813 * 1) verifies the passed buffer,
814 * 2) calls buf_prepare callback in the driver (if provided), in which
815 * driver-specific buffer initialization can be performed,
816 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
817 * callback for processing.
819 * The return values from this function are intended to be directly returned
820 * from vidioc_qbuf handler in driver.
822 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
824 struct vb2_buffer *vb;
825 int ret = 0;
827 if (b->type != q->type) {
828 dprintk(1, "qbuf: invalid buffer type\n");
829 return -EINVAL;
832 if (b->index >= q->num_buffers) {
833 dprintk(1, "qbuf: buffer index out of range\n");
834 return -EINVAL;
837 vb = q->bufs[b->index];
838 if (NULL == vb) {
839 /* Should never happen */
840 dprintk(1, "qbuf: buffer is NULL\n");
841 return -EINVAL;
844 if (b->memory != q->memory) {
845 dprintk(1, "qbuf: invalid memory type\n");
846 return -EINVAL;
849 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
850 dprintk(1, "qbuf: buffer already in use\n");
851 return -EINVAL;
854 if (q->memory == V4L2_MEMORY_MMAP)
855 ret = __qbuf_mmap(vb, b);
856 else if (q->memory == V4L2_MEMORY_USERPTR)
857 ret = __qbuf_userptr(vb, b);
858 else {
859 WARN(1, "Invalid queue type\n");
860 return -EINVAL;
863 if (ret)
864 return ret;
866 ret = call_qop(q, buf_prepare, vb);
867 if (ret) {
868 dprintk(1, "qbuf: buffer preparation failed\n");
869 return ret;
873 * Add to the queued buffers list, a buffer will stay on it until
874 * dequeued in dqbuf.
876 list_add_tail(&vb->queued_entry, &q->queued_list);
877 vb->state = VB2_BUF_STATE_QUEUED;
880 * If already streaming, give the buffer to driver for processing.
881 * If not, the buffer will be given to driver on next streamon.
883 if (q->streaming)
884 __enqueue_in_driver(vb);
886 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
887 return 0;
889 EXPORT_SYMBOL_GPL(vb2_qbuf);
892 * __vb2_wait_for_done_vb() - wait for a buffer to become available
893 * for dequeuing
895 * Will sleep if required for nonblocking == false.
897 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
900 * All operations on vb_done_list are performed under done_lock
901 * spinlock protection. However, buffers may be removed from
902 * it and returned to userspace only while holding both driver's
903 * lock and the done_lock spinlock. Thus we can be sure that as
904 * long as we hold the driver's lock, the list will remain not
905 * empty if list_empty() check succeeds.
908 for (;;) {
909 int ret;
911 if (!q->streaming) {
912 dprintk(1, "Streaming off, will not wait for buffers\n");
913 return -EINVAL;
916 if (!list_empty(&q->done_list)) {
918 * Found a buffer that we were waiting for.
920 break;
923 if (nonblocking) {
924 dprintk(1, "Nonblocking and no buffers to dequeue, "
925 "will not wait\n");
926 return -EAGAIN;
930 * We are streaming and blocking, wait for another buffer to
931 * become ready or for streamoff. Driver's lock is released to
932 * allow streamoff or qbuf to be called while waiting.
934 call_qop(q, wait_prepare, q);
937 * All locks have been released, it is safe to sleep now.
939 dprintk(3, "Will sleep waiting for buffers\n");
940 ret = wait_event_interruptible(q->done_wq,
941 !list_empty(&q->done_list) || !q->streaming);
944 * We need to reevaluate both conditions again after reacquiring
945 * the locks or return an error if one occurred.
947 call_qop(q, wait_finish, q);
948 if (ret)
949 return ret;
951 return 0;
955 * __vb2_get_done_vb() - get a buffer ready for dequeuing
957 * Will sleep if required for nonblocking == false.
959 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
960 int nonblocking)
962 unsigned long flags;
963 int ret;
966 * Wait for at least one buffer to become available on the done_list.
968 ret = __vb2_wait_for_done_vb(q, nonblocking);
969 if (ret)
970 return ret;
973 * Driver's lock has been held since we last verified that done_list
974 * is not empty, so no need for another list_empty(done_list) check.
976 spin_lock_irqsave(&q->done_lock, flags);
977 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
978 list_del(&(*vb)->done_entry);
979 spin_unlock_irqrestore(&q->done_lock, flags);
981 return 0;
985 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
986 * @q: videobuf2 queue
988 * This function will wait until all buffers that have been given to the driver
989 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
990 * wait_prepare, wait_finish pair. It is intended to be called with all locks
991 * taken, for example from stop_streaming() callback.
993 int vb2_wait_for_all_buffers(struct vb2_queue *q)
995 if (!q->streaming) {
996 dprintk(1, "Streaming off, will not wait for buffers\n");
997 return -EINVAL;
1000 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1001 return 0;
1003 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1006 * vb2_dqbuf() - Dequeue a buffer to the userspace
1007 * @q: videobuf2 queue
1008 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1009 * in driver
1010 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1011 * buffers ready for dequeuing are present. Normally the driver
1012 * would be passing (file->f_flags & O_NONBLOCK) here
1014 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1015 * This function:
1016 * 1) verifies the passed buffer,
1017 * 2) calls buf_finish callback in the driver (if provided), in which
1018 * driver can perform any additional operations that may be required before
1019 * returning the buffer to userspace, such as cache sync,
1020 * 3) the buffer struct members are filled with relevant information for
1021 * the userspace.
1023 * The return values from this function are intended to be directly returned
1024 * from vidioc_dqbuf handler in driver.
1026 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1028 struct vb2_buffer *vb = NULL;
1029 int ret;
1031 if (b->type != q->type) {
1032 dprintk(1, "dqbuf: invalid buffer type\n");
1033 return -EINVAL;
1036 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1037 if (ret < 0) {
1038 dprintk(1, "dqbuf: error getting next done buffer\n");
1039 return ret;
1042 ret = call_qop(q, buf_finish, vb);
1043 if (ret) {
1044 dprintk(1, "dqbuf: buffer finish failed\n");
1045 return ret;
1048 switch (vb->state) {
1049 case VB2_BUF_STATE_DONE:
1050 dprintk(3, "dqbuf: Returning done buffer\n");
1051 break;
1052 case VB2_BUF_STATE_ERROR:
1053 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1054 break;
1055 default:
1056 dprintk(1, "dqbuf: Invalid buffer state\n");
1057 return -EINVAL;
1060 /* Fill buffer information for the userspace */
1061 __fill_v4l2_buffer(vb, b);
1062 /* Remove from videobuf queue */
1063 list_del(&vb->queued_entry);
1065 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1066 vb->v4l2_buf.index, vb->state);
1068 vb->state = VB2_BUF_STATE_DEQUEUED;
1069 return 0;
1071 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1074 * vb2_streamon - start streaming
1075 * @q: videobuf2 queue
1076 * @type: type argument passed from userspace to vidioc_streamon handler
1078 * Should be called from vidioc_streamon handler of a driver.
1079 * This function:
1080 * 1) verifies current state
1081 * 2) starts streaming and passes any previously queued buffers to the driver
1083 * The return values from this function are intended to be directly returned
1084 * from vidioc_streamon handler in the driver.
1086 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1088 struct vb2_buffer *vb;
1090 if (type != q->type) {
1091 dprintk(1, "streamon: invalid stream type\n");
1092 return -EINVAL;
1095 if (q->streaming) {
1096 dprintk(1, "streamon: already streaming\n");
1097 return -EBUSY;
1101 * Cannot start streaming on an OUTPUT device if no buffers have
1102 * been queued yet.
1104 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1105 if (list_empty(&q->queued_list)) {
1106 dprintk(1, "streamon: no output buffers queued\n");
1107 return -EINVAL;
1111 q->streaming = 1;
1114 * Let driver notice that streaming state has been enabled.
1116 call_qop(q, start_streaming, q);
1119 * If any buffers were queued before streamon,
1120 * we can now pass them to driver for processing.
1122 list_for_each_entry(vb, &q->queued_list, queued_entry)
1123 __enqueue_in_driver(vb);
1125 dprintk(3, "Streamon successful\n");
1126 return 0;
1128 EXPORT_SYMBOL_GPL(vb2_streamon);
1131 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1133 * Removes all queued buffers from driver's queue and all buffers queued by
1134 * userspace from videobuf's queue. Returns to state after reqbufs.
1136 static void __vb2_queue_cancel(struct vb2_queue *q)
1138 unsigned int i;
1141 * Tell driver to stop all transactions and release all queued
1142 * buffers.
1144 if (q->streaming)
1145 call_qop(q, stop_streaming, q);
1146 q->streaming = 0;
1149 * Remove all buffers from videobuf's list...
1151 INIT_LIST_HEAD(&q->queued_list);
1153 * ...and done list; userspace will not receive any buffers it
1154 * has not already dequeued before initiating cancel.
1156 INIT_LIST_HEAD(&q->done_list);
1157 wake_up_all(&q->done_wq);
1160 * Reinitialize all buffers for next use.
1162 for (i = 0; i < q->num_buffers; ++i)
1163 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1167 * vb2_streamoff - stop streaming
1168 * @q: videobuf2 queue
1169 * @type: type argument passed from userspace to vidioc_streamoff handler
1171 * Should be called from vidioc_streamoff handler of a driver.
1172 * This function:
1173 * 1) verifies current state,
1174 * 2) stop streaming and dequeues any queued buffers, including those previously
1175 * passed to the driver (after waiting for the driver to finish).
1177 * This call can be used for pausing playback.
1178 * The return values from this function are intended to be directly returned
1179 * from vidioc_streamoff handler in the driver
1181 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1183 if (type != q->type) {
1184 dprintk(1, "streamoff: invalid stream type\n");
1185 return -EINVAL;
1188 if (!q->streaming) {
1189 dprintk(1, "streamoff: not streaming\n");
1190 return -EINVAL;
1194 * Cancel will pause streaming and remove all buffers from the driver
1195 * and videobuf, effectively returning control over them to userspace.
1197 __vb2_queue_cancel(q);
1199 dprintk(3, "Streamoff successful\n");
1200 return 0;
1202 EXPORT_SYMBOL_GPL(vb2_streamoff);
1205 * __find_plane_by_offset() - find plane associated with the given offset off
1207 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1208 unsigned int *_buffer, unsigned int *_plane)
1210 struct vb2_buffer *vb;
1211 unsigned int buffer, plane;
1214 * Go over all buffers and their planes, comparing the given offset
1215 * with an offset assigned to each plane. If a match is found,
1216 * return its buffer and plane numbers.
1218 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1219 vb = q->bufs[buffer];
1221 for (plane = 0; plane < vb->num_planes; ++plane) {
1222 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1223 *_buffer = buffer;
1224 *_plane = plane;
1225 return 0;
1230 return -EINVAL;
1234 * vb2_mmap() - map video buffers into application address space
1235 * @q: videobuf2 queue
1236 * @vma: vma passed to the mmap file operation handler in the driver
1238 * Should be called from mmap file operation handler of a driver.
1239 * This function maps one plane of one of the available video buffers to
1240 * userspace. To map whole video memory allocated on reqbufs, this function
1241 * has to be called once per each plane per each buffer previously allocated.
1243 * When the userspace application calls mmap, it passes to it an offset returned
1244 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1245 * a "cookie", which is then used to identify the plane to be mapped.
1246 * This function finds a plane with a matching offset and a mapping is performed
1247 * by the means of a provided memory operation.
1249 * The return values from this function are intended to be directly returned
1250 * from the mmap handler in driver.
1252 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1254 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1255 struct vb2_plane *vb_plane;
1256 struct vb2_buffer *vb;
1257 unsigned int buffer, plane;
1258 int ret;
1260 if (q->memory != V4L2_MEMORY_MMAP) {
1261 dprintk(1, "Queue is not currently set up for mmap\n");
1262 return -EINVAL;
1266 * Check memory area access mode.
1268 if (!(vma->vm_flags & VM_SHARED)) {
1269 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1270 return -EINVAL;
1272 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1273 if (!(vma->vm_flags & VM_WRITE)) {
1274 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1275 return -EINVAL;
1277 } else {
1278 if (!(vma->vm_flags & VM_READ)) {
1279 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1280 return -EINVAL;
1285 * Find the plane corresponding to the offset passed by userspace.
1287 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1288 if (ret)
1289 return ret;
1291 vb = q->bufs[buffer];
1292 vb_plane = &vb->planes[plane];
1294 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1295 if (ret)
1296 return ret;
1298 vb_plane->mapped = 1;
1299 vb->num_planes_mapped++;
1301 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1302 return 0;
1304 EXPORT_SYMBOL_GPL(vb2_mmap);
1308 * vb2_poll() - implements poll userspace operation
1309 * @q: videobuf2 queue
1310 * @file: file argument passed to the poll file operation handler
1311 * @wait: wait argument passed to the poll file operation handler
1313 * This function implements poll file operation handler for a driver.
1314 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1315 * be informed that the file descriptor of a video device is available for
1316 * reading.
1317 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1318 * will be reported as available for writing.
1320 * The return values from this function are intended to be directly returned
1321 * from poll handler in driver.
1323 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1325 unsigned long flags;
1326 struct vb2_buffer *vb = NULL;
1329 * There is nothing to wait for if no buffers have already been queued.
1331 if (list_empty(&q->queued_list))
1332 return POLLERR;
1334 poll_wait(file, &q->done_wq, wait);
1337 * Take first buffer available for dequeuing.
1339 spin_lock_irqsave(&q->done_lock, flags);
1340 if (!list_empty(&q->done_list))
1341 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1342 done_entry);
1343 spin_unlock_irqrestore(&q->done_lock, flags);
1345 if (vb && (vb->state == VB2_BUF_STATE_DONE
1346 || vb->state == VB2_BUF_STATE_ERROR)) {
1347 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1348 POLLIN | POLLRDNORM;
1350 return 0;
1352 EXPORT_SYMBOL_GPL(vb2_poll);
1355 * vb2_queue_init() - initialize a videobuf2 queue
1356 * @q: videobuf2 queue; this structure should be allocated in driver
1358 * The vb2_queue structure should be allocated by the driver. The driver is
1359 * responsible of clearing it's content and setting initial values for some
1360 * required entries before calling this function.
1361 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1362 * to the struct vb2_queue description in include/media/videobuf2-core.h
1363 * for more information.
1365 int vb2_queue_init(struct vb2_queue *q)
1367 BUG_ON(!q);
1368 BUG_ON(!q->ops);
1369 BUG_ON(!q->mem_ops);
1370 BUG_ON(!q->type);
1371 BUG_ON(!q->io_modes);
1373 BUG_ON(!q->ops->queue_setup);
1374 BUG_ON(!q->ops->buf_queue);
1376 INIT_LIST_HEAD(&q->queued_list);
1377 INIT_LIST_HEAD(&q->done_list);
1378 spin_lock_init(&q->done_lock);
1379 init_waitqueue_head(&q->done_wq);
1381 if (q->buf_struct_size == 0)
1382 q->buf_struct_size = sizeof(struct vb2_buffer);
1384 return 0;
1386 EXPORT_SYMBOL_GPL(vb2_queue_init);
1389 * vb2_queue_release() - stop streaming, release the queue and free memory
1390 * @q: videobuf2 queue
1392 * This function stops streaming and performs necessary clean ups, including
1393 * freeing video buffer memory. The driver is responsible for freeing
1394 * the vb2_queue structure itself.
1396 void vb2_queue_release(struct vb2_queue *q)
1398 __vb2_queue_cancel(q);
1399 __vb2_queue_free(q);
1401 EXPORT_SYMBOL_GPL(vb2_queue_release);
1403 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1404 MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski");
1405 MODULE_LICENSE("GPL");