blob: 2e8f1df775b622ef9f877be16916fd7d9d0e0c80 [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
22#include <media/videobuf2-core.h>
23
24static int debug;
25module_param(debug, int, 0644);
26
27#define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
31 } while (0)
32
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030033#define call_memop(q, op, args...) \
Pawel Osciake23ccc02010-10-11 10:56:41 -030034 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
36
37#define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
39
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030040#define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -030041 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
42 V4L2_BUF_FLAG_PREPARED)
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -030043
Pawel Osciake23ccc02010-10-11 10:56:41 -030044/**
45 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
46 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030047static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030048{
49 struct vb2_queue *q = vb->vb2_queue;
50 void *mem_priv;
51 int plane;
52
53 /* Allocate memory for all planes in this buffer */
54 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030055 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030056 q->plane_sizes[plane]);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030057 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030058 goto free;
59
60 /* Associate allocator private data with this plane */
61 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030062 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030063 }
64
65 return 0;
66free:
67 /* Free already allocated memory if one of the allocations failed */
Marek Szyprowskia00d0262011-12-15 05:53:06 -030068 for (; plane > 0; --plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030069 call_memop(q, put, vb->planes[plane - 1].mem_priv);
Marek Szyprowskia00d0262011-12-15 05:53:06 -030070 vb->planes[plane - 1].mem_priv = NULL;
71 }
Pawel Osciake23ccc02010-10-11 10:56:41 -030072
73 return -ENOMEM;
74}
75
76/**
77 * __vb2_buf_mem_free() - free memory of the given buffer
78 */
79static void __vb2_buf_mem_free(struct vb2_buffer *vb)
80{
81 struct vb2_queue *q = vb->vb2_queue;
82 unsigned int plane;
83
84 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski5931ffe2011-12-15 05:44:12 -030085 call_memop(q, put, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -030086 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskia00d0262011-12-15 05:53:06 -030087 dprintk(3, "Freed plane %d of buffer %d\n", plane,
88 vb->v4l2_buf.index);
Pawel Osciake23ccc02010-10-11 10:56:41 -030089 }
90}
91
92/**
93 * __vb2_buf_userptr_put() - release userspace memory associated with
94 * a USERPTR buffer
95 */
96static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
97{
98 struct vb2_queue *q = vb->vb2_queue;
99 unsigned int plane;
100
101 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300102 if (vb->planes[plane].mem_priv)
103 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
104 vb->planes[plane].mem_priv = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300105 }
106}
107
108/**
109 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
110 * every buffer on the queue
111 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300112static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300113{
114 unsigned int buffer, plane;
115 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300116 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300117
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300118 if (q->num_buffers) {
119 struct v4l2_plane *p;
120 vb = q->bufs[q->num_buffers - 1];
121 p = &vb->v4l2_planes[vb->num_planes - 1];
122 off = PAGE_ALIGN(p->m.mem_offset + p->length);
123 } else {
124 off = 0;
125 }
126
127 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300128 vb = q->bufs[buffer];
129 if (!vb)
130 continue;
131
132 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300133 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300134 vb->v4l2_planes[plane].m.mem_offset = off;
135
136 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
137 buffer, plane, off);
138
139 off += vb->v4l2_planes[plane].length;
140 off = PAGE_ALIGN(off);
141 }
142 }
143}
144
145/**
146 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
147 * video buffer memory for all buffers/planes on the queue and initializes the
148 * queue
149 *
150 * Returns the number of buffers successfully allocated.
151 */
152static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300153 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300154{
155 unsigned int buffer;
156 struct vb2_buffer *vb;
157 int ret;
158
159 for (buffer = 0; buffer < num_buffers; ++buffer) {
160 /* Allocate videobuf buffer structures */
161 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
162 if (!vb) {
163 dprintk(1, "Memory alloc for buffer struct failed\n");
164 break;
165 }
166
167 /* Length stores number of planes for multiplanar buffers */
168 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
169 vb->v4l2_buf.length = num_planes;
170
171 vb->state = VB2_BUF_STATE_DEQUEUED;
172 vb->vb2_queue = q;
173 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300174 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300175 vb->v4l2_buf.type = q->type;
176 vb->v4l2_buf.memory = memory;
177
178 /* Allocate video buffer memory for the MMAP type */
179 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300180 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300181 if (ret) {
182 dprintk(1, "Failed allocating memory for "
183 "buffer %d\n", buffer);
184 kfree(vb);
185 break;
186 }
187 /*
188 * Call the driver-provided buffer initialization
189 * callback, if given. An error in initialization
190 * results in queue setup failure.
191 */
192 ret = call_qop(q, buf_init, vb);
193 if (ret) {
194 dprintk(1, "Buffer %d %p initialization"
195 " failed\n", buffer, vb);
196 __vb2_buf_mem_free(vb);
197 kfree(vb);
198 break;
199 }
200 }
201
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300202 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300203 }
204
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300205 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300206
207 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300208 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300209
210 return buffer;
211}
212
213/**
214 * __vb2_free_mem() - release all video buffer memory for a given queue
215 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300216static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300217{
218 unsigned int buffer;
219 struct vb2_buffer *vb;
220
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300221 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
222 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300223 vb = q->bufs[buffer];
224 if (!vb)
225 continue;
226
227 /* Free MMAP buffers or release USERPTR buffers */
228 if (q->memory == V4L2_MEMORY_MMAP)
229 __vb2_buf_mem_free(vb);
230 else
231 __vb2_buf_userptr_put(vb);
232 }
233}
234
235/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300236 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
237 * related information, if no buffers are left return the queue to an
238 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300239 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300240static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300241{
242 unsigned int buffer;
243
244 /* Call driver-provided cleanup function for each buffer, if provided */
245 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300246 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
247 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300248 if (NULL == q->bufs[buffer])
249 continue;
250 q->ops->buf_cleanup(q->bufs[buffer]);
251 }
252 }
253
254 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300255 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300256
257 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300258 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
259 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300260 kfree(q->bufs[buffer]);
261 q->bufs[buffer] = NULL;
262 }
263
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300264 q->num_buffers -= buffers;
265 if (!q->num_buffers)
266 q->memory = 0;
Marek Szyprowskibd50d992011-10-25 03:07:59 -0300267 INIT_LIST_HEAD(&q->queued_list);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300268}
269
270/**
271 * __verify_planes_array() - verify that the planes array passed in struct
272 * v4l2_buffer from userspace can be safely used
273 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300274static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300275{
276 /* Is memory for copying plane information present? */
277 if (NULL == b->m.planes) {
278 dprintk(1, "Multi-planar buffer passed but "
279 "planes array not provided\n");
280 return -EINVAL;
281 }
282
283 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
284 dprintk(1, "Incorrect planes array length, "
285 "expected %d, got %d\n", vb->num_planes, b->length);
286 return -EINVAL;
287 }
288
289 return 0;
290}
291
292/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300293 * __buffer_in_use() - return true if the buffer is in use and
294 * the queue cannot be freed (by the means of REQBUFS(0)) call
295 */
296static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
297{
298 unsigned int plane;
299 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300300 void *mem_priv = vb->planes[plane].mem_priv;
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300301 /*
302 * If num_users() has not been provided, call_memop
303 * will return 0, apparently nobody cares about this
304 * case anyway. If num_users() returns more than 1,
305 * we are not the only user of the plane's memory.
306 */
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300307 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300308 return true;
309 }
310 return false;
311}
312
313/**
314 * __buffers_in_use() - return true if any buffers on the queue are in use and
315 * the queue cannot be freed (by the means of REQBUFS(0)) call
316 */
317static bool __buffers_in_use(struct vb2_queue *q)
318{
319 unsigned int buffer;
320 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
321 if (__buffer_in_use(q, q->bufs[buffer]))
322 return true;
323 }
324 return false;
325}
326
327/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300328 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
329 * returned to userspace
330 */
331static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
332{
333 struct vb2_queue *q = vb->vb2_queue;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300334 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300335
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300336 /* Copy back data such as timestamp, flags, input, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300337 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
338 b->input = vb->v4l2_buf.input;
339 b->reserved = vb->v4l2_buf.reserved;
340
341 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
342 ret = __verify_planes_array(vb, b);
343 if (ret)
344 return ret;
345
346 /*
347 * Fill in plane-related data if userspace provided an array
348 * for it. The memory and size is verified above.
349 */
350 memcpy(b->m.planes, vb->v4l2_planes,
351 b->length * sizeof(struct v4l2_plane));
352 } else {
353 /*
354 * We use length and offset in v4l2_planes array even for
355 * single-planar buffers, but userspace does not.
356 */
357 b->length = vb->v4l2_planes[0].length;
358 b->bytesused = vb->v4l2_planes[0].bytesused;
359 if (q->memory == V4L2_MEMORY_MMAP)
360 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
361 else if (q->memory == V4L2_MEMORY_USERPTR)
362 b->m.userptr = vb->v4l2_planes[0].m.userptr;
363 }
364
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300365 /*
366 * Clear any buffer state related flags.
367 */
368 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300369
370 switch (vb->state) {
371 case VB2_BUF_STATE_QUEUED:
372 case VB2_BUF_STATE_ACTIVE:
373 b->flags |= V4L2_BUF_FLAG_QUEUED;
374 break;
375 case VB2_BUF_STATE_ERROR:
376 b->flags |= V4L2_BUF_FLAG_ERROR;
377 /* fall through */
378 case VB2_BUF_STATE_DONE:
379 b->flags |= V4L2_BUF_FLAG_DONE;
380 break;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300381 case VB2_BUF_STATE_PREPARED:
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300382 b->flags |= V4L2_BUF_FLAG_PREPARED;
383 break;
384 case VB2_BUF_STATE_DEQUEUED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300385 /* nothing */
386 break;
387 }
388
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300389 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300390 b->flags |= V4L2_BUF_FLAG_MAPPED;
391
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300392 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300393}
394
395/**
396 * vb2_querybuf() - query video buffer information
397 * @q: videobuf queue
398 * @b: buffer struct passed from userspace to vidioc_querybuf handler
399 * in driver
400 *
401 * Should be called from vidioc_querybuf ioctl handler in driver.
402 * This function will verify the passed v4l2_buffer structure and fill the
403 * relevant information for the userspace.
404 *
405 * The return values from this function are intended to be directly returned
406 * from vidioc_querybuf handler in driver.
407 */
408int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
409{
410 struct vb2_buffer *vb;
411
412 if (b->type != q->type) {
413 dprintk(1, "querybuf: wrong buffer type\n");
414 return -EINVAL;
415 }
416
417 if (b->index >= q->num_buffers) {
418 dprintk(1, "querybuf: buffer index out of range\n");
419 return -EINVAL;
420 }
421 vb = q->bufs[b->index];
422
423 return __fill_v4l2_buffer(vb, b);
424}
425EXPORT_SYMBOL(vb2_querybuf);
426
427/**
428 * __verify_userptr_ops() - verify that all memory operations required for
429 * USERPTR queue type have been provided
430 */
431static int __verify_userptr_ops(struct vb2_queue *q)
432{
433 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
434 !q->mem_ops->put_userptr)
435 return -EINVAL;
436
437 return 0;
438}
439
440/**
441 * __verify_mmap_ops() - verify that all memory operations required for
442 * MMAP queue type have been provided
443 */
444static int __verify_mmap_ops(struct vb2_queue *q)
445{
446 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
447 !q->mem_ops->put || !q->mem_ops->mmap)
448 return -EINVAL;
449
450 return 0;
451}
452
453/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300454 * vb2_reqbufs() - Initiate streaming
455 * @q: videobuf2 queue
456 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
457 *
458 * Should be called from vidioc_reqbufs ioctl handler of a driver.
459 * This function:
460 * 1) verifies streaming parameters passed from the userspace,
461 * 2) sets up the queue,
462 * 3) negotiates number of buffers and planes per buffer with the driver
463 * to be used during streaming,
464 * 4) allocates internal buffer structures (struct vb2_buffer), according to
465 * the agreed parameters,
466 * 5) for MMAP memory type, allocates actual video memory, using the
467 * memory handling/allocation routines provided during queue initialization
468 *
469 * If req->count is 0, all the memory will be freed instead.
470 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
471 * and the queue is not busy, memory will be reallocated.
472 *
473 * The return values from this function are intended to be directly returned
474 * from vidioc_reqbufs handler in driver.
475 */
476int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
477{
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300478 unsigned int num_buffers, allocated_buffers, num_planes = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300479 int ret = 0;
480
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300481 if (q->fileio) {
482 dprintk(1, "reqbufs: file io in progress\n");
483 return -EBUSY;
484 }
485
Pawel Osciake23ccc02010-10-11 10:56:41 -0300486 if (req->memory != V4L2_MEMORY_MMAP
487 && req->memory != V4L2_MEMORY_USERPTR) {
488 dprintk(1, "reqbufs: unsupported memory type\n");
489 return -EINVAL;
490 }
491
492 if (req->type != q->type) {
493 dprintk(1, "reqbufs: requested type is incorrect\n");
494 return -EINVAL;
495 }
496
497 if (q->streaming) {
498 dprintk(1, "reqbufs: streaming active\n");
499 return -EBUSY;
500 }
501
502 /*
503 * Make sure all the required memory ops for given memory type
504 * are available.
505 */
506 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
507 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
508 return -EINVAL;
509 }
510
511 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
512 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
513 return -EINVAL;
514 }
515
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300516 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300517 /*
518 * We already have buffers allocated, so first check if they
519 * are not in use and can be freed.
520 */
521 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
522 dprintk(1, "reqbufs: memory in use, cannot free\n");
523 return -EBUSY;
524 }
525
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300526 __vb2_queue_free(q, q->num_buffers);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300527
528 /*
529 * In case of REQBUFS(0) return immediately without calling
530 * driver's queue_setup() callback and allocating resources.
531 */
532 if (req->count == 0)
533 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300534 }
535
536 /*
537 * Make sure the requested values and current defaults are sane.
538 */
539 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300540 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300541 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300542 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300543
544 /*
545 * Ask the driver how many buffers and planes per buffer it requires.
546 * Driver also sets the size and allocator context for each plane.
547 */
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300548 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300549 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300550 if (ret)
551 return ret;
552
553 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300554 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300555 if (ret == 0) {
556 dprintk(1, "Memory allocation failed\n");
557 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300558 }
559
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300560 allocated_buffers = ret;
561
Pawel Osciake23ccc02010-10-11 10:56:41 -0300562 /*
563 * Check if driver can handle the allocated number of buffers.
564 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300565 if (allocated_buffers < num_buffers) {
566 num_buffers = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300567
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300568 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
569 &num_planes, q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300570
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300571 if (!ret && allocated_buffers < num_buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300572 ret = -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300573
574 /*
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300575 * Either the driver has accepted a smaller number of buffers,
576 * or .queue_setup() returned an error
Pawel Osciake23ccc02010-10-11 10:56:41 -0300577 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300578 }
579
580 q->num_buffers = allocated_buffers;
581
582 if (ret < 0) {
583 __vb2_queue_free(q, allocated_buffers);
584 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300585 }
586
Pawel Osciake23ccc02010-10-11 10:56:41 -0300587 /*
588 * Return the number of successfully allocated buffers
589 * to the userspace.
590 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300591 req->count = allocated_buffers;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300592
593 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300594}
595EXPORT_SYMBOL_GPL(vb2_reqbufs);
596
597/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300598 * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
599 * @q: videobuf2 queue
600 * @create: creation parameters, passed from userspace to vidioc_create_bufs
601 * handler in driver
602 *
603 * Should be called from vidioc_create_bufs ioctl handler of a driver.
604 * This function:
605 * 1) verifies parameter sanity
606 * 2) calls the .queue_setup() queue operation
607 * 3) performs any necessary memory allocations
608 *
609 * The return values from this function are intended to be directly returned
610 * from vidioc_create_bufs handler in driver.
611 */
612int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
613{
614 unsigned int num_planes = 0, num_buffers, allocated_buffers;
615 int ret = 0;
616
617 if (q->fileio) {
618 dprintk(1, "%s(): file io in progress\n", __func__);
619 return -EBUSY;
620 }
621
622 if (create->memory != V4L2_MEMORY_MMAP
623 && create->memory != V4L2_MEMORY_USERPTR) {
624 dprintk(1, "%s(): unsupported memory type\n", __func__);
625 return -EINVAL;
626 }
627
628 if (create->format.type != q->type) {
629 dprintk(1, "%s(): requested type is incorrect\n", __func__);
630 return -EINVAL;
631 }
632
633 /*
634 * Make sure all the required memory ops for given memory type
635 * are available.
636 */
637 if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
638 dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
639 return -EINVAL;
640 }
641
642 if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
643 dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
644 return -EINVAL;
645 }
646
647 if (q->num_buffers == VIDEO_MAX_FRAME) {
648 dprintk(1, "%s(): maximum number of buffers already allocated\n",
649 __func__);
650 return -ENOBUFS;
651 }
652
653 create->index = q->num_buffers;
654
655 if (!q->num_buffers) {
656 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
657 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
658 q->memory = create->memory;
659 }
660
661 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
662
663 /*
664 * Ask the driver, whether the requested number of buffers, planes per
665 * buffer and their sizes are acceptable
666 */
667 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
668 &num_planes, q->plane_sizes, q->alloc_ctx);
669 if (ret)
670 return ret;
671
672 /* Finally, allocate buffers and video memory */
673 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
674 num_planes);
675 if (ret < 0) {
676 dprintk(1, "Memory allocation failed with error: %d\n", ret);
677 return ret;
678 }
679
680 allocated_buffers = ret;
681
682 /*
683 * Check if driver can handle the so far allocated number of buffers.
684 */
685 if (ret < num_buffers) {
686 num_buffers = ret;
687
688 /*
689 * q->num_buffers contains the total number of buffers, that the
690 * queue driver has set up
691 */
692 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
693 &num_planes, q->plane_sizes, q->alloc_ctx);
694
695 if (!ret && allocated_buffers < num_buffers)
696 ret = -ENOMEM;
697
698 /*
699 * Either the driver has accepted a smaller number of buffers,
700 * or .queue_setup() returned an error
701 */
702 }
703
704 q->num_buffers += allocated_buffers;
705
706 if (ret < 0) {
707 __vb2_queue_free(q, allocated_buffers);
708 return ret;
709 }
710
711 /*
712 * Return the number of successfully allocated buffers
713 * to the userspace.
714 */
715 create->count = allocated_buffers;
716
717 return 0;
718}
719EXPORT_SYMBOL_GPL(vb2_create_bufs);
720
721/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300722 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
723 * @vb: vb2_buffer to which the plane in question belongs to
724 * @plane_no: plane number for which the address is to be returned
725 *
726 * This function returns a kernel virtual address of a given plane if
727 * such a mapping exist, NULL otherwise.
728 */
729void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
730{
731 struct vb2_queue *q = vb->vb2_queue;
732
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300733 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300734 return NULL;
735
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300736 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300737
738}
739EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
740
741/**
742 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
743 * @vb: vb2_buffer to which the plane in question belongs to
744 * @plane_no: plane number for which the cookie is to be returned
745 *
746 * This function returns an allocator specific cookie for a given plane if
747 * available, NULL otherwise. The allocator should provide some simple static
748 * inline function, which would convert this cookie to the allocator specific
749 * type that can be used directly by the driver to access the buffer. This can
750 * be for example physical address, pointer to scatter list or IOMMU mapping.
751 */
752void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
753{
754 struct vb2_queue *q = vb->vb2_queue;
755
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300756 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300757 return NULL;
758
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300759 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300760}
761EXPORT_SYMBOL_GPL(vb2_plane_cookie);
762
763/**
764 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
765 * @vb: vb2_buffer returned from the driver
766 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
767 * or VB2_BUF_STATE_ERROR if the operation finished with an error
768 *
769 * This function should be called by the driver after a hardware operation on
770 * a buffer is finished and the buffer may be returned to userspace. The driver
771 * cannot use this buffer anymore until it is queued back to it by videobuf
772 * by the means of buf_queue callback. Only buffers previously queued to the
773 * driver by buf_queue can be passed to this function.
774 */
775void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
776{
777 struct vb2_queue *q = vb->vb2_queue;
778 unsigned long flags;
779
780 if (vb->state != VB2_BUF_STATE_ACTIVE)
781 return;
782
783 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
784 return;
785
786 dprintk(4, "Done processing on buffer %d, state: %d\n",
787 vb->v4l2_buf.index, vb->state);
788
789 /* Add the buffer to the done buffers list */
790 spin_lock_irqsave(&q->done_lock, flags);
791 vb->state = state;
792 list_add_tail(&vb->done_entry, &q->done_list);
793 atomic_dec(&q->queued_count);
794 spin_unlock_irqrestore(&q->done_lock, flags);
795
796 /* Inform any processes that may be waiting for buffers */
797 wake_up(&q->done_wq);
798}
799EXPORT_SYMBOL_GPL(vb2_buffer_done);
800
801/**
802 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
803 * a v4l2_buffer by the userspace
804 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300805static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300806 struct v4l2_plane *v4l2_planes)
807{
808 unsigned int plane;
809 int ret;
810
811 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
812 /*
813 * Verify that the userspace gave us a valid array for
814 * plane information.
815 */
816 ret = __verify_planes_array(vb, b);
817 if (ret)
818 return ret;
819
820 /* Fill in driver-provided information for OUTPUT types */
821 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
822 /*
823 * Will have to go up to b->length when API starts
824 * accepting variable number of planes.
825 */
826 for (plane = 0; plane < vb->num_planes; ++plane) {
827 v4l2_planes[plane].bytesused =
828 b->m.planes[plane].bytesused;
829 v4l2_planes[plane].data_offset =
830 b->m.planes[plane].data_offset;
831 }
832 }
833
834 if (b->memory == V4L2_MEMORY_USERPTR) {
835 for (plane = 0; plane < vb->num_planes; ++plane) {
836 v4l2_planes[plane].m.userptr =
837 b->m.planes[plane].m.userptr;
838 v4l2_planes[plane].length =
839 b->m.planes[plane].length;
840 }
841 }
842 } else {
843 /*
844 * Single-planar buffers do not use planes array,
845 * so fill in relevant v4l2_buffer struct fields instead.
846 * In videobuf we use our internal V4l2_planes struct for
847 * single-planar buffers as well, for simplicity.
848 */
849 if (V4L2_TYPE_IS_OUTPUT(b->type))
850 v4l2_planes[0].bytesused = b->bytesused;
851
852 if (b->memory == V4L2_MEMORY_USERPTR) {
853 v4l2_planes[0].m.userptr = b->m.userptr;
854 v4l2_planes[0].length = b->length;
855 }
856 }
857
858 vb->v4l2_buf.field = b->field;
859 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300860 vb->v4l2_buf.input = b->input;
861 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300862
863 return 0;
864}
865
866/**
867 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
868 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300869static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300870{
871 struct v4l2_plane planes[VIDEO_MAX_PLANES];
872 struct vb2_queue *q = vb->vb2_queue;
873 void *mem_priv;
874 unsigned int plane;
875 int ret;
876 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
877
878 /* Verify and copy relevant information provided by the userspace */
879 ret = __fill_vb2_buffer(vb, b, planes);
880 if (ret)
881 return ret;
882
883 for (plane = 0; plane < vb->num_planes; ++plane) {
884 /* Skip the plane if already verified */
Marek Szyprowskif0b7c7f2011-11-16 15:09:40 -0300885 if (vb->v4l2_planes[plane].m.userptr &&
886 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
Pawel Osciake23ccc02010-10-11 10:56:41 -0300887 && vb->v4l2_planes[plane].length == planes[plane].length)
888 continue;
889
890 dprintk(3, "qbuf: userspace address for plane %d changed, "
891 "reacquiring memory\n", plane);
892
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300893 /* Check if the provided plane buffer is large enough */
894 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300895 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300896 goto err;
897 }
898
Pawel Osciake23ccc02010-10-11 10:56:41 -0300899 /* Release previously acquired memory if present */
900 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300901 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300902
903 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300904 vb->v4l2_planes[plane].m.userptr = 0;
905 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300906
907 /* Acquire each plane's memory */
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300908 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
909 planes[plane].m.userptr,
910 planes[plane].length, write);
911 if (IS_ERR_OR_NULL(mem_priv)) {
912 dprintk(1, "qbuf: failed acquiring userspace "
Pawel Osciake23ccc02010-10-11 10:56:41 -0300913 "memory for plane %d\n", plane);
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300914 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
915 goto err;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300916 }
Marek Szyprowskia00d0262011-12-15 05:53:06 -0300917 vb->planes[plane].mem_priv = mem_priv;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300918 }
919
920 /*
921 * Call driver-specific initialization on the newly acquired buffer,
922 * if provided.
923 */
924 ret = call_qop(q, buf_init, vb);
925 if (ret) {
926 dprintk(1, "qbuf: buffer initialization failed\n");
927 goto err;
928 }
929
930 /*
931 * Now that everything is in order, copy relevant information
932 * provided by userspace.
933 */
934 for (plane = 0; plane < vb->num_planes; ++plane)
935 vb->v4l2_planes[plane] = planes[plane];
936
937 return 0;
938err:
939 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300940 for (plane = 0; plane < vb->num_planes; ++plane) {
941 if (vb->planes[plane].mem_priv)
Marek Szyprowski5931ffe2011-12-15 05:44:12 -0300942 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300943 vb->planes[plane].mem_priv = NULL;
944 vb->v4l2_planes[plane].m.userptr = 0;
945 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300946 }
947
948 return ret;
949}
950
951/**
952 * __qbuf_mmap() - handle qbuf of an MMAP buffer
953 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300954static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300955{
956 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
957}
958
959/**
960 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
961 */
962static void __enqueue_in_driver(struct vb2_buffer *vb)
963{
964 struct vb2_queue *q = vb->vb2_queue;
965
966 vb->state = VB2_BUF_STATE_ACTIVE;
967 atomic_inc(&q->queued_count);
968 q->ops->buf_queue(vb);
969}
970
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300971static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300972{
973 struct vb2_queue *q = vb->vb2_queue;
974 int ret;
975
976 switch (q->memory) {
977 case V4L2_MEMORY_MMAP:
978 ret = __qbuf_mmap(vb, b);
979 break;
980 case V4L2_MEMORY_USERPTR:
981 ret = __qbuf_userptr(vb, b);
982 break;
983 default:
984 WARN(1, "Invalid queue type\n");
985 ret = -EINVAL;
986 }
987
988 if (!ret)
989 ret = call_qop(q, buf_prepare, vb);
990 if (ret)
991 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
992 else
993 vb->state = VB2_BUF_STATE_PREPARED;
994
995 return ret;
996}
997
Pawel Osciake23ccc02010-10-11 10:56:41 -0300998/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300999 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1000 * @q: videobuf2 queue
1001 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1002 * handler in driver
1003 *
1004 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1005 * This function:
1006 * 1) verifies the passed buffer,
1007 * 2) calls buf_prepare callback in the driver (if provided), in which
1008 * driver-specific buffer initialization can be performed,
1009 *
1010 * The return values from this function are intended to be directly returned
1011 * from vidioc_prepare_buf handler in driver.
1012 */
1013int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1014{
1015 struct vb2_buffer *vb;
1016 int ret;
1017
1018 if (q->fileio) {
1019 dprintk(1, "%s(): file io in progress\n", __func__);
1020 return -EBUSY;
1021 }
1022
1023 if (b->type != q->type) {
1024 dprintk(1, "%s(): invalid buffer type\n", __func__);
1025 return -EINVAL;
1026 }
1027
1028 if (b->index >= q->num_buffers) {
1029 dprintk(1, "%s(): buffer index out of range\n", __func__);
1030 return -EINVAL;
1031 }
1032
1033 vb = q->bufs[b->index];
1034 if (NULL == vb) {
1035 /* Should never happen */
1036 dprintk(1, "%s(): buffer is NULL\n", __func__);
1037 return -EINVAL;
1038 }
1039
1040 if (b->memory != q->memory) {
1041 dprintk(1, "%s(): invalid memory type\n", __func__);
1042 return -EINVAL;
1043 }
1044
1045 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1046 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1047 return -EINVAL;
1048 }
1049
1050 ret = __buf_prepare(vb, b);
1051 if (ret < 0)
1052 return ret;
1053
1054 __fill_v4l2_buffer(vb, b);
1055
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1059
1060/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001061 * vb2_qbuf() - Queue a buffer from userspace
1062 * @q: videobuf2 queue
1063 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1064 * in driver
1065 *
1066 * Should be called from vidioc_qbuf ioctl handler of a driver.
1067 * This function:
1068 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001069 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1070 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001071 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1072 * callback for processing.
1073 *
1074 * The return values from this function are intended to be directly returned
1075 * from vidioc_qbuf handler in driver.
1076 */
1077int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1078{
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001079 struct rw_semaphore *mmap_sem = NULL;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001080 struct vb2_buffer *vb;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001081 int ret = 0;
1082
1083 /*
1084 * In case of user pointer buffers vb2 allocator needs to get direct
1085 * access to userspace pages. This requires getting read access on
1086 * mmap semaphore in the current process structure. The same
1087 * semaphore is taken before calling mmap operation, while both mmap
1088 * and qbuf are called by the driver or v4l2 core with driver's lock
1089 * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
1090 * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
1091 * release driver's lock, takes mmap_sem and then takes again driver's
1092 * lock.
1093 *
1094 * To avoid race with other vb2 calls, which might be called after
1095 * releasing driver's lock, this operation is performed at the
1096 * beggining of qbuf processing. This way the queue status is
1097 * consistent after getting driver's lock back.
1098 */
Mauro Carvalho Chehab57e43cf2011-12-30 16:10:44 -02001099 if (q->memory == V4L2_MEMORY_USERPTR) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001100 mmap_sem = &current->mm->mmap_sem;
1101 call_qop(q, wait_prepare, q);
1102 down_read(mmap_sem);
1103 call_qop(q, wait_finish, q);
1104 }
Pawel Osciake23ccc02010-10-11 10:56:41 -03001105
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001106 if (q->fileio) {
1107 dprintk(1, "qbuf: file io in progress\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001108 ret = -EBUSY;
1109 goto unlock;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001110 }
1111
Pawel Osciake23ccc02010-10-11 10:56:41 -03001112 if (b->type != q->type) {
1113 dprintk(1, "qbuf: invalid buffer type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001114 ret = -EINVAL;
1115 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001116 }
1117
1118 if (b->index >= q->num_buffers) {
1119 dprintk(1, "qbuf: buffer index out of range\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001120 ret = -EINVAL;
1121 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001122 }
1123
1124 vb = q->bufs[b->index];
1125 if (NULL == vb) {
1126 /* Should never happen */
1127 dprintk(1, "qbuf: buffer is NULL\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001128 ret = -EINVAL;
1129 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001130 }
1131
1132 if (b->memory != q->memory) {
1133 dprintk(1, "qbuf: invalid memory type\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001134 ret = -EINVAL;
1135 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001136 }
1137
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001138 switch (vb->state) {
1139 case VB2_BUF_STATE_DEQUEUED:
1140 ret = __buf_prepare(vb, b);
1141 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001142 goto unlock;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001143 case VB2_BUF_STATE_PREPARED:
1144 break;
1145 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001146 dprintk(1, "qbuf: buffer already in use\n");
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001147 ret = -EINVAL;
1148 goto unlock;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001149 }
1150
Pawel Osciake23ccc02010-10-11 10:56:41 -03001151 /*
1152 * Add to the queued buffers list, a buffer will stay on it until
1153 * dequeued in dqbuf.
1154 */
1155 list_add_tail(&vb->queued_entry, &q->queued_list);
1156 vb->state = VB2_BUF_STATE_QUEUED;
1157
1158 /*
1159 * If already streaming, give the buffer to driver for processing.
1160 * If not, the buffer will be given to driver on next streamon.
1161 */
1162 if (q->streaming)
1163 __enqueue_in_driver(vb);
1164
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001165 /* Fill buffer information for the userspace */
1166 __fill_v4l2_buffer(vb, b);
1167
Pawel Osciake23ccc02010-10-11 10:56:41 -03001168 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -03001169unlock:
1170 if (mmap_sem)
1171 up_read(mmap_sem);
1172 return ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001173}
1174EXPORT_SYMBOL_GPL(vb2_qbuf);
1175
1176/**
1177 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1178 * for dequeuing
1179 *
1180 * Will sleep if required for nonblocking == false.
1181 */
1182static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1183{
1184 /*
1185 * All operations on vb_done_list are performed under done_lock
1186 * spinlock protection. However, buffers may be removed from
1187 * it and returned to userspace only while holding both driver's
1188 * lock and the done_lock spinlock. Thus we can be sure that as
1189 * long as we hold the driver's lock, the list will remain not
1190 * empty if list_empty() check succeeds.
1191 */
1192
1193 for (;;) {
1194 int ret;
1195
1196 if (!q->streaming) {
1197 dprintk(1, "Streaming off, will not wait for buffers\n");
1198 return -EINVAL;
1199 }
1200
1201 if (!list_empty(&q->done_list)) {
1202 /*
1203 * Found a buffer that we were waiting for.
1204 */
1205 break;
1206 }
1207
1208 if (nonblocking) {
1209 dprintk(1, "Nonblocking and no buffers to dequeue, "
1210 "will not wait\n");
1211 return -EAGAIN;
1212 }
1213
1214 /*
1215 * We are streaming and blocking, wait for another buffer to
1216 * become ready or for streamoff. Driver's lock is released to
1217 * allow streamoff or qbuf to be called while waiting.
1218 */
1219 call_qop(q, wait_prepare, q);
1220
1221 /*
1222 * All locks have been released, it is safe to sleep now.
1223 */
1224 dprintk(3, "Will sleep waiting for buffers\n");
1225 ret = wait_event_interruptible(q->done_wq,
1226 !list_empty(&q->done_list) || !q->streaming);
1227
1228 /*
1229 * We need to reevaluate both conditions again after reacquiring
1230 * the locks or return an error if one occurred.
1231 */
1232 call_qop(q, wait_finish, q);
1233 if (ret)
1234 return ret;
1235 }
1236 return 0;
1237}
1238
1239/**
1240 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1241 *
1242 * Will sleep if required for nonblocking == false.
1243 */
1244static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1245 int nonblocking)
1246{
1247 unsigned long flags;
1248 int ret;
1249
1250 /*
1251 * Wait for at least one buffer to become available on the done_list.
1252 */
1253 ret = __vb2_wait_for_done_vb(q, nonblocking);
1254 if (ret)
1255 return ret;
1256
1257 /*
1258 * Driver's lock has been held since we last verified that done_list
1259 * is not empty, so no need for another list_empty(done_list) check.
1260 */
1261 spin_lock_irqsave(&q->done_lock, flags);
1262 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1263 list_del(&(*vb)->done_entry);
1264 spin_unlock_irqrestore(&q->done_lock, flags);
1265
1266 return 0;
1267}
1268
1269/**
1270 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1271 * @q: videobuf2 queue
1272 *
1273 * This function will wait until all buffers that have been given to the driver
1274 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1275 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1276 * taken, for example from stop_streaming() callback.
1277 */
1278int vb2_wait_for_all_buffers(struct vb2_queue *q)
1279{
1280 if (!q->streaming) {
1281 dprintk(1, "Streaming off, will not wait for buffers\n");
1282 return -EINVAL;
1283 }
1284
1285 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1289
1290/**
1291 * vb2_dqbuf() - Dequeue a buffer to the userspace
1292 * @q: videobuf2 queue
1293 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1294 * in driver
1295 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1296 * buffers ready for dequeuing are present. Normally the driver
1297 * would be passing (file->f_flags & O_NONBLOCK) here
1298 *
1299 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1300 * This function:
1301 * 1) verifies the passed buffer,
1302 * 2) calls buf_finish callback in the driver (if provided), in which
1303 * driver can perform any additional operations that may be required before
1304 * returning the buffer to userspace, such as cache sync,
1305 * 3) the buffer struct members are filled with relevant information for
1306 * the userspace.
1307 *
1308 * The return values from this function are intended to be directly returned
1309 * from vidioc_dqbuf handler in driver.
1310 */
1311int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1312{
1313 struct vb2_buffer *vb = NULL;
1314 int ret;
1315
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001316 if (q->fileio) {
1317 dprintk(1, "dqbuf: file io in progress\n");
1318 return -EBUSY;
1319 }
1320
Pawel Osciake23ccc02010-10-11 10:56:41 -03001321 if (b->type != q->type) {
1322 dprintk(1, "dqbuf: invalid buffer type\n");
1323 return -EINVAL;
1324 }
1325
1326 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1327 if (ret < 0) {
1328 dprintk(1, "dqbuf: error getting next done buffer\n");
1329 return ret;
1330 }
1331
1332 ret = call_qop(q, buf_finish, vb);
1333 if (ret) {
1334 dprintk(1, "dqbuf: buffer finish failed\n");
1335 return ret;
1336 }
1337
1338 switch (vb->state) {
1339 case VB2_BUF_STATE_DONE:
1340 dprintk(3, "dqbuf: Returning done buffer\n");
1341 break;
1342 case VB2_BUF_STATE_ERROR:
1343 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1344 break;
1345 default:
1346 dprintk(1, "dqbuf: Invalid buffer state\n");
1347 return -EINVAL;
1348 }
1349
1350 /* Fill buffer information for the userspace */
1351 __fill_v4l2_buffer(vb, b);
1352 /* Remove from videobuf queue */
1353 list_del(&vb->queued_entry);
1354
1355 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1356 vb->v4l2_buf.index, vb->state);
1357
1358 vb->state = VB2_BUF_STATE_DEQUEUED;
1359 return 0;
1360}
1361EXPORT_SYMBOL_GPL(vb2_dqbuf);
1362
1363/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001364 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1365 *
1366 * Removes all queued buffers from driver's queue and all buffers queued by
1367 * userspace from videobuf's queue. Returns to state after reqbufs.
1368 */
1369static void __vb2_queue_cancel(struct vb2_queue *q)
1370{
1371 unsigned int i;
1372
1373 /*
1374 * Tell driver to stop all transactions and release all queued
1375 * buffers.
1376 */
1377 if (q->streaming)
1378 call_qop(q, stop_streaming, q);
1379 q->streaming = 0;
1380
1381 /*
1382 * Remove all buffers from videobuf's list...
1383 */
1384 INIT_LIST_HEAD(&q->queued_list);
1385 /*
1386 * ...and done list; userspace will not receive any buffers it
1387 * has not already dequeued before initiating cancel.
1388 */
1389 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001390 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001391 wake_up_all(&q->done_wq);
1392
1393 /*
1394 * Reinitialize all buffers for next use.
1395 */
1396 for (i = 0; i < q->num_buffers; ++i)
1397 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1398}
1399
1400/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001401 * vb2_streamon - start streaming
1402 * @q: videobuf2 queue
1403 * @type: type argument passed from userspace to vidioc_streamon handler
1404 *
1405 * Should be called from vidioc_streamon handler of a driver.
1406 * This function:
1407 * 1) verifies current state
1408 * 2) passes any previously queued buffers to the driver and starts streaming
1409 *
1410 * The return values from this function are intended to be directly returned
1411 * from vidioc_streamon handler in the driver.
1412 */
1413int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1414{
1415 struct vb2_buffer *vb;
1416 int ret;
1417
1418 if (q->fileio) {
1419 dprintk(1, "streamon: file io in progress\n");
1420 return -EBUSY;
1421 }
1422
1423 if (type != q->type) {
1424 dprintk(1, "streamon: invalid stream type\n");
1425 return -EINVAL;
1426 }
1427
1428 if (q->streaming) {
1429 dprintk(1, "streamon: already streaming\n");
1430 return -EBUSY;
1431 }
1432
1433 /*
1434 * If any buffers were queued before streamon,
1435 * we can now pass them to driver for processing.
1436 */
1437 list_for_each_entry(vb, &q->queued_list, queued_entry)
1438 __enqueue_in_driver(vb);
1439
1440 /*
1441 * Let driver notice that streaming state has been enabled.
1442 */
1443 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1444 if (ret) {
1445 dprintk(1, "streamon: driver refused to start streaming\n");
1446 __vb2_queue_cancel(q);
1447 return ret;
1448 }
1449
1450 q->streaming = 1;
1451
1452 dprintk(3, "Streamon successful\n");
1453 return 0;
1454}
1455EXPORT_SYMBOL_GPL(vb2_streamon);
1456
1457
1458/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001459 * vb2_streamoff - stop streaming
1460 * @q: videobuf2 queue
1461 * @type: type argument passed from userspace to vidioc_streamoff handler
1462 *
1463 * Should be called from vidioc_streamoff handler of a driver.
1464 * This function:
1465 * 1) verifies current state,
1466 * 2) stop streaming and dequeues any queued buffers, including those previously
1467 * passed to the driver (after waiting for the driver to finish).
1468 *
1469 * This call can be used for pausing playback.
1470 * The return values from this function are intended to be directly returned
1471 * from vidioc_streamoff handler in the driver
1472 */
1473int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1474{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001475 if (q->fileio) {
1476 dprintk(1, "streamoff: file io in progress\n");
1477 return -EBUSY;
1478 }
1479
Pawel Osciake23ccc02010-10-11 10:56:41 -03001480 if (type != q->type) {
1481 dprintk(1, "streamoff: invalid stream type\n");
1482 return -EINVAL;
1483 }
1484
1485 if (!q->streaming) {
1486 dprintk(1, "streamoff: not streaming\n");
1487 return -EINVAL;
1488 }
1489
1490 /*
1491 * Cancel will pause streaming and remove all buffers from the driver
1492 * and videobuf, effectively returning control over them to userspace.
1493 */
1494 __vb2_queue_cancel(q);
1495
1496 dprintk(3, "Streamoff successful\n");
1497 return 0;
1498}
1499EXPORT_SYMBOL_GPL(vb2_streamoff);
1500
1501/**
1502 * __find_plane_by_offset() - find plane associated with the given offset off
1503 */
1504static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1505 unsigned int *_buffer, unsigned int *_plane)
1506{
1507 struct vb2_buffer *vb;
1508 unsigned int buffer, plane;
1509
1510 /*
1511 * Go over all buffers and their planes, comparing the given offset
1512 * with an offset assigned to each plane. If a match is found,
1513 * return its buffer and plane numbers.
1514 */
1515 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1516 vb = q->bufs[buffer];
1517
1518 for (plane = 0; plane < vb->num_planes; ++plane) {
1519 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1520 *_buffer = buffer;
1521 *_plane = plane;
1522 return 0;
1523 }
1524 }
1525 }
1526
1527 return -EINVAL;
1528}
1529
1530/**
1531 * vb2_mmap() - map video buffers into application address space
1532 * @q: videobuf2 queue
1533 * @vma: vma passed to the mmap file operation handler in the driver
1534 *
1535 * Should be called from mmap file operation handler of a driver.
1536 * This function maps one plane of one of the available video buffers to
1537 * userspace. To map whole video memory allocated on reqbufs, this function
1538 * has to be called once per each plane per each buffer previously allocated.
1539 *
1540 * When the userspace application calls mmap, it passes to it an offset returned
1541 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1542 * a "cookie", which is then used to identify the plane to be mapped.
1543 * This function finds a plane with a matching offset and a mapping is performed
1544 * by the means of a provided memory operation.
1545 *
1546 * The return values from this function are intended to be directly returned
1547 * from the mmap handler in driver.
1548 */
1549int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1550{
1551 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001552 struct vb2_buffer *vb;
1553 unsigned int buffer, plane;
1554 int ret;
1555
1556 if (q->memory != V4L2_MEMORY_MMAP) {
1557 dprintk(1, "Queue is not currently set up for mmap\n");
1558 return -EINVAL;
1559 }
1560
1561 /*
1562 * Check memory area access mode.
1563 */
1564 if (!(vma->vm_flags & VM_SHARED)) {
1565 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1566 return -EINVAL;
1567 }
1568 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1569 if (!(vma->vm_flags & VM_WRITE)) {
1570 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1571 return -EINVAL;
1572 }
1573 } else {
1574 if (!(vma->vm_flags & VM_READ)) {
1575 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1576 return -EINVAL;
1577 }
1578 }
1579
1580 /*
1581 * Find the plane corresponding to the offset passed by userspace.
1582 */
1583 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1584 if (ret)
1585 return ret;
1586
1587 vb = q->bufs[buffer];
Pawel Osciake23ccc02010-10-11 10:56:41 -03001588
Marek Szyprowskia00d0262011-12-15 05:53:06 -03001589 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001590 if (ret)
1591 return ret;
1592
Pawel Osciake23ccc02010-10-11 10:56:41 -03001593 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1594 return 0;
1595}
1596EXPORT_SYMBOL_GPL(vb2_mmap);
1597
Scott Jiang6f524ec2011-09-21 09:25:23 -03001598#ifndef CONFIG_MMU
1599unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1600 unsigned long addr,
1601 unsigned long len,
1602 unsigned long pgoff,
1603 unsigned long flags)
1604{
1605 unsigned long off = pgoff << PAGE_SHIFT;
1606 struct vb2_buffer *vb;
1607 unsigned int buffer, plane;
1608 int ret;
1609
1610 if (q->memory != V4L2_MEMORY_MMAP) {
1611 dprintk(1, "Queue is not currently set up for mmap\n");
1612 return -EINVAL;
1613 }
1614
1615 /*
1616 * Find the plane corresponding to the offset passed by userspace.
1617 */
1618 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1619 if (ret)
1620 return ret;
1621
1622 vb = q->bufs[buffer];
1623
1624 return (unsigned long)vb2_plane_vaddr(vb, plane);
1625}
1626EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1627#endif
1628
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001629static int __vb2_init_fileio(struct vb2_queue *q, int read);
1630static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001631
1632/**
1633 * vb2_poll() - implements poll userspace operation
1634 * @q: videobuf2 queue
1635 * @file: file argument passed to the poll file operation handler
1636 * @wait: wait argument passed to the poll file operation handler
1637 *
1638 * This function implements poll file operation handler for a driver.
1639 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1640 * be informed that the file descriptor of a video device is available for
1641 * reading.
1642 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1643 * will be reported as available for writing.
1644 *
1645 * The return values from this function are intended to be directly returned
1646 * from poll handler in driver.
1647 */
1648unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1649{
1650 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001651 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001652 struct vb2_buffer *vb = NULL;
1653
1654 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001655 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001656 */
1657 if (q->num_buffers == 0 && q->fileio == NULL) {
1658 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1659 ret = __vb2_init_fileio(q, 1);
1660 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001661 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001662 }
1663 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1664 ret = __vb2_init_fileio(q, 0);
1665 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001666 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001667 /*
1668 * Write to OUTPUT queue can be done immediately.
1669 */
1670 return POLLOUT | POLLWRNORM;
1671 }
1672 }
1673
1674 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001675 * There is nothing to wait for if no buffers have already been queued.
1676 */
1677 if (list_empty(&q->queued_list))
1678 return POLLERR;
1679
1680 poll_wait(file, &q->done_wq, wait);
1681
1682 /*
1683 * Take first buffer available for dequeuing.
1684 */
1685 spin_lock_irqsave(&q->done_lock, flags);
1686 if (!list_empty(&q->done_list))
1687 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1688 done_entry);
1689 spin_unlock_irqrestore(&q->done_lock, flags);
1690
1691 if (vb && (vb->state == VB2_BUF_STATE_DONE
1692 || vb->state == VB2_BUF_STATE_ERROR)) {
1693 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1694 POLLIN | POLLRDNORM;
1695 }
1696 return 0;
1697}
1698EXPORT_SYMBOL_GPL(vb2_poll);
1699
1700/**
1701 * vb2_queue_init() - initialize a videobuf2 queue
1702 * @q: videobuf2 queue; this structure should be allocated in driver
1703 *
1704 * The vb2_queue structure should be allocated by the driver. The driver is
1705 * responsible of clearing it's content and setting initial values for some
1706 * required entries before calling this function.
1707 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1708 * to the struct vb2_queue description in include/media/videobuf2-core.h
1709 * for more information.
1710 */
1711int vb2_queue_init(struct vb2_queue *q)
1712{
1713 BUG_ON(!q);
1714 BUG_ON(!q->ops);
1715 BUG_ON(!q->mem_ops);
1716 BUG_ON(!q->type);
1717 BUG_ON(!q->io_modes);
1718
1719 BUG_ON(!q->ops->queue_setup);
1720 BUG_ON(!q->ops->buf_queue);
1721
1722 INIT_LIST_HEAD(&q->queued_list);
1723 INIT_LIST_HEAD(&q->done_list);
1724 spin_lock_init(&q->done_lock);
1725 init_waitqueue_head(&q->done_wq);
1726
1727 if (q->buf_struct_size == 0)
1728 q->buf_struct_size = sizeof(struct vb2_buffer);
1729
1730 return 0;
1731}
1732EXPORT_SYMBOL_GPL(vb2_queue_init);
1733
1734/**
1735 * vb2_queue_release() - stop streaming, release the queue and free memory
1736 * @q: videobuf2 queue
1737 *
1738 * This function stops streaming and performs necessary clean ups, including
1739 * freeing video buffer memory. The driver is responsible for freeing
1740 * the vb2_queue structure itself.
1741 */
1742void vb2_queue_release(struct vb2_queue *q)
1743{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001744 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001745 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001746 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001747}
1748EXPORT_SYMBOL_GPL(vb2_queue_release);
1749
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001750/**
1751 * struct vb2_fileio_buf - buffer context used by file io emulator
1752 *
1753 * vb2 provides a compatibility layer and emulator of file io (read and
1754 * write) calls on top of streaming API. This structure is used for
1755 * tracking context related to the buffers.
1756 */
1757struct vb2_fileio_buf {
1758 void *vaddr;
1759 unsigned int size;
1760 unsigned int pos;
1761 unsigned int queued:1;
1762};
1763
1764/**
1765 * struct vb2_fileio_data - queue context used by file io emulator
1766 *
1767 * vb2 provides a compatibility layer and emulator of file io (read and
1768 * write) calls on top of streaming API. For proper operation it required
1769 * this structure to save the driver state between each call of the read
1770 * or write function.
1771 */
1772struct vb2_fileio_data {
1773 struct v4l2_requestbuffers req;
1774 struct v4l2_buffer b;
1775 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1776 unsigned int index;
1777 unsigned int q_count;
1778 unsigned int dq_count;
1779 unsigned int flags;
1780};
1781
1782/**
1783 * __vb2_init_fileio() - initialize file io emulator
1784 * @q: videobuf2 queue
1785 * @read: mode selector (1 means read, 0 means write)
1786 */
1787static int __vb2_init_fileio(struct vb2_queue *q, int read)
1788{
1789 struct vb2_fileio_data *fileio;
1790 int i, ret;
1791 unsigned int count = 0;
1792
1793 /*
1794 * Sanity check
1795 */
1796 if ((read && !(q->io_modes & VB2_READ)) ||
1797 (!read && !(q->io_modes & VB2_WRITE)))
1798 BUG();
1799
1800 /*
1801 * Check if device supports mapping buffers to kernel virtual space.
1802 */
1803 if (!q->mem_ops->vaddr)
1804 return -EBUSY;
1805
1806 /*
1807 * Check if streaming api has not been already activated.
1808 */
1809 if (q->streaming || q->num_buffers > 0)
1810 return -EBUSY;
1811
1812 /*
1813 * Start with count 1, driver can increase it in queue_setup()
1814 */
1815 count = 1;
1816
1817 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1818 (read) ? "read" : "write", count, q->io_flags);
1819
1820 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1821 if (fileio == NULL)
1822 return -ENOMEM;
1823
1824 fileio->flags = q->io_flags;
1825
1826 /*
1827 * Request buffers and use MMAP type to force driver
1828 * to allocate buffers by itself.
1829 */
1830 fileio->req.count = count;
1831 fileio->req.memory = V4L2_MEMORY_MMAP;
1832 fileio->req.type = q->type;
1833 ret = vb2_reqbufs(q, &fileio->req);
1834 if (ret)
1835 goto err_kfree;
1836
1837 /*
1838 * Check if plane_count is correct
1839 * (multiplane buffers are not supported).
1840 */
1841 if (q->bufs[0]->num_planes != 1) {
1842 fileio->req.count = 0;
1843 ret = -EBUSY;
1844 goto err_reqbufs;
1845 }
1846
1847 /*
1848 * Get kernel address of each buffer.
1849 */
1850 for (i = 0; i < q->num_buffers; i++) {
1851 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1852 if (fileio->bufs[i].vaddr == NULL)
1853 goto err_reqbufs;
1854 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1855 }
1856
1857 /*
1858 * Read mode requires pre queuing of all buffers.
1859 */
1860 if (read) {
1861 /*
1862 * Queue all buffers.
1863 */
1864 for (i = 0; i < q->num_buffers; i++) {
1865 struct v4l2_buffer *b = &fileio->b;
1866 memset(b, 0, sizeof(*b));
1867 b->type = q->type;
1868 b->memory = q->memory;
1869 b->index = i;
1870 ret = vb2_qbuf(q, b);
1871 if (ret)
1872 goto err_reqbufs;
1873 fileio->bufs[i].queued = 1;
1874 }
1875
1876 /*
1877 * Start streaming.
1878 */
1879 ret = vb2_streamon(q, q->type);
1880 if (ret)
1881 goto err_reqbufs;
1882 }
1883
1884 q->fileio = fileio;
1885
1886 return ret;
1887
1888err_reqbufs:
1889 vb2_reqbufs(q, &fileio->req);
1890
1891err_kfree:
1892 kfree(fileio);
1893 return ret;
1894}
1895
1896/**
1897 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1898 * @q: videobuf2 queue
1899 */
1900static int __vb2_cleanup_fileio(struct vb2_queue *q)
1901{
1902 struct vb2_fileio_data *fileio = q->fileio;
1903
1904 if (fileio) {
1905 /*
1906 * Hack fileio context to enable direct calls to vb2 ioctl
1907 * interface.
1908 */
1909 q->fileio = NULL;
1910
1911 vb2_streamoff(q, q->type);
1912 fileio->req.count = 0;
1913 vb2_reqbufs(q, &fileio->req);
1914 kfree(fileio);
1915 dprintk(3, "file io emulator closed\n");
1916 }
1917 return 0;
1918}
1919
1920/**
1921 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1922 * @q: videobuf2 queue
1923 * @data: pointed to target userspace buffer
1924 * @count: number of bytes to read or write
1925 * @ppos: file handle position tracking pointer
1926 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1927 * @read: access mode selector (1 means read, 0 means write)
1928 */
1929static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1930 loff_t *ppos, int nonblock, int read)
1931{
1932 struct vb2_fileio_data *fileio;
1933 struct vb2_fileio_buf *buf;
1934 int ret, index;
1935
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001936 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001937 read ? "read" : "write", (long)*ppos, count,
1938 nonblock ? "non" : "");
1939
1940 if (!data)
1941 return -EINVAL;
1942
1943 /*
1944 * Initialize emulator on first call.
1945 */
1946 if (!q->fileio) {
1947 ret = __vb2_init_fileio(q, read);
1948 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1949 if (ret)
1950 return ret;
1951 }
1952 fileio = q->fileio;
1953
1954 /*
1955 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1956 * The pointer will be restored before returning from this function.
1957 */
1958 q->fileio = NULL;
1959
1960 index = fileio->index;
1961 buf = &fileio->bufs[index];
1962
1963 /*
1964 * Check if we need to dequeue the buffer.
1965 */
1966 if (buf->queued) {
1967 struct vb2_buffer *vb;
1968
1969 /*
1970 * Call vb2_dqbuf to get buffer back.
1971 */
1972 memset(&fileio->b, 0, sizeof(fileio->b));
1973 fileio->b.type = q->type;
1974 fileio->b.memory = q->memory;
1975 fileio->b.index = index;
1976 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1977 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1978 if (ret)
1979 goto end;
1980 fileio->dq_count += 1;
1981
1982 /*
1983 * Get number of bytes filled by the driver
1984 */
1985 vb = q->bufs[index];
1986 buf->size = vb2_get_plane_payload(vb, 0);
1987 buf->queued = 0;
1988 }
1989
1990 /*
1991 * Limit count on last few bytes of the buffer.
1992 */
1993 if (buf->pos + count > buf->size) {
1994 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001995 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001996 }
1997
1998 /*
1999 * Transfer data to userspace.
2000 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03002001 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03002002 count, index, buf->pos);
2003 if (read)
2004 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2005 else
2006 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2007 if (ret) {
2008 dprintk(3, "file io: error copying data\n");
2009 ret = -EFAULT;
2010 goto end;
2011 }
2012
2013 /*
2014 * Update counters.
2015 */
2016 buf->pos += count;
2017 *ppos += count;
2018
2019 /*
2020 * Queue next buffer if required.
2021 */
2022 if (buf->pos == buf->size ||
2023 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2024 /*
2025 * Check if this is the last buffer to read.
2026 */
2027 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2028 fileio->dq_count == 1) {
2029 dprintk(3, "file io: read limit reached\n");
2030 /*
2031 * Restore fileio pointer and release the context.
2032 */
2033 q->fileio = fileio;
2034 return __vb2_cleanup_fileio(q);
2035 }
2036
2037 /*
2038 * Call vb2_qbuf and give buffer to the driver.
2039 */
2040 memset(&fileio->b, 0, sizeof(fileio->b));
2041 fileio->b.type = q->type;
2042 fileio->b.memory = q->memory;
2043 fileio->b.index = index;
2044 fileio->b.bytesused = buf->pos;
2045 ret = vb2_qbuf(q, &fileio->b);
2046 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2047 if (ret)
2048 goto end;
2049
2050 /*
2051 * Buffer has been queued, update the status
2052 */
2053 buf->pos = 0;
2054 buf->queued = 1;
2055 buf->size = q->bufs[0]->v4l2_planes[0].length;
2056 fileio->q_count += 1;
2057
2058 /*
2059 * Switch to the next buffer
2060 */
2061 fileio->index = (index + 1) % q->num_buffers;
2062
2063 /*
2064 * Start streaming if required.
2065 */
2066 if (!read && !q->streaming) {
2067 ret = vb2_streamon(q, q->type);
2068 if (ret)
2069 goto end;
2070 }
2071 }
2072
2073 /*
2074 * Return proper number of bytes processed.
2075 */
2076 if (ret == 0)
2077 ret = count;
2078end:
2079 /*
2080 * Restore the fileio context and block vb2 ioctl interface.
2081 */
2082 q->fileio = fileio;
2083 return ret;
2084}
2085
2086size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2087 loff_t *ppos, int nonblocking)
2088{
2089 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2090}
2091EXPORT_SYMBOL_GPL(vb2_read);
2092
2093size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2094 loff_t *ppos, int nonblocking)
2095{
2096 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2097}
2098EXPORT_SYMBOL_GPL(vb2_write);
2099
Pawel Osciake23ccc02010-10-11 10:56:41 -03002100MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002101MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002102MODULE_LICENSE("GPL");