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