2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-core.h>
28 module_param(debug, int, 0644);
30 #define dprintk(level, fmt, arg...) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
36 #define call_memop(q, op, args...) \
37 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
40 #define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
43 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
44 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
45 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
51 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
53 struct vb2_queue *q = vb->vb2_queue;
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
61 for (plane = 0; plane < vb->num_planes; ++plane) {
62 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
64 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
66 if (IS_ERR_OR_NULL(mem_priv))
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
71 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
76 /* Free already allocated memory if one of the allocations failed */
77 for (; plane > 0; --plane) {
78 call_memop(q, put, vb->planes[plane - 1].mem_priv);
79 vb->planes[plane - 1].mem_priv = NULL;
86 * __vb2_buf_mem_free() - free memory of the given buffer
88 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
90 struct vb2_queue *q = vb->vb2_queue;
93 for (plane = 0; plane < vb->num_planes; ++plane) {
94 call_memop(q, put, vb->planes[plane].mem_priv);
95 vb->planes[plane].mem_priv = NULL;
96 dprintk(3, "Freed plane %d of buffer %d\n", plane,
102 * __vb2_buf_userptr_put() - release userspace memory associated with
105 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
107 struct vb2_queue *q = vb->vb2_queue;
110 for (plane = 0; plane < vb->num_planes; ++plane) {
111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
121 static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
138 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
140 struct vb2_queue *q = vb->vb2_queue;
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
148 * __setup_lengths() - setup initial lengths for every plane in
149 * every buffer on the queue
151 static void __setup_lengths(struct vb2_queue *q, unsigned int n)
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
156 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
157 vb = q->bufs[buffer];
161 for (plane = 0; plane < vb->num_planes; ++plane)
162 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
167 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
168 * every buffer on the queue
170 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
172 unsigned int buffer, plane;
173 struct vb2_buffer *vb;
176 if (q->num_buffers) {
177 struct v4l2_plane *p;
178 vb = q->bufs[q->num_buffers - 1];
179 p = &vb->v4l2_planes[vb->num_planes - 1];
180 off = PAGE_ALIGN(p->m.mem_offset + p->length);
185 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
186 vb = q->bufs[buffer];
190 for (plane = 0; plane < vb->num_planes; ++plane) {
191 vb->v4l2_planes[plane].m.mem_offset = off;
193 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
196 off += vb->v4l2_planes[plane].length;
197 off = PAGE_ALIGN(off);
203 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
204 * video buffer memory for all buffers/planes on the queue and initializes the
207 * Returns the number of buffers successfully allocated.
209 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
210 unsigned int num_buffers, unsigned int num_planes)
213 struct vb2_buffer *vb;
216 for (buffer = 0; buffer < num_buffers; ++buffer) {
217 /* Allocate videobuf buffer structures */
218 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
220 dprintk(1, "Memory alloc for buffer struct failed\n");
224 /* Length stores number of planes for multiplanar buffers */
225 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
226 vb->v4l2_buf.length = num_planes;
228 vb->state = VB2_BUF_STATE_DEQUEUED;
230 vb->num_planes = num_planes;
231 vb->v4l2_buf.index = q->num_buffers + buffer;
232 vb->v4l2_buf.type = q->type;
233 vb->v4l2_buf.memory = memory;
235 /* Allocate video buffer memory for the MMAP type */
236 if (memory == V4L2_MEMORY_MMAP) {
237 ret = __vb2_buf_mem_alloc(vb);
239 dprintk(1, "Failed allocating memory for "
240 "buffer %d\n", buffer);
245 * Call the driver-provided buffer initialization
246 * callback, if given. An error in initialization
247 * results in queue setup failure.
249 ret = call_qop(q, buf_init, vb);
251 dprintk(1, "Buffer %d %p initialization"
252 " failed\n", buffer, vb);
253 __vb2_buf_mem_free(vb);
259 q->bufs[q->num_buffers + buffer] = vb;
262 __setup_lengths(q, buffer);
263 if (memory == V4L2_MEMORY_MMAP)
264 __setup_offsets(q, buffer);
266 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
273 * __vb2_free_mem() - release all video buffer memory for a given queue
275 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
278 struct vb2_buffer *vb;
280 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
282 vb = q->bufs[buffer];
286 /* Free MMAP buffers or release USERPTR buffers */
287 if (q->memory == V4L2_MEMORY_MMAP)
288 __vb2_buf_mem_free(vb);
289 else if (q->memory == V4L2_MEMORY_DMABUF)
290 __vb2_buf_dmabuf_put(vb);
292 __vb2_buf_userptr_put(vb);
297 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
298 * related information, if no buffers are left return the queue to an
299 * uninitialized state. Might be called even if the queue has already been freed.
301 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
306 * Sanity check: when preparing a buffer the queue lock is released for
307 * a short while (see __buf_prepare for the details), which would allow
308 * a race with a reqbufs which can call this function. Removing the
309 * buffers from underneath __buf_prepare is obviously a bad idea, so we
310 * check if any of the buffers is in the state PREPARING, and if so we
311 * just return -EAGAIN.
313 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
315 if (q->bufs[buffer] == NULL)
317 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
318 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
323 /* Call driver-provided cleanup function for each buffer, if provided */
324 if (q->ops->buf_cleanup) {
325 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
327 if (NULL == q->bufs[buffer])
329 q->ops->buf_cleanup(q->bufs[buffer]);
333 /* Release video buffer memory */
334 __vb2_free_mem(q, buffers);
336 /* Free videobuf buffers */
337 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
339 kfree(q->bufs[buffer]);
340 q->bufs[buffer] = NULL;
343 q->num_buffers -= buffers;
346 INIT_LIST_HEAD(&q->queued_list);
351 * __verify_planes_array() - verify that the planes array passed in struct
352 * v4l2_buffer from userspace can be safely used
354 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
356 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
359 /* Is memory for copying plane information present? */
360 if (NULL == b->m.planes) {
361 dprintk(1, "Multi-planar buffer passed but "
362 "planes array not provided\n");
366 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
367 dprintk(1, "Incorrect planes array length, "
368 "expected %d, got %d\n", vb->num_planes, b->length);
376 * __verify_length() - Verify that the bytesused value for each plane fits in
377 * the plane length and that the data offset doesn't exceed the bytesused value.
379 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
384 if (!V4L2_TYPE_IS_OUTPUT(b->type))
387 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
388 for (plane = 0; plane < vb->num_planes; ++plane) {
389 length = (b->memory == V4L2_MEMORY_USERPTR)
390 ? b->m.planes[plane].length
391 : vb->v4l2_planes[plane].length;
393 if (b->m.planes[plane].bytesused > length)
396 if (b->m.planes[plane].data_offset > 0 &&
397 b->m.planes[plane].data_offset >=
398 b->m.planes[plane].bytesused)
402 length = (b->memory == V4L2_MEMORY_USERPTR)
403 ? b->length : vb->v4l2_planes[0].length;
405 if (b->bytesused > length)
413 * __buffer_in_use() - return true if the buffer is in use and
414 * the queue cannot be freed (by the means of REQBUFS(0)) call
416 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
419 for (plane = 0; plane < vb->num_planes; ++plane) {
420 void *mem_priv = vb->planes[plane].mem_priv;
422 * If num_users() has not been provided, call_memop
423 * will return 0, apparently nobody cares about this
424 * case anyway. If num_users() returns more than 1,
425 * we are not the only user of the plane's memory.
427 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
434 * __buffers_in_use() - return true if any buffers on the queue are in use and
435 * the queue cannot be freed (by the means of REQBUFS(0)) call
437 static bool __buffers_in_use(struct vb2_queue *q)
440 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
441 if (__buffer_in_use(q, q->bufs[buffer]))
448 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
449 * returned to userspace
451 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
453 struct vb2_queue *q = vb->vb2_queue;
455 /* Copy back data such as timestamp, flags, etc. */
456 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
457 b->reserved2 = vb->v4l2_buf.reserved2;
458 b->reserved = vb->v4l2_buf.reserved;
460 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
462 * Fill in plane-related data if userspace provided an array
463 * for it. The caller has already verified memory and size.
465 b->length = vb->num_planes;
466 memcpy(b->m.planes, vb->v4l2_planes,
467 b->length * sizeof(struct v4l2_plane));
470 * We use length and offset in v4l2_planes array even for
471 * single-planar buffers, but userspace does not.
473 b->length = vb->v4l2_planes[0].length;
474 b->bytesused = vb->v4l2_planes[0].bytesused;
475 if (q->memory == V4L2_MEMORY_MMAP)
476 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
477 else if (q->memory == V4L2_MEMORY_USERPTR)
478 b->m.userptr = vb->v4l2_planes[0].m.userptr;
479 else if (q->memory == V4L2_MEMORY_DMABUF)
480 b->m.fd = vb->v4l2_planes[0].m.fd;
484 * Clear any buffer state related flags.
486 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
487 b->flags |= q->timestamp_type;
490 case VB2_BUF_STATE_QUEUED:
491 case VB2_BUF_STATE_ACTIVE:
492 b->flags |= V4L2_BUF_FLAG_QUEUED;
494 case VB2_BUF_STATE_ERROR:
495 b->flags |= V4L2_BUF_FLAG_ERROR;
497 case VB2_BUF_STATE_DONE:
498 b->flags |= V4L2_BUF_FLAG_DONE;
500 case VB2_BUF_STATE_PREPARED:
501 b->flags |= V4L2_BUF_FLAG_PREPARED;
503 case VB2_BUF_STATE_PREPARING:
504 case VB2_BUF_STATE_DEQUEUED:
509 if (__buffer_in_use(q, vb))
510 b->flags |= V4L2_BUF_FLAG_MAPPED;
514 * vb2_querybuf() - query video buffer information
516 * @b: buffer struct passed from userspace to vidioc_querybuf handler
519 * Should be called from vidioc_querybuf ioctl handler in driver.
520 * This function will verify the passed v4l2_buffer structure and fill the
521 * relevant information for the userspace.
523 * The return values from this function are intended to be directly returned
524 * from vidioc_querybuf handler in driver.
526 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
528 struct vb2_buffer *vb;
531 if (b->type != q->type) {
532 dprintk(1, "querybuf: wrong buffer type\n");
536 if (b->index >= q->num_buffers) {
537 dprintk(1, "querybuf: buffer index out of range\n");
540 vb = q->bufs[b->index];
541 ret = __verify_planes_array(vb, b);
543 __fill_v4l2_buffer(vb, b);
546 EXPORT_SYMBOL(vb2_querybuf);
549 * __verify_userptr_ops() - verify that all memory operations required for
550 * USERPTR queue type have been provided
552 static int __verify_userptr_ops(struct vb2_queue *q)
554 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
555 !q->mem_ops->put_userptr)
562 * __verify_mmap_ops() - verify that all memory operations required for
563 * MMAP queue type have been provided
565 static int __verify_mmap_ops(struct vb2_queue *q)
567 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
568 !q->mem_ops->put || !q->mem_ops->mmap)
575 * __verify_dmabuf_ops() - verify that all memory operations required for
576 * DMABUF queue type have been provided
578 static int __verify_dmabuf_ops(struct vb2_queue *q)
580 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
581 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
582 !q->mem_ops->unmap_dmabuf)
589 * __verify_memory_type() - Check whether the memory type and buffer type
590 * passed to a buffer operation are compatible with the queue.
592 static int __verify_memory_type(struct vb2_queue *q,
593 enum v4l2_memory memory, enum v4l2_buf_type type)
595 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
596 memory != V4L2_MEMORY_DMABUF) {
597 dprintk(1, "reqbufs: unsupported memory type\n");
601 if (type != q->type) {
602 dprintk(1, "reqbufs: requested type is incorrect\n");
607 * Make sure all the required memory ops for given memory type
610 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
611 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
615 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
616 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
620 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
621 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
626 * Place the busy tests at the end: -EBUSY can be ignored when
627 * create_bufs is called with count == 0, but count == 0 should still
628 * do the memory and type validation.
631 dprintk(1, "reqbufs: file io in progress\n");
638 * __reqbufs() - Initiate streaming
639 * @q: videobuf2 queue
640 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
642 * Should be called from vidioc_reqbufs ioctl handler of a driver.
644 * 1) verifies streaming parameters passed from the userspace,
645 * 2) sets up the queue,
646 * 3) negotiates number of buffers and planes per buffer with the driver
647 * to be used during streaming,
648 * 4) allocates internal buffer structures (struct vb2_buffer), according to
649 * the agreed parameters,
650 * 5) for MMAP memory type, allocates actual video memory, using the
651 * memory handling/allocation routines provided during queue initialization
653 * If req->count is 0, all the memory will be freed instead.
654 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
655 * and the queue is not busy, memory will be reallocated.
657 * The return values from this function are intended to be directly returned
658 * from vidioc_reqbufs handler in driver.
660 static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
662 unsigned int num_buffers, allocated_buffers, num_planes = 0;
666 dprintk(1, "reqbufs: streaming active\n");
670 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
672 * We already have buffers allocated, so first check if they
673 * are not in use and can be freed.
675 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
676 dprintk(1, "reqbufs: memory in use, cannot free\n");
680 ret = __vb2_queue_free(q, q->num_buffers);
685 * In case of REQBUFS(0) return immediately without calling
686 * driver's queue_setup() callback and allocating resources.
693 * Make sure the requested values and current defaults are sane.
695 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
696 num_buffers = max_t(unsigned int, req->count, q->min_buffers_needed);
697 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
698 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
699 q->memory = req->memory;
702 * Ask the driver how many buffers and planes per buffer it requires.
703 * Driver also sets the size and allocator context for each plane.
705 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
706 q->plane_sizes, q->alloc_ctx);
710 /* Finally, allocate buffers and video memory */
711 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
713 dprintk(1, "Memory allocation failed\n");
717 allocated_buffers = ret;
720 * There is no point in continuing if we can't allocate the minimum
721 * number of buffers needed by this vb2_queue.
723 if (allocated_buffers < q->min_buffers_needed)
727 * Check if driver can handle the allocated number of buffers.
729 if (!ret && allocated_buffers < num_buffers) {
730 num_buffers = allocated_buffers;
732 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
733 &num_planes, q->plane_sizes, q->alloc_ctx);
735 if (!ret && allocated_buffers < num_buffers)
739 * Either the driver has accepted a smaller number of buffers,
740 * or .queue_setup() returned an error
744 q->num_buffers = allocated_buffers;
747 __vb2_queue_free(q, allocated_buffers);
752 * Return the number of successfully allocated buffers
755 req->count = allocated_buffers;
756 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
762 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
764 * @q: videobuf2 queue
765 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
767 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
769 int ret = __verify_memory_type(q, req->memory, req->type);
771 return ret ? ret : __reqbufs(q, req);
773 EXPORT_SYMBOL_GPL(vb2_reqbufs);
776 * __create_bufs() - Allocate buffers and any required auxiliary structs
777 * @q: videobuf2 queue
778 * @create: creation parameters, passed from userspace to vidioc_create_bufs
781 * Should be called from vidioc_create_bufs ioctl handler of a driver.
783 * 1) verifies parameter sanity
784 * 2) calls the .queue_setup() queue operation
785 * 3) performs any necessary memory allocations
787 * The return values from this function are intended to be directly returned
788 * from vidioc_create_bufs handler in driver.
790 static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
792 unsigned int num_planes = 0, num_buffers, allocated_buffers;
795 if (q->num_buffers == VIDEO_MAX_FRAME) {
796 dprintk(1, "%s(): maximum number of buffers already allocated\n",
801 if (!q->num_buffers) {
802 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
803 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
804 q->memory = create->memory;
805 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
808 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
811 * Ask the driver, whether the requested number of buffers, planes per
812 * buffer and their sizes are acceptable
814 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
815 &num_planes, q->plane_sizes, q->alloc_ctx);
819 /* Finally, allocate buffers and video memory */
820 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
823 dprintk(1, "Memory allocation failed\n");
827 allocated_buffers = ret;
830 * Check if driver can handle the so far allocated number of buffers.
832 if (ret < num_buffers) {
836 * q->num_buffers contains the total number of buffers, that the
837 * queue driver has set up
839 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
840 &num_planes, q->plane_sizes, q->alloc_ctx);
842 if (!ret && allocated_buffers < num_buffers)
846 * Either the driver has accepted a smaller number of buffers,
847 * or .queue_setup() returned an error
851 q->num_buffers += allocated_buffers;
854 __vb2_queue_free(q, allocated_buffers);
859 * Return the number of successfully allocated buffers
862 create->count = allocated_buffers;
868 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
869 * memory and type values.
870 * @q: videobuf2 queue
871 * @create: creation parameters, passed from userspace to vidioc_create_bufs
874 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
876 int ret = __verify_memory_type(q, create->memory, create->format.type);
878 create->index = q->num_buffers;
879 if (create->count == 0)
880 return ret != -EBUSY ? ret : 0;
881 return ret ? ret : __create_bufs(q, create);
883 EXPORT_SYMBOL_GPL(vb2_create_bufs);
886 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
887 * @vb: vb2_buffer to which the plane in question belongs to
888 * @plane_no: plane number for which the address is to be returned
890 * This function returns a kernel virtual address of a given plane if
891 * such a mapping exist, NULL otherwise.
893 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
895 struct vb2_queue *q = vb->vb2_queue;
897 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
900 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
903 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
906 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
907 * @vb: vb2_buffer to which the plane in question belongs to
908 * @plane_no: plane number for which the cookie is to be returned
910 * This function returns an allocator specific cookie for a given plane if
911 * available, NULL otherwise. The allocator should provide some simple static
912 * inline function, which would convert this cookie to the allocator specific
913 * type that can be used directly by the driver to access the buffer. This can
914 * be for example physical address, pointer to scatter list or IOMMU mapping.
916 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
918 struct vb2_queue *q = vb->vb2_queue;
920 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
923 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
925 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
928 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
929 * @vb: vb2_buffer returned from the driver
930 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
931 * or VB2_BUF_STATE_ERROR if the operation finished with an error.
932 * If start_streaming fails then it should return buffers with state
933 * VB2_BUF_STATE_QUEUED to put them back into the queue.
935 * This function should be called by the driver after a hardware operation on
936 * a buffer is finished and the buffer may be returned to userspace. The driver
937 * cannot use this buffer anymore until it is queued back to it by videobuf
938 * by the means of buf_queue callback. Only buffers previously queued to the
939 * driver by buf_queue can be passed to this function.
941 * While streaming a buffer can only be returned in state DONE or ERROR.
942 * The start_streaming op can also return them in case the DMA engine cannot
943 * be started for some reason. In that case the buffers should be returned with
946 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
948 struct vb2_queue *q = vb->vb2_queue;
952 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
955 if (!q->start_streaming_called) {
956 if (WARN_ON(state != VB2_BUF_STATE_QUEUED))
957 state = VB2_BUF_STATE_QUEUED;
958 } else if (!WARN_ON(!q->start_streaming_called)) {
959 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
960 state != VB2_BUF_STATE_ERROR))
961 state = VB2_BUF_STATE_ERROR;
964 dprintk(4, "Done processing on buffer %d, state: %d\n",
965 vb->v4l2_buf.index, state);
968 for (plane = 0; plane < vb->num_planes; ++plane)
969 call_memop(q, finish, vb->planes[plane].mem_priv);
971 /* Add the buffer to the done buffers list */
972 spin_lock_irqsave(&q->done_lock, flags);
974 if (state != VB2_BUF_STATE_QUEUED)
975 list_add_tail(&vb->done_entry, &q->done_list);
976 atomic_dec(&q->owned_by_drv_count);
977 spin_unlock_irqrestore(&q->done_lock, flags);
979 if (state == VB2_BUF_STATE_QUEUED)
982 /* Inform any processes that may be waiting for buffers */
983 wake_up(&q->done_wq);
985 EXPORT_SYMBOL_GPL(vb2_buffer_done);
988 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
989 * v4l2_buffer by the userspace. The caller has already verified that struct
990 * v4l2_buffer has a valid number of planes.
992 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
993 struct v4l2_plane *v4l2_planes)
997 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
998 /* Fill in driver-provided information for OUTPUT types */
999 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1001 * Will have to go up to b->length when API starts
1002 * accepting variable number of planes.
1004 for (plane = 0; plane < vb->num_planes; ++plane) {
1005 v4l2_planes[plane].bytesused =
1006 b->m.planes[plane].bytesused;
1007 v4l2_planes[plane].data_offset =
1008 b->m.planes[plane].data_offset;
1012 if (b->memory == V4L2_MEMORY_USERPTR) {
1013 for (plane = 0; plane < vb->num_planes; ++plane) {
1014 v4l2_planes[plane].m.userptr =
1015 b->m.planes[plane].m.userptr;
1016 v4l2_planes[plane].length =
1017 b->m.planes[plane].length;
1020 if (b->memory == V4L2_MEMORY_DMABUF) {
1021 for (plane = 0; plane < vb->num_planes; ++plane) {
1022 v4l2_planes[plane].m.fd =
1023 b->m.planes[plane].m.fd;
1024 v4l2_planes[plane].length =
1025 b->m.planes[plane].length;
1026 v4l2_planes[plane].data_offset =
1027 b->m.planes[plane].data_offset;
1032 * Single-planar buffers do not use planes array,
1033 * so fill in relevant v4l2_buffer struct fields instead.
1034 * In videobuf we use our internal V4l2_planes struct for
1035 * single-planar buffers as well, for simplicity.
1037 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1038 v4l2_planes[0].bytesused = b->bytesused;
1039 v4l2_planes[0].data_offset = 0;
1042 if (b->memory == V4L2_MEMORY_USERPTR) {
1043 v4l2_planes[0].m.userptr = b->m.userptr;
1044 v4l2_planes[0].length = b->length;
1047 if (b->memory == V4L2_MEMORY_DMABUF) {
1048 v4l2_planes[0].m.fd = b->m.fd;
1049 v4l2_planes[0].length = b->length;
1050 v4l2_planes[0].data_offset = 0;
1055 vb->v4l2_buf.field = b->field;
1056 vb->v4l2_buf.timestamp = b->timestamp;
1057 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
1061 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1063 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1065 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1066 struct vb2_queue *q = vb->vb2_queue;
1070 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1072 /* Copy relevant information provided by the userspace */
1073 __fill_vb2_buffer(vb, b, planes);
1075 for (plane = 0; plane < vb->num_planes; ++plane) {
1076 /* Skip the plane if already verified */
1077 if (vb->v4l2_planes[plane].m.userptr &&
1078 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
1079 && vb->v4l2_planes[plane].length == planes[plane].length)
1082 dprintk(3, "qbuf: userspace address for plane %d changed, "
1083 "reacquiring memory\n", plane);
1085 /* Check if the provided plane buffer is large enough */
1086 if (planes[plane].length < q->plane_sizes[plane]) {
1087 dprintk(1, "qbuf: provided buffer size %u is less than "
1088 "setup size %u for plane %d\n",
1089 planes[plane].length,
1090 q->plane_sizes[plane], plane);
1095 /* Release previously acquired memory if present */
1096 if (vb->planes[plane].mem_priv)
1097 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1099 vb->planes[plane].mem_priv = NULL;
1100 vb->v4l2_planes[plane].m.userptr = 0;
1101 vb->v4l2_planes[plane].length = 0;
1103 /* Acquire each plane's memory */
1104 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1105 planes[plane].m.userptr,
1106 planes[plane].length, write);
1107 if (IS_ERR_OR_NULL(mem_priv)) {
1108 dprintk(1, "qbuf: failed acquiring userspace "
1109 "memory for plane %d\n", plane);
1110 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1113 vb->planes[plane].mem_priv = mem_priv;
1117 * Call driver-specific initialization on the newly acquired buffer,
1120 ret = call_qop(q, buf_init, vb);
1122 dprintk(1, "qbuf: buffer initialization failed\n");
1127 * Now that everything is in order, copy relevant information
1128 * provided by userspace.
1130 for (plane = 0; plane < vb->num_planes; ++plane)
1131 vb->v4l2_planes[plane] = planes[plane];
1135 /* In case of errors, release planes that were already acquired */
1136 for (plane = 0; plane < vb->num_planes; ++plane) {
1137 if (vb->planes[plane].mem_priv)
1138 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1139 vb->planes[plane].mem_priv = NULL;
1140 vb->v4l2_planes[plane].m.userptr = 0;
1141 vb->v4l2_planes[plane].length = 0;
1148 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1150 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1152 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1157 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1159 static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1161 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1162 struct vb2_queue *q = vb->vb2_queue;
1166 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1168 /* Copy relevant information provided by the userspace */
1169 __fill_vb2_buffer(vb, b, planes);
1171 for (plane = 0; plane < vb->num_planes; ++plane) {
1172 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1174 if (IS_ERR_OR_NULL(dbuf)) {
1175 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1181 /* use DMABUF size if length is not provided */
1182 if (planes[plane].length == 0)
1183 planes[plane].length = dbuf->size;
1185 if (planes[plane].length < planes[plane].data_offset +
1186 q->plane_sizes[plane]) {
1187 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1193 /* Skip the plane if already verified */
1194 if (dbuf == vb->planes[plane].dbuf &&
1195 vb->v4l2_planes[plane].length == planes[plane].length) {
1200 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1202 /* Release previously acquired memory if present */
1203 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1204 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1206 /* Acquire each plane's memory */
1207 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1208 dbuf, planes[plane].length, write);
1209 if (IS_ERR(mem_priv)) {
1210 dprintk(1, "qbuf: failed to attach dmabuf\n");
1211 ret = PTR_ERR(mem_priv);
1216 vb->planes[plane].dbuf = dbuf;
1217 vb->planes[plane].mem_priv = mem_priv;
1220 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1221 * really we want to do this just before the DMA, not while queueing
1224 for (plane = 0; plane < vb->num_planes; ++plane) {
1225 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1227 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1231 vb->planes[plane].dbuf_mapped = 1;
1235 * Call driver-specific initialization on the newly acquired buffer,
1238 ret = call_qop(q, buf_init, vb);
1240 dprintk(1, "qbuf: buffer initialization failed\n");
1245 * Now that everything is in order, copy relevant information
1246 * provided by userspace.
1248 for (plane = 0; plane < vb->num_planes; ++plane)
1249 vb->v4l2_planes[plane] = planes[plane];
1253 /* In case of errors, release planes that were already acquired */
1254 __vb2_buf_dmabuf_put(vb);
1260 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1262 static void __enqueue_in_driver(struct vb2_buffer *vb)
1264 struct vb2_queue *q = vb->vb2_queue;
1267 vb->state = VB2_BUF_STATE_ACTIVE;
1268 atomic_inc(&q->owned_by_drv_count);
1271 for (plane = 0; plane < vb->num_planes; ++plane)
1272 call_memop(q, prepare, vb->planes[plane].mem_priv);
1274 q->ops->buf_queue(vb);
1277 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1279 struct vb2_queue *q = vb->vb2_queue;
1280 struct rw_semaphore *mmap_sem;
1283 ret = __verify_length(vb, b);
1285 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1290 vb->state = VB2_BUF_STATE_PREPARING;
1291 switch (q->memory) {
1292 case V4L2_MEMORY_MMAP:
1293 ret = __qbuf_mmap(vb, b);
1295 case V4L2_MEMORY_USERPTR:
1297 * In case of user pointer buffers vb2 allocators need to get
1298 * direct access to userspace pages. This requires getting
1299 * the mmap semaphore for read access in the current process
1300 * structure. The same semaphore is taken before calling mmap
1301 * operation, while both qbuf/prepare_buf and mmap are called
1302 * by the driver or v4l2 core with the driver's lock held.
1303 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1304 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1305 * the videobuf2 core releases the driver's lock, takes
1306 * mmap_sem and then takes the driver's lock again.
1308 mmap_sem = ¤t->mm->mmap_sem;
1309 call_qop(q, wait_prepare, q);
1310 down_read(mmap_sem);
1311 call_qop(q, wait_finish, q);
1313 ret = __qbuf_userptr(vb, b);
1317 case V4L2_MEMORY_DMABUF:
1318 ret = __qbuf_dmabuf(vb, b);
1321 WARN(1, "Invalid queue type\n");
1326 ret = call_qop(q, buf_prepare, vb);
1328 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1329 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
1334 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1337 if (b->type != q->type) {
1338 dprintk(1, "%s(): invalid buffer type\n", opname);
1342 if (b->index >= q->num_buffers) {
1343 dprintk(1, "%s(): buffer index out of range\n", opname);
1347 if (q->bufs[b->index] == NULL) {
1348 /* Should never happen */
1349 dprintk(1, "%s(): buffer is NULL\n", opname);
1353 if (b->memory != q->memory) {
1354 dprintk(1, "%s(): invalid memory type\n", opname);
1358 return __verify_planes_array(q->bufs[b->index], b);
1362 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1363 * @q: videobuf2 queue
1364 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1367 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1369 * 1) verifies the passed buffer,
1370 * 2) calls buf_prepare callback in the driver (if provided), in which
1371 * driver-specific buffer initialization can be performed,
1373 * The return values from this function are intended to be directly returned
1374 * from vidioc_prepare_buf handler in driver.
1376 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1378 struct vb2_buffer *vb;
1382 dprintk(1, "%s(): file io in progress\n", __func__);
1386 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
1390 vb = q->bufs[b->index];
1391 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1392 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1397 ret = __buf_prepare(vb, b);
1399 /* Fill buffer information for the userspace */
1400 __fill_v4l2_buffer(vb, b);
1402 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1406 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1409 * vb2_start_streaming() - Attempt to start streaming.
1410 * @q: videobuf2 queue
1412 * Attempt to start streaming. When this function is called there must be
1413 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1414 * number of buffers required for the DMA engine to function). If the
1415 * @start_streaming op fails it is supposed to return all the driver-owned
1416 * buffers back to vb2 in state QUEUED. Check if that happened and if
1417 * not warn and reclaim them forcefully.
1419 static int vb2_start_streaming(struct vb2_queue *q)
1421 struct vb2_buffer *vb;
1425 * If any buffers were queued before streamon,
1426 * we can now pass them to driver for processing.
1428 list_for_each_entry(vb, &q->queued_list, queued_entry)
1429 __enqueue_in_driver(vb);
1431 /* Tell the driver to start streaming */
1432 ret = call_qop(q, start_streaming, q,
1433 atomic_read(&q->owned_by_drv_count));
1434 q->start_streaming_called = ret == 0;
1438 dprintk(1, "qbuf: driver refused to start streaming\n");
1439 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1443 * Forcefully reclaim buffers if the driver did not
1444 * correctly return them to vb2.
1446 for (i = 0; i < q->num_buffers; ++i) {
1448 if (vb->state == VB2_BUF_STATE_ACTIVE)
1449 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1451 /* Must be zero now */
1452 WARN_ON(atomic_read(&q->owned_by_drv_count));
1457 static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1459 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1460 struct vb2_buffer *vb;
1465 vb = q->bufs[b->index];
1466 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1467 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1472 switch (vb->state) {
1473 case VB2_BUF_STATE_DEQUEUED:
1474 ret = __buf_prepare(vb, b);
1478 case VB2_BUF_STATE_PREPARED:
1480 case VB2_BUF_STATE_PREPARING:
1481 dprintk(1, "qbuf: buffer still being prepared\n");
1484 dprintk(1, "qbuf: buffer already in use\n");
1489 * Add to the queued buffers list, a buffer will stay on it until
1490 * dequeued in dqbuf.
1492 list_add_tail(&vb->queued_entry, &q->queued_list);
1494 q->waiting_for_buffers = false;
1495 vb->state = VB2_BUF_STATE_QUEUED;
1498 * If already streaming, give the buffer to driver for processing.
1499 * If not, the buffer will be given to driver on next streamon.
1501 if (q->start_streaming_called)
1502 __enqueue_in_driver(vb);
1504 /* Fill buffer information for the userspace */
1505 __fill_v4l2_buffer(vb, b);
1508 * If streamon has been called, and we haven't yet called
1509 * start_streaming() since not enough buffers were queued, and
1510 * we now have reached the minimum number of queued buffers,
1511 * then we can finally call start_streaming().
1513 if (q->streaming && !q->start_streaming_called &&
1514 q->queued_count >= q->min_buffers_needed) {
1515 ret = vb2_start_streaming(q);
1520 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1525 * vb2_qbuf() - Queue a buffer from userspace
1526 * @q: videobuf2 queue
1527 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1530 * Should be called from vidioc_qbuf ioctl handler of a driver.
1532 * 1) verifies the passed buffer,
1533 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1534 * which driver-specific buffer initialization can be performed,
1535 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1536 * callback for processing.
1538 * The return values from this function are intended to be directly returned
1539 * from vidioc_qbuf handler in driver.
1541 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1544 dprintk(1, "%s(): file io in progress\n", __func__);
1548 return vb2_internal_qbuf(q, b);
1550 EXPORT_SYMBOL_GPL(vb2_qbuf);
1553 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1556 * Will sleep if required for nonblocking == false.
1558 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1561 * All operations on vb_done_list are performed under done_lock
1562 * spinlock protection. However, buffers may be removed from
1563 * it and returned to userspace only while holding both driver's
1564 * lock and the done_lock spinlock. Thus we can be sure that as
1565 * long as we hold the driver's lock, the list will remain not
1566 * empty if list_empty() check succeeds.
1572 if (!q->streaming) {
1573 dprintk(1, "Streaming off, will not wait for buffers\n");
1577 if (!list_empty(&q->done_list)) {
1579 * Found a buffer that we were waiting for.
1585 dprintk(1, "Nonblocking and no buffers to dequeue, "
1591 * We are streaming and blocking, wait for another buffer to
1592 * become ready or for streamoff. Driver's lock is released to
1593 * allow streamoff or qbuf to be called while waiting.
1595 call_qop(q, wait_prepare, q);
1598 * All locks have been released, it is safe to sleep now.
1600 dprintk(3, "Will sleep waiting for buffers\n");
1601 ret = wait_event_interruptible(q->done_wq,
1602 !list_empty(&q->done_list) || !q->streaming);
1605 * We need to reevaluate both conditions again after reacquiring
1606 * the locks or return an error if one occurred.
1608 call_qop(q, wait_finish, q);
1610 dprintk(1, "Sleep was interrupted\n");
1618 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1620 * Will sleep if required for nonblocking == false.
1622 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1623 struct v4l2_buffer *b, int nonblocking)
1625 unsigned long flags;
1629 * Wait for at least one buffer to become available on the done_list.
1631 ret = __vb2_wait_for_done_vb(q, nonblocking);
1636 * Driver's lock has been held since we last verified that done_list
1637 * is not empty, so no need for another list_empty(done_list) check.
1639 spin_lock_irqsave(&q->done_lock, flags);
1640 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1642 * Only remove the buffer from done_list if v4l2_buffer can handle all
1645 ret = __verify_planes_array(*vb, b);
1647 list_del(&(*vb)->done_entry);
1648 spin_unlock_irqrestore(&q->done_lock, flags);
1654 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1655 * @q: videobuf2 queue
1657 * This function will wait until all buffers that have been given to the driver
1658 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1659 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1660 * taken, for example from stop_streaming() callback.
1662 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1664 if (!q->streaming) {
1665 dprintk(1, "Streaming off, will not wait for buffers\n");
1669 if (q->start_streaming_called)
1670 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1673 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1676 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1678 static void __vb2_dqbuf(struct vb2_buffer *vb)
1680 struct vb2_queue *q = vb->vb2_queue;
1683 /* nothing to do if the buffer is already dequeued */
1684 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1687 vb->state = VB2_BUF_STATE_DEQUEUED;
1689 /* unmap DMABUF buffer */
1690 if (q->memory == V4L2_MEMORY_DMABUF)
1691 for (i = 0; i < vb->num_planes; ++i) {
1692 if (!vb->planes[i].dbuf_mapped)
1694 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1695 vb->planes[i].dbuf_mapped = 0;
1699 static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1701 struct vb2_buffer *vb = NULL;
1704 if (b->type != q->type) {
1705 dprintk(1, "dqbuf: invalid buffer type\n");
1708 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1712 ret = call_qop(q, buf_finish, vb);
1714 dprintk(1, "dqbuf: buffer finish failed\n");
1718 switch (vb->state) {
1719 case VB2_BUF_STATE_DONE:
1720 dprintk(3, "dqbuf: Returning done buffer\n");
1722 case VB2_BUF_STATE_ERROR:
1723 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1726 dprintk(1, "dqbuf: Invalid buffer state\n");
1730 /* Fill buffer information for the userspace */
1731 __fill_v4l2_buffer(vb, b);
1732 /* Remove from videobuf queue */
1733 list_del(&vb->queued_entry);
1735 /* go back to dequeued state */
1738 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1739 vb->v4l2_buf.index, vb->state);
1745 * vb2_dqbuf() - Dequeue a buffer to the userspace
1746 * @q: videobuf2 queue
1747 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1749 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1750 * buffers ready for dequeuing are present. Normally the driver
1751 * would be passing (file->f_flags & O_NONBLOCK) here
1753 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1755 * 1) verifies the passed buffer,
1756 * 2) calls buf_finish callback in the driver (if provided), in which
1757 * driver can perform any additional operations that may be required before
1758 * returning the buffer to userspace, such as cache sync,
1759 * 3) the buffer struct members are filled with relevant information for
1762 * The return values from this function are intended to be directly returned
1763 * from vidioc_dqbuf handler in driver.
1765 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1768 dprintk(1, "dqbuf: file io in progress\n");
1771 return vb2_internal_dqbuf(q, b, nonblocking);
1773 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1776 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1778 * Removes all queued buffers from driver's queue and all buffers queued by
1779 * userspace from videobuf's queue. Returns to state after reqbufs.
1781 static void __vb2_queue_cancel(struct vb2_queue *q)
1786 * Tell driver to stop all transactions and release all queued
1789 if (q->start_streaming_called)
1790 call_qop(q, stop_streaming, q);
1792 q->start_streaming_called = 0;
1793 q->queued_count = 0;
1795 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1796 for (i = 0; i < q->num_buffers; ++i)
1797 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
1798 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1799 /* Must be zero now */
1800 WARN_ON(atomic_read(&q->owned_by_drv_count));
1804 * Remove all buffers from videobuf's list...
1806 INIT_LIST_HEAD(&q->queued_list);
1808 * ...and done list; userspace will not receive any buffers it
1809 * has not already dequeued before initiating cancel.
1811 INIT_LIST_HEAD(&q->done_list);
1812 atomic_set(&q->owned_by_drv_count, 0);
1813 wake_up_all(&q->done_wq);
1816 * Reinitialize all buffers for next use.
1818 for (i = 0; i < q->num_buffers; ++i)
1819 __vb2_dqbuf(q->bufs[i]);
1822 static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1826 if (type != q->type) {
1827 dprintk(1, "streamon: invalid stream type\n");
1832 dprintk(3, "streamon successful: already streaming\n");
1836 if (!q->num_buffers) {
1837 dprintk(1, "streamon: no buffers have been allocated\n");
1841 if (!q->num_buffers) {
1842 dprintk(1, "streamon: no buffers have been allocated\n");
1845 if (q->num_buffers < q->min_buffers_needed) {
1846 dprintk(1, "streamon: need at least %u allocated buffers\n",
1847 q->min_buffers_needed);
1852 * Tell driver to start streaming provided sufficient buffers
1855 if (q->queued_count >= q->min_buffers_needed) {
1856 ret = vb2_start_streaming(q);
1858 __vb2_queue_cancel(q);
1865 dprintk(3, "Streamon successful\n");
1870 * vb2_streamon - start streaming
1871 * @q: videobuf2 queue
1872 * @type: type argument passed from userspace to vidioc_streamon handler
1874 * Should be called from vidioc_streamon handler of a driver.
1876 * 1) verifies current state
1877 * 2) passes any previously queued buffers to the driver and starts streaming
1879 * The return values from this function are intended to be directly returned
1880 * from vidioc_streamon handler in the driver.
1882 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1885 dprintk(1, "streamon: file io in progress\n");
1888 return vb2_internal_streamon(q, type);
1890 EXPORT_SYMBOL_GPL(vb2_streamon);
1892 static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1894 if (type != q->type) {
1895 dprintk(1, "streamoff: invalid stream type\n");
1899 if (!q->streaming) {
1900 dprintk(3, "streamoff successful: not streaming\n");
1905 * Cancel will pause streaming and remove all buffers from the driver
1906 * and videobuf, effectively returning control over them to userspace.
1908 __vb2_queue_cancel(q);
1909 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
1911 dprintk(3, "Streamoff successful\n");
1916 * vb2_streamoff - stop streaming
1917 * @q: videobuf2 queue
1918 * @type: type argument passed from userspace to vidioc_streamoff handler
1920 * Should be called from vidioc_streamoff handler of a driver.
1922 * 1) verifies current state,
1923 * 2) stop streaming and dequeues any queued buffers, including those previously
1924 * passed to the driver (after waiting for the driver to finish).
1926 * This call can be used for pausing playback.
1927 * The return values from this function are intended to be directly returned
1928 * from vidioc_streamoff handler in the driver
1930 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1933 dprintk(1, "streamoff: file io in progress\n");
1936 return vb2_internal_streamoff(q, type);
1938 EXPORT_SYMBOL_GPL(vb2_streamoff);
1941 * __find_plane_by_offset() - find plane associated with the given offset off
1943 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1944 unsigned int *_buffer, unsigned int *_plane)
1946 struct vb2_buffer *vb;
1947 unsigned int buffer, plane;
1950 * Go over all buffers and their planes, comparing the given offset
1951 * with an offset assigned to each plane. If a match is found,
1952 * return its buffer and plane numbers.
1954 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1955 vb = q->bufs[buffer];
1957 for (plane = 0; plane < vb->num_planes; ++plane) {
1958 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1970 * vb2_expbuf() - Export a buffer as a file descriptor
1971 * @q: videobuf2 queue
1972 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1975 * The return values from this function are intended to be directly returned
1976 * from vidioc_expbuf handler in driver.
1978 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1980 struct vb2_buffer *vb = NULL;
1981 struct vb2_plane *vb_plane;
1983 struct dma_buf *dbuf;
1985 if (q->memory != V4L2_MEMORY_MMAP) {
1986 dprintk(1, "Queue is not currently set up for mmap\n");
1990 if (!q->mem_ops->get_dmabuf) {
1991 dprintk(1, "Queue does not support DMA buffer exporting\n");
1995 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1996 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
2000 if (eb->type != q->type) {
2001 dprintk(1, "qbuf: invalid buffer type\n");
2005 if (eb->index >= q->num_buffers) {
2006 dprintk(1, "buffer index out of range\n");
2010 vb = q->bufs[eb->index];
2012 if (eb->plane >= vb->num_planes) {
2013 dprintk(1, "buffer plane out of range\n");
2017 vb_plane = &vb->planes[eb->plane];
2019 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
2020 if (IS_ERR_OR_NULL(dbuf)) {
2021 dprintk(1, "Failed to export buffer %d, plane %d\n",
2022 eb->index, eb->plane);
2026 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
2028 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2029 eb->index, eb->plane, ret);
2034 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2035 eb->index, eb->plane, ret);
2040 EXPORT_SYMBOL_GPL(vb2_expbuf);
2043 * vb2_mmap() - map video buffers into application address space
2044 * @q: videobuf2 queue
2045 * @vma: vma passed to the mmap file operation handler in the driver
2047 * Should be called from mmap file operation handler of a driver.
2048 * This function maps one plane of one of the available video buffers to
2049 * userspace. To map whole video memory allocated on reqbufs, this function
2050 * has to be called once per each plane per each buffer previously allocated.
2052 * When the userspace application calls mmap, it passes to it an offset returned
2053 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2054 * a "cookie", which is then used to identify the plane to be mapped.
2055 * This function finds a plane with a matching offset and a mapping is performed
2056 * by the means of a provided memory operation.
2058 * The return values from this function are intended to be directly returned
2059 * from the mmap handler in driver.
2061 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2063 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
2064 struct vb2_buffer *vb;
2065 unsigned int buffer, plane;
2067 unsigned long length;
2069 if (q->memory != V4L2_MEMORY_MMAP) {
2070 dprintk(1, "Queue is not currently set up for mmap\n");
2075 * Check memory area access mode.
2077 if (!(vma->vm_flags & VM_SHARED)) {
2078 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2081 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2082 if (!(vma->vm_flags & VM_WRITE)) {
2083 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2087 if (!(vma->vm_flags & VM_READ)) {
2088 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2094 * Find the plane corresponding to the offset passed by userspace.
2096 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2100 vb = q->bufs[buffer];
2103 * MMAP requires page_aligned buffers.
2104 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2105 * so, we need to do the same here.
2107 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
2108 if (length < (vma->vm_end - vma->vm_start)) {
2110 "MMAP invalid, as it would overflow buffer length\n");
2114 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
2118 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
2121 EXPORT_SYMBOL_GPL(vb2_mmap);
2124 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2127 unsigned long pgoff,
2128 unsigned long flags)
2130 unsigned long off = pgoff << PAGE_SHIFT;
2131 struct vb2_buffer *vb;
2132 unsigned int buffer, plane;
2135 if (q->memory != V4L2_MEMORY_MMAP) {
2136 dprintk(1, "Queue is not currently set up for mmap\n");
2141 * Find the plane corresponding to the offset passed by userspace.
2143 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2147 vb = q->bufs[buffer];
2149 return (unsigned long)vb2_plane_vaddr(vb, plane);
2151 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2154 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2155 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2158 * vb2_poll() - implements poll userspace operation
2159 * @q: videobuf2 queue
2160 * @file: file argument passed to the poll file operation handler
2161 * @wait: wait argument passed to the poll file operation handler
2163 * This function implements poll file operation handler for a driver.
2164 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2165 * be informed that the file descriptor of a video device is available for
2167 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2168 * will be reported as available for writing.
2170 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2173 * The return values from this function are intended to be directly returned
2174 * from poll handler in driver.
2176 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2178 struct video_device *vfd = video_devdata(file);
2179 unsigned long req_events = poll_requested_events(wait);
2180 struct vb2_buffer *vb = NULL;
2181 unsigned int res = 0;
2182 unsigned long flags;
2184 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2185 struct v4l2_fh *fh = file->private_data;
2187 if (v4l2_event_pending(fh))
2189 else if (req_events & POLLPRI)
2190 poll_wait(file, &fh->wait, wait);
2193 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2195 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2199 * Start file I/O emulator only if streaming API has not been used yet.
2201 if (q->num_buffers == 0 && q->fileio == NULL) {
2202 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2203 (req_events & (POLLIN | POLLRDNORM))) {
2204 if (__vb2_init_fileio(q, 1))
2205 return res | POLLERR;
2207 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2208 (req_events & (POLLOUT | POLLWRNORM))) {
2209 if (__vb2_init_fileio(q, 0))
2210 return res | POLLERR;
2212 * Write to OUTPUT queue can be done immediately.
2214 return res | POLLOUT | POLLWRNORM;
2219 * There is nothing to wait for if the queue isn't streaming.
2221 if (!vb2_is_streaming(q))
2222 return res | POLLERR;
2224 * For compatibility with vb1: if QBUF hasn't been called yet, then
2225 * return POLLERR as well. This only affects capture queues, output
2226 * queues will always initialize waiting_for_buffers to false.
2228 if (q->waiting_for_buffers)
2229 return res | POLLERR;
2231 if (list_empty(&q->done_list))
2232 poll_wait(file, &q->done_wq, wait);
2235 * Take first buffer available for dequeuing.
2237 spin_lock_irqsave(&q->done_lock, flags);
2238 if (!list_empty(&q->done_list))
2239 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2241 spin_unlock_irqrestore(&q->done_lock, flags);
2243 if (vb && (vb->state == VB2_BUF_STATE_DONE
2244 || vb->state == VB2_BUF_STATE_ERROR)) {
2245 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2246 res | POLLOUT | POLLWRNORM :
2247 res | POLLIN | POLLRDNORM;
2251 EXPORT_SYMBOL_GPL(vb2_poll);
2254 * vb2_queue_init() - initialize a videobuf2 queue
2255 * @q: videobuf2 queue; this structure should be allocated in driver
2257 * The vb2_queue structure should be allocated by the driver. The driver is
2258 * responsible of clearing it's content and setting initial values for some
2259 * required entries before calling this function.
2260 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2261 * to the struct vb2_queue description in include/media/videobuf2-core.h
2262 * for more information.
2264 int vb2_queue_init(struct vb2_queue *q)
2271 WARN_ON(!q->mem_ops) ||
2272 WARN_ON(!q->type) ||
2273 WARN_ON(!q->io_modes) ||
2274 WARN_ON(!q->ops->queue_setup) ||
2275 WARN_ON(!q->ops->buf_queue) ||
2276 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
2279 /* Warn that the driver should choose an appropriate timestamp type */
2280 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2282 INIT_LIST_HEAD(&q->queued_list);
2283 INIT_LIST_HEAD(&q->done_list);
2284 spin_lock_init(&q->done_lock);
2285 init_waitqueue_head(&q->done_wq);
2287 if (q->buf_struct_size == 0)
2288 q->buf_struct_size = sizeof(struct vb2_buffer);
2292 EXPORT_SYMBOL_GPL(vb2_queue_init);
2295 * vb2_queue_release() - stop streaming, release the queue and free memory
2296 * @q: videobuf2 queue
2298 * This function stops streaming and performs necessary clean ups, including
2299 * freeing video buffer memory. The driver is responsible for freeing
2300 * the vb2_queue structure itself.
2302 void vb2_queue_release(struct vb2_queue *q)
2304 __vb2_cleanup_fileio(q);
2305 __vb2_queue_cancel(q);
2306 __vb2_queue_free(q, q->num_buffers);
2308 EXPORT_SYMBOL_GPL(vb2_queue_release);
2311 * struct vb2_fileio_buf - buffer context used by file io emulator
2313 * vb2 provides a compatibility layer and emulator of file io (read and
2314 * write) calls on top of streaming API. This structure is used for
2315 * tracking context related to the buffers.
2317 struct vb2_fileio_buf {
2321 unsigned int queued:1;
2325 * struct vb2_fileio_data - queue context used by file io emulator
2327 * vb2 provides a compatibility layer and emulator of file io (read and
2328 * write) calls on top of streaming API. For proper operation it required
2329 * this structure to save the driver state between each call of the read
2330 * or write function.
2332 struct vb2_fileio_data {
2333 struct v4l2_requestbuffers req;
2334 struct v4l2_buffer b;
2335 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2337 unsigned int q_count;
2338 unsigned int dq_count;
2343 * __vb2_init_fileio() - initialize file io emulator
2344 * @q: videobuf2 queue
2345 * @read: mode selector (1 means read, 0 means write)
2347 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2349 struct vb2_fileio_data *fileio;
2351 unsigned int count = 0;
2356 if ((read && !(q->io_modes & VB2_READ)) ||
2357 (!read && !(q->io_modes & VB2_WRITE)))
2361 * Check if device supports mapping buffers to kernel virtual space.
2363 if (!q->mem_ops->vaddr)
2367 * Check if streaming api has not been already activated.
2369 if (q->streaming || q->num_buffers > 0)
2373 * Start with count 1, driver can increase it in queue_setup()
2377 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2378 (read) ? "read" : "write", count, q->io_flags);
2380 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2384 fileio->flags = q->io_flags;
2387 * Request buffers and use MMAP type to force driver
2388 * to allocate buffers by itself.
2390 fileio->req.count = count;
2391 fileio->req.memory = V4L2_MEMORY_MMAP;
2392 fileio->req.type = q->type;
2393 ret = vb2_reqbufs(q, &fileio->req);
2398 * Check if plane_count is correct
2399 * (multiplane buffers are not supported).
2401 if (q->bufs[0]->num_planes != 1) {
2407 * Get kernel address of each buffer.
2409 for (i = 0; i < q->num_buffers; i++) {
2410 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2411 if (fileio->bufs[i].vaddr == NULL) {
2415 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2419 * Read mode requires pre queuing of all buffers.
2423 * Queue all buffers.
2425 for (i = 0; i < q->num_buffers; i++) {
2426 struct v4l2_buffer *b = &fileio->b;
2427 memset(b, 0, sizeof(*b));
2429 b->memory = q->memory;
2431 ret = vb2_qbuf(q, b);
2434 fileio->bufs[i].queued = 1;
2436 fileio->index = q->num_buffers;
2442 ret = vb2_streamon(q, q->type);
2451 fileio->req.count = 0;
2452 vb2_reqbufs(q, &fileio->req);
2460 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2461 * @q: videobuf2 queue
2463 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2465 struct vb2_fileio_data *fileio = q->fileio;
2468 vb2_internal_streamoff(q, q->type);
2470 fileio->req.count = 0;
2471 vb2_reqbufs(q, &fileio->req);
2473 dprintk(3, "file io emulator closed\n");
2479 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2480 * @q: videobuf2 queue
2481 * @data: pointed to target userspace buffer
2482 * @count: number of bytes to read or write
2483 * @ppos: file handle position tracking pointer
2484 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2485 * @read: access mode selector (1 means read, 0 means write)
2487 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2488 loff_t *ppos, int nonblock, int read)
2490 struct vb2_fileio_data *fileio;
2491 struct vb2_fileio_buf *buf;
2494 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2495 read ? "read" : "write", (long)*ppos, count,
2496 nonblock ? "non" : "");
2502 * Initialize emulator on first call.
2505 ret = __vb2_init_fileio(q, read);
2506 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2513 * Check if we need to dequeue the buffer.
2515 index = fileio->index;
2516 if (index >= q->num_buffers) {
2518 * Call vb2_dqbuf to get buffer back.
2520 memset(&fileio->b, 0, sizeof(fileio->b));
2521 fileio->b.type = q->type;
2522 fileio->b.memory = q->memory;
2523 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
2524 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2527 fileio->dq_count += 1;
2529 index = fileio->b.index;
2530 buf = &fileio->bufs[index];
2533 * Get number of bytes filled by the driver
2537 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2538 : vb2_plane_size(q->bufs[index], 0);
2540 buf = &fileio->bufs[index];
2544 * Limit count on last few bytes of the buffer.
2546 if (buf->pos + count > buf->size) {
2547 count = buf->size - buf->pos;
2548 dprintk(5, "reducing read count: %zd\n", count);
2552 * Transfer data to userspace.
2554 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2555 count, index, buf->pos);
2557 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2559 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2561 dprintk(3, "file io: error copying data\n");
2572 * Queue next buffer if required.
2574 if (buf->pos == buf->size ||
2575 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2577 * Check if this is the last buffer to read.
2579 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2580 fileio->dq_count == 1) {
2581 dprintk(3, "file io: read limit reached\n");
2582 return __vb2_cleanup_fileio(q);
2586 * Call vb2_qbuf and give buffer to the driver.
2588 memset(&fileio->b, 0, sizeof(fileio->b));
2589 fileio->b.type = q->type;
2590 fileio->b.memory = q->memory;
2591 fileio->b.index = index;
2592 fileio->b.bytesused = buf->pos;
2593 ret = vb2_internal_qbuf(q, &fileio->b);
2594 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2599 * Buffer has been queued, update the status
2603 buf->size = vb2_plane_size(q->bufs[index], 0);
2604 fileio->q_count += 1;
2605 if (fileio->index < q->num_buffers)
2610 * Return proper number of bytes processed.
2617 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2618 loff_t *ppos, int nonblocking)
2620 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2622 EXPORT_SYMBOL_GPL(vb2_read);
2624 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
2625 loff_t *ppos, int nonblocking)
2627 return __vb2_perform_fileio(q, (char __user *) data, count,
2628 ppos, nonblocking, 0);
2630 EXPORT_SYMBOL_GPL(vb2_write);
2634 * The following functions are not part of the vb2 core API, but are helper
2635 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2636 * and struct vb2_ops.
2637 * They contain boilerplate code that most if not all drivers have to do
2638 * and so they simplify the driver code.
2641 /* The queue is busy if there is a owner and you are not that owner. */
2642 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2644 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2647 /* vb2 ioctl helpers */
2649 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2650 struct v4l2_requestbuffers *p)
2652 struct video_device *vdev = video_devdata(file);
2653 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2657 if (vb2_queue_is_busy(vdev, file))
2659 res = __reqbufs(vdev->queue, p);
2660 /* If count == 0, then the owner has released all buffers and he
2661 is no longer owner of the queue. Otherwise we have a new owner. */
2663 vdev->queue->owner = p->count ? file->private_data : NULL;
2666 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2668 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2669 struct v4l2_create_buffers *p)
2671 struct video_device *vdev = video_devdata(file);
2672 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2674 p->index = vdev->queue->num_buffers;
2675 /* If count == 0, then just check if memory and type are valid.
2676 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2678 return res != -EBUSY ? res : 0;
2681 if (vb2_queue_is_busy(vdev, file))
2683 res = __create_bufs(vdev->queue, p);
2685 vdev->queue->owner = file->private_data;
2688 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2690 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2691 struct v4l2_buffer *p)
2693 struct video_device *vdev = video_devdata(file);
2695 if (vb2_queue_is_busy(vdev, file))
2697 return vb2_prepare_buf(vdev->queue, p);
2699 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2701 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2703 struct video_device *vdev = video_devdata(file);
2705 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2706 return vb2_querybuf(vdev->queue, p);
2708 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2710 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2712 struct video_device *vdev = video_devdata(file);
2714 if (vb2_queue_is_busy(vdev, file))
2716 return vb2_qbuf(vdev->queue, p);
2718 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2720 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2722 struct video_device *vdev = video_devdata(file);
2724 if (vb2_queue_is_busy(vdev, file))
2726 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2728 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2730 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2732 struct video_device *vdev = video_devdata(file);
2734 if (vb2_queue_is_busy(vdev, file))
2736 return vb2_streamon(vdev->queue, i);
2738 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2740 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2742 struct video_device *vdev = video_devdata(file);
2744 if (vb2_queue_is_busy(vdev, file))
2746 return vb2_streamoff(vdev->queue, i);
2748 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2750 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2752 struct video_device *vdev = video_devdata(file);
2754 if (vb2_queue_is_busy(vdev, file))
2756 return vb2_expbuf(vdev->queue, p);
2758 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2760 /* v4l2_file_operations helpers */
2762 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2764 struct video_device *vdev = video_devdata(file);
2765 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2768 if (lock && mutex_lock_interruptible(lock))
2769 return -ERESTARTSYS;
2770 err = vb2_mmap(vdev->queue, vma);
2775 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2777 int _vb2_fop_release(struct file *file, struct mutex *lock)
2779 struct video_device *vdev = video_devdata(file);
2781 if (file->private_data == vdev->queue->owner) {
2784 vb2_queue_release(vdev->queue);
2785 vdev->queue->owner = NULL;
2789 return v4l2_fh_release(file);
2791 EXPORT_SYMBOL_GPL(_vb2_fop_release);
2793 int vb2_fop_release(struct file *file)
2795 struct video_device *vdev = video_devdata(file);
2796 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2798 return _vb2_fop_release(file, lock);
2800 EXPORT_SYMBOL_GPL(vb2_fop_release);
2802 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
2803 size_t count, loff_t *ppos)
2805 struct video_device *vdev = video_devdata(file);
2806 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2809 if (lock && mutex_lock_interruptible(lock))
2810 return -ERESTARTSYS;
2811 if (vb2_queue_is_busy(vdev, file))
2813 err = vb2_write(vdev->queue, buf, count, ppos,
2814 file->f_flags & O_NONBLOCK);
2815 if (vdev->queue->fileio)
2816 vdev->queue->owner = file->private_data;
2822 EXPORT_SYMBOL_GPL(vb2_fop_write);
2824 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2825 size_t count, loff_t *ppos)
2827 struct video_device *vdev = video_devdata(file);
2828 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2831 if (lock && mutex_lock_interruptible(lock))
2832 return -ERESTARTSYS;
2833 if (vb2_queue_is_busy(vdev, file))
2835 err = vb2_read(vdev->queue, buf, count, ppos,
2836 file->f_flags & O_NONBLOCK);
2837 if (vdev->queue->fileio)
2838 vdev->queue->owner = file->private_data;
2844 EXPORT_SYMBOL_GPL(vb2_fop_read);
2846 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2848 struct video_device *vdev = video_devdata(file);
2849 struct vb2_queue *q = vdev->queue;
2850 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2851 unsigned long req_events = poll_requested_events(wait);
2854 bool must_lock = false;
2856 /* Try to be smart: only lock if polling might start fileio,
2857 otherwise locking will only introduce unwanted delays. */
2858 if (q->num_buffers == 0 && q->fileio == NULL) {
2859 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2860 (req_events & (POLLIN | POLLRDNORM)))
2862 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2863 (req_events & (POLLOUT | POLLWRNORM)))
2867 /* If locking is needed, but this helper doesn't know how, then you
2868 shouldn't be using this helper but you should write your own. */
2869 WARN_ON(must_lock && !lock);
2871 if (must_lock && lock && mutex_lock_interruptible(lock))
2876 res = vb2_poll(vdev->queue, file, wait);
2878 /* If fileio was started, then we have a new queue owner. */
2879 if (must_lock && !fileio && q->fileio)
2880 q->owner = file->private_data;
2881 if (must_lock && lock)
2885 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2888 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2889 unsigned long len, unsigned long pgoff, unsigned long flags)
2891 struct video_device *vdev = video_devdata(file);
2892 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2895 if (lock && mutex_lock_interruptible(lock))
2896 return -ERESTARTSYS;
2897 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2902 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2905 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2907 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2909 mutex_unlock(vq->lock);
2911 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2913 void vb2_ops_wait_finish(struct vb2_queue *vq)
2915 mutex_lock(vq->lock);
2917 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2919 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2920 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2921 MODULE_LICENSE("GPL");