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_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;
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);
166 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
167 vb = q->bufs[buffer];
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",
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
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)
195 struct vb2_buffer *vb;
198 for (buffer = 0; buffer < num_buffers; ++buffer) {
199 /* Allocate videobuf buffer structures */
200 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
202 dprintk(1, "Memory alloc for buffer struct failed\n");
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;
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);
221 dprintk(1, "Failed allocating memory for "
222 "buffer %d\n", buffer);
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);
233 dprintk(1, "Buffer %d %p initialization"
234 " failed\n", buffer, vb);
235 __vb2_buf_mem_free(vb);
241 q->bufs[q->num_buffers + buffer] = vb;
244 __setup_offsets(q, buffer);
246 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
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)
258 struct vb2_buffer *vb;
260 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
262 vb = q->bufs[buffer];
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);
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)
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;
289 if (NULL == q->bufs[buffer])
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;
301 kfree(q->bufs[buffer]);
302 q->bufs[buffer] = NULL;
305 q->num_buffers -= buffers;
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))
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");
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);
337 * __buffer_in_use() - return true if the buffer is in use and
338 * the queue cannot be freed (by the means of REQBUFS(0)) call
340 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
343 for (plane = 0; plane < vb->num_planes; ++plane) {
344 void *mem_priv = vb->planes[plane].mem_priv;
346 * If num_users() has not been provided, call_memop
347 * will return 0, apparently nobody cares about this
348 * case anyway. If num_users() returns more than 1,
349 * we are not the only user of the plane's memory.
351 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
358 * __buffers_in_use() - return true if any buffers on the queue are in use and
359 * the queue cannot be freed (by the means of REQBUFS(0)) call
361 static bool __buffers_in_use(struct vb2_queue *q)
364 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
365 if (__buffer_in_use(q, q->bufs[buffer]))
372 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
373 * returned to userspace
375 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
377 struct vb2_queue *q = vb->vb2_queue;
379 /* Copy back data such as timestamp, flags, etc. */
380 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
381 b->reserved2 = vb->v4l2_buf.reserved2;
382 b->reserved = vb->v4l2_buf.reserved;
384 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
386 * Fill in plane-related data if userspace provided an array
387 * for it. The caller has already verified memory and size.
389 b->length = vb->num_planes;
390 memcpy(b->m.planes, vb->v4l2_planes,
391 b->length * sizeof(struct v4l2_plane));
394 * We use length and offset in v4l2_planes array even for
395 * single-planar buffers, but userspace does not.
397 b->length = vb->v4l2_planes[0].length;
398 b->bytesused = vb->v4l2_planes[0].bytesused;
399 if (q->memory == V4L2_MEMORY_MMAP)
400 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
401 else if (q->memory == V4L2_MEMORY_USERPTR)
402 b->m.userptr = vb->v4l2_planes[0].m.userptr;
403 else if (q->memory == V4L2_MEMORY_DMABUF)
404 b->m.fd = vb->v4l2_planes[0].m.fd;
408 * Clear any buffer state related flags.
410 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
411 b->flags |= q->timestamp_type;
414 case VB2_BUF_STATE_QUEUED:
415 case VB2_BUF_STATE_ACTIVE:
416 b->flags |= V4L2_BUF_FLAG_QUEUED;
418 case VB2_BUF_STATE_ERROR:
419 b->flags |= V4L2_BUF_FLAG_ERROR;
421 case VB2_BUF_STATE_DONE:
422 b->flags |= V4L2_BUF_FLAG_DONE;
424 case VB2_BUF_STATE_PREPARED:
425 b->flags |= V4L2_BUF_FLAG_PREPARED;
427 case VB2_BUF_STATE_DEQUEUED:
432 if (__buffer_in_use(q, vb))
433 b->flags |= V4L2_BUF_FLAG_MAPPED;
437 * vb2_querybuf() - query video buffer information
439 * @b: buffer struct passed from userspace to vidioc_querybuf handler
442 * Should be called from vidioc_querybuf ioctl handler in driver.
443 * This function will verify the passed v4l2_buffer structure and fill the
444 * relevant information for the userspace.
446 * The return values from this function are intended to be directly returned
447 * from vidioc_querybuf handler in driver.
449 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
451 struct vb2_buffer *vb;
454 if (b->type != q->type) {
455 dprintk(1, "querybuf: wrong buffer type\n");
459 if (b->index >= q->num_buffers) {
460 dprintk(1, "querybuf: buffer index out of range\n");
463 vb = q->bufs[b->index];
464 ret = __verify_planes_array(vb, b);
466 __fill_v4l2_buffer(vb, b);
469 EXPORT_SYMBOL(vb2_querybuf);
472 * __verify_userptr_ops() - verify that all memory operations required for
473 * USERPTR queue type have been provided
475 static int __verify_userptr_ops(struct vb2_queue *q)
477 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
478 !q->mem_ops->put_userptr)
485 * __verify_mmap_ops() - verify that all memory operations required for
486 * MMAP queue type have been provided
488 static int __verify_mmap_ops(struct vb2_queue *q)
490 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
491 !q->mem_ops->put || !q->mem_ops->mmap)
498 * __verify_dmabuf_ops() - verify that all memory operations required for
499 * DMABUF queue type have been provided
501 static int __verify_dmabuf_ops(struct vb2_queue *q)
503 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
504 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
505 !q->mem_ops->unmap_dmabuf)
512 * __verify_memory_type() - Check whether the memory type and buffer type
513 * passed to a buffer operation are compatible with the queue.
515 static int __verify_memory_type(struct vb2_queue *q,
516 enum v4l2_memory memory, enum v4l2_buf_type type)
518 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
519 memory != V4L2_MEMORY_DMABUF) {
520 dprintk(1, "reqbufs: unsupported memory type\n");
524 if (type != q->type) {
525 dprintk(1, "reqbufs: requested type is incorrect\n");
530 * Make sure all the required memory ops for given memory type
533 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
534 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
538 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
539 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
543 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
544 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
549 * Place the busy tests at the end: -EBUSY can be ignored when
550 * create_bufs is called with count == 0, but count == 0 should still
551 * do the memory and type validation.
554 dprintk(1, "reqbufs: file io in progress\n");
561 * __reqbufs() - Initiate streaming
562 * @q: videobuf2 queue
563 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
565 * Should be called from vidioc_reqbufs ioctl handler of a driver.
567 * 1) verifies streaming parameters passed from the userspace,
568 * 2) sets up the queue,
569 * 3) negotiates number of buffers and planes per buffer with the driver
570 * to be used during streaming,
571 * 4) allocates internal buffer structures (struct vb2_buffer), according to
572 * the agreed parameters,
573 * 5) for MMAP memory type, allocates actual video memory, using the
574 * memory handling/allocation routines provided during queue initialization
576 * If req->count is 0, all the memory will be freed instead.
577 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
578 * and the queue is not busy, memory will be reallocated.
580 * The return values from this function are intended to be directly returned
581 * from vidioc_reqbufs handler in driver.
583 static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
585 unsigned int num_buffers, allocated_buffers, num_planes = 0;
589 dprintk(1, "reqbufs: streaming active\n");
593 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
595 * We already have buffers allocated, so first check if they
596 * are not in use and can be freed.
598 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
599 dprintk(1, "reqbufs: memory in use, cannot free\n");
603 __vb2_queue_free(q, q->num_buffers);
606 * In case of REQBUFS(0) return immediately without calling
607 * driver's queue_setup() callback and allocating resources.
614 * Make sure the requested values and current defaults are sane.
616 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
617 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
618 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
619 q->memory = req->memory;
622 * Ask the driver how many buffers and planes per buffer it requires.
623 * Driver also sets the size and allocator context for each plane.
625 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
626 q->plane_sizes, q->alloc_ctx);
630 /* Finally, allocate buffers and video memory */
631 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
633 dprintk(1, "Memory allocation failed\n");
637 allocated_buffers = ret;
640 * Check if driver can handle the allocated number of buffers.
642 if (allocated_buffers < num_buffers) {
643 num_buffers = allocated_buffers;
645 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
646 &num_planes, q->plane_sizes, q->alloc_ctx);
648 if (!ret && allocated_buffers < num_buffers)
652 * Either the driver has accepted a smaller number of buffers,
653 * or .queue_setup() returned an error
657 q->num_buffers = allocated_buffers;
660 __vb2_queue_free(q, allocated_buffers);
665 * Return the number of successfully allocated buffers
668 req->count = allocated_buffers;
674 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
676 * @q: videobuf2 queue
677 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
679 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
681 int ret = __verify_memory_type(q, req->memory, req->type);
683 return ret ? ret : __reqbufs(q, req);
685 EXPORT_SYMBOL_GPL(vb2_reqbufs);
688 * __create_bufs() - Allocate buffers and any required auxiliary structs
689 * @q: videobuf2 queue
690 * @create: creation parameters, passed from userspace to vidioc_create_bufs
693 * Should be called from vidioc_create_bufs ioctl handler of a driver.
695 * 1) verifies parameter sanity
696 * 2) calls the .queue_setup() queue operation
697 * 3) performs any necessary memory allocations
699 * The return values from this function are intended to be directly returned
700 * from vidioc_create_bufs handler in driver.
702 static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
704 unsigned int num_planes = 0, num_buffers, allocated_buffers;
707 if (q->num_buffers == VIDEO_MAX_FRAME) {
708 dprintk(1, "%s(): maximum number of buffers already allocated\n",
713 if (!q->num_buffers) {
714 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
715 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
716 q->memory = create->memory;
719 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
722 * Ask the driver, whether the requested number of buffers, planes per
723 * buffer and their sizes are acceptable
725 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
726 &num_planes, q->plane_sizes, q->alloc_ctx);
730 /* Finally, allocate buffers and video memory */
731 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
734 dprintk(1, "Memory allocation failed\n");
738 allocated_buffers = ret;
741 * Check if driver can handle the so far allocated number of buffers.
743 if (ret < num_buffers) {
747 * q->num_buffers contains the total number of buffers, that the
748 * queue driver has set up
750 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
751 &num_planes, q->plane_sizes, q->alloc_ctx);
753 if (!ret && allocated_buffers < num_buffers)
757 * Either the driver has accepted a smaller number of buffers,
758 * or .queue_setup() returned an error
762 q->num_buffers += allocated_buffers;
765 __vb2_queue_free(q, allocated_buffers);
770 * Return the number of successfully allocated buffers
773 create->count = allocated_buffers;
779 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
780 * memory and type values.
781 * @q: videobuf2 queue
782 * @create: creation parameters, passed from userspace to vidioc_create_bufs
785 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
787 int ret = __verify_memory_type(q, create->memory, create->format.type);
789 create->index = q->num_buffers;
790 if (create->count == 0)
791 return ret != -EBUSY ? ret : 0;
792 return ret ? ret : __create_bufs(q, create);
794 EXPORT_SYMBOL_GPL(vb2_create_bufs);
797 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
798 * @vb: vb2_buffer to which the plane in question belongs to
799 * @plane_no: plane number for which the address is to be returned
801 * This function returns a kernel virtual address of a given plane if
802 * such a mapping exist, NULL otherwise.
804 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
806 struct vb2_queue *q = vb->vb2_queue;
808 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
811 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
814 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
817 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
818 * @vb: vb2_buffer to which the plane in question belongs to
819 * @plane_no: plane number for which the cookie is to be returned
821 * This function returns an allocator specific cookie for a given plane if
822 * available, NULL otherwise. The allocator should provide some simple static
823 * inline function, which would convert this cookie to the allocator specific
824 * type that can be used directly by the driver to access the buffer. This can
825 * be for example physical address, pointer to scatter list or IOMMU mapping.
827 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
829 struct vb2_queue *q = vb->vb2_queue;
831 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
834 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
836 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
839 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
840 * @vb: vb2_buffer returned from the driver
841 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
842 * or VB2_BUF_STATE_ERROR if the operation finished with an error
844 * This function should be called by the driver after a hardware operation on
845 * a buffer is finished and the buffer may be returned to userspace. The driver
846 * cannot use this buffer anymore until it is queued back to it by videobuf
847 * by the means of buf_queue callback. Only buffers previously queued to the
848 * driver by buf_queue can be passed to this function.
850 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
852 struct vb2_queue *q = vb->vb2_queue;
856 if (vb->state != VB2_BUF_STATE_ACTIVE)
859 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
862 dprintk(4, "Done processing on buffer %d, state: %d\n",
863 vb->v4l2_buf.index, state);
866 for (plane = 0; plane < vb->num_planes; ++plane)
867 call_memop(q, finish, vb->planes[plane].mem_priv);
869 /* Add the buffer to the done buffers list */
870 spin_lock_irqsave(&q->done_lock, flags);
872 list_add_tail(&vb->done_entry, &q->done_list);
873 atomic_dec(&q->queued_count);
874 spin_unlock_irqrestore(&q->done_lock, flags);
876 /* Inform any processes that may be waiting for buffers */
877 wake_up(&q->done_wq);
879 EXPORT_SYMBOL_GPL(vb2_buffer_done);
882 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
883 * v4l2_buffer by the userspace. The caller has already verified that struct
884 * v4l2_buffer has a valid number of planes.
886 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
887 struct v4l2_plane *v4l2_planes)
891 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
892 /* Fill in driver-provided information for OUTPUT types */
893 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
895 * Will have to go up to b->length when API starts
896 * accepting variable number of planes.
898 for (plane = 0; plane < vb->num_planes; ++plane) {
899 v4l2_planes[plane].bytesused =
900 b->m.planes[plane].bytesused;
901 v4l2_planes[plane].data_offset =
902 b->m.planes[plane].data_offset;
906 if (b->memory == V4L2_MEMORY_USERPTR) {
907 for (plane = 0; plane < vb->num_planes; ++plane) {
908 v4l2_planes[plane].m.userptr =
909 b->m.planes[plane].m.userptr;
910 v4l2_planes[plane].length =
911 b->m.planes[plane].length;
914 if (b->memory == V4L2_MEMORY_DMABUF) {
915 for (plane = 0; plane < vb->num_planes; ++plane) {
916 v4l2_planes[plane].m.fd =
917 b->m.planes[plane].m.fd;
918 v4l2_planes[plane].length =
919 b->m.planes[plane].length;
920 v4l2_planes[plane].data_offset =
921 b->m.planes[plane].data_offset;
926 * Single-planar buffers do not use planes array,
927 * so fill in relevant v4l2_buffer struct fields instead.
928 * In videobuf we use our internal V4l2_planes struct for
929 * single-planar buffers as well, for simplicity.
931 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
932 v4l2_planes[0].bytesused = b->bytesused;
933 v4l2_planes[0].data_offset = 0;
936 if (b->memory == V4L2_MEMORY_USERPTR) {
937 v4l2_planes[0].m.userptr = b->m.userptr;
938 v4l2_planes[0].length = b->length;
941 if (b->memory == V4L2_MEMORY_DMABUF) {
942 v4l2_planes[0].m.fd = b->m.fd;
943 v4l2_planes[0].length = b->length;
944 v4l2_planes[0].data_offset = 0;
949 vb->v4l2_buf.field = b->field;
950 vb->v4l2_buf.timestamp = b->timestamp;
951 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
955 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
957 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
959 struct v4l2_plane planes[VIDEO_MAX_PLANES];
960 struct vb2_queue *q = vb->vb2_queue;
964 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
966 /* Copy relevant information provided by the userspace */
967 __fill_vb2_buffer(vb, b, planes);
969 for (plane = 0; plane < vb->num_planes; ++plane) {
970 /* Skip the plane if already verified */
971 if (vb->v4l2_planes[plane].m.userptr &&
972 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
973 && vb->v4l2_planes[plane].length == planes[plane].length)
976 dprintk(3, "qbuf: userspace address for plane %d changed, "
977 "reacquiring memory\n", plane);
979 /* Check if the provided plane buffer is large enough */
980 if (planes[plane].length < q->plane_sizes[plane]) {
985 /* Release previously acquired memory if present */
986 if (vb->planes[plane].mem_priv)
987 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
989 vb->planes[plane].mem_priv = NULL;
990 vb->v4l2_planes[plane].m.userptr = 0;
991 vb->v4l2_planes[plane].length = 0;
993 /* Acquire each plane's memory */
994 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
995 planes[plane].m.userptr,
996 planes[plane].length, write);
997 if (IS_ERR_OR_NULL(mem_priv)) {
998 dprintk(1, "qbuf: failed acquiring userspace "
999 "memory for plane %d\n", plane);
1000 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1003 vb->planes[plane].mem_priv = mem_priv;
1007 * Call driver-specific initialization on the newly acquired buffer,
1010 ret = call_qop(q, buf_init, vb);
1012 dprintk(1, "qbuf: buffer initialization failed\n");
1017 * Now that everything is in order, copy relevant information
1018 * provided by userspace.
1020 for (plane = 0; plane < vb->num_planes; ++plane)
1021 vb->v4l2_planes[plane] = planes[plane];
1025 /* In case of errors, release planes that were already acquired */
1026 for (plane = 0; plane < vb->num_planes; ++plane) {
1027 if (vb->planes[plane].mem_priv)
1028 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1029 vb->planes[plane].mem_priv = NULL;
1030 vb->v4l2_planes[plane].m.userptr = 0;
1031 vb->v4l2_planes[plane].length = 0;
1038 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1040 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1042 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1047 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1049 static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1051 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1052 struct vb2_queue *q = vb->vb2_queue;
1056 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1058 /* Verify and copy relevant information provided by the userspace */
1059 __fill_vb2_buffer(vb, b, planes);
1061 for (plane = 0; plane < vb->num_planes; ++plane) {
1062 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1064 if (IS_ERR_OR_NULL(dbuf)) {
1065 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1071 /* use DMABUF size if length is not provided */
1072 if (planes[plane].length == 0)
1073 planes[plane].length = dbuf->size;
1075 if (planes[plane].length < planes[plane].data_offset +
1076 q->plane_sizes[plane]) {
1081 /* Skip the plane if already verified */
1082 if (dbuf == vb->planes[plane].dbuf &&
1083 vb->v4l2_planes[plane].length == planes[plane].length) {
1088 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1090 /* Release previously acquired memory if present */
1091 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1092 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1094 /* Acquire each plane's memory */
1095 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1096 dbuf, planes[plane].length, write);
1097 if (IS_ERR(mem_priv)) {
1098 dprintk(1, "qbuf: failed to attach dmabuf\n");
1099 ret = PTR_ERR(mem_priv);
1104 vb->planes[plane].dbuf = dbuf;
1105 vb->planes[plane].mem_priv = mem_priv;
1108 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1109 * really we want to do this just before the DMA, not while queueing
1112 for (plane = 0; plane < vb->num_planes; ++plane) {
1113 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1115 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1119 vb->planes[plane].dbuf_mapped = 1;
1123 * Call driver-specific initialization on the newly acquired buffer,
1126 ret = call_qop(q, buf_init, vb);
1128 dprintk(1, "qbuf: buffer initialization failed\n");
1133 * Now that everything is in order, copy relevant information
1134 * provided by userspace.
1136 for (plane = 0; plane < vb->num_planes; ++plane)
1137 vb->v4l2_planes[plane] = planes[plane];
1141 /* In case of errors, release planes that were already acquired */
1142 __vb2_buf_dmabuf_put(vb);
1148 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1150 static void __enqueue_in_driver(struct vb2_buffer *vb)
1152 struct vb2_queue *q = vb->vb2_queue;
1155 vb->state = VB2_BUF_STATE_ACTIVE;
1156 atomic_inc(&q->queued_count);
1159 for (plane = 0; plane < vb->num_planes; ++plane)
1160 call_memop(q, prepare, vb->planes[plane].mem_priv);
1162 q->ops->buf_queue(vb);
1165 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1167 struct vb2_queue *q = vb->vb2_queue;
1170 switch (q->memory) {
1171 case V4L2_MEMORY_MMAP:
1172 ret = __qbuf_mmap(vb, b);
1174 case V4L2_MEMORY_USERPTR:
1175 ret = __qbuf_userptr(vb, b);
1177 case V4L2_MEMORY_DMABUF:
1178 ret = __qbuf_dmabuf(vb, b);
1181 WARN(1, "Invalid queue type\n");
1186 ret = call_qop(q, buf_prepare, vb);
1188 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1190 vb->state = VB2_BUF_STATE_PREPARED;
1196 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1197 * @q: videobuf2 queue
1198 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1201 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1203 * 1) verifies the passed buffer,
1204 * 2) calls buf_prepare callback in the driver (if provided), in which
1205 * driver-specific buffer initialization can be performed,
1207 * The return values from this function are intended to be directly returned
1208 * from vidioc_prepare_buf handler in driver.
1210 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1212 struct vb2_buffer *vb;
1216 dprintk(1, "%s(): file io in progress\n", __func__);
1220 if (b->type != q->type) {
1221 dprintk(1, "%s(): invalid buffer type\n", __func__);
1225 if (b->index >= q->num_buffers) {
1226 dprintk(1, "%s(): buffer index out of range\n", __func__);
1230 vb = q->bufs[b->index];
1232 /* Should never happen */
1233 dprintk(1, "%s(): buffer is NULL\n", __func__);
1237 if (b->memory != q->memory) {
1238 dprintk(1, "%s(): invalid memory type\n", __func__);
1242 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1243 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1246 ret = __verify_planes_array(vb, b);
1249 ret = __buf_prepare(vb, b);
1253 __fill_v4l2_buffer(vb, b);
1257 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1260 * vb2_qbuf() - Queue a buffer from userspace
1261 * @q: videobuf2 queue
1262 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1265 * Should be called from vidioc_qbuf ioctl handler of a driver.
1267 * 1) verifies the passed buffer,
1268 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1269 * which driver-specific buffer initialization can be performed,
1270 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1271 * callback for processing.
1273 * The return values from this function are intended to be directly returned
1274 * from vidioc_qbuf handler in driver.
1276 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1278 struct rw_semaphore *mmap_sem = NULL;
1279 struct vb2_buffer *vb;
1283 * In case of user pointer buffers vb2 allocator needs to get direct
1284 * access to userspace pages. This requires getting read access on
1285 * mmap semaphore in the current process structure. The same
1286 * semaphore is taken before calling mmap operation, while both mmap
1287 * and qbuf are called by the driver or v4l2 core with driver's lock
1288 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1289 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1290 * release driver's lock, takes mmap_sem and then takes again driver's
1293 * To avoid race with other vb2 calls, which might be called after
1294 * releasing driver's lock, this operation is performed at the
1295 * beggining of qbuf processing. This way the queue status is
1296 * consistent after getting driver's lock back.
1298 if (q->memory == V4L2_MEMORY_USERPTR) {
1299 mmap_sem = ¤t->mm->mmap_sem;
1300 call_qop(q, wait_prepare, q);
1301 down_read(mmap_sem);
1302 call_qop(q, wait_finish, q);
1306 dprintk(1, "qbuf: file io in progress\n");
1311 if (b->type != q->type) {
1312 dprintk(1, "qbuf: invalid buffer type\n");
1317 if (b->index >= q->num_buffers) {
1318 dprintk(1, "qbuf: buffer index out of range\n");
1323 vb = q->bufs[b->index];
1325 /* Should never happen */
1326 dprintk(1, "qbuf: buffer is NULL\n");
1331 if (b->memory != q->memory) {
1332 dprintk(1, "qbuf: invalid memory type\n");
1336 ret = __verify_planes_array(vb, b);
1340 switch (vb->state) {
1341 case VB2_BUF_STATE_DEQUEUED:
1342 ret = __buf_prepare(vb, b);
1345 case VB2_BUF_STATE_PREPARED:
1348 dprintk(1, "qbuf: buffer already in use\n");
1354 * Add to the queued buffers list, a buffer will stay on it until
1355 * dequeued in dqbuf.
1357 list_add_tail(&vb->queued_entry, &q->queued_list);
1358 vb->state = VB2_BUF_STATE_QUEUED;
1361 * If already streaming, give the buffer to driver for processing.
1362 * If not, the buffer will be given to driver on next streamon.
1365 __enqueue_in_driver(vb);
1367 /* Fill buffer information for the userspace */
1368 __fill_v4l2_buffer(vb, b);
1370 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1376 EXPORT_SYMBOL_GPL(vb2_qbuf);
1379 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1382 * Will sleep if required for nonblocking == false.
1384 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1387 * All operations on vb_done_list are performed under done_lock
1388 * spinlock protection. However, buffers may be removed from
1389 * it and returned to userspace only while holding both driver's
1390 * lock and the done_lock spinlock. Thus we can be sure that as
1391 * long as we hold the driver's lock, the list will remain not
1392 * empty if list_empty() check succeeds.
1398 if (!q->streaming) {
1399 dprintk(1, "Streaming off, will not wait for buffers\n");
1403 if (!list_empty(&q->done_list)) {
1405 * Found a buffer that we were waiting for.
1411 dprintk(1, "Nonblocking and no buffers to dequeue, "
1417 * We are streaming and blocking, wait for another buffer to
1418 * become ready or for streamoff. Driver's lock is released to
1419 * allow streamoff or qbuf to be called while waiting.
1421 call_qop(q, wait_prepare, q);
1424 * All locks have been released, it is safe to sleep now.
1426 dprintk(3, "Will sleep waiting for buffers\n");
1427 ret = wait_event_interruptible(q->done_wq,
1428 !list_empty(&q->done_list) || !q->streaming);
1431 * We need to reevaluate both conditions again after reacquiring
1432 * the locks or return an error if one occurred.
1434 call_qop(q, wait_finish, q);
1436 dprintk(1, "Sleep was interrupted\n");
1444 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1446 * Will sleep if required for nonblocking == false.
1448 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1449 struct v4l2_buffer *b, int nonblocking)
1451 unsigned long flags;
1455 * Wait for at least one buffer to become available on the done_list.
1457 ret = __vb2_wait_for_done_vb(q, nonblocking);
1462 * Driver's lock has been held since we last verified that done_list
1463 * is not empty, so no need for another list_empty(done_list) check.
1465 spin_lock_irqsave(&q->done_lock, flags);
1466 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1468 * Only remove the buffer from done_list if v4l2_buffer can handle all
1471 ret = __verify_planes_array(*vb, b);
1473 list_del(&(*vb)->done_entry);
1474 spin_unlock_irqrestore(&q->done_lock, flags);
1480 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1481 * @q: videobuf2 queue
1483 * This function will wait until all buffers that have been given to the driver
1484 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1485 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1486 * taken, for example from stop_streaming() callback.
1488 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1490 if (!q->streaming) {
1491 dprintk(1, "Streaming off, will not wait for buffers\n");
1495 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1498 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1501 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1503 static void __vb2_dqbuf(struct vb2_buffer *vb)
1505 struct vb2_queue *q = vb->vb2_queue;
1508 /* nothing to do if the buffer is already dequeued */
1509 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1512 vb->state = VB2_BUF_STATE_DEQUEUED;
1514 /* unmap DMABUF buffer */
1515 if (q->memory == V4L2_MEMORY_DMABUF)
1516 for (i = 0; i < vb->num_planes; ++i) {
1517 if (!vb->planes[i].dbuf_mapped)
1519 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1520 vb->planes[i].dbuf_mapped = 0;
1525 * vb2_dqbuf() - Dequeue a buffer to the userspace
1526 * @q: videobuf2 queue
1527 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1529 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1530 * buffers ready for dequeuing are present. Normally the driver
1531 * would be passing (file->f_flags & O_NONBLOCK) here
1533 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1535 * 1) verifies the passed buffer,
1536 * 2) calls buf_finish callback in the driver (if provided), in which
1537 * driver can perform any additional operations that may be required before
1538 * returning the buffer to userspace, such as cache sync,
1539 * 3) the buffer struct members are filled with relevant information for
1542 * The return values from this function are intended to be directly returned
1543 * from vidioc_dqbuf handler in driver.
1545 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1547 struct vb2_buffer *vb = NULL;
1551 dprintk(1, "dqbuf: file io in progress\n");
1555 if (b->type != q->type) {
1556 dprintk(1, "dqbuf: invalid buffer type\n");
1559 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1563 ret = call_qop(q, buf_finish, vb);
1565 dprintk(1, "dqbuf: buffer finish failed\n");
1569 switch (vb->state) {
1570 case VB2_BUF_STATE_DONE:
1571 dprintk(3, "dqbuf: Returning done buffer\n");
1573 case VB2_BUF_STATE_ERROR:
1574 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1577 dprintk(1, "dqbuf: Invalid buffer state\n");
1581 /* Fill buffer information for the userspace */
1582 __fill_v4l2_buffer(vb, b);
1583 /* Remove from videobuf queue */
1584 list_del(&vb->queued_entry);
1585 /* go back to dequeued state */
1588 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1589 vb->v4l2_buf.index, vb->state);
1593 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1596 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1598 * Removes all queued buffers from driver's queue and all buffers queued by
1599 * userspace from videobuf's queue. Returns to state after reqbufs.
1601 static void __vb2_queue_cancel(struct vb2_queue *q)
1606 * Tell driver to stop all transactions and release all queued
1610 call_qop(q, stop_streaming, q);
1614 * Remove all buffers from videobuf's list...
1616 INIT_LIST_HEAD(&q->queued_list);
1618 * ...and done list; userspace will not receive any buffers it
1619 * has not already dequeued before initiating cancel.
1621 INIT_LIST_HEAD(&q->done_list);
1622 atomic_set(&q->queued_count, 0);
1623 wake_up_all(&q->done_wq);
1626 * Reinitialize all buffers for next use.
1628 for (i = 0; i < q->num_buffers; ++i)
1629 __vb2_dqbuf(q->bufs[i]);
1633 * vb2_streamon - start streaming
1634 * @q: videobuf2 queue
1635 * @type: type argument passed from userspace to vidioc_streamon handler
1637 * Should be called from vidioc_streamon handler of a driver.
1639 * 1) verifies current state
1640 * 2) passes any previously queued buffers to the driver and starts streaming
1642 * The return values from this function are intended to be directly returned
1643 * from vidioc_streamon handler in the driver.
1645 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1647 struct vb2_buffer *vb;
1651 dprintk(1, "streamon: file io in progress\n");
1655 if (type != q->type) {
1656 dprintk(1, "streamon: invalid stream type\n");
1661 dprintk(1, "streamon: already streaming\n");
1666 * If any buffers were queued before streamon,
1667 * we can now pass them to driver for processing.
1669 list_for_each_entry(vb, &q->queued_list, queued_entry)
1670 __enqueue_in_driver(vb);
1673 * Let driver notice that streaming state has been enabled.
1675 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1677 dprintk(1, "streamon: driver refused to start streaming\n");
1678 __vb2_queue_cancel(q);
1684 dprintk(3, "Streamon successful\n");
1687 EXPORT_SYMBOL_GPL(vb2_streamon);
1691 * vb2_streamoff - stop streaming
1692 * @q: videobuf2 queue
1693 * @type: type argument passed from userspace to vidioc_streamoff handler
1695 * Should be called from vidioc_streamoff handler of a driver.
1697 * 1) verifies current state,
1698 * 2) stop streaming and dequeues any queued buffers, including those previously
1699 * passed to the driver (after waiting for the driver to finish).
1701 * This call can be used for pausing playback.
1702 * The return values from this function are intended to be directly returned
1703 * from vidioc_streamoff handler in the driver
1705 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1708 dprintk(1, "streamoff: file io in progress\n");
1712 if (type != q->type) {
1713 dprintk(1, "streamoff: invalid stream type\n");
1717 if (!q->streaming) {
1718 dprintk(1, "streamoff: not streaming\n");
1723 * Cancel will pause streaming and remove all buffers from the driver
1724 * and videobuf, effectively returning control over them to userspace.
1726 __vb2_queue_cancel(q);
1728 dprintk(3, "Streamoff successful\n");
1731 EXPORT_SYMBOL_GPL(vb2_streamoff);
1734 * __find_plane_by_offset() - find plane associated with the given offset off
1736 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1737 unsigned int *_buffer, unsigned int *_plane)
1739 struct vb2_buffer *vb;
1740 unsigned int buffer, plane;
1743 * Go over all buffers and their planes, comparing the given offset
1744 * with an offset assigned to each plane. If a match is found,
1745 * return its buffer and plane numbers.
1747 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1748 vb = q->bufs[buffer];
1750 for (plane = 0; plane < vb->num_planes; ++plane) {
1751 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1763 * vb2_expbuf() - Export a buffer as a file descriptor
1764 * @q: videobuf2 queue
1765 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1768 * The return values from this function are intended to be directly returned
1769 * from vidioc_expbuf handler in driver.
1771 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1773 struct vb2_buffer *vb = NULL;
1774 struct vb2_plane *vb_plane;
1776 struct dma_buf *dbuf;
1778 if (q->memory != V4L2_MEMORY_MMAP) {
1779 dprintk(1, "Queue is not currently set up for mmap\n");
1783 if (!q->mem_ops->get_dmabuf) {
1784 dprintk(1, "Queue does not support DMA buffer exporting\n");
1788 if (eb->flags & ~O_CLOEXEC) {
1789 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1793 if (eb->type != q->type) {
1794 dprintk(1, "qbuf: invalid buffer type\n");
1798 if (eb->index >= q->num_buffers) {
1799 dprintk(1, "buffer index out of range\n");
1803 vb = q->bufs[eb->index];
1805 if (eb->plane >= vb->num_planes) {
1806 dprintk(1, "buffer plane out of range\n");
1810 vb_plane = &vb->planes[eb->plane];
1812 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1813 if (IS_ERR_OR_NULL(dbuf)) {
1814 dprintk(1, "Failed to export buffer %d, plane %d\n",
1815 eb->index, eb->plane);
1819 ret = dma_buf_fd(dbuf, eb->flags);
1821 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1822 eb->index, eb->plane, ret);
1827 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1828 eb->index, eb->plane, ret);
1833 EXPORT_SYMBOL_GPL(vb2_expbuf);
1836 * vb2_mmap() - map video buffers into application address space
1837 * @q: videobuf2 queue
1838 * @vma: vma passed to the mmap file operation handler in the driver
1840 * Should be called from mmap file operation handler of a driver.
1841 * This function maps one plane of one of the available video buffers to
1842 * userspace. To map whole video memory allocated on reqbufs, this function
1843 * has to be called once per each plane per each buffer previously allocated.
1845 * When the userspace application calls mmap, it passes to it an offset returned
1846 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1847 * a "cookie", which is then used to identify the plane to be mapped.
1848 * This function finds a plane with a matching offset and a mapping is performed
1849 * by the means of a provided memory operation.
1851 * The return values from this function are intended to be directly returned
1852 * from the mmap handler in driver.
1854 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1856 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1857 struct vb2_buffer *vb;
1858 unsigned int buffer, plane;
1860 unsigned long length;
1862 if (q->memory != V4L2_MEMORY_MMAP) {
1863 dprintk(1, "Queue is not currently set up for mmap\n");
1868 * Check memory area access mode.
1870 if (!(vma->vm_flags & VM_SHARED)) {
1871 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1874 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1875 if (!(vma->vm_flags & VM_WRITE)) {
1876 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1880 if (!(vma->vm_flags & VM_READ)) {
1881 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1887 * Find the plane corresponding to the offset passed by userspace.
1889 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1893 vb = q->bufs[buffer];
1896 * MMAP requires page_aligned buffers.
1897 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1898 * so, we need to do the same here.
1900 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1901 if (length < (vma->vm_end - vma->vm_start)) {
1903 "MMAP invalid, as it would overflow buffer length\n");
1907 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
1911 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1914 EXPORT_SYMBOL_GPL(vb2_mmap);
1917 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1920 unsigned long pgoff,
1921 unsigned long flags)
1923 unsigned long off = pgoff << PAGE_SHIFT;
1924 struct vb2_buffer *vb;
1925 unsigned int buffer, plane;
1928 if (q->memory != V4L2_MEMORY_MMAP) {
1929 dprintk(1, "Queue is not currently set up for mmap\n");
1934 * Find the plane corresponding to the offset passed by userspace.
1936 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1940 vb = q->bufs[buffer];
1942 return (unsigned long)vb2_plane_vaddr(vb, plane);
1944 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1947 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1948 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1951 * vb2_poll() - implements poll userspace operation
1952 * @q: videobuf2 queue
1953 * @file: file argument passed to the poll file operation handler
1954 * @wait: wait argument passed to the poll file operation handler
1956 * This function implements poll file operation handler for a driver.
1957 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1958 * be informed that the file descriptor of a video device is available for
1960 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1961 * will be reported as available for writing.
1963 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1966 * The return values from this function are intended to be directly returned
1967 * from poll handler in driver.
1969 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1971 struct video_device *vfd = video_devdata(file);
1972 unsigned long req_events = poll_requested_events(wait);
1973 struct vb2_buffer *vb = NULL;
1974 unsigned int res = 0;
1975 unsigned long flags;
1977 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1978 struct v4l2_fh *fh = file->private_data;
1980 if (v4l2_event_pending(fh))
1982 else if (req_events & POLLPRI)
1983 poll_wait(file, &fh->wait, wait);
1986 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
1988 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
1992 * Start file I/O emulator only if streaming API has not been used yet.
1994 if (q->num_buffers == 0 && q->fileio == NULL) {
1995 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1996 (req_events & (POLLIN | POLLRDNORM))) {
1997 if (__vb2_init_fileio(q, 1))
1998 return res | POLLERR;
2000 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2001 (req_events & (POLLOUT | POLLWRNORM))) {
2002 if (__vb2_init_fileio(q, 0))
2003 return res | POLLERR;
2005 * Write to OUTPUT queue can be done immediately.
2007 return res | POLLOUT | POLLWRNORM;
2012 * There is nothing to wait for if no buffers have already been queued.
2014 if (list_empty(&q->queued_list))
2015 return res | POLLERR;
2017 if (list_empty(&q->done_list))
2018 poll_wait(file, &q->done_wq, wait);
2021 * Take first buffer available for dequeuing.
2023 spin_lock_irqsave(&q->done_lock, flags);
2024 if (!list_empty(&q->done_list))
2025 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2027 spin_unlock_irqrestore(&q->done_lock, flags);
2029 if (vb && (vb->state == VB2_BUF_STATE_DONE
2030 || vb->state == VB2_BUF_STATE_ERROR)) {
2031 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2032 res | POLLOUT | POLLWRNORM :
2033 res | POLLIN | POLLRDNORM;
2037 EXPORT_SYMBOL_GPL(vb2_poll);
2040 * vb2_queue_init() - initialize a videobuf2 queue
2041 * @q: videobuf2 queue; this structure should be allocated in driver
2043 * The vb2_queue structure should be allocated by the driver. The driver is
2044 * responsible of clearing it's content and setting initial values for some
2045 * required entries before calling this function.
2046 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2047 * to the struct vb2_queue description in include/media/videobuf2-core.h
2048 * for more information.
2050 int vb2_queue_init(struct vb2_queue *q)
2057 WARN_ON(!q->mem_ops) ||
2058 WARN_ON(!q->type) ||
2059 WARN_ON(!q->io_modes) ||
2060 WARN_ON(!q->ops->queue_setup) ||
2061 WARN_ON(!q->ops->buf_queue) ||
2062 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
2065 /* Warn that the driver should choose an appropriate timestamp type */
2066 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2068 INIT_LIST_HEAD(&q->queued_list);
2069 INIT_LIST_HEAD(&q->done_list);
2070 spin_lock_init(&q->done_lock);
2071 init_waitqueue_head(&q->done_wq);
2073 if (q->buf_struct_size == 0)
2074 q->buf_struct_size = sizeof(struct vb2_buffer);
2078 EXPORT_SYMBOL_GPL(vb2_queue_init);
2081 * vb2_queue_release() - stop streaming, release the queue and free memory
2082 * @q: videobuf2 queue
2084 * This function stops streaming and performs necessary clean ups, including
2085 * freeing video buffer memory. The driver is responsible for freeing
2086 * the vb2_queue structure itself.
2088 void vb2_queue_release(struct vb2_queue *q)
2090 __vb2_cleanup_fileio(q);
2091 __vb2_queue_cancel(q);
2092 __vb2_queue_free(q, q->num_buffers);
2094 EXPORT_SYMBOL_GPL(vb2_queue_release);
2097 * struct vb2_fileio_buf - buffer context used by file io emulator
2099 * vb2 provides a compatibility layer and emulator of file io (read and
2100 * write) calls on top of streaming API. This structure is used for
2101 * tracking context related to the buffers.
2103 struct vb2_fileio_buf {
2107 unsigned int queued:1;
2111 * struct vb2_fileio_data - queue context used by file io emulator
2113 * vb2 provides a compatibility layer and emulator of file io (read and
2114 * write) calls on top of streaming API. For proper operation it required
2115 * this structure to save the driver state between each call of the read
2116 * or write function.
2118 struct vb2_fileio_data {
2119 struct v4l2_requestbuffers req;
2120 struct v4l2_buffer b;
2121 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2123 unsigned int q_count;
2124 unsigned int dq_count;
2129 * __vb2_init_fileio() - initialize file io emulator
2130 * @q: videobuf2 queue
2131 * @read: mode selector (1 means read, 0 means write)
2133 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2135 struct vb2_fileio_data *fileio;
2137 unsigned int count = 0;
2142 if ((read && !(q->io_modes & VB2_READ)) ||
2143 (!read && !(q->io_modes & VB2_WRITE)))
2147 * Check if device supports mapping buffers to kernel virtual space.
2149 if (!q->mem_ops->vaddr)
2153 * Check if streaming api has not been already activated.
2155 if (q->streaming || q->num_buffers > 0)
2159 * Start with count 1, driver can increase it in queue_setup()
2163 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2164 (read) ? "read" : "write", count, q->io_flags);
2166 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2170 fileio->flags = q->io_flags;
2173 * Request buffers and use MMAP type to force driver
2174 * to allocate buffers by itself.
2176 fileio->req.count = count;
2177 fileio->req.memory = V4L2_MEMORY_MMAP;
2178 fileio->req.type = q->type;
2179 ret = vb2_reqbufs(q, &fileio->req);
2184 * Check if plane_count is correct
2185 * (multiplane buffers are not supported).
2187 if (q->bufs[0]->num_planes != 1) {
2193 * Get kernel address of each buffer.
2195 for (i = 0; i < q->num_buffers; i++) {
2196 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2197 if (fileio->bufs[i].vaddr == NULL) {
2201 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2205 * Read mode requires pre queuing of all buffers.
2209 * Queue all buffers.
2211 for (i = 0; i < q->num_buffers; i++) {
2212 struct v4l2_buffer *b = &fileio->b;
2213 memset(b, 0, sizeof(*b));
2215 b->memory = q->memory;
2217 ret = vb2_qbuf(q, b);
2220 fileio->bufs[i].queued = 1;
2226 ret = vb2_streamon(q, q->type);
2236 fileio->req.count = 0;
2237 vb2_reqbufs(q, &fileio->req);
2245 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2246 * @q: videobuf2 queue
2248 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2250 struct vb2_fileio_data *fileio = q->fileio;
2254 * Hack fileio context to enable direct calls to vb2 ioctl
2259 vb2_streamoff(q, q->type);
2260 fileio->req.count = 0;
2261 vb2_reqbufs(q, &fileio->req);
2263 dprintk(3, "file io emulator closed\n");
2269 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2270 * @q: videobuf2 queue
2271 * @data: pointed to target userspace buffer
2272 * @count: number of bytes to read or write
2273 * @ppos: file handle position tracking pointer
2274 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2275 * @read: access mode selector (1 means read, 0 means write)
2277 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2278 loff_t *ppos, int nonblock, int read)
2280 struct vb2_fileio_data *fileio;
2281 struct vb2_fileio_buf *buf;
2284 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2285 read ? "read" : "write", (long)*ppos, count,
2286 nonblock ? "non" : "");
2292 * Initialize emulator on first call.
2295 ret = __vb2_init_fileio(q, read);
2296 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2303 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2304 * The pointer will be restored before returning from this function.
2308 index = fileio->index;
2309 buf = &fileio->bufs[index];
2312 * Check if we need to dequeue the buffer.
2315 struct vb2_buffer *vb;
2318 * Call vb2_dqbuf to get buffer back.
2320 memset(&fileio->b, 0, sizeof(fileio->b));
2321 fileio->b.type = q->type;
2322 fileio->b.memory = q->memory;
2323 fileio->b.index = index;
2324 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2325 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2328 fileio->dq_count += 1;
2331 * Get number of bytes filled by the driver
2333 vb = q->bufs[index];
2334 buf->size = vb2_get_plane_payload(vb, 0);
2339 * Limit count on last few bytes of the buffer.
2341 if (buf->pos + count > buf->size) {
2342 count = buf->size - buf->pos;
2343 dprintk(5, "reducing read count: %zd\n", count);
2347 * Transfer data to userspace.
2349 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2350 count, index, buf->pos);
2352 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2354 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2356 dprintk(3, "file io: error copying data\n");
2368 * Queue next buffer if required.
2370 if (buf->pos == buf->size ||
2371 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2373 * Check if this is the last buffer to read.
2375 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2376 fileio->dq_count == 1) {
2377 dprintk(3, "file io: read limit reached\n");
2379 * Restore fileio pointer and release the context.
2382 return __vb2_cleanup_fileio(q);
2386 * Call vb2_qbuf and give buffer to the driver.
2388 memset(&fileio->b, 0, sizeof(fileio->b));
2389 fileio->b.type = q->type;
2390 fileio->b.memory = q->memory;
2391 fileio->b.index = index;
2392 fileio->b.bytesused = buf->pos;
2393 ret = vb2_qbuf(q, &fileio->b);
2394 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2399 * Buffer has been queued, update the status
2403 buf->size = q->bufs[0]->v4l2_planes[0].length;
2404 fileio->q_count += 1;
2407 * Switch to the next buffer
2409 fileio->index = (index + 1) % q->num_buffers;
2412 * Start streaming if required.
2414 if (!read && !q->streaming) {
2415 ret = vb2_streamon(q, q->type);
2422 * Return proper number of bytes processed.
2428 * Restore the fileio context and block vb2 ioctl interface.
2434 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2435 loff_t *ppos, int nonblocking)
2437 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2439 EXPORT_SYMBOL_GPL(vb2_read);
2441 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2442 loff_t *ppos, int nonblocking)
2444 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2446 EXPORT_SYMBOL_GPL(vb2_write);
2450 * The following functions are not part of the vb2 core API, but are helper
2451 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2452 * and struct vb2_ops.
2453 * They contain boilerplate code that most if not all drivers have to do
2454 * and so they simplify the driver code.
2457 /* The queue is busy if there is a owner and you are not that owner. */
2458 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2460 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2463 /* vb2 ioctl helpers */
2465 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2466 struct v4l2_requestbuffers *p)
2468 struct video_device *vdev = video_devdata(file);
2469 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2473 if (vb2_queue_is_busy(vdev, file))
2475 res = __reqbufs(vdev->queue, p);
2476 /* If count == 0, then the owner has released all buffers and he
2477 is no longer owner of the queue. Otherwise we have a new owner. */
2479 vdev->queue->owner = p->count ? file->private_data : NULL;
2482 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2484 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2485 struct v4l2_create_buffers *p)
2487 struct video_device *vdev = video_devdata(file);
2488 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2490 p->index = vdev->queue->num_buffers;
2491 /* If count == 0, then just check if memory and type are valid.
2492 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2494 return res != -EBUSY ? res : 0;
2497 if (vb2_queue_is_busy(vdev, file))
2499 res = __create_bufs(vdev->queue, p);
2501 vdev->queue->owner = file->private_data;
2504 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2506 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2507 struct v4l2_buffer *p)
2509 struct video_device *vdev = video_devdata(file);
2511 if (vb2_queue_is_busy(vdev, file))
2513 return vb2_prepare_buf(vdev->queue, p);
2515 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2517 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2519 struct video_device *vdev = video_devdata(file);
2521 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2522 return vb2_querybuf(vdev->queue, p);
2524 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2526 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2528 struct video_device *vdev = video_devdata(file);
2530 if (vb2_queue_is_busy(vdev, file))
2532 return vb2_qbuf(vdev->queue, p);
2534 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2536 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2538 struct video_device *vdev = video_devdata(file);
2540 if (vb2_queue_is_busy(vdev, file))
2542 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2544 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2546 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2548 struct video_device *vdev = video_devdata(file);
2550 if (vb2_queue_is_busy(vdev, file))
2552 return vb2_streamon(vdev->queue, i);
2554 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2556 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2558 struct video_device *vdev = video_devdata(file);
2560 if (vb2_queue_is_busy(vdev, file))
2562 return vb2_streamoff(vdev->queue, i);
2564 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2566 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2568 struct video_device *vdev = video_devdata(file);
2570 if (vb2_queue_is_busy(vdev, file))
2572 return vb2_expbuf(vdev->queue, p);
2574 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2576 /* v4l2_file_operations helpers */
2578 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2580 struct video_device *vdev = video_devdata(file);
2582 return vb2_mmap(vdev->queue, vma);
2584 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2586 int vb2_fop_release(struct file *file)
2588 struct video_device *vdev = video_devdata(file);
2590 if (file->private_data == vdev->queue->owner) {
2591 vb2_queue_release(vdev->queue);
2592 vdev->queue->owner = NULL;
2594 return v4l2_fh_release(file);
2596 EXPORT_SYMBOL_GPL(vb2_fop_release);
2598 ssize_t vb2_fop_write(struct file *file, char __user *buf,
2599 size_t count, loff_t *ppos)
2601 struct video_device *vdev = video_devdata(file);
2602 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2605 if (lock && mutex_lock_interruptible(lock))
2606 return -ERESTARTSYS;
2607 if (vb2_queue_is_busy(vdev, file))
2609 err = vb2_write(vdev->queue, buf, count, ppos,
2610 file->f_flags & O_NONBLOCK);
2611 if (vdev->queue->fileio)
2612 vdev->queue->owner = file->private_data;
2618 EXPORT_SYMBOL_GPL(vb2_fop_write);
2620 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2621 size_t count, loff_t *ppos)
2623 struct video_device *vdev = video_devdata(file);
2624 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2627 if (lock && mutex_lock_interruptible(lock))
2628 return -ERESTARTSYS;
2629 if (vb2_queue_is_busy(vdev, file))
2631 err = vb2_read(vdev->queue, buf, count, ppos,
2632 file->f_flags & O_NONBLOCK);
2633 if (vdev->queue->fileio)
2634 vdev->queue->owner = file->private_data;
2640 EXPORT_SYMBOL_GPL(vb2_fop_read);
2642 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2644 struct video_device *vdev = video_devdata(file);
2645 struct vb2_queue *q = vdev->queue;
2646 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2647 unsigned long req_events = poll_requested_events(wait);
2650 bool must_lock = false;
2652 /* Try to be smart: only lock if polling might start fileio,
2653 otherwise locking will only introduce unwanted delays. */
2654 if (q->num_buffers == 0 && q->fileio == NULL) {
2655 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2656 (req_events & (POLLIN | POLLRDNORM)))
2658 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2659 (req_events & (POLLOUT | POLLWRNORM)))
2663 /* If locking is needed, but this helper doesn't know how, then you
2664 shouldn't be using this helper but you should write your own. */
2665 WARN_ON(must_lock && !lock);
2667 if (must_lock && lock && mutex_lock_interruptible(lock))
2672 res = vb2_poll(vdev->queue, file, wait);
2674 /* If fileio was started, then we have a new queue owner. */
2675 if (must_lock && !fileio && q->fileio)
2676 q->owner = file->private_data;
2677 if (must_lock && lock)
2681 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2684 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2685 unsigned long len, unsigned long pgoff, unsigned long flags)
2687 struct video_device *vdev = video_devdata(file);
2689 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2691 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2694 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2696 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2698 mutex_unlock(vq->lock);
2700 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2702 void vb2_ops_wait_finish(struct vb2_queue *vq)
2704 mutex_lock(vq->lock);
2706 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2708 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2709 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2710 MODULE_LICENSE("GPL");