[media] media: vb2: add a check if queued userptr buffer is large enough
[deliverable/linux.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
279/**
280 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
281 * returned to userspace
282 */
283static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
284{
285 struct vb2_queue *q = vb->vb2_queue;
286 int ret = 0;
287
ea42c8ec 288 /* Copy back data such as timestamp, flags, input, etc. */
e23ccc0a
PO
289 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
290 b->input = vb->v4l2_buf.input;
291 b->reserved = vb->v4l2_buf.reserved;
292
293 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
294 ret = __verify_planes_array(vb, b);
295 if (ret)
296 return ret;
297
298 /*
299 * Fill in plane-related data if userspace provided an array
300 * for it. The memory and size is verified above.
301 */
302 memcpy(b->m.planes, vb->v4l2_planes,
303 b->length * sizeof(struct v4l2_plane));
304 } else {
305 /*
306 * We use length and offset in v4l2_planes array even for
307 * single-planar buffers, but userspace does not.
308 */
309 b->length = vb->v4l2_planes[0].length;
310 b->bytesused = vb->v4l2_planes[0].bytesused;
311 if (q->memory == V4L2_MEMORY_MMAP)
312 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
313 else if (q->memory == V4L2_MEMORY_USERPTR)
314 b->m.userptr = vb->v4l2_planes[0].m.userptr;
315 }
316
ea42c8ec
MS
317 /*
318 * Clear any buffer state related flags.
319 */
320 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
e23ccc0a
PO
321
322 switch (vb->state) {
323 case VB2_BUF_STATE_QUEUED:
324 case VB2_BUF_STATE_ACTIVE:
325 b->flags |= V4L2_BUF_FLAG_QUEUED;
326 break;
327 case VB2_BUF_STATE_ERROR:
328 b->flags |= V4L2_BUF_FLAG_ERROR;
329 /* fall through */
330 case VB2_BUF_STATE_DONE:
331 b->flags |= V4L2_BUF_FLAG_DONE;
332 break;
333 case VB2_BUF_STATE_DEQUEUED:
334 /* nothing */
335 break;
336 }
337
338 if (vb->num_planes_mapped == vb->num_planes)
339 b->flags |= V4L2_BUF_FLAG_MAPPED;
340
341 return ret;
342}
343
344/**
345 * vb2_querybuf() - query video buffer information
346 * @q: videobuf queue
347 * @b: buffer struct passed from userspace to vidioc_querybuf handler
348 * in driver
349 *
350 * Should be called from vidioc_querybuf ioctl handler in driver.
351 * This function will verify the passed v4l2_buffer structure and fill the
352 * relevant information for the userspace.
353 *
354 * The return values from this function are intended to be directly returned
355 * from vidioc_querybuf handler in driver.
356 */
357int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
358{
359 struct vb2_buffer *vb;
360
361 if (b->type != q->type) {
362 dprintk(1, "querybuf: wrong buffer type\n");
363 return -EINVAL;
364 }
365
366 if (b->index >= q->num_buffers) {
367 dprintk(1, "querybuf: buffer index out of range\n");
368 return -EINVAL;
369 }
370 vb = q->bufs[b->index];
371
372 return __fill_v4l2_buffer(vb, b);
373}
374EXPORT_SYMBOL(vb2_querybuf);
375
376/**
377 * __verify_userptr_ops() - verify that all memory operations required for
378 * USERPTR queue type have been provided
379 */
380static int __verify_userptr_ops(struct vb2_queue *q)
381{
382 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
383 !q->mem_ops->put_userptr)
384 return -EINVAL;
385
386 return 0;
387}
388
389/**
390 * __verify_mmap_ops() - verify that all memory operations required for
391 * MMAP queue type have been provided
392 */
393static int __verify_mmap_ops(struct vb2_queue *q)
394{
395 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
396 !q->mem_ops->put || !q->mem_ops->mmap)
397 return -EINVAL;
398
399 return 0;
400}
401
402/**
403 * __buffers_in_use() - return true if any buffers on the queue are in use and
404 * the queue cannot be freed (by the means of REQBUFS(0)) call
405 */
406static bool __buffers_in_use(struct vb2_queue *q)
407{
408 unsigned int buffer, plane;
409 struct vb2_buffer *vb;
410
411 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
412 vb = q->bufs[buffer];
413 for (plane = 0; plane < vb->num_planes; ++plane) {
414 /*
415 * If num_users() has not been provided, call_memop
416 * will return 0, apparently nobody cares about this
417 * case anyway. If num_users() returns more than 1,
418 * we are not the only user of the plane's memory.
419 */
420 if (call_memop(q, plane, num_users,
421 vb->planes[plane].mem_priv) > 1)
422 return true;
423 }
424 }
425
426 return false;
427}
428
429/**
430 * vb2_reqbufs() - Initiate streaming
431 * @q: videobuf2 queue
432 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
433 *
434 * Should be called from vidioc_reqbufs ioctl handler of a driver.
435 * This function:
436 * 1) verifies streaming parameters passed from the userspace,
437 * 2) sets up the queue,
438 * 3) negotiates number of buffers and planes per buffer with the driver
439 * to be used during streaming,
440 * 4) allocates internal buffer structures (struct vb2_buffer), according to
441 * the agreed parameters,
442 * 5) for MMAP memory type, allocates actual video memory, using the
443 * memory handling/allocation routines provided during queue initialization
444 *
445 * If req->count is 0, all the memory will be freed instead.
446 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
447 * and the queue is not busy, memory will be reallocated.
448 *
449 * The return values from this function are intended to be directly returned
450 * from vidioc_reqbufs handler in driver.
451 */
452int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
453{
454 unsigned int num_buffers, num_planes;
e23ccc0a
PO
455 int ret = 0;
456
b25748fe
MS
457 if (q->fileio) {
458 dprintk(1, "reqbufs: file io in progress\n");
459 return -EBUSY;
460 }
461
e23ccc0a
PO
462 if (req->memory != V4L2_MEMORY_MMAP
463 && req->memory != V4L2_MEMORY_USERPTR) {
464 dprintk(1, "reqbufs: unsupported memory type\n");
465 return -EINVAL;
466 }
467
468 if (req->type != q->type) {
469 dprintk(1, "reqbufs: requested type is incorrect\n");
470 return -EINVAL;
471 }
472
473 if (q->streaming) {
474 dprintk(1, "reqbufs: streaming active\n");
475 return -EBUSY;
476 }
477
478 /*
479 * Make sure all the required memory ops for given memory type
480 * are available.
481 */
482 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
483 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
484 return -EINVAL;
485 }
486
487 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
488 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
489 return -EINVAL;
490 }
491
29e3fbd8 492 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
e23ccc0a
PO
493 /*
494 * We already have buffers allocated, so first check if they
495 * are not in use and can be freed.
496 */
497 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
498 dprintk(1, "reqbufs: memory in use, cannot free\n");
499 return -EBUSY;
500 }
501
72f1fc33 502 __vb2_queue_free(q);
29e3fbd8
MS
503
504 /*
505 * In case of REQBUFS(0) return immediately without calling
506 * driver's queue_setup() callback and allocating resources.
507 */
508 if (req->count == 0)
509 return 0;
e23ccc0a
PO
510 }
511
512 /*
513 * Make sure the requested values and current defaults are sane.
514 */
515 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
c1426bc7 516 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
e23ccc0a 517 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
13b14095 518 q->memory = req->memory;
e23ccc0a
PO
519
520 /*
521 * Ask the driver how many buffers and planes per buffer it requires.
522 * Driver also sets the size and allocator context for each plane.
523 */
524 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
c1426bc7 525 q->plane_sizes, q->alloc_ctx);
e23ccc0a
PO
526 if (ret)
527 return ret;
528
529 /* Finally, allocate buffers and video memory */
c1426bc7 530 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
66072d4f
MS
531 if (ret == 0) {
532 dprintk(1, "Memory allocation failed\n");
533 return -ENOMEM;
e23ccc0a
PO
534 }
535
536 /*
537 * Check if driver can handle the allocated number of buffers.
538 */
539 if (ret < num_buffers) {
540 unsigned int orig_num_buffers;
541
542 orig_num_buffers = num_buffers = ret;
543 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
c1426bc7 544 q->plane_sizes, q->alloc_ctx);
e23ccc0a
PO
545 if (ret)
546 goto free_mem;
547
548 if (orig_num_buffers < num_buffers) {
549 ret = -ENOMEM;
550 goto free_mem;
551 }
552
553 /*
554 * Ok, driver accepted smaller number of buffers.
555 */
556 ret = num_buffers;
557 }
558
e23ccc0a
PO
559 /*
560 * Return the number of successfully allocated buffers
561 * to the userspace.
562 */
563 req->count = ret;
564
565 return 0;
566
567free_mem:
568 __vb2_queue_free(q);
569 return ret;
570}
571EXPORT_SYMBOL_GPL(vb2_reqbufs);
572
573/**
574 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
575 * @vb: vb2_buffer to which the plane in question belongs to
576 * @plane_no: plane number for which the address is to be returned
577 *
578 * This function returns a kernel virtual address of a given plane if
579 * such a mapping exist, NULL otherwise.
580 */
581void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
582{
583 struct vb2_queue *q = vb->vb2_queue;
584
585 if (plane_no > vb->num_planes)
586 return NULL;
587
588 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
589
590}
591EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
592
593/**
594 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
595 * @vb: vb2_buffer to which the plane in question belongs to
596 * @plane_no: plane number for which the cookie is to be returned
597 *
598 * This function returns an allocator specific cookie for a given plane if
599 * available, NULL otherwise. The allocator should provide some simple static
600 * inline function, which would convert this cookie to the allocator specific
601 * type that can be used directly by the driver to access the buffer. This can
602 * be for example physical address, pointer to scatter list or IOMMU mapping.
603 */
604void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
605{
606 struct vb2_queue *q = vb->vb2_queue;
607
608 if (plane_no > vb->num_planes)
609 return NULL;
610
611 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
612}
613EXPORT_SYMBOL_GPL(vb2_plane_cookie);
614
615/**
616 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
617 * @vb: vb2_buffer returned from the driver
618 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
619 * or VB2_BUF_STATE_ERROR if the operation finished with an error
620 *
621 * This function should be called by the driver after a hardware operation on
622 * a buffer is finished and the buffer may be returned to userspace. The driver
623 * cannot use this buffer anymore until it is queued back to it by videobuf
624 * by the means of buf_queue callback. Only buffers previously queued to the
625 * driver by buf_queue can be passed to this function.
626 */
627void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
628{
629 struct vb2_queue *q = vb->vb2_queue;
630 unsigned long flags;
631
632 if (vb->state != VB2_BUF_STATE_ACTIVE)
633 return;
634
635 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
636 return;
637
638 dprintk(4, "Done processing on buffer %d, state: %d\n",
639 vb->v4l2_buf.index, vb->state);
640
641 /* Add the buffer to the done buffers list */
642 spin_lock_irqsave(&q->done_lock, flags);
643 vb->state = state;
644 list_add_tail(&vb->done_entry, &q->done_list);
645 atomic_dec(&q->queued_count);
646 spin_unlock_irqrestore(&q->done_lock, flags);
647
648 /* Inform any processes that may be waiting for buffers */
649 wake_up(&q->done_wq);
650}
651EXPORT_SYMBOL_GPL(vb2_buffer_done);
652
653/**
654 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
655 * a v4l2_buffer by the userspace
656 */
657static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
658 struct v4l2_plane *v4l2_planes)
659{
660 unsigned int plane;
661 int ret;
662
663 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
664 /*
665 * Verify that the userspace gave us a valid array for
666 * plane information.
667 */
668 ret = __verify_planes_array(vb, b);
669 if (ret)
670 return ret;
671
672 /* Fill in driver-provided information for OUTPUT types */
673 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
674 /*
675 * Will have to go up to b->length when API starts
676 * accepting variable number of planes.
677 */
678 for (plane = 0; plane < vb->num_planes; ++plane) {
679 v4l2_planes[plane].bytesused =
680 b->m.planes[plane].bytesused;
681 v4l2_planes[plane].data_offset =
682 b->m.planes[plane].data_offset;
683 }
684 }
685
686 if (b->memory == V4L2_MEMORY_USERPTR) {
687 for (plane = 0; plane < vb->num_planes; ++plane) {
688 v4l2_planes[plane].m.userptr =
689 b->m.planes[plane].m.userptr;
690 v4l2_planes[plane].length =
691 b->m.planes[plane].length;
692 }
693 }
694 } else {
695 /*
696 * Single-planar buffers do not use planes array,
697 * so fill in relevant v4l2_buffer struct fields instead.
698 * In videobuf we use our internal V4l2_planes struct for
699 * single-planar buffers as well, for simplicity.
700 */
701 if (V4L2_TYPE_IS_OUTPUT(b->type))
702 v4l2_planes[0].bytesused = b->bytesused;
703
704 if (b->memory == V4L2_MEMORY_USERPTR) {
705 v4l2_planes[0].m.userptr = b->m.userptr;
706 v4l2_planes[0].length = b->length;
707 }
708 }
709
710 vb->v4l2_buf.field = b->field;
711 vb->v4l2_buf.timestamp = b->timestamp;
ea42c8ec
MS
712 vb->v4l2_buf.input = b->input;
713 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
e23ccc0a
PO
714
715 return 0;
716}
717
718/**
719 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
720 */
721static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
722{
723 struct v4l2_plane planes[VIDEO_MAX_PLANES];
724 struct vb2_queue *q = vb->vb2_queue;
725 void *mem_priv;
726 unsigned int plane;
727 int ret;
728 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
729
730 /* Verify and copy relevant information provided by the userspace */
731 ret = __fill_vb2_buffer(vb, b, planes);
732 if (ret)
733 return ret;
734
735 for (plane = 0; plane < vb->num_planes; ++plane) {
736 /* Skip the plane if already verified */
737 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
738 && vb->v4l2_planes[plane].length == planes[plane].length)
739 continue;
740
741 dprintk(3, "qbuf: userspace address for plane %d changed, "
742 "reacquiring memory\n", plane);
743
c1426bc7
MS
744 /* Check if the provided plane buffer is large enough */
745 if (planes[plane].length < q->plane_sizes[plane]) {
746 ret = EINVAL;
747 goto err;
748 }
749
e23ccc0a
PO
750 /* Release previously acquired memory if present */
751 if (vb->planes[plane].mem_priv)
752 call_memop(q, plane, put_userptr,
753 vb->planes[plane].mem_priv);
754
755 vb->planes[plane].mem_priv = NULL;
c1426bc7
MS
756 vb->v4l2_planes[plane].m.userptr = 0;
757 vb->v4l2_planes[plane].length = 0;
e23ccc0a
PO
758
759 /* Acquire each plane's memory */
760 if (q->mem_ops->get_userptr) {
761 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
762 planes[plane].m.userptr,
763 planes[plane].length,
764 write);
765 if (IS_ERR(mem_priv)) {
766 dprintk(1, "qbuf: failed acquiring userspace "
767 "memory for plane %d\n", plane);
768 ret = PTR_ERR(mem_priv);
769 goto err;
770 }
771 vb->planes[plane].mem_priv = mem_priv;
772 }
773 }
774
775 /*
776 * Call driver-specific initialization on the newly acquired buffer,
777 * if provided.
778 */
779 ret = call_qop(q, buf_init, vb);
780 if (ret) {
781 dprintk(1, "qbuf: buffer initialization failed\n");
782 goto err;
783 }
784
785 /*
786 * Now that everything is in order, copy relevant information
787 * provided by userspace.
788 */
789 for (plane = 0; plane < vb->num_planes; ++plane)
790 vb->v4l2_planes[plane] = planes[plane];
791
792 return 0;
793err:
794 /* In case of errors, release planes that were already acquired */
c1426bc7
MS
795 for (plane = 0; plane < vb->num_planes; ++plane) {
796 if (vb->planes[plane].mem_priv)
797 call_memop(q, plane, put_userptr,
798 vb->planes[plane].mem_priv);
799 vb->planes[plane].mem_priv = NULL;
800 vb->v4l2_planes[plane].m.userptr = 0;
801 vb->v4l2_planes[plane].length = 0;
e23ccc0a
PO
802 }
803
804 return ret;
805}
806
807/**
808 * __qbuf_mmap() - handle qbuf of an MMAP buffer
809 */
810static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
811{
812 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
813}
814
815/**
816 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
817 */
818static void __enqueue_in_driver(struct vb2_buffer *vb)
819{
820 struct vb2_queue *q = vb->vb2_queue;
821
822 vb->state = VB2_BUF_STATE_ACTIVE;
823 atomic_inc(&q->queued_count);
824 q->ops->buf_queue(vb);
825}
826
827/**
828 * vb2_qbuf() - Queue a buffer from userspace
829 * @q: videobuf2 queue
830 * @b: buffer structure passed from userspace to vidioc_qbuf handler
831 * in driver
832 *
833 * Should be called from vidioc_qbuf ioctl handler of a driver.
834 * This function:
835 * 1) verifies the passed buffer,
836 * 2) calls buf_prepare callback in the driver (if provided), in which
837 * driver-specific buffer initialization can be performed,
838 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
839 * callback for processing.
840 *
841 * The return values from this function are intended to be directly returned
842 * from vidioc_qbuf handler in driver.
843 */
844int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
845{
846 struct vb2_buffer *vb;
847 int ret = 0;
848
b25748fe
MS
849 if (q->fileio) {
850 dprintk(1, "qbuf: file io in progress\n");
851 return -EBUSY;
852 }
853
e23ccc0a
PO
854 if (b->type != q->type) {
855 dprintk(1, "qbuf: invalid buffer type\n");
856 return -EINVAL;
857 }
858
859 if (b->index >= q->num_buffers) {
860 dprintk(1, "qbuf: buffer index out of range\n");
861 return -EINVAL;
862 }
863
864 vb = q->bufs[b->index];
865 if (NULL == vb) {
866 /* Should never happen */
867 dprintk(1, "qbuf: buffer is NULL\n");
868 return -EINVAL;
869 }
870
871 if (b->memory != q->memory) {
872 dprintk(1, "qbuf: invalid memory type\n");
873 return -EINVAL;
874 }
875
876 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
877 dprintk(1, "qbuf: buffer already in use\n");
878 return -EINVAL;
879 }
880
881 if (q->memory == V4L2_MEMORY_MMAP)
882 ret = __qbuf_mmap(vb, b);
883 else if (q->memory == V4L2_MEMORY_USERPTR)
884 ret = __qbuf_userptr(vb, b);
885 else {
886 WARN(1, "Invalid queue type\n");
887 return -EINVAL;
888 }
889
890 if (ret)
891 return ret;
892
893 ret = call_qop(q, buf_prepare, vb);
894 if (ret) {
895 dprintk(1, "qbuf: buffer preparation failed\n");
896 return ret;
897 }
898
899 /*
900 * Add to the queued buffers list, a buffer will stay on it until
901 * dequeued in dqbuf.
902 */
903 list_add_tail(&vb->queued_entry, &q->queued_list);
904 vb->state = VB2_BUF_STATE_QUEUED;
905
906 /*
907 * If already streaming, give the buffer to driver for processing.
908 * If not, the buffer will be given to driver on next streamon.
909 */
910 if (q->streaming)
911 __enqueue_in_driver(vb);
912
913 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
914 return 0;
915}
916EXPORT_SYMBOL_GPL(vb2_qbuf);
917
918/**
919 * __vb2_wait_for_done_vb() - wait for a buffer to become available
920 * for dequeuing
921 *
922 * Will sleep if required for nonblocking == false.
923 */
924static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
925{
926 /*
927 * All operations on vb_done_list are performed under done_lock
928 * spinlock protection. However, buffers may be removed from
929 * it and returned to userspace only while holding both driver's
930 * lock and the done_lock spinlock. Thus we can be sure that as
931 * long as we hold the driver's lock, the list will remain not
932 * empty if list_empty() check succeeds.
933 */
934
935 for (;;) {
936 int ret;
937
938 if (!q->streaming) {
939 dprintk(1, "Streaming off, will not wait for buffers\n");
940 return -EINVAL;
941 }
942
943 if (!list_empty(&q->done_list)) {
944 /*
945 * Found a buffer that we were waiting for.
946 */
947 break;
948 }
949
950 if (nonblocking) {
951 dprintk(1, "Nonblocking and no buffers to dequeue, "
952 "will not wait\n");
953 return -EAGAIN;
954 }
955
956 /*
957 * We are streaming and blocking, wait for another buffer to
958 * become ready or for streamoff. Driver's lock is released to
959 * allow streamoff or qbuf to be called while waiting.
960 */
961 call_qop(q, wait_prepare, q);
962
963 /*
964 * All locks have been released, it is safe to sleep now.
965 */
966 dprintk(3, "Will sleep waiting for buffers\n");
967 ret = wait_event_interruptible(q->done_wq,
968 !list_empty(&q->done_list) || !q->streaming);
969
970 /*
971 * We need to reevaluate both conditions again after reacquiring
972 * the locks or return an error if one occurred.
973 */
974 call_qop(q, wait_finish, q);
975 if (ret)
976 return ret;
977 }
978 return 0;
979}
980
981/**
982 * __vb2_get_done_vb() - get a buffer ready for dequeuing
983 *
984 * Will sleep if required for nonblocking == false.
985 */
986static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
987 int nonblocking)
988{
989 unsigned long flags;
990 int ret;
991
992 /*
993 * Wait for at least one buffer to become available on the done_list.
994 */
995 ret = __vb2_wait_for_done_vb(q, nonblocking);
996 if (ret)
997 return ret;
998
999 /*
1000 * Driver's lock has been held since we last verified that done_list
1001 * is not empty, so no need for another list_empty(done_list) check.
1002 */
1003 spin_lock_irqsave(&q->done_lock, flags);
1004 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1005 list_del(&(*vb)->done_entry);
1006 spin_unlock_irqrestore(&q->done_lock, flags);
1007
1008 return 0;
1009}
1010
1011/**
1012 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1013 * @q: videobuf2 queue
1014 *
1015 * This function will wait until all buffers that have been given to the driver
1016 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1017 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1018 * taken, for example from stop_streaming() callback.
1019 */
1020int vb2_wait_for_all_buffers(struct vb2_queue *q)
1021{
1022 if (!q->streaming) {
1023 dprintk(1, "Streaming off, will not wait for buffers\n");
1024 return -EINVAL;
1025 }
1026
1027 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1028 return 0;
1029}
1030EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1031
1032/**
1033 * vb2_dqbuf() - Dequeue a buffer to the userspace
1034 * @q: videobuf2 queue
1035 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1036 * in driver
1037 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1038 * buffers ready for dequeuing are present. Normally the driver
1039 * would be passing (file->f_flags & O_NONBLOCK) here
1040 *
1041 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1042 * This function:
1043 * 1) verifies the passed buffer,
1044 * 2) calls buf_finish callback in the driver (if provided), in which
1045 * driver can perform any additional operations that may be required before
1046 * returning the buffer to userspace, such as cache sync,
1047 * 3) the buffer struct members are filled with relevant information for
1048 * the userspace.
1049 *
1050 * The return values from this function are intended to be directly returned
1051 * from vidioc_dqbuf handler in driver.
1052 */
1053int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1054{
1055 struct vb2_buffer *vb = NULL;
1056 int ret;
1057
b25748fe
MS
1058 if (q->fileio) {
1059 dprintk(1, "dqbuf: file io in progress\n");
1060 return -EBUSY;
1061 }
1062
e23ccc0a
PO
1063 if (b->type != q->type) {
1064 dprintk(1, "dqbuf: invalid buffer type\n");
1065 return -EINVAL;
1066 }
1067
1068 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1069 if (ret < 0) {
1070 dprintk(1, "dqbuf: error getting next done buffer\n");
1071 return ret;
1072 }
1073
1074 ret = call_qop(q, buf_finish, vb);
1075 if (ret) {
1076 dprintk(1, "dqbuf: buffer finish failed\n");
1077 return ret;
1078 }
1079
1080 switch (vb->state) {
1081 case VB2_BUF_STATE_DONE:
1082 dprintk(3, "dqbuf: Returning done buffer\n");
1083 break;
1084 case VB2_BUF_STATE_ERROR:
1085 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1086 break;
1087 default:
1088 dprintk(1, "dqbuf: Invalid buffer state\n");
1089 return -EINVAL;
1090 }
1091
1092 /* Fill buffer information for the userspace */
1093 __fill_v4l2_buffer(vb, b);
1094 /* Remove from videobuf queue */
1095 list_del(&vb->queued_entry);
1096
1097 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1098 vb->v4l2_buf.index, vb->state);
1099
1100 vb->state = VB2_BUF_STATE_DEQUEUED;
1101 return 0;
1102}
1103EXPORT_SYMBOL_GPL(vb2_dqbuf);
1104
1105/**
1106 * vb2_streamon - start streaming
1107 * @q: videobuf2 queue
1108 * @type: type argument passed from userspace to vidioc_streamon handler
1109 *
1110 * Should be called from vidioc_streamon handler of a driver.
1111 * This function:
1112 * 1) verifies current state
1113 * 2) starts streaming and passes any previously queued buffers to the driver
1114 *
1115 * The return values from this function are intended to be directly returned
1116 * from vidioc_streamon handler in the driver.
1117 */
1118int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1119{
1120 struct vb2_buffer *vb;
5db2c3ba 1121 int ret;
e23ccc0a 1122
b25748fe
MS
1123 if (q->fileio) {
1124 dprintk(1, "streamon: file io in progress\n");
1125 return -EBUSY;
1126 }
1127
e23ccc0a
PO
1128 if (type != q->type) {
1129 dprintk(1, "streamon: invalid stream type\n");
1130 return -EINVAL;
1131 }
1132
1133 if (q->streaming) {
1134 dprintk(1, "streamon: already streaming\n");
1135 return -EBUSY;
1136 }
1137
1138 /*
1139 * Cannot start streaming on an OUTPUT device if no buffers have
1140 * been queued yet.
1141 */
1142 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1143 if (list_empty(&q->queued_list)) {
1144 dprintk(1, "streamon: no output buffers queued\n");
1145 return -EINVAL;
1146 }
1147 }
1148
e23ccc0a
PO
1149 /*
1150 * Let driver notice that streaming state has been enabled.
1151 */
5db2c3ba
PO
1152 ret = call_qop(q, start_streaming, q);
1153 if (ret) {
1154 dprintk(1, "streamon: driver refused to start streaming\n");
1155 return ret;
1156 }
1157
1158 q->streaming = 1;
e23ccc0a
PO
1159
1160 /*
1161 * If any buffers were queued before streamon,
1162 * we can now pass them to driver for processing.
1163 */
1164 list_for_each_entry(vb, &q->queued_list, queued_entry)
1165 __enqueue_in_driver(vb);
1166
1167 dprintk(3, "Streamon successful\n");
1168 return 0;
1169}
1170EXPORT_SYMBOL_GPL(vb2_streamon);
1171
1172/**
1173 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1174 *
1175 * Removes all queued buffers from driver's queue and all buffers queued by
1176 * userspace from videobuf's queue. Returns to state after reqbufs.
1177 */
1178static void __vb2_queue_cancel(struct vb2_queue *q)
1179{
1180 unsigned int i;
1181
1182 /*
1183 * Tell driver to stop all transactions and release all queued
1184 * buffers.
1185 */
1186 if (q->streaming)
1187 call_qop(q, stop_streaming, q);
1188 q->streaming = 0;
1189
1190 /*
1191 * Remove all buffers from videobuf's list...
1192 */
1193 INIT_LIST_HEAD(&q->queued_list);
1194 /*
1195 * ...and done list; userspace will not receive any buffers it
1196 * has not already dequeued before initiating cancel.
1197 */
1198 INIT_LIST_HEAD(&q->done_list);
afdea8ba 1199 atomic_set(&q->queued_count, 0);
e23ccc0a
PO
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{
b25748fe
MS
1226 if (q->fileio) {
1227 dprintk(1, "streamoff: file io in progress\n");
1228 return -EBUSY;
1229 }
1230
e23ccc0a
PO
1231 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
b25748fe
MS
1354static int __vb2_init_fileio(struct vb2_queue *q, int read);
1355static int __vb2_cleanup_fileio(struct vb2_queue *q);
e23ccc0a
PO
1356
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;
b25748fe 1376 unsigned int ret;
e23ccc0a
PO
1377 struct vb2_buffer *vb = NULL;
1378
b25748fe 1379 /*
4ffabdb3 1380 * Start file I/O emulator only if streaming API has not been used yet.
b25748fe
MS
1381 */
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)
4ffabdb3 1386 return POLLERR;
b25748fe
MS
1387 }
1388 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1389 ret = __vb2_init_fileio(q, 0);
1390 if (ret)
4ffabdb3 1391 return POLLERR;
b25748fe
MS
1392 /*
1393 * Write to OUTPUT queue can be done immediately.
1394 */
1395 return POLLOUT | POLLWRNORM;
1396 }
1397 }
1398
e23ccc0a
PO
1399 /*
1400 * 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{
b25748fe 1469 __vb2_cleanup_fileio(q);
e23ccc0a
PO
1470 __vb2_queue_cancel(q);
1471 __vb2_queue_free(q);
1472}
1473EXPORT_SYMBOL_GPL(vb2_queue_release);
1474
b25748fe
MS
1475/**
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
08b99e26 1661 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
b25748fe
MS
1662 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;
08b99e26 1720 dprintk(5, "reducing read count: %zd\n", count);
b25748fe
MS
1721 }
1722
1723 /*
1724 * Transfer data to userspace.
1725 */
08b99e26 1726 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
b25748fe
MS
1727 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
e23ccc0a 1825MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
95072084 1826MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
e23ccc0a 1827MODULE_LICENSE("GPL");
This page took 0.175137 seconds and 5 git commands to generate.