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