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