Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1 | /* |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 2 | * videobuf2-core.c - video buffer 2 core framework |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
| 5 | * |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 6 | * Author: Pawel Osciak <pawel@osciak.com> |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 7 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 8 | * |
Hans Verkuil | 3415a89 | 2014-04-14 07:33:00 -0300 | [diff] [blame] | 9 | * The vb2_thread implementation was based on code from videobuf-dvb.c: |
| 10 | * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs] |
| 11 | * |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/poll.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/sched.h> |
Hans Verkuil | 3415a89 | 2014-04-14 07:33:00 -0300 | [diff] [blame] | 24 | #include <linux/freezer.h> |
| 25 | #include <linux/kthread.h> |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 26 | |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 27 | #include <media/videobuf2-core.h> |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 28 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 29 | #include <trace/events/vb2.h> |
Philipp Zabel | 2091f51 | 2015-07-10 10:49:26 -0300 | [diff] [blame] | 30 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 31 | static int debug; |
| 32 | module_param(debug, int, 0644); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 33 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 34 | #define dprintk(level, fmt, arg...) \ |
| 35 | do { \ |
| 36 | if (debug >= level) \ |
| 37 | pr_info("vb2-core: %s: " fmt, __func__, ## arg); \ |
| 38 | } while (0) |
| 39 | |
| 40 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 41 | |
| 42 | /* |
| 43 | * If advanced debugging is on, then count how often each op is called |
| 44 | * successfully, which can either be per-buffer or per-queue. |
| 45 | * |
| 46 | * This makes it easy to check that the 'init' and 'cleanup' |
| 47 | * (and variations thereof) stay balanced. |
| 48 | */ |
| 49 | |
| 50 | #define log_memop(vb, op) \ |
| 51 | dprintk(2, "call_memop(%p, %d, %s)%s\n", \ |
| 52 | (vb)->vb2_queue, (vb)->index, #op, \ |
| 53 | (vb)->vb2_queue->mem_ops->op ? "" : " (nop)") |
| 54 | |
| 55 | #define call_memop(vb, op, args...) \ |
| 56 | ({ \ |
| 57 | struct vb2_queue *_q = (vb)->vb2_queue; \ |
| 58 | int err; \ |
| 59 | \ |
| 60 | log_memop(vb, op); \ |
| 61 | err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \ |
| 62 | if (!err) \ |
| 63 | (vb)->cnt_mem_ ## op++; \ |
| 64 | err; \ |
| 65 | }) |
| 66 | |
| 67 | #define call_ptr_memop(vb, op, args...) \ |
| 68 | ({ \ |
| 69 | struct vb2_queue *_q = (vb)->vb2_queue; \ |
| 70 | void *ptr; \ |
| 71 | \ |
| 72 | log_memop(vb, op); \ |
| 73 | ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \ |
| 74 | if (!IS_ERR_OR_NULL(ptr)) \ |
| 75 | (vb)->cnt_mem_ ## op++; \ |
| 76 | ptr; \ |
| 77 | }) |
| 78 | |
| 79 | #define call_void_memop(vb, op, args...) \ |
| 80 | ({ \ |
| 81 | struct vb2_queue *_q = (vb)->vb2_queue; \ |
| 82 | \ |
| 83 | log_memop(vb, op); \ |
| 84 | if (_q->mem_ops->op) \ |
| 85 | _q->mem_ops->op(args); \ |
| 86 | (vb)->cnt_mem_ ## op++; \ |
| 87 | }) |
| 88 | |
| 89 | #define log_qop(q, op) \ |
| 90 | dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \ |
| 91 | (q)->ops->op ? "" : " (nop)") |
| 92 | |
| 93 | #define call_qop(q, op, args...) \ |
| 94 | ({ \ |
| 95 | int err; \ |
| 96 | \ |
| 97 | log_qop(q, op); \ |
| 98 | err = (q)->ops->op ? (q)->ops->op(args) : 0; \ |
| 99 | if (!err) \ |
| 100 | (q)->cnt_ ## op++; \ |
| 101 | err; \ |
| 102 | }) |
| 103 | |
| 104 | #define call_void_qop(q, op, args...) \ |
| 105 | ({ \ |
| 106 | log_qop(q, op); \ |
| 107 | if ((q)->ops->op) \ |
| 108 | (q)->ops->op(args); \ |
| 109 | (q)->cnt_ ## op++; \ |
| 110 | }) |
| 111 | |
| 112 | #define log_vb_qop(vb, op, args...) \ |
| 113 | dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \ |
| 114 | (vb)->vb2_queue, (vb)->index, #op, \ |
| 115 | (vb)->vb2_queue->ops->op ? "" : " (nop)") |
| 116 | |
| 117 | #define call_vb_qop(vb, op, args...) \ |
| 118 | ({ \ |
| 119 | int err; \ |
| 120 | \ |
| 121 | log_vb_qop(vb, op); \ |
| 122 | err = (vb)->vb2_queue->ops->op ? \ |
| 123 | (vb)->vb2_queue->ops->op(args) : 0; \ |
| 124 | if (!err) \ |
| 125 | (vb)->cnt_ ## op++; \ |
| 126 | err; \ |
| 127 | }) |
| 128 | |
| 129 | #define call_void_vb_qop(vb, op, args...) \ |
| 130 | ({ \ |
| 131 | log_vb_qop(vb, op); \ |
| 132 | if ((vb)->vb2_queue->ops->op) \ |
| 133 | (vb)->vb2_queue->ops->op(args); \ |
| 134 | (vb)->cnt_ ## op++; \ |
| 135 | }) |
| 136 | |
| 137 | #else |
| 138 | |
| 139 | #define call_memop(vb, op, args...) \ |
| 140 | ((vb)->vb2_queue->mem_ops->op ? \ |
| 141 | (vb)->vb2_queue->mem_ops->op(args) : 0) |
| 142 | |
| 143 | #define call_ptr_memop(vb, op, args...) \ |
| 144 | ((vb)->vb2_queue->mem_ops->op ? \ |
| 145 | (vb)->vb2_queue->mem_ops->op(args) : NULL) |
| 146 | |
| 147 | #define call_void_memop(vb, op, args...) \ |
| 148 | do { \ |
| 149 | if ((vb)->vb2_queue->mem_ops->op) \ |
| 150 | (vb)->vb2_queue->mem_ops->op(args); \ |
| 151 | } while (0) |
| 152 | |
| 153 | #define call_qop(q, op, args...) \ |
| 154 | ((q)->ops->op ? (q)->ops->op(args) : 0) |
| 155 | |
| 156 | #define call_void_qop(q, op, args...) \ |
| 157 | do { \ |
| 158 | if ((q)->ops->op) \ |
| 159 | (q)->ops->op(args); \ |
| 160 | } while (0) |
| 161 | |
| 162 | #define call_vb_qop(vb, op, args...) \ |
| 163 | ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0) |
| 164 | |
| 165 | #define call_void_vb_qop(vb, op, args...) \ |
| 166 | do { \ |
| 167 | if ((vb)->vb2_queue->ops->op) \ |
| 168 | (vb)->vb2_queue->ops->op(args); \ |
| 169 | } while (0) |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | #define call_bufop(q, op, args...) \ |
| 174 | ({ \ |
| 175 | int ret = 0; \ |
| 176 | if (q && q->buf_ops && q->buf_ops->op) \ |
| 177 | ret = q->buf_ops->op(args); \ |
| 178 | ret; \ |
| 179 | }) |
Marek Szyprowski | ea42c8e | 2011-04-12 10:14:13 -0300 | [diff] [blame] | 180 | |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 181 | #define call_void_bufop(q, op, args...) \ |
| 182 | ({ \ |
| 183 | if (q && q->buf_ops && q->buf_ops->op) \ |
| 184 | q->buf_ops->op(args); \ |
| 185 | }) |
| 186 | |
Hans Verkuil | fb64dca | 2014-02-28 12:49:18 -0300 | [diff] [blame] | 187 | static void __vb2_queue_cancel(struct vb2_queue *q); |
Hans Verkuil | ce0eff0 | 2015-01-20 12:18:16 -0300 | [diff] [blame] | 188 | static void __enqueue_in_driver(struct vb2_buffer *vb); |
Hans Verkuil | fb64dca | 2014-02-28 12:49:18 -0300 | [diff] [blame] | 189 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 190 | /** |
| 191 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer |
| 192 | */ |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 193 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 194 | { |
| 195 | struct vb2_queue *q = vb->vb2_queue; |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 196 | enum dma_data_direction dma_dir = |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 197 | q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 198 | void *mem_priv; |
| 199 | int plane; |
| 200 | |
Mauro Carvalho Chehab | 7f84145 | 2013-04-19 07:18:01 -0300 | [diff] [blame] | 201 | /* |
| 202 | * Allocate memory for all planes in this buffer |
| 203 | * NOTE: mmapped areas should be page aligned |
| 204 | */ |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 205 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 206 | unsigned long size = PAGE_ALIGN(vb->planes[plane].length); |
Mauro Carvalho Chehab | 7f84145 | 2013-04-19 07:18:01 -0300 | [diff] [blame] | 207 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 208 | mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane], |
Hans Verkuil | d935c57 | 2014-11-18 09:50:59 -0300 | [diff] [blame] | 209 | size, dma_dir, q->gfp_flags); |
Guennadi Liakhovetski | 62a7943 | 2011-03-22 09:24:58 -0300 | [diff] [blame] | 210 | if (IS_ERR_OR_NULL(mem_priv)) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 211 | goto free; |
| 212 | |
| 213 | /* Associate allocator private data with this plane */ |
| 214 | vb->planes[plane].mem_priv = mem_priv; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | free: |
| 219 | /* Free already allocated memory if one of the allocations failed */ |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 220 | for (; plane > 0; --plane) { |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 221 | call_void_memop(vb, put, vb->planes[plane - 1].mem_priv); |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 222 | vb->planes[plane - 1].mem_priv = NULL; |
| 223 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 224 | |
| 225 | return -ENOMEM; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * __vb2_buf_mem_free() - free memory of the given buffer |
| 230 | */ |
| 231 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) |
| 232 | { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 233 | unsigned int plane; |
| 234 | |
| 235 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 236 | call_void_memop(vb, put, vb->planes[plane].mem_priv); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 237 | vb->planes[plane].mem_priv = NULL; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 238 | dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * __vb2_buf_userptr_put() - release userspace memory associated with |
| 244 | * a USERPTR buffer |
| 245 | */ |
| 246 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) |
| 247 | { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 248 | unsigned int plane; |
| 249 | |
| 250 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 251 | if (vb->planes[plane].mem_priv) |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 252 | call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv); |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 253 | vb->planes[plane].mem_priv = NULL; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 258 | * __vb2_plane_dmabuf_put() - release memory associated with |
| 259 | * a DMABUF shared plane |
| 260 | */ |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 261 | static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p) |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 262 | { |
| 263 | if (!p->mem_priv) |
| 264 | return; |
| 265 | |
| 266 | if (p->dbuf_mapped) |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 267 | call_void_memop(vb, unmap_dmabuf, p->mem_priv); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 268 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 269 | call_void_memop(vb, detach_dmabuf, p->mem_priv); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 270 | dma_buf_put(p->dbuf); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 271 | p->mem_priv = NULL; |
| 272 | p->dbuf = NULL; |
| 273 | p->dbuf_mapped = 0; |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * __vb2_buf_dmabuf_put() - release memory associated with |
| 278 | * a DMABUF shared buffer |
| 279 | */ |
| 280 | static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb) |
| 281 | { |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 282 | unsigned int plane; |
| 283 | |
| 284 | for (plane = 0; plane < vb->num_planes; ++plane) |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 285 | __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 289 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 290 | * the buffer. |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 291 | */ |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 292 | static void __setup_offsets(struct vb2_buffer *vb) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 293 | { |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 294 | struct vb2_queue *q = vb->vb2_queue; |
| 295 | unsigned int plane; |
| 296 | unsigned long off = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 297 | |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 298 | if (vb->index) { |
| 299 | struct vb2_buffer *prev = q->bufs[vb->index - 1]; |
| 300 | struct vb2_plane *p = &prev->planes[prev->num_planes - 1]; |
| 301 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 302 | off = PAGE_ALIGN(p->m.offset + p->length); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 303 | } |
| 304 | |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 305 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 306 | vb->planes[plane].m.offset = off; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 307 | |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 308 | dprintk(3, "buffer %d, plane %d offset 0x%08lx\n", |
| 309 | vb->index, plane, off); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 310 | |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 311 | off += vb->planes[plane].length; |
| 312 | off = PAGE_ALIGN(off); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) |
| 318 | * video buffer memory for all buffers/planes on the queue and initializes the |
| 319 | * queue |
| 320 | * |
| 321 | * Returns the number of buffers successfully allocated. |
| 322 | */ |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 323 | static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 324 | unsigned int num_buffers, unsigned int num_planes, |
| 325 | const unsigned plane_sizes[VB2_MAX_PLANES]) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 326 | { |
Hans Verkuil | 489648a | 2015-11-20 09:25:52 -0200 | [diff] [blame] | 327 | unsigned int buffer, plane; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 328 | struct vb2_buffer *vb; |
| 329 | int ret; |
| 330 | |
| 331 | for (buffer = 0; buffer < num_buffers; ++buffer) { |
| 332 | /* Allocate videobuf buffer structures */ |
| 333 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); |
| 334 | if (!vb) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 335 | dprintk(1, "memory alloc for buffer struct failed\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 339 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 340 | vb->vb2_queue = q; |
| 341 | vb->num_planes = num_planes; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 342 | vb->index = q->num_buffers + buffer; |
| 343 | vb->type = q->type; |
| 344 | vb->memory = memory; |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 345 | for (plane = 0; plane < num_planes; ++plane) { |
| 346 | vb->planes[plane].length = plane_sizes[plane]; |
| 347 | vb->planes[plane].min_length = plane_sizes[plane]; |
| 348 | } |
Hans Verkuil | e32f856 | 2015-11-20 09:31:11 -0200 | [diff] [blame] | 349 | q->bufs[vb->index] = vb; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 350 | |
| 351 | /* Allocate video buffer memory for the MMAP type */ |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 352 | if (memory == VB2_MEMORY_MMAP) { |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 353 | ret = __vb2_buf_mem_alloc(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 354 | if (ret) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 355 | dprintk(1, "failed allocating memory for " |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 356 | "buffer %d\n", buffer); |
Hans Verkuil | e32f856 | 2015-11-20 09:31:11 -0200 | [diff] [blame] | 357 | q->bufs[vb->index] = NULL; |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 358 | kfree(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 359 | break; |
| 360 | } |
Hans Verkuil | 20eedf0e1 | 2015-11-20 09:36:49 -0200 | [diff] [blame] | 361 | __setup_offsets(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 362 | /* |
| 363 | * Call the driver-provided buffer initialization |
| 364 | * callback, if given. An error in initialization |
| 365 | * results in queue setup failure. |
| 366 | */ |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 367 | ret = call_vb_qop(vb, buf_init, vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 368 | if (ret) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 369 | dprintk(1, "buffer %d %p initialization" |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 370 | " failed\n", buffer, vb); |
| 371 | __vb2_buf_mem_free(vb); |
Hans Verkuil | e32f856 | 2015-11-20 09:31:11 -0200 | [diff] [blame] | 372 | q->bufs[vb->index] = NULL; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 373 | kfree(vb); |
| 374 | break; |
| 375 | } |
| 376 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 377 | } |
| 378 | |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 379 | dprintk(1, "allocated %d buffers, %d plane(s) each\n", |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 380 | buffer, num_planes); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 381 | |
| 382 | return buffer; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * __vb2_free_mem() - release all video buffer memory for a given queue |
| 387 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 388 | static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 389 | { |
| 390 | unsigned int buffer; |
| 391 | struct vb2_buffer *vb; |
| 392 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 393 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 394 | ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 395 | vb = q->bufs[buffer]; |
| 396 | if (!vb) |
| 397 | continue; |
| 398 | |
| 399 | /* Free MMAP buffers or release USERPTR buffers */ |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 400 | if (q->memory == VB2_MEMORY_MMAP) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 401 | __vb2_buf_mem_free(vb); |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 402 | else if (q->memory == VB2_MEMORY_DMABUF) |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 403 | __vb2_buf_dmabuf_put(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 404 | else |
| 405 | __vb2_buf_userptr_put(vb); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 410 | * __vb2_queue_free() - free buffers at the end of the queue - video memory and |
| 411 | * related information, if no buffers are left return the queue to an |
| 412 | * uninitialized state. Might be called even if the queue has already been freed. |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 413 | */ |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 414 | static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 415 | { |
| 416 | unsigned int buffer; |
| 417 | |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 418 | /* |
| 419 | * Sanity check: when preparing a buffer the queue lock is released for |
| 420 | * a short while (see __buf_prepare for the details), which would allow |
| 421 | * a race with a reqbufs which can call this function. Removing the |
| 422 | * buffers from underneath __buf_prepare is obviously a bad idea, so we |
| 423 | * check if any of the buffers is in the state PREPARING, and if so we |
| 424 | * just return -EAGAIN. |
| 425 | */ |
| 426 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 427 | ++buffer) { |
| 428 | if (q->bufs[buffer] == NULL) |
| 429 | continue; |
| 430 | if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 431 | dprintk(1, "preparing buffers, cannot free\n"); |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 432 | return -EAGAIN; |
| 433 | } |
| 434 | } |
| 435 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 436 | /* Call driver-provided cleanup function for each buffer, if provided */ |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 437 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 438 | ++buffer) { |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 439 | struct vb2_buffer *vb = q->bufs[buffer]; |
| 440 | |
| 441 | if (vb && vb->planes[0].mem_priv) |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 442 | call_void_vb_qop(vb, buf_cleanup, vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Release video buffer memory */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 446 | __vb2_free_mem(q, buffers); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 447 | |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 448 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 449 | /* |
| 450 | * Check that all the calls were balances during the life-time of this |
| 451 | * queue. If not (or if the debug level is 1 or up), then dump the |
| 452 | * counters to the kernel log. |
| 453 | */ |
| 454 | if (q->num_buffers) { |
| 455 | bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || |
| 456 | q->cnt_wait_prepare != q->cnt_wait_finish; |
| 457 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 458 | if (unbalanced || debug) { |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 459 | pr_info("vb2: counters for queue %p:%s\n", q, |
| 460 | unbalanced ? " UNBALANCED!" : ""); |
| 461 | pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n", |
| 462 | q->cnt_queue_setup, q->cnt_start_streaming, |
| 463 | q->cnt_stop_streaming); |
| 464 | pr_info("vb2: wait_prepare: %u wait_finish: %u\n", |
| 465 | q->cnt_wait_prepare, q->cnt_wait_finish); |
| 466 | } |
| 467 | q->cnt_queue_setup = 0; |
| 468 | q->cnt_wait_prepare = 0; |
| 469 | q->cnt_wait_finish = 0; |
| 470 | q->cnt_start_streaming = 0; |
| 471 | q->cnt_stop_streaming = 0; |
| 472 | } |
| 473 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 474 | struct vb2_buffer *vb = q->bufs[buffer]; |
| 475 | bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put || |
| 476 | vb->cnt_mem_prepare != vb->cnt_mem_finish || |
| 477 | vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr || |
| 478 | vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf || |
| 479 | vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf || |
| 480 | vb->cnt_buf_queue != vb->cnt_buf_done || |
| 481 | vb->cnt_buf_prepare != vb->cnt_buf_finish || |
| 482 | vb->cnt_buf_init != vb->cnt_buf_cleanup; |
| 483 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 484 | if (unbalanced || debug) { |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 485 | pr_info("vb2: counters for queue %p, buffer %d:%s\n", |
| 486 | q, buffer, unbalanced ? " UNBALANCED!" : ""); |
| 487 | pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n", |
| 488 | vb->cnt_buf_init, vb->cnt_buf_cleanup, |
| 489 | vb->cnt_buf_prepare, vb->cnt_buf_finish); |
| 490 | pr_info("vb2: buf_queue: %u buf_done: %u\n", |
| 491 | vb->cnt_buf_queue, vb->cnt_buf_done); |
| 492 | pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n", |
| 493 | vb->cnt_mem_alloc, vb->cnt_mem_put, |
| 494 | vb->cnt_mem_prepare, vb->cnt_mem_finish, |
| 495 | vb->cnt_mem_mmap); |
| 496 | pr_info("vb2: get_userptr: %u put_userptr: %u\n", |
| 497 | vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr); |
| 498 | pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n", |
| 499 | vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf, |
| 500 | vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf); |
| 501 | pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n", |
| 502 | vb->cnt_mem_get_dmabuf, |
| 503 | vb->cnt_mem_num_users, |
| 504 | vb->cnt_mem_vaddr, |
| 505 | vb->cnt_mem_cookie); |
| 506 | } |
| 507 | } |
| 508 | #endif |
| 509 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 510 | /* Free videobuf buffers */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 511 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
| 512 | ++buffer) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 513 | kfree(q->bufs[buffer]); |
| 514 | q->bufs[buffer] = NULL; |
| 515 | } |
| 516 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 517 | q->num_buffers -= buffers; |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 518 | if (!q->num_buffers) { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 519 | q->memory = 0; |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 520 | INIT_LIST_HEAD(&q->queued_list); |
| 521 | } |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 522 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 526 | * vb2_buffer_in_use() - return true if the buffer is in use and |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 527 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 528 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 529 | bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 530 | { |
| 531 | unsigned int plane; |
| 532 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Marek Szyprowski | 2c2dd6ac | 2011-10-12 13:09:53 -0300 | [diff] [blame] | 533 | void *mem_priv = vb->planes[plane].mem_priv; |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 534 | /* |
| 535 | * If num_users() has not been provided, call_memop |
| 536 | * will return 0, apparently nobody cares about this |
| 537 | * case anyway. If num_users() returns more than 1, |
| 538 | * we are not the only user of the plane's memory. |
| 539 | */ |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 540 | if (mem_priv && call_memop(vb, num_users, mem_priv) > 1) |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 541 | return true; |
| 542 | } |
| 543 | return false; |
| 544 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 545 | EXPORT_SYMBOL(vb2_buffer_in_use); |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 546 | |
| 547 | /** |
| 548 | * __buffers_in_use() - return true if any buffers on the queue are in use and |
| 549 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 550 | */ |
| 551 | static bool __buffers_in_use(struct vb2_queue *q) |
| 552 | { |
| 553 | unsigned int buffer; |
| 554 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 555 | if (vb2_buffer_in_use(q, q->bufs[buffer])) |
Marek Szyprowski | 25a27d9 | 2011-08-24 06:49:35 -0300 | [diff] [blame] | 556 | return true; |
| 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 562 | * vb2_core_querybuf() - query video buffer information |
| 563 | * @q: videobuf queue |
| 564 | * @index: id number of the buffer |
| 565 | * @pb: buffer struct passed from userspace |
| 566 | * |
| 567 | * Should be called from vidioc_querybuf ioctl handler in driver. |
| 568 | * The passed buffer should have been verified. |
| 569 | * This function fills the relevant information for the userspace. |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 570 | */ |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 571 | void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb) |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 572 | { |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 573 | call_void_bufop(q, fill_user_buffer, q->bufs[index], pb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 574 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 575 | EXPORT_SYMBOL_GPL(vb2_core_querybuf); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * __verify_userptr_ops() - verify that all memory operations required for |
| 579 | * USERPTR queue type have been provided |
| 580 | */ |
| 581 | static int __verify_userptr_ops(struct vb2_queue *q) |
| 582 | { |
| 583 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || |
| 584 | !q->mem_ops->put_userptr) |
| 585 | return -EINVAL; |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * __verify_mmap_ops() - verify that all memory operations required for |
| 592 | * MMAP queue type have been provided |
| 593 | */ |
| 594 | static int __verify_mmap_ops(struct vb2_queue *q) |
| 595 | { |
| 596 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || |
| 597 | !q->mem_ops->put || !q->mem_ops->mmap) |
| 598 | return -EINVAL; |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 604 | * __verify_dmabuf_ops() - verify that all memory operations required for |
| 605 | * DMABUF queue type have been provided |
| 606 | */ |
| 607 | static int __verify_dmabuf_ops(struct vb2_queue *q) |
| 608 | { |
| 609 | if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf || |
| 610 | !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf || |
| 611 | !q->mem_ops->unmap_dmabuf) |
| 612 | return -EINVAL; |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 618 | * vb2_verify_memory_type() - Check whether the memory type and buffer type |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 619 | * passed to a buffer operation are compatible with the queue. |
| 620 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 621 | int vb2_verify_memory_type(struct vb2_queue *q, |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 622 | enum vb2_memory memory, unsigned int type) |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 623 | { |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 624 | if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR && |
| 625 | memory != VB2_MEMORY_DMABUF) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 626 | dprintk(1, "unsupported memory type\n"); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 627 | return -EINVAL; |
| 628 | } |
| 629 | |
| 630 | if (type != q->type) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 631 | dprintk(1, "requested type is incorrect\n"); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 632 | return -EINVAL; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Make sure all the required memory ops for given memory type |
| 637 | * are available. |
| 638 | */ |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 639 | if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 640 | dprintk(1, "MMAP for current setup unsupported\n"); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 641 | return -EINVAL; |
| 642 | } |
| 643 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 644 | if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 645 | dprintk(1, "USERPTR for current setup unsupported\n"); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 646 | return -EINVAL; |
| 647 | } |
| 648 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 649 | if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 650 | dprintk(1, "DMABUF for current setup unsupported\n"); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | } |
| 653 | |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 654 | /* |
| 655 | * Place the busy tests at the end: -EBUSY can be ignored when |
| 656 | * create_bufs is called with count == 0, but count == 0 should still |
| 657 | * do the memory and type validation. |
| 658 | */ |
Hans Verkuil | 74753cffa | 2014-04-07 09:23:50 -0300 | [diff] [blame] | 659 | if (vb2_fileio_is_active(q)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 660 | dprintk(1, "file io in progress\n"); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 661 | return -EBUSY; |
| 662 | } |
| 663 | return 0; |
| 664 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 665 | EXPORT_SYMBOL(vb2_verify_memory_type); |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 666 | |
| 667 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 668 | * vb2_core_reqbufs() - Initiate streaming |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 669 | * @q: videobuf2 queue |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 670 | * @memory: memory type |
| 671 | * @count: requested buffer count |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 672 | * |
| 673 | * Should be called from vidioc_reqbufs ioctl handler of a driver. |
| 674 | * This function: |
| 675 | * 1) verifies streaming parameters passed from the userspace, |
| 676 | * 2) sets up the queue, |
| 677 | * 3) negotiates number of buffers and planes per buffer with the driver |
| 678 | * to be used during streaming, |
| 679 | * 4) allocates internal buffer structures (struct vb2_buffer), according to |
| 680 | * the agreed parameters, |
| 681 | * 5) for MMAP memory type, allocates actual video memory, using the |
| 682 | * memory handling/allocation routines provided during queue initialization |
| 683 | * |
| 684 | * If req->count is 0, all the memory will be freed instead. |
| 685 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call |
| 686 | * and the queue is not busy, memory will be reallocated. |
| 687 | * |
| 688 | * The return values from this function are intended to be directly returned |
| 689 | * from vidioc_reqbufs handler in driver. |
| 690 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 691 | int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 692 | unsigned int *count) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 693 | { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 694 | unsigned int num_buffers, allocated_buffers, num_planes = 0; |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 695 | unsigned plane_sizes[VB2_MAX_PLANES] = { }; |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 696 | int ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 697 | |
| 698 | if (q->streaming) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 699 | dprintk(1, "streaming active\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 700 | return -EBUSY; |
| 701 | } |
| 702 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 703 | if (*count == 0 || q->num_buffers != 0 || q->memory != memory) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 704 | /* |
| 705 | * We already have buffers allocated, so first check if they |
| 706 | * are not in use and can be freed. |
| 707 | */ |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 708 | mutex_lock(&q->mmap_lock); |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 709 | if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) { |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 710 | mutex_unlock(&q->mmap_lock); |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 711 | dprintk(1, "memory in use, cannot free\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 712 | return -EBUSY; |
| 713 | } |
| 714 | |
Hans Verkuil | fb64dca | 2014-02-28 12:49:18 -0300 | [diff] [blame] | 715 | /* |
| 716 | * Call queue_cancel to clean up any buffers in the PREPARED or |
| 717 | * QUEUED state which is possible if buffers were prepared or |
| 718 | * queued without ever calling STREAMON. |
| 719 | */ |
| 720 | __vb2_queue_cancel(q); |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 721 | ret = __vb2_queue_free(q, q->num_buffers); |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 722 | mutex_unlock(&q->mmap_lock); |
Hans Verkuil | 63faabf | 2013-12-13 13:13:40 -0300 | [diff] [blame] | 723 | if (ret) |
| 724 | return ret; |
Marek Szyprowski | 29e3fbd | 2011-03-09 14:03:24 -0300 | [diff] [blame] | 725 | |
| 726 | /* |
| 727 | * In case of REQBUFS(0) return immediately without calling |
| 728 | * driver's queue_setup() callback and allocating resources. |
| 729 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 730 | if (*count == 0) |
Marek Szyprowski | 29e3fbd | 2011-03-09 14:03:24 -0300 | [diff] [blame] | 731 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Make sure the requested values and current defaults are sane. |
| 736 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 737 | num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME); |
Philipp Zabel | 4cf743d | 2014-05-09 12:32:10 -0300 | [diff] [blame] | 738 | num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 739 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 740 | q->memory = memory; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * Ask the driver how many buffers and planes per buffer it requires. |
| 744 | * Driver also sets the size and allocator context for each plane. |
| 745 | */ |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 746 | ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 747 | plane_sizes, q->alloc_ctx); |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 748 | if (ret) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 749 | return ret; |
| 750 | |
| 751 | /* Finally, allocate buffers and video memory */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 752 | allocated_buffers = |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 753 | __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 754 | if (allocated_buffers == 0) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 755 | dprintk(1, "memory allocation failed\n"); |
Marek Szyprowski | 66072d4 | 2011-06-28 08:29:02 -0300 | [diff] [blame] | 756 | return -ENOMEM; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 760 | * There is no point in continuing if we can't allocate the minimum |
| 761 | * number of buffers needed by this vb2_queue. |
| 762 | */ |
| 763 | if (allocated_buffers < q->min_buffers_needed) |
| 764 | ret = -ENOMEM; |
| 765 | |
| 766 | /* |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 767 | * Check if driver can handle the allocated number of buffers. |
| 768 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 769 | if (!ret && allocated_buffers < num_buffers) { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 770 | num_buffers = allocated_buffers; |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 771 | /* |
| 772 | * num_planes is set by the previous queue_setup(), but since it |
| 773 | * signals to queue_setup() whether it is called from create_bufs() |
| 774 | * vs reqbufs() we zero it here to signal that queue_setup() is |
| 775 | * called for the reqbufs() case. |
| 776 | */ |
| 777 | num_planes = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 778 | |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 779 | ret = call_qop(q, queue_setup, q, &num_buffers, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 780 | &num_planes, plane_sizes, q->alloc_ctx); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 781 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 782 | if (!ret && allocated_buffers < num_buffers) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 783 | ret = -ENOMEM; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 784 | |
| 785 | /* |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 786 | * Either the driver has accepted a smaller number of buffers, |
| 787 | * or .queue_setup() returned an error |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 788 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 789 | } |
| 790 | |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 791 | mutex_lock(&q->mmap_lock); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 792 | q->num_buffers = allocated_buffers; |
| 793 | |
| 794 | if (ret < 0) { |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 795 | /* |
| 796 | * Note: __vb2_queue_free() will subtract 'allocated_buffers' |
| 797 | * from q->num_buffers. |
| 798 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 799 | __vb2_queue_free(q, allocated_buffers); |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 800 | mutex_unlock(&q->mmap_lock); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 801 | return ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 802 | } |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 803 | mutex_unlock(&q->mmap_lock); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 804 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 805 | /* |
| 806 | * Return the number of successfully allocated buffers |
| 807 | * to the userspace. |
| 808 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 809 | *count = allocated_buffers; |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 810 | q->waiting_for_buffers = !q->is_output; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 811 | |
| 812 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 813 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(vb2_core_reqbufs); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 815 | |
| 816 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 817 | * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 818 | * @q: videobuf2 queue |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 819 | * @memory: memory type |
| 820 | * @count: requested buffer count |
| 821 | * @parg: parameter passed to device driver |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 822 | * |
| 823 | * Should be called from vidioc_create_bufs ioctl handler of a driver. |
| 824 | * This function: |
| 825 | * 1) verifies parameter sanity |
| 826 | * 2) calls the .queue_setup() queue operation |
| 827 | * 3) performs any necessary memory allocations |
| 828 | * |
| 829 | * The return values from this function are intended to be directly returned |
| 830 | * from vidioc_create_bufs handler in driver. |
| 831 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 832 | int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 833 | unsigned int *count, unsigned requested_planes, |
| 834 | const unsigned requested_sizes[]) |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 835 | { |
| 836 | unsigned int num_planes = 0, num_buffers, allocated_buffers; |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 837 | unsigned plane_sizes[VB2_MAX_PLANES] = { }; |
Hans Verkuil | 37d9ed9 | 2012-06-27 17:10:30 -0300 | [diff] [blame] | 838 | int ret; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 839 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 840 | if (q->num_buffers == VB2_MAX_FRAME) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 841 | dprintk(1, "maximum number of buffers already allocated\n"); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 842 | return -ENOBUFS; |
| 843 | } |
| 844 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 845 | if (!q->num_buffers) { |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 846 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 847 | q->memory = memory; |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 848 | q->waiting_for_buffers = !q->is_output; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 849 | } |
| 850 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 851 | num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 852 | |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 853 | if (requested_planes && requested_sizes) { |
| 854 | num_planes = requested_planes; |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 855 | memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes)); |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 856 | } |
| 857 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 858 | /* |
| 859 | * Ask the driver, whether the requested number of buffers, planes per |
| 860 | * buffer and their sizes are acceptable |
| 861 | */ |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 862 | ret = call_qop(q, queue_setup, q, &num_buffers, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 863 | &num_planes, plane_sizes, q->alloc_ctx); |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 864 | if (ret) |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 865 | return ret; |
| 866 | |
| 867 | /* Finally, allocate buffers and video memory */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 868 | allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 869 | num_planes, plane_sizes); |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 870 | if (allocated_buffers == 0) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 871 | dprintk(1, "memory allocation failed\n"); |
Hans Verkuil | f05393d2 | 2012-06-22 05:44:14 -0300 | [diff] [blame] | 872 | return -ENOMEM; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 873 | } |
| 874 | |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 875 | /* |
| 876 | * Check if driver can handle the so far allocated number of buffers. |
| 877 | */ |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 878 | if (allocated_buffers < num_buffers) { |
| 879 | num_buffers = allocated_buffers; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * q->num_buffers contains the total number of buffers, that the |
| 883 | * queue driver has set up |
| 884 | */ |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 885 | ret = call_qop(q, queue_setup, q, &num_buffers, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 886 | &num_planes, plane_sizes, q->alloc_ctx); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 887 | |
| 888 | if (!ret && allocated_buffers < num_buffers) |
| 889 | ret = -ENOMEM; |
| 890 | |
| 891 | /* |
| 892 | * Either the driver has accepted a smaller number of buffers, |
| 893 | * or .queue_setup() returned an error |
| 894 | */ |
| 895 | } |
| 896 | |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 897 | mutex_lock(&q->mmap_lock); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 898 | q->num_buffers += allocated_buffers; |
| 899 | |
| 900 | if (ret < 0) { |
Hans Verkuil | a7afcac | 2014-02-24 13:41:20 -0300 | [diff] [blame] | 901 | /* |
| 902 | * Note: __vb2_queue_free() will subtract 'allocated_buffers' |
| 903 | * from q->num_buffers. |
| 904 | */ |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 905 | __vb2_queue_free(q, allocated_buffers); |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 906 | mutex_unlock(&q->mmap_lock); |
Hans Verkuil | f05393d2 | 2012-06-22 05:44:14 -0300 | [diff] [blame] | 907 | return -ENOMEM; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 908 | } |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 909 | mutex_unlock(&q->mmap_lock); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 910 | |
| 911 | /* |
| 912 | * Return the number of successfully allocated buffers |
| 913 | * to the userspace. |
| 914 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 915 | *count = allocated_buffers; |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 916 | |
| 917 | return 0; |
| 918 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 919 | EXPORT_SYMBOL_GPL(vb2_core_create_bufs); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 920 | |
| 921 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 922 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane |
| 923 | * @vb: vb2_buffer to which the plane in question belongs to |
| 924 | * @plane_no: plane number for which the address is to be returned |
| 925 | * |
| 926 | * This function returns a kernel virtual address of a given plane if |
| 927 | * such a mapping exist, NULL otherwise. |
| 928 | */ |
| 929 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) |
| 930 | { |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 931 | if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 932 | return NULL; |
| 933 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 934 | return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 935 | |
| 936 | } |
| 937 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); |
| 938 | |
| 939 | /** |
| 940 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane |
| 941 | * @vb: vb2_buffer to which the plane in question belongs to |
| 942 | * @plane_no: plane number for which the cookie is to be returned |
| 943 | * |
| 944 | * This function returns an allocator specific cookie for a given plane if |
| 945 | * available, NULL otherwise. The allocator should provide some simple static |
| 946 | * inline function, which would convert this cookie to the allocator specific |
| 947 | * type that can be used directly by the driver to access the buffer. This can |
| 948 | * be for example physical address, pointer to scatter list or IOMMU mapping. |
| 949 | */ |
| 950 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) |
| 951 | { |
Zhaowei Yuan | a9ae469 | 2014-08-21 23:28:21 -0300 | [diff] [blame] | 952 | if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 953 | return NULL; |
| 954 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 955 | return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 956 | } |
| 957 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); |
| 958 | |
| 959 | /** |
| 960 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished |
| 961 | * @vb: vb2_buffer returned from the driver |
Hans Verkuil | ce0eff0 | 2015-01-20 12:18:16 -0300 | [diff] [blame] | 962 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully, |
| 963 | * VB2_BUF_STATE_ERROR if the operation finished with an error or |
| 964 | * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers. |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 965 | * If start_streaming fails then it should return buffers with state |
| 966 | * VB2_BUF_STATE_QUEUED to put them back into the queue. |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 967 | * |
| 968 | * This function should be called by the driver after a hardware operation on |
| 969 | * a buffer is finished and the buffer may be returned to userspace. The driver |
| 970 | * cannot use this buffer anymore until it is queued back to it by videobuf |
| 971 | * by the means of buf_queue callback. Only buffers previously queued to the |
| 972 | * driver by buf_queue can be passed to this function. |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 973 | * |
| 974 | * While streaming a buffer can only be returned in state DONE or ERROR. |
| 975 | * The start_streaming op can also return them in case the DMA engine cannot |
| 976 | * be started for some reason. In that case the buffers should be returned with |
| 977 | * state QUEUED. |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 978 | */ |
| 979 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) |
| 980 | { |
| 981 | struct vb2_queue *q = vb->vb2_queue; |
| 982 | unsigned long flags; |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 983 | unsigned int plane; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 984 | |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 985 | if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE)) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 986 | return; |
| 987 | |
Hans Verkuil | bf3593d | 2014-08-04 07:14:14 -0300 | [diff] [blame] | 988 | if (WARN_ON(state != VB2_BUF_STATE_DONE && |
| 989 | state != VB2_BUF_STATE_ERROR && |
Sakari Ailus | 6d058c5 | 2015-07-03 04:37:07 -0300 | [diff] [blame] | 990 | state != VB2_BUF_STATE_QUEUED && |
| 991 | state != VB2_BUF_STATE_REQUEUEING)) |
Hans Verkuil | bf3593d | 2014-08-04 07:14:14 -0300 | [diff] [blame] | 992 | state = VB2_BUF_STATE_ERROR; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 993 | |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 994 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 995 | /* |
| 996 | * Although this is not a callback, it still does have to balance |
| 997 | * with the buf_queue op. So update this counter manually. |
| 998 | */ |
| 999 | vb->cnt_buf_done++; |
| 1000 | #endif |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1001 | dprintk(4, "done processing on buffer %d, state: %d\n", |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1002 | vb->index, state); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1003 | |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1004 | /* sync buffers */ |
| 1005 | for (plane = 0; plane < vb->num_planes; ++plane) |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1006 | call_void_memop(vb, finish, vb->planes[plane].mem_priv); |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1007 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1008 | spin_lock_irqsave(&q->done_lock, flags); |
Sakari Ailus | 6d058c5 | 2015-07-03 04:37:07 -0300 | [diff] [blame] | 1009 | if (state == VB2_BUF_STATE_QUEUED || |
| 1010 | state == VB2_BUF_STATE_REQUEUEING) { |
| 1011 | vb->state = VB2_BUF_STATE_QUEUED; |
| 1012 | } else { |
| 1013 | /* Add the buffer to the done buffers list */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1014 | list_add_tail(&vb->done_entry, &q->done_list); |
Sakari Ailus | 6d058c5 | 2015-07-03 04:37:07 -0300 | [diff] [blame] | 1015 | vb->state = state; |
| 1016 | } |
Hans Verkuil | 6ea3b98 | 2014-02-06 05:46:11 -0300 | [diff] [blame] | 1017 | atomic_dec(&q->owned_by_drv_count); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1018 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1019 | |
Philipp Zabel | 2091f51 | 2015-07-10 10:49:26 -0300 | [diff] [blame] | 1020 | trace_vb2_buf_done(q, vb); |
| 1021 | |
Sakari Ailus | 6d058c5 | 2015-07-03 04:37:07 -0300 | [diff] [blame] | 1022 | switch (state) { |
| 1023 | case VB2_BUF_STATE_QUEUED: |
| 1024 | return; |
| 1025 | case VB2_BUF_STATE_REQUEUEING: |
Hans Verkuil | ce0eff0 | 2015-01-20 12:18:16 -0300 | [diff] [blame] | 1026 | if (q->start_streaming_called) |
| 1027 | __enqueue_in_driver(vb); |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1028 | return; |
Sakari Ailus | 6d058c5 | 2015-07-03 04:37:07 -0300 | [diff] [blame] | 1029 | default: |
| 1030 | /* Inform any processes that may be waiting for buffers */ |
| 1031 | wake_up(&q->done_wq); |
| 1032 | break; |
Hans Verkuil | ce0eff0 | 2015-01-20 12:18:16 -0300 | [diff] [blame] | 1033 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(vb2_buffer_done); |
| 1036 | |
| 1037 | /** |
Laurent Pinchart | 34ea4d4 | 2014-03-09 21:42:52 -0300 | [diff] [blame] | 1038 | * vb2_discard_done() - discard all buffers marked as DONE |
| 1039 | * @q: videobuf2 queue |
| 1040 | * |
| 1041 | * This function is intended to be used with suspend/resume operations. It |
| 1042 | * discards all 'done' buffers as they would be too old to be requested after |
| 1043 | * resume. |
| 1044 | * |
| 1045 | * Drivers must stop the hardware and synchronize with interrupt handlers and/or |
| 1046 | * delayed works before calling this function to make sure no buffer will be |
| 1047 | * touched by the driver and/or hardware. |
| 1048 | */ |
| 1049 | void vb2_discard_done(struct vb2_queue *q) |
| 1050 | { |
| 1051 | struct vb2_buffer *vb; |
| 1052 | unsigned long flags; |
| 1053 | |
| 1054 | spin_lock_irqsave(&q->done_lock, flags); |
| 1055 | list_for_each_entry(vb, &q->done_list, done_entry) |
| 1056 | vb->state = VB2_BUF_STATE_ERROR; |
| 1057 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1058 | } |
| 1059 | EXPORT_SYMBOL_GPL(vb2_discard_done); |
| 1060 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1061 | /** |
Hans Verkuil | dcc2428 | 2014-03-10 12:23:13 -0300 | [diff] [blame] | 1062 | * __qbuf_mmap() - handle qbuf of an MMAP buffer |
| 1063 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1064 | static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb) |
Hans Verkuil | dcc2428 | 2014-03-10 12:23:13 -0300 | [diff] [blame] | 1065 | { |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1066 | int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, |
| 1067 | vb, pb, vb->planes); |
| 1068 | return ret ? ret : call_vb_qop(vb, buf_prepare, vb); |
Hans Verkuil | dcc2428 | 2014-03-10 12:23:13 -0300 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1072 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer |
| 1073 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1074 | static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1075 | { |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1076 | struct vb2_plane planes[VB2_MAX_PLANES]; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1077 | struct vb2_queue *q = vb->vb2_queue; |
| 1078 | void *mem_priv; |
| 1079 | unsigned int plane; |
| 1080 | int ret; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 1081 | enum dma_data_direction dma_dir = |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1082 | q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1083 | bool reacquired = vb->planes[0].mem_priv == NULL; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1084 | |
Hans Verkuil | 412376a | 2014-04-07 08:44:56 -0300 | [diff] [blame] | 1085 | memset(planes, 0, sizeof(planes[0]) * vb->num_planes); |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1086 | /* Copy relevant information provided by the userspace */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1087 | ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes); |
| 1088 | if (ret) |
| 1089 | return ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1090 | |
| 1091 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1092 | /* Skip the plane if already verified */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1093 | if (vb->planes[plane].m.userptr && |
| 1094 | vb->planes[plane].m.userptr == planes[plane].m.userptr |
| 1095 | && vb->planes[plane].length == planes[plane].length) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1096 | continue; |
| 1097 | |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1098 | dprintk(3, "userspace address for plane %d changed, " |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1099 | "reacquiring memory\n", plane); |
| 1100 | |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 1101 | /* Check if the provided plane buffer is large enough */ |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 1102 | if (planes[plane].length < vb->planes[plane].min_length) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1103 | dprintk(1, "provided buffer size %u is less than " |
Seung-Woo Kim | 2484a7e | 2013-08-20 04:48:06 -0300 | [diff] [blame] | 1104 | "setup size %u for plane %d\n", |
| 1105 | planes[plane].length, |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 1106 | vb->planes[plane].min_length, |
| 1107 | plane); |
Marek Szyprowski | 4c2625d | 2011-10-03 03:21:45 -0300 | [diff] [blame] | 1108 | ret = -EINVAL; |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 1109 | goto err; |
| 1110 | } |
| 1111 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1112 | /* Release previously acquired memory if present */ |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1113 | if (vb->planes[plane].mem_priv) { |
| 1114 | if (!reacquired) { |
| 1115 | reacquired = true; |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1116 | call_void_vb_qop(vb, buf_cleanup, vb); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1117 | } |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1118 | call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1119 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1120 | |
| 1121 | vb->planes[plane].mem_priv = NULL; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1122 | vb->planes[plane].bytesused = 0; |
| 1123 | vb->planes[plane].length = 0; |
| 1124 | vb->planes[plane].m.userptr = 0; |
| 1125 | vb->planes[plane].data_offset = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1126 | |
| 1127 | /* Acquire each plane's memory */ |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1128 | mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane], |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 1129 | planes[plane].m.userptr, |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 1130 | planes[plane].length, dma_dir); |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 1131 | if (IS_ERR_OR_NULL(mem_priv)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1132 | dprintk(1, "failed acquiring userspace " |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1133 | "memory for plane %d\n", plane); |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 1134 | ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL; |
| 1135 | goto err; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1136 | } |
Marek Szyprowski | a00d026 | 2011-12-15 05:53:06 -0300 | [diff] [blame] | 1137 | vb->planes[plane].mem_priv = mem_priv; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /* |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1141 | * Now that everything is in order, copy relevant information |
| 1142 | * provided by userspace. |
| 1143 | */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1144 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1145 | vb->planes[plane].bytesused = planes[plane].bytesused; |
| 1146 | vb->planes[plane].length = planes[plane].length; |
| 1147 | vb->planes[plane].m.userptr = planes[plane].m.userptr; |
| 1148 | vb->planes[plane].data_offset = planes[plane].data_offset; |
| 1149 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1150 | |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1151 | if (reacquired) { |
| 1152 | /* |
| 1153 | * One or more planes changed, so we must call buf_init to do |
| 1154 | * the driver-specific initialization on the newly acquired |
| 1155 | * buffer, if provided. |
| 1156 | */ |
| 1157 | ret = call_vb_qop(vb, buf_init, vb); |
| 1158 | if (ret) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1159 | dprintk(1, "buffer initialization failed\n"); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1160 | goto err; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | ret = call_vb_qop(vb, buf_prepare, vb); |
| 1165 | if (ret) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1166 | dprintk(1, "buffer preparation failed\n"); |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1167 | call_void_vb_qop(vb, buf_cleanup, vb); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1168 | goto err; |
| 1169 | } |
| 1170 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1171 | return 0; |
| 1172 | err: |
| 1173 | /* In case of errors, release planes that were already acquired */ |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 1174 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1175 | if (vb->planes[plane].mem_priv) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1176 | call_void_memop(vb, put_userptr, |
| 1177 | vb->planes[plane].mem_priv); |
Marek Szyprowski | c1426bc | 2011-08-24 06:36:26 -0300 | [diff] [blame] | 1178 | vb->planes[plane].mem_priv = NULL; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1179 | vb->planes[plane].m.userptr = 0; |
| 1180 | vb->planes[plane].length = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1187 | * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer |
| 1188 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1189 | static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb) |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1190 | { |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1191 | struct vb2_plane planes[VB2_MAX_PLANES]; |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1192 | struct vb2_queue *q = vb->vb2_queue; |
| 1193 | void *mem_priv; |
| 1194 | unsigned int plane; |
| 1195 | int ret; |
Hans Verkuil | cd47403 | 2014-11-18 09:50:58 -0300 | [diff] [blame] | 1196 | enum dma_data_direction dma_dir = |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1197 | q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1198 | bool reacquired = vb->planes[0].mem_priv == NULL; |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1199 | |
Hans Verkuil | 412376a | 2014-04-07 08:44:56 -0300 | [diff] [blame] | 1200 | memset(planes, 0, sizeof(planes[0]) * vb->num_planes); |
Laurent Pinchart | 6f546c5 | 2014-01-01 09:10:48 -0300 | [diff] [blame] | 1201 | /* Copy relevant information provided by the userspace */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1202 | ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes); |
| 1203 | if (ret) |
| 1204 | return ret; |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1205 | |
| 1206 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1207 | struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd); |
| 1208 | |
| 1209 | if (IS_ERR_OR_NULL(dbuf)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1210 | dprintk(1, "invalid dmabuf fd for plane %d\n", |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1211 | plane); |
| 1212 | ret = -EINVAL; |
| 1213 | goto err; |
| 1214 | } |
| 1215 | |
| 1216 | /* use DMABUF size if length is not provided */ |
| 1217 | if (planes[plane].length == 0) |
| 1218 | planes[plane].length = dbuf->size; |
| 1219 | |
Hans Verkuil | 58e1ba3 | 2015-11-20 09:40:14 -0200 | [diff] [blame] | 1220 | if (planes[plane].length < vb->planes[plane].min_length) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1221 | dprintk(1, "invalid dmabuf length for plane %d\n", |
Seung-Woo Kim | 77c0782 | 2013-11-29 04:50:29 -0300 | [diff] [blame] | 1222 | plane); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1223 | ret = -EINVAL; |
| 1224 | goto err; |
| 1225 | } |
| 1226 | |
| 1227 | /* Skip the plane if already verified */ |
| 1228 | if (dbuf == vb->planes[plane].dbuf && |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1229 | vb->planes[plane].length == planes[plane].length) { |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1230 | dma_buf_put(dbuf); |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1234 | dprintk(1, "buffer for plane %d changed\n", plane); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1235 | |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1236 | if (!reacquired) { |
| 1237 | reacquired = true; |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1238 | call_void_vb_qop(vb, buf_cleanup, vb); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1239 | } |
| 1240 | |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1241 | /* Release previously acquired memory if present */ |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 1242 | __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1243 | vb->planes[plane].bytesused = 0; |
| 1244 | vb->planes[plane].length = 0; |
| 1245 | vb->planes[plane].m.fd = 0; |
| 1246 | vb->planes[plane].data_offset = 0; |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1247 | |
| 1248 | /* Acquire each plane's memory */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1249 | mem_priv = call_ptr_memop(vb, attach_dmabuf, |
| 1250 | q->alloc_ctx[plane], dbuf, planes[plane].length, |
| 1251 | dma_dir); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1252 | if (IS_ERR(mem_priv)) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1253 | dprintk(1, "failed to attach dmabuf\n"); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1254 | ret = PTR_ERR(mem_priv); |
| 1255 | dma_buf_put(dbuf); |
| 1256 | goto err; |
| 1257 | } |
| 1258 | |
| 1259 | vb->planes[plane].dbuf = dbuf; |
| 1260 | vb->planes[plane].mem_priv = mem_priv; |
| 1261 | } |
| 1262 | |
| 1263 | /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but |
| 1264 | * really we want to do this just before the DMA, not while queueing |
| 1265 | * the buffer(s).. |
| 1266 | */ |
| 1267 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 1268 | ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1269 | if (ret) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1270 | dprintk(1, "failed to map dmabuf for plane %d\n", |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1271 | plane); |
| 1272 | goto err; |
| 1273 | } |
| 1274 | vb->planes[plane].dbuf_mapped = 1; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1278 | * Now that everything is in order, copy relevant information |
| 1279 | * provided by userspace. |
| 1280 | */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1281 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1282 | vb->planes[plane].bytesused = planes[plane].bytesused; |
| 1283 | vb->planes[plane].length = planes[plane].length; |
| 1284 | vb->planes[plane].m.fd = planes[plane].m.fd; |
| 1285 | vb->planes[plane].data_offset = planes[plane].data_offset; |
| 1286 | } |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1287 | |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1288 | if (reacquired) { |
| 1289 | /* |
| 1290 | * Call driver-specific initialization on the newly acquired buffer, |
| 1291 | * if provided. |
| 1292 | */ |
| 1293 | ret = call_vb_qop(vb, buf_init, vb); |
| 1294 | if (ret) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1295 | dprintk(1, "buffer initialization failed\n"); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1296 | goto err; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | ret = call_vb_qop(vb, buf_prepare, vb); |
| 1301 | if (ret) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1302 | dprintk(1, "buffer preparation failed\n"); |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1303 | call_void_vb_qop(vb, buf_cleanup, vb); |
Hans Verkuil | 256f316 | 2014-01-29 13:36:53 -0300 | [diff] [blame] | 1304 | goto err; |
| 1305 | } |
| 1306 | |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1307 | return 0; |
| 1308 | err: |
| 1309 | /* In case of errors, release planes that were already acquired */ |
| 1310 | __vb2_buf_dmabuf_put(vb); |
| 1311 | |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1316 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing |
| 1317 | */ |
| 1318 | static void __enqueue_in_driver(struct vb2_buffer *vb) |
| 1319 | { |
| 1320 | struct vb2_queue *q = vb->vb2_queue; |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1321 | unsigned int plane; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1322 | |
| 1323 | vb->state = VB2_BUF_STATE_ACTIVE; |
Hans Verkuil | 6ea3b98 | 2014-02-06 05:46:11 -0300 | [diff] [blame] | 1324 | atomic_inc(&q->owned_by_drv_count); |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1325 | |
Philipp Zabel | 2091f51 | 2015-07-10 10:49:26 -0300 | [diff] [blame] | 1326 | trace_vb2_buf_queue(q, vb); |
| 1327 | |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1328 | /* sync buffers */ |
| 1329 | for (plane = 0; plane < vb->num_planes; ++plane) |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1330 | call_void_memop(vb, prepare, vb->planes[plane].mem_priv); |
Marek Szyprowski | 3e0c2f2 | 2012-06-14 10:37:43 -0300 | [diff] [blame] | 1331 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1332 | call_void_vb_qop(vb, buf_queue, vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1333 | } |
| 1334 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1335 | static int __buf_prepare(struct vb2_buffer *vb, const void *pb) |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1336 | { |
| 1337 | struct vb2_queue *q = vb->vb2_queue; |
| 1338 | int ret; |
| 1339 | |
Laurent Pinchart | 4bb7267 | 2014-06-03 18:53:25 -0300 | [diff] [blame] | 1340 | if (q->error) { |
| 1341 | dprintk(1, "fatal error occurred on queue\n"); |
| 1342 | return -EIO; |
| 1343 | } |
| 1344 | |
Hans Verkuil | b18a8ff | 2013-12-13 13:13:38 -0300 | [diff] [blame] | 1345 | vb->state = VB2_BUF_STATE_PREPARING; |
Hans Verkuil | f134328 | 2014-02-24 14:44:50 -0300 | [diff] [blame] | 1346 | |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1347 | switch (q->memory) { |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1348 | case VB2_MEMORY_MMAP: |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1349 | ret = __qbuf_mmap(vb, pb); |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1350 | break; |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1351 | case VB2_MEMORY_USERPTR: |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1352 | ret = __qbuf_userptr(vb, pb); |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1353 | break; |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1354 | case VB2_MEMORY_DMABUF: |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1355 | ret = __qbuf_dmabuf(vb, pb); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1356 | break; |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1357 | default: |
| 1358 | WARN(1, "Invalid queue type\n"); |
| 1359 | ret = -EINVAL; |
| 1360 | } |
| 1361 | |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1362 | if (ret) |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1363 | dprintk(1, "buffer preparation failed: %d\n", ret); |
Hans Verkuil | b18a8ff | 2013-12-13 13:13:38 -0300 | [diff] [blame] | 1364 | vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED; |
Guennadi Liakhovetski | ebc087d | 2011-08-31 06:51:10 -0300 | [diff] [blame] | 1365 | |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1369 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1370 | * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace |
| 1371 | * to the kernel |
| 1372 | * @q: videobuf2 queue |
| 1373 | * @index: id number of the buffer |
| 1374 | * @pb: buffer structure passed from userspace to vidioc_prepare_buf |
| 1375 | * handler in driver |
| 1376 | * |
| 1377 | * Should be called from vidioc_prepare_buf ioctl handler of a driver. |
| 1378 | * The passed buffer should have been verified. |
| 1379 | * This function calls buf_prepare callback in the driver (if provided), |
| 1380 | * in which driver-specific buffer initialization can be performed, |
| 1381 | * |
| 1382 | * The return values from this function are intended to be directly returned |
| 1383 | * from vidioc_prepare_buf handler in driver. |
| 1384 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1385 | int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb) |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1386 | { |
| 1387 | struct vb2_buffer *vb; |
| 1388 | int ret; |
| 1389 | |
| 1390 | vb = q->bufs[index]; |
| 1391 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { |
| 1392 | dprintk(1, "invalid buffer state %d\n", |
| 1393 | vb->state); |
| 1394 | return -EINVAL; |
| 1395 | } |
| 1396 | |
| 1397 | ret = __buf_prepare(vb, pb); |
| 1398 | if (ret) |
| 1399 | return ret; |
| 1400 | |
| 1401 | /* Fill buffer information for the userspace */ |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 1402 | call_void_bufop(q, fill_user_buffer, vb, pb); |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1403 | |
| 1404 | dprintk(1, "prepare of buffer %d succeeded\n", vb->index); |
| 1405 | |
| 1406 | return ret; |
| 1407 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1408 | EXPORT_SYMBOL_GPL(vb2_core_prepare_buf); |
Guennadi Liakhovetski | 2d86401 | 2011-09-28 09:23:02 -0300 | [diff] [blame] | 1409 | |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1410 | /** |
| 1411 | * vb2_start_streaming() - Attempt to start streaming. |
| 1412 | * @q: videobuf2 queue |
| 1413 | * |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1414 | * Attempt to start streaming. When this function is called there must be |
| 1415 | * at least q->min_buffers_needed buffers queued up (i.e. the minimum |
| 1416 | * number of buffers required for the DMA engine to function). If the |
| 1417 | * @start_streaming op fails it is supposed to return all the driver-owned |
| 1418 | * buffers back to vb2 in state QUEUED. Check if that happened and if |
| 1419 | * not warn and reclaim them forcefully. |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1420 | */ |
| 1421 | static int vb2_start_streaming(struct vb2_queue *q) |
| 1422 | { |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1423 | struct vb2_buffer *vb; |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1424 | int ret; |
| 1425 | |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1426 | /* |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1427 | * If any buffers were queued before streamon, |
| 1428 | * we can now pass them to driver for processing. |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1429 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1430 | list_for_each_entry(vb, &q->queued_list, queued_entry) |
| 1431 | __enqueue_in_driver(vb); |
| 1432 | |
| 1433 | /* Tell the driver to start streaming */ |
Laurent Pinchart | bd994dd | 2014-06-23 18:00:22 -0300 | [diff] [blame] | 1434 | q->start_streaming_called = 1; |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1435 | ret = call_qop(q, start_streaming, q, |
| 1436 | atomic_read(&q->owned_by_drv_count)); |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1437 | if (!ret) |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1438 | return 0; |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1439 | |
Laurent Pinchart | bd994dd | 2014-06-23 18:00:22 -0300 | [diff] [blame] | 1440 | q->start_streaming_called = 0; |
| 1441 | |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1442 | dprintk(1, "driver refused to start streaming\n"); |
Hans Verkuil | 23cd08c | 2014-08-04 02:33:53 -0300 | [diff] [blame] | 1443 | /* |
| 1444 | * If you see this warning, then the driver isn't cleaning up properly |
| 1445 | * after a failed start_streaming(). See the start_streaming() |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1446 | * documentation in videobuf2-core.h for more information how buffers |
Hans Verkuil | 23cd08c | 2014-08-04 02:33:53 -0300 | [diff] [blame] | 1447 | * should be returned to vb2 in start_streaming(). |
| 1448 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1449 | if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { |
| 1450 | unsigned i; |
| 1451 | |
| 1452 | /* |
| 1453 | * Forcefully reclaim buffers if the driver did not |
| 1454 | * correctly return them to vb2. |
| 1455 | */ |
| 1456 | for (i = 0; i < q->num_buffers; ++i) { |
| 1457 | vb = q->bufs[i]; |
| 1458 | if (vb->state == VB2_BUF_STATE_ACTIVE) |
| 1459 | vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED); |
| 1460 | } |
| 1461 | /* Must be zero now */ |
| 1462 | WARN_ON(atomic_read(&q->owned_by_drv_count)); |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1463 | } |
Hans Verkuil | bf3593d | 2014-08-04 07:14:14 -0300 | [diff] [blame] | 1464 | /* |
| 1465 | * If done_list is not empty, then start_streaming() didn't call |
| 1466 | * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or |
| 1467 | * STATE_DONE. |
| 1468 | */ |
| 1469 | WARN_ON(!list_empty(&q->done_list)); |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1470 | return ret; |
| 1471 | } |
| 1472 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1473 | /** |
| 1474 | * vb2_core_qbuf() - Queue a buffer from userspace |
| 1475 | * @q: videobuf2 queue |
| 1476 | * @index: id number of the buffer |
| 1477 | * @pb: buffer structure passed from userspace to vidioc_qbuf handler |
| 1478 | * in driver |
| 1479 | * |
| 1480 | * Should be called from vidioc_qbuf ioctl handler of a driver. |
| 1481 | * The passed buffer should have been verified. |
| 1482 | * This function: |
| 1483 | * 1) if necessary, calls buf_prepare callback in the driver (if provided), in |
| 1484 | * which driver-specific buffer initialization can be performed, |
| 1485 | * 2) if streaming is on, queues the buffer in driver by the means of buf_queue |
| 1486 | * callback for processing. |
| 1487 | * |
| 1488 | * The return values from this function are intended to be directly returned |
| 1489 | * from vidioc_qbuf handler in driver. |
| 1490 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1491 | int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb) |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1492 | { |
Hans Verkuil | 4138111 | 2013-12-13 13:13:39 -0300 | [diff] [blame] | 1493 | struct vb2_buffer *vb; |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1494 | int ret; |
Hans Verkuil | 4138111 | 2013-12-13 13:13:39 -0300 | [diff] [blame] | 1495 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1496 | vb = q->bufs[index]; |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1497 | |
| 1498 | switch (vb->state) { |
| 1499 | case VB2_BUF_STATE_DEQUEUED: |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1500 | ret = __buf_prepare(vb, pb); |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1501 | if (ret) |
| 1502 | return ret; |
Hans Verkuil | 4138111 | 2013-12-13 13:13:39 -0300 | [diff] [blame] | 1503 | break; |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1504 | case VB2_BUF_STATE_PREPARED: |
| 1505 | break; |
Hans Verkuil | b18a8ff | 2013-12-13 13:13:38 -0300 | [diff] [blame] | 1506 | case VB2_BUF_STATE_PREPARING: |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1507 | dprintk(1, "buffer still being prepared\n"); |
Hans Verkuil | b18a8ff | 2013-12-13 13:13:38 -0300 | [diff] [blame] | 1508 | return -EINVAL; |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1509 | default: |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1510 | dprintk(1, "invalid buffer state %d\n", vb->state); |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1511 | return -EINVAL; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Add to the queued buffers list, a buffer will stay on it until |
| 1516 | * dequeued in dqbuf. |
| 1517 | */ |
| 1518 | list_add_tail(&vb->queued_entry, &q->queued_list); |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1519 | q->queued_count++; |
Hans Verkuil | 58d75f4 | 2014-09-20 16:16:35 -0300 | [diff] [blame] | 1520 | q->waiting_for_buffers = false; |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1521 | vb->state = VB2_BUF_STATE_QUEUED; |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1522 | |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 1523 | call_void_bufop(q, copy_timestamp, vb, pb); |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1524 | |
Philipp Zabel | 2091f51 | 2015-07-10 10:49:26 -0300 | [diff] [blame] | 1525 | trace_vb2_qbuf(q, vb); |
| 1526 | |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1527 | /* |
| 1528 | * If already streaming, give the buffer to driver for processing. |
| 1529 | * If not, the buffer will be given to driver on next streamon. |
| 1530 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1531 | if (q->start_streaming_called) |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1532 | __enqueue_in_driver(vb); |
| 1533 | |
Hans Verkuil | 4138111 | 2013-12-13 13:13:39 -0300 | [diff] [blame] | 1534 | /* Fill buffer information for the userspace */ |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 1535 | call_void_bufop(q, fill_user_buffer, vb, pb); |
Laurent Pinchart | 012043b | 2013-08-09 08:11:26 -0300 | [diff] [blame] | 1536 | |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1537 | /* |
| 1538 | * If streamon has been called, and we haven't yet called |
| 1539 | * start_streaming() since not enough buffers were queued, and |
| 1540 | * we now have reached the minimum number of queued buffers, |
| 1541 | * then we can finally call start_streaming(). |
| 1542 | */ |
| 1543 | if (q->streaming && !q->start_streaming_called && |
| 1544 | q->queued_count >= q->min_buffers_needed) { |
Hans Verkuil | 02f142e | 2013-12-13 13:13:42 -0300 | [diff] [blame] | 1545 | ret = vb2_start_streaming(q); |
| 1546 | if (ret) |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1550 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->index); |
Hans Verkuil | 4138111 | 2013-12-13 13:13:39 -0300 | [diff] [blame] | 1551 | return 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1552 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1553 | EXPORT_SYMBOL_GPL(vb2_core_qbuf); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1554 | |
| 1555 | /** |
| 1556 | * __vb2_wait_for_done_vb() - wait for a buffer to become available |
| 1557 | * for dequeuing |
| 1558 | * |
| 1559 | * Will sleep if required for nonblocking == false. |
| 1560 | */ |
| 1561 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) |
| 1562 | { |
| 1563 | /* |
| 1564 | * All operations on vb_done_list are performed under done_lock |
| 1565 | * spinlock protection. However, buffers may be removed from |
| 1566 | * it and returned to userspace only while holding both driver's |
| 1567 | * lock and the done_lock spinlock. Thus we can be sure that as |
| 1568 | * long as we hold the driver's lock, the list will remain not |
| 1569 | * empty if list_empty() check succeeds. |
| 1570 | */ |
| 1571 | |
| 1572 | for (;;) { |
| 1573 | int ret; |
| 1574 | |
| 1575 | if (!q->streaming) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1576 | dprintk(1, "streaming off, will not wait for buffers\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1577 | return -EINVAL; |
| 1578 | } |
| 1579 | |
Laurent Pinchart | 4bb7267 | 2014-06-03 18:53:25 -0300 | [diff] [blame] | 1580 | if (q->error) { |
| 1581 | dprintk(1, "Queue in error state, will not wait for buffers\n"); |
| 1582 | return -EIO; |
| 1583 | } |
| 1584 | |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 1585 | if (q->last_buffer_dequeued) { |
| 1586 | dprintk(3, "last buffer dequeued already, will not wait for buffers\n"); |
| 1587 | return -EPIPE; |
| 1588 | } |
| 1589 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1590 | if (!list_empty(&q->done_list)) { |
| 1591 | /* |
| 1592 | * Found a buffer that we were waiting for. |
| 1593 | */ |
| 1594 | break; |
| 1595 | } |
| 1596 | |
| 1597 | if (nonblocking) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1598 | dprintk(1, "nonblocking and no buffers to dequeue, " |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1599 | "will not wait\n"); |
| 1600 | return -EAGAIN; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * We are streaming and blocking, wait for another buffer to |
| 1605 | * become ready or for streamoff. Driver's lock is released to |
| 1606 | * allow streamoff or qbuf to be called while waiting. |
| 1607 | */ |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1608 | call_void_qop(q, wait_prepare, q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1609 | |
| 1610 | /* |
| 1611 | * All locks have been released, it is safe to sleep now. |
| 1612 | */ |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1613 | dprintk(3, "will sleep waiting for buffers\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1614 | ret = wait_event_interruptible(q->done_wq, |
Laurent Pinchart | 4bb7267 | 2014-06-03 18:53:25 -0300 | [diff] [blame] | 1615 | !list_empty(&q->done_list) || !q->streaming || |
| 1616 | q->error); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1617 | |
| 1618 | /* |
| 1619 | * We need to reevaluate both conditions again after reacquiring |
| 1620 | * the locks or return an error if one occurred. |
| 1621 | */ |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1622 | call_void_qop(q, wait_finish, q); |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1623 | if (ret) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1624 | dprintk(1, "sleep was interrupted\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1625 | return ret; |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1626 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1627 | } |
| 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * __vb2_get_done_vb() - get a buffer ready for dequeuing |
| 1633 | * |
| 1634 | * Will sleep if required for nonblocking == false. |
| 1635 | */ |
| 1636 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1637 | int nonblocking) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1638 | { |
| 1639 | unsigned long flags; |
| 1640 | int ret; |
| 1641 | |
| 1642 | /* |
| 1643 | * Wait for at least one buffer to become available on the done_list. |
| 1644 | */ |
| 1645 | ret = __vb2_wait_for_done_vb(q, nonblocking); |
| 1646 | if (ret) |
| 1647 | return ret; |
| 1648 | |
| 1649 | /* |
| 1650 | * Driver's lock has been held since we last verified that done_list |
| 1651 | * is not empty, so no need for another list_empty(done_list) check. |
| 1652 | */ |
| 1653 | spin_lock_irqsave(&q->done_lock, flags); |
| 1654 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1655 | /* |
| 1656 | * Only remove the buffer from done_list if v4l2_buffer can handle all |
| 1657 | * the planes. |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1658 | * Verifying planes is NOT necessary since it already has been checked |
| 1659 | * before the buffer is queued/prepared. So it can never fail. |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1660 | */ |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1661 | list_del(&(*vb)->done_entry); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1662 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1663 | |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1664 | return ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 |
| 1669 | * @q: videobuf2 queue |
| 1670 | * |
| 1671 | * This function will wait until all buffers that have been given to the driver |
| 1672 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call |
| 1673 | * wait_prepare, wait_finish pair. It is intended to be called with all locks |
| 1674 | * taken, for example from stop_streaming() callback. |
| 1675 | */ |
| 1676 | int vb2_wait_for_all_buffers(struct vb2_queue *q) |
| 1677 | { |
| 1678 | if (!q->streaming) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1679 | dprintk(1, "streaming off, will not wait for buffers\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1680 | return -EINVAL; |
| 1681 | } |
| 1682 | |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1683 | if (q->start_streaming_called) |
Hans Verkuil | 6ea3b98 | 2014-02-06 05:46:11 -0300 | [diff] [blame] | 1684 | wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count)); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1685 | return 0; |
| 1686 | } |
| 1687 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); |
| 1688 | |
| 1689 | /** |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1690 | * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state |
| 1691 | */ |
| 1692 | static void __vb2_dqbuf(struct vb2_buffer *vb) |
| 1693 | { |
| 1694 | struct vb2_queue *q = vb->vb2_queue; |
| 1695 | unsigned int i; |
| 1696 | |
| 1697 | /* nothing to do if the buffer is already dequeued */ |
| 1698 | if (vb->state == VB2_BUF_STATE_DEQUEUED) |
| 1699 | return; |
| 1700 | |
| 1701 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 1702 | |
| 1703 | /* unmap DMABUF buffer */ |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1704 | if (q->memory == VB2_MEMORY_DMABUF) |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1705 | for (i = 0; i < vb->num_planes; ++i) { |
| 1706 | if (!vb->planes[i].dbuf_mapped) |
| 1707 | continue; |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1708 | call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv); |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1709 | vb->planes[i].dbuf_mapped = 0; |
| 1710 | } |
| 1711 | } |
| 1712 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1713 | /** |
| 1714 | * vb2_dqbuf() - Dequeue a buffer to the userspace |
| 1715 | * @q: videobuf2 queue |
| 1716 | * @pb: buffer structure passed from userspace to vidioc_dqbuf handler |
| 1717 | * in driver |
| 1718 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no |
| 1719 | * buffers ready for dequeuing are present. Normally the driver |
| 1720 | * would be passing (file->f_flags & O_NONBLOCK) here |
| 1721 | * |
| 1722 | * Should be called from vidioc_dqbuf ioctl handler of a driver. |
| 1723 | * The passed buffer should have been verified. |
| 1724 | * This function: |
| 1725 | * 1) calls buf_finish callback in the driver (if provided), in which |
| 1726 | * driver can perform any additional operations that may be required before |
| 1727 | * returning the buffer to userspace, such as cache sync, |
| 1728 | * 2) the buffer struct members are filled with relevant information for |
| 1729 | * the userspace. |
| 1730 | * |
| 1731 | * The return values from this function are intended to be directly returned |
| 1732 | * from vidioc_dqbuf handler in driver. |
| 1733 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1734 | int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1735 | { |
| 1736 | struct vb2_buffer *vb = NULL; |
| 1737 | int ret; |
| 1738 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1739 | ret = __vb2_get_done_vb(q, &vb, nonblocking); |
Hans Verkuil | 32a7726 | 2012-09-28 06:12:53 -0300 | [diff] [blame] | 1740 | if (ret < 0) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1741 | return ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1742 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1743 | switch (vb->state) { |
| 1744 | case VB2_BUF_STATE_DONE: |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1745 | dprintk(3, "returning done buffer\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1746 | break; |
| 1747 | case VB2_BUF_STATE_ERROR: |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1748 | dprintk(3, "returning done buffer with errors\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1749 | break; |
| 1750 | default: |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1751 | dprintk(1, "invalid buffer state\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1752 | return -EINVAL; |
| 1753 | } |
| 1754 | |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1755 | call_void_vb_qop(vb, buf_finish, vb); |
Hans Verkuil | 9cf3c31 | 2014-02-28 13:30:48 -0300 | [diff] [blame] | 1756 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1757 | /* Fill buffer information for the userspace */ |
Hans Verkuil | 10cc3b1 | 2015-11-20 08:32:00 -0200 | [diff] [blame] | 1758 | call_void_bufop(q, fill_user_buffer, vb, pb); |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1759 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1760 | /* Remove from videobuf queue */ |
| 1761 | list_del(&vb->queued_entry); |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1762 | q->queued_count--; |
Philipp Zabel | 2091f51 | 2015-07-10 10:49:26 -0300 | [diff] [blame] | 1763 | |
| 1764 | trace_vb2_dqbuf(q, vb); |
| 1765 | |
Sumit Semwal | c538404 | 2012-06-14 10:37:37 -0300 | [diff] [blame] | 1766 | /* go back to dequeued state */ |
| 1767 | __vb2_dqbuf(vb); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1768 | |
| 1769 | dprintk(1, "dqbuf of buffer %d, with state %d\n", |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1770 | vb->index, vb->state); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1771 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1772 | return 0; |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1773 | |
| 1774 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1775 | EXPORT_SYMBOL_GPL(vb2_core_dqbuf); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1776 | |
| 1777 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1778 | * __vb2_queue_cancel() - cancel and stop (pause) streaming |
| 1779 | * |
| 1780 | * Removes all queued buffers from driver's queue and all buffers queued by |
| 1781 | * userspace from videobuf's queue. Returns to state after reqbufs. |
| 1782 | */ |
| 1783 | static void __vb2_queue_cancel(struct vb2_queue *q) |
| 1784 | { |
| 1785 | unsigned int i; |
| 1786 | |
| 1787 | /* |
| 1788 | * Tell driver to stop all transactions and release all queued |
| 1789 | * buffers. |
| 1790 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1791 | if (q->start_streaming_called) |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1792 | call_void_qop(q, stop_streaming, q); |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1793 | |
Hans Verkuil | 23cd08c | 2014-08-04 02:33:53 -0300 | [diff] [blame] | 1794 | /* |
| 1795 | * If you see this warning, then the driver isn't cleaning up properly |
| 1796 | * in stop_streaming(). See the stop_streaming() documentation in |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1797 | * videobuf2-core.h for more information how buffers should be returned |
Hans Verkuil | 23cd08c | 2014-08-04 02:33:53 -0300 | [diff] [blame] | 1798 | * to vb2 in stop_streaming(). |
| 1799 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1800 | if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { |
| 1801 | for (i = 0; i < q->num_buffers; ++i) |
| 1802 | if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) |
| 1803 | vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR); |
| 1804 | /* Must be zero now */ |
| 1805 | WARN_ON(atomic_read(&q->owned_by_drv_count)); |
| 1806 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1807 | |
Laurent Pinchart | b646f0b | 2014-04-20 20:55:41 -0300 | [diff] [blame] | 1808 | q->streaming = 0; |
| 1809 | q->start_streaming_called = 0; |
| 1810 | q->queued_count = 0; |
Laurent Pinchart | 4bb7267 | 2014-06-03 18:53:25 -0300 | [diff] [blame] | 1811 | q->error = 0; |
Laurent Pinchart | b646f0b | 2014-04-20 20:55:41 -0300 | [diff] [blame] | 1812 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1813 | /* |
| 1814 | * Remove all buffers from videobuf's list... |
| 1815 | */ |
| 1816 | INIT_LIST_HEAD(&q->queued_list); |
| 1817 | /* |
| 1818 | * ...and done list; userspace will not receive any buffers it |
| 1819 | * has not already dequeued before initiating cancel. |
| 1820 | */ |
| 1821 | INIT_LIST_HEAD(&q->done_list); |
Hans Verkuil | 6ea3b98 | 2014-02-06 05:46:11 -0300 | [diff] [blame] | 1822 | atomic_set(&q->owned_by_drv_count, 0); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1823 | wake_up_all(&q->done_wq); |
| 1824 | |
| 1825 | /* |
| 1826 | * Reinitialize all buffers for next use. |
Hans Verkuil | 9c0863b | 2014-03-04 07:34:49 -0300 | [diff] [blame] | 1827 | * Make sure to call buf_finish for any queued buffers. Normally |
| 1828 | * that's done in dqbuf, but that's not going to happen when we |
| 1829 | * cancel the whole queue. Note: this code belongs here, not in |
| 1830 | * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical |
| 1831 | * call to __fill_v4l2_buffer() after buf_finish(). That order can't |
| 1832 | * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1833 | */ |
Hans Verkuil | 9c0863b | 2014-03-04 07:34:49 -0300 | [diff] [blame] | 1834 | for (i = 0; i < q->num_buffers; ++i) { |
| 1835 | struct vb2_buffer *vb = q->bufs[i]; |
| 1836 | |
| 1837 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { |
| 1838 | vb->state = VB2_BUF_STATE_PREPARED; |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 1839 | call_void_vb_qop(vb, buf_finish, vb); |
Hans Verkuil | 9c0863b | 2014-03-04 07:34:49 -0300 | [diff] [blame] | 1840 | } |
| 1841 | __vb2_dqbuf(vb); |
| 1842 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1843 | } |
| 1844 | |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1845 | int vb2_core_streamon(struct vb2_queue *q, unsigned int type) |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1846 | { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1847 | int ret; |
| 1848 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1849 | if (type != q->type) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1850 | dprintk(1, "invalid stream type\n"); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1851 | return -EINVAL; |
| 1852 | } |
| 1853 | |
| 1854 | if (q->streaming) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1855 | dprintk(3, "already streaming\n"); |
Ricardo Ribalda | f956035 | 2013-11-08 07:08:45 -0300 | [diff] [blame] | 1856 | return 0; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1857 | } |
| 1858 | |
Ricardo Ribalda | 548df78 | 2014-01-08 05:01:33 -0300 | [diff] [blame] | 1859 | if (!q->num_buffers) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1860 | dprintk(1, "no buffers have been allocated\n"); |
Ricardo Ribalda | 548df78 | 2014-01-08 05:01:33 -0300 | [diff] [blame] | 1861 | return -EINVAL; |
| 1862 | } |
| 1863 | |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1864 | if (q->num_buffers < q->min_buffers_needed) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1865 | dprintk(1, "need at least %u allocated buffers\n", |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1866 | q->min_buffers_needed); |
| 1867 | return -EINVAL; |
| 1868 | } |
Ricardo Ribalda Delgado | 249f5a5 | 2014-01-08 05:01:33 -0300 | [diff] [blame] | 1869 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1870 | /* |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1871 | * Tell driver to start streaming provided sufficient buffers |
| 1872 | * are available. |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1873 | */ |
Hans Verkuil | b3379c6 | 2014-02-24 13:51:03 -0300 | [diff] [blame] | 1874 | if (q->queued_count >= q->min_buffers_needed) { |
| 1875 | ret = vb2_start_streaming(q); |
| 1876 | if (ret) { |
| 1877 | __vb2_queue_cancel(q); |
| 1878 | return ret; |
| 1879 | } |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | q->streaming = 1; |
| 1883 | |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1884 | dprintk(3, "successful\n"); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1887 | EXPORT_SYMBOL_GPL(vb2_core_streamon); |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1888 | |
| 1889 | /** |
Laurent Pinchart | 4bb7267 | 2014-06-03 18:53:25 -0300 | [diff] [blame] | 1890 | * vb2_queue_error() - signal a fatal error on the queue |
| 1891 | * @q: videobuf2 queue |
| 1892 | * |
| 1893 | * Flag that a fatal unrecoverable error has occurred and wake up all processes |
| 1894 | * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing |
| 1895 | * buffers will return -EIO. |
| 1896 | * |
| 1897 | * The error flag will be cleared when cancelling the queue, either from |
| 1898 | * vb2_streamoff or vb2_queue_release. Drivers should thus not call this |
| 1899 | * function before starting the stream, otherwise the error flag will remain set |
| 1900 | * until the queue is released when closing the device node. |
| 1901 | */ |
| 1902 | void vb2_queue_error(struct vb2_queue *q) |
| 1903 | { |
| 1904 | q->error = 1; |
| 1905 | |
| 1906 | wake_up_all(&q->done_wq); |
| 1907 | } |
| 1908 | EXPORT_SYMBOL_GPL(vb2_queue_error); |
| 1909 | |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1910 | int vb2_core_streamoff(struct vb2_queue *q, unsigned int type) |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1911 | { |
| 1912 | if (type != q->type) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1913 | dprintk(1, "invalid stream type\n"); |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1914 | return -EINVAL; |
| 1915 | } |
| 1916 | |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1917 | /* |
| 1918 | * Cancel will pause streaming and remove all buffers from the driver |
| 1919 | * and videobuf, effectively returning control over them to userspace. |
Hans Verkuil | 3f1a9a3 | 2014-02-25 09:42:45 -0300 | [diff] [blame] | 1920 | * |
| 1921 | * Note that we do this even if q->streaming == 0: if you prepare or |
| 1922 | * queue buffers, and then call streamoff without ever having called |
| 1923 | * streamon, you would still expect those buffers to be returned to |
| 1924 | * their normal dequeued state. |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1925 | */ |
| 1926 | __vb2_queue_cancel(q); |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1927 | q->waiting_for_buffers = !q->is_output; |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 1928 | q->last_buffer_dequeued = false; |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1929 | |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 1930 | dprintk(3, "successful\n"); |
Hans Verkuil | b2f2f04 | 2013-12-13 13:13:41 -0300 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1933 | EXPORT_SYMBOL_GPL(vb2_core_streamoff); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1934 | |
| 1935 | /** |
| 1936 | * __find_plane_by_offset() - find plane associated with the given offset off |
| 1937 | */ |
| 1938 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, |
| 1939 | unsigned int *_buffer, unsigned int *_plane) |
| 1940 | { |
| 1941 | struct vb2_buffer *vb; |
| 1942 | unsigned int buffer, plane; |
| 1943 | |
| 1944 | /* |
| 1945 | * Go over all buffers and their planes, comparing the given offset |
| 1946 | * with an offset assigned to each plane. If a match is found, |
| 1947 | * return its buffer and plane numbers. |
| 1948 | */ |
| 1949 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 1950 | vb = q->bufs[buffer]; |
| 1951 | |
| 1952 | for (plane = 0; plane < vb->num_planes; ++plane) { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1953 | if (vb->planes[plane].m.offset == off) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1954 | *_buffer = buffer; |
| 1955 | *_plane = plane; |
| 1956 | return 0; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | return -EINVAL; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1965 | * vb2_core_expbuf() - Export a buffer as a file descriptor |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1966 | * @q: videobuf2 queue |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1967 | * @fd: file descriptor associated with DMABUF (set by driver) * |
| 1968 | * @type: buffer type |
| 1969 | * @index: id number of the buffer |
| 1970 | * @plane: index of the plane to be exported, 0 for single plane queues |
| 1971 | * @flags: flags for newly created file, currently only O_CLOEXEC is |
| 1972 | * supported, refer to manual of open syscall for more details |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1973 | * |
| 1974 | * The return values from this function are intended to be directly returned |
| 1975 | * from vidioc_expbuf handler in driver. |
| 1976 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 1977 | int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1978 | unsigned int index, unsigned int plane, unsigned int flags) |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1979 | { |
| 1980 | struct vb2_buffer *vb = NULL; |
| 1981 | struct vb2_plane *vb_plane; |
| 1982 | int ret; |
| 1983 | struct dma_buf *dbuf; |
| 1984 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 1985 | if (q->memory != VB2_MEMORY_MMAP) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1986 | dprintk(1, "queue is not currently set up for mmap\n"); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1987 | return -EINVAL; |
| 1988 | } |
| 1989 | |
| 1990 | if (!q->mem_ops->get_dmabuf) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1991 | dprintk(1, "queue does not support DMA buffer exporting\n"); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1992 | return -EINVAL; |
| 1993 | } |
| 1994 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 1995 | if (flags & ~(O_CLOEXEC | O_ACCMODE)) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 1996 | dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n"); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 1997 | return -EINVAL; |
| 1998 | } |
| 1999 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2000 | if (type != q->type) { |
Hans Verkuil | fd4354c | 2014-04-07 09:08:47 -0300 | [diff] [blame] | 2001 | dprintk(1, "invalid buffer type\n"); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2002 | return -EINVAL; |
| 2003 | } |
| 2004 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2005 | if (index >= q->num_buffers) { |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2006 | dprintk(1, "buffer index out of range\n"); |
| 2007 | return -EINVAL; |
| 2008 | } |
| 2009 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2010 | vb = q->bufs[index]; |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2011 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2012 | if (plane >= vb->num_planes) { |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2013 | dprintk(1, "buffer plane out of range\n"); |
| 2014 | return -EINVAL; |
| 2015 | } |
| 2016 | |
Hans Verkuil | 74753cffa | 2014-04-07 09:23:50 -0300 | [diff] [blame] | 2017 | if (vb2_fileio_is_active(q)) { |
| 2018 | dprintk(1, "expbuf: file io in progress\n"); |
| 2019 | return -EBUSY; |
| 2020 | } |
| 2021 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2022 | vb_plane = &vb->planes[plane]; |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2023 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2024 | dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv, |
| 2025 | flags & O_ACCMODE); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2026 | if (IS_ERR_OR_NULL(dbuf)) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2027 | dprintk(1, "failed to export buffer %d, plane %d\n", |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2028 | index, plane); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2029 | return -EINVAL; |
| 2030 | } |
| 2031 | |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2032 | ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2033 | if (ret < 0) { |
| 2034 | dprintk(3, "buffer %d, plane %d failed to export (%d)\n", |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2035 | index, plane, ret); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2036 | dma_buf_put(dbuf); |
| 2037 | return ret; |
| 2038 | } |
| 2039 | |
| 2040 | dprintk(3, "buffer %d, plane %d exported as %d descriptor\n", |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2041 | index, plane, ret); |
| 2042 | *fd = ret; |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2043 | |
| 2044 | return 0; |
| 2045 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 2046 | EXPORT_SYMBOL_GPL(vb2_core_expbuf); |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 2047 | |
| 2048 | /** |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2049 | * vb2_mmap() - map video buffers into application address space |
| 2050 | * @q: videobuf2 queue |
| 2051 | * @vma: vma passed to the mmap file operation handler in the driver |
| 2052 | * |
| 2053 | * Should be called from mmap file operation handler of a driver. |
| 2054 | * This function maps one plane of one of the available video buffers to |
| 2055 | * userspace. To map whole video memory allocated on reqbufs, this function |
| 2056 | * has to be called once per each plane per each buffer previously allocated. |
| 2057 | * |
| 2058 | * When the userspace application calls mmap, it passes to it an offset returned |
| 2059 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as |
| 2060 | * a "cookie", which is then used to identify the plane to be mapped. |
| 2061 | * This function finds a plane with a matching offset and a mapping is performed |
| 2062 | * by the means of a provided memory operation. |
| 2063 | * |
| 2064 | * The return values from this function are intended to be directly returned |
| 2065 | * from the mmap handler in driver. |
| 2066 | */ |
| 2067 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) |
| 2068 | { |
| 2069 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2070 | struct vb2_buffer *vb; |
Hans Verkuil | ce9c224 | 2014-04-17 03:17:08 -0300 | [diff] [blame] | 2071 | unsigned int buffer = 0, plane = 0; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2072 | int ret; |
Mauro Carvalho Chehab | 7f84145 | 2013-04-19 07:18:01 -0300 | [diff] [blame] | 2073 | unsigned long length; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2074 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 2075 | if (q->memory != VB2_MEMORY_MMAP) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2076 | dprintk(1, "queue is not currently set up for mmap\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2077 | return -EINVAL; |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * Check memory area access mode. |
| 2082 | */ |
| 2083 | if (!(vma->vm_flags & VM_SHARED)) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2084 | dprintk(1, "invalid vma flags, VM_SHARED needed\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2085 | return -EINVAL; |
| 2086 | } |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 2087 | if (q->is_output) { |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2088 | if (!(vma->vm_flags & VM_WRITE)) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2089 | dprintk(1, "invalid vma flags, VM_WRITE needed\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2090 | return -EINVAL; |
| 2091 | } |
| 2092 | } else { |
| 2093 | if (!(vma->vm_flags & VM_READ)) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2094 | dprintk(1, "invalid vma flags, VM_READ needed\n"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2095 | return -EINVAL; |
| 2096 | } |
| 2097 | } |
Hans Verkuil | 74753cffa | 2014-04-07 09:23:50 -0300 | [diff] [blame] | 2098 | if (vb2_fileio_is_active(q)) { |
| 2099 | dprintk(1, "mmap: file io in progress\n"); |
| 2100 | return -EBUSY; |
| 2101 | } |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2102 | |
| 2103 | /* |
| 2104 | * Find the plane corresponding to the offset passed by userspace. |
| 2105 | */ |
| 2106 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 2107 | if (ret) |
| 2108 | return ret; |
| 2109 | |
| 2110 | vb = q->bufs[buffer]; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2111 | |
Mauro Carvalho Chehab | 7f84145 | 2013-04-19 07:18:01 -0300 | [diff] [blame] | 2112 | /* |
| 2113 | * MMAP requires page_aligned buffers. |
| 2114 | * The buffer length was page_aligned at __vb2_buf_mem_alloc(), |
| 2115 | * so, we need to do the same here. |
| 2116 | */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 2117 | length = PAGE_ALIGN(vb->planes[plane].length); |
Mauro Carvalho Chehab | 7f84145 | 2013-04-19 07:18:01 -0300 | [diff] [blame] | 2118 | if (length < (vma->vm_end - vma->vm_start)) { |
| 2119 | dprintk(1, |
| 2120 | "MMAP invalid, as it would overflow buffer length\n"); |
Seung-Woo Kim | 068a0df | 2013-04-11 23:57:57 -0300 | [diff] [blame] | 2121 | return -EINVAL; |
| 2122 | } |
| 2123 | |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 2124 | mutex_lock(&q->mmap_lock); |
Hans Verkuil | b5b4541 | 2014-01-29 11:53:25 -0300 | [diff] [blame] | 2125 | ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma); |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 2126 | mutex_unlock(&q->mmap_lock); |
Hans Verkuil | a1d36d8 | 2014-03-17 09:54:21 -0300 | [diff] [blame] | 2127 | if (ret) |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2128 | return ret; |
| 2129 | |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2130 | dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2131 | return 0; |
| 2132 | } |
| 2133 | EXPORT_SYMBOL_GPL(vb2_mmap); |
| 2134 | |
Scott Jiang | 6f524ec | 2011-09-21 09:25:23 -0300 | [diff] [blame] | 2135 | #ifndef CONFIG_MMU |
| 2136 | unsigned long vb2_get_unmapped_area(struct vb2_queue *q, |
| 2137 | unsigned long addr, |
| 2138 | unsigned long len, |
| 2139 | unsigned long pgoff, |
| 2140 | unsigned long flags) |
| 2141 | { |
| 2142 | unsigned long off = pgoff << PAGE_SHIFT; |
| 2143 | struct vb2_buffer *vb; |
| 2144 | unsigned int buffer, plane; |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 2145 | void *vaddr; |
Scott Jiang | 6f524ec | 2011-09-21 09:25:23 -0300 | [diff] [blame] | 2146 | int ret; |
| 2147 | |
Junghak Sung | bed04f9 | 2015-10-06 06:37:47 -0300 | [diff] [blame] | 2148 | if (q->memory != VB2_MEMORY_MMAP) { |
Hans Verkuil | 3050040 | 2014-04-07 09:13:22 -0300 | [diff] [blame] | 2149 | dprintk(1, "queue is not currently set up for mmap\n"); |
Scott Jiang | 6f524ec | 2011-09-21 09:25:23 -0300 | [diff] [blame] | 2150 | return -EINVAL; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * Find the plane corresponding to the offset passed by userspace. |
| 2155 | */ |
| 2156 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 2157 | if (ret) |
| 2158 | return ret; |
| 2159 | |
| 2160 | vb = q->bufs[buffer]; |
| 2161 | |
Hans Verkuil | f035eb4 | 2014-08-07 03:47:14 -0300 | [diff] [blame] | 2162 | vaddr = vb2_plane_vaddr(vb, plane); |
| 2163 | return vaddr ? (unsigned long)vaddr : -EINVAL; |
Scott Jiang | 6f524ec | 2011-09-21 09:25:23 -0300 | [diff] [blame] | 2164 | } |
| 2165 | EXPORT_SYMBOL_GPL(vb2_get_unmapped_area); |
| 2166 | #endif |
| 2167 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2168 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2169 | * vb2_core_queue_init() - initialize a videobuf2 queue |
| 2170 | * @q: videobuf2 queue; this structure should be allocated in driver |
| 2171 | * |
| 2172 | * The vb2_queue structure should be allocated by the driver. The driver is |
| 2173 | * responsible of clearing it's content and setting initial values for some |
| 2174 | * required entries before calling this function. |
| 2175 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer |
| 2176 | * to the struct vb2_queue description in include/media/videobuf2-core.h |
| 2177 | * for more information. |
| 2178 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 2179 | int vb2_core_queue_init(struct vb2_queue *q) |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2180 | { |
| 2181 | /* |
| 2182 | * Sanity check |
| 2183 | */ |
| 2184 | if (WARN_ON(!q) || |
| 2185 | WARN_ON(!q->ops) || |
| 2186 | WARN_ON(!q->mem_ops) || |
| 2187 | WARN_ON(!q->type) || |
| 2188 | WARN_ON(!q->io_modes) || |
| 2189 | WARN_ON(!q->ops->queue_setup) || |
| 2190 | WARN_ON(!q->ops->buf_queue)) |
| 2191 | return -EINVAL; |
| 2192 | |
| 2193 | INIT_LIST_HEAD(&q->queued_list); |
| 2194 | INIT_LIST_HEAD(&q->done_list); |
| 2195 | spin_lock_init(&q->done_lock); |
| 2196 | mutex_init(&q->mmap_lock); |
| 2197 | init_waitqueue_head(&q->done_wq); |
| 2198 | |
| 2199 | if (q->buf_struct_size == 0) |
| 2200 | q->buf_struct_size = sizeof(struct vb2_buffer); |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 2204 | EXPORT_SYMBOL_GPL(vb2_core_queue_init); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2205 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2206 | static int __vb2_init_fileio(struct vb2_queue *q, int read); |
| 2207 | static int __vb2_cleanup_fileio(struct vb2_queue *q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2208 | /** |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2209 | * vb2_core_queue_release() - stop streaming, release the queue and free memory |
| 2210 | * @q: videobuf2 queue |
| 2211 | * |
| 2212 | * This function stops streaming and performs necessary clean ups, including |
| 2213 | * freeing video buffer memory. The driver is responsible for freeing |
| 2214 | * the vb2_queue structure itself. |
| 2215 | */ |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 2216 | void vb2_core_queue_release(struct vb2_queue *q) |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2217 | { |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2218 | __vb2_cleanup_fileio(q); |
Junghak Sung | b0e0e1f | 2015-10-06 06:37:48 -0300 | [diff] [blame] | 2219 | __vb2_queue_cancel(q); |
| 2220 | mutex_lock(&q->mmap_lock); |
| 2221 | __vb2_queue_free(q, q->num_buffers); |
| 2222 | mutex_unlock(&q->mmap_lock); |
| 2223 | } |
Junghak Sung | 3c5be98 | 2015-10-06 06:37:49 -0300 | [diff] [blame] | 2224 | EXPORT_SYMBOL_GPL(vb2_core_queue_release); |
Hans Verkuil | 4c1ffca | 2012-07-02 05:59:18 -0300 | [diff] [blame] | 2225 | |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2226 | /** |
| 2227 | * vb2_core_poll() - implements poll userspace operation |
| 2228 | * @q: videobuf2 queue |
| 2229 | * @file: file argument passed to the poll file operation handler |
| 2230 | * @wait: wait argument passed to the poll file operation handler |
| 2231 | * |
| 2232 | * This function implements poll file operation handler for a driver. |
| 2233 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will |
| 2234 | * be informed that the file descriptor of a video device is available for |
| 2235 | * reading. |
| 2236 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor |
| 2237 | * will be reported as available for writing. |
| 2238 | * |
| 2239 | * The return values from this function are intended to be directly returned |
| 2240 | * from poll handler in driver. |
| 2241 | */ |
| 2242 | unsigned int vb2_core_poll(struct vb2_queue *q, struct file *file, |
| 2243 | poll_table *wait) |
| 2244 | { |
| 2245 | unsigned long req_events = poll_requested_events(wait); |
| 2246 | struct vb2_buffer *vb = NULL; |
| 2247 | unsigned long flags; |
| 2248 | |
| 2249 | if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM))) |
| 2250 | return 0; |
| 2251 | if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM))) |
| 2252 | return 0; |
| 2253 | |
| 2254 | /* |
| 2255 | * Start file I/O emulator only if streaming API has not been used yet. |
| 2256 | */ |
| 2257 | if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) { |
| 2258 | if (!q->is_output && (q->io_modes & VB2_READ) && |
| 2259 | (req_events & (POLLIN | POLLRDNORM))) { |
| 2260 | if (__vb2_init_fileio(q, 1)) |
| 2261 | return POLLERR; |
| 2262 | } |
| 2263 | if (q->is_output && (q->io_modes & VB2_WRITE) && |
| 2264 | (req_events & (POLLOUT | POLLWRNORM))) { |
| 2265 | if (__vb2_init_fileio(q, 0)) |
| 2266 | return POLLERR; |
| 2267 | /* |
| 2268 | * Write to OUTPUT queue can be done immediately. |
| 2269 | */ |
| 2270 | return POLLOUT | POLLWRNORM; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * There is nothing to wait for if the queue isn't streaming, or if the |
| 2276 | * error flag is set. |
| 2277 | */ |
| 2278 | if (!vb2_is_streaming(q) || q->error) |
| 2279 | return POLLERR; |
| 2280 | |
| 2281 | /* |
| 2282 | * For output streams you can call write() as long as there are fewer |
| 2283 | * buffers queued than there are buffers available. |
| 2284 | */ |
| 2285 | if (q->is_output && q->fileio && q->queued_count < q->num_buffers) |
| 2286 | return POLLOUT | POLLWRNORM; |
| 2287 | |
| 2288 | if (list_empty(&q->done_list)) { |
| 2289 | /* |
| 2290 | * If the last buffer was dequeued from a capture queue, |
| 2291 | * return immediately. DQBUF will return -EPIPE. |
| 2292 | */ |
| 2293 | if (q->last_buffer_dequeued) |
| 2294 | return POLLIN | POLLRDNORM; |
| 2295 | |
| 2296 | poll_wait(file, &q->done_wq, wait); |
| 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * Take first buffer available for dequeuing. |
| 2301 | */ |
| 2302 | spin_lock_irqsave(&q->done_lock, flags); |
| 2303 | if (!list_empty(&q->done_list)) |
| 2304 | vb = list_first_entry(&q->done_list, struct vb2_buffer, |
| 2305 | done_entry); |
| 2306 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 2307 | |
| 2308 | if (vb && (vb->state == VB2_BUF_STATE_DONE |
| 2309 | || vb->state == VB2_BUF_STATE_ERROR)) { |
| 2310 | return (q->is_output) ? |
| 2311 | POLLOUT | POLLWRNORM : |
| 2312 | POLLIN | POLLRDNORM; |
| 2313 | } |
| 2314 | return 0; |
| 2315 | } |
| 2316 | EXPORT_SYMBOL_GPL(vb2_core_poll); |
| 2317 | |
| 2318 | /** |
| 2319 | * struct vb2_fileio_buf - buffer context used by file io emulator |
| 2320 | * |
| 2321 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 2322 | * write) calls on top of streaming API. This structure is used for |
| 2323 | * tracking context related to the buffers. |
| 2324 | */ |
| 2325 | struct vb2_fileio_buf { |
| 2326 | void *vaddr; |
| 2327 | unsigned int size; |
| 2328 | unsigned int pos; |
| 2329 | unsigned int queued:1; |
| 2330 | }; |
| 2331 | |
| 2332 | /** |
| 2333 | * struct vb2_fileio_data - queue context used by file io emulator |
| 2334 | * |
| 2335 | * @cur_index: the index of the buffer currently being read from or |
| 2336 | * written to. If equal to q->num_buffers then a new buffer |
| 2337 | * must be dequeued. |
| 2338 | * @initial_index: in the read() case all buffers are queued up immediately |
| 2339 | * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles |
| 2340 | * buffers. However, in the write() case no buffers are initially |
| 2341 | * queued, instead whenever a buffer is full it is queued up by |
| 2342 | * __vb2_perform_fileio(). Only once all available buffers have |
| 2343 | * been queued up will __vb2_perform_fileio() start to dequeue |
| 2344 | * buffers. This means that initially __vb2_perform_fileio() |
| 2345 | * needs to know what buffer index to use when it is queuing up |
| 2346 | * the buffers for the first time. That initial index is stored |
| 2347 | * in this field. Once it is equal to q->num_buffers all |
| 2348 | * available buffers have been queued and __vb2_perform_fileio() |
| 2349 | * should start the normal dequeue/queue cycle. |
| 2350 | * |
| 2351 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 2352 | * write) calls on top of streaming API. For proper operation it required |
| 2353 | * this structure to save the driver state between each call of the read |
| 2354 | * or write function. |
| 2355 | */ |
| 2356 | struct vb2_fileio_data { |
| 2357 | unsigned int count; |
| 2358 | unsigned int type; |
| 2359 | unsigned int memory; |
| 2360 | struct vb2_buffer *b; |
| 2361 | struct vb2_fileio_buf bufs[VB2_MAX_FRAME]; |
| 2362 | unsigned int cur_index; |
| 2363 | unsigned int initial_index; |
| 2364 | unsigned int q_count; |
| 2365 | unsigned int dq_count; |
| 2366 | unsigned read_once:1; |
| 2367 | unsigned write_immediately:1; |
| 2368 | }; |
| 2369 | |
| 2370 | /** |
| 2371 | * __vb2_init_fileio() - initialize file io emulator |
| 2372 | * @q: videobuf2 queue |
| 2373 | * @read: mode selector (1 means read, 0 means write) |
| 2374 | */ |
| 2375 | static int __vb2_init_fileio(struct vb2_queue *q, int read) |
| 2376 | { |
| 2377 | struct vb2_fileio_data *fileio; |
| 2378 | int i, ret; |
| 2379 | unsigned int count = 0; |
| 2380 | |
| 2381 | /* |
| 2382 | * Sanity check |
| 2383 | */ |
| 2384 | if (WARN_ON((read && !(q->io_modes & VB2_READ)) || |
| 2385 | (!read && !(q->io_modes & VB2_WRITE)))) |
| 2386 | return -EINVAL; |
| 2387 | |
| 2388 | /* |
| 2389 | * Check if device supports mapping buffers to kernel virtual space. |
| 2390 | */ |
| 2391 | if (!q->mem_ops->vaddr) |
| 2392 | return -EBUSY; |
| 2393 | |
| 2394 | /* |
| 2395 | * Check if streaming api has not been already activated. |
| 2396 | */ |
| 2397 | if (q->streaming || q->num_buffers > 0) |
| 2398 | return -EBUSY; |
| 2399 | |
| 2400 | /* |
| 2401 | * Start with count 1, driver can increase it in queue_setup() |
| 2402 | */ |
| 2403 | count = 1; |
| 2404 | |
| 2405 | dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", |
| 2406 | (read) ? "read" : "write", count, q->fileio_read_once, |
| 2407 | q->fileio_write_immediately); |
| 2408 | |
Mauro Carvalho Chehab | b62ef37 | 2015-12-18 14:11:31 -0200 | [diff] [blame] | 2409 | fileio = kzalloc(sizeof(*fileio), GFP_KERNEL); |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2410 | if (fileio == NULL) |
| 2411 | return -ENOMEM; |
| 2412 | |
| 2413 | fileio->b = kzalloc(q->buf_struct_size, GFP_KERNEL); |
Mauro Carvalho Chehab | b62ef37 | 2015-12-18 14:11:31 -0200 | [diff] [blame] | 2414 | if (fileio->b == NULL) { |
| 2415 | kfree(fileio); |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2416 | return -ENOMEM; |
Mauro Carvalho Chehab | b62ef37 | 2015-12-18 14:11:31 -0200 | [diff] [blame] | 2417 | } |
Junghak Sung | af3bac1 | 2015-11-03 08:16:42 -0200 | [diff] [blame] | 2418 | |
| 2419 | fileio->read_once = q->fileio_read_once; |
| 2420 | fileio->write_immediately = q->fileio_write_immediately; |
| 2421 | |
| 2422 | /* |
| 2423 | * Request buffers and use MMAP type to force driver |
| 2424 | * to allocate buffers by itself. |
| 2425 | */ |
| 2426 | fileio->count = count; |
| 2427 | fileio->memory = VB2_MEMORY_MMAP; |
| 2428 | fileio->type = q->type; |
| 2429 | q->fileio = fileio; |
| 2430 | ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count); |
| 2431 | if (ret) |
| 2432 | goto err_kfree; |
| 2433 | |
| 2434 | /* |
| 2435 | * Check if plane_count is correct |
| 2436 | * (multiplane buffers are not supported). |
| 2437 | */ |
| 2438 | if (q->bufs[0]->num_planes != 1) { |
| 2439 | ret = -EBUSY; |
| 2440 | goto err_reqbufs; |
| 2441 | } |
| 2442 | |
| 2443 | /* |
| 2444 | * Get kernel address of each buffer. |
| 2445 | */ |
| 2446 | for (i = 0; i < q->num_buffers; i++) { |
| 2447 | fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); |
| 2448 | if (fileio->bufs[i].vaddr == NULL) { |
| 2449 | ret = -EINVAL; |
| 2450 | goto err_reqbufs; |
| 2451 | } |
| 2452 | fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); |
| 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * Read mode requires pre queuing of all buffers. |
| 2457 | */ |
| 2458 | if (read) { |
| 2459 | /* |
| 2460 | * Queue all buffers. |
| 2461 | */ |
| 2462 | for (i = 0; i < q->num_buffers; i++) { |
| 2463 | struct vb2_buffer *b = fileio->b; |
| 2464 | |
| 2465 | memset(b, 0, q->buf_struct_size); |
| 2466 | b->type = q->type; |
| 2467 | b->memory = q->memory; |
| 2468 | b->index = i; |
| 2469 | ret = vb2_core_qbuf(q, i, b); |
| 2470 | if (ret) |
| 2471 | goto err_reqbufs; |
| 2472 | fileio->bufs[i].queued = 1; |
| 2473 | } |
| 2474 | /* |
| 2475 | * All buffers have been queued, so mark that by setting |
| 2476 | * initial_index to q->num_buffers |
| 2477 | */ |
| 2478 | fileio->initial_index = q->num_buffers; |
| 2479 | fileio->cur_index = q->num_buffers; |
| 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * Start streaming. |
| 2484 | */ |
| 2485 | ret = vb2_core_streamon(q, q->type); |
| 2486 | if (ret) |
| 2487 | goto err_reqbufs; |
| 2488 | |
| 2489 | return ret; |
| 2490 | |
| 2491 | err_reqbufs: |
| 2492 | fileio->count = 0; |
| 2493 | vb2_core_reqbufs(q, fileio->memory, &fileio->count); |
| 2494 | |
| 2495 | err_kfree: |
| 2496 | q->fileio = NULL; |
| 2497 | kfree(fileio); |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
| 2501 | /** |
| 2502 | * __vb2_cleanup_fileio() - free resourced used by file io emulator |
| 2503 | * @q: videobuf2 queue |
| 2504 | */ |
| 2505 | static int __vb2_cleanup_fileio(struct vb2_queue *q) |
| 2506 | { |
| 2507 | struct vb2_fileio_data *fileio = q->fileio; |
| 2508 | |
| 2509 | if (fileio) { |
| 2510 | vb2_core_streamoff(q, q->type); |
| 2511 | q->fileio = NULL; |
| 2512 | fileio->count = 0; |
| 2513 | vb2_core_reqbufs(q, fileio->memory, &fileio->count); |
| 2514 | kfree(fileio->b); |
| 2515 | kfree(fileio); |
| 2516 | dprintk(3, "file io emulator closed\n"); |
| 2517 | } |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
| 2521 | /** |
| 2522 | * __vb2_perform_fileio() - perform a single file io (read or write) operation |
| 2523 | * @q: videobuf2 queue |
| 2524 | * @data: pointed to target userspace buffer |
| 2525 | * @count: number of bytes to read or write |
| 2526 | * @ppos: file handle position tracking pointer |
| 2527 | * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) |
| 2528 | * @read: access mode selector (1 means read, 0 means write) |
| 2529 | */ |
| 2530 | static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, |
| 2531 | loff_t *ppos, int nonblock, int read) |
| 2532 | { |
| 2533 | struct vb2_fileio_data *fileio; |
| 2534 | struct vb2_fileio_buf *buf; |
| 2535 | bool is_multiplanar = q->is_multiplanar; |
| 2536 | /* |
| 2537 | * When using write() to write data to an output video node the vb2 core |
| 2538 | * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody |
| 2539 | * else is able to provide this information with the write() operation. |
| 2540 | */ |
| 2541 | bool copy_timestamp = !read && q->copy_timestamp; |
| 2542 | int ret, index; |
| 2543 | |
| 2544 | dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n", |
| 2545 | read ? "read" : "write", (long)*ppos, count, |
| 2546 | nonblock ? "non" : ""); |
| 2547 | |
| 2548 | if (!data) |
| 2549 | return -EINVAL; |
| 2550 | |
| 2551 | /* |
| 2552 | * Initialize emulator on first call. |
| 2553 | */ |
| 2554 | if (!vb2_fileio_is_active(q)) { |
| 2555 | ret = __vb2_init_fileio(q, read); |
| 2556 | dprintk(3, "vb2_init_fileio result: %d\n", ret); |
| 2557 | if (ret) |
| 2558 | return ret; |
| 2559 | } |
| 2560 | fileio = q->fileio; |
| 2561 | |
| 2562 | /* |
| 2563 | * Check if we need to dequeue the buffer. |
| 2564 | */ |
| 2565 | index = fileio->cur_index; |
| 2566 | if (index >= q->num_buffers) { |
| 2567 | struct vb2_buffer *b = fileio->b; |
| 2568 | |
| 2569 | /* |
| 2570 | * Call vb2_dqbuf to get buffer back. |
| 2571 | */ |
| 2572 | memset(b, 0, q->buf_struct_size); |
| 2573 | b->type = q->type; |
| 2574 | b->memory = q->memory; |
| 2575 | ret = vb2_core_dqbuf(q, b, nonblock); |
| 2576 | dprintk(5, "vb2_dqbuf result: %d\n", ret); |
| 2577 | if (ret) |
| 2578 | return ret; |
| 2579 | fileio->dq_count += 1; |
| 2580 | |
| 2581 | fileio->cur_index = index = b->index; |
| 2582 | buf = &fileio->bufs[index]; |
| 2583 | |
| 2584 | /* |
| 2585 | * Get number of bytes filled by the driver |
| 2586 | */ |
| 2587 | buf->pos = 0; |
| 2588 | buf->queued = 0; |
| 2589 | buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0) |
| 2590 | : vb2_plane_size(q->bufs[index], 0); |
| 2591 | /* Compensate for data_offset on read in the multiplanar case. */ |
| 2592 | if (is_multiplanar && read && |
| 2593 | b->planes[0].data_offset < buf->size) { |
| 2594 | buf->pos = b->planes[0].data_offset; |
| 2595 | buf->size -= buf->pos; |
| 2596 | } |
| 2597 | } else { |
| 2598 | buf = &fileio->bufs[index]; |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * Limit count on last few bytes of the buffer. |
| 2603 | */ |
| 2604 | if (buf->pos + count > buf->size) { |
| 2605 | count = buf->size - buf->pos; |
| 2606 | dprintk(5, "reducing read count: %zd\n", count); |
| 2607 | } |
| 2608 | |
| 2609 | /* |
| 2610 | * Transfer data to userspace. |
| 2611 | */ |
| 2612 | dprintk(3, "copying %zd bytes - buffer %d, offset %u\n", |
| 2613 | count, index, buf->pos); |
| 2614 | if (read) |
| 2615 | ret = copy_to_user(data, buf->vaddr + buf->pos, count); |
| 2616 | else |
| 2617 | ret = copy_from_user(buf->vaddr + buf->pos, data, count); |
| 2618 | if (ret) { |
| 2619 | dprintk(3, "error copying data\n"); |
| 2620 | return -EFAULT; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Update counters. |
| 2625 | */ |
| 2626 | buf->pos += count; |
| 2627 | *ppos += count; |
| 2628 | |
| 2629 | /* |
| 2630 | * Queue next buffer if required. |
| 2631 | */ |
| 2632 | if (buf->pos == buf->size || (!read && fileio->write_immediately)) { |
| 2633 | struct vb2_buffer *b = fileio->b; |
| 2634 | |
| 2635 | /* |
| 2636 | * Check if this is the last buffer to read. |
| 2637 | */ |
| 2638 | if (read && fileio->read_once && fileio->dq_count == 1) { |
| 2639 | dprintk(3, "read limit reached\n"); |
| 2640 | return __vb2_cleanup_fileio(q); |
| 2641 | } |
| 2642 | |
| 2643 | /* |
| 2644 | * Call vb2_qbuf and give buffer to the driver. |
| 2645 | */ |
| 2646 | memset(b, 0, q->buf_struct_size); |
| 2647 | b->type = q->type; |
| 2648 | b->memory = q->memory; |
| 2649 | b->index = index; |
| 2650 | b->planes[0].bytesused = buf->pos; |
| 2651 | |
| 2652 | if (copy_timestamp) |
| 2653 | b->timestamp = ktime_get_ns(); |
| 2654 | ret = vb2_core_qbuf(q, index, b); |
| 2655 | dprintk(5, "vb2_dbuf result: %d\n", ret); |
| 2656 | if (ret) |
| 2657 | return ret; |
| 2658 | |
| 2659 | /* |
| 2660 | * Buffer has been queued, update the status |
| 2661 | */ |
| 2662 | buf->pos = 0; |
| 2663 | buf->queued = 1; |
| 2664 | buf->size = vb2_plane_size(q->bufs[index], 0); |
| 2665 | fileio->q_count += 1; |
| 2666 | /* |
| 2667 | * If we are queuing up buffers for the first time, then |
| 2668 | * increase initial_index by one. |
| 2669 | */ |
| 2670 | if (fileio->initial_index < q->num_buffers) |
| 2671 | fileio->initial_index++; |
| 2672 | /* |
| 2673 | * The next buffer to use is either a buffer that's going to be |
| 2674 | * queued for the first time (initial_index < q->num_buffers) |
| 2675 | * or it is equal to q->num_buffers, meaning that the next |
| 2676 | * time we need to dequeue a buffer since we've now queued up |
| 2677 | * all the 'first time' buffers. |
| 2678 | */ |
| 2679 | fileio->cur_index = fileio->initial_index; |
| 2680 | } |
| 2681 | |
| 2682 | /* |
| 2683 | * Return proper number of bytes processed. |
| 2684 | */ |
| 2685 | if (ret == 0) |
| 2686 | ret = count; |
| 2687 | return ret; |
| 2688 | } |
| 2689 | |
| 2690 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, |
| 2691 | loff_t *ppos, int nonblocking) |
| 2692 | { |
| 2693 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); |
| 2694 | } |
| 2695 | EXPORT_SYMBOL_GPL(vb2_read); |
| 2696 | |
| 2697 | size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count, |
| 2698 | loff_t *ppos, int nonblocking) |
| 2699 | { |
| 2700 | return __vb2_perform_fileio(q, (char __user *) data, count, |
| 2701 | ppos, nonblocking, 0); |
| 2702 | } |
| 2703 | EXPORT_SYMBOL_GPL(vb2_write); |
| 2704 | |
| 2705 | struct vb2_threadio_data { |
| 2706 | struct task_struct *thread; |
| 2707 | vb2_thread_fnc fnc; |
| 2708 | void *priv; |
| 2709 | bool stop; |
| 2710 | }; |
| 2711 | |
| 2712 | static int vb2_thread(void *data) |
| 2713 | { |
| 2714 | struct vb2_queue *q = data; |
| 2715 | struct vb2_threadio_data *threadio = q->threadio; |
| 2716 | struct vb2_fileio_data *fileio = q->fileio; |
| 2717 | bool copy_timestamp = false; |
| 2718 | int prequeue = 0; |
| 2719 | int index = 0; |
| 2720 | int ret = 0; |
| 2721 | |
| 2722 | if (q->is_output) { |
| 2723 | prequeue = q->num_buffers; |
| 2724 | copy_timestamp = q->copy_timestamp; |
| 2725 | } |
| 2726 | |
| 2727 | set_freezable(); |
| 2728 | |
| 2729 | for (;;) { |
| 2730 | struct vb2_buffer *vb; |
| 2731 | struct vb2_buffer *b = fileio->b; |
| 2732 | |
| 2733 | /* |
| 2734 | * Call vb2_dqbuf to get buffer back. |
| 2735 | */ |
| 2736 | memset(b, 0, q->buf_struct_size); |
| 2737 | b->type = q->type; |
| 2738 | b->memory = q->memory; |
| 2739 | if (prequeue) { |
| 2740 | b->index = index++; |
| 2741 | prequeue--; |
| 2742 | } else { |
| 2743 | call_void_qop(q, wait_finish, q); |
| 2744 | if (!threadio->stop) |
| 2745 | ret = vb2_core_dqbuf(q, b, 0); |
| 2746 | call_void_qop(q, wait_prepare, q); |
| 2747 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); |
| 2748 | } |
| 2749 | if (ret || threadio->stop) |
| 2750 | break; |
| 2751 | try_to_freeze(); |
| 2752 | |
| 2753 | vb = q->bufs[b->index]; |
| 2754 | if (b->state == VB2_BUF_STATE_DONE) |
| 2755 | if (threadio->fnc(vb, threadio->priv)) |
| 2756 | break; |
| 2757 | call_void_qop(q, wait_finish, q); |
| 2758 | if (copy_timestamp) |
| 2759 | b->timestamp = ktime_get_ns();; |
| 2760 | if (!threadio->stop) |
| 2761 | ret = vb2_core_qbuf(q, b->index, b); |
| 2762 | call_void_qop(q, wait_prepare, q); |
| 2763 | if (ret || threadio->stop) |
| 2764 | break; |
| 2765 | } |
| 2766 | |
| 2767 | /* Hmm, linux becomes *very* unhappy without this ... */ |
| 2768 | while (!kthread_should_stop()) { |
| 2769 | set_current_state(TASK_INTERRUPTIBLE); |
| 2770 | schedule(); |
| 2771 | } |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | /* |
| 2776 | * This function should not be used for anything else but the videobuf2-dvb |
| 2777 | * support. If you think you have another good use-case for this, then please |
| 2778 | * contact the linux-media mailinglist first. |
| 2779 | */ |
| 2780 | int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv, |
| 2781 | const char *thread_name) |
| 2782 | { |
| 2783 | struct vb2_threadio_data *threadio; |
| 2784 | int ret = 0; |
| 2785 | |
| 2786 | if (q->threadio) |
| 2787 | return -EBUSY; |
| 2788 | if (vb2_is_busy(q)) |
| 2789 | return -EBUSY; |
| 2790 | if (WARN_ON(q->fileio)) |
| 2791 | return -EBUSY; |
| 2792 | |
| 2793 | threadio = kzalloc(sizeof(*threadio), GFP_KERNEL); |
| 2794 | if (threadio == NULL) |
| 2795 | return -ENOMEM; |
| 2796 | threadio->fnc = fnc; |
| 2797 | threadio->priv = priv; |
| 2798 | |
| 2799 | ret = __vb2_init_fileio(q, !q->is_output); |
| 2800 | dprintk(3, "file io: vb2_init_fileio result: %d\n", ret); |
| 2801 | if (ret) |
| 2802 | goto nomem; |
| 2803 | q->threadio = threadio; |
| 2804 | threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name); |
| 2805 | if (IS_ERR(threadio->thread)) { |
| 2806 | ret = PTR_ERR(threadio->thread); |
| 2807 | threadio->thread = NULL; |
| 2808 | goto nothread; |
| 2809 | } |
| 2810 | return 0; |
| 2811 | |
| 2812 | nothread: |
| 2813 | __vb2_cleanup_fileio(q); |
| 2814 | nomem: |
| 2815 | kfree(threadio); |
| 2816 | return ret; |
| 2817 | } |
| 2818 | EXPORT_SYMBOL_GPL(vb2_thread_start); |
| 2819 | |
| 2820 | int vb2_thread_stop(struct vb2_queue *q) |
| 2821 | { |
| 2822 | struct vb2_threadio_data *threadio = q->threadio; |
| 2823 | int err; |
| 2824 | |
| 2825 | if (threadio == NULL) |
| 2826 | return 0; |
| 2827 | threadio->stop = true; |
| 2828 | /* Wake up all pending sleeps in the thread */ |
| 2829 | vb2_queue_error(q); |
| 2830 | err = kthread_stop(threadio->thread); |
| 2831 | __vb2_cleanup_fileio(q); |
| 2832 | threadio->thread = NULL; |
| 2833 | kfree(threadio); |
| 2834 | q->threadio = NULL; |
| 2835 | return err; |
| 2836 | } |
| 2837 | EXPORT_SYMBOL_GPL(vb2_thread_stop); |
| 2838 | |
Hans Verkuil | df868ea | 2015-11-20 08:17:23 -0200 | [diff] [blame] | 2839 | MODULE_DESCRIPTION("Media buffer core framework"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 2840 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 2841 | MODULE_LICENSE("GPL"); |