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