blob: e7c52ff3c76138eb94601e9304bd85c2402b1fb1 [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
33#define call_memop(q, plane, op, args...) \
34 (((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) {
55 mem_priv = call_memop(q, plane, 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 */
68 for (; plane > 0; --plane)
69 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
70
71 return -ENOMEM;
72}
73
74/**
75 * __vb2_buf_mem_free() - free memory of the given buffer
76 */
77static void __vb2_buf_mem_free(struct vb2_buffer *vb)
78{
79 struct vb2_queue *q = vb->vb2_queue;
80 unsigned int plane;
81
82 for (plane = 0; plane < vb->num_planes; ++plane) {
83 call_memop(q, plane, put, vb->planes[plane].mem_priv);
84 vb->planes[plane].mem_priv = NULL;
85 dprintk(3, "Freed plane %d of buffer %d\n",
86 plane, vb->v4l2_buf.index);
87 }
88}
89
90/**
91 * __vb2_buf_userptr_put() - release userspace memory associated with
92 * a USERPTR buffer
93 */
94static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
95{
96 struct vb2_queue *q = vb->vb2_queue;
97 unsigned int plane;
98
99 for (plane = 0; plane < vb->num_planes; ++plane) {
100 void *mem_priv = vb->planes[plane].mem_priv;
101
102 if (mem_priv) {
103 call_memop(q, plane, put_userptr, mem_priv);
104 vb->planes[plane].mem_priv = NULL;
105 }
106 }
107}
108
109/**
110 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
111 * every buffer on the queue
112 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300113static void __setup_offsets(struct vb2_queue *q, unsigned int n)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300114{
115 unsigned int buffer, plane;
116 struct vb2_buffer *vb;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300117 unsigned long off;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300118
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300119 if (q->num_buffers) {
120 struct v4l2_plane *p;
121 vb = q->bufs[q->num_buffers - 1];
122 p = &vb->v4l2_planes[vb->num_planes - 1];
123 off = PAGE_ALIGN(p->m.mem_offset + p->length);
124 } else {
125 off = 0;
126 }
127
128 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300129 vb = q->bufs[buffer];
130 if (!vb)
131 continue;
132
133 for (plane = 0; plane < vb->num_planes; ++plane) {
Marek Szyprowski49076022011-10-13 07:07:24 -0300134 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -0300135 vb->v4l2_planes[plane].m.mem_offset = off;
136
137 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
138 buffer, plane, off);
139
140 off += vb->v4l2_planes[plane].length;
141 off = PAGE_ALIGN(off);
142 }
143 }
144}
145
146/**
147 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
148 * video buffer memory for all buffers/planes on the queue and initializes the
149 * queue
150 *
151 * Returns the number of buffers successfully allocated.
152 */
153static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300154 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300155{
156 unsigned int buffer;
157 struct vb2_buffer *vb;
158 int ret;
159
160 for (buffer = 0; buffer < num_buffers; ++buffer) {
161 /* Allocate videobuf buffer structures */
162 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
163 if (!vb) {
164 dprintk(1, "Memory alloc for buffer struct failed\n");
165 break;
166 }
167
168 /* Length stores number of planes for multiplanar buffers */
169 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
170 vb->v4l2_buf.length = num_planes;
171
172 vb->state = VB2_BUF_STATE_DEQUEUED;
173 vb->vb2_queue = q;
174 vb->num_planes = num_planes;
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300175 vb->v4l2_buf.index = q->num_buffers + buffer;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300176 vb->v4l2_buf.type = q->type;
177 vb->v4l2_buf.memory = memory;
178
179 /* Allocate video buffer memory for the MMAP type */
180 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300181 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300182 if (ret) {
183 dprintk(1, "Failed allocating memory for "
184 "buffer %d\n", buffer);
185 kfree(vb);
186 break;
187 }
188 /*
189 * Call the driver-provided buffer initialization
190 * callback, if given. An error in initialization
191 * results in queue setup failure.
192 */
193 ret = call_qop(q, buf_init, vb);
194 if (ret) {
195 dprintk(1, "Buffer %d %p initialization"
196 " failed\n", buffer, vb);
197 __vb2_buf_mem_free(vb);
198 kfree(vb);
199 break;
200 }
201 }
202
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300203 q->bufs[q->num_buffers + buffer] = vb;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300204 }
205
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300206 __setup_offsets(q, buffer);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300207
208 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300209 buffer, num_planes);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300210
211 return buffer;
212}
213
214/**
215 * __vb2_free_mem() - release all video buffer memory for a given queue
216 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300217static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300218{
219 unsigned int buffer;
220 struct vb2_buffer *vb;
221
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300222 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
223 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300224 vb = q->bufs[buffer];
225 if (!vb)
226 continue;
227
228 /* Free MMAP buffers or release USERPTR buffers */
229 if (q->memory == V4L2_MEMORY_MMAP)
230 __vb2_buf_mem_free(vb);
231 else
232 __vb2_buf_userptr_put(vb);
233 }
234}
235
236/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300237 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
238 * related information, if no buffers are left return the queue to an
239 * uninitialized state. Might be called even if the queue has already been freed.
Pawel Osciake23ccc02010-10-11 10:56:41 -0300240 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300241static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300242{
243 unsigned int buffer;
244
245 /* Call driver-provided cleanup function for each buffer, if provided */
246 if (q->ops->buf_cleanup) {
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300247 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
248 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300249 if (NULL == q->bufs[buffer])
250 continue;
251 q->ops->buf_cleanup(q->bufs[buffer]);
252 }
253 }
254
255 /* Release video buffer memory */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300256 __vb2_free_mem(q, buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300257
258 /* Free videobuf buffers */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300259 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
260 ++buffer) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300261 kfree(q->bufs[buffer]);
262 q->bufs[buffer] = NULL;
263 }
264
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300265 q->num_buffers -= buffers;
266 if (!q->num_buffers)
267 q->memory = 0;
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 Szyprowski2c2dd6ac2011-10-12 13:09:53 -0300307 if (mem_priv && call_memop(q, plane, 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
733 if (plane_no > vb->num_planes)
734 return NULL;
735
736 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
737
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
756 if (plane_no > vb->num_planes)
757 return NULL;
758
759 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
760}
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 */
885 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
886 && vb->v4l2_planes[plane].length == planes[plane].length)
887 continue;
888
889 dprintk(3, "qbuf: userspace address for plane %d changed, "
890 "reacquiring memory\n", plane);
891
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300892 /* Check if the provided plane buffer is large enough */
893 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300894 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300895 goto err;
896 }
897
Pawel Osciake23ccc02010-10-11 10:56:41 -0300898 /* Release previously acquired memory if present */
899 if (vb->planes[plane].mem_priv)
900 call_memop(q, plane, put_userptr,
901 vb->planes[plane].mem_priv);
902
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 */
908 if (q->mem_ops->get_userptr) {
909 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
910 planes[plane].m.userptr,
911 planes[plane].length,
912 write);
913 if (IS_ERR(mem_priv)) {
914 dprintk(1, "qbuf: failed acquiring userspace "
915 "memory for plane %d\n", plane);
916 ret = PTR_ERR(mem_priv);
917 goto err;
918 }
919 vb->planes[plane].mem_priv = mem_priv;
920 }
921 }
922
923 /*
924 * Call driver-specific initialization on the newly acquired buffer,
925 * if provided.
926 */
927 ret = call_qop(q, buf_init, vb);
928 if (ret) {
929 dprintk(1, "qbuf: buffer initialization failed\n");
930 goto err;
931 }
932
933 /*
934 * Now that everything is in order, copy relevant information
935 * provided by userspace.
936 */
937 for (plane = 0; plane < vb->num_planes; ++plane)
938 vb->v4l2_planes[plane] = planes[plane];
939
940 return 0;
941err:
942 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300943 for (plane = 0; plane < vb->num_planes; ++plane) {
944 if (vb->planes[plane].mem_priv)
945 call_memop(q, plane, put_userptr,
946 vb->planes[plane].mem_priv);
947 vb->planes[plane].mem_priv = NULL;
948 vb->v4l2_planes[plane].m.userptr = 0;
949 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300950 }
951
952 return ret;
953}
954
955/**
956 * __qbuf_mmap() - handle qbuf of an MMAP buffer
957 */
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300958static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300959{
960 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
961}
962
963/**
964 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
965 */
966static void __enqueue_in_driver(struct vb2_buffer *vb)
967{
968 struct vb2_queue *q = vb->vb2_queue;
969
970 vb->state = VB2_BUF_STATE_ACTIVE;
971 atomic_inc(&q->queued_count);
972 q->ops->buf_queue(vb);
973}
974
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -0300975static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300976{
977 struct vb2_queue *q = vb->vb2_queue;
978 int ret;
979
980 switch (q->memory) {
981 case V4L2_MEMORY_MMAP:
982 ret = __qbuf_mmap(vb, b);
983 break;
984 case V4L2_MEMORY_USERPTR:
985 ret = __qbuf_userptr(vb, b);
986 break;
987 default:
988 WARN(1, "Invalid queue type\n");
989 ret = -EINVAL;
990 }
991
992 if (!ret)
993 ret = call_qop(q, buf_prepare, vb);
994 if (ret)
995 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
996 else
997 vb->state = VB2_BUF_STATE_PREPARED;
998
999 return ret;
1000}
1001
Pawel Osciake23ccc02010-10-11 10:56:41 -03001002/**
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001003 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1004 * @q: videobuf2 queue
1005 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1006 * handler in driver
1007 *
1008 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1009 * This function:
1010 * 1) verifies the passed buffer,
1011 * 2) calls buf_prepare callback in the driver (if provided), in which
1012 * driver-specific buffer initialization can be performed,
1013 *
1014 * The return values from this function are intended to be directly returned
1015 * from vidioc_prepare_buf handler in driver.
1016 */
1017int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
1018{
1019 struct vb2_buffer *vb;
1020 int ret;
1021
1022 if (q->fileio) {
1023 dprintk(1, "%s(): file io in progress\n", __func__);
1024 return -EBUSY;
1025 }
1026
1027 if (b->type != q->type) {
1028 dprintk(1, "%s(): invalid buffer type\n", __func__);
1029 return -EINVAL;
1030 }
1031
1032 if (b->index >= q->num_buffers) {
1033 dprintk(1, "%s(): buffer index out of range\n", __func__);
1034 return -EINVAL;
1035 }
1036
1037 vb = q->bufs[b->index];
1038 if (NULL == vb) {
1039 /* Should never happen */
1040 dprintk(1, "%s(): buffer is NULL\n", __func__);
1041 return -EINVAL;
1042 }
1043
1044 if (b->memory != q->memory) {
1045 dprintk(1, "%s(): invalid memory type\n", __func__);
1046 return -EINVAL;
1047 }
1048
1049 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1050 dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
1051 return -EINVAL;
1052 }
1053
1054 ret = __buf_prepare(vb, b);
1055 if (ret < 0)
1056 return ret;
1057
1058 __fill_v4l2_buffer(vb, b);
1059
1060 return 0;
1061}
1062EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1063
1064/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001065 * vb2_qbuf() - Queue a buffer from userspace
1066 * @q: videobuf2 queue
1067 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1068 * in driver
1069 *
1070 * Should be called from vidioc_qbuf ioctl handler of a driver.
1071 * This function:
1072 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001073 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1074 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -03001075 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1076 * callback for processing.
1077 *
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_qbuf handler in driver.
1080 */
1081int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1082{
1083 struct vb2_buffer *vb;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001084 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001085
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001086 if (q->fileio) {
1087 dprintk(1, "qbuf: file io in progress\n");
1088 return -EBUSY;
1089 }
1090
Pawel Osciake23ccc02010-10-11 10:56:41 -03001091 if (b->type != q->type) {
1092 dprintk(1, "qbuf: invalid buffer type\n");
1093 return -EINVAL;
1094 }
1095
1096 if (b->index >= q->num_buffers) {
1097 dprintk(1, "qbuf: buffer index out of range\n");
1098 return -EINVAL;
1099 }
1100
1101 vb = q->bufs[b->index];
1102 if (NULL == vb) {
1103 /* Should never happen */
1104 dprintk(1, "qbuf: buffer is NULL\n");
1105 return -EINVAL;
1106 }
1107
1108 if (b->memory != q->memory) {
1109 dprintk(1, "qbuf: invalid memory type\n");
1110 return -EINVAL;
1111 }
1112
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -03001113 switch (vb->state) {
1114 case VB2_BUF_STATE_DEQUEUED:
1115 ret = __buf_prepare(vb, b);
1116 if (ret)
1117 return ret;
1118 case VB2_BUF_STATE_PREPARED:
1119 break;
1120 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -03001121 dprintk(1, "qbuf: buffer already in use\n");
1122 return -EINVAL;
1123 }
1124
Pawel Osciake23ccc02010-10-11 10:56:41 -03001125 /*
1126 * Add to the queued buffers list, a buffer will stay on it until
1127 * dequeued in dqbuf.
1128 */
1129 list_add_tail(&vb->queued_entry, &q->queued_list);
1130 vb->state = VB2_BUF_STATE_QUEUED;
1131
1132 /*
1133 * If already streaming, give the buffer to driver for processing.
1134 * If not, the buffer will be given to driver on next streamon.
1135 */
1136 if (q->streaming)
1137 __enqueue_in_driver(vb);
1138
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -03001139 /* Fill buffer information for the userspace */
1140 __fill_v4l2_buffer(vb, b);
1141
Pawel Osciake23ccc02010-10-11 10:56:41 -03001142 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
1143 return 0;
1144}
1145EXPORT_SYMBOL_GPL(vb2_qbuf);
1146
1147/**
1148 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1149 * for dequeuing
1150 *
1151 * Will sleep if required for nonblocking == false.
1152 */
1153static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1154{
1155 /*
1156 * All operations on vb_done_list are performed under done_lock
1157 * spinlock protection. However, buffers may be removed from
1158 * it and returned to userspace only while holding both driver's
1159 * lock and the done_lock spinlock. Thus we can be sure that as
1160 * long as we hold the driver's lock, the list will remain not
1161 * empty if list_empty() check succeeds.
1162 */
1163
1164 for (;;) {
1165 int ret;
1166
1167 if (!q->streaming) {
1168 dprintk(1, "Streaming off, will not wait for buffers\n");
1169 return -EINVAL;
1170 }
1171
1172 if (!list_empty(&q->done_list)) {
1173 /*
1174 * Found a buffer that we were waiting for.
1175 */
1176 break;
1177 }
1178
1179 if (nonblocking) {
1180 dprintk(1, "Nonblocking and no buffers to dequeue, "
1181 "will not wait\n");
1182 return -EAGAIN;
1183 }
1184
1185 /*
1186 * We are streaming and blocking, wait for another buffer to
1187 * become ready or for streamoff. Driver's lock is released to
1188 * allow streamoff or qbuf to be called while waiting.
1189 */
1190 call_qop(q, wait_prepare, q);
1191
1192 /*
1193 * All locks have been released, it is safe to sleep now.
1194 */
1195 dprintk(3, "Will sleep waiting for buffers\n");
1196 ret = wait_event_interruptible(q->done_wq,
1197 !list_empty(&q->done_list) || !q->streaming);
1198
1199 /*
1200 * We need to reevaluate both conditions again after reacquiring
1201 * the locks or return an error if one occurred.
1202 */
1203 call_qop(q, wait_finish, q);
1204 if (ret)
1205 return ret;
1206 }
1207 return 0;
1208}
1209
1210/**
1211 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1212 *
1213 * Will sleep if required for nonblocking == false.
1214 */
1215static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1216 int nonblocking)
1217{
1218 unsigned long flags;
1219 int ret;
1220
1221 /*
1222 * Wait for at least one buffer to become available on the done_list.
1223 */
1224 ret = __vb2_wait_for_done_vb(q, nonblocking);
1225 if (ret)
1226 return ret;
1227
1228 /*
1229 * Driver's lock has been held since we last verified that done_list
1230 * is not empty, so no need for another list_empty(done_list) check.
1231 */
1232 spin_lock_irqsave(&q->done_lock, flags);
1233 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1234 list_del(&(*vb)->done_entry);
1235 spin_unlock_irqrestore(&q->done_lock, flags);
1236
1237 return 0;
1238}
1239
1240/**
1241 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1242 * @q: videobuf2 queue
1243 *
1244 * This function will wait until all buffers that have been given to the driver
1245 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1246 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1247 * taken, for example from stop_streaming() callback.
1248 */
1249int vb2_wait_for_all_buffers(struct vb2_queue *q)
1250{
1251 if (!q->streaming) {
1252 dprintk(1, "Streaming off, will not wait for buffers\n");
1253 return -EINVAL;
1254 }
1255
1256 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1257 return 0;
1258}
1259EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1260
1261/**
1262 * vb2_dqbuf() - Dequeue a buffer to the userspace
1263 * @q: videobuf2 queue
1264 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1265 * in driver
1266 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1267 * buffers ready for dequeuing are present. Normally the driver
1268 * would be passing (file->f_flags & O_NONBLOCK) here
1269 *
1270 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1271 * This function:
1272 * 1) verifies the passed buffer,
1273 * 2) calls buf_finish callback in the driver (if provided), in which
1274 * driver can perform any additional operations that may be required before
1275 * returning the buffer to userspace, such as cache sync,
1276 * 3) the buffer struct members are filled with relevant information for
1277 * the userspace.
1278 *
1279 * The return values from this function are intended to be directly returned
1280 * from vidioc_dqbuf handler in driver.
1281 */
1282int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1283{
1284 struct vb2_buffer *vb = NULL;
1285 int ret;
1286
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001287 if (q->fileio) {
1288 dprintk(1, "dqbuf: file io in progress\n");
1289 return -EBUSY;
1290 }
1291
Pawel Osciake23ccc02010-10-11 10:56:41 -03001292 if (b->type != q->type) {
1293 dprintk(1, "dqbuf: invalid buffer type\n");
1294 return -EINVAL;
1295 }
1296
1297 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1298 if (ret < 0) {
1299 dprintk(1, "dqbuf: error getting next done buffer\n");
1300 return ret;
1301 }
1302
1303 ret = call_qop(q, buf_finish, vb);
1304 if (ret) {
1305 dprintk(1, "dqbuf: buffer finish failed\n");
1306 return ret;
1307 }
1308
1309 switch (vb->state) {
1310 case VB2_BUF_STATE_DONE:
1311 dprintk(3, "dqbuf: Returning done buffer\n");
1312 break;
1313 case VB2_BUF_STATE_ERROR:
1314 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1315 break;
1316 default:
1317 dprintk(1, "dqbuf: Invalid buffer state\n");
1318 return -EINVAL;
1319 }
1320
1321 /* Fill buffer information for the userspace */
1322 __fill_v4l2_buffer(vb, b);
1323 /* Remove from videobuf queue */
1324 list_del(&vb->queued_entry);
1325
1326 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1327 vb->v4l2_buf.index, vb->state);
1328
1329 vb->state = VB2_BUF_STATE_DEQUEUED;
1330 return 0;
1331}
1332EXPORT_SYMBOL_GPL(vb2_dqbuf);
1333
1334/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001335 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1336 *
1337 * Removes all queued buffers from driver's queue and all buffers queued by
1338 * userspace from videobuf's queue. Returns to state after reqbufs.
1339 */
1340static void __vb2_queue_cancel(struct vb2_queue *q)
1341{
1342 unsigned int i;
1343
1344 /*
1345 * Tell driver to stop all transactions and release all queued
1346 * buffers.
1347 */
1348 if (q->streaming)
1349 call_qop(q, stop_streaming, q);
1350 q->streaming = 0;
1351
1352 /*
1353 * Remove all buffers from videobuf's list...
1354 */
1355 INIT_LIST_HEAD(&q->queued_list);
1356 /*
1357 * ...and done list; userspace will not receive any buffers it
1358 * has not already dequeued before initiating cancel.
1359 */
1360 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001361 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001362 wake_up_all(&q->done_wq);
1363
1364 /*
1365 * Reinitialize all buffers for next use.
1366 */
1367 for (i = 0; i < q->num_buffers; ++i)
1368 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1369}
1370
1371/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001372 * vb2_streamon - start streaming
1373 * @q: videobuf2 queue
1374 * @type: type argument passed from userspace to vidioc_streamon handler
1375 *
1376 * Should be called from vidioc_streamon handler of a driver.
1377 * This function:
1378 * 1) verifies current state
1379 * 2) passes any previously queued buffers to the driver and starts streaming
1380 *
1381 * The return values from this function are intended to be directly returned
1382 * from vidioc_streamon handler in the driver.
1383 */
1384int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1385{
1386 struct vb2_buffer *vb;
1387 int ret;
1388
1389 if (q->fileio) {
1390 dprintk(1, "streamon: file io in progress\n");
1391 return -EBUSY;
1392 }
1393
1394 if (type != q->type) {
1395 dprintk(1, "streamon: invalid stream type\n");
1396 return -EINVAL;
1397 }
1398
1399 if (q->streaming) {
1400 dprintk(1, "streamon: already streaming\n");
1401 return -EBUSY;
1402 }
1403
1404 /*
1405 * If any buffers were queued before streamon,
1406 * we can now pass them to driver for processing.
1407 */
1408 list_for_each_entry(vb, &q->queued_list, queued_entry)
1409 __enqueue_in_driver(vb);
1410
1411 /*
1412 * Let driver notice that streaming state has been enabled.
1413 */
1414 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1415 if (ret) {
1416 dprintk(1, "streamon: driver refused to start streaming\n");
1417 __vb2_queue_cancel(q);
1418 return ret;
1419 }
1420
1421 q->streaming = 1;
1422
1423 dprintk(3, "Streamon successful\n");
1424 return 0;
1425}
1426EXPORT_SYMBOL_GPL(vb2_streamon);
1427
1428
1429/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001430 * vb2_streamoff - stop streaming
1431 * @q: videobuf2 queue
1432 * @type: type argument passed from userspace to vidioc_streamoff handler
1433 *
1434 * Should be called from vidioc_streamoff handler of a driver.
1435 * This function:
1436 * 1) verifies current state,
1437 * 2) stop streaming and dequeues any queued buffers, including those previously
1438 * passed to the driver (after waiting for the driver to finish).
1439 *
1440 * This call can be used for pausing playback.
1441 * The return values from this function are intended to be directly returned
1442 * from vidioc_streamoff handler in the driver
1443 */
1444int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1445{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001446 if (q->fileio) {
1447 dprintk(1, "streamoff: file io in progress\n");
1448 return -EBUSY;
1449 }
1450
Pawel Osciake23ccc02010-10-11 10:56:41 -03001451 if (type != q->type) {
1452 dprintk(1, "streamoff: invalid stream type\n");
1453 return -EINVAL;
1454 }
1455
1456 if (!q->streaming) {
1457 dprintk(1, "streamoff: not streaming\n");
1458 return -EINVAL;
1459 }
1460
1461 /*
1462 * Cancel will pause streaming and remove all buffers from the driver
1463 * and videobuf, effectively returning control over them to userspace.
1464 */
1465 __vb2_queue_cancel(q);
1466
1467 dprintk(3, "Streamoff successful\n");
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(vb2_streamoff);
1471
1472/**
1473 * __find_plane_by_offset() - find plane associated with the given offset off
1474 */
1475static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1476 unsigned int *_buffer, unsigned int *_plane)
1477{
1478 struct vb2_buffer *vb;
1479 unsigned int buffer, plane;
1480
1481 /*
1482 * Go over all buffers and their planes, comparing the given offset
1483 * with an offset assigned to each plane. If a match is found,
1484 * return its buffer and plane numbers.
1485 */
1486 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1487 vb = q->bufs[buffer];
1488
1489 for (plane = 0; plane < vb->num_planes; ++plane) {
1490 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1491 *_buffer = buffer;
1492 *_plane = plane;
1493 return 0;
1494 }
1495 }
1496 }
1497
1498 return -EINVAL;
1499}
1500
1501/**
1502 * vb2_mmap() - map video buffers into application address space
1503 * @q: videobuf2 queue
1504 * @vma: vma passed to the mmap file operation handler in the driver
1505 *
1506 * Should be called from mmap file operation handler of a driver.
1507 * This function maps one plane of one of the available video buffers to
1508 * userspace. To map whole video memory allocated on reqbufs, this function
1509 * has to be called once per each plane per each buffer previously allocated.
1510 *
1511 * When the userspace application calls mmap, it passes to it an offset returned
1512 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1513 * a "cookie", which is then used to identify the plane to be mapped.
1514 * This function finds a plane with a matching offset and a mapping is performed
1515 * by the means of a provided memory operation.
1516 *
1517 * The return values from this function are intended to be directly returned
1518 * from the mmap handler in driver.
1519 */
1520int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1521{
1522 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1523 struct vb2_plane *vb_plane;
1524 struct vb2_buffer *vb;
1525 unsigned int buffer, plane;
1526 int ret;
1527
1528 if (q->memory != V4L2_MEMORY_MMAP) {
1529 dprintk(1, "Queue is not currently set up for mmap\n");
1530 return -EINVAL;
1531 }
1532
1533 /*
1534 * Check memory area access mode.
1535 */
1536 if (!(vma->vm_flags & VM_SHARED)) {
1537 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1538 return -EINVAL;
1539 }
1540 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1541 if (!(vma->vm_flags & VM_WRITE)) {
1542 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1543 return -EINVAL;
1544 }
1545 } else {
1546 if (!(vma->vm_flags & VM_READ)) {
1547 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1548 return -EINVAL;
1549 }
1550 }
1551
1552 /*
1553 * Find the plane corresponding to the offset passed by userspace.
1554 */
1555 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1556 if (ret)
1557 return ret;
1558
1559 vb = q->bufs[buffer];
1560 vb_plane = &vb->planes[plane];
1561
1562 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1563 if (ret)
1564 return ret;
1565
Pawel Osciake23ccc02010-10-11 10:56:41 -03001566 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1567 return 0;
1568}
1569EXPORT_SYMBOL_GPL(vb2_mmap);
1570
Scott Jiang6f524ec2011-09-21 09:25:23 -03001571#ifndef CONFIG_MMU
1572unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1573 unsigned long addr,
1574 unsigned long len,
1575 unsigned long pgoff,
1576 unsigned long flags)
1577{
1578 unsigned long off = pgoff << PAGE_SHIFT;
1579 struct vb2_buffer *vb;
1580 unsigned int buffer, plane;
1581 int ret;
1582
1583 if (q->memory != V4L2_MEMORY_MMAP) {
1584 dprintk(1, "Queue is not currently set up for mmap\n");
1585 return -EINVAL;
1586 }
1587
1588 /*
1589 * Find the plane corresponding to the offset passed by userspace.
1590 */
1591 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1592 if (ret)
1593 return ret;
1594
1595 vb = q->bufs[buffer];
1596
1597 return (unsigned long)vb2_plane_vaddr(vb, plane);
1598}
1599EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1600#endif
1601
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001602static int __vb2_init_fileio(struct vb2_queue *q, int read);
1603static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001604
1605/**
1606 * vb2_poll() - implements poll userspace operation
1607 * @q: videobuf2 queue
1608 * @file: file argument passed to the poll file operation handler
1609 * @wait: wait argument passed to the poll file operation handler
1610 *
1611 * This function implements poll file operation handler for a driver.
1612 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1613 * be informed that the file descriptor of a video device is available for
1614 * reading.
1615 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1616 * will be reported as available for writing.
1617 *
1618 * The return values from this function are intended to be directly returned
1619 * from poll handler in driver.
1620 */
1621unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1622{
1623 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001624 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001625 struct vb2_buffer *vb = NULL;
1626
1627 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001628 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001629 */
1630 if (q->num_buffers == 0 && q->fileio == NULL) {
1631 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1632 ret = __vb2_init_fileio(q, 1);
1633 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001634 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001635 }
1636 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1637 ret = __vb2_init_fileio(q, 0);
1638 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001639 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001640 /*
1641 * Write to OUTPUT queue can be done immediately.
1642 */
1643 return POLLOUT | POLLWRNORM;
1644 }
1645 }
1646
1647 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001648 * There is nothing to wait for if no buffers have already been queued.
1649 */
1650 if (list_empty(&q->queued_list))
1651 return POLLERR;
1652
1653 poll_wait(file, &q->done_wq, wait);
1654
1655 /*
1656 * Take first buffer available for dequeuing.
1657 */
1658 spin_lock_irqsave(&q->done_lock, flags);
1659 if (!list_empty(&q->done_list))
1660 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1661 done_entry);
1662 spin_unlock_irqrestore(&q->done_lock, flags);
1663
1664 if (vb && (vb->state == VB2_BUF_STATE_DONE
1665 || vb->state == VB2_BUF_STATE_ERROR)) {
1666 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1667 POLLIN | POLLRDNORM;
1668 }
1669 return 0;
1670}
1671EXPORT_SYMBOL_GPL(vb2_poll);
1672
1673/**
1674 * vb2_queue_init() - initialize a videobuf2 queue
1675 * @q: videobuf2 queue; this structure should be allocated in driver
1676 *
1677 * The vb2_queue structure should be allocated by the driver. The driver is
1678 * responsible of clearing it's content and setting initial values for some
1679 * required entries before calling this function.
1680 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1681 * to the struct vb2_queue description in include/media/videobuf2-core.h
1682 * for more information.
1683 */
1684int vb2_queue_init(struct vb2_queue *q)
1685{
1686 BUG_ON(!q);
1687 BUG_ON(!q->ops);
1688 BUG_ON(!q->mem_ops);
1689 BUG_ON(!q->type);
1690 BUG_ON(!q->io_modes);
1691
1692 BUG_ON(!q->ops->queue_setup);
1693 BUG_ON(!q->ops->buf_queue);
1694
1695 INIT_LIST_HEAD(&q->queued_list);
1696 INIT_LIST_HEAD(&q->done_list);
1697 spin_lock_init(&q->done_lock);
1698 init_waitqueue_head(&q->done_wq);
1699
1700 if (q->buf_struct_size == 0)
1701 q->buf_struct_size = sizeof(struct vb2_buffer);
1702
1703 return 0;
1704}
1705EXPORT_SYMBOL_GPL(vb2_queue_init);
1706
1707/**
1708 * vb2_queue_release() - stop streaming, release the queue and free memory
1709 * @q: videobuf2 queue
1710 *
1711 * This function stops streaming and performs necessary clean ups, including
1712 * freeing video buffer memory. The driver is responsible for freeing
1713 * the vb2_queue structure itself.
1714 */
1715void vb2_queue_release(struct vb2_queue *q)
1716{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001717 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001718 __vb2_queue_cancel(q);
Guennadi Liakhovetski2d864012011-09-28 09:23:02 -03001719 __vb2_queue_free(q, q->num_buffers);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001720}
1721EXPORT_SYMBOL_GPL(vb2_queue_release);
1722
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001723/**
1724 * struct vb2_fileio_buf - buffer context used by file io emulator
1725 *
1726 * vb2 provides a compatibility layer and emulator of file io (read and
1727 * write) calls on top of streaming API. This structure is used for
1728 * tracking context related to the buffers.
1729 */
1730struct vb2_fileio_buf {
1731 void *vaddr;
1732 unsigned int size;
1733 unsigned int pos;
1734 unsigned int queued:1;
1735};
1736
1737/**
1738 * struct vb2_fileio_data - queue context used by file io emulator
1739 *
1740 * vb2 provides a compatibility layer and emulator of file io (read and
1741 * write) calls on top of streaming API. For proper operation it required
1742 * this structure to save the driver state between each call of the read
1743 * or write function.
1744 */
1745struct vb2_fileio_data {
1746 struct v4l2_requestbuffers req;
1747 struct v4l2_buffer b;
1748 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1749 unsigned int index;
1750 unsigned int q_count;
1751 unsigned int dq_count;
1752 unsigned int flags;
1753};
1754
1755/**
1756 * __vb2_init_fileio() - initialize file io emulator
1757 * @q: videobuf2 queue
1758 * @read: mode selector (1 means read, 0 means write)
1759 */
1760static int __vb2_init_fileio(struct vb2_queue *q, int read)
1761{
1762 struct vb2_fileio_data *fileio;
1763 int i, ret;
1764 unsigned int count = 0;
1765
1766 /*
1767 * Sanity check
1768 */
1769 if ((read && !(q->io_modes & VB2_READ)) ||
1770 (!read && !(q->io_modes & VB2_WRITE)))
1771 BUG();
1772
1773 /*
1774 * Check if device supports mapping buffers to kernel virtual space.
1775 */
1776 if (!q->mem_ops->vaddr)
1777 return -EBUSY;
1778
1779 /*
1780 * Check if streaming api has not been already activated.
1781 */
1782 if (q->streaming || q->num_buffers > 0)
1783 return -EBUSY;
1784
1785 /*
1786 * Start with count 1, driver can increase it in queue_setup()
1787 */
1788 count = 1;
1789
1790 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1791 (read) ? "read" : "write", count, q->io_flags);
1792
1793 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1794 if (fileio == NULL)
1795 return -ENOMEM;
1796
1797 fileio->flags = q->io_flags;
1798
1799 /*
1800 * Request buffers and use MMAP type to force driver
1801 * to allocate buffers by itself.
1802 */
1803 fileio->req.count = count;
1804 fileio->req.memory = V4L2_MEMORY_MMAP;
1805 fileio->req.type = q->type;
1806 ret = vb2_reqbufs(q, &fileio->req);
1807 if (ret)
1808 goto err_kfree;
1809
1810 /*
1811 * Check if plane_count is correct
1812 * (multiplane buffers are not supported).
1813 */
1814 if (q->bufs[0]->num_planes != 1) {
1815 fileio->req.count = 0;
1816 ret = -EBUSY;
1817 goto err_reqbufs;
1818 }
1819
1820 /*
1821 * Get kernel address of each buffer.
1822 */
1823 for (i = 0; i < q->num_buffers; i++) {
1824 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1825 if (fileio->bufs[i].vaddr == NULL)
1826 goto err_reqbufs;
1827 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1828 }
1829
1830 /*
1831 * Read mode requires pre queuing of all buffers.
1832 */
1833 if (read) {
1834 /*
1835 * Queue all buffers.
1836 */
1837 for (i = 0; i < q->num_buffers; i++) {
1838 struct v4l2_buffer *b = &fileio->b;
1839 memset(b, 0, sizeof(*b));
1840 b->type = q->type;
1841 b->memory = q->memory;
1842 b->index = i;
1843 ret = vb2_qbuf(q, b);
1844 if (ret)
1845 goto err_reqbufs;
1846 fileio->bufs[i].queued = 1;
1847 }
1848
1849 /*
1850 * Start streaming.
1851 */
1852 ret = vb2_streamon(q, q->type);
1853 if (ret)
1854 goto err_reqbufs;
1855 }
1856
1857 q->fileio = fileio;
1858
1859 return ret;
1860
1861err_reqbufs:
1862 vb2_reqbufs(q, &fileio->req);
1863
1864err_kfree:
1865 kfree(fileio);
1866 return ret;
1867}
1868
1869/**
1870 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1871 * @q: videobuf2 queue
1872 */
1873static int __vb2_cleanup_fileio(struct vb2_queue *q)
1874{
1875 struct vb2_fileio_data *fileio = q->fileio;
1876
1877 if (fileio) {
1878 /*
1879 * Hack fileio context to enable direct calls to vb2 ioctl
1880 * interface.
1881 */
1882 q->fileio = NULL;
1883
1884 vb2_streamoff(q, q->type);
1885 fileio->req.count = 0;
1886 vb2_reqbufs(q, &fileio->req);
1887 kfree(fileio);
1888 dprintk(3, "file io emulator closed\n");
1889 }
1890 return 0;
1891}
1892
1893/**
1894 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1895 * @q: videobuf2 queue
1896 * @data: pointed to target userspace buffer
1897 * @count: number of bytes to read or write
1898 * @ppos: file handle position tracking pointer
1899 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1900 * @read: access mode selector (1 means read, 0 means write)
1901 */
1902static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1903 loff_t *ppos, int nonblock, int read)
1904{
1905 struct vb2_fileio_data *fileio;
1906 struct vb2_fileio_buf *buf;
1907 int ret, index;
1908
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001909 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001910 read ? "read" : "write", (long)*ppos, count,
1911 nonblock ? "non" : "");
1912
1913 if (!data)
1914 return -EINVAL;
1915
1916 /*
1917 * Initialize emulator on first call.
1918 */
1919 if (!q->fileio) {
1920 ret = __vb2_init_fileio(q, read);
1921 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1922 if (ret)
1923 return ret;
1924 }
1925 fileio = q->fileio;
1926
1927 /*
1928 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1929 * The pointer will be restored before returning from this function.
1930 */
1931 q->fileio = NULL;
1932
1933 index = fileio->index;
1934 buf = &fileio->bufs[index];
1935
1936 /*
1937 * Check if we need to dequeue the buffer.
1938 */
1939 if (buf->queued) {
1940 struct vb2_buffer *vb;
1941
1942 /*
1943 * Call vb2_dqbuf to get buffer back.
1944 */
1945 memset(&fileio->b, 0, sizeof(fileio->b));
1946 fileio->b.type = q->type;
1947 fileio->b.memory = q->memory;
1948 fileio->b.index = index;
1949 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1950 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1951 if (ret)
1952 goto end;
1953 fileio->dq_count += 1;
1954
1955 /*
1956 * Get number of bytes filled by the driver
1957 */
1958 vb = q->bufs[index];
1959 buf->size = vb2_get_plane_payload(vb, 0);
1960 buf->queued = 0;
1961 }
1962
1963 /*
1964 * Limit count on last few bytes of the buffer.
1965 */
1966 if (buf->pos + count > buf->size) {
1967 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001968 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001969 }
1970
1971 /*
1972 * Transfer data to userspace.
1973 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001974 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001975 count, index, buf->pos);
1976 if (read)
1977 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1978 else
1979 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1980 if (ret) {
1981 dprintk(3, "file io: error copying data\n");
1982 ret = -EFAULT;
1983 goto end;
1984 }
1985
1986 /*
1987 * Update counters.
1988 */
1989 buf->pos += count;
1990 *ppos += count;
1991
1992 /*
1993 * Queue next buffer if required.
1994 */
1995 if (buf->pos == buf->size ||
1996 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1997 /*
1998 * Check if this is the last buffer to read.
1999 */
2000 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2001 fileio->dq_count == 1) {
2002 dprintk(3, "file io: read limit reached\n");
2003 /*
2004 * Restore fileio pointer and release the context.
2005 */
2006 q->fileio = fileio;
2007 return __vb2_cleanup_fileio(q);
2008 }
2009
2010 /*
2011 * Call vb2_qbuf and give buffer to the driver.
2012 */
2013 memset(&fileio->b, 0, sizeof(fileio->b));
2014 fileio->b.type = q->type;
2015 fileio->b.memory = q->memory;
2016 fileio->b.index = index;
2017 fileio->b.bytesused = buf->pos;
2018 ret = vb2_qbuf(q, &fileio->b);
2019 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2020 if (ret)
2021 goto end;
2022
2023 /*
2024 * Buffer has been queued, update the status
2025 */
2026 buf->pos = 0;
2027 buf->queued = 1;
2028 buf->size = q->bufs[0]->v4l2_planes[0].length;
2029 fileio->q_count += 1;
2030
2031 /*
2032 * Switch to the next buffer
2033 */
2034 fileio->index = (index + 1) % q->num_buffers;
2035
2036 /*
2037 * Start streaming if required.
2038 */
2039 if (!read && !q->streaming) {
2040 ret = vb2_streamon(q, q->type);
2041 if (ret)
2042 goto end;
2043 }
2044 }
2045
2046 /*
2047 * Return proper number of bytes processed.
2048 */
2049 if (ret == 0)
2050 ret = count;
2051end:
2052 /*
2053 * Restore the fileio context and block vb2 ioctl interface.
2054 */
2055 q->fileio = fileio;
2056 return ret;
2057}
2058
2059size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2060 loff_t *ppos, int nonblocking)
2061{
2062 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2063}
2064EXPORT_SYMBOL_GPL(vb2_read);
2065
2066size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
2067 loff_t *ppos, int nonblocking)
2068{
2069 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
2070}
2071EXPORT_SYMBOL_GPL(vb2_write);
2072
Pawel Osciake23ccc02010-10-11 10:56:41 -03002073MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03002074MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03002075MODULE_LICENSE("GPL");