blob: e02c4797b1c65fe46ef1352fa72f84e05317fe6c [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
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030043#define V4L2_BUFFER_STATE_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 | \
45 V4L2_BUF_FLAG_PREPARED)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030046
Pawel Osciake23ccc02010-10-11 10:56:41 -030047/**
48 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
49 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030050static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030051{
52 struct vb2_queue *q = vb->vb2_queue;
53 void *mem_priv;
54 int plane;
55
56 /* Allocate memory for all planes in this buffer */
57 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030058 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030059 q->plane_sizes[plane]);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030060 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030061 goto free;
62
63 /* Associate allocator private data with this plane */
64 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030065 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030066 }
67
68 return 0;
69free:
70 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030071 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030072 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030073 vb->planes[plane - 1].mem_priv = NULL;
74 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030075
76 return -ENOMEM;
77}
78
79/**
80 * __vb2_buf_mem_free() - free memory of the given buffer
81 */
82static void __vb2_buf_mem_free(struct vb2_buffer *vb)
83{
84 struct vb2_queue *q = vb->vb2_queue;
85 unsigned int plane;
86
87 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030088 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030089 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030090 dprintk(3, "Freed plane %d of buffer %d\n", plane,
91 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030092 }
93}
94
95/**
96 * __vb2_buf_userptr_put() - release userspace memory associated with
97 * a USERPTR buffer
98 */
99static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
100{
101 struct vb2_queue *q = vb->vb2_queue;
102 unsigned int plane;
103
104 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300105 if (vb->planes[plane].mem_priv)
106 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
107 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300108 }
109}
110
111/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300112 * __vb2_plane_dmabuf_put() - release memory associated with
113 * a DMABUF shared plane
114 */
115static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
116{
117 if (!p->mem_priv)
118 return;
119
120 if (p->dbuf_mapped)
121 call_memop(q, unmap_dmabuf, p->mem_priv);
122
123 call_memop(q, detach_dmabuf, p->mem_priv);
124 dma_buf_put(p->dbuf);
125 memset(p, 0, sizeof(*p));
126}
127
128/**
129 * __vb2_buf_dmabuf_put() - release memory associated with
130 * a DMABUF shared buffer
131 */
132static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
133{
134 struct vb2_queue *q = vb->vb2_queue;
135 unsigned int plane;
136
137 for (plane = 0; plane < vb->num_planes; ++plane)
138 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
139}
140
141/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300142 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
143 * every buffer on the queue
144 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300145static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300146{
147 unsigned int buffer, plane;
148 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300149 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300150
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300151 if (q->num_buffers) {
152 struct v4l2_plane *p;
153 vb = q->bufs[q->num_buffers - 1];
154 p = &vb->v4l2_planes[vb->num_planes - 1];
155 off = PAGE_ALIGN(p->m.mem_offset + p->length);
156 } else {
157 off = 0;
158 }
159
160 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300161 vb = q->bufs[buffer];
162 if (!vb)
163 continue;
164
165 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300166 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300167 vb->v4l2_planes[plane].m.mem_offset = off;
168
169 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
170 buffer, plane, off);
171
172 off += vb->v4l2_planes[plane].length;
173 off = PAGE_ALIGN(off);
174 }
175 }
176}
177
178/**
179 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
180 * video buffer memory for all buffers/planes on the queue and initializes the
181 * queue
182 *
183 * Returns the number of buffers successfully allocated.
184 */
185static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300186 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300187{
188 unsigned int buffer;
189 struct vb2_buffer *vb;
190 int ret;
191
192 for (buffer = 0; buffer < num_buffers; ++buffer) {
193 /* Allocate videobuf buffer structures */
194 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
195 if (!vb) {
196 dprintk(1, "Memory alloc for buffer struct failed\n");
197 break;
198 }
199
200 /* Length stores number of planes for multiplanar buffers */
201 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
202 vb->v4l2_buf.length = num_planes;
203
204 vb->state = VB2_BUF_STATE_DEQUEUED;
205 vb->vb2_queue = q;
206 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300207 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300208 vb->v4l2_buf.type = q->type;
209 vb->v4l2_buf.memory = memory;
210
211 /* Allocate video buffer memory for the MMAP type */
212 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300213 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300214 if (ret) {
215 dprintk(1, "Failed allocating memory for "
216 "buffer %d\n", buffer);
217 kfree(vb);
218 break;
219 }
220 /*
221 * Call the driver-provided buffer initialization
222 * callback, if given. An error in initialization
223 * results in queue setup failure.
224 */
225 ret = call_qop(q, buf_init, vb);
226 if (ret) {
227 dprintk(1, "Buffer %d %p initialization"
228 " failed\n", buffer, vb);
229 __vb2_buf_mem_free(vb);
230 kfree(vb);
231 break;
232 }
233 }
234
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300235 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300236 }
237
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300238 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300239
240 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300241 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242
243 return buffer;
244}
245
246/**
247 * __vb2_free_mem() - release all video buffer memory for a given queue
248 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300249static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300250{
251 unsigned int buffer;
252 struct vb2_buffer *vb;
253
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300254 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
255 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300256 vb = q->bufs[buffer];
257 if (!vb)
258 continue;
259
260 /* Free MMAP buffers or release USERPTR buffers */
261 if (q->memory == V4L2_MEMORY_MMAP)
262 __vb2_buf_mem_free(vb);
Sumit Semwalc5384042012-06-14 10:37:37 -0300263 else if (q->memory == V4L2_MEMORY_DMABUF)
264 __vb2_buf_dmabuf_put(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300265 else
266 __vb2_buf_userptr_put(vb);
267 }
268}
269
270/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300271 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
272 * related information, if no buffers are left return the queue to an
273 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300274 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300275static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300276{
277 unsigned int buffer;
278
279 /* Call driver-provided cleanup function for each buffer, if provided */
280 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300281 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
282 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300283 if (NULL == q->bufs[buffer])
284 continue;
285 q->ops->buf_cleanup(q->bufs[buffer]);
286 }
287 }
288
289 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300290 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300291
292 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300293 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
294 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300295 kfree(q->bufs[buffer]);
296 q->bufs[buffer] = NULL;
297 }
298
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300299 q->num_buffers -= buffers;
300 if (!q->num_buffers)
301 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300302 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300303}
304
305/**
306 * __verify_planes_array() - verify that the planes array passed in struct
307 * v4l2_buffer from userspace can be safely used
308 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300309static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300310{
Hans Verkuil32a77262012-09-28 06:12:53 -0300311 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
312 return 0;
313
Pawel Osciake23ccc02010-10-11 10:56:41 -0300314 /* Is memory for copying plane information present? */
315 if (NULL == b->m.planes) {
316 dprintk(1, "Multi-planar buffer passed but "
317 "planes array not provided\n");
318 return -EINVAL;
319 }
320
321 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
322 dprintk(1, "Incorrect planes array length, "
323 "expected %d, got %d\n", vb->num_planes, b->length);
324 return -EINVAL;
325 }
326
327 return 0;
328}
329
330/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300331 * __buffer_in_use() - return true if the buffer is in use and
332 * the queue cannot be freed (by the means of REQBUFS(0)) call
333 */
334static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
335{
336 unsigned int plane;
337 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300338 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300339 /*
340 * If num_users() has not been provided, call_memop
341 * will return 0, apparently nobody cares about this
342 * case anyway. If num_users() returns more than 1,
343 * we are not the only user of the plane's memory.
344 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300345 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300346 return true;
347 }
348 return false;
349}
350
351/**
352 * __buffers_in_use() - return true if any buffers on the queue are in use and
353 * the queue cannot be freed (by the means of REQBUFS(0)) call
354 */
355static bool __buffers_in_use(struct vb2_queue *q)
356{
357 unsigned int buffer;
358 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
359 if (__buffer_in_use(q, q->bufs[buffer]))
360 return true;
361 }
362 return false;
363}
364
365/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300366 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
367 * returned to userspace
368 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300369static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300370{
371 struct vb2_queue *q = vb->vb2_queue;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300372
Sakari Ailus2b719d72012-05-02 09:40:03 -0300373 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300374 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300375 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300376 b->reserved = vb->v4l2_buf.reserved;
377
378 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300379 /*
380 * Fill in plane-related data if userspace provided an array
Hans Verkuil32a77262012-09-28 06:12:53 -0300381 * for it. The caller has already verified memory and size.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300382 */
Hans Verkuil3c0b6062012-09-28 06:24:18 -0300383 b->length = vb->num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300384 memcpy(b->m.planes, vb->v4l2_planes,
385 b->length * sizeof(struct v4l2_plane));
386 } else {
387 /*
388 * We use length and offset in v4l2_planes array even for
389 * single-planar buffers, but userspace does not.
390 */
391 b->length = vb->v4l2_planes[0].length;
392 b->bytesused = vb->v4l2_planes[0].bytesused;
393 if (q->memory == V4L2_MEMORY_MMAP)
394 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
395 else if (q->memory == V4L2_MEMORY_USERPTR)
396 b->m.userptr = vb->v4l2_planes[0].m.userptr;
Sumit Semwalc5384042012-06-14 10:37:37 -0300397 else if (q->memory == V4L2_MEMORY_DMABUF)
398 b->m.fd = vb->v4l2_planes[0].m.fd;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300399 }
400
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300401 /*
402 * Clear any buffer state related flags.
403 */
404 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300405
406 switch (vb->state) {
407 case VB2_BUF_STATE_QUEUED:
408 case VB2_BUF_STATE_ACTIVE:
409 b->flags |= V4L2_BUF_FLAG_QUEUED;
410 break;
411 case VB2_BUF_STATE_ERROR:
412 b->flags |= V4L2_BUF_FLAG_ERROR;
413 /* fall through */
414 case VB2_BUF_STATE_DONE:
415 b->flags |= V4L2_BUF_FLAG_DONE;
416 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300417 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300418 b->flags |= V4L2_BUF_FLAG_PREPARED;
419 break;
420 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300421 /* nothing */
422 break;
423 }
424
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300425 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300426 b->flags |= V4L2_BUF_FLAG_MAPPED;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300427}
428
429/**
430 * vb2_querybuf() - query video buffer information
431 * @q: videobuf queue
432 * @b: buffer struct passed from userspace to vidioc_querybuf handler
433 * in driver
434 *
435 * Should be called from vidioc_querybuf ioctl handler in driver.
436 * This function will verify the passed v4l2_buffer structure and fill the
437 * relevant information for the userspace.
438 *
439 * The return values from this function are intended to be directly returned
440 * from vidioc_querybuf handler in driver.
441 */
442int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
443{
444 struct vb2_buffer *vb;
Hans Verkuil32a77262012-09-28 06:12:53 -0300445 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300446
447 if (b->type != q->type) {
448 dprintk(1, "querybuf: wrong buffer type\n");
449 return -EINVAL;
450 }
451
452 if (b->index >= q->num_buffers) {
453 dprintk(1, "querybuf: buffer index out of range\n");
454 return -EINVAL;
455 }
456 vb = q->bufs[b->index];
Hans Verkuil32a77262012-09-28 06:12:53 -0300457 ret = __verify_planes_array(vb, b);
458 if (!ret)
459 __fill_v4l2_buffer(vb, b);
460 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300461}
462EXPORT_SYMBOL(vb2_querybuf);
463
464/**
465 * __verify_userptr_ops() - verify that all memory operations required for
466 * USERPTR queue type have been provided
467 */
468static int __verify_userptr_ops(struct vb2_queue *q)
469{
470 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
471 !q->mem_ops->put_userptr)
472 return -EINVAL;
473
474 return 0;
475}
476
477/**
478 * __verify_mmap_ops() - verify that all memory operations required for
479 * MMAP queue type have been provided
480 */
481static int __verify_mmap_ops(struct vb2_queue *q)
482{
483 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
484 !q->mem_ops->put || !q->mem_ops->mmap)
485 return -EINVAL;
486
487 return 0;
488}
489
490/**
Sumit Semwalc5384042012-06-14 10:37:37 -0300491 * __verify_dmabuf_ops() - verify that all memory operations required for
492 * DMABUF queue type have been provided
493 */
494static int __verify_dmabuf_ops(struct vb2_queue *q)
495{
496 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
497 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
498 !q->mem_ops->unmap_dmabuf)
499 return -EINVAL;
500
501 return 0;
502}
503
504/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300505 * __verify_memory_type() - Check whether the memory type and buffer type
506 * passed to a buffer operation are compatible with the queue.
507 */
508static int __verify_memory_type(struct vb2_queue *q,
509 enum v4l2_memory memory, enum v4l2_buf_type type)
510{
Sumit Semwalc5384042012-06-14 10:37:37 -0300511 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
512 memory != V4L2_MEMORY_DMABUF) {
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300513 dprintk(1, "reqbufs: unsupported memory type\n");
514 return -EINVAL;
515 }
516
517 if (type != q->type) {
518 dprintk(1, "reqbufs: requested type is incorrect\n");
519 return -EINVAL;
520 }
521
522 /*
523 * Make sure all the required memory ops for given memory type
524 * are available.
525 */
526 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
527 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
528 return -EINVAL;
529 }
530
531 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
532 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
533 return -EINVAL;
534 }
535
Sumit Semwalc5384042012-06-14 10:37:37 -0300536 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
537 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
538 return -EINVAL;
539 }
540
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300541 /*
542 * Place the busy tests at the end: -EBUSY can be ignored when
543 * create_bufs is called with count == 0, but count == 0 should still
544 * do the memory and type validation.
545 */
546 if (q->fileio) {
547 dprintk(1, "reqbufs: file io in progress\n");
548 return -EBUSY;
549 }
550 return 0;
551}
552
553/**
554 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300555 * @q: videobuf2 queue
556 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
557 *
558 * Should be called from vidioc_reqbufs ioctl handler of a driver.
559 * This function:
560 * 1) verifies streaming parameters passed from the userspace,
561 * 2) sets up the queue,
562 * 3) negotiates number of buffers and planes per buffer with the driver
563 * to be used during streaming,
564 * 4) allocates internal buffer structures (struct vb2_buffer), according to
565 * the agreed parameters,
566 * 5) for MMAP memory type, allocates actual video memory, using the
567 * memory handling/allocation routines provided during queue initialization
568 *
569 * If req->count is 0, all the memory will be freed instead.
570 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
571 * and the queue is not busy, memory will be reallocated.
572 *
573 * The return values from this function are intended to be directly returned
574 * from vidioc_reqbufs handler in driver.
575 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300576static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300577{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300578 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300579 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300580
581 if (q->streaming) {
582 dprintk(1, "reqbufs: streaming active\n");
583 return -EBUSY;
584 }
585
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300586 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300587 /*
588 * We already have buffers allocated, so first check if they
589 * are not in use and can be freed.
590 */
591 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
592 dprintk(1, "reqbufs: memory in use, cannot free\n");
593 return -EBUSY;
594 }
595
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300596 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300597
598 /*
599 * In case of REQBUFS(0) return immediately without calling
600 * driver's queue_setup() callback and allocating resources.
601 */
602 if (req->count == 0)
603 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300604 }
605
606 /*
607 * Make sure the requested values and current defaults are sane.
608 */
609 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300610 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300611 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300612 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300613
614 /*
615 * Ask the driver how many buffers and planes per buffer it requires.
616 * Driver also sets the size and allocator context for each plane.
617 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300618 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300619 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300620 if (ret)
621 return ret;
622
623 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300624 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300625 if (ret == 0) {
626 dprintk(1, "Memory allocation failed\n");
627 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300628 }
629
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300630 allocated_buffers = ret;
631
Pawel Osciake23ccc02010-10-11 10:56:41 -0300632 /*
633 * Check if driver can handle the allocated number of buffers.
634 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300635 if (allocated_buffers < num_buffers) {
636 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300637
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300638 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
639 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300640
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300641 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300642 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300643
644 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300645 * Either the driver has accepted a smaller number of buffers,
646 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300647 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300648 }
649
650 q->num_buffers = allocated_buffers;
651
652 if (ret < 0) {
653 __vb2_queue_free(q, allocated_buffers);
654 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300655 }
656
Pawel Osciake23ccc02010-10-11 10:56:41 -0300657 /*
658 * Return the number of successfully allocated buffers
659 * to the userspace.
660 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300661 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300662
663 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300664}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300665
666/**
667 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
668 * type values.
669 * @q: videobuf2 queue
670 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
671 */
672int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
673{
674 int ret = __verify_memory_type(q, req->memory, req->type);
675
676 return ret ? ret : __reqbufs(q, req);
677}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300678EXPORT_SYMBOL_GPL(vb2_reqbufs);
679
680/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300681 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300682 * @q: videobuf2 queue
683 * @create: creation parameters, passed from userspace to vidioc_create_bufs
684 * handler in driver
685 *
686 * Should be called from vidioc_create_bufs ioctl handler of a driver.
687 * This function:
688 * 1) verifies parameter sanity
689 * 2) calls the .queue_setup() queue operation
690 * 3) performs any necessary memory allocations
691 *
692 * The return values from this function are intended to be directly returned
693 * from vidioc_create_bufs handler in driver.
694 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300695static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300696{
697 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300698 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300699
700 if (q->num_buffers == VIDEO_MAX_FRAME) {
701 dprintk(1, "%s(): maximum number of buffers already allocated\n",
702 __func__);
703 return -ENOBUFS;
704 }
705
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300706 if (!q->num_buffers) {
707 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
708 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
709 q->memory = create->memory;
710 }
711
712 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
713
714 /*
715 * Ask the driver, whether the requested number of buffers, planes per
716 * buffer and their sizes are acceptable
717 */
718 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
719 &num_planes, q->plane_sizes, q->alloc_ctx);
720 if (ret)
721 return ret;
722
723 /* Finally, allocate buffers and video memory */
724 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
725 num_planes);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300726 if (ret == 0) {
727 dprintk(1, "Memory allocation failed\n");
728 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300729 }
730
731 allocated_buffers = ret;
732
733 /*
734 * Check if driver can handle the so far allocated number of buffers.
735 */
736 if (ret < num_buffers) {
737 num_buffers = ret;
738
739 /*
740 * q->num_buffers contains the total number of buffers, that the
741 * queue driver has set up
742 */
743 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
744 &num_planes, q->plane_sizes, q->alloc_ctx);
745
746 if (!ret && allocated_buffers < num_buffers)
747 ret = -ENOMEM;
748
749 /*
750 * Either the driver has accepted a smaller number of buffers,
751 * or .queue_setup() returned an error
752 */
753 }
754
755 q->num_buffers += allocated_buffers;
756
757 if (ret < 0) {
758 __vb2_queue_free(q, allocated_buffers);
Hans Verkuilf05393d22012-06-22 05:44:14 -0300759 return -ENOMEM;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300760 }
761
762 /*
763 * Return the number of successfully allocated buffers
764 * to the userspace.
765 */
766 create->count = allocated_buffers;
767
768 return 0;
769}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300770
771/**
Nicolas THERY53aa3b12012-07-20 09:25:37 -0300772 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
773 * memory and type values.
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300774 * @q: videobuf2 queue
775 * @create: creation parameters, passed from userspace to vidioc_create_bufs
776 * handler in driver
777 */
778int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
779{
780 int ret = __verify_memory_type(q, create->memory, create->format.type);
781
782 create->index = q->num_buffers;
Hans Verkuilf05393d22012-06-22 05:44:14 -0300783 if (create->count == 0)
784 return ret != -EBUSY ? ret : 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300785 return ret ? ret : __create_bufs(q, create);
786}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300787EXPORT_SYMBOL_GPL(vb2_create_bufs);
788
789/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300790 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
791 * @vb: vb2_buffer to which the plane in question belongs to
792 * @plane_no: plane number for which the address is to be returned
793 *
794 * This function returns a kernel virtual address of a given plane if
795 * such a mapping exist, NULL otherwise.
796 */
797void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
798{
799 struct vb2_queue *q = vb->vb2_queue;
800
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300801 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300802 return NULL;
803
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300804 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300805
806}
807EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
808
809/**
810 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
811 * @vb: vb2_buffer to which the plane in question belongs to
812 * @plane_no: plane number for which the cookie is to be returned
813 *
814 * This function returns an allocator specific cookie for a given plane if
815 * available, NULL otherwise. The allocator should provide some simple static
816 * inline function, which would convert this cookie to the allocator specific
817 * type that can be used directly by the driver to access the buffer. This can
818 * be for example physical address, pointer to scatter list or IOMMU mapping.
819 */
820void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
821{
822 struct vb2_queue *q = vb->vb2_queue;
823
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300824 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300825 return NULL;
826
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300827 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300828}
829EXPORT_SYMBOL_GPL(vb2_plane_cookie);
830
831/**
832 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
833 * @vb: vb2_buffer returned from the driver
834 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
835 * or VB2_BUF_STATE_ERROR if the operation finished with an error
836 *
837 * This function should be called by the driver after a hardware operation on
838 * a buffer is finished and the buffer may be returned to userspace. The driver
839 * cannot use this buffer anymore until it is queued back to it by videobuf
840 * by the means of buf_queue callback. Only buffers previously queued to the
841 * driver by buf_queue can be passed to this function.
842 */
843void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
844{
845 struct vb2_queue *q = vb->vb2_queue;
846 unsigned long flags;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300847 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300848
849 if (vb->state != VB2_BUF_STATE_ACTIVE)
850 return;
851
852 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
853 return;
854
855 dprintk(4, "Done processing on buffer %d, state: %d\n",
856 vb->v4l2_buf.index, vb->state);
857
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -0300858 /* sync buffers */
859 for (plane = 0; plane < vb->num_planes; ++plane)
860 call_memop(q, finish, vb->planes[plane].mem_priv);
861
Pawel Osciake23ccc02010-10-11 10:56:41 -0300862 /* Add the buffer to the done buffers list */
863 spin_lock_irqsave(&q->done_lock, flags);
864 vb->state = state;
865 list_add_tail(&vb->done_entry, &q->done_list);
866 atomic_dec(&q->queued_count);
867 spin_unlock_irqrestore(&q->done_lock, flags);
868
869 /* Inform any processes that may be waiting for buffers */
870 wake_up(&q->done_wq);
871}
872EXPORT_SYMBOL_GPL(vb2_buffer_done);
873
874/**
Hans Verkuil32a77262012-09-28 06:12:53 -0300875 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
876 * v4l2_buffer by the userspace. The caller has already verified that struct
877 * v4l2_buffer has a valid number of planes.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300878 */
Hans Verkuil32a77262012-09-28 06:12:53 -0300879static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300880 struct v4l2_plane *v4l2_planes)
881{
882 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300883
884 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300885 /* Fill in driver-provided information for OUTPUT types */
886 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
887 /*
888 * Will have to go up to b->length when API starts
889 * accepting variable number of planes.
890 */
891 for (plane = 0; plane < vb->num_planes; ++plane) {
892 v4l2_planes[plane].bytesused =
893 b->m.planes[plane].bytesused;
894 v4l2_planes[plane].data_offset =
895 b->m.planes[plane].data_offset;
896 }
897 }
898
899 if (b->memory == V4L2_MEMORY_USERPTR) {
900 for (plane = 0; plane < vb->num_planes; ++plane) {
901 v4l2_planes[plane].m.userptr =
902 b->m.planes[plane].m.userptr;
903 v4l2_planes[plane].length =
904 b->m.planes[plane].length;
905 }
906 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300907 if (b->memory == V4L2_MEMORY_DMABUF) {
908 for (plane = 0; plane < vb->num_planes; ++plane) {
909 v4l2_planes[plane].m.fd =
910 b->m.planes[plane].m.fd;
911 v4l2_planes[plane].length =
912 b->m.planes[plane].length;
913 v4l2_planes[plane].data_offset =
914 b->m.planes[plane].data_offset;
915 }
916 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300917 } else {
918 /*
919 * Single-planar buffers do not use planes array,
920 * so fill in relevant v4l2_buffer struct fields instead.
921 * In videobuf we use our internal V4l2_planes struct for
922 * single-planar buffers as well, for simplicity.
923 */
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300924 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300925 v4l2_planes[0].bytesused = b->bytesused;
Laurent Pinchartac706bf2012-12-17 07:38:16 -0300926 v4l2_planes[0].data_offset = 0;
927 }
Pawel Osciake23ccc02010-10-11 10:56:41 -0300928
929 if (b->memory == V4L2_MEMORY_USERPTR) {
930 v4l2_planes[0].m.userptr = b->m.userptr;
931 v4l2_planes[0].length = b->length;
932 }
Sumit Semwalc5384042012-06-14 10:37:37 -0300933
934 if (b->memory == V4L2_MEMORY_DMABUF) {
935 v4l2_planes[0].m.fd = b->m.fd;
936 v4l2_planes[0].length = b->length;
937 v4l2_planes[0].data_offset = 0;
938 }
939
Pawel Osciake23ccc02010-10-11 10:56:41 -0300940 }
941
942 vb->v4l2_buf.field = b->field;
943 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300944 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300945}
946
947/**
948 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
949 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300950static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300951{
952 struct v4l2_plane planes[VIDEO_MAX_PLANES];
953 struct vb2_queue *q = vb->vb2_queue;
954 void *mem_priv;
955 unsigned int plane;
956 int ret;
957 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
958
Hans Verkuil32a77262012-09-28 06:12:53 -0300959 /* Copy relevant information provided by the userspace */
960 __fill_vb2_buffer(vb, b, planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300961
962 for (plane = 0; plane < vb->num_planes; ++plane) {
963 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300964 if (vb->v4l2_planes[plane].m.userptr &&
965 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300966 && vb->v4l2_planes[plane].length == planes[plane].length)
967 continue;
968
969 dprintk(3, "qbuf: userspace address for plane %d changed, "
970 "reacquiring memory\n", plane);
971
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300972 /* Check if the provided plane buffer is large enough */
973 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300974 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300975 goto err;
976 }
977
Pawel Osciake23ccc02010-10-11 10:56:41 -0300978 /* Release previously acquired memory if present */
979 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300980 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300981
982 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300983 vb->v4l2_planes[plane].m.userptr = 0;
984 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300985
986 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300987 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
988 planes[plane].m.userptr,
989 planes[plane].length, write);
990 if (IS_ERR_OR_NULL(mem_priv)) {
991 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300992 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300993 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
994 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300995 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300996 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300997 }
998
999 /*
1000 * Call driver-specific initialization on the newly acquired buffer,
1001 * if provided.
1002 */
1003 ret = call_qop(q, buf_init, vb);
1004 if (ret) {
1005 dprintk(1, "qbuf: buffer initialization failed\n");
1006 goto err;
1007 }
1008
1009 /*
1010 * Now that everything is in order, copy relevant information
1011 * provided by userspace.
1012 */
1013 for (plane = 0; plane < vb->num_planes; ++plane)
1014 vb->v4l2_planes[plane] = planes[plane];
1015
1016 return 0;
1017err:
1018 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001019 for (plane = 0; plane < vb->num_planes; ++plane) {
1020 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -03001021 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -03001022 vb->planes[plane].mem_priv = NULL;
1023 vb->v4l2_planes[plane].m.userptr = 0;
1024 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001025 }
1026
1027 return ret;
1028}
1029
1030/**
1031 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1032 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001033static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001034{
Hans Verkuil32a77262012-09-28 06:12:53 -03001035 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1036 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001037}
1038
1039/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001040 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1041 */
1042static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1043{
1044 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1045 struct vb2_queue *q = vb->vb2_queue;
1046 void *mem_priv;
1047 unsigned int plane;
1048 int ret;
1049 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1050
1051 /* Verify and copy relevant information provided by the userspace */
1052 __fill_vb2_buffer(vb, b, planes);
1053
1054 for (plane = 0; plane < vb->num_planes; ++plane) {
1055 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1056
1057 if (IS_ERR_OR_NULL(dbuf)) {
1058 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1059 plane);
1060 ret = -EINVAL;
1061 goto err;
1062 }
1063
1064 /* use DMABUF size if length is not provided */
1065 if (planes[plane].length == 0)
1066 planes[plane].length = dbuf->size;
1067
1068 if (planes[plane].length < planes[plane].data_offset +
1069 q->plane_sizes[plane]) {
1070 ret = -EINVAL;
1071 goto err;
1072 }
1073
1074 /* Skip the plane if already verified */
1075 if (dbuf == vb->planes[plane].dbuf &&
1076 vb->v4l2_planes[plane].length == planes[plane].length) {
1077 dma_buf_put(dbuf);
1078 continue;
1079 }
1080
1081 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1082
1083 /* Release previously acquired memory if present */
1084 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1085 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1086
1087 /* Acquire each plane's memory */
1088 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1089 dbuf, planes[plane].length, write);
1090 if (IS_ERR(mem_priv)) {
1091 dprintk(1, "qbuf: failed to attach dmabuf\n");
1092 ret = PTR_ERR(mem_priv);
1093 dma_buf_put(dbuf);
1094 goto err;
1095 }
1096
1097 vb->planes[plane].dbuf = dbuf;
1098 vb->planes[plane].mem_priv = mem_priv;
1099 }
1100
1101 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1102 * really we want to do this just before the DMA, not while queueing
1103 * the buffer(s)..
1104 */
1105 for (plane = 0; plane < vb->num_planes; ++plane) {
1106 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1107 if (ret) {
1108 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1109 plane);
1110 goto err;
1111 }
1112 vb->planes[plane].dbuf_mapped = 1;
1113 }
1114
1115 /*
1116 * Call driver-specific initialization on the newly acquired buffer,
1117 * if provided.
1118 */
1119 ret = call_qop(q, buf_init, vb);
1120 if (ret) {
1121 dprintk(1, "qbuf: buffer initialization failed\n");
1122 goto err;
1123 }
1124
1125 /*
1126 * Now that everything is in order, copy relevant information
1127 * provided by userspace.
1128 */
1129 for (plane = 0; plane < vb->num_planes; ++plane)
1130 vb->v4l2_planes[plane] = planes[plane];
1131
1132 return 0;
1133err:
1134 /* In case of errors, release planes that were already acquired */
1135 __vb2_buf_dmabuf_put(vb);
1136
1137 return ret;
1138}
1139
1140/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001141 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1142 */
1143static void __enqueue_in_driver(struct vb2_buffer *vb)
1144{
1145 struct vb2_queue *q = vb->vb2_queue;
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001146 unsigned int plane;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001147
1148 vb->state = VB2_BUF_STATE_ACTIVE;
1149 atomic_inc(&q->queued_count);
Marek Szyprowski3e0c2f22012-06-14 10:37:43 -03001150
1151 /* sync buffers */
1152 for (plane = 0; plane < vb->num_planes; ++plane)
1153 call_memop(q, prepare, vb->planes[plane].mem_priv);
1154
Pawel Osciake23ccc02010-10-11 10:56:41 -03001155 q->ops->buf_queue(vb);
1156}
1157
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001158static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001159{
1160 struct vb2_queue *q = vb->vb2_queue;
1161 int ret;
1162
1163 switch (q->memory) {
1164 case V4L2_MEMORY_MMAP:
1165 ret = __qbuf_mmap(vb, b);
1166 break;
1167 case V4L2_MEMORY_USERPTR:
1168 ret = __qbuf_userptr(vb, b);
1169 break;
Sumit Semwalc5384042012-06-14 10:37:37 -03001170 case V4L2_MEMORY_DMABUF:
1171 ret = __qbuf_dmabuf(vb, b);
1172 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001173 default:
1174 WARN(1, "Invalid queue type\n");
1175 ret = -EINVAL;
1176 }
1177
1178 if (!ret)
1179 ret = call_qop(q, buf_prepare, vb);
1180 if (ret)
1181 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1182 else
1183 vb->state = VB2_BUF_STATE_PREPARED;
1184
1185 return ret;
1186}
1187
Pawel Osciake23ccc02010-10-11 10:56:41 -03001188/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001189 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1190 * @q: videobuf2 queue
1191 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1192 * handler in driver
1193 *
1194 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1195 * This function:
1196 * 1) verifies the passed buffer,
1197 * 2) calls buf_prepare callback in the driver (if provided), in which
1198 * driver-specific buffer initialization can be performed,
1199 *
1200 * The return values from this function are intended to be directly returned
1201 * from vidioc_prepare_buf handler in driver.
1202 */
1203int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1204{
1205 struct vb2_buffer *vb;
1206 int ret;
1207
1208 if (q->fileio) {
1209 dprintk(1, "%s(): file io in progress\n", __func__);
1210 return -EBUSY;
1211 }
1212
1213 if (b->type != q->type) {
1214 dprintk(1, "%s(): invalid buffer type\n", __func__);
1215 return -EINVAL;
1216 }
1217
1218 if (b->index >= q->num_buffers) {
1219 dprintk(1, "%s(): buffer index out of range\n", __func__);
1220 return -EINVAL;
1221 }
1222
1223 vb = q->bufs[b->index];
1224 if (NULL == vb) {
1225 /* Should never happen */
1226 dprintk(1, "%s(): buffer is NULL\n", __func__);
1227 return -EINVAL;
1228 }
1229
1230 if (b->memory != q->memory) {
1231 dprintk(1, "%s(): invalid memory type\n", __func__);
1232 return -EINVAL;
1233 }
1234
1235 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1236 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1237 return -EINVAL;
1238 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001239 ret = __verify_planes_array(vb, b);
1240 if (ret < 0)
1241 return ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001242 ret = __buf_prepare(vb, b);
1243 if (ret < 0)
1244 return ret;
1245
1246 __fill_v4l2_buffer(vb, b);
1247
1248 return 0;
1249}
1250EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1251
1252/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001253 * vb2_qbuf() - Queue a buffer from userspace
1254 * @q: videobuf2 queue
1255 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1256 * in driver
1257 *
1258 * Should be called from vidioc_qbuf ioctl handler of a driver.
1259 * This function:
1260 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001261 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1262 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001263 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1264 * callback for processing.
1265 *
1266 * The return values from this function are intended to be directly returned
1267 * from vidioc_qbuf handler in driver.
1268 */
1269int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1270{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001271 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001272 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001273 int ret = 0;
1274
1275 /*
1276 * In case of user pointer buffers vb2 allocator needs to get direct
1277 * access to userspace pages. This requires getting read access on
1278 * mmap semaphore in the current process structure. The same
1279 * semaphore is taken before calling mmap operation, while both mmap
1280 * and qbuf are called by the driver or v4l2 core with driver's lock
1281 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1282 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1283 * release driver's lock, takes mmap_sem and then takes again driver's
1284 * lock.
1285 *
1286 * To avoid race with other vb2 calls, which might be called after
1287 * releasing driver's lock, this operation is performed at the
1288 * beggining of qbuf processing. This way the queue status is
1289 * consistent after getting driver's lock back.
1290 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001291 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001292 mmap_sem = &current->mm->mmap_sem;
1293 call_qop(q, wait_prepare, q);
1294 down_read(mmap_sem);
1295 call_qop(q, wait_finish, q);
1296 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001297
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001298 if (q->fileio) {
1299 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001300 ret = -EBUSY;
1301 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001302 }
1303
Pawel Osciake23ccc02010-10-11 10:56:41 -03001304 if (b->type != q->type) {
1305 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001306 ret = -EINVAL;
1307 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001308 }
1309
1310 if (b->index >= q->num_buffers) {
1311 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001312 ret = -EINVAL;
1313 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001314 }
1315
1316 vb = q->bufs[b->index];
1317 if (NULL == vb) {
1318 /* Should never happen */
1319 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001320 ret = -EINVAL;
1321 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001322 }
1323
1324 if (b->memory != q->memory) {
1325 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001326 ret = -EINVAL;
1327 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001328 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001329 ret = __verify_planes_array(vb, b);
1330 if (ret)
1331 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001332
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001333 switch (vb->state) {
1334 case VB2_BUF_STATE_DEQUEUED:
1335 ret = __buf_prepare(vb, b);
1336 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001337 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001338 case VB2_BUF_STATE_PREPARED:
1339 break;
1340 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001341 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001342 ret = -EINVAL;
1343 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001344 }
1345
Pawel Osciake23ccc02010-10-11 10:56:41 -03001346 /*
1347 * Add to the queued buffers list, a buffer will stay on it until
1348 * dequeued in dqbuf.
1349 */
1350 list_add_tail(&vb->queued_entry, &q->queued_list);
1351 vb->state = VB2_BUF_STATE_QUEUED;
1352
1353 /*
1354 * If already streaming, give the buffer to driver for processing.
1355 * If not, the buffer will be given to driver on next streamon.
1356 */
1357 if (q->streaming)
1358 __enqueue_in_driver(vb);
1359
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001360 /* Fill buffer information for the userspace */
1361 __fill_v4l2_buffer(vb, b);
1362
Pawel Osciake23ccc02010-10-11 10:56:41 -03001363 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001364unlock:
1365 if (mmap_sem)
1366 up_read(mmap_sem);
1367 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001368}
1369EXPORT_SYMBOL_GPL(vb2_qbuf);
1370
1371/**
1372 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1373 * for dequeuing
1374 *
1375 * Will sleep if required for nonblocking == false.
1376 */
1377static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1378{
1379 /*
1380 * All operations on vb_done_list are performed under done_lock
1381 * spinlock protection. However, buffers may be removed from
1382 * it and returned to userspace only while holding both driver's
1383 * lock and the done_lock spinlock. Thus we can be sure that as
1384 * long as we hold the driver's lock, the list will remain not
1385 * empty if list_empty() check succeeds.
1386 */
1387
1388 for (;;) {
1389 int ret;
1390
1391 if (!q->streaming) {
1392 dprintk(1, "Streaming off, will not wait for buffers\n");
1393 return -EINVAL;
1394 }
1395
1396 if (!list_empty(&q->done_list)) {
1397 /*
1398 * Found a buffer that we were waiting for.
1399 */
1400 break;
1401 }
1402
1403 if (nonblocking) {
1404 dprintk(1, "Nonblocking and no buffers to dequeue, "
1405 "will not wait\n");
1406 return -EAGAIN;
1407 }
1408
1409 /*
1410 * We are streaming and blocking, wait for another buffer to
1411 * become ready or for streamoff. Driver's lock is released to
1412 * allow streamoff or qbuf to be called while waiting.
1413 */
1414 call_qop(q, wait_prepare, q);
1415
1416 /*
1417 * All locks have been released, it is safe to sleep now.
1418 */
1419 dprintk(3, "Will sleep waiting for buffers\n");
1420 ret = wait_event_interruptible(q->done_wq,
1421 !list_empty(&q->done_list) || !q->streaming);
1422
1423 /*
1424 * We need to reevaluate both conditions again after reacquiring
1425 * the locks or return an error if one occurred.
1426 */
1427 call_qop(q, wait_finish, q);
Hans Verkuil32a77262012-09-28 06:12:53 -03001428 if (ret) {
1429 dprintk(1, "Sleep was interrupted\n");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001430 return ret;
Hans Verkuil32a77262012-09-28 06:12:53 -03001431 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001432 }
1433 return 0;
1434}
1435
1436/**
1437 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1438 *
1439 * Will sleep if required for nonblocking == false.
1440 */
1441static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
Hans Verkuil32a77262012-09-28 06:12:53 -03001442 struct v4l2_buffer *b, int nonblocking)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001443{
1444 unsigned long flags;
1445 int ret;
1446
1447 /*
1448 * Wait for at least one buffer to become available on the done_list.
1449 */
1450 ret = __vb2_wait_for_done_vb(q, nonblocking);
1451 if (ret)
1452 return ret;
1453
1454 /*
1455 * Driver's lock has been held since we last verified that done_list
1456 * is not empty, so no need for another list_empty(done_list) check.
1457 */
1458 spin_lock_irqsave(&q->done_lock, flags);
1459 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
Hans Verkuil32a77262012-09-28 06:12:53 -03001460 /*
1461 * Only remove the buffer from done_list if v4l2_buffer can handle all
1462 * the planes.
1463 */
1464 ret = __verify_planes_array(*vb, b);
1465 if (!ret)
1466 list_del(&(*vb)->done_entry);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001467 spin_unlock_irqrestore(&q->done_lock, flags);
1468
Hans Verkuil32a77262012-09-28 06:12:53 -03001469 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001470}
1471
1472/**
1473 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1474 * @q: videobuf2 queue
1475 *
1476 * This function will wait until all buffers that have been given to the driver
1477 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1478 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1479 * taken, for example from stop_streaming() callback.
1480 */
1481int vb2_wait_for_all_buffers(struct vb2_queue *q)
1482{
1483 if (!q->streaming) {
1484 dprintk(1, "Streaming off, will not wait for buffers\n");
1485 return -EINVAL;
1486 }
1487
1488 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1489 return 0;
1490}
1491EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1492
1493/**
Sumit Semwalc5384042012-06-14 10:37:37 -03001494 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1495 */
1496static void __vb2_dqbuf(struct vb2_buffer *vb)
1497{
1498 struct vb2_queue *q = vb->vb2_queue;
1499 unsigned int i;
1500
1501 /* nothing to do if the buffer is already dequeued */
1502 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1503 return;
1504
1505 vb->state = VB2_BUF_STATE_DEQUEUED;
1506
1507 /* unmap DMABUF buffer */
1508 if (q->memory == V4L2_MEMORY_DMABUF)
1509 for (i = 0; i < vb->num_planes; ++i) {
1510 if (!vb->planes[i].dbuf_mapped)
1511 continue;
1512 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1513 vb->planes[i].dbuf_mapped = 0;
1514 }
1515}
1516
1517/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001518 * vb2_dqbuf() - Dequeue a buffer to the userspace
1519 * @q: videobuf2 queue
1520 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1521 * in driver
1522 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1523 * buffers ready for dequeuing are present. Normally the driver
1524 * would be passing (file->f_flags & O_NONBLOCK) here
1525 *
1526 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1527 * This function:
1528 * 1) verifies the passed buffer,
1529 * 2) calls buf_finish callback in the driver (if provided), in which
1530 * driver can perform any additional operations that may be required before
1531 * returning the buffer to userspace, such as cache sync,
1532 * 3) the buffer struct members are filled with relevant information for
1533 * the userspace.
1534 *
1535 * The return values from this function are intended to be directly returned
1536 * from vidioc_dqbuf handler in driver.
1537 */
1538int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1539{
1540 struct vb2_buffer *vb = NULL;
1541 int ret;
1542
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001543 if (q->fileio) {
1544 dprintk(1, "dqbuf: file io in progress\n");
1545 return -EBUSY;
1546 }
1547
Pawel Osciake23ccc02010-10-11 10:56:41 -03001548 if (b->type != q->type) {
1549 dprintk(1, "dqbuf: invalid buffer type\n");
1550 return -EINVAL;
1551 }
Hans Verkuil32a77262012-09-28 06:12:53 -03001552 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1553 if (ret < 0)
Pawel Osciake23ccc02010-10-11 10:56:41 -03001554 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001555
1556 ret = call_qop(q, buf_finish, vb);
1557 if (ret) {
1558 dprintk(1, "dqbuf: buffer finish failed\n");
1559 return ret;
1560 }
1561
1562 switch (vb->state) {
1563 case VB2_BUF_STATE_DONE:
1564 dprintk(3, "dqbuf: Returning done buffer\n");
1565 break;
1566 case VB2_BUF_STATE_ERROR:
1567 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1568 break;
1569 default:
1570 dprintk(1, "dqbuf: Invalid buffer state\n");
1571 return -EINVAL;
1572 }
1573
1574 /* Fill buffer information for the userspace */
1575 __fill_v4l2_buffer(vb, b);
1576 /* Remove from videobuf queue */
1577 list_del(&vb->queued_entry);
Sumit Semwalc5384042012-06-14 10:37:37 -03001578 /* go back to dequeued state */
1579 __vb2_dqbuf(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001580
1581 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1582 vb->v4l2_buf.index, vb->state);
1583
Pawel Osciake23ccc02010-10-11 10:56:41 -03001584 return 0;
1585}
1586EXPORT_SYMBOL_GPL(vb2_dqbuf);
1587
1588/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001589 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1590 *
1591 * Removes all queued buffers from driver's queue and all buffers queued by
1592 * userspace from videobuf's queue. Returns to state after reqbufs.
1593 */
1594static void __vb2_queue_cancel(struct vb2_queue *q)
1595{
1596 unsigned int i;
1597
1598 /*
1599 * Tell driver to stop all transactions and release all queued
1600 * buffers.
1601 */
1602 if (q->streaming)
1603 call_qop(q, stop_streaming, q);
1604 q->streaming = 0;
1605
1606 /*
1607 * Remove all buffers from videobuf's list...
1608 */
1609 INIT_LIST_HEAD(&q->queued_list);
1610 /*
1611 * ...and done list; userspace will not receive any buffers it
1612 * has not already dequeued before initiating cancel.
1613 */
1614 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001615 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001616 wake_up_all(&q->done_wq);
1617
1618 /*
1619 * Reinitialize all buffers for next use.
1620 */
1621 for (i = 0; i < q->num_buffers; ++i)
Sumit Semwalc5384042012-06-14 10:37:37 -03001622 __vb2_dqbuf(q->bufs[i]);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001623}
1624
1625/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001626 * vb2_streamon - start streaming
1627 * @q: videobuf2 queue
1628 * @type: type argument passed from userspace to vidioc_streamon handler
1629 *
1630 * Should be called from vidioc_streamon handler of a driver.
1631 * This function:
1632 * 1) verifies current state
1633 * 2) passes any previously queued buffers to the driver and starts streaming
1634 *
1635 * The return values from this function are intended to be directly returned
1636 * from vidioc_streamon handler in the driver.
1637 */
1638int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1639{
1640 struct vb2_buffer *vb;
1641 int ret;
1642
1643 if (q->fileio) {
1644 dprintk(1, "streamon: file io in progress\n");
1645 return -EBUSY;
1646 }
1647
1648 if (type != q->type) {
1649 dprintk(1, "streamon: invalid stream type\n");
1650 return -EINVAL;
1651 }
1652
1653 if (q->streaming) {
1654 dprintk(1, "streamon: already streaming\n");
1655 return -EBUSY;
1656 }
1657
1658 /*
1659 * If any buffers were queued before streamon,
1660 * we can now pass them to driver for processing.
1661 */
1662 list_for_each_entry(vb, &q->queued_list, queued_entry)
1663 __enqueue_in_driver(vb);
1664
1665 /*
1666 * Let driver notice that streaming state has been enabled.
1667 */
1668 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1669 if (ret) {
1670 dprintk(1, "streamon: driver refused to start streaming\n");
1671 __vb2_queue_cancel(q);
1672 return ret;
1673 }
1674
1675 q->streaming = 1;
1676
1677 dprintk(3, "Streamon successful\n");
1678 return 0;
1679}
1680EXPORT_SYMBOL_GPL(vb2_streamon);
1681
1682
1683/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001684 * vb2_streamoff - stop streaming
1685 * @q: videobuf2 queue
1686 * @type: type argument passed from userspace to vidioc_streamoff handler
1687 *
1688 * Should be called from vidioc_streamoff handler of a driver.
1689 * This function:
1690 * 1) verifies current state,
1691 * 2) stop streaming and dequeues any queued buffers, including those previously
1692 * passed to the driver (after waiting for the driver to finish).
1693 *
1694 * This call can be used for pausing playback.
1695 * The return values from this function are intended to be directly returned
1696 * from vidioc_streamoff handler in the driver
1697 */
1698int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1699{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001700 if (q->fileio) {
1701 dprintk(1, "streamoff: file io in progress\n");
1702 return -EBUSY;
1703 }
1704
Pawel Osciake23ccc02010-10-11 10:56:41 -03001705 if (type != q->type) {
1706 dprintk(1, "streamoff: invalid stream type\n");
1707 return -EINVAL;
1708 }
1709
1710 if (!q->streaming) {
1711 dprintk(1, "streamoff: not streaming\n");
1712 return -EINVAL;
1713 }
1714
1715 /*
1716 * Cancel will pause streaming and remove all buffers from the driver
1717 * and videobuf, effectively returning control over them to userspace.
1718 */
1719 __vb2_queue_cancel(q);
1720
1721 dprintk(3, "Streamoff successful\n");
1722 return 0;
1723}
1724EXPORT_SYMBOL_GPL(vb2_streamoff);
1725
1726/**
1727 * __find_plane_by_offset() - find plane associated with the given offset off
1728 */
1729static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1730 unsigned int *_buffer, unsigned int *_plane)
1731{
1732 struct vb2_buffer *vb;
1733 unsigned int buffer, plane;
1734
1735 /*
1736 * Go over all buffers and their planes, comparing the given offset
1737 * with an offset assigned to each plane. If a match is found,
1738 * return its buffer and plane numbers.
1739 */
1740 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1741 vb = q->bufs[buffer];
1742
1743 for (plane = 0; plane < vb->num_planes; ++plane) {
1744 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1745 *_buffer = buffer;
1746 *_plane = plane;
1747 return 0;
1748 }
1749 }
1750 }
1751
1752 return -EINVAL;
1753}
1754
1755/**
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03001756 * vb2_expbuf() - Export a buffer as a file descriptor
1757 * @q: videobuf2 queue
1758 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1759 * handler in driver
1760 *
1761 * The return values from this function are intended to be directly returned
1762 * from vidioc_expbuf handler in driver.
1763 */
1764int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1765{
1766 struct vb2_buffer *vb = NULL;
1767 struct vb2_plane *vb_plane;
1768 int ret;
1769 struct dma_buf *dbuf;
1770
1771 if (q->memory != V4L2_MEMORY_MMAP) {
1772 dprintk(1, "Queue is not currently set up for mmap\n");
1773 return -EINVAL;
1774 }
1775
1776 if (!q->mem_ops->get_dmabuf) {
1777 dprintk(1, "Queue does not support DMA buffer exporting\n");
1778 return -EINVAL;
1779 }
1780
1781 if (eb->flags & ~O_CLOEXEC) {
1782 dprintk(1, "Queue does support only O_CLOEXEC flag\n");
1783 return -EINVAL;
1784 }
1785
1786 if (eb->type != q->type) {
1787 dprintk(1, "qbuf: invalid buffer type\n");
1788 return -EINVAL;
1789 }
1790
1791 if (eb->index >= q->num_buffers) {
1792 dprintk(1, "buffer index out of range\n");
1793 return -EINVAL;
1794 }
1795
1796 vb = q->bufs[eb->index];
1797
1798 if (eb->plane >= vb->num_planes) {
1799 dprintk(1, "buffer plane out of range\n");
1800 return -EINVAL;
1801 }
1802
1803 vb_plane = &vb->planes[eb->plane];
1804
1805 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
1806 if (IS_ERR_OR_NULL(dbuf)) {
1807 dprintk(1, "Failed to export buffer %d, plane %d\n",
1808 eb->index, eb->plane);
1809 return -EINVAL;
1810 }
1811
1812 ret = dma_buf_fd(dbuf, eb->flags);
1813 if (ret < 0) {
1814 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1815 eb->index, eb->plane, ret);
1816 dma_buf_put(dbuf);
1817 return ret;
1818 }
1819
1820 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1821 eb->index, eb->plane, ret);
1822 eb->fd = ret;
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(vb2_expbuf);
1827
1828/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001829 * vb2_mmap() - map video buffers into application address space
1830 * @q: videobuf2 queue
1831 * @vma: vma passed to the mmap file operation handler in the driver
1832 *
1833 * Should be called from mmap file operation handler of a driver.
1834 * This function maps one plane of one of the available video buffers to
1835 * userspace. To map whole video memory allocated on reqbufs, this function
1836 * has to be called once per each plane per each buffer previously allocated.
1837 *
1838 * When the userspace application calls mmap, it passes to it an offset returned
1839 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1840 * a "cookie", which is then used to identify the plane to be mapped.
1841 * This function finds a plane with a matching offset and a mapping is performed
1842 * by the means of a provided memory operation.
1843 *
1844 * The return values from this function are intended to be directly returned
1845 * from the mmap handler in driver.
1846 */
1847int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1848{
1849 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001850 struct vb2_buffer *vb;
1851 unsigned int buffer, plane;
1852 int ret;
1853
1854 if (q->memory != V4L2_MEMORY_MMAP) {
1855 dprintk(1, "Queue is not currently set up for mmap\n");
1856 return -EINVAL;
1857 }
1858
1859 /*
1860 * Check memory area access mode.
1861 */
1862 if (!(vma->vm_flags & VM_SHARED)) {
1863 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1864 return -EINVAL;
1865 }
1866 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1867 if (!(vma->vm_flags & VM_WRITE)) {
1868 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1869 return -EINVAL;
1870 }
1871 } else {
1872 if (!(vma->vm_flags & VM_READ)) {
1873 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1874 return -EINVAL;
1875 }
1876 }
1877
1878 /*
1879 * Find the plane corresponding to the offset passed by userspace.
1880 */
1881 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1882 if (ret)
1883 return ret;
1884
1885 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001886
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001887 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001888 if (ret)
1889 return ret;
1890
Pawel Osciake23ccc02010-10-11 10:56:41 -03001891 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1892 return 0;
1893}
1894EXPORT_SYMBOL_GPL(vb2_mmap);
1895
Scott Jiang6f524ec2011-09-21 09:25:23 -03001896#ifndef CONFIG_MMU
1897unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1898 unsigned long addr,
1899 unsigned long len,
1900 unsigned long pgoff,
1901 unsigned long flags)
1902{
1903 unsigned long off = pgoff << PAGE_SHIFT;
1904 struct vb2_buffer *vb;
1905 unsigned int buffer, plane;
1906 int ret;
1907
1908 if (q->memory != V4L2_MEMORY_MMAP) {
1909 dprintk(1, "Queue is not currently set up for mmap\n");
1910 return -EINVAL;
1911 }
1912
1913 /*
1914 * Find the plane corresponding to the offset passed by userspace.
1915 */
1916 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1917 if (ret)
1918 return ret;
1919
1920 vb = q->bufs[buffer];
1921
1922 return (unsigned long)vb2_plane_vaddr(vb, plane);
1923}
1924EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1925#endif
1926
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001927static int __vb2_init_fileio(struct vb2_queue *q, int read);
1928static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001929
1930/**
1931 * vb2_poll() - implements poll userspace operation
1932 * @q: videobuf2 queue
1933 * @file: file argument passed to the poll file operation handler
1934 * @wait: wait argument passed to the poll file operation handler
1935 *
1936 * This function implements poll file operation handler for a driver.
1937 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1938 * be informed that the file descriptor of a video device is available for
1939 * reading.
1940 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1941 * will be reported as available for writing.
1942 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03001943 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1944 * pending events.
1945 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03001946 * The return values from this function are intended to be directly returned
1947 * from poll handler in driver.
1948 */
1949unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1950{
Hans Verkuil95213ce2011-07-13 04:26:52 -03001951 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001952 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001953 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03001954 unsigned int res = 0;
1955 unsigned long flags;
1956
1957 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1958 struct v4l2_fh *fh = file->private_data;
1959
1960 if (v4l2_event_pending(fh))
1961 res = POLLPRI;
1962 else if (req_events & POLLPRI)
1963 poll_wait(file, &fh->wait, wait);
1964 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001965
1966 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001967 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001968 */
1969 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001970 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1971 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001972 if (__vb2_init_fileio(q, 1))
1973 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001974 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001975 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1976 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001977 if (__vb2_init_fileio(q, 0))
1978 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001979 /*
1980 * Write to OUTPUT queue can be done immediately.
1981 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03001982 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001983 }
1984 }
1985
1986 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001987 * There is nothing to wait for if no buffers have already been queued.
1988 */
1989 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03001990 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001991
1992 poll_wait(file, &q->done_wq, wait);
1993
1994 /*
1995 * Take first buffer available for dequeuing.
1996 */
1997 spin_lock_irqsave(&q->done_lock, flags);
1998 if (!list_empty(&q->done_list))
1999 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2000 done_entry);
2001 spin_unlock_irqrestore(&q->done_lock, flags);
2002
2003 if (vb && (vb->state == VB2_BUF_STATE_DONE
2004 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03002005 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2006 res | POLLOUT | POLLWRNORM :
2007 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002008 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03002009 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002010}
2011EXPORT_SYMBOL_GPL(vb2_poll);
2012
2013/**
2014 * vb2_queue_init() - initialize a videobuf2 queue
2015 * @q: videobuf2 queue; this structure should be allocated in driver
2016 *
2017 * The vb2_queue structure should be allocated by the driver. The driver is
2018 * responsible of clearing it's content and setting initial values for some
2019 * required entries before calling this function.
2020 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2021 * to the struct vb2_queue description in include/media/videobuf2-core.h
2022 * for more information.
2023 */
2024int vb2_queue_init(struct vb2_queue *q)
2025{
Ezequiel Garcia896f38f2012-09-17 14:59:30 -03002026 /*
2027 * Sanity check
2028 */
2029 if (WARN_ON(!q) ||
2030 WARN_ON(!q->ops) ||
2031 WARN_ON(!q->mem_ops) ||
2032 WARN_ON(!q->type) ||
2033 WARN_ON(!q->io_modes) ||
2034 WARN_ON(!q->ops->queue_setup) ||
2035 WARN_ON(!q->ops->buf_queue))
2036 return -EINVAL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03002037
2038 INIT_LIST_HEAD(&q->queued_list);
2039 INIT_LIST_HEAD(&q->done_list);
2040 spin_lock_init(&q->done_lock);
2041 init_waitqueue_head(&q->done_wq);
2042
2043 if (q->buf_struct_size == 0)
2044 q->buf_struct_size = sizeof(struct vb2_buffer);
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL_GPL(vb2_queue_init);
2049
2050/**
2051 * vb2_queue_release() - stop streaming, release the queue and free memory
2052 * @q: videobuf2 queue
2053 *
2054 * This function stops streaming and performs necessary clean ups, including
2055 * freeing video buffer memory. The driver is responsible for freeing
2056 * the vb2_queue structure itself.
2057 */
2058void vb2_queue_release(struct vb2_queue *q)
2059{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002060 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002061 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03002062 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03002063}
2064EXPORT_SYMBOL_GPL(vb2_queue_release);
2065
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002066/**
2067 * struct vb2_fileio_buf - buffer context used by file io emulator
2068 *
2069 * vb2 provides a compatibility layer and emulator of file io (read and
2070 * write) calls on top of streaming API. This structure is used for
2071 * tracking context related to the buffers.
2072 */
2073struct vb2_fileio_buf {
2074 void *vaddr;
2075 unsigned int size;
2076 unsigned int pos;
2077 unsigned int queued:1;
2078};
2079
2080/**
2081 * struct vb2_fileio_data - queue context used by file io emulator
2082 *
2083 * vb2 provides a compatibility layer and emulator of file io (read and
2084 * write) calls on top of streaming API. For proper operation it required
2085 * this structure to save the driver state between each call of the read
2086 * or write function.
2087 */
2088struct vb2_fileio_data {
2089 struct v4l2_requestbuffers req;
2090 struct v4l2_buffer b;
2091 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2092 unsigned int index;
2093 unsigned int q_count;
2094 unsigned int dq_count;
2095 unsigned int flags;
2096};
2097
2098/**
2099 * __vb2_init_fileio() - initialize file io emulator
2100 * @q: videobuf2 queue
2101 * @read: mode selector (1 means read, 0 means write)
2102 */
2103static int __vb2_init_fileio(struct vb2_queue *q, int read)
2104{
2105 struct vb2_fileio_data *fileio;
2106 int i, ret;
2107 unsigned int count = 0;
2108
2109 /*
2110 * Sanity check
2111 */
2112 if ((read && !(q->io_modes & VB2_READ)) ||
2113 (!read && !(q->io_modes & VB2_WRITE)))
2114 BUG();
2115
2116 /*
2117 * Check if device supports mapping buffers to kernel virtual space.
2118 */
2119 if (!q->mem_ops->vaddr)
2120 return -EBUSY;
2121
2122 /*
2123 * Check if streaming api has not been already activated.
2124 */
2125 if (q->streaming || q->num_buffers > 0)
2126 return -EBUSY;
2127
2128 /*
2129 * Start with count 1, driver can increase it in queue_setup()
2130 */
2131 count = 1;
2132
2133 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2134 (read) ? "read" : "write", count, q->io_flags);
2135
2136 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2137 if (fileio == NULL)
2138 return -ENOMEM;
2139
2140 fileio->flags = q->io_flags;
2141
2142 /*
2143 * Request buffers and use MMAP type to force driver
2144 * to allocate buffers by itself.
2145 */
2146 fileio->req.count = count;
2147 fileio->req.memory = V4L2_MEMORY_MMAP;
2148 fileio->req.type = q->type;
2149 ret = vb2_reqbufs(q, &fileio->req);
2150 if (ret)
2151 goto err_kfree;
2152
2153 /*
2154 * Check if plane_count is correct
2155 * (multiplane buffers are not supported).
2156 */
2157 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002158 ret = -EBUSY;
2159 goto err_reqbufs;
2160 }
2161
2162 /*
2163 * Get kernel address of each buffer.
2164 */
2165 for (i = 0; i < q->num_buffers; i++) {
2166 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2167 if (fileio->bufs[i].vaddr == NULL)
2168 goto err_reqbufs;
2169 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2170 }
2171
2172 /*
2173 * Read mode requires pre queuing of all buffers.
2174 */
2175 if (read) {
2176 /*
2177 * Queue all buffers.
2178 */
2179 for (i = 0; i < q->num_buffers; i++) {
2180 struct v4l2_buffer *b = &fileio->b;
2181 memset(b, 0, sizeof(*b));
2182 b->type = q->type;
2183 b->memory = q->memory;
2184 b->index = i;
2185 ret = vb2_qbuf(q, b);
2186 if (ret)
2187 goto err_reqbufs;
2188 fileio->bufs[i].queued = 1;
2189 }
2190
2191 /*
2192 * Start streaming.
2193 */
2194 ret = vb2_streamon(q, q->type);
2195 if (ret)
2196 goto err_reqbufs;
2197 }
2198
2199 q->fileio = fileio;
2200
2201 return ret;
2202
2203err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03002204 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002205 vb2_reqbufs(q, &fileio->req);
2206
2207err_kfree:
2208 kfree(fileio);
2209 return ret;
2210}
2211
2212/**
2213 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2214 * @q: videobuf2 queue
2215 */
2216static int __vb2_cleanup_fileio(struct vb2_queue *q)
2217{
2218 struct vb2_fileio_data *fileio = q->fileio;
2219
2220 if (fileio) {
2221 /*
2222 * Hack fileio context to enable direct calls to vb2 ioctl
2223 * interface.
2224 */
2225 q->fileio = NULL;
2226
2227 vb2_streamoff(q, q->type);
2228 fileio->req.count = 0;
2229 vb2_reqbufs(q, &fileio->req);
2230 kfree(fileio);
2231 dprintk(3, "file io emulator closed\n");
2232 }
2233 return 0;
2234}
2235
2236/**
2237 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2238 * @q: videobuf2 queue
2239 * @data: pointed to target userspace buffer
2240 * @count: number of bytes to read or write
2241 * @ppos: file handle position tracking pointer
2242 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2243 * @read: access mode selector (1 means read, 0 means write)
2244 */
2245static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2246 loff_t *ppos, int nonblock, int read)
2247{
2248 struct vb2_fileio_data *fileio;
2249 struct vb2_fileio_buf *buf;
2250 int ret, index;
2251
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002252 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002253 read ? "read" : "write", (long)*ppos, count,
2254 nonblock ? "non" : "");
2255
2256 if (!data)
2257 return -EINVAL;
2258
2259 /*
2260 * Initialize emulator on first call.
2261 */
2262 if (!q->fileio) {
2263 ret = __vb2_init_fileio(q, read);
2264 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2265 if (ret)
2266 return ret;
2267 }
2268 fileio = q->fileio;
2269
2270 /*
2271 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2272 * The pointer will be restored before returning from this function.
2273 */
2274 q->fileio = NULL;
2275
2276 index = fileio->index;
2277 buf = &fileio->bufs[index];
2278
2279 /*
2280 * Check if we need to dequeue the buffer.
2281 */
2282 if (buf->queued) {
2283 struct vb2_buffer *vb;
2284
2285 /*
2286 * Call vb2_dqbuf to get buffer back.
2287 */
2288 memset(&fileio->b, 0, sizeof(fileio->b));
2289 fileio->b.type = q->type;
2290 fileio->b.memory = q->memory;
2291 fileio->b.index = index;
2292 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2293 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2294 if (ret)
2295 goto end;
2296 fileio->dq_count += 1;
2297
2298 /*
2299 * Get number of bytes filled by the driver
2300 */
2301 vb = q->bufs[index];
2302 buf->size = vb2_get_plane_payload(vb, 0);
2303 buf->queued = 0;
2304 }
2305
2306 /*
2307 * Limit count on last few bytes of the buffer.
2308 */
2309 if (buf->pos + count > buf->size) {
2310 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002311 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002312 }
2313
2314 /*
2315 * Transfer data to userspace.
2316 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002317 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002318 count, index, buf->pos);
2319 if (read)
2320 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2321 else
2322 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2323 if (ret) {
2324 dprintk(3, "file io: error copying data\n");
2325 ret = -EFAULT;
2326 goto end;
2327 }
2328
2329 /*
2330 * Update counters.
2331 */
2332 buf->pos += count;
2333 *ppos += count;
2334
2335 /*
2336 * Queue next buffer if required.
2337 */
2338 if (buf->pos == buf->size ||
2339 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2340 /*
2341 * Check if this is the last buffer to read.
2342 */
2343 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2344 fileio->dq_count == 1) {
2345 dprintk(3, "file io: read limit reached\n");
2346 /*
2347 * Restore fileio pointer and release the context.
2348 */
2349 q->fileio = fileio;
2350 return __vb2_cleanup_fileio(q);
2351 }
2352
2353 /*
2354 * Call vb2_qbuf and give buffer to the driver.
2355 */
2356 memset(&fileio->b, 0, sizeof(fileio->b));
2357 fileio->b.type = q->type;
2358 fileio->b.memory = q->memory;
2359 fileio->b.index = index;
2360 fileio->b.bytesused = buf->pos;
2361 ret = vb2_qbuf(q, &fileio->b);
2362 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2363 if (ret)
2364 goto end;
2365
2366 /*
2367 * Buffer has been queued, update the status
2368 */
2369 buf->pos = 0;
2370 buf->queued = 1;
2371 buf->size = q->bufs[0]->v4l2_planes[0].length;
2372 fileio->q_count += 1;
2373
2374 /*
2375 * Switch to the next buffer
2376 */
2377 fileio->index = (index + 1) % q->num_buffers;
2378
2379 /*
2380 * Start streaming if required.
2381 */
2382 if (!read && !q->streaming) {
2383 ret = vb2_streamon(q, q->type);
2384 if (ret)
2385 goto end;
2386 }
2387 }
2388
2389 /*
2390 * Return proper number of bytes processed.
2391 */
2392 if (ret == 0)
2393 ret = count;
2394end:
2395 /*
2396 * Restore the fileio context and block vb2 ioctl interface.
2397 */
2398 q->fileio = fileio;
2399 return ret;
2400}
2401
2402size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2403 loff_t *ppos, int nonblocking)
2404{
2405 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2406}
2407EXPORT_SYMBOL_GPL(vb2_read);
2408
2409size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2410 loff_t *ppos, int nonblocking)
2411{
2412 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2413}
2414EXPORT_SYMBOL_GPL(vb2_write);
2415
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002416
2417/*
2418 * The following functions are not part of the vb2 core API, but are helper
2419 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2420 * and struct vb2_ops.
2421 * They contain boilerplate code that most if not all drivers have to do
2422 * and so they simplify the driver code.
2423 */
2424
2425/* The queue is busy if there is a owner and you are not that owner. */
2426static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2427{
2428 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2429}
2430
2431/* vb2 ioctl helpers */
2432
2433int vb2_ioctl_reqbufs(struct file *file, void *priv,
2434 struct v4l2_requestbuffers *p)
2435{
2436 struct video_device *vdev = video_devdata(file);
2437 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2438
2439 if (res)
2440 return res;
2441 if (vb2_queue_is_busy(vdev, file))
2442 return -EBUSY;
2443 res = __reqbufs(vdev->queue, p);
2444 /* If count == 0, then the owner has released all buffers and he
2445 is no longer owner of the queue. Otherwise we have a new owner. */
2446 if (res == 0)
2447 vdev->queue->owner = p->count ? file->private_data : NULL;
2448 return res;
2449}
2450EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2451
2452int vb2_ioctl_create_bufs(struct file *file, void *priv,
2453 struct v4l2_create_buffers *p)
2454{
2455 struct video_device *vdev = video_devdata(file);
2456 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2457
2458 p->index = vdev->queue->num_buffers;
2459 /* If count == 0, then just check if memory and type are valid.
2460 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2461 if (p->count == 0)
2462 return res != -EBUSY ? res : 0;
2463 if (res)
2464 return res;
2465 if (vb2_queue_is_busy(vdev, file))
2466 return -EBUSY;
2467 res = __create_bufs(vdev->queue, p);
2468 if (res == 0)
2469 vdev->queue->owner = file->private_data;
2470 return res;
2471}
2472EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2473
2474int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2475 struct v4l2_buffer *p)
2476{
2477 struct video_device *vdev = video_devdata(file);
2478
2479 if (vb2_queue_is_busy(vdev, file))
2480 return -EBUSY;
2481 return vb2_prepare_buf(vdev->queue, p);
2482}
2483EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2484
2485int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2486{
2487 struct video_device *vdev = video_devdata(file);
2488
2489 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2490 return vb2_querybuf(vdev->queue, p);
2491}
2492EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2493
2494int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2495{
2496 struct video_device *vdev = video_devdata(file);
2497
2498 if (vb2_queue_is_busy(vdev, file))
2499 return -EBUSY;
2500 return vb2_qbuf(vdev->queue, p);
2501}
2502EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2503
2504int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2505{
2506 struct video_device *vdev = video_devdata(file);
2507
2508 if (vb2_queue_is_busy(vdev, file))
2509 return -EBUSY;
2510 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2511}
2512EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2513
2514int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2515{
2516 struct video_device *vdev = video_devdata(file);
2517
2518 if (vb2_queue_is_busy(vdev, file))
2519 return -EBUSY;
2520 return vb2_streamon(vdev->queue, i);
2521}
2522EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2523
2524int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2525{
2526 struct video_device *vdev = video_devdata(file);
2527
2528 if (vb2_queue_is_busy(vdev, file))
2529 return -EBUSY;
2530 return vb2_streamoff(vdev->queue, i);
2531}
2532EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2533
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -03002534int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2535{
2536 struct video_device *vdev = video_devdata(file);
2537
2538 if (vb2_queue_is_busy(vdev, file))
2539 return -EBUSY;
2540 return vb2_expbuf(vdev->queue, p);
2541}
2542EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2543
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002544/* v4l2_file_operations helpers */
2545
2546int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2547{
2548 struct video_device *vdev = video_devdata(file);
2549
2550 return vb2_mmap(vdev->queue, vma);
2551}
2552EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2553
2554int vb2_fop_release(struct file *file)
2555{
2556 struct video_device *vdev = video_devdata(file);
2557
2558 if (file->private_data == vdev->queue->owner) {
2559 vb2_queue_release(vdev->queue);
2560 vdev->queue->owner = NULL;
2561 }
2562 return v4l2_fh_release(file);
2563}
2564EXPORT_SYMBOL_GPL(vb2_fop_release);
2565
2566ssize_t vb2_fop_write(struct file *file, char __user *buf,
2567 size_t count, loff_t *ppos)
2568{
2569 struct video_device *vdev = video_devdata(file);
2570 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002571 int err = -EBUSY;
2572
Hans Verkuilcf533732012-07-31 04:02:25 -03002573 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002574 return -ERESTARTSYS;
2575 if (vb2_queue_is_busy(vdev, file))
2576 goto exit;
2577 err = vb2_write(vdev->queue, buf, count, ppos,
2578 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002579 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002580 vdev->queue->owner = file->private_data;
2581exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002582 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002583 mutex_unlock(lock);
2584 return err;
2585}
2586EXPORT_SYMBOL_GPL(vb2_fop_write);
2587
2588ssize_t vb2_fop_read(struct file *file, char __user *buf,
2589 size_t count, loff_t *ppos)
2590{
2591 struct video_device *vdev = video_devdata(file);
2592 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002593 int err = -EBUSY;
2594
Hans Verkuilcf533732012-07-31 04:02:25 -03002595 if (lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002596 return -ERESTARTSYS;
2597 if (vb2_queue_is_busy(vdev, file))
2598 goto exit;
2599 err = vb2_read(vdev->queue, buf, count, ppos,
2600 file->f_flags & O_NONBLOCK);
Hans Verkuil8c82c752012-09-07 12:50:02 -03002601 if (vdev->queue->fileio)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002602 vdev->queue->owner = file->private_data;
2603exit:
Hans Verkuilcf533732012-07-31 04:02:25 -03002604 if (lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002605 mutex_unlock(lock);
2606 return err;
2607}
2608EXPORT_SYMBOL_GPL(vb2_fop_read);
2609
2610unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2611{
2612 struct video_device *vdev = video_devdata(file);
2613 struct vb2_queue *q = vdev->queue;
2614 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2615 unsigned long req_events = poll_requested_events(wait);
2616 unsigned res;
2617 void *fileio;
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002618 bool must_lock = false;
2619
2620 /* Try to be smart: only lock if polling might start fileio,
2621 otherwise locking will only introduce unwanted delays. */
2622 if (q->num_buffers == 0 && q->fileio == NULL) {
2623 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2624 (req_events & (POLLIN | POLLRDNORM)))
2625 must_lock = true;
2626 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2627 (req_events & (POLLOUT | POLLWRNORM)))
2628 must_lock = true;
2629 }
2630
2631 /* If locking is needed, but this helper doesn't know how, then you
2632 shouldn't be using this helper but you should write your own. */
Hans Verkuilcf533732012-07-31 04:02:25 -03002633 WARN_ON(must_lock && !lock);
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002634
Hans Verkuilcf533732012-07-31 04:02:25 -03002635 if (must_lock && lock && mutex_lock_interruptible(lock))
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002636 return POLLERR;
2637
2638 fileio = q->fileio;
2639
2640 res = vb2_poll(vdev->queue, file, wait);
2641
2642 /* If fileio was started, then we have a new queue owner. */
2643 if (must_lock && !fileio && q->fileio)
2644 q->owner = file->private_data;
Hans Verkuilcf533732012-07-31 04:02:25 -03002645 if (must_lock && lock)
Hans Verkuil4c1ffca2012-07-02 05:59:18 -03002646 mutex_unlock(lock);
2647 return res;
2648}
2649EXPORT_SYMBOL_GPL(vb2_fop_poll);
2650
2651#ifndef CONFIG_MMU
2652unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2653 unsigned long len, unsigned long pgoff, unsigned long flags)
2654{
2655 struct video_device *vdev = video_devdata(file);
2656
2657 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2658}
2659EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2660#endif
2661
2662/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2663
2664void vb2_ops_wait_prepare(struct vb2_queue *vq)
2665{
2666 mutex_unlock(vq->lock);
2667}
2668EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2669
2670void vb2_ops_wait_finish(struct vb2_queue *vq)
2671{
2672 mutex_lock(vq->lock);
2673}
2674EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2675
Pawel Osciake23ccc02010-10-11 10:56:41 -03002676MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002677MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002678MODULE_LICENSE("GPL");