[media] videobuf2: Fix CodingStyle
[deliverable/linux.git] / drivers / media / v4l2-core / 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
95213ceb
HV
22#include <media/v4l2-dev.h>
23#include <media/v4l2-fh.h>
24#include <media/v4l2-event.h>
e23ccc0a
PO
25#include <media/videobuf2-core.h>
26
27static int debug;
28module_param(debug, int, 0644);
29
30#define dprintk(level, fmt, arg...) \
31 do { \
32 if (debug >= level) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
34 } while (0)
35
5931ffe3 36#define call_memop(q, op, args...) \
e23ccc0a
PO
37 (((q)->mem_ops->op) ? \
38 ((q)->mem_ops->op(args)) : 0)
39
40#define call_qop(q, op, args...) \
41 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
42
1b18e7a0 43#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
2d86401c 44 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
1b18e7a0
SA
45 V4L2_BUF_FLAG_PREPARED | \
46 V4L2_BUF_FLAG_TIMESTAMP_MASK)
ea42c8ec 47
e23ccc0a
PO
48/**
49 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
50 */
c1426bc7 51static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
e23ccc0a
PO
52{
53 struct vb2_queue *q = vb->vb2_queue;
54 void *mem_priv;
55 int plane;
56
7f841459
MCC
57 /*
58 * Allocate memory for all planes in this buffer
59 * NOTE: mmapped areas should be page aligned
60 */
e23ccc0a 61 for (plane = 0; plane < vb->num_planes; ++plane) {
7f841459
MCC
62 unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
63
5931ffe3 64 mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
7f841459 65 size, q->gfp_flags);
62a79436 66 if (IS_ERR_OR_NULL(mem_priv))
e23ccc0a
PO
67 goto free;
68
69 /* Associate allocator private data with this plane */
70 vb->planes[plane].mem_priv = mem_priv;
c1426bc7 71 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
e23ccc0a
PO
72 }
73
74 return 0;
75free:
76 /* Free already allocated memory if one of the allocations failed */
a00d0266 77 for (; plane > 0; --plane) {
5931ffe3 78 call_memop(q, put, vb->planes[plane - 1].mem_priv);
a00d0266
MS
79 vb->planes[plane - 1].mem_priv = NULL;
80 }
e23ccc0a
PO
81
82 return -ENOMEM;
83}
84
85/**
86 * __vb2_buf_mem_free() - free memory of the given buffer
87 */
88static void __vb2_buf_mem_free(struct vb2_buffer *vb)
89{
90 struct vb2_queue *q = vb->vb2_queue;
91 unsigned int plane;
92
93 for (plane = 0; plane < vb->num_planes; ++plane) {
5931ffe3 94 call_memop(q, put, vb->planes[plane].mem_priv);
e23ccc0a 95 vb->planes[plane].mem_priv = NULL;
a00d0266
MS
96 dprintk(3, "Freed plane %d of buffer %d\n", plane,
97 vb->v4l2_buf.index);
e23ccc0a
PO
98 }
99}
100
101/**
102 * __vb2_buf_userptr_put() - release userspace memory associated with
103 * a USERPTR buffer
104 */
105static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
106{
107 struct vb2_queue *q = vb->vb2_queue;
108 unsigned int plane;
109
110 for (plane = 0; plane < vb->num_planes; ++plane) {
a00d0266
MS
111 if (vb->planes[plane].mem_priv)
112 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
113 vb->planes[plane].mem_priv = NULL;
e23ccc0a
PO
114 }
115}
116
c5384048
SS
117/**
118 * __vb2_plane_dmabuf_put() - release memory associated with
119 * a DMABUF shared plane
120 */
121static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
122{
123 if (!p->mem_priv)
124 return;
125
126 if (p->dbuf_mapped)
127 call_memop(q, unmap_dmabuf, p->mem_priv);
128
129 call_memop(q, detach_dmabuf, p->mem_priv);
130 dma_buf_put(p->dbuf);
131 memset(p, 0, sizeof(*p));
132}
133
134/**
135 * __vb2_buf_dmabuf_put() - release memory associated with
136 * a DMABUF shared buffer
137 */
138static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
139{
140 struct vb2_queue *q = vb->vb2_queue;
141 unsigned int plane;
142
143 for (plane = 0; plane < vb->num_planes; ++plane)
144 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
145}
146
a5e3d743
HV
147/**
148 * __setup_lengths() - setup initial lengths for every plane in
149 * every buffer on the queue
150 */
151static void __setup_lengths(struct vb2_queue *q, unsigned int n)
152{
153 unsigned int buffer, plane;
154 struct vb2_buffer *vb;
155
156 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
157 vb = q->bufs[buffer];
158 if (!vb)
159 continue;
160
161 for (plane = 0; plane < vb->num_planes; ++plane)
162 vb->v4l2_planes[plane].length = q->plane_sizes[plane];
163 }
164}
165
e23ccc0a
PO
166/**
167 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
168 * every buffer on the queue
169 */
2d86401c 170static void __setup_offsets(struct vb2_queue *q, unsigned int n)
e23ccc0a
PO
171{
172 unsigned int buffer, plane;
173 struct vb2_buffer *vb;
2d86401c 174 unsigned long off;
e23ccc0a 175
2d86401c
GL
176 if (q->num_buffers) {
177 struct v4l2_plane *p;
178 vb = q->bufs[q->num_buffers - 1];
179 p = &vb->v4l2_planes[vb->num_planes - 1];
180 off = PAGE_ALIGN(p->m.mem_offset + p->length);
181 } else {
182 off = 0;
183 }
184
185 for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
e23ccc0a
PO
186 vb = q->bufs[buffer];
187 if (!vb)
188 continue;
189
190 for (plane = 0; plane < vb->num_planes; ++plane) {
191 vb->v4l2_planes[plane].m.mem_offset = off;
192
193 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
194 buffer, plane, off);
195
196 off += vb->v4l2_planes[plane].length;
197 off = PAGE_ALIGN(off);
198 }
199 }
200}
201
202/**
203 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
204 * video buffer memory for all buffers/planes on the queue and initializes the
205 * queue
206 *
207 * Returns the number of buffers successfully allocated.
208 */
209static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
c1426bc7 210 unsigned int num_buffers, unsigned int num_planes)
e23ccc0a
PO
211{
212 unsigned int buffer;
213 struct vb2_buffer *vb;
214 int ret;
215
216 for (buffer = 0; buffer < num_buffers; ++buffer) {
217 /* Allocate videobuf buffer structures */
218 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
219 if (!vb) {
220 dprintk(1, "Memory alloc for buffer struct failed\n");
221 break;
222 }
223
224 /* Length stores number of planes for multiplanar buffers */
225 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
226 vb->v4l2_buf.length = num_planes;
227
228 vb->state = VB2_BUF_STATE_DEQUEUED;
229 vb->vb2_queue = q;
230 vb->num_planes = num_planes;
2d86401c 231 vb->v4l2_buf.index = q->num_buffers + buffer;
e23ccc0a
PO
232 vb->v4l2_buf.type = q->type;
233 vb->v4l2_buf.memory = memory;
234
235 /* Allocate video buffer memory for the MMAP type */
236 if (memory == V4L2_MEMORY_MMAP) {
c1426bc7 237 ret = __vb2_buf_mem_alloc(vb);
e23ccc0a
PO
238 if (ret) {
239 dprintk(1, "Failed allocating memory for "
240 "buffer %d\n", buffer);
241 kfree(vb);
242 break;
243 }
244 /*
245 * Call the driver-provided buffer initialization
246 * callback, if given. An error in initialization
247 * results in queue setup failure.
248 */
249 ret = call_qop(q, buf_init, vb);
250 if (ret) {
251 dprintk(1, "Buffer %d %p initialization"
252 " failed\n", buffer, vb);
253 __vb2_buf_mem_free(vb);
254 kfree(vb);
255 break;
256 }
257 }
258
2d86401c 259 q->bufs[q->num_buffers + buffer] = vb;
e23ccc0a
PO
260 }
261
a5e3d743 262 __setup_lengths(q, buffer);
dc77523c
PZ
263 if (memory == V4L2_MEMORY_MMAP)
264 __setup_offsets(q, buffer);
e23ccc0a
PO
265
266 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
2d86401c 267 buffer, num_planes);
e23ccc0a
PO
268
269 return buffer;
270}
271
272/**
273 * __vb2_free_mem() - release all video buffer memory for a given queue
274 */
2d86401c 275static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
e23ccc0a
PO
276{
277 unsigned int buffer;
278 struct vb2_buffer *vb;
279
2d86401c
GL
280 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
281 ++buffer) {
e23ccc0a
PO
282 vb = q->bufs[buffer];
283 if (!vb)
284 continue;
285
286 /* Free MMAP buffers or release USERPTR buffers */
287 if (q->memory == V4L2_MEMORY_MMAP)
288 __vb2_buf_mem_free(vb);
c5384048
SS
289 else if (q->memory == V4L2_MEMORY_DMABUF)
290 __vb2_buf_dmabuf_put(vb);
e23ccc0a
PO
291 else
292 __vb2_buf_userptr_put(vb);
293 }
294}
295
296/**
2d86401c
GL
297 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
298 * related information, if no buffers are left return the queue to an
299 * uninitialized state. Might be called even if the queue has already been freed.
e23ccc0a 300 */
2d86401c 301static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
e23ccc0a
PO
302{
303 unsigned int buffer;
304
305 /* Call driver-provided cleanup function for each buffer, if provided */
306 if (q->ops->buf_cleanup) {
2d86401c
GL
307 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
308 ++buffer) {
e23ccc0a
PO
309 if (NULL == q->bufs[buffer])
310 continue;
311 q->ops->buf_cleanup(q->bufs[buffer]);
312 }
313 }
314
315 /* Release video buffer memory */
2d86401c 316 __vb2_free_mem(q, buffers);
e23ccc0a
PO
317
318 /* Free videobuf buffers */
2d86401c
GL
319 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
320 ++buffer) {
e23ccc0a
PO
321 kfree(q->bufs[buffer]);
322 q->bufs[buffer] = NULL;
323 }
324
2d86401c
GL
325 q->num_buffers -= buffers;
326 if (!q->num_buffers)
327 q->memory = 0;
bd50d999 328 INIT_LIST_HEAD(&q->queued_list);
e23ccc0a
PO
329}
330
331/**
332 * __verify_planes_array() - verify that the planes array passed in struct
333 * v4l2_buffer from userspace can be safely used
334 */
2d86401c 335static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
e23ccc0a 336{
32a77260
HV
337 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
338 return 0;
339
e23ccc0a
PO
340 /* Is memory for copying plane information present? */
341 if (NULL == b->m.planes) {
342 dprintk(1, "Multi-planar buffer passed but "
343 "planes array not provided\n");
344 return -EINVAL;
345 }
346
347 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
348 dprintk(1, "Incorrect planes array length, "
349 "expected %d, got %d\n", vb->num_planes, b->length);
350 return -EINVAL;
351 }
352
353 return 0;
354}
355
8023ed09
LP
356/**
357 * __verify_length() - Verify that the bytesused value for each plane fits in
358 * the plane length and that the data offset doesn't exceed the bytesused value.
359 */
360static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
361{
362 unsigned int length;
363 unsigned int plane;
364
365 if (!V4L2_TYPE_IS_OUTPUT(b->type))
366 return 0;
367
368 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
369 for (plane = 0; plane < vb->num_planes; ++plane) {
370 length = (b->memory == V4L2_MEMORY_USERPTR)
371 ? b->m.planes[plane].length
372 : vb->v4l2_planes[plane].length;
373
374 if (b->m.planes[plane].bytesused > length)
375 return -EINVAL;
3c5c23c5
SN
376
377 if (b->m.planes[plane].data_offset > 0 &&
378 b->m.planes[plane].data_offset >=
8023ed09
LP
379 b->m.planes[plane].bytesused)
380 return -EINVAL;
381 }
382 } else {
383 length = (b->memory == V4L2_MEMORY_USERPTR)
384 ? b->length : vb->v4l2_planes[0].length;
385
386 if (b->bytesused > length)
387 return -EINVAL;
388 }
389
390 return 0;
391}
392
25a27d91
MS
393/**
394 * __buffer_in_use() - return true if the buffer is in use and
395 * the queue cannot be freed (by the means of REQBUFS(0)) call
396 */
397static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
398{
399 unsigned int plane;
400 for (plane = 0; plane < vb->num_planes; ++plane) {
2c2dd6ac 401 void *mem_priv = vb->planes[plane].mem_priv;
25a27d91
MS
402 /*
403 * If num_users() has not been provided, call_memop
404 * will return 0, apparently nobody cares about this
405 * case anyway. If num_users() returns more than 1,
406 * we are not the only user of the plane's memory.
407 */
5931ffe3 408 if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
25a27d91
MS
409 return true;
410 }
411 return false;
412}
413
414/**
415 * __buffers_in_use() - return true if any buffers on the queue are in use and
416 * the queue cannot be freed (by the means of REQBUFS(0)) call
417 */
418static bool __buffers_in_use(struct vb2_queue *q)
419{
420 unsigned int buffer;
421 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
422 if (__buffer_in_use(q, q->bufs[buffer]))
423 return true;
424 }
425 return false;
426}
427
e23ccc0a
PO
428/**
429 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
430 * returned to userspace
431 */
32a77260 432static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
e23ccc0a
PO
433{
434 struct vb2_queue *q = vb->vb2_queue;
e23ccc0a 435
2b719d7b 436 /* Copy back data such as timestamp, flags, etc. */
e23ccc0a 437 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
2b719d7b 438 b->reserved2 = vb->v4l2_buf.reserved2;
e23ccc0a
PO
439 b->reserved = vb->v4l2_buf.reserved;
440
441 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
e23ccc0a
PO
442 /*
443 * Fill in plane-related data if userspace provided an array
32a77260 444 * for it. The caller has already verified memory and size.
e23ccc0a 445 */
3c0b6061 446 b->length = vb->num_planes;
e23ccc0a
PO
447 memcpy(b->m.planes, vb->v4l2_planes,
448 b->length * sizeof(struct v4l2_plane));
449 } else {
450 /*
451 * We use length and offset in v4l2_planes array even for
452 * single-planar buffers, but userspace does not.
453 */
454 b->length = vb->v4l2_planes[0].length;
455 b->bytesused = vb->v4l2_planes[0].bytesused;
456 if (q->memory == V4L2_MEMORY_MMAP)
457 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
458 else if (q->memory == V4L2_MEMORY_USERPTR)
459 b->m.userptr = vb->v4l2_planes[0].m.userptr;
c5384048
SS
460 else if (q->memory == V4L2_MEMORY_DMABUF)
461 b->m.fd = vb->v4l2_planes[0].m.fd;
e23ccc0a
PO
462 }
463
ea42c8ec
MS
464 /*
465 * Clear any buffer state related flags.
466 */
1b18e7a0 467 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
6aa69f99 468 b->flags |= q->timestamp_type;
e23ccc0a
PO
469
470 switch (vb->state) {
471 case VB2_BUF_STATE_QUEUED:
472 case VB2_BUF_STATE_ACTIVE:
473 b->flags |= V4L2_BUF_FLAG_QUEUED;
474 break;
475 case VB2_BUF_STATE_ERROR:
476 b->flags |= V4L2_BUF_FLAG_ERROR;
477 /* fall through */
478 case VB2_BUF_STATE_DONE:
479 b->flags |= V4L2_BUF_FLAG_DONE;
480 break;
ebc087d0 481 case VB2_BUF_STATE_PREPARED:
2d86401c
GL
482 b->flags |= V4L2_BUF_FLAG_PREPARED;
483 break;
b18a8ff2 484 case VB2_BUF_STATE_PREPARING:
2d86401c 485 case VB2_BUF_STATE_DEQUEUED:
e23ccc0a
PO
486 /* nothing */
487 break;
488 }
489
25a27d91 490 if (__buffer_in_use(q, vb))
e23ccc0a 491 b->flags |= V4L2_BUF_FLAG_MAPPED;
e23ccc0a
PO
492}
493
494/**
495 * vb2_querybuf() - query video buffer information
496 * @q: videobuf queue
497 * @b: buffer struct passed from userspace to vidioc_querybuf handler
498 * in driver
499 *
500 * Should be called from vidioc_querybuf ioctl handler in driver.
501 * This function will verify the passed v4l2_buffer structure and fill the
502 * relevant information for the userspace.
503 *
504 * The return values from this function are intended to be directly returned
505 * from vidioc_querybuf handler in driver.
506 */
507int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
508{
509 struct vb2_buffer *vb;
32a77260 510 int ret;
e23ccc0a
PO
511
512 if (b->type != q->type) {
513 dprintk(1, "querybuf: wrong buffer type\n");
514 return -EINVAL;
515 }
516
517 if (b->index >= q->num_buffers) {
518 dprintk(1, "querybuf: buffer index out of range\n");
519 return -EINVAL;
520 }
521 vb = q->bufs[b->index];
32a77260
HV
522 ret = __verify_planes_array(vb, b);
523 if (!ret)
524 __fill_v4l2_buffer(vb, b);
525 return ret;
e23ccc0a
PO
526}
527EXPORT_SYMBOL(vb2_querybuf);
528
529/**
530 * __verify_userptr_ops() - verify that all memory operations required for
531 * USERPTR queue type have been provided
532 */
533static int __verify_userptr_ops(struct vb2_queue *q)
534{
535 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
536 !q->mem_ops->put_userptr)
537 return -EINVAL;
538
539 return 0;
540}
541
542/**
543 * __verify_mmap_ops() - verify that all memory operations required for
544 * MMAP queue type have been provided
545 */
546static int __verify_mmap_ops(struct vb2_queue *q)
547{
548 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
549 !q->mem_ops->put || !q->mem_ops->mmap)
550 return -EINVAL;
551
552 return 0;
553}
554
c5384048
SS
555/**
556 * __verify_dmabuf_ops() - verify that all memory operations required for
557 * DMABUF queue type have been provided
558 */
559static int __verify_dmabuf_ops(struct vb2_queue *q)
560{
561 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
562 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
563 !q->mem_ops->unmap_dmabuf)
564 return -EINVAL;
565
566 return 0;
567}
568
e23ccc0a 569/**
37d9ed94
HV
570 * __verify_memory_type() - Check whether the memory type and buffer type
571 * passed to a buffer operation are compatible with the queue.
572 */
573static int __verify_memory_type(struct vb2_queue *q,
574 enum v4l2_memory memory, enum v4l2_buf_type type)
575{
c5384048
SS
576 if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
577 memory != V4L2_MEMORY_DMABUF) {
37d9ed94
HV
578 dprintk(1, "reqbufs: unsupported memory type\n");
579 return -EINVAL;
580 }
581
582 if (type != q->type) {
583 dprintk(1, "reqbufs: requested type is incorrect\n");
584 return -EINVAL;
585 }
586
587 /*
588 * Make sure all the required memory ops for given memory type
589 * are available.
590 */
591 if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
592 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
593 return -EINVAL;
594 }
595
596 if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
597 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
598 return -EINVAL;
599 }
600
c5384048
SS
601 if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
602 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
603 return -EINVAL;
604 }
605
37d9ed94
HV
606 /*
607 * Place the busy tests at the end: -EBUSY can be ignored when
608 * create_bufs is called with count == 0, but count == 0 should still
609 * do the memory and type validation.
610 */
611 if (q->fileio) {
612 dprintk(1, "reqbufs: file io in progress\n");
613 return -EBUSY;
614 }
615 return 0;
616}
617
618/**
619 * __reqbufs() - Initiate streaming
e23ccc0a
PO
620 * @q: videobuf2 queue
621 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
622 *
623 * Should be called from vidioc_reqbufs ioctl handler of a driver.
624 * This function:
625 * 1) verifies streaming parameters passed from the userspace,
626 * 2) sets up the queue,
627 * 3) negotiates number of buffers and planes per buffer with the driver
628 * to be used during streaming,
629 * 4) allocates internal buffer structures (struct vb2_buffer), according to
630 * the agreed parameters,
631 * 5) for MMAP memory type, allocates actual video memory, using the
632 * memory handling/allocation routines provided during queue initialization
633 *
634 * If req->count is 0, all the memory will be freed instead.
635 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
636 * and the queue is not busy, memory will be reallocated.
637 *
638 * The return values from this function are intended to be directly returned
639 * from vidioc_reqbufs handler in driver.
640 */
37d9ed94 641static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
e23ccc0a 642{
2d86401c 643 unsigned int num_buffers, allocated_buffers, num_planes = 0;
37d9ed94 644 int ret;
e23ccc0a
PO
645
646 if (q->streaming) {
647 dprintk(1, "reqbufs: streaming active\n");
648 return -EBUSY;
649 }
650
29e3fbd8 651 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
e23ccc0a
PO
652 /*
653 * We already have buffers allocated, so first check if they
654 * are not in use and can be freed.
655 */
656 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
657 dprintk(1, "reqbufs: memory in use, cannot free\n");
658 return -EBUSY;
659 }
660
2d86401c 661 __vb2_queue_free(q, q->num_buffers);
29e3fbd8
MS
662
663 /*
664 * In case of REQBUFS(0) return immediately without calling
665 * driver's queue_setup() callback and allocating resources.
666 */
667 if (req->count == 0)
668 return 0;
e23ccc0a
PO
669 }
670
671 /*
672 * Make sure the requested values and current defaults are sane.
673 */
674 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
c1426bc7 675 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
e23ccc0a 676 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
13b14095 677 q->memory = req->memory;
e23ccc0a
PO
678
679 /*
680 * Ask the driver how many buffers and planes per buffer it requires.
681 * Driver also sets the size and allocator context for each plane.
682 */
fc714e70 683 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
c1426bc7 684 q->plane_sizes, q->alloc_ctx);
e23ccc0a
PO
685 if (ret)
686 return ret;
687
688 /* Finally, allocate buffers and video memory */
c1426bc7 689 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
66072d4f
MS
690 if (ret == 0) {
691 dprintk(1, "Memory allocation failed\n");
692 return -ENOMEM;
e23ccc0a
PO
693 }
694
2d86401c
GL
695 allocated_buffers = ret;
696
e23ccc0a
PO
697 /*
698 * Check if driver can handle the allocated number of buffers.
699 */
2d86401c
GL
700 if (allocated_buffers < num_buffers) {
701 num_buffers = allocated_buffers;
e23ccc0a 702
fc714e70
GL
703 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
704 &num_planes, q->plane_sizes, q->alloc_ctx);
e23ccc0a 705
2d86401c 706 if (!ret && allocated_buffers < num_buffers)
e23ccc0a 707 ret = -ENOMEM;
e23ccc0a
PO
708
709 /*
2d86401c
GL
710 * Either the driver has accepted a smaller number of buffers,
711 * or .queue_setup() returned an error
e23ccc0a 712 */
2d86401c
GL
713 }
714
715 q->num_buffers = allocated_buffers;
716
717 if (ret < 0) {
718 __vb2_queue_free(q, allocated_buffers);
719 return ret;
e23ccc0a
PO
720 }
721
e23ccc0a
PO
722 /*
723 * Return the number of successfully allocated buffers
724 * to the userspace.
725 */
2d86401c 726 req->count = allocated_buffers;
e23ccc0a
PO
727
728 return 0;
e23ccc0a 729}
37d9ed94
HV
730
731/**
732 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
733 * type values.
734 * @q: videobuf2 queue
735 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
736 */
737int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
738{
739 int ret = __verify_memory_type(q, req->memory, req->type);
740
741 return ret ? ret : __reqbufs(q, req);
742}
e23ccc0a
PO
743EXPORT_SYMBOL_GPL(vb2_reqbufs);
744
2d86401c 745/**
37d9ed94 746 * __create_bufs() - Allocate buffers and any required auxiliary structs
2d86401c
GL
747 * @q: videobuf2 queue
748 * @create: creation parameters, passed from userspace to vidioc_create_bufs
749 * handler in driver
750 *
751 * Should be called from vidioc_create_bufs ioctl handler of a driver.
752 * This function:
753 * 1) verifies parameter sanity
754 * 2) calls the .queue_setup() queue operation
755 * 3) performs any necessary memory allocations
756 *
757 * The return values from this function are intended to be directly returned
758 * from vidioc_create_bufs handler in driver.
759 */
37d9ed94 760static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
2d86401c
GL
761{
762 unsigned int num_planes = 0, num_buffers, allocated_buffers;
37d9ed94 763 int ret;
2d86401c
GL
764
765 if (q->num_buffers == VIDEO_MAX_FRAME) {
766 dprintk(1, "%s(): maximum number of buffers already allocated\n",
767 __func__);
768 return -ENOBUFS;
769 }
770
2d86401c
GL
771 if (!q->num_buffers) {
772 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
773 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
774 q->memory = create->memory;
775 }
776
777 num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
778
779 /*
780 * Ask the driver, whether the requested number of buffers, planes per
781 * buffer and their sizes are acceptable
782 */
783 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
784 &num_planes, q->plane_sizes, q->alloc_ctx);
785 if (ret)
786 return ret;
787
788 /* Finally, allocate buffers and video memory */
789 ret = __vb2_queue_alloc(q, create->memory, num_buffers,
790 num_planes);
f05393d2
HV
791 if (ret == 0) {
792 dprintk(1, "Memory allocation failed\n");
793 return -ENOMEM;
2d86401c
GL
794 }
795
796 allocated_buffers = ret;
797
798 /*
799 * Check if driver can handle the so far allocated number of buffers.
800 */
801 if (ret < num_buffers) {
802 num_buffers = ret;
803
804 /*
805 * q->num_buffers contains the total number of buffers, that the
806 * queue driver has set up
807 */
808 ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
809 &num_planes, q->plane_sizes, q->alloc_ctx);
810
811 if (!ret && allocated_buffers < num_buffers)
812 ret = -ENOMEM;
813
814 /*
815 * Either the driver has accepted a smaller number of buffers,
816 * or .queue_setup() returned an error
817 */
818 }
819
820 q->num_buffers += allocated_buffers;
821
822 if (ret < 0) {
823 __vb2_queue_free(q, allocated_buffers);
f05393d2 824 return -ENOMEM;
2d86401c
GL
825 }
826
827 /*
828 * Return the number of successfully allocated buffers
829 * to the userspace.
830 */
831 create->count = allocated_buffers;
832
833 return 0;
834}
37d9ed94
HV
835
836/**
53aa3b19
NT
837 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
838 * memory and type values.
37d9ed94
HV
839 * @q: videobuf2 queue
840 * @create: creation parameters, passed from userspace to vidioc_create_bufs
841 * handler in driver
842 */
843int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
844{
845 int ret = __verify_memory_type(q, create->memory, create->format.type);
846
847 create->index = q->num_buffers;
f05393d2
HV
848 if (create->count == 0)
849 return ret != -EBUSY ? ret : 0;
37d9ed94
HV
850 return ret ? ret : __create_bufs(q, create);
851}
2d86401c
GL
852EXPORT_SYMBOL_GPL(vb2_create_bufs);
853
e23ccc0a
PO
854/**
855 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
856 * @vb: vb2_buffer to which the plane in question belongs to
857 * @plane_no: plane number for which the address is to be returned
858 *
859 * This function returns a kernel virtual address of a given plane if
860 * such a mapping exist, NULL otherwise.
861 */
862void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
863{
864 struct vb2_queue *q = vb->vb2_queue;
865
a00d0266 866 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
e23ccc0a
PO
867 return NULL;
868
5931ffe3 869 return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
e23ccc0a
PO
870
871}
872EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
873
874/**
875 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
876 * @vb: vb2_buffer to which the plane in question belongs to
877 * @plane_no: plane number for which the cookie is to be returned
878 *
879 * This function returns an allocator specific cookie for a given plane if
880 * available, NULL otherwise. The allocator should provide some simple static
881 * inline function, which would convert this cookie to the allocator specific
882 * type that can be used directly by the driver to access the buffer. This can
883 * be for example physical address, pointer to scatter list or IOMMU mapping.
884 */
885void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
886{
887 struct vb2_queue *q = vb->vb2_queue;
888
a00d0266 889 if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
e23ccc0a
PO
890 return NULL;
891
5931ffe3 892 return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
e23ccc0a
PO
893}
894EXPORT_SYMBOL_GPL(vb2_plane_cookie);
895
896/**
897 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
898 * @vb: vb2_buffer returned from the driver
899 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
900 * or VB2_BUF_STATE_ERROR if the operation finished with an error
901 *
902 * This function should be called by the driver after a hardware operation on
903 * a buffer is finished and the buffer may be returned to userspace. The driver
904 * cannot use this buffer anymore until it is queued back to it by videobuf
905 * by the means of buf_queue callback. Only buffers previously queued to the
906 * driver by buf_queue can be passed to this function.
907 */
908void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
909{
910 struct vb2_queue *q = vb->vb2_queue;
911 unsigned long flags;
3e0c2f20 912 unsigned int plane;
e23ccc0a
PO
913
914 if (vb->state != VB2_BUF_STATE_ACTIVE)
915 return;
916
917 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
918 return;
919
920 dprintk(4, "Done processing on buffer %d, state: %d\n",
9b6f5dc0 921 vb->v4l2_buf.index, state);
e23ccc0a 922
3e0c2f20
MS
923 /* sync buffers */
924 for (plane = 0; plane < vb->num_planes; ++plane)
925 call_memop(q, finish, vb->planes[plane].mem_priv);
926
e23ccc0a
PO
927 /* Add the buffer to the done buffers list */
928 spin_lock_irqsave(&q->done_lock, flags);
929 vb->state = state;
930 list_add_tail(&vb->done_entry, &q->done_list);
931 atomic_dec(&q->queued_count);
932 spin_unlock_irqrestore(&q->done_lock, flags);
933
934 /* Inform any processes that may be waiting for buffers */
935 wake_up(&q->done_wq);
936}
937EXPORT_SYMBOL_GPL(vb2_buffer_done);
938
939/**
32a77260
HV
940 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
941 * v4l2_buffer by the userspace. The caller has already verified that struct
942 * v4l2_buffer has a valid number of planes.
e23ccc0a 943 */
32a77260 944static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
e23ccc0a
PO
945 struct v4l2_plane *v4l2_planes)
946{
947 unsigned int plane;
e23ccc0a
PO
948
949 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
e23ccc0a
PO
950 /* Fill in driver-provided information for OUTPUT types */
951 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
952 /*
953 * Will have to go up to b->length when API starts
954 * accepting variable number of planes.
955 */
956 for (plane = 0; plane < vb->num_planes; ++plane) {
957 v4l2_planes[plane].bytesused =
958 b->m.planes[plane].bytesused;
959 v4l2_planes[plane].data_offset =
960 b->m.planes[plane].data_offset;
961 }
962 }
963
964 if (b->memory == V4L2_MEMORY_USERPTR) {
965 for (plane = 0; plane < vb->num_planes; ++plane) {
966 v4l2_planes[plane].m.userptr =
967 b->m.planes[plane].m.userptr;
968 v4l2_planes[plane].length =
969 b->m.planes[plane].length;
970 }
971 }
c5384048
SS
972 if (b->memory == V4L2_MEMORY_DMABUF) {
973 for (plane = 0; plane < vb->num_planes; ++plane) {
974 v4l2_planes[plane].m.fd =
975 b->m.planes[plane].m.fd;
976 v4l2_planes[plane].length =
977 b->m.planes[plane].length;
978 v4l2_planes[plane].data_offset =
979 b->m.planes[plane].data_offset;
980 }
981 }
e23ccc0a
PO
982 } else {
983 /*
984 * Single-planar buffers do not use planes array,
985 * so fill in relevant v4l2_buffer struct fields instead.
986 * In videobuf we use our internal V4l2_planes struct for
987 * single-planar buffers as well, for simplicity.
988 */
ac706bf7 989 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
e23ccc0a 990 v4l2_planes[0].bytesused = b->bytesused;
ac706bf7
LP
991 v4l2_planes[0].data_offset = 0;
992 }
e23ccc0a
PO
993
994 if (b->memory == V4L2_MEMORY_USERPTR) {
995 v4l2_planes[0].m.userptr = b->m.userptr;
996 v4l2_planes[0].length = b->length;
997 }
c5384048
SS
998
999 if (b->memory == V4L2_MEMORY_DMABUF) {
1000 v4l2_planes[0].m.fd = b->m.fd;
1001 v4l2_planes[0].length = b->length;
1002 v4l2_planes[0].data_offset = 0;
1003 }
1004
e23ccc0a
PO
1005 }
1006
1007 vb->v4l2_buf.field = b->field;
1008 vb->v4l2_buf.timestamp = b->timestamp;
1b18e7a0 1009 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
e23ccc0a
PO
1010}
1011
1012/**
1013 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1014 */
2d86401c 1015static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
e23ccc0a
PO
1016{
1017 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1018 struct vb2_queue *q = vb->vb2_queue;
1019 void *mem_priv;
1020 unsigned int plane;
1021 int ret;
1022 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1023
32a77260
HV
1024 /* Copy relevant information provided by the userspace */
1025 __fill_vb2_buffer(vb, b, planes);
e23ccc0a
PO
1026
1027 for (plane = 0; plane < vb->num_planes; ++plane) {
1028 /* Skip the plane if already verified */
f0b7c7fc
MS
1029 if (vb->v4l2_planes[plane].m.userptr &&
1030 vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
e23ccc0a
PO
1031 && vb->v4l2_planes[plane].length == planes[plane].length)
1032 continue;
1033
1034 dprintk(3, "qbuf: userspace address for plane %d changed, "
1035 "reacquiring memory\n", plane);
1036
c1426bc7
MS
1037 /* Check if the provided plane buffer is large enough */
1038 if (planes[plane].length < q->plane_sizes[plane]) {
2484a7e2
SWK
1039 dprintk(1, "qbuf: provided buffer size %u is less than "
1040 "setup size %u for plane %d\n",
1041 planes[plane].length,
1042 q->plane_sizes[plane], plane);
4c2625db 1043 ret = -EINVAL;
c1426bc7
MS
1044 goto err;
1045 }
1046
e23ccc0a
PO
1047 /* Release previously acquired memory if present */
1048 if (vb->planes[plane].mem_priv)
5931ffe3 1049 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
e23ccc0a
PO
1050
1051 vb->planes[plane].mem_priv = NULL;
c1426bc7
MS
1052 vb->v4l2_planes[plane].m.userptr = 0;
1053 vb->v4l2_planes[plane].length = 0;
e23ccc0a
PO
1054
1055 /* Acquire each plane's memory */
a00d0266
MS
1056 mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
1057 planes[plane].m.userptr,
1058 planes[plane].length, write);
1059 if (IS_ERR_OR_NULL(mem_priv)) {
1060 dprintk(1, "qbuf: failed acquiring userspace "
e23ccc0a 1061 "memory for plane %d\n", plane);
a00d0266
MS
1062 ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
1063 goto err;
e23ccc0a 1064 }
a00d0266 1065 vb->planes[plane].mem_priv = mem_priv;
e23ccc0a
PO
1066 }
1067
1068 /*
1069 * Call driver-specific initialization on the newly acquired buffer,
1070 * if provided.
1071 */
1072 ret = call_qop(q, buf_init, vb);
1073 if (ret) {
1074 dprintk(1, "qbuf: buffer initialization failed\n");
1075 goto err;
1076 }
1077
1078 /*
1079 * Now that everything is in order, copy relevant information
1080 * provided by userspace.
1081 */
1082 for (plane = 0; plane < vb->num_planes; ++plane)
1083 vb->v4l2_planes[plane] = planes[plane];
1084
1085 return 0;
1086err:
1087 /* In case of errors, release planes that were already acquired */
c1426bc7
MS
1088 for (plane = 0; plane < vb->num_planes; ++plane) {
1089 if (vb->planes[plane].mem_priv)
5931ffe3 1090 call_memop(q, put_userptr, vb->planes[plane].mem_priv);
c1426bc7
MS
1091 vb->planes[plane].mem_priv = NULL;
1092 vb->v4l2_planes[plane].m.userptr = 0;
1093 vb->v4l2_planes[plane].length = 0;
e23ccc0a
PO
1094 }
1095
1096 return ret;
1097}
1098
1099/**
1100 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1101 */
2d86401c 1102static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
e23ccc0a 1103{
32a77260
HV
1104 __fill_vb2_buffer(vb, b, vb->v4l2_planes);
1105 return 0;
e23ccc0a
PO
1106}
1107
c5384048
SS
1108/**
1109 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1110 */
1111static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
1112{
1113 struct v4l2_plane planes[VIDEO_MAX_PLANES];
1114 struct vb2_queue *q = vb->vb2_queue;
1115 void *mem_priv;
1116 unsigned int plane;
1117 int ret;
1118 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
1119
1120 /* Verify and copy relevant information provided by the userspace */
1121 __fill_vb2_buffer(vb, b, planes);
1122
1123 for (plane = 0; plane < vb->num_planes; ++plane) {
1124 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1125
1126 if (IS_ERR_OR_NULL(dbuf)) {
1127 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1128 plane);
1129 ret = -EINVAL;
1130 goto err;
1131 }
1132
1133 /* use DMABUF size if length is not provided */
1134 if (planes[plane].length == 0)
1135 planes[plane].length = dbuf->size;
1136
1137 if (planes[plane].length < planes[plane].data_offset +
1138 q->plane_sizes[plane]) {
77c0782e
SWK
1139 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1140 plane);
c5384048
SS
1141 ret = -EINVAL;
1142 goto err;
1143 }
1144
1145 /* Skip the plane if already verified */
1146 if (dbuf == vb->planes[plane].dbuf &&
1147 vb->v4l2_planes[plane].length == planes[plane].length) {
1148 dma_buf_put(dbuf);
1149 continue;
1150 }
1151
1152 dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
1153
1154 /* Release previously acquired memory if present */
1155 __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
1156 memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
1157
1158 /* Acquire each plane's memory */
1159 mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
1160 dbuf, planes[plane].length, write);
1161 if (IS_ERR(mem_priv)) {
1162 dprintk(1, "qbuf: failed to attach dmabuf\n");
1163 ret = PTR_ERR(mem_priv);
1164 dma_buf_put(dbuf);
1165 goto err;
1166 }
1167
1168 vb->planes[plane].dbuf = dbuf;
1169 vb->planes[plane].mem_priv = mem_priv;
1170 }
1171
1172 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1173 * really we want to do this just before the DMA, not while queueing
1174 * the buffer(s)..
1175 */
1176 for (plane = 0; plane < vb->num_planes; ++plane) {
1177 ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
1178 if (ret) {
1179 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1180 plane);
1181 goto err;
1182 }
1183 vb->planes[plane].dbuf_mapped = 1;
1184 }
1185
1186 /*
1187 * Call driver-specific initialization on the newly acquired buffer,
1188 * if provided.
1189 */
1190 ret = call_qop(q, buf_init, vb);
1191 if (ret) {
1192 dprintk(1, "qbuf: buffer initialization failed\n");
1193 goto err;
1194 }
1195
1196 /*
1197 * Now that everything is in order, copy relevant information
1198 * provided by userspace.
1199 */
1200 for (plane = 0; plane < vb->num_planes; ++plane)
1201 vb->v4l2_planes[plane] = planes[plane];
1202
1203 return 0;
1204err:
1205 /* In case of errors, release planes that were already acquired */
1206 __vb2_buf_dmabuf_put(vb);
1207
1208 return ret;
1209}
1210
e23ccc0a
PO
1211/**
1212 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1213 */
1214static void __enqueue_in_driver(struct vb2_buffer *vb)
1215{
1216 struct vb2_queue *q = vb->vb2_queue;
3e0c2f20 1217 unsigned int plane;
e23ccc0a
PO
1218
1219 vb->state = VB2_BUF_STATE_ACTIVE;
1220 atomic_inc(&q->queued_count);
3e0c2f20
MS
1221
1222 /* sync buffers */
1223 for (plane = 0; plane < vb->num_planes; ++plane)
1224 call_memop(q, prepare, vb->planes[plane].mem_priv);
1225
e23ccc0a
PO
1226 q->ops->buf_queue(vb);
1227}
1228
2d86401c 1229static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
ebc087d0
GL
1230{
1231 struct vb2_queue *q = vb->vb2_queue;
b18a8ff2 1232 struct rw_semaphore *mmap_sem;
ebc087d0
GL
1233 int ret;
1234
8023ed09 1235 ret = __verify_length(vb, b);
3a9621b0
SN
1236 if (ret < 0) {
1237 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1238 __func__, ret);
8023ed09 1239 return ret;
3a9621b0 1240 }
8023ed09 1241
b18a8ff2 1242 vb->state = VB2_BUF_STATE_PREPARING;
ebc087d0
GL
1243 switch (q->memory) {
1244 case V4L2_MEMORY_MMAP:
1245 ret = __qbuf_mmap(vb, b);
1246 break;
1247 case V4L2_MEMORY_USERPTR:
b18a8ff2 1248 /*
f103b5d6
MCC
1249 * In case of user pointer buffers vb2 allocators need to get
1250 * direct access to userspace pages. This requires getting
1251 * the mmap semaphore for read access in the current process
1252 * structure. The same semaphore is taken before calling mmap
1253 * operation, while both qbuf/prepare_buf and mmap are called
1254 * by the driver or v4l2 core with the driver's lock held.
1255 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1256 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1257 * the videobuf2 core releases the driver's lock, takes
1258 * mmap_sem and then takes the driver's lock again.
b18a8ff2
HV
1259 */
1260 mmap_sem = &current->mm->mmap_sem;
1261 call_qop(q, wait_prepare, q);
1262 down_read(mmap_sem);
1263 call_qop(q, wait_finish, q);
1264
ebc087d0 1265 ret = __qbuf_userptr(vb, b);
b18a8ff2
HV
1266
1267 up_read(mmap_sem);
ebc087d0 1268 break;
c5384048
SS
1269 case V4L2_MEMORY_DMABUF:
1270 ret = __qbuf_dmabuf(vb, b);
1271 break;
ebc087d0
GL
1272 default:
1273 WARN(1, "Invalid queue type\n");
1274 ret = -EINVAL;
1275 }
1276
1277 if (!ret)
1278 ret = call_qop(q, buf_prepare, vb);
1279 if (ret)
1280 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
b18a8ff2 1281 vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
ebc087d0
GL
1282
1283 return ret;
1284}
1285
012043b8
LP
1286static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1287 const char *opname,
1288 int (*handler)(struct vb2_queue *,
1289 struct v4l2_buffer *,
1290 struct vb2_buffer *))
2d86401c
GL
1291{
1292 struct vb2_buffer *vb;
1293 int ret;
1294
1295 if (q->fileio) {
012043b8 1296 dprintk(1, "%s(): file io in progress\n", opname);
b18a8ff2 1297 return -EBUSY;
2d86401c
GL
1298 }
1299
1300 if (b->type != q->type) {
012043b8 1301 dprintk(1, "%s(): invalid buffer type\n", opname);
b18a8ff2 1302 return -EINVAL;
2d86401c
GL
1303 }
1304
1305 if (b->index >= q->num_buffers) {
012043b8 1306 dprintk(1, "%s(): buffer index out of range\n", opname);
b18a8ff2 1307 return -EINVAL;
2d86401c
GL
1308 }
1309
1310 vb = q->bufs[b->index];
1311 if (NULL == vb) {
1312 /* Should never happen */
012043b8 1313 dprintk(1, "%s(): buffer is NULL\n", opname);
b18a8ff2 1314 return -EINVAL;
2d86401c
GL
1315 }
1316
1317 if (b->memory != q->memory) {
012043b8 1318 dprintk(1, "%s(): invalid memory type\n", opname);
b18a8ff2 1319 return -EINVAL;
2d86401c
GL
1320 }
1321
32a77260 1322 ret = __verify_planes_array(vb, b);
012043b8 1323 if (ret)
b18a8ff2 1324 return ret;
a517cca6 1325
012043b8 1326 ret = handler(q, b, vb);
b18a8ff2
HV
1327 if (!ret) {
1328 /* Fill buffer information for the userspace */
1329 __fill_v4l2_buffer(vb, b);
2d86401c 1330
b18a8ff2
HV
1331 dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
1332 }
a517cca6 1333 return ret;
2d86401c 1334}
012043b8
LP
1335
1336static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
1337 struct vb2_buffer *vb)
1338{
1339 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1340 dprintk(1, "%s(): invalid buffer state %d\n", __func__,
1341 vb->state);
1342 return -EINVAL;
1343 }
1344
1345 return __buf_prepare(vb, b);
1346}
2d86401c 1347
e23ccc0a 1348/**
012043b8 1349 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
e23ccc0a 1350 * @q: videobuf2 queue
012043b8
LP
1351 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1352 * handler in driver
e23ccc0a 1353 *
012043b8 1354 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
e23ccc0a
PO
1355 * This function:
1356 * 1) verifies the passed buffer,
012043b8
LP
1357 * 2) calls buf_prepare callback in the driver (if provided), in which
1358 * driver-specific buffer initialization can be performed,
e23ccc0a
PO
1359 *
1360 * The return values from this function are intended to be directly returned
012043b8 1361 * from vidioc_prepare_buf handler in driver.
e23ccc0a 1362 */
012043b8 1363int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
e23ccc0a 1364{
012043b8
LP
1365 return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
1366}
1367EXPORT_SYMBOL_GPL(vb2_prepare_buf);
e23ccc0a 1368
012043b8
LP
1369static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
1370 struct vb2_buffer *vb)
1371{
1372 int ret;
e23ccc0a 1373
ebc087d0
GL
1374 switch (vb->state) {
1375 case VB2_BUF_STATE_DEQUEUED:
1376 ret = __buf_prepare(vb, b);
1377 if (ret)
012043b8 1378 return ret;
ebc087d0
GL
1379 case VB2_BUF_STATE_PREPARED:
1380 break;
b18a8ff2
HV
1381 case VB2_BUF_STATE_PREPARING:
1382 dprintk(1, "qbuf: buffer still being prepared\n");
1383 return -EINVAL;
ebc087d0 1384 default:
e23ccc0a 1385 dprintk(1, "qbuf: buffer already in use\n");
012043b8 1386 return -EINVAL;
e23ccc0a
PO
1387 }
1388
e23ccc0a
PO
1389 /*
1390 * Add to the queued buffers list, a buffer will stay on it until
1391 * dequeued in dqbuf.
1392 */
1393 list_add_tail(&vb->queued_entry, &q->queued_list);
1394 vb->state = VB2_BUF_STATE_QUEUED;
1395
1396 /*
1397 * If already streaming, give the buffer to driver for processing.
1398 * If not, the buffer will be given to driver on next streamon.
1399 */
1400 if (q->streaming)
1401 __enqueue_in_driver(vb);
1402
012043b8
LP
1403 return 0;
1404}
21db3e07 1405
012043b8
LP
1406/**
1407 * vb2_qbuf() - Queue a buffer from userspace
1408 * @q: videobuf2 queue
1409 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1410 * in driver
1411 *
1412 * Should be called from vidioc_qbuf ioctl handler of a driver.
1413 * This function:
1414 * 1) verifies the passed buffer,
1415 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1416 * which driver-specific buffer initialization can be performed,
1417 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1418 * callback for processing.
1419 *
1420 * The return values from this function are intended to be directly returned
1421 * from vidioc_qbuf handler in driver.
1422 */
1423int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1424{
1425 return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
e23ccc0a
PO
1426}
1427EXPORT_SYMBOL_GPL(vb2_qbuf);
1428
1429/**
1430 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1431 * for dequeuing
1432 *
1433 * Will sleep if required for nonblocking == false.
1434 */
1435static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1436{
1437 /*
1438 * All operations on vb_done_list are performed under done_lock
1439 * spinlock protection. However, buffers may be removed from
1440 * it and returned to userspace only while holding both driver's
1441 * lock and the done_lock spinlock. Thus we can be sure that as
1442 * long as we hold the driver's lock, the list will remain not
1443 * empty if list_empty() check succeeds.
1444 */
1445
1446 for (;;) {
1447 int ret;
1448
1449 if (!q->streaming) {
1450 dprintk(1, "Streaming off, will not wait for buffers\n");
1451 return -EINVAL;
1452 }
1453
1454 if (!list_empty(&q->done_list)) {
1455 /*
1456 * Found a buffer that we were waiting for.
1457 */
1458 break;
1459 }
1460
1461 if (nonblocking) {
1462 dprintk(1, "Nonblocking and no buffers to dequeue, "
1463 "will not wait\n");
1464 return -EAGAIN;
1465 }
1466
1467 /*
1468 * We are streaming and blocking, wait for another buffer to
1469 * become ready or for streamoff. Driver's lock is released to
1470 * allow streamoff or qbuf to be called while waiting.
1471 */
1472 call_qop(q, wait_prepare, q);
1473
1474 /*
1475 * All locks have been released, it is safe to sleep now.
1476 */
1477 dprintk(3, "Will sleep waiting for buffers\n");
1478 ret = wait_event_interruptible(q->done_wq,
1479 !list_empty(&q->done_list) || !q->streaming);
1480
1481 /*
1482 * We need to reevaluate both conditions again after reacquiring
1483 * the locks or return an error if one occurred.
1484 */
1485 call_qop(q, wait_finish, q);
32a77260
HV
1486 if (ret) {
1487 dprintk(1, "Sleep was interrupted\n");
e23ccc0a 1488 return ret;
32a77260 1489 }
e23ccc0a
PO
1490 }
1491 return 0;
1492}
1493
1494/**
1495 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1496 *
1497 * Will sleep if required for nonblocking == false.
1498 */
1499static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
32a77260 1500 struct v4l2_buffer *b, int nonblocking)
e23ccc0a
PO
1501{
1502 unsigned long flags;
1503 int ret;
1504
1505 /*
1506 * Wait for at least one buffer to become available on the done_list.
1507 */
1508 ret = __vb2_wait_for_done_vb(q, nonblocking);
1509 if (ret)
1510 return ret;
1511
1512 /*
1513 * Driver's lock has been held since we last verified that done_list
1514 * is not empty, so no need for another list_empty(done_list) check.
1515 */
1516 spin_lock_irqsave(&q->done_lock, flags);
1517 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
32a77260
HV
1518 /*
1519 * Only remove the buffer from done_list if v4l2_buffer can handle all
1520 * the planes.
1521 */
1522 ret = __verify_planes_array(*vb, b);
1523 if (!ret)
1524 list_del(&(*vb)->done_entry);
e23ccc0a
PO
1525 spin_unlock_irqrestore(&q->done_lock, flags);
1526
32a77260 1527 return ret;
e23ccc0a
PO
1528}
1529
1530/**
1531 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1532 * @q: videobuf2 queue
1533 *
1534 * This function will wait until all buffers that have been given to the driver
1535 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1536 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1537 * taken, for example from stop_streaming() callback.
1538 */
1539int vb2_wait_for_all_buffers(struct vb2_queue *q)
1540{
1541 if (!q->streaming) {
1542 dprintk(1, "Streaming off, will not wait for buffers\n");
1543 return -EINVAL;
1544 }
1545
1546 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1547 return 0;
1548}
1549EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1550
c5384048
SS
1551/**
1552 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1553 */
1554static void __vb2_dqbuf(struct vb2_buffer *vb)
1555{
1556 struct vb2_queue *q = vb->vb2_queue;
1557 unsigned int i;
1558
1559 /* nothing to do if the buffer is already dequeued */
1560 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1561 return;
1562
1563 vb->state = VB2_BUF_STATE_DEQUEUED;
1564
1565 /* unmap DMABUF buffer */
1566 if (q->memory == V4L2_MEMORY_DMABUF)
1567 for (i = 0; i < vb->num_planes; ++i) {
1568 if (!vb->planes[i].dbuf_mapped)
1569 continue;
1570 call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
1571 vb->planes[i].dbuf_mapped = 0;
1572 }
1573}
1574
e23ccc0a
PO
1575/**
1576 * vb2_dqbuf() - Dequeue a buffer to the userspace
1577 * @q: videobuf2 queue
1578 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1579 * in driver
1580 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1581 * buffers ready for dequeuing are present. Normally the driver
1582 * would be passing (file->f_flags & O_NONBLOCK) here
1583 *
1584 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1585 * This function:
1586 * 1) verifies the passed buffer,
1587 * 2) calls buf_finish callback in the driver (if provided), in which
1588 * driver can perform any additional operations that may be required before
1589 * returning the buffer to userspace, such as cache sync,
1590 * 3) the buffer struct members are filled with relevant information for
1591 * the userspace.
1592 *
1593 * The return values from this function are intended to be directly returned
1594 * from vidioc_dqbuf handler in driver.
1595 */
1596int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1597{
1598 struct vb2_buffer *vb = NULL;
1599 int ret;
1600
b25748fe
MS
1601 if (q->fileio) {
1602 dprintk(1, "dqbuf: file io in progress\n");
1603 return -EBUSY;
1604 }
1605
e23ccc0a
PO
1606 if (b->type != q->type) {
1607 dprintk(1, "dqbuf: invalid buffer type\n");
1608 return -EINVAL;
1609 }
32a77260
HV
1610 ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
1611 if (ret < 0)
e23ccc0a 1612 return ret;
e23ccc0a
PO
1613
1614 ret = call_qop(q, buf_finish, vb);
1615 if (ret) {
1616 dprintk(1, "dqbuf: buffer finish failed\n");
1617 return ret;
1618 }
1619
1620 switch (vb->state) {
1621 case VB2_BUF_STATE_DONE:
1622 dprintk(3, "dqbuf: Returning done buffer\n");
1623 break;
1624 case VB2_BUF_STATE_ERROR:
1625 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1626 break;
1627 default:
1628 dprintk(1, "dqbuf: Invalid buffer state\n");
1629 return -EINVAL;
1630 }
1631
1632 /* Fill buffer information for the userspace */
1633 __fill_v4l2_buffer(vb, b);
1634 /* Remove from videobuf queue */
1635 list_del(&vb->queued_entry);
c5384048
SS
1636 /* go back to dequeued state */
1637 __vb2_dqbuf(vb);
e23ccc0a
PO
1638
1639 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1640 vb->v4l2_buf.index, vb->state);
1641
e23ccc0a
PO
1642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(vb2_dqbuf);
1645
bd323e28
MS
1646/**
1647 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1648 *
1649 * Removes all queued buffers from driver's queue and all buffers queued by
1650 * userspace from videobuf's queue. Returns to state after reqbufs.
1651 */
1652static void __vb2_queue_cancel(struct vb2_queue *q)
1653{
1654 unsigned int i;
1655
1656 /*
1657 * Tell driver to stop all transactions and release all queued
1658 * buffers.
1659 */
1660 if (q->streaming)
1661 call_qop(q, stop_streaming, q);
1662 q->streaming = 0;
1663
1664 /*
1665 * Remove all buffers from videobuf's list...
1666 */
1667 INIT_LIST_HEAD(&q->queued_list);
1668 /*
1669 * ...and done list; userspace will not receive any buffers it
1670 * has not already dequeued before initiating cancel.
1671 */
1672 INIT_LIST_HEAD(&q->done_list);
1673 atomic_set(&q->queued_count, 0);
1674 wake_up_all(&q->done_wq);
1675
1676 /*
1677 * Reinitialize all buffers for next use.
1678 */
1679 for (i = 0; i < q->num_buffers; ++i)
c5384048 1680 __vb2_dqbuf(q->bufs[i]);
bd323e28
MS
1681}
1682
e23ccc0a
PO
1683/**
1684 * vb2_streamon - start streaming
1685 * @q: videobuf2 queue
1686 * @type: type argument passed from userspace to vidioc_streamon handler
1687 *
1688 * Should be called from vidioc_streamon handler of a driver.
1689 * This function:
1690 * 1) verifies current state
bd323e28 1691 * 2) passes any previously queued buffers to the driver and starts streaming
e23ccc0a
PO
1692 *
1693 * The return values from this function are intended to be directly returned
1694 * from vidioc_streamon handler in the driver.
1695 */
1696int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1697{
1698 struct vb2_buffer *vb;
5db2c3ba 1699 int ret;
e23ccc0a 1700
b25748fe
MS
1701 if (q->fileio) {
1702 dprintk(1, "streamon: file io in progress\n");
1703 return -EBUSY;
1704 }
1705
e23ccc0a
PO
1706 if (type != q->type) {
1707 dprintk(1, "streamon: invalid stream type\n");
1708 return -EINVAL;
1709 }
1710
1711 if (q->streaming) {
f956035c
RR
1712 dprintk(3, "streamon successful: already streaming\n");
1713 return 0;
e23ccc0a
PO
1714 }
1715
1716 /*
bd323e28
MS
1717 * If any buffers were queued before streamon,
1718 * we can now pass them to driver for processing.
e23ccc0a 1719 */
bd323e28
MS
1720 list_for_each_entry(vb, &q->queued_list, queued_entry)
1721 __enqueue_in_driver(vb);
e23ccc0a 1722
e23ccc0a
PO
1723 /*
1724 * Let driver notice that streaming state has been enabled.
1725 */
bd323e28 1726 ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
5db2c3ba
PO
1727 if (ret) {
1728 dprintk(1, "streamon: driver refused to start streaming\n");
bd323e28 1729 __vb2_queue_cancel(q);
5db2c3ba
PO
1730 return ret;
1731 }
1732
1733 q->streaming = 1;
e23ccc0a 1734
e23ccc0a
PO
1735 dprintk(3, "Streamon successful\n");
1736 return 0;
1737}
1738EXPORT_SYMBOL_GPL(vb2_streamon);
1739
e23ccc0a
PO
1740
1741/**
1742 * vb2_streamoff - stop streaming
1743 * @q: videobuf2 queue
1744 * @type: type argument passed from userspace to vidioc_streamoff handler
1745 *
1746 * Should be called from vidioc_streamoff handler of a driver.
1747 * This function:
1748 * 1) verifies current state,
1749 * 2) stop streaming and dequeues any queued buffers, including those previously
1750 * passed to the driver (after waiting for the driver to finish).
1751 *
1752 * This call can be used for pausing playback.
1753 * The return values from this function are intended to be directly returned
1754 * from vidioc_streamoff handler in the driver
1755 */
1756int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1757{
b25748fe
MS
1758 if (q->fileio) {
1759 dprintk(1, "streamoff: file io in progress\n");
1760 return -EBUSY;
1761 }
1762
e23ccc0a
PO
1763 if (type != q->type) {
1764 dprintk(1, "streamoff: invalid stream type\n");
1765 return -EINVAL;
1766 }
1767
1768 if (!q->streaming) {
f956035c
RR
1769 dprintk(3, "streamoff successful: not streaming\n");
1770 return 0;
e23ccc0a
PO
1771 }
1772
1773 /*
1774 * Cancel will pause streaming and remove all buffers from the driver
1775 * and videobuf, effectively returning control over them to userspace.
1776 */
1777 __vb2_queue_cancel(q);
1778
1779 dprintk(3, "Streamoff successful\n");
1780 return 0;
1781}
1782EXPORT_SYMBOL_GPL(vb2_streamoff);
1783
1784/**
1785 * __find_plane_by_offset() - find plane associated with the given offset off
1786 */
1787static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1788 unsigned int *_buffer, unsigned int *_plane)
1789{
1790 struct vb2_buffer *vb;
1791 unsigned int buffer, plane;
1792
1793 /*
1794 * Go over all buffers and their planes, comparing the given offset
1795 * with an offset assigned to each plane. If a match is found,
1796 * return its buffer and plane numbers.
1797 */
1798 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1799 vb = q->bufs[buffer];
1800
1801 for (plane = 0; plane < vb->num_planes; ++plane) {
1802 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1803 *_buffer = buffer;
1804 *_plane = plane;
1805 return 0;
1806 }
1807 }
1808 }
1809
1810 return -EINVAL;
1811}
1812
83ae7c5a
TS
1813/**
1814 * vb2_expbuf() - Export a buffer as a file descriptor
1815 * @q: videobuf2 queue
1816 * @eb: export buffer structure passed from userspace to vidioc_expbuf
1817 * handler in driver
1818 *
1819 * The return values from this function are intended to be directly returned
1820 * from vidioc_expbuf handler in driver.
1821 */
1822int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
1823{
1824 struct vb2_buffer *vb = NULL;
1825 struct vb2_plane *vb_plane;
1826 int ret;
1827 struct dma_buf *dbuf;
1828
1829 if (q->memory != V4L2_MEMORY_MMAP) {
1830 dprintk(1, "Queue is not currently set up for mmap\n");
1831 return -EINVAL;
1832 }
1833
1834 if (!q->mem_ops->get_dmabuf) {
1835 dprintk(1, "Queue does not support DMA buffer exporting\n");
1836 return -EINVAL;
1837 }
1838
ea3aba84
PZ
1839 if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
1840 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
83ae7c5a
TS
1841 return -EINVAL;
1842 }
1843
1844 if (eb->type != q->type) {
1845 dprintk(1, "qbuf: invalid buffer type\n");
1846 return -EINVAL;
1847 }
1848
1849 if (eb->index >= q->num_buffers) {
1850 dprintk(1, "buffer index out of range\n");
1851 return -EINVAL;
1852 }
1853
1854 vb = q->bufs[eb->index];
1855
1856 if (eb->plane >= vb->num_planes) {
1857 dprintk(1, "buffer plane out of range\n");
1858 return -EINVAL;
1859 }
1860
1861 vb_plane = &vb->planes[eb->plane];
1862
ea3aba84 1863 dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
83ae7c5a
TS
1864 if (IS_ERR_OR_NULL(dbuf)) {
1865 dprintk(1, "Failed to export buffer %d, plane %d\n",
1866 eb->index, eb->plane);
1867 return -EINVAL;
1868 }
1869
ea3aba84 1870 ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
83ae7c5a
TS
1871 if (ret < 0) {
1872 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1873 eb->index, eb->plane, ret);
1874 dma_buf_put(dbuf);
1875 return ret;
1876 }
1877
1878 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1879 eb->index, eb->plane, ret);
1880 eb->fd = ret;
1881
1882 return 0;
1883}
1884EXPORT_SYMBOL_GPL(vb2_expbuf);
1885
e23ccc0a
PO
1886/**
1887 * vb2_mmap() - map video buffers into application address space
1888 * @q: videobuf2 queue
1889 * @vma: vma passed to the mmap file operation handler in the driver
1890 *
1891 * Should be called from mmap file operation handler of a driver.
1892 * This function maps one plane of one of the available video buffers to
1893 * userspace. To map whole video memory allocated on reqbufs, this function
1894 * has to be called once per each plane per each buffer previously allocated.
1895 *
1896 * When the userspace application calls mmap, it passes to it an offset returned
1897 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
1898 * a "cookie", which is then used to identify the plane to be mapped.
1899 * This function finds a plane with a matching offset and a mapping is performed
1900 * by the means of a provided memory operation.
1901 *
1902 * The return values from this function are intended to be directly returned
1903 * from the mmap handler in driver.
1904 */
1905int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1906{
1907 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
e23ccc0a
PO
1908 struct vb2_buffer *vb;
1909 unsigned int buffer, plane;
1910 int ret;
7f841459 1911 unsigned long length;
e23ccc0a
PO
1912
1913 if (q->memory != V4L2_MEMORY_MMAP) {
1914 dprintk(1, "Queue is not currently set up for mmap\n");
1915 return -EINVAL;
1916 }
1917
1918 /*
1919 * Check memory area access mode.
1920 */
1921 if (!(vma->vm_flags & VM_SHARED)) {
1922 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1923 return -EINVAL;
1924 }
1925 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1926 if (!(vma->vm_flags & VM_WRITE)) {
1927 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1928 return -EINVAL;
1929 }
1930 } else {
1931 if (!(vma->vm_flags & VM_READ)) {
1932 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1933 return -EINVAL;
1934 }
1935 }
1936
1937 /*
1938 * Find the plane corresponding to the offset passed by userspace.
1939 */
1940 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1941 if (ret)
1942 return ret;
1943
1944 vb = q->bufs[buffer];
e23ccc0a 1945
7f841459
MCC
1946 /*
1947 * MMAP requires page_aligned buffers.
1948 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
1949 * so, we need to do the same here.
1950 */
1951 length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
1952 if (length < (vma->vm_end - vma->vm_start)) {
1953 dprintk(1,
1954 "MMAP invalid, as it would overflow buffer length\n");
068a0df7
SWK
1955 return -EINVAL;
1956 }
1957
a00d0266 1958 ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
e23ccc0a
PO
1959 if (ret)
1960 return ret;
1961
e23ccc0a
PO
1962 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1963 return 0;
1964}
1965EXPORT_SYMBOL_GPL(vb2_mmap);
1966
6f524ec1
SJ
1967#ifndef CONFIG_MMU
1968unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1969 unsigned long addr,
1970 unsigned long len,
1971 unsigned long pgoff,
1972 unsigned long flags)
1973{
1974 unsigned long off = pgoff << PAGE_SHIFT;
1975 struct vb2_buffer *vb;
1976 unsigned int buffer, plane;
1977 int ret;
1978
1979 if (q->memory != V4L2_MEMORY_MMAP) {
1980 dprintk(1, "Queue is not currently set up for mmap\n");
1981 return -EINVAL;
1982 }
1983
1984 /*
1985 * Find the plane corresponding to the offset passed by userspace.
1986 */
1987 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1988 if (ret)
1989 return ret;
1990
1991 vb = q->bufs[buffer];
1992
1993 return (unsigned long)vb2_plane_vaddr(vb, plane);
1994}
1995EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
1996#endif
1997
b25748fe
MS
1998static int __vb2_init_fileio(struct vb2_queue *q, int read);
1999static int __vb2_cleanup_fileio(struct vb2_queue *q);
e23ccc0a
PO
2000
2001/**
2002 * vb2_poll() - implements poll userspace operation
2003 * @q: videobuf2 queue
2004 * @file: file argument passed to the poll file operation handler
2005 * @wait: wait argument passed to the poll file operation handler
2006 *
2007 * This function implements poll file operation handler for a driver.
2008 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2009 * be informed that the file descriptor of a video device is available for
2010 * reading.
2011 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2012 * will be reported as available for writing.
2013 *
95213ceb
HV
2014 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2015 * pending events.
2016 *
e23ccc0a
PO
2017 * The return values from this function are intended to be directly returned
2018 * from poll handler in driver.
2019 */
2020unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
2021{
95213ceb 2022 struct video_device *vfd = video_devdata(file);
bf5c7cbb 2023 unsigned long req_events = poll_requested_events(wait);
e23ccc0a 2024 struct vb2_buffer *vb = NULL;
95213ceb
HV
2025 unsigned int res = 0;
2026 unsigned long flags;
2027
2028 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2029 struct v4l2_fh *fh = file->private_data;
2030
2031 if (v4l2_event_pending(fh))
2032 res = POLLPRI;
2033 else if (req_events & POLLPRI)
2034 poll_wait(file, &fh->wait, wait);
2035 }
e23ccc0a 2036
cd13823f
HV
2037 if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
2038 return res;
2039 if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
2040 return res;
2041
b25748fe 2042 /*
4ffabdb3 2043 * Start file I/O emulator only if streaming API has not been used yet.
b25748fe
MS
2044 */
2045 if (q->num_buffers == 0 && q->fileio == NULL) {
bf5c7cbb
HV
2046 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2047 (req_events & (POLLIN | POLLRDNORM))) {
95213ceb
HV
2048 if (__vb2_init_fileio(q, 1))
2049 return res | POLLERR;
b25748fe 2050 }
bf5c7cbb
HV
2051 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2052 (req_events & (POLLOUT | POLLWRNORM))) {
95213ceb
HV
2053 if (__vb2_init_fileio(q, 0))
2054 return res | POLLERR;
b25748fe
MS
2055 /*
2056 * Write to OUTPUT queue can be done immediately.
2057 */
95213ceb 2058 return res | POLLOUT | POLLWRNORM;
b25748fe
MS
2059 }
2060 }
2061
e23ccc0a
PO
2062 /*
2063 * There is nothing to wait for if no buffers have already been queued.
2064 */
2065 if (list_empty(&q->queued_list))
95213ceb 2066 return res | POLLERR;
e23ccc0a 2067
412cb87d
SWK
2068 if (list_empty(&q->done_list))
2069 poll_wait(file, &q->done_wq, wait);
e23ccc0a
PO
2070
2071 /*
2072 * Take first buffer available for dequeuing.
2073 */
2074 spin_lock_irqsave(&q->done_lock, flags);
2075 if (!list_empty(&q->done_list))
2076 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2077 done_entry);
2078 spin_unlock_irqrestore(&q->done_lock, flags);
2079
2080 if (vb && (vb->state == VB2_BUF_STATE_DONE
2081 || vb->state == VB2_BUF_STATE_ERROR)) {
95213ceb
HV
2082 return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
2083 res | POLLOUT | POLLWRNORM :
2084 res | POLLIN | POLLRDNORM;
e23ccc0a 2085 }
95213ceb 2086 return res;
e23ccc0a
PO
2087}
2088EXPORT_SYMBOL_GPL(vb2_poll);
2089
2090/**
2091 * vb2_queue_init() - initialize a videobuf2 queue
2092 * @q: videobuf2 queue; this structure should be allocated in driver
2093 *
2094 * The vb2_queue structure should be allocated by the driver. The driver is
2095 * responsible of clearing it's content and setting initial values for some
2096 * required entries before calling this function.
2097 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2098 * to the struct vb2_queue description in include/media/videobuf2-core.h
2099 * for more information.
2100 */
2101int vb2_queue_init(struct vb2_queue *q)
2102{
896f38f5
EG
2103 /*
2104 * Sanity check
2105 */
2106 if (WARN_ON(!q) ||
2107 WARN_ON(!q->ops) ||
2108 WARN_ON(!q->mem_ops) ||
2109 WARN_ON(!q->type) ||
2110 WARN_ON(!q->io_modes) ||
2111 WARN_ON(!q->ops->queue_setup) ||
6aa69f99
KD
2112 WARN_ON(!q->ops->buf_queue) ||
2113 WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
896f38f5 2114 return -EINVAL;
e23ccc0a 2115
6aa69f99
KD
2116 /* Warn that the driver should choose an appropriate timestamp type */
2117 WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
2118
e23ccc0a
PO
2119 INIT_LIST_HEAD(&q->queued_list);
2120 INIT_LIST_HEAD(&q->done_list);
2121 spin_lock_init(&q->done_lock);
2122 init_waitqueue_head(&q->done_wq);
2123
2124 if (q->buf_struct_size == 0)
2125 q->buf_struct_size = sizeof(struct vb2_buffer);
2126
2127 return 0;
2128}
2129EXPORT_SYMBOL_GPL(vb2_queue_init);
2130
2131/**
2132 * vb2_queue_release() - stop streaming, release the queue and free memory
2133 * @q: videobuf2 queue
2134 *
2135 * This function stops streaming and performs necessary clean ups, including
2136 * freeing video buffer memory. The driver is responsible for freeing
2137 * the vb2_queue structure itself.
2138 */
2139void vb2_queue_release(struct vb2_queue *q)
2140{
b25748fe 2141 __vb2_cleanup_fileio(q);
e23ccc0a 2142 __vb2_queue_cancel(q);
2d86401c 2143 __vb2_queue_free(q, q->num_buffers);
e23ccc0a
PO
2144}
2145EXPORT_SYMBOL_GPL(vb2_queue_release);
2146
b25748fe
MS
2147/**
2148 * struct vb2_fileio_buf - buffer context used by file io emulator
2149 *
2150 * vb2 provides a compatibility layer and emulator of file io (read and
2151 * write) calls on top of streaming API. This structure is used for
2152 * tracking context related to the buffers.
2153 */
2154struct vb2_fileio_buf {
2155 void *vaddr;
2156 unsigned int size;
2157 unsigned int pos;
2158 unsigned int queued:1;
2159};
2160
2161/**
2162 * struct vb2_fileio_data - queue context used by file io emulator
2163 *
2164 * vb2 provides a compatibility layer and emulator of file io (read and
2165 * write) calls on top of streaming API. For proper operation it required
2166 * this structure to save the driver state between each call of the read
2167 * or write function.
2168 */
2169struct vb2_fileio_data {
2170 struct v4l2_requestbuffers req;
2171 struct v4l2_buffer b;
2172 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
2173 unsigned int index;
2174 unsigned int q_count;
2175 unsigned int dq_count;
2176 unsigned int flags;
2177};
2178
2179/**
2180 * __vb2_init_fileio() - initialize file io emulator
2181 * @q: videobuf2 queue
2182 * @read: mode selector (1 means read, 0 means write)
2183 */
2184static int __vb2_init_fileio(struct vb2_queue *q, int read)
2185{
2186 struct vb2_fileio_data *fileio;
2187 int i, ret;
2188 unsigned int count = 0;
2189
2190 /*
2191 * Sanity check
2192 */
2193 if ((read && !(q->io_modes & VB2_READ)) ||
2194 (!read && !(q->io_modes & VB2_WRITE)))
2195 BUG();
2196
2197 /*
2198 * Check if device supports mapping buffers to kernel virtual space.
2199 */
2200 if (!q->mem_ops->vaddr)
2201 return -EBUSY;
2202
2203 /*
2204 * Check if streaming api has not been already activated.
2205 */
2206 if (q->streaming || q->num_buffers > 0)
2207 return -EBUSY;
2208
2209 /*
2210 * Start with count 1, driver can increase it in queue_setup()
2211 */
2212 count = 1;
2213
2214 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2215 (read) ? "read" : "write", count, q->io_flags);
2216
2217 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
2218 if (fileio == NULL)
2219 return -ENOMEM;
2220
2221 fileio->flags = q->io_flags;
2222
2223 /*
2224 * Request buffers and use MMAP type to force driver
2225 * to allocate buffers by itself.
2226 */
2227 fileio->req.count = count;
2228 fileio->req.memory = V4L2_MEMORY_MMAP;
2229 fileio->req.type = q->type;
2230 ret = vb2_reqbufs(q, &fileio->req);
2231 if (ret)
2232 goto err_kfree;
2233
2234 /*
2235 * Check if plane_count is correct
2236 * (multiplane buffers are not supported).
2237 */
2238 if (q->bufs[0]->num_planes != 1) {
b25748fe
MS
2239 ret = -EBUSY;
2240 goto err_reqbufs;
2241 }
2242
2243 /*
2244 * Get kernel address of each buffer.
2245 */
2246 for (i = 0; i < q->num_buffers; i++) {
2247 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
5dd6946c
WY
2248 if (fileio->bufs[i].vaddr == NULL) {
2249 ret = -EINVAL;
b25748fe 2250 goto err_reqbufs;
5dd6946c 2251 }
b25748fe
MS
2252 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2253 }
2254
2255 /*
2256 * Read mode requires pre queuing of all buffers.
2257 */
2258 if (read) {
2259 /*
2260 * Queue all buffers.
2261 */
2262 for (i = 0; i < q->num_buffers; i++) {
2263 struct v4l2_buffer *b = &fileio->b;
2264 memset(b, 0, sizeof(*b));
2265 b->type = q->type;
2266 b->memory = q->memory;
2267 b->index = i;
2268 ret = vb2_qbuf(q, b);
2269 if (ret)
2270 goto err_reqbufs;
2271 fileio->bufs[i].queued = 1;
2272 }
2273
2274 /*
2275 * Start streaming.
2276 */
2277 ret = vb2_streamon(q, q->type);
2278 if (ret)
2279 goto err_reqbufs;
2280 }
2281
2282 q->fileio = fileio;
2283
2284 return ret;
2285
2286err_reqbufs:
a67e1722 2287 fileio->req.count = 0;
b25748fe
MS
2288 vb2_reqbufs(q, &fileio->req);
2289
2290err_kfree:
2291 kfree(fileio);
2292 return ret;
2293}
2294
2295/**
2296 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2297 * @q: videobuf2 queue
2298 */
2299static int __vb2_cleanup_fileio(struct vb2_queue *q)
2300{
2301 struct vb2_fileio_data *fileio = q->fileio;
2302
2303 if (fileio) {
2304 /*
2305 * Hack fileio context to enable direct calls to vb2 ioctl
2306 * interface.
2307 */
2308 q->fileio = NULL;
2309
2310 vb2_streamoff(q, q->type);
2311 fileio->req.count = 0;
2312 vb2_reqbufs(q, &fileio->req);
2313 kfree(fileio);
2314 dprintk(3, "file io emulator closed\n");
2315 }
2316 return 0;
2317}
2318
2319/**
2320 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2321 * @q: videobuf2 queue
2322 * @data: pointed to target userspace buffer
2323 * @count: number of bytes to read or write
2324 * @ppos: file handle position tracking pointer
2325 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2326 * @read: access mode selector (1 means read, 0 means write)
2327 */
2328static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2329 loff_t *ppos, int nonblock, int read)
2330{
2331 struct vb2_fileio_data *fileio;
2332 struct vb2_fileio_buf *buf;
2333 int ret, index;
2334
08b99e26 2335 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
b25748fe
MS
2336 read ? "read" : "write", (long)*ppos, count,
2337 nonblock ? "non" : "");
2338
2339 if (!data)
2340 return -EINVAL;
2341
2342 /*
2343 * Initialize emulator on first call.
2344 */
2345 if (!q->fileio) {
2346 ret = __vb2_init_fileio(q, read);
2347 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2348 if (ret)
2349 return ret;
2350 }
2351 fileio = q->fileio;
2352
2353 /*
2354 * Hack fileio context to enable direct calls to vb2 ioctl interface.
2355 * The pointer will be restored before returning from this function.
2356 */
2357 q->fileio = NULL;
2358
2359 index = fileio->index;
2360 buf = &fileio->bufs[index];
2361
2362 /*
2363 * Check if we need to dequeue the buffer.
2364 */
2365 if (buf->queued) {
2366 struct vb2_buffer *vb;
2367
2368 /*
2369 * Call vb2_dqbuf to get buffer back.
2370 */
2371 memset(&fileio->b, 0, sizeof(fileio->b));
2372 fileio->b.type = q->type;
2373 fileio->b.memory = q->memory;
2374 fileio->b.index = index;
2375 ret = vb2_dqbuf(q, &fileio->b, nonblock);
2376 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2377 if (ret)
2378 goto end;
2379 fileio->dq_count += 1;
2380
2381 /*
2382 * Get number of bytes filled by the driver
2383 */
2384 vb = q->bufs[index];
2385 buf->size = vb2_get_plane_payload(vb, 0);
2386 buf->queued = 0;
2387 }
2388
2389 /*
2390 * Limit count on last few bytes of the buffer.
2391 */
2392 if (buf->pos + count > buf->size) {
2393 count = buf->size - buf->pos;
08b99e26 2394 dprintk(5, "reducing read count: %zd\n", count);
b25748fe
MS
2395 }
2396
2397 /*
2398 * Transfer data to userspace.
2399 */
08b99e26 2400 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
b25748fe
MS
2401 count, index, buf->pos);
2402 if (read)
2403 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2404 else
2405 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2406 if (ret) {
2407 dprintk(3, "file io: error copying data\n");
2408 ret = -EFAULT;
2409 goto end;
2410 }
2411
2412 /*
2413 * Update counters.
2414 */
2415 buf->pos += count;
2416 *ppos += count;
2417
2418 /*
2419 * Queue next buffer if required.
2420 */
2421 if (buf->pos == buf->size ||
2422 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
2423 /*
2424 * Check if this is the last buffer to read.
2425 */
2426 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
2427 fileio->dq_count == 1) {
2428 dprintk(3, "file io: read limit reached\n");
2429 /*
2430 * Restore fileio pointer and release the context.
2431 */
2432 q->fileio = fileio;
2433 return __vb2_cleanup_fileio(q);
2434 }
2435
2436 /*
2437 * Call vb2_qbuf and give buffer to the driver.
2438 */
2439 memset(&fileio->b, 0, sizeof(fileio->b));
2440 fileio->b.type = q->type;
2441 fileio->b.memory = q->memory;
2442 fileio->b.index = index;
2443 fileio->b.bytesused = buf->pos;
2444 ret = vb2_qbuf(q, &fileio->b);
2445 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
2446 if (ret)
2447 goto end;
2448
2449 /*
2450 * Buffer has been queued, update the status
2451 */
2452 buf->pos = 0;
2453 buf->queued = 1;
2454 buf->size = q->bufs[0]->v4l2_planes[0].length;
2455 fileio->q_count += 1;
2456
2457 /*
2458 * Switch to the next buffer
2459 */
2460 fileio->index = (index + 1) % q->num_buffers;
2461
2462 /*
2463 * Start streaming if required.
2464 */
2465 if (!read && !q->streaming) {
2466 ret = vb2_streamon(q, q->type);
2467 if (ret)
2468 goto end;
2469 }
2470 }
2471
2472 /*
2473 * Return proper number of bytes processed.
2474 */
2475 if (ret == 0)
2476 ret = count;
2477end:
2478 /*
2479 * Restore the fileio context and block vb2 ioctl interface.
2480 */
2481 q->fileio = fileio;
2482 return ret;
2483}
2484
2485size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2486 loff_t *ppos, int nonblocking)
2487{
2488 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2489}
2490EXPORT_SYMBOL_GPL(vb2_read);
2491
819585bc 2492size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
b25748fe
MS
2493 loff_t *ppos, int nonblocking)
2494{
819585bc
RR
2495 return __vb2_perform_fileio(q, (char __user *) data, count,
2496 ppos, nonblocking, 0);
b25748fe
MS
2497}
2498EXPORT_SYMBOL_GPL(vb2_write);
2499
4c1ffcaa
HV
2500
2501/*
2502 * The following functions are not part of the vb2 core API, but are helper
2503 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2504 * and struct vb2_ops.
2505 * They contain boilerplate code that most if not all drivers have to do
2506 * and so they simplify the driver code.
2507 */
2508
2509/* The queue is busy if there is a owner and you are not that owner. */
2510static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
2511{
2512 return vdev->queue->owner && vdev->queue->owner != file->private_data;
2513}
2514
2515/* vb2 ioctl helpers */
2516
2517int vb2_ioctl_reqbufs(struct file *file, void *priv,
2518 struct v4l2_requestbuffers *p)
2519{
2520 struct video_device *vdev = video_devdata(file);
2521 int res = __verify_memory_type(vdev->queue, p->memory, p->type);
2522
2523 if (res)
2524 return res;
2525 if (vb2_queue_is_busy(vdev, file))
2526 return -EBUSY;
2527 res = __reqbufs(vdev->queue, p);
2528 /* If count == 0, then the owner has released all buffers and he
2529 is no longer owner of the queue. Otherwise we have a new owner. */
2530 if (res == 0)
2531 vdev->queue->owner = p->count ? file->private_data : NULL;
2532 return res;
2533}
2534EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
2535
2536int vb2_ioctl_create_bufs(struct file *file, void *priv,
2537 struct v4l2_create_buffers *p)
2538{
2539 struct video_device *vdev = video_devdata(file);
2540 int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
2541
2542 p->index = vdev->queue->num_buffers;
2543 /* If count == 0, then just check if memory and type are valid.
2544 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2545 if (p->count == 0)
2546 return res != -EBUSY ? res : 0;
2547 if (res)
2548 return res;
2549 if (vb2_queue_is_busy(vdev, file))
2550 return -EBUSY;
2551 res = __create_bufs(vdev->queue, p);
2552 if (res == 0)
2553 vdev->queue->owner = file->private_data;
2554 return res;
2555}
2556EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
2557
2558int vb2_ioctl_prepare_buf(struct file *file, void *priv,
2559 struct v4l2_buffer *p)
2560{
2561 struct video_device *vdev = video_devdata(file);
2562
2563 if (vb2_queue_is_busy(vdev, file))
2564 return -EBUSY;
2565 return vb2_prepare_buf(vdev->queue, p);
2566}
2567EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
2568
2569int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
2570{
2571 struct video_device *vdev = video_devdata(file);
2572
2573 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2574 return vb2_querybuf(vdev->queue, p);
2575}
2576EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
2577
2578int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2579{
2580 struct video_device *vdev = video_devdata(file);
2581
2582 if (vb2_queue_is_busy(vdev, file))
2583 return -EBUSY;
2584 return vb2_qbuf(vdev->queue, p);
2585}
2586EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
2587
2588int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
2589{
2590 struct video_device *vdev = video_devdata(file);
2591
2592 if (vb2_queue_is_busy(vdev, file))
2593 return -EBUSY;
2594 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
2595}
2596EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
2597
2598int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
2599{
2600 struct video_device *vdev = video_devdata(file);
2601
2602 if (vb2_queue_is_busy(vdev, file))
2603 return -EBUSY;
2604 return vb2_streamon(vdev->queue, i);
2605}
2606EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
2607
2608int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
2609{
2610 struct video_device *vdev = video_devdata(file);
2611
2612 if (vb2_queue_is_busy(vdev, file))
2613 return -EBUSY;
2614 return vb2_streamoff(vdev->queue, i);
2615}
2616EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
2617
83ae7c5a
TS
2618int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
2619{
2620 struct video_device *vdev = video_devdata(file);
2621
2622 if (vb2_queue_is_busy(vdev, file))
2623 return -EBUSY;
2624 return vb2_expbuf(vdev->queue, p);
2625}
2626EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
2627
4c1ffcaa
HV
2628/* v4l2_file_operations helpers */
2629
2630int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
2631{
2632 struct video_device *vdev = video_devdata(file);
8a90f1a6
LP
2633 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2634 int err;
4c1ffcaa 2635
8a90f1a6
LP
2636 if (lock && mutex_lock_interruptible(lock))
2637 return -ERESTARTSYS;
2638 err = vb2_mmap(vdev->queue, vma);
2639 if (lock)
2640 mutex_unlock(lock);
2641 return err;
4c1ffcaa
HV
2642}
2643EXPORT_SYMBOL_GPL(vb2_fop_mmap);
2644
1380f575 2645int _vb2_fop_release(struct file *file, struct mutex *lock)
4c1ffcaa
HV
2646{
2647 struct video_device *vdev = video_devdata(file);
2648
2649 if (file->private_data == vdev->queue->owner) {
1380f575
RR
2650 if (lock)
2651 mutex_lock(lock);
4c1ffcaa
HV
2652 vb2_queue_release(vdev->queue);
2653 vdev->queue->owner = NULL;
1380f575
RR
2654 if (lock)
2655 mutex_unlock(lock);
4c1ffcaa
HV
2656 }
2657 return v4l2_fh_release(file);
2658}
1380f575
RR
2659EXPORT_SYMBOL_GPL(_vb2_fop_release);
2660
2661int vb2_fop_release(struct file *file)
2662{
2663 struct video_device *vdev = video_devdata(file);
2664 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2665
2666 return _vb2_fop_release(file, lock);
2667}
4c1ffcaa
HV
2668EXPORT_SYMBOL_GPL(vb2_fop_release);
2669
819585bc 2670ssize_t vb2_fop_write(struct file *file, const char __user *buf,
4c1ffcaa
HV
2671 size_t count, loff_t *ppos)
2672{
2673 struct video_device *vdev = video_devdata(file);
2674 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
4c1ffcaa
HV
2675 int err = -EBUSY;
2676
cf533735 2677 if (lock && mutex_lock_interruptible(lock))
4c1ffcaa
HV
2678 return -ERESTARTSYS;
2679 if (vb2_queue_is_busy(vdev, file))
2680 goto exit;
2681 err = vb2_write(vdev->queue, buf, count, ppos,
2682 file->f_flags & O_NONBLOCK);
8c82c75c 2683 if (vdev->queue->fileio)
4c1ffcaa
HV
2684 vdev->queue->owner = file->private_data;
2685exit:
cf533735 2686 if (lock)
4c1ffcaa
HV
2687 mutex_unlock(lock);
2688 return err;
2689}
2690EXPORT_SYMBOL_GPL(vb2_fop_write);
2691
2692ssize_t vb2_fop_read(struct file *file, char __user *buf,
2693 size_t count, loff_t *ppos)
2694{
2695 struct video_device *vdev = video_devdata(file);
2696 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
4c1ffcaa
HV
2697 int err = -EBUSY;
2698
cf533735 2699 if (lock && mutex_lock_interruptible(lock))
4c1ffcaa
HV
2700 return -ERESTARTSYS;
2701 if (vb2_queue_is_busy(vdev, file))
2702 goto exit;
2703 err = vb2_read(vdev->queue, buf, count, ppos,
2704 file->f_flags & O_NONBLOCK);
8c82c75c 2705 if (vdev->queue->fileio)
4c1ffcaa
HV
2706 vdev->queue->owner = file->private_data;
2707exit:
cf533735 2708 if (lock)
4c1ffcaa
HV
2709 mutex_unlock(lock);
2710 return err;
2711}
2712EXPORT_SYMBOL_GPL(vb2_fop_read);
2713
2714unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
2715{
2716 struct video_device *vdev = video_devdata(file);
2717 struct vb2_queue *q = vdev->queue;
2718 struct mutex *lock = q->lock ? q->lock : vdev->lock;
2719 unsigned long req_events = poll_requested_events(wait);
2720 unsigned res;
2721 void *fileio;
4c1ffcaa
HV
2722 bool must_lock = false;
2723
2724 /* Try to be smart: only lock if polling might start fileio,
2725 otherwise locking will only introduce unwanted delays. */
2726 if (q->num_buffers == 0 && q->fileio == NULL) {
2727 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
2728 (req_events & (POLLIN | POLLRDNORM)))
2729 must_lock = true;
2730 else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
2731 (req_events & (POLLOUT | POLLWRNORM)))
2732 must_lock = true;
2733 }
2734
2735 /* If locking is needed, but this helper doesn't know how, then you
2736 shouldn't be using this helper but you should write your own. */
cf533735 2737 WARN_ON(must_lock && !lock);
4c1ffcaa 2738
cf533735 2739 if (must_lock && lock && mutex_lock_interruptible(lock))
4c1ffcaa
HV
2740 return POLLERR;
2741
2742 fileio = q->fileio;
2743
2744 res = vb2_poll(vdev->queue, file, wait);
2745
2746 /* If fileio was started, then we have a new queue owner. */
2747 if (must_lock && !fileio && q->fileio)
2748 q->owner = file->private_data;
cf533735 2749 if (must_lock && lock)
4c1ffcaa
HV
2750 mutex_unlock(lock);
2751 return res;
2752}
2753EXPORT_SYMBOL_GPL(vb2_fop_poll);
2754
2755#ifndef CONFIG_MMU
2756unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
2757 unsigned long len, unsigned long pgoff, unsigned long flags)
2758{
2759 struct video_device *vdev = video_devdata(file);
8a90f1a6
LP
2760 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
2761 int ret;
4c1ffcaa 2762
8a90f1a6
LP
2763 if (lock && mutex_lock_interruptible(lock))
2764 return -ERESTARTSYS;
2765 ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
2766 if (lock)
2767 mutex_unlock(lock);
2768 return ret;
4c1ffcaa
HV
2769}
2770EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
2771#endif
2772
2773/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
2774
2775void vb2_ops_wait_prepare(struct vb2_queue *vq)
2776{
2777 mutex_unlock(vq->lock);
2778}
2779EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
2780
2781void vb2_ops_wait_finish(struct vb2_queue *vq)
2782{
2783 mutex_lock(vq->lock);
2784}
2785EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
2786
e23ccc0a 2787MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
95072084 2788MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
e23ccc0a 2789MODULE_LICENSE("GPL");
This page took 0.370316 seconds and 5 git commands to generate.