blob: 82b51be3ca720eb012a0ccaaf6f8984deca74b3e [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
Guennadi Liakhovetski21db3e02011-09-28 07:23:27 -0300938 /* Fill buffer information for the userspace */
939 __fill_v4l2_buffer(vb, b);
940
Pawel Osciake23ccc02010-10-11 10:56:41 -0300941 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
942 return 0;
943}
944EXPORT_SYMBOL_GPL(vb2_qbuf);
945
946/**
947 * __vb2_wait_for_done_vb() - wait for a buffer to become available
948 * for dequeuing
949 *
950 * Will sleep if required for nonblocking == false.
951 */
952static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
953{
954 /*
955 * All operations on vb_done_list are performed under done_lock
956 * spinlock protection. However, buffers may be removed from
957 * it and returned to userspace only while holding both driver's
958 * lock and the done_lock spinlock. Thus we can be sure that as
959 * long as we hold the driver's lock, the list will remain not
960 * empty if list_empty() check succeeds.
961 */
962
963 for (;;) {
964 int ret;
965
966 if (!q->streaming) {
967 dprintk(1, "Streaming off, will not wait for buffers\n");
968 return -EINVAL;
969 }
970
971 if (!list_empty(&q->done_list)) {
972 /*
973 * Found a buffer that we were waiting for.
974 */
975 break;
976 }
977
978 if (nonblocking) {
979 dprintk(1, "Nonblocking and no buffers to dequeue, "
980 "will not wait\n");
981 return -EAGAIN;
982 }
983
984 /*
985 * We are streaming and blocking, wait for another buffer to
986 * become ready or for streamoff. Driver's lock is released to
987 * allow streamoff or qbuf to be called while waiting.
988 */
989 call_qop(q, wait_prepare, q);
990
991 /*
992 * All locks have been released, it is safe to sleep now.
993 */
994 dprintk(3, "Will sleep waiting for buffers\n");
995 ret = wait_event_interruptible(q->done_wq,
996 !list_empty(&q->done_list) || !q->streaming);
997
998 /*
999 * We need to reevaluate both conditions again after reacquiring
1000 * the locks or return an error if one occurred.
1001 */
1002 call_qop(q, wait_finish, q);
1003 if (ret)
1004 return ret;
1005 }
1006 return 0;
1007}
1008
1009/**
1010 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1011 *
1012 * Will sleep if required for nonblocking == false.
1013 */
1014static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1015 int nonblocking)
1016{
1017 unsigned long flags;
1018 int ret;
1019
1020 /*
1021 * Wait for at least one buffer to become available on the done_list.
1022 */
1023 ret = __vb2_wait_for_done_vb(q, nonblocking);
1024 if (ret)
1025 return ret;
1026
1027 /*
1028 * Driver's lock has been held since we last verified that done_list
1029 * is not empty, so no need for another list_empty(done_list) check.
1030 */
1031 spin_lock_irqsave(&q->done_lock, flags);
1032 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1033 list_del(&(*vb)->done_entry);
1034 spin_unlock_irqrestore(&q->done_lock, flags);
1035
1036 return 0;
1037}
1038
1039/**
1040 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1041 * @q: videobuf2 queue
1042 *
1043 * This function will wait until all buffers that have been given to the driver
1044 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1045 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1046 * taken, for example from stop_streaming() callback.
1047 */
1048int vb2_wait_for_all_buffers(struct vb2_queue *q)
1049{
1050 if (!q->streaming) {
1051 dprintk(1, "Streaming off, will not wait for buffers\n");
1052 return -EINVAL;
1053 }
1054
1055 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1056 return 0;
1057}
1058EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1059
1060/**
1061 * vb2_dqbuf() - Dequeue a buffer to the userspace
1062 * @q: videobuf2 queue
1063 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1064 * in driver
1065 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1066 * buffers ready for dequeuing are present. Normally the driver
1067 * would be passing (file->f_flags & O_NONBLOCK) here
1068 *
1069 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1070 * This function:
1071 * 1) verifies the passed buffer,
1072 * 2) calls buf_finish callback in the driver (if provided), in which
1073 * driver can perform any additional operations that may be required before
1074 * returning the buffer to userspace, such as cache sync,
1075 * 3) the buffer struct members are filled with relevant information for
1076 * the userspace.
1077 *
1078 * The return values from this function are intended to be directly returned
1079 * from vidioc_dqbuf handler in driver.
1080 */
1081int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1082{
1083 struct vb2_buffer *vb = NULL;
1084 int ret;
1085
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001086 if (q->fileio) {
1087 dprintk(1, "dqbuf: 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, "dqbuf: invalid buffer type\n");
1093 return -EINVAL;
1094 }
1095
1096 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1097 if (ret < 0) {
1098 dprintk(1, "dqbuf: error getting next done buffer\n");
1099 return ret;
1100 }
1101
1102 ret = call_qop(q, buf_finish, vb);
1103 if (ret) {
1104 dprintk(1, "dqbuf: buffer finish failed\n");
1105 return ret;
1106 }
1107
1108 switch (vb->state) {
1109 case VB2_BUF_STATE_DONE:
1110 dprintk(3, "dqbuf: Returning done buffer\n");
1111 break;
1112 case VB2_BUF_STATE_ERROR:
1113 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1114 break;
1115 default:
1116 dprintk(1, "dqbuf: Invalid buffer state\n");
1117 return -EINVAL;
1118 }
1119
1120 /* Fill buffer information for the userspace */
1121 __fill_v4l2_buffer(vb, b);
1122 /* Remove from videobuf queue */
1123 list_del(&vb->queued_entry);
1124
1125 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1126 vb->v4l2_buf.index, vb->state);
1127
1128 vb->state = VB2_BUF_STATE_DEQUEUED;
1129 return 0;
1130}
1131EXPORT_SYMBOL_GPL(vb2_dqbuf);
1132
1133/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001134 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1135 *
1136 * Removes all queued buffers from driver's queue and all buffers queued by
1137 * userspace from videobuf's queue. Returns to state after reqbufs.
1138 */
1139static void __vb2_queue_cancel(struct vb2_queue *q)
1140{
1141 unsigned int i;
1142
1143 /*
1144 * Tell driver to stop all transactions and release all queued
1145 * buffers.
1146 */
1147 if (q->streaming)
1148 call_qop(q, stop_streaming, q);
1149 q->streaming = 0;
1150
1151 /*
1152 * Remove all buffers from videobuf's list...
1153 */
1154 INIT_LIST_HEAD(&q->queued_list);
1155 /*
1156 * ...and done list; userspace will not receive any buffers it
1157 * has not already dequeued before initiating cancel.
1158 */
1159 INIT_LIST_HEAD(&q->done_list);
Marek Szyprowskiafdea8b2011-06-10 08:58:42 -03001160 atomic_set(&q->queued_count, 0);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001161 wake_up_all(&q->done_wq);
1162
1163 /*
1164 * Reinitialize all buffers for next use.
1165 */
1166 for (i = 0; i < q->num_buffers; ++i)
1167 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1168}
1169
1170/**
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001171 * vb2_streamon - start streaming
1172 * @q: videobuf2 queue
1173 * @type: type argument passed from userspace to vidioc_streamon handler
1174 *
1175 * Should be called from vidioc_streamon handler of a driver.
1176 * This function:
1177 * 1) verifies current state
1178 * 2) passes any previously queued buffers to the driver and starts streaming
1179 *
1180 * The return values from this function are intended to be directly returned
1181 * from vidioc_streamon handler in the driver.
1182 */
1183int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1184{
1185 struct vb2_buffer *vb;
1186 int ret;
1187
1188 if (q->fileio) {
1189 dprintk(1, "streamon: file io in progress\n");
1190 return -EBUSY;
1191 }
1192
1193 if (type != q->type) {
1194 dprintk(1, "streamon: invalid stream type\n");
1195 return -EINVAL;
1196 }
1197
1198 if (q->streaming) {
1199 dprintk(1, "streamon: already streaming\n");
1200 return -EBUSY;
1201 }
1202
1203 /*
1204 * If any buffers were queued before streamon,
1205 * we can now pass them to driver for processing.
1206 */
1207 list_for_each_entry(vb, &q->queued_list, queued_entry)
1208 __enqueue_in_driver(vb);
1209
1210 /*
1211 * Let driver notice that streaming state has been enabled.
1212 */
1213 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
1214 if (ret) {
1215 dprintk(1, "streamon: driver refused to start streaming\n");
1216 __vb2_queue_cancel(q);
1217 return ret;
1218 }
1219
1220 q->streaming = 1;
1221
1222 dprintk(3, "Streamon successful\n");
1223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(vb2_streamon);
1226
1227
1228/**
Pawel Osciake23ccc02010-10-11 10:56:41 -03001229 * vb2_streamoff - stop streaming
1230 * @q: videobuf2 queue
1231 * @type: type argument passed from userspace to vidioc_streamoff handler
1232 *
1233 * Should be called from vidioc_streamoff handler of a driver.
1234 * This function:
1235 * 1) verifies current state,
1236 * 2) stop streaming and dequeues any queued buffers, including those previously
1237 * passed to the driver (after waiting for the driver to finish).
1238 *
1239 * This call can be used for pausing playback.
1240 * The return values from this function are intended to be directly returned
1241 * from vidioc_streamoff handler in the driver
1242 */
1243int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1244{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001245 if (q->fileio) {
1246 dprintk(1, "streamoff: file io in progress\n");
1247 return -EBUSY;
1248 }
1249
Pawel Osciake23ccc02010-10-11 10:56:41 -03001250 if (type != q->type) {
1251 dprintk(1, "streamoff: invalid stream type\n");
1252 return -EINVAL;
1253 }
1254
1255 if (!q->streaming) {
1256 dprintk(1, "streamoff: not streaming\n");
1257 return -EINVAL;
1258 }
1259
1260 /*
1261 * Cancel will pause streaming and remove all buffers from the driver
1262 * and videobuf, effectively returning control over them to userspace.
1263 */
1264 __vb2_queue_cancel(q);
1265
1266 dprintk(3, "Streamoff successful\n");
1267 return 0;
1268}
1269EXPORT_SYMBOL_GPL(vb2_streamoff);
1270
1271/**
1272 * __find_plane_by_offset() - find plane associated with the given offset off
1273 */
1274static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1275 unsigned int *_buffer, unsigned int *_plane)
1276{
1277 struct vb2_buffer *vb;
1278 unsigned int buffer, plane;
1279
1280 /*
1281 * Go over all buffers and their planes, comparing the given offset
1282 * with an offset assigned to each plane. If a match is found,
1283 * return its buffer and plane numbers.
1284 */
1285 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1286 vb = q->bufs[buffer];
1287
1288 for (plane = 0; plane < vb->num_planes; ++plane) {
1289 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1290 *_buffer = buffer;
1291 *_plane = plane;
1292 return 0;
1293 }
1294 }
1295 }
1296
1297 return -EINVAL;
1298}
1299
1300/**
1301 * vb2_mmap() - map video buffers into application address space
1302 * @q: videobuf2 queue
1303 * @vma: vma passed to the mmap file operation handler in the driver
1304 *
1305 * Should be called from mmap file operation handler of a driver.
1306 * This function maps one plane of one of the available video buffers to
1307 * userspace. To map whole video memory allocated on reqbufs, this function
1308 * has to be called once per each plane per each buffer previously allocated.
1309 *
1310 * When the userspace application calls mmap, it passes to it an offset returned
1311 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1312 * a "cookie", which is then used to identify the plane to be mapped.
1313 * This function finds a plane with a matching offset and a mapping is performed
1314 * by the means of a provided memory operation.
1315 *
1316 * The return values from this function are intended to be directly returned
1317 * from the mmap handler in driver.
1318 */
1319int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1320{
1321 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1322 struct vb2_plane *vb_plane;
1323 struct vb2_buffer *vb;
1324 unsigned int buffer, plane;
1325 int ret;
1326
1327 if (q->memory != V4L2_MEMORY_MMAP) {
1328 dprintk(1, "Queue is not currently set up for mmap\n");
1329 return -EINVAL;
1330 }
1331
1332 /*
1333 * Check memory area access mode.
1334 */
1335 if (!(vma->vm_flags & VM_SHARED)) {
1336 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1337 return -EINVAL;
1338 }
1339 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1340 if (!(vma->vm_flags & VM_WRITE)) {
1341 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1342 return -EINVAL;
1343 }
1344 } else {
1345 if (!(vma->vm_flags & VM_READ)) {
1346 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1347 return -EINVAL;
1348 }
1349 }
1350
1351 /*
1352 * Find the plane corresponding to the offset passed by userspace.
1353 */
1354 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1355 if (ret)
1356 return ret;
1357
1358 vb = q->bufs[buffer];
1359 vb_plane = &vb->planes[plane];
1360
1361 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1362 if (ret)
1363 return ret;
1364
Pawel Osciake23ccc02010-10-11 10:56:41 -03001365 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(vb2_mmap);
1369
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001370static int __vb2_init_fileio(struct vb2_queue *q, int read);
1371static int __vb2_cleanup_fileio(struct vb2_queue *q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001372
1373/**
1374 * vb2_poll() - implements poll userspace operation
1375 * @q: videobuf2 queue
1376 * @file: file argument passed to the poll file operation handler
1377 * @wait: wait argument passed to the poll file operation handler
1378 *
1379 * This function implements poll file operation handler for a driver.
1380 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1381 * be informed that the file descriptor of a video device is available for
1382 * reading.
1383 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1384 * will be reported as available for writing.
1385 *
1386 * The return values from this function are intended to be directly returned
1387 * from poll handler in driver.
1388 */
1389unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1390{
1391 unsigned long flags;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001392 unsigned int ret;
Pawel Osciake23ccc02010-10-11 10:56:41 -03001393 struct vb2_buffer *vb = NULL;
1394
1395 /*
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001396 * Start file I/O emulator only if streaming API has not been used yet.
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001397 */
1398 if (q->num_buffers == 0 && q->fileio == NULL) {
1399 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1400 ret = __vb2_init_fileio(q, 1);
1401 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001402 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001403 }
1404 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1405 ret = __vb2_init_fileio(q, 0);
1406 if (ret)
Pawel Osciak4ffabdb2011-03-20 18:17:34 -03001407 return POLLERR;
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001408 /*
1409 * Write to OUTPUT queue can be done immediately.
1410 */
1411 return POLLOUT | POLLWRNORM;
1412 }
1413 }
1414
1415 /*
Pawel Osciake23ccc02010-10-11 10:56:41 -03001416 * There is nothing to wait for if no buffers have already been queued.
1417 */
1418 if (list_empty(&q->queued_list))
1419 return POLLERR;
1420
1421 poll_wait(file, &q->done_wq, wait);
1422
1423 /*
1424 * Take first buffer available for dequeuing.
1425 */
1426 spin_lock_irqsave(&q->done_lock, flags);
1427 if (!list_empty(&q->done_list))
1428 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1429 done_entry);
1430 spin_unlock_irqrestore(&q->done_lock, flags);
1431
1432 if (vb && (vb->state == VB2_BUF_STATE_DONE
1433 || vb->state == VB2_BUF_STATE_ERROR)) {
1434 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1435 POLLIN | POLLRDNORM;
1436 }
1437 return 0;
1438}
1439EXPORT_SYMBOL_GPL(vb2_poll);
1440
1441/**
1442 * vb2_queue_init() - initialize a videobuf2 queue
1443 * @q: videobuf2 queue; this structure should be allocated in driver
1444 *
1445 * The vb2_queue structure should be allocated by the driver. The driver is
1446 * responsible of clearing it's content and setting initial values for some
1447 * required entries before calling this function.
1448 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
1449 * to the struct vb2_queue description in include/media/videobuf2-core.h
1450 * for more information.
1451 */
1452int vb2_queue_init(struct vb2_queue *q)
1453{
1454 BUG_ON(!q);
1455 BUG_ON(!q->ops);
1456 BUG_ON(!q->mem_ops);
1457 BUG_ON(!q->type);
1458 BUG_ON(!q->io_modes);
1459
1460 BUG_ON(!q->ops->queue_setup);
1461 BUG_ON(!q->ops->buf_queue);
1462
1463 INIT_LIST_HEAD(&q->queued_list);
1464 INIT_LIST_HEAD(&q->done_list);
1465 spin_lock_init(&q->done_lock);
1466 init_waitqueue_head(&q->done_wq);
1467
1468 if (q->buf_struct_size == 0)
1469 q->buf_struct_size = sizeof(struct vb2_buffer);
1470
1471 return 0;
1472}
1473EXPORT_SYMBOL_GPL(vb2_queue_init);
1474
1475/**
1476 * vb2_queue_release() - stop streaming, release the queue and free memory
1477 * @q: videobuf2 queue
1478 *
1479 * This function stops streaming and performs necessary clean ups, including
1480 * freeing video buffer memory. The driver is responsible for freeing
1481 * the vb2_queue structure itself.
1482 */
1483void vb2_queue_release(struct vb2_queue *q)
1484{
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001485 __vb2_cleanup_fileio(q);
Pawel Osciake23ccc02010-10-11 10:56:41 -03001486 __vb2_queue_cancel(q);
1487 __vb2_queue_free(q);
1488}
1489EXPORT_SYMBOL_GPL(vb2_queue_release);
1490
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001491/**
1492 * struct vb2_fileio_buf - buffer context used by file io emulator
1493 *
1494 * vb2 provides a compatibility layer and emulator of file io (read and
1495 * write) calls on top of streaming API. This structure is used for
1496 * tracking context related to the buffers.
1497 */
1498struct vb2_fileio_buf {
1499 void *vaddr;
1500 unsigned int size;
1501 unsigned int pos;
1502 unsigned int queued:1;
1503};
1504
1505/**
1506 * struct vb2_fileio_data - queue context used by file io emulator
1507 *
1508 * vb2 provides a compatibility layer and emulator of file io (read and
1509 * write) calls on top of streaming API. For proper operation it required
1510 * this structure to save the driver state between each call of the read
1511 * or write function.
1512 */
1513struct vb2_fileio_data {
1514 struct v4l2_requestbuffers req;
1515 struct v4l2_buffer b;
1516 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1517 unsigned int index;
1518 unsigned int q_count;
1519 unsigned int dq_count;
1520 unsigned int flags;
1521};
1522
1523/**
1524 * __vb2_init_fileio() - initialize file io emulator
1525 * @q: videobuf2 queue
1526 * @read: mode selector (1 means read, 0 means write)
1527 */
1528static int __vb2_init_fileio(struct vb2_queue *q, int read)
1529{
1530 struct vb2_fileio_data *fileio;
1531 int i, ret;
1532 unsigned int count = 0;
1533
1534 /*
1535 * Sanity check
1536 */
1537 if ((read && !(q->io_modes & VB2_READ)) ||
1538 (!read && !(q->io_modes & VB2_WRITE)))
1539 BUG();
1540
1541 /*
1542 * Check if device supports mapping buffers to kernel virtual space.
1543 */
1544 if (!q->mem_ops->vaddr)
1545 return -EBUSY;
1546
1547 /*
1548 * Check if streaming api has not been already activated.
1549 */
1550 if (q->streaming || q->num_buffers > 0)
1551 return -EBUSY;
1552
1553 /*
1554 * Start with count 1, driver can increase it in queue_setup()
1555 */
1556 count = 1;
1557
1558 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1559 (read) ? "read" : "write", count, q->io_flags);
1560
1561 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1562 if (fileio == NULL)
1563 return -ENOMEM;
1564
1565 fileio->flags = q->io_flags;
1566
1567 /*
1568 * Request buffers and use MMAP type to force driver
1569 * to allocate buffers by itself.
1570 */
1571 fileio->req.count = count;
1572 fileio->req.memory = V4L2_MEMORY_MMAP;
1573 fileio->req.type = q->type;
1574 ret = vb2_reqbufs(q, &fileio->req);
1575 if (ret)
1576 goto err_kfree;
1577
1578 /*
1579 * Check if plane_count is correct
1580 * (multiplane buffers are not supported).
1581 */
1582 if (q->bufs[0]->num_planes != 1) {
1583 fileio->req.count = 0;
1584 ret = -EBUSY;
1585 goto err_reqbufs;
1586 }
1587
1588 /*
1589 * Get kernel address of each buffer.
1590 */
1591 for (i = 0; i < q->num_buffers; i++) {
1592 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1593 if (fileio->bufs[i].vaddr == NULL)
1594 goto err_reqbufs;
1595 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1596 }
1597
1598 /*
1599 * Read mode requires pre queuing of all buffers.
1600 */
1601 if (read) {
1602 /*
1603 * Queue all buffers.
1604 */
1605 for (i = 0; i < q->num_buffers; i++) {
1606 struct v4l2_buffer *b = &fileio->b;
1607 memset(b, 0, sizeof(*b));
1608 b->type = q->type;
1609 b->memory = q->memory;
1610 b->index = i;
1611 ret = vb2_qbuf(q, b);
1612 if (ret)
1613 goto err_reqbufs;
1614 fileio->bufs[i].queued = 1;
1615 }
1616
1617 /*
1618 * Start streaming.
1619 */
1620 ret = vb2_streamon(q, q->type);
1621 if (ret)
1622 goto err_reqbufs;
1623 }
1624
1625 q->fileio = fileio;
1626
1627 return ret;
1628
1629err_reqbufs:
1630 vb2_reqbufs(q, &fileio->req);
1631
1632err_kfree:
1633 kfree(fileio);
1634 return ret;
1635}
1636
1637/**
1638 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1639 * @q: videobuf2 queue
1640 */
1641static int __vb2_cleanup_fileio(struct vb2_queue *q)
1642{
1643 struct vb2_fileio_data *fileio = q->fileio;
1644
1645 if (fileio) {
1646 /*
1647 * Hack fileio context to enable direct calls to vb2 ioctl
1648 * interface.
1649 */
1650 q->fileio = NULL;
1651
1652 vb2_streamoff(q, q->type);
1653 fileio->req.count = 0;
1654 vb2_reqbufs(q, &fileio->req);
1655 kfree(fileio);
1656 dprintk(3, "file io emulator closed\n");
1657 }
1658 return 0;
1659}
1660
1661/**
1662 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1663 * @q: videobuf2 queue
1664 * @data: pointed to target userspace buffer
1665 * @count: number of bytes to read or write
1666 * @ppos: file handle position tracking pointer
1667 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1668 * @read: access mode selector (1 means read, 0 means write)
1669 */
1670static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1671 loff_t *ppos, int nonblock, int read)
1672{
1673 struct vb2_fileio_data *fileio;
1674 struct vb2_fileio_buf *buf;
1675 int ret, index;
1676
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001677 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001678 read ? "read" : "write", (long)*ppos, count,
1679 nonblock ? "non" : "");
1680
1681 if (!data)
1682 return -EINVAL;
1683
1684 /*
1685 * Initialize emulator on first call.
1686 */
1687 if (!q->fileio) {
1688 ret = __vb2_init_fileio(q, read);
1689 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1690 if (ret)
1691 return ret;
1692 }
1693 fileio = q->fileio;
1694
1695 /*
1696 * Hack fileio context to enable direct calls to vb2 ioctl interface.
1697 * The pointer will be restored before returning from this function.
1698 */
1699 q->fileio = NULL;
1700
1701 index = fileio->index;
1702 buf = &fileio->bufs[index];
1703
1704 /*
1705 * Check if we need to dequeue the buffer.
1706 */
1707 if (buf->queued) {
1708 struct vb2_buffer *vb;
1709
1710 /*
1711 * Call vb2_dqbuf to get buffer back.
1712 */
1713 memset(&fileio->b, 0, sizeof(fileio->b));
1714 fileio->b.type = q->type;
1715 fileio->b.memory = q->memory;
1716 fileio->b.index = index;
1717 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1718 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1719 if (ret)
1720 goto end;
1721 fileio->dq_count += 1;
1722
1723 /*
1724 * Get number of bytes filled by the driver
1725 */
1726 vb = q->bufs[index];
1727 buf->size = vb2_get_plane_payload(vb, 0);
1728 buf->queued = 0;
1729 }
1730
1731 /*
1732 * Limit count on last few bytes of the buffer.
1733 */
1734 if (buf->pos + count > buf->size) {
1735 count = buf->size - buf->pos;
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001736 dprintk(5, "reducing read count: %zd\n", count);
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001737 }
1738
1739 /*
1740 * Transfer data to userspace.
1741 */
Mauro Carvalho Chehab08b99e22011-01-11 17:12:34 -03001742 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
Marek Szyprowskib25748f2010-12-06 05:56:55 -03001743 count, index, buf->pos);
1744 if (read)
1745 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1746 else
1747 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1748 if (ret) {
1749 dprintk(3, "file io: error copying data\n");
1750 ret = -EFAULT;
1751 goto end;
1752 }
1753
1754 /*
1755 * Update counters.
1756 */
1757 buf->pos += count;
1758 *ppos += count;
1759
1760 /*
1761 * Queue next buffer if required.
1762 */
1763 if (buf->pos == buf->size ||
1764 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1765 /*
1766 * Check if this is the last buffer to read.
1767 */
1768 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
1769 fileio->dq_count == 1) {
1770 dprintk(3, "file io: read limit reached\n");
1771 /*
1772 * Restore fileio pointer and release the context.
1773 */
1774 q->fileio = fileio;
1775 return __vb2_cleanup_fileio(q);
1776 }
1777
1778 /*
1779 * Call vb2_qbuf and give buffer to the driver.
1780 */
1781 memset(&fileio->b, 0, sizeof(fileio->b));
1782 fileio->b.type = q->type;
1783 fileio->b.memory = q->memory;
1784 fileio->b.index = index;
1785 fileio->b.bytesused = buf->pos;
1786 ret = vb2_qbuf(q, &fileio->b);
1787 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
1788 if (ret)
1789 goto end;
1790
1791 /*
1792 * Buffer has been queued, update the status
1793 */
1794 buf->pos = 0;
1795 buf->queued = 1;
1796 buf->size = q->bufs[0]->v4l2_planes[0].length;
1797 fileio->q_count += 1;
1798
1799 /*
1800 * Switch to the next buffer
1801 */
1802 fileio->index = (index + 1) % q->num_buffers;
1803
1804 /*
1805 * Start streaming if required.
1806 */
1807 if (!read && !q->streaming) {
1808 ret = vb2_streamon(q, q->type);
1809 if (ret)
1810 goto end;
1811 }
1812 }
1813
1814 /*
1815 * Return proper number of bytes processed.
1816 */
1817 if (ret == 0)
1818 ret = count;
1819end:
1820 /*
1821 * Restore the fileio context and block vb2 ioctl interface.
1822 */
1823 q->fileio = fileio;
1824 return ret;
1825}
1826
1827size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1828 loff_t *ppos, int nonblocking)
1829{
1830 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1831}
1832EXPORT_SYMBOL_GPL(vb2_read);
1833
1834size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
1835 loff_t *ppos, int nonblocking)
1836{
1837 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
1838}
1839EXPORT_SYMBOL_GPL(vb2_write);
1840
Pawel Osciake23ccc02010-10-11 10:56:41 -03001841MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
Pawel Osciak95072082011-03-13 15:23:32 -03001842MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
Pawel Osciake23ccc02010-10-11 10:56:41 -03001843MODULE_LICENSE("GPL");