blob: ec24718b3ed026574f67e48e4b1e6b1c209cd883 [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/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300457 * vb2_reqbufs() - Initiate streaming
458 * @q: videobuf2 queue
459 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
460 *
461 * Should be called from vidioc_reqbufs ioctl handler of a driver.
462 * This function:
463 * 1) verifies streaming parameters passed from the userspace,
464 * 2) sets up the queue,
465 * 3) negotiates number of buffers and planes per buffer with the driver
466 * to be used during streaming,
467 * 4) allocates internal buffer structures (struct vb2_buffer), according to
468 * the agreed parameters,
469 * 5) for MMAP memory type, allocates actual video memory, using the
470 * memory handling/allocation routines provided during queue initialization
471 *
472 * If req->count is 0, all the memory will be freed instead.
473 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
474 * and the queue is not busy, memory will be reallocated.
475 *
476 * The return values from this function are intended to be directly returned
477 * from vidioc_reqbufs handler in driver.
478 */
479int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
480{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300481 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300482 int ret = 0;
483
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300484 if (q->fileio) {
485 dprintk(1, "reqbufs: file io in progress\n");
486 return -EBUSY;
487 }
488
Pawel Osciake23ccc02010-10-11 10:56:41 -0300489 if (req->memory != V4L2_MEMORY_MMAP
490 && req->memory != V4L2_MEMORY_USERPTR) {
491 dprintk(1, "reqbufs: unsupported memory type\n");
492 return -EINVAL;
493 }
494
495 if (req->type != q->type) {
496 dprintk(1, "reqbufs: requested type is incorrect\n");
497 return -EINVAL;
498 }
499
500 if (q->streaming) {
501 dprintk(1, "reqbufs: streaming active\n");
502 return -EBUSY;
503 }
504
505 /*
506 * Make sure all the required memory ops for given memory type
507 * are available.
508 */
509 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
510 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
511 return -EINVAL;
512 }
513
514 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
515 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
516 return -EINVAL;
517 }
518
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300519 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300520 /*
521 * We already have buffers allocated, so first check if they
522 * are not in use and can be freed.
523 */
524 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
525 dprintk(1, "reqbufs: memory in use, cannot free\n");
526 return -EBUSY;
527 }
528
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300529 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300530
531 /*
532 * In case of REQBUFS(0) return immediately without calling
533 * driver's queue_setup() callback and allocating resources.
534 */
535 if (req->count == 0)
536 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300537 }
538
539 /*
540 * Make sure the requested values and current defaults are sane.
541 */
542 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300543 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300544 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300545 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300546
547 /*
548 * Ask the driver how many buffers and planes per buffer it requires.
549 * Driver also sets the size and allocator context for each plane.
550 */
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300551 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300552 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300553 if (ret)
554 return ret;
555
556 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300557 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300558 if (ret == 0) {
559 dprintk(1, "Memory allocation failed\n");
560 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300561 }
562
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300563 allocated_buffers = ret;
564
Pawel Osciake23ccc02010-10-11 10:56:41 -0300565 /*
566 * Check if driver can handle the allocated number of buffers.
567 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300568 if (allocated_buffers < num_buffers) {
569 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300570
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300571 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
572 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300573
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300574 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300575 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300576
577 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300578 * Either the driver has accepted a smaller number of buffers,
579 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300580 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300581 }
582
583 q->num_buffers = allocated_buffers;
584
585 if (ret < 0) {
586 __vb2_queue_free(q, allocated_buffers);
587 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300588 }
589
Pawel Osciake23ccc02010-10-11 10:56:41 -0300590 /*
591 * Return the number of successfully allocated buffers
592 * to the userspace.
593 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300594 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300595
596 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300597}
598EXPORT_SYMBOL_GPL(vb2_reqbufs);
599
600/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300601 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
602 * @q: videobuf2 queue
603 * @create: creation parameters, passed from userspace to vidioc_create_bufs
604 * handler in driver
605 *
606 * Should be called from vidioc_create_bufs ioctl handler of a driver.
607 * This function:
608 * 1) verifies parameter sanity
609 * 2) calls the .queue_setup() queue operation
610 * 3) performs any necessary memory allocations
611 *
612 * The return values from this function are intended to be directly returned
613 * from vidioc_create_bufs handler in driver.
614 */
615int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
616{
617 unsigned int num_planes = 0, num_buffers, allocated_buffers;
618 int ret = 0;
619
620 if (q->fileio) {
621 dprintk(1, "%s(): file io in progress\n", __func__);
622 return -EBUSY;
623 }
624
625 if (create->memory != V4L2_MEMORY_MMAP
626 && create->memory != V4L2_MEMORY_USERPTR) {
627 dprintk(1, "%s(): unsupported memory type\n", __func__);
628 return -EINVAL;
629 }
630
631 if (create->format.type != q->type) {
632 dprintk(1, "%s(): requested type is incorrect\n", __func__);
633 return -EINVAL;
634 }
635
636 /*
637 * Make sure all the required memory ops for given memory type
638 * are available.
639 */
640 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
641 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
642 return -EINVAL;
643 }
644
645 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
646 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
647 return -EINVAL;
648 }
649
650 if (q->num_buffers == VIDEO_MAX_FRAME) {
651 dprintk(1, "%s(): maximum number of buffers already allocated\n",
652 __func__);
653 return -ENOBUFS;
654 }
655
656 create->index = q->num_buffers;
657
658 if (!q->num_buffers) {
659 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
660 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
661 q->memory = create->memory;
662 }
663
664 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
665
666 /*
667 * Ask the driver, whether the requested number of buffers, planes per
668 * buffer and their sizes are acceptable
669 */
670 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
671 &num_planes, q->plane_sizes, q->alloc_ctx);
672 if (ret)
673 return ret;
674
675 /* Finally, allocate buffers and video memory */
676 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
677 num_planes);
678 if (ret < 0) {
679 dprintk(1, "Memory allocation failed with error: %d\n", ret);
680 return ret;
681 }
682
683 allocated_buffers = ret;
684
685 /*
686 * Check if driver can handle the so far allocated number of buffers.
687 */
688 if (ret < num_buffers) {
689 num_buffers = ret;
690
691 /*
692 * q->num_buffers contains the total number of buffers, that the
693 * queue driver has set up
694 */
695 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
696 &num_planes, q->plane_sizes, q->alloc_ctx);
697
698 if (!ret && allocated_buffers < num_buffers)
699 ret = -ENOMEM;
700
701 /*
702 * Either the driver has accepted a smaller number of buffers,
703 * or .queue_setup() returned an error
704 */
705 }
706
707 q->num_buffers += allocated_buffers;
708
709 if (ret < 0) {
710 __vb2_queue_free(q, allocated_buffers);
711 return ret;
712 }
713
714 /*
715 * Return the number of successfully allocated buffers
716 * to the userspace.
717 */
718 create->count = allocated_buffers;
719
720 return 0;
721}
722EXPORT_SYMBOL_GPL(vb2_create_bufs);
723
724/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300725 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
726 * @vb: vb2_buffer to which the plane in question belongs to
727 * @plane_no: plane number for which the address is to be returned
728 *
729 * This function returns a kernel virtual address of a given plane if
730 * such a mapping exist, NULL otherwise.
731 */
732void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
733{
734 struct vb2_queue *q = vb->vb2_queue;
735
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300736 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300737 return NULL;
738
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300739 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300740
741}
742EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
743
744/**
745 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
746 * @vb: vb2_buffer to which the plane in question belongs to
747 * @plane_no: plane number for which the cookie is to be returned
748 *
749 * This function returns an allocator specific cookie for a given plane if
750 * available, NULL otherwise. The allocator should provide some simple static
751 * inline function, which would convert this cookie to the allocator specific
752 * type that can be used directly by the driver to access the buffer. This can
753 * be for example physical address, pointer to scatter list or IOMMU mapping.
754 */
755void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
756{
757 struct vb2_queue *q = vb->vb2_queue;
758
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300759 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300760 return NULL;
761
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300762 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300763}
764EXPORT_SYMBOL_GPL(vb2_plane_cookie);
765
766/**
767 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
768 * @vb: vb2_buffer returned from the driver
769 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
770 * or VB2_BUF_STATE_ERROR if the operation finished with an error
771 *
772 * This function should be called by the driver after a hardware operation on
773 * a buffer is finished and the buffer may be returned to userspace. The driver
774 * cannot use this buffer anymore until it is queued back to it by videobuf
775 * by the means of buf_queue callback. Only buffers previously queued to the
776 * driver by buf_queue can be passed to this function.
777 */
778void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
779{
780 struct vb2_queue *q = vb->vb2_queue;
781 unsigned long flags;
782
783 if (vb->state != VB2_BUF_STATE_ACTIVE)
784 return;
785
786 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
787 return;
788
789 dprintk(4, "Done processing on buffer %d, state: %d\n",
790 vb->v4l2_buf.index, vb->state);
791
792 /* Add the buffer to the done buffers list */
793 spin_lock_irqsave(&q->done_lock, flags);
794 vb->state = state;
795 list_add_tail(&vb->done_entry, &q->done_list);
796 atomic_dec(&q->queued_count);
797 spin_unlock_irqrestore(&q->done_lock, flags);
798
799 /* Inform any processes that may be waiting for buffers */
800 wake_up(&q->done_wq);
801}
802EXPORT_SYMBOL_GPL(vb2_buffer_done);
803
804/**
805 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
806 * a v4l2_buffer by the userspace
807 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300808static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300809 struct v4l2_plane *v4l2_planes)
810{
811 unsigned int plane;
812 int ret;
813
814 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
815 /*
816 * Verify that the userspace gave us a valid array for
817 * plane information.
818 */
819 ret = __verify_planes_array(vb, b);
820 if (ret)
821 return ret;
822
823 /* Fill in driver-provided information for OUTPUT types */
824 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
825 /*
826 * Will have to go up to b->length when API starts
827 * accepting variable number of planes.
828 */
829 for (plane = 0; plane < vb->num_planes; ++plane) {
830 v4l2_planes[plane].bytesused =
831 b->m.planes[plane].bytesused;
832 v4l2_planes[plane].data_offset =
833 b->m.planes[plane].data_offset;
834 }
835 }
836
837 if (b->memory == V4L2_MEMORY_USERPTR) {
838 for (plane = 0; plane < vb->num_planes; ++plane) {
839 v4l2_planes[plane].m.userptr =
840 b->m.planes[plane].m.userptr;
841 v4l2_planes[plane].length =
842 b->m.planes[plane].length;
843 }
844 }
845 } else {
846 /*
847 * Single-planar buffers do not use planes array,
848 * so fill in relevant v4l2_buffer struct fields instead.
849 * In videobuf we use our internal V4l2_planes struct for
850 * single-planar buffers as well, for simplicity.
851 */
852 if (V4L2_TYPE_IS_OUTPUT(b->type))
853 v4l2_planes[0].bytesused = b->bytesused;
854
855 if (b->memory == V4L2_MEMORY_USERPTR) {
856 v4l2_planes[0].m.userptr = b->m.userptr;
857 v4l2_planes[0].length = b->length;
858 }
859 }
860
861 vb->v4l2_buf.field = b->field;
862 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300863 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300864
865 return 0;
866}
867
868/**
869 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
870 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300871static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300872{
873 struct v4l2_plane planes[VIDEO_MAX_PLANES];
874 struct vb2_queue *q = vb->vb2_queue;
875 void *mem_priv;
876 unsigned int plane;
877 int ret;
878 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
879
880 /* Verify and copy relevant information provided by the userspace */
881 ret = __fill_vb2_buffer(vb, b, planes);
882 if (ret)
883 return ret;
884
885 for (plane = 0; plane < vb->num_planes; ++plane) {
886 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300887 if (vb->v4l2_planes[plane].m.userptr &&
888 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300889 && vb->v4l2_planes[plane].length == planes[plane].length)
890 continue;
891
892 dprintk(3, "qbuf: userspace address for plane %d changed, "
893 "reacquiring memory\n", plane);
894
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300895 /* Check if the provided plane buffer is large enough */
896 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300897 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300898 goto err;
899 }
900
Pawel Osciake23ccc02010-10-11 10:56:41 -0300901 /* Release previously acquired memory if present */
902 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300903 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300904
905 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300906 vb->v4l2_planes[plane].m.userptr = 0;
907 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300908
909 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300910 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
911 planes[plane].m.userptr,
912 planes[plane].length, write);
913 if (IS_ERR_OR_NULL(mem_priv)) {
914 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300915 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300916 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
917 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300918 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300919 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300920 }
921
922 /*
923 * Call driver-specific initialization on the newly acquired buffer,
924 * if provided.
925 */
926 ret = call_qop(q, buf_init, vb);
927 if (ret) {
928 dprintk(1, "qbuf: buffer initialization failed\n");
929 goto err;
930 }
931
932 /*
933 * Now that everything is in order, copy relevant information
934 * provided by userspace.
935 */
936 for (plane = 0; plane < vb->num_planes; ++plane)
937 vb->v4l2_planes[plane] = planes[plane];
938
939 return 0;
940err:
941 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300942 for (plane = 0; plane < vb->num_planes; ++plane) {
943 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300944 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300945 vb->planes[plane].mem_priv = NULL;
946 vb->v4l2_planes[plane].m.userptr = 0;
947 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300948 }
949
950 return ret;
951}
952
953/**
954 * __qbuf_mmap() - handle qbuf of an MMAP buffer
955 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300956static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300957{
958 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
959}
960
961/**
962 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
963 */
964static void __enqueue_in_driver(struct vb2_buffer *vb)
965{
966 struct vb2_queue *q = vb->vb2_queue;
967
968 vb->state = VB2_BUF_STATE_ACTIVE;
969 atomic_inc(&q->queued_count);
970 q->ops->buf_queue(vb);
971}
972
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300973static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300974{
975 struct vb2_queue *q = vb->vb2_queue;
976 int ret;
977
978 switch (q->memory) {
979 case V4L2_MEMORY_MMAP:
980 ret = __qbuf_mmap(vb, b);
981 break;
982 case V4L2_MEMORY_USERPTR:
983 ret = __qbuf_userptr(vb, b);
984 break;
985 default:
986 WARN(1, "Invalid queue type\n");
987 ret = -EINVAL;
988 }
989
990 if (!ret)
991 ret = call_qop(q, buf_prepare, vb);
992 if (ret)
993 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
994 else
995 vb->state = VB2_BUF_STATE_PREPARED;
996
997 return ret;
998}
999
Pawel Osciake23ccc02010-10-11 10:56:41 -03001000/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001001 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1002 * @q: videobuf2 queue
1003 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1004 * handler in driver
1005 *
1006 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1007 * This function:
1008 * 1) verifies the passed buffer,
1009 * 2) calls buf_prepare callback in the driver (if provided), in which
1010 * driver-specific buffer initialization can be performed,
1011 *
1012 * The return values from this function are intended to be directly returned
1013 * from vidioc_prepare_buf handler in driver.
1014 */
1015int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1016{
1017 struct vb2_buffer *vb;
1018 int ret;
1019
1020 if (q->fileio) {
1021 dprintk(1, "%s(): file io in progress\n", __func__);
1022 return -EBUSY;
1023 }
1024
1025 if (b->type != q->type) {
1026 dprintk(1, "%s(): invalid buffer type\n", __func__);
1027 return -EINVAL;
1028 }
1029
1030 if (b->index >= q->num_buffers) {
1031 dprintk(1, "%s(): buffer index out of range\n", __func__);
1032 return -EINVAL;
1033 }
1034
1035 vb = q->bufs[b->index];
1036 if (NULL == vb) {
1037 /* Should never happen */
1038 dprintk(1, "%s(): buffer is NULL\n", __func__);
1039 return -EINVAL;
1040 }
1041
1042 if (b->memory != q->memory) {
1043 dprintk(1, "%s(): invalid memory type\n", __func__);
1044 return -EINVAL;
1045 }
1046
1047 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1048 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1049 return -EINVAL;
1050 }
1051
1052 ret = __buf_prepare(vb, b);
1053 if (ret < 0)
1054 return ret;
1055
1056 __fill_v4l2_buffer(vb, b);
1057
1058 return 0;
1059}
1060EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1061
1062/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001063 * vb2_qbuf() - Queue a buffer from userspace
1064 * @q: videobuf2 queue
1065 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1066 * in driver
1067 *
1068 * Should be called from vidioc_qbuf ioctl handler of a driver.
1069 * This function:
1070 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001071 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1072 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001073 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1074 * callback for processing.
1075 *
1076 * The return values from this function are intended to be directly returned
1077 * from vidioc_qbuf handler in driver.
1078 */
1079int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1080{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001081 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001082 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001083 int ret = 0;
1084
1085 /*
1086 * In case of user pointer buffers vb2 allocator needs to get direct
1087 * access to userspace pages. This requires getting read access on
1088 * mmap semaphore in the current process structure. The same
1089 * semaphore is taken before calling mmap operation, while both mmap
1090 * and qbuf are called by the driver or v4l2 core with driver's lock
1091 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1092 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1093 * release driver's lock, takes mmap_sem and then takes again driver's
1094 * lock.
1095 *
1096 * To avoid race with other vb2 calls, which might be called after
1097 * releasing driver's lock, this operation is performed at the
1098 * beggining of qbuf processing. This way the queue status is
1099 * consistent after getting driver's lock back.
1100 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001101 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001102 mmap_sem = &current->mm->mmap_sem;
1103 call_qop(q, wait_prepare, q);
1104 down_read(mmap_sem);
1105 call_qop(q, wait_finish, q);
1106 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001107
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001108 if (q->fileio) {
1109 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001110 ret = -EBUSY;
1111 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001112 }
1113
Pawel Osciake23ccc02010-10-11 10:56:41 -03001114 if (b->type != q->type) {
1115 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001116 ret = -EINVAL;
1117 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001118 }
1119
1120 if (b->index >= q->num_buffers) {
1121 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001122 ret = -EINVAL;
1123 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001124 }
1125
1126 vb = q->bufs[b->index];
1127 if (NULL == vb) {
1128 /* Should never happen */
1129 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001130 ret = -EINVAL;
1131 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001132 }
1133
1134 if (b->memory != q->memory) {
1135 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001136 ret = -EINVAL;
1137 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001138 }
1139
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001140 switch (vb->state) {
1141 case VB2_BUF_STATE_DEQUEUED:
1142 ret = __buf_prepare(vb, b);
1143 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001144 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001145 case VB2_BUF_STATE_PREPARED:
1146 break;
1147 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001148 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001149 ret = -EINVAL;
1150 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001151 }
1152
Pawel Osciake23ccc02010-10-11 10:56:41 -03001153 /*
1154 * Add to the queued buffers list, a buffer will stay on it until
1155 * dequeued in dqbuf.
1156 */
1157 list_add_tail(&vb->queued_entry, &q->queued_list);
1158 vb->state = VB2_BUF_STATE_QUEUED;
1159
1160 /*
1161 * If already streaming, give the buffer to driver for processing.
1162 * If not, the buffer will be given to driver on next streamon.
1163 */
1164 if (q->streaming)
1165 __enqueue_in_driver(vb);
1166
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001167 /* Fill buffer information for the userspace */
1168 __fill_v4l2_buffer(vb, b);
1169
Pawel Osciake23ccc02010-10-11 10:56:41 -03001170 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001171unlock:
1172 if (mmap_sem)
1173 up_read(mmap_sem);
1174 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001175}
1176EXPORT_SYMBOL_GPL(vb2_qbuf);
1177
1178/**
1179 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1180 * for dequeuing
1181 *
1182 * Will sleep if required for nonblocking == false.
1183 */
1184static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1185{
1186 /*
1187 * All operations on vb_done_list are performed under done_lock
1188 * spinlock protection. However, buffers may be removed from
1189 * it and returned to userspace only while holding both driver's
1190 * lock and the done_lock spinlock. Thus we can be sure that as
1191 * long as we hold the driver's lock, the list will remain not
1192 * empty if list_empty() check succeeds.
1193 */
1194
1195 for (;;) {
1196 int ret;
1197
1198 if (!q->streaming) {
1199 dprintk(1, "Streaming off, will not wait for buffers\n");
1200 return -EINVAL;
1201 }
1202
1203 if (!list_empty(&q->done_list)) {
1204 /*
1205 * Found a buffer that we were waiting for.
1206 */
1207 break;
1208 }
1209
1210 if (nonblocking) {
1211 dprintk(1, "Nonblocking and no buffers to dequeue, "
1212 "will not wait\n");
1213 return -EAGAIN;
1214 }
1215
1216 /*
1217 * We are streaming and blocking, wait for another buffer to
1218 * become ready or for streamoff. Driver's lock is released to
1219 * allow streamoff or qbuf to be called while waiting.
1220 */
1221 call_qop(q, wait_prepare, q);
1222
1223 /*
1224 * All locks have been released, it is safe to sleep now.
1225 */
1226 dprintk(3, "Will sleep waiting for buffers\n");
1227 ret = wait_event_interruptible(q->done_wq,
1228 !list_empty(&q->done_list) || !q->streaming);
1229
1230 /*
1231 * We need to reevaluate both conditions again after reacquiring
1232 * the locks or return an error if one occurred.
1233 */
1234 call_qop(q, wait_finish, q);
1235 if (ret)
1236 return ret;
1237 }
1238 return 0;
1239}
1240
1241/**
1242 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1243 *
1244 * Will sleep if required for nonblocking == false.
1245 */
1246static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1247 int nonblocking)
1248{
1249 unsigned long flags;
1250 int ret;
1251
1252 /*
1253 * Wait for at least one buffer to become available on the done_list.
1254 */
1255 ret = __vb2_wait_for_done_vb(q, nonblocking);
1256 if (ret)
1257 return ret;
1258
1259 /*
1260 * Driver's lock has been held since we last verified that done_list
1261 * is not empty, so no need for another list_empty(done_list) check.
1262 */
1263 spin_lock_irqsave(&q->done_lock, flags);
1264 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1265 list_del(&(*vb)->done_entry);
1266 spin_unlock_irqrestore(&q->done_lock, flags);
1267
1268 return 0;
1269}
1270
1271/**
1272 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1273 * @q: videobuf2 queue
1274 *
1275 * This function will wait until all buffers that have been given to the driver
1276 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1277 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1278 * taken, for example from stop_streaming() callback.
1279 */
1280int vb2_wait_for_all_buffers(struct vb2_queue *q)
1281{
1282 if (!q->streaming) {
1283 dprintk(1, "Streaming off, will not wait for buffers\n");
1284 return -EINVAL;
1285 }
1286
1287 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1288 return 0;
1289}
1290EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1291
1292/**
1293 * vb2_dqbuf() - Dequeue a buffer to the userspace
1294 * @q: videobuf2 queue
1295 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1296 * in driver
1297 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1298 * buffers ready for dequeuing are present. Normally the driver
1299 * would be passing (file->f_flags & O_NONBLOCK) here
1300 *
1301 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1302 * This function:
1303 * 1) verifies the passed buffer,
1304 * 2) calls buf_finish callback in the driver (if provided), in which
1305 * driver can perform any additional operations that may be required before
1306 * returning the buffer to userspace, such as cache sync,
1307 * 3) the buffer struct members are filled with relevant information for
1308 * the userspace.
1309 *
1310 * The return values from this function are intended to be directly returned
1311 * from vidioc_dqbuf handler in driver.
1312 */
1313int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1314{
1315 struct vb2_buffer *vb = NULL;
1316 int ret;
1317
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001318 if (q->fileio) {
1319 dprintk(1, "dqbuf: file io in progress\n");
1320 return -EBUSY;
1321 }
1322
Pawel Osciake23ccc02010-10-11 10:56:41 -03001323 if (b->type != q->type) {
1324 dprintk(1, "dqbuf: invalid buffer type\n");
1325 return -EINVAL;
1326 }
1327
1328 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1329 if (ret < 0) {
1330 dprintk(1, "dqbuf: error getting next done buffer\n");
1331 return ret;
1332 }
1333
1334 ret = call_qop(q, buf_finish, vb);
1335 if (ret) {
1336 dprintk(1, "dqbuf: buffer finish failed\n");
1337 return ret;
1338 }
1339
1340 switch (vb->state) {
1341 case VB2_BUF_STATE_DONE:
1342 dprintk(3, "dqbuf: Returning done buffer\n");
1343 break;
1344 case VB2_BUF_STATE_ERROR:
1345 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1346 break;
1347 default:
1348 dprintk(1, "dqbuf: Invalid buffer state\n");
1349 return -EINVAL;
1350 }
1351
1352 /* Fill buffer information for the userspace */
1353 __fill_v4l2_buffer(vb, b);
1354 /* Remove from videobuf queue */
1355 list_del(&vb->queued_entry);
1356
1357 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1358 vb->v4l2_buf.index, vb->state);
1359
1360 vb->state = VB2_BUF_STATE_DEQUEUED;
1361 return 0;
1362}
1363EXPORT_SYMBOL_GPL(vb2_dqbuf);
1364
1365/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001366 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1367 *
1368 * Removes all queued buffers from driver's queue and all buffers queued by
1369 * userspace from videobuf's queue. Returns to state after reqbufs.
1370 */
1371static void __vb2_queue_cancel(struct vb2_queue *q)
1372{
1373 unsigned int i;
1374
1375 /*
1376 * Tell driver to stop all transactions and release all queued
1377 * buffers.
1378 */
1379 if (q->streaming)
1380 call_qop(q, stop_streaming, q);
1381 q->streaming = 0;
1382
1383 /*
1384 * Remove all buffers from videobuf's list...
1385 */
1386 INIT_LIST_HEAD(&q->queued_list);
1387 /*
1388 * ...and done list; userspace will not receive any buffers it
1389 * has not already dequeued before initiating cancel.
1390 */
1391 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001392 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001393 wake_up_all(&q->done_wq);
1394
1395 /*
1396 * Reinitialize all buffers for next use.
1397 */
1398 for (i = 0; i < q->num_buffers; ++i)
1399 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1400}
1401
1402/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001403 * vb2_streamon - start streaming
1404 * @q: videobuf2 queue
1405 * @type: type argument passed from userspace to vidioc_streamon handler
1406 *
1407 * Should be called from vidioc_streamon handler of a driver.
1408 * This function:
1409 * 1) verifies current state
1410 * 2) passes any previously queued buffers to the driver and starts streaming
1411 *
1412 * The return values from this function are intended to be directly returned
1413 * from vidioc_streamon handler in the driver.
1414 */
1415int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1416{
1417 struct vb2_buffer *vb;
1418 int ret;
1419
1420 if (q->fileio) {
1421 dprintk(1, "streamon: file io in progress\n");
1422 return -EBUSY;
1423 }
1424
1425 if (type != q->type) {
1426 dprintk(1, "streamon: invalid stream type\n");
1427 return -EINVAL;
1428 }
1429
1430 if (q->streaming) {
1431 dprintk(1, "streamon: already streaming\n");
1432 return -EBUSY;
1433 }
1434
1435 /*
1436 * If any buffers were queued before streamon,
1437 * we can now pass them to driver for processing.
1438 */
1439 list_for_each_entry(vb, &q->queued_list, queued_entry)
1440 __enqueue_in_driver(vb);
1441
1442 /*
1443 * Let driver notice that streaming state has been enabled.
1444 */
1445 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1446 if (ret) {
1447 dprintk(1, "streamon: driver refused to start streaming\n");
1448 __vb2_queue_cancel(q);
1449 return ret;
1450 }
1451
1452 q->streaming = 1;
1453
1454 dprintk(3, "Streamon successful\n");
1455 return 0;
1456}
1457EXPORT_SYMBOL_GPL(vb2_streamon);
1458
1459
1460/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001461 * vb2_streamoff - stop streaming
1462 * @q: videobuf2 queue
1463 * @type: type argument passed from userspace to vidioc_streamoff handler
1464 *
1465 * Should be called from vidioc_streamoff handler of a driver.
1466 * This function:
1467 * 1) verifies current state,
1468 * 2) stop streaming and dequeues any queued buffers, including those previously
1469 * passed to the driver (after waiting for the driver to finish).
1470 *
1471 * This call can be used for pausing playback.
1472 * The return values from this function are intended to be directly returned
1473 * from vidioc_streamoff handler in the driver
1474 */
1475int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1476{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001477 if (q->fileio) {
1478 dprintk(1, "streamoff: file io in progress\n");
1479 return -EBUSY;
1480 }
1481
Pawel Osciake23ccc02010-10-11 10:56:41 -03001482 if (type != q->type) {
1483 dprintk(1, "streamoff: invalid stream type\n");
1484 return -EINVAL;
1485 }
1486
1487 if (!q->streaming) {
1488 dprintk(1, "streamoff: not streaming\n");
1489 return -EINVAL;
1490 }
1491
1492 /*
1493 * Cancel will pause streaming and remove all buffers from the driver
1494 * and videobuf, effectively returning control over them to userspace.
1495 */
1496 __vb2_queue_cancel(q);
1497
1498 dprintk(3, "Streamoff successful\n");
1499 return 0;
1500}
1501EXPORT_SYMBOL_GPL(vb2_streamoff);
1502
1503/**
1504 * __find_plane_by_offset() - find plane associated with the given offset off
1505 */
1506static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1507 unsigned int *_buffer, unsigned int *_plane)
1508{
1509 struct vb2_buffer *vb;
1510 unsigned int buffer, plane;
1511
1512 /*
1513 * Go over all buffers and their planes, comparing the given offset
1514 * with an offset assigned to each plane. If a match is found,
1515 * return its buffer and plane numbers.
1516 */
1517 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1518 vb = q->bufs[buffer];
1519
1520 for (plane = 0; plane < vb->num_planes; ++plane) {
1521 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1522 *_buffer = buffer;
1523 *_plane = plane;
1524 return 0;
1525 }
1526 }
1527 }
1528
1529 return -EINVAL;
1530}
1531
1532/**
1533 * vb2_mmap() - map video buffers into application address space
1534 * @q: videobuf2 queue
1535 * @vma: vma passed to the mmap file operation handler in the driver
1536 *
1537 * Should be called from mmap file operation handler of a driver.
1538 * This function maps one plane of one of the available video buffers to
1539 * userspace. To map whole video memory allocated on reqbufs, this function
1540 * has to be called once per each plane per each buffer previously allocated.
1541 *
1542 * When the userspace application calls mmap, it passes to it an offset returned
1543 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1544 * a "cookie", which is then used to identify the plane to be mapped.
1545 * This function finds a plane with a matching offset and a mapping is performed
1546 * by the means of a provided memory operation.
1547 *
1548 * The return values from this function are intended to be directly returned
1549 * from the mmap handler in driver.
1550 */
1551int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1552{
1553 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001554 struct vb2_buffer *vb;
1555 unsigned int buffer, plane;
1556 int ret;
1557
1558 if (q->memory != V4L2_MEMORY_MMAP) {
1559 dprintk(1, "Queue is not currently set up for mmap\n");
1560 return -EINVAL;
1561 }
1562
1563 /*
1564 * Check memory area access mode.
1565 */
1566 if (!(vma->vm_flags & VM_SHARED)) {
1567 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1568 return -EINVAL;
1569 }
1570 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1571 if (!(vma->vm_flags & VM_WRITE)) {
1572 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1573 return -EINVAL;
1574 }
1575 } else {
1576 if (!(vma->vm_flags & VM_READ)) {
1577 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1578 return -EINVAL;
1579 }
1580 }
1581
1582 /*
1583 * Find the plane corresponding to the offset passed by userspace.
1584 */
1585 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1586 if (ret)
1587 return ret;
1588
1589 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001590
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001591 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001592 if (ret)
1593 return ret;
1594
Pawel Osciake23ccc02010-10-11 10:56:41 -03001595 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1596 return 0;
1597}
1598EXPORT_SYMBOL_GPL(vb2_mmap);
1599
Scott Jiang6f524ec2011-09-21 09:25:23 -03001600#ifndef CONFIG_MMU
1601unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1602 unsigned long addr,
1603 unsigned long len,
1604 unsigned long pgoff,
1605 unsigned long flags)
1606{
1607 unsigned long off = pgoff << PAGE_SHIFT;
1608 struct vb2_buffer *vb;
1609 unsigned int buffer, plane;
1610 int ret;
1611
1612 if (q->memory != V4L2_MEMORY_MMAP) {
1613 dprintk(1, "Queue is not currently set up for mmap\n");
1614 return -EINVAL;
1615 }
1616
1617 /*
1618 * Find the plane corresponding to the offset passed by userspace.
1619 */
1620 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1621 if (ret)
1622 return ret;
1623
1624 vb = q->bufs[buffer];
1625
1626 return (unsigned long)vb2_plane_vaddr(vb, plane);
1627}
1628EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1629#endif
1630
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001631static int __vb2_init_fileio(struct vb2_queue *q, int read);
1632static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001633
1634/**
1635 * vb2_poll() - implements poll userspace operation
1636 * @q: videobuf2 queue
1637 * @file: file argument passed to the poll file operation handler
1638 * @wait: wait argument passed to the poll file operation handler
1639 *
1640 * This function implements poll file operation handler for a driver.
1641 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1642 * be informed that the file descriptor of a video device is available for
1643 * reading.
1644 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1645 * will be reported as available for writing.
1646 *
Hans Verkuil95213ce2011-07-13 04:26:52 -03001647 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
1648 * pending events.
1649 *
Pawel Osciake23ccc02010-10-11 10:56:41 -03001650 * The return values from this function are intended to be directly returned
1651 * from poll handler in driver.
1652 */
1653unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1654{
Hans Verkuil95213ce2011-07-13 04:26:52 -03001655 struct video_device *vfd = video_devdata(file);
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001656 unsigned long req_events = poll_requested_events(wait);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001657 struct vb2_buffer *vb = NULL;
Hans Verkuil95213ce2011-07-13 04:26:52 -03001658 unsigned int res = 0;
1659 unsigned long flags;
1660
1661 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1662 struct v4l2_fh *fh = file->private_data;
1663
1664 if (v4l2_event_pending(fh))
1665 res = POLLPRI;
1666 else if (req_events & POLLPRI)
1667 poll_wait(file, &fh->wait, wait);
1668 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001669
1670 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001671 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001672 */
1673 if (q->num_buffers == 0 && q->fileio == NULL) {
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001674 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
1675 (req_events & (POLLIN | POLLRDNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001676 if (__vb2_init_fileio(q, 1))
1677 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001678 }
Hans Verkuilbf5c7cb2011-07-13 04:01:30 -03001679 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
1680 (req_events & (POLLOUT | POLLWRNORM))) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001681 if (__vb2_init_fileio(q, 0))
1682 return res | POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001683 /*
1684 * Write to OUTPUT queue can be done immediately.
1685 */
Hans Verkuil95213ce2011-07-13 04:26:52 -03001686 return res | POLLOUT | POLLWRNORM;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001687 }
1688 }
1689
1690 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001691 * There is nothing to wait for if no buffers have already been queued.
1692 */
1693 if (list_empty(&q->queued_list))
Hans Verkuil95213ce2011-07-13 04:26:52 -03001694 return res | POLLERR;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001695
1696 poll_wait(file, &q->done_wq, wait);
1697
1698 /*
1699 * Take first buffer available for dequeuing.
1700 */
1701 spin_lock_irqsave(&q->done_lock, flags);
1702 if (!list_empty(&q->done_list))
1703 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1704 done_entry);
1705 spin_unlock_irqrestore(&q->done_lock, flags);
1706
1707 if (vb && (vb->state == VB2_BUF_STATE_DONE
1708 || vb->state == VB2_BUF_STATE_ERROR)) {
Hans Verkuil95213ce2011-07-13 04:26:52 -03001709 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
1710 res | POLLOUT | POLLWRNORM :
1711 res | POLLIN | POLLRDNORM;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001712 }
Hans Verkuil95213ce2011-07-13 04:26:52 -03001713 return res;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001714}
1715EXPORT_SYMBOL_GPL(vb2_poll);
1716
1717/**
1718 * vb2_queue_init() - initialize a videobuf2 queue
1719 * @q: videobuf2 queue; this structure should be allocated in driver
1720 *
1721 * The vb2_queue structure should be allocated by the driver. The driver is
1722 * responsible of clearing it's content and setting initial values for some
1723 * required entries before calling this function.
1724 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1725 * to the struct vb2_queue description in include/media/videobuf2-core.h
1726 * for more information.
1727 */
1728int vb2_queue_init(struct vb2_queue *q)
1729{
1730 BUG_ON(!q);
1731 BUG_ON(!q->ops);
1732 BUG_ON(!q->mem_ops);
1733 BUG_ON(!q->type);
1734 BUG_ON(!q->io_modes);
1735
1736 BUG_ON(!q->ops->queue_setup);
1737 BUG_ON(!q->ops->buf_queue);
1738
1739 INIT_LIST_HEAD(&q->queued_list);
1740 INIT_LIST_HEAD(&q->done_list);
1741 spin_lock_init(&q->done_lock);
1742 init_waitqueue_head(&q->done_wq);
1743
1744 if (q->buf_struct_size == 0)
1745 q->buf_struct_size = sizeof(struct vb2_buffer);
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL_GPL(vb2_queue_init);
1750
1751/**
1752 * vb2_queue_release() - stop streaming, release the queue and free memory
1753 * @q: videobuf2 queue
1754 *
1755 * This function stops streaming and performs necessary clean ups, including
1756 * freeing video buffer memory. The driver is responsible for freeing
1757 * the vb2_queue structure itself.
1758 */
1759void vb2_queue_release(struct vb2_queue *q)
1760{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001761 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001762 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001763 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001764}
1765EXPORT_SYMBOL_GPL(vb2_queue_release);
1766
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001767/**
1768 * struct vb2_fileio_buf - buffer context used by file io emulator
1769 *
1770 * vb2 provides a compatibility layer and emulator of file io (read and
1771 * write) calls on top of streaming API. This structure is used for
1772 * tracking context related to the buffers.
1773 */
1774struct vb2_fileio_buf {
1775 void *vaddr;
1776 unsigned int size;
1777 unsigned int pos;
1778 unsigned int queued:1;
1779};
1780
1781/**
1782 * struct vb2_fileio_data - queue context used by file io emulator
1783 *
1784 * vb2 provides a compatibility layer and emulator of file io (read and
1785 * write) calls on top of streaming API. For proper operation it required
1786 * this structure to save the driver state between each call of the read
1787 * or write function.
1788 */
1789struct vb2_fileio_data {
1790 struct v4l2_requestbuffers req;
1791 struct v4l2_buffer b;
1792 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1793 unsigned int index;
1794 unsigned int q_count;
1795 unsigned int dq_count;
1796 unsigned int flags;
1797};
1798
1799/**
1800 * __vb2_init_fileio() - initialize file io emulator
1801 * @q: videobuf2 queue
1802 * @read: mode selector (1 means read, 0 means write)
1803 */
1804static int __vb2_init_fileio(struct vb2_queue *q, int read)
1805{
1806 struct vb2_fileio_data *fileio;
1807 int i, ret;
1808 unsigned int count = 0;
1809
1810 /*
1811 * Sanity check
1812 */
1813 if ((read && !(q->io_modes & VB2_READ)) ||
1814 (!read && !(q->io_modes & VB2_WRITE)))
1815 BUG();
1816
1817 /*
1818 * Check if device supports mapping buffers to kernel virtual space.
1819 */
1820 if (!q->mem_ops->vaddr)
1821 return -EBUSY;
1822
1823 /*
1824 * Check if streaming api has not been already activated.
1825 */
1826 if (q->streaming || q->num_buffers > 0)
1827 return -EBUSY;
1828
1829 /*
1830 * Start with count 1, driver can increase it in queue_setup()
1831 */
1832 count = 1;
1833
1834 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1835 (read) ? "read" : "write", count, q->io_flags);
1836
1837 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1838 if (fileio == NULL)
1839 return -ENOMEM;
1840
1841 fileio->flags = q->io_flags;
1842
1843 /*
1844 * Request buffers and use MMAP type to force driver
1845 * to allocate buffers by itself.
1846 */
1847 fileio->req.count = count;
1848 fileio->req.memory = V4L2_MEMORY_MMAP;
1849 fileio->req.type = q->type;
1850 ret = vb2_reqbufs(q, &fileio->req);
1851 if (ret)
1852 goto err_kfree;
1853
1854 /*
1855 * Check if plane_count is correct
1856 * (multiplane buffers are not supported).
1857 */
1858 if (q->bufs[0]->num_planes != 1) {
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001859 ret = -EBUSY;
1860 goto err_reqbufs;
1861 }
1862
1863 /*
1864 * Get kernel address of each buffer.
1865 */
1866 for (i = 0; i < q->num_buffers; i++) {
1867 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1868 if (fileio->bufs[i].vaddr == NULL)
1869 goto err_reqbufs;
1870 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1871 }
1872
1873 /*
1874 * Read mode requires pre queuing of all buffers.
1875 */
1876 if (read) {
1877 /*
1878 * Queue all buffers.
1879 */
1880 for (i = 0; i < q->num_buffers; i++) {
1881 struct v4l2_buffer *b = &fileio->b;
1882 memset(b, 0, sizeof(*b));
1883 b->type = q->type;
1884 b->memory = q->memory;
1885 b->index = i;
1886 ret = vb2_qbuf(q, b);
1887 if (ret)
1888 goto err_reqbufs;
1889 fileio->bufs[i].queued = 1;
1890 }
1891
1892 /*
1893 * Start streaming.
1894 */
1895 ret = vb2_streamon(q, q->type);
1896 if (ret)
1897 goto err_reqbufs;
1898 }
1899
1900 q->fileio = fileio;
1901
1902 return ret;
1903
1904err_reqbufs:
Hans de Goedea67e1722012-05-08 14:47:39 -03001905 fileio->req.count = 0;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001906 vb2_reqbufs(q, &fileio->req);
1907
1908err_kfree:
1909 kfree(fileio);
1910 return ret;
1911}
1912
1913/**
1914 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1915 * @q: videobuf2 queue
1916 */
1917static int __vb2_cleanup_fileio(struct vb2_queue *q)
1918{
1919 struct vb2_fileio_data *fileio = q->fileio;
1920
1921 if (fileio) {
1922 /*
1923 * Hack fileio context to enable direct calls to vb2 ioctl
1924 * interface.
1925 */
1926 q->fileio = NULL;
1927
1928 vb2_streamoff(q, q->type);
1929 fileio->req.count = 0;
1930 vb2_reqbufs(q, &fileio->req);
1931 kfree(fileio);
1932 dprintk(3, "file io emulator closed\n");
1933 }
1934 return 0;
1935}
1936
1937/**
1938 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1939 * @q: videobuf2 queue
1940 * @data: pointed to target userspace buffer
1941 * @count: number of bytes to read or write
1942 * @ppos: file handle position tracking pointer
1943 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1944 * @read: access mode selector (1 means read, 0 means write)
1945 */
1946static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1947 loff_t *ppos, int nonblock, int read)
1948{
1949 struct vb2_fileio_data *fileio;
1950 struct vb2_fileio_buf *buf;
1951 int ret, index;
1952
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001953 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001954 read ? "read" : "write", (long)*ppos, count,
1955 nonblock ? "non" : "");
1956
1957 if (!data)
1958 return -EINVAL;
1959
1960 /*
1961 * Initialize emulator on first call.
1962 */
1963 if (!q->fileio) {
1964 ret = __vb2_init_fileio(q, read);
1965 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1966 if (ret)
1967 return ret;
1968 }
1969 fileio = q->fileio;
1970
1971 /*
1972 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1973 * The pointer will be restored before returning from this function.
1974 */
1975 q->fileio = NULL;
1976
1977 index = fileio->index;
1978 buf = &fileio->bufs[index];
1979
1980 /*
1981 * Check if we need to dequeue the buffer.
1982 */
1983 if (buf->queued) {
1984 struct vb2_buffer *vb;
1985
1986 /*
1987 * Call vb2_dqbuf to get buffer back.
1988 */
1989 memset(&fileio->b, 0, sizeof(fileio->b));
1990 fileio->b.type = q->type;
1991 fileio->b.memory = q->memory;
1992 fileio->b.index = index;
1993 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1994 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1995 if (ret)
1996 goto end;
1997 fileio->dq_count += 1;
1998
1999 /*
2000 * Get number of bytes filled by the driver
2001 */
2002 vb = q->bufs[index];
2003 buf->size = vb2_get_plane_payload(vb, 0);
2004 buf->queued = 0;
2005 }
2006
2007 /*
2008 * Limit count on last few bytes of the buffer.
2009 */
2010 if (buf->pos + count > buf->size) {
2011 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002012 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002013 }
2014
2015 /*
2016 * Transfer data to userspace.
2017 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002018 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002019 count, index, buf->pos);
2020 if (read)
2021 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2022 else
2023 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2024 if (ret) {
2025 dprintk(3, "file io: error copying data\n");
2026 ret = -EFAULT;
2027 goto end;
2028 }
2029
2030 /*
2031 * Update counters.
2032 */
2033 buf->pos += count;
2034 *ppos += count;
2035
2036 /*
2037 * Queue next buffer if required.
2038 */
2039 if (buf->pos == buf->size ||
2040 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2041 /*
2042 * Check if this is the last buffer to read.
2043 */
2044 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2045 fileio->dq_count == 1) {
2046 dprintk(3, "file io: read limit reached\n");
2047 /*
2048 * Restore fileio pointer and release the context.
2049 */
2050 q->fileio = fileio;
2051 return __vb2_cleanup_fileio(q);
2052 }
2053
2054 /*
2055 * Call vb2_qbuf and give buffer to the driver.
2056 */
2057 memset(&fileio->b, 0, sizeof(fileio->b));
2058 fileio->b.type = q->type;
2059 fileio->b.memory = q->memory;
2060 fileio->b.index = index;
2061 fileio->b.bytesused = buf->pos;
2062 ret = vb2_qbuf(q, &fileio->b);
2063 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2064 if (ret)
2065 goto end;
2066
2067 /*
2068 * Buffer has been queued, update the status
2069 */
2070 buf->pos = 0;
2071 buf->queued = 1;
2072 buf->size = q->bufs[0]->v4l2_planes[0].length;
2073 fileio->q_count += 1;
2074
2075 /*
2076 * Switch to the next buffer
2077 */
2078 fileio->index = (index + 1) % q->num_buffers;
2079
2080 /*
2081 * Start streaming if required.
2082 */
2083 if (!read && !q->streaming) {
2084 ret = vb2_streamon(q, q->type);
2085 if (ret)
2086 goto end;
2087 }
2088 }
2089
2090 /*
2091 * Return proper number of bytes processed.
2092 */
2093 if (ret == 0)
2094 ret = count;
2095end:
2096 /*
2097 * Restore the fileio context and block vb2 ioctl interface.
2098 */
2099 q->fileio = fileio;
2100 return ret;
2101}
2102
2103size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2104 loff_t *ppos, int nonblocking)
2105{
2106 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2107}
2108EXPORT_SYMBOL_GPL(vb2_read);
2109
2110size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2111 loff_t *ppos, int nonblocking)
2112{
2113 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2114}
2115EXPORT_SYMBOL_GPL(vb2_write);
2116
Pawel Osciake23ccc02010-10-11 10:56:41 -03002117MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002118MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002119MODULE_LICENSE("GPL");