mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf2-core.c
blobc96bf9465bafee86a092ed927bcd2129501c75f0
1 /*
2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-core.h>
27 static int debug;
28 module_param(debug, int, 0644);
30 #define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
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)
48 /**
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;
54 void *mem_priv;
55 int plane;
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],
65 size, q->gfp_flags);
66 if (IS_ERR_OR_NULL(mem_priv))
67 goto free;
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];
74 return 0;
75 free:
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;
82 return -ENOMEM;
85 /**
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;
91 unsigned int plane;
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,
97 vb->v4l2_buf.index);
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
105 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
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)
123 if (!p->mem_priv)
124 return;
126 if (p->dbuf_mapped)
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;
141 unsigned int plane;
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
148 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
149 * every buffer on the queue
151 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
155 unsigned long off;
157 if (q->num_buffers) {
158 struct v4l2_plane *p;
159 vb = q->bufs[q->num_buffers - 1];
160 p = &vb->v4l2_planes[vb->num_planes - 1];
161 off = PAGE_ALIGN(p->m.mem_offset + p->length);
162 } else {
163 off = 0;
166 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
167 vb = q->bufs[buffer];
168 if (!vb)
169 continue;
171 for (plane = 0; plane < vb->num_planes; ++plane) {
172 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
173 vb->v4l2_planes[plane].m.mem_offset = off;
175 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
176 buffer, plane, off);
178 off += vb->v4l2_planes[plane].length;
179 off = PAGE_ALIGN(off);
185 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
186 * video buffer memory for all buffers/planes on the queue and initializes the
187 * queue
189 * Returns the number of buffers successfully allocated.
191 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
192 unsigned int num_buffers, unsigned int num_planes)
194 unsigned int buffer;
195 struct vb2_buffer *vb;
196 int ret;
198 for (buffer = 0; buffer < num_buffers; ++buffer) {
199 /* Allocate videobuf buffer structures */
200 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
201 if (!vb) {
202 dprintk(1, "Memory alloc for buffer struct failed\n");
203 break;
206 /* Length stores number of planes for multiplanar buffers */
207 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
208 vb->v4l2_buf.length = num_planes;
210 vb->state = VB2_BUF_STATE_DEQUEUED;
211 vb->vb2_queue = q;
212 vb->num_planes = num_planes;
213 vb->v4l2_buf.index = q->num_buffers + buffer;
214 vb->v4l2_buf.type = q->type;
215 vb->v4l2_buf.memory = memory;
217 /* Allocate video buffer memory for the MMAP type */
218 if (memory == V4L2_MEMORY_MMAP) {
219 ret = __vb2_buf_mem_alloc(vb);
220 if (ret) {
221 dprintk(1, "Failed allocating memory for "
222 "buffer %d\n", buffer);
223 kfree(vb);
224 break;
227 * Call the driver-provided buffer initialization
228 * callback, if given. An error in initialization
229 * results in queue setup failure.
231 ret = call_qop(q, buf_init, vb);
232 if (ret) {
233 dprintk(1, "Buffer %d %p initialization"
234 " failed\n", buffer, vb);
235 __vb2_buf_mem_free(vb);
236 kfree(vb);
237 break;
241 q->bufs[q->num_buffers + buffer] = vb;
244 __setup_offsets(q, buffer);
246 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
247 buffer, num_planes);
249 return buffer;
253 * __vb2_free_mem() - release all video buffer memory for a given queue
255 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
257 unsigned int buffer;
258 struct vb2_buffer *vb;
260 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
261 ++buffer) {
262 vb = q->bufs[buffer];
263 if (!vb)
264 continue;
266 /* Free MMAP buffers or release USERPTR buffers */
267 if (q->memory == V4L2_MEMORY_MMAP)
268 __vb2_buf_mem_free(vb);
269 else if (q->memory == V4L2_MEMORY_DMABUF)
270 __vb2_buf_dmabuf_put(vb);
271 else
272 __vb2_buf_userptr_put(vb);
277 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
278 * related information, if no buffers are left return the queue to an
279 * uninitialized state. Might be called even if the queue has already been freed.
281 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
283 unsigned int buffer;
285 /* Call driver-provided cleanup function for each buffer, if provided */
286 if (q->ops->buf_cleanup) {
287 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
288 ++buffer) {
289 if (NULL == q->bufs[buffer])
290 continue;
291 q->ops->buf_cleanup(q->bufs[buffer]);
295 /* Release video buffer memory */
296 __vb2_free_mem(q, buffers);
298 /* Free videobuf buffers */
299 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
300 ++buffer) {
301 kfree(q->bufs[buffer]);
302 q->bufs[buffer] = NULL;
305 q->num_buffers -= buffers;
306 if (!q->num_buffers)
307 q->memory = 0;
308 INIT_LIST_HEAD(&q->queued_list);
312 * __verify_planes_array() - verify that the planes array passed in struct
313 * v4l2_buffer from userspace can be safely used
315 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
317 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
318 return 0;
320 /* Is memory for copying plane information present? */
321 if (NULL == b->m.planes) {
322 dprintk(1, "Multi-planar buffer passed but "
323 "planes array not provided\n");
324 return -EINVAL;
327 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
328 dprintk(1, "Incorrect planes array length, "
329 "expected %d, got %d\n", vb->num_planes, b->length);
330 return -EINVAL;
333 return 0;
337 * __verify_length() - Verify that the bytesused value for each plane fits in
338 * the plane length and that the data offset doesn't exceed the bytesused value.
340 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
342 unsigned int length;
343 unsigned int plane;
345 if (!V4L2_TYPE_IS_OUTPUT(b->type))
346 return 0;
348 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
349 for (plane = 0; plane < vb->num_planes; ++plane) {
350 length = (b->memory == V4L2_MEMORY_USERPTR)
351 ? b->m.planes[plane].length
352 : vb->v4l2_planes[plane].length;
354 if (b->m.planes[plane].bytesused > length)
355 return -EINVAL;
357 if (b->m.planes[plane].data_offset > 0 &&
358 b->m.planes[plane].data_offset >=
359 b->m.planes[plane].bytesused)
360 return -EINVAL;
362 } else {
363 length = (b->memory == V4L2_MEMORY_USERPTR)
364 ? b->length : vb->v4l2_planes[0].length;
366 if (b->bytesused > length)
367 return -EINVAL;
370 return 0;
374 * __buffer_in_use() - return true if the buffer is in use and
375 * the queue cannot be freed (by the means of REQBUFS(0)) call
377 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
379 unsigned int plane;
380 for (plane = 0; plane < vb->num_planes; ++plane) {
381 void *mem_priv = vb->planes[plane].mem_priv;
383 * If num_users() has not been provided, call_memop
384 * will return 0, apparently nobody cares about this
385 * case anyway. If num_users() returns more than 1,
386 * we are not the only user of the plane's memory.
388 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
389 return true;
391 return false;
395 * __buffers_in_use() - return true if any buffers on the queue are in use and
396 * the queue cannot be freed (by the means of REQBUFS(0)) call
398 static bool __buffers_in_use(struct vb2_queue *q)
400 unsigned int buffer;
401 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
402 if (__buffer_in_use(q, q->bufs[buffer]))
403 return true;
405 return false;
409 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
410 * returned to userspace
412 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
414 struct vb2_queue *q = vb->vb2_queue;
416 /* Copy back data such as timestamp, flags, etc. */
417 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
418 b->reserved2 = vb->v4l2_buf.reserved2;
419 b->reserved = vb->v4l2_buf.reserved;
421 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
423 * Fill in plane-related data if userspace provided an array
424 * for it. The caller has already verified memory and size.
426 b->length = vb->num_planes;
427 memcpy(b->m.planes, vb->v4l2_planes,
428 b->length * sizeof(struct v4l2_plane));
429 } else {
431 * We use length and offset in v4l2_planes array even for
432 * single-planar buffers, but userspace does not.
434 b->length = vb->v4l2_planes[0].length;
435 b->bytesused = vb->v4l2_planes[0].bytesused;
436 if (q->memory == V4L2_MEMORY_MMAP)
437 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
438 else if (q->memory == V4L2_MEMORY_USERPTR)
439 b->m.userptr = vb->v4l2_planes[0].m.userptr;
440 else if (q->memory == V4L2_MEMORY_DMABUF)
441 b->m.fd = vb->v4l2_planes[0].m.fd;
445 * Clear any buffer state related flags.
447 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
448 b->flags |= q->timestamp_type;
450 switch (vb->state) {
451 case VB2_BUF_STATE_QUEUED:
452 case VB2_BUF_STATE_ACTIVE:
453 b->flags |= V4L2_BUF_FLAG_QUEUED;
454 break;
455 case VB2_BUF_STATE_ERROR:
456 b->flags |= V4L2_BUF_FLAG_ERROR;
457 /* fall through */
458 case VB2_BUF_STATE_DONE:
459 b->flags |= V4L2_BUF_FLAG_DONE;
460 break;
461 case VB2_BUF_STATE_PREPARED:
462 b->flags |= V4L2_BUF_FLAG_PREPARED;
463 break;
464 case VB2_BUF_STATE_DEQUEUED:
465 /* nothing */
466 break;
469 if (__buffer_in_use(q, vb))
470 b->flags |= V4L2_BUF_FLAG_MAPPED;
474 * vb2_querybuf() - query video buffer information
475 * @q: videobuf queue
476 * @b: buffer struct passed from userspace to vidioc_querybuf handler
477 * in driver
479 * Should be called from vidioc_querybuf ioctl handler in driver.
480 * This function will verify the passed v4l2_buffer structure and fill the
481 * relevant information for the userspace.
483 * The return values from this function are intended to be directly returned
484 * from vidioc_querybuf handler in driver.
486 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
488 struct vb2_buffer *vb;
489 int ret;
491 if (b->type != q->type) {
492 dprintk(1, "querybuf: wrong buffer type\n");
493 return -EINVAL;
496 if (b->index >= q->num_buffers) {
497 dprintk(1, "querybuf: buffer index out of range\n");
498 return -EINVAL;
500 vb = q->bufs[b->index];
501 ret = __verify_planes_array(vb, b);
502 if (!ret)
503 __fill_v4l2_buffer(vb, b);
504 return ret;
506 EXPORT_SYMBOL(vb2_querybuf);
509 * __verify_userptr_ops() - verify that all memory operations required for
510 * USERPTR queue type have been provided
512 static int __verify_userptr_ops(struct vb2_queue *q)
514 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
515 !q->mem_ops->put_userptr)
516 return -EINVAL;
518 return 0;
522 * __verify_mmap_ops() - verify that all memory operations required for
523 * MMAP queue type have been provided
525 static int __verify_mmap_ops(struct vb2_queue *q)
527 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
528 !q->mem_ops->put || !q->mem_ops->mmap)
529 return -EINVAL;
531 return 0;
535 * __verify_dmabuf_ops() - verify that all memory operations required for
536 * DMABUF queue type have been provided
538 static int __verify_dmabuf_ops(struct vb2_queue *q)
540 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
541 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
542 !q->mem_ops->unmap_dmabuf)
543 return -EINVAL;
545 return 0;
549 * __verify_memory_type() - Check whether the memory type and buffer type
550 * passed to a buffer operation are compatible with the queue.
552 static int __verify_memory_type(struct vb2_queue *q,
553 enum v4l2_memory memory, enum v4l2_buf_type type)
555 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
556 memory != V4L2_MEMORY_DMABUF) {
557 dprintk(1, "reqbufs: unsupported memory type\n");
558 return -EINVAL;
561 if (type != q->type) {
562 dprintk(1, "reqbufs: requested type is incorrect\n");
563 return -EINVAL;
567 * Make sure all the required memory ops for given memory type
568 * are available.
570 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
571 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
572 return -EINVAL;
575 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
576 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
577 return -EINVAL;
580 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
581 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
582 return -EINVAL;
586 * Place the busy tests at the end: -EBUSY can be ignored when
587 * create_bufs is called with count == 0, but count == 0 should still
588 * do the memory and type validation.
590 if (q->fileio) {
591 dprintk(1, "reqbufs: file io in progress\n");
592 return -EBUSY;
594 return 0;
598 * __reqbufs() - Initiate streaming
599 * @q: videobuf2 queue
600 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
602 * Should be called from vidioc_reqbufs ioctl handler of a driver.
603 * This function:
604 * 1) verifies streaming parameters passed from the userspace,
605 * 2) sets up the queue,
606 * 3) negotiates number of buffers and planes per buffer with the driver
607 * to be used during streaming,
608 * 4) allocates internal buffer structures (struct vb2_buffer), according to
609 * the agreed parameters,
610 * 5) for MMAP memory type, allocates actual video memory, using the
611 * memory handling/allocation routines provided during queue initialization
613 * If req->count is 0, all the memory will be freed instead.
614 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
615 * and the queue is not busy, memory will be reallocated.
617 * The return values from this function are intended to be directly returned
618 * from vidioc_reqbufs handler in driver.
620 static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
622 unsigned int num_buffers, allocated_buffers, num_planes = 0;
623 int ret;
625 if (q->streaming) {
626 dprintk(1, "reqbufs: streaming active\n");
627 return -EBUSY;
630 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
632 * We already have buffers allocated, so first check if they
633 * are not in use and can be freed.
635 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
636 dprintk(1, "reqbufs: memory in use, cannot free\n");
637 return -EBUSY;
640 __vb2_queue_free(q, q->num_buffers);
643 * In case of REQBUFS(0) return immediately without calling
644 * driver's queue_setup() callback and allocating resources.
646 if (req->count == 0)
647 return 0;
651 * Make sure the requested values and current defaults are sane.
653 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
654 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
655 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
656 q->memory = req->memory;
659 * Ask the driver how many buffers and planes per buffer it requires.
660 * Driver also sets the size and allocator context for each plane.
662 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
663 q->plane_sizes, q->alloc_ctx);
664 if (ret)
665 return ret;
667 /* Finally, allocate buffers and video memory */
668 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
669 if (ret == 0) {
670 dprintk(1, "Memory allocation failed\n");
671 return -ENOMEM;
674 allocated_buffers = ret;
677 * Check if driver can handle the allocated number of buffers.
679 if (allocated_buffers < num_buffers) {
680 num_buffers = allocated_buffers;
682 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
683 &num_planes, q->plane_sizes, q->alloc_ctx);
685 if (!ret && allocated_buffers < num_buffers)
686 ret = -ENOMEM;
689 * Either the driver has accepted a smaller number of buffers,
690 * or .queue_setup() returned an error
694 q->num_buffers = allocated_buffers;
696 if (ret < 0) {
697 __vb2_queue_free(q, allocated_buffers);
698 return ret;
702 * Return the number of successfully allocated buffers
703 * to the userspace.
705 req->count = allocated_buffers;
706 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
708 return 0;
712 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
713 * type values.
714 * @q: videobuf2 queue
715 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
717 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
719 int ret = __verify_memory_type(q, req->memory, req->type);
721 return ret ? ret : __reqbufs(q, req);
723 EXPORT_SYMBOL_GPL(vb2_reqbufs);
726 * __create_bufs() - Allocate buffers and any required auxiliary structs
727 * @q: videobuf2 queue
728 * @create: creation parameters, passed from userspace to vidioc_create_bufs
729 * handler in driver
731 * Should be called from vidioc_create_bufs ioctl handler of a driver.
732 * This function:
733 * 1) verifies parameter sanity
734 * 2) calls the .queue_setup() queue operation
735 * 3) performs any necessary memory allocations
737 * The return values from this function are intended to be directly returned
738 * from vidioc_create_bufs handler in driver.
740 static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
742 unsigned int num_planes = 0, num_buffers, allocated_buffers;
743 int ret;
745 if (q->num_buffers == VIDEO_MAX_FRAME) {
746 dprintk(1, "%s(): maximum number of buffers already allocated\n",
747 __func__);
748 return -ENOBUFS;
751 if (!q->num_buffers) {
752 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
753 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
754 q->memory = create->memory;
755 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
758 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
761 * Ask the driver, whether the requested number of buffers, planes per
762 * buffer and their sizes are acceptable
764 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
765 &num_planes, q->plane_sizes, q->alloc_ctx);
766 if (ret)
767 return ret;
769 /* Finally, allocate buffers and video memory */
770 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
771 num_planes);
772 if (ret == 0) {
773 dprintk(1, "Memory allocation failed\n");
774 return -ENOMEM;
777 allocated_buffers = ret;
780 * Check if driver can handle the so far allocated number of buffers.
782 if (ret < num_buffers) {
783 num_buffers = ret;
786 * q->num_buffers contains the total number of buffers, that the
787 * queue driver has set up
789 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
790 &num_planes, q->plane_sizes, q->alloc_ctx);
792 if (!ret && allocated_buffers < num_buffers)
793 ret = -ENOMEM;
796 * Either the driver has accepted a smaller number of buffers,
797 * or .queue_setup() returned an error
801 q->num_buffers += allocated_buffers;
803 if (ret < 0) {
804 __vb2_queue_free(q, allocated_buffers);
805 return -ENOMEM;
809 * Return the number of successfully allocated buffers
810 * to the userspace.
812 create->count = allocated_buffers;
814 return 0;
818 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
819 * memory and type values.
820 * @q: videobuf2 queue
821 * @create: creation parameters, passed from userspace to vidioc_create_bufs
822 * handler in driver
824 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
826 int ret = __verify_memory_type(q, create->memory, create->format.type);
828 create->index = q->num_buffers;
829 if (create->count == 0)
830 return ret != -EBUSY ? ret : 0;
831 return ret ? ret : __create_bufs(q, create);
833 EXPORT_SYMBOL_GPL(vb2_create_bufs);
836 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
837 * @vb: vb2_buffer to which the plane in question belongs to
838 * @plane_no: plane number for which the address is to be returned
840 * This function returns a kernel virtual address of a given plane if
841 * such a mapping exist, NULL otherwise.
843 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
845 struct vb2_queue *q = vb->vb2_queue;
847 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
848 return NULL;
850 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
853 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
856 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
857 * @vb: vb2_buffer to which the plane in question belongs to
858 * @plane_no: plane number for which the cookie is to be returned
860 * This function returns an allocator specific cookie for a given plane if
861 * available, NULL otherwise. The allocator should provide some simple static
862 * inline function, which would convert this cookie to the allocator specific
863 * type that can be used directly by the driver to access the buffer. This can
864 * be for example physical address, pointer to scatter list or IOMMU mapping.
866 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
868 struct vb2_queue *q = vb->vb2_queue;
870 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
871 return NULL;
873 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
875 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
878 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
879 * @vb: vb2_buffer returned from the driver
880 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
881 * or VB2_BUF_STATE_ERROR if the operation finished with an error
883 * This function should be called by the driver after a hardware operation on
884 * a buffer is finished and the buffer may be returned to userspace. The driver
885 * cannot use this buffer anymore until it is queued back to it by videobuf
886 * by the means of buf_queue callback. Only buffers previously queued to the
887 * driver by buf_queue can be passed to this function.
889 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
891 struct vb2_queue *q = vb->vb2_queue;
892 unsigned long flags;
893 unsigned int plane;
895 if (vb->state != VB2_BUF_STATE_ACTIVE)
896 return;
898 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
899 return;
901 dprintk(4, "Done processing on buffer %d, state: %d\n",
902 vb->v4l2_buf.index, state);
904 /* sync buffers */
905 for (plane = 0; plane < vb->num_planes; ++plane)
906 call_memop(q, finish, vb->planes[plane].mem_priv);
908 /* Add the buffer to the done buffers list */
909 spin_lock_irqsave(&q->done_lock, flags);
910 vb->state = state;
911 list_add_tail(&vb->done_entry, &q->done_list);
912 atomic_dec(&q->queued_count);
913 spin_unlock_irqrestore(&q->done_lock, flags);
915 /* Inform any processes that may be waiting for buffers */
916 wake_up(&q->done_wq);
918 EXPORT_SYMBOL_GPL(vb2_buffer_done);
921 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
922 * v4l2_buffer by the userspace. The caller has already verified that struct
923 * v4l2_buffer has a valid number of planes.
925 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
926 struct v4l2_plane *v4l2_planes)
928 unsigned int plane;
930 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
931 /* Fill in driver-provided information for OUTPUT types */
932 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
934 * Will have to go up to b->length when API starts
935 * accepting variable number of planes.
937 for (plane = 0; plane < vb->num_planes; ++plane) {
938 v4l2_planes[plane].bytesused =
939 b->m.planes[plane].bytesused;
940 v4l2_planes[plane].data_offset =
941 b->m.planes[plane].data_offset;
945 if (b->memory == V4L2_MEMORY_USERPTR) {
946 for (plane = 0; plane < vb->num_planes; ++plane) {
947 v4l2_planes[plane].m.userptr =
948 b->m.planes[plane].m.userptr;
949 v4l2_planes[plane].length =
950 b->m.planes[plane].length;
953 if (b->memory == V4L2_MEMORY_DMABUF) {
954 for (plane = 0; plane < vb->num_planes; ++plane) {
955 v4l2_planes[plane].m.fd =
956 b->m.planes[plane].m.fd;
957 v4l2_planes[plane].length =
958 b->m.planes[plane].length;
959 v4l2_planes[plane].data_offset =
960 b->m.planes[plane].data_offset;
963 } else {
965 * Single-planar buffers do not use planes array,
966 * so fill in relevant v4l2_buffer struct fields instead.
967 * In videobuf we use our internal V4l2_planes struct for
968 * single-planar buffers as well, for simplicity.
970 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
971 v4l2_planes[0].bytesused = b->bytesused;
972 v4l2_planes[0].data_offset = 0;
975 if (b->memory == V4L2_MEMORY_USERPTR) {
976 v4l2_planes[0].m.userptr = b->m.userptr;
977 v4l2_planes[0].length = b->length;
980 if (b->memory == V4L2_MEMORY_DMABUF) {
981 v4l2_planes[0].m.fd = b->m.fd;
982 v4l2_planes[0].length = b->length;
983 v4l2_planes[0].data_offset = 0;
988 vb->v4l2_buf.field = b->field;
989 vb->v4l2_buf.timestamp = b->timestamp;
990 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
994 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
996 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
998 struct v4l2_plane planes[VIDEO_MAX_PLANES];
999 struct vb2_queue *q = vb->vb2_queue;
1000 void *mem_priv;
1001 unsigned int plane;
1002 int ret;
1003 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1005 /* Copy relevant information provided by the userspace */
1006 __fill_vb2_buffer(vb, b, planes);
1008 for (plane = 0; plane < vb->num_planes; ++plane) {
1009 /* Skip the plane if already verified */
1010 if (vb->v4l2_planes[plane].m.userptr &&
1011 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
1012 && vb->v4l2_planes[plane].length == planes[plane].length)
1013 continue;
1015 dprintk(3, "qbuf: userspace address for plane %d changed, "
1016 "reacquiring memory\n", plane);
1018 /* Check if the provided plane buffer is large enough */
1019 if (planes[plane].length < q->plane_sizes[plane]) {
1020 ret = -EINVAL;
1021 goto err;
1024 /* Release previously acquired memory if present */
1025 if (vb->planes[plane].mem_priv)
1026 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1028 vb->planes[plane].mem_priv = NULL;
1029 vb->v4l2_planes[plane].m.userptr = 0;
1030 vb->v4l2_planes[plane].length = 0;
1032 /* Acquire each plane's memory */
1033 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1034 planes[plane].m.userptr,
1035 planes[plane].length, write);
1036 if (IS_ERR_OR_NULL(mem_priv)) {
1037 dprintk(1, "qbuf: failed acquiring userspace "
1038 "memory for plane %d\n", plane);
1039 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1040 goto err;
1042 vb->planes[plane].mem_priv = mem_priv;
1046 * Call driver-specific initialization on the newly acquired buffer,
1047 * if provided.
1049 ret = call_qop(q, buf_init, vb);
1050 if (ret) {
1051 dprintk(1, "qbuf: buffer initialization failed\n");
1052 goto err;
1056 * Now that everything is in order, copy relevant information
1057 * provided by userspace.
1059 for (plane = 0; plane < vb->num_planes; ++plane)
1060 vb->v4l2_planes[plane] = planes[plane];
1062 return 0;
1063 err:
1064 /* In case of errors, release planes that were already acquired */
1065 for (plane = 0; plane < vb->num_planes; ++plane) {
1066 if (vb->planes[plane].mem_priv)
1067 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1068 vb->planes[plane].mem_priv = NULL;
1069 vb->v4l2_planes[plane].m.userptr = 0;
1070 vb->v4l2_planes[plane].length = 0;
1073 return ret;
1077 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1079 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1081 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1082 return 0;
1086 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1088 static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1090 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1091 struct vb2_queue *q = vb->vb2_queue;
1092 void *mem_priv;
1093 unsigned int plane;
1094 int ret;
1095 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1097 /* Verify and copy relevant information provided by the userspace */
1098 __fill_vb2_buffer(vb, b, planes);
1100 for (plane = 0; plane < vb->num_planes; ++plane) {
1101 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1103 if (IS_ERR_OR_NULL(dbuf)) {
1104 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1105 plane);
1106 ret = -EINVAL;
1107 goto err;
1110 /* use DMABUF size if length is not provided */
1111 if (planes[plane].length == 0)
1112 planes[plane].length = dbuf->size;
1114 if (planes[plane].length < planes[plane].data_offset +
1115 q->plane_sizes[plane]) {
1116 ret = -EINVAL;
1117 goto err;
1120 /* Skip the plane if already verified */
1121 if (dbuf == vb->planes[plane].dbuf &&
1122 vb->v4l2_planes[plane].length == planes[plane].length) {
1123 dma_buf_put(dbuf);
1124 continue;
1127 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1129 /* Release previously acquired memory if present */
1130 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1131 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1133 /* Acquire each plane's memory */
1134 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1135 dbuf, planes[plane].length, write);
1136 if (IS_ERR(mem_priv)) {
1137 dprintk(1, "qbuf: failed to attach dmabuf\n");
1138 ret = PTR_ERR(mem_priv);
1139 dma_buf_put(dbuf);
1140 goto err;
1143 vb->planes[plane].dbuf = dbuf;
1144 vb->planes[plane].mem_priv = mem_priv;
1147 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1148 * really we want to do this just before the DMA, not while queueing
1149 * the buffer(s)..
1151 for (plane = 0; plane < vb->num_planes; ++plane) {
1152 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1153 if (ret) {
1154 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1155 plane);
1156 goto err;
1158 vb->planes[plane].dbuf_mapped = 1;
1162 * Call driver-specific initialization on the newly acquired buffer,
1163 * if provided.
1165 ret = call_qop(q, buf_init, vb);
1166 if (ret) {
1167 dprintk(1, "qbuf: buffer initialization failed\n");
1168 goto err;
1172 * Now that everything is in order, copy relevant information
1173 * provided by userspace.
1175 for (plane = 0; plane < vb->num_planes; ++plane)
1176 vb->v4l2_planes[plane] = planes[plane];
1178 return 0;
1179 err:
1180 /* In case of errors, release planes that were already acquired */
1181 __vb2_buf_dmabuf_put(vb);
1183 return ret;
1187 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1189 static void __enqueue_in_driver(struct vb2_buffer *vb)
1191 struct vb2_queue *q = vb->vb2_queue;
1192 unsigned int plane;
1194 vb->state = VB2_BUF_STATE_ACTIVE;
1195 atomic_inc(&q->queued_count);
1197 /* sync buffers */
1198 for (plane = 0; plane < vb->num_planes; ++plane)
1199 call_memop(q, prepare, vb->planes[plane].mem_priv);
1201 q->ops->buf_queue(vb);
1204 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1206 struct vb2_queue *q = vb->vb2_queue;
1207 int ret;
1209 ret = __verify_length(vb, b);
1210 if (ret < 0)
1211 return ret;
1213 switch (q->memory) {
1214 case V4L2_MEMORY_MMAP:
1215 ret = __qbuf_mmap(vb, b);
1216 break;
1217 case V4L2_MEMORY_USERPTR:
1218 ret = __qbuf_userptr(vb, b);
1219 break;
1220 case V4L2_MEMORY_DMABUF:
1221 ret = __qbuf_dmabuf(vb, b);
1222 break;
1223 default:
1224 WARN(1, "Invalid queue type\n");
1225 ret = -EINVAL;
1228 if (!ret)
1229 ret = call_qop(q, buf_prepare, vb);
1230 if (ret)
1231 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1232 else
1233 vb->state = VB2_BUF_STATE_PREPARED;
1235 return ret;
1238 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1239 const char *opname,
1240 int (*handler)(struct vb2_queue *,
1241 struct v4l2_buffer *,
1242 struct vb2_buffer *))
1244 struct rw_semaphore *mmap_sem = NULL;
1245 struct vb2_buffer *vb;
1246 int ret;
1249 * In case of user pointer buffers vb2 allocators need to get direct
1250 * access to userspace pages. This requires getting the mmap semaphore
1251 * for read access in the current process structure. The same semaphore
1252 * is taken before calling mmap operation, while both qbuf/prepare_buf
1253 * and mmap are called by the driver or v4l2 core with the driver's lock
1254 * held. To avoid an AB-BA deadlock (mmap_sem then driver's lock in mmap
1255 * and driver's lock then mmap_sem in qbuf/prepare_buf) the videobuf2
1256 * core releases the driver's lock, takes mmap_sem and then takes the
1257 * driver's lock again.
1259 * To avoid racing with other vb2 calls, which might be called after
1260 * releasing the driver's lock, this operation is performed at the
1261 * beginning of qbuf/prepare_buf processing. This way the queue status
1262 * is consistent after getting the driver's lock back.
1264 if (q->memory == V4L2_MEMORY_USERPTR) {
1265 mmap_sem = &current->mm->mmap_sem;
1266 call_qop(q, wait_prepare, q);
1267 down_read(mmap_sem);
1268 call_qop(q, wait_finish, q);
1271 if (q->fileio) {
1272 dprintk(1, "%s(): file io in progress\n", opname);
1273 ret = -EBUSY;
1274 goto unlock;
1277 if (b->type != q->type) {
1278 dprintk(1, "%s(): invalid buffer type\n", opname);
1279 ret = -EINVAL;
1280 goto unlock;
1283 if (b->index >= q->num_buffers) {
1284 dprintk(1, "%s(): buffer index out of range\n", opname);
1285 ret = -EINVAL;
1286 goto unlock;
1289 vb = q->bufs[b->index];
1290 if (NULL == vb) {
1291 /* Should never happen */
1292 dprintk(1, "%s(): buffer is NULL\n", opname);
1293 ret = -EINVAL;
1294 goto unlock;
1297 if (b->memory != q->memory) {
1298 dprintk(1, "%s(): invalid memory type\n", opname);
1299 ret = -EINVAL;
1300 goto unlock;
1303 ret = __verify_planes_array(vb, b);
1304 if (ret)
1305 goto unlock;
1307 ret = handler(q, b, vb);
1308 if (ret)
1309 goto unlock;
1311 /* Fill buffer information for the userspace */
1312 __fill_v4l2_buffer(vb, b);
1314 dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
1315 unlock:
1316 if (mmap_sem)
1317 up_read(mmap_sem);
1318 return ret;
1321 static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1322 struct vb2_buffer *vb)
1324 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1325 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1326 vb->state);
1327 return -EINVAL;
1330 return __buf_prepare(vb, b);
1334 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1335 * @q: videobuf2 queue
1336 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1337 * handler in driver
1339 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1340 * This function:
1341 * 1) verifies the passed buffer,
1342 * 2) calls buf_prepare callback in the driver (if provided), in which
1343 * driver-specific buffer initialization can be performed,
1345 * The return values from this function are intended to be directly returned
1346 * from vidioc_prepare_buf handler in driver.
1348 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1350 return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
1352 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1354 static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
1355 struct vb2_buffer *vb)
1357 int ret;
1359 switch (vb->state) {
1360 case VB2_BUF_STATE_DEQUEUED:
1361 ret = __buf_prepare(vb, b);
1362 if (ret)
1363 return ret;
1364 case VB2_BUF_STATE_PREPARED:
1365 break;
1366 default:
1367 dprintk(1, "qbuf: buffer already in use\n");
1368 return -EINVAL;
1372 * Add to the queued buffers list, a buffer will stay on it until
1373 * dequeued in dqbuf.
1375 list_add_tail(&vb->queued_entry, &q->queued_list);
1376 q->waiting_for_buffers = false;
1377 vb->state = VB2_BUF_STATE_QUEUED;
1380 * If already streaming, give the buffer to driver for processing.
1381 * If not, the buffer will be given to driver on next streamon.
1383 if (q->streaming)
1384 __enqueue_in_driver(vb);
1386 return 0;
1390 * vb2_qbuf() - Queue a buffer from userspace
1391 * @q: videobuf2 queue
1392 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1393 * in driver
1395 * Should be called from vidioc_qbuf ioctl handler of a driver.
1396 * This function:
1397 * 1) verifies the passed buffer,
1398 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1399 * which driver-specific buffer initialization can be performed,
1400 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1401 * callback for processing.
1403 * The return values from this function are intended to be directly returned
1404 * from vidioc_qbuf handler in driver.
1406 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1408 return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
1410 EXPORT_SYMBOL_GPL(vb2_qbuf);
1413 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1414 * for dequeuing
1416 * Will sleep if required for nonblocking == false.
1418 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1421 * All operations on vb_done_list are performed under done_lock
1422 * spinlock protection. However, buffers may be removed from
1423 * it and returned to userspace only while holding both driver's
1424 * lock and the done_lock spinlock. Thus we can be sure that as
1425 * long as we hold the driver's lock, the list will remain not
1426 * empty if list_empty() check succeeds.
1429 for (;;) {
1430 int ret;
1432 if (!q->streaming) {
1433 dprintk(1, "Streaming off, will not wait for buffers\n");
1434 return -EINVAL;
1437 if (!list_empty(&q->done_list)) {
1439 * Found a buffer that we were waiting for.
1441 break;
1444 if (nonblocking) {
1445 dprintk(1, "Nonblocking and no buffers to dequeue, "
1446 "will not wait\n");
1447 return -EAGAIN;
1451 * We are streaming and blocking, wait for another buffer to
1452 * become ready or for streamoff. Driver's lock is released to
1453 * allow streamoff or qbuf to be called while waiting.
1455 call_qop(q, wait_prepare, q);
1458 * All locks have been released, it is safe to sleep now.
1460 dprintk(3, "Will sleep waiting for buffers\n");
1461 ret = wait_event_interruptible(q->done_wq,
1462 !list_empty(&q->done_list) || !q->streaming);
1465 * We need to reevaluate both conditions again after reacquiring
1466 * the locks or return an error if one occurred.
1468 call_qop(q, wait_finish, q);
1469 if (ret) {
1470 dprintk(1, "Sleep was interrupted\n");
1471 return ret;
1474 return 0;
1478 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1480 * Will sleep if required for nonblocking == false.
1482 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1483 struct v4l2_buffer *b, int nonblocking)
1485 unsigned long flags;
1486 int ret;
1489 * Wait for at least one buffer to become available on the done_list.
1491 ret = __vb2_wait_for_done_vb(q, nonblocking);
1492 if (ret)
1493 return ret;
1496 * Driver's lock has been held since we last verified that done_list
1497 * is not empty, so no need for another list_empty(done_list) check.
1499 spin_lock_irqsave(&q->done_lock, flags);
1500 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1502 * Only remove the buffer from done_list if v4l2_buffer can handle all
1503 * the planes.
1505 ret = __verify_planes_array(*vb, b);
1506 if (!ret)
1507 list_del(&(*vb)->done_entry);
1508 spin_unlock_irqrestore(&q->done_lock, flags);
1510 return ret;
1514 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1515 * @q: videobuf2 queue
1517 * This function will wait until all buffers that have been given to the driver
1518 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1519 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1520 * taken, for example from stop_streaming() callback.
1522 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1524 if (!q->streaming) {
1525 dprintk(1, "Streaming off, will not wait for buffers\n");
1526 return -EINVAL;
1529 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1530 return 0;
1532 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1535 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1537 static void __vb2_dqbuf(struct vb2_buffer *vb)
1539 struct vb2_queue *q = vb->vb2_queue;
1540 unsigned int i;
1542 /* nothing to do if the buffer is already dequeued */
1543 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1544 return;
1546 vb->state = VB2_BUF_STATE_DEQUEUED;
1548 /* unmap DMABUF buffer */
1549 if (q->memory == V4L2_MEMORY_DMABUF)
1550 for (i = 0; i < vb->num_planes; ++i) {
1551 if (!vb->planes[i].dbuf_mapped)
1552 continue;
1553 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1554 vb->planes[i].dbuf_mapped = 0;
1559 * vb2_dqbuf() - Dequeue a buffer to the userspace
1560 * @q: videobuf2 queue
1561 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1562 * in driver
1563 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1564 * buffers ready for dequeuing are present. Normally the driver
1565 * would be passing (file->f_flags & O_NONBLOCK) here
1567 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1568 * This function:
1569 * 1) verifies the passed buffer,
1570 * 2) calls buf_finish callback in the driver (if provided), in which
1571 * driver can perform any additional operations that may be required before
1572 * returning the buffer to userspace, such as cache sync,
1573 * 3) the buffer struct members are filled with relevant information for
1574 * the userspace.
1576 * The return values from this function are intended to be directly returned
1577 * from vidioc_dqbuf handler in driver.
1579 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1581 struct vb2_buffer *vb = NULL;
1582 int ret;
1584 if (q->fileio) {
1585 dprintk(1, "dqbuf: file io in progress\n");
1586 return -EBUSY;
1589 if (b->type != q->type) {
1590 dprintk(1, "dqbuf: invalid buffer type\n");
1591 return -EINVAL;
1593 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1594 if (ret < 0)
1595 return ret;
1597 ret = call_qop(q, buf_finish, vb);
1598 if (ret) {
1599 dprintk(1, "dqbuf: buffer finish failed\n");
1600 return ret;
1603 switch (vb->state) {
1604 case VB2_BUF_STATE_DONE:
1605 dprintk(3, "dqbuf: Returning done buffer\n");
1606 break;
1607 case VB2_BUF_STATE_ERROR:
1608 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1609 break;
1610 default:
1611 dprintk(1, "dqbuf: Invalid buffer state\n");
1612 return -EINVAL;
1615 /* Fill buffer information for the userspace */
1616 __fill_v4l2_buffer(vb, b);
1617 /* Remove from videobuf queue */
1618 list_del(&vb->queued_entry);
1619 /* go back to dequeued state */
1620 __vb2_dqbuf(vb);
1622 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1623 vb->v4l2_buf.index, vb->state);
1625 return 0;
1627 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1630 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1632 * Removes all queued buffers from driver's queue and all buffers queued by
1633 * userspace from videobuf's queue. Returns to state after reqbufs.
1635 static void __vb2_queue_cancel(struct vb2_queue *q)
1637 unsigned int i;
1640 * Tell driver to stop all transactions and release all queued
1641 * buffers.
1643 if (q->streaming)
1644 call_qop(q, stop_streaming, q);
1645 q->streaming = 0;
1648 * Remove all buffers from videobuf's list...
1650 INIT_LIST_HEAD(&q->queued_list);
1652 * ...and done list; userspace will not receive any buffers it
1653 * has not already dequeued before initiating cancel.
1655 INIT_LIST_HEAD(&q->done_list);
1656 atomic_set(&q->queued_count, 0);
1657 wake_up_all(&q->done_wq);
1660 * Reinitialize all buffers for next use.
1662 for (i = 0; i < q->num_buffers; ++i)
1663 __vb2_dqbuf(q->bufs[i]);
1667 * vb2_streamon - start streaming
1668 * @q: videobuf2 queue
1669 * @type: type argument passed from userspace to vidioc_streamon handler
1671 * Should be called from vidioc_streamon handler of a driver.
1672 * This function:
1673 * 1) verifies current state
1674 * 2) passes any previously queued buffers to the driver and starts streaming
1676 * The return values from this function are intended to be directly returned
1677 * from vidioc_streamon handler in the driver.
1679 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1681 struct vb2_buffer *vb;
1682 int ret;
1684 if (q->fileio) {
1685 dprintk(1, "streamon: file io in progress\n");
1686 return -EBUSY;
1689 if (type != q->type) {
1690 dprintk(1, "streamon: invalid stream type\n");
1691 return -EINVAL;
1694 if (q->streaming) {
1695 dprintk(1, "streamon: already streaming\n");
1696 return -EBUSY;
1700 * If any buffers were queued before streamon,
1701 * we can now pass them to driver for processing.
1703 list_for_each_entry(vb, &q->queued_list, queued_entry)
1704 __enqueue_in_driver(vb);
1707 * Let driver notice that streaming state has been enabled.
1709 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1710 if (ret) {
1711 dprintk(1, "streamon: driver refused to start streaming\n");
1712 __vb2_queue_cancel(q);
1713 return ret;
1716 q->streaming = 1;
1718 dprintk(3, "Streamon successful\n");
1719 return 0;
1721 EXPORT_SYMBOL_GPL(vb2_streamon);
1725 * vb2_streamoff - stop streaming
1726 * @q: videobuf2 queue
1727 * @type: type argument passed from userspace to vidioc_streamoff handler
1729 * Should be called from vidioc_streamoff handler of a driver.
1730 * This function:
1731 * 1) verifies current state,
1732 * 2) stop streaming and dequeues any queued buffers, including those previously
1733 * passed to the driver (after waiting for the driver to finish).
1735 * This call can be used for pausing playback.
1736 * The return values from this function are intended to be directly returned
1737 * from vidioc_streamoff handler in the driver
1739 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1741 if (q->fileio) {
1742 dprintk(1, "streamoff: file io in progress\n");
1743 return -EBUSY;
1746 if (type != q->type) {
1747 dprintk(1, "streamoff: invalid stream type\n");
1748 return -EINVAL;
1751 if (!q->streaming) {
1752 dprintk(1, "streamoff: not streaming\n");
1753 return -EINVAL;
1757 * Cancel will pause streaming and remove all buffers from the driver
1758 * and videobuf, effectively returning control over them to userspace.
1760 __vb2_queue_cancel(q);
1761 q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
1763 dprintk(3, "Streamoff successful\n");
1764 return 0;
1766 EXPORT_SYMBOL_GPL(vb2_streamoff);
1769 * __find_plane_by_offset() - find plane associated with the given offset off
1771 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1772 unsigned int *_buffer, unsigned int *_plane)
1774 struct vb2_buffer *vb;
1775 unsigned int buffer, plane;
1778 * Go over all buffers and their planes, comparing the given offset
1779 * with an offset assigned to each plane. If a match is found,
1780 * return its buffer and plane numbers.
1782 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1783 vb = q->bufs[buffer];
1785 for (plane = 0; plane < vb->num_planes; ++plane) {
1786 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1787 *_buffer = buffer;
1788 *_plane = plane;
1789 return 0;
1794 return -EINVAL;
1798 * vb2_expbuf() - Export a buffer as a file descriptor
1799 * @q: videobuf2 queue
1800 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1801 * handler in driver
1803 * The return values from this function are intended to be directly returned
1804 * from vidioc_expbuf handler in driver.
1806 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1808 struct vb2_buffer *vb = NULL;
1809 struct vb2_plane *vb_plane;
1810 int ret;
1811 struct dma_buf *dbuf;
1813 if (q->memory != V4L2_MEMORY_MMAP) {
1814 dprintk(1, "Queue is not currently set up for mmap\n");
1815 return -EINVAL;
1818 if (!q->mem_ops->get_dmabuf) {
1819 dprintk(1, "Queue does not support DMA buffer exporting\n");
1820 return -EINVAL;
1823 if (eb->flags & ~O_CLOEXEC) {
1824 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1825 return -EINVAL;
1828 if (eb->type != q->type) {
1829 dprintk(1, "qbuf: invalid buffer type\n");
1830 return -EINVAL;
1833 if (eb->index >= q->num_buffers) {
1834 dprintk(1, "buffer index out of range\n");
1835 return -EINVAL;
1838 vb = q->bufs[eb->index];
1840 if (eb->plane >= vb->num_planes) {
1841 dprintk(1, "buffer plane out of range\n");
1842 return -EINVAL;
1845 vb_plane = &vb->planes[eb->plane];
1847 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1848 if (IS_ERR_OR_NULL(dbuf)) {
1849 dprintk(1, "Failed to export buffer %d, plane %d\n",
1850 eb->index, eb->plane);
1851 return -EINVAL;
1854 ret = dma_buf_fd(dbuf, eb->flags);
1855 if (ret < 0) {
1856 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1857 eb->index, eb->plane, ret);
1858 dma_buf_put(dbuf);
1859 return ret;
1862 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1863 eb->index, eb->plane, ret);
1864 eb->fd = ret;
1866 return 0;
1868 EXPORT_SYMBOL_GPL(vb2_expbuf);
1871 * vb2_mmap() - map video buffers into application address space
1872 * @q: videobuf2 queue
1873 * @vma: vma passed to the mmap file operation handler in the driver
1875 * Should be called from mmap file operation handler of a driver.
1876 * This function maps one plane of one of the available video buffers to
1877 * userspace. To map whole video memory allocated on reqbufs, this function
1878 * has to be called once per each plane per each buffer previously allocated.
1880 * When the userspace application calls mmap, it passes to it an offset returned
1881 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1882 * a "cookie", which is then used to identify the plane to be mapped.
1883 * This function finds a plane with a matching offset and a mapping is performed
1884 * by the means of a provided memory operation.
1886 * The return values from this function are intended to be directly returned
1887 * from the mmap handler in driver.
1889 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1891 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1892 struct vb2_buffer *vb;
1893 unsigned int buffer, plane;
1894 int ret;
1895 unsigned long length;
1897 if (q->memory != V4L2_MEMORY_MMAP) {
1898 dprintk(1, "Queue is not currently set up for mmap\n");
1899 return -EINVAL;
1903 * Check memory area access mode.
1905 if (!(vma->vm_flags & VM_SHARED)) {
1906 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1907 return -EINVAL;
1909 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1910 if (!(vma->vm_flags & VM_WRITE)) {
1911 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1912 return -EINVAL;
1914 } else {
1915 if (!(vma->vm_flags & VM_READ)) {
1916 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1917 return -EINVAL;
1922 * Find the plane corresponding to the offset passed by userspace.
1924 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1925 if (ret)
1926 return ret;
1928 vb = q->bufs[buffer];
1931 * MMAP requires page_aligned buffers.
1932 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1933 * so, we need to do the same here.
1935 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1936 if (length < (vma->vm_end - vma->vm_start)) {
1937 dprintk(1,
1938 "MMAP invalid, as it would overflow buffer length\n");
1939 return -EINVAL;
1942 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
1943 if (ret)
1944 return ret;
1946 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1947 return 0;
1949 EXPORT_SYMBOL_GPL(vb2_mmap);
1951 #ifndef CONFIG_MMU
1952 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1953 unsigned long addr,
1954 unsigned long len,
1955 unsigned long pgoff,
1956 unsigned long flags)
1958 unsigned long off = pgoff << PAGE_SHIFT;
1959 struct vb2_buffer *vb;
1960 unsigned int buffer, plane;
1961 int ret;
1963 if (q->memory != V4L2_MEMORY_MMAP) {
1964 dprintk(1, "Queue is not currently set up for mmap\n");
1965 return -EINVAL;
1969 * Find the plane corresponding to the offset passed by userspace.
1971 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1972 if (ret)
1973 return ret;
1975 vb = q->bufs[buffer];
1977 return (unsigned long)vb2_plane_vaddr(vb, plane);
1979 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1980 #endif
1982 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1983 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1986 * vb2_poll() - implements poll userspace operation
1987 * @q: videobuf2 queue
1988 * @file: file argument passed to the poll file operation handler
1989 * @wait: wait argument passed to the poll file operation handler
1991 * This function implements poll file operation handler for a driver.
1992 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1993 * be informed that the file descriptor of a video device is available for
1994 * reading.
1995 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1996 * will be reported as available for writing.
1998 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1999 * pending events.
2001 * The return values from this function are intended to be directly returned
2002 * from poll handler in driver.
2004 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2006 struct video_device *vfd = video_devdata(file);
2007 unsigned long req_events = poll_requested_events(wait);
2008 struct vb2_buffer *vb = NULL;
2009 unsigned int res = 0;
2010 unsigned long flags;
2012 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2013 struct v4l2_fh *fh = file->private_data;
2015 if (v4l2_event_pending(fh))
2016 res = POLLPRI;
2017 else if (req_events & POLLPRI)
2018 poll_wait(file, &fh->wait, wait);
2021 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2022 return res;
2023 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2024 return res;
2027 * Start file I/O emulator only if streaming API has not been used yet.
2029 if (q->num_buffers == 0 && q->fileio == NULL) {
2030 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2031 (req_events & (POLLIN | POLLRDNORM))) {
2032 if (__vb2_init_fileio(q, 1))
2033 return res | POLLERR;
2035 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2036 (req_events & (POLLOUT | POLLWRNORM))) {
2037 if (__vb2_init_fileio(q, 0))
2038 return res | POLLERR;
2040 * Write to OUTPUT queue can be done immediately.
2042 return res | POLLOUT | POLLWRNORM;
2047 * There is nothing to wait for if the queue isn't streaming.
2049 if (!vb2_is_streaming(q))
2050 return res | POLLERR;
2052 * For compatibility with vb1: if QBUF hasn't been called yet, then
2053 * return POLLERR as well. This only affects capture queues, output
2054 * queues will always initialize waiting_for_buffers to false.
2056 if (q->waiting_for_buffers)
2057 return res | POLLERR;
2059 if (list_empty(&q->done_list))
2060 poll_wait(file, &q->done_wq, wait);
2063 * Take first buffer available for dequeuing.
2065 spin_lock_irqsave(&q->done_lock, flags);
2066 if (!list_empty(&q->done_list))
2067 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2068 done_entry);
2069 spin_unlock_irqrestore(&q->done_lock, flags);
2071 if (vb && (vb->state == VB2_BUF_STATE_DONE
2072 || vb->state == VB2_BUF_STATE_ERROR)) {
2073 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2074 res | POLLOUT | POLLWRNORM :
2075 res | POLLIN | POLLRDNORM;
2077 return res;
2079 EXPORT_SYMBOL_GPL(vb2_poll);
2082 * vb2_queue_init() - initialize a videobuf2 queue
2083 * @q: videobuf2 queue; this structure should be allocated in driver
2085 * The vb2_queue structure should be allocated by the driver. The driver is
2086 * responsible of clearing it's content and setting initial values for some
2087 * required entries before calling this function.
2088 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2089 * to the struct vb2_queue description in include/media/videobuf2-core.h
2090 * for more information.
2092 int vb2_queue_init(struct vb2_queue *q)
2095 * Sanity check
2097 if (WARN_ON(!q) ||
2098 WARN_ON(!q->ops) ||
2099 WARN_ON(!q->mem_ops) ||
2100 WARN_ON(!q->type) ||
2101 WARN_ON(!q->io_modes) ||
2102 WARN_ON(!q->ops->queue_setup) ||
2103 WARN_ON(!q->ops->buf_queue) ||
2104 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
2105 return -EINVAL;
2107 /* Warn that the driver should choose an appropriate timestamp type */
2108 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2110 INIT_LIST_HEAD(&q->queued_list);
2111 INIT_LIST_HEAD(&q->done_list);
2112 spin_lock_init(&q->done_lock);
2113 init_waitqueue_head(&q->done_wq);
2115 if (q->buf_struct_size == 0)
2116 q->buf_struct_size = sizeof(struct vb2_buffer);
2118 return 0;
2120 EXPORT_SYMBOL_GPL(vb2_queue_init);
2123 * vb2_queue_release() - stop streaming, release the queue and free memory
2124 * @q: videobuf2 queue
2126 * This function stops streaming and performs necessary clean ups, including
2127 * freeing video buffer memory. The driver is responsible for freeing
2128 * the vb2_queue structure itself.
2130 void vb2_queue_release(struct vb2_queue *q)
2132 __vb2_cleanup_fileio(q);
2133 __vb2_queue_cancel(q);
2134 __vb2_queue_free(q, q->num_buffers);
2136 EXPORT_SYMBOL_GPL(vb2_queue_release);
2139 * struct vb2_fileio_buf - buffer context used by file io emulator
2141 * vb2 provides a compatibility layer and emulator of file io (read and
2142 * write) calls on top of streaming API. This structure is used for
2143 * tracking context related to the buffers.
2145 struct vb2_fileio_buf {
2146 void *vaddr;
2147 unsigned int size;
2148 unsigned int pos;
2149 unsigned int queued:1;
2153 * struct vb2_fileio_data - queue context used by file io emulator
2155 * vb2 provides a compatibility layer and emulator of file io (read and
2156 * write) calls on top of streaming API. For proper operation it required
2157 * this structure to save the driver state between each call of the read
2158 * or write function.
2160 struct vb2_fileio_data {
2161 struct v4l2_requestbuffers req;
2162 struct v4l2_buffer b;
2163 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2164 unsigned int index;
2165 unsigned int q_count;
2166 unsigned int dq_count;
2167 unsigned int flags;
2171 * __vb2_init_fileio() - initialize file io emulator
2172 * @q: videobuf2 queue
2173 * @read: mode selector (1 means read, 0 means write)
2175 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2177 struct vb2_fileio_data *fileio;
2178 int i, ret;
2179 unsigned int count = 0;
2182 * Sanity check
2184 if ((read && !(q->io_modes & VB2_READ)) ||
2185 (!read && !(q->io_modes & VB2_WRITE)))
2186 BUG();
2189 * Check if device supports mapping buffers to kernel virtual space.
2191 if (!q->mem_ops->vaddr)
2192 return -EBUSY;
2195 * Check if streaming api has not been already activated.
2197 if (q->streaming || q->num_buffers > 0)
2198 return -EBUSY;
2201 * Start with count 1, driver can increase it in queue_setup()
2203 count = 1;
2205 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2206 (read) ? "read" : "write", count, q->io_flags);
2208 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2209 if (fileio == NULL)
2210 return -ENOMEM;
2212 fileio->flags = q->io_flags;
2215 * Request buffers and use MMAP type to force driver
2216 * to allocate buffers by itself.
2218 fileio->req.count = count;
2219 fileio->req.memory = V4L2_MEMORY_MMAP;
2220 fileio->req.type = q->type;
2221 ret = vb2_reqbufs(q, &fileio->req);
2222 if (ret)
2223 goto err_kfree;
2226 * Check if plane_count is correct
2227 * (multiplane buffers are not supported).
2229 if (q->bufs[0]->num_planes != 1) {
2230 ret = -EBUSY;
2231 goto err_reqbufs;
2235 * Get kernel address of each buffer.
2237 for (i = 0; i < q->num_buffers; i++) {
2238 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2239 if (fileio->bufs[i].vaddr == NULL) {
2240 ret = -EINVAL;
2241 goto err_reqbufs;
2243 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2247 * Read mode requires pre queuing of all buffers.
2249 if (read) {
2251 * Queue all buffers.
2253 for (i = 0; i < q->num_buffers; i++) {
2254 struct v4l2_buffer *b = &fileio->b;
2255 memset(b, 0, sizeof(*b));
2256 b->type = q->type;
2257 b->memory = q->memory;
2258 b->index = i;
2259 ret = vb2_qbuf(q, b);
2260 if (ret)
2261 goto err_reqbufs;
2262 fileio->bufs[i].queued = 1;
2266 * Start streaming.
2268 ret = vb2_streamon(q, q->type);
2269 if (ret)
2270 goto err_reqbufs;
2273 q->fileio = fileio;
2275 return ret;
2277 err_reqbufs:
2278 fileio->req.count = 0;
2279 vb2_reqbufs(q, &fileio->req);
2281 err_kfree:
2282 kfree(fileio);
2283 return ret;
2287 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2288 * @q: videobuf2 queue
2290 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2292 struct vb2_fileio_data *fileio = q->fileio;
2294 if (fileio) {
2296 * Hack fileio context to enable direct calls to vb2 ioctl
2297 * interface.
2299 q->fileio = NULL;
2301 vb2_streamoff(q, q->type);
2302 fileio->req.count = 0;
2303 vb2_reqbufs(q, &fileio->req);
2304 kfree(fileio);
2305 dprintk(3, "file io emulator closed\n");
2307 return 0;
2311 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2312 * @q: videobuf2 queue
2313 * @data: pointed to target userspace buffer
2314 * @count: number of bytes to read or write
2315 * @ppos: file handle position tracking pointer
2316 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2317 * @read: access mode selector (1 means read, 0 means write)
2319 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2320 loff_t *ppos, int nonblock, int read)
2322 struct vb2_fileio_data *fileio;
2323 struct vb2_fileio_buf *buf;
2324 int ret, index;
2326 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2327 read ? "read" : "write", (long)*ppos, count,
2328 nonblock ? "non" : "");
2330 if (!data)
2331 return -EINVAL;
2334 * Initialize emulator on first call.
2336 if (!q->fileio) {
2337 ret = __vb2_init_fileio(q, read);
2338 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2339 if (ret)
2340 return ret;
2342 fileio = q->fileio;
2345 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2346 * The pointer will be restored before returning from this function.
2348 q->fileio = NULL;
2350 index = fileio->index;
2351 buf = &fileio->bufs[index];
2354 * Check if we need to dequeue the buffer.
2356 if (buf->queued) {
2357 struct vb2_buffer *vb;
2360 * Call vb2_dqbuf to get buffer back.
2362 memset(&fileio->b, 0, sizeof(fileio->b));
2363 fileio->b.type = q->type;
2364 fileio->b.memory = q->memory;
2365 fileio->b.index = index;
2366 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2367 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2368 if (ret)
2369 goto end;
2370 fileio->dq_count += 1;
2373 * Get number of bytes filled by the driver
2375 vb = q->bufs[index];
2376 buf->size = vb2_get_plane_payload(vb, 0);
2377 buf->queued = 0;
2381 * Limit count on last few bytes of the buffer.
2383 if (buf->pos + count > buf->size) {
2384 count = buf->size - buf->pos;
2385 dprintk(5, "reducing read count: %zd\n", count);
2389 * Transfer data to userspace.
2391 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2392 count, index, buf->pos);
2393 if (read)
2394 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2395 else
2396 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2397 if (ret) {
2398 dprintk(3, "file io: error copying data\n");
2399 ret = -EFAULT;
2400 goto end;
2404 * Update counters.
2406 buf->pos += count;
2407 *ppos += count;
2410 * Queue next buffer if required.
2412 if (buf->pos == buf->size ||
2413 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2415 * Check if this is the last buffer to read.
2417 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2418 fileio->dq_count == 1) {
2419 dprintk(3, "file io: read limit reached\n");
2421 * Restore fileio pointer and release the context.
2423 q->fileio = fileio;
2424 return __vb2_cleanup_fileio(q);
2428 * Call vb2_qbuf and give buffer to the driver.
2430 memset(&fileio->b, 0, sizeof(fileio->b));
2431 fileio->b.type = q->type;
2432 fileio->b.memory = q->memory;
2433 fileio->b.index = index;
2434 fileio->b.bytesused = buf->pos;
2435 ret = vb2_qbuf(q, &fileio->b);
2436 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2437 if (ret)
2438 goto end;
2441 * Buffer has been queued, update the status
2443 buf->pos = 0;
2444 buf->queued = 1;
2445 buf->size = q->bufs[0]->v4l2_planes[0].length;
2446 fileio->q_count += 1;
2449 * Switch to the next buffer
2451 fileio->index = (index + 1) % q->num_buffers;
2454 * Start streaming if required.
2456 if (!read && !q->streaming) {
2457 ret = vb2_streamon(q, q->type);
2458 if (ret)
2459 goto end;
2464 * Return proper number of bytes processed.
2466 if (ret == 0)
2467 ret = count;
2468 end:
2470 * Restore the fileio context and block vb2 ioctl interface.
2472 q->fileio = fileio;
2473 return ret;
2476 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2477 loff_t *ppos, int nonblocking)
2479 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2481 EXPORT_SYMBOL_GPL(vb2_read);
2483 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2484 loff_t *ppos, int nonblocking)
2486 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2488 EXPORT_SYMBOL_GPL(vb2_write);
2492 * The following functions are not part of the vb2 core API, but are helper
2493 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2494 * and struct vb2_ops.
2495 * They contain boilerplate code that most if not all drivers have to do
2496 * and so they simplify the driver code.
2499 /* The queue is busy if there is a owner and you are not that owner. */
2500 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2502 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2505 /* vb2 ioctl helpers */
2507 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2508 struct v4l2_requestbuffers *p)
2510 struct video_device *vdev = video_devdata(file);
2511 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2513 if (res)
2514 return res;
2515 if (vb2_queue_is_busy(vdev, file))
2516 return -EBUSY;
2517 res = __reqbufs(vdev->queue, p);
2518 /* If count == 0, then the owner has released all buffers and he
2519 is no longer owner of the queue. Otherwise we have a new owner. */
2520 if (res == 0)
2521 vdev->queue->owner = p->count ? file->private_data : NULL;
2522 return res;
2524 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2526 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2527 struct v4l2_create_buffers *p)
2529 struct video_device *vdev = video_devdata(file);
2530 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2532 p->index = vdev->queue->num_buffers;
2533 /* If count == 0, then just check if memory and type are valid.
2534 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2535 if (p->count == 0)
2536 return res != -EBUSY ? res : 0;
2537 if (res)
2538 return res;
2539 if (vb2_queue_is_busy(vdev, file))
2540 return -EBUSY;
2541 res = __create_bufs(vdev->queue, p);
2542 if (res == 0)
2543 vdev->queue->owner = file->private_data;
2544 return res;
2546 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2548 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2549 struct v4l2_buffer *p)
2551 struct video_device *vdev = video_devdata(file);
2553 if (vb2_queue_is_busy(vdev, file))
2554 return -EBUSY;
2555 return vb2_prepare_buf(vdev->queue, p);
2557 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2559 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2561 struct video_device *vdev = video_devdata(file);
2563 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2564 return vb2_querybuf(vdev->queue, p);
2566 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2568 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2570 struct video_device *vdev = video_devdata(file);
2572 if (vb2_queue_is_busy(vdev, file))
2573 return -EBUSY;
2574 return vb2_qbuf(vdev->queue, p);
2576 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2578 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2580 struct video_device *vdev = video_devdata(file);
2582 if (vb2_queue_is_busy(vdev, file))
2583 return -EBUSY;
2584 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2586 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2588 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2590 struct video_device *vdev = video_devdata(file);
2592 if (vb2_queue_is_busy(vdev, file))
2593 return -EBUSY;
2594 return vb2_streamon(vdev->queue, i);
2596 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2598 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2600 struct video_device *vdev = video_devdata(file);
2602 if (vb2_queue_is_busy(vdev, file))
2603 return -EBUSY;
2604 return vb2_streamoff(vdev->queue, i);
2606 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2608 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2610 struct video_device *vdev = video_devdata(file);
2612 if (vb2_queue_is_busy(vdev, file))
2613 return -EBUSY;
2614 return vb2_expbuf(vdev->queue, p);
2616 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2618 /* v4l2_file_operations helpers */
2620 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2622 struct video_device *vdev = video_devdata(file);
2623 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2624 int err;
2626 if (lock && mutex_lock_interruptible(lock))
2627 return -ERESTARTSYS;
2628 err = vb2_mmap(vdev->queue, vma);
2629 if (lock)
2630 mutex_unlock(lock);
2631 return err;
2633 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2635 int vb2_fop_release(struct file *file)
2637 struct video_device *vdev = video_devdata(file);
2639 if (file->private_data == vdev->queue->owner) {
2640 vb2_queue_release(vdev->queue);
2641 vdev->queue->owner = NULL;
2643 return v4l2_fh_release(file);
2645 EXPORT_SYMBOL_GPL(vb2_fop_release);
2647 ssize_t vb2_fop_write(struct file *file, char __user *buf,
2648 size_t count, loff_t *ppos)
2650 struct video_device *vdev = video_devdata(file);
2651 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2652 int err = -EBUSY;
2654 if (lock && mutex_lock_interruptible(lock))
2655 return -ERESTARTSYS;
2656 if (vb2_queue_is_busy(vdev, file))
2657 goto exit;
2658 err = vb2_write(vdev->queue, buf, count, ppos,
2659 file->f_flags & O_NONBLOCK);
2660 if (vdev->queue->fileio)
2661 vdev->queue->owner = file->private_data;
2662 exit:
2663 if (lock)
2664 mutex_unlock(lock);
2665 return err;
2667 EXPORT_SYMBOL_GPL(vb2_fop_write);
2669 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2670 size_t count, loff_t *ppos)
2672 struct video_device *vdev = video_devdata(file);
2673 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2674 int err = -EBUSY;
2676 if (lock && mutex_lock_interruptible(lock))
2677 return -ERESTARTSYS;
2678 if (vb2_queue_is_busy(vdev, file))
2679 goto exit;
2680 err = vb2_read(vdev->queue, buf, count, ppos,
2681 file->f_flags & O_NONBLOCK);
2682 if (vdev->queue->fileio)
2683 vdev->queue->owner = file->private_data;
2684 exit:
2685 if (lock)
2686 mutex_unlock(lock);
2687 return err;
2689 EXPORT_SYMBOL_GPL(vb2_fop_read);
2691 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2693 struct video_device *vdev = video_devdata(file);
2694 struct vb2_queue *q = vdev->queue;
2695 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2696 unsigned long req_events = poll_requested_events(wait);
2697 unsigned res;
2698 void *fileio;
2699 bool must_lock = false;
2701 /* Try to be smart: only lock if polling might start fileio,
2702 otherwise locking will only introduce unwanted delays. */
2703 if (q->num_buffers == 0 && q->fileio == NULL) {
2704 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2705 (req_events & (POLLIN | POLLRDNORM)))
2706 must_lock = true;
2707 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2708 (req_events & (POLLOUT | POLLWRNORM)))
2709 must_lock = true;
2712 /* If locking is needed, but this helper doesn't know how, then you
2713 shouldn't be using this helper but you should write your own. */
2714 WARN_ON(must_lock && !lock);
2716 if (must_lock && lock && mutex_lock_interruptible(lock))
2717 return POLLERR;
2719 fileio = q->fileio;
2721 res = vb2_poll(vdev->queue, file, wait);
2723 /* If fileio was started, then we have a new queue owner. */
2724 if (must_lock && !fileio && q->fileio)
2725 q->owner = file->private_data;
2726 if (must_lock && lock)
2727 mutex_unlock(lock);
2728 return res;
2730 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2732 #ifndef CONFIG_MMU
2733 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2734 unsigned long len, unsigned long pgoff, unsigned long flags)
2736 struct video_device *vdev = video_devdata(file);
2737 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2738 int ret;
2740 if (lock && mutex_lock_interruptible(lock))
2741 return -ERESTARTSYS;
2742 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2743 if (lock)
2744 mutex_unlock(lock);
2745 return ret;
2747 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2748 #endif
2750 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2752 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2754 mutex_unlock(vq->lock);
2756 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2758 void vb2_ops_wait_finish(struct vb2_queue *vq)
2760 mutex_lock(vq->lock);
2762 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2764 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2765 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2766 MODULE_LICENSE("GPL");