blob: b88b5b08792b04cb96665dff902ff5490e81d8b1 [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 | \
41 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR)
42
Pawel Osciake23ccc02010-10-11 10:56:41 -030043/**
44 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
45 */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030046static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
Pawel Osciake23ccc02010-10-11 10:56:41 -030047{
48 struct vb2_queue *q = vb->vb2_queue;
49 void *mem_priv;
50 int plane;
51
52 /* Allocate memory for all planes in this buffer */
53 for (plane = 0; plane < vb->num_planes; ++plane) {
54 mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030055 q->plane_sizes[plane]);
Guennadi Liakhovetski62a79432011-03-22 09:24:58 -030056 if (IS_ERR_OR_NULL(mem_priv))
Pawel Osciake23ccc02010-10-11 10:56:41 -030057 goto free;
58
59 /* Associate allocator private data with this plane */
60 vb->planes[plane].mem_priv = mem_priv;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -030061 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
Pawel Osciake23ccc02010-10-11 10:56:41 -030062 }
63
64 return 0;
65free:
66 /* Free already allocated memory if one of the allocations failed */
67 for (; plane > 0; --plane)
68 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
69
70 return -ENOMEM;
71}
72
73/**
74 * __vb2_buf_mem_free() - free memory of the given buffer
75 */
76static void __vb2_buf_mem_free(struct vb2_buffer *vb)
77{
78 struct vb2_queue *q = vb->vb2_queue;
79 unsigned int plane;
80
81 for (plane = 0; plane < vb->num_planes; ++plane) {
82 call_memop(q, plane, put, vb->planes[plane].mem_priv);
83 vb->planes[plane].mem_priv = NULL;
84 dprintk(3, "Freed plane %d of buffer %d\n",
85 plane, vb->v4l2_buf.index);
86 }
87}
88
89/**
90 * __vb2_buf_userptr_put() - release userspace memory associated with
91 * a USERPTR buffer
92 */
93static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
94{
95 struct vb2_queue *q = vb->vb2_queue;
96 unsigned int plane;
97
98 for (plane = 0; plane < vb->num_planes; ++plane) {
99 void *mem_priv = vb->planes[plane].mem_priv;
100
101 if (mem_priv) {
102 call_memop(q, plane, put_userptr, mem_priv);
103 vb->planes[plane].mem_priv = NULL;
104 }
105 }
106}
107
108/**
109 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
110 * every buffer on the queue
111 */
112static void __setup_offsets(struct vb2_queue *q)
113{
114 unsigned int buffer, plane;
115 struct vb2_buffer *vb;
116 unsigned long off = 0;
117
118 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
119 vb = q->bufs[buffer];
120 if (!vb)
121 continue;
122
123 for (plane = 0; plane < vb->num_planes; ++plane) {
124 vb->v4l2_planes[plane].m.mem_offset = off;
125
126 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
127 buffer, plane, off);
128
129 off += vb->v4l2_planes[plane].length;
130 off = PAGE_ALIGN(off);
131 }
132 }
133}
134
135/**
136 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
137 * video buffer memory for all buffers/planes on the queue and initializes the
138 * queue
139 *
140 * Returns the number of buffers successfully allocated.
141 */
142static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300143 unsigned int num_buffers, unsigned int num_planes)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300144{
145 unsigned int buffer;
146 struct vb2_buffer *vb;
147 int ret;
148
149 for (buffer = 0; buffer < num_buffers; ++buffer) {
150 /* Allocate videobuf buffer structures */
151 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
152 if (!vb) {
153 dprintk(1, "Memory alloc for buffer struct failed\n");
154 break;
155 }
156
157 /* Length stores number of planes for multiplanar buffers */
158 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
159 vb->v4l2_buf.length = num_planes;
160
161 vb->state = VB2_BUF_STATE_DEQUEUED;
162 vb->vb2_queue = q;
163 vb->num_planes = num_planes;
164 vb->v4l2_buf.index = buffer;
165 vb->v4l2_buf.type = q->type;
166 vb->v4l2_buf.memory = memory;
167
168 /* Allocate video buffer memory for the MMAP type */
169 if (memory == V4L2_MEMORY_MMAP) {
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300170 ret = __vb2_buf_mem_alloc(vb);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300171 if (ret) {
172 dprintk(1, "Failed allocating memory for "
173 "buffer %d\n", buffer);
174 kfree(vb);
175 break;
176 }
177 /*
178 * Call the driver-provided buffer initialization
179 * callback, if given. An error in initialization
180 * results in queue setup failure.
181 */
182 ret = call_qop(q, buf_init, vb);
183 if (ret) {
184 dprintk(1, "Buffer %d %p initialization"
185 " failed\n", buffer, vb);
186 __vb2_buf_mem_free(vb);
187 kfree(vb);
188 break;
189 }
190 }
191
192 q->bufs[buffer] = vb;
193 }
194
195 q->num_buffers = buffer;
196
197 __setup_offsets(q);
198
199 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
200 q->num_buffers, num_planes);
201
202 return buffer;
203}
204
205/**
206 * __vb2_free_mem() - release all video buffer memory for a given queue
207 */
208static void __vb2_free_mem(struct vb2_queue *q)
209{
210 unsigned int buffer;
211 struct vb2_buffer *vb;
212
213 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
214 vb = q->bufs[buffer];
215 if (!vb)
216 continue;
217
218 /* Free MMAP buffers or release USERPTR buffers */
219 if (q->memory == V4L2_MEMORY_MMAP)
220 __vb2_buf_mem_free(vb);
221 else
222 __vb2_buf_userptr_put(vb);
223 }
224}
225
226/**
227 * __vb2_queue_free() - free the queue - video memory and related information
228 * and return the queue to an uninitialized state. Might be called even if the
229 * queue has already been freed.
230 */
Marek Szyprowski72f1fc32011-03-10 09:10:49 -0300231static void __vb2_queue_free(struct vb2_queue *q)
Pawel Osciake23ccc02010-10-11 10:56:41 -0300232{
233 unsigned int buffer;
234
235 /* Call driver-provided cleanup function for each buffer, if provided */
236 if (q->ops->buf_cleanup) {
237 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
238 if (NULL == q->bufs[buffer])
239 continue;
240 q->ops->buf_cleanup(q->bufs[buffer]);
241 }
242 }
243
244 /* Release video buffer memory */
245 __vb2_free_mem(q);
246
247 /* Free videobuf buffers */
248 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
249 kfree(q->bufs[buffer]);
250 q->bufs[buffer] = NULL;
251 }
252
253 q->num_buffers = 0;
254 q->memory = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300255}
256
257/**
258 * __verify_planes_array() - verify that the planes array passed in struct
259 * v4l2_buffer from userspace can be safely used
260 */
261static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b)
262{
263 /* Is memory for copying plane information present? */
264 if (NULL == b->m.planes) {
265 dprintk(1, "Multi-planar buffer passed but "
266 "planes array not provided\n");
267 return -EINVAL;
268 }
269
270 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
271 dprintk(1, "Incorrect planes array length, "
272 "expected %d, got %d\n", vb->num_planes, b->length);
273 return -EINVAL;
274 }
275
276 return 0;
277}
278
279/**
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300280 * __buffer_in_use() - return true if the buffer is in use and
281 * the queue cannot be freed (by the means of REQBUFS(0)) call
282 */
283static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
284{
285 unsigned int plane;
286 for (plane = 0; plane < vb->num_planes; ++plane) {
287 /*
288 * If num_users() has not been provided, call_memop
289 * will return 0, apparently nobody cares about this
290 * case anyway. If num_users() returns more than 1,
291 * we are not the only user of the plane's memory.
292 */
293 if (call_memop(q, plane, num_users,
294 vb->planes[plane].mem_priv) > 1)
295 return true;
296 }
297 return false;
298}
299
300/**
301 * __buffers_in_use() - return true if any buffers on the queue are in use and
302 * the queue cannot be freed (by the means of REQBUFS(0)) call
303 */
304static bool __buffers_in_use(struct vb2_queue *q)
305{
306 unsigned int buffer;
307 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
308 if (__buffer_in_use(q, q->bufs[buffer]))
309 return true;
310 }
311 return false;
312}
313
314/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300315 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
316 * returned to userspace
317 */
318static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
319{
320 struct vb2_queue *q = vb->vb2_queue;
321 int ret = 0;
322
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300323 /* Copy back data such as timestamp, flags, input, etc. */
Pawel Osciake23ccc02010-10-11 10:56:41 -0300324 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
325 b->input = vb->v4l2_buf.input;
326 b->reserved = vb->v4l2_buf.reserved;
327
328 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
329 ret = __verify_planes_array(vb, b);
330 if (ret)
331 return ret;
332
333 /*
334 * Fill in plane-related data if userspace provided an array
335 * for it. The memory and size is verified above.
336 */
337 memcpy(b->m.planes, vb->v4l2_planes,
338 b->length * sizeof(struct v4l2_plane));
339 } else {
340 /*
341 * We use length and offset in v4l2_planes array even for
342 * single-planar buffers, but userspace does not.
343 */
344 b->length = vb->v4l2_planes[0].length;
345 b->bytesused = vb->v4l2_planes[0].bytesused;
346 if (q->memory == V4L2_MEMORY_MMAP)
347 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
348 else if (q->memory == V4L2_MEMORY_USERPTR)
349 b->m.userptr = vb->v4l2_planes[0].m.userptr;
350 }
351
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300352 /*
353 * Clear any buffer state related flags.
354 */
355 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300356
357 switch (vb->state) {
358 case VB2_BUF_STATE_QUEUED:
359 case VB2_BUF_STATE_ACTIVE:
360 b->flags |= V4L2_BUF_FLAG_QUEUED;
361 break;
362 case VB2_BUF_STATE_ERROR:
363 b->flags |= V4L2_BUF_FLAG_ERROR;
364 /* fall through */
365 case VB2_BUF_STATE_DONE:
366 b->flags |= V4L2_BUF_FLAG_DONE;
367 break;
368 case VB2_BUF_STATE_DEQUEUED:
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300369 case VB2_BUF_STATE_PREPARED:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300370 /* nothing */
371 break;
372 }
373
Marek Szyprowski25a27d92011-08-24 06:49:35 -0300374 if (__buffer_in_use(q, vb))
Pawel Osciake23ccc02010-10-11 10:56:41 -0300375 b->flags |= V4L2_BUF_FLAG_MAPPED;
376
377 return ret;
378}
379
380/**
381 * vb2_querybuf() - query video buffer information
382 * @q: videobuf queue
383 * @b: buffer struct passed from userspace to vidioc_querybuf handler
384 * in driver
385 *
386 * Should be called from vidioc_querybuf ioctl handler in driver.
387 * This function will verify the passed v4l2_buffer structure and fill the
388 * relevant information for the userspace.
389 *
390 * The return values from this function are intended to be directly returned
391 * from vidioc_querybuf handler in driver.
392 */
393int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
394{
395 struct vb2_buffer *vb;
396
397 if (b->type != q->type) {
398 dprintk(1, "querybuf: wrong buffer type\n");
399 return -EINVAL;
400 }
401
402 if (b->index >= q->num_buffers) {
403 dprintk(1, "querybuf: buffer index out of range\n");
404 return -EINVAL;
405 }
406 vb = q->bufs[b->index];
407
408 return __fill_v4l2_buffer(vb, b);
409}
410EXPORT_SYMBOL(vb2_querybuf);
411
412/**
413 * __verify_userptr_ops() - verify that all memory operations required for
414 * USERPTR queue type have been provided
415 */
416static int __verify_userptr_ops(struct vb2_queue *q)
417{
418 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
419 !q->mem_ops->put_userptr)
420 return -EINVAL;
421
422 return 0;
423}
424
425/**
426 * __verify_mmap_ops() - verify that all memory operations required for
427 * MMAP queue type have been provided
428 */
429static int __verify_mmap_ops(struct vb2_queue *q)
430{
431 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
432 !q->mem_ops->put || !q->mem_ops->mmap)
433 return -EINVAL;
434
435 return 0;
436}
437
438/**
Pawel Osciake23ccc02010-10-11 10:56:41 -0300439 * vb2_reqbufs() - Initiate streaming
440 * @q: videobuf2 queue
441 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
442 *
443 * Should be called from vidioc_reqbufs ioctl handler of a driver.
444 * This function:
445 * 1) verifies streaming parameters passed from the userspace,
446 * 2) sets up the queue,
447 * 3) negotiates number of buffers and planes per buffer with the driver
448 * to be used during streaming,
449 * 4) allocates internal buffer structures (struct vb2_buffer), according to
450 * the agreed parameters,
451 * 5) for MMAP memory type, allocates actual video memory, using the
452 * memory handling/allocation routines provided during queue initialization
453 *
454 * If req->count is 0, all the memory will be freed instead.
455 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
456 * and the queue is not busy, memory will be reallocated.
457 *
458 * The return values from this function are intended to be directly returned
459 * from vidioc_reqbufs handler in driver.
460 */
461int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
462{
463 unsigned int num_buffers, num_planes;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300464 int ret = 0;
465
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300466 if (q->fileio) {
467 dprintk(1, "reqbufs: file io in progress\n");
468 return -EBUSY;
469 }
470
Pawel Osciake23ccc02010-10-11 10:56:41 -0300471 if (req->memory != V4L2_MEMORY_MMAP
472 && req->memory != V4L2_MEMORY_USERPTR) {
473 dprintk(1, "reqbufs: unsupported memory type\n");
474 return -EINVAL;
475 }
476
477 if (req->type != q->type) {
478 dprintk(1, "reqbufs: requested type is incorrect\n");
479 return -EINVAL;
480 }
481
482 if (q->streaming) {
483 dprintk(1, "reqbufs: streaming active\n");
484 return -EBUSY;
485 }
486
487 /*
488 * Make sure all the required memory ops for given memory type
489 * are available.
490 */
491 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
492 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
493 return -EINVAL;
494 }
495
496 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
497 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
498 return -EINVAL;
499 }
500
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300501 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
Pawel Osciake23ccc02010-10-11 10:56:41 -0300502 /*
503 * We already have buffers allocated, so first check if they
504 * are not in use and can be freed.
505 */
506 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
507 dprintk(1, "reqbufs: memory in use, cannot free\n");
508 return -EBUSY;
509 }
510
Marek Szyprowski72f1fc32011-03-10 09:10:49 -0300511 __vb2_queue_free(q);
Marek Szyprowski29e3fbd2011-03-09 14:03:24 -0300512
513 /*
514 * In case of REQBUFS(0) return immediately without calling
515 * driver's queue_setup() callback and allocating resources.
516 */
517 if (req->count == 0)
518 return 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300519 }
520
521 /*
522 * Make sure the requested values and current defaults are sane.
523 */
524 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300525 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
Pawel Osciake23ccc02010-10-11 10:56:41 -0300526 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
Marek Szyprowski13b14092011-04-14 07:17:44 -0300527 q->memory = req->memory;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300528
529 /*
530 * Ask the driver how many buffers and planes per buffer it requires.
531 * Driver also sets the size and allocator context for each plane.
532 */
533 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300534 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300535 if (ret)
536 return ret;
537
538 /* Finally, allocate buffers and video memory */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300539 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
Marek Szyprowski66072d42011-06-28 08:29:02 -0300540 if (ret == 0) {
541 dprintk(1, "Memory allocation failed\n");
542 return -ENOMEM;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300543 }
544
545 /*
546 * Check if driver can handle the allocated number of buffers.
547 */
548 if (ret < num_buffers) {
549 unsigned int orig_num_buffers;
550
551 orig_num_buffers = num_buffers = ret;
552 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300553 q->plane_sizes, q->alloc_ctx);
Pawel Osciake23ccc02010-10-11 10:56:41 -0300554 if (ret)
555 goto free_mem;
556
557 if (orig_num_buffers < num_buffers) {
558 ret = -ENOMEM;
559 goto free_mem;
560 }
561
562 /*
563 * Ok, driver accepted smaller number of buffers.
564 */
565 ret = num_buffers;
566 }
567
Pawel Osciake23ccc02010-10-11 10:56:41 -0300568 /*
569 * Return the number of successfully allocated buffers
570 * to the userspace.
571 */
572 req->count = ret;
573
574 return 0;
575
576free_mem:
577 __vb2_queue_free(q);
578 return ret;
579}
580EXPORT_SYMBOL_GPL(vb2_reqbufs);
581
582/**
583 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
584 * @vb: vb2_buffer to which the plane in question belongs to
585 * @plane_no: plane number for which the address is to be returned
586 *
587 * This function returns a kernel virtual address of a given plane if
588 * such a mapping exist, NULL otherwise.
589 */
590void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
591{
592 struct vb2_queue *q = vb->vb2_queue;
593
594 if (plane_no > vb->num_planes)
595 return NULL;
596
597 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
598
599}
600EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
601
602/**
603 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
604 * @vb: vb2_buffer to which the plane in question belongs to
605 * @plane_no: plane number for which the cookie is to be returned
606 *
607 * This function returns an allocator specific cookie for a given plane if
608 * available, NULL otherwise. The allocator should provide some simple static
609 * inline function, which would convert this cookie to the allocator specific
610 * type that can be used directly by the driver to access the buffer. This can
611 * be for example physical address, pointer to scatter list or IOMMU mapping.
612 */
613void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
614{
615 struct vb2_queue *q = vb->vb2_queue;
616
617 if (plane_no > vb->num_planes)
618 return NULL;
619
620 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
621}
622EXPORT_SYMBOL_GPL(vb2_plane_cookie);
623
624/**
625 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
626 * @vb: vb2_buffer returned from the driver
627 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
628 * or VB2_BUF_STATE_ERROR if the operation finished with an error
629 *
630 * This function should be called by the driver after a hardware operation on
631 * a buffer is finished and the buffer may be returned to userspace. The driver
632 * cannot use this buffer anymore until it is queued back to it by videobuf
633 * by the means of buf_queue callback. Only buffers previously queued to the
634 * driver by buf_queue can be passed to this function.
635 */
636void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
637{
638 struct vb2_queue *q = vb->vb2_queue;
639 unsigned long flags;
640
641 if (vb->state != VB2_BUF_STATE_ACTIVE)
642 return;
643
644 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
645 return;
646
647 dprintk(4, "Done processing on buffer %d, state: %d\n",
648 vb->v4l2_buf.index, vb->state);
649
650 /* Add the buffer to the done buffers list */
651 spin_lock_irqsave(&q->done_lock, flags);
652 vb->state = state;
653 list_add_tail(&vb->done_entry, &q->done_list);
654 atomic_dec(&q->queued_count);
655 spin_unlock_irqrestore(&q->done_lock, flags);
656
657 /* Inform any processes that may be waiting for buffers */
658 wake_up(&q->done_wq);
659}
660EXPORT_SYMBOL_GPL(vb2_buffer_done);
661
662/**
663 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
664 * a v4l2_buffer by the userspace
665 */
666static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
667 struct v4l2_plane *v4l2_planes)
668{
669 unsigned int plane;
670 int ret;
671
672 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
673 /*
674 * Verify that the userspace gave us a valid array for
675 * plane information.
676 */
677 ret = __verify_planes_array(vb, b);
678 if (ret)
679 return ret;
680
681 /* Fill in driver-provided information for OUTPUT types */
682 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
683 /*
684 * Will have to go up to b->length when API starts
685 * accepting variable number of planes.
686 */
687 for (plane = 0; plane < vb->num_planes; ++plane) {
688 v4l2_planes[plane].bytesused =
689 b->m.planes[plane].bytesused;
690 v4l2_planes[plane].data_offset =
691 b->m.planes[plane].data_offset;
692 }
693 }
694
695 if (b->memory == V4L2_MEMORY_USERPTR) {
696 for (plane = 0; plane < vb->num_planes; ++plane) {
697 v4l2_planes[plane].m.userptr =
698 b->m.planes[plane].m.userptr;
699 v4l2_planes[plane].length =
700 b->m.planes[plane].length;
701 }
702 }
703 } else {
704 /*
705 * Single-planar buffers do not use planes array,
706 * so fill in relevant v4l2_buffer struct fields instead.
707 * In videobuf we use our internal V4l2_planes struct for
708 * single-planar buffers as well, for simplicity.
709 */
710 if (V4L2_TYPE_IS_OUTPUT(b->type))
711 v4l2_planes[0].bytesused = b->bytesused;
712
713 if (b->memory == V4L2_MEMORY_USERPTR) {
714 v4l2_planes[0].m.userptr = b->m.userptr;
715 v4l2_planes[0].length = b->length;
716 }
717 }
718
719 vb->v4l2_buf.field = b->field;
720 vb->v4l2_buf.timestamp = b->timestamp;
Marek Szyprowskiea42c8e2011-04-12 10:14:13 -0300721 vb->v4l2_buf.input = b->input;
722 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300723
724 return 0;
725}
726
727/**
728 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
729 */
730static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
731{
732 struct v4l2_plane planes[VIDEO_MAX_PLANES];
733 struct vb2_queue *q = vb->vb2_queue;
734 void *mem_priv;
735 unsigned int plane;
736 int ret;
737 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
738
739 /* Verify and copy relevant information provided by the userspace */
740 ret = __fill_vb2_buffer(vb, b, planes);
741 if (ret)
742 return ret;
743
744 for (plane = 0; plane < vb->num_planes; ++plane) {
745 /* Skip the plane if already verified */
746 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
747 && vb->v4l2_planes[plane].length == planes[plane].length)
748 continue;
749
750 dprintk(3, "qbuf: userspace address for plane %d changed, "
751 "reacquiring memory\n", plane);
752
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300753 /* Check if the provided plane buffer is large enough */
754 if (planes[plane].length < q->plane_sizes[plane]) {
Marek Szyprowski4c2625d2011-10-03 03:21:45 -0300755 ret = -EINVAL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300756 goto err;
757 }
758
Pawel Osciake23ccc02010-10-11 10:56:41 -0300759 /* Release previously acquired memory if present */
760 if (vb->planes[plane].mem_priv)
761 call_memop(q, plane, put_userptr,
762 vb->planes[plane].mem_priv);
763
764 vb->planes[plane].mem_priv = NULL;
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300765 vb->v4l2_planes[plane].m.userptr = 0;
766 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300767
768 /* Acquire each plane's memory */
769 if (q->mem_ops->get_userptr) {
770 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
771 planes[plane].m.userptr,
772 planes[plane].length,
773 write);
774 if (IS_ERR(mem_priv)) {
775 dprintk(1, "qbuf: failed acquiring userspace "
776 "memory for plane %d\n", plane);
777 ret = PTR_ERR(mem_priv);
778 goto err;
779 }
780 vb->planes[plane].mem_priv = mem_priv;
781 }
782 }
783
784 /*
785 * Call driver-specific initialization on the newly acquired buffer,
786 * if provided.
787 */
788 ret = call_qop(q, buf_init, vb);
789 if (ret) {
790 dprintk(1, "qbuf: buffer initialization failed\n");
791 goto err;
792 }
793
794 /*
795 * Now that everything is in order, copy relevant information
796 * provided by userspace.
797 */
798 for (plane = 0; plane < vb->num_planes; ++plane)
799 vb->v4l2_planes[plane] = planes[plane];
800
801 return 0;
802err:
803 /* In case of errors, release planes that were already acquired */
Marek Szyprowskic1426bc2011-08-24 06:36:26 -0300804 for (plane = 0; plane < vb->num_planes; ++plane) {
805 if (vb->planes[plane].mem_priv)
806 call_memop(q, plane, put_userptr,
807 vb->planes[plane].mem_priv);
808 vb->planes[plane].mem_priv = NULL;
809 vb->v4l2_planes[plane].m.userptr = 0;
810 vb->v4l2_planes[plane].length = 0;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300811 }
812
813 return ret;
814}
815
816/**
817 * __qbuf_mmap() - handle qbuf of an MMAP buffer
818 */
819static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
820{
821 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
822}
823
824/**
825 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
826 */
827static void __enqueue_in_driver(struct vb2_buffer *vb)
828{
829 struct vb2_queue *q = vb->vb2_queue;
830
831 vb->state = VB2_BUF_STATE_ACTIVE;
832 atomic_inc(&q->queued_count);
833 q->ops->buf_queue(vb);
834}
835
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300836static int __buf_prepare(struct vb2_buffer *vb, struct v4l2_buffer *b)
837{
838 struct vb2_queue *q = vb->vb2_queue;
839 int ret;
840
841 switch (q->memory) {
842 case V4L2_MEMORY_MMAP:
843 ret = __qbuf_mmap(vb, b);
844 break;
845 case V4L2_MEMORY_USERPTR:
846 ret = __qbuf_userptr(vb, b);
847 break;
848 default:
849 WARN(1, "Invalid queue type\n");
850 ret = -EINVAL;
851 }
852
853 if (!ret)
854 ret = call_qop(q, buf_prepare, vb);
855 if (ret)
856 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
857 else
858 vb->state = VB2_BUF_STATE_PREPARED;
859
860 return ret;
861}
862
Pawel Osciake23ccc02010-10-11 10:56:41 -0300863/**
864 * vb2_qbuf() - Queue a buffer from userspace
865 * @q: videobuf2 queue
866 * @b: buffer structure passed from userspace to vidioc_qbuf handler
867 * in driver
868 *
869 * Should be called from vidioc_qbuf ioctl handler of a driver.
870 * This function:
871 * 1) verifies the passed buffer,
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300872 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
873 * which driver-specific buffer initialization can be performed,
Pawel Osciake23ccc02010-10-11 10:56:41 -0300874 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
875 * callback for processing.
876 *
877 * The return values from this function are intended to be directly returned
878 * from vidioc_qbuf handler in driver.
879 */
880int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
881{
882 struct vb2_buffer *vb;
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300883 int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -0300884
Marek Szyprowskib25748f2010-12-06 05:56:55 -0300885 if (q->fileio) {
886 dprintk(1, "qbuf: file io in progress\n");
887 return -EBUSY;
888 }
889
Pawel Osciake23ccc02010-10-11 10:56:41 -0300890 if (b->type != q->type) {
891 dprintk(1, "qbuf: invalid buffer type\n");
892 return -EINVAL;
893 }
894
895 if (b->index >= q->num_buffers) {
896 dprintk(1, "qbuf: buffer index out of range\n");
897 return -EINVAL;
898 }
899
900 vb = q->bufs[b->index];
901 if (NULL == vb) {
902 /* Should never happen */
903 dprintk(1, "qbuf: buffer is NULL\n");
904 return -EINVAL;
905 }
906
907 if (b->memory != q->memory) {
908 dprintk(1, "qbuf: invalid memory type\n");
909 return -EINVAL;
910 }
911
Guennadi Liakhovetskiebc087d2011-08-31 06:51:10 -0300912 switch (vb->state) {
913 case VB2_BUF_STATE_DEQUEUED:
914 ret = __buf_prepare(vb, b);
915 if (ret)
916 return ret;
917 case VB2_BUF_STATE_PREPARED:
918 break;
919 default:
Pawel Osciake23ccc02010-10-11 10:56:41 -0300920 dprintk(1, "qbuf: buffer already in use\n");
921 return -EINVAL;
922 }
923
Pawel Osciake23ccc02010-10-11 10:56:41 -0300924 /*
925 * Add to the queued buffers list, a buffer will stay on it until
926 * dequeued in dqbuf.
927 */
928 list_add_tail(&vb->queued_entry, &q->queued_list);
929 vb->state = VB2_BUF_STATE_QUEUED;
930
931 /*
932 * If already streaming, give the buffer to driver for processing.
933 * If not, the buffer will be given to driver on next streamon.
934 */
935 if (q->streaming)
936 __enqueue_in_driver(vb);
937
938 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
939 return 0;
940}
941EXPORT_SYMBOL_GPL(vb2_qbuf);
942
943/**
944 * __vb2_wait_for_done_vb() - wait for a buffer to become available
945 * for dequeuing
946 *
947 * Will sleep if required for nonblocking == false.
948 */
949static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
950{
951 /*
952 * All operations on vb_done_list are performed under done_lock
953 * spinlock protection. However, buffers may be removed from
954 * it and returned to userspace only while holding both driver's
955 * lock and the done_lock spinlock. Thus we can be sure that as
956 * long as we hold the driver's lock, the list will remain not
957 * empty if list_empty() check succeeds.
958 */
959
960 for (;;) {
961 int ret;
962
963 if (!q->streaming) {
964 dprintk(1, "Streaming off, will not wait for buffers\n");
965 return -EINVAL;
966 }
967
968 if (!list_empty(&q->done_list)) {
969 /*
970 * Found a buffer that we were waiting for.
971 */
972 break;
973 }
974
975 if (nonblocking) {
976 dprintk(1, "Nonblocking and no buffers to dequeue, "
977 "will not wait\n");
978 return -EAGAIN;
979 }
980
981 /*
982 * We are streaming and blocking, wait for another buffer to
983 * become ready or for streamoff. Driver's lock is released to
984 * allow streamoff or qbuf to be called while waiting.
985 */
986 call_qop(q, wait_prepare, q);
987
988 /*
989 * All locks have been released, it is safe to sleep now.
990 */
991 dprintk(3, "Will sleep waiting for buffers\n");
992 ret = wait_event_interruptible(q->done_wq,
993 !list_empty(&q->done_list) || !q->streaming);
994
995 /*
996 * We need to reevaluate both conditions again after reacquiring
997 * the locks or return an error if one occurred.
998 */
999 call_qop(q, wait_finish, q);
1000 if (ret)
1001 return ret;
1002 }
1003 return 0;
1004}
1005
1006/**
1007 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1008 *
1009 * Will sleep if required for nonblocking == false.
1010 */
1011static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1012 int nonblocking)
1013{
1014 unsigned long flags;
1015 int ret;
1016
1017 /*
1018 * Wait for at least one buffer to become available on the done_list.
1019 */
1020 ret = __vb2_wait_for_done_vb(q, nonblocking);
1021 if (ret)
1022 return ret;
1023
1024 /*
1025 * Driver's lock has been held since we last verified that done_list
1026 * is not empty, so no need for another list_empty(done_list) check.
1027 */
1028 spin_lock_irqsave(&q->done_lock, flags);
1029 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1030 list_del(&(*vb)->done_entry);
1031 spin_unlock_irqrestore(&q->done_lock, flags);
1032
1033 return 0;
1034}
1035
1036/**
1037 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1038 * @q: videobuf2 queue
1039 *
1040 * This function will wait until all buffers that have been given to the driver
1041 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1042 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1043 * taken, for example from stop_streaming() callback.
1044 */
1045int vb2_wait_for_all_buffers(struct vb2_queue *q)
1046{
1047 if (!q->streaming) {
1048 dprintk(1, "Streaming off, will not wait for buffers\n");
1049 return -EINVAL;
1050 }
1051
1052 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1053 return 0;
1054}
1055EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1056
1057/**
1058 * vb2_dqbuf() - Dequeue a buffer to the userspace
1059 * @q: videobuf2 queue
1060 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1061 * in driver
1062 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1063 * buffers ready for dequeuing are present. Normally the driver
1064 * would be passing (file->f_flags & O_NONBLOCK) here
1065 *
1066 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1067 * This function:
1068 * 1) verifies the passed buffer,
1069 * 2) calls buf_finish callback in the driver (if provided), in which
1070 * driver can perform any additional operations that may be required before
1071 * returning the buffer to userspace, such as cache sync,
1072 * 3) the buffer struct members are filled with relevant information for
1073 * the userspace.
1074 *
1075 * The return values from this function are intended to be directly returned
1076 * from vidioc_dqbuf handler in driver.
1077 */
1078int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1079{
1080 struct vb2_buffer *vb = NULL;
1081 int ret;
1082
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001083 if (q->fileio) {
1084 dprintk(1, "dqbuf: file io in progress\n");
1085 return -EBUSY;
1086 }
1087
Pawel Osciake23ccc02010-10-11 10:56:41 -03001088 if (b->type != q->type) {
1089 dprintk(1, "dqbuf: invalid buffer type\n");
1090 return -EINVAL;
1091 }
1092
1093 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1094 if (ret < 0) {
1095 dprintk(1, "dqbuf: error getting next done buffer\n");
1096 return ret;
1097 }
1098
1099 ret = call_qop(q, buf_finish, vb);
1100 if (ret) {
1101 dprintk(1, "dqbuf: buffer finish failed\n");
1102 return ret;
1103 }
1104
1105 switch (vb->state) {
1106 case VB2_BUF_STATE_DONE:
1107 dprintk(3, "dqbuf: Returning done buffer\n");
1108 break;
1109 case VB2_BUF_STATE_ERROR:
1110 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1111 break;
1112 default:
1113 dprintk(1, "dqbuf: Invalid buffer state\n");
1114 return -EINVAL;
1115 }
1116
1117 /* Fill buffer information for the userspace */
1118 __fill_v4l2_buffer(vb, b);
1119 /* Remove from videobuf queue */
1120 list_del(&vb->queued_entry);
1121
1122 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1123 vb->v4l2_buf.index, vb->state);
1124
1125 vb->state = VB2_BUF_STATE_DEQUEUED;
1126 return 0;
1127}
1128EXPORT_SYMBOL_GPL(vb2_dqbuf);
1129
1130/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001131 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1132 *
1133 * Removes all queued buffers from driver's queue and all buffers queued by
1134 * userspace from videobuf's queue. Returns to state after reqbufs.
1135 */
1136static void __vb2_queue_cancel(struct vb2_queue *q)
1137{
1138 unsigned int i;
1139
1140 /*
1141 * Tell driver to stop all transactions and release all queued
1142 * buffers.
1143 */
1144 if (q->streaming)
1145 call_qop(q, stop_streaming, q);
1146 q->streaming = 0;
1147
1148 /*
1149 * Remove all buffers from videobuf's list...
1150 */
1151 INIT_LIST_HEAD(&q->queued_list);
1152 /*
1153 * ...and done list; userspace will not receive any buffers it
1154 * has not already dequeued before initiating cancel.
1155 */
1156 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001157 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001158 wake_up_all(&q->done_wq);
1159
1160 /*
1161 * Reinitialize all buffers for next use.
1162 */
1163 for (i = 0; i < q->num_buffers; ++i)
1164 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1165}
1166
1167/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001168 * vb2_streamon - start streaming
1169 * @q: videobuf2 queue
1170 * @type: type argument passed from userspace to vidioc_streamon handler
1171 *
1172 * Should be called from vidioc_streamon handler of a driver.
1173 * This function:
1174 * 1) verifies current state
1175 * 2) passes any previously queued buffers to the driver and starts streaming
1176 *
1177 * The return values from this function are intended to be directly returned
1178 * from vidioc_streamon handler in the driver.
1179 */
1180int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1181{
1182 struct vb2_buffer *vb;
1183 int ret;
1184
1185 if (q->fileio) {
1186 dprintk(1, "streamon: file io in progress\n");
1187 return -EBUSY;
1188 }
1189
1190 if (type != q->type) {
1191 dprintk(1, "streamon: invalid stream type\n");
1192 return -EINVAL;
1193 }
1194
1195 if (q->streaming) {
1196 dprintk(1, "streamon: already streaming\n");
1197 return -EBUSY;
1198 }
1199
1200 /*
1201 * If any buffers were queued before streamon,
1202 * we can now pass them to driver for processing.
1203 */
1204 list_for_each_entry(vb, &q->queued_list, queued_entry)
1205 __enqueue_in_driver(vb);
1206
1207 /*
1208 * Let driver notice that streaming state has been enabled.
1209 */
1210 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1211 if (ret) {
1212 dprintk(1, "streamon: driver refused to start streaming\n");
1213 __vb2_queue_cancel(q);
1214 return ret;
1215 }
1216
1217 q->streaming = 1;
1218
1219 dprintk(3, "Streamon successful\n");
1220 return 0;
1221}
1222EXPORT_SYMBOL_GPL(vb2_streamon);
1223
1224
1225/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001226 * vb2_streamoff - stop streaming
1227 * @q: videobuf2 queue
1228 * @type: type argument passed from userspace to vidioc_streamoff handler
1229 *
1230 * Should be called from vidioc_streamoff handler of a driver.
1231 * This function:
1232 * 1) verifies current state,
1233 * 2) stop streaming and dequeues any queued buffers, including those previously
1234 * passed to the driver (after waiting for the driver to finish).
1235 *
1236 * This call can be used for pausing playback.
1237 * The return values from this function are intended to be directly returned
1238 * from vidioc_streamoff handler in the driver
1239 */
1240int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1241{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001242 if (q->fileio) {
1243 dprintk(1, "streamoff: file io in progress\n");
1244 return -EBUSY;
1245 }
1246
Pawel Osciake23ccc02010-10-11 10:56:41 -03001247 if (type != q->type) {
1248 dprintk(1, "streamoff: invalid stream type\n");
1249 return -EINVAL;
1250 }
1251
1252 if (!q->streaming) {
1253 dprintk(1, "streamoff: not streaming\n");
1254 return -EINVAL;
1255 }
1256
1257 /*
1258 * Cancel will pause streaming and remove all buffers from the driver
1259 * and videobuf, effectively returning control over them to userspace.
1260 */
1261 __vb2_queue_cancel(q);
1262
1263 dprintk(3, "Streamoff successful\n");
1264 return 0;
1265}
1266EXPORT_SYMBOL_GPL(vb2_streamoff);
1267
1268/**
1269 * __find_plane_by_offset() - find plane associated with the given offset off
1270 */
1271static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1272 unsigned int *_buffer, unsigned int *_plane)
1273{
1274 struct vb2_buffer *vb;
1275 unsigned int buffer, plane;
1276
1277 /*
1278 * Go over all buffers and their planes, comparing the given offset
1279 * with an offset assigned to each plane. If a match is found,
1280 * return its buffer and plane numbers.
1281 */
1282 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1283 vb = q->bufs[buffer];
1284
1285 for (plane = 0; plane < vb->num_planes; ++plane) {
1286 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1287 *_buffer = buffer;
1288 *_plane = plane;
1289 return 0;
1290 }
1291 }
1292 }
1293
1294 return -EINVAL;
1295}
1296
1297/**
1298 * vb2_mmap() - map video buffers into application address space
1299 * @q: videobuf2 queue
1300 * @vma: vma passed to the mmap file operation handler in the driver
1301 *
1302 * Should be called from mmap file operation handler of a driver.
1303 * This function maps one plane of one of the available video buffers to
1304 * userspace. To map whole video memory allocated on reqbufs, this function
1305 * has to be called once per each plane per each buffer previously allocated.
1306 *
1307 * When the userspace application calls mmap, it passes to it an offset returned
1308 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1309 * a "cookie", which is then used to identify the plane to be mapped.
1310 * This function finds a plane with a matching offset and a mapping is performed
1311 * by the means of a provided memory operation.
1312 *
1313 * The return values from this function are intended to be directly returned
1314 * from the mmap handler in driver.
1315 */
1316int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1317{
1318 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1319 struct vb2_plane *vb_plane;
1320 struct vb2_buffer *vb;
1321 unsigned int buffer, plane;
1322 int ret;
1323
1324 if (q->memory != V4L2_MEMORY_MMAP) {
1325 dprintk(1, "Queue is not currently set up for mmap\n");
1326 return -EINVAL;
1327 }
1328
1329 /*
1330 * Check memory area access mode.
1331 */
1332 if (!(vma->vm_flags & VM_SHARED)) {
1333 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1334 return -EINVAL;
1335 }
1336 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1337 if (!(vma->vm_flags & VM_WRITE)) {
1338 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1339 return -EINVAL;
1340 }
1341 } else {
1342 if (!(vma->vm_flags & VM_READ)) {
1343 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1344 return -EINVAL;
1345 }
1346 }
1347
1348 /*
1349 * Find the plane corresponding to the offset passed by userspace.
1350 */
1351 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1352 if (ret)
1353 return ret;
1354
1355 vb = q->bufs[buffer];
1356 vb_plane = &vb->planes[plane];
1357
1358 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1359 if (ret)
1360 return ret;
1361
Pawel Osciake23ccc02010-10-11 10:56:41 -03001362 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1363 return 0;
1364}
1365EXPORT_SYMBOL_GPL(vb2_mmap);
1366
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001367static int __vb2_init_fileio(struct vb2_queue *q, int read);
1368static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001369
1370/**
1371 * vb2_poll() - implements poll userspace operation
1372 * @q: videobuf2 queue
1373 * @file: file argument passed to the poll file operation handler
1374 * @wait: wait argument passed to the poll file operation handler
1375 *
1376 * This function implements poll file operation handler for a driver.
1377 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1378 * be informed that the file descriptor of a video device is available for
1379 * reading.
1380 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1381 * will be reported as available for writing.
1382 *
1383 * The return values from this function are intended to be directly returned
1384 * from poll handler in driver.
1385 */
1386unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1387{
1388 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001389 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001390 struct vb2_buffer *vb = NULL;
1391
1392 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001393 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001394 */
1395 if (q->num_buffers == 0 && q->fileio == NULL) {
1396 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1397 ret = __vb2_init_fileio(q, 1);
1398 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001399 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001400 }
1401 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1402 ret = __vb2_init_fileio(q, 0);
1403 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001404 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001405 /*
1406 * Write to OUTPUT queue can be done immediately.
1407 */
1408 return POLLOUT | POLLWRNORM;
1409 }
1410 }
1411
1412 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001413 * There is nothing to wait for if no buffers have already been queued.
1414 */
1415 if (list_empty(&q->queued_list))
1416 return POLLERR;
1417
1418 poll_wait(file, &q->done_wq, wait);
1419
1420 /*
1421 * Take first buffer available for dequeuing.
1422 */
1423 spin_lock_irqsave(&q->done_lock, flags);
1424 if (!list_empty(&q->done_list))
1425 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1426 done_entry);
1427 spin_unlock_irqrestore(&q->done_lock, flags);
1428
1429 if (vb && (vb->state == VB2_BUF_STATE_DONE
1430 || vb->state == VB2_BUF_STATE_ERROR)) {
1431 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1432 POLLIN | POLLRDNORM;
1433 }
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(vb2_poll);
1437
1438/**
1439 * vb2_queue_init() - initialize a videobuf2 queue
1440 * @q: videobuf2 queue; this structure should be allocated in driver
1441 *
1442 * The vb2_queue structure should be allocated by the driver. The driver is
1443 * responsible of clearing it's content and setting initial values for some
1444 * required entries before calling this function.
1445 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1446 * to the struct vb2_queue description in include/media/videobuf2-core.h
1447 * for more information.
1448 */
1449int vb2_queue_init(struct vb2_queue *q)
1450{
1451 BUG_ON(!q);
1452 BUG_ON(!q->ops);
1453 BUG_ON(!q->mem_ops);
1454 BUG_ON(!q->type);
1455 BUG_ON(!q->io_modes);
1456
1457 BUG_ON(!q->ops->queue_setup);
1458 BUG_ON(!q->ops->buf_queue);
1459
1460 INIT_LIST_HEAD(&q->queued_list);
1461 INIT_LIST_HEAD(&q->done_list);
1462 spin_lock_init(&q->done_lock);
1463 init_waitqueue_head(&q->done_wq);
1464
1465 if (q->buf_struct_size == 0)
1466 q->buf_struct_size = sizeof(struct vb2_buffer);
1467
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(vb2_queue_init);
1471
1472/**
1473 * vb2_queue_release() - stop streaming, release the queue and free memory
1474 * @q: videobuf2 queue
1475 *
1476 * This function stops streaming and performs necessary clean ups, including
1477 * freeing video buffer memory. The driver is responsible for freeing
1478 * the vb2_queue structure itself.
1479 */
1480void vb2_queue_release(struct vb2_queue *q)
1481{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001482 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001483 __vb2_queue_cancel(q);
1484 __vb2_queue_free(q);
1485}
1486EXPORT_SYMBOL_GPL(vb2_queue_release);
1487
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001488/**
1489 * struct vb2_fileio_buf - buffer context used by file io emulator
1490 *
1491 * vb2 provides a compatibility layer and emulator of file io (read and
1492 * write) calls on top of streaming API. This structure is used for
1493 * tracking context related to the buffers.
1494 */
1495struct vb2_fileio_buf {
1496 void *vaddr;
1497 unsigned int size;
1498 unsigned int pos;
1499 unsigned int queued:1;
1500};
1501
1502/**
1503 * struct vb2_fileio_data - queue context used by file io emulator
1504 *
1505 * vb2 provides a compatibility layer and emulator of file io (read and
1506 * write) calls on top of streaming API. For proper operation it required
1507 * this structure to save the driver state between each call of the read
1508 * or write function.
1509 */
1510struct vb2_fileio_data {
1511 struct v4l2_requestbuffers req;
1512 struct v4l2_buffer b;
1513 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1514 unsigned int index;
1515 unsigned int q_count;
1516 unsigned int dq_count;
1517 unsigned int flags;
1518};
1519
1520/**
1521 * __vb2_init_fileio() - initialize file io emulator
1522 * @q: videobuf2 queue
1523 * @read: mode selector (1 means read, 0 means write)
1524 */
1525static int __vb2_init_fileio(struct vb2_queue *q, int read)
1526{
1527 struct vb2_fileio_data *fileio;
1528 int i, ret;
1529 unsigned int count = 0;
1530
1531 /*
1532 * Sanity check
1533 */
1534 if ((read && !(q->io_modes & VB2_READ)) ||
1535 (!read && !(q->io_modes & VB2_WRITE)))
1536 BUG();
1537
1538 /*
1539 * Check if device supports mapping buffers to kernel virtual space.
1540 */
1541 if (!q->mem_ops->vaddr)
1542 return -EBUSY;
1543
1544 /*
1545 * Check if streaming api has not been already activated.
1546 */
1547 if (q->streaming || q->num_buffers > 0)
1548 return -EBUSY;
1549
1550 /*
1551 * Start with count 1, driver can increase it in queue_setup()
1552 */
1553 count = 1;
1554
1555 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1556 (read) ? "read" : "write", count, q->io_flags);
1557
1558 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1559 if (fileio == NULL)
1560 return -ENOMEM;
1561
1562 fileio->flags = q->io_flags;
1563
1564 /*
1565 * Request buffers and use MMAP type to force driver
1566 * to allocate buffers by itself.
1567 */
1568 fileio->req.count = count;
1569 fileio->req.memory = V4L2_MEMORY_MMAP;
1570 fileio->req.type = q->type;
1571 ret = vb2_reqbufs(q, &fileio->req);
1572 if (ret)
1573 goto err_kfree;
1574
1575 /*
1576 * Check if plane_count is correct
1577 * (multiplane buffers are not supported).
1578 */
1579 if (q->bufs[0]->num_planes != 1) {
1580 fileio->req.count = 0;
1581 ret = -EBUSY;
1582 goto err_reqbufs;
1583 }
1584
1585 /*
1586 * Get kernel address of each buffer.
1587 */
1588 for (i = 0; i < q->num_buffers; i++) {
1589 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1590 if (fileio->bufs[i].vaddr == NULL)
1591 goto err_reqbufs;
1592 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1593 }
1594
1595 /*
1596 * Read mode requires pre queuing of all buffers.
1597 */
1598 if (read) {
1599 /*
1600 * Queue all buffers.
1601 */
1602 for (i = 0; i < q->num_buffers; i++) {
1603 struct v4l2_buffer *b = &fileio->b;
1604 memset(b, 0, sizeof(*b));
1605 b->type = q->type;
1606 b->memory = q->memory;
1607 b->index = i;
1608 ret = vb2_qbuf(q, b);
1609 if (ret)
1610 goto err_reqbufs;
1611 fileio->bufs[i].queued = 1;
1612 }
1613
1614 /*
1615 * Start streaming.
1616 */
1617 ret = vb2_streamon(q, q->type);
1618 if (ret)
1619 goto err_reqbufs;
1620 }
1621
1622 q->fileio = fileio;
1623
1624 return ret;
1625
1626err_reqbufs:
1627 vb2_reqbufs(q, &fileio->req);
1628
1629err_kfree:
1630 kfree(fileio);
1631 return ret;
1632}
1633
1634/**
1635 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1636 * @q: videobuf2 queue
1637 */
1638static int __vb2_cleanup_fileio(struct vb2_queue *q)
1639{
1640 struct vb2_fileio_data *fileio = q->fileio;
1641
1642 if (fileio) {
1643 /*
1644 * Hack fileio context to enable direct calls to vb2 ioctl
1645 * interface.
1646 */
1647 q->fileio = NULL;
1648
1649 vb2_streamoff(q, q->type);
1650 fileio->req.count = 0;
1651 vb2_reqbufs(q, &fileio->req);
1652 kfree(fileio);
1653 dprintk(3, "file io emulator closed\n");
1654 }
1655 return 0;
1656}
1657
1658/**
1659 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1660 * @q: videobuf2 queue
1661 * @data: pointed to target userspace buffer
1662 * @count: number of bytes to read or write
1663 * @ppos: file handle position tracking pointer
1664 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1665 * @read: access mode selector (1 means read, 0 means write)
1666 */
1667static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1668 loff_t *ppos, int nonblock, int read)
1669{
1670 struct vb2_fileio_data *fileio;
1671 struct vb2_fileio_buf *buf;
1672 int ret, index;
1673
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001674 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001675 read ? "read" : "write", (long)*ppos, count,
1676 nonblock ? "non" : "");
1677
1678 if (!data)
1679 return -EINVAL;
1680
1681 /*
1682 * Initialize emulator on first call.
1683 */
1684 if (!q->fileio) {
1685 ret = __vb2_init_fileio(q, read);
1686 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1687 if (ret)
1688 return ret;
1689 }
1690 fileio = q->fileio;
1691
1692 /*
1693 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1694 * The pointer will be restored before returning from this function.
1695 */
1696 q->fileio = NULL;
1697
1698 index = fileio->index;
1699 buf = &fileio->bufs[index];
1700
1701 /*
1702 * Check if we need to dequeue the buffer.
1703 */
1704 if (buf->queued) {
1705 struct vb2_buffer *vb;
1706
1707 /*
1708 * Call vb2_dqbuf to get buffer back.
1709 */
1710 memset(&fileio->b, 0, sizeof(fileio->b));
1711 fileio->b.type = q->type;
1712 fileio->b.memory = q->memory;
1713 fileio->b.index = index;
1714 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1715 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1716 if (ret)
1717 goto end;
1718 fileio->dq_count += 1;
1719
1720 /*
1721 * Get number of bytes filled by the driver
1722 */
1723 vb = q->bufs[index];
1724 buf->size = vb2_get_plane_payload(vb, 0);
1725 buf->queued = 0;
1726 }
1727
1728 /*
1729 * Limit count on last few bytes of the buffer.
1730 */
1731 if (buf->pos + count > buf->size) {
1732 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001733 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001734 }
1735
1736 /*
1737 * Transfer data to userspace.
1738 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001739 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001740 count, index, buf->pos);
1741 if (read)
1742 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1743 else
1744 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1745 if (ret) {
1746 dprintk(3, "file io: error copying data\n");
1747 ret = -EFAULT;
1748 goto end;
1749 }
1750
1751 /*
1752 * Update counters.
1753 */
1754 buf->pos += count;
1755 *ppos += count;
1756
1757 /*
1758 * Queue next buffer if required.
1759 */
1760 if (buf->pos == buf->size ||
1761 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1762 /*
1763 * Check if this is the last buffer to read.
1764 */
1765 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
1766 fileio->dq_count == 1) {
1767 dprintk(3, "file io: read limit reached\n");
1768 /*
1769 * Restore fileio pointer and release the context.
1770 */
1771 q->fileio = fileio;
1772 return __vb2_cleanup_fileio(q);
1773 }
1774
1775 /*
1776 * Call vb2_qbuf and give buffer to the driver.
1777 */
1778 memset(&fileio->b, 0, sizeof(fileio->b));
1779 fileio->b.type = q->type;
1780 fileio->b.memory = q->memory;
1781 fileio->b.index = index;
1782 fileio->b.bytesused = buf->pos;
1783 ret = vb2_qbuf(q, &fileio->b);
1784 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
1785 if (ret)
1786 goto end;
1787
1788 /*
1789 * Buffer has been queued, update the status
1790 */
1791 buf->pos = 0;
1792 buf->queued = 1;
1793 buf->size = q->bufs[0]->v4l2_planes[0].length;
1794 fileio->q_count += 1;
1795
1796 /*
1797 * Switch to the next buffer
1798 */
1799 fileio->index = (index + 1) % q->num_buffers;
1800
1801 /*
1802 * Start streaming if required.
1803 */
1804 if (!read && !q->streaming) {
1805 ret = vb2_streamon(q, q->type);
1806 if (ret)
1807 goto end;
1808 }
1809 }
1810
1811 /*
1812 * Return proper number of bytes processed.
1813 */
1814 if (ret == 0)
1815 ret = count;
1816end:
1817 /*
1818 * Restore the fileio context and block vb2 ioctl interface.
1819 */
1820 q->fileio = fileio;
1821 return ret;
1822}
1823
1824size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1825 loff_t *ppos, int nonblocking)
1826{
1827 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1828}
1829EXPORT_SYMBOL_GPL(vb2_read);
1830
1831size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
1832 loff_t *ppos, int nonblocking)
1833{
1834 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
1835}
1836EXPORT_SYMBOL_GPL(vb2_write);
1837
Pawel Osciake23ccc02010-10-11 10:56:41 -03001838MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03001839MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001840MODULE_LICENSE("GPL");