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 #include "videobuf2-internal.h"
32
33 int vb2_debug;
34 EXPORT_SYMBOL_GPL(vb2_debug);
35 module_param_named(debug, vb2_debug, int, 0644);
36
37 static void __vb2_queue_cancel(struct vb2_queue *q);
38 static void __enqueue_in_driver(struct vb2_buffer *vb);
39
40 /**
41 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
42 */
__vb2_buf_mem_alloc(struct vb2_buffer * vb)43 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
44 {
45 struct vb2_queue *q = vb->vb2_queue;
46 enum dma_data_direction dma_dir =
47 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
48 void *mem_priv;
49 int plane;
50
51 /*
52 * Allocate memory for all planes in this buffer
53 * NOTE: mmapped areas should be page aligned
54 */
55 for (plane = 0; plane < vb->num_planes; ++plane) {
56 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
57
58 mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
59 size, dma_dir, q->gfp_flags);
60 if (IS_ERR_OR_NULL(mem_priv))
61 goto free;
62
63 /* Associate allocator private data with this plane */
64 vb->planes[plane].mem_priv = mem_priv;
65 vb->planes[plane].length = q->plane_sizes[plane];
66 }
67
68 return 0;
69 free:
70 /* Free already allocated memory if one of the allocations failed */
71 for (; plane > 0; --plane) {
72 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
73 vb->planes[plane - 1].mem_priv = NULL;
74 }
75
76 return -ENOMEM;
77 }
78
79 /**
80 * __vb2_buf_mem_free() - free memory of the given buffer
81 */
__vb2_buf_mem_free(struct vb2_buffer * vb)82 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
83 {
84 unsigned int plane;
85
86 for (plane = 0; plane < vb->num_planes; ++plane) {
87 call_void_memop(vb, put, vb->planes[plane].mem_priv);
88 vb->planes[plane].mem_priv = NULL;
89 dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
90 }
91 }
92
93 /**
94 * __vb2_buf_userptr_put() - release userspace memory associated with
95 * a USERPTR buffer
96 */
__vb2_buf_userptr_put(struct vb2_buffer * vb)97 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
98 {
99 unsigned int plane;
100
101 for (plane = 0; plane < vb->num_planes; ++plane) {
102 if (vb->planes[plane].mem_priv)
103 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
104 vb->planes[plane].mem_priv = NULL;
105 }
106 }
107
108 /**
109 * __vb2_plane_dmabuf_put() - release memory associated with
110 * a DMABUF shared plane
111 */
__vb2_plane_dmabuf_put(struct vb2_buffer * vb,struct vb2_plane * p)112 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
113 {
114 if (!p->mem_priv)
115 return;
116
117 if (p->dbuf_mapped)
118 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
119
120 call_void_memop(vb, detach_dmabuf, p->mem_priv);
121 dma_buf_put(p->dbuf);
122 p->mem_priv = NULL;
123 p->dbuf = NULL;
124 p->dbuf_mapped = 0;
125 }
126
127 /**
128 * __vb2_buf_dmabuf_put() - release memory associated with
129 * a DMABUF shared buffer
130 */
__vb2_buf_dmabuf_put(struct vb2_buffer * vb)131 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
132 {
133 unsigned int plane;
134
135 for (plane = 0; plane < vb->num_planes; ++plane)
136 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
137 }
138
139 /**
140 * __setup_lengths() - setup initial lengths for every plane in
141 * every buffer on the queue
142 */
__setup_lengths(struct vb2_queue * q,unsigned int n)143 static void __setup_lengths(struct vb2_queue *q, unsigned int n)
144 {
145 unsigned int buffer, plane;
146 struct vb2_buffer *vb;
147
148 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
149 vb = q->bufs[buffer];
150 if (!vb)
151 continue;
152
153 for (plane = 0; plane < vb->num_planes; ++plane)
154 vb->planes[plane].length = q->plane_sizes[plane];
155 }
156 }
157
158 /**
159 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
160 * every buffer on the queue
161 */
__setup_offsets(struct vb2_queue * q,unsigned int n)162 static void __setup_offsets(struct vb2_queue *q, unsigned int n)
163 {
164 unsigned int buffer, plane;
165 struct vb2_buffer *vb;
166 unsigned long off;
167
168 if (q->num_buffers) {
169 struct vb2_plane *p;
170 vb = q->bufs[q->num_buffers - 1];
171 p = &vb->planes[vb->num_planes - 1];
172 off = PAGE_ALIGN(p->m.offset + p->length);
173 } else {
174 off = 0;
175 }
176
177 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
178 vb = q->bufs[buffer];
179 if (!vb)
180 continue;
181
182 for (plane = 0; plane < vb->num_planes; ++plane) {
183 vb->planes[plane].m.offset = off;
184
185 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
186 buffer, plane, off);
187
188 off += vb->planes[plane].length;
189 off = PAGE_ALIGN(off);
190 }
191 }
192 }
193
194 /**
195 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
196 * video buffer memory for all buffers/planes on the queue and initializes the
197 * queue
198 *
199 * Returns the number of buffers successfully allocated.
200 */
__vb2_queue_alloc(struct vb2_queue * q,enum vb2_memory memory,unsigned int num_buffers,unsigned int num_planes)201 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
202 unsigned int num_buffers, unsigned int num_planes)
203 {
204 unsigned int buffer;
205 struct vb2_buffer *vb;
206 int ret;
207
208 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
209 num_buffers = min_t(unsigned int, num_buffers,
210 VB2_MAX_FRAME - q->num_buffers);
211
212 for (buffer = 0; buffer < num_buffers; ++buffer) {
213 /* Allocate videobuf buffer structures */
214 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
215 if (!vb) {
216 dprintk(1, "memory alloc for buffer struct failed\n");
217 break;
218 }
219
220 vb->state = VB2_BUF_STATE_DEQUEUED;
221 vb->vb2_queue = q;
222 vb->num_planes = num_planes;
223 vb->index = q->num_buffers + buffer;
224 vb->type = q->type;
225 vb->memory = memory;
226
227 /* Allocate video buffer memory for the MMAP type */
228 if (memory == VB2_MEMORY_MMAP) {
229 ret = __vb2_buf_mem_alloc(vb);
230 if (ret) {
231 dprintk(1, "failed allocating memory for "
232 "buffer %d\n", buffer);
233 kfree(vb);
234 break;
235 }
236 /*
237 * Call the driver-provided buffer initialization
238 * callback, if given. An error in initialization
239 * results in queue setup failure.
240 */
241 ret = call_vb_qop(vb, buf_init, vb);
242 if (ret) {
243 dprintk(1, "buffer %d %p initialization"
244 " failed\n", buffer, vb);
245 __vb2_buf_mem_free(vb);
246 kfree(vb);
247 break;
248 }
249 }
250
251 q->bufs[q->num_buffers + buffer] = vb;
252 }
253
254 __setup_lengths(q, buffer);
255 if (memory == VB2_MEMORY_MMAP)
256 __setup_offsets(q, buffer);
257
258 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
259 buffer, num_planes);
260
261 return buffer;
262 }
263
264 /**
265 * __vb2_free_mem() - release all video buffer memory for a given queue
266 */
__vb2_free_mem(struct vb2_queue * q,unsigned int buffers)267 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
268 {
269 unsigned int buffer;
270 struct vb2_buffer *vb;
271
272 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
273 ++buffer) {
274 vb = q->bufs[buffer];
275 if (!vb)
276 continue;
277
278 /* Free MMAP buffers or release USERPTR buffers */
279 if (q->memory == VB2_MEMORY_MMAP)
280 __vb2_buf_mem_free(vb);
281 else if (q->memory == VB2_MEMORY_DMABUF)
282 __vb2_buf_dmabuf_put(vb);
283 else
284 __vb2_buf_userptr_put(vb);
285 }
286 }
287
288 /**
289 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
290 * related information, if no buffers are left return the queue to an
291 * uninitialized state. Might be called even if the queue has already been freed.
292 */
__vb2_queue_free(struct vb2_queue * q,unsigned int buffers)293 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
294 {
295 unsigned int buffer;
296
297 /*
298 * Sanity check: when preparing a buffer the queue lock is released for
299 * a short while (see __buf_prepare for the details), which would allow
300 * a race with a reqbufs which can call this function. Removing the
301 * buffers from underneath __buf_prepare is obviously a bad idea, so we
302 * check if any of the buffers is in the state PREPARING, and if so we
303 * just return -EAGAIN.
304 */
305 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
306 ++buffer) {
307 if (q->bufs[buffer] == NULL)
308 continue;
309 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
310 dprintk(1, "preparing buffers, cannot free\n");
311 return -EAGAIN;
312 }
313 }
314
315 /* Call driver-provided cleanup function for each buffer, if provided */
316 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
317 ++buffer) {
318 struct vb2_buffer *vb = q->bufs[buffer];
319
320 if (vb && vb->planes[0].mem_priv)
321 call_void_vb_qop(vb, buf_cleanup, vb);
322 }
323
324 /* Release video buffer memory */
325 __vb2_free_mem(q, buffers);
326
327 #ifdef CONFIG_VIDEO_ADV_DEBUG
328 /*
329 * Check that all the calls were balances during the life-time of this
330 * queue. If not (or if the debug level is 1 or up), then dump the
331 * counters to the kernel log.
332 */
333 if (q->num_buffers) {
334 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
335 q->cnt_wait_prepare != q->cnt_wait_finish;
336
337 if (unbalanced || vb2_debug) {
338 pr_info("vb2: counters for queue %p:%s\n", q,
339 unbalanced ? " UNBALANCED!" : "");
340 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
341 q->cnt_queue_setup, q->cnt_start_streaming,
342 q->cnt_stop_streaming);
343 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
344 q->cnt_wait_prepare, q->cnt_wait_finish);
345 }
346 q->cnt_queue_setup = 0;
347 q->cnt_wait_prepare = 0;
348 q->cnt_wait_finish = 0;
349 q->cnt_start_streaming = 0;
350 q->cnt_stop_streaming = 0;
351 }
352 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
353 struct vb2_buffer *vb = q->bufs[buffer];
354 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
355 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
356 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
357 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
358 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
359 vb->cnt_buf_queue != vb->cnt_buf_done ||
360 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
361 vb->cnt_buf_init != vb->cnt_buf_cleanup;
362
363 if (unbalanced || vb2_debug) {
364 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
365 q, buffer, unbalanced ? " UNBALANCED!" : "");
366 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
367 vb->cnt_buf_init, vb->cnt_buf_cleanup,
368 vb->cnt_buf_prepare, vb->cnt_buf_finish);
369 pr_info("vb2: buf_queue: %u buf_done: %u\n",
370 vb->cnt_buf_queue, vb->cnt_buf_done);
371 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
372 vb->cnt_mem_alloc, vb->cnt_mem_put,
373 vb->cnt_mem_prepare, vb->cnt_mem_finish,
374 vb->cnt_mem_mmap);
375 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
376 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
377 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
378 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
379 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
380 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
381 vb->cnt_mem_get_dmabuf,
382 vb->cnt_mem_num_users,
383 vb->cnt_mem_vaddr,
384 vb->cnt_mem_cookie);
385 }
386 }
387 #endif
388
389 /* Free videobuf buffers */
390 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
391 ++buffer) {
392 kfree(q->bufs[buffer]);
393 q->bufs[buffer] = NULL;
394 }
395
396 q->num_buffers -= buffers;
397 if (!q->num_buffers) {
398 q->memory = 0;
399 INIT_LIST_HEAD(&q->queued_list);
400 }
401 return 0;
402 }
403
404 /**
405 * vb2_buffer_in_use() - return true if the buffer is in use and
406 * the queue cannot be freed (by the means of REQBUFS(0)) call
407 */
vb2_buffer_in_use(struct vb2_queue * q,struct vb2_buffer * vb)408 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
409 {
410 unsigned int plane;
411 for (plane = 0; plane < vb->num_planes; ++plane) {
412 void *mem_priv = vb->planes[plane].mem_priv;
413 /*
414 * If num_users() has not been provided, call_memop
415 * will return 0, apparently nobody cares about this
416 * case anyway. If num_users() returns more than 1,
417 * we are not the only user of the plane's memory.
418 */
419 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
420 return true;
421 }
422 return false;
423 }
424 EXPORT_SYMBOL(vb2_buffer_in_use);
425
426 /**
427 * __buffers_in_use() - return true if any buffers on the queue are in use and
428 * the queue cannot be freed (by the means of REQBUFS(0)) call
429 */
__buffers_in_use(struct vb2_queue * q)430 static bool __buffers_in_use(struct vb2_queue *q)
431 {
432 unsigned int buffer;
433 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
434 if (vb2_buffer_in_use(q, q->bufs[buffer]))
435 return true;
436 }
437 return false;
438 }
439
440 /**
441 * vb2_core_querybuf() - query video buffer information
442 * @q: videobuf queue
443 * @index: id number of the buffer
444 * @pb: buffer struct passed from userspace
445 *
446 * Should be called from vidioc_querybuf ioctl handler in driver.
447 * The passed buffer should have been verified.
448 * This function fills the relevant information for the userspace.
449 *
450 * The return values from this function are intended to be directly returned
451 * from vidioc_querybuf handler in driver.
452 */
vb2_core_querybuf(struct vb2_queue * q,unsigned int index,void * pb)453 int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
454 {
455 return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
456 }
457 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
458
459 /**
460 * __verify_userptr_ops() - verify that all memory operations required for
461 * USERPTR queue type have been provided
462 */
__verify_userptr_ops(struct vb2_queue * q)463 static int __verify_userptr_ops(struct vb2_queue *q)
464 {
465 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
466 !q->mem_ops->put_userptr)
467 return -EINVAL;
468
469 return 0;
470 }
471
472 /**
473 * __verify_mmap_ops() - verify that all memory operations required for
474 * MMAP queue type have been provided
475 */
__verify_mmap_ops(struct vb2_queue * q)476 static int __verify_mmap_ops(struct vb2_queue *q)
477 {
478 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
479 !q->mem_ops->put || !q->mem_ops->mmap)
480 return -EINVAL;
481
482 return 0;
483 }
484
485 /**
486 * __verify_dmabuf_ops() - verify that all memory operations required for
487 * DMABUF queue type have been provided
488 */
__verify_dmabuf_ops(struct vb2_queue * q)489 static int __verify_dmabuf_ops(struct vb2_queue *q)
490 {
491 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
492 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
493 !q->mem_ops->unmap_dmabuf)
494 return -EINVAL;
495
496 return 0;
497 }
498
499 /**
500 * vb2_verify_memory_type() - Check whether the memory type and buffer type
501 * passed to a buffer operation are compatible with the queue.
502 */
vb2_verify_memory_type(struct vb2_queue * q,enum vb2_memory memory,unsigned int type)503 int vb2_verify_memory_type(struct vb2_queue *q,
504 enum vb2_memory memory, unsigned int type)
505 {
506 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
507 memory != VB2_MEMORY_DMABUF) {
508 dprintk(1, "unsupported memory type\n");
509 return -EINVAL;
510 }
511
512 if (type != q->type) {
513 dprintk(1, "requested type is incorrect\n");
514 return -EINVAL;
515 }
516
517 /*
518 * Make sure all the required memory ops for given memory type
519 * are available.
520 */
521 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
522 dprintk(1, "MMAP for current setup unsupported\n");
523 return -EINVAL;
524 }
525
526 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
527 dprintk(1, "USERPTR for current setup unsupported\n");
528 return -EINVAL;
529 }
530
531 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
532 dprintk(1, "DMABUF for current setup unsupported\n");
533 return -EINVAL;
534 }
535
536 /*
537 * Place the busy tests at the end: -EBUSY can be ignored when
538 * create_bufs is called with count == 0, but count == 0 should still
539 * do the memory and type validation.
540 */
541 if (vb2_fileio_is_active(q)) {
542 dprintk(1, "file io in progress\n");
543 return -EBUSY;
544 }
545 return 0;
546 }
547 EXPORT_SYMBOL(vb2_verify_memory_type);
548
549 /**
550 * vb2_core_reqbufs() - Initiate streaming
551 * @q: videobuf2 queue
552 * @memory: memory type
553 * @count: requested buffer count
554 *
555 * Should be called from vidioc_reqbufs ioctl handler of a driver.
556 * This function:
557 * 1) verifies streaming parameters passed from the userspace,
558 * 2) sets up the queue,
559 * 3) negotiates number of buffers and planes per buffer with the driver
560 * to be used during streaming,
561 * 4) allocates internal buffer structures (struct vb2_buffer), according to
562 * the agreed parameters,
563 * 5) for MMAP memory type, allocates actual video memory, using the
564 * memory handling/allocation routines provided during queue initialization
565 *
566 * If req->count is 0, all the memory will be freed instead.
567 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
568 * and the queue is not busy, memory will be reallocated.
569 *
570 * The return values from this function are intended to be directly returned
571 * from vidioc_reqbufs handler in driver.
572 */
vb2_core_reqbufs(struct vb2_queue * q,enum vb2_memory memory,unsigned int * count)573 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
574 unsigned int *count)
575 {
576 unsigned int num_buffers, allocated_buffers, num_planes = 0;
577 int ret;
578
579 if (q->streaming) {
580 dprintk(1, "streaming active\n");
581 return -EBUSY;
582 }
583
584 if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
585 /*
586 * We already have buffers allocated, so first check if they
587 * are not in use and can be freed.
588 */
589 mutex_lock(&q->mmap_lock);
590 if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
591 mutex_unlock(&q->mmap_lock);
592 dprintk(1, "memory in use, cannot free\n");
593 return -EBUSY;
594 }
595
596 /*
597 * Call queue_cancel to clean up any buffers in the PREPARED or
598 * QUEUED state which is possible if buffers were prepared or
599 * queued without ever calling STREAMON.
600 */
601 __vb2_queue_cancel(q);
602 ret = __vb2_queue_free(q, q->num_buffers);
603 mutex_unlock(&q->mmap_lock);
604 if (ret)
605 return ret;
606
607 /*
608 * In case of REQBUFS(0) return immediately without calling
609 * driver's queue_setup() callback and allocating resources.
610 */
611 if (*count == 0)
612 return 0;
613 }
614
615 /*
616 * Make sure the requested values and current defaults are sane.
617 */
618 num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
619 num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
620 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
621 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
622 q->memory = memory;
623
624 /*
625 * Ask the driver how many buffers and planes per buffer it requires.
626 * Driver also sets the size and allocator context for each plane.
627 */
628 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
629 q->plane_sizes, q->alloc_ctx);
630 if (ret)
631 return ret;
632
633 /* Finally, allocate buffers and video memory */
634 allocated_buffers =
635 __vb2_queue_alloc(q, memory, num_buffers, num_planes);
636 if (allocated_buffers == 0) {
637 dprintk(1, "memory allocation failed\n");
638 return -ENOMEM;
639 }
640
641 /*
642 * There is no point in continuing if we can't allocate the minimum
643 * number of buffers needed by this vb2_queue.
644 */
645 if (allocated_buffers < q->min_buffers_needed)
646 ret = -ENOMEM;
647
648 /*
649 * Check if driver can handle the allocated number of buffers.
650 */
651 if (!ret && allocated_buffers < num_buffers) {
652 num_buffers = allocated_buffers;
653
654 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
655 &num_planes, q->plane_sizes, q->alloc_ctx);
656
657 if (!ret && allocated_buffers < num_buffers)
658 ret = -ENOMEM;
659
660 /*
661 * Either the driver has accepted a smaller number of buffers,
662 * or .queue_setup() returned an error
663 */
664 }
665
666 mutex_lock(&q->mmap_lock);
667 q->num_buffers = allocated_buffers;
668
669 if (ret < 0) {
670 /*
671 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
672 * from q->num_buffers.
673 */
674 __vb2_queue_free(q, allocated_buffers);
675 mutex_unlock(&q->mmap_lock);
676 return ret;
677 }
678 mutex_unlock(&q->mmap_lock);
679
680 /*
681 * Return the number of successfully allocated buffers
682 * to the userspace.
683 */
684 *count = allocated_buffers;
685 q->waiting_for_buffers = !q->is_output;
686
687 return 0;
688 }
689 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
690
691 /**
692 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
693 * @q: videobuf2 queue
694 * @memory: memory type
695 * @count: requested buffer count
696 * @parg: parameter passed to device driver
697 *
698 * Should be called from vidioc_create_bufs ioctl handler of a driver.
699 * This function:
700 * 1) verifies parameter sanity
701 * 2) calls the .queue_setup() queue operation
702 * 3) performs any necessary memory allocations
703 *
704 * The return values from this function are intended to be directly returned
705 * from vidioc_create_bufs handler in driver.
706 */
vb2_core_create_bufs(struct vb2_queue * q,enum vb2_memory memory,unsigned int * count,const void * parg)707 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
708 unsigned int *count, const void *parg)
709 {
710 unsigned int num_planes = 0, num_buffers, allocated_buffers;
711 int ret;
712
713 if (q->num_buffers == VB2_MAX_FRAME) {
714 dprintk(1, "maximum number of buffers already allocated\n");
715 return -ENOBUFS;
716 }
717
718 if (!q->num_buffers) {
719 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
720 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
721 q->memory = memory;
722 q->waiting_for_buffers = !q->is_output;
723 }
724
725 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
726
727 /*
728 * Ask the driver, whether the requested number of buffers, planes per
729 * buffer and their sizes are acceptable
730 */
731 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
732 &num_planes, q->plane_sizes, q->alloc_ctx);
733 if (ret)
734 return ret;
735
736 /* Finally, allocate buffers and video memory */
737 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
738 num_planes);
739 if (allocated_buffers == 0) {
740 dprintk(1, "memory allocation failed\n");
741 return -ENOMEM;
742 }
743
744 /*
745 * Check if driver can handle the so far allocated number of buffers.
746 */
747 if (allocated_buffers < num_buffers) {
748 num_buffers = allocated_buffers;
749
750 /*
751 * q->num_buffers contains the total number of buffers, that the
752 * queue driver has set up
753 */
754 ret = call_qop(q, queue_setup, q, parg, &num_buffers,
755 &num_planes, q->plane_sizes, q->alloc_ctx);
756
757 if (!ret && allocated_buffers < num_buffers)
758 ret = -ENOMEM;
759
760 /*
761 * Either the driver has accepted a smaller number of buffers,
762 * or .queue_setup() returned an error
763 */
764 }
765
766 mutex_lock(&q->mmap_lock);
767 q->num_buffers += allocated_buffers;
768
769 if (ret < 0) {
770 /*
771 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
772 * from q->num_buffers.
773 */
774 __vb2_queue_free(q, allocated_buffers);
775 mutex_unlock(&q->mmap_lock);
776 return -ENOMEM;
777 }
778 mutex_unlock(&q->mmap_lock);
779
780 /*
781 * Return the number of successfully allocated buffers
782 * to the userspace.
783 */
784 *count = allocated_buffers;
785
786 return 0;
787 }
788 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
789
790 /**
791 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
792 * @vb: vb2_buffer to which the plane in question belongs to
793 * @plane_no: plane number for which the address is to be returned
794 *
795 * This function returns a kernel virtual address of a given plane if
796 * such a mapping exist, NULL otherwise.
797 */
vb2_plane_vaddr(struct vb2_buffer * vb,unsigned int plane_no)798 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
799 {
800 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
801 return NULL;
802
803 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
804
805 }
806 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
807
808 /**
809 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
810 * @vb: vb2_buffer to which the plane in question belongs to
811 * @plane_no: plane number for which the cookie is to be returned
812 *
813 * This function returns an allocator specific cookie for a given plane if
814 * available, NULL otherwise. The allocator should provide some simple static
815 * inline function, which would convert this cookie to the allocator specific
816 * type that can be used directly by the driver to access the buffer. This can
817 * be for example physical address, pointer to scatter list or IOMMU mapping.
818 */
vb2_plane_cookie(struct vb2_buffer * vb,unsigned int plane_no)819 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
820 {
821 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
822 return NULL;
823
824 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
825 }
826 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
827
828 /**
829 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
830 * @vb: vb2_buffer returned from the driver
831 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
832 * VB2_BUF_STATE_ERROR if the operation finished with an error or
833 * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
834 * If start_streaming fails then it should return buffers with state
835 * VB2_BUF_STATE_QUEUED to put them back into the queue.
836 *
837 * This function should be called by the driver after a hardware operation on
838 * a buffer is finished and the buffer may be returned to userspace. The driver
839 * cannot use this buffer anymore until it is queued back to it by videobuf
840 * by the means of buf_queue callback. Only buffers previously queued to the
841 * driver by buf_queue can be passed to this function.
842 *
843 * While streaming a buffer can only be returned in state DONE or ERROR.
844 * The start_streaming op can also return them in case the DMA engine cannot
845 * be started for some reason. In that case the buffers should be returned with
846 * state QUEUED.
847 */
vb2_buffer_done(struct vb2_buffer * vb,enum vb2_buffer_state state)848 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
849 {
850 struct vb2_queue *q = vb->vb2_queue;
851 unsigned long flags;
852 unsigned int plane;
853
854 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
855 return;
856
857 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
858 state != VB2_BUF_STATE_ERROR &&
859 state != VB2_BUF_STATE_QUEUED &&
860 state != VB2_BUF_STATE_REQUEUEING))
861 state = VB2_BUF_STATE_ERROR;
862
863 #ifdef CONFIG_VIDEO_ADV_DEBUG
864 /*
865 * Although this is not a callback, it still does have to balance
866 * with the buf_queue op. So update this counter manually.
867 */
868 vb->cnt_buf_done++;
869 #endif
870 dprintk(4, "done processing on buffer %d, state: %d\n",
871 vb->index, state);
872
873 /* sync buffers */
874 for (plane = 0; plane < vb->num_planes; ++plane)
875 call_void_memop(vb, finish, vb->planes[plane].mem_priv);
876
877 spin_lock_irqsave(&q->done_lock, flags);
878 if (state == VB2_BUF_STATE_QUEUED ||
879 state == VB2_BUF_STATE_REQUEUEING) {
880 vb->state = VB2_BUF_STATE_QUEUED;
881 } else {
882 /* Add the buffer to the done buffers list */
883 list_add_tail(&vb->done_entry, &q->done_list);
884 vb->state = state;
885 }
886 atomic_dec(&q->owned_by_drv_count);
887 spin_unlock_irqrestore(&q->done_lock, flags);
888
889 trace_vb2_buf_done(q, vb);
890
891 switch (state) {
892 case VB2_BUF_STATE_QUEUED:
893 return;
894 case VB2_BUF_STATE_REQUEUEING:
895 if (q->start_streaming_called)
896 __enqueue_in_driver(vb);
897 return;
898 default:
899 /* Inform any processes that may be waiting for buffers */
900 wake_up(&q->done_wq);
901 break;
902 }
903 }
904 EXPORT_SYMBOL_GPL(vb2_buffer_done);
905
906 /**
907 * vb2_discard_done() - discard all buffers marked as DONE
908 * @q: videobuf2 queue
909 *
910 * This function is intended to be used with suspend/resume operations. It
911 * discards all 'done' buffers as they would be too old to be requested after
912 * resume.
913 *
914 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
915 * delayed works before calling this function to make sure no buffer will be
916 * touched by the driver and/or hardware.
917 */
vb2_discard_done(struct vb2_queue * q)918 void vb2_discard_done(struct vb2_queue *q)
919 {
920 struct vb2_buffer *vb;
921 unsigned long flags;
922
923 spin_lock_irqsave(&q->done_lock, flags);
924 list_for_each_entry(vb, &q->done_list, done_entry)
925 vb->state = VB2_BUF_STATE_ERROR;
926 spin_unlock_irqrestore(&q->done_lock, flags);
927 }
928 EXPORT_SYMBOL_GPL(vb2_discard_done);
929
930 /**
931 * __qbuf_mmap() - handle qbuf of an MMAP buffer
932 */
__qbuf_mmap(struct vb2_buffer * vb,const void * pb)933 static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
934 {
935 int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
936 vb, pb, vb->planes);
937 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
938 }
939
940 /**
941 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
942 */
__qbuf_userptr(struct vb2_buffer * vb,const void * pb)943 static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
944 {
945 struct vb2_plane planes[VB2_MAX_PLANES];
946 struct vb2_queue *q = vb->vb2_queue;
947 void *mem_priv;
948 unsigned int plane;
949 int ret;
950 enum dma_data_direction dma_dir =
951 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
952 bool reacquired = vb->planes[0].mem_priv == NULL;
953
954 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
955 /* Copy relevant information provided by the userspace */
956 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
957 if (ret)
958 return ret;
959
960 for (plane = 0; plane < vb->num_planes; ++plane) {
961 /* Skip the plane if already verified */
962 if (vb->planes[plane].m.userptr &&
963 vb->planes[plane].m.userptr == planes[plane].m.userptr
964 && vb->planes[plane].length == planes[plane].length)
965 continue;
966
967 dprintk(3, "userspace address for plane %d changed, "
968 "reacquiring memory\n", plane);
969
970 /* Check if the provided plane buffer is large enough */
971 if (planes[plane].length < q->plane_sizes[plane]) {
972 dprintk(1, "provided buffer size %u is less than "
973 "setup size %u for plane %d\n",
974 planes[plane].length,
975 q->plane_sizes[plane], plane);
976 ret = -EINVAL;
977 goto err;
978 }
979
980 /* Release previously acquired memory if present */
981 if (vb->planes[plane].mem_priv) {
982 if (!reacquired) {
983 reacquired = true;
984 call_void_vb_qop(vb, buf_cleanup, vb);
985 }
986 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
987 }
988
989 vb->planes[plane].mem_priv = NULL;
990 vb->planes[plane].bytesused = 0;
991 vb->planes[plane].length = 0;
992 vb->planes[plane].m.userptr = 0;
993 vb->planes[plane].data_offset = 0;
994
995 /* Acquire each plane's memory */
996 mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
997 planes[plane].m.userptr,
998 planes[plane].length, dma_dir);
999 if (IS_ERR_OR_NULL(mem_priv)) {
1000 dprintk(1, "failed acquiring userspace "
1001 "memory for plane %d\n", plane);
1002 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1003 goto err;
1004 }
1005 vb->planes[plane].mem_priv = mem_priv;
1006 }
1007
1008 /*
1009 * Now that everything is in order, copy relevant information
1010 * provided by userspace.
1011 */
1012 for (plane = 0; plane < vb->num_planes; ++plane) {
1013 vb->planes[plane].bytesused = planes[plane].bytesused;
1014 vb->planes[plane].length = planes[plane].length;
1015 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1016 vb->planes[plane].data_offset = planes[plane].data_offset;
1017 }
1018
1019 if (reacquired) {
1020 /*
1021 * One or more planes changed, so we must call buf_init to do
1022 * the driver-specific initialization on the newly acquired
1023 * buffer, if provided.
1024 */
1025 ret = call_vb_qop(vb, buf_init, vb);
1026 if (ret) {
1027 dprintk(1, "buffer initialization failed\n");
1028 goto err;
1029 }
1030 }
1031
1032 ret = call_vb_qop(vb, buf_prepare, vb);
1033 if (ret) {
1034 dprintk(1, "buffer preparation failed\n");
1035 call_void_vb_qop(vb, buf_cleanup, vb);
1036 goto err;
1037 }
1038
1039 return 0;
1040 err:
1041 /* In case of errors, release planes that were already acquired */
1042 for (plane = 0; plane < vb->num_planes; ++plane) {
1043 if (vb->planes[plane].mem_priv)
1044 call_void_memop(vb, put_userptr,
1045 vb->planes[plane].mem_priv);
1046 vb->planes[plane].mem_priv = NULL;
1047 vb->planes[plane].m.userptr = 0;
1048 vb->planes[plane].length = 0;
1049 }
1050
1051 return ret;
1052 }
1053
1054 /**
1055 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1056 */
__qbuf_dmabuf(struct vb2_buffer * vb,const void * pb)1057 static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
1058 {
1059 struct vb2_plane planes[VB2_MAX_PLANES];
1060 struct vb2_queue *q = vb->vb2_queue;
1061 void *mem_priv;
1062 unsigned int plane;
1063 int ret;
1064 enum dma_data_direction dma_dir =
1065 q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1066 bool reacquired = vb->planes[0].mem_priv == NULL;
1067
1068 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1069 /* Copy relevant information provided by the userspace */
1070 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
1071 if (ret)
1072 return ret;
1073
1074 for (plane = 0; plane < vb->num_planes; ++plane) {
1075 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1076
1077 if (IS_ERR_OR_NULL(dbuf)) {
1078 dprintk(1, "invalid dmabuf fd for plane %d\n",
1079 plane);
1080 ret = -EINVAL;
1081 goto err;
1082 }
1083
1084 /* use DMABUF size if length is not provided */
1085 if (planes[plane].length == 0)
1086 planes[plane].length = dbuf->size;
1087
1088 if (planes[plane].length < q->plane_sizes[plane]) {
1089 dprintk(1, "invalid dmabuf length for plane %d\n",
1090 plane);
1091 ret = -EINVAL;
1092 goto err;
1093 }
1094
1095 /* Skip the plane if already verified */
1096 if (dbuf == vb->planes[plane].dbuf &&
1097 vb->planes[plane].length == planes[plane].length) {
1098 dma_buf_put(dbuf);
1099 continue;
1100 }
1101
1102 dprintk(1, "buffer for plane %d changed\n", plane);
1103
1104 if (!reacquired) {
1105 reacquired = true;
1106 call_void_vb_qop(vb, buf_cleanup, vb);
1107 }
1108
1109 /* Release previously acquired memory if present */
1110 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1111 vb->planes[plane].bytesused = 0;
1112 vb->planes[plane].length = 0;
1113 vb->planes[plane].m.fd = 0;
1114 vb->planes[plane].data_offset = 0;
1115
1116 /* Acquire each plane's memory */
1117 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1118 q->alloc_ctx[plane], dbuf, planes[plane].length,
1119 dma_dir);
1120 if (IS_ERR(mem_priv)) {
1121 dprintk(1, "failed to attach dmabuf\n");
1122 ret = PTR_ERR(mem_priv);
1123 dma_buf_put(dbuf);
1124 goto err;
1125 }
1126
1127 vb->planes[plane].dbuf = dbuf;
1128 vb->planes[plane].mem_priv = mem_priv;
1129 }
1130
1131 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1132 * really we want to do this just before the DMA, not while queueing
1133 * the buffer(s)..
1134 */
1135 for (plane = 0; plane < vb->num_planes; ++plane) {
1136 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1137 if (ret) {
1138 dprintk(1, "failed to map dmabuf for plane %d\n",
1139 plane);
1140 goto err;
1141 }
1142 vb->planes[plane].dbuf_mapped = 1;
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.fd = planes[plane].m.fd;
1153 vb->planes[plane].data_offset = planes[plane].data_offset;
1154 }
1155
1156 if (reacquired) {
1157 /*
1158 * Call driver-specific initialization on the newly acquired buffer,
1159 * if provided.
1160 */
1161 ret = call_vb_qop(vb, buf_init, vb);
1162 if (ret) {
1163 dprintk(1, "buffer initialization failed\n");
1164 goto err;
1165 }
1166 }
1167
1168 ret = call_vb_qop(vb, buf_prepare, vb);
1169 if (ret) {
1170 dprintk(1, "buffer preparation failed\n");
1171 call_void_vb_qop(vb, buf_cleanup, vb);
1172 goto err;
1173 }
1174
1175 return 0;
1176 err:
1177 /* In case of errors, release planes that were already acquired */
1178 __vb2_buf_dmabuf_put(vb);
1179
1180 return ret;
1181 }
1182
1183 /**
1184 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1185 */
__enqueue_in_driver(struct vb2_buffer * vb)1186 static void __enqueue_in_driver(struct vb2_buffer *vb)
1187 {
1188 struct vb2_queue *q = vb->vb2_queue;
1189 unsigned int plane;
1190
1191 vb->state = VB2_BUF_STATE_ACTIVE;
1192 atomic_inc(&q->owned_by_drv_count);
1193
1194 trace_vb2_buf_queue(q, vb);
1195
1196 /* sync buffers */
1197 for (plane = 0; plane < vb->num_planes; ++plane)
1198 call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
1199
1200 call_void_vb_qop(vb, buf_queue, vb);
1201 }
1202
__buf_prepare(struct vb2_buffer * vb,const void * pb)1203 static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1204 {
1205 struct vb2_queue *q = vb->vb2_queue;
1206 int ret;
1207
1208 if (q->error) {
1209 dprintk(1, "fatal error occurred on queue\n");
1210 return -EIO;
1211 }
1212
1213 vb->state = VB2_BUF_STATE_PREPARING;
1214
1215 switch (q->memory) {
1216 case VB2_MEMORY_MMAP:
1217 ret = __qbuf_mmap(vb, pb);
1218 break;
1219 case VB2_MEMORY_USERPTR:
1220 ret = __qbuf_userptr(vb, pb);
1221 break;
1222 case VB2_MEMORY_DMABUF:
1223 ret = __qbuf_dmabuf(vb, pb);
1224 break;
1225 default:
1226 WARN(1, "Invalid queue type\n");
1227 ret = -EINVAL;
1228 }
1229
1230 if (ret)
1231 dprintk(1, "buffer preparation failed: %d\n", ret);
1232 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
1233
1234 return ret;
1235 }
1236
1237 /**
1238 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
1239 * to the kernel
1240 * @q: videobuf2 queue
1241 * @index: id number of the buffer
1242 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
1243 * handler in driver
1244 *
1245 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1246 * The passed buffer should have been verified.
1247 * This function calls buf_prepare callback in the driver (if provided),
1248 * in which driver-specific buffer initialization can be performed,
1249 *
1250 * The return values from this function are intended to be directly returned
1251 * from vidioc_prepare_buf handler in driver.
1252 */
vb2_core_prepare_buf(struct vb2_queue * q,unsigned int index,void * pb)1253 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1254 {
1255 struct vb2_buffer *vb;
1256 int ret;
1257
1258 vb = q->bufs[index];
1259 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1260 dprintk(1, "invalid buffer state %d\n",
1261 vb->state);
1262 return -EINVAL;
1263 }
1264
1265 ret = __buf_prepare(vb, pb);
1266 if (ret)
1267 return ret;
1268
1269 /* Fill buffer information for the userspace */
1270 ret = call_bufop(q, fill_user_buffer, vb, pb);
1271 if (ret)
1272 return ret;
1273
1274 dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
1275
1276 return ret;
1277 }
1278 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1279
1280 /**
1281 * vb2_start_streaming() - Attempt to start streaming.
1282 * @q: videobuf2 queue
1283 *
1284 * Attempt to start streaming. When this function is called there must be
1285 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1286 * number of buffers required for the DMA engine to function). If the
1287 * @start_streaming op fails it is supposed to return all the driver-owned
1288 * buffers back to vb2 in state QUEUED. Check if that happened and if
1289 * not warn and reclaim them forcefully.
1290 */
vb2_start_streaming(struct vb2_queue * q)1291 static int vb2_start_streaming(struct vb2_queue *q)
1292 {
1293 struct vb2_buffer *vb;
1294 int ret;
1295
1296 /*
1297 * If any buffers were queued before streamon,
1298 * we can now pass them to driver for processing.
1299 */
1300 list_for_each_entry(vb, &q->queued_list, queued_entry)
1301 __enqueue_in_driver(vb);
1302
1303 /* Tell the driver to start streaming */
1304 q->start_streaming_called = 1;
1305 ret = call_qop(q, start_streaming, q,
1306 atomic_read(&q->owned_by_drv_count));
1307 if (!ret)
1308 return 0;
1309
1310 q->start_streaming_called = 0;
1311
1312 dprintk(1, "driver refused to start streaming\n");
1313 /*
1314 * If you see this warning, then the driver isn't cleaning up properly
1315 * after a failed start_streaming(). See the start_streaming()
1316 * documentation in videobuf2-core.h for more information how buffers
1317 * should be returned to vb2 in start_streaming().
1318 */
1319 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1320 unsigned i;
1321
1322 /*
1323 * Forcefully reclaim buffers if the driver did not
1324 * correctly return them to vb2.
1325 */
1326 for (i = 0; i < q->num_buffers; ++i) {
1327 vb = q->bufs[i];
1328 if (vb->state == VB2_BUF_STATE_ACTIVE)
1329 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1330 }
1331 /* Must be zero now */
1332 WARN_ON(atomic_read(&q->owned_by_drv_count));
1333 }
1334 /*
1335 * If done_list is not empty, then start_streaming() didn't call
1336 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
1337 * STATE_DONE.
1338 */
1339 WARN_ON(!list_empty(&q->done_list));
1340 return ret;
1341 }
1342
1343 /**
1344 * vb2_core_qbuf() - Queue a buffer from userspace
1345 * @q: videobuf2 queue
1346 * @index: id number of the buffer
1347 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
1348 * in driver
1349 *
1350 * Should be called from vidioc_qbuf ioctl handler of a driver.
1351 * The passed buffer should have been verified.
1352 * This function:
1353 * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
1354 * which driver-specific buffer initialization can be performed,
1355 * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
1356 * callback for processing.
1357 *
1358 * The return values from this function are intended to be directly returned
1359 * from vidioc_qbuf handler in driver.
1360 */
vb2_core_qbuf(struct vb2_queue * q,unsigned int index,void * pb)1361 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1362 {
1363 struct vb2_buffer *vb;
1364 enum vb2_buffer_state orig_state;
1365 int ret;
1366
1367 if (q->error) {
1368 dprintk(1, "fatal error occurred on queue\n");
1369 return -EIO;
1370 }
1371
1372 vb = q->bufs[index];
1373
1374 switch (vb->state) {
1375 case VB2_BUF_STATE_DEQUEUED:
1376 ret = __buf_prepare(vb, pb);
1377 if (ret)
1378 return ret;
1379 break;
1380 case VB2_BUF_STATE_PREPARED:
1381 break;
1382 case VB2_BUF_STATE_PREPARING:
1383 dprintk(1, "buffer still being prepared\n");
1384 return -EINVAL;
1385 default:
1386 dprintk(1, "invalid buffer state %d\n", vb->state);
1387 return -EINVAL;
1388 }
1389
1390 /*
1391 * Add to the queued buffers list, a buffer will stay on it until
1392 * dequeued in dqbuf.
1393 */
1394 orig_state = vb->state;
1395 list_add_tail(&vb->queued_entry, &q->queued_list);
1396 q->queued_count++;
1397 q->waiting_for_buffers = false;
1398 vb->state = VB2_BUF_STATE_QUEUED;
1399
1400 call_bufop(q, set_timestamp, vb, pb);
1401
1402 trace_vb2_qbuf(q, vb);
1403
1404 /*
1405 * If already streaming, give the buffer to driver for processing.
1406 * If not, the buffer will be given to driver on next streamon.
1407 */
1408 if (q->start_streaming_called)
1409 __enqueue_in_driver(vb);
1410
1411 /* Fill buffer information for the userspace */
1412 ret = call_bufop(q, fill_user_buffer, vb, pb);
1413 if (ret)
1414 return ret;
1415
1416 /*
1417 * If streamon has been called, and we haven't yet called
1418 * start_streaming() since not enough buffers were queued, and
1419 * we now have reached the minimum number of queued buffers,
1420 * then we can finally call start_streaming().
1421 */
1422 if (q->streaming && !q->start_streaming_called &&
1423 q->queued_count >= q->min_buffers_needed) {
1424 ret = vb2_start_streaming(q);
1425 if (ret) {
1426 /*
1427 * Since vb2_core_qbuf will return with an error,
1428 * we should return it to state DEQUEUED since
1429 * the error indicates that the buffer wasn't queued.
1430 */
1431 list_del(&vb->queued_entry);
1432 q->queued_count--;
1433 vb->state = orig_state;
1434 return ret;
1435 }
1436 }
1437
1438 dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
1439 return 0;
1440 }
1441 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1442
1443 /**
1444 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1445 * for dequeuing
1446 *
1447 * Will sleep if required for nonblocking == false.
1448 */
__vb2_wait_for_done_vb(struct vb2_queue * q,int nonblocking)1449 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1450 {
1451 /*
1452 * All operations on vb_done_list are performed under done_lock
1453 * spinlock protection. However, buffers may be removed from
1454 * it and returned to userspace only while holding both driver's
1455 * lock and the done_lock spinlock. Thus we can be sure that as
1456 * long as we hold the driver's lock, the list will remain not
1457 * empty if list_empty() check succeeds.
1458 */
1459
1460 for (;;) {
1461 int ret;
1462
1463 if (!q->streaming) {
1464 dprintk(1, "streaming off, will not wait for buffers\n");
1465 return -EINVAL;
1466 }
1467
1468 if (q->error) {
1469 dprintk(1, "Queue in error state, will not wait for buffers\n");
1470 return -EIO;
1471 }
1472
1473 if (q->last_buffer_dequeued) {
1474 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1475 return -EPIPE;
1476 }
1477
1478 if (!list_empty(&q->done_list)) {
1479 /*
1480 * Found a buffer that we were waiting for.
1481 */
1482 break;
1483 }
1484
1485 if (nonblocking) {
1486 dprintk(1, "nonblocking and no buffers to dequeue, "
1487 "will not wait\n");
1488 return -EAGAIN;
1489 }
1490
1491 /*
1492 * We are streaming and blocking, wait for another buffer to
1493 * become ready or for streamoff. Driver's lock is released to
1494 * allow streamoff or qbuf to be called while waiting.
1495 */
1496 call_void_qop(q, wait_prepare, q);
1497
1498 /*
1499 * All locks have been released, it is safe to sleep now.
1500 */
1501 dprintk(3, "will sleep waiting for buffers\n");
1502 ret = wait_event_interruptible(q->done_wq,
1503 !list_empty(&q->done_list) || !q->streaming ||
1504 q->error);
1505
1506 /*
1507 * We need to reevaluate both conditions again after reacquiring
1508 * the locks or return an error if one occurred.
1509 */
1510 call_void_qop(q, wait_finish, q);
1511 if (ret) {
1512 dprintk(1, "sleep was interrupted\n");
1513 return ret;
1514 }
1515 }
1516 return 0;
1517 }
1518
1519 /**
1520 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1521 *
1522 * Will sleep if required for nonblocking == false.
1523 */
__vb2_get_done_vb(struct vb2_queue * q,struct vb2_buffer ** vb,void * pb,int nonblocking)1524 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1525 void *pb, int nonblocking)
1526 {
1527 unsigned long flags;
1528 int ret = 0;
1529
1530 /*
1531 * Wait for at least one buffer to become available on the done_list.
1532 */
1533 ret = __vb2_wait_for_done_vb(q, nonblocking);
1534 if (ret)
1535 return ret;
1536
1537 /*
1538 * Driver's lock has been held since we last verified that done_list
1539 * is not empty, so no need for another list_empty(done_list) check.
1540 */
1541 spin_lock_irqsave(&q->done_lock, flags);
1542 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1543 /*
1544 * Only remove the buffer from done_list if all planes can be
1545 * handled. Some cases such as V4L2 file I/O and DVB have pb
1546 * == NULL; skip the check then as there's nothing to verify.
1547 */
1548 if (pb)
1549 ret = call_bufop(q, verify_planes_array, *vb, pb);
1550 if (!ret)
1551 list_del(&(*vb)->done_entry);
1552 spin_unlock_irqrestore(&q->done_lock, flags);
1553
1554 return ret;
1555 }
1556
1557 /**
1558 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1559 * @q: videobuf2 queue
1560 *
1561 * This function will wait until all buffers that have been given to the driver
1562 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1563 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1564 * taken, for example from stop_streaming() callback.
1565 */
vb2_wait_for_all_buffers(struct vb2_queue * q)1566 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1567 {
1568 if (!q->streaming) {
1569 dprintk(1, "streaming off, will not wait for buffers\n");
1570 return -EINVAL;
1571 }
1572
1573 if (q->start_streaming_called)
1574 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1575 return 0;
1576 }
1577 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1578
1579 /**
1580 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1581 */
__vb2_dqbuf(struct vb2_buffer * vb)1582 static void __vb2_dqbuf(struct vb2_buffer *vb)
1583 {
1584 struct vb2_queue *q = vb->vb2_queue;
1585 unsigned int i;
1586
1587 /* nothing to do if the buffer is already dequeued */
1588 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1589 return;
1590
1591 vb->state = VB2_BUF_STATE_DEQUEUED;
1592
1593 /* unmap DMABUF buffer */
1594 if (q->memory == VB2_MEMORY_DMABUF)
1595 for (i = 0; i < vb->num_planes; ++i) {
1596 if (!vb->planes[i].dbuf_mapped)
1597 continue;
1598 call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1599 vb->planes[i].dbuf_mapped = 0;
1600 }
1601 }
1602
1603 /**
1604 * vb2_dqbuf() - Dequeue a buffer to the userspace
1605 * @q: videobuf2 queue
1606 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
1607 * in driver
1608 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1609 * buffers ready for dequeuing are present. Normally the driver
1610 * would be passing (file->f_flags & O_NONBLOCK) here
1611 *
1612 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1613 * The passed buffer should have been verified.
1614 * This function:
1615 * 1) calls buf_finish callback in the driver (if provided), in which
1616 * driver can perform any additional operations that may be required before
1617 * returning the buffer to userspace, such as cache sync,
1618 * 2) the buffer struct members are filled with relevant information for
1619 * the userspace.
1620 *
1621 * The return values from this function are intended to be directly returned
1622 * from vidioc_dqbuf handler in driver.
1623 */
vb2_core_dqbuf(struct vb2_queue * q,void * pb,bool nonblocking)1624 int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
1625 {
1626 struct vb2_buffer *vb = NULL;
1627 int ret;
1628
1629 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1630 if (ret < 0)
1631 return ret;
1632
1633 switch (vb->state) {
1634 case VB2_BUF_STATE_DONE:
1635 dprintk(3, "returning done buffer\n");
1636 break;
1637 case VB2_BUF_STATE_ERROR:
1638 dprintk(3, "returning done buffer with errors\n");
1639 break;
1640 default:
1641 dprintk(1, "invalid buffer state\n");
1642 return -EINVAL;
1643 }
1644
1645 call_void_vb_qop(vb, buf_finish, vb);
1646
1647 /* Fill buffer information for the userspace */
1648 ret = call_bufop(q, fill_user_buffer, vb, pb);
1649 if (ret)
1650 return ret;
1651
1652 /* Remove from videobuf queue */
1653 list_del(&vb->queued_entry);
1654 q->queued_count--;
1655
1656 trace_vb2_dqbuf(q, vb);
1657
1658 /* go back to dequeued state */
1659 __vb2_dqbuf(vb);
1660
1661 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1662 vb->index, vb->state);
1663
1664 return 0;
1665
1666 }
1667 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1668
1669 /**
1670 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1671 *
1672 * Removes all queued buffers from driver's queue and all buffers queued by
1673 * userspace from videobuf's queue. Returns to state after reqbufs.
1674 */
__vb2_queue_cancel(struct vb2_queue * q)1675 static void __vb2_queue_cancel(struct vb2_queue *q)
1676 {
1677 unsigned int i;
1678
1679 /*
1680 * Tell driver to stop all transactions and release all queued
1681 * buffers.
1682 */
1683 if (q->start_streaming_called)
1684 call_void_qop(q, stop_streaming, q);
1685
1686 /*
1687 * If you see this warning, then the driver isn't cleaning up properly
1688 * in stop_streaming(). See the stop_streaming() documentation in
1689 * videobuf2-core.h for more information how buffers should be returned
1690 * to vb2 in stop_streaming().
1691 */
1692 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1693 for (i = 0; i < q->num_buffers; ++i)
1694 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
1695 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1696 /* Must be zero now */
1697 WARN_ON(atomic_read(&q->owned_by_drv_count));
1698 }
1699
1700 q->streaming = 0;
1701 q->start_streaming_called = 0;
1702 q->queued_count = 0;
1703 q->error = 0;
1704
1705 /*
1706 * Remove all buffers from videobuf's list...
1707 */
1708 INIT_LIST_HEAD(&q->queued_list);
1709 /*
1710 * ...and done list; userspace will not receive any buffers it
1711 * has not already dequeued before initiating cancel.
1712 */
1713 INIT_LIST_HEAD(&q->done_list);
1714 atomic_set(&q->owned_by_drv_count, 0);
1715 wake_up_all(&q->done_wq);
1716
1717 /*
1718 * Reinitialize all buffers for next use.
1719 * Make sure to call buf_finish for any queued buffers. Normally
1720 * that's done in dqbuf, but that's not going to happen when we
1721 * cancel the whole queue. Note: this code belongs here, not in
1722 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1723 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1724 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1725 */
1726 for (i = 0; i < q->num_buffers; ++i) {
1727 struct vb2_buffer *vb = q->bufs[i];
1728
1729 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1730 vb->state = VB2_BUF_STATE_PREPARED;
1731 call_void_vb_qop(vb, buf_finish, vb);
1732 }
1733 __vb2_dqbuf(vb);
1734 }
1735 }
1736
vb2_core_streamon(struct vb2_queue * q,unsigned int type)1737 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1738 {
1739 int ret;
1740
1741 if (type != q->type) {
1742 dprintk(1, "invalid stream type\n");
1743 return -EINVAL;
1744 }
1745
1746 if (q->streaming) {
1747 dprintk(3, "already streaming\n");
1748 return 0;
1749 }
1750
1751 if (!q->num_buffers) {
1752 dprintk(1, "no buffers have been allocated\n");
1753 return -EINVAL;
1754 }
1755
1756 if (q->num_buffers < q->min_buffers_needed) {
1757 dprintk(1, "need at least %u allocated buffers\n",
1758 q->min_buffers_needed);
1759 return -EINVAL;
1760 }
1761
1762 /*
1763 * Tell driver to start streaming provided sufficient buffers
1764 * are available.
1765 */
1766 if (q->queued_count >= q->min_buffers_needed) {
1767 ret = vb2_start_streaming(q);
1768 if (ret) {
1769 __vb2_queue_cancel(q);
1770 return ret;
1771 }
1772 }
1773
1774 q->streaming = 1;
1775
1776 dprintk(3, "successful\n");
1777 return 0;
1778 }
1779 EXPORT_SYMBOL_GPL(vb2_core_streamon);
1780
1781 /**
1782 * vb2_queue_error() - signal a fatal error on the queue
1783 * @q: videobuf2 queue
1784 *
1785 * Flag that a fatal unrecoverable error has occurred and wake up all processes
1786 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
1787 * buffers will return -EIO.
1788 *
1789 * The error flag will be cleared when cancelling the queue, either from
1790 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
1791 * function before starting the stream, otherwise the error flag will remain set
1792 * until the queue is released when closing the device node.
1793 */
vb2_queue_error(struct vb2_queue * q)1794 void vb2_queue_error(struct vb2_queue *q)
1795 {
1796 q->error = 1;
1797
1798 wake_up_all(&q->done_wq);
1799 }
1800 EXPORT_SYMBOL_GPL(vb2_queue_error);
1801
vb2_core_streamoff(struct vb2_queue * q,unsigned int type)1802 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
1803 {
1804 if (type != q->type) {
1805 dprintk(1, "invalid stream type\n");
1806 return -EINVAL;
1807 }
1808
1809 /*
1810 * Cancel will pause streaming and remove all buffers from the driver
1811 * and videobuf, effectively returning control over them to userspace.
1812 *
1813 * Note that we do this even if q->streaming == 0: if you prepare or
1814 * queue buffers, and then call streamoff without ever having called
1815 * streamon, you would still expect those buffers to be returned to
1816 * their normal dequeued state.
1817 */
1818 __vb2_queue_cancel(q);
1819 q->waiting_for_buffers = !q->is_output;
1820 q->last_buffer_dequeued = false;
1821
1822 dprintk(3, "successful\n");
1823 return 0;
1824 }
1825 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
1826
1827 /**
1828 * __find_plane_by_offset() - find plane associated with the given offset off
1829 */
__find_plane_by_offset(struct vb2_queue * q,unsigned long off,unsigned int * _buffer,unsigned int * _plane)1830 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1831 unsigned int *_buffer, unsigned int *_plane)
1832 {
1833 struct vb2_buffer *vb;
1834 unsigned int buffer, plane;
1835
1836 /*
1837 * Go over all buffers and their planes, comparing the given offset
1838 * with an offset assigned to each plane. If a match is found,
1839 * return its buffer and plane numbers.
1840 */
1841 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1842 vb = q->bufs[buffer];
1843
1844 for (plane = 0; plane < vb->num_planes; ++plane) {
1845 if (vb->planes[plane].m.offset == off) {
1846 *_buffer = buffer;
1847 *_plane = plane;
1848 return 0;
1849 }
1850 }
1851 }
1852
1853 return -EINVAL;
1854 }
1855
1856 /**
1857 * vb2_core_expbuf() - Export a buffer as a file descriptor
1858 * @q: videobuf2 queue
1859 * @fd: file descriptor associated with DMABUF (set by driver) *
1860 * @type: buffer type
1861 * @index: id number of the buffer
1862 * @plane: index of the plane to be exported, 0 for single plane queues
1863 * @flags: flags for newly created file, currently only O_CLOEXEC is
1864 * supported, refer to manual of open syscall for more details
1865 *
1866 * The return values from this function are intended to be directly returned
1867 * from vidioc_expbuf handler in driver.
1868 */
vb2_core_expbuf(struct vb2_queue * q,int * fd,unsigned int type,unsigned int index,unsigned int plane,unsigned int flags)1869 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
1870 unsigned int index, unsigned int plane, unsigned int flags)
1871 {
1872 struct vb2_buffer *vb = NULL;
1873 struct vb2_plane *vb_plane;
1874 int ret;
1875 struct dma_buf *dbuf;
1876
1877 if (q->memory != VB2_MEMORY_MMAP) {
1878 dprintk(1, "queue is not currently set up for mmap\n");
1879 return -EINVAL;
1880 }
1881
1882 if (!q->mem_ops->get_dmabuf) {
1883 dprintk(1, "queue does not support DMA buffer exporting\n");
1884 return -EINVAL;
1885 }
1886
1887 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
1888 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
1889 return -EINVAL;
1890 }
1891
1892 if (type != q->type) {
1893 dprintk(1, "invalid buffer type\n");
1894 return -EINVAL;
1895 }
1896
1897 if (index >= q->num_buffers) {
1898 dprintk(1, "buffer index out of range\n");
1899 return -EINVAL;
1900 }
1901
1902 vb = q->bufs[index];
1903
1904 if (plane >= vb->num_planes) {
1905 dprintk(1, "buffer plane out of range\n");
1906 return -EINVAL;
1907 }
1908
1909 if (vb2_fileio_is_active(q)) {
1910 dprintk(1, "expbuf: file io in progress\n");
1911 return -EBUSY;
1912 }
1913
1914 vb_plane = &vb->planes[plane];
1915
1916 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
1917 flags & O_ACCMODE);
1918 if (IS_ERR_OR_NULL(dbuf)) {
1919 dprintk(1, "failed to export buffer %d, plane %d\n",
1920 index, plane);
1921 return -EINVAL;
1922 }
1923
1924 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
1925 if (ret < 0) {
1926 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1927 index, plane, ret);
1928 dma_buf_put(dbuf);
1929 return ret;
1930 }
1931
1932 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1933 index, plane, ret);
1934 *fd = ret;
1935
1936 return 0;
1937 }
1938 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
1939
1940 /**
1941 * vb2_mmap() - map video buffers into application address space
1942 * @q: videobuf2 queue
1943 * @vma: vma passed to the mmap file operation handler in the driver
1944 *
1945 * Should be called from mmap file operation handler of a driver.
1946 * This function maps one plane of one of the available video buffers to
1947 * userspace. To map whole video memory allocated on reqbufs, this function
1948 * has to be called once per each plane per each buffer previously allocated.
1949 *
1950 * When the userspace application calls mmap, it passes to it an offset returned
1951 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1952 * a "cookie", which is then used to identify the plane to be mapped.
1953 * This function finds a plane with a matching offset and a mapping is performed
1954 * by the means of a provided memory operation.
1955 *
1956 * The return values from this function are intended to be directly returned
1957 * from the mmap handler in driver.
1958 */
vb2_mmap(struct vb2_queue * q,struct vm_area_struct * vma)1959 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1960 {
1961 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1962 struct vb2_buffer *vb;
1963 unsigned int buffer = 0, plane = 0;
1964 int ret;
1965 unsigned long length;
1966
1967 if (q->memory != VB2_MEMORY_MMAP) {
1968 dprintk(1, "queue is not currently set up for mmap\n");
1969 return -EINVAL;
1970 }
1971
1972 /*
1973 * Check memory area access mode.
1974 */
1975 if (!(vma->vm_flags & VM_SHARED)) {
1976 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
1977 return -EINVAL;
1978 }
1979 if (q->is_output) {
1980 if (!(vma->vm_flags & VM_WRITE)) {
1981 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
1982 return -EINVAL;
1983 }
1984 } else {
1985 if (!(vma->vm_flags & VM_READ)) {
1986 dprintk(1, "invalid vma flags, VM_READ needed\n");
1987 return -EINVAL;
1988 }
1989 }
1990
1991 mutex_lock(&q->mmap_lock);
1992
1993 if (vb2_fileio_is_active(q)) {
1994 dprintk(1, "mmap: file io in progress\n");
1995 ret = -EBUSY;
1996 goto unlock;
1997 }
1998
1999 /*
2000 * Find the plane corresponding to the offset passed by userspace.
2001 */
2002 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2003 if (ret)
2004 goto unlock;
2005
2006 vb = q->bufs[buffer];
2007
2008 /*
2009 * MMAP requires page_aligned buffers.
2010 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2011 * so, we need to do the same here.
2012 */
2013 length = PAGE_ALIGN(vb->planes[plane].length);
2014 if (length < (vma->vm_end - vma->vm_start)) {
2015 dprintk(1,
2016 "MMAP invalid, as it would overflow buffer length\n");
2017 ret = -EINVAL;
2018 goto unlock;
2019 }
2020
2021 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2022
2023 unlock:
2024 mutex_unlock(&q->mmap_lock);
2025 if (ret)
2026 return ret;
2027
2028 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2029 return 0;
2030 }
2031 EXPORT_SYMBOL_GPL(vb2_mmap);
2032
2033 #ifndef CONFIG_MMU
vb2_get_unmapped_area(struct vb2_queue * q,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)2034 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2035 unsigned long addr,
2036 unsigned long len,
2037 unsigned long pgoff,
2038 unsigned long flags)
2039 {
2040 unsigned long off = pgoff << PAGE_SHIFT;
2041 struct vb2_buffer *vb;
2042 unsigned int buffer, plane;
2043 void *vaddr;
2044 int ret;
2045
2046 if (q->memory != VB2_MEMORY_MMAP) {
2047 dprintk(1, "queue is not currently set up for mmap\n");
2048 return -EINVAL;
2049 }
2050
2051 /*
2052 * Find the plane corresponding to the offset passed by userspace.
2053 */
2054 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2055 if (ret)
2056 return ret;
2057
2058 vb = q->bufs[buffer];
2059
2060 vaddr = vb2_plane_vaddr(vb, plane);
2061 return vaddr ? (unsigned long)vaddr : -EINVAL;
2062 }
2063 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2064 #endif
2065
2066 /**
2067 * vb2_core_queue_init() - initialize a videobuf2 queue
2068 * @q: videobuf2 queue; this structure should be allocated in driver
2069 *
2070 * The vb2_queue structure should be allocated by the driver. The driver is
2071 * responsible of clearing it's content and setting initial values for some
2072 * required entries before calling this function.
2073 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2074 * to the struct vb2_queue description in include/media/videobuf2-core.h
2075 * for more information.
2076 */
vb2_core_queue_init(struct vb2_queue * q)2077 int vb2_core_queue_init(struct vb2_queue *q)
2078 {
2079 /*
2080 * Sanity check
2081 */
2082 if (WARN_ON(!q) ||
2083 WARN_ON(!q->ops) ||
2084 WARN_ON(!q->mem_ops) ||
2085 WARN_ON(!q->type) ||
2086 WARN_ON(!q->io_modes) ||
2087 WARN_ON(!q->ops->queue_setup) ||
2088 WARN_ON(!q->ops->buf_queue))
2089 return -EINVAL;
2090
2091 INIT_LIST_HEAD(&q->queued_list);
2092 INIT_LIST_HEAD(&q->done_list);
2093 spin_lock_init(&q->done_lock);
2094 mutex_init(&q->mmap_lock);
2095 init_waitqueue_head(&q->done_wq);
2096
2097 if (q->buf_struct_size == 0)
2098 q->buf_struct_size = sizeof(struct vb2_buffer);
2099
2100 return 0;
2101 }
2102 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2103
2104 /**
2105 * vb2_core_queue_release() - stop streaming, release the queue and free memory
2106 * @q: videobuf2 queue
2107 *
2108 * This function stops streaming and performs necessary clean ups, including
2109 * freeing video buffer memory. The driver is responsible for freeing
2110 * the vb2_queue structure itself.
2111 */
vb2_core_queue_release(struct vb2_queue * q)2112 void vb2_core_queue_release(struct vb2_queue *q)
2113 {
2114 __vb2_queue_cancel(q);
2115 mutex_lock(&q->mmap_lock);
2116 __vb2_queue_free(q, q->num_buffers);
2117 mutex_unlock(&q->mmap_lock);
2118 }
2119 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2120
2121 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
2122 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2123 MODULE_LICENSE("GPL");
2124