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