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