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