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