blob: 60e7bc78d190551e889fb0ce0aad8935d21ad381 [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/**
112 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
113 * every buffer on the queue
114 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300115static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300116{
117 unsigned int buffer, plane;
118 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300119 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300120
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300121 if (q->num_buffers) {
122 struct v4l2_plane *p;
123 vb = q->bufs[q->num_buffers - 1];
124 p = &vb->v4l2_planes[vb->num_planes - 1];
125 off = PAGE_ALIGN(p->m.mem_offset + p->length);
126 } else {
127 off = 0;
128 }
129
130 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300131 vb = q->bufs[buffer];
132 if (!vb)
133 continue;
134
135 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300136 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300137 vb->v4l2_planes[plane].m.mem_offset = off;
138
139 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
140 buffer, plane, off);
141
142 off += vb->v4l2_planes[plane].length;
143 off = PAGE_ALIGN(off);
144 }
145 }
146}
147
148/**
149 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
150 * video buffer memory for all buffers/planes on the queue and initializes the
151 * queue
152 *
153 * Returns the number of buffers successfully allocated.
154 */
155static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300156 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300157{
158 unsigned int buffer;
159 struct vb2_buffer *vb;
160 int ret;
161
162 for (buffer = 0; buffer < num_buffers; ++buffer) {
163 /* Allocate videobuf buffer structures */
164 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
165 if (!vb) {
166 dprintk(1, "Memory alloc for buffer struct failed\n");
167 break;
168 }
169
170 /* Length stores number of planes for multiplanar buffers */
171 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
172 vb->v4l2_buf.length = num_planes;
173
174 vb->state = VB2_BUF_STATE_DEQUEUED;
175 vb->vb2_queue = q;
176 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300177 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300178 vb->v4l2_buf.type = q->type;
179 vb->v4l2_buf.memory = memory;
180
181 /* Allocate video buffer memory for the MMAP type */
182 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300183 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300184 if (ret) {
185 dprintk(1, "Failed allocating memory for "
186 "buffer %d\n", buffer);
187 kfree(vb);
188 break;
189 }
190 /*
191 * Call the driver-provided buffer initialization
192 * callback, if given. An error in initialization
193 * results in queue setup failure.
194 */
195 ret = call_qop(q, buf_init, vb);
196 if (ret) {
197 dprintk(1, "Buffer %d %p initialization"
198 " failed\n", buffer, vb);
199 __vb2_buf_mem_free(vb);
200 kfree(vb);
201 break;
202 }
203 }
204
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300205 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300206 }
207
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300208 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300209
210 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300211 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300212
213 return buffer;
214}
215
216/**
217 * __vb2_free_mem() - release all video buffer memory for a given queue
218 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300219static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300220{
221 unsigned int buffer;
222 struct vb2_buffer *vb;
223
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300224 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
225 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300226 vb = q->bufs[buffer];
227 if (!vb)
228 continue;
229
230 /* Free MMAP buffers or release USERPTR buffers */
231 if (q->memory == V4L2_MEMORY_MMAP)
232 __vb2_buf_mem_free(vb);
233 else
234 __vb2_buf_userptr_put(vb);
235 }
236}
237
238/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300239 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
240 * related information, if no buffers are left return the queue to an
241 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300243static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300244{
245 unsigned int buffer;
246
247 /* Call driver-provided cleanup function for each buffer, if provided */
248 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300249 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
250 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300251 if (NULL == q->bufs[buffer])
252 continue;
253 q->ops->buf_cleanup(q->bufs[buffer]);
254 }
255 }
256
257 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300258 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300259
260 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300261 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
262 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300263 kfree(q->bufs[buffer]);
264 q->bufs[buffer] = NULL;
265 }
266
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300267 q->num_buffers -= buffers;
268 if (!q->num_buffers)
269 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300270 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300271}
272
273/**
274 * __verify_planes_array() - verify that the planes array passed in struct
275 * v4l2_buffer from userspace can be safely used
276 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300277static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300278{
279 /* Is memory for copying plane information present? */
280 if (NULL == b->m.planes) {
281 dprintk(1, "Multi-planar buffer passed but "
282 "planes array not provided\n");
283 return -EINVAL;
284 }
285
286 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
287 dprintk(1, "Incorrect planes array length, "
288 "expected %d, got %d\n", vb->num_planes, b->length);
289 return -EINVAL;
290 }
291
292 return 0;
293}
294
295/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300296 * __buffer_in_use() - return true if the buffer is in use and
297 * the queue cannot be freed (by the means of REQBUFS(0)) call
298 */
299static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
300{
301 unsigned int plane;
302 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300303 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300304 /*
305 * If num_users() has not been provided, call_memop
306 * will return 0, apparently nobody cares about this
307 * case anyway. If num_users() returns more than 1,
308 * we are not the only user of the plane's memory.
309 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300310 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300311 return true;
312 }
313 return false;
314}
315
316/**
317 * __buffers_in_use() - return true if any buffers on the queue are in use and
318 * the queue cannot be freed (by the means of REQBUFS(0)) call
319 */
320static bool __buffers_in_use(struct vb2_queue *q)
321{
322 unsigned int buffer;
323 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
324 if (__buffer_in_use(q, q->bufs[buffer]))
325 return true;
326 }
327 return false;
328}
329
330/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300331 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
332 * returned to userspace
333 */
334static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
335{
336 struct vb2_queue *q = vb->vb2_queue;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300337 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300338
Sakari Ailus2b719d72012-05-02 09:40:03 -0300339 /* Copy back data such as timestamp, flags, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300340 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
Sakari Ailus2b719d72012-05-02 09:40:03 -0300341 b->reserved2 = vb->v4l2_buf.reserved2;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300342 b->reserved = vb->v4l2_buf.reserved;
343
344 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
345 ret = __verify_planes_array(vb, b);
346 if (ret)
347 return ret;
348
349 /*
350 * Fill in plane-related data if userspace provided an array
351 * for it. The memory and size is verified above.
352 */
353 memcpy(b->m.planes, vb->v4l2_planes,
354 b->length * sizeof(struct v4l2_plane));
355 } else {
356 /*
357 * We use length and offset in v4l2_planes array even for
358 * single-planar buffers, but userspace does not.
359 */
360 b->length = vb->v4l2_planes[0].length;
361 b->bytesused = vb->v4l2_planes[0].bytesused;
362 if (q->memory == V4L2_MEMORY_MMAP)
363 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
364 else if (q->memory == V4L2_MEMORY_USERPTR)
365 b->m.userptr = vb->v4l2_planes[0].m.userptr;
366 }
367
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300368 /*
369 * Clear any buffer state related flags.
370 */
371 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300372
373 switch (vb->state) {
374 case VB2_BUF_STATE_QUEUED:
375 case VB2_BUF_STATE_ACTIVE:
376 b->flags |= V4L2_BUF_FLAG_QUEUED;
377 break;
378 case VB2_BUF_STATE_ERROR:
379 b->flags |= V4L2_BUF_FLAG_ERROR;
380 /* fall through */
381 case VB2_BUF_STATE_DONE:
382 b->flags |= V4L2_BUF_FLAG_DONE;
383 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300384 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300385 b->flags |= V4L2_BUF_FLAG_PREPARED;
386 break;
387 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300388 /* nothing */
389 break;
390 }
391
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300392 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300393 b->flags |= V4L2_BUF_FLAG_MAPPED;
394
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300395 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300396}
397
398/**
399 * vb2_querybuf() - query video buffer information
400 * @q: videobuf queue
401 * @b: buffer struct passed from userspace to vidioc_querybuf handler
402 * in driver
403 *
404 * Should be called from vidioc_querybuf ioctl handler in driver.
405 * This function will verify the passed v4l2_buffer structure and fill the
406 * relevant information for the userspace.
407 *
408 * The return values from this function are intended to be directly returned
409 * from vidioc_querybuf handler in driver.
410 */
411int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
412{
413 struct vb2_buffer *vb;
414
415 if (b->type != q->type) {
416 dprintk(1, "querybuf: wrong buffer type\n");
417 return -EINVAL;
418 }
419
420 if (b->index >= q->num_buffers) {
421 dprintk(1, "querybuf: buffer index out of range\n");
422 return -EINVAL;
423 }
424 vb = q->bufs[b->index];
425
426 return __fill_v4l2_buffer(vb, b);
427}
428EXPORT_SYMBOL(vb2_querybuf);
429
430/**
431 * __verify_userptr_ops() - verify that all memory operations required for
432 * USERPTR queue type have been provided
433 */
434static int __verify_userptr_ops(struct vb2_queue *q)
435{
436 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
437 !q->mem_ops->put_userptr)
438 return -EINVAL;
439
440 return 0;
441}
442
443/**
444 * __verify_mmap_ops() - verify that all memory operations required for
445 * MMAP queue type have been provided
446 */
447static int __verify_mmap_ops(struct vb2_queue *q)
448{
449 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
450 !q->mem_ops->put || !q->mem_ops->mmap)
451 return -EINVAL;
452
453 return 0;
454}
455
456/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300457 * __verify_memory_type() - Check whether the memory type and buffer type
458 * passed to a buffer operation are compatible with the queue.
459 */
460static int __verify_memory_type(struct vb2_queue *q,
461 enum v4l2_memory memory, enum v4l2_buf_type type)
462{
463 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR) {
464 dprintk(1, "reqbufs: unsupported memory type\n");
465 return -EINVAL;
466 }
467
468 if (type != q->type) {
469 dprintk(1, "reqbufs: requested type is incorrect\n");
470 return -EINVAL;
471 }
472
473 /*
474 * Make sure all the required memory ops for given memory type
475 * are available.
476 */
477 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
478 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
479 return -EINVAL;
480 }
481
482 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
483 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
484 return -EINVAL;
485 }
486
487 /*
488 * Place the busy tests at the end: -EBUSY can be ignored when
489 * create_bufs is called with count == 0, but count == 0 should still
490 * do the memory and type validation.
491 */
492 if (q->fileio) {
493 dprintk(1, "reqbufs: file io in progress\n");
494 return -EBUSY;
495 }
496 return 0;
497}
498
499/**
500 * __reqbufs() - Initiate streaming
Pawel Osciake23ccc02010-10-11 10:56:41 -0300501 * @q: videobuf2 queue
502 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
503 *
504 * Should be called from vidioc_reqbufs ioctl handler of a driver.
505 * This function:
506 * 1) verifies streaming parameters passed from the userspace,
507 * 2) sets up the queue,
508 * 3) negotiates number of buffers and planes per buffer with the driver
509 * to be used during streaming,
510 * 4) allocates internal buffer structures (struct vb2_buffer), according to
511 * the agreed parameters,
512 * 5) for MMAP memory type, allocates actual video memory, using the
513 * memory handling/allocation routines provided during queue initialization
514 *
515 * If req->count is 0, all the memory will be freed instead.
516 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
517 * and the queue is not busy, memory will be reallocated.
518 *
519 * The return values from this function are intended to be directly returned
520 * from vidioc_reqbufs handler in driver.
521 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300522static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300523{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300524 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300525 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300526
527 if (q->streaming) {
528 dprintk(1, "reqbufs: streaming active\n");
529 return -EBUSY;
530 }
531
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300532 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300533 /*
534 * We already have buffers allocated, so first check if they
535 * are not in use and can be freed.
536 */
537 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
538 dprintk(1, "reqbufs: memory in use, cannot free\n");
539 return -EBUSY;
540 }
541
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300542 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300543
544 /*
545 * In case of REQBUFS(0) return immediately without calling
546 * driver's queue_setup() callback and allocating resources.
547 */
548 if (req->count == 0)
549 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300550 }
551
552 /*
553 * Make sure the requested values and current defaults are sane.
554 */
555 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300556 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300557 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300558 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300559
560 /*
561 * Ask the driver how many buffers and planes per buffer it requires.
562 * Driver also sets the size and allocator context for each plane.
563 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300564 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300565 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300566 if (ret)
567 return ret;
568
569 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300570 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300571 if (ret == 0) {
572 dprintk(1, "Memory allocation failed\n");
573 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300574 }
575
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300576 allocated_buffers = ret;
577
Pawel Osciake23ccc02010-10-11 10:56:41 -0300578 /*
579 * Check if driver can handle the allocated number of buffers.
580 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300581 if (allocated_buffers < num_buffers) {
582 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300583
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300584 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
585 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300586
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300587 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300588 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300589
590 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300591 * Either the driver has accepted a smaller number of buffers,
592 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300593 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300594 }
595
596 q->num_buffers = allocated_buffers;
597
598 if (ret < 0) {
599 __vb2_queue_free(q, allocated_buffers);
600 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300601 }
602
Pawel Osciake23ccc02010-10-11 10:56:41 -0300603 /*
604 * Return the number of successfully allocated buffers
605 * to the userspace.
606 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300607 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300608
609 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300610}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300611
612/**
613 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
614 * type values.
615 * @q: videobuf2 queue
616 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
617 */
618int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
619{
620 int ret = __verify_memory_type(q, req->memory, req->type);
621
622 return ret ? ret : __reqbufs(q, req);
623}
Pawel Osciake23ccc02010-10-11 10:56:41 -0300624EXPORT_SYMBOL_GPL(vb2_reqbufs);
625
626/**
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300627 * __create_bufs() - Allocate buffers and any required auxiliary structs
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300628 * @q: videobuf2 queue
629 * @create: creation parameters, passed from userspace to vidioc_create_bufs
630 * handler in driver
631 *
632 * Should be called from vidioc_create_bufs ioctl handler of a driver.
633 * This function:
634 * 1) verifies parameter sanity
635 * 2) calls the .queue_setup() queue operation
636 * 3) performs any necessary memory allocations
637 *
638 * The return values from this function are intended to be directly returned
639 * from vidioc_create_bufs handler in driver.
640 */
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300641static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300642{
643 unsigned int num_planes = 0, num_buffers, allocated_buffers;
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300644 int ret;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300645
646 if (q->num_buffers == VIDEO_MAX_FRAME) {
647 dprintk(1, "%s(): maximum number of buffers already allocated\n",
648 __func__);
649 return -ENOBUFS;
650 }
651
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300652 if (!q->num_buffers) {
653 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
654 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
655 q->memory = create->memory;
656 }
657
658 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
659
660 /*
661 * Ask the driver, whether the requested number of buffers, planes per
662 * buffer and their sizes are acceptable
663 */
664 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
665 &num_planes, q->plane_sizes, q->alloc_ctx);
666 if (ret)
667 return ret;
668
669 /* Finally, allocate buffers and video memory */
670 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
671 num_planes);
672 if (ret < 0) {
673 dprintk(1, "Memory allocation failed with error: %d\n", ret);
674 return ret;
675 }
676
677 allocated_buffers = ret;
678
679 /*
680 * Check if driver can handle the so far allocated number of buffers.
681 */
682 if (ret < num_buffers) {
683 num_buffers = ret;
684
685 /*
686 * q->num_buffers contains the total number of buffers, that the
687 * queue driver has set up
688 */
689 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
690 &num_planes, q->plane_sizes, q->alloc_ctx);
691
692 if (!ret && allocated_buffers < num_buffers)
693 ret = -ENOMEM;
694
695 /*
696 * Either the driver has accepted a smaller number of buffers,
697 * or .queue_setup() returned an error
698 */
699 }
700
701 q->num_buffers += allocated_buffers;
702
703 if (ret < 0) {
704 __vb2_queue_free(q, allocated_buffers);
705 return ret;
706 }
707
708 /*
709 * Return the number of successfully allocated buffers
710 * to the userspace.
711 */
712 create->count = allocated_buffers;
713
714 return 0;
715}
Hans Verkuil37d9ed92012-06-27 17:10:30 -0300716
717/**
718 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
719 * type values.
720 * @q: videobuf2 queue
721 * @create: creation parameters, passed from userspace to vidioc_create_bufs
722 * handler in driver
723 */
724int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
725{
726 int ret = __verify_memory_type(q, create->memory, create->format.type);
727
728 create->index = q->num_buffers;
729 return ret ? ret : __create_bufs(q, create);
730}
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300731EXPORT_SYMBOL_GPL(vb2_create_bufs);
732
733/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300734 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
735 * @vb: vb2_buffer to which the plane in question belongs to
736 * @plane_no: plane number for which the address is to be returned
737 *
738 * This function returns a kernel virtual address of a given plane if
739 * such a mapping exist, NULL otherwise.
740 */
741void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
742{
743 struct vb2_queue *q = vb->vb2_queue;
744
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300745 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300746 return NULL;
747
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300748 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300749
750}
751EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
752
753/**
754 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
755 * @vb: vb2_buffer to which the plane in question belongs to
756 * @plane_no: plane number for which the cookie is to be returned
757 *
758 * This function returns an allocator specific cookie for a given plane if
759 * available, NULL otherwise. The allocator should provide some simple static
760 * inline function, which would convert this cookie to the allocator specific
761 * type that can be used directly by the driver to access the buffer. This can
762 * be for example physical address, pointer to scatter list or IOMMU mapping.
763 */
764void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
765{
766 struct vb2_queue *q = vb->vb2_queue;
767
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300768 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300769 return NULL;
770
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300771 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300772}
773EXPORT_SYMBOL_GPL(vb2_plane_cookie);
774
775/**
776 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
777 * @vb: vb2_buffer returned from the driver
778 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
779 * or VB2_BUF_STATE_ERROR if the operation finished with an error
780 *
781 * This function should be called by the driver after a hardware operation on
782 * a buffer is finished and the buffer may be returned to userspace. The driver
783 * cannot use this buffer anymore until it is queued back to it by videobuf
784 * by the means of buf_queue callback. Only buffers previously queued to the
785 * driver by buf_queue can be passed to this function.
786 */
787void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
788{
789 struct vb2_queue *q = vb->vb2_queue;
790 unsigned long flags;
791
792 if (vb->state != VB2_BUF_STATE_ACTIVE)
793 return;
794
795 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
796 return;
797
798 dprintk(4, "Done processing on buffer %d, state: %d\n",
799 vb->v4l2_buf.index, vb->state);
800
801 /* Add the buffer to the done buffers list */
802 spin_lock_irqsave(&q->done_lock, flags);
803 vb->state = state;
804 list_add_tail(&vb->done_entry, &q->done_list);
805 atomic_dec(&q->queued_count);
806 spin_unlock_irqrestore(&q->done_lock, flags);
807
808 /* Inform any processes that may be waiting for buffers */
809 wake_up(&q->done_wq);
810}
811EXPORT_SYMBOL_GPL(vb2_buffer_done);
812
813/**
814 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
815 * a v4l2_buffer by the userspace
816 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300817static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300818 struct v4l2_plane *v4l2_planes)
819{
820 unsigned int plane;
821 int ret;
822
823 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
824 /*
825 * Verify that the userspace gave us a valid array for
826 * plane information.
827 */
828 ret = __verify_planes_array(vb, b);
829 if (ret)
830 return ret;
831
832 /* Fill in driver-provided information for OUTPUT types */
833 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
834 /*
835 * Will have to go up to b->length when API starts
836 * accepting variable number of planes.
837 */
838 for (plane = 0; plane < vb->num_planes; ++plane) {
839 v4l2_planes[plane].bytesused =
840 b->m.planes[plane].bytesused;
841 v4l2_planes[plane].data_offset =
842 b->m.planes[plane].data_offset;
843 }
844 }
845
846 if (b->memory == V4L2_MEMORY_USERPTR) {
847 for (plane = 0; plane < vb->num_planes; ++plane) {
848 v4l2_planes[plane].m.userptr =
849 b->m.planes[plane].m.userptr;
850 v4l2_planes[plane].length =
851 b->m.planes[plane].length;
852 }
853 }
854 } else {
855 /*
856 * Single-planar buffers do not use planes array,
857 * so fill in relevant v4l2_buffer struct fields instead.
858 * In videobuf we use our internal V4l2_planes struct for
859 * single-planar buffers as well, for simplicity.
860 */
861 if (V4L2_TYPE_IS_OUTPUT(b->type))
862 v4l2_planes[0].bytesused = b->bytesused;
863
864 if (b->memory == V4L2_MEMORY_USERPTR) {
865 v4l2_planes[0].m.userptr = b->m.userptr;
866 v4l2_planes[0].length = b->length;
867 }
868 }
869
870 vb->v4l2_buf.field = b->field;
871 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300872 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300873
874 return 0;
875}
876
877/**
878 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
879 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300880static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300881{
882 struct v4l2_plane planes[VIDEO_MAX_PLANES];
883 struct vb2_queue *q = vb->vb2_queue;
884 void *mem_priv;
885 unsigned int plane;
886 int ret;
887 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
888
889 /* Verify and copy relevant information provided by the userspace */
890 ret = __fill_vb2_buffer(vb, b, planes);
891 if (ret)
892 return ret;
893
894 for (plane = 0; plane < vb->num_planes; ++plane) {
895 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300896 if (vb->v4l2_planes[plane].m.userptr &&
897 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300898 && vb->v4l2_planes[plane].length == planes[plane].length)
899 continue;
900
901 dprintk(3, "qbuf: userspace address for plane %d changed, "
902 "reacquiring memory\n", plane);
903
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300904 /* Check if the provided plane buffer is large enough */
905 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300906 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300907 goto err;
908 }
909
Pawel Osciake23ccc02010-10-11 10:56:41 -0300910 /* Release previously acquired memory if present */
911 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300912 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300913
914 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300915 vb->v4l2_planes[plane].m.userptr = 0;
916 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300917
918 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300919 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
920 planes[plane].m.userptr,
921 planes[plane].length, write);
922 if (IS_ERR_OR_NULL(mem_priv)) {
923 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300924 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300925 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
926 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300927 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300928 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300929 }
930
931 /*
932 * Call driver-specific initialization on the newly acquired buffer,
933 * if provided.
934 */
935 ret = call_qop(q, buf_init, vb);
936 if (ret) {
937 dprintk(1, "qbuf: buffer initialization failed\n");
938 goto err;
939 }
940
941 /*
942 * Now that everything is in order, copy relevant information
943 * provided by userspace.
944 */
945 for (plane = 0; plane < vb->num_planes; ++plane)
946 vb->v4l2_planes[plane] = planes[plane];
947
948 return 0;
949err:
950 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300951 for (plane = 0; plane < vb->num_planes; ++plane) {
952 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300953 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300954 vb->planes[plane].mem_priv = NULL;
955 vb->v4l2_planes[plane].m.userptr = 0;
956 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300957 }
958
959 return ret;
960}
961
962/**
963 * __qbuf_mmap() - handle qbuf of an MMAP buffer
964 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300965static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300966{
967 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
968}
969
970/**
971 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
972 */
973static void __enqueue_in_driver(struct vb2_buffer *vb)
974{
975 struct vb2_queue *q = vb->vb2_queue;
976
977 vb->state = VB2_BUF_STATE_ACTIVE;
978 atomic_inc(&q->queued_count);
979 q->ops->buf_queue(vb);
980}
981
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300982static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300983{
984 struct vb2_queue *q = vb->vb2_queue;
985 int ret;
986
987 switch (q->memory) {
988 case V4L2_MEMORY_MMAP:
989 ret = __qbuf_mmap(vb, b);
990 break;
991 case V4L2_MEMORY_USERPTR:
992 ret = __qbuf_userptr(vb, b);
993 break;
994 default:
995 WARN(1, "Invalid queue type\n");
996 ret = -EINVAL;
997 }
998
999 if (!ret)
1000 ret = call_qop(q, buf_prepare, vb);
1001 if (ret)
1002 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
1003 else
1004 vb->state = VB2_BUF_STATE_PREPARED;
1005
1006 return ret;
1007}
1008
Pawel Osciake23ccc02010-10-11 10:56:41 -03001009/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001010 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1011 * @q: videobuf2 queue
1012 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1013 * handler in driver
1014 *
1015 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1016 * This function:
1017 * 1) verifies the passed buffer,
1018 * 2) calls buf_prepare callback in the driver (if provided), in which
1019 * driver-specific buffer initialization can be performed,
1020 *
1021 * The return values from this function are intended to be directly returned
1022 * from vidioc_prepare_buf handler in driver.
1023 */
1024int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1025{
1026 struct vb2_buffer *vb;
1027 int ret;
1028
1029 if (q->fileio) {
1030 dprintk(1, "%s(): file io in progress\n", __func__);
1031 return -EBUSY;
1032 }
1033
1034 if (b->type != q->type) {
1035 dprintk(1, "%s(): invalid buffer type\n", __func__);
1036 return -EINVAL;
1037 }
1038
1039 if (b->index >= q->num_buffers) {
1040 dprintk(1, "%s(): buffer index out of range\n", __func__);
1041 return -EINVAL;
1042 }
1043
1044 vb = q->bufs[b->index];
1045 if (NULL == vb) {
1046 /* Should never happen */
1047 dprintk(1, "%s(): buffer is NULL\n", __func__);
1048 return -EINVAL;
1049 }
1050
1051 if (b->memory != q->memory) {
1052 dprintk(1, "%s(): invalid memory type\n", __func__);
1053 return -EINVAL;
1054 }
1055
1056 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1057 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1058 return -EINVAL;
1059 }
1060
1061 ret = __buf_prepare(vb, b);
1062 if (ret < 0)
1063 return ret;
1064
1065 __fill_v4l2_buffer(vb, b);
1066
1067 return 0;
1068}
1069EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1070
1071/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001072 * vb2_qbuf() - Queue a buffer from userspace
1073 * @q: videobuf2 queue
1074 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1075 * in driver
1076 *
1077 * Should be called from vidioc_qbuf ioctl handler of a driver.
1078 * This function:
1079 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001080 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1081 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001082 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1083 * callback for processing.
1084 *
1085 * The return values from this function are intended to be directly returned
1086 * from vidioc_qbuf handler in driver.
1087 */
1088int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1089{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001090 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001091 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001092 int ret = 0;
1093
1094 /*
1095 * In case of user pointer buffers vb2 allocator needs to get direct
1096 * access to userspace pages. This requires getting read access on
1097 * mmap semaphore in the current process structure. The same
1098 * semaphore is taken before calling mmap operation, while both mmap
1099 * and qbuf are called by the driver or v4l2 core with driver's lock
1100 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1101 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1102 * release driver's lock, takes mmap_sem and then takes again driver's
1103 * lock.
1104 *
1105 * To avoid race with other vb2 calls, which might be called after
1106 * releasing driver's lock, this operation is performed at the
1107 * beggining of qbuf processing. This way the queue status is
1108 * consistent after getting driver's lock back.
1109 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001110 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001111 mmap_sem = &current->mm->mmap_sem;
1112 call_qop(q, wait_prepare, q);
1113 down_read(mmap_sem);
1114 call_qop(q, wait_finish, q);
1115 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001116
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001117 if (q->fileio) {
1118 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001119 ret = -EBUSY;
1120 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001121 }
1122
Pawel Osciake23ccc02010-10-11 10:56:41 -03001123 if (b->type != q->type) {
1124 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001125 ret = -EINVAL;
1126 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001127 }
1128
1129 if (b->index >= q->num_buffers) {
1130 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001131 ret = -EINVAL;
1132 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001133 }
1134
1135 vb = q->bufs[b->index];
1136 if (NULL == vb) {
1137 /* Should never happen */
1138 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001139 ret = -EINVAL;
1140 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001141 }
1142
1143 if (b->memory != q->memory) {
1144 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001145 ret = -EINVAL;
1146 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001147 }
1148
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001149 switch (vb->state) {
1150 case VB2_BUF_STATE_DEQUEUED:
1151 ret = __buf_prepare(vb, b);
1152 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001153 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001154 case VB2_BUF_STATE_PREPARED:
1155 break;
1156 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001157 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001158 ret = -EINVAL;
1159 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001160 }
1161
Pawel Osciake23ccc02010-10-11 10:56:41 -03001162 /*
1163 * Add to the queued buffers list, a buffer will stay on it until
1164 * dequeued in dqbuf.
1165 */
1166 list_add_tail(&vb->queued_entry, &q->queued_list);
1167 vb->state = VB2_BUF_STATE_QUEUED;
1168
1169 /*
1170 * If already streaming, give the buffer to driver for processing.
1171 * If not, the buffer will be given to driver on next streamon.
1172 */
1173 if (q->streaming)
1174 __enqueue_in_driver(vb);
1175
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001176 /* Fill buffer information for the userspace */
1177 __fill_v4l2_buffer(vb, b);
1178
Pawel Osciake23ccc02010-10-11 10:56:41 -03001179 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001180unlock:
1181 if (mmap_sem)
1182 up_read(mmap_sem);
1183 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001184}
1185EXPORT_SYMBOL_GPL(vb2_qbuf);
1186
1187/**
1188 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1189 * for dequeuing
1190 *
1191 * Will sleep if required for nonblocking == false.
1192 */
1193static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1194{
1195 /*
1196 * All operations on vb_done_list are performed under done_lock
1197 * spinlock protection. However, buffers may be removed from
1198 * it and returned to userspace only while holding both driver's
1199 * lock and the done_lock spinlock. Thus we can be sure that as
1200 * long as we hold the driver's lock, the list will remain not
1201 * empty if list_empty() check succeeds.
1202 */
1203
1204 for (;;) {
1205 int ret;
1206
1207 if (!q->streaming) {
1208 dprintk(1, "Streaming off, will not wait for buffers\n");
1209 return -EINVAL;
1210 }
1211
1212 if (!list_empty(&q->done_list)) {
1213 /*
1214 * Found a buffer that we were waiting for.
1215 */
1216 break;
1217 }
1218
1219 if (nonblocking) {
1220 dprintk(1, "Nonblocking and no buffers to dequeue, "
1221 "will not wait\n");
1222 return -EAGAIN;
1223 }
1224
1225 /*
1226 * We are streaming and blocking, wait for another buffer to
1227 * become ready or for streamoff. Driver's lock is released to
1228 * allow streamoff or qbuf to be called while waiting.
1229 */
1230 call_qop(q, wait_prepare, q);
1231
1232 /*
1233 * All locks have been released, it is safe to sleep now.
1234 */
1235 dprintk(3, "Will sleep waiting for buffers\n");
1236 ret = wait_event_interruptible(q->done_wq,
1237 !list_empty(&q->done_list) || !q->streaming);
1238
1239 /*
1240 * We need to reevaluate both conditions again after reacquiring
1241 * the locks or return an error if one occurred.
1242 */
1243 call_qop(q, wait_finish, q);
1244 if (ret)
1245 return ret;
1246 }
1247 return 0;
1248}
1249
1250/**
1251 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1252 *
1253 * Will sleep if required for nonblocking == false.
1254 */
1255static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1256 int nonblocking)
1257{
1258 unsigned long flags;
1259 int ret;
1260
1261 /*
1262 * Wait for at least one buffer to become available on the done_list.
1263 */
1264 ret = __vb2_wait_for_done_vb(q, nonblocking);
1265 if (ret)
1266 return ret;
1267
1268 /*
1269 * Driver's lock has been held since we last verified that done_list
1270 * is not empty, so no need for another list_empty(done_list) check.
1271 */
1272 spin_lock_irqsave(&q->done_lock, flags);
1273 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1274 list_del(&(*vb)->done_entry);
1275 spin_unlock_irqrestore(&q->done_lock, flags);
1276
1277 return 0;
1278}
1279
1280/**
1281 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1282 * @q: videobuf2 queue
1283 *
1284 * This function will wait until all buffers that have been given to the driver
1285 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1286 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1287 * taken, for example from stop_streaming() callback.
1288 */
1289int vb2_wait_for_all_buffers(struct vb2_queue *q)
1290{
1291 if (!q->streaming) {
1292 dprintk(1, "Streaming off, will not wait for buffers\n");
1293 return -EINVAL;
1294 }
1295
1296 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1297 return 0;
1298}
1299EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1300
1301/**
1302 * vb2_dqbuf() - Dequeue a buffer to the userspace
1303 * @q: videobuf2 queue
1304 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1305 * in driver
1306 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1307 * buffers ready for dequeuing are present. Normally the driver
1308 * would be passing (file->f_flags & O_NONBLOCK) here
1309 *
1310 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1311 * This function:
1312 * 1) verifies the passed buffer,
1313 * 2) calls buf_finish callback in the driver (if provided), in which
1314 * driver can perform any additional operations that may be required before
1315 * returning the buffer to userspace, such as cache sync,
1316 * 3) the buffer struct members are filled with relevant information for
1317 * the userspace.
1318 *
1319 * The return values from this function are intended to be directly returned
1320 * from vidioc_dqbuf handler in driver.
1321 */
1322int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1323{
1324 struct vb2_buffer *vb = NULL;
1325 int ret;
1326
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001327 if (q->fileio) {
1328 dprintk(1, "dqbuf: file io in progress\n");
1329 return -EBUSY;
1330 }
1331
Pawel Osciake23ccc02010-10-11 10:56:41 -03001332 if (b->type != q->type) {
1333 dprintk(1, "dqbuf: invalid buffer type\n");
1334 return -EINVAL;
1335 }
1336
1337 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1338 if (ret < 0) {
1339 dprintk(1, "dqbuf: error getting next done buffer\n");
1340 return ret;
1341 }
1342
1343 ret = call_qop(q, buf_finish, vb);
1344 if (ret) {
1345 dprintk(1, "dqbuf: buffer finish failed\n");
1346 return ret;
1347 }
1348
1349 switch (vb->state) {
1350 case VB2_BUF_STATE_DONE:
1351 dprintk(3, "dqbuf: Returning done buffer\n");
1352 break;
1353 case VB2_BUF_STATE_ERROR:
1354 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1355 break;
1356 default:
1357 dprintk(1, "dqbuf: Invalid buffer state\n");
1358 return -EINVAL;
1359 }
1360
1361 /* Fill buffer information for the userspace */
1362 __fill_v4l2_buffer(vb, b);
1363 /* Remove from videobuf queue */
1364 list_del(&vb->queued_entry);
1365
1366 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1367 vb->v4l2_buf.index, vb->state);
1368
1369 vb->state = VB2_BUF_STATE_DEQUEUED;
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(vb2_dqbuf);
1373
1374/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001375 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1376 *
1377 * Removes all queued buffers from driver's queue and all buffers queued by
1378 * userspace from videobuf's queue. Returns to state after reqbufs.
1379 */
1380static void __vb2_queue_cancel(struct vb2_queue *q)
1381{
1382 unsigned int i;
1383
1384 /*
1385 * Tell driver to stop all transactions and release all queued
1386 * buffers.
1387 */
1388 if (q->streaming)
1389 call_qop(q, stop_streaming, q);
1390 q->streaming = 0;
1391
1392 /*
1393 * Remove all buffers from videobuf's list...
1394 */
1395 INIT_LIST_HEAD(&q->queued_list);
1396 /*
1397 * ...and done list; userspace will not receive any buffers it
1398 * has not already dequeued before initiating cancel.
1399 */
1400 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001401 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001402 wake_up_all(&q->done_wq);
1403
1404 /*
1405 * Reinitialize all buffers for next use.
1406 */
1407 for (i = 0; i < q->num_buffers; ++i)
1408 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1409}
1410
1411/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001412 * vb2_streamon - start streaming
1413 * @q: videobuf2 queue
1414 * @type: type argument passed from userspace to vidioc_streamon handler
1415 *
1416 * Should be called from vidioc_streamon handler of a driver.
1417 * This function:
1418 * 1) verifies current state
1419 * 2) passes any previously queued buffers to the driver and starts streaming
1420 *
1421 * The return values from this function are intended to be directly returned
1422 * from vidioc_streamon handler in the driver.
1423 */
1424int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1425{
1426 struct vb2_buffer *vb;
1427 int ret;
1428
1429 if (q->fileio) {
1430 dprintk(1, "streamon: file io in progress\n");
1431 return -EBUSY;
1432 }
1433
1434 if (type != q->type) {
1435 dprintk(1, "streamon: invalid stream type\n");
1436 return -EINVAL;
1437 }
1438
1439 if (q->streaming) {
1440 dprintk(1, "streamon: already streaming\n");
1441 return -EBUSY;
1442 }
1443
1444 /*
1445 * If any buffers were queued before streamon,
1446 * we can now pass them to driver for processing.
1447 */
1448 list_for_each_entry(vb, &q->queued_list, queued_entry)
1449 __enqueue_in_driver(vb);
1450
1451 /*
1452 * Let driver notice that streaming state has been enabled.
1453 */
1454 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1455 if (ret) {
1456 dprintk(1, "streamon: driver refused to start streaming\n");
1457 __vb2_queue_cancel(q);
1458 return ret;
1459 }
1460
1461 q->streaming = 1;
1462
1463 dprintk(3, "Streamon successful\n");
1464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(vb2_streamon);
1467
1468
1469/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001470 * vb2_streamoff - stop streaming
1471 * @q: videobuf2 queue
1472 * @type: type argument passed from userspace to vidioc_streamoff handler
1473 *
1474 * Should be called from vidioc_streamoff handler of a driver.
1475 * This function:
1476 * 1) verifies current state,
1477 * 2) stop streaming and dequeues any queued buffers, including those previously
1478 * passed to the driver (after waiting for the driver to finish).
1479 *
1480 * This call can be used for pausing playback.
1481 * The return values from this function are intended to be directly returned
1482 * from vidioc_streamoff handler in the driver
1483 */
1484int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1485{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001486 if (q->fileio) {
1487 dprintk(1, "streamoff: file io in progress\n");
1488 return -EBUSY;
1489 }
1490
Pawel Osciake23ccc02010-10-11 10:56:41 -03001491 if (type != q->type) {
1492 dprintk(1, "streamoff: invalid stream type\n");
1493 return -EINVAL;
1494 }
1495
1496 if (!q->streaming) {
1497 dprintk(1, "streamoff: not streaming\n");
1498 return -EINVAL;
1499 }
1500
1501 /*
1502 * Cancel will pause streaming and remove all buffers from the driver
1503 * and videobuf, effectively returning control over them to userspace.
1504 */
1505 __vb2_queue_cancel(q);
1506
1507 dprintk(3, "Streamoff successful\n");
1508 return 0;
1509}
1510EXPORT_SYMBOL_GPL(vb2_streamoff);
1511
1512/**
1513 * __find_plane_by_offset() - find plane associated with the given offset off
1514 */
1515static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1516 unsigned int *_buffer, unsigned int *_plane)
1517{
1518 struct vb2_buffer *vb;
1519 unsigned int buffer, plane;
1520
1521 /*
1522 * Go over all buffers and their planes, comparing the given offset
1523 * with an offset assigned to each plane. If a match is found,
1524 * return its buffer and plane numbers.
1525 */
1526 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1527 vb = q->bufs[buffer];
1528
1529 for (plane = 0; plane < vb->num_planes; ++plane) {
1530 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1531 *_buffer = buffer;
1532 *_plane = plane;
1533 return 0;
1534 }
1535 }
1536 }
1537
1538 return -EINVAL;
1539}
1540
1541/**
1542 * vb2_mmap() - map video buffers into application address space
1543 * @q: videobuf2 queue
1544 * @vma: vma passed to the mmap file operation handler in the driver
1545 *
1546 * Should be called from mmap file operation handler of a driver.
1547 * This function maps one plane of one of the available video buffers to
1548 * userspace. To map whole video memory allocated on reqbufs, this function
1549 * has to be called once per each plane per each buffer previously allocated.
1550 *
1551 * When the userspace application calls mmap, it passes to it an offset returned
1552 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1553 * a "cookie", which is then used to identify the plane to be mapped.
1554 * This function finds a plane with a matching offset and a mapping is performed
1555 * by the means of a provided memory operation.
1556 *
1557 * The return values from this function are intended to be directly returned
1558 * from the mmap handler in driver.
1559 */
1560int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1561{
1562 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001563 struct vb2_buffer *vb;
1564 unsigned int buffer, plane;
1565 int ret;
1566
1567 if (q->memory != V4L2_MEMORY_MMAP) {
1568 dprintk(1, "Queue is not currently set up for mmap\n");
1569 return -EINVAL;
1570 }
1571
1572 /*
1573 * Check memory area access mode.
1574 */
1575 if (!(vma->vm_flags & VM_SHARED)) {
1576 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1577 return -EINVAL;
1578 }
1579 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1580 if (!(vma->vm_flags & VM_WRITE)) {
1581 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1582 return -EINVAL;
1583 }
1584 } else {
1585 if (!(vma->vm_flags & VM_READ)) {
1586 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1587 return -EINVAL;
1588 }
1589 }
1590
1591 /*
1592 * Find the plane corresponding to the offset passed by userspace.
1593 */
1594 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1595 if (ret)
1596 return ret;
1597
1598 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001599
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001600 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001601 if (ret)
1602 return ret;
1603
Pawel Osciake23ccc02010-10-11 10:56:41 -03001604 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(vb2_mmap);
1608
Scott Jiang6f524ec2011-09-21 09:25:23 -03001609#ifndef CONFIG_MMU
1610unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1611 unsigned long addr,
1612 unsigned long len,
1613 unsigned long pgoff,
1614 unsigned long flags)
1615{
1616 unsigned long off = pgoff << PAGE_SHIFT;
1617 struct vb2_buffer *vb;
1618 unsigned int buffer, plane;
1619 int ret;
1620
1621 if (q->memory != V4L2_MEMORY_MMAP) {
1622 dprintk(1, "Queue is not currently set up for mmap\n");
1623 return -EINVAL;
1624 }
1625
1626 /*
1627 * Find the plane corresponding to the offset passed by userspace.
1628 */
1629 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1630 if (ret)
1631 return ret;
1632
1633 vb = q->bufs[buffer];
1634
1635 return (unsigned long)vb2_plane_vaddr(vb, plane);
1636}
1637EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1638#endif
1639
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001640static int __vb2_init_fileio(struct vb2_queue *q, int read);
1641static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001642
1643/**
1644 * vb2_poll() - implements poll userspace operation
1645 * @q: videobuf2 queue
1646 * @file: file argument passed to the poll file operation handler
1647 * @wait: wait argument passed to the poll file operation handler
1648 *
1649 * This function implements poll file operation handler for a driver.
1650 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1651 * be informed that the file descriptor of a video device is available for
1652 * reading.
1653 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1654 * will be reported as available for writing.
1655 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03001656 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1657 * pending events.
1658 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03001659 * The return values from this function are intended to be directly returned
1660 * from poll handler in driver.
1661 */
1662unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1663{
Hans Verkuil95213ce2011-07-13 04:26:52 -03001664 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001665 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001666 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03001667 unsigned int res = 0;
1668 unsigned long flags;
1669
1670 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1671 struct v4l2_fh *fh = file->private_data;
1672
1673 if (v4l2_event_pending(fh))
1674 res = POLLPRI;
1675 else if (req_events & POLLPRI)
1676 poll_wait(file, &fh->wait, wait);
1677 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001678
1679 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001680 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001681 */
1682 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001683 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1684 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001685 if (__vb2_init_fileio(q, 1))
1686 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001687 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001688 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1689 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001690 if (__vb2_init_fileio(q, 0))
1691 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001692 /*
1693 * Write to OUTPUT queue can be done immediately.
1694 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03001695 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001696 }
1697 }
1698
1699 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001700 * There is nothing to wait for if no buffers have already been queued.
1701 */
1702 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03001703 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001704
1705 poll_wait(file, &q->done_wq, wait);
1706
1707 /*
1708 * Take first buffer available for dequeuing.
1709 */
1710 spin_lock_irqsave(&q->done_lock, flags);
1711 if (!list_empty(&q->done_list))
1712 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1713 done_entry);
1714 spin_unlock_irqrestore(&q->done_lock, flags);
1715
1716 if (vb && (vb->state == VB2_BUF_STATE_DONE
1717 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001718 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
1719 res | POLLOUT | POLLWRNORM :
1720 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001721 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03001722 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001723}
1724EXPORT_SYMBOL_GPL(vb2_poll);
1725
1726/**
1727 * vb2_queue_init() - initialize a videobuf2 queue
1728 * @q: videobuf2 queue; this structure should be allocated in driver
1729 *
1730 * The vb2_queue structure should be allocated by the driver. The driver is
1731 * responsible of clearing it's content and setting initial values for some
1732 * required entries before calling this function.
1733 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1734 * to the struct vb2_queue description in include/media/videobuf2-core.h
1735 * for more information.
1736 */
1737int vb2_queue_init(struct vb2_queue *q)
1738{
1739 BUG_ON(!q);
1740 BUG_ON(!q->ops);
1741 BUG_ON(!q->mem_ops);
1742 BUG_ON(!q->type);
1743 BUG_ON(!q->io_modes);
1744
1745 BUG_ON(!q->ops->queue_setup);
1746 BUG_ON(!q->ops->buf_queue);
1747
1748 INIT_LIST_HEAD(&q->queued_list);
1749 INIT_LIST_HEAD(&q->done_list);
1750 spin_lock_init(&q->done_lock);
1751 init_waitqueue_head(&q->done_wq);
1752
1753 if (q->buf_struct_size == 0)
1754 q->buf_struct_size = sizeof(struct vb2_buffer);
1755
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(vb2_queue_init);
1759
1760/**
1761 * vb2_queue_release() - stop streaming, release the queue and free memory
1762 * @q: videobuf2 queue
1763 *
1764 * This function stops streaming and performs necessary clean ups, including
1765 * freeing video buffer memory. The driver is responsible for freeing
1766 * the vb2_queue structure itself.
1767 */
1768void vb2_queue_release(struct vb2_queue *q)
1769{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001770 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001771 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001772 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001773}
1774EXPORT_SYMBOL_GPL(vb2_queue_release);
1775
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001776/**
1777 * struct vb2_fileio_buf - buffer context used by file io emulator
1778 *
1779 * vb2 provides a compatibility layer and emulator of file io (read and
1780 * write) calls on top of streaming API. This structure is used for
1781 * tracking context related to the buffers.
1782 */
1783struct vb2_fileio_buf {
1784 void *vaddr;
1785 unsigned int size;
1786 unsigned int pos;
1787 unsigned int queued:1;
1788};
1789
1790/**
1791 * struct vb2_fileio_data - queue context used by file io emulator
1792 *
1793 * vb2 provides a compatibility layer and emulator of file io (read and
1794 * write) calls on top of streaming API. For proper operation it required
1795 * this structure to save the driver state between each call of the read
1796 * or write function.
1797 */
1798struct vb2_fileio_data {
1799 struct v4l2_requestbuffers req;
1800 struct v4l2_buffer b;
1801 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1802 unsigned int index;
1803 unsigned int q_count;
1804 unsigned int dq_count;
1805 unsigned int flags;
1806};
1807
1808/**
1809 * __vb2_init_fileio() - initialize file io emulator
1810 * @q: videobuf2 queue
1811 * @read: mode selector (1 means read, 0 means write)
1812 */
1813static int __vb2_init_fileio(struct vb2_queue *q, int read)
1814{
1815 struct vb2_fileio_data *fileio;
1816 int i, ret;
1817 unsigned int count = 0;
1818
1819 /*
1820 * Sanity check
1821 */
1822 if ((read && !(q->io_modes & VB2_READ)) ||
1823 (!read && !(q->io_modes & VB2_WRITE)))
1824 BUG();
1825
1826 /*
1827 * Check if device supports mapping buffers to kernel virtual space.
1828 */
1829 if (!q->mem_ops->vaddr)
1830 return -EBUSY;
1831
1832 /*
1833 * Check if streaming api has not been already activated.
1834 */
1835 if (q->streaming || q->num_buffers > 0)
1836 return -EBUSY;
1837
1838 /*
1839 * Start with count 1, driver can increase it in queue_setup()
1840 */
1841 count = 1;
1842
1843 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1844 (read) ? "read" : "write", count, q->io_flags);
1845
1846 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1847 if (fileio == NULL)
1848 return -ENOMEM;
1849
1850 fileio->flags = q->io_flags;
1851
1852 /*
1853 * Request buffers and use MMAP type to force driver
1854 * to allocate buffers by itself.
1855 */
1856 fileio->req.count = count;
1857 fileio->req.memory = V4L2_MEMORY_MMAP;
1858 fileio->req.type = q->type;
1859 ret = vb2_reqbufs(q, &fileio->req);
1860 if (ret)
1861 goto err_kfree;
1862
1863 /*
1864 * Check if plane_count is correct
1865 * (multiplane buffers are not supported).
1866 */
1867 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001868 ret = -EBUSY;
1869 goto err_reqbufs;
1870 }
1871
1872 /*
1873 * Get kernel address of each buffer.
1874 */
1875 for (i = 0; i < q->num_buffers; i++) {
1876 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1877 if (fileio->bufs[i].vaddr == NULL)
1878 goto err_reqbufs;
1879 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1880 }
1881
1882 /*
1883 * Read mode requires pre queuing of all buffers.
1884 */
1885 if (read) {
1886 /*
1887 * Queue all buffers.
1888 */
1889 for (i = 0; i < q->num_buffers; i++) {
1890 struct v4l2_buffer *b = &fileio->b;
1891 memset(b, 0, sizeof(*b));
1892 b->type = q->type;
1893 b->memory = q->memory;
1894 b->index = i;
1895 ret = vb2_qbuf(q, b);
1896 if (ret)
1897 goto err_reqbufs;
1898 fileio->bufs[i].queued = 1;
1899 }
1900
1901 /*
1902 * Start streaming.
1903 */
1904 ret = vb2_streamon(q, q->type);
1905 if (ret)
1906 goto err_reqbufs;
1907 }
1908
1909 q->fileio = fileio;
1910
1911 return ret;
1912
1913err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03001914 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001915 vb2_reqbufs(q, &fileio->req);
1916
1917err_kfree:
1918 kfree(fileio);
1919 return ret;
1920}
1921
1922/**
1923 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1924 * @q: videobuf2 queue
1925 */
1926static int __vb2_cleanup_fileio(struct vb2_queue *q)
1927{
1928 struct vb2_fileio_data *fileio = q->fileio;
1929
1930 if (fileio) {
1931 /*
1932 * Hack fileio context to enable direct calls to vb2 ioctl
1933 * interface.
1934 */
1935 q->fileio = NULL;
1936
1937 vb2_streamoff(q, q->type);
1938 fileio->req.count = 0;
1939 vb2_reqbufs(q, &fileio->req);
1940 kfree(fileio);
1941 dprintk(3, "file io emulator closed\n");
1942 }
1943 return 0;
1944}
1945
1946/**
1947 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1948 * @q: videobuf2 queue
1949 * @data: pointed to target userspace buffer
1950 * @count: number of bytes to read or write
1951 * @ppos: file handle position tracking pointer
1952 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1953 * @read: access mode selector (1 means read, 0 means write)
1954 */
1955static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1956 loff_t *ppos, int nonblock, int read)
1957{
1958 struct vb2_fileio_data *fileio;
1959 struct vb2_fileio_buf *buf;
1960 int ret, index;
1961
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001962 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001963 read ? "read" : "write", (long)*ppos, count,
1964 nonblock ? "non" : "");
1965
1966 if (!data)
1967 return -EINVAL;
1968
1969 /*
1970 * Initialize emulator on first call.
1971 */
1972 if (!q->fileio) {
1973 ret = __vb2_init_fileio(q, read);
1974 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1975 if (ret)
1976 return ret;
1977 }
1978 fileio = q->fileio;
1979
1980 /*
1981 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1982 * The pointer will be restored before returning from this function.
1983 */
1984 q->fileio = NULL;
1985
1986 index = fileio->index;
1987 buf = &fileio->bufs[index];
1988
1989 /*
1990 * Check if we need to dequeue the buffer.
1991 */
1992 if (buf->queued) {
1993 struct vb2_buffer *vb;
1994
1995 /*
1996 * Call vb2_dqbuf to get buffer back.
1997 */
1998 memset(&fileio->b, 0, sizeof(fileio->b));
1999 fileio->b.type = q->type;
2000 fileio->b.memory = q->memory;
2001 fileio->b.index = index;
2002 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2003 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2004 if (ret)
2005 goto end;
2006 fileio->dq_count += 1;
2007
2008 /*
2009 * Get number of bytes filled by the driver
2010 */
2011 vb = q->bufs[index];
2012 buf->size = vb2_get_plane_payload(vb, 0);
2013 buf->queued = 0;
2014 }
2015
2016 /*
2017 * Limit count on last few bytes of the buffer.
2018 */
2019 if (buf->pos + count > buf->size) {
2020 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002021 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002022 }
2023
2024 /*
2025 * Transfer data to userspace.
2026 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002027 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002028 count, index, buf->pos);
2029 if (read)
2030 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2031 else
2032 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2033 if (ret) {
2034 dprintk(3, "file io: error copying data\n");
2035 ret = -EFAULT;
2036 goto end;
2037 }
2038
2039 /*
2040 * Update counters.
2041 */
2042 buf->pos += count;
2043 *ppos += count;
2044
2045 /*
2046 * Queue next buffer if required.
2047 */
2048 if (buf->pos == buf->size ||
2049 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2050 /*
2051 * Check if this is the last buffer to read.
2052 */
2053 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2054 fileio->dq_count == 1) {
2055 dprintk(3, "file io: read limit reached\n");
2056 /*
2057 * Restore fileio pointer and release the context.
2058 */
2059 q->fileio = fileio;
2060 return __vb2_cleanup_fileio(q);
2061 }
2062
2063 /*
2064 * Call vb2_qbuf and give buffer to the driver.
2065 */
2066 memset(&fileio->b, 0, sizeof(fileio->b));
2067 fileio->b.type = q->type;
2068 fileio->b.memory = q->memory;
2069 fileio->b.index = index;
2070 fileio->b.bytesused = buf->pos;
2071 ret = vb2_qbuf(q, &fileio->b);
2072 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2073 if (ret)
2074 goto end;
2075
2076 /*
2077 * Buffer has been queued, update the status
2078 */
2079 buf->pos = 0;
2080 buf->queued = 1;
2081 buf->size = q->bufs[0]->v4l2_planes[0].length;
2082 fileio->q_count += 1;
2083
2084 /*
2085 * Switch to the next buffer
2086 */
2087 fileio->index = (index + 1) % q->num_buffers;
2088
2089 /*
2090 * Start streaming if required.
2091 */
2092 if (!read && !q->streaming) {
2093 ret = vb2_streamon(q, q->type);
2094 if (ret)
2095 goto end;
2096 }
2097 }
2098
2099 /*
2100 * Return proper number of bytes processed.
2101 */
2102 if (ret == 0)
2103 ret = count;
2104end:
2105 /*
2106 * Restore the fileio context and block vb2 ioctl interface.
2107 */
2108 q->fileio = fileio;
2109 return ret;
2110}
2111
2112size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2113 loff_t *ppos, int nonblocking)
2114{
2115 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2116}
2117EXPORT_SYMBOL_GPL(vb2_read);
2118
2119size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2120 loff_t *ppos, int nonblocking)
2121{
2122 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2123}
2124EXPORT_SYMBOL_GPL(vb2_write);
2125
Pawel Osciake23ccc02010-10-11 10:56:41 -03002126MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002127MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002128MODULE_LICENSE("GPL");