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>
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)
50 static int __vb2_buf_mem_alloc(
struct vb2_buffer *vb)
60 if (IS_ERR_OR_NULL(mem_priv))
71 for (; plane > 0; --
plane) {
82 static void __vb2_buf_mem_free(
struct vb2_buffer *vb)
90 dprintk(3,
"Freed plane %d of buffer %d\n", plane,
99 static void __vb2_buf_userptr_put(
struct vb2_buffer *vb)
105 if (vb->
planes[plane].mem_priv)
115 static void __setup_offsets(
struct vb2_queue *q,
unsigned int n)
139 dprintk(3,
"Buffer %d, plane %d offset 0x%08lx\n",
156 unsigned int num_buffers,
unsigned int num_planes)
162 for (buffer = 0; buffer < num_buffers; ++
buffer) {
166 dprintk(1,
"Memory alloc for buffer struct failed\n");
183 ret = __vb2_buf_mem_alloc(vb);
185 dprintk(1,
"Failed allocating memory for "
186 "buffer %d\n", buffer);
197 dprintk(1,
"Buffer %d %p initialization"
198 " failed\n", buffer, vb);
199 __vb2_buf_mem_free(vb);
208 __setup_offsets(q, buffer);
210 dprintk(1,
"Allocated %d buffers, %d plane(s) each\n",
219 static void __vb2_free_mem(
struct vb2_queue *q,
unsigned int buffers)
232 __vb2_buf_mem_free(vb);
234 __vb2_buf_userptr_put(vb);
243 static void __vb2_queue_free(
struct vb2_queue *q,
unsigned int buffers)
248 if (q->
ops->buf_cleanup) {
253 q->
ops->buf_cleanup(q->
bufs[buffer]);
258 __vb2_free_mem(q, buffers);
284 dprintk(1,
"Multi-planar buffer passed but "
285 "planes array not provided\n");
290 dprintk(1,
"Incorrect planes array length, "
313 if (mem_priv &&
call_memop(q, num_users, mem_priv) > 1)
323 static bool __buffers_in_use(
struct vb2_queue *q)
327 if (__buffer_in_use(q, q->
bufs[buffer]))
391 if (__buffer_in_use(q, vb))
414 dprintk(1,
"querybuf: wrong buffer type\n");
419 dprintk(1,
"querybuf: buffer index out of range\n");
423 ret = __verify_planes_array(vb, b);
425 __fill_v4l2_buffer(vb, b);
434 static int __verify_userptr_ops(
struct vb2_queue *q)
447 static int __verify_mmap_ops(
struct vb2_queue *q)
460 static int __verify_memory_type(
struct vb2_queue *q,
464 dprintk(1,
"reqbufs: unsupported memory type\n");
468 if (type != q->
type) {
469 dprintk(1,
"reqbufs: requested type is incorrect\n");
478 dprintk(1,
"reqbufs: MMAP for current setup unsupported\n");
483 dprintk(1,
"reqbufs: USERPTR for current setup unsupported\n");
493 dprintk(1,
"reqbufs: file io in progress\n");
524 unsigned int num_buffers, allocated_buffers, num_planes = 0;
528 dprintk(1,
"reqbufs: streaming active\n");
538 dprintk(1,
"reqbufs: memory in use, cannot free\n");
564 ret =
call_qop(q, queue_setup, q,
NULL, &num_buffers, &num_planes,
570 ret = __vb2_queue_alloc(q, req->
memory, num_buffers, num_planes);
572 dprintk(1,
"Memory allocation failed\n");
576 allocated_buffers =
ret;
581 if (allocated_buffers < num_buffers) {
582 num_buffers = allocated_buffers;
587 if (!ret && allocated_buffers < num_buffers)
599 __vb2_queue_free(q, allocated_buffers);
607 req->
count = allocated_buffers;
620 int ret = __verify_memory_type(q, req->
memory, req->
type);
622 return ret ? ret : __reqbufs(q, req);
643 unsigned int num_planes = 0, num_buffers, allocated_buffers;
647 dprintk(1,
"%s(): maximum number of buffers already allocated\n",
670 ret = __vb2_queue_alloc(q, create->
memory, num_buffers,
673 dprintk(1,
"Memory allocation failed\n");
677 allocated_buffers =
ret;
682 if (ret < num_buffers) {
692 if (!ret && allocated_buffers < num_buffers)
704 __vb2_queue_free(q, allocated_buffers);
712 create->
count = allocated_buffers;
726 int ret = __verify_memory_type(q, create->
memory, create->
format.type);
729 if (create->
count == 0)
730 return ret != -
EBUSY ? ret : 0;
731 return ret ? ret : __create_bufs(q, create);
800 dprintk(4,
"Done processing on buffer %d, state: %d\n",
808 spin_unlock_irqrestore(&q->
done_lock, flags);
882 __fill_vb2_buffer(vb, b, planes);
887 vb->
v4l2_planes[plane].m.userptr == planes[plane].m.userptr
888 && vb->
v4l2_planes[plane].length == planes[plane].length)
891 dprintk(3,
"qbuf: userspace address for plane %d changed, "
892 "reacquiring memory\n", plane);
895 if (planes[plane].length < q->
plane_sizes[plane]) {
901 if (vb->
planes[plane].mem_priv)
910 planes[plane].m.userptr,
911 planes[plane].length, write);
912 if (IS_ERR_OR_NULL(mem_priv)) {
913 dprintk(1,
"qbuf: failed acquiring userspace "
914 "memory for plane %d\n", plane);
915 ret = mem_priv ? PTR_ERR(mem_priv) : -
EINVAL;
927 dprintk(1,
"qbuf: buffer initialization failed\n");
942 if (vb->
planes[plane].mem_priv)
964 static void __enqueue_in_driver(
struct vb2_buffer *vb)
970 q->
ops->buf_queue(vb);
980 ret = __qbuf_mmap(vb, b);
983 ret = __qbuf_userptr(vb, b);
986 WARN(1,
"Invalid queue type\n");
993 dprintk(1,
"qbuf: buffer preparation failed: %d\n", ret);
1021 dprintk(1,
"%s(): file io in progress\n", __func__);
1026 dprintk(1,
"%s(): invalid buffer type\n", __func__);
1031 dprintk(1,
"%s(): buffer index out of range\n", __func__);
1038 dprintk(1,
"%s(): buffer is NULL\n", __func__);
1043 dprintk(1,
"%s(): invalid memory type\n", __func__);
1048 dprintk(1,
"%s(): invalid buffer state %d\n", __func__, vb->
state);
1051 ret = __verify_planes_array(vb, b);
1054 ret = __buf_prepare(vb, b);
1058 __fill_v4l2_buffer(vb, b);
1104 mmap_sem = &
current->mm->mmap_sem;
1111 dprintk(1,
"qbuf: file io in progress\n");
1117 dprintk(1,
"qbuf: invalid buffer type\n");
1123 dprintk(1,
"qbuf: buffer index out of range\n");
1131 dprintk(1,
"qbuf: buffer is NULL\n");
1137 dprintk(1,
"qbuf: invalid memory type\n");
1141 ret = __verify_planes_array(vb, b);
1145 switch (vb->
state) {
1147 ret = __buf_prepare(vb, b);
1153 dprintk(1,
"qbuf: buffer already in use\n");
1170 __enqueue_in_driver(vb);
1173 __fill_v4l2_buffer(vb, b);
1189 static int __vb2_wait_for_done_vb(
struct vb2_queue *q,
int nonblocking)
1204 dprintk(1,
"Streaming off, will not wait for buffers\n");
1216 dprintk(1,
"Nonblocking and no buffers to dequeue, "
1231 dprintk(3,
"Will sleep waiting for buffers\n");
1241 dprintk(1,
"Sleep was interrupted\n");
1256 unsigned long flags;
1262 ret = __vb2_wait_for_done_vb(q, nonblocking);
1276 ret = __verify_planes_array(*vb, b);
1279 spin_unlock_irqrestore(&q->
done_lock, flags);
1296 dprintk(1,
"Streaming off, will not wait for buffers\n");
1332 dprintk(1,
"dqbuf: file io in progress\n");
1337 dprintk(1,
"dqbuf: invalid buffer type\n");
1340 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1346 dprintk(1,
"dqbuf: buffer finish failed\n");
1350 switch (vb->
state) {
1352 dprintk(3,
"dqbuf: Returning done buffer\n");
1355 dprintk(3,
"dqbuf: Returning done buffer with errors\n");
1358 dprintk(1,
"dqbuf: Invalid buffer state\n");
1363 __fill_v4l2_buffer(vb, b);
1367 dprintk(1,
"dqbuf of buffer %d, with state %d\n",
1381 static void __vb2_queue_cancel(
struct vb2_queue *q)
1431 dprintk(1,
"streamon: file io in progress\n");
1435 if (type != q->
type) {
1436 dprintk(1,
"streamon: invalid stream type\n");
1441 dprintk(1,
"streamon: already streaming\n");
1450 __enqueue_in_driver(vb);
1457 dprintk(1,
"streamon: driver refused to start streaming\n");
1458 __vb2_queue_cancel(q);
1464 dprintk(3,
"Streamon successful\n");
1488 dprintk(1,
"streamoff: file io in progress\n");
1492 if (type != q->
type) {
1493 dprintk(1,
"streamoff: invalid stream type\n");
1498 dprintk(1,
"streamoff: not streaming\n");
1506 __vb2_queue_cancel(q);
1508 dprintk(3,
"Streamoff successful\n");
1516 static int __find_plane_by_offset(
struct vb2_queue *q,
unsigned long off,
1517 unsigned int *
_buffer,
unsigned int *_plane)
1569 dprintk(1,
"Queue is not currently set up for mmap\n");
1576 if (!(vma->
vm_flags & VM_SHARED)) {
1577 dprintk(1,
"Invalid vma flags, VM_SHARED needed\n");
1582 dprintk(1,
"Invalid vma flags, VM_WRITE needed\n");
1587 dprintk(1,
"Invalid vma flags, VM_READ needed\n");
1595 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1605 dprintk(3,
"Buffer %d, plane %d successfully mapped\n", buffer, plane);
1614 unsigned long pgoff,
1615 unsigned long flags)
1623 dprintk(1,
"Queue is not currently set up for mmap\n");
1630 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1641 static int __vb2_init_fileio(
struct vb2_queue *q,
int read);
1642 static int __vb2_cleanup_fileio(
struct vb2_queue *q);
1666 unsigned long req_events = poll_requested_events(wait);
1668 unsigned int res = 0;
1669 unsigned long flags;
1676 else if (req_events &
POLLPRI)
1677 poll_wait(file, &fh->
wait, wait);
1686 if (__vb2_init_fileio(q, 1))
1691 if (__vb2_init_fileio(q, 0))
1706 poll_wait(file, &q->
done_wq, wait);
1715 spin_unlock_irqrestore(&q->
done_lock, flags);
1774 __vb2_cleanup_fileio(q);
1775 __vb2_queue_cancel(q);
1821 unsigned int count = 0;
1847 dprintk(3,
"setting up file io: mode %s, count %d, flags %08x\n",
1848 (read) ?
"read" :
"write", count, q->
io_flags);
1871 if (q->
bufs[0]->num_planes != 1) {
1883 fileio->
bufs[
i].size = vb2_plane_size(q->
bufs[i], 0);
1895 memset(b, 0,
sizeof(*b));
1902 fileio->
bufs[
i].queued = 1;
1918 fileio->
req.count = 0;
1930 static int __vb2_cleanup_fileio(
struct vb2_queue *q)
1942 fileio->
req.count = 0;
1945 dprintk(3,
"file io emulator closed\n");
1959 static size_t __vb2_perform_fileio(
struct vb2_queue *q,
char __user *
data,
size_t count,
1960 loff_t *ppos,
int nonblock,
int read)
1966 dprintk(3,
"file io: mode %s, offset %ld, count %zd, %sblocking\n",
1967 read ?
"read" :
"write", (
long)*ppos, count,
1968 nonblock ?
"non" :
"");
1977 ret = __vb2_init_fileio(q, read);
1978 dprintk(3,
"file io: vb2_init_fileio result: %d\n", ret);
1990 index = fileio->
index;
2002 memset(&fileio->
b, 0,
sizeof(fileio->
b));
2003 fileio->
b.type = q->
type;
2007 dprintk(5,
"file io: vb2_dqbuf result: %d\n", ret);
2016 buf->
size = vb2_get_plane_payload(vb, 0);
2023 if (buf->
pos + count > buf->
size) {
2025 dprintk(5,
"reducing read count: %zd\n", count);
2031 dprintk(3,
"file io: copying %zd bytes - buffer %d, offset %u\n",
2032 count, index, buf->
pos);
2038 dprintk(3,
"file io: error copying data\n");
2059 dprintk(3,
"file io: read limit reached\n");
2064 return __vb2_cleanup_fileio(q);
2070 memset(&fileio->
b, 0,
sizeof(fileio->
b));
2071 fileio->
b.type = q->
type;
2074 fileio->
b.bytesused = buf->
pos;
2076 dprintk(5,
"file io: vb2_dbuf result: %d\n", ret);
2085 buf->
size = q->
bufs[0]->v4l2_planes[0].length;
2117 loff_t *ppos,
int nonblocking)
2119 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2124 loff_t *ppos,
int nonblocking)
2126 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2155 if (vb2_queue_is_busy(vdev, file))
2157 res = __reqbufs(vdev->
queue, p);
2176 return res != -
EBUSY ? res : 0;
2179 if (vb2_queue_is_busy(vdev, file))
2181 res = __create_bufs(vdev->
queue, p);
2193 if (vb2_queue_is_busy(vdev, file))
2212 if (vb2_queue_is_busy(vdev, file))
2222 if (vb2_queue_is_busy(vdev, file))
2232 if (vb2_queue_is_busy(vdev, file))
2242 if (vb2_queue_is_busy(vdev, file))
2271 size_t count, loff_t *ppos)
2279 if (vb2_queue_is_busy(vdev, file))
2283 if (vdev->
queue->fileio)
2293 size_t count, loff_t *ppos)
2301 if (vb2_queue_is_busy(vdev, file))
2305 if (vdev->
queue->fileio)
2319 unsigned long req_events = poll_requested_events(wait);
2322 bool must_lock =
false;
2347 if (must_lock && !fileio && q->
fileio)
2349 if (must_lock && lock)
2357 unsigned long len,
unsigned long pgoff,
unsigned long flags)