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