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