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_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
44 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
45 V4L2_BUF_FLAG_PREPARED)
48 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
52 struct vb2_queue *q = vb->vb2_queue;
56 /* Allocate memory for all planes in this buffer */
57 for (plane = 0; plane < vb->num_planes; ++plane) {
58 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
59 q->plane_sizes[plane]);
60 if (IS_ERR_OR_NULL(mem_priv))
63 /* Associate allocator private data with this plane */
64 vb->planes[plane].mem_priv = mem_priv;
65 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
70 /* Free already allocated memory if one of the allocations failed */
71 for (; plane > 0; --plane) {
72 call_memop(q, put, vb->planes[plane - 1].mem_priv);
73 vb->planes[plane - 1].mem_priv = NULL;
80 * __vb2_buf_mem_free() - free memory of the given buffer
82 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
84 struct vb2_queue *q = vb->vb2_queue;
87 for (plane = 0; plane < vb->num_planes; ++plane) {
88 call_memop(q, put, vb->planes[plane].mem_priv);
89 vb->planes[plane].mem_priv = NULL;
90 dprintk(3, "Freed plane %d of buffer %d\n", plane,
96 * __vb2_buf_userptr_put() - release userspace memory associated with
99 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
101 struct vb2_queue *q = vb->vb2_queue;
104 for (plane = 0; plane < vb->num_planes; ++plane) {
105 if (vb->planes[plane].mem_priv)
106 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
107 vb->planes[plane].mem_priv = NULL;
112 * __vb2_plane_dmabuf_put() - release memory associated with
113 * a DMABUF shared plane
115 static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
121 call_memop(q, unmap_dmabuf, p->mem_priv);
123 call_memop(q, detach_dmabuf, p->mem_priv);
124 dma_buf_put(p->dbuf);
125 memset(p, 0, sizeof(*p));
129 * __vb2_buf_dmabuf_put() - release memory associated with
130 * a DMABUF shared buffer
132 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
134 struct vb2_queue *q = vb->vb2_queue;
137 for (plane = 0; plane < vb->num_planes; ++plane)
138 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
142 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
143 * every buffer on the queue
145 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
147 unsigned int buffer, plane;
148 struct vb2_buffer *vb;
151 if (q->num_buffers) {
152 struct v4l2_plane *p;
153 vb = q->bufs[q->num_buffers - 1];
154 p = &vb->v4l2_planes[vb->num_planes - 1];
155 off = PAGE_ALIGN(p->m.mem_offset + p->length);
160 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
161 vb = q->bufs[buffer];
165 for (plane = 0; plane < vb->num_planes; ++plane) {
166 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
167 vb->v4l2_planes[plane].m.mem_offset = off;
169 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
172 off += vb->v4l2_planes[plane].length;
173 off = PAGE_ALIGN(off);
179 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
180 * video buffer memory for all buffers/planes on the queue and initializes the
183 * Returns the number of buffers successfully allocated.
185 static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
186 unsigned int num_buffers, unsigned int num_planes)
189 struct vb2_buffer *vb;
192 for (buffer = 0; buffer < num_buffers; ++buffer) {
193 /* Allocate videobuf buffer structures */
194 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
196 dprintk(1, "Memory alloc for buffer struct failed\n");
200 /* Length stores number of planes for multiplanar buffers */
201 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
202 vb->v4l2_buf.length = num_planes;
204 vb->state = VB2_BUF_STATE_DEQUEUED;
206 vb->num_planes = num_planes;
207 vb->v4l2_buf.index = q->num_buffers + buffer;
208 vb->v4l2_buf.type = q->type;
209 vb->v4l2_buf.memory = memory;
211 /* Allocate video buffer memory for the MMAP type */
212 if (memory == V4L2_MEMORY_MMAP) {
213 ret = __vb2_buf_mem_alloc(vb);
215 dprintk(1, "Failed allocating memory for "
216 "buffer %d\n", buffer);
221 * Call the driver-provided buffer initialization
222 * callback, if given. An error in initialization
223 * results in queue setup failure.
225 ret = call_qop(q, buf_init, vb);
227 dprintk(1, "Buffer %d %p initialization"
228 " failed\n", buffer, vb);
229 __vb2_buf_mem_free(vb);
235 q->bufs[q->num_buffers + buffer] = vb;
238 __setup_offsets(q, buffer);
240 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
247 * __vb2_free_mem() - release all video buffer memory for a given queue
249 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
252 struct vb2_buffer *vb;
254 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
256 vb = q->bufs[buffer];
260 /* Free MMAP buffers or release USERPTR buffers */
261 if (q->memory == V4L2_MEMORY_MMAP)
262 __vb2_buf_mem_free(vb);
263 else if (q->memory == V4L2_MEMORY_DMABUF)
264 __vb2_buf_dmabuf_put(vb);
266 __vb2_buf_userptr_put(vb);
271 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
272 * related information, if no buffers are left return the queue to an
273 * uninitialized state. Might be called even if the queue has already been freed.
275 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
279 /* Call driver-provided cleanup function for each buffer, if provided */
280 if (q->ops->buf_cleanup) {
281 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
283 if (NULL == q->bufs[buffer])
285 q->ops->buf_cleanup(q->bufs[buffer]);
289 /* Release video buffer memory */
290 __vb2_free_mem(q, buffers);
292 /* Free videobuf buffers */
293 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
295 kfree(q->bufs[buffer]);
296 q->bufs[buffer] = NULL;
299 q->num_buffers -= buffers;
302 INIT_LIST_HEAD(&q->queued_list);
306 * __verify_planes_array() - verify that the planes array passed in struct
307 * v4l2_buffer from userspace can be safely used
309 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
311 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
314 /* Is memory for copying plane information present? */
315 if (NULL == b->m.planes) {
316 dprintk(1, "Multi-planar buffer passed but "
317 "planes array not provided\n");
321 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
322 dprintk(1, "Incorrect planes array length, "
323 "expected %d, got %d\n", vb->num_planes, b->length);
331 * __buffer_in_use() - return true if the buffer is in use and
332 * the queue cannot be freed (by the means of REQBUFS(0)) call
334 static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
337 for (plane = 0; plane < vb->num_planes; ++plane) {
338 void *mem_priv = vb->planes[plane].mem_priv;
340 * If num_users() has not been provided, call_memop
341 * will return 0, apparently nobody cares about this
342 * case anyway. If num_users() returns more than 1,
343 * we are not the only user of the plane's memory.
345 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
352 * __buffers_in_use() - return true if any buffers on the queue are in use and
353 * the queue cannot be freed (by the means of REQBUFS(0)) call
355 static bool __buffers_in_use(struct vb2_queue *q)
358 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
359 if (__buffer_in_use(q, q->bufs[buffer]))
366 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
367 * returned to userspace
369 static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
371 struct vb2_queue *q = vb->vb2_queue;
373 /* Copy back data such as timestamp, flags, etc. */
374 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
375 b->reserved2 = vb->v4l2_buf.reserved2;
376 b->reserved = vb->v4l2_buf.reserved;
378 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
380 * Fill in plane-related data if userspace provided an array
381 * for it. The caller has already verified memory and size.
383 b->length = vb->num_planes;
384 memcpy(b->m.planes, vb->v4l2_planes,
385 b->length * sizeof(struct v4l2_plane));
388 * We use length and offset in v4l2_planes array even for
389 * single-planar buffers, but userspace does not.
391 b->length = vb->v4l2_planes[0].length;
392 b->bytesused = vb->v4l2_planes[0].bytesused;
393 if (q->memory == V4L2_MEMORY_MMAP)
394 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
395 else if (q->memory == V4L2_MEMORY_USERPTR)
396 b->m.userptr = vb->v4l2_planes[0].m.userptr;
397 else if (q->memory == V4L2_MEMORY_DMABUF)
398 b->m.fd = vb->v4l2_planes[0].m.fd;
402 * Clear any buffer state related flags.
404 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
407 case VB2_BUF_STATE_QUEUED:
408 case VB2_BUF_STATE_ACTIVE:
409 b->flags |= V4L2_BUF_FLAG_QUEUED;
411 case VB2_BUF_STATE_ERROR:
412 b->flags |= V4L2_BUF_FLAG_ERROR;
414 case VB2_BUF_STATE_DONE:
415 b->flags |= V4L2_BUF_FLAG_DONE;
417 case VB2_BUF_STATE_PREPARED:
418 b->flags |= V4L2_BUF_FLAG_PREPARED;
420 case VB2_BUF_STATE_DEQUEUED:
425 if (__buffer_in_use(q, vb))
426 b->flags |= V4L2_BUF_FLAG_MAPPED;
430 * vb2_querybuf() - query video buffer information
432 * @b: buffer struct passed from userspace to vidioc_querybuf handler
435 * Should be called from vidioc_querybuf ioctl handler in driver.
436 * This function will verify the passed v4l2_buffer structure and fill the
437 * relevant information for the userspace.
439 * The return values from this function are intended to be directly returned
440 * from vidioc_querybuf handler in driver.
442 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
444 struct vb2_buffer *vb;
447 if (b->type != q->type) {
448 dprintk(1, "querybuf: wrong buffer type\n");
452 if (b->index >= q->num_buffers) {
453 dprintk(1, "querybuf: buffer index out of range\n");
456 vb = q->bufs[b->index];
457 ret = __verify_planes_array(vb, b);
459 __fill_v4l2_buffer(vb, b);
462 EXPORT_SYMBOL(vb2_querybuf);
465 * __verify_userptr_ops() - verify that all memory operations required for
466 * USERPTR queue type have been provided
468 static int __verify_userptr_ops(struct vb2_queue *q)
470 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
471 !q->mem_ops->put_userptr)
478 * __verify_mmap_ops() - verify that all memory operations required for
479 * MMAP queue type have been provided
481 static int __verify_mmap_ops(struct vb2_queue *q)
483 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
484 !q->mem_ops->put || !q->mem_ops->mmap)
491 * __verify_dmabuf_ops() - verify that all memory operations required for
492 * DMABUF queue type have been provided
494 static int __verify_dmabuf_ops(struct vb2_queue *q)
496 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
497 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
498 !q->mem_ops->unmap_dmabuf)
505 * __verify_memory_type() - Check whether the memory type and buffer type
506 * passed to a buffer operation are compatible with the queue.
508 static int __verify_memory_type(struct vb2_queue *q,
509 enum v4l2_memory memory, enum v4l2_buf_type type)
511 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
512 memory != V4L2_MEMORY_DMABUF) {
513 dprintk(1, "reqbufs: unsupported memory type\n");
517 if (type != q->type) {
518 dprintk(1, "reqbufs: requested type is incorrect\n");
523 * Make sure all the required memory ops for given memory type
526 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
527 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
531 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
532 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
536 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
537 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
542 * Place the busy tests at the end: -EBUSY can be ignored when
543 * create_bufs is called with count == 0, but count == 0 should still
544 * do the memory and type validation.
547 dprintk(1, "reqbufs: file io in progress\n");
554 * __reqbufs() - Initiate streaming
555 * @q: videobuf2 queue
556 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
558 * Should be called from vidioc_reqbufs ioctl handler of a driver.
560 * 1) verifies streaming parameters passed from the userspace,
561 * 2) sets up the queue,
562 * 3) negotiates number of buffers and planes per buffer with the driver
563 * to be used during streaming,
564 * 4) allocates internal buffer structures (struct vb2_buffer), according to
565 * the agreed parameters,
566 * 5) for MMAP memory type, allocates actual video memory, using the
567 * memory handling/allocation routines provided during queue initialization
569 * If req->count is 0, all the memory will be freed instead.
570 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
571 * and the queue is not busy, memory will be reallocated.
573 * The return values from this function are intended to be directly returned
574 * from vidioc_reqbufs handler in driver.
576 static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
578 unsigned int num_buffers, allocated_buffers, num_planes = 0;
582 dprintk(1, "reqbufs: streaming active\n");
586 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
588 * We already have buffers allocated, so first check if they
589 * are not in use and can be freed.
591 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
592 dprintk(1, "reqbufs: memory in use, cannot free\n");
596 __vb2_queue_free(q, q->num_buffers);
599 * In case of REQBUFS(0) return immediately without calling
600 * driver's queue_setup() callback and allocating resources.
607 * Make sure the requested values and current defaults are sane.
609 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
610 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
611 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
612 q->memory = req->memory;
615 * Ask the driver how many buffers and planes per buffer it requires.
616 * Driver also sets the size and allocator context for each plane.
618 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
619 q->plane_sizes, q->alloc_ctx);
623 /* Finally, allocate buffers and video memory */
624 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
626 dprintk(1, "Memory allocation failed\n");
630 allocated_buffers = ret;
633 * Check if driver can handle the allocated number of buffers.
635 if (allocated_buffers < num_buffers) {
636 num_buffers = allocated_buffers;
638 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
639 &num_planes, q->plane_sizes, q->alloc_ctx);
641 if (!ret && allocated_buffers < num_buffers)
645 * Either the driver has accepted a smaller number of buffers,
646 * or .queue_setup() returned an error
650 q->num_buffers = allocated_buffers;
653 __vb2_queue_free(q, allocated_buffers);
658 * Return the number of successfully allocated buffers
661 req->count = allocated_buffers;
667 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
669 * @q: videobuf2 queue
670 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
672 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
674 int ret = __verify_memory_type(q, req->memory, req->type);
676 return ret ? ret : __reqbufs(q, req);
678 EXPORT_SYMBOL_GPL(vb2_reqbufs);
681 * __create_bufs() - Allocate buffers and any required auxiliary structs
682 * @q: videobuf2 queue
683 * @create: creation parameters, passed from userspace to vidioc_create_bufs
686 * Should be called from vidioc_create_bufs ioctl handler of a driver.
688 * 1) verifies parameter sanity
689 * 2) calls the .queue_setup() queue operation
690 * 3) performs any necessary memory allocations
692 * The return values from this function are intended to be directly returned
693 * from vidioc_create_bufs handler in driver.
695 static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
697 unsigned int num_planes = 0, num_buffers, allocated_buffers;
700 if (q->num_buffers == VIDEO_MAX_FRAME) {
701 dprintk(1, "%s(): maximum number of buffers already allocated\n",
706 if (!q->num_buffers) {
707 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
708 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
709 q->memory = create->memory;
712 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
715 * Ask the driver, whether the requested number of buffers, planes per
716 * buffer and their sizes are acceptable
718 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
719 &num_planes, q->plane_sizes, q->alloc_ctx);
723 /* Finally, allocate buffers and video memory */
724 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
727 dprintk(1, "Memory allocation failed\n");
731 allocated_buffers = ret;
734 * Check if driver can handle the so far allocated number of buffers.
736 if (ret < num_buffers) {
740 * q->num_buffers contains the total number of buffers, that the
741 * queue driver has set up
743 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
744 &num_planes, q->plane_sizes, q->alloc_ctx);
746 if (!ret && allocated_buffers < num_buffers)
750 * Either the driver has accepted a smaller number of buffers,
751 * or .queue_setup() returned an error
755 q->num_buffers += allocated_buffers;
758 __vb2_queue_free(q, allocated_buffers);
763 * Return the number of successfully allocated buffers
766 create->count = allocated_buffers;
772 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
773 * memory and type values.
774 * @q: videobuf2 queue
775 * @create: creation parameters, passed from userspace to vidioc_create_bufs
778 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
780 int ret = __verify_memory_type(q, create->memory, create->format.type);
782 create->index = q->num_buffers;
783 if (create->count == 0)
784 return ret != -EBUSY ? ret : 0;
785 return ret ? ret : __create_bufs(q, create);
787 EXPORT_SYMBOL_GPL(vb2_create_bufs);
790 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
791 * @vb: vb2_buffer to which the plane in question belongs to
792 * @plane_no: plane number for which the address is to be returned
794 * This function returns a kernel virtual address of a given plane if
795 * such a mapping exist, NULL otherwise.
797 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
799 struct vb2_queue *q = vb->vb2_queue;
801 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
804 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
807 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
810 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
811 * @vb: vb2_buffer to which the plane in question belongs to
812 * @plane_no: plane number for which the cookie is to be returned
814 * This function returns an allocator specific cookie for a given plane if
815 * available, NULL otherwise. The allocator should provide some simple static
816 * inline function, which would convert this cookie to the allocator specific
817 * type that can be used directly by the driver to access the buffer. This can
818 * be for example physical address, pointer to scatter list or IOMMU mapping.
820 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
822 struct vb2_queue *q = vb->vb2_queue;
824 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
827 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
829 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
832 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
833 * @vb: vb2_buffer returned from the driver
834 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
835 * or VB2_BUF_STATE_ERROR if the operation finished with an error
837 * This function should be called by the driver after a hardware operation on
838 * a buffer is finished and the buffer may be returned to userspace. The driver
839 * cannot use this buffer anymore until it is queued back to it by videobuf
840 * by the means of buf_queue callback. Only buffers previously queued to the
841 * driver by buf_queue can be passed to this function.
843 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
845 struct vb2_queue *q = vb->vb2_queue;
849 if (vb->state != VB2_BUF_STATE_ACTIVE)
852 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
855 dprintk(4, "Done processing on buffer %d, state: %d\n",
856 vb->v4l2_buf.index, vb->state);
859 for (plane = 0; plane < vb->num_planes; ++plane)
860 call_memop(q, finish, vb->planes[plane].mem_priv);
862 /* Add the buffer to the done buffers list */
863 spin_lock_irqsave(&q->done_lock, flags);
865 list_add_tail(&vb->done_entry, &q->done_list);
866 atomic_dec(&q->queued_count);
867 spin_unlock_irqrestore(&q->done_lock, flags);
869 /* Inform any processes that may be waiting for buffers */
870 wake_up(&q->done_wq);
872 EXPORT_SYMBOL_GPL(vb2_buffer_done);
875 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
876 * v4l2_buffer by the userspace. The caller has already verified that struct
877 * v4l2_buffer has a valid number of planes.
879 static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
880 struct v4l2_plane *v4l2_planes)
884 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
885 /* Fill in driver-provided information for OUTPUT types */
886 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
888 * Will have to go up to b->length when API starts
889 * accepting variable number of planes.
891 for (plane = 0; plane < vb->num_planes; ++plane) {
892 v4l2_planes[plane].bytesused =
893 b->m.planes[plane].bytesused;
894 v4l2_planes[plane].data_offset =
895 b->m.planes[plane].data_offset;
899 if (b->memory == V4L2_MEMORY_USERPTR) {
900 for (plane = 0; plane < vb->num_planes; ++plane) {
901 v4l2_planes[plane].m.userptr =
902 b->m.planes[plane].m.userptr;
903 v4l2_planes[plane].length =
904 b->m.planes[plane].length;
907 if (b->memory == V4L2_MEMORY_DMABUF) {
908 for (plane = 0; plane < vb->num_planes; ++plane) {
909 v4l2_planes[plane].m.fd =
910 b->m.planes[plane].m.fd;
911 v4l2_planes[plane].length =
912 b->m.planes[plane].length;
913 v4l2_planes[plane].data_offset =
914 b->m.planes[plane].data_offset;
919 * Single-planar buffers do not use planes array,
920 * so fill in relevant v4l2_buffer struct fields instead.
921 * In videobuf we use our internal V4l2_planes struct for
922 * single-planar buffers as well, for simplicity.
924 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
925 v4l2_planes[0].bytesused = b->bytesused;
926 v4l2_planes[0].data_offset = 0;
929 if (b->memory == V4L2_MEMORY_USERPTR) {
930 v4l2_planes[0].m.userptr = b->m.userptr;
931 v4l2_planes[0].length = b->length;
934 if (b->memory == V4L2_MEMORY_DMABUF) {
935 v4l2_planes[0].m.fd = b->m.fd;
936 v4l2_planes[0].length = b->length;
937 v4l2_planes[0].data_offset = 0;
942 vb->v4l2_buf.field = b->field;
943 vb->v4l2_buf.timestamp = b->timestamp;
944 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
948 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
950 static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
952 struct v4l2_plane planes[VIDEO_MAX_PLANES];
953 struct vb2_queue *q = vb->vb2_queue;
957 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
959 /* Copy relevant information provided by the userspace */
960 __fill_vb2_buffer(vb, b, planes);
962 for (plane = 0; plane < vb->num_planes; ++plane) {
963 /* Skip the plane if already verified */
964 if (vb->v4l2_planes[plane].m.userptr &&
965 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
966 && vb->v4l2_planes[plane].length == planes[plane].length)
969 dprintk(3, "qbuf: userspace address for plane %d changed, "
970 "reacquiring memory\n", plane);
972 /* Check if the provided plane buffer is large enough */
973 if (planes[plane].length < q->plane_sizes[plane]) {
978 /* Release previously acquired memory if present */
979 if (vb->planes[plane].mem_priv)
980 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
982 vb->planes[plane].mem_priv = NULL;
983 vb->v4l2_planes[plane].m.userptr = 0;
984 vb->v4l2_planes[plane].length = 0;
986 /* Acquire each plane's memory */
987 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
988 planes[plane].m.userptr,
989 planes[plane].length, write);
990 if (IS_ERR_OR_NULL(mem_priv)) {
991 dprintk(1, "qbuf: failed acquiring userspace "
992 "memory for plane %d\n", plane);
993 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
996 vb->planes[plane].mem_priv = mem_priv;
1000 * Call driver-specific initialization on the newly acquired buffer,
1003 ret = call_qop(q, buf_init, vb);
1005 dprintk(1, "qbuf: buffer initialization failed\n");
1010 * Now that everything is in order, copy relevant information
1011 * provided by userspace.
1013 for (plane = 0; plane < vb->num_planes; ++plane)
1014 vb->v4l2_planes[plane] = planes[plane];
1018 /* In case of errors, release planes that were already acquired */
1019 for (plane = 0; plane < vb->num_planes; ++plane) {
1020 if (vb->planes[plane].mem_priv)
1021 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
1022 vb->planes[plane].mem_priv = NULL;
1023 vb->v4l2_planes[plane].m.userptr = 0;
1024 vb->v4l2_planes[plane].length = 0;
1031 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1033 static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1035 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1040 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1042 static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1044 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1045 struct vb2_queue *q = vb->vb2_queue;
1049 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1051 /* Verify and copy relevant information provided by the userspace */
1052 __fill_vb2_buffer(vb, b, planes);
1054 for (plane = 0; plane < vb->num_planes; ++plane) {
1055 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1057 if (IS_ERR_OR_NULL(dbuf)) {
1058 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1064 /* use DMABUF size if length is not provided */
1065 if (planes[plane].length == 0)
1066 planes[plane].length = dbuf->size;
1068 if (planes[plane].length < planes[plane].data_offset +
1069 q->plane_sizes[plane]) {
1074 /* Skip the plane if already verified */
1075 if (dbuf == vb->planes[plane].dbuf &&
1076 vb->v4l2_planes[plane].length == planes[plane].length) {
1081 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1083 /* Release previously acquired memory if present */
1084 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1085 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1087 /* Acquire each plane's memory */
1088 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1089 dbuf, planes[plane].length, write);
1090 if (IS_ERR(mem_priv)) {
1091 dprintk(1, "qbuf: failed to attach dmabuf\n");
1092 ret = PTR_ERR(mem_priv);
1097 vb->planes[plane].dbuf = dbuf;
1098 vb->planes[plane].mem_priv = mem_priv;
1101 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1102 * really we want to do this just before the DMA, not while queueing
1105 for (plane = 0; plane < vb->num_planes; ++plane) {
1106 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1108 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1112 vb->planes[plane].dbuf_mapped = 1;
1116 * Call driver-specific initialization on the newly acquired buffer,
1119 ret = call_qop(q, buf_init, vb);
1121 dprintk(1, "qbuf: buffer initialization failed\n");
1126 * Now that everything is in order, copy relevant information
1127 * provided by userspace.
1129 for (plane = 0; plane < vb->num_planes; ++plane)
1130 vb->v4l2_planes[plane] = planes[plane];
1134 /* In case of errors, release planes that were already acquired */
1135 __vb2_buf_dmabuf_put(vb);
1141 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1143 static void __enqueue_in_driver(struct vb2_buffer *vb)
1145 struct vb2_queue *q = vb->vb2_queue;
1148 vb->state = VB2_BUF_STATE_ACTIVE;
1149 atomic_inc(&q->queued_count);
1152 for (plane = 0; plane < vb->num_planes; ++plane)
1153 call_memop(q, prepare, vb->planes[plane].mem_priv);
1155 q->ops->buf_queue(vb);
1158 static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1160 struct vb2_queue *q = vb->vb2_queue;
1163 switch (q->memory) {
1164 case V4L2_MEMORY_MMAP:
1165 ret = __qbuf_mmap(vb, b);
1167 case V4L2_MEMORY_USERPTR:
1168 ret = __qbuf_userptr(vb, b);
1170 case V4L2_MEMORY_DMABUF:
1171 ret = __qbuf_dmabuf(vb, b);
1174 WARN(1, "Invalid queue type\n");
1179 ret = call_qop(q, buf_prepare, vb);
1181 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1183 vb->state = VB2_BUF_STATE_PREPARED;
1189 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1190 * @q: videobuf2 queue
1191 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1194 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1196 * 1) verifies the passed buffer,
1197 * 2) calls buf_prepare callback in the driver (if provided), in which
1198 * driver-specific buffer initialization can be performed,
1200 * The return values from this function are intended to be directly returned
1201 * from vidioc_prepare_buf handler in driver.
1203 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1205 struct vb2_buffer *vb;
1209 dprintk(1, "%s(): file io in progress\n", __func__);
1213 if (b->type != q->type) {
1214 dprintk(1, "%s(): invalid buffer type\n", __func__);
1218 if (b->index >= q->num_buffers) {
1219 dprintk(1, "%s(): buffer index out of range\n", __func__);
1223 vb = q->bufs[b->index];
1225 /* Should never happen */
1226 dprintk(1, "%s(): buffer is NULL\n", __func__);
1230 if (b->memory != q->memory) {
1231 dprintk(1, "%s(): invalid memory type\n", __func__);
1235 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1236 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1239 ret = __verify_planes_array(vb, b);
1242 ret = __buf_prepare(vb, b);
1246 __fill_v4l2_buffer(vb, b);
1250 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1253 * vb2_qbuf() - Queue a buffer from userspace
1254 * @q: videobuf2 queue
1255 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1258 * Should be called from vidioc_qbuf ioctl handler of a driver.
1260 * 1) verifies the passed buffer,
1261 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1262 * which driver-specific buffer initialization can be performed,
1263 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1264 * callback for processing.
1266 * The return values from this function are intended to be directly returned
1267 * from vidioc_qbuf handler in driver.
1269 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1271 struct rw_semaphore *mmap_sem = NULL;
1272 struct vb2_buffer *vb;
1276 * In case of user pointer buffers vb2 allocator needs to get direct
1277 * access to userspace pages. This requires getting read access on
1278 * mmap semaphore in the current process structure. The same
1279 * semaphore is taken before calling mmap operation, while both mmap
1280 * and qbuf are called by the driver or v4l2 core with driver's lock
1281 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1282 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1283 * release driver's lock, takes mmap_sem and then takes again driver's
1286 * To avoid race with other vb2 calls, which might be called after
1287 * releasing driver's lock, this operation is performed at the
1288 * beggining of qbuf processing. This way the queue status is
1289 * consistent after getting driver's lock back.
1291 if (q->memory == V4L2_MEMORY_USERPTR) {
1292 mmap_sem = ¤t->mm->mmap_sem;
1293 call_qop(q, wait_prepare, q);
1294 down_read(mmap_sem);
1295 call_qop(q, wait_finish, q);
1299 dprintk(1, "qbuf: file io in progress\n");
1304 if (b->type != q->type) {
1305 dprintk(1, "qbuf: invalid buffer type\n");
1310 if (b->index >= q->num_buffers) {
1311 dprintk(1, "qbuf: buffer index out of range\n");
1316 vb = q->bufs[b->index];
1318 /* Should never happen */
1319 dprintk(1, "qbuf: buffer is NULL\n");
1324 if (b->memory != q->memory) {
1325 dprintk(1, "qbuf: invalid memory type\n");
1329 ret = __verify_planes_array(vb, b);
1333 switch (vb->state) {
1334 case VB2_BUF_STATE_DEQUEUED:
1335 ret = __buf_prepare(vb, b);
1338 case VB2_BUF_STATE_PREPARED:
1341 dprintk(1, "qbuf: buffer already in use\n");
1347 * Add to the queued buffers list, a buffer will stay on it until
1348 * dequeued in dqbuf.
1350 list_add_tail(&vb->queued_entry, &q->queued_list);
1351 vb->state = VB2_BUF_STATE_QUEUED;
1354 * If already streaming, give the buffer to driver for processing.
1355 * If not, the buffer will be given to driver on next streamon.
1358 __enqueue_in_driver(vb);
1360 /* Fill buffer information for the userspace */
1361 __fill_v4l2_buffer(vb, b);
1363 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1369 EXPORT_SYMBOL_GPL(vb2_qbuf);
1372 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1375 * Will sleep if required for nonblocking == false.
1377 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1380 * All operations on vb_done_list are performed under done_lock
1381 * spinlock protection. However, buffers may be removed from
1382 * it and returned to userspace only while holding both driver's
1383 * lock and the done_lock spinlock. Thus we can be sure that as
1384 * long as we hold the driver's lock, the list will remain not
1385 * empty if list_empty() check succeeds.
1391 if (!q->streaming) {
1392 dprintk(1, "Streaming off, will not wait for buffers\n");
1396 if (!list_empty(&q->done_list)) {
1398 * Found a buffer that we were waiting for.
1404 dprintk(1, "Nonblocking and no buffers to dequeue, "
1410 * We are streaming and blocking, wait for another buffer to
1411 * become ready or for streamoff. Driver's lock is released to
1412 * allow streamoff or qbuf to be called while waiting.
1414 call_qop(q, wait_prepare, q);
1417 * All locks have been released, it is safe to sleep now.
1419 dprintk(3, "Will sleep waiting for buffers\n");
1420 ret = wait_event_interruptible(q->done_wq,
1421 !list_empty(&q->done_list) || !q->streaming);
1424 * We need to reevaluate both conditions again after reacquiring
1425 * the locks or return an error if one occurred.
1427 call_qop(q, wait_finish, q);
1429 dprintk(1, "Sleep was interrupted\n");
1437 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1439 * Will sleep if required for nonblocking == false.
1441 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1442 struct v4l2_buffer *b, int nonblocking)
1444 unsigned long flags;
1448 * Wait for at least one buffer to become available on the done_list.
1450 ret = __vb2_wait_for_done_vb(q, nonblocking);
1455 * Driver's lock has been held since we last verified that done_list
1456 * is not empty, so no need for another list_empty(done_list) check.
1458 spin_lock_irqsave(&q->done_lock, flags);
1459 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1461 * Only remove the buffer from done_list if v4l2_buffer can handle all
1464 ret = __verify_planes_array(*vb, b);
1466 list_del(&(*vb)->done_entry);
1467 spin_unlock_irqrestore(&q->done_lock, flags);
1473 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1474 * @q: videobuf2 queue
1476 * This function will wait until all buffers that have been given to the driver
1477 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1478 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1479 * taken, for example from stop_streaming() callback.
1481 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1483 if (!q->streaming) {
1484 dprintk(1, "Streaming off, will not wait for buffers\n");
1488 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1491 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1494 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1496 static void __vb2_dqbuf(struct vb2_buffer *vb)
1498 struct vb2_queue *q = vb->vb2_queue;
1501 /* nothing to do if the buffer is already dequeued */
1502 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1505 vb->state = VB2_BUF_STATE_DEQUEUED;
1507 /* unmap DMABUF buffer */
1508 if (q->memory == V4L2_MEMORY_DMABUF)
1509 for (i = 0; i < vb->num_planes; ++i) {
1510 if (!vb->planes[i].dbuf_mapped)
1512 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1513 vb->planes[i].dbuf_mapped = 0;
1518 * vb2_dqbuf() - Dequeue a buffer to the userspace
1519 * @q: videobuf2 queue
1520 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1522 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1523 * buffers ready for dequeuing are present. Normally the driver
1524 * would be passing (file->f_flags & O_NONBLOCK) here
1526 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1528 * 1) verifies the passed buffer,
1529 * 2) calls buf_finish callback in the driver (if provided), in which
1530 * driver can perform any additional operations that may be required before
1531 * returning the buffer to userspace, such as cache sync,
1532 * 3) the buffer struct members are filled with relevant information for
1535 * The return values from this function are intended to be directly returned
1536 * from vidioc_dqbuf handler in driver.
1538 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1540 struct vb2_buffer *vb = NULL;
1544 dprintk(1, "dqbuf: file io in progress\n");
1548 if (b->type != q->type) {
1549 dprintk(1, "dqbuf: invalid buffer type\n");
1552 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1556 ret = call_qop(q, buf_finish, vb);
1558 dprintk(1, "dqbuf: buffer finish failed\n");
1562 switch (vb->state) {
1563 case VB2_BUF_STATE_DONE:
1564 dprintk(3, "dqbuf: Returning done buffer\n");
1566 case VB2_BUF_STATE_ERROR:
1567 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1570 dprintk(1, "dqbuf: Invalid buffer state\n");
1574 /* Fill buffer information for the userspace */
1575 __fill_v4l2_buffer(vb, b);
1576 /* Remove from videobuf queue */
1577 list_del(&vb->queued_entry);
1578 /* go back to dequeued state */
1581 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1582 vb->v4l2_buf.index, vb->state);
1586 EXPORT_SYMBOL_GPL(vb2_dqbuf);
1589 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1591 * Removes all queued buffers from driver's queue and all buffers queued by
1592 * userspace from videobuf's queue. Returns to state after reqbufs.
1594 static void __vb2_queue_cancel(struct vb2_queue *q)
1599 * Tell driver to stop all transactions and release all queued
1603 call_qop(q, stop_streaming, q);
1607 * Remove all buffers from videobuf's list...
1609 INIT_LIST_HEAD(&q->queued_list);
1611 * ...and done list; userspace will not receive any buffers it
1612 * has not already dequeued before initiating cancel.
1614 INIT_LIST_HEAD(&q->done_list);
1615 atomic_set(&q->queued_count, 0);
1616 wake_up_all(&q->done_wq);
1619 * Reinitialize all buffers for next use.
1621 for (i = 0; i < q->num_buffers; ++i)
1622 __vb2_dqbuf(q->bufs[i]);
1626 * vb2_streamon - start streaming
1627 * @q: videobuf2 queue
1628 * @type: type argument passed from userspace to vidioc_streamon handler
1630 * Should be called from vidioc_streamon handler of a driver.
1632 * 1) verifies current state
1633 * 2) passes any previously queued buffers to the driver and starts streaming
1635 * The return values from this function are intended to be directly returned
1636 * from vidioc_streamon handler in the driver.
1638 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1640 struct vb2_buffer *vb;
1644 dprintk(1, "streamon: file io in progress\n");
1648 if (type != q->type) {
1649 dprintk(1, "streamon: invalid stream type\n");
1654 dprintk(1, "streamon: already streaming\n");
1659 * If any buffers were queued before streamon,
1660 * we can now pass them to driver for processing.
1662 list_for_each_entry(vb, &q->queued_list, queued_entry)
1663 __enqueue_in_driver(vb);
1666 * Let driver notice that streaming state has been enabled.
1668 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1670 dprintk(1, "streamon: driver refused to start streaming\n");
1671 __vb2_queue_cancel(q);
1677 dprintk(3, "Streamon successful\n");
1680 EXPORT_SYMBOL_GPL(vb2_streamon);
1684 * vb2_streamoff - stop streaming
1685 * @q: videobuf2 queue
1686 * @type: type argument passed from userspace to vidioc_streamoff handler
1688 * Should be called from vidioc_streamoff handler of a driver.
1690 * 1) verifies current state,
1691 * 2) stop streaming and dequeues any queued buffers, including those previously
1692 * passed to the driver (after waiting for the driver to finish).
1694 * This call can be used for pausing playback.
1695 * The return values from this function are intended to be directly returned
1696 * from vidioc_streamoff handler in the driver
1698 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1701 dprintk(1, "streamoff: file io in progress\n");
1705 if (type != q->type) {
1706 dprintk(1, "streamoff: invalid stream type\n");
1710 if (!q->streaming) {
1711 dprintk(1, "streamoff: not streaming\n");
1716 * Cancel will pause streaming and remove all buffers from the driver
1717 * and videobuf, effectively returning control over them to userspace.
1719 __vb2_queue_cancel(q);
1721 dprintk(3, "Streamoff successful\n");
1724 EXPORT_SYMBOL_GPL(vb2_streamoff);
1727 * __find_plane_by_offset() - find plane associated with the given offset off
1729 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1730 unsigned int *_buffer, unsigned int *_plane)
1732 struct vb2_buffer *vb;
1733 unsigned int buffer, plane;
1736 * Go over all buffers and their planes, comparing the given offset
1737 * with an offset assigned to each plane. If a match is found,
1738 * return its buffer and plane numbers.
1740 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1741 vb = q->bufs[buffer];
1743 for (plane = 0; plane < vb->num_planes; ++plane) {
1744 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1756 * vb2_expbuf() - Export a buffer as a file descriptor
1757 * @q: videobuf2 queue
1758 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1761 * The return values from this function are intended to be directly returned
1762 * from vidioc_expbuf handler in driver.
1764 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1766 struct vb2_buffer *vb = NULL;
1767 struct vb2_plane *vb_plane;
1769 struct dma_buf *dbuf;
1771 if (q->memory != V4L2_MEMORY_MMAP) {
1772 dprintk(1, "Queue is not currently set up for mmap\n");
1776 if (!q->mem_ops->get_dmabuf) {
1777 dprintk(1, "Queue does not support DMA buffer exporting\n");
1781 if (eb->flags & ~O_CLOEXEC) {
1782 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1786 if (eb->type != q->type) {
1787 dprintk(1, "qbuf: invalid buffer type\n");
1791 if (eb->index >= q->num_buffers) {
1792 dprintk(1, "buffer index out of range\n");
1796 vb = q->bufs[eb->index];
1798 if (eb->plane >= vb->num_planes) {
1799 dprintk(1, "buffer plane out of range\n");
1803 vb_plane = &vb->planes[eb->plane];
1805 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1806 if (IS_ERR_OR_NULL(dbuf)) {
1807 dprintk(1, "Failed to export buffer %d, plane %d\n",
1808 eb->index, eb->plane);
1812 ret = dma_buf_fd(dbuf, eb->flags);
1814 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1815 eb->index, eb->plane, ret);
1820 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1821 eb->index, eb->plane, ret);
1826 EXPORT_SYMBOL_GPL(vb2_expbuf);
1829 * vb2_mmap() - map video buffers into application address space
1830 * @q: videobuf2 queue
1831 * @vma: vma passed to the mmap file operation handler in the driver
1833 * Should be called from mmap file operation handler of a driver.
1834 * This function maps one plane of one of the available video buffers to
1835 * userspace. To map whole video memory allocated on reqbufs, this function
1836 * has to be called once per each plane per each buffer previously allocated.
1838 * When the userspace application calls mmap, it passes to it an offset returned
1839 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1840 * a "cookie", which is then used to identify the plane to be mapped.
1841 * This function finds a plane with a matching offset and a mapping is performed
1842 * by the means of a provided memory operation.
1844 * The return values from this function are intended to be directly returned
1845 * from the mmap handler in driver.
1847 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1849 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1850 struct vb2_buffer *vb;
1851 unsigned int buffer, plane;
1854 if (q->memory != V4L2_MEMORY_MMAP) {
1855 dprintk(1, "Queue is not currently set up for mmap\n");
1860 * Check memory area access mode.
1862 if (!(vma->vm_flags & VM_SHARED)) {
1863 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1866 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1867 if (!(vma->vm_flags & VM_WRITE)) {
1868 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1872 if (!(vma->vm_flags & VM_READ)) {
1873 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1879 * Find the plane corresponding to the offset passed by userspace.
1881 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1885 vb = q->bufs[buffer];
1887 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
1891 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1894 EXPORT_SYMBOL_GPL(vb2_mmap);
1897 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1900 unsigned long pgoff,
1901 unsigned long flags)
1903 unsigned long off = pgoff << PAGE_SHIFT;
1904 struct vb2_buffer *vb;
1905 unsigned int buffer, plane;
1908 if (q->memory != V4L2_MEMORY_MMAP) {
1909 dprintk(1, "Queue is not currently set up for mmap\n");
1914 * Find the plane corresponding to the offset passed by userspace.
1916 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1920 vb = q->bufs[buffer];
1922 return (unsigned long)vb2_plane_vaddr(vb, plane);
1924 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1927 static int __vb2_init_fileio(struct vb2_queue *q, int read);
1928 static int __vb2_cleanup_fileio(struct vb2_queue *q);
1931 * vb2_poll() - implements poll userspace operation
1932 * @q: videobuf2 queue
1933 * @file: file argument passed to the poll file operation handler
1934 * @wait: wait argument passed to the poll file operation handler
1936 * This function implements poll file operation handler for a driver.
1937 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1938 * be informed that the file descriptor of a video device is available for
1940 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1941 * will be reported as available for writing.
1943 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1946 * The return values from this function are intended to be directly returned
1947 * from poll handler in driver.
1949 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1951 struct video_device *vfd = video_devdata(file);
1952 unsigned long req_events = poll_requested_events(wait);
1953 struct vb2_buffer *vb = NULL;
1954 unsigned int res = 0;
1955 unsigned long flags;
1957 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1958 struct v4l2_fh *fh = file->private_data;
1960 if (v4l2_event_pending(fh))
1962 else if (req_events & POLLPRI)
1963 poll_wait(file, &fh->wait, wait);
1967 * Start file I/O emulator only if streaming API has not been used yet.
1969 if (q->num_buffers == 0 && q->fileio == NULL) {
1970 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1971 (req_events & (POLLIN | POLLRDNORM))) {
1972 if (__vb2_init_fileio(q, 1))
1973 return res | POLLERR;
1975 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1976 (req_events & (POLLOUT | POLLWRNORM))) {
1977 if (__vb2_init_fileio(q, 0))
1978 return res | POLLERR;
1980 * Write to OUTPUT queue can be done immediately.
1982 return res | POLLOUT | POLLWRNORM;
1987 * There is nothing to wait for if no buffers have already been queued.
1989 if (list_empty(&q->queued_list))
1990 return res | POLLERR;
1992 poll_wait(file, &q->done_wq, wait);
1995 * Take first buffer available for dequeuing.
1997 spin_lock_irqsave(&q->done_lock, flags);
1998 if (!list_empty(&q->done_list))
1999 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2001 spin_unlock_irqrestore(&q->done_lock, flags);
2003 if (vb && (vb->state == VB2_BUF_STATE_DONE
2004 || vb->state == VB2_BUF_STATE_ERROR)) {
2005 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2006 res | POLLOUT | POLLWRNORM :
2007 res | POLLIN | POLLRDNORM;
2011 EXPORT_SYMBOL_GPL(vb2_poll);
2014 * vb2_queue_init() - initialize a videobuf2 queue
2015 * @q: videobuf2 queue; this structure should be allocated in driver
2017 * The vb2_queue structure should be allocated by the driver. The driver is
2018 * responsible of clearing it's content and setting initial values for some
2019 * required entries before calling this function.
2020 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2021 * to the struct vb2_queue description in include/media/videobuf2-core.h
2022 * for more information.
2024 int vb2_queue_init(struct vb2_queue *q)
2031 WARN_ON(!q->mem_ops) ||
2032 WARN_ON(!q->type) ||
2033 WARN_ON(!q->io_modes) ||
2034 WARN_ON(!q->ops->queue_setup) ||
2035 WARN_ON(!q->ops->buf_queue))
2038 INIT_LIST_HEAD(&q->queued_list);
2039 INIT_LIST_HEAD(&q->done_list);
2040 spin_lock_init(&q->done_lock);
2041 init_waitqueue_head(&q->done_wq);
2043 if (q->buf_struct_size == 0)
2044 q->buf_struct_size = sizeof(struct vb2_buffer);
2048 EXPORT_SYMBOL_GPL(vb2_queue_init);
2051 * vb2_queue_release() - stop streaming, release the queue and free memory
2052 * @q: videobuf2 queue
2054 * This function stops streaming and performs necessary clean ups, including
2055 * freeing video buffer memory. The driver is responsible for freeing
2056 * the vb2_queue structure itself.
2058 void vb2_queue_release(struct vb2_queue *q)
2060 __vb2_cleanup_fileio(q);
2061 __vb2_queue_cancel(q);
2062 __vb2_queue_free(q, q->num_buffers);
2064 EXPORT_SYMBOL_GPL(vb2_queue_release);
2067 * struct vb2_fileio_buf - buffer context used by file io emulator
2069 * vb2 provides a compatibility layer and emulator of file io (read and
2070 * write) calls on top of streaming API. This structure is used for
2071 * tracking context related to the buffers.
2073 struct vb2_fileio_buf {
2077 unsigned int queued:1;
2081 * struct vb2_fileio_data - queue context used by file io emulator
2083 * vb2 provides a compatibility layer and emulator of file io (read and
2084 * write) calls on top of streaming API. For proper operation it required
2085 * this structure to save the driver state between each call of the read
2086 * or write function.
2088 struct vb2_fileio_data {
2089 struct v4l2_requestbuffers req;
2090 struct v4l2_buffer b;
2091 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2093 unsigned int q_count;
2094 unsigned int dq_count;
2099 * __vb2_init_fileio() - initialize file io emulator
2100 * @q: videobuf2 queue
2101 * @read: mode selector (1 means read, 0 means write)
2103 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2105 struct vb2_fileio_data *fileio;
2107 unsigned int count = 0;
2112 if ((read && !(q->io_modes & VB2_READ)) ||
2113 (!read && !(q->io_modes & VB2_WRITE)))
2117 * Check if device supports mapping buffers to kernel virtual space.
2119 if (!q->mem_ops->vaddr)
2123 * Check if streaming api has not been already activated.
2125 if (q->streaming || q->num_buffers > 0)
2129 * Start with count 1, driver can increase it in queue_setup()
2133 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2134 (read) ? "read" : "write", count, q->io_flags);
2136 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2140 fileio->flags = q->io_flags;
2143 * Request buffers and use MMAP type to force driver
2144 * to allocate buffers by itself.
2146 fileio->req.count = count;
2147 fileio->req.memory = V4L2_MEMORY_MMAP;
2148 fileio->req.type = q->type;
2149 ret = vb2_reqbufs(q, &fileio->req);
2154 * Check if plane_count is correct
2155 * (multiplane buffers are not supported).
2157 if (q->bufs[0]->num_planes != 1) {
2163 * Get kernel address of each buffer.
2165 for (i = 0; i < q->num_buffers; i++) {
2166 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2167 if (fileio->bufs[i].vaddr == NULL)
2169 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2173 * Read mode requires pre queuing of all buffers.
2177 * Queue all buffers.
2179 for (i = 0; i < q->num_buffers; i++) {
2180 struct v4l2_buffer *b = &fileio->b;
2181 memset(b, 0, sizeof(*b));
2183 b->memory = q->memory;
2185 ret = vb2_qbuf(q, b);
2188 fileio->bufs[i].queued = 1;
2194 ret = vb2_streamon(q, q->type);
2204 fileio->req.count = 0;
2205 vb2_reqbufs(q, &fileio->req);
2213 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2214 * @q: videobuf2 queue
2216 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2218 struct vb2_fileio_data *fileio = q->fileio;
2222 * Hack fileio context to enable direct calls to vb2 ioctl
2227 vb2_streamoff(q, q->type);
2228 fileio->req.count = 0;
2229 vb2_reqbufs(q, &fileio->req);
2231 dprintk(3, "file io emulator closed\n");
2237 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2238 * @q: videobuf2 queue
2239 * @data: pointed to target userspace buffer
2240 * @count: number of bytes to read or write
2241 * @ppos: file handle position tracking pointer
2242 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2243 * @read: access mode selector (1 means read, 0 means write)
2245 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2246 loff_t *ppos, int nonblock, int read)
2248 struct vb2_fileio_data *fileio;
2249 struct vb2_fileio_buf *buf;
2252 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2253 read ? "read" : "write", (long)*ppos, count,
2254 nonblock ? "non" : "");
2260 * Initialize emulator on first call.
2263 ret = __vb2_init_fileio(q, read);
2264 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2271 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2272 * The pointer will be restored before returning from this function.
2276 index = fileio->index;
2277 buf = &fileio->bufs[index];
2280 * Check if we need to dequeue the buffer.
2283 struct vb2_buffer *vb;
2286 * Call vb2_dqbuf to get buffer back.
2288 memset(&fileio->b, 0, sizeof(fileio->b));
2289 fileio->b.type = q->type;
2290 fileio->b.memory = q->memory;
2291 fileio->b.index = index;
2292 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2293 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2296 fileio->dq_count += 1;
2299 * Get number of bytes filled by the driver
2301 vb = q->bufs[index];
2302 buf->size = vb2_get_plane_payload(vb, 0);
2307 * Limit count on last few bytes of the buffer.
2309 if (buf->pos + count > buf->size) {
2310 count = buf->size - buf->pos;
2311 dprintk(5, "reducing read count: %zd\n", count);
2315 * Transfer data to userspace.
2317 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2318 count, index, buf->pos);
2320 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2322 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2324 dprintk(3, "file io: error copying data\n");
2336 * Queue next buffer if required.
2338 if (buf->pos == buf->size ||
2339 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2341 * Check if this is the last buffer to read.
2343 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2344 fileio->dq_count == 1) {
2345 dprintk(3, "file io: read limit reached\n");
2347 * Restore fileio pointer and release the context.
2350 return __vb2_cleanup_fileio(q);
2354 * Call vb2_qbuf and give buffer to the driver.
2356 memset(&fileio->b, 0, sizeof(fileio->b));
2357 fileio->b.type = q->type;
2358 fileio->b.memory = q->memory;
2359 fileio->b.index = index;
2360 fileio->b.bytesused = buf->pos;
2361 ret = vb2_qbuf(q, &fileio->b);
2362 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2367 * Buffer has been queued, update the status
2371 buf->size = q->bufs[0]->v4l2_planes[0].length;
2372 fileio->q_count += 1;
2375 * Switch to the next buffer
2377 fileio->index = (index + 1) % q->num_buffers;
2380 * Start streaming if required.
2382 if (!read && !q->streaming) {
2383 ret = vb2_streamon(q, q->type);
2390 * Return proper number of bytes processed.
2396 * Restore the fileio context and block vb2 ioctl interface.
2402 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2403 loff_t *ppos, int nonblocking)
2405 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2407 EXPORT_SYMBOL_GPL(vb2_read);
2409 size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2410 loff_t *ppos, int nonblocking)
2412 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2414 EXPORT_SYMBOL_GPL(vb2_write);
2418 * The following functions are not part of the vb2 core API, but are helper
2419 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2420 * and struct vb2_ops.
2421 * They contain boilerplate code that most if not all drivers have to do
2422 * and so they simplify the driver code.
2425 /* The queue is busy if there is a owner and you are not that owner. */
2426 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2428 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2431 /* vb2 ioctl helpers */
2433 int vb2_ioctl_reqbufs(struct file *file, void *priv,
2434 struct v4l2_requestbuffers *p)
2436 struct video_device *vdev = video_devdata(file);
2437 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2441 if (vb2_queue_is_busy(vdev, file))
2443 res = __reqbufs(vdev->queue, p);
2444 /* If count == 0, then the owner has released all buffers and he
2445 is no longer owner of the queue. Otherwise we have a new owner. */
2447 vdev->queue->owner = p->count ? file->private_data : NULL;
2450 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2452 int vb2_ioctl_create_bufs(struct file *file, void *priv,
2453 struct v4l2_create_buffers *p)
2455 struct video_device *vdev = video_devdata(file);
2456 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2458 p->index = vdev->queue->num_buffers;
2459 /* If count == 0, then just check if memory and type are valid.
2460 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2462 return res != -EBUSY ? res : 0;
2465 if (vb2_queue_is_busy(vdev, file))
2467 res = __create_bufs(vdev->queue, p);
2469 vdev->queue->owner = file->private_data;
2472 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2474 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2475 struct v4l2_buffer *p)
2477 struct video_device *vdev = video_devdata(file);
2479 if (vb2_queue_is_busy(vdev, file))
2481 return vb2_prepare_buf(vdev->queue, p);
2483 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2485 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2487 struct video_device *vdev = video_devdata(file);
2489 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2490 return vb2_querybuf(vdev->queue, p);
2492 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2494 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2496 struct video_device *vdev = video_devdata(file);
2498 if (vb2_queue_is_busy(vdev, file))
2500 return vb2_qbuf(vdev->queue, p);
2502 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2504 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2506 struct video_device *vdev = video_devdata(file);
2508 if (vb2_queue_is_busy(vdev, file))
2510 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2512 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2514 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2516 struct video_device *vdev = video_devdata(file);
2518 if (vb2_queue_is_busy(vdev, file))
2520 return vb2_streamon(vdev->queue, i);
2522 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2524 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2526 struct video_device *vdev = video_devdata(file);
2528 if (vb2_queue_is_busy(vdev, file))
2530 return vb2_streamoff(vdev->queue, i);
2532 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2534 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2536 struct video_device *vdev = video_devdata(file);
2538 if (vb2_queue_is_busy(vdev, file))
2540 return vb2_expbuf(vdev->queue, p);
2542 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2544 /* v4l2_file_operations helpers */
2546 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2548 struct video_device *vdev = video_devdata(file);
2550 return vb2_mmap(vdev->queue, vma);
2552 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2554 int vb2_fop_release(struct file *file)
2556 struct video_device *vdev = video_devdata(file);
2558 if (file->private_data == vdev->queue->owner) {
2559 vb2_queue_release(vdev->queue);
2560 vdev->queue->owner = NULL;
2562 return v4l2_fh_release(file);
2564 EXPORT_SYMBOL_GPL(vb2_fop_release);
2566 ssize_t vb2_fop_write(struct file *file, char __user *buf,
2567 size_t count, loff_t *ppos)
2569 struct video_device *vdev = video_devdata(file);
2570 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2573 if (lock && mutex_lock_interruptible(lock))
2574 return -ERESTARTSYS;
2575 if (vb2_queue_is_busy(vdev, file))
2577 err = vb2_write(vdev->queue, buf, count, ppos,
2578 file->f_flags & O_NONBLOCK);
2579 if (vdev->queue->fileio)
2580 vdev->queue->owner = file->private_data;
2586 EXPORT_SYMBOL_GPL(vb2_fop_write);
2588 ssize_t vb2_fop_read(struct file *file, char __user *buf,
2589 size_t count, loff_t *ppos)
2591 struct video_device *vdev = video_devdata(file);
2592 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2595 if (lock && mutex_lock_interruptible(lock))
2596 return -ERESTARTSYS;
2597 if (vb2_queue_is_busy(vdev, file))
2599 err = vb2_read(vdev->queue, buf, count, ppos,
2600 file->f_flags & O_NONBLOCK);
2601 if (vdev->queue->fileio)
2602 vdev->queue->owner = file->private_data;
2608 EXPORT_SYMBOL_GPL(vb2_fop_read);
2610 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2612 struct video_device *vdev = video_devdata(file);
2613 struct vb2_queue *q = vdev->queue;
2614 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2615 unsigned long req_events = poll_requested_events(wait);
2618 bool must_lock = false;
2620 /* Try to be smart: only lock if polling might start fileio,
2621 otherwise locking will only introduce unwanted delays. */
2622 if (q->num_buffers == 0 && q->fileio == NULL) {
2623 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2624 (req_events & (POLLIN | POLLRDNORM)))
2626 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2627 (req_events & (POLLOUT | POLLWRNORM)))
2631 /* If locking is needed, but this helper doesn't know how, then you
2632 shouldn't be using this helper but you should write your own. */
2633 WARN_ON(must_lock && !lock);
2635 if (must_lock && lock && mutex_lock_interruptible(lock))
2640 res = vb2_poll(vdev->queue, file, wait);
2642 /* If fileio was started, then we have a new queue owner. */
2643 if (must_lock && !fileio && q->fileio)
2644 q->owner = file->private_data;
2645 if (must_lock && lock)
2649 EXPORT_SYMBOL_GPL(vb2_fop_poll);
2652 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2653 unsigned long len, unsigned long pgoff, unsigned long flags)
2655 struct video_device *vdev = video_devdata(file);
2657 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2659 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2662 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2664 void vb2_ops_wait_prepare(struct vb2_queue *vq)
2666 mutex_unlock(vq->lock);
2668 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2670 void vb2_ops_wait_finish(struct vb2_queue *vq)
2672 mutex_lock(vq->lock);
2674 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2676 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2677 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2678 MODULE_LICENSE("GPL");