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
;
57 /* Allocate memory for all planes in this buffer */
58 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
59 mem_priv
= call_memop(q
, alloc
, q
->alloc_ctx
[plane
],
60 q
->plane_sizes
[plane
]);
61 if (IS_ERR_OR_NULL(mem_priv
))
64 /* Associate allocator private data with this plane */
65 vb
->planes
[plane
].mem_priv
= mem_priv
;
66 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
71 /* Free already allocated memory if one of the allocations failed */
72 for (; plane
> 0; --plane
) {
73 call_memop(q
, put
, vb
->planes
[plane
- 1].mem_priv
);
74 vb
->planes
[plane
- 1].mem_priv
= NULL
;
81 * __vb2_buf_mem_free() - free memory of the given buffer
83 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
85 struct vb2_queue
*q
= vb
->vb2_queue
;
88 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
89 call_memop(q
, put
, vb
->planes
[plane
].mem_priv
);
90 vb
->planes
[plane
].mem_priv
= NULL
;
91 dprintk(3, "Freed plane %d of buffer %d\n", plane
,
97 * __vb2_buf_userptr_put() - release userspace memory associated with
100 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
102 struct vb2_queue
*q
= vb
->vb2_queue
;
105 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
106 if (vb
->planes
[plane
].mem_priv
)
107 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
108 vb
->planes
[plane
].mem_priv
= NULL
;
113 * __vb2_plane_dmabuf_put() - release memory associated with
114 * a DMABUF shared plane
116 static void __vb2_plane_dmabuf_put(struct vb2_queue
*q
, struct vb2_plane
*p
)
122 call_memop(q
, unmap_dmabuf
, p
->mem_priv
);
124 call_memop(q
, detach_dmabuf
, p
->mem_priv
);
125 dma_buf_put(p
->dbuf
);
126 memset(p
, 0, sizeof(*p
));
130 * __vb2_buf_dmabuf_put() - release memory associated with
131 * a DMABUF shared buffer
133 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
135 struct vb2_queue
*q
= vb
->vb2_queue
;
138 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
139 __vb2_plane_dmabuf_put(q
, &vb
->planes
[plane
]);
143 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
144 * every buffer on the queue
146 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
148 unsigned int buffer
, plane
;
149 struct vb2_buffer
*vb
;
152 if (q
->num_buffers
) {
153 struct v4l2_plane
*p
;
154 vb
= q
->bufs
[q
->num_buffers
- 1];
155 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
156 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
161 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
162 vb
= q
->bufs
[buffer
];
166 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
167 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
168 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
170 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
173 off
+= vb
->v4l2_planes
[plane
].length
;
174 off
= PAGE_ALIGN(off
);
180 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
181 * video buffer memory for all buffers/planes on the queue and initializes the
184 * Returns the number of buffers successfully allocated.
186 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
187 unsigned int num_buffers
, unsigned int num_planes
)
190 struct vb2_buffer
*vb
;
193 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
194 /* Allocate videobuf buffer structures */
195 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
197 dprintk(1, "Memory alloc for buffer struct failed\n");
201 /* Length stores number of planes for multiplanar buffers */
202 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
203 vb
->v4l2_buf
.length
= num_planes
;
205 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
207 vb
->num_planes
= num_planes
;
208 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
209 vb
->v4l2_buf
.type
= q
->type
;
210 vb
->v4l2_buf
.memory
= memory
;
212 /* Allocate video buffer memory for the MMAP type */
213 if (memory
== V4L2_MEMORY_MMAP
) {
214 ret
= __vb2_buf_mem_alloc(vb
);
216 dprintk(1, "Failed allocating memory for "
217 "buffer %d\n", buffer
);
222 * Call the driver-provided buffer initialization
223 * callback, if given. An error in initialization
224 * results in queue setup failure.
226 ret
= call_qop(q
, buf_init
, vb
);
228 dprintk(1, "Buffer %d %p initialization"
229 " failed\n", buffer
, vb
);
230 __vb2_buf_mem_free(vb
);
236 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
239 __setup_offsets(q
, buffer
);
241 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
248 * __vb2_free_mem() - release all video buffer memory for a given queue
250 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
253 struct vb2_buffer
*vb
;
255 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
257 vb
= q
->bufs
[buffer
];
261 /* Free MMAP buffers or release USERPTR buffers */
262 if (q
->memory
== V4L2_MEMORY_MMAP
)
263 __vb2_buf_mem_free(vb
);
264 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
265 __vb2_buf_dmabuf_put(vb
);
267 __vb2_buf_userptr_put(vb
);
272 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
273 * related information, if no buffers are left return the queue to an
274 * uninitialized state. Might be called even if the queue has already been freed.
276 static void __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
280 /* Call driver-provided cleanup function for each buffer, if provided */
281 if (q
->ops
->buf_cleanup
) {
282 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
284 if (NULL
== q
->bufs
[buffer
])
286 q
->ops
->buf_cleanup(q
->bufs
[buffer
]);
290 /* Release video buffer memory */
291 __vb2_free_mem(q
, buffers
);
293 /* Free videobuf buffers */
294 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
296 kfree(q
->bufs
[buffer
]);
297 q
->bufs
[buffer
] = NULL
;
300 q
->num_buffers
-= buffers
;
303 INIT_LIST_HEAD(&q
->queued_list
);
307 * __verify_planes_array() - verify that the planes array passed in struct
308 * v4l2_buffer from userspace can be safely used
310 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
312 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
315 /* Is memory for copying plane information present? */
316 if (NULL
== b
->m
.planes
) {
317 dprintk(1, "Multi-planar buffer passed but "
318 "planes array not provided\n");
322 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
323 dprintk(1, "Incorrect planes array length, "
324 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
332 * __buffer_in_use() - return true if the buffer is in use and
333 * the queue cannot be freed (by the means of REQBUFS(0)) call
335 static bool __buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
338 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
339 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
341 * If num_users() has not been provided, call_memop
342 * will return 0, apparently nobody cares about this
343 * case anyway. If num_users() returns more than 1,
344 * we are not the only user of the plane's memory.
346 if (mem_priv
&& call_memop(q
, num_users
, mem_priv
) > 1)
353 * __buffers_in_use() - return true if any buffers on the queue are in use and
354 * the queue cannot be freed (by the means of REQBUFS(0)) call
356 static bool __buffers_in_use(struct vb2_queue
*q
)
359 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
360 if (__buffer_in_use(q
, q
->bufs
[buffer
]))
367 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
368 * returned to userspace
370 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
372 struct vb2_queue
*q
= vb
->vb2_queue
;
374 /* Copy back data such as timestamp, flags, etc. */
375 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
376 b
->reserved2
= vb
->v4l2_buf
.reserved2
;
377 b
->reserved
= vb
->v4l2_buf
.reserved
;
379 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
381 * Fill in plane-related data if userspace provided an array
382 * for it. The caller has already verified memory and size.
384 b
->length
= vb
->num_planes
;
385 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
386 b
->length
* sizeof(struct v4l2_plane
));
389 * We use length and offset in v4l2_planes array even for
390 * single-planar buffers, but userspace does not.
392 b
->length
= vb
->v4l2_planes
[0].length
;
393 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
394 if (q
->memory
== V4L2_MEMORY_MMAP
)
395 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
396 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
397 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
398 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
399 b
->m
.fd
= vb
->v4l2_planes
[0].m
.fd
;
403 * Clear any buffer state related flags.
405 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
406 b
->flags
|= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
409 case VB2_BUF_STATE_QUEUED
:
410 case VB2_BUF_STATE_ACTIVE
:
411 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
413 case VB2_BUF_STATE_ERROR
:
414 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
416 case VB2_BUF_STATE_DONE
:
417 b
->flags
|= V4L2_BUF_FLAG_DONE
;
419 case VB2_BUF_STATE_PREPARED
:
420 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
422 case VB2_BUF_STATE_DEQUEUED
:
427 if (__buffer_in_use(q
, vb
))
428 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
432 * vb2_querybuf() - query video buffer information
434 * @b: buffer struct passed from userspace to vidioc_querybuf handler
437 * Should be called from vidioc_querybuf ioctl handler in driver.
438 * This function will verify the passed v4l2_buffer structure and fill the
439 * relevant information for the userspace.
441 * The return values from this function are intended to be directly returned
442 * from vidioc_querybuf handler in driver.
444 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
446 struct vb2_buffer
*vb
;
449 if (b
->type
!= q
->type
) {
450 dprintk(1, "querybuf: wrong buffer type\n");
454 if (b
->index
>= q
->num_buffers
) {
455 dprintk(1, "querybuf: buffer index out of range\n");
458 vb
= q
->bufs
[b
->index
];
459 ret
= __verify_planes_array(vb
, b
);
461 __fill_v4l2_buffer(vb
, b
);
464 EXPORT_SYMBOL(vb2_querybuf
);
467 * __verify_userptr_ops() - verify that all memory operations required for
468 * USERPTR queue type have been provided
470 static int __verify_userptr_ops(struct vb2_queue
*q
)
472 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
473 !q
->mem_ops
->put_userptr
)
480 * __verify_mmap_ops() - verify that all memory operations required for
481 * MMAP queue type have been provided
483 static int __verify_mmap_ops(struct vb2_queue
*q
)
485 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
486 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
493 * __verify_dmabuf_ops() - verify that all memory operations required for
494 * DMABUF queue type have been provided
496 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
498 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
499 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
500 !q
->mem_ops
->unmap_dmabuf
)
507 * __verify_memory_type() - Check whether the memory type and buffer type
508 * passed to a buffer operation are compatible with the queue.
510 static int __verify_memory_type(struct vb2_queue
*q
,
511 enum v4l2_memory memory
, enum v4l2_buf_type type
)
513 if (memory
!= V4L2_MEMORY_MMAP
&& memory
!= V4L2_MEMORY_USERPTR
&&
514 memory
!= V4L2_MEMORY_DMABUF
) {
515 dprintk(1, "reqbufs: unsupported memory type\n");
519 if (type
!= q
->type
) {
520 dprintk(1, "reqbufs: requested type is incorrect\n");
525 * Make sure all the required memory ops for given memory type
528 if (memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
529 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
533 if (memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
534 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
538 if (memory
== V4L2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
539 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
544 * Place the busy tests at the end: -EBUSY can be ignored when
545 * create_bufs is called with count == 0, but count == 0 should still
546 * do the memory and type validation.
549 dprintk(1, "reqbufs: file io in progress\n");
556 * __reqbufs() - Initiate streaming
557 * @q: videobuf2 queue
558 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
560 * Should be called from vidioc_reqbufs ioctl handler of a driver.
562 * 1) verifies streaming parameters passed from the userspace,
563 * 2) sets up the queue,
564 * 3) negotiates number of buffers and planes per buffer with the driver
565 * to be used during streaming,
566 * 4) allocates internal buffer structures (struct vb2_buffer), according to
567 * the agreed parameters,
568 * 5) for MMAP memory type, allocates actual video memory, using the
569 * memory handling/allocation routines provided during queue initialization
571 * If req->count is 0, all the memory will be freed instead.
572 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
573 * and the queue is not busy, memory will be reallocated.
575 * The return values from this function are intended to be directly returned
576 * from vidioc_reqbufs handler in driver.
578 static int __reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
580 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
584 dprintk(1, "reqbufs: streaming active\n");
588 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
590 * We already have buffers allocated, so first check if they
591 * are not in use and can be freed.
593 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
594 dprintk(1, "reqbufs: memory in use, cannot free\n");
598 __vb2_queue_free(q
, q
->num_buffers
);
601 * In case of REQBUFS(0) return immediately without calling
602 * driver's queue_setup() callback and allocating resources.
609 * Make sure the requested values and current defaults are sane.
611 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
612 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
613 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
614 q
->memory
= req
->memory
;
617 * Ask the driver how many buffers and planes per buffer it requires.
618 * Driver also sets the size and allocator context for each plane.
620 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
621 q
->plane_sizes
, q
->alloc_ctx
);
625 /* Finally, allocate buffers and video memory */
626 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
628 dprintk(1, "Memory allocation failed\n");
632 allocated_buffers
= ret
;
635 * Check if driver can handle the allocated number of buffers.
637 if (allocated_buffers
< num_buffers
) {
638 num_buffers
= allocated_buffers
;
640 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
641 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
643 if (!ret
&& allocated_buffers
< num_buffers
)
647 * Either the driver has accepted a smaller number of buffers,
648 * or .queue_setup() returned an error
652 q
->num_buffers
= allocated_buffers
;
655 __vb2_queue_free(q
, allocated_buffers
);
660 * Return the number of successfully allocated buffers
663 req
->count
= allocated_buffers
;
669 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
671 * @q: videobuf2 queue
672 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
674 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
676 int ret
= __verify_memory_type(q
, req
->memory
, req
->type
);
678 return ret
? ret
: __reqbufs(q
, req
);
680 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
683 * __create_bufs() - Allocate buffers and any required auxiliary structs
684 * @q: videobuf2 queue
685 * @create: creation parameters, passed from userspace to vidioc_create_bufs
688 * Should be called from vidioc_create_bufs ioctl handler of a driver.
690 * 1) verifies parameter sanity
691 * 2) calls the .queue_setup() queue operation
692 * 3) performs any necessary memory allocations
694 * The return values from this function are intended to be directly returned
695 * from vidioc_create_bufs handler in driver.
697 static int __create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
699 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
702 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
703 dprintk(1, "%s(): maximum number of buffers already allocated\n",
708 if (!q
->num_buffers
) {
709 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
710 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
711 q
->memory
= create
->memory
;
714 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
717 * Ask the driver, whether the requested number of buffers, planes per
718 * buffer and their sizes are acceptable
720 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
721 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
725 /* Finally, allocate buffers and video memory */
726 ret
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
729 dprintk(1, "Memory allocation failed\n");
733 allocated_buffers
= ret
;
736 * Check if driver can handle the so far allocated number of buffers.
738 if (ret
< num_buffers
) {
742 * q->num_buffers contains the total number of buffers, that the
743 * queue driver has set up
745 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
746 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
748 if (!ret
&& allocated_buffers
< num_buffers
)
752 * Either the driver has accepted a smaller number of buffers,
753 * or .queue_setup() returned an error
757 q
->num_buffers
+= allocated_buffers
;
760 __vb2_queue_free(q
, allocated_buffers
);
765 * Return the number of successfully allocated buffers
768 create
->count
= allocated_buffers
;
774 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
775 * memory and type values.
776 * @q: videobuf2 queue
777 * @create: creation parameters, passed from userspace to vidioc_create_bufs
780 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
782 int ret
= __verify_memory_type(q
, create
->memory
, create
->format
.type
);
784 create
->index
= q
->num_buffers
;
785 if (create
->count
== 0)
786 return ret
!= -EBUSY
? ret
: 0;
787 return ret
? ret
: __create_bufs(q
, create
);
789 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
792 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
793 * @vb: vb2_buffer to which the plane in question belongs to
794 * @plane_no: plane number for which the address is to be returned
796 * This function returns a kernel virtual address of a given plane if
797 * such a mapping exist, NULL otherwise.
799 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
801 struct vb2_queue
*q
= vb
->vb2_queue
;
803 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
806 return call_memop(q
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
809 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
812 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
813 * @vb: vb2_buffer to which the plane in question belongs to
814 * @plane_no: plane number for which the cookie is to be returned
816 * This function returns an allocator specific cookie for a given plane if
817 * available, NULL otherwise. The allocator should provide some simple static
818 * inline function, which would convert this cookie to the allocator specific
819 * type that can be used directly by the driver to access the buffer. This can
820 * be for example physical address, pointer to scatter list or IOMMU mapping.
822 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
824 struct vb2_queue
*q
= vb
->vb2_queue
;
826 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
829 return call_memop(q
, cookie
, vb
->planes
[plane_no
].mem_priv
);
831 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
834 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
835 * @vb: vb2_buffer returned from the driver
836 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
837 * or VB2_BUF_STATE_ERROR if the operation finished with an error
839 * This function should be called by the driver after a hardware operation on
840 * a buffer is finished and the buffer may be returned to userspace. The driver
841 * cannot use this buffer anymore until it is queued back to it by videobuf
842 * by the means of buf_queue callback. Only buffers previously queued to the
843 * driver by buf_queue can be passed to this function.
845 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
847 struct vb2_queue
*q
= vb
->vb2_queue
;
851 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
854 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
857 dprintk(4, "Done processing on buffer %d, state: %d\n",
858 vb
->v4l2_buf
.index
, vb
->state
);
861 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
862 call_memop(q
, finish
, vb
->planes
[plane
].mem_priv
);
864 /* Add the buffer to the done buffers list */
865 spin_lock_irqsave(&q
->done_lock
, flags
);
867 list_add_tail(&vb
->done_entry
, &q
->done_list
);
868 atomic_dec(&q
->queued_count
);
869 spin_unlock_irqrestore(&q
->done_lock
, flags
);
871 /* Inform any processes that may be waiting for buffers */
872 wake_up(&q
->done_wq
);
874 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
877 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
878 * v4l2_buffer by the userspace. The caller has already verified that struct
879 * v4l2_buffer has a valid number of planes.
881 static void __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
882 struct v4l2_plane
*v4l2_planes
)
886 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
887 /* Fill in driver-provided information for OUTPUT types */
888 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
890 * Will have to go up to b->length when API starts
891 * accepting variable number of planes.
893 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
894 v4l2_planes
[plane
].bytesused
=
895 b
->m
.planes
[plane
].bytesused
;
896 v4l2_planes
[plane
].data_offset
=
897 b
->m
.planes
[plane
].data_offset
;
901 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
902 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
903 v4l2_planes
[plane
].m
.userptr
=
904 b
->m
.planes
[plane
].m
.userptr
;
905 v4l2_planes
[plane
].length
=
906 b
->m
.planes
[plane
].length
;
909 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
910 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
911 v4l2_planes
[plane
].m
.fd
=
912 b
->m
.planes
[plane
].m
.fd
;
913 v4l2_planes
[plane
].length
=
914 b
->m
.planes
[plane
].length
;
915 v4l2_planes
[plane
].data_offset
=
916 b
->m
.planes
[plane
].data_offset
;
921 * Single-planar buffers do not use planes array,
922 * so fill in relevant v4l2_buffer struct fields instead.
923 * In videobuf we use our internal V4l2_planes struct for
924 * single-planar buffers as well, for simplicity.
926 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
927 v4l2_planes
[0].bytesused
= b
->bytesused
;
928 v4l2_planes
[0].data_offset
= 0;
931 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
932 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
933 v4l2_planes
[0].length
= b
->length
;
936 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
937 v4l2_planes
[0].m
.fd
= b
->m
.fd
;
938 v4l2_planes
[0].length
= b
->length
;
939 v4l2_planes
[0].data_offset
= 0;
944 vb
->v4l2_buf
.field
= b
->field
;
945 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
946 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
950 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
952 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
954 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
955 struct vb2_queue
*q
= vb
->vb2_queue
;
959 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
961 /* Copy relevant information provided by the userspace */
962 __fill_vb2_buffer(vb
, b
, planes
);
964 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
965 /* Skip the plane if already verified */
966 if (vb
->v4l2_planes
[plane
].m
.userptr
&&
967 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
968 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
971 dprintk(3, "qbuf: userspace address for plane %d changed, "
972 "reacquiring memory\n", plane
);
974 /* Check if the provided plane buffer is large enough */
975 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
980 /* Release previously acquired memory if present */
981 if (vb
->planes
[plane
].mem_priv
)
982 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
984 vb
->planes
[plane
].mem_priv
= NULL
;
985 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
986 vb
->v4l2_planes
[plane
].length
= 0;
988 /* Acquire each plane's memory */
989 mem_priv
= call_memop(q
, get_userptr
, q
->alloc_ctx
[plane
],
990 planes
[plane
].m
.userptr
,
991 planes
[plane
].length
, write
);
992 if (IS_ERR_OR_NULL(mem_priv
)) {
993 dprintk(1, "qbuf: failed acquiring userspace "
994 "memory for plane %d\n", plane
);
995 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
998 vb
->planes
[plane
].mem_priv
= mem_priv
;
1002 * Call driver-specific initialization on the newly acquired buffer,
1005 ret
= call_qop(q
, buf_init
, vb
);
1007 dprintk(1, "qbuf: buffer initialization failed\n");
1012 * Now that everything is in order, copy relevant information
1013 * provided by userspace.
1015 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1016 vb
->v4l2_planes
[plane
] = planes
[plane
];
1020 /* In case of errors, release planes that were already acquired */
1021 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1022 if (vb
->planes
[plane
].mem_priv
)
1023 call_memop(q
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1024 vb
->planes
[plane
].mem_priv
= NULL
;
1025 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
1026 vb
->v4l2_planes
[plane
].length
= 0;
1033 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1035 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1037 __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
1042 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1044 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1046 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
1047 struct vb2_queue
*q
= vb
->vb2_queue
;
1051 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
1053 /* Verify and copy relevant information provided by the userspace */
1054 __fill_vb2_buffer(vb
, b
, planes
);
1056 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1057 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1059 if (IS_ERR_OR_NULL(dbuf
)) {
1060 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1066 /* use DMABUF size if length is not provided */
1067 if (planes
[plane
].length
== 0)
1068 planes
[plane
].length
= dbuf
->size
;
1070 if (planes
[plane
].length
< planes
[plane
].data_offset
+
1071 q
->plane_sizes
[plane
]) {
1076 /* Skip the plane if already verified */
1077 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1078 vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
) {
1083 dprintk(1, "qbuf: buffer for plane %d changed\n", plane
);
1085 /* Release previously acquired memory if present */
1086 __vb2_plane_dmabuf_put(q
, &vb
->planes
[plane
]);
1087 memset(&vb
->v4l2_planes
[plane
], 0, sizeof(struct v4l2_plane
));
1089 /* Acquire each plane's memory */
1090 mem_priv
= call_memop(q
, attach_dmabuf
, q
->alloc_ctx
[plane
],
1091 dbuf
, planes
[plane
].length
, write
);
1092 if (IS_ERR(mem_priv
)) {
1093 dprintk(1, "qbuf: failed to attach dmabuf\n");
1094 ret
= PTR_ERR(mem_priv
);
1099 vb
->planes
[plane
].dbuf
= dbuf
;
1100 vb
->planes
[plane
].mem_priv
= mem_priv
;
1103 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1104 * really we want to do this just before the DMA, not while queueing
1107 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1108 ret
= call_memop(q
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1110 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1114 vb
->planes
[plane
].dbuf_mapped
= 1;
1118 * Call driver-specific initialization on the newly acquired buffer,
1121 ret
= call_qop(q
, buf_init
, vb
);
1123 dprintk(1, "qbuf: buffer initialization failed\n");
1128 * Now that everything is in order, copy relevant information
1129 * provided by userspace.
1131 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1132 vb
->v4l2_planes
[plane
] = planes
[plane
];
1136 /* In case of errors, release planes that were already acquired */
1137 __vb2_buf_dmabuf_put(vb
);
1143 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1145 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1147 struct vb2_queue
*q
= vb
->vb2_queue
;
1150 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1151 atomic_inc(&q
->queued_count
);
1154 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1155 call_memop(q
, prepare
, vb
->planes
[plane
].mem_priv
);
1157 q
->ops
->buf_queue(vb
);
1160 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1162 struct vb2_queue
*q
= vb
->vb2_queue
;
1165 switch (q
->memory
) {
1166 case V4L2_MEMORY_MMAP
:
1167 ret
= __qbuf_mmap(vb
, b
);
1169 case V4L2_MEMORY_USERPTR
:
1170 ret
= __qbuf_userptr(vb
, b
);
1172 case V4L2_MEMORY_DMABUF
:
1173 ret
= __qbuf_dmabuf(vb
, b
);
1176 WARN(1, "Invalid queue type\n");
1181 ret
= call_qop(q
, buf_prepare
, vb
);
1183 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
1185 vb
->state
= VB2_BUF_STATE_PREPARED
;
1191 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1192 * @q: videobuf2 queue
1193 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1196 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1198 * 1) verifies the passed buffer,
1199 * 2) calls buf_prepare callback in the driver (if provided), in which
1200 * driver-specific buffer initialization can be performed,
1202 * The return values from this function are intended to be directly returned
1203 * from vidioc_prepare_buf handler in driver.
1205 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1207 struct vb2_buffer
*vb
;
1211 dprintk(1, "%s(): file io in progress\n", __func__
);
1215 if (b
->type
!= q
->type
) {
1216 dprintk(1, "%s(): invalid buffer type\n", __func__
);
1220 if (b
->index
>= q
->num_buffers
) {
1221 dprintk(1, "%s(): buffer index out of range\n", __func__
);
1225 vb
= q
->bufs
[b
->index
];
1227 /* Should never happen */
1228 dprintk(1, "%s(): buffer is NULL\n", __func__
);
1232 if (b
->memory
!= q
->memory
) {
1233 dprintk(1, "%s(): invalid memory type\n", __func__
);
1237 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1238 dprintk(1, "%s(): invalid buffer state %d\n", __func__
, vb
->state
);
1241 ret
= __verify_planes_array(vb
, b
);
1244 ret
= __buf_prepare(vb
, b
);
1248 __fill_v4l2_buffer(vb
, b
);
1252 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1255 * vb2_qbuf() - Queue a buffer from userspace
1256 * @q: videobuf2 queue
1257 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1260 * Should be called from vidioc_qbuf ioctl handler of a driver.
1262 * 1) verifies the passed buffer,
1263 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1264 * which driver-specific buffer initialization can be performed,
1265 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1266 * callback for processing.
1268 * The return values from this function are intended to be directly returned
1269 * from vidioc_qbuf handler in driver.
1271 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1273 struct rw_semaphore
*mmap_sem
= NULL
;
1274 struct vb2_buffer
*vb
;
1278 * In case of user pointer buffers vb2 allocator needs to get direct
1279 * access to userspace pages. This requires getting read access on
1280 * mmap semaphore in the current process structure. The same
1281 * semaphore is taken before calling mmap operation, while both mmap
1282 * and qbuf are called by the driver or v4l2 core with driver's lock
1283 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1284 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1285 * release driver's lock, takes mmap_sem and then takes again driver's
1288 * To avoid race with other vb2 calls, which might be called after
1289 * releasing driver's lock, this operation is performed at the
1290 * beggining of qbuf processing. This way the queue status is
1291 * consistent after getting driver's lock back.
1293 if (q
->memory
== V4L2_MEMORY_USERPTR
) {
1294 mmap_sem
= ¤t
->mm
->mmap_sem
;
1295 call_qop(q
, wait_prepare
, q
);
1296 down_read(mmap_sem
);
1297 call_qop(q
, wait_finish
, q
);
1301 dprintk(1, "qbuf: file io in progress\n");
1306 if (b
->type
!= q
->type
) {
1307 dprintk(1, "qbuf: invalid buffer type\n");
1312 if (b
->index
>= q
->num_buffers
) {
1313 dprintk(1, "qbuf: buffer index out of range\n");
1318 vb
= q
->bufs
[b
->index
];
1320 /* Should never happen */
1321 dprintk(1, "qbuf: buffer is NULL\n");
1326 if (b
->memory
!= q
->memory
) {
1327 dprintk(1, "qbuf: invalid memory type\n");
1331 ret
= __verify_planes_array(vb
, b
);
1335 switch (vb
->state
) {
1336 case VB2_BUF_STATE_DEQUEUED
:
1337 ret
= __buf_prepare(vb
, b
);
1340 case VB2_BUF_STATE_PREPARED
:
1343 dprintk(1, "qbuf: buffer already in use\n");
1349 * Add to the queued buffers list, a buffer will stay on it until
1350 * dequeued in dqbuf.
1352 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1353 vb
->state
= VB2_BUF_STATE_QUEUED
;
1356 * If already streaming, give the buffer to driver for processing.
1357 * If not, the buffer will be given to driver on next streamon.
1360 __enqueue_in_driver(vb
);
1362 /* Fill buffer information for the userspace */
1363 __fill_v4l2_buffer(vb
, b
);
1365 dprintk(1, "qbuf of buffer %d succeeded\n", vb
->v4l2_buf
.index
);
1371 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1374 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1377 * Will sleep if required for nonblocking == false.
1379 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1382 * All operations on vb_done_list are performed under done_lock
1383 * spinlock protection. However, buffers may be removed from
1384 * it and returned to userspace only while holding both driver's
1385 * lock and the done_lock spinlock. Thus we can be sure that as
1386 * long as we hold the driver's lock, the list will remain not
1387 * empty if list_empty() check succeeds.
1393 if (!q
->streaming
) {
1394 dprintk(1, "Streaming off, will not wait for buffers\n");
1398 if (!list_empty(&q
->done_list
)) {
1400 * Found a buffer that we were waiting for.
1406 dprintk(1, "Nonblocking and no buffers to dequeue, "
1412 * We are streaming and blocking, wait for another buffer to
1413 * become ready or for streamoff. Driver's lock is released to
1414 * allow streamoff or qbuf to be called while waiting.
1416 call_qop(q
, wait_prepare
, q
);
1419 * All locks have been released, it is safe to sleep now.
1421 dprintk(3, "Will sleep waiting for buffers\n");
1422 ret
= wait_event_interruptible(q
->done_wq
,
1423 !list_empty(&q
->done_list
) || !q
->streaming
);
1426 * We need to reevaluate both conditions again after reacquiring
1427 * the locks or return an error if one occurred.
1429 call_qop(q
, wait_finish
, q
);
1431 dprintk(1, "Sleep was interrupted\n");
1439 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1441 * Will sleep if required for nonblocking == false.
1443 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1444 struct v4l2_buffer
*b
, int nonblocking
)
1446 unsigned long flags
;
1450 * Wait for at least one buffer to become available on the done_list.
1452 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1457 * Driver's lock has been held since we last verified that done_list
1458 * is not empty, so no need for another list_empty(done_list) check.
1460 spin_lock_irqsave(&q
->done_lock
, flags
);
1461 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1463 * Only remove the buffer from done_list if v4l2_buffer can handle all
1466 ret
= __verify_planes_array(*vb
, b
);
1468 list_del(&(*vb
)->done_entry
);
1469 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1475 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1476 * @q: videobuf2 queue
1478 * This function will wait until all buffers that have been given to the driver
1479 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1480 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1481 * taken, for example from stop_streaming() callback.
1483 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1485 if (!q
->streaming
) {
1486 dprintk(1, "Streaming off, will not wait for buffers\n");
1490 wait_event(q
->done_wq
, !atomic_read(&q
->queued_count
));
1493 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1496 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1498 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1500 struct vb2_queue
*q
= vb
->vb2_queue
;
1503 /* nothing to do if the buffer is already dequeued */
1504 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1507 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1509 /* unmap DMABUF buffer */
1510 if (q
->memory
== V4L2_MEMORY_DMABUF
)
1511 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1512 if (!vb
->planes
[i
].dbuf_mapped
)
1514 call_memop(q
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1515 vb
->planes
[i
].dbuf_mapped
= 0;
1520 * vb2_dqbuf() - Dequeue a buffer to the userspace
1521 * @q: videobuf2 queue
1522 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1524 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1525 * buffers ready for dequeuing are present. Normally the driver
1526 * would be passing (file->f_flags & O_NONBLOCK) here
1528 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1530 * 1) verifies the passed buffer,
1531 * 2) calls buf_finish callback in the driver (if provided), in which
1532 * driver can perform any additional operations that may be required before
1533 * returning the buffer to userspace, such as cache sync,
1534 * 3) the buffer struct members are filled with relevant information for
1537 * The return values from this function are intended to be directly returned
1538 * from vidioc_dqbuf handler in driver.
1540 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1542 struct vb2_buffer
*vb
= NULL
;
1546 dprintk(1, "dqbuf: file io in progress\n");
1550 if (b
->type
!= q
->type
) {
1551 dprintk(1, "dqbuf: invalid buffer type\n");
1554 ret
= __vb2_get_done_vb(q
, &vb
, b
, nonblocking
);
1558 ret
= call_qop(q
, buf_finish
, vb
);
1560 dprintk(1, "dqbuf: buffer finish failed\n");
1564 switch (vb
->state
) {
1565 case VB2_BUF_STATE_DONE
:
1566 dprintk(3, "dqbuf: Returning done buffer\n");
1568 case VB2_BUF_STATE_ERROR
:
1569 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1572 dprintk(1, "dqbuf: Invalid buffer state\n");
1576 /* Fill buffer information for the userspace */
1577 __fill_v4l2_buffer(vb
, b
);
1578 /* Remove from videobuf queue */
1579 list_del(&vb
->queued_entry
);
1580 /* go back to dequeued state */
1583 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1584 vb
->v4l2_buf
.index
, vb
->state
);
1588 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1591 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1593 * Removes all queued buffers from driver's queue and all buffers queued by
1594 * userspace from videobuf's queue. Returns to state after reqbufs.
1596 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1601 * Tell driver to stop all transactions and release all queued
1605 call_qop(q
, stop_streaming
, q
);
1609 * Remove all buffers from videobuf's list...
1611 INIT_LIST_HEAD(&q
->queued_list
);
1613 * ...and done list; userspace will not receive any buffers it
1614 * has not already dequeued before initiating cancel.
1616 INIT_LIST_HEAD(&q
->done_list
);
1617 atomic_set(&q
->queued_count
, 0);
1618 wake_up_all(&q
->done_wq
);
1621 * Reinitialize all buffers for next use.
1623 for (i
= 0; i
< q
->num_buffers
; ++i
)
1624 __vb2_dqbuf(q
->bufs
[i
]);
1628 * vb2_streamon - start streaming
1629 * @q: videobuf2 queue
1630 * @type: type argument passed from userspace to vidioc_streamon handler
1632 * Should be called from vidioc_streamon handler of a driver.
1634 * 1) verifies current state
1635 * 2) passes any previously queued buffers to the driver and starts streaming
1637 * The return values from this function are intended to be directly returned
1638 * from vidioc_streamon handler in the driver.
1640 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1642 struct vb2_buffer
*vb
;
1646 dprintk(1, "streamon: file io in progress\n");
1650 if (type
!= q
->type
) {
1651 dprintk(1, "streamon: invalid stream type\n");
1656 dprintk(1, "streamon: already streaming\n");
1661 * If any buffers were queued before streamon,
1662 * we can now pass them to driver for processing.
1664 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1665 __enqueue_in_driver(vb
);
1668 * Let driver notice that streaming state has been enabled.
1670 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->queued_count
));
1672 dprintk(1, "streamon: driver refused to start streaming\n");
1673 __vb2_queue_cancel(q
);
1679 dprintk(3, "Streamon successful\n");
1682 EXPORT_SYMBOL_GPL(vb2_streamon
);
1686 * vb2_streamoff - stop streaming
1687 * @q: videobuf2 queue
1688 * @type: type argument passed from userspace to vidioc_streamoff handler
1690 * Should be called from vidioc_streamoff handler of a driver.
1692 * 1) verifies current state,
1693 * 2) stop streaming and dequeues any queued buffers, including those previously
1694 * passed to the driver (after waiting for the driver to finish).
1696 * This call can be used for pausing playback.
1697 * The return values from this function are intended to be directly returned
1698 * from vidioc_streamoff handler in the driver
1700 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1703 dprintk(1, "streamoff: file io in progress\n");
1707 if (type
!= q
->type
) {
1708 dprintk(1, "streamoff: invalid stream type\n");
1712 if (!q
->streaming
) {
1713 dprintk(1, "streamoff: not streaming\n");
1718 * Cancel will pause streaming and remove all buffers from the driver
1719 * and videobuf, effectively returning control over them to userspace.
1721 __vb2_queue_cancel(q
);
1723 dprintk(3, "Streamoff successful\n");
1726 EXPORT_SYMBOL_GPL(vb2_streamoff
);
1729 * __find_plane_by_offset() - find plane associated with the given offset off
1731 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
1732 unsigned int *_buffer
, unsigned int *_plane
)
1734 struct vb2_buffer
*vb
;
1735 unsigned int buffer
, plane
;
1738 * Go over all buffers and their planes, comparing the given offset
1739 * with an offset assigned to each plane. If a match is found,
1740 * return its buffer and plane numbers.
1742 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
1743 vb
= q
->bufs
[buffer
];
1745 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1746 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
1758 * vb2_expbuf() - Export a buffer as a file descriptor
1759 * @q: videobuf2 queue
1760 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1763 * The return values from this function are intended to be directly returned
1764 * from vidioc_expbuf handler in driver.
1766 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
1768 struct vb2_buffer
*vb
= NULL
;
1769 struct vb2_plane
*vb_plane
;
1771 struct dma_buf
*dbuf
;
1773 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1774 dprintk(1, "Queue is not currently set up for mmap\n");
1778 if (!q
->mem_ops
->get_dmabuf
) {
1779 dprintk(1, "Queue does not support DMA buffer exporting\n");
1783 if (eb
->flags
& ~O_CLOEXEC
) {
1784 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1788 if (eb
->type
!= q
->type
) {
1789 dprintk(1, "qbuf: invalid buffer type\n");
1793 if (eb
->index
>= q
->num_buffers
) {
1794 dprintk(1, "buffer index out of range\n");
1798 vb
= q
->bufs
[eb
->index
];
1800 if (eb
->plane
>= vb
->num_planes
) {
1801 dprintk(1, "buffer plane out of range\n");
1805 vb_plane
= &vb
->planes
[eb
->plane
];
1807 dbuf
= call_memop(q
, get_dmabuf
, vb_plane
->mem_priv
);
1808 if (IS_ERR_OR_NULL(dbuf
)) {
1809 dprintk(1, "Failed to export buffer %d, plane %d\n",
1810 eb
->index
, eb
->plane
);
1814 ret
= dma_buf_fd(dbuf
, eb
->flags
);
1816 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1817 eb
->index
, eb
->plane
, ret
);
1822 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1823 eb
->index
, eb
->plane
, ret
);
1828 EXPORT_SYMBOL_GPL(vb2_expbuf
);
1831 * vb2_mmap() - map video buffers into application address space
1832 * @q: videobuf2 queue
1833 * @vma: vma passed to the mmap file operation handler in the driver
1835 * Should be called from mmap file operation handler of a driver.
1836 * This function maps one plane of one of the available video buffers to
1837 * userspace. To map whole video memory allocated on reqbufs, this function
1838 * has to be called once per each plane per each buffer previously allocated.
1840 * When the userspace application calls mmap, it passes to it an offset returned
1841 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1842 * a "cookie", which is then used to identify the plane to be mapped.
1843 * This function finds a plane with a matching offset and a mapping is performed
1844 * by the means of a provided memory operation.
1846 * The return values from this function are intended to be directly returned
1847 * from the mmap handler in driver.
1849 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
1851 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1852 struct vb2_buffer
*vb
;
1853 unsigned int buffer
, plane
;
1856 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1857 dprintk(1, "Queue is not currently set up for mmap\n");
1862 * Check memory area access mode.
1864 if (!(vma
->vm_flags
& VM_SHARED
)) {
1865 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1868 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1869 if (!(vma
->vm_flags
& VM_WRITE
)) {
1870 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1874 if (!(vma
->vm_flags
& VM_READ
)) {
1875 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1881 * Find the plane corresponding to the offset passed by userspace.
1883 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1887 vb
= q
->bufs
[buffer
];
1889 ret
= call_memop(q
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
1893 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
1896 EXPORT_SYMBOL_GPL(vb2_mmap
);
1899 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
1902 unsigned long pgoff
,
1903 unsigned long flags
)
1905 unsigned long off
= pgoff
<< PAGE_SHIFT
;
1906 struct vb2_buffer
*vb
;
1907 unsigned int buffer
, plane
;
1910 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
1911 dprintk(1, "Queue is not currently set up for mmap\n");
1916 * Find the plane corresponding to the offset passed by userspace.
1918 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
1922 vb
= q
->bufs
[buffer
];
1924 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
1926 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
1929 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
1930 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
1933 * vb2_poll() - implements poll userspace operation
1934 * @q: videobuf2 queue
1935 * @file: file argument passed to the poll file operation handler
1936 * @wait: wait argument passed to the poll file operation handler
1938 * This function implements poll file operation handler for a driver.
1939 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1940 * be informed that the file descriptor of a video device is available for
1942 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1943 * will be reported as available for writing.
1945 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1948 * The return values from this function are intended to be directly returned
1949 * from poll handler in driver.
1951 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
1953 struct video_device
*vfd
= video_devdata(file
);
1954 unsigned long req_events
= poll_requested_events(wait
);
1955 struct vb2_buffer
*vb
= NULL
;
1956 unsigned int res
= 0;
1957 unsigned long flags
;
1959 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
1960 struct v4l2_fh
*fh
= file
->private_data
;
1962 if (v4l2_event_pending(fh
))
1964 else if (req_events
& POLLPRI
)
1965 poll_wait(file
, &fh
->wait
, wait
);
1969 * Start file I/O emulator only if streaming API has not been used yet.
1971 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
1972 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
1973 (req_events
& (POLLIN
| POLLRDNORM
))) {
1974 if (__vb2_init_fileio(q
, 1))
1975 return res
| POLLERR
;
1977 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
1978 (req_events
& (POLLOUT
| POLLWRNORM
))) {
1979 if (__vb2_init_fileio(q
, 0))
1980 return res
| POLLERR
;
1982 * Write to OUTPUT queue can be done immediately.
1984 return res
| POLLOUT
| POLLWRNORM
;
1989 * There is nothing to wait for if no buffers have already been queued.
1991 if (list_empty(&q
->queued_list
))
1992 return res
| POLLERR
;
1994 poll_wait(file
, &q
->done_wq
, wait
);
1997 * Take first buffer available for dequeuing.
1999 spin_lock_irqsave(&q
->done_lock
, flags
);
2000 if (!list_empty(&q
->done_list
))
2001 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2003 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2005 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2006 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2007 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ?
2008 res
| POLLOUT
| POLLWRNORM
:
2009 res
| POLLIN
| POLLRDNORM
;
2013 EXPORT_SYMBOL_GPL(vb2_poll
);
2016 * vb2_queue_init() - initialize a videobuf2 queue
2017 * @q: videobuf2 queue; this structure should be allocated in driver
2019 * The vb2_queue structure should be allocated by the driver. The driver is
2020 * responsible of clearing it's content and setting initial values for some
2021 * required entries before calling this function.
2022 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2023 * to the struct vb2_queue description in include/media/videobuf2-core.h
2024 * for more information.
2026 int vb2_queue_init(struct vb2_queue
*q
)
2033 WARN_ON(!q
->mem_ops
) ||
2034 WARN_ON(!q
->type
) ||
2035 WARN_ON(!q
->io_modes
) ||
2036 WARN_ON(!q
->ops
->queue_setup
) ||
2037 WARN_ON(!q
->ops
->buf_queue
))
2040 INIT_LIST_HEAD(&q
->queued_list
);
2041 INIT_LIST_HEAD(&q
->done_list
);
2042 spin_lock_init(&q
->done_lock
);
2043 init_waitqueue_head(&q
->done_wq
);
2045 if (q
->buf_struct_size
== 0)
2046 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2050 EXPORT_SYMBOL_GPL(vb2_queue_init
);
2053 * vb2_queue_release() - stop streaming, release the queue and free memory
2054 * @q: videobuf2 queue
2056 * This function stops streaming and performs necessary clean ups, including
2057 * freeing video buffer memory. The driver is responsible for freeing
2058 * the vb2_queue structure itself.
2060 void vb2_queue_release(struct vb2_queue
*q
)
2062 __vb2_cleanup_fileio(q
);
2063 __vb2_queue_cancel(q
);
2064 __vb2_queue_free(q
, q
->num_buffers
);
2066 EXPORT_SYMBOL_GPL(vb2_queue_release
);
2069 * struct vb2_fileio_buf - buffer context used by file io emulator
2071 * vb2 provides a compatibility layer and emulator of file io (read and
2072 * write) calls on top of streaming API. This structure is used for
2073 * tracking context related to the buffers.
2075 struct vb2_fileio_buf
{
2079 unsigned int queued
:1;
2083 * struct vb2_fileio_data - queue context used by file io emulator
2085 * vb2 provides a compatibility layer and emulator of file io (read and
2086 * write) calls on top of streaming API. For proper operation it required
2087 * this structure to save the driver state between each call of the read
2088 * or write function.
2090 struct vb2_fileio_data
{
2091 struct v4l2_requestbuffers req
;
2092 struct v4l2_buffer b
;
2093 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
2095 unsigned int q_count
;
2096 unsigned int dq_count
;
2101 * __vb2_init_fileio() - initialize file io emulator
2102 * @q: videobuf2 queue
2103 * @read: mode selector (1 means read, 0 means write)
2105 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2107 struct vb2_fileio_data
*fileio
;
2109 unsigned int count
= 0;
2114 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
2115 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
2119 * Check if device supports mapping buffers to kernel virtual space.
2121 if (!q
->mem_ops
->vaddr
)
2125 * Check if streaming api has not been already activated.
2127 if (q
->streaming
|| q
->num_buffers
> 0)
2131 * Start with count 1, driver can increase it in queue_setup()
2135 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2136 (read
) ? "read" : "write", count
, q
->io_flags
);
2138 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
2142 fileio
->flags
= q
->io_flags
;
2145 * Request buffers and use MMAP type to force driver
2146 * to allocate buffers by itself.
2148 fileio
->req
.count
= count
;
2149 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
2150 fileio
->req
.type
= q
->type
;
2151 ret
= vb2_reqbufs(q
, &fileio
->req
);
2156 * Check if plane_count is correct
2157 * (multiplane buffers are not supported).
2159 if (q
->bufs
[0]->num_planes
!= 1) {
2165 * Get kernel address of each buffer.
2167 for (i
= 0; i
< q
->num_buffers
; i
++) {
2168 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2169 if (fileio
->bufs
[i
].vaddr
== NULL
)
2171 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2175 * Read mode requires pre queuing of all buffers.
2179 * Queue all buffers.
2181 for (i
= 0; i
< q
->num_buffers
; i
++) {
2182 struct v4l2_buffer
*b
= &fileio
->b
;
2183 memset(b
, 0, sizeof(*b
));
2185 b
->memory
= q
->memory
;
2187 ret
= vb2_qbuf(q
, b
);
2190 fileio
->bufs
[i
].queued
= 1;
2196 ret
= vb2_streamon(q
, q
->type
);
2206 fileio
->req
.count
= 0;
2207 vb2_reqbufs(q
, &fileio
->req
);
2215 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2216 * @q: videobuf2 queue
2218 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2220 struct vb2_fileio_data
*fileio
= q
->fileio
;
2224 * Hack fileio context to enable direct calls to vb2 ioctl
2229 vb2_streamoff(q
, q
->type
);
2230 fileio
->req
.count
= 0;
2231 vb2_reqbufs(q
, &fileio
->req
);
2233 dprintk(3, "file io emulator closed\n");
2239 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2240 * @q: videobuf2 queue
2241 * @data: pointed to target userspace buffer
2242 * @count: number of bytes to read or write
2243 * @ppos: file handle position tracking pointer
2244 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2245 * @read: access mode selector (1 means read, 0 means write)
2247 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2248 loff_t
*ppos
, int nonblock
, int read
)
2250 struct vb2_fileio_data
*fileio
;
2251 struct vb2_fileio_buf
*buf
;
2254 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2255 read
? "read" : "write", (long)*ppos
, count
,
2256 nonblock
? "non" : "");
2262 * Initialize emulator on first call.
2265 ret
= __vb2_init_fileio(q
, read
);
2266 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2273 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2274 * The pointer will be restored before returning from this function.
2278 index
= fileio
->index
;
2279 buf
= &fileio
->bufs
[index
];
2282 * Check if we need to dequeue the buffer.
2285 struct vb2_buffer
*vb
;
2288 * Call vb2_dqbuf to get buffer back.
2290 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2291 fileio
->b
.type
= q
->type
;
2292 fileio
->b
.memory
= q
->memory
;
2293 fileio
->b
.index
= index
;
2294 ret
= vb2_dqbuf(q
, &fileio
->b
, nonblock
);
2295 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2298 fileio
->dq_count
+= 1;
2301 * Get number of bytes filled by the driver
2303 vb
= q
->bufs
[index
];
2304 buf
->size
= vb2_get_plane_payload(vb
, 0);
2309 * Limit count on last few bytes of the buffer.
2311 if (buf
->pos
+ count
> buf
->size
) {
2312 count
= buf
->size
- buf
->pos
;
2313 dprintk(5, "reducing read count: %zd\n", count
);
2317 * Transfer data to userspace.
2319 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2320 count
, index
, buf
->pos
);
2322 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2324 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2326 dprintk(3, "file io: error copying data\n");
2338 * Queue next buffer if required.
2340 if (buf
->pos
== buf
->size
||
2341 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2343 * Check if this is the last buffer to read.
2345 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2346 fileio
->dq_count
== 1) {
2347 dprintk(3, "file io: read limit reached\n");
2349 * Restore fileio pointer and release the context.
2352 return __vb2_cleanup_fileio(q
);
2356 * Call vb2_qbuf and give buffer to the driver.
2358 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2359 fileio
->b
.type
= q
->type
;
2360 fileio
->b
.memory
= q
->memory
;
2361 fileio
->b
.index
= index
;
2362 fileio
->b
.bytesused
= buf
->pos
;
2363 ret
= vb2_qbuf(q
, &fileio
->b
);
2364 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2369 * Buffer has been queued, update the status
2373 buf
->size
= q
->bufs
[0]->v4l2_planes
[0].length
;
2374 fileio
->q_count
+= 1;
2377 * Switch to the next buffer
2379 fileio
->index
= (index
+ 1) % q
->num_buffers
;
2382 * Start streaming if required.
2384 if (!read
&& !q
->streaming
) {
2385 ret
= vb2_streamon(q
, q
->type
);
2392 * Return proper number of bytes processed.
2398 * Restore the fileio context and block vb2 ioctl interface.
2404 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2405 loff_t
*ppos
, int nonblocking
)
2407 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2409 EXPORT_SYMBOL_GPL(vb2_read
);
2411 size_t vb2_write(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2412 loff_t
*ppos
, int nonblocking
)
2414 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 0);
2416 EXPORT_SYMBOL_GPL(vb2_write
);
2420 * The following functions are not part of the vb2 core API, but are helper
2421 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2422 * and struct vb2_ops.
2423 * They contain boilerplate code that most if not all drivers have to do
2424 * and so they simplify the driver code.
2427 /* The queue is busy if there is a owner and you are not that owner. */
2428 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
2430 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
2433 /* vb2 ioctl helpers */
2435 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
2436 struct v4l2_requestbuffers
*p
)
2438 struct video_device
*vdev
= video_devdata(file
);
2439 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
2443 if (vb2_queue_is_busy(vdev
, file
))
2445 res
= __reqbufs(vdev
->queue
, p
);
2446 /* If count == 0, then the owner has released all buffers and he
2447 is no longer owner of the queue. Otherwise we have a new owner. */
2449 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
2452 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
2454 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
2455 struct v4l2_create_buffers
*p
)
2457 struct video_device
*vdev
= video_devdata(file
);
2458 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->format
.type
);
2460 p
->index
= vdev
->queue
->num_buffers
;
2461 /* If count == 0, then just check if memory and type are valid.
2462 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2464 return res
!= -EBUSY
? res
: 0;
2467 if (vb2_queue_is_busy(vdev
, file
))
2469 res
= __create_bufs(vdev
->queue
, p
);
2471 vdev
->queue
->owner
= file
->private_data
;
2474 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
2476 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
2477 struct v4l2_buffer
*p
)
2479 struct video_device
*vdev
= video_devdata(file
);
2481 if (vb2_queue_is_busy(vdev
, file
))
2483 return vb2_prepare_buf(vdev
->queue
, p
);
2485 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
2487 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2489 struct video_device
*vdev
= video_devdata(file
);
2491 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2492 return vb2_querybuf(vdev
->queue
, p
);
2494 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
2496 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2498 struct video_device
*vdev
= video_devdata(file
);
2500 if (vb2_queue_is_busy(vdev
, file
))
2502 return vb2_qbuf(vdev
->queue
, p
);
2504 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
2506 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2508 struct video_device
*vdev
= video_devdata(file
);
2510 if (vb2_queue_is_busy(vdev
, file
))
2512 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
2514 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
2516 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2518 struct video_device
*vdev
= video_devdata(file
);
2520 if (vb2_queue_is_busy(vdev
, file
))
2522 return vb2_streamon(vdev
->queue
, i
);
2524 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
2526 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2528 struct video_device
*vdev
= video_devdata(file
);
2530 if (vb2_queue_is_busy(vdev
, file
))
2532 return vb2_streamoff(vdev
->queue
, i
);
2534 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
2536 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
2538 struct video_device
*vdev
= video_devdata(file
);
2540 if (vb2_queue_is_busy(vdev
, file
))
2542 return vb2_expbuf(vdev
->queue
, p
);
2544 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
2546 /* v4l2_file_operations helpers */
2548 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2550 struct video_device
*vdev
= video_devdata(file
);
2552 return vb2_mmap(vdev
->queue
, vma
);
2554 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
2556 int vb2_fop_release(struct file
*file
)
2558 struct video_device
*vdev
= video_devdata(file
);
2560 if (file
->private_data
== vdev
->queue
->owner
) {
2561 vb2_queue_release(vdev
->queue
);
2562 vdev
->queue
->owner
= NULL
;
2564 return v4l2_fh_release(file
);
2566 EXPORT_SYMBOL_GPL(vb2_fop_release
);
2568 ssize_t
vb2_fop_write(struct file
*file
, char __user
*buf
,
2569 size_t count
, loff_t
*ppos
)
2571 struct video_device
*vdev
= video_devdata(file
);
2572 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2575 if (lock
&& mutex_lock_interruptible(lock
))
2576 return -ERESTARTSYS
;
2577 if (vb2_queue_is_busy(vdev
, file
))
2579 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
2580 file
->f_flags
& O_NONBLOCK
);
2581 if (vdev
->queue
->fileio
)
2582 vdev
->queue
->owner
= file
->private_data
;
2588 EXPORT_SYMBOL_GPL(vb2_fop_write
);
2590 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
2591 size_t count
, loff_t
*ppos
)
2593 struct video_device
*vdev
= video_devdata(file
);
2594 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2597 if (lock
&& mutex_lock_interruptible(lock
))
2598 return -ERESTARTSYS
;
2599 if (vb2_queue_is_busy(vdev
, file
))
2601 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
2602 file
->f_flags
& O_NONBLOCK
);
2603 if (vdev
->queue
->fileio
)
2604 vdev
->queue
->owner
= file
->private_data
;
2610 EXPORT_SYMBOL_GPL(vb2_fop_read
);
2612 unsigned int vb2_fop_poll(struct file
*file
, poll_table
*wait
)
2614 struct video_device
*vdev
= video_devdata(file
);
2615 struct vb2_queue
*q
= vdev
->queue
;
2616 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
2617 unsigned long req_events
= poll_requested_events(wait
);
2620 bool must_lock
= false;
2622 /* Try to be smart: only lock if polling might start fileio,
2623 otherwise locking will only introduce unwanted delays. */
2624 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
2625 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
2626 (req_events
& (POLLIN
| POLLRDNORM
)))
2628 else if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
2629 (req_events
& (POLLOUT
| POLLWRNORM
)))
2633 /* If locking is needed, but this helper doesn't know how, then you
2634 shouldn't be using this helper but you should write your own. */
2635 WARN_ON(must_lock
&& !lock
);
2637 if (must_lock
&& lock
&& mutex_lock_interruptible(lock
))
2642 res
= vb2_poll(vdev
->queue
, file
, wait
);
2644 /* If fileio was started, then we have a new queue owner. */
2645 if (must_lock
&& !fileio
&& q
->fileio
)
2646 q
->owner
= file
->private_data
;
2647 if (must_lock
&& lock
)
2651 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
2654 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
2655 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
2657 struct video_device
*vdev
= video_devdata(file
);
2659 return vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
2661 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
2664 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2666 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
2668 mutex_unlock(vq
->lock
);
2670 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
2672 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
2674 mutex_lock(vq
->lock
);
2676 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
2678 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2679 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2680 MODULE_LICENSE("GPL");