blob: 58c17444d22faf4c1117383c1a86698aed2450e7 [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
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030036#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030037 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
39
40#define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030043#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030044 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
Sakari Ailus1b18e7a2012-10-22 17:10:16 -030045 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030047
Pawel Osciake23ccc02010-10-11 10:56:41 -030048/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030051static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030052{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
57 /* Allocate memory for all planes in this buffer */
58 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030059 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Hans Verkuilb6ba2052013-03-01 15:44:20 -030060 q->plane_sizes[plane], q->gfp_flags);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030061 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030062 goto free;
63
64 /* Associate allocator private data with this plane */
65 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030066 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030067 }
68
69 return 0;
70free:
71 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030072 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030073 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030074 vb->planes[plane - 1].mem_priv = NULL;
75 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030076
77 return -ENOMEM;
78}
79
80/**
81 * __vb2_buf_mem_free() - free memory of the given buffer
82 */
83static void __vb2_buf_mem_free(struct vb2_buffer *vb)
84{
85 struct vb2_queue *q = vb->vb2_queue;
86 unsigned int plane;
87
88 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030089 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030090 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030091 dprintk(3, "Freed plane %d of buffer %d\n", plane,
92 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030093 }
94}
95
96/**
97 * __vb2_buf_userptr_put() - release userspace memory associated with
98 * a USERPTR buffer
99 */
100static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
101{
102 struct vb2_queue *q = vb->vb2_queue;
103 unsigned int plane;
104
105 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300106 if (vb->planes[plane].mem_priv)
107 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
108 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300109 }
110}
111
112/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300113 * __vb2_plane_dmabuf_put() - release memory associated with
114 * a DMABUF shared plane
115 */
116static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
117{
118 if (!p->mem_priv)
119 return;
120
121 if (p->dbuf_mapped)
122 call_memop(q, unmap_dmabuf, p->mem_priv);
123
124 call_memop(q, detach_dmabuf, p->mem_priv);
125 dma_buf_put(p->dbuf);
126 memset(p, 0, sizeof(*p));
127}
128
129/**
130 * __vb2_buf_dmabuf_put() - release memory associated with
131 * a DMABUF shared buffer
132 */
133static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
134{
135 struct vb2_queue *q = vb->vb2_queue;
136 unsigned int plane;
137
138 for (plane = 0; plane < vb->num_planes; ++plane)
139 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
140}
141
142/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300143 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
144 * every buffer on the queue
145 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300146static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300147{
148 unsigned int buffer, plane;
149 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300150 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300151
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300152 if (q->num_buffers) {
153 struct v4l2_plane *p;
154 vb = q->bufs[q->num_buffers - 1];
155 p = &vb->v4l2_planes[vb->num_planes - 1];
156 off = PAGE_ALIGN(p->m.mem_offset + p->length);
157 } else {
158 off = 0;
159 }
160
161 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300162 vb = q->bufs[buffer];
163 if (!vb)
164 continue;
165
166 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300167 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300168 vb->v4l2_planes[plane].m.mem_offset = off;
169
170 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
171 buffer, plane, off);
172
173 off += vb->v4l2_planes[plane].length;
174 off = PAGE_ALIGN(off);
175 }
176 }
177}
178
179/**
180 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
181 * video buffer memory for all buffers/planes on the queue and initializes the
182 * queue
183 *
184 * Returns the number of buffers successfully allocated.
185 */
186static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300187 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300188{
189 unsigned int buffer;
190 struct vb2_buffer *vb;
191 int ret;
192
193 for (buffer = 0; buffer < num_buffers; ++buffer) {
194 /* Allocate videobuf buffer structures */
195 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
196 if (!vb) {
197 dprintk(1, "Memory alloc for buffer struct failed\n");
198 break;
199 }
200
201 /* Length stores number of planes for multiplanar buffers */
202 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
203 vb->v4l2_buf.length = num_planes;
204
205 vb->state = VB2_BUF_STATE_DEQUEUED;
206 vb->vb2_queue = q;
207 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300208 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300209 vb->v4l2_buf.type = q->type;
210 vb->v4l2_buf.memory = memory;
211
212 /* Allocate video buffer memory for the MMAP type */
213 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300214 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300215 if (ret) {
216 dprintk(1, "Failed allocating memory for "
217 "buffer %d\n", buffer);
218 kfree(vb);
219 break;
220 }
221 /*
222 * Call the driver-provided buffer initialization
223 * callback, if given. An error in initialization
224 * results in queue setup failure.
225 */
226 ret = call_qop(q, buf_init, vb);
227 if (ret) {
228 dprintk(1, "Buffer %d %p initialization"
229 " failed\n", buffer, vb);
230 __vb2_buf_mem_free(vb);
231 kfree(vb);
232 break;
233 }
234 }
235
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300236 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300237 }
238
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300239 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300240
241 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300242 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300243
244 return buffer;
245}
246
247/**
248 * __vb2_free_mem() - release all video buffer memory for a given queue
249 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300250static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300251{
252 unsigned int buffer;
253 struct vb2_buffer *vb;
254
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300255 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
256 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300257 vb = q->bufs[buffer];
258 if (!vb)
259 continue;
260
261 /* Free MMAP buffers or release USERPTR buffers */
262 if (q->memory == V4L2_MEMORY_MMAP)
263 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300264 else if (q->memory == V4L2_MEMORY_DMABUF)
265 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300266 else
267 __vb2_buf_userptr_put(vb);
268 }
269}
270
271/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300272 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
273 * related information, if no buffers are left return the queue to an
274 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300275 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300276static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300277{
278 unsigned int buffer;
279
280 /* Call driver-provided cleanup function for each buffer, if provided */
281 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300282 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
283 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300284 if (NULL == q->bufs[buffer])
285 continue;
286 q->ops->buf_cleanup(q->bufs[buffer]);
287 }
288 }
289
290 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300291 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300292
293 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300294 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
295 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300296 kfree(q->bufs[buffer]);
297 q->bufs[buffer] = NULL;
298 }
299
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300300 q->num_buffers -= buffers;
301 if (!q->num_buffers)
302 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300303 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300304}
305
306/**
307 * __verify_planes_array() - verify that the planes array passed in struct
308 * v4l2_buffer from userspace can be safely used
309 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300310static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300311{
Hans Verkuil32a77262012-09-28 06:12:53 -0300312 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
313 return 0;
314
Pawel Osciake23ccc02010-10-11 10:56:41 -0300315 /* Is memory for copying plane information present? */
316 if (NULL == b->m.planes) {
317 dprintk(1, "Multi-planar buffer passed but "
318 "planes array not provided\n");
319 return -EINVAL;
320 }
321
322 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
323 dprintk(1, "Incorrect planes array length, "
324 "expected %d, got %d\n", vb->num_planes, b->length);
325 return -EINVAL;
326 }
327
328 return 0;
329}
330
331/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300332 * __buffer_in_use() - return true if the buffer is in use and
333 * the queue cannot be freed (by the means of REQBUFS(0)) call
334 */
335static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
336{
337 unsigned int plane;
338 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300339 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300340 /*
341 * If num_users() has not been provided, call_memop
342 * will return 0, apparently nobody cares about this
343 * case anyway. If num_users() returns more than 1,
344 * we are not the only user of the plane's memory.
345 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300346 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300347 return true;
348 }
349 return false;
350}
351
352/**
353 * __buffers_in_use() - return true if any buffers on the queue are in use and
354 * the queue cannot be freed (by the means of REQBUFS(0)) call
355 */
356static bool __buffers_in_use(struct vb2_queue *q)
357{
358 unsigned int buffer;
359 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
360 if (__buffer_in_use(q, q->bufs[buffer]))
361 return true;
362 }
363 return false;
364}
365
366/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300367 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
368 * returned to userspace
369 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300370static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300371{
372 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300373
Sakari Ailus2b719d72012-05-02 09:40:03 -0300374 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300375 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300376 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300377 b->reserved = vb->v4l2_buf.reserved;
378
379 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300380 /*
381 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300382 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300383 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300384 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300385 memcpy(b->m.planes, vb->v4l2_planes,
386 b->length * sizeof(struct v4l2_plane));
387 } else {
388 /*
389 * We use length and offset in v4l2_planes array even for
390 * single-planar buffers, but userspace does not.
391 */
392 b->length = vb->v4l2_planes[0].length;
393 b->bytesused = vb->v4l2_planes[0].bytesused;
394 if (q->memory == V4L2_MEMORY_MMAP)
395 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
396 else if (q->memory == V4L2_MEMORY_USERPTR)
397 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300398 else if (q->memory == V4L2_MEMORY_DMABUF)
399 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300400 }
401
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300402 /*
403 * Clear any buffer state related flags.
404 */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300405 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300406 b->flags |= q->timestamp_type;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300407
408 switch (vb->state) {
409 case VB2_BUF_STATE_QUEUED:
410 case VB2_BUF_STATE_ACTIVE:
411 b->flags |= V4L2_BUF_FLAG_QUEUED;
412 break;
413 case VB2_BUF_STATE_ERROR:
414 b->flags |= V4L2_BUF_FLAG_ERROR;
415 /* fall through */
416 case VB2_BUF_STATE_DONE:
417 b->flags |= V4L2_BUF_FLAG_DONE;
418 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300419 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300420 b->flags |= V4L2_BUF_FLAG_PREPARED;
421 break;
422 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300423 /* nothing */
424 break;
425 }
426
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300427 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300428 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300429}
430
431/**
432 * vb2_querybuf() - query video buffer information
433 * @q: videobuf queue
434 * @b: buffer struct passed from userspace to vidioc_querybuf handler
435 * in driver
436 *
437 * Should be called from vidioc_querybuf ioctl handler in driver.
438 * This function will verify the passed v4l2_buffer structure and fill the
439 * relevant information for the userspace.
440 *
441 * The return values from this function are intended to be directly returned
442 * from vidioc_querybuf handler in driver.
443 */
444int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
445{
446 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300447 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300448
449 if (b->type != q->type) {
450 dprintk(1, "querybuf: wrong buffer type\n");
451 return -EINVAL;
452 }
453
454 if (b->index >= q->num_buffers) {
455 dprintk(1, "querybuf: buffer index out of range\n");
456 return -EINVAL;
457 }
458 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300459 ret = __verify_planes_array(vb, b);
460 if (!ret)
461 __fill_v4l2_buffer(vb, b);
462 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300463}
464EXPORT_SYMBOL(vb2_querybuf);
465
466/**
467 * __verify_userptr_ops() - verify that all memory operations required for
468 * USERPTR queue type have been provided
469 */
470static int __verify_userptr_ops(struct vb2_queue *q)
471{
472 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
473 !q->mem_ops->put_userptr)
474 return -EINVAL;
475
476 return 0;
477}
478
479/**
480 * __verify_mmap_ops() - verify that all memory operations required for
481 * MMAP queue type have been provided
482 */
483static int __verify_mmap_ops(struct vb2_queue *q)
484{
485 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
486 !q->mem_ops->put || !q->mem_ops->mmap)
487 return -EINVAL;
488
489 return 0;
490}
491
492/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300493 * __verify_dmabuf_ops() - verify that all memory operations required for
494 * DMABUF queue type have been provided
495 */
496static int __verify_dmabuf_ops(struct vb2_queue *q)
497{
498 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
499 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
500 !q->mem_ops->unmap_dmabuf)
501 return -EINVAL;
502
503 return 0;
504}
505
506/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300507 * __verify_memory_type() - Check whether the memory type and buffer type
508 * passed to a buffer operation are compatible with the queue.
509 */
510static int __verify_memory_type(struct vb2_queue *q,
511 enum v4l2_memory memory, enum v4l2_buf_type type)
512{
Sumit Semwalc5384042012-06-14 10:37:37 -0300513 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
514 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300515 dprintk(1, "reqbufs: unsupported memory type\n");
516 return -EINVAL;
517 }
518
519 if (type != q->type) {
520 dprintk(1, "reqbufs: requested type is incorrect\n");
521 return -EINVAL;
522 }
523
524 /*
525 * Make sure all the required memory ops for given memory type
526 * are available.
527 */
528 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
529 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
530 return -EINVAL;
531 }
532
533 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
534 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
535 return -EINVAL;
536 }
537
Sumit Semwalc5384042012-06-14 10:37:37 -0300538 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
539 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
540 return -EINVAL;
541 }
542
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300543 /*
544 * Place the busy tests at the end: -EBUSY can be ignored when
545 * create_bufs is called with count == 0, but count == 0 should still
546 * do the memory and type validation.
547 */
548 if (q->fileio) {
549 dprintk(1, "reqbufs: file io in progress\n");
550 return -EBUSY;
551 }
552 return 0;
553}
554
555/**
556 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300557 * @q: videobuf2 queue
558 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
559 *
560 * Should be called from vidioc_reqbufs ioctl handler of a driver.
561 * This function:
562 * 1) verifies streaming parameters passed from the userspace,
563 * 2) sets up the queue,
564 * 3) negotiates number of buffers and planes per buffer with the driver
565 * to be used during streaming,
566 * 4) allocates internal buffer structures (struct vb2_buffer), according to
567 * the agreed parameters,
568 * 5) for MMAP memory type, allocates actual video memory, using the
569 * memory handling/allocation routines provided during queue initialization
570 *
571 * If req->count is 0, all the memory will be freed instead.
572 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
573 * and the queue is not busy, memory will be reallocated.
574 *
575 * The return values from this function are intended to be directly returned
576 * from vidioc_reqbufs handler in driver.
577 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300578static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300579{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300580 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300581 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300582
583 if (q->streaming) {
584 dprintk(1, "reqbufs: streaming active\n");
585 return -EBUSY;
586 }
587
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300588 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300589 /*
590 * We already have buffers allocated, so first check if they
591 * are not in use and can be freed.
592 */
593 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
594 dprintk(1, "reqbufs: memory in use, cannot free\n");
595 return -EBUSY;
596 }
597
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300598 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300599
600 /*
601 * In case of REQBUFS(0) return immediately without calling
602 * driver's queue_setup() callback and allocating resources.
603 */
604 if (req->count == 0)
605 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300606 }
607
608 /*
609 * Make sure the requested values and current defaults are sane.
610 */
611 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300612 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300613 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300614 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300615
616 /*
617 * Ask the driver how many buffers and planes per buffer it requires.
618 * Driver also sets the size and allocator context for each plane.
619 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300620 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300621 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300622 if (ret)
623 return ret;
624
625 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300626 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300627 if (ret == 0) {
628 dprintk(1, "Memory allocation failed\n");
629 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300630 }
631
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300632 allocated_buffers = ret;
633
Pawel Osciake23ccc02010-10-11 10:56:41 -0300634 /*
635 * Check if driver can handle the allocated number of buffers.
636 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300637 if (allocated_buffers < num_buffers) {
638 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300639
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300640 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
641 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300642
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300643 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300644 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300645
646 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300647 * Either the driver has accepted a smaller number of buffers,
648 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300649 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300650 }
651
652 q->num_buffers = allocated_buffers;
653
654 if (ret < 0) {
655 __vb2_queue_free(q, allocated_buffers);
656 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300657 }
658
Pawel Osciake23ccc02010-10-11 10:56:41 -0300659 /*
660 * Return the number of successfully allocated buffers
661 * to the userspace.
662 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300663 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300664
665 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300666}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300667
668/**
669 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
670 * type values.
671 * @q: videobuf2 queue
672 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
673 */
674int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
675{
676 int ret = __verify_memory_type(q, req->memory, req->type);
677
678 return ret ? ret : __reqbufs(q, req);
679}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300680EXPORT_SYMBOL_GPL(vb2_reqbufs);
681
682/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300683 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300684 * @q: videobuf2 queue
685 * @create: creation parameters, passed from userspace to vidioc_create_bufs
686 * handler in driver
687 *
688 * Should be called from vidioc_create_bufs ioctl handler of a driver.
689 * This function:
690 * 1) verifies parameter sanity
691 * 2) calls the .queue_setup() queue operation
692 * 3) performs any necessary memory allocations
693 *
694 * The return values from this function are intended to be directly returned
695 * from vidioc_create_bufs handler in driver.
696 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300697static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300698{
699 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300700 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300701
702 if (q->num_buffers == VIDEO_MAX_FRAME) {
703 dprintk(1, "%s(): maximum number of buffers already allocated\n",
704 __func__);
705 return -ENOBUFS;
706 }
707
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300708 if (!q->num_buffers) {
709 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
710 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
711 q->memory = create->memory;
712 }
713
714 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
715
716 /*
717 * Ask the driver, whether the requested number of buffers, planes per
718 * buffer and their sizes are acceptable
719 */
720 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
721 &num_planes, q->plane_sizes, q->alloc_ctx);
722 if (ret)
723 return ret;
724
725 /* Finally, allocate buffers and video memory */
726 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
727 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300728 if (ret == 0) {
729 dprintk(1, "Memory allocation failed\n");
730 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300731 }
732
733 allocated_buffers = ret;
734
735 /*
736 * Check if driver can handle the so far allocated number of buffers.
737 */
738 if (ret < num_buffers) {
739 num_buffers = ret;
740
741 /*
742 * q->num_buffers contains the total number of buffers, that the
743 * queue driver has set up
744 */
745 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
746 &num_planes, q->plane_sizes, q->alloc_ctx);
747
748 if (!ret && allocated_buffers < num_buffers)
749 ret = -ENOMEM;
750
751 /*
752 * Either the driver has accepted a smaller number of buffers,
753 * or .queue_setup() returned an error
754 */
755 }
756
757 q->num_buffers += allocated_buffers;
758
759 if (ret < 0) {
760 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300761 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300762 }
763
764 /*
765 * Return the number of successfully allocated buffers
766 * to the userspace.
767 */
768 create->count = allocated_buffers;
769
770 return 0;
771}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300772
773/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300774 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
775 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300776 * @q: videobuf2 queue
777 * @create: creation parameters, passed from userspace to vidioc_create_bufs
778 * handler in driver
779 */
780int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
781{
782 int ret = __verify_memory_type(q, create->memory, create->format.type);
783
784 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300785 if (create->count == 0)
786 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300787 return ret ? ret : __create_bufs(q, create);
788}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300789EXPORT_SYMBOL_GPL(vb2_create_bufs);
790
791/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300792 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
793 * @vb: vb2_buffer to which the plane in question belongs to
794 * @plane_no: plane number for which the address is to be returned
795 *
796 * This function returns a kernel virtual address of a given plane if
797 * such a mapping exist, NULL otherwise.
798 */
799void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
800{
801 struct vb2_queue *q = vb->vb2_queue;
802
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300803 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300804 return NULL;
805
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300806 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300807
808}
809EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
810
811/**
812 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
813 * @vb: vb2_buffer to which the plane in question belongs to
814 * @plane_no: plane number for which the cookie is to be returned
815 *
816 * This function returns an allocator specific cookie for a given plane if
817 * available, NULL otherwise. The allocator should provide some simple static
818 * inline function, which would convert this cookie to the allocator specific
819 * type that can be used directly by the driver to access the buffer. This can
820 * be for example physical address, pointer to scatter list or IOMMU mapping.
821 */
822void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
823{
824 struct vb2_queue *q = vb->vb2_queue;
825
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300826 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300827 return NULL;
828
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300829 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300830}
831EXPORT_SYMBOL_GPL(vb2_plane_cookie);
832
833/**
834 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
835 * @vb: vb2_buffer returned from the driver
836 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
837 * or VB2_BUF_STATE_ERROR if the operation finished with an error
838 *
839 * This function should be called by the driver after a hardware operation on
840 * a buffer is finished and the buffer may be returned to userspace. The driver
841 * cannot use this buffer anymore until it is queued back to it by videobuf
842 * by the means of buf_queue callback. Only buffers previously queued to the
843 * driver by buf_queue can be passed to this function.
844 */
845void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
846{
847 struct vb2_queue *q = vb->vb2_queue;
848 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300849 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300850
851 if (vb->state != VB2_BUF_STATE_ACTIVE)
852 return;
853
854 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
855 return;
856
857 dprintk(4, "Done processing on buffer %d, state: %d\n",
Tushar Behera9b6f5dc2012-11-12 04:01:29 -0300858 vb->v4l2_buf.index, state);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300859
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300860 /* sync buffers */
861 for (plane = 0; plane < vb->num_planes; ++plane)
862 call_memop(q, finish, vb->planes[plane].mem_priv);
863
Pawel Osciake23ccc02010-10-11 10:56:41 -0300864 /* Add the buffer to the done buffers list */
865 spin_lock_irqsave(&q->done_lock, flags);
866 vb->state = state;
867 list_add_tail(&vb->done_entry, &q->done_list);
868 atomic_dec(&q->queued_count);
869 spin_unlock_irqrestore(&q->done_lock, flags);
870
871 /* Inform any processes that may be waiting for buffers */
872 wake_up(&q->done_wq);
873}
874EXPORT_SYMBOL_GPL(vb2_buffer_done);
875
876/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300877 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
878 * v4l2_buffer by the userspace. The caller has already verified that struct
879 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300880 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300881static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300882 struct v4l2_plane *v4l2_planes)
883{
884 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300885
886 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300887 /* Fill in driver-provided information for OUTPUT types */
888 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
889 /*
890 * Will have to go up to b->length when API starts
891 * accepting variable number of planes.
892 */
893 for (plane = 0; plane < vb->num_planes; ++plane) {
894 v4l2_planes[plane].bytesused =
895 b->m.planes[plane].bytesused;
896 v4l2_planes[plane].data_offset =
897 b->m.planes[plane].data_offset;
898 }
899 }
900
901 if (b->memory == V4L2_MEMORY_USERPTR) {
902 for (plane = 0; plane < vb->num_planes; ++plane) {
903 v4l2_planes[plane].m.userptr =
904 b->m.planes[plane].m.userptr;
905 v4l2_planes[plane].length =
906 b->m.planes[plane].length;
907 }
908 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300909 if (b->memory == V4L2_MEMORY_DMABUF) {
910 for (plane = 0; plane < vb->num_planes; ++plane) {
911 v4l2_planes[plane].m.fd =
912 b->m.planes[plane].m.fd;
913 v4l2_planes[plane].length =
914 b->m.planes[plane].length;
915 v4l2_planes[plane].data_offset =
916 b->m.planes[plane].data_offset;
917 }
918 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300919 } else {
920 /*
921 * Single-planar buffers do not use planes array,
922 * so fill in relevant v4l2_buffer struct fields instead.
923 * In videobuf we use our internal V4l2_planes struct for
924 * single-planar buffers as well, for simplicity.
925 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300926 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300927 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300928 v4l2_planes[0].data_offset = 0;
929 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300930
931 if (b->memory == V4L2_MEMORY_USERPTR) {
932 v4l2_planes[0].m.userptr = b->m.userptr;
933 v4l2_planes[0].length = b->length;
934 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300935
936 if (b->memory == V4L2_MEMORY_DMABUF) {
937 v4l2_planes[0].m.fd = b->m.fd;
938 v4l2_planes[0].length = b->length;
939 v4l2_planes[0].data_offset = 0;
940 }
941
Pawel Osciake23ccc02010-10-11 10:56:41 -0300942 }
943
944 vb->v4l2_buf.field = b->field;
945 vb->v4l2_buf.timestamp = b->timestamp;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300946 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300947}
948
949/**
950 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
951 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300952static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300953{
954 struct v4l2_plane planes[VIDEO_MAX_PLANES];
955 struct vb2_queue *q = vb->vb2_queue;
956 void *mem_priv;
957 unsigned int plane;
958 int ret;
959 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
960
Hans Verkuil32a77262012-09-28 06:12:53 -0300961 /* Copy relevant information provided by the userspace */
962 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300963
964 for (plane = 0; plane < vb->num_planes; ++plane) {
965 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300966 if (vb->v4l2_planes[plane].m.userptr &&
967 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300968 && vb->v4l2_planes[plane].length == planes[plane].length)
969 continue;
970
971 dprintk(3, "qbuf: userspace address for plane %d changed, "
972 "reacquiring memory\n", plane);
973
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300974 /* Check if the provided plane buffer is large enough */
975 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300976 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300977 goto err;
978 }
979
Pawel Osciake23ccc02010-10-11 10:56:41 -0300980 /* Release previously acquired memory if present */
981 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300982 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300983
984 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300985 vb->v4l2_planes[plane].m.userptr = 0;
986 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300987
988 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300989 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
990 planes[plane].m.userptr,
991 planes[plane].length, write);
992 if (IS_ERR_OR_NULL(mem_priv)) {
993 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300994 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300995 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
996 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300997 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300998 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300999 }
1000
1001 /*
1002 * Call driver-specific initialization on the newly acquired buffer,
1003 * if provided.
1004 */
1005 ret = call_qop(q, buf_init, vb);
1006 if (ret) {
1007 dprintk(1, "qbuf: buffer initialization failed\n");
1008 goto err;
1009 }
1010
1011 /*
1012 * Now that everything is in order, copy relevant information
1013 * provided by userspace.
1014 */
1015 for (plane = 0; plane < vb->num_planes; ++plane)
1016 vb->v4l2_planes[plane] = planes[plane];
1017
1018 return 0;
1019err:
1020 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001021 for (plane = 0; plane < vb->num_planes; ++plane) {
1022 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001023 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001024 vb->planes[plane].mem_priv = NULL;
1025 vb->v4l2_planes[plane].m.userptr = 0;
1026 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001027 }
1028
1029 return ret;
1030}
1031
1032/**
1033 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1034 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001035static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001036{
Hans Verkuil32a77262012-09-28 06:12:53 -03001037 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1038 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001039}
1040
1041/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001042 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1043 */
1044static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1045{
1046 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1047 struct vb2_queue *q = vb->vb2_queue;
1048 void *mem_priv;
1049 unsigned int plane;
1050 int ret;
1051 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1052
1053 /* Verify and copy relevant information provided by the userspace */
1054 __fill_vb2_buffer(vb, b, planes);
1055
1056 for (plane = 0; plane < vb->num_planes; ++plane) {
1057 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1058
1059 if (IS_ERR_OR_NULL(dbuf)) {
1060 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1061 plane);
1062 ret = -EINVAL;
1063 goto err;
1064 }
1065
1066 /* use DMABUF size if length is not provided */
1067 if (planes[plane].length == 0)
1068 planes[plane].length = dbuf->size;
1069
1070 if (planes[plane].length < planes[plane].data_offset +
1071 q->plane_sizes[plane]) {
1072 ret = -EINVAL;
1073 goto err;
1074 }
1075
1076 /* Skip the plane if already verified */
1077 if (dbuf == vb->planes[plane].dbuf &&
1078 vb->v4l2_planes[plane].length == planes[plane].length) {
1079 dma_buf_put(dbuf);
1080 continue;
1081 }
1082
1083 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1084
1085 /* Release previously acquired memory if present */
1086 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1087 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1088
1089 /* Acquire each plane's memory */
1090 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1091 dbuf, planes[plane].length, write);
1092 if (IS_ERR(mem_priv)) {
1093 dprintk(1, "qbuf: failed to attach dmabuf\n");
1094 ret = PTR_ERR(mem_priv);
1095 dma_buf_put(dbuf);
1096 goto err;
1097 }
1098
1099 vb->planes[plane].dbuf = dbuf;
1100 vb->planes[plane].mem_priv = mem_priv;
1101 }
1102
1103 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1104 * really we want to do this just before the DMA, not while queueing
1105 * the buffer(s)..
1106 */
1107 for (plane = 0; plane < vb->num_planes; ++plane) {
1108 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1109 if (ret) {
1110 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1111 plane);
1112 goto err;
1113 }
1114 vb->planes[plane].dbuf_mapped = 1;
1115 }
1116
1117 /*
1118 * Call driver-specific initialization on the newly acquired buffer,
1119 * if provided.
1120 */
1121 ret = call_qop(q, buf_init, vb);
1122 if (ret) {
1123 dprintk(1, "qbuf: buffer initialization failed\n");
1124 goto err;
1125 }
1126
1127 /*
1128 * Now that everything is in order, copy relevant information
1129 * provided by userspace.
1130 */
1131 for (plane = 0; plane < vb->num_planes; ++plane)
1132 vb->v4l2_planes[plane] = planes[plane];
1133
1134 return 0;
1135err:
1136 /* In case of errors, release planes that were already acquired */
1137 __vb2_buf_dmabuf_put(vb);
1138
1139 return ret;
1140}
1141
1142/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001143 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1144 */
1145static void __enqueue_in_driver(struct vb2_buffer *vb)
1146{
1147 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001148 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001149
1150 vb->state = VB2_BUF_STATE_ACTIVE;
1151 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001152
1153 /* sync buffers */
1154 for (plane = 0; plane < vb->num_planes; ++plane)
1155 call_memop(q, prepare, vb->planes[plane].mem_priv);
1156
Pawel Osciake23ccc02010-10-11 10:56:41 -03001157 q->ops->buf_queue(vb);
1158}
1159
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001160static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001161{
1162 struct vb2_queue *q = vb->vb2_queue;
1163 int ret;
1164
1165 switch (q->memory) {
1166 case V4L2_MEMORY_MMAP:
1167 ret = __qbuf_mmap(vb, b);
1168 break;
1169 case V4L2_MEMORY_USERPTR:
1170 ret = __qbuf_userptr(vb, b);
1171 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001172 case V4L2_MEMORY_DMABUF:
1173 ret = __qbuf_dmabuf(vb, b);
1174 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001175 default:
1176 WARN(1, "Invalid queue type\n");
1177 ret = -EINVAL;
1178 }
1179
1180 if (!ret)
1181 ret = call_qop(q, buf_prepare, vb);
1182 if (ret)
1183 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1184 else
1185 vb->state = VB2_BUF_STATE_PREPARED;
1186
1187 return ret;
1188}
1189
Pawel Osciake23ccc02010-10-11 10:56:41 -03001190/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001191 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1192 * @q: videobuf2 queue
1193 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1194 * handler in driver
1195 *
1196 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1197 * This function:
1198 * 1) verifies the passed buffer,
1199 * 2) calls buf_prepare callback in the driver (if provided), in which
1200 * driver-specific buffer initialization can be performed,
1201 *
1202 * The return values from this function are intended to be directly returned
1203 * from vidioc_prepare_buf handler in driver.
1204 */
1205int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1206{
1207 struct vb2_buffer *vb;
1208 int ret;
1209
1210 if (q->fileio) {
1211 dprintk(1, "%s(): file io in progress\n", __func__);
1212 return -EBUSY;
1213 }
1214
1215 if (b->type != q->type) {
1216 dprintk(1, "%s(): invalid buffer type\n", __func__);
1217 return -EINVAL;
1218 }
1219
1220 if (b->index >= q->num_buffers) {
1221 dprintk(1, "%s(): buffer index out of range\n", __func__);
1222 return -EINVAL;
1223 }
1224
1225 vb = q->bufs[b->index];
1226 if (NULL == vb) {
1227 /* Should never happen */
1228 dprintk(1, "%s(): buffer is NULL\n", __func__);
1229 return -EINVAL;
1230 }
1231
1232 if (b->memory != q->memory) {
1233 dprintk(1, "%s(): invalid memory type\n", __func__);
1234 return -EINVAL;
1235 }
1236
1237 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1238 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1239 return -EINVAL;
1240 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001241 ret = __verify_planes_array(vb, b);
1242 if (ret < 0)
1243 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001244 ret = __buf_prepare(vb, b);
1245 if (ret < 0)
1246 return ret;
1247
1248 __fill_v4l2_buffer(vb, b);
1249
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1253
1254/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001255 * vb2_qbuf() - Queue a buffer from userspace
1256 * @q: videobuf2 queue
1257 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1258 * in driver
1259 *
1260 * Should be called from vidioc_qbuf ioctl handler of a driver.
1261 * This function:
1262 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001263 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1264 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001265 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1266 * callback for processing.
1267 *
1268 * The return values from this function are intended to be directly returned
1269 * from vidioc_qbuf handler in driver.
1270 */
1271int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1272{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001273 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001274 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001275 int ret = 0;
1276
1277 /*
1278 * In case of user pointer buffers vb2 allocator needs to get direct
1279 * access to userspace pages. This requires getting read access on
1280 * mmap semaphore in the current process structure. The same
1281 * semaphore is taken before calling mmap operation, while both mmap
1282 * and qbuf are called by the driver or v4l2 core with driver's lock
1283 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1284 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1285 * release driver's lock, takes mmap_sem and then takes again driver's
1286 * lock.
1287 *
1288 * To avoid race with other vb2 calls, which might be called after
1289 * releasing driver's lock, this operation is performed at the
1290 * beggining of qbuf processing. This way the queue status is
1291 * consistent after getting driver's lock back.
1292 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001293 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001294 mmap_sem = &current->mm->mmap_sem;
1295 call_qop(q, wait_prepare, q);
1296 down_read(mmap_sem);
1297 call_qop(q, wait_finish, q);
1298 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001299
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001300 if (q->fileio) {
1301 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001302 ret = -EBUSY;
1303 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001304 }
1305
Pawel Osciake23ccc02010-10-11 10:56:41 -03001306 if (b->type != q->type) {
1307 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001308 ret = -EINVAL;
1309 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001310 }
1311
1312 if (b->index >= q->num_buffers) {
1313 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001314 ret = -EINVAL;
1315 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001316 }
1317
1318 vb = q->bufs[b->index];
1319 if (NULL == vb) {
1320 /* Should never happen */
1321 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001322 ret = -EINVAL;
1323 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001324 }
1325
1326 if (b->memory != q->memory) {
1327 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001328 ret = -EINVAL;
1329 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001330 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001331 ret = __verify_planes_array(vb, b);
1332 if (ret)
1333 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001334
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001335 switch (vb->state) {
1336 case VB2_BUF_STATE_DEQUEUED:
1337 ret = __buf_prepare(vb, b);
1338 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001339 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001340 case VB2_BUF_STATE_PREPARED:
1341 break;
1342 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001343 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001344 ret = -EINVAL;
1345 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001346 }
1347
Pawel Osciake23ccc02010-10-11 10:56:41 -03001348 /*
1349 * Add to the queued buffers list, a buffer will stay on it until
1350 * dequeued in dqbuf.
1351 */
1352 list_add_tail(&vb->queued_entry, &q->queued_list);
1353 vb->state = VB2_BUF_STATE_QUEUED;
1354
1355 /*
1356 * If already streaming, give the buffer to driver for processing.
1357 * If not, the buffer will be given to driver on next streamon.
1358 */
1359 if (q->streaming)
1360 __enqueue_in_driver(vb);
1361
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001362 /* Fill buffer information for the userspace */
1363 __fill_v4l2_buffer(vb, b);
1364
Pawel Osciake23ccc02010-10-11 10:56:41 -03001365 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001366unlock:
1367 if (mmap_sem)
1368 up_read(mmap_sem);
1369 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001370}
1371EXPORT_SYMBOL_GPL(vb2_qbuf);
1372
1373/**
1374 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1375 * for dequeuing
1376 *
1377 * Will sleep if required for nonblocking == false.
1378 */
1379static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1380{
1381 /*
1382 * All operations on vb_done_list are performed under done_lock
1383 * spinlock protection. However, buffers may be removed from
1384 * it and returned to userspace only while holding both driver's
1385 * lock and the done_lock spinlock. Thus we can be sure that as
1386 * long as we hold the driver's lock, the list will remain not
1387 * empty if list_empty() check succeeds.
1388 */
1389
1390 for (;;) {
1391 int ret;
1392
1393 if (!q->streaming) {
1394 dprintk(1, "Streaming off, will not wait for buffers\n");
1395 return -EINVAL;
1396 }
1397
1398 if (!list_empty(&q->done_list)) {
1399 /*
1400 * Found a buffer that we were waiting for.
1401 */
1402 break;
1403 }
1404
1405 if (nonblocking) {
1406 dprintk(1, "Nonblocking and no buffers to dequeue, "
1407 "will not wait\n");
1408 return -EAGAIN;
1409 }
1410
1411 /*
1412 * We are streaming and blocking, wait for another buffer to
1413 * become ready or for streamoff. Driver's lock is released to
1414 * allow streamoff or qbuf to be called while waiting.
1415 */
1416 call_qop(q, wait_prepare, q);
1417
1418 /*
1419 * All locks have been released, it is safe to sleep now.
1420 */
1421 dprintk(3, "Will sleep waiting for buffers\n");
1422 ret = wait_event_interruptible(q->done_wq,
1423 !list_empty(&q->done_list) || !q->streaming);
1424
1425 /*
1426 * We need to reevaluate both conditions again after reacquiring
1427 * the locks or return an error if one occurred.
1428 */
1429 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001430 if (ret) {
1431 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001432 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001433 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001434 }
1435 return 0;
1436}
1437
1438/**
1439 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1440 *
1441 * Will sleep if required for nonblocking == false.
1442 */
1443static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001444 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001445{
1446 unsigned long flags;
1447 int ret;
1448
1449 /*
1450 * Wait for at least one buffer to become available on the done_list.
1451 */
1452 ret = __vb2_wait_for_done_vb(q, nonblocking);
1453 if (ret)
1454 return ret;
1455
1456 /*
1457 * Driver's lock has been held since we last verified that done_list
1458 * is not empty, so no need for another list_empty(done_list) check.
1459 */
1460 spin_lock_irqsave(&q->done_lock, flags);
1461 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001462 /*
1463 * Only remove the buffer from done_list if v4l2_buffer can handle all
1464 * the planes.
1465 */
1466 ret = __verify_planes_array(*vb, b);
1467 if (!ret)
1468 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001469 spin_unlock_irqrestore(&q->done_lock, flags);
1470
Hans Verkuil32a77262012-09-28 06:12:53 -03001471 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001472}
1473
1474/**
1475 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1476 * @q: videobuf2 queue
1477 *
1478 * This function will wait until all buffers that have been given to the driver
1479 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1480 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1481 * taken, for example from stop_streaming() callback.
1482 */
1483int vb2_wait_for_all_buffers(struct vb2_queue *q)
1484{
1485 if (!q->streaming) {
1486 dprintk(1, "Streaming off, will not wait for buffers\n");
1487 return -EINVAL;
1488 }
1489
1490 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1491 return 0;
1492}
1493EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1494
1495/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001496 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1497 */
1498static void __vb2_dqbuf(struct vb2_buffer *vb)
1499{
1500 struct vb2_queue *q = vb->vb2_queue;
1501 unsigned int i;
1502
1503 /* nothing to do if the buffer is already dequeued */
1504 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1505 return;
1506
1507 vb->state = VB2_BUF_STATE_DEQUEUED;
1508
1509 /* unmap DMABUF buffer */
1510 if (q->memory == V4L2_MEMORY_DMABUF)
1511 for (i = 0; i < vb->num_planes; ++i) {
1512 if (!vb->planes[i].dbuf_mapped)
1513 continue;
1514 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1515 vb->planes[i].dbuf_mapped = 0;
1516 }
1517}
1518
1519/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001520 * vb2_dqbuf() - Dequeue a buffer to the userspace
1521 * @q: videobuf2 queue
1522 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1523 * in driver
1524 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1525 * buffers ready for dequeuing are present. Normally the driver
1526 * would be passing (file->f_flags & O_NONBLOCK) here
1527 *
1528 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1529 * This function:
1530 * 1) verifies the passed buffer,
1531 * 2) calls buf_finish callback in the driver (if provided), in which
1532 * driver can perform any additional operations that may be required before
1533 * returning the buffer to userspace, such as cache sync,
1534 * 3) the buffer struct members are filled with relevant information for
1535 * the userspace.
1536 *
1537 * The return values from this function are intended to be directly returned
1538 * from vidioc_dqbuf handler in driver.
1539 */
1540int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1541{
1542 struct vb2_buffer *vb = NULL;
1543 int ret;
1544
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001545 if (q->fileio) {
1546 dprintk(1, "dqbuf: file io in progress\n");
1547 return -EBUSY;
1548 }
1549
Pawel Osciake23ccc02010-10-11 10:56:41 -03001550 if (b->type != q->type) {
1551 dprintk(1, "dqbuf: invalid buffer type\n");
1552 return -EINVAL;
1553 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001554 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1555 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001556 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001557
1558 ret = call_qop(q, buf_finish, vb);
1559 if (ret) {
1560 dprintk(1, "dqbuf: buffer finish failed\n");
1561 return ret;
1562 }
1563
1564 switch (vb->state) {
1565 case VB2_BUF_STATE_DONE:
1566 dprintk(3, "dqbuf: Returning done buffer\n");
1567 break;
1568 case VB2_BUF_STATE_ERROR:
1569 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1570 break;
1571 default:
1572 dprintk(1, "dqbuf: Invalid buffer state\n");
1573 return -EINVAL;
1574 }
1575
1576 /* Fill buffer information for the userspace */
1577 __fill_v4l2_buffer(vb, b);
1578 /* Remove from videobuf queue */
1579 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001580 /* go back to dequeued state */
1581 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001582
1583 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1584 vb->v4l2_buf.index, vb->state);
1585
Pawel Osciake23ccc02010-10-11 10:56:41 -03001586 return 0;
1587}
1588EXPORT_SYMBOL_GPL(vb2_dqbuf);
1589
1590/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001591 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1592 *
1593 * Removes all queued buffers from driver's queue and all buffers queued by
1594 * userspace from videobuf's queue. Returns to state after reqbufs.
1595 */
1596static void __vb2_queue_cancel(struct vb2_queue *q)
1597{
1598 unsigned int i;
1599
1600 /*
1601 * Tell driver to stop all transactions and release all queued
1602 * buffers.
1603 */
1604 if (q->streaming)
1605 call_qop(q, stop_streaming, q);
1606 q->streaming = 0;
1607
1608 /*
1609 * Remove all buffers from videobuf's list...
1610 */
1611 INIT_LIST_HEAD(&q->queued_list);
1612 /*
1613 * ...and done list; userspace will not receive any buffers it
1614 * has not already dequeued before initiating cancel.
1615 */
1616 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001617 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001618 wake_up_all(&q->done_wq);
1619
1620 /*
1621 * Reinitialize all buffers for next use.
1622 */
1623 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001624 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001625}
1626
1627/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001628 * vb2_streamon - start streaming
1629 * @q: videobuf2 queue
1630 * @type: type argument passed from userspace to vidioc_streamon handler
1631 *
1632 * Should be called from vidioc_streamon handler of a driver.
1633 * This function:
1634 * 1) verifies current state
1635 * 2) passes any previously queued buffers to the driver and starts streaming
1636 *
1637 * The return values from this function are intended to be directly returned
1638 * from vidioc_streamon handler in the driver.
1639 */
1640int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1641{
1642 struct vb2_buffer *vb;
1643 int ret;
1644
1645 if (q->fileio) {
1646 dprintk(1, "streamon: file io in progress\n");
1647 return -EBUSY;
1648 }
1649
1650 if (type != q->type) {
1651 dprintk(1, "streamon: invalid stream type\n");
1652 return -EINVAL;
1653 }
1654
1655 if (q->streaming) {
1656 dprintk(1, "streamon: already streaming\n");
1657 return -EBUSY;
1658 }
1659
1660 /*
1661 * If any buffers were queued before streamon,
1662 * we can now pass them to driver for processing.
1663 */
1664 list_for_each_entry(vb, &q->queued_list, queued_entry)
1665 __enqueue_in_driver(vb);
1666
1667 /*
1668 * Let driver notice that streaming state has been enabled.
1669 */
1670 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1671 if (ret) {
1672 dprintk(1, "streamon: driver refused to start streaming\n");
1673 __vb2_queue_cancel(q);
1674 return ret;
1675 }
1676
1677 q->streaming = 1;
1678
1679 dprintk(3, "Streamon successful\n");
1680 return 0;
1681}
1682EXPORT_SYMBOL_GPL(vb2_streamon);
1683
1684
1685/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001686 * vb2_streamoff - stop streaming
1687 * @q: videobuf2 queue
1688 * @type: type argument passed from userspace to vidioc_streamoff handler
1689 *
1690 * Should be called from vidioc_streamoff handler of a driver.
1691 * This function:
1692 * 1) verifies current state,
1693 * 2) stop streaming and dequeues any queued buffers, including those previously
1694 * passed to the driver (after waiting for the driver to finish).
1695 *
1696 * This call can be used for pausing playback.
1697 * The return values from this function are intended to be directly returned
1698 * from vidioc_streamoff handler in the driver
1699 */
1700int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1701{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001702 if (q->fileio) {
1703 dprintk(1, "streamoff: file io in progress\n");
1704 return -EBUSY;
1705 }
1706
Pawel Osciake23ccc02010-10-11 10:56:41 -03001707 if (type != q->type) {
1708 dprintk(1, "streamoff: invalid stream type\n");
1709 return -EINVAL;
1710 }
1711
1712 if (!q->streaming) {
1713 dprintk(1, "streamoff: not streaming\n");
1714 return -EINVAL;
1715 }
1716
1717 /*
1718 * Cancel will pause streaming and remove all buffers from the driver
1719 * and videobuf, effectively returning control over them to userspace.
1720 */
1721 __vb2_queue_cancel(q);
1722
1723 dprintk(3, "Streamoff successful\n");
1724 return 0;
1725}
1726EXPORT_SYMBOL_GPL(vb2_streamoff);
1727
1728/**
1729 * __find_plane_by_offset() - find plane associated with the given offset off
1730 */
1731static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1732 unsigned int *_buffer, unsigned int *_plane)
1733{
1734 struct vb2_buffer *vb;
1735 unsigned int buffer, plane;
1736
1737 /*
1738 * Go over all buffers and their planes, comparing the given offset
1739 * with an offset assigned to each plane. If a match is found,
1740 * return its buffer and plane numbers.
1741 */
1742 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1743 vb = q->bufs[buffer];
1744
1745 for (plane = 0; plane < vb->num_planes; ++plane) {
1746 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1747 *_buffer = buffer;
1748 *_plane = plane;
1749 return 0;
1750 }
1751 }
1752 }
1753
1754 return -EINVAL;
1755}
1756
1757/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001758 * vb2_expbuf() - Export a buffer as a file descriptor
1759 * @q: videobuf2 queue
1760 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1761 * handler in driver
1762 *
1763 * The return values from this function are intended to be directly returned
1764 * from vidioc_expbuf handler in driver.
1765 */
1766int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1767{
1768 struct vb2_buffer *vb = NULL;
1769 struct vb2_plane *vb_plane;
1770 int ret;
1771 struct dma_buf *dbuf;
1772
1773 if (q->memory != V4L2_MEMORY_MMAP) {
1774 dprintk(1, "Queue is not currently set up for mmap\n");
1775 return -EINVAL;
1776 }
1777
1778 if (!q->mem_ops->get_dmabuf) {
1779 dprintk(1, "Queue does not support DMA buffer exporting\n");
1780 return -EINVAL;
1781 }
1782
1783 if (eb->flags & ~O_CLOEXEC) {
1784 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1785 return -EINVAL;
1786 }
1787
1788 if (eb->type != q->type) {
1789 dprintk(1, "qbuf: invalid buffer type\n");
1790 return -EINVAL;
1791 }
1792
1793 if (eb->index >= q->num_buffers) {
1794 dprintk(1, "buffer index out of range\n");
1795 return -EINVAL;
1796 }
1797
1798 vb = q->bufs[eb->index];
1799
1800 if (eb->plane >= vb->num_planes) {
1801 dprintk(1, "buffer plane out of range\n");
1802 return -EINVAL;
1803 }
1804
1805 vb_plane = &vb->planes[eb->plane];
1806
1807 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1808 if (IS_ERR_OR_NULL(dbuf)) {
1809 dprintk(1, "Failed to export buffer %d, plane %d\n",
1810 eb->index, eb->plane);
1811 return -EINVAL;
1812 }
1813
1814 ret = dma_buf_fd(dbuf, eb->flags);
1815 if (ret < 0) {
1816 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1817 eb->index, eb->plane, ret);
1818 dma_buf_put(dbuf);
1819 return ret;
1820 }
1821
1822 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1823 eb->index, eb->plane, ret);
1824 eb->fd = ret;
1825
1826 return 0;
1827}
1828EXPORT_SYMBOL_GPL(vb2_expbuf);
1829
1830/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001831 * vb2_mmap() - map video buffers into application address space
1832 * @q: videobuf2 queue
1833 * @vma: vma passed to the mmap file operation handler in the driver
1834 *
1835 * Should be called from mmap file operation handler of a driver.
1836 * This function maps one plane of one of the available video buffers to
1837 * userspace. To map whole video memory allocated on reqbufs, this function
1838 * has to be called once per each plane per each buffer previously allocated.
1839 *
1840 * When the userspace application calls mmap, it passes to it an offset returned
1841 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1842 * a "cookie", which is then used to identify the plane to be mapped.
1843 * This function finds a plane with a matching offset and a mapping is performed
1844 * by the means of a provided memory operation.
1845 *
1846 * The return values from this function are intended to be directly returned
1847 * from the mmap handler in driver.
1848 */
1849int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1850{
1851 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001852 struct vb2_buffer *vb;
1853 unsigned int buffer, plane;
1854 int ret;
1855
1856 if (q->memory != V4L2_MEMORY_MMAP) {
1857 dprintk(1, "Queue is not currently set up for mmap\n");
1858 return -EINVAL;
1859 }
1860
1861 /*
1862 * Check memory area access mode.
1863 */
1864 if (!(vma->vm_flags & VM_SHARED)) {
1865 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1866 return -EINVAL;
1867 }
1868 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1869 if (!(vma->vm_flags & VM_WRITE)) {
1870 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1871 return -EINVAL;
1872 }
1873 } else {
1874 if (!(vma->vm_flags & VM_READ)) {
1875 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1876 return -EINVAL;
1877 }
1878 }
1879
1880 /*
1881 * Find the plane corresponding to the offset passed by userspace.
1882 */
1883 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1884 if (ret)
1885 return ret;
1886
1887 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001888
Seung-Woo Kim068a0df2013-04-11 23:57:57 -03001889 if (vb->v4l2_planes[plane].length < (vma->vm_end - vma->vm_start)) {
1890 dprintk(1, "Invalid length\n");
1891 return -EINVAL;
1892 }
1893
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001894 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001895 if (ret)
1896 return ret;
1897
Pawel Osciake23ccc02010-10-11 10:56:41 -03001898 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1899 return 0;
1900}
1901EXPORT_SYMBOL_GPL(vb2_mmap);
1902
Scott Jiang6f524ec2011-09-21 09:25:23 -03001903#ifndef CONFIG_MMU
1904unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1905 unsigned long addr,
1906 unsigned long len,
1907 unsigned long pgoff,
1908 unsigned long flags)
1909{
1910 unsigned long off = pgoff << PAGE_SHIFT;
1911 struct vb2_buffer *vb;
1912 unsigned int buffer, plane;
1913 int ret;
1914
1915 if (q->memory != V4L2_MEMORY_MMAP) {
1916 dprintk(1, "Queue is not currently set up for mmap\n");
1917 return -EINVAL;
1918 }
1919
1920 /*
1921 * Find the plane corresponding to the offset passed by userspace.
1922 */
1923 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1924 if (ret)
1925 return ret;
1926
1927 vb = q->bufs[buffer];
1928
1929 return (unsigned long)vb2_plane_vaddr(vb, plane);
1930}
1931EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1932#endif
1933
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001934static int __vb2_init_fileio(struct vb2_queue *q, int read);
1935static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001936
1937/**
1938 * vb2_poll() - implements poll userspace operation
1939 * @q: videobuf2 queue
1940 * @file: file argument passed to the poll file operation handler
1941 * @wait: wait argument passed to the poll file operation handler
1942 *
1943 * This function implements poll file operation handler for a driver.
1944 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1945 * be informed that the file descriptor of a video device is available for
1946 * reading.
1947 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1948 * will be reported as available for writing.
1949 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03001950 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1951 * pending events.
1952 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03001953 * The return values from this function are intended to be directly returned
1954 * from poll handler in driver.
1955 */
1956unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1957{
Hans Verkuil95213ce2011-07-13 04:26:52 -03001958 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001959 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001960 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03001961 unsigned int res = 0;
1962 unsigned long flags;
1963
1964 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1965 struct v4l2_fh *fh = file->private_data;
1966
1967 if (v4l2_event_pending(fh))
1968 res = POLLPRI;
1969 else if (req_events & POLLPRI)
1970 poll_wait(file, &fh->wait, wait);
1971 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001972
Hans Verkuilcd138232013-01-30 13:29:02 -03001973 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
1974 return res;
1975 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
1976 return res;
1977
Pawel Osciake23ccc02010-10-11 10:56:41 -03001978 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001979 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001980 */
1981 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001982 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1983 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001984 if (__vb2_init_fileio(q, 1))
1985 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001986 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001987 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1988 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001989 if (__vb2_init_fileio(q, 0))
1990 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001991 /*
1992 * Write to OUTPUT queue can be done immediately.
1993 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03001994 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001995 }
1996 }
1997
1998 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001999 * There is nothing to wait for if no buffers have already been queued.
2000 */
2001 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03002002 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002003
2004 poll_wait(file, &q->done_wq, wait);
2005
2006 /*
2007 * Take first buffer available for dequeuing.
2008 */
2009 spin_lock_irqsave(&q->done_lock, flags);
2010 if (!list_empty(&q->done_list))
2011 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2012 done_entry);
2013 spin_unlock_irqrestore(&q->done_lock, flags);
2014
2015 if (vb && (vb->state == VB2_BUF_STATE_DONE
2016 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002017 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2018 res | POLLOUT | POLLWRNORM :
2019 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002020 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002021 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002022}
2023EXPORT_SYMBOL_GPL(vb2_poll);
2024
2025/**
2026 * vb2_queue_init() - initialize a videobuf2 queue
2027 * @q: videobuf2 queue; this structure should be allocated in driver
2028 *
2029 * The vb2_queue structure should be allocated by the driver. The driver is
2030 * responsible of clearing it's content and setting initial values for some
2031 * required entries before calling this function.
2032 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2033 * to the struct vb2_queue description in include/media/videobuf2-core.h
2034 * for more information.
2035 */
2036int vb2_queue_init(struct vb2_queue *q)
2037{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002038 /*
2039 * Sanity check
2040 */
2041 if (WARN_ON(!q) ||
2042 WARN_ON(!q->ops) ||
2043 WARN_ON(!q->mem_ops) ||
2044 WARN_ON(!q->type) ||
2045 WARN_ON(!q->io_modes) ||
2046 WARN_ON(!q->ops->queue_setup) ||
Kamil Debski6aa69f92013-01-25 06:29:57 -03002047 WARN_ON(!q->ops->buf_queue) ||
2048 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002049 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002050
Kamil Debski6aa69f92013-01-25 06:29:57 -03002051 /* Warn that the driver should choose an appropriate timestamp type */
2052 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2053
Pawel Osciake23ccc02010-10-11 10:56:41 -03002054 INIT_LIST_HEAD(&q->queued_list);
2055 INIT_LIST_HEAD(&q->done_list);
2056 spin_lock_init(&q->done_lock);
2057 init_waitqueue_head(&q->done_wq);
2058
2059 if (q->buf_struct_size == 0)
2060 q->buf_struct_size = sizeof(struct vb2_buffer);
2061
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(vb2_queue_init);
2065
2066/**
2067 * vb2_queue_release() - stop streaming, release the queue and free memory
2068 * @q: videobuf2 queue
2069 *
2070 * This function stops streaming and performs necessary clean ups, including
2071 * freeing video buffer memory. The driver is responsible for freeing
2072 * the vb2_queue structure itself.
2073 */
2074void vb2_queue_release(struct vb2_queue *q)
2075{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002076 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002077 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002078 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002079}
2080EXPORT_SYMBOL_GPL(vb2_queue_release);
2081
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002082/**
2083 * struct vb2_fileio_buf - buffer context used by file io emulator
2084 *
2085 * vb2 provides a compatibility layer and emulator of file io (read and
2086 * write) calls on top of streaming API. This structure is used for
2087 * tracking context related to the buffers.
2088 */
2089struct vb2_fileio_buf {
2090 void *vaddr;
2091 unsigned int size;
2092 unsigned int pos;
2093 unsigned int queued:1;
2094};
2095
2096/**
2097 * struct vb2_fileio_data - queue context used by file io emulator
2098 *
2099 * vb2 provides a compatibility layer and emulator of file io (read and
2100 * write) calls on top of streaming API. For proper operation it required
2101 * this structure to save the driver state between each call of the read
2102 * or write function.
2103 */
2104struct vb2_fileio_data {
2105 struct v4l2_requestbuffers req;
2106 struct v4l2_buffer b;
2107 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2108 unsigned int index;
2109 unsigned int q_count;
2110 unsigned int dq_count;
2111 unsigned int flags;
2112};
2113
2114/**
2115 * __vb2_init_fileio() - initialize file io emulator
2116 * @q: videobuf2 queue
2117 * @read: mode selector (1 means read, 0 means write)
2118 */
2119static int __vb2_init_fileio(struct vb2_queue *q, int read)
2120{
2121 struct vb2_fileio_data *fileio;
2122 int i, ret;
2123 unsigned int count = 0;
2124
2125 /*
2126 * Sanity check
2127 */
2128 if ((read && !(q->io_modes & VB2_READ)) ||
2129 (!read && !(q->io_modes & VB2_WRITE)))
2130 BUG();
2131
2132 /*
2133 * Check if device supports mapping buffers to kernel virtual space.
2134 */
2135 if (!q->mem_ops->vaddr)
2136 return -EBUSY;
2137
2138 /*
2139 * Check if streaming api has not been already activated.
2140 */
2141 if (q->streaming || q->num_buffers > 0)
2142 return -EBUSY;
2143
2144 /*
2145 * Start with count 1, driver can increase it in queue_setup()
2146 */
2147 count = 1;
2148
2149 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2150 (read) ? "read" : "write", count, q->io_flags);
2151
2152 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2153 if (fileio == NULL)
2154 return -ENOMEM;
2155
2156 fileio->flags = q->io_flags;
2157
2158 /*
2159 * Request buffers and use MMAP type to force driver
2160 * to allocate buffers by itself.
2161 */
2162 fileio->req.count = count;
2163 fileio->req.memory = V4L2_MEMORY_MMAP;
2164 fileio->req.type = q->type;
2165 ret = vb2_reqbufs(q, &fileio->req);
2166 if (ret)
2167 goto err_kfree;
2168
2169 /*
2170 * Check if plane_count is correct
2171 * (multiplane buffers are not supported).
2172 */
2173 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002174 ret = -EBUSY;
2175 goto err_reqbufs;
2176 }
2177
2178 /*
2179 * Get kernel address of each buffer.
2180 */
2181 for (i = 0; i < q->num_buffers; i++) {
2182 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2183 if (fileio->bufs[i].vaddr == NULL)
2184 goto err_reqbufs;
2185 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2186 }
2187
2188 /*
2189 * Read mode requires pre queuing of all buffers.
2190 */
2191 if (read) {
2192 /*
2193 * Queue all buffers.
2194 */
2195 for (i = 0; i < q->num_buffers; i++) {
2196 struct v4l2_buffer *b = &fileio->b;
2197 memset(b, 0, sizeof(*b));
2198 b->type = q->type;
2199 b->memory = q->memory;
2200 b->index = i;
2201 ret = vb2_qbuf(q, b);
2202 if (ret)
2203 goto err_reqbufs;
2204 fileio->bufs[i].queued = 1;
2205 }
2206
2207 /*
2208 * Start streaming.
2209 */
2210 ret = vb2_streamon(q, q->type);
2211 if (ret)
2212 goto err_reqbufs;
2213 }
2214
2215 q->fileio = fileio;
2216
2217 return ret;
2218
2219err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002220 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002221 vb2_reqbufs(q, &fileio->req);
2222
2223err_kfree:
2224 kfree(fileio);
2225 return ret;
2226}
2227
2228/**
2229 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2230 * @q: videobuf2 queue
2231 */
2232static int __vb2_cleanup_fileio(struct vb2_queue *q)
2233{
2234 struct vb2_fileio_data *fileio = q->fileio;
2235
2236 if (fileio) {
2237 /*
2238 * Hack fileio context to enable direct calls to vb2 ioctl
2239 * interface.
2240 */
2241 q->fileio = NULL;
2242
2243 vb2_streamoff(q, q->type);
2244 fileio->req.count = 0;
2245 vb2_reqbufs(q, &fileio->req);
2246 kfree(fileio);
2247 dprintk(3, "file io emulator closed\n");
2248 }
2249 return 0;
2250}
2251
2252/**
2253 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2254 * @q: videobuf2 queue
2255 * @data: pointed to target userspace buffer
2256 * @count: number of bytes to read or write
2257 * @ppos: file handle position tracking pointer
2258 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2259 * @read: access mode selector (1 means read, 0 means write)
2260 */
2261static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2262 loff_t *ppos, int nonblock, int read)
2263{
2264 struct vb2_fileio_data *fileio;
2265 struct vb2_fileio_buf *buf;
2266 int ret, index;
2267
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002268 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002269 read ? "read" : "write", (long)*ppos, count,
2270 nonblock ? "non" : "");
2271
2272 if (!data)
2273 return -EINVAL;
2274
2275 /*
2276 * Initialize emulator on first call.
2277 */
2278 if (!q->fileio) {
2279 ret = __vb2_init_fileio(q, read);
2280 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2281 if (ret)
2282 return ret;
2283 }
2284 fileio = q->fileio;
2285
2286 /*
2287 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2288 * The pointer will be restored before returning from this function.
2289 */
2290 q->fileio = NULL;
2291
2292 index = fileio->index;
2293 buf = &fileio->bufs[index];
2294
2295 /*
2296 * Check if we need to dequeue the buffer.
2297 */
2298 if (buf->queued) {
2299 struct vb2_buffer *vb;
2300
2301 /*
2302 * Call vb2_dqbuf to get buffer back.
2303 */
2304 memset(&fileio->b, 0, sizeof(fileio->b));
2305 fileio->b.type = q->type;
2306 fileio->b.memory = q->memory;
2307 fileio->b.index = index;
2308 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2309 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2310 if (ret)
2311 goto end;
2312 fileio->dq_count += 1;
2313
2314 /*
2315 * Get number of bytes filled by the driver
2316 */
2317 vb = q->bufs[index];
2318 buf->size = vb2_get_plane_payload(vb, 0);
2319 buf->queued = 0;
2320 }
2321
2322 /*
2323 * Limit count on last few bytes of the buffer.
2324 */
2325 if (buf->pos + count > buf->size) {
2326 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002327 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002328 }
2329
2330 /*
2331 * Transfer data to userspace.
2332 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002333 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002334 count, index, buf->pos);
2335 if (read)
2336 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2337 else
2338 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2339 if (ret) {
2340 dprintk(3, "file io: error copying data\n");
2341 ret = -EFAULT;
2342 goto end;
2343 }
2344
2345 /*
2346 * Update counters.
2347 */
2348 buf->pos += count;
2349 *ppos += count;
2350
2351 /*
2352 * Queue next buffer if required.
2353 */
2354 if (buf->pos == buf->size ||
2355 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2356 /*
2357 * Check if this is the last buffer to read.
2358 */
2359 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2360 fileio->dq_count == 1) {
2361 dprintk(3, "file io: read limit reached\n");
2362 /*
2363 * Restore fileio pointer and release the context.
2364 */
2365 q->fileio = fileio;
2366 return __vb2_cleanup_fileio(q);
2367 }
2368
2369 /*
2370 * Call vb2_qbuf and give buffer to the driver.
2371 */
2372 memset(&fileio->b, 0, sizeof(fileio->b));
2373 fileio->b.type = q->type;
2374 fileio->b.memory = q->memory;
2375 fileio->b.index = index;
2376 fileio->b.bytesused = buf->pos;
2377 ret = vb2_qbuf(q, &fileio->b);
2378 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2379 if (ret)
2380 goto end;
2381
2382 /*
2383 * Buffer has been queued, update the status
2384 */
2385 buf->pos = 0;
2386 buf->queued = 1;
2387 buf->size = q->bufs[0]->v4l2_planes[0].length;
2388 fileio->q_count += 1;
2389
2390 /*
2391 * Switch to the next buffer
2392 */
2393 fileio->index = (index + 1) % q->num_buffers;
2394
2395 /*
2396 * Start streaming if required.
2397 */
2398 if (!read && !q->streaming) {
2399 ret = vb2_streamon(q, q->type);
2400 if (ret)
2401 goto end;
2402 }
2403 }
2404
2405 /*
2406 * Return proper number of bytes processed.
2407 */
2408 if (ret == 0)
2409 ret = count;
2410end:
2411 /*
2412 * Restore the fileio context and block vb2 ioctl interface.
2413 */
2414 q->fileio = fileio;
2415 return ret;
2416}
2417
2418size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2419 loff_t *ppos, int nonblocking)
2420{
2421 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2422}
2423EXPORT_SYMBOL_GPL(vb2_read);
2424
2425size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2426 loff_t *ppos, int nonblocking)
2427{
2428 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2429}
2430EXPORT_SYMBOL_GPL(vb2_write);
2431
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002432
2433/*
2434 * The following functions are not part of the vb2 core API, but are helper
2435 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2436 * and struct vb2_ops.
2437 * They contain boilerplate code that most if not all drivers have to do
2438 * and so they simplify the driver code.
2439 */
2440
2441/* The queue is busy if there is a owner and you are not that owner. */
2442static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2443{
2444 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2445}
2446
2447/* vb2 ioctl helpers */
2448
2449int vb2_ioctl_reqbufs(struct file *file, void *priv,
2450 struct v4l2_requestbuffers *p)
2451{
2452 struct video_device *vdev = video_devdata(file);
2453 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2454
2455 if (res)
2456 return res;
2457 if (vb2_queue_is_busy(vdev, file))
2458 return -EBUSY;
2459 res = __reqbufs(vdev->queue, p);
2460 /* If count == 0, then the owner has released all buffers and he
2461 is no longer owner of the queue. Otherwise we have a new owner. */
2462 if (res == 0)
2463 vdev->queue->owner = p->count ? file->private_data : NULL;
2464 return res;
2465}
2466EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2467
2468int vb2_ioctl_create_bufs(struct file *file, void *priv,
2469 struct v4l2_create_buffers *p)
2470{
2471 struct video_device *vdev = video_devdata(file);
2472 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2473
2474 p->index = vdev->queue->num_buffers;
2475 /* If count == 0, then just check if memory and type are valid.
2476 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2477 if (p->count == 0)
2478 return res != -EBUSY ? res : 0;
2479 if (res)
2480 return res;
2481 if (vb2_queue_is_busy(vdev, file))
2482 return -EBUSY;
2483 res = __create_bufs(vdev->queue, p);
2484 if (res == 0)
2485 vdev->queue->owner = file->private_data;
2486 return res;
2487}
2488EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2489
2490int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2491 struct v4l2_buffer *p)
2492{
2493 struct video_device *vdev = video_devdata(file);
2494
2495 if (vb2_queue_is_busy(vdev, file))
2496 return -EBUSY;
2497 return vb2_prepare_buf(vdev->queue, p);
2498}
2499EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2500
2501int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2502{
2503 struct video_device *vdev = video_devdata(file);
2504
2505 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2506 return vb2_querybuf(vdev->queue, p);
2507}
2508EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2509
2510int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2511{
2512 struct video_device *vdev = video_devdata(file);
2513
2514 if (vb2_queue_is_busy(vdev, file))
2515 return -EBUSY;
2516 return vb2_qbuf(vdev->queue, p);
2517}
2518EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2519
2520int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2521{
2522 struct video_device *vdev = video_devdata(file);
2523
2524 if (vb2_queue_is_busy(vdev, file))
2525 return -EBUSY;
2526 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2527}
2528EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2529
2530int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2531{
2532 struct video_device *vdev = video_devdata(file);
2533
2534 if (vb2_queue_is_busy(vdev, file))
2535 return -EBUSY;
2536 return vb2_streamon(vdev->queue, i);
2537}
2538EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2539
2540int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2541{
2542 struct video_device *vdev = video_devdata(file);
2543
2544 if (vb2_queue_is_busy(vdev, file))
2545 return -EBUSY;
2546 return vb2_streamoff(vdev->queue, i);
2547}
2548EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2549
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002550int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2551{
2552 struct video_device *vdev = video_devdata(file);
2553
2554 if (vb2_queue_is_busy(vdev, file))
2555 return -EBUSY;
2556 return vb2_expbuf(vdev->queue, p);
2557}
2558EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2559
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002560/* v4l2_file_operations helpers */
2561
2562int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2563{
2564 struct video_device *vdev = video_devdata(file);
2565
2566 return vb2_mmap(vdev->queue, vma);
2567}
2568EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2569
2570int vb2_fop_release(struct file *file)
2571{
2572 struct video_device *vdev = video_devdata(file);
2573
2574 if (file->private_data == vdev->queue->owner) {
2575 vb2_queue_release(vdev->queue);
2576 vdev->queue->owner = NULL;
2577 }
2578 return v4l2_fh_release(file);
2579}
2580EXPORT_SYMBOL_GPL(vb2_fop_release);
2581
2582ssize_t vb2_fop_write(struct file *file, char __user *buf,
2583 size_t count, loff_t *ppos)
2584{
2585 struct video_device *vdev = video_devdata(file);
2586 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002587 int err = -EBUSY;
2588
Hans Verkuilcf533732012-07-31 04:02:25 -03002589 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002590 return -ERESTARTSYS;
2591 if (vb2_queue_is_busy(vdev, file))
2592 goto exit;
2593 err = vb2_write(vdev->queue, buf, count, ppos,
2594 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002595 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002596 vdev->queue->owner = file->private_data;
2597exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002598 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002599 mutex_unlock(lock);
2600 return err;
2601}
2602EXPORT_SYMBOL_GPL(vb2_fop_write);
2603
2604ssize_t vb2_fop_read(struct file *file, char __user *buf,
2605 size_t count, loff_t *ppos)
2606{
2607 struct video_device *vdev = video_devdata(file);
2608 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002609 int err = -EBUSY;
2610
Hans Verkuilcf533732012-07-31 04:02:25 -03002611 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002612 return -ERESTARTSYS;
2613 if (vb2_queue_is_busy(vdev, file))
2614 goto exit;
2615 err = vb2_read(vdev->queue, buf, count, ppos,
2616 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002617 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002618 vdev->queue->owner = file->private_data;
2619exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002620 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002621 mutex_unlock(lock);
2622 return err;
2623}
2624EXPORT_SYMBOL_GPL(vb2_fop_read);
2625
2626unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2627{
2628 struct video_device *vdev = video_devdata(file);
2629 struct vb2_queue *q = vdev->queue;
2630 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2631 unsigned long req_events = poll_requested_events(wait);
2632 unsigned res;
2633 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002634 bool must_lock = false;
2635
2636 /* Try to be smart: only lock if polling might start fileio,
2637 otherwise locking will only introduce unwanted delays. */
2638 if (q->num_buffers == 0 && q->fileio == NULL) {
2639 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2640 (req_events & (POLLIN | POLLRDNORM)))
2641 must_lock = true;
2642 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2643 (req_events & (POLLOUT | POLLWRNORM)))
2644 must_lock = true;
2645 }
2646
2647 /* If locking is needed, but this helper doesn't know how, then you
2648 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002649 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002650
Hans Verkuilcf533732012-07-31 04:02:25 -03002651 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002652 return POLLERR;
2653
2654 fileio = q->fileio;
2655
2656 res = vb2_poll(vdev->queue, file, wait);
2657
2658 /* If fileio was started, then we have a new queue owner. */
2659 if (must_lock && !fileio && q->fileio)
2660 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002661 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002662 mutex_unlock(lock);
2663 return res;
2664}
2665EXPORT_SYMBOL_GPL(vb2_fop_poll);
2666
2667#ifndef CONFIG_MMU
2668unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2669 unsigned long len, unsigned long pgoff, unsigned long flags)
2670{
2671 struct video_device *vdev = video_devdata(file);
2672
2673 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2674}
2675EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2676#endif
2677
2678/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2679
2680void vb2_ops_wait_prepare(struct vb2_queue *vq)
2681{
2682 mutex_unlock(vq->lock);
2683}
2684EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2685
2686void vb2_ops_wait_finish(struct vb2_queue *vq)
2687{
2688 mutex_lock(vq->lock);
2689}
2690EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2691
Pawel Osciake23ccc02010-10-11 10:56:41 -03002692MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002693MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002694MODULE_LICENSE("GPL");