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