blob: 917b1cbb5cbf034ecd649fffecadd59ab983a896 [file] [log] [blame]
Pawel Osciake23ccc02010-10-11 10:56:41 -03001/*
2 * videobuf2-core.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciake23ccc02010-10-11 10:56:41 -03007 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/err.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21
Hans Verkuil95213ce2011-07-13 04:26:52 -030022#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
Pawel Osciake23ccc02010-10-11 10:56:41 -030025#include <media/videobuf2-core.h>
26
27static int debug;
28module_param(debug, int, 0644);
29
30#define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
35
Hans Verkuilb5b45412014-01-29 11:53:25 -030036#ifdef CONFIG_VIDEO_ADV_DEBUG
37
38/*
39 * If advanced debugging is on, then count how often each op is called,
40 * which can either be per-buffer or per-queue.
41 *
42 * If the op failed then the 'fail_' variant is called to decrease the
43 * counter. That makes it easy to check that the 'init' and 'cleanup'
44 * (and variations thereof) stay balanced.
45 */
46
47#define call_memop(vb, op, args...) \
48({ \
49 struct vb2_queue *_q = (vb)->vb2_queue; \
50 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
51 _q, (vb)->v4l2_buf.index, #op, \
52 _q->mem_ops->op ? "" : " (nop)"); \
53 (vb)->cnt_mem_ ## op++; \
54 _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
55})
56#define fail_memop(vb, op) ((vb)->cnt_mem_ ## op--)
Pawel Osciake23ccc02010-10-11 10:56:41 -030057
58#define call_qop(q, op, args...) \
Hans Verkuilb5b45412014-01-29 11:53:25 -030059({ \
60 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
61 (q)->ops->op ? "" : " (nop)"); \
62 (q)->cnt_ ## op++; \
63 (q)->ops->op ? (q)->ops->op(args) : 0; \
64})
65#define fail_qop(q, op) ((q)->cnt_ ## op--)
66
67#define call_vb_qop(vb, op, args...) \
68({ \
69 struct vb2_queue *_q = (vb)->vb2_queue; \
70 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
71 _q, (vb)->v4l2_buf.index, #op, \
72 _q->ops->op ? "" : " (nop)"); \
73 (vb)->cnt_ ## op++; \
74 _q->ops->op ? _q->ops->op(args) : 0; \
75})
76#define fail_vb_qop(vb, op) ((vb)->cnt_ ## op--)
77
78#else
79
80#define call_memop(vb, op, args...) \
81 ((vb)->vb2_queue->mem_ops->op ? (vb)->vb2_queue->mem_ops->op(args) : 0)
82#define fail_memop(vb, op)
83
84#define call_qop(q, op, args...) \
85 ((q)->ops->op ? (q)->ops->op(args) : 0)
86#define fail_qop(q, op)
87
88#define call_vb_qop(vb, op, args...) \
89 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
90#define fail_vb_qop(vb, op)
91
92#endif
Pawel Osciake23ccc02010-10-11 10:56:41 -030093
Hans Verkuilf1343282014-02-24 14:44:50 -030094/* Flags that are set by the vb2 core */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030095#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030096 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030097 V4L2_BUF_FLAG_PREPARED | \
98 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Hans Verkuilf1343282014-02-24 14:44:50 -030099/* Output buffer flags that should be passed on to the driver */
100#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
101 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300102
Pawel Osciake23ccc02010-10-11 10:56:41 -0300103/**
104 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
105 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300106static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300107{
108 struct vb2_queue *q = vb->vb2_queue;
109 void *mem_priv;
110 int plane;
111
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -0300112 /*
113 * Allocate memory for all planes in this buffer
114 * NOTE: mmapped areas should be page aligned
115 */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300116 for (plane = 0; plane < vb->num_planes; ++plane) {
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -0300117 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
118
Hans Verkuilb5b45412014-01-29 11:53:25 -0300119 mem_priv = call_memop(vb, alloc, q->alloc_ctx[plane],
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -0300120 size, q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -0300121 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300122 goto free;
123
124 /* Associate allocator private data with this plane */
125 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300126 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300127 }
128
129 return 0;
130free:
Hans Verkuilb5b45412014-01-29 11:53:25 -0300131 fail_memop(vb, alloc);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300132 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300133 for (; plane > 0; --plane) {
Hans Verkuilb5b45412014-01-29 11:53:25 -0300134 call_memop(vb, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300135 vb->planes[plane - 1].mem_priv = NULL;
136 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300137
138 return -ENOMEM;
139}
140
141/**
142 * __vb2_buf_mem_free() - free memory of the given buffer
143 */
144static void __vb2_buf_mem_free(struct vb2_buffer *vb)
145{
Pawel Osciake23ccc02010-10-11 10:56:41 -0300146 unsigned int plane;
147
148 for (plane = 0; plane < vb->num_planes; ++plane) {
Hans Verkuilb5b45412014-01-29 11:53:25 -0300149 call_memop(vb, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300150 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300151 dprintk(3, "Freed plane %d of buffer %d\n", plane,
152 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300153 }
154}
155
156/**
157 * __vb2_buf_userptr_put() - release userspace memory associated with
158 * a USERPTR buffer
159 */
160static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
161{
Pawel Osciake23ccc02010-10-11 10:56:41 -0300162 unsigned int plane;
163
164 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300165 if (vb->planes[plane].mem_priv)
Hans Verkuilb5b45412014-01-29 11:53:25 -0300166 call_memop(vb, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300167 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300168 }
169}
170
171/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300172 * __vb2_plane_dmabuf_put() - release memory associated with
173 * a DMABUF shared plane
174 */
Hans Verkuilb5b45412014-01-29 11:53:25 -0300175static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
Sumit Semwalc5384042012-06-14 10:37:37 -0300176{
177 if (!p->mem_priv)
178 return;
179
180 if (p->dbuf_mapped)
Hans Verkuilb5b45412014-01-29 11:53:25 -0300181 call_memop(vb, unmap_dmabuf, p->mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -0300182
Hans Verkuilb5b45412014-01-29 11:53:25 -0300183 call_memop(vb, detach_dmabuf, p->mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -0300184 dma_buf_put(p->dbuf);
185 memset(p, 0, sizeof(*p));
186}
187
188/**
189 * __vb2_buf_dmabuf_put() - release memory associated with
190 * a DMABUF shared buffer
191 */
192static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
193{
Sumit Semwalc5384042012-06-14 10:37:37 -0300194 unsigned int plane;
195
196 for (plane = 0; plane < vb->num_planes; ++plane)
Hans Verkuilb5b45412014-01-29 11:53:25 -0300197 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
Sumit Semwalc5384042012-06-14 10:37:37 -0300198}
199
200/**
Hans Verkuila5e3d742013-12-04 15:14:05 +0100201 * __setup_lengths() - setup initial lengths for every plane in
202 * every buffer on the queue
203 */
204static void __setup_lengths(struct vb2_queue *q, unsigned int n)
205{
206 unsigned int buffer, plane;
207 struct vb2_buffer *vb;
208
209 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
210 vb = q->bufs[buffer];
211 if (!vb)
212 continue;
213
214 for (plane = 0; plane < vb->num_planes; ++plane)
215 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
216 }
217}
218
219/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300220 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
221 * every buffer on the queue
222 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300223static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300224{
225 unsigned int buffer, plane;
226 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300227 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300228
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300229 if (q->num_buffers) {
230 struct v4l2_plane *p;
231 vb = q->bufs[q->num_buffers - 1];
232 p = &vb->v4l2_planes[vb->num_planes - 1];
233 off = PAGE_ALIGN(p->m.mem_offset + p->length);
234 } else {
235 off = 0;
236 }
237
238 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300239 vb = q->bufs[buffer];
240 if (!vb)
241 continue;
242
243 for (plane = 0; plane < vb->num_planes; ++plane) {
244 vb->v4l2_planes[plane].m.mem_offset = off;
245
246 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
247 buffer, plane, off);
248
249 off += vb->v4l2_planes[plane].length;
250 off = PAGE_ALIGN(off);
251 }
252 }
253}
254
255/**
256 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
257 * video buffer memory for all buffers/planes on the queue and initializes the
258 * queue
259 *
260 * Returns the number of buffers successfully allocated.
261 */
262static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300263 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300264{
265 unsigned int buffer;
266 struct vb2_buffer *vb;
267 int ret;
268
269 for (buffer = 0; buffer < num_buffers; ++buffer) {
270 /* Allocate videobuf buffer structures */
271 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
272 if (!vb) {
273 dprintk(1, "Memory alloc for buffer struct failed\n");
274 break;
275 }
276
277 /* Length stores number of planes for multiplanar buffers */
278 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
279 vb->v4l2_buf.length = num_planes;
280
281 vb->state = VB2_BUF_STATE_DEQUEUED;
282 vb->vb2_queue = q;
283 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300284 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300285 vb->v4l2_buf.type = q->type;
286 vb->v4l2_buf.memory = memory;
287
288 /* Allocate video buffer memory for the MMAP type */
289 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300290 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300291 if (ret) {
292 dprintk(1, "Failed allocating memory for "
293 "buffer %d\n", buffer);
294 kfree(vb);
295 break;
296 }
297 /*
298 * Call the driver-provided buffer initialization
299 * callback, if given. An error in initialization
300 * results in queue setup failure.
301 */
Hans Verkuilb5b45412014-01-29 11:53:25 -0300302 ret = call_vb_qop(vb, buf_init, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300303 if (ret) {
304 dprintk(1, "Buffer %d %p initialization"
305 " failed\n", buffer, vb);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300306 fail_vb_qop(vb, buf_init);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300307 __vb2_buf_mem_free(vb);
308 kfree(vb);
309 break;
310 }
311 }
312
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300313 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300314 }
315
Hans Verkuila5e3d742013-12-04 15:14:05 +0100316 __setup_lengths(q, buffer);
Philipp Zabeldc775232013-09-19 04:37:29 -0300317 if (memory == V4L2_MEMORY_MMAP)
318 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300319
320 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300321 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300322
323 return buffer;
324}
325
326/**
327 * __vb2_free_mem() - release all video buffer memory for a given queue
328 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300329static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300330{
331 unsigned int buffer;
332 struct vb2_buffer *vb;
333
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300334 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
335 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300336 vb = q->bufs[buffer];
337 if (!vb)
338 continue;
339
340 /* Free MMAP buffers or release USERPTR buffers */
341 if (q->memory == V4L2_MEMORY_MMAP)
342 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300343 else if (q->memory == V4L2_MEMORY_DMABUF)
344 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300345 else
346 __vb2_buf_userptr_put(vb);
347 }
348}
349
350/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300351 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
352 * related information, if no buffers are left return the queue to an
353 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300354 */
Hans Verkuil63faabf2013-12-13 13:13:40 -0300355static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300356{
357 unsigned int buffer;
358
Hans Verkuil63faabf2013-12-13 13:13:40 -0300359 /*
360 * Sanity check: when preparing a buffer the queue lock is released for
361 * a short while (see __buf_prepare for the details), which would allow
362 * a race with a reqbufs which can call this function. Removing the
363 * buffers from underneath __buf_prepare is obviously a bad idea, so we
364 * check if any of the buffers is in the state PREPARING, and if so we
365 * just return -EAGAIN.
366 */
367 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
368 ++buffer) {
369 if (q->bufs[buffer] == NULL)
370 continue;
371 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
372 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
373 return -EAGAIN;
374 }
375 }
376
Pawel Osciake23ccc02010-10-11 10:56:41 -0300377 /* Call driver-provided cleanup function for each buffer, if provided */
Hans Verkuilb5b45412014-01-29 11:53:25 -0300378 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
379 ++buffer) {
380 if (q->bufs[buffer])
381 call_vb_qop(q->bufs[buffer], buf_cleanup, q->bufs[buffer]);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300382 }
383
384 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300385 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300386
Hans Verkuilb5b45412014-01-29 11:53:25 -0300387#ifdef CONFIG_VIDEO_ADV_DEBUG
388 /*
389 * Check that all the calls were balances during the life-time of this
390 * queue. If not (or if the debug level is 1 or up), then dump the
391 * counters to the kernel log.
392 */
393 if (q->num_buffers) {
394 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
395 q->cnt_wait_prepare != q->cnt_wait_finish;
396
397 if (unbalanced || debug) {
398 pr_info("vb2: counters for queue %p:%s\n", q,
399 unbalanced ? " UNBALANCED!" : "");
400 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
401 q->cnt_queue_setup, q->cnt_start_streaming,
402 q->cnt_stop_streaming);
403 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
404 q->cnt_wait_prepare, q->cnt_wait_finish);
405 }
406 q->cnt_queue_setup = 0;
407 q->cnt_wait_prepare = 0;
408 q->cnt_wait_finish = 0;
409 q->cnt_start_streaming = 0;
410 q->cnt_stop_streaming = 0;
411 }
412 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
413 struct vb2_buffer *vb = q->bufs[buffer];
414 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
415 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
416 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
417 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
418 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
419 vb->cnt_buf_queue != vb->cnt_buf_done ||
420 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
421 vb->cnt_buf_init != vb->cnt_buf_cleanup;
422
423 if (unbalanced || debug) {
424 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
425 q, buffer, unbalanced ? " UNBALANCED!" : "");
426 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
427 vb->cnt_buf_init, vb->cnt_buf_cleanup,
428 vb->cnt_buf_prepare, vb->cnt_buf_finish);
429 pr_info("vb2: buf_queue: %u buf_done: %u\n",
430 vb->cnt_buf_queue, vb->cnt_buf_done);
431 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
432 vb->cnt_mem_alloc, vb->cnt_mem_put,
433 vb->cnt_mem_prepare, vb->cnt_mem_finish,
434 vb->cnt_mem_mmap);
435 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
436 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
437 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
438 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
439 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
440 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
441 vb->cnt_mem_get_dmabuf,
442 vb->cnt_mem_num_users,
443 vb->cnt_mem_vaddr,
444 vb->cnt_mem_cookie);
445 }
446 }
447#endif
448
Pawel Osciake23ccc02010-10-11 10:56:41 -0300449 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300450 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
451 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300452 kfree(q->bufs[buffer]);
453 q->bufs[buffer] = NULL;
454 }
455
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300456 q->num_buffers -= buffers;
457 if (!q->num_buffers)
458 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300459 INIT_LIST_HEAD(&q->queued_list);
Hans Verkuil63faabf2013-12-13 13:13:40 -0300460 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300461}
462
463/**
464 * __verify_planes_array() - verify that the planes array passed in struct
465 * v4l2_buffer from userspace can be safely used
466 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300467static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300468{
Hans Verkuil32a77262012-09-28 06:12:53 -0300469 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
470 return 0;
471
Pawel Osciake23ccc02010-10-11 10:56:41 -0300472 /* Is memory for copying plane information present? */
473 if (NULL == b->m.planes) {
474 dprintk(1, "Multi-planar buffer passed but "
475 "planes array not provided\n");
476 return -EINVAL;
477 }
478
479 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
480 dprintk(1, "Incorrect planes array length, "
481 "expected %d, got %d\n", vb->num_planes, b->length);
482 return -EINVAL;
483 }
484
485 return 0;
486}
487
488/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300489 * __verify_length() - Verify that the bytesused value for each plane fits in
490 * the plane length and that the data offset doesn't exceed the bytesused value.
491 */
492static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
493{
494 unsigned int length;
495 unsigned int plane;
496
497 if (!V4L2_TYPE_IS_OUTPUT(b->type))
498 return 0;
499
500 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
501 for (plane = 0; plane < vb->num_planes; ++plane) {
502 length = (b->memory == V4L2_MEMORY_USERPTR)
503 ? b->m.planes[plane].length
504 : vb->v4l2_planes[plane].length;
505
506 if (b->m.planes[plane].bytesused > length)
507 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300508
509 if (b->m.planes[plane].data_offset > 0 &&
510 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300511 b->m.planes[plane].bytesused)
512 return -EINVAL;
513 }
514 } else {
515 length = (b->memory == V4L2_MEMORY_USERPTR)
516 ? b->length : vb->v4l2_planes[0].length;
517
518 if (b->bytesused > length)
519 return -EINVAL;
520 }
521
522 return 0;
523}
524
525/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300526 * __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 */
529static bool __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) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300533 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300534 /*
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 */
Hans Verkuilb5b45412014-01-29 11:53:25 -0300540 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300541 return true;
542 }
543 return false;
544}
545
546/**
547 * __buffers_in_use() - return true if any buffers on the queue are in use and
548 * the queue cannot be freed (by the means of REQBUFS(0)) call
549 */
550static bool __buffers_in_use(struct vb2_queue *q)
551{
552 unsigned int buffer;
553 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
554 if (__buffer_in_use(q, q->bufs[buffer]))
555 return true;
556 }
557 return false;
558}
559
560/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300561 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
562 * returned to userspace
563 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300564static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300565{
566 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300567
Sakari Ailus2b719d72012-05-02 09:40:03 -0300568 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300569 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300570 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300571 b->reserved = vb->v4l2_buf.reserved;
572
573 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300574 /*
575 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300576 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300577 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300578 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300579 memcpy(b->m.planes, vb->v4l2_planes,
580 b->length * sizeof(struct v4l2_plane));
581 } else {
582 /*
583 * We use length and offset in v4l2_planes array even for
584 * single-planar buffers, but userspace does not.
585 */
586 b->length = vb->v4l2_planes[0].length;
587 b->bytesused = vb->v4l2_planes[0].bytesused;
588 if (q->memory == V4L2_MEMORY_MMAP)
589 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
590 else if (q->memory == V4L2_MEMORY_USERPTR)
591 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300592 else if (q->memory == V4L2_MEMORY_DMABUF)
593 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300594 }
595
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300596 /*
597 * Clear any buffer state related flags.
598 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300599 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -0300600 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
601 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
602 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
603 /*
604 * For non-COPY timestamps, drop timestamp source bits
605 * and obtain the timestamp source from the queue.
606 */
607 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
608 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
609 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300610
611 switch (vb->state) {
612 case VB2_BUF_STATE_QUEUED:
613 case VB2_BUF_STATE_ACTIVE:
614 b->flags |= V4L2_BUF_FLAG_QUEUED;
615 break;
616 case VB2_BUF_STATE_ERROR:
617 b->flags |= V4L2_BUF_FLAG_ERROR;
618 /* fall through */
619 case VB2_BUF_STATE_DONE:
620 b->flags |= V4L2_BUF_FLAG_DONE;
621 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300622 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300623 b->flags |= V4L2_BUF_FLAG_PREPARED;
624 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -0300625 case VB2_BUF_STATE_PREPARING:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300626 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300627 /* nothing */
628 break;
629 }
630
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300631 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300632 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300633}
634
635/**
636 * vb2_querybuf() - query video buffer information
637 * @q: videobuf queue
638 * @b: buffer struct passed from userspace to vidioc_querybuf handler
639 * in driver
640 *
641 * Should be called from vidioc_querybuf ioctl handler in driver.
642 * This function will verify the passed v4l2_buffer structure and fill the
643 * relevant information for the userspace.
644 *
645 * The return values from this function are intended to be directly returned
646 * from vidioc_querybuf handler in driver.
647 */
648int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
649{
650 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300651 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300652
653 if (b->type != q->type) {
654 dprintk(1, "querybuf: wrong buffer type\n");
655 return -EINVAL;
656 }
657
658 if (b->index >= q->num_buffers) {
659 dprintk(1, "querybuf: buffer index out of range\n");
660 return -EINVAL;
661 }
662 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300663 ret = __verify_planes_array(vb, b);
664 if (!ret)
665 __fill_v4l2_buffer(vb, b);
666 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300667}
668EXPORT_SYMBOL(vb2_querybuf);
669
670/**
671 * __verify_userptr_ops() - verify that all memory operations required for
672 * USERPTR queue type have been provided
673 */
674static int __verify_userptr_ops(struct vb2_queue *q)
675{
676 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
677 !q->mem_ops->put_userptr)
678 return -EINVAL;
679
680 return 0;
681}
682
683/**
684 * __verify_mmap_ops() - verify that all memory operations required for
685 * MMAP queue type have been provided
686 */
687static int __verify_mmap_ops(struct vb2_queue *q)
688{
689 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
690 !q->mem_ops->put || !q->mem_ops->mmap)
691 return -EINVAL;
692
693 return 0;
694}
695
696/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300697 * __verify_dmabuf_ops() - verify that all memory operations required for
698 * DMABUF queue type have been provided
699 */
700static int __verify_dmabuf_ops(struct vb2_queue *q)
701{
702 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
703 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
704 !q->mem_ops->unmap_dmabuf)
705 return -EINVAL;
706
707 return 0;
708}
709
710/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300711 * __verify_memory_type() - Check whether the memory type and buffer type
712 * passed to a buffer operation are compatible with the queue.
713 */
714static int __verify_memory_type(struct vb2_queue *q,
715 enum v4l2_memory memory, enum v4l2_buf_type type)
716{
Sumit Semwalc5384042012-06-14 10:37:37 -0300717 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
718 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300719 dprintk(1, "reqbufs: unsupported memory type\n");
720 return -EINVAL;
721 }
722
723 if (type != q->type) {
724 dprintk(1, "reqbufs: requested type is incorrect\n");
725 return -EINVAL;
726 }
727
728 /*
729 * Make sure all the required memory ops for given memory type
730 * are available.
731 */
732 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
733 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
734 return -EINVAL;
735 }
736
737 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
738 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
739 return -EINVAL;
740 }
741
Sumit Semwalc5384042012-06-14 10:37:37 -0300742 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
743 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
744 return -EINVAL;
745 }
746
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300747 /*
748 * Place the busy tests at the end: -EBUSY can be ignored when
749 * create_bufs is called with count == 0, but count == 0 should still
750 * do the memory and type validation.
751 */
752 if (q->fileio) {
753 dprintk(1, "reqbufs: file io in progress\n");
754 return -EBUSY;
755 }
756 return 0;
757}
758
759/**
760 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300761 * @q: videobuf2 queue
762 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
763 *
764 * Should be called from vidioc_reqbufs ioctl handler of a driver.
765 * This function:
766 * 1) verifies streaming parameters passed from the userspace,
767 * 2) sets up the queue,
768 * 3) negotiates number of buffers and planes per buffer with the driver
769 * to be used during streaming,
770 * 4) allocates internal buffer structures (struct vb2_buffer), according to
771 * the agreed parameters,
772 * 5) for MMAP memory type, allocates actual video memory, using the
773 * memory handling/allocation routines provided during queue initialization
774 *
775 * If req->count is 0, all the memory will be freed instead.
776 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
777 * and the queue is not busy, memory will be reallocated.
778 *
779 * The return values from this function are intended to be directly returned
780 * from vidioc_reqbufs handler in driver.
781 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300782static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300783{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300784 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300785 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300786
787 if (q->streaming) {
788 dprintk(1, "reqbufs: streaming active\n");
789 return -EBUSY;
790 }
791
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300792 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300793 /*
794 * We already have buffers allocated, so first check if they
795 * are not in use and can be freed.
796 */
797 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
798 dprintk(1, "reqbufs: memory in use, cannot free\n");
799 return -EBUSY;
800 }
801
Hans Verkuil63faabf2013-12-13 13:13:40 -0300802 ret = __vb2_queue_free(q, q->num_buffers);
803 if (ret)
804 return ret;
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300805
806 /*
807 * In case of REQBUFS(0) return immediately without calling
808 * driver's queue_setup() callback and allocating resources.
809 */
810 if (req->count == 0)
811 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300812 }
813
814 /*
815 * Make sure the requested values and current defaults are sane.
816 */
817 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300818 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300819 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300820 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300821
822 /*
823 * Ask the driver how many buffers and planes per buffer it requires.
824 * Driver also sets the size and allocator context for each plane.
825 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300826 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300827 q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300828 if (ret) {
829 fail_qop(q, queue_setup);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300830 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -0300831 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300832
833 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300834 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300835 if (ret == 0) {
836 dprintk(1, "Memory allocation failed\n");
837 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300838 }
839
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300840 allocated_buffers = ret;
841
Pawel Osciake23ccc02010-10-11 10:56:41 -0300842 /*
843 * Check if driver can handle the allocated number of buffers.
844 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300845 if (allocated_buffers < num_buffers) {
846 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300847
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300848 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
849 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300850 if (ret)
851 fail_qop(q, queue_setup);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300852
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300853 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300854 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300855
856 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300857 * Either the driver has accepted a smaller number of buffers,
858 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300859 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300860 }
861
862 q->num_buffers = allocated_buffers;
863
864 if (ret < 0) {
865 __vb2_queue_free(q, allocated_buffers);
866 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300867 }
868
Pawel Osciake23ccc02010-10-11 10:56:41 -0300869 /*
870 * Return the number of successfully allocated buffers
871 * to the userspace.
872 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300873 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300874
875 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300876}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300877
878/**
879 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
880 * type values.
881 * @q: videobuf2 queue
882 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
883 */
884int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
885{
886 int ret = __verify_memory_type(q, req->memory, req->type);
887
888 return ret ? ret : __reqbufs(q, req);
889}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300890EXPORT_SYMBOL_GPL(vb2_reqbufs);
891
892/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300893 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300894 * @q: videobuf2 queue
895 * @create: creation parameters, passed from userspace to vidioc_create_bufs
896 * handler in driver
897 *
898 * Should be called from vidioc_create_bufs ioctl handler of a driver.
899 * This function:
900 * 1) verifies parameter sanity
901 * 2) calls the .queue_setup() queue operation
902 * 3) performs any necessary memory allocations
903 *
904 * The return values from this function are intended to be directly returned
905 * from vidioc_create_bufs handler in driver.
906 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300907static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300908{
909 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300910 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300911
912 if (q->num_buffers == VIDEO_MAX_FRAME) {
913 dprintk(1, "%s(): maximum number of buffers already allocated\n",
914 __func__);
915 return -ENOBUFS;
916 }
917
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300918 if (!q->num_buffers) {
919 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
920 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
921 q->memory = create->memory;
922 }
923
924 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
925
926 /*
927 * Ask the driver, whether the requested number of buffers, planes per
928 * buffer and their sizes are acceptable
929 */
930 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
931 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300932 if (ret) {
933 fail_qop(q, queue_setup);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300934 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -0300935 }
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300936
937 /* Finally, allocate buffers and video memory */
938 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
939 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300940 if (ret == 0) {
941 dprintk(1, "Memory allocation failed\n");
942 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300943 }
944
945 allocated_buffers = ret;
946
947 /*
948 * Check if driver can handle the so far allocated number of buffers.
949 */
950 if (ret < num_buffers) {
951 num_buffers = ret;
952
953 /*
954 * q->num_buffers contains the total number of buffers, that the
955 * queue driver has set up
956 */
957 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
958 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300959 if (ret)
960 fail_qop(q, queue_setup);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300961
962 if (!ret && allocated_buffers < num_buffers)
963 ret = -ENOMEM;
964
965 /*
966 * Either the driver has accepted a smaller number of buffers,
967 * or .queue_setup() returned an error
968 */
969 }
970
971 q->num_buffers += allocated_buffers;
972
973 if (ret < 0) {
974 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300975 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300976 }
977
978 /*
979 * Return the number of successfully allocated buffers
980 * to the userspace.
981 */
982 create->count = allocated_buffers;
983
984 return 0;
985}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300986
987/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300988 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
989 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300990 * @q: videobuf2 queue
991 * @create: creation parameters, passed from userspace to vidioc_create_bufs
992 * handler in driver
993 */
994int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
995{
996 int ret = __verify_memory_type(q, create->memory, create->format.type);
997
998 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300999 if (create->count == 0)
1000 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -03001001 return ret ? ret : __create_bufs(q, create);
1002}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001003EXPORT_SYMBOL_GPL(vb2_create_bufs);
1004
1005/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001006 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
1007 * @vb: vb2_buffer to which the plane in question belongs to
1008 * @plane_no: plane number for which the address is to be returned
1009 *
1010 * This function returns a kernel virtual address of a given plane if
1011 * such a mapping exist, NULL otherwise.
1012 */
1013void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
1014{
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001015 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001016 return NULL;
1017
Hans Verkuilb5b45412014-01-29 11:53:25 -03001018 return call_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001019
1020}
1021EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
1022
1023/**
1024 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
1025 * @vb: vb2_buffer to which the plane in question belongs to
1026 * @plane_no: plane number for which the cookie is to be returned
1027 *
1028 * This function returns an allocator specific cookie for a given plane if
1029 * available, NULL otherwise. The allocator should provide some simple static
1030 * inline function, which would convert this cookie to the allocator specific
1031 * type that can be used directly by the driver to access the buffer. This can
1032 * be for example physical address, pointer to scatter list or IOMMU mapping.
1033 */
1034void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
1035{
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001036 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001037 return NULL;
1038
Hans Verkuilb5b45412014-01-29 11:53:25 -03001039 return call_memop(vb, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001040}
1041EXPORT_SYMBOL_GPL(vb2_plane_cookie);
1042
1043/**
1044 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1045 * @vb: vb2_buffer returned from the driver
1046 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1047 * or VB2_BUF_STATE_ERROR if the operation finished with an error
1048 *
1049 * This function should be called by the driver after a hardware operation on
1050 * a buffer is finished and the buffer may be returned to userspace. The driver
1051 * cannot use this buffer anymore until it is queued back to it by videobuf
1052 * by the means of buf_queue callback. Only buffers previously queued to the
1053 * driver by buf_queue can be passed to this function.
1054 */
1055void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1056{
1057 struct vb2_queue *q = vb->vb2_queue;
1058 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001059 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001060
1061 if (vb->state != VB2_BUF_STATE_ACTIVE)
1062 return;
1063
1064 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
1065 return;
1066
Hans Verkuilb5b45412014-01-29 11:53:25 -03001067#ifdef CONFIG_VIDEO_ADV_DEBUG
1068 /*
1069 * Although this is not a callback, it still does have to balance
1070 * with the buf_queue op. So update this counter manually.
1071 */
1072 vb->cnt_buf_done++;
1073#endif
Pawel Osciake23ccc02010-10-11 10:56:41 -03001074 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -03001075 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001076
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001077 /* sync buffers */
1078 for (plane = 0; plane < vb->num_planes; ++plane)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001079 call_memop(vb, finish, vb->planes[plane].mem_priv);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001080
Pawel Osciake23ccc02010-10-11 10:56:41 -03001081 /* Add the buffer to the done buffers list */
1082 spin_lock_irqsave(&q->done_lock, flags);
1083 vb->state = state;
1084 list_add_tail(&vb->done_entry, &q->done_list);
1085 atomic_dec(&q->queued_count);
1086 spin_unlock_irqrestore(&q->done_lock, flags);
1087
1088 /* Inform any processes that may be waiting for buffers */
1089 wake_up(&q->done_wq);
1090}
1091EXPORT_SYMBOL_GPL(vb2_buffer_done);
1092
1093/**
Hans Verkuil32a77262012-09-28 06:12:53 -03001094 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1095 * v4l2_buffer by the userspace. The caller has already verified that struct
1096 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -03001097 */
Hans Verkuil32a77262012-09-28 06:12:53 -03001098static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001099 struct v4l2_plane *v4l2_planes)
1100{
1101 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001102
1103 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001104 /* Fill in driver-provided information for OUTPUT types */
1105 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1106 /*
1107 * Will have to go up to b->length when API starts
1108 * accepting variable number of planes.
1109 */
1110 for (plane = 0; plane < vb->num_planes; ++plane) {
1111 v4l2_planes[plane].bytesused =
1112 b->m.planes[plane].bytesused;
1113 v4l2_planes[plane].data_offset =
1114 b->m.planes[plane].data_offset;
1115 }
1116 }
1117
1118 if (b->memory == V4L2_MEMORY_USERPTR) {
1119 for (plane = 0; plane < vb->num_planes; ++plane) {
1120 v4l2_planes[plane].m.userptr =
1121 b->m.planes[plane].m.userptr;
1122 v4l2_planes[plane].length =
1123 b->m.planes[plane].length;
1124 }
1125 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001126 if (b->memory == V4L2_MEMORY_DMABUF) {
1127 for (plane = 0; plane < vb->num_planes; ++plane) {
1128 v4l2_planes[plane].m.fd =
1129 b->m.planes[plane].m.fd;
1130 v4l2_planes[plane].length =
1131 b->m.planes[plane].length;
1132 v4l2_planes[plane].data_offset =
1133 b->m.planes[plane].data_offset;
1134 }
1135 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001136 } else {
1137 /*
1138 * Single-planar buffers do not use planes array,
1139 * so fill in relevant v4l2_buffer struct fields instead.
1140 * In videobuf we use our internal V4l2_planes struct for
1141 * single-planar buffers as well, for simplicity.
1142 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001143 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001144 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001145 v4l2_planes[0].data_offset = 0;
1146 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001147
1148 if (b->memory == V4L2_MEMORY_USERPTR) {
1149 v4l2_planes[0].m.userptr = b->m.userptr;
1150 v4l2_planes[0].length = b->length;
1151 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001152
1153 if (b->memory == V4L2_MEMORY_DMABUF) {
1154 v4l2_planes[0].m.fd = b->m.fd;
1155 v4l2_planes[0].length = b->length;
1156 v4l2_planes[0].data_offset = 0;
1157 }
1158
Pawel Osciake23ccc02010-10-11 10:56:41 -03001159 }
1160
Hans Verkuilf1343282014-02-24 14:44:50 -03001161 /* Zero flags that the vb2 core handles */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03001162 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -03001163 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
1164 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
1165 /*
1166 * Non-COPY timestamps and non-OUTPUT queues will get
1167 * their timestamp and timestamp source flags from the
1168 * queue.
1169 */
1170 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1171 }
1172
Hans Verkuilf1343282014-02-24 14:44:50 -03001173 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1174 /*
1175 * For output buffers mask out the timecode flag:
1176 * this will be handled later in vb2_internal_qbuf().
1177 * The 'field' is valid metadata for this output buffer
1178 * and so that needs to be copied here.
1179 */
1180 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TIMECODE;
1181 vb->v4l2_buf.field = b->field;
1182 } else {
1183 /* Zero any output buffer flags as this is a capture buffer */
1184 vb->v4l2_buf.flags &= ~V4L2_BUFFER_OUT_FLAGS;
1185 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001186}
1187
1188/**
1189 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1190 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001191static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001192{
1193 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1194 struct vb2_queue *q = vb->vb2_queue;
1195 void *mem_priv;
1196 unsigned int plane;
1197 int ret;
1198 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1199
Hans Verkuil32a77262012-09-28 06:12:53 -03001200 /* Copy relevant information provided by the userspace */
1201 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001202
1203 for (plane = 0; plane < vb->num_planes; ++plane) {
1204 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001205 if (vb->v4l2_planes[plane].m.userptr &&
1206 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001207 && vb->v4l2_planes[plane].length == planes[plane].length)
1208 continue;
1209
1210 dprintk(3, "qbuf: userspace address for plane %d changed, "
1211 "reacquiring memory\n", plane);
1212
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001213 /* Check if the provided plane buffer is large enough */
1214 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001215 dprintk(1, "qbuf: provided buffer size %u is less than "
1216 "setup size %u for plane %d\n",
1217 planes[plane].length,
1218 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001219 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001220 goto err;
1221 }
1222
Pawel Osciake23ccc02010-10-11 10:56:41 -03001223 /* Release previously acquired memory if present */
1224 if (vb->planes[plane].mem_priv)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001225 call_memop(vb, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001226
1227 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001228 vb->v4l2_planes[plane].m.userptr = 0;
1229 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001230
1231 /* Acquire each plane's memory */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001232 mem_priv = call_memop(vb, get_userptr, q->alloc_ctx[plane],
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001233 planes[plane].m.userptr,
1234 planes[plane].length, write);
1235 if (IS_ERR_OR_NULL(mem_priv)) {
1236 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001237 "memory for plane %d\n", plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03001238 fail_memop(vb, get_userptr);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001239 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1240 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001241 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001242 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001243 }
1244
1245 /*
1246 * Call driver-specific initialization on the newly acquired buffer,
1247 * if provided.
1248 */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001249 ret = call_vb_qop(vb, buf_init, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001250 if (ret) {
1251 dprintk(1, "qbuf: buffer initialization failed\n");
Hans Verkuilb5b45412014-01-29 11:53:25 -03001252 fail_vb_qop(vb, buf_init);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001253 goto err;
1254 }
1255
1256 /*
1257 * Now that everything is in order, copy relevant information
1258 * provided by userspace.
1259 */
1260 for (plane = 0; plane < vb->num_planes; ++plane)
1261 vb->v4l2_planes[plane] = planes[plane];
1262
1263 return 0;
1264err:
1265 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001266 for (plane = 0; plane < vb->num_planes; ++plane) {
1267 if (vb->planes[plane].mem_priv)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001268 call_memop(vb, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001269 vb->planes[plane].mem_priv = NULL;
1270 vb->v4l2_planes[plane].m.userptr = 0;
1271 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001272 }
1273
1274 return ret;
1275}
1276
1277/**
1278 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1279 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001280static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001281{
Hans Verkuil32a77262012-09-28 06:12:53 -03001282 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1283 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001284}
1285
1286/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001287 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1288 */
1289static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1290{
1291 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1292 struct vb2_queue *q = vb->vb2_queue;
1293 void *mem_priv;
1294 unsigned int plane;
1295 int ret;
1296 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1297
Laurent Pinchart6f546c52014-01-01 09:10:48 -03001298 /* Copy relevant information provided by the userspace */
Sumit Semwalc5384042012-06-14 10:37:37 -03001299 __fill_vb2_buffer(vb, b, planes);
1300
1301 for (plane = 0; plane < vb->num_planes; ++plane) {
1302 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1303
1304 if (IS_ERR_OR_NULL(dbuf)) {
1305 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1306 plane);
1307 ret = -EINVAL;
1308 goto err;
1309 }
1310
1311 /* use DMABUF size if length is not provided */
1312 if (planes[plane].length == 0)
1313 planes[plane].length = dbuf->size;
1314
1315 if (planes[plane].length < planes[plane].data_offset +
1316 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001317 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1318 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001319 ret = -EINVAL;
1320 goto err;
1321 }
1322
1323 /* Skip the plane if already verified */
1324 if (dbuf == vb->planes[plane].dbuf &&
1325 vb->v4l2_planes[plane].length == planes[plane].length) {
1326 dma_buf_put(dbuf);
1327 continue;
1328 }
1329
1330 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1331
1332 /* Release previously acquired memory if present */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001333 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
Sumit Semwalc5384042012-06-14 10:37:37 -03001334 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1335
1336 /* Acquire each plane's memory */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001337 mem_priv = call_memop(vb, attach_dmabuf, q->alloc_ctx[plane],
Sumit Semwalc5384042012-06-14 10:37:37 -03001338 dbuf, planes[plane].length, write);
1339 if (IS_ERR(mem_priv)) {
1340 dprintk(1, "qbuf: failed to attach dmabuf\n");
Hans Verkuilb5b45412014-01-29 11:53:25 -03001341 fail_memop(vb, attach_dmabuf);
Sumit Semwalc5384042012-06-14 10:37:37 -03001342 ret = PTR_ERR(mem_priv);
1343 dma_buf_put(dbuf);
1344 goto err;
1345 }
1346
1347 vb->planes[plane].dbuf = dbuf;
1348 vb->planes[plane].mem_priv = mem_priv;
1349 }
1350
1351 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1352 * really we want to do this just before the DMA, not while queueing
1353 * the buffer(s)..
1354 */
1355 for (plane = 0; plane < vb->num_planes; ++plane) {
Hans Verkuilb5b45412014-01-29 11:53:25 -03001356 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -03001357 if (ret) {
1358 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1359 plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03001360 fail_memop(vb, map_dmabuf);
Sumit Semwalc5384042012-06-14 10:37:37 -03001361 goto err;
1362 }
1363 vb->planes[plane].dbuf_mapped = 1;
1364 }
1365
1366 /*
1367 * Call driver-specific initialization on the newly acquired buffer,
1368 * if provided.
1369 */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001370 ret = call_vb_qop(vb, buf_init, vb);
Sumit Semwalc5384042012-06-14 10:37:37 -03001371 if (ret) {
1372 dprintk(1, "qbuf: buffer initialization failed\n");
Hans Verkuilb5b45412014-01-29 11:53:25 -03001373 fail_vb_qop(vb, buf_init);
Sumit Semwalc5384042012-06-14 10:37:37 -03001374 goto err;
1375 }
1376
1377 /*
1378 * Now that everything is in order, copy relevant information
1379 * provided by userspace.
1380 */
1381 for (plane = 0; plane < vb->num_planes; ++plane)
1382 vb->v4l2_planes[plane] = planes[plane];
1383
1384 return 0;
1385err:
1386 /* In case of errors, release planes that were already acquired */
1387 __vb2_buf_dmabuf_put(vb);
1388
1389 return ret;
1390}
1391
1392/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001393 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1394 */
1395static void __enqueue_in_driver(struct vb2_buffer *vb)
1396{
1397 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001398 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001399
1400 vb->state = VB2_BUF_STATE_ACTIVE;
1401 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001402
1403 /* sync buffers */
1404 for (plane = 0; plane < vb->num_planes; ++plane)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001405 call_memop(vb, prepare, vb->planes[plane].mem_priv);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001406
Hans Verkuilb5b45412014-01-29 11:53:25 -03001407 call_vb_qop(vb, buf_queue, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001408}
1409
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001410static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001411{
1412 struct vb2_queue *q = vb->vb2_queue;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001413 struct rw_semaphore *mmap_sem;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001414 int ret;
1415
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001416 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001417 if (ret < 0) {
1418 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1419 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001420 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001421 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001422
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001423 vb->state = VB2_BUF_STATE_PREPARING;
Hans Verkuilf1343282014-02-24 14:44:50 -03001424 vb->v4l2_buf.timestamp.tv_sec = 0;
1425 vb->v4l2_buf.timestamp.tv_usec = 0;
1426 vb->v4l2_buf.sequence = 0;
1427
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001428 switch (q->memory) {
1429 case V4L2_MEMORY_MMAP:
1430 ret = __qbuf_mmap(vb, b);
1431 break;
1432 case V4L2_MEMORY_USERPTR:
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001433 /*
Mauro Carvalho Chehabf103b5d2014-01-07 07:03:09 -02001434 * In case of user pointer buffers vb2 allocators need to get
1435 * direct access to userspace pages. This requires getting
1436 * the mmap semaphore for read access in the current process
1437 * structure. The same semaphore is taken before calling mmap
1438 * operation, while both qbuf/prepare_buf and mmap are called
1439 * by the driver or v4l2 core with the driver's lock held.
1440 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1441 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1442 * the videobuf2 core releases the driver's lock, takes
1443 * mmap_sem and then takes the driver's lock again.
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001444 */
1445 mmap_sem = &current->mm->mmap_sem;
1446 call_qop(q, wait_prepare, q);
1447 down_read(mmap_sem);
1448 call_qop(q, wait_finish, q);
1449
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001450 ret = __qbuf_userptr(vb, b);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001451
1452 up_read(mmap_sem);
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001453 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001454 case V4L2_MEMORY_DMABUF:
1455 ret = __qbuf_dmabuf(vb, b);
1456 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001457 default:
1458 WARN(1, "Invalid queue type\n");
1459 ret = -EINVAL;
1460 }
1461
Hans Verkuilb5b45412014-01-29 11:53:25 -03001462 if (!ret) {
1463 ret = call_vb_qop(vb, buf_prepare, vb);
1464 if (ret)
1465 fail_vb_qop(vb, buf_prepare);
1466 }
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001467 if (ret)
1468 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001469 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001470
1471 return ret;
1472}
1473
Laurent Pinchart012043b2013-08-09 08:11:26 -03001474static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
Hans Verkuil41381112013-12-13 13:13:39 -03001475 const char *opname)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001476{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001477 if (b->type != q->type) {
1478 dprintk(1, "%s(): invalid buffer type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001479 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001480 }
1481
1482 if (b->index >= q->num_buffers) {
1483 dprintk(1, "%s(): buffer index out of range\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001484 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001485 }
1486
Hans Verkuil41381112013-12-13 13:13:39 -03001487 if (q->bufs[b->index] == NULL) {
Laurent Pinchart012043b2013-08-09 08:11:26 -03001488 /* Should never happen */
1489 dprintk(1, "%s(): buffer is NULL\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001490 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001491 }
1492
1493 if (b->memory != q->memory) {
1494 dprintk(1, "%s(): invalid memory type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001495 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001496 }
1497
Hans Verkuil41381112013-12-13 13:13:39 -03001498 return __verify_planes_array(q->bufs[b->index], b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001499}
1500
Pawel Osciake23ccc02010-10-11 10:56:41 -03001501/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001502 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1503 * @q: videobuf2 queue
1504 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1505 * handler in driver
1506 *
1507 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1508 * This function:
1509 * 1) verifies the passed buffer,
1510 * 2) calls buf_prepare callback in the driver (if provided), in which
1511 * driver-specific buffer initialization can be performed,
1512 *
1513 * The return values from this function are intended to be directly returned
1514 * from vidioc_prepare_buf handler in driver.
1515 */
1516int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1517{
Hans Verkuil41381112013-12-13 13:13:39 -03001518 struct vb2_buffer *vb;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001519 int ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001520
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001521 if (q->fileio) {
1522 dprintk(1, "%s(): file io in progress\n", __func__);
1523 return -EBUSY;
1524 }
1525
1526 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
Hans Verkuil41381112013-12-13 13:13:39 -03001527 if (ret)
1528 return ret;
1529
1530 vb = q->bufs[b->index];
1531 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1532 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1533 vb->state);
1534 return -EINVAL;
1535 }
1536
1537 ret = __buf_prepare(vb, b);
1538 if (!ret) {
1539 /* Fill buffer information for the userspace */
1540 __fill_v4l2_buffer(vb, b);
1541
1542 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1543 }
1544 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001545}
1546EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1547
Hans Verkuil02f142e2013-12-13 13:13:42 -03001548/**
1549 * vb2_start_streaming() - Attempt to start streaming.
1550 * @q: videobuf2 queue
1551 *
1552 * If there are not enough buffers, then retry_start_streaming is set to
1553 * 1 and 0 is returned. The next time a buffer is queued and
1554 * retry_start_streaming is 1, this function will be called again to
1555 * retry starting the DMA engine.
1556 */
1557static int vb2_start_streaming(struct vb2_queue *q)
1558{
1559 int ret;
1560
1561 /* Tell the driver to start streaming */
1562 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
Hans Verkuilb5b45412014-01-29 11:53:25 -03001563 if (ret)
1564 fail_qop(q, start_streaming);
Hans Verkuil02f142e2013-12-13 13:13:42 -03001565
1566 /*
1567 * If there are not enough buffers queued to start streaming, then
1568 * the start_streaming operation will return -ENOBUFS and you have to
1569 * retry when the next buffer is queued.
1570 */
1571 if (ret == -ENOBUFS) {
1572 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1573 q->retry_start_streaming = 1;
1574 return 0;
1575 }
1576 if (ret)
1577 dprintk(1, "qbuf: driver refused to start streaming\n");
1578 else
1579 q->retry_start_streaming = 0;
1580 return ret;
1581}
1582
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001583static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001584{
Hans Verkuil41381112013-12-13 13:13:39 -03001585 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1586 struct vb2_buffer *vb;
1587
1588 if (ret)
1589 return ret;
1590
1591 vb = q->bufs[b->index];
Laurent Pinchart012043b2013-08-09 08:11:26 -03001592
1593 switch (vb->state) {
1594 case VB2_BUF_STATE_DEQUEUED:
1595 ret = __buf_prepare(vb, b);
1596 if (ret)
1597 return ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001598 break;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001599 case VB2_BUF_STATE_PREPARED:
1600 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001601 case VB2_BUF_STATE_PREPARING:
1602 dprintk(1, "qbuf: buffer still being prepared\n");
1603 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001604 default:
Hans Verkuil952c9ee2014-02-10 13:12:00 -03001605 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1606 vb->state);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001607 return -EINVAL;
1608 }
1609
1610 /*
1611 * Add to the queued buffers list, a buffer will stay on it until
1612 * dequeued in dqbuf.
1613 */
1614 list_add_tail(&vb->queued_entry, &q->queued_list);
1615 vb->state = VB2_BUF_STATE_QUEUED;
Hans Verkuilf1343282014-02-24 14:44:50 -03001616 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1617 /*
1618 * For output buffers copy the timestamp if needed,
1619 * and the timecode field and flag if needed.
1620 */
Sakari Ailusc57ff792014-03-01 10:28:02 -03001621 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1622 V4L2_BUF_FLAG_TIMESTAMP_COPY)
Hans Verkuilf1343282014-02-24 14:44:50 -03001623 vb->v4l2_buf.timestamp = b->timestamp;
1624 vb->v4l2_buf.flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
1625 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
1626 vb->v4l2_buf.timecode = b->timecode;
1627 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001628
1629 /*
1630 * If already streaming, give the buffer to driver for processing.
1631 * If not, the buffer will be given to driver on next streamon.
1632 */
1633 if (q->streaming)
1634 __enqueue_in_driver(vb);
1635
Hans Verkuil41381112013-12-13 13:13:39 -03001636 /* Fill buffer information for the userspace */
1637 __fill_v4l2_buffer(vb, b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001638
Hans Verkuil02f142e2013-12-13 13:13:42 -03001639 if (q->retry_start_streaming) {
1640 ret = vb2_start_streaming(q);
1641 if (ret)
1642 return ret;
1643 }
1644
Hans Verkuil41381112013-12-13 13:13:39 -03001645 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1646 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001647}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001648
1649/**
1650 * vb2_qbuf() - Queue a buffer from userspace
1651 * @q: videobuf2 queue
1652 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1653 * in driver
1654 *
1655 * Should be called from vidioc_qbuf ioctl handler of a driver.
1656 * This function:
1657 * 1) verifies the passed buffer,
1658 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1659 * which driver-specific buffer initialization can be performed,
1660 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1661 * callback for processing.
1662 *
1663 * The return values from this function are intended to be directly returned
1664 * from vidioc_qbuf handler in driver.
1665 */
1666int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1667{
1668 if (q->fileio) {
1669 dprintk(1, "%s(): file io in progress\n", __func__);
1670 return -EBUSY;
1671 }
1672
1673 return vb2_internal_qbuf(q, b);
1674}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001675EXPORT_SYMBOL_GPL(vb2_qbuf);
1676
1677/**
1678 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1679 * for dequeuing
1680 *
1681 * Will sleep if required for nonblocking == false.
1682 */
1683static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1684{
1685 /*
1686 * All operations on vb_done_list are performed under done_lock
1687 * spinlock protection. However, buffers may be removed from
1688 * it and returned to userspace only while holding both driver's
1689 * lock and the done_lock spinlock. Thus we can be sure that as
1690 * long as we hold the driver's lock, the list will remain not
1691 * empty if list_empty() check succeeds.
1692 */
1693
1694 for (;;) {
1695 int ret;
1696
1697 if (!q->streaming) {
1698 dprintk(1, "Streaming off, will not wait for buffers\n");
1699 return -EINVAL;
1700 }
1701
1702 if (!list_empty(&q->done_list)) {
1703 /*
1704 * Found a buffer that we were waiting for.
1705 */
1706 break;
1707 }
1708
1709 if (nonblocking) {
1710 dprintk(1, "Nonblocking and no buffers to dequeue, "
1711 "will not wait\n");
1712 return -EAGAIN;
1713 }
1714
1715 /*
1716 * We are streaming and blocking, wait for another buffer to
1717 * become ready or for streamoff. Driver's lock is released to
1718 * allow streamoff or qbuf to be called while waiting.
1719 */
1720 call_qop(q, wait_prepare, q);
1721
1722 /*
1723 * All locks have been released, it is safe to sleep now.
1724 */
1725 dprintk(3, "Will sleep waiting for buffers\n");
1726 ret = wait_event_interruptible(q->done_wq,
1727 !list_empty(&q->done_list) || !q->streaming);
1728
1729 /*
1730 * We need to reevaluate both conditions again after reacquiring
1731 * the locks or return an error if one occurred.
1732 */
1733 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001734 if (ret) {
1735 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001736 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001737 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001738 }
1739 return 0;
1740}
1741
1742/**
1743 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1744 *
1745 * Will sleep if required for nonblocking == false.
1746 */
1747static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001748 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001749{
1750 unsigned long flags;
1751 int ret;
1752
1753 /*
1754 * Wait for at least one buffer to become available on the done_list.
1755 */
1756 ret = __vb2_wait_for_done_vb(q, nonblocking);
1757 if (ret)
1758 return ret;
1759
1760 /*
1761 * Driver's lock has been held since we last verified that done_list
1762 * is not empty, so no need for another list_empty(done_list) check.
1763 */
1764 spin_lock_irqsave(&q->done_lock, flags);
1765 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001766 /*
1767 * Only remove the buffer from done_list if v4l2_buffer can handle all
1768 * the planes.
1769 */
1770 ret = __verify_planes_array(*vb, b);
1771 if (!ret)
1772 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001773 spin_unlock_irqrestore(&q->done_lock, flags);
1774
Hans Verkuil32a77262012-09-28 06:12:53 -03001775 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001776}
1777
1778/**
1779 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1780 * @q: videobuf2 queue
1781 *
1782 * This function will wait until all buffers that have been given to the driver
1783 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1784 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1785 * taken, for example from stop_streaming() callback.
1786 */
1787int vb2_wait_for_all_buffers(struct vb2_queue *q)
1788{
1789 if (!q->streaming) {
1790 dprintk(1, "Streaming off, will not wait for buffers\n");
1791 return -EINVAL;
1792 }
1793
Hans Verkuil02f142e2013-12-13 13:13:42 -03001794 if (!q->retry_start_streaming)
1795 wait_event(q->done_wq, !atomic_read(&q->queued_count));
Pawel Osciake23ccc02010-10-11 10:56:41 -03001796 return 0;
1797}
1798EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1799
1800/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001801 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1802 */
1803static void __vb2_dqbuf(struct vb2_buffer *vb)
1804{
1805 struct vb2_queue *q = vb->vb2_queue;
1806 unsigned int i;
1807
1808 /* nothing to do if the buffer is already dequeued */
1809 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1810 return;
1811
1812 vb->state = VB2_BUF_STATE_DEQUEUED;
1813
1814 /* unmap DMABUF buffer */
1815 if (q->memory == V4L2_MEMORY_DMABUF)
1816 for (i = 0; i < vb->num_planes; ++i) {
1817 if (!vb->planes[i].dbuf_mapped)
1818 continue;
Hans Verkuilb5b45412014-01-29 11:53:25 -03001819 call_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -03001820 vb->planes[i].dbuf_mapped = 0;
1821 }
1822}
1823
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001824static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001825{
1826 struct vb2_buffer *vb = NULL;
1827 int ret;
1828
1829 if (b->type != q->type) {
1830 dprintk(1, "dqbuf: invalid buffer type\n");
1831 return -EINVAL;
1832 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001833 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1834 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001835 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001836
Hans Verkuilb5b45412014-01-29 11:53:25 -03001837 ret = call_vb_qop(vb, buf_finish, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001838 if (ret) {
1839 dprintk(1, "dqbuf: buffer finish failed\n");
1840 return ret;
1841 }
1842
1843 switch (vb->state) {
1844 case VB2_BUF_STATE_DONE:
1845 dprintk(3, "dqbuf: Returning done buffer\n");
1846 break;
1847 case VB2_BUF_STATE_ERROR:
1848 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1849 break;
1850 default:
1851 dprintk(1, "dqbuf: Invalid buffer state\n");
1852 return -EINVAL;
1853 }
1854
1855 /* Fill buffer information for the userspace */
1856 __fill_v4l2_buffer(vb, b);
1857 /* Remove from videobuf queue */
1858 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001859 /* go back to dequeued state */
1860 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001861
1862 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1863 vb->v4l2_buf.index, vb->state);
1864
Pawel Osciake23ccc02010-10-11 10:56:41 -03001865 return 0;
1866}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001867
1868/**
1869 * vb2_dqbuf() - Dequeue a buffer to the userspace
1870 * @q: videobuf2 queue
1871 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1872 * in driver
1873 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1874 * buffers ready for dequeuing are present. Normally the driver
1875 * would be passing (file->f_flags & O_NONBLOCK) here
1876 *
1877 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1878 * This function:
1879 * 1) verifies the passed buffer,
1880 * 2) calls buf_finish callback in the driver (if provided), in which
1881 * driver can perform any additional operations that may be required before
1882 * returning the buffer to userspace, such as cache sync,
1883 * 3) the buffer struct members are filled with relevant information for
1884 * the userspace.
1885 *
1886 * The return values from this function are intended to be directly returned
1887 * from vidioc_dqbuf handler in driver.
1888 */
1889int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1890{
1891 if (q->fileio) {
1892 dprintk(1, "dqbuf: file io in progress\n");
1893 return -EBUSY;
1894 }
1895 return vb2_internal_dqbuf(q, b, nonblocking);
1896}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001897EXPORT_SYMBOL_GPL(vb2_dqbuf);
1898
1899/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001900 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1901 *
1902 * Removes all queued buffers from driver's queue and all buffers queued by
1903 * userspace from videobuf's queue. Returns to state after reqbufs.
1904 */
1905static void __vb2_queue_cancel(struct vb2_queue *q)
1906{
1907 unsigned int i;
1908
Hans Verkuil02f142e2013-12-13 13:13:42 -03001909 if (q->retry_start_streaming) {
1910 q->retry_start_streaming = 0;
1911 q->streaming = 0;
1912 }
1913
Pawel Osciake23ccc02010-10-11 10:56:41 -03001914 /*
1915 * Tell driver to stop all transactions and release all queued
1916 * buffers.
1917 */
1918 if (q->streaming)
1919 call_qop(q, stop_streaming, q);
1920 q->streaming = 0;
1921
1922 /*
1923 * Remove all buffers from videobuf's list...
1924 */
1925 INIT_LIST_HEAD(&q->queued_list);
1926 /*
1927 * ...and done list; userspace will not receive any buffers it
1928 * has not already dequeued before initiating cancel.
1929 */
1930 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001931 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001932 wake_up_all(&q->done_wq);
1933
1934 /*
1935 * Reinitialize all buffers for next use.
1936 */
1937 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001938 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001939}
1940
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001941static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001942{
1943 struct vb2_buffer *vb;
1944 int ret;
1945
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001946 if (type != q->type) {
1947 dprintk(1, "streamon: invalid stream type\n");
1948 return -EINVAL;
1949 }
1950
1951 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03001952 dprintk(3, "streamon successful: already streaming\n");
1953 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001954 }
1955
Ricardo Ribalda548df782014-01-08 05:01:33 -03001956 if (!q->num_buffers) {
1957 dprintk(1, "streamon: no buffers have been allocated\n");
1958 return -EINVAL;
1959 }
1960
Ricardo Ribalda Delgado249f5a52014-01-08 05:01:33 -03001961 if (!q->num_buffers) {
1962 dprintk(1, "streamon: no buffers have been allocated\n");
1963 return -EINVAL;
1964 }
1965
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001966 /*
1967 * If any buffers were queued before streamon,
1968 * we can now pass them to driver for processing.
1969 */
1970 list_for_each_entry(vb, &q->queued_list, queued_entry)
1971 __enqueue_in_driver(vb);
1972
Hans Verkuil02f142e2013-12-13 13:13:42 -03001973 /* Tell driver to start streaming. */
1974 ret = vb2_start_streaming(q);
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001975 if (ret) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001976 __vb2_queue_cancel(q);
1977 return ret;
1978 }
1979
1980 q->streaming = 1;
1981
1982 dprintk(3, "Streamon successful\n");
1983 return 0;
1984}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001985
1986/**
1987 * vb2_streamon - start streaming
1988 * @q: videobuf2 queue
1989 * @type: type argument passed from userspace to vidioc_streamon handler
1990 *
1991 * Should be called from vidioc_streamon handler of a driver.
1992 * This function:
1993 * 1) verifies current state
1994 * 2) passes any previously queued buffers to the driver and starts streaming
1995 *
1996 * The return values from this function are intended to be directly returned
1997 * from vidioc_streamon handler in the driver.
1998 */
1999int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
2000{
2001 if (q->fileio) {
2002 dprintk(1, "streamon: file io in progress\n");
2003 return -EBUSY;
2004 }
2005 return vb2_internal_streamon(q, type);
2006}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002007EXPORT_SYMBOL_GPL(vb2_streamon);
2008
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002009static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
2010{
2011 if (type != q->type) {
2012 dprintk(1, "streamoff: invalid stream type\n");
2013 return -EINVAL;
2014 }
2015
2016 if (!q->streaming) {
2017 dprintk(3, "streamoff successful: not streaming\n");
2018 return 0;
2019 }
2020
2021 /*
2022 * Cancel will pause streaming and remove all buffers from the driver
2023 * and videobuf, effectively returning control over them to userspace.
2024 */
2025 __vb2_queue_cancel(q);
2026
2027 dprintk(3, "Streamoff successful\n");
2028 return 0;
2029}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002030
2031/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002032 * vb2_streamoff - stop streaming
2033 * @q: videobuf2 queue
2034 * @type: type argument passed from userspace to vidioc_streamoff handler
2035 *
2036 * Should be called from vidioc_streamoff handler of a driver.
2037 * This function:
2038 * 1) verifies current state,
2039 * 2) stop streaming and dequeues any queued buffers, including those previously
2040 * passed to the driver (after waiting for the driver to finish).
2041 *
2042 * This call can be used for pausing playback.
2043 * The return values from this function are intended to be directly returned
2044 * from vidioc_streamoff handler in the driver
2045 */
2046int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
2047{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002048 if (q->fileio) {
2049 dprintk(1, "streamoff: file io in progress\n");
2050 return -EBUSY;
2051 }
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002052 return vb2_internal_streamoff(q, type);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002053}
2054EXPORT_SYMBOL_GPL(vb2_streamoff);
2055
2056/**
2057 * __find_plane_by_offset() - find plane associated with the given offset off
2058 */
2059static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2060 unsigned int *_buffer, unsigned int *_plane)
2061{
2062 struct vb2_buffer *vb;
2063 unsigned int buffer, plane;
2064
2065 /*
2066 * Go over all buffers and their planes, comparing the given offset
2067 * with an offset assigned to each plane. If a match is found,
2068 * return its buffer and plane numbers.
2069 */
2070 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2071 vb = q->bufs[buffer];
2072
2073 for (plane = 0; plane < vb->num_planes; ++plane) {
2074 if (vb->v4l2_planes[plane].m.mem_offset == off) {
2075 *_buffer = buffer;
2076 *_plane = plane;
2077 return 0;
2078 }
2079 }
2080 }
2081
2082 return -EINVAL;
2083}
2084
2085/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002086 * vb2_expbuf() - Export a buffer as a file descriptor
2087 * @q: videobuf2 queue
2088 * @eb: export buffer structure passed from userspace to vidioc_expbuf
2089 * handler in driver
2090 *
2091 * The return values from this function are intended to be directly returned
2092 * from vidioc_expbuf handler in driver.
2093 */
2094int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
2095{
2096 struct vb2_buffer *vb = NULL;
2097 struct vb2_plane *vb_plane;
2098 int ret;
2099 struct dma_buf *dbuf;
2100
2101 if (q->memory != V4L2_MEMORY_MMAP) {
2102 dprintk(1, "Queue is not currently set up for mmap\n");
2103 return -EINVAL;
2104 }
2105
2106 if (!q->mem_ops->get_dmabuf) {
2107 dprintk(1, "Queue does not support DMA buffer exporting\n");
2108 return -EINVAL;
2109 }
2110
Philipp Zabelea3aba82013-05-21 05:11:35 -03002111 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
2112 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002113 return -EINVAL;
2114 }
2115
2116 if (eb->type != q->type) {
2117 dprintk(1, "qbuf: invalid buffer type\n");
2118 return -EINVAL;
2119 }
2120
2121 if (eb->index >= q->num_buffers) {
2122 dprintk(1, "buffer index out of range\n");
2123 return -EINVAL;
2124 }
2125
2126 vb = q->bufs[eb->index];
2127
2128 if (eb->plane >= vb->num_planes) {
2129 dprintk(1, "buffer plane out of range\n");
2130 return -EINVAL;
2131 }
2132
2133 vb_plane = &vb->planes[eb->plane];
2134
Hans Verkuilb5b45412014-01-29 11:53:25 -03002135 dbuf = call_memop(vb, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002136 if (IS_ERR_OR_NULL(dbuf)) {
2137 dprintk(1, "Failed to export buffer %d, plane %d\n",
2138 eb->index, eb->plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03002139 fail_memop(vb, get_dmabuf);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002140 return -EINVAL;
2141 }
2142
Philipp Zabelea3aba82013-05-21 05:11:35 -03002143 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002144 if (ret < 0) {
2145 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2146 eb->index, eb->plane, ret);
2147 dma_buf_put(dbuf);
2148 return ret;
2149 }
2150
2151 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2152 eb->index, eb->plane, ret);
2153 eb->fd = ret;
2154
2155 return 0;
2156}
2157EXPORT_SYMBOL_GPL(vb2_expbuf);
2158
2159/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002160 * vb2_mmap() - map video buffers into application address space
2161 * @q: videobuf2 queue
2162 * @vma: vma passed to the mmap file operation handler in the driver
2163 *
2164 * Should be called from mmap file operation handler of a driver.
2165 * This function maps one plane of one of the available video buffers to
2166 * userspace. To map whole video memory allocated on reqbufs, this function
2167 * has to be called once per each plane per each buffer previously allocated.
2168 *
2169 * When the userspace application calls mmap, it passes to it an offset returned
2170 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2171 * a "cookie", which is then used to identify the plane to be mapped.
2172 * This function finds a plane with a matching offset and a mapping is performed
2173 * by the means of a provided memory operation.
2174 *
2175 * The return values from this function are intended to be directly returned
2176 * from the mmap handler in driver.
2177 */
2178int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2179{
2180 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002181 struct vb2_buffer *vb;
2182 unsigned int buffer, plane;
2183 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002184 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002185
2186 if (q->memory != V4L2_MEMORY_MMAP) {
2187 dprintk(1, "Queue is not currently set up for mmap\n");
2188 return -EINVAL;
2189 }
2190
2191 /*
2192 * Check memory area access mode.
2193 */
2194 if (!(vma->vm_flags & VM_SHARED)) {
2195 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2196 return -EINVAL;
2197 }
2198 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2199 if (!(vma->vm_flags & VM_WRITE)) {
2200 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2201 return -EINVAL;
2202 }
2203 } else {
2204 if (!(vma->vm_flags & VM_READ)) {
2205 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2206 return -EINVAL;
2207 }
2208 }
2209
2210 /*
2211 * Find the plane corresponding to the offset passed by userspace.
2212 */
2213 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2214 if (ret)
2215 return ret;
2216
2217 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03002218
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002219 /*
2220 * MMAP requires page_aligned buffers.
2221 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2222 * so, we need to do the same here.
2223 */
2224 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
2225 if (length < (vma->vm_end - vma->vm_start)) {
2226 dprintk(1,
2227 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03002228 return -EINVAL;
2229 }
2230
Hans Verkuilb5b45412014-01-29 11:53:25 -03002231 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2232 if (ret) {
2233 fail_memop(vb, mmap);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002234 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -03002235 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002236
Pawel Osciake23ccc02010-10-11 10:56:41 -03002237 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
2238 return 0;
2239}
2240EXPORT_SYMBOL_GPL(vb2_mmap);
2241
Scott Jiang6f524ec2011-09-21 09:25:23 -03002242#ifndef CONFIG_MMU
2243unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2244 unsigned long addr,
2245 unsigned long len,
2246 unsigned long pgoff,
2247 unsigned long flags)
2248{
2249 unsigned long off = pgoff << PAGE_SHIFT;
2250 struct vb2_buffer *vb;
2251 unsigned int buffer, plane;
2252 int ret;
2253
2254 if (q->memory != V4L2_MEMORY_MMAP) {
2255 dprintk(1, "Queue is not currently set up for mmap\n");
2256 return -EINVAL;
2257 }
2258
2259 /*
2260 * Find the plane corresponding to the offset passed by userspace.
2261 */
2262 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2263 if (ret)
2264 return ret;
2265
2266 vb = q->bufs[buffer];
2267
2268 return (unsigned long)vb2_plane_vaddr(vb, plane);
2269}
2270EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2271#endif
2272
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002273static int __vb2_init_fileio(struct vb2_queue *q, int read);
2274static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002275
2276/**
2277 * vb2_poll() - implements poll userspace operation
2278 * @q: videobuf2 queue
2279 * @file: file argument passed to the poll file operation handler
2280 * @wait: wait argument passed to the poll file operation handler
2281 *
2282 * This function implements poll file operation handler for a driver.
2283 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2284 * be informed that the file descriptor of a video device is available for
2285 * reading.
2286 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2287 * will be reported as available for writing.
2288 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002289 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2290 * pending events.
2291 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002292 * The return values from this function are intended to be directly returned
2293 * from poll handler in driver.
2294 */
2295unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2296{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002297 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002298 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002299 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002300 unsigned int res = 0;
2301 unsigned long flags;
2302
2303 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2304 struct v4l2_fh *fh = file->private_data;
2305
2306 if (v4l2_event_pending(fh))
2307 res = POLLPRI;
2308 else if (req_events & POLLPRI)
2309 poll_wait(file, &fh->wait, wait);
2310 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002311
Hans Verkuilcd138232013-01-30 13:29:02 -03002312 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2313 return res;
2314 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2315 return res;
2316
Pawel Osciake23ccc02010-10-11 10:56:41 -03002317 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002318 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002319 */
2320 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002321 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2322 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002323 if (__vb2_init_fileio(q, 1))
2324 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002325 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002326 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2327 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002328 if (__vb2_init_fileio(q, 0))
2329 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002330 /*
2331 * Write to OUTPUT queue can be done immediately.
2332 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002333 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002334 }
2335 }
2336
2337 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002338 * There is nothing to wait for if no buffers have already been queued.
2339 */
2340 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002341 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002342
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002343 if (list_empty(&q->done_list))
2344 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002345
2346 /*
2347 * Take first buffer available for dequeuing.
2348 */
2349 spin_lock_irqsave(&q->done_lock, flags);
2350 if (!list_empty(&q->done_list))
2351 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2352 done_entry);
2353 spin_unlock_irqrestore(&q->done_lock, flags);
2354
2355 if (vb && (vb->state == VB2_BUF_STATE_DONE
2356 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002357 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2358 res | POLLOUT | POLLWRNORM :
2359 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002360 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002361 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002362}
2363EXPORT_SYMBOL_GPL(vb2_poll);
2364
2365/**
2366 * vb2_queue_init() - initialize a videobuf2 queue
2367 * @q: videobuf2 queue; this structure should be allocated in driver
2368 *
2369 * The vb2_queue structure should be allocated by the driver. The driver is
2370 * responsible of clearing it's content and setting initial values for some
2371 * required entries before calling this function.
2372 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2373 * to the struct vb2_queue description in include/media/videobuf2-core.h
2374 * for more information.
2375 */
2376int vb2_queue_init(struct vb2_queue *q)
2377{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002378 /*
2379 * Sanity check
2380 */
2381 if (WARN_ON(!q) ||
2382 WARN_ON(!q->ops) ||
2383 WARN_ON(!q->mem_ops) ||
2384 WARN_ON(!q->type) ||
2385 WARN_ON(!q->io_modes) ||
2386 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002387 WARN_ON(!q->ops->buf_queue) ||
Sakari Ailus872484c2013-08-25 17:57:03 -03002388 WARN_ON(q->timestamp_flags &
2389 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
2390 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002391 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002392
Kamil Debski6aa69f92013-01-25 06:29:57 -03002393 /* Warn that the driver should choose an appropriate timestamp type */
Sakari Ailusc57ff792014-03-01 10:28:02 -03002394 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
2395 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
Kamil Debski6aa69f92013-01-25 06:29:57 -03002396
Pawel Osciake23ccc02010-10-11 10:56:41 -03002397 INIT_LIST_HEAD(&q->queued_list);
2398 INIT_LIST_HEAD(&q->done_list);
2399 spin_lock_init(&q->done_lock);
2400 init_waitqueue_head(&q->done_wq);
2401
2402 if (q->buf_struct_size == 0)
2403 q->buf_struct_size = sizeof(struct vb2_buffer);
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(vb2_queue_init);
2408
2409/**
2410 * vb2_queue_release() - stop streaming, release the queue and free memory
2411 * @q: videobuf2 queue
2412 *
2413 * This function stops streaming and performs necessary clean ups, including
2414 * freeing video buffer memory. The driver is responsible for freeing
2415 * the vb2_queue structure itself.
2416 */
2417void vb2_queue_release(struct vb2_queue *q)
2418{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002419 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002420 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002421 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002422}
2423EXPORT_SYMBOL_GPL(vb2_queue_release);
2424
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002425/**
2426 * struct vb2_fileio_buf - buffer context used by file io emulator
2427 *
2428 * vb2 provides a compatibility layer and emulator of file io (read and
2429 * write) calls on top of streaming API. This structure is used for
2430 * tracking context related to the buffers.
2431 */
2432struct vb2_fileio_buf {
2433 void *vaddr;
2434 unsigned int size;
2435 unsigned int pos;
2436 unsigned int queued:1;
2437};
2438
2439/**
2440 * struct vb2_fileio_data - queue context used by file io emulator
2441 *
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002442 * @cur_index: the index of the buffer currently being read from or
2443 * written to. If equal to q->num_buffers then a new buffer
2444 * must be dequeued.
2445 * @initial_index: in the read() case all buffers are queued up immediately
2446 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2447 * buffers. However, in the write() case no buffers are initially
2448 * queued, instead whenever a buffer is full it is queued up by
2449 * __vb2_perform_fileio(). Only once all available buffers have
2450 * been queued up will __vb2_perform_fileio() start to dequeue
2451 * buffers. This means that initially __vb2_perform_fileio()
2452 * needs to know what buffer index to use when it is queuing up
2453 * the buffers for the first time. That initial index is stored
2454 * in this field. Once it is equal to q->num_buffers all
2455 * available buffers have been queued and __vb2_perform_fileio()
2456 * should start the normal dequeue/queue cycle.
2457 *
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002458 * vb2 provides a compatibility layer and emulator of file io (read and
2459 * write) calls on top of streaming API. For proper operation it required
2460 * this structure to save the driver state between each call of the read
2461 * or write function.
2462 */
2463struct vb2_fileio_data {
2464 struct v4l2_requestbuffers req;
2465 struct v4l2_buffer b;
2466 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002467 unsigned int cur_index;
2468 unsigned int initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002469 unsigned int q_count;
2470 unsigned int dq_count;
2471 unsigned int flags;
2472};
2473
2474/**
2475 * __vb2_init_fileio() - initialize file io emulator
2476 * @q: videobuf2 queue
2477 * @read: mode selector (1 means read, 0 means write)
2478 */
2479static int __vb2_init_fileio(struct vb2_queue *q, int read)
2480{
2481 struct vb2_fileio_data *fileio;
2482 int i, ret;
2483 unsigned int count = 0;
2484
2485 /*
2486 * Sanity check
2487 */
2488 if ((read && !(q->io_modes & VB2_READ)) ||
2489 (!read && !(q->io_modes & VB2_WRITE)))
2490 BUG();
2491
2492 /*
2493 * Check if device supports mapping buffers to kernel virtual space.
2494 */
2495 if (!q->mem_ops->vaddr)
2496 return -EBUSY;
2497
2498 /*
2499 * Check if streaming api has not been already activated.
2500 */
2501 if (q->streaming || q->num_buffers > 0)
2502 return -EBUSY;
2503
2504 /*
2505 * Start with count 1, driver can increase it in queue_setup()
2506 */
2507 count = 1;
2508
2509 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2510 (read) ? "read" : "write", count, q->io_flags);
2511
2512 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2513 if (fileio == NULL)
2514 return -ENOMEM;
2515
2516 fileio->flags = q->io_flags;
2517
2518 /*
2519 * Request buffers and use MMAP type to force driver
2520 * to allocate buffers by itself.
2521 */
2522 fileio->req.count = count;
2523 fileio->req.memory = V4L2_MEMORY_MMAP;
2524 fileio->req.type = q->type;
2525 ret = vb2_reqbufs(q, &fileio->req);
2526 if (ret)
2527 goto err_kfree;
2528
2529 /*
2530 * Check if plane_count is correct
2531 * (multiplane buffers are not supported).
2532 */
2533 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002534 ret = -EBUSY;
2535 goto err_reqbufs;
2536 }
2537
2538 /*
2539 * Get kernel address of each buffer.
2540 */
2541 for (i = 0; i < q->num_buffers; i++) {
2542 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002543 if (fileio->bufs[i].vaddr == NULL) {
2544 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002545 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002546 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002547 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2548 }
2549
2550 /*
2551 * Read mode requires pre queuing of all buffers.
2552 */
2553 if (read) {
2554 /*
2555 * Queue all buffers.
2556 */
2557 for (i = 0; i < q->num_buffers; i++) {
2558 struct v4l2_buffer *b = &fileio->b;
2559 memset(b, 0, sizeof(*b));
2560 b->type = q->type;
2561 b->memory = q->memory;
2562 b->index = i;
2563 ret = vb2_qbuf(q, b);
2564 if (ret)
2565 goto err_reqbufs;
2566 fileio->bufs[i].queued = 1;
2567 }
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002568 /*
2569 * All buffers have been queued, so mark that by setting
2570 * initial_index to q->num_buffers
2571 */
2572 fileio->initial_index = q->num_buffers;
2573 fileio->cur_index = q->num_buffers;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002574 }
2575
Hans Verkuil02f142e2013-12-13 13:13:42 -03002576 /*
2577 * Start streaming.
2578 */
2579 ret = vb2_streamon(q, q->type);
2580 if (ret)
2581 goto err_reqbufs;
2582
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002583 q->fileio = fileio;
2584
2585 return ret;
2586
2587err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002588 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002589 vb2_reqbufs(q, &fileio->req);
2590
2591err_kfree:
2592 kfree(fileio);
2593 return ret;
2594}
2595
2596/**
2597 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2598 * @q: videobuf2 queue
2599 */
2600static int __vb2_cleanup_fileio(struct vb2_queue *q)
2601{
2602 struct vb2_fileio_data *fileio = q->fileio;
2603
2604 if (fileio) {
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002605 vb2_internal_streamoff(q, q->type);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002606 q->fileio = NULL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002607 fileio->req.count = 0;
2608 vb2_reqbufs(q, &fileio->req);
2609 kfree(fileio);
2610 dprintk(3, "file io emulator closed\n");
2611 }
2612 return 0;
2613}
2614
2615/**
2616 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2617 * @q: videobuf2 queue
2618 * @data: pointed to target userspace buffer
2619 * @count: number of bytes to read or write
2620 * @ppos: file handle position tracking pointer
2621 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2622 * @read: access mode selector (1 means read, 0 means write)
2623 */
2624static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2625 loff_t *ppos, int nonblock, int read)
2626{
2627 struct vb2_fileio_data *fileio;
2628 struct vb2_fileio_buf *buf;
2629 int ret, index;
2630
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002631 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002632 read ? "read" : "write", (long)*ppos, count,
2633 nonblock ? "non" : "");
2634
2635 if (!data)
2636 return -EINVAL;
2637
2638 /*
2639 * Initialize emulator on first call.
2640 */
2641 if (!q->fileio) {
2642 ret = __vb2_init_fileio(q, read);
2643 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2644 if (ret)
2645 return ret;
2646 }
2647 fileio = q->fileio;
2648
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002649 /*
2650 * Check if we need to dequeue the buffer.
2651 */
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002652 index = fileio->cur_index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002653 if (index >= q->num_buffers) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002654 /*
2655 * Call vb2_dqbuf to get buffer back.
2656 */
2657 memset(&fileio->b, 0, sizeof(fileio->b));
2658 fileio->b.type = q->type;
2659 fileio->b.memory = q->memory;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002660 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002661 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2662 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002663 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002664 fileio->dq_count += 1;
2665
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002666 fileio->cur_index = index = fileio->b.index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002667 buf = &fileio->bufs[index];
2668
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002669 /*
2670 * Get number of bytes filled by the driver
2671 */
Hans Verkuil88e26872013-12-13 13:13:45 -03002672 buf->pos = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002673 buf->queued = 0;
Hans Verkuil88e26872013-12-13 13:13:45 -03002674 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2675 : vb2_plane_size(q->bufs[index], 0);
2676 } else {
2677 buf = &fileio->bufs[index];
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002678 }
2679
2680 /*
2681 * Limit count on last few bytes of the buffer.
2682 */
2683 if (buf->pos + count > buf->size) {
2684 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002685 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002686 }
2687
2688 /*
2689 * Transfer data to userspace.
2690 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002691 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002692 count, index, buf->pos);
2693 if (read)
2694 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2695 else
2696 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2697 if (ret) {
2698 dprintk(3, "file io: error copying data\n");
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002699 return -EFAULT;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002700 }
2701
2702 /*
2703 * Update counters.
2704 */
2705 buf->pos += count;
2706 *ppos += count;
2707
2708 /*
2709 * Queue next buffer if required.
2710 */
2711 if (buf->pos == buf->size ||
2712 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2713 /*
2714 * Check if this is the last buffer to read.
2715 */
2716 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2717 fileio->dq_count == 1) {
2718 dprintk(3, "file io: read limit reached\n");
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002719 return __vb2_cleanup_fileio(q);
2720 }
2721
2722 /*
2723 * Call vb2_qbuf and give buffer to the driver.
2724 */
2725 memset(&fileio->b, 0, sizeof(fileio->b));
2726 fileio->b.type = q->type;
2727 fileio->b.memory = q->memory;
2728 fileio->b.index = index;
2729 fileio->b.bytesused = buf->pos;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002730 ret = vb2_internal_qbuf(q, &fileio->b);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002731 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2732 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002733 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002734
2735 /*
2736 * Buffer has been queued, update the status
2737 */
2738 buf->pos = 0;
2739 buf->queued = 1;
Hans Verkuil88e26872013-12-13 13:13:45 -03002740 buf->size = vb2_plane_size(q->bufs[index], 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002741 fileio->q_count += 1;
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002742 /*
2743 * If we are queuing up buffers for the first time, then
2744 * increase initial_index by one.
2745 */
2746 if (fileio->initial_index < q->num_buffers)
2747 fileio->initial_index++;
2748 /*
2749 * The next buffer to use is either a buffer that's going to be
2750 * queued for the first time (initial_index < q->num_buffers)
2751 * or it is equal to q->num_buffers, meaning that the next
2752 * time we need to dequeue a buffer since we've now queued up
2753 * all the 'first time' buffers.
2754 */
2755 fileio->cur_index = fileio->initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002756 }
2757
2758 /*
2759 * Return proper number of bytes processed.
2760 */
2761 if (ret == 0)
2762 ret = count;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002763 return ret;
2764}
2765
2766size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2767 loff_t *ppos, int nonblocking)
2768{
2769 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2770}
2771EXPORT_SYMBOL_GPL(vb2_read);
2772
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002773size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002774 loff_t *ppos, int nonblocking)
2775{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002776 return __vb2_perform_fileio(q, (char __user *) data, count,
2777 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002778}
2779EXPORT_SYMBOL_GPL(vb2_write);
2780
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002781
2782/*
2783 * The following functions are not part of the vb2 core API, but are helper
2784 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2785 * and struct vb2_ops.
2786 * They contain boilerplate code that most if not all drivers have to do
2787 * and so they simplify the driver code.
2788 */
2789
2790/* The queue is busy if there is a owner and you are not that owner. */
2791static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2792{
2793 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2794}
2795
2796/* vb2 ioctl helpers */
2797
2798int vb2_ioctl_reqbufs(struct file *file, void *priv,
2799 struct v4l2_requestbuffers *p)
2800{
2801 struct video_device *vdev = video_devdata(file);
2802 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2803
2804 if (res)
2805 return res;
2806 if (vb2_queue_is_busy(vdev, file))
2807 return -EBUSY;
2808 res = __reqbufs(vdev->queue, p);
2809 /* If count == 0, then the owner has released all buffers and he
2810 is no longer owner of the queue. Otherwise we have a new owner. */
2811 if (res == 0)
2812 vdev->queue->owner = p->count ? file->private_data : NULL;
2813 return res;
2814}
2815EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2816
2817int vb2_ioctl_create_bufs(struct file *file, void *priv,
2818 struct v4l2_create_buffers *p)
2819{
2820 struct video_device *vdev = video_devdata(file);
2821 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2822
2823 p->index = vdev->queue->num_buffers;
2824 /* If count == 0, then just check if memory and type are valid.
2825 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2826 if (p->count == 0)
2827 return res != -EBUSY ? res : 0;
2828 if (res)
2829 return res;
2830 if (vb2_queue_is_busy(vdev, file))
2831 return -EBUSY;
2832 res = __create_bufs(vdev->queue, p);
2833 if (res == 0)
2834 vdev->queue->owner = file->private_data;
2835 return res;
2836}
2837EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2838
2839int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2840 struct v4l2_buffer *p)
2841{
2842 struct video_device *vdev = video_devdata(file);
2843
2844 if (vb2_queue_is_busy(vdev, file))
2845 return -EBUSY;
2846 return vb2_prepare_buf(vdev->queue, p);
2847}
2848EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2849
2850int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2851{
2852 struct video_device *vdev = video_devdata(file);
2853
2854 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2855 return vb2_querybuf(vdev->queue, p);
2856}
2857EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2858
2859int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2860{
2861 struct video_device *vdev = video_devdata(file);
2862
2863 if (vb2_queue_is_busy(vdev, file))
2864 return -EBUSY;
2865 return vb2_qbuf(vdev->queue, p);
2866}
2867EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2868
2869int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2870{
2871 struct video_device *vdev = video_devdata(file);
2872
2873 if (vb2_queue_is_busy(vdev, file))
2874 return -EBUSY;
2875 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2876}
2877EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2878
2879int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2880{
2881 struct video_device *vdev = video_devdata(file);
2882
2883 if (vb2_queue_is_busy(vdev, file))
2884 return -EBUSY;
2885 return vb2_streamon(vdev->queue, i);
2886}
2887EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2888
2889int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2890{
2891 struct video_device *vdev = video_devdata(file);
2892
2893 if (vb2_queue_is_busy(vdev, file))
2894 return -EBUSY;
2895 return vb2_streamoff(vdev->queue, i);
2896}
2897EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2898
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002899int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2900{
2901 struct video_device *vdev = video_devdata(file);
2902
2903 if (vb2_queue_is_busy(vdev, file))
2904 return -EBUSY;
2905 return vb2_expbuf(vdev->queue, p);
2906}
2907EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2908
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002909/* v4l2_file_operations helpers */
2910
2911int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2912{
2913 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002914 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2915 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002916
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002917 if (lock && mutex_lock_interruptible(lock))
2918 return -ERESTARTSYS;
2919 err = vb2_mmap(vdev->queue, vma);
2920 if (lock)
2921 mutex_unlock(lock);
2922 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002923}
2924EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2925
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002926int _vb2_fop_release(struct file *file, struct mutex *lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002927{
2928 struct video_device *vdev = video_devdata(file);
2929
2930 if (file->private_data == vdev->queue->owner) {
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002931 if (lock)
2932 mutex_lock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002933 vb2_queue_release(vdev->queue);
2934 vdev->queue->owner = NULL;
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002935 if (lock)
2936 mutex_unlock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002937 }
2938 return v4l2_fh_release(file);
2939}
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002940EXPORT_SYMBOL_GPL(_vb2_fop_release);
2941
2942int vb2_fop_release(struct file *file)
2943{
2944 struct video_device *vdev = video_devdata(file);
2945 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2946
2947 return _vb2_fop_release(file, lock);
2948}
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002949EXPORT_SYMBOL_GPL(vb2_fop_release);
2950
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002951ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002952 size_t count, loff_t *ppos)
2953{
2954 struct video_device *vdev = video_devdata(file);
2955 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002956 int err = -EBUSY;
2957
Hans Verkuilcf533732012-07-31 04:02:25 -03002958 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002959 return -ERESTARTSYS;
2960 if (vb2_queue_is_busy(vdev, file))
2961 goto exit;
2962 err = vb2_write(vdev->queue, buf, count, ppos,
2963 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002964 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002965 vdev->queue->owner = file->private_data;
2966exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002967 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002968 mutex_unlock(lock);
2969 return err;
2970}
2971EXPORT_SYMBOL_GPL(vb2_fop_write);
2972
2973ssize_t vb2_fop_read(struct file *file, char __user *buf,
2974 size_t count, loff_t *ppos)
2975{
2976 struct video_device *vdev = video_devdata(file);
2977 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002978 int err = -EBUSY;
2979
Hans Verkuilcf533732012-07-31 04:02:25 -03002980 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002981 return -ERESTARTSYS;
2982 if (vb2_queue_is_busy(vdev, file))
2983 goto exit;
2984 err = vb2_read(vdev->queue, buf, count, ppos,
2985 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002986 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002987 vdev->queue->owner = file->private_data;
2988exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002989 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002990 mutex_unlock(lock);
2991 return err;
2992}
2993EXPORT_SYMBOL_GPL(vb2_fop_read);
2994
2995unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2996{
2997 struct video_device *vdev = video_devdata(file);
2998 struct vb2_queue *q = vdev->queue;
2999 struct mutex *lock = q->lock ? q->lock : vdev->lock;
3000 unsigned long req_events = poll_requested_events(wait);
3001 unsigned res;
3002 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003003 bool must_lock = false;
3004
3005 /* Try to be smart: only lock if polling might start fileio,
3006 otherwise locking will only introduce unwanted delays. */
3007 if (q->num_buffers == 0 && q->fileio == NULL) {
3008 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
3009 (req_events & (POLLIN | POLLRDNORM)))
3010 must_lock = true;
3011 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
3012 (req_events & (POLLOUT | POLLWRNORM)))
3013 must_lock = true;
3014 }
3015
3016 /* If locking is needed, but this helper doesn't know how, then you
3017 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03003018 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003019
Hans Verkuilcf533732012-07-31 04:02:25 -03003020 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003021 return POLLERR;
3022
3023 fileio = q->fileio;
3024
3025 res = vb2_poll(vdev->queue, file, wait);
3026
3027 /* If fileio was started, then we have a new queue owner. */
3028 if (must_lock && !fileio && q->fileio)
3029 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03003030 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003031 mutex_unlock(lock);
3032 return res;
3033}
3034EXPORT_SYMBOL_GPL(vb2_fop_poll);
3035
3036#ifndef CONFIG_MMU
3037unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
3038 unsigned long len, unsigned long pgoff, unsigned long flags)
3039{
3040 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03003041 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
3042 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003043
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03003044 if (lock && mutex_lock_interruptible(lock))
3045 return -ERESTARTSYS;
3046 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
3047 if (lock)
3048 mutex_unlock(lock);
3049 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003050}
3051EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
3052#endif
3053
3054/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
3055
3056void vb2_ops_wait_prepare(struct vb2_queue *vq)
3057{
3058 mutex_unlock(vq->lock);
3059}
3060EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
3061
3062void vb2_ops_wait_finish(struct vb2_queue *vq)
3063{
3064 mutex_lock(vq->lock);
3065}
3066EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
3067
Pawel Osciake23ccc02010-10-11 10:56:41 -03003068MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03003069MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03003070MODULE_LICENSE("GPL");