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