blob: 4e5004335309b2b93ab81faa675f2d1c7bd53ded [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) {
Hans Verkuil256f3162014-01-29 13:36:53 -0300380 struct vb2_buffer *vb = q->bufs[buffer];
381
382 if (vb && vb->planes[0].mem_priv)
383 call_vb_qop(vb, buf_cleanup, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300384 }
385
386 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300387 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300388
Hans Verkuilb5b45412014-01-29 11:53:25 -0300389#ifdef CONFIG_VIDEO_ADV_DEBUG
390 /*
391 * Check that all the calls were balances during the life-time of this
392 * queue. If not (or if the debug level is 1 or up), then dump the
393 * counters to the kernel log.
394 */
395 if (q->num_buffers) {
396 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
397 q->cnt_wait_prepare != q->cnt_wait_finish;
398
399 if (unbalanced || debug) {
400 pr_info("vb2: counters for queue %p:%s\n", q,
401 unbalanced ? " UNBALANCED!" : "");
402 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
403 q->cnt_queue_setup, q->cnt_start_streaming,
404 q->cnt_stop_streaming);
405 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
406 q->cnt_wait_prepare, q->cnt_wait_finish);
407 }
408 q->cnt_queue_setup = 0;
409 q->cnt_wait_prepare = 0;
410 q->cnt_wait_finish = 0;
411 q->cnt_start_streaming = 0;
412 q->cnt_stop_streaming = 0;
413 }
414 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
415 struct vb2_buffer *vb = q->bufs[buffer];
416 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
417 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
418 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
419 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
420 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
421 vb->cnt_buf_queue != vb->cnt_buf_done ||
422 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
423 vb->cnt_buf_init != vb->cnt_buf_cleanup;
424
425 if (unbalanced || debug) {
426 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
427 q, buffer, unbalanced ? " UNBALANCED!" : "");
428 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
429 vb->cnt_buf_init, vb->cnt_buf_cleanup,
430 vb->cnt_buf_prepare, vb->cnt_buf_finish);
431 pr_info("vb2: buf_queue: %u buf_done: %u\n",
432 vb->cnt_buf_queue, vb->cnt_buf_done);
433 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
434 vb->cnt_mem_alloc, vb->cnt_mem_put,
435 vb->cnt_mem_prepare, vb->cnt_mem_finish,
436 vb->cnt_mem_mmap);
437 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
438 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
439 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
440 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
441 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
442 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
443 vb->cnt_mem_get_dmabuf,
444 vb->cnt_mem_num_users,
445 vb->cnt_mem_vaddr,
446 vb->cnt_mem_cookie);
447 }
448 }
449#endif
450
Pawel Osciake23ccc02010-10-11 10:56:41 -0300451 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300452 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
453 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300454 kfree(q->bufs[buffer]);
455 q->bufs[buffer] = NULL;
456 }
457
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300458 q->num_buffers -= buffers;
Hans Verkuila7afcac2014-02-24 13:41:20 -0300459 if (!q->num_buffers) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300460 q->memory = 0;
Hans Verkuila7afcac2014-02-24 13:41:20 -0300461 INIT_LIST_HEAD(&q->queued_list);
462 }
Hans Verkuil63faabf2013-12-13 13:13:40 -0300463 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300464}
465
466/**
467 * __verify_planes_array() - verify that the planes array passed in struct
468 * v4l2_buffer from userspace can be safely used
469 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300470static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300471{
Hans Verkuil32a77262012-09-28 06:12:53 -0300472 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
473 return 0;
474
Pawel Osciake23ccc02010-10-11 10:56:41 -0300475 /* Is memory for copying plane information present? */
476 if (NULL == b->m.planes) {
477 dprintk(1, "Multi-planar buffer passed but "
478 "planes array not provided\n");
479 return -EINVAL;
480 }
481
482 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
483 dprintk(1, "Incorrect planes array length, "
484 "expected %d, got %d\n", vb->num_planes, b->length);
485 return -EINVAL;
486 }
487
488 return 0;
489}
490
491/**
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300492 * __verify_length() - Verify that the bytesused value for each plane fits in
493 * the plane length and that the data offset doesn't exceed the bytesused value.
494 */
495static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
496{
497 unsigned int length;
498 unsigned int plane;
499
500 if (!V4L2_TYPE_IS_OUTPUT(b->type))
501 return 0;
502
503 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
504 for (plane = 0; plane < vb->num_planes; ++plane) {
505 length = (b->memory == V4L2_MEMORY_USERPTR)
506 ? b->m.planes[plane].length
507 : vb->v4l2_planes[plane].length;
508
509 if (b->m.planes[plane].bytesused > length)
510 return -EINVAL;
Sylwester Nawrocki3c5c23c2013-08-26 11:47:09 -0300511
512 if (b->m.planes[plane].data_offset > 0 &&
513 b->m.planes[plane].data_offset >=
Laurent Pinchart8023ed02012-07-10 10:41:40 -0300514 b->m.planes[plane].bytesused)
515 return -EINVAL;
516 }
517 } else {
518 length = (b->memory == V4L2_MEMORY_USERPTR)
519 ? b->length : vb->v4l2_planes[0].length;
520
521 if (b->bytesused > length)
522 return -EINVAL;
523 }
524
525 return 0;
526}
527
528/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300529 * __buffer_in_use() - return true if the buffer is in use and
530 * the queue cannot be freed (by the means of REQBUFS(0)) call
531 */
532static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
533{
534 unsigned int plane;
535 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300536 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300537 /*
538 * If num_users() has not been provided, call_memop
539 * will return 0, apparently nobody cares about this
540 * case anyway. If num_users() returns more than 1,
541 * we are not the only user of the plane's memory.
542 */
Hans Verkuilb5b45412014-01-29 11:53:25 -0300543 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300544 return true;
545 }
546 return false;
547}
548
549/**
550 * __buffers_in_use() - return true if any buffers on the queue are in use and
551 * the queue cannot be freed (by the means of REQBUFS(0)) call
552 */
553static bool __buffers_in_use(struct vb2_queue *q)
554{
555 unsigned int buffer;
556 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
557 if (__buffer_in_use(q, q->bufs[buffer]))
558 return true;
559 }
560 return false;
561}
562
563/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300564 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
565 * returned to userspace
566 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300567static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300568{
569 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300570
Sakari Ailus2b719d72012-05-02 09:40:03 -0300571 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300572 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300573 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300574 b->reserved = vb->v4l2_buf.reserved;
575
576 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300577 /*
578 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300579 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300580 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300581 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300582 memcpy(b->m.planes, vb->v4l2_planes,
583 b->length * sizeof(struct v4l2_plane));
584 } else {
585 /*
586 * We use length and offset in v4l2_planes array even for
587 * single-planar buffers, but userspace does not.
588 */
589 b->length = vb->v4l2_planes[0].length;
590 b->bytesused = vb->v4l2_planes[0].bytesused;
591 if (q->memory == V4L2_MEMORY_MMAP)
592 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
593 else if (q->memory == V4L2_MEMORY_USERPTR)
594 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300595 else if (q->memory == V4L2_MEMORY_DMABUF)
596 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300597 }
598
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300599 /*
600 * Clear any buffer state related flags.
601 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300602 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -0300603 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
604 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
605 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
606 /*
607 * For non-COPY timestamps, drop timestamp source bits
608 * and obtain the timestamp source from the queue.
609 */
610 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
611 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
612 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300613
614 switch (vb->state) {
615 case VB2_BUF_STATE_QUEUED:
616 case VB2_BUF_STATE_ACTIVE:
617 b->flags |= V4L2_BUF_FLAG_QUEUED;
618 break;
619 case VB2_BUF_STATE_ERROR:
620 b->flags |= V4L2_BUF_FLAG_ERROR;
621 /* fall through */
622 case VB2_BUF_STATE_DONE:
623 b->flags |= V4L2_BUF_FLAG_DONE;
624 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300625 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300626 b->flags |= V4L2_BUF_FLAG_PREPARED;
627 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -0300628 case VB2_BUF_STATE_PREPARING:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300629 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300630 /* nothing */
631 break;
632 }
633
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300634 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300635 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300636}
637
638/**
639 * vb2_querybuf() - query video buffer information
640 * @q: videobuf queue
641 * @b: buffer struct passed from userspace to vidioc_querybuf handler
642 * in driver
643 *
644 * Should be called from vidioc_querybuf ioctl handler in driver.
645 * This function will verify the passed v4l2_buffer structure and fill the
646 * relevant information for the userspace.
647 *
648 * The return values from this function are intended to be directly returned
649 * from vidioc_querybuf handler in driver.
650 */
651int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
652{
653 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300654 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300655
656 if (b->type != q->type) {
657 dprintk(1, "querybuf: wrong buffer type\n");
658 return -EINVAL;
659 }
660
661 if (b->index >= q->num_buffers) {
662 dprintk(1, "querybuf: buffer index out of range\n");
663 return -EINVAL;
664 }
665 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300666 ret = __verify_planes_array(vb, b);
667 if (!ret)
668 __fill_v4l2_buffer(vb, b);
669 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300670}
671EXPORT_SYMBOL(vb2_querybuf);
672
673/**
674 * __verify_userptr_ops() - verify that all memory operations required for
675 * USERPTR queue type have been provided
676 */
677static int __verify_userptr_ops(struct vb2_queue *q)
678{
679 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
680 !q->mem_ops->put_userptr)
681 return -EINVAL;
682
683 return 0;
684}
685
686/**
687 * __verify_mmap_ops() - verify that all memory operations required for
688 * MMAP queue type have been provided
689 */
690static int __verify_mmap_ops(struct vb2_queue *q)
691{
692 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
693 !q->mem_ops->put || !q->mem_ops->mmap)
694 return -EINVAL;
695
696 return 0;
697}
698
699/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300700 * __verify_dmabuf_ops() - verify that all memory operations required for
701 * DMABUF queue type have been provided
702 */
703static int __verify_dmabuf_ops(struct vb2_queue *q)
704{
705 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
706 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
707 !q->mem_ops->unmap_dmabuf)
708 return -EINVAL;
709
710 return 0;
711}
712
713/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300714 * __verify_memory_type() - Check whether the memory type and buffer type
715 * passed to a buffer operation are compatible with the queue.
716 */
717static int __verify_memory_type(struct vb2_queue *q,
718 enum v4l2_memory memory, enum v4l2_buf_type type)
719{
Sumit Semwalc5384042012-06-14 10:37:37 -0300720 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
721 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300722 dprintk(1, "reqbufs: unsupported memory type\n");
723 return -EINVAL;
724 }
725
726 if (type != q->type) {
727 dprintk(1, "reqbufs: requested type is incorrect\n");
728 return -EINVAL;
729 }
730
731 /*
732 * Make sure all the required memory ops for given memory type
733 * are available.
734 */
735 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
736 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
737 return -EINVAL;
738 }
739
740 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
741 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
742 return -EINVAL;
743 }
744
Sumit Semwalc5384042012-06-14 10:37:37 -0300745 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
746 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
747 return -EINVAL;
748 }
749
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300750 /*
751 * Place the busy tests at the end: -EBUSY can be ignored when
752 * create_bufs is called with count == 0, but count == 0 should still
753 * do the memory and type validation.
754 */
755 if (q->fileio) {
756 dprintk(1, "reqbufs: file io in progress\n");
757 return -EBUSY;
758 }
759 return 0;
760}
761
762/**
763 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300764 * @q: videobuf2 queue
765 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
766 *
767 * Should be called from vidioc_reqbufs ioctl handler of a driver.
768 * This function:
769 * 1) verifies streaming parameters passed from the userspace,
770 * 2) sets up the queue,
771 * 3) negotiates number of buffers and planes per buffer with the driver
772 * to be used during streaming,
773 * 4) allocates internal buffer structures (struct vb2_buffer), according to
774 * the agreed parameters,
775 * 5) for MMAP memory type, allocates actual video memory, using the
776 * memory handling/allocation routines provided during queue initialization
777 *
778 * If req->count is 0, all the memory will be freed instead.
779 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
780 * and the queue is not busy, memory will be reallocated.
781 *
782 * The return values from this function are intended to be directly returned
783 * from vidioc_reqbufs handler in driver.
784 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300785static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300786{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300787 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300788 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300789
790 if (q->streaming) {
791 dprintk(1, "reqbufs: streaming active\n");
792 return -EBUSY;
793 }
794
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300795 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300796 /*
797 * We already have buffers allocated, so first check if they
798 * are not in use and can be freed.
799 */
800 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
801 dprintk(1, "reqbufs: memory in use, cannot free\n");
802 return -EBUSY;
803 }
804
Hans Verkuil63faabf2013-12-13 13:13:40 -0300805 ret = __vb2_queue_free(q, q->num_buffers);
806 if (ret)
807 return ret;
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300808
809 /*
810 * In case of REQBUFS(0) return immediately without calling
811 * driver's queue_setup() callback and allocating resources.
812 */
813 if (req->count == 0)
814 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300815 }
816
817 /*
818 * Make sure the requested values and current defaults are sane.
819 */
820 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300821 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300822 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300823 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300824
825 /*
826 * Ask the driver how many buffers and planes per buffer it requires.
827 * Driver also sets the size and allocator context for each plane.
828 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300829 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300830 q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300831 if (ret) {
832 fail_qop(q, queue_setup);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300833 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -0300834 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300835
836 /* Finally, allocate buffers and video memory */
Hans Verkuila7afcac2014-02-24 13:41:20 -0300837 allocated_buffers = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
838 if (allocated_buffers == 0) {
Marek Szyprowski66072d42011-06-28 08:29:02 -0300839 dprintk(1, "Memory allocation failed\n");
840 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300841 }
842
843 /*
844 * Check if driver can handle the allocated number of buffers.
845 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300846 if (allocated_buffers < num_buffers) {
847 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300848
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300849 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
850 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300851 if (ret)
852 fail_qop(q, queue_setup);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300853
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300854 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300855 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300856
857 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300858 * Either the driver has accepted a smaller number of buffers,
859 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300860 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300861 }
862
863 q->num_buffers = allocated_buffers;
864
865 if (ret < 0) {
Hans Verkuila7afcac2014-02-24 13:41:20 -0300866 /*
867 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
868 * from q->num_buffers.
869 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300870 __vb2_queue_free(q, allocated_buffers);
871 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300872 }
873
Pawel Osciake23ccc02010-10-11 10:56:41 -0300874 /*
875 * Return the number of successfully allocated buffers
876 * to the userspace.
877 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300878 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300879
880 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300881}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300882
883/**
884 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
885 * type values.
886 * @q: videobuf2 queue
887 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
888 */
889int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
890{
891 int ret = __verify_memory_type(q, req->memory, req->type);
892
893 return ret ? ret : __reqbufs(q, req);
894}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300895EXPORT_SYMBOL_GPL(vb2_reqbufs);
896
897/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300898 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300899 * @q: videobuf2 queue
900 * @create: creation parameters, passed from userspace to vidioc_create_bufs
901 * handler in driver
902 *
903 * Should be called from vidioc_create_bufs ioctl handler of a driver.
904 * This function:
905 * 1) verifies parameter sanity
906 * 2) calls the .queue_setup() queue operation
907 * 3) performs any necessary memory allocations
908 *
909 * The return values from this function are intended to be directly returned
910 * from vidioc_create_bufs handler in driver.
911 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300912static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300913{
914 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300915 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300916
917 if (q->num_buffers == VIDEO_MAX_FRAME) {
918 dprintk(1, "%s(): maximum number of buffers already allocated\n",
919 __func__);
920 return -ENOBUFS;
921 }
922
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300923 if (!q->num_buffers) {
924 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
925 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
926 q->memory = create->memory;
927 }
928
929 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
930
931 /*
932 * Ask the driver, whether the requested number of buffers, planes per
933 * buffer and their sizes are acceptable
934 */
935 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
936 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300937 if (ret) {
938 fail_qop(q, queue_setup);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300939 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -0300940 }
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300941
942 /* Finally, allocate buffers and video memory */
Hans Verkuila7afcac2014-02-24 13:41:20 -0300943 allocated_buffers = __vb2_queue_alloc(q, create->memory, num_buffers,
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300944 num_planes);
Hans Verkuila7afcac2014-02-24 13:41:20 -0300945 if (allocated_buffers == 0) {
Hans Verkuilf05393d22012-06-22 05:44:14 -0300946 dprintk(1, "Memory allocation failed\n");
947 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300948 }
949
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300950 /*
951 * Check if driver can handle the so far allocated number of buffers.
952 */
Hans Verkuila7afcac2014-02-24 13:41:20 -0300953 if (allocated_buffers < num_buffers) {
954 num_buffers = allocated_buffers;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300955
956 /*
957 * q->num_buffers contains the total number of buffers, that the
958 * queue driver has set up
959 */
960 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
961 &num_planes, q->plane_sizes, q->alloc_ctx);
Hans Verkuilb5b45412014-01-29 11:53:25 -0300962 if (ret)
963 fail_qop(q, queue_setup);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300964
965 if (!ret && allocated_buffers < num_buffers)
966 ret = -ENOMEM;
967
968 /*
969 * Either the driver has accepted a smaller number of buffers,
970 * or .queue_setup() returned an error
971 */
972 }
973
974 q->num_buffers += allocated_buffers;
975
976 if (ret < 0) {
Hans Verkuila7afcac2014-02-24 13:41:20 -0300977 /*
978 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
979 * from q->num_buffers.
980 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300981 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300982 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300983 }
984
985 /*
986 * Return the number of successfully allocated buffers
987 * to the userspace.
988 */
989 create->count = allocated_buffers;
990
991 return 0;
992}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300993
994/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300995 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
996 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300997 * @q: videobuf2 queue
998 * @create: creation parameters, passed from userspace to vidioc_create_bufs
999 * handler in driver
1000 */
1001int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
1002{
1003 int ret = __verify_memory_type(q, create->memory, create->format.type);
1004
1005 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -03001006 if (create->count == 0)
1007 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -03001008 return ret ? ret : __create_bufs(q, create);
1009}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001010EXPORT_SYMBOL_GPL(vb2_create_bufs);
1011
1012/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001013 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
1014 * @vb: vb2_buffer to which the plane in question belongs to
1015 * @plane_no: plane number for which the address is to be returned
1016 *
1017 * This function returns a kernel virtual address of a given plane if
1018 * such a mapping exist, NULL otherwise.
1019 */
1020void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
1021{
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001022 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001023 return NULL;
1024
Hans Verkuilb5b45412014-01-29 11:53:25 -03001025 return call_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001026
1027}
1028EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
1029
1030/**
1031 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
1032 * @vb: vb2_buffer to which the plane in question belongs to
1033 * @plane_no: plane number for which the cookie is to be returned
1034 *
1035 * This function returns an allocator specific cookie for a given plane if
1036 * available, NULL otherwise. The allocator should provide some simple static
1037 * inline function, which would convert this cookie to the allocator specific
1038 * type that can be used directly by the driver to access the buffer. This can
1039 * be for example physical address, pointer to scatter list or IOMMU mapping.
1040 */
1041void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
1042{
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001043 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001044 return NULL;
1045
Hans Verkuilb5b45412014-01-29 11:53:25 -03001046 return call_memop(vb, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001047}
1048EXPORT_SYMBOL_GPL(vb2_plane_cookie);
1049
1050/**
1051 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1052 * @vb: vb2_buffer returned from the driver
1053 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1054 * or VB2_BUF_STATE_ERROR if the operation finished with an error
1055 *
1056 * This function should be called by the driver after a hardware operation on
1057 * a buffer is finished and the buffer may be returned to userspace. The driver
1058 * cannot use this buffer anymore until it is queued back to it by videobuf
1059 * by the means of buf_queue callback. Only buffers previously queued to the
1060 * driver by buf_queue can be passed to this function.
1061 */
1062void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1063{
1064 struct vb2_queue *q = vb->vb2_queue;
1065 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001066 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001067
1068 if (vb->state != VB2_BUF_STATE_ACTIVE)
1069 return;
1070
1071 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
1072 return;
1073
Hans Verkuilb5b45412014-01-29 11:53:25 -03001074#ifdef CONFIG_VIDEO_ADV_DEBUG
1075 /*
1076 * Although this is not a callback, it still does have to balance
1077 * with the buf_queue op. So update this counter manually.
1078 */
1079 vb->cnt_buf_done++;
1080#endif
Pawel Osciake23ccc02010-10-11 10:56:41 -03001081 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -03001082 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001083
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001084 /* sync buffers */
1085 for (plane = 0; plane < vb->num_planes; ++plane)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001086 call_memop(vb, finish, vb->planes[plane].mem_priv);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001087
Pawel Osciake23ccc02010-10-11 10:56:41 -03001088 /* Add the buffer to the done buffers list */
1089 spin_lock_irqsave(&q->done_lock, flags);
1090 vb->state = state;
1091 list_add_tail(&vb->done_entry, &q->done_list);
Hans Verkuil6ea3b982014-02-06 05:46:11 -03001092 atomic_dec(&q->owned_by_drv_count);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001093 spin_unlock_irqrestore(&q->done_lock, flags);
1094
1095 /* Inform any processes that may be waiting for buffers */
1096 wake_up(&q->done_wq);
1097}
1098EXPORT_SYMBOL_GPL(vb2_buffer_done);
1099
1100/**
Hans Verkuil32a77262012-09-28 06:12:53 -03001101 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1102 * v4l2_buffer by the userspace. The caller has already verified that struct
1103 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -03001104 */
Hans Verkuil32a77262012-09-28 06:12:53 -03001105static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001106 struct v4l2_plane *v4l2_planes)
1107{
1108 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001109
1110 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001111 /* Fill in driver-provided information for OUTPUT types */
1112 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1113 /*
1114 * Will have to go up to b->length when API starts
1115 * accepting variable number of planes.
1116 */
1117 for (plane = 0; plane < vb->num_planes; ++plane) {
1118 v4l2_planes[plane].bytesused =
1119 b->m.planes[plane].bytesused;
1120 v4l2_planes[plane].data_offset =
1121 b->m.planes[plane].data_offset;
1122 }
1123 }
1124
1125 if (b->memory == V4L2_MEMORY_USERPTR) {
1126 for (plane = 0; plane < vb->num_planes; ++plane) {
1127 v4l2_planes[plane].m.userptr =
1128 b->m.planes[plane].m.userptr;
1129 v4l2_planes[plane].length =
1130 b->m.planes[plane].length;
1131 }
1132 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001133 if (b->memory == V4L2_MEMORY_DMABUF) {
1134 for (plane = 0; plane < vb->num_planes; ++plane) {
1135 v4l2_planes[plane].m.fd =
1136 b->m.planes[plane].m.fd;
1137 v4l2_planes[plane].length =
1138 b->m.planes[plane].length;
1139 v4l2_planes[plane].data_offset =
1140 b->m.planes[plane].data_offset;
1141 }
1142 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001143 } else {
1144 /*
1145 * Single-planar buffers do not use planes array,
1146 * so fill in relevant v4l2_buffer struct fields instead.
1147 * In videobuf we use our internal V4l2_planes struct for
1148 * single-planar buffers as well, for simplicity.
1149 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001150 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -03001151 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -03001152 v4l2_planes[0].data_offset = 0;
1153 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001154
1155 if (b->memory == V4L2_MEMORY_USERPTR) {
1156 v4l2_planes[0].m.userptr = b->m.userptr;
1157 v4l2_planes[0].length = b->length;
1158 }
Sumit Semwalc5384042012-06-14 10:37:37 -03001159
1160 if (b->memory == V4L2_MEMORY_DMABUF) {
1161 v4l2_planes[0].m.fd = b->m.fd;
1162 v4l2_planes[0].length = b->length;
1163 v4l2_planes[0].data_offset = 0;
1164 }
1165
Pawel Osciake23ccc02010-10-11 10:56:41 -03001166 }
1167
Hans Verkuilf1343282014-02-24 14:44:50 -03001168 /* Zero flags that the vb2 core handles */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -03001169 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Sakari Ailus7ce6fd82014-02-25 19:08:52 -03001170 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
1171 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
1172 /*
1173 * Non-COPY timestamps and non-OUTPUT queues will get
1174 * their timestamp and timestamp source flags from the
1175 * queue.
1176 */
1177 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1178 }
1179
Hans Verkuilf1343282014-02-24 14:44:50 -03001180 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
1181 /*
1182 * For output buffers mask out the timecode flag:
1183 * this will be handled later in vb2_internal_qbuf().
1184 * The 'field' is valid metadata for this output buffer
1185 * and so that needs to be copied here.
1186 */
1187 vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TIMECODE;
1188 vb->v4l2_buf.field = b->field;
1189 } else {
1190 /* Zero any output buffer flags as this is a capture buffer */
1191 vb->v4l2_buf.flags &= ~V4L2_BUFFER_OUT_FLAGS;
1192 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001193}
1194
1195/**
1196 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1197 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001198static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001199{
1200 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1201 struct vb2_queue *q = vb->vb2_queue;
1202 void *mem_priv;
1203 unsigned int plane;
1204 int ret;
1205 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
Hans Verkuil256f3162014-01-29 13:36:53 -03001206 bool reacquired = vb->planes[0].mem_priv == NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001207
Hans Verkuil32a77262012-09-28 06:12:53 -03001208 /* Copy relevant information provided by the userspace */
1209 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001210
1211 for (plane = 0; plane < vb->num_planes; ++plane) {
1212 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -03001213 if (vb->v4l2_planes[plane].m.userptr &&
1214 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -03001215 && vb->v4l2_planes[plane].length == planes[plane].length)
1216 continue;
1217
1218 dprintk(3, "qbuf: userspace address for plane %d changed, "
1219 "reacquiring memory\n", plane);
1220
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001221 /* Check if the provided plane buffer is large enough */
1222 if (planes[plane].length < q->plane_sizes[plane]) {
Seung-Woo Kim2484a7e2013-08-20 04:48:06 -03001223 dprintk(1, "qbuf: provided buffer size %u is less than "
1224 "setup size %u for plane %d\n",
1225 planes[plane].length,
1226 q->plane_sizes[plane], plane);
Marek Szyprowski4c2625d2011-10-03 03:21:45 -03001227 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001228 goto err;
1229 }
1230
Pawel Osciake23ccc02010-10-11 10:56:41 -03001231 /* Release previously acquired memory if present */
Hans Verkuil256f3162014-01-29 13:36:53 -03001232 if (vb->planes[plane].mem_priv) {
1233 if (!reacquired) {
1234 reacquired = true;
1235 call_vb_qop(vb, buf_cleanup, vb);
1236 }
Hans Verkuilb5b45412014-01-29 11:53:25 -03001237 call_memop(vb, put_userptr, vb->planes[plane].mem_priv);
Hans Verkuil256f3162014-01-29 13:36:53 -03001238 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001239
1240 vb->planes[plane].mem_priv = NULL;
Hans Verkuil256f3162014-01-29 13:36:53 -03001241 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
Pawel Osciake23ccc02010-10-11 10:56:41 -03001242
1243 /* Acquire each plane's memory */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001244 mem_priv = call_memop(vb, get_userptr, q->alloc_ctx[plane],
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001245 planes[plane].m.userptr,
1246 planes[plane].length, write);
1247 if (IS_ERR_OR_NULL(mem_priv)) {
1248 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -03001249 "memory for plane %d\n", plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03001250 fail_memop(vb, get_userptr);
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001251 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1252 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001253 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001254 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001255 }
1256
1257 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001258 * Now that everything is in order, copy relevant information
1259 * provided by userspace.
1260 */
1261 for (plane = 0; plane < vb->num_planes; ++plane)
1262 vb->v4l2_planes[plane] = planes[plane];
1263
Hans Verkuil256f3162014-01-29 13:36:53 -03001264 if (reacquired) {
1265 /*
1266 * One or more planes changed, so we must call buf_init to do
1267 * the driver-specific initialization on the newly acquired
1268 * buffer, if provided.
1269 */
1270 ret = call_vb_qop(vb, buf_init, vb);
1271 if (ret) {
1272 dprintk(1, "qbuf: buffer initialization failed\n");
1273 fail_vb_qop(vb, buf_init);
1274 goto err;
1275 }
1276 }
1277
1278 ret = call_vb_qop(vb, buf_prepare, vb);
1279 if (ret) {
1280 dprintk(1, "qbuf: buffer preparation failed\n");
1281 fail_vb_qop(vb, buf_prepare);
1282 call_vb_qop(vb, buf_cleanup, vb);
1283 goto err;
1284 }
1285
Pawel Osciake23ccc02010-10-11 10:56:41 -03001286 return 0;
1287err:
1288 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001289 for (plane = 0; plane < vb->num_planes; ++plane) {
1290 if (vb->planes[plane].mem_priv)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001291 call_memop(vb, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001292 vb->planes[plane].mem_priv = NULL;
1293 vb->v4l2_planes[plane].m.userptr = 0;
1294 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001295 }
1296
1297 return ret;
1298}
1299
1300/**
1301 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1302 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001303static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001304{
Hans Verkuil256f3162014-01-29 13:36:53 -03001305 int ret;
1306
Hans Verkuil32a77262012-09-28 06:12:53 -03001307 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
Hans Verkuil256f3162014-01-29 13:36:53 -03001308 ret = call_vb_qop(vb, buf_prepare, vb);
1309 if (ret)
1310 fail_vb_qop(vb, buf_prepare);
1311 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001312}
1313
1314/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001315 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1316 */
1317static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1318{
1319 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1320 struct vb2_queue *q = vb->vb2_queue;
1321 void *mem_priv;
1322 unsigned int plane;
1323 int ret;
1324 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
Hans Verkuil256f3162014-01-29 13:36:53 -03001325 bool reacquired = vb->planes[0].mem_priv == NULL;
Sumit Semwalc5384042012-06-14 10:37:37 -03001326
Laurent Pinchart6f546c52014-01-01 09:10:48 -03001327 /* Copy relevant information provided by the userspace */
Sumit Semwalc5384042012-06-14 10:37:37 -03001328 __fill_vb2_buffer(vb, b, planes);
1329
1330 for (plane = 0; plane < vb->num_planes; ++plane) {
1331 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1332
1333 if (IS_ERR_OR_NULL(dbuf)) {
1334 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1335 plane);
1336 ret = -EINVAL;
1337 goto err;
1338 }
1339
1340 /* use DMABUF size if length is not provided */
1341 if (planes[plane].length == 0)
1342 planes[plane].length = dbuf->size;
1343
1344 if (planes[plane].length < planes[plane].data_offset +
1345 q->plane_sizes[plane]) {
Seung-Woo Kim77c07822013-11-29 04:50:29 -03001346 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1347 plane);
Sumit Semwalc5384042012-06-14 10:37:37 -03001348 ret = -EINVAL;
1349 goto err;
1350 }
1351
1352 /* Skip the plane if already verified */
1353 if (dbuf == vb->planes[plane].dbuf &&
1354 vb->v4l2_planes[plane].length == planes[plane].length) {
1355 dma_buf_put(dbuf);
1356 continue;
1357 }
1358
1359 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1360
Hans Verkuil256f3162014-01-29 13:36:53 -03001361 if (!reacquired) {
1362 reacquired = true;
1363 call_vb_qop(vb, buf_cleanup, vb);
1364 }
1365
Sumit Semwalc5384042012-06-14 10:37:37 -03001366 /* Release previously acquired memory if present */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001367 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
Sumit Semwalc5384042012-06-14 10:37:37 -03001368 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1369
1370 /* Acquire each plane's memory */
Hans Verkuilb5b45412014-01-29 11:53:25 -03001371 mem_priv = call_memop(vb, attach_dmabuf, q->alloc_ctx[plane],
Sumit Semwalc5384042012-06-14 10:37:37 -03001372 dbuf, planes[plane].length, write);
1373 if (IS_ERR(mem_priv)) {
1374 dprintk(1, "qbuf: failed to attach dmabuf\n");
Hans Verkuilb5b45412014-01-29 11:53:25 -03001375 fail_memop(vb, attach_dmabuf);
Sumit Semwalc5384042012-06-14 10:37:37 -03001376 ret = PTR_ERR(mem_priv);
1377 dma_buf_put(dbuf);
1378 goto err;
1379 }
1380
1381 vb->planes[plane].dbuf = dbuf;
1382 vb->planes[plane].mem_priv = mem_priv;
1383 }
1384
1385 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1386 * really we want to do this just before the DMA, not while queueing
1387 * the buffer(s)..
1388 */
1389 for (plane = 0; plane < vb->num_planes; ++plane) {
Hans Verkuilb5b45412014-01-29 11:53:25 -03001390 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -03001391 if (ret) {
1392 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1393 plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03001394 fail_memop(vb, map_dmabuf);
Sumit Semwalc5384042012-06-14 10:37:37 -03001395 goto err;
1396 }
1397 vb->planes[plane].dbuf_mapped = 1;
1398 }
1399
1400 /*
Sumit Semwalc5384042012-06-14 10:37:37 -03001401 * Now that everything is in order, copy relevant information
1402 * provided by userspace.
1403 */
1404 for (plane = 0; plane < vb->num_planes; ++plane)
1405 vb->v4l2_planes[plane] = planes[plane];
1406
Hans Verkuil256f3162014-01-29 13:36:53 -03001407 if (reacquired) {
1408 /*
1409 * Call driver-specific initialization on the newly acquired buffer,
1410 * if provided.
1411 */
1412 ret = call_vb_qop(vb, buf_init, vb);
1413 if (ret) {
1414 dprintk(1, "qbuf: buffer initialization failed\n");
1415 fail_vb_qop(vb, buf_init);
1416 goto err;
1417 }
1418 }
1419
1420 ret = call_vb_qop(vb, buf_prepare, vb);
1421 if (ret) {
1422 dprintk(1, "qbuf: buffer preparation failed\n");
1423 fail_vb_qop(vb, buf_prepare);
1424 call_vb_qop(vb, buf_cleanup, vb);
1425 goto err;
1426 }
1427
Sumit Semwalc5384042012-06-14 10:37:37 -03001428 return 0;
1429err:
1430 /* In case of errors, release planes that were already acquired */
1431 __vb2_buf_dmabuf_put(vb);
1432
1433 return ret;
1434}
1435
1436/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001437 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1438 */
1439static void __enqueue_in_driver(struct vb2_buffer *vb)
1440{
1441 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001442 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001443
1444 vb->state = VB2_BUF_STATE_ACTIVE;
Hans Verkuil6ea3b982014-02-06 05:46:11 -03001445 atomic_inc(&q->owned_by_drv_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001446
1447 /* sync buffers */
1448 for (plane = 0; plane < vb->num_planes; ++plane)
Hans Verkuilb5b45412014-01-29 11:53:25 -03001449 call_memop(vb, prepare, vb->planes[plane].mem_priv);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001450
Hans Verkuilb5b45412014-01-29 11:53:25 -03001451 call_vb_qop(vb, buf_queue, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001452}
1453
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001454static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001455{
1456 struct vb2_queue *q = vb->vb2_queue;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001457 struct rw_semaphore *mmap_sem;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001458 int ret;
1459
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001460 ret = __verify_length(vb, b);
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001461 if (ret < 0) {
1462 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1463 __func__, ret);
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001464 return ret;
Sylwester Nawrocki3a9621b2013-08-26 11:47:53 -03001465 }
Laurent Pinchart8023ed02012-07-10 10:41:40 -03001466
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001467 vb->state = VB2_BUF_STATE_PREPARING;
Hans Verkuilf1343282014-02-24 14:44:50 -03001468 vb->v4l2_buf.timestamp.tv_sec = 0;
1469 vb->v4l2_buf.timestamp.tv_usec = 0;
1470 vb->v4l2_buf.sequence = 0;
1471
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001472 switch (q->memory) {
1473 case V4L2_MEMORY_MMAP:
1474 ret = __qbuf_mmap(vb, b);
1475 break;
1476 case V4L2_MEMORY_USERPTR:
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001477 /*
Mauro Carvalho Chehabf103b5d2014-01-07 07:03:09 -02001478 * In case of user pointer buffers vb2 allocators need to get
1479 * direct access to userspace pages. This requires getting
1480 * the mmap semaphore for read access in the current process
1481 * structure. The same semaphore is taken before calling mmap
1482 * operation, while both qbuf/prepare_buf and mmap are called
1483 * by the driver or v4l2 core with the driver's lock held.
1484 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1485 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1486 * the videobuf2 core releases the driver's lock, takes
1487 * mmap_sem and then takes the driver's lock again.
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001488 */
1489 mmap_sem = &current->mm->mmap_sem;
1490 call_qop(q, wait_prepare, q);
1491 down_read(mmap_sem);
1492 call_qop(q, wait_finish, q);
1493
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001494 ret = __qbuf_userptr(vb, b);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001495
1496 up_read(mmap_sem);
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001497 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001498 case V4L2_MEMORY_DMABUF:
1499 ret = __qbuf_dmabuf(vb, b);
1500 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001501 default:
1502 WARN(1, "Invalid queue type\n");
1503 ret = -EINVAL;
1504 }
1505
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001506 if (ret)
1507 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001508 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001509
1510 return ret;
1511}
1512
Laurent Pinchart012043b2013-08-09 08:11:26 -03001513static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
Hans Verkuil41381112013-12-13 13:13:39 -03001514 const char *opname)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001515{
Laurent Pinchart012043b2013-08-09 08:11:26 -03001516 if (b->type != q->type) {
1517 dprintk(1, "%s(): invalid buffer type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001518 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001519 }
1520
1521 if (b->index >= q->num_buffers) {
1522 dprintk(1, "%s(): buffer index out of range\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001523 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001524 }
1525
Hans Verkuil41381112013-12-13 13:13:39 -03001526 if (q->bufs[b->index] == NULL) {
Laurent Pinchart012043b2013-08-09 08:11:26 -03001527 /* Should never happen */
1528 dprintk(1, "%s(): buffer is NULL\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001529 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001530 }
1531
1532 if (b->memory != q->memory) {
1533 dprintk(1, "%s(): invalid memory type\n", opname);
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001534 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001535 }
1536
Hans Verkuil41381112013-12-13 13:13:39 -03001537 return __verify_planes_array(q->bufs[b->index], b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001538}
1539
Pawel Osciake23ccc02010-10-11 10:56:41 -03001540/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001541 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1542 * @q: videobuf2 queue
1543 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1544 * handler in driver
1545 *
1546 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1547 * This function:
1548 * 1) verifies the passed buffer,
1549 * 2) calls buf_prepare callback in the driver (if provided), in which
1550 * driver-specific buffer initialization can be performed,
1551 *
1552 * The return values from this function are intended to be directly returned
1553 * from vidioc_prepare_buf handler in driver.
1554 */
1555int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1556{
Hans Verkuil41381112013-12-13 13:13:39 -03001557 struct vb2_buffer *vb;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001558 int ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001559
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001560 if (q->fileio) {
1561 dprintk(1, "%s(): file io in progress\n", __func__);
1562 return -EBUSY;
1563 }
1564
1565 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
Hans Verkuil41381112013-12-13 13:13:39 -03001566 if (ret)
1567 return ret;
1568
1569 vb = q->bufs[b->index];
1570 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1571 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1572 vb->state);
1573 return -EINVAL;
1574 }
1575
1576 ret = __buf_prepare(vb, b);
1577 if (!ret) {
1578 /* Fill buffer information for the userspace */
1579 __fill_v4l2_buffer(vb, b);
1580
1581 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1582 }
1583 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001584}
1585EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1586
Hans Verkuil02f142e2013-12-13 13:13:42 -03001587/**
1588 * vb2_start_streaming() - Attempt to start streaming.
1589 * @q: videobuf2 queue
1590 *
1591 * If there are not enough buffers, then retry_start_streaming is set to
1592 * 1 and 0 is returned. The next time a buffer is queued and
1593 * retry_start_streaming is 1, this function will be called again to
1594 * retry starting the DMA engine.
1595 */
1596static int vb2_start_streaming(struct vb2_queue *q)
1597{
1598 int ret;
1599
1600 /* Tell the driver to start streaming */
Hans Verkuil6ea3b982014-02-06 05:46:11 -03001601 ret = call_qop(q, start_streaming, q, atomic_read(&q->owned_by_drv_count));
Hans Verkuilb5b45412014-01-29 11:53:25 -03001602 if (ret)
1603 fail_qop(q, start_streaming);
Hans Verkuil02f142e2013-12-13 13:13:42 -03001604
1605 /*
1606 * If there are not enough buffers queued to start streaming, then
1607 * the start_streaming operation will return -ENOBUFS and you have to
1608 * retry when the next buffer is queued.
1609 */
1610 if (ret == -ENOBUFS) {
1611 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1612 q->retry_start_streaming = 1;
1613 return 0;
1614 }
1615 if (ret)
1616 dprintk(1, "qbuf: driver refused to start streaming\n");
1617 else
1618 q->retry_start_streaming = 0;
1619 return ret;
1620}
1621
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001622static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
Laurent Pinchart012043b2013-08-09 08:11:26 -03001623{
Hans Verkuil41381112013-12-13 13:13:39 -03001624 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
1625 struct vb2_buffer *vb;
1626
1627 if (ret)
1628 return ret;
1629
1630 vb = q->bufs[b->index];
Laurent Pinchart012043b2013-08-09 08:11:26 -03001631
1632 switch (vb->state) {
1633 case VB2_BUF_STATE_DEQUEUED:
1634 ret = __buf_prepare(vb, b);
1635 if (ret)
1636 return ret;
Hans Verkuil41381112013-12-13 13:13:39 -03001637 break;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001638 case VB2_BUF_STATE_PREPARED:
1639 break;
Hans Verkuilb18a8ff2013-12-13 13:13:38 -03001640 case VB2_BUF_STATE_PREPARING:
1641 dprintk(1, "qbuf: buffer still being prepared\n");
1642 return -EINVAL;
Laurent Pinchart012043b2013-08-09 08:11:26 -03001643 default:
Hans Verkuil952c9ee2014-02-10 13:12:00 -03001644 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1645 vb->state);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001646 return -EINVAL;
1647 }
1648
1649 /*
1650 * Add to the queued buffers list, a buffer will stay on it until
1651 * dequeued in dqbuf.
1652 */
1653 list_add_tail(&vb->queued_entry, &q->queued_list);
1654 vb->state = VB2_BUF_STATE_QUEUED;
Hans Verkuilf1343282014-02-24 14:44:50 -03001655 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1656 /*
1657 * For output buffers copy the timestamp if needed,
1658 * and the timecode field and flag if needed.
1659 */
Sakari Ailusc57ff792014-03-01 10:28:02 -03001660 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1661 V4L2_BUF_FLAG_TIMESTAMP_COPY)
Hans Verkuilf1343282014-02-24 14:44:50 -03001662 vb->v4l2_buf.timestamp = b->timestamp;
1663 vb->v4l2_buf.flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
1664 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
1665 vb->v4l2_buf.timecode = b->timecode;
1666 }
Laurent Pinchart012043b2013-08-09 08:11:26 -03001667
1668 /*
1669 * If already streaming, give the buffer to driver for processing.
1670 * If not, the buffer will be given to driver on next streamon.
1671 */
1672 if (q->streaming)
1673 __enqueue_in_driver(vb);
1674
Hans Verkuil41381112013-12-13 13:13:39 -03001675 /* Fill buffer information for the userspace */
1676 __fill_v4l2_buffer(vb, b);
Laurent Pinchart012043b2013-08-09 08:11:26 -03001677
Hans Verkuil02f142e2013-12-13 13:13:42 -03001678 if (q->retry_start_streaming) {
1679 ret = vb2_start_streaming(q);
1680 if (ret)
1681 return ret;
1682 }
1683
Hans Verkuil41381112013-12-13 13:13:39 -03001684 dprintk(1, "%s() of buffer %d succeeded\n", __func__, vb->v4l2_buf.index);
1685 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001686}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001687
1688/**
1689 * vb2_qbuf() - Queue a buffer from userspace
1690 * @q: videobuf2 queue
1691 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1692 * in driver
1693 *
1694 * Should be called from vidioc_qbuf ioctl handler of a driver.
1695 * This function:
1696 * 1) verifies the passed buffer,
1697 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1698 * which driver-specific buffer initialization can be performed,
1699 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1700 * callback for processing.
1701 *
1702 * The return values from this function are intended to be directly returned
1703 * from vidioc_qbuf handler in driver.
1704 */
1705int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1706{
1707 if (q->fileio) {
1708 dprintk(1, "%s(): file io in progress\n", __func__);
1709 return -EBUSY;
1710 }
1711
1712 return vb2_internal_qbuf(q, b);
1713}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001714EXPORT_SYMBOL_GPL(vb2_qbuf);
1715
1716/**
1717 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1718 * for dequeuing
1719 *
1720 * Will sleep if required for nonblocking == false.
1721 */
1722static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1723{
1724 /*
1725 * All operations on vb_done_list are performed under done_lock
1726 * spinlock protection. However, buffers may be removed from
1727 * it and returned to userspace only while holding both driver's
1728 * lock and the done_lock spinlock. Thus we can be sure that as
1729 * long as we hold the driver's lock, the list will remain not
1730 * empty if list_empty() check succeeds.
1731 */
1732
1733 for (;;) {
1734 int ret;
1735
1736 if (!q->streaming) {
1737 dprintk(1, "Streaming off, will not wait for buffers\n");
1738 return -EINVAL;
1739 }
1740
1741 if (!list_empty(&q->done_list)) {
1742 /*
1743 * Found a buffer that we were waiting for.
1744 */
1745 break;
1746 }
1747
1748 if (nonblocking) {
1749 dprintk(1, "Nonblocking and no buffers to dequeue, "
1750 "will not wait\n");
1751 return -EAGAIN;
1752 }
1753
1754 /*
1755 * We are streaming and blocking, wait for another buffer to
1756 * become ready or for streamoff. Driver's lock is released to
1757 * allow streamoff or qbuf to be called while waiting.
1758 */
1759 call_qop(q, wait_prepare, q);
1760
1761 /*
1762 * All locks have been released, it is safe to sleep now.
1763 */
1764 dprintk(3, "Will sleep waiting for buffers\n");
1765 ret = wait_event_interruptible(q->done_wq,
1766 !list_empty(&q->done_list) || !q->streaming);
1767
1768 /*
1769 * We need to reevaluate both conditions again after reacquiring
1770 * the locks or return an error if one occurred.
1771 */
1772 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001773 if (ret) {
1774 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001775 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001776 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001777 }
1778 return 0;
1779}
1780
1781/**
1782 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1783 *
1784 * Will sleep if required for nonblocking == false.
1785 */
1786static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001787 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001788{
1789 unsigned long flags;
1790 int ret;
1791
1792 /*
1793 * Wait for at least one buffer to become available on the done_list.
1794 */
1795 ret = __vb2_wait_for_done_vb(q, nonblocking);
1796 if (ret)
1797 return ret;
1798
1799 /*
1800 * Driver's lock has been held since we last verified that done_list
1801 * is not empty, so no need for another list_empty(done_list) check.
1802 */
1803 spin_lock_irqsave(&q->done_lock, flags);
1804 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001805 /*
1806 * Only remove the buffer from done_list if v4l2_buffer can handle all
1807 * the planes.
1808 */
1809 ret = __verify_planes_array(*vb, b);
1810 if (!ret)
1811 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001812 spin_unlock_irqrestore(&q->done_lock, flags);
1813
Hans Verkuil32a77262012-09-28 06:12:53 -03001814 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001815}
1816
1817/**
1818 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1819 * @q: videobuf2 queue
1820 *
1821 * This function will wait until all buffers that have been given to the driver
1822 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1823 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1824 * taken, for example from stop_streaming() callback.
1825 */
1826int vb2_wait_for_all_buffers(struct vb2_queue *q)
1827{
1828 if (!q->streaming) {
1829 dprintk(1, "Streaming off, will not wait for buffers\n");
1830 return -EINVAL;
1831 }
1832
Hans Verkuil02f142e2013-12-13 13:13:42 -03001833 if (!q->retry_start_streaming)
Hans Verkuil6ea3b982014-02-06 05:46:11 -03001834 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
Pawel Osciake23ccc02010-10-11 10:56:41 -03001835 return 0;
1836}
1837EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1838
1839/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001840 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1841 */
1842static void __vb2_dqbuf(struct vb2_buffer *vb)
1843{
1844 struct vb2_queue *q = vb->vb2_queue;
1845 unsigned int i;
1846
1847 /* nothing to do if the buffer is already dequeued */
1848 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1849 return;
1850
1851 vb->state = VB2_BUF_STATE_DEQUEUED;
1852
1853 /* unmap DMABUF buffer */
1854 if (q->memory == V4L2_MEMORY_DMABUF)
1855 for (i = 0; i < vb->num_planes; ++i) {
1856 if (!vb->planes[i].dbuf_mapped)
1857 continue;
Hans Verkuilb5b45412014-01-29 11:53:25 -03001858 call_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
Sumit Semwalc5384042012-06-14 10:37:37 -03001859 vb->planes[i].dbuf_mapped = 0;
1860 }
1861}
1862
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001863static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001864{
1865 struct vb2_buffer *vb = NULL;
1866 int ret;
1867
1868 if (b->type != q->type) {
1869 dprintk(1, "dqbuf: invalid buffer type\n");
1870 return -EINVAL;
1871 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001872 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1873 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001874 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001875
Hans Verkuil06470642014-03-04 07:27:13 -03001876 call_vb_qop(vb, buf_finish, vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001877
1878 switch (vb->state) {
1879 case VB2_BUF_STATE_DONE:
1880 dprintk(3, "dqbuf: Returning done buffer\n");
1881 break;
1882 case VB2_BUF_STATE_ERROR:
1883 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1884 break;
1885 default:
1886 dprintk(1, "dqbuf: Invalid buffer state\n");
1887 return -EINVAL;
1888 }
1889
1890 /* Fill buffer information for the userspace */
1891 __fill_v4l2_buffer(vb, b);
1892 /* Remove from videobuf queue */
1893 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001894 /* go back to dequeued state */
1895 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001896
1897 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1898 vb->v4l2_buf.index, vb->state);
1899
Pawel Osciake23ccc02010-10-11 10:56:41 -03001900 return 0;
1901}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001902
1903/**
1904 * vb2_dqbuf() - Dequeue a buffer to the userspace
1905 * @q: videobuf2 queue
1906 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1907 * in driver
1908 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1909 * buffers ready for dequeuing are present. Normally the driver
1910 * would be passing (file->f_flags & O_NONBLOCK) here
1911 *
1912 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1913 * This function:
1914 * 1) verifies the passed buffer,
1915 * 2) calls buf_finish callback in the driver (if provided), in which
1916 * driver can perform any additional operations that may be required before
1917 * returning the buffer to userspace, such as cache sync,
1918 * 3) the buffer struct members are filled with relevant information for
1919 * the userspace.
1920 *
1921 * The return values from this function are intended to be directly returned
1922 * from vidioc_dqbuf handler in driver.
1923 */
1924int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1925{
1926 if (q->fileio) {
1927 dprintk(1, "dqbuf: file io in progress\n");
1928 return -EBUSY;
1929 }
1930 return vb2_internal_dqbuf(q, b, nonblocking);
1931}
Pawel Osciake23ccc02010-10-11 10:56:41 -03001932EXPORT_SYMBOL_GPL(vb2_dqbuf);
1933
1934/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001935 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1936 *
1937 * Removes all queued buffers from driver's queue and all buffers queued by
1938 * userspace from videobuf's queue. Returns to state after reqbufs.
1939 */
1940static void __vb2_queue_cancel(struct vb2_queue *q)
1941{
1942 unsigned int i;
1943
Hans Verkuil02f142e2013-12-13 13:13:42 -03001944 if (q->retry_start_streaming) {
1945 q->retry_start_streaming = 0;
1946 q->streaming = 0;
1947 }
1948
Pawel Osciake23ccc02010-10-11 10:56:41 -03001949 /*
1950 * Tell driver to stop all transactions and release all queued
1951 * buffers.
1952 */
1953 if (q->streaming)
1954 call_qop(q, stop_streaming, q);
1955 q->streaming = 0;
1956
1957 /*
1958 * Remove all buffers from videobuf's list...
1959 */
1960 INIT_LIST_HEAD(&q->queued_list);
1961 /*
1962 * ...and done list; userspace will not receive any buffers it
1963 * has not already dequeued before initiating cancel.
1964 */
1965 INIT_LIST_HEAD(&q->done_list);
Hans Verkuil6ea3b982014-02-06 05:46:11 -03001966 atomic_set(&q->owned_by_drv_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001967 wake_up_all(&q->done_wq);
1968
1969 /*
1970 * Reinitialize all buffers for next use.
Hans Verkuil9c0863b2014-03-04 07:34:49 -03001971 * Make sure to call buf_finish for any queued buffers. Normally
1972 * that's done in dqbuf, but that's not going to happen when we
1973 * cancel the whole queue. Note: this code belongs here, not in
1974 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1975 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1976 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
Pawel Osciake23ccc02010-10-11 10:56:41 -03001977 */
Hans Verkuil9c0863b2014-03-04 07:34:49 -03001978 for (i = 0; i < q->num_buffers; ++i) {
1979 struct vb2_buffer *vb = q->bufs[i];
1980
1981 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1982 vb->state = VB2_BUF_STATE_PREPARED;
1983 call_vb_qop(vb, buf_finish, vb);
1984 }
1985 __vb2_dqbuf(vb);
1986 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001987}
1988
Hans Verkuilb2f2f042013-12-13 13:13:41 -03001989static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001990{
1991 struct vb2_buffer *vb;
1992 int ret;
1993
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001994 if (type != q->type) {
1995 dprintk(1, "streamon: invalid stream type\n");
1996 return -EINVAL;
1997 }
1998
1999 if (q->streaming) {
Ricardo Ribaldaf9560352013-11-08 07:08:45 -03002000 dprintk(3, "streamon successful: already streaming\n");
2001 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002002 }
2003
Ricardo Ribalda548df782014-01-08 05:01:33 -03002004 if (!q->num_buffers) {
2005 dprintk(1, "streamon: no buffers have been allocated\n");
2006 return -EINVAL;
2007 }
2008
Ricardo Ribalda Delgado249f5a52014-01-08 05:01:33 -03002009 if (!q->num_buffers) {
2010 dprintk(1, "streamon: no buffers have been allocated\n");
2011 return -EINVAL;
2012 }
2013
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002014 /*
2015 * If any buffers were queued before streamon,
2016 * we can now pass them to driver for processing.
2017 */
2018 list_for_each_entry(vb, &q->queued_list, queued_entry)
2019 __enqueue_in_driver(vb);
2020
Hans Verkuil02f142e2013-12-13 13:13:42 -03002021 /* Tell driver to start streaming. */
2022 ret = vb2_start_streaming(q);
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002023 if (ret) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002024 __vb2_queue_cancel(q);
2025 return ret;
2026 }
2027
2028 q->streaming = 1;
2029
2030 dprintk(3, "Streamon successful\n");
2031 return 0;
2032}
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002033
2034/**
2035 * vb2_streamon - start streaming
2036 * @q: videobuf2 queue
2037 * @type: type argument passed from userspace to vidioc_streamon handler
2038 *
2039 * Should be called from vidioc_streamon handler of a driver.
2040 * This function:
2041 * 1) verifies current state
2042 * 2) passes any previously queued buffers to the driver and starts streaming
2043 *
2044 * The return values from this function are intended to be directly returned
2045 * from vidioc_streamon handler in the driver.
2046 */
2047int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
2048{
2049 if (q->fileio) {
2050 dprintk(1, "streamon: file io in progress\n");
2051 return -EBUSY;
2052 }
2053 return vb2_internal_streamon(q, type);
2054}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002055EXPORT_SYMBOL_GPL(vb2_streamon);
2056
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002057static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
2058{
2059 if (type != q->type) {
2060 dprintk(1, "streamoff: invalid stream type\n");
2061 return -EINVAL;
2062 }
2063
2064 if (!q->streaming) {
2065 dprintk(3, "streamoff successful: not streaming\n");
2066 return 0;
2067 }
2068
2069 /*
2070 * Cancel will pause streaming and remove all buffers from the driver
2071 * and videobuf, effectively returning control over them to userspace.
2072 */
2073 __vb2_queue_cancel(q);
2074
2075 dprintk(3, "Streamoff successful\n");
2076 return 0;
2077}
Marek Szyprowskibd323e22011-08-29 08:51:49 -03002078
2079/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002080 * vb2_streamoff - stop streaming
2081 * @q: videobuf2 queue
2082 * @type: type argument passed from userspace to vidioc_streamoff handler
2083 *
2084 * Should be called from vidioc_streamoff handler of a driver.
2085 * This function:
2086 * 1) verifies current state,
2087 * 2) stop streaming and dequeues any queued buffers, including those previously
2088 * passed to the driver (after waiting for the driver to finish).
2089 *
2090 * This call can be used for pausing playback.
2091 * The return values from this function are intended to be directly returned
2092 * from vidioc_streamoff handler in the driver
2093 */
2094int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
2095{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002096 if (q->fileio) {
2097 dprintk(1, "streamoff: file io in progress\n");
2098 return -EBUSY;
2099 }
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002100 return vb2_internal_streamoff(q, type);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002101}
2102EXPORT_SYMBOL_GPL(vb2_streamoff);
2103
2104/**
2105 * __find_plane_by_offset() - find plane associated with the given offset off
2106 */
2107static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2108 unsigned int *_buffer, unsigned int *_plane)
2109{
2110 struct vb2_buffer *vb;
2111 unsigned int buffer, plane;
2112
2113 /*
2114 * Go over all buffers and their planes, comparing the given offset
2115 * with an offset assigned to each plane. If a match is found,
2116 * return its buffer and plane numbers.
2117 */
2118 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2119 vb = q->bufs[buffer];
2120
2121 for (plane = 0; plane < vb->num_planes; ++plane) {
2122 if (vb->v4l2_planes[plane].m.mem_offset == off) {
2123 *_buffer = buffer;
2124 *_plane = plane;
2125 return 0;
2126 }
2127 }
2128 }
2129
2130 return -EINVAL;
2131}
2132
2133/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002134 * vb2_expbuf() - Export a buffer as a file descriptor
2135 * @q: videobuf2 queue
2136 * @eb: export buffer structure passed from userspace to vidioc_expbuf
2137 * handler in driver
2138 *
2139 * The return values from this function are intended to be directly returned
2140 * from vidioc_expbuf handler in driver.
2141 */
2142int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
2143{
2144 struct vb2_buffer *vb = NULL;
2145 struct vb2_plane *vb_plane;
2146 int ret;
2147 struct dma_buf *dbuf;
2148
2149 if (q->memory != V4L2_MEMORY_MMAP) {
2150 dprintk(1, "Queue is not currently set up for mmap\n");
2151 return -EINVAL;
2152 }
2153
2154 if (!q->mem_ops->get_dmabuf) {
2155 dprintk(1, "Queue does not support DMA buffer exporting\n");
2156 return -EINVAL;
2157 }
2158
Philipp Zabelea3aba82013-05-21 05:11:35 -03002159 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
2160 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002161 return -EINVAL;
2162 }
2163
2164 if (eb->type != q->type) {
2165 dprintk(1, "qbuf: invalid buffer type\n");
2166 return -EINVAL;
2167 }
2168
2169 if (eb->index >= q->num_buffers) {
2170 dprintk(1, "buffer index out of range\n");
2171 return -EINVAL;
2172 }
2173
2174 vb = q->bufs[eb->index];
2175
2176 if (eb->plane >= vb->num_planes) {
2177 dprintk(1, "buffer plane out of range\n");
2178 return -EINVAL;
2179 }
2180
2181 vb_plane = &vb->planes[eb->plane];
2182
Hans Verkuilb5b45412014-01-29 11:53:25 -03002183 dbuf = call_memop(vb, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002184 if (IS_ERR_OR_NULL(dbuf)) {
2185 dprintk(1, "Failed to export buffer %d, plane %d\n",
2186 eb->index, eb->plane);
Hans Verkuilb5b45412014-01-29 11:53:25 -03002187 fail_memop(vb, get_dmabuf);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002188 return -EINVAL;
2189 }
2190
Philipp Zabelea3aba82013-05-21 05:11:35 -03002191 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002192 if (ret < 0) {
2193 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2194 eb->index, eb->plane, ret);
2195 dma_buf_put(dbuf);
2196 return ret;
2197 }
2198
2199 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2200 eb->index, eb->plane, ret);
2201 eb->fd = ret;
2202
2203 return 0;
2204}
2205EXPORT_SYMBOL_GPL(vb2_expbuf);
2206
2207/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03002208 * vb2_mmap() - map video buffers into application address space
2209 * @q: videobuf2 queue
2210 * @vma: vma passed to the mmap file operation handler in the driver
2211 *
2212 * Should be called from mmap file operation handler of a driver.
2213 * This function maps one plane of one of the available video buffers to
2214 * userspace. To map whole video memory allocated on reqbufs, this function
2215 * has to be called once per each plane per each buffer previously allocated.
2216 *
2217 * When the userspace application calls mmap, it passes to it an offset returned
2218 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2219 * a "cookie", which is then used to identify the plane to be mapped.
2220 * This function finds a plane with a matching offset and a mapping is performed
2221 * by the means of a provided memory operation.
2222 *
2223 * The return values from this function are intended to be directly returned
2224 * from the mmap handler in driver.
2225 */
2226int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2227{
2228 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002229 struct vb2_buffer *vb;
2230 unsigned int buffer, plane;
2231 int ret;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002232 unsigned long length;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002233
2234 if (q->memory != V4L2_MEMORY_MMAP) {
2235 dprintk(1, "Queue is not currently set up for mmap\n");
2236 return -EINVAL;
2237 }
2238
2239 /*
2240 * Check memory area access mode.
2241 */
2242 if (!(vma->vm_flags & VM_SHARED)) {
2243 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2244 return -EINVAL;
2245 }
2246 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2247 if (!(vma->vm_flags & VM_WRITE)) {
2248 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2249 return -EINVAL;
2250 }
2251 } else {
2252 if (!(vma->vm_flags & VM_READ)) {
2253 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2254 return -EINVAL;
2255 }
2256 }
2257
2258 /*
2259 * Find the plane corresponding to the offset passed by userspace.
2260 */
2261 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2262 if (ret)
2263 return ret;
2264
2265 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03002266
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -03002267 /*
2268 * MMAP requires page_aligned buffers.
2269 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2270 * so, we need to do the same here.
2271 */
2272 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
2273 if (length < (vma->vm_end - vma->vm_start)) {
2274 dprintk(1,
2275 "MMAP invalid, as it would overflow buffer length\n");
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03002276 return -EINVAL;
2277 }
2278
Hans Verkuilb5b45412014-01-29 11:53:25 -03002279 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2280 if (ret) {
2281 fail_memop(vb, mmap);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002282 return ret;
Hans Verkuilb5b45412014-01-29 11:53:25 -03002283 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002284
Pawel Osciake23ccc02010-10-11 10:56:41 -03002285 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(vb2_mmap);
2289
Scott Jiang6f524ec2011-09-21 09:25:23 -03002290#ifndef CONFIG_MMU
2291unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2292 unsigned long addr,
2293 unsigned long len,
2294 unsigned long pgoff,
2295 unsigned long flags)
2296{
2297 unsigned long off = pgoff << PAGE_SHIFT;
2298 struct vb2_buffer *vb;
2299 unsigned int buffer, plane;
2300 int ret;
2301
2302 if (q->memory != V4L2_MEMORY_MMAP) {
2303 dprintk(1, "Queue is not currently set up for mmap\n");
2304 return -EINVAL;
2305 }
2306
2307 /*
2308 * Find the plane corresponding to the offset passed by userspace.
2309 */
2310 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2311 if (ret)
2312 return ret;
2313
2314 vb = q->bufs[buffer];
2315
2316 return (unsigned long)vb2_plane_vaddr(vb, plane);
2317}
2318EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2319#endif
2320
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002321static int __vb2_init_fileio(struct vb2_queue *q, int read);
2322static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002323
2324/**
2325 * vb2_poll() - implements poll userspace operation
2326 * @q: videobuf2 queue
2327 * @file: file argument passed to the poll file operation handler
2328 * @wait: wait argument passed to the poll file operation handler
2329 *
2330 * This function implements poll file operation handler for a driver.
2331 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2332 * be informed that the file descriptor of a video device is available for
2333 * reading.
2334 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2335 * will be reported as available for writing.
2336 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03002337 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2338 * pending events.
2339 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03002340 * The return values from this function are intended to be directly returned
2341 * from poll handler in driver.
2342 */
2343unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2344{
Hans Verkuil95213ce2011-07-13 04:26:52 -03002345 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002346 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002347 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03002348 unsigned int res = 0;
2349 unsigned long flags;
2350
2351 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2352 struct v4l2_fh *fh = file->private_data;
2353
2354 if (v4l2_event_pending(fh))
2355 res = POLLPRI;
2356 else if (req_events & POLLPRI)
2357 poll_wait(file, &fh->wait, wait);
2358 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03002359
Hans Verkuilcd138232013-01-30 13:29:02 -03002360 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2361 return res;
2362 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2363 return res;
2364
Pawel Osciake23ccc02010-10-11 10:56:41 -03002365 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03002366 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002367 */
2368 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002369 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2370 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002371 if (__vb2_init_fileio(q, 1))
2372 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002373 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03002374 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2375 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002376 if (__vb2_init_fileio(q, 0))
2377 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002378 /*
2379 * Write to OUTPUT queue can be done immediately.
2380 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03002381 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002382 }
2383 }
2384
2385 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03002386 * There is nothing to wait for if no buffers have already been queued.
2387 */
2388 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002389 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002390
Seung-Woo Kim412cb872013-05-20 23:47:29 -03002391 if (list_empty(&q->done_list))
2392 poll_wait(file, &q->done_wq, wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002393
2394 /*
2395 * Take first buffer available for dequeuing.
2396 */
2397 spin_lock_irqsave(&q->done_lock, flags);
2398 if (!list_empty(&q->done_list))
2399 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2400 done_entry);
2401 spin_unlock_irqrestore(&q->done_lock, flags);
2402
2403 if (vb && (vb->state == VB2_BUF_STATE_DONE
2404 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002405 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2406 res | POLLOUT | POLLWRNORM :
2407 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002408 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002409 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002410}
2411EXPORT_SYMBOL_GPL(vb2_poll);
2412
2413/**
2414 * vb2_queue_init() - initialize a videobuf2 queue
2415 * @q: videobuf2 queue; this structure should be allocated in driver
2416 *
2417 * The vb2_queue structure should be allocated by the driver. The driver is
2418 * responsible of clearing it's content and setting initial values for some
2419 * required entries before calling this function.
2420 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2421 * to the struct vb2_queue description in include/media/videobuf2-core.h
2422 * for more information.
2423 */
2424int vb2_queue_init(struct vb2_queue *q)
2425{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002426 /*
2427 * Sanity check
2428 */
2429 if (WARN_ON(!q) ||
2430 WARN_ON(!q->ops) ||
2431 WARN_ON(!q->mem_ops) ||
2432 WARN_ON(!q->type) ||
2433 WARN_ON(!q->io_modes) ||
2434 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002435 WARN_ON(!q->ops->buf_queue) ||
Sakari Ailus872484c2013-08-25 17:57:03 -03002436 WARN_ON(q->timestamp_flags &
2437 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
2438 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002439 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002440
Kamil Debski6aa69f92013-01-25 06:29:57 -03002441 /* Warn that the driver should choose an appropriate timestamp type */
Sakari Ailusc57ff792014-03-01 10:28:02 -03002442 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
2443 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
Kamil Debski6aa69f92013-01-25 06:29:57 -03002444
Pawel Osciake23ccc02010-10-11 10:56:41 -03002445 INIT_LIST_HEAD(&q->queued_list);
2446 INIT_LIST_HEAD(&q->done_list);
2447 spin_lock_init(&q->done_lock);
2448 init_waitqueue_head(&q->done_wq);
2449
2450 if (q->buf_struct_size == 0)
2451 q->buf_struct_size = sizeof(struct vb2_buffer);
2452
2453 return 0;
2454}
2455EXPORT_SYMBOL_GPL(vb2_queue_init);
2456
2457/**
2458 * vb2_queue_release() - stop streaming, release the queue and free memory
2459 * @q: videobuf2 queue
2460 *
2461 * This function stops streaming and performs necessary clean ups, including
2462 * freeing video buffer memory. The driver is responsible for freeing
2463 * the vb2_queue structure itself.
2464 */
2465void vb2_queue_release(struct vb2_queue *q)
2466{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002467 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002468 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002469 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002470}
2471EXPORT_SYMBOL_GPL(vb2_queue_release);
2472
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002473/**
2474 * struct vb2_fileio_buf - buffer context used by file io emulator
2475 *
2476 * vb2 provides a compatibility layer and emulator of file io (read and
2477 * write) calls on top of streaming API. This structure is used for
2478 * tracking context related to the buffers.
2479 */
2480struct vb2_fileio_buf {
2481 void *vaddr;
2482 unsigned int size;
2483 unsigned int pos;
2484 unsigned int queued:1;
2485};
2486
2487/**
2488 * struct vb2_fileio_data - queue context used by file io emulator
2489 *
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002490 * @cur_index: the index of the buffer currently being read from or
2491 * written to. If equal to q->num_buffers then a new buffer
2492 * must be dequeued.
2493 * @initial_index: in the read() case all buffers are queued up immediately
2494 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2495 * buffers. However, in the write() case no buffers are initially
2496 * queued, instead whenever a buffer is full it is queued up by
2497 * __vb2_perform_fileio(). Only once all available buffers have
2498 * been queued up will __vb2_perform_fileio() start to dequeue
2499 * buffers. This means that initially __vb2_perform_fileio()
2500 * needs to know what buffer index to use when it is queuing up
2501 * the buffers for the first time. That initial index is stored
2502 * in this field. Once it is equal to q->num_buffers all
2503 * available buffers have been queued and __vb2_perform_fileio()
2504 * should start the normal dequeue/queue cycle.
2505 *
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002506 * vb2 provides a compatibility layer and emulator of file io (read and
2507 * write) calls on top of streaming API. For proper operation it required
2508 * this structure to save the driver state between each call of the read
2509 * or write function.
2510 */
2511struct vb2_fileio_data {
2512 struct v4l2_requestbuffers req;
2513 struct v4l2_buffer b;
2514 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002515 unsigned int cur_index;
2516 unsigned int initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002517 unsigned int q_count;
2518 unsigned int dq_count;
2519 unsigned int flags;
2520};
2521
2522/**
2523 * __vb2_init_fileio() - initialize file io emulator
2524 * @q: videobuf2 queue
2525 * @read: mode selector (1 means read, 0 means write)
2526 */
2527static int __vb2_init_fileio(struct vb2_queue *q, int read)
2528{
2529 struct vb2_fileio_data *fileio;
2530 int i, ret;
2531 unsigned int count = 0;
2532
2533 /*
2534 * Sanity check
2535 */
2536 if ((read && !(q->io_modes & VB2_READ)) ||
2537 (!read && !(q->io_modes & VB2_WRITE)))
2538 BUG();
2539
2540 /*
2541 * Check if device supports mapping buffers to kernel virtual space.
2542 */
2543 if (!q->mem_ops->vaddr)
2544 return -EBUSY;
2545
2546 /*
2547 * Check if streaming api has not been already activated.
2548 */
2549 if (q->streaming || q->num_buffers > 0)
2550 return -EBUSY;
2551
2552 /*
2553 * Start with count 1, driver can increase it in queue_setup()
2554 */
2555 count = 1;
2556
2557 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2558 (read) ? "read" : "write", count, q->io_flags);
2559
2560 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2561 if (fileio == NULL)
2562 return -ENOMEM;
2563
2564 fileio->flags = q->io_flags;
2565
2566 /*
2567 * Request buffers and use MMAP type to force driver
2568 * to allocate buffers by itself.
2569 */
2570 fileio->req.count = count;
2571 fileio->req.memory = V4L2_MEMORY_MMAP;
2572 fileio->req.type = q->type;
2573 ret = vb2_reqbufs(q, &fileio->req);
2574 if (ret)
2575 goto err_kfree;
2576
2577 /*
2578 * Check if plane_count is correct
2579 * (multiplane buffers are not supported).
2580 */
2581 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002582 ret = -EBUSY;
2583 goto err_reqbufs;
2584 }
2585
2586 /*
2587 * Get kernel address of each buffer.
2588 */
2589 for (i = 0; i < q->num_buffers; i++) {
2590 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
Wei Yongjun5dd69462013-05-13 01:48:45 -03002591 if (fileio->bufs[i].vaddr == NULL) {
2592 ret = -EINVAL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002593 goto err_reqbufs;
Wei Yongjun5dd69462013-05-13 01:48:45 -03002594 }
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002595 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2596 }
2597
2598 /*
2599 * Read mode requires pre queuing of all buffers.
2600 */
2601 if (read) {
2602 /*
2603 * Queue all buffers.
2604 */
2605 for (i = 0; i < q->num_buffers; i++) {
2606 struct v4l2_buffer *b = &fileio->b;
2607 memset(b, 0, sizeof(*b));
2608 b->type = q->type;
2609 b->memory = q->memory;
2610 b->index = i;
2611 ret = vb2_qbuf(q, b);
2612 if (ret)
2613 goto err_reqbufs;
2614 fileio->bufs[i].queued = 1;
2615 }
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002616 /*
2617 * All buffers have been queued, so mark that by setting
2618 * initial_index to q->num_buffers
2619 */
2620 fileio->initial_index = q->num_buffers;
2621 fileio->cur_index = q->num_buffers;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002622 }
2623
Hans Verkuil02f142e2013-12-13 13:13:42 -03002624 /*
2625 * Start streaming.
2626 */
2627 ret = vb2_streamon(q, q->type);
2628 if (ret)
2629 goto err_reqbufs;
2630
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002631 q->fileio = fileio;
2632
2633 return ret;
2634
2635err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002636 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002637 vb2_reqbufs(q, &fileio->req);
2638
2639err_kfree:
2640 kfree(fileio);
2641 return ret;
2642}
2643
2644/**
2645 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2646 * @q: videobuf2 queue
2647 */
2648static int __vb2_cleanup_fileio(struct vb2_queue *q)
2649{
2650 struct vb2_fileio_data *fileio = q->fileio;
2651
2652 if (fileio) {
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002653 vb2_internal_streamoff(q, q->type);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002654 q->fileio = NULL;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002655 fileio->req.count = 0;
2656 vb2_reqbufs(q, &fileio->req);
2657 kfree(fileio);
2658 dprintk(3, "file io emulator closed\n");
2659 }
2660 return 0;
2661}
2662
2663/**
2664 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2665 * @q: videobuf2 queue
2666 * @data: pointed to target userspace buffer
2667 * @count: number of bytes to read or write
2668 * @ppos: file handle position tracking pointer
2669 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2670 * @read: access mode selector (1 means read, 0 means write)
2671 */
2672static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2673 loff_t *ppos, int nonblock, int read)
2674{
2675 struct vb2_fileio_data *fileio;
2676 struct vb2_fileio_buf *buf;
2677 int ret, index;
2678
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002679 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002680 read ? "read" : "write", (long)*ppos, count,
2681 nonblock ? "non" : "");
2682
2683 if (!data)
2684 return -EINVAL;
2685
2686 /*
2687 * Initialize emulator on first call.
2688 */
2689 if (!q->fileio) {
2690 ret = __vb2_init_fileio(q, read);
2691 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2692 if (ret)
2693 return ret;
2694 }
2695 fileio = q->fileio;
2696
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002697 /*
2698 * Check if we need to dequeue the buffer.
2699 */
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002700 index = fileio->cur_index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002701 if (index >= q->num_buffers) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002702 /*
2703 * Call vb2_dqbuf to get buffer back.
2704 */
2705 memset(&fileio->b, 0, sizeof(fileio->b));
2706 fileio->b.type = q->type;
2707 fileio->b.memory = q->memory;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002708 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002709 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2710 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002711 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002712 fileio->dq_count += 1;
2713
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002714 fileio->cur_index = index = fileio->b.index;
Hans Verkuil88e26872013-12-13 13:13:45 -03002715 buf = &fileio->bufs[index];
2716
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002717 /*
2718 * Get number of bytes filled by the driver
2719 */
Hans Verkuil88e26872013-12-13 13:13:45 -03002720 buf->pos = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002721 buf->queued = 0;
Hans Verkuil88e26872013-12-13 13:13:45 -03002722 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2723 : vb2_plane_size(q->bufs[index], 0);
2724 } else {
2725 buf = &fileio->bufs[index];
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002726 }
2727
2728 /*
2729 * Limit count on last few bytes of the buffer.
2730 */
2731 if (buf->pos + count > buf->size) {
2732 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002733 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002734 }
2735
2736 /*
2737 * Transfer data to userspace.
2738 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002739 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002740 count, index, buf->pos);
2741 if (read)
2742 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2743 else
2744 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2745 if (ret) {
2746 dprintk(3, "file io: error copying data\n");
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002747 return -EFAULT;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002748 }
2749
2750 /*
2751 * Update counters.
2752 */
2753 buf->pos += count;
2754 *ppos += count;
2755
2756 /*
2757 * Queue next buffer if required.
2758 */
2759 if (buf->pos == buf->size ||
2760 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2761 /*
2762 * Check if this is the last buffer to read.
2763 */
2764 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2765 fileio->dq_count == 1) {
2766 dprintk(3, "file io: read limit reached\n");
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002767 return __vb2_cleanup_fileio(q);
2768 }
2769
2770 /*
2771 * Call vb2_qbuf and give buffer to the driver.
2772 */
2773 memset(&fileio->b, 0, sizeof(fileio->b));
2774 fileio->b.type = q->type;
2775 fileio->b.memory = q->memory;
2776 fileio->b.index = index;
2777 fileio->b.bytesused = buf->pos;
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002778 ret = vb2_internal_qbuf(q, &fileio->b);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002779 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2780 if (ret)
Hans Verkuilb2f2f042013-12-13 13:13:41 -03002781 return ret;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002782
2783 /*
2784 * Buffer has been queued, update the status
2785 */
2786 buf->pos = 0;
2787 buf->queued = 1;
Hans Verkuil88e26872013-12-13 13:13:45 -03002788 buf->size = vb2_plane_size(q->bufs[index], 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002789 fileio->q_count += 1;
Hans Verkuil4e5a4d82014-02-14 06:46:50 -03002790 /*
2791 * If we are queuing up buffers for the first time, then
2792 * increase initial_index by one.
2793 */
2794 if (fileio->initial_index < q->num_buffers)
2795 fileio->initial_index++;
2796 /*
2797 * The next buffer to use is either a buffer that's going to be
2798 * queued for the first time (initial_index < q->num_buffers)
2799 * or it is equal to q->num_buffers, meaning that the next
2800 * time we need to dequeue a buffer since we've now queued up
2801 * all the 'first time' buffers.
2802 */
2803 fileio->cur_index = fileio->initial_index;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002804 }
2805
2806 /*
2807 * Return proper number of bytes processed.
2808 */
2809 if (ret == 0)
2810 ret = count;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002811 return ret;
2812}
2813
2814size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2815 loff_t *ppos, int nonblocking)
2816{
2817 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2818}
2819EXPORT_SYMBOL_GPL(vb2_read);
2820
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002821size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002822 loff_t *ppos, int nonblocking)
2823{
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002824 return __vb2_perform_fileio(q, (char __user *) data, count,
2825 ppos, nonblocking, 0);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002826}
2827EXPORT_SYMBOL_GPL(vb2_write);
2828
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002829
2830/*
2831 * The following functions are not part of the vb2 core API, but are helper
2832 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2833 * and struct vb2_ops.
2834 * They contain boilerplate code that most if not all drivers have to do
2835 * and so they simplify the driver code.
2836 */
2837
2838/* The queue is busy if there is a owner and you are not that owner. */
2839static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2840{
2841 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2842}
2843
2844/* vb2 ioctl helpers */
2845
2846int vb2_ioctl_reqbufs(struct file *file, void *priv,
2847 struct v4l2_requestbuffers *p)
2848{
2849 struct video_device *vdev = video_devdata(file);
2850 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2851
2852 if (res)
2853 return res;
2854 if (vb2_queue_is_busy(vdev, file))
2855 return -EBUSY;
2856 res = __reqbufs(vdev->queue, p);
2857 /* If count == 0, then the owner has released all buffers and he
2858 is no longer owner of the queue. Otherwise we have a new owner. */
2859 if (res == 0)
2860 vdev->queue->owner = p->count ? file->private_data : NULL;
2861 return res;
2862}
2863EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2864
2865int vb2_ioctl_create_bufs(struct file *file, void *priv,
2866 struct v4l2_create_buffers *p)
2867{
2868 struct video_device *vdev = video_devdata(file);
2869 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2870
2871 p->index = vdev->queue->num_buffers;
2872 /* If count == 0, then just check if memory and type are valid.
2873 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2874 if (p->count == 0)
2875 return res != -EBUSY ? res : 0;
2876 if (res)
2877 return res;
2878 if (vb2_queue_is_busy(vdev, file))
2879 return -EBUSY;
2880 res = __create_bufs(vdev->queue, p);
2881 if (res == 0)
2882 vdev->queue->owner = file->private_data;
2883 return res;
2884}
2885EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2886
2887int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2888 struct v4l2_buffer *p)
2889{
2890 struct video_device *vdev = video_devdata(file);
2891
2892 if (vb2_queue_is_busy(vdev, file))
2893 return -EBUSY;
2894 return vb2_prepare_buf(vdev->queue, p);
2895}
2896EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2897
2898int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2899{
2900 struct video_device *vdev = video_devdata(file);
2901
2902 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2903 return vb2_querybuf(vdev->queue, p);
2904}
2905EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2906
2907int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2908{
2909 struct video_device *vdev = video_devdata(file);
2910
2911 if (vb2_queue_is_busy(vdev, file))
2912 return -EBUSY;
2913 return vb2_qbuf(vdev->queue, p);
2914}
2915EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2916
2917int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2918{
2919 struct video_device *vdev = video_devdata(file);
2920
2921 if (vb2_queue_is_busy(vdev, file))
2922 return -EBUSY;
2923 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2924}
2925EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2926
2927int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2928{
2929 struct video_device *vdev = video_devdata(file);
2930
2931 if (vb2_queue_is_busy(vdev, file))
2932 return -EBUSY;
2933 return vb2_streamon(vdev->queue, i);
2934}
2935EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2936
2937int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2938{
2939 struct video_device *vdev = video_devdata(file);
2940
2941 if (vb2_queue_is_busy(vdev, file))
2942 return -EBUSY;
2943 return vb2_streamoff(vdev->queue, i);
2944}
2945EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2946
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002947int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2948{
2949 struct video_device *vdev = video_devdata(file);
2950
2951 if (vb2_queue_is_busy(vdev, file))
2952 return -EBUSY;
2953 return vb2_expbuf(vdev->queue, p);
2954}
2955EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2956
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002957/* v4l2_file_operations helpers */
2958
2959int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2960{
2961 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002962 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2963 int err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002964
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03002965 if (lock && mutex_lock_interruptible(lock))
2966 return -ERESTARTSYS;
2967 err = vb2_mmap(vdev->queue, vma);
2968 if (lock)
2969 mutex_unlock(lock);
2970 return err;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002971}
2972EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2973
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002974int _vb2_fop_release(struct file *file, struct mutex *lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002975{
2976 struct video_device *vdev = video_devdata(file);
2977
2978 if (file->private_data == vdev->queue->owner) {
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002979 if (lock)
2980 mutex_lock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002981 vb2_queue_release(vdev->queue);
2982 vdev->queue->owner = NULL;
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002983 if (lock)
2984 mutex_unlock(lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002985 }
2986 return v4l2_fh_release(file);
2987}
Ricardo Ribalda1380f572013-11-25 05:49:02 -03002988EXPORT_SYMBOL_GPL(_vb2_fop_release);
2989
2990int vb2_fop_release(struct file *file)
2991{
2992 struct video_device *vdev = video_devdata(file);
2993 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2994
2995 return _vb2_fop_release(file, lock);
2996}
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002997EXPORT_SYMBOL_GPL(vb2_fop_release);
2998
Ricardo Ribalda819585b2013-08-28 04:39:29 -03002999ssize_t vb2_fop_write(struct file *file, const char __user *buf,
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003000 size_t count, loff_t *ppos)
3001{
3002 struct video_device *vdev = video_devdata(file);
3003 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003004 int err = -EBUSY;
3005
Hans Verkuilcf533732012-07-31 04:02:25 -03003006 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003007 return -ERESTARTSYS;
3008 if (vb2_queue_is_busy(vdev, file))
3009 goto exit;
3010 err = vb2_write(vdev->queue, buf, count, ppos,
3011 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03003012 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003013 vdev->queue->owner = file->private_data;
3014exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03003015 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003016 mutex_unlock(lock);
3017 return err;
3018}
3019EXPORT_SYMBOL_GPL(vb2_fop_write);
3020
3021ssize_t vb2_fop_read(struct file *file, char __user *buf,
3022 size_t count, loff_t *ppos)
3023{
3024 struct video_device *vdev = video_devdata(file);
3025 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003026 int err = -EBUSY;
3027
Hans Verkuilcf533732012-07-31 04:02:25 -03003028 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003029 return -ERESTARTSYS;
3030 if (vb2_queue_is_busy(vdev, file))
3031 goto exit;
3032 err = vb2_read(vdev->queue, buf, count, ppos,
3033 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03003034 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003035 vdev->queue->owner = file->private_data;
3036exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03003037 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003038 mutex_unlock(lock);
3039 return err;
3040}
3041EXPORT_SYMBOL_GPL(vb2_fop_read);
3042
3043unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
3044{
3045 struct video_device *vdev = video_devdata(file);
3046 struct vb2_queue *q = vdev->queue;
3047 struct mutex *lock = q->lock ? q->lock : vdev->lock;
3048 unsigned long req_events = poll_requested_events(wait);
3049 unsigned res;
3050 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003051 bool must_lock = false;
3052
3053 /* Try to be smart: only lock if polling might start fileio,
3054 otherwise locking will only introduce unwanted delays. */
3055 if (q->num_buffers == 0 && q->fileio == NULL) {
3056 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
3057 (req_events & (POLLIN | POLLRDNORM)))
3058 must_lock = true;
3059 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
3060 (req_events & (POLLOUT | POLLWRNORM)))
3061 must_lock = true;
3062 }
3063
3064 /* If locking is needed, but this helper doesn't know how, then you
3065 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03003066 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003067
Hans Verkuilcf533732012-07-31 04:02:25 -03003068 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003069 return POLLERR;
3070
3071 fileio = q->fileio;
3072
3073 res = vb2_poll(vdev->queue, file, wait);
3074
3075 /* If fileio was started, then we have a new queue owner. */
3076 if (must_lock && !fileio && q->fileio)
3077 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03003078 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003079 mutex_unlock(lock);
3080 return res;
3081}
3082EXPORT_SYMBOL_GPL(vb2_fop_poll);
3083
3084#ifndef CONFIG_MMU
3085unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
3086 unsigned long len, unsigned long pgoff, unsigned long flags)
3087{
3088 struct video_device *vdev = video_devdata(file);
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03003089 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
3090 int ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003091
Laurent Pinchart8a90f1a2013-08-02 13:55:21 -03003092 if (lock && mutex_lock_interruptible(lock))
3093 return -ERESTARTSYS;
3094 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
3095 if (lock)
3096 mutex_unlock(lock);
3097 return ret;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03003098}
3099EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
3100#endif
3101
3102/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
3103
3104void vb2_ops_wait_prepare(struct vb2_queue *vq)
3105{
3106 mutex_unlock(vq->lock);
3107}
3108EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
3109
3110void vb2_ops_wait_finish(struct vb2_queue *vq)
3111{
3112 mutex_lock(vq->lock);
3113}
3114EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
3115
Pawel Osciake23ccc02010-10-11 10:56:41 -03003116MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03003117MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03003118MODULE_LICENSE("GPL");