2 * videobuf2-core.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
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.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-core.h>
28 module_param(debug
, int, 0644);
30 #define dprintk(level, fmt, arg...) \
33 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
36 #ifdef CONFIG_VIDEO_ADV_DEBUG
39 * If advanced debugging is on, then count how often each op is called,
40 * which can either be per-buffer or per-queue.
42 * If the op failed then the 'fail_' variant is called to decrease the
43 * counter. That makes it easy to check that the 'init' and 'cleanup'
44 * (and variations thereof) stay balanced.
47 #define call_memop(vb, op, args...) \
49 struct vb2_queue *_q = (vb)->vb2_queue; \
50 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
51 _q, (vb)->v4l2_buf.index, #op, \
52 _q->mem_ops->op ? "" : " (nop)"); \
53 (vb)->cnt_mem_ ## op++; \
54 _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
56 #define fail_memop(vb, op) ((vb)->cnt_mem_ ## op--)
58 #define call_qop(q, op, args...) \
60 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
61 (q)->ops->op ? "" : " (nop)"); \
63 (q)->ops->op ? (q)->ops->op(args) : 0; \
65 #define fail_qop(q, op) ((q)->cnt_ ## op--)
67 #define call_vb_qop(vb, op, args...) \
69 struct vb2_queue *_q = (vb)->vb2_queue; \
70 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
71 _q, (vb)->v4l2_buf.index, #op, \
72 _q->ops->op ? "" : " (nop)"); \
74 _q->ops->op ? _q->ops->op(args) : 0; \
76 #define fail_vb_qop(vb, op) ((vb)->cnt_ ## op--)
80 #define call_memop(vb, op, args...) \
81 ((vb)->vb2_queue->mem_ops->op ? (vb)->vb2_queue->mem_ops->op(args) : 0)
82 #define fail_memop(vb, op)
84 #define call_qop(q, op, args...) \
85 ((q)->ops->op ? (q)->ops->op(args) : 0)
86 #define fail_qop(q, op)
88 #define call_vb_qop(vb, op, args...) \
89 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
90 #define fail_vb_qop(vb, op)
94 /* Flags that are set by the vb2 core */
95 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
96 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
97 V4L2_BUF_FLAG_PREPARED | \
98 V4L2_BUF_FLAG_TIMESTAMP_MASK)
99 /* Output buffer flags that should be passed on to the driver */
100 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
101 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
104 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
106 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
108 struct vb2_queue
*q
= vb
->vb2_queue
;
113 * Allocate memory for all planes in this buffer
114 * NOTE: mmapped areas should be page aligned
116 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
117 unsigned long size
= PAGE_ALIGN(q
->plane_sizes
[plane
]);
119 mem_priv
= call_memop(vb
, alloc
, q
->alloc_ctx
[plane
],
121 if (IS_ERR_OR_NULL(mem_priv
))
124 /* Associate allocator private data with this plane */
125 vb
->planes
[plane
].mem_priv
= mem_priv
;
126 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
131 fail_memop(vb
, alloc
);
132 /* Free already allocated memory if one of the allocations failed */
133 for (; plane
> 0; --plane
) {
134 call_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
135 vb
->planes
[plane
- 1].mem_priv
= NULL
;
142 * __vb2_buf_mem_free() - free memory of the given buffer
144 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
148 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
149 call_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
150 vb
->planes
[plane
].mem_priv
= NULL
;
151 dprintk(3, "Freed plane %d of buffer %d\n", plane
,
157 * __vb2_buf_userptr_put() - release userspace memory associated with
160 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
164 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
165 if (vb
->planes
[plane
].mem_priv
)
166 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
167 vb
->planes
[plane
].mem_priv
= NULL
;
172 * __vb2_plane_dmabuf_put() - release memory associated with
173 * a DMABUF shared plane
175 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
181 call_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
183 call_memop(vb
, detach_dmabuf
, p
->mem_priv
);
184 dma_buf_put(p
->dbuf
);
185 memset(p
, 0, sizeof(*p
));
189 * __vb2_buf_dmabuf_put() - release memory associated with
190 * a DMABUF shared buffer
192 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
196 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
197 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
201 * __setup_lengths() - setup initial lengths for every plane in
202 * every buffer on the queue
204 static void __setup_lengths(struct vb2_queue
*q
, unsigned int n
)
206 unsigned int buffer
, plane
;
207 struct vb2_buffer
*vb
;
209 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
210 vb
= q
->bufs
[buffer
];
214 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
215 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
220 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
221 * every buffer on the queue
223 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
225 unsigned int buffer
, plane
;
226 struct vb2_buffer
*vb
;
229 if (q
->num_buffers
) {
230 struct v4l2_plane
*p
;
231 vb
= q
->bufs
[q
->num_buffers
- 1];
232 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
233 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
238 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
239 vb
= q
->bufs
[buffer
];
243 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
244 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
246 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
249 off
+= vb
->v4l2_planes
[plane
].length
;
250 off
= PAGE_ALIGN(off
);
256 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
257 * video buffer memory for all buffers/planes on the queue and initializes the
260 * Returns the number of buffers successfully allocated.
262 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
263 unsigned int num_buffers
, unsigned int num_planes
)
266 struct vb2_buffer
*vb
;
269 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
270 /* Allocate videobuf buffer structures */
271 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
273 dprintk(1, "Memory alloc for buffer struct failed\n");
277 /* Length stores number of planes for multiplanar buffers */
278 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
279 vb
->v4l2_buf
.length
= num_planes
;
281 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
283 vb
->num_planes
= num_planes
;
284 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
285 vb
->v4l2_buf
.type
= q
->type
;
286 vb
->v4l2_buf
.memory
= memory
;
288 /* Allocate video buffer memory for the MMAP type */
289 if (memory
== V4L2_MEMORY_MMAP
) {
290 ret
= __vb2_buf_mem_alloc(vb
);
292 dprintk(1, "Failed allocating memory for "
293 "buffer %d\n", buffer
);
298 * Call the driver-provided buffer initialization
299 * callback, if given. An error in initialization
300 * results in queue setup failure.
302 ret
= call_vb_qop(vb
, buf_init
, vb
);
304 dprintk(1, "Buffer %d %p initialization"
305 " failed\n", buffer
, vb
);
306 fail_vb_qop(vb
, buf_init
);
307 __vb2_buf_mem_free(vb
);
313 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
316 __setup_lengths(q
, buffer
);
317 if (memory
== V4L2_MEMORY_MMAP
)
318 __setup_offsets(q
, buffer
);
320 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
327 * __vb2_free_mem() - release all video buffer memory for a given queue
329 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
332 struct vb2_buffer
*vb
;
334 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
336 vb
= q
->bufs
[buffer
];
340 /* Free MMAP buffers or release USERPTR buffers */
341 if (q
->memory
== V4L2_MEMORY_MMAP
)
342 __vb2_buf_mem_free(vb
);
343 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
344 __vb2_buf_dmabuf_put(vb
);
346 __vb2_buf_userptr_put(vb
);
351 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
352 * related information, if no buffers are left return the queue to an
353 * uninitialized state. Might be called even if the queue has already been freed.
355 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
360 * Sanity check: when preparing a buffer the queue lock is released for
361 * a short while (see __buf_prepare for the details), which would allow
362 * a race with a reqbufs which can call this function. Removing the
363 * buffers from underneath __buf_prepare is obviously a bad idea, so we
364 * check if any of the buffers is in the state PREPARING, and if so we
365 * just return -EAGAIN.
367 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
369 if (q
->bufs
[buffer
] == NULL
)
371 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
372 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
377 /* Call driver-provided cleanup function for each buffer, if provided */
378 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
380 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
382 if (vb
&& vb
->planes
[0].mem_priv
)
383 call_vb_qop(vb
, buf_cleanup
, vb
);
386 /* Release video buffer memory */
387 __vb2_free_mem(q
, buffers
);
389 #ifdef CONFIG_VIDEO_ADV_DEBUG
391 * Check that all the calls were balances during the life-time of this
392 * queue. If not (or if the debug level is 1 or up), then dump the
393 * counters to the kernel log.
395 if (q
->num_buffers
) {
396 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
397 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
399 if (unbalanced
|| debug
) {
400 pr_info("vb2: counters for queue %p:%s\n", q
,
401 unbalanced
? " UNBALANCED!" : "");
402 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
403 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
404 q
->cnt_stop_streaming
);
405 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
406 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
408 q
->cnt_queue_setup
= 0;
409 q
->cnt_wait_prepare
= 0;
410 q
->cnt_wait_finish
= 0;
411 q
->cnt_start_streaming
= 0;
412 q
->cnt_stop_streaming
= 0;
414 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
415 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
416 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
417 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
418 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
419 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
420 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
421 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
422 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
423 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
425 if (unbalanced
|| debug
) {
426 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
427 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
428 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
429 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
430 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
431 pr_info("vb2: buf_queue: %u buf_done: %u\n",
432 vb
->cnt_buf_queue
, vb
->cnt_buf_done
);
433 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
434 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
435 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
437 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
438 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
439 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
440 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
441 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
442 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
443 vb
->cnt_mem_get_dmabuf
,
444 vb
->cnt_mem_num_users
,
451 /* Free videobuf buffers */
452 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
454 kfree(q
->bufs
[buffer
]);
455 q
->bufs
[buffer
] = NULL
;
458 q
->num_buffers
-= buffers
;
461 INIT_LIST_HEAD(&q
->queued_list
);
466 * __verify_planes_array() - verify that the planes array passed in struct
467 * v4l2_buffer from userspace can be safely used
469 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
471 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
474 /* Is memory for copying plane information present? */
475 if (NULL
== b
->m
.planes
) {
476 dprintk(1, "Multi-planar buffer passed but "
477 "planes array not provided\n");
481 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
482 dprintk(1, "Incorrect planes array length, "
483 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
491 * __verify_length() - Verify that the bytesused value for each plane fits in
492 * the plane length and that the data offset doesn't exceed the bytesused value.
494 static int __verify_length(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
499 if (!V4L2_TYPE_IS_OUTPUT(b
->type
))
502 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
503 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
504 length
= (b
->memory
== V4L2_MEMORY_USERPTR
)
505 ? b
->m
.planes
[plane
].length
506 : vb
->v4l2_planes
[plane
].length
;
508 if (b
->m
.planes
[plane
].bytesused
> length
)
511 if (b
->m
.planes
[plane
].data_offset
> 0 &&
512 b
->m
.planes
[plane
].data_offset
>=
513 b
->m
.planes
[plane
].bytesused
)
517 length
= (b
->memory
== V4L2_MEMORY_USERPTR
)
518 ? b
->length
: vb
->v4l2_planes
[0].length
;
520 if (b
->bytesused
> length
)
528 * __buffer_in_use() - return true if the buffer is in use and
529 * the queue cannot be freed (by the means of REQBUFS(0)) call
531 static bool __buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
534 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
535 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
537 * If num_users() has not been provided, call_memop
538 * will return 0, apparently nobody cares about this
539 * case anyway. If num_users() returns more than 1,
540 * we are not the only user of the plane's memory.
542 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
549 * __buffers_in_use() - return true if any buffers on the queue are in use and
550 * the queue cannot be freed (by the means of REQBUFS(0)) call
552 static bool __buffers_in_use(struct vb2_queue
*q
)
555 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
556 if (__buffer_in_use(q
, q
->bufs
[buffer
]))
563 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
564 * returned to userspace
566 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
568 struct vb2_queue
*q
= vb
->vb2_queue
;
570 /* Copy back data such as timestamp, flags, etc. */
571 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
572 b
->reserved2
= vb
->v4l2_buf
.reserved2
;
573 b
->reserved
= vb
->v4l2_buf
.reserved
;
575 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
577 * Fill in plane-related data if userspace provided an array
578 * for it. The caller has already verified memory and size.
580 b
->length
= vb
->num_planes
;
581 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
582 b
->length
* sizeof(struct v4l2_plane
));
585 * We use length and offset in v4l2_planes array even for
586 * single-planar buffers, but userspace does not.
588 b
->length
= vb
->v4l2_planes
[0].length
;
589 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
590 if (q
->memory
== V4L2_MEMORY_MMAP
)
591 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
592 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
593 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
594 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
595 b
->m
.fd
= vb
->v4l2_planes
[0].m
.fd
;
599 * Clear any buffer state related flags.
601 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
602 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
;
603 if ((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) !=
604 V4L2_BUF_FLAG_TIMESTAMP_COPY
) {
606 * For non-COPY timestamps, drop timestamp source bits
607 * and obtain the timestamp source from the queue.
609 b
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
610 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
614 case VB2_BUF_STATE_QUEUED
:
615 case VB2_BUF_STATE_ACTIVE
:
616 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
618 case VB2_BUF_STATE_ERROR
:
619 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
621 case VB2_BUF_STATE_DONE
:
622 b
->flags
|= V4L2_BUF_FLAG_DONE
;
624 case VB2_BUF_STATE_PREPARED
:
625 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
627 case VB2_BUF_STATE_PREPARING
:
628 case VB2_BUF_STATE_DEQUEUED
:
633 if (__buffer_in_use(q
, vb
))
634 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
638 * vb2_querybuf() - query video buffer information
640 * @b: buffer struct passed from userspace to vidioc_querybuf handler
643 * Should be called from vidioc_querybuf ioctl handler in driver.
644 * This function will verify the passed v4l2_buffer structure and fill the
645 * relevant information for the userspace.
647 * The return values from this function are intended to be directly returned
648 * from vidioc_querybuf handler in driver.
650 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
652 struct vb2_buffer
*vb
;
655 if (b
->type
!= q
->type
) {
656 dprintk(1, "querybuf: wrong buffer type\n");
660 if (b
->index
>= q
->num_buffers
) {
661 dprintk(1, "querybuf: buffer index out of range\n");
664 vb
= q
->bufs
[b
->index
];
665 ret
= __verify_planes_array(vb
, b
);
667 __fill_v4l2_buffer(vb
, b
);
670 EXPORT_SYMBOL(vb2_querybuf
);
673 * __verify_userptr_ops() - verify that all memory operations required for
674 * USERPTR queue type have been provided
676 static int __verify_userptr_ops(struct vb2_queue
*q
)
678 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
679 !q
->mem_ops
->put_userptr
)
686 * __verify_mmap_ops() - verify that all memory operations required for
687 * MMAP queue type have been provided
689 static int __verify_mmap_ops(struct vb2_queue
*q
)
691 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
692 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
699 * __verify_dmabuf_ops() - verify that all memory operations required for
700 * DMABUF queue type have been provided
702 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
704 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
705 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
706 !q
->mem_ops
->unmap_dmabuf
)
713 * __verify_memory_type() - Check whether the memory type and buffer type
714 * passed to a buffer operation are compatible with the queue.
716 static int __verify_memory_type(struct vb2_queue
*q
,
717 enum v4l2_memory memory
, enum v4l2_buf_type type
)
719 if (memory
!= V4L2_MEMORY_MMAP
&& memory
!= V4L2_MEMORY_USERPTR
&&
720 memory
!= V4L2_MEMORY_DMABUF
) {
721 dprintk(1, "reqbufs: unsupported memory type\n");
725 if (type
!= q
->type
) {
726 dprintk(1, "reqbufs: requested type is incorrect\n");
731 * Make sure all the required memory ops for given memory type
734 if (memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
735 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
739 if (memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
740 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
744 if (memory
== V4L2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
745 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
750 * Place the busy tests at the end: -EBUSY can be ignored when
751 * create_bufs is called with count == 0, but count == 0 should still
752 * do the memory and type validation.
755 dprintk(1, "reqbufs: file io in progress\n");
762 * __reqbufs() - Initiate streaming
763 * @q: videobuf2 queue
764 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
766 * Should be called from vidioc_reqbufs ioctl handler of a driver.
768 * 1) verifies streaming parameters passed from the userspace,
769 * 2) sets up the queue,
770 * 3) negotiates number of buffers and planes per buffer with the driver
771 * to be used during streaming,
772 * 4) allocates internal buffer structures (struct vb2_buffer), according to
773 * the agreed parameters,
774 * 5) for MMAP memory type, allocates actual video memory, using the
775 * memory handling/allocation routines provided during queue initialization
777 * If req->count is 0, all the memory will be freed instead.
778 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
779 * and the queue is not busy, memory will be reallocated.
781 * The return values from this function are intended to be directly returned
782 * from vidioc_reqbufs handler in driver.
784 static int __reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
786 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
790 dprintk(1, "reqbufs: streaming active\n");
794 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
796 * We already have buffers allocated, so first check if they
797 * are not in use and can be freed.
799 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
800 dprintk(1, "reqbufs: memory in use, cannot free\n");
804 ret
= __vb2_queue_free(q
, q
->num_buffers
);
809 * In case of REQBUFS(0) return immediately without calling
810 * driver's queue_setup() callback and allocating resources.
817 * Make sure the requested values and current defaults are sane.
819 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
820 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
821 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
822 q
->memory
= req
->memory
;
825 * Ask the driver how many buffers and planes per buffer it requires.
826 * Driver also sets the size and allocator context for each plane.
828 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
829 q
->plane_sizes
, q
->alloc_ctx
);
831 fail_qop(q
, queue_setup
);
835 /* Finally, allocate buffers and video memory */
836 ret
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
838 dprintk(1, "Memory allocation failed\n");
842 allocated_buffers
= ret
;
845 * Check if driver can handle the allocated number of buffers.
847 if (allocated_buffers
< num_buffers
) {
848 num_buffers
= allocated_buffers
;
850 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
851 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
853 fail_qop(q
, queue_setup
);
855 if (!ret
&& allocated_buffers
< num_buffers
)
859 * Either the driver has accepted a smaller number of buffers,
860 * or .queue_setup() returned an error
864 q
->num_buffers
= allocated_buffers
;
867 __vb2_queue_free(q
, allocated_buffers
);
872 * Return the number of successfully allocated buffers
875 req
->count
= allocated_buffers
;
881 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
883 * @q: videobuf2 queue
884 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
886 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
888 int ret
= __verify_memory_type(q
, req
->memory
, req
->type
);
890 return ret
? ret
: __reqbufs(q
, req
);
892 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
895 * __create_bufs() - Allocate buffers and any required auxiliary structs
896 * @q: videobuf2 queue
897 * @create: creation parameters, passed from userspace to vidioc_create_bufs
900 * Should be called from vidioc_create_bufs ioctl handler of a driver.
902 * 1) verifies parameter sanity
903 * 2) calls the .queue_setup() queue operation
904 * 3) performs any necessary memory allocations
906 * The return values from this function are intended to be directly returned
907 * from vidioc_create_bufs handler in driver.
909 static int __create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
911 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
914 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
915 dprintk(1, "%s(): maximum number of buffers already allocated\n",
920 if (!q
->num_buffers
) {
921 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
922 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
923 q
->memory
= create
->memory
;
926 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
929 * Ask the driver, whether the requested number of buffers, planes per
930 * buffer and their sizes are acceptable
932 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
933 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
935 fail_qop(q
, queue_setup
);
939 /* Finally, allocate buffers and video memory */
940 ret
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
943 dprintk(1, "Memory allocation failed\n");
947 allocated_buffers
= ret
;
950 * Check if driver can handle the so far allocated number of buffers.
952 if (ret
< num_buffers
) {
956 * q->num_buffers contains the total number of buffers, that the
957 * queue driver has set up
959 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
960 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
962 fail_qop(q
, queue_setup
);
964 if (!ret
&& allocated_buffers
< num_buffers
)
968 * Either the driver has accepted a smaller number of buffers,
969 * or .queue_setup() returned an error
973 q
->num_buffers
+= allocated_buffers
;
976 __vb2_queue_free(q
, allocated_buffers
);
981 * Return the number of successfully allocated buffers
984 create
->count
= allocated_buffers
;
990 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
991 * memory and type values.
992 * @q: videobuf2 queue
993 * @create: creation parameters, passed from userspace to vidioc_create_bufs
996 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
998 int ret
= __verify_memory_type(q
, create
->memory
, create
->format
.type
);
1000 create
->index
= q
->num_buffers
;
1001 if (create
->count
== 0)
1002 return ret
!= -EBUSY
? ret
: 0;
1003 return ret
? ret
: __create_bufs(q
, create
);
1005 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
1008 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
1009 * @vb: vb2_buffer to which the plane in question belongs to
1010 * @plane_no: plane number for which the address is to be returned
1012 * This function returns a kernel virtual address of a given plane if
1013 * such a mapping exist, NULL otherwise.
1015 void *vb2_plane_vaddr(struct vb2_buffer
*vb
, unsigned int plane_no
)
1017 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
1020 return call_memop(vb
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
1023 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
1026 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
1027 * @vb: vb2_buffer to which the plane in question belongs to
1028 * @plane_no: plane number for which the cookie is to be returned
1030 * This function returns an allocator specific cookie for a given plane if
1031 * available, NULL otherwise. The allocator should provide some simple static
1032 * inline function, which would convert this cookie to the allocator specific
1033 * type that can be used directly by the driver to access the buffer. This can
1034 * be for example physical address, pointer to scatter list or IOMMU mapping.
1036 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
1038 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
1041 return call_memop(vb
, cookie
, vb
->planes
[plane_no
].mem_priv
);
1043 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
1046 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1047 * @vb: vb2_buffer returned from the driver
1048 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1049 * or VB2_BUF_STATE_ERROR if the operation finished with an error
1051 * This function should be called by the driver after a hardware operation on
1052 * a buffer is finished and the buffer may be returned to userspace. The driver
1053 * cannot use this buffer anymore until it is queued back to it by videobuf
1054 * by the means of buf_queue callback. Only buffers previously queued to the
1055 * driver by buf_queue can be passed to this function.
1057 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
1059 struct vb2_queue
*q
= vb
->vb2_queue
;
1060 unsigned long flags
;
1063 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
)
1066 if (state
!= VB2_BUF_STATE_DONE
&& state
!= VB2_BUF_STATE_ERROR
)
1069 #ifdef CONFIG_VIDEO_ADV_DEBUG
1071 * Although this is not a callback, it still does have to balance
1072 * with the buf_queue op. So update this counter manually.
1076 dprintk(4, "Done processing on buffer %d, state: %d\n",
1077 vb
->v4l2_buf
.index
, state
);
1080 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1081 call_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
1083 /* Add the buffer to the done buffers list */
1084 spin_lock_irqsave(&q
->done_lock
, flags
);
1086 list_add_tail(&vb
->done_entry
, &q
->done_list
);
1087 atomic_dec(&q
->owned_by_drv_count
);
1088 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1090 /* Inform any processes that may be waiting for buffers */
1091 wake_up(&q
->done_wq
);
1093 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
1096 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1097 * v4l2_buffer by the userspace. The caller has already verified that struct
1098 * v4l2_buffer has a valid number of planes.
1100 static void __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
1101 struct v4l2_plane
*v4l2_planes
)
1105 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
1106 /* Fill in driver-provided information for OUTPUT types */
1107 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1109 * Will have to go up to b->length when API starts
1110 * accepting variable number of planes.
1112 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1113 v4l2_planes
[plane
].bytesused
=
1114 b
->m
.planes
[plane
].bytesused
;
1115 v4l2_planes
[plane
].data_offset
=
1116 b
->m
.planes
[plane
].data_offset
;
1120 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
1121 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1122 v4l2_planes
[plane
].m
.userptr
=
1123 b
->m
.planes
[plane
].m
.userptr
;
1124 v4l2_planes
[plane
].length
=
1125 b
->m
.planes
[plane
].length
;
1128 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
1129 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1130 v4l2_planes
[plane
].m
.fd
=
1131 b
->m
.planes
[plane
].m
.fd
;
1132 v4l2_planes
[plane
].length
=
1133 b
->m
.planes
[plane
].length
;
1134 v4l2_planes
[plane
].data_offset
=
1135 b
->m
.planes
[plane
].data_offset
;
1140 * Single-planar buffers do not use planes array,
1141 * so fill in relevant v4l2_buffer struct fields instead.
1142 * In videobuf we use our internal V4l2_planes struct for
1143 * single-planar buffers as well, for simplicity.
1145 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1146 v4l2_planes
[0].bytesused
= b
->bytesused
;
1147 v4l2_planes
[0].data_offset
= 0;
1150 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
1151 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
1152 v4l2_planes
[0].length
= b
->length
;
1155 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
1156 v4l2_planes
[0].m
.fd
= b
->m
.fd
;
1157 v4l2_planes
[0].length
= b
->length
;
1158 v4l2_planes
[0].data_offset
= 0;
1163 /* Zero flags that the vb2 core handles */
1164 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
1165 if ((vb
->vb2_queue
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) !=
1166 V4L2_BUF_FLAG_TIMESTAMP_COPY
|| !V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1168 * Non-COPY timestamps and non-OUTPUT queues will get
1169 * their timestamp and timestamp source flags from the
1172 vb
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
1175 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1177 * For output buffers mask out the timecode flag:
1178 * this will be handled later in vb2_internal_qbuf().
1179 * The 'field' is valid metadata for this output buffer
1180 * and so that needs to be copied here.
1182 vb
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_TIMECODE
;
1183 vb
->v4l2_buf
.field
= b
->field
;
1185 /* Zero any output buffer flags as this is a capture buffer */
1186 vb
->v4l2_buf
.flags
&= ~V4L2_BUFFER_OUT_FLAGS
;
1191 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1193 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1195 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
1196 struct vb2_queue
*q
= vb
->vb2_queue
;
1200 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
1201 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1203 /* Copy relevant information provided by the userspace */
1204 __fill_vb2_buffer(vb
, b
, planes
);
1206 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1207 /* Skip the plane if already verified */
1208 if (vb
->v4l2_planes
[plane
].m
.userptr
&&
1209 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1210 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
1213 dprintk(3, "qbuf: userspace address for plane %d changed, "
1214 "reacquiring memory\n", plane
);
1216 /* Check if the provided plane buffer is large enough */
1217 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
1218 dprintk(1, "qbuf: provided buffer size %u is less than "
1219 "setup size %u for plane %d\n",
1220 planes
[plane
].length
,
1221 q
->plane_sizes
[plane
], plane
);
1226 /* Release previously acquired memory if present */
1227 if (vb
->planes
[plane
].mem_priv
) {
1230 call_vb_qop(vb
, buf_cleanup
, vb
);
1232 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1235 vb
->planes
[plane
].mem_priv
= NULL
;
1236 memset(&vb
->v4l2_planes
[plane
], 0, sizeof(struct v4l2_plane
));
1238 /* Acquire each plane's memory */
1239 mem_priv
= call_memop(vb
, get_userptr
, q
->alloc_ctx
[plane
],
1240 planes
[plane
].m
.userptr
,
1241 planes
[plane
].length
, write
);
1242 if (IS_ERR_OR_NULL(mem_priv
)) {
1243 dprintk(1, "qbuf: failed acquiring userspace "
1244 "memory for plane %d\n", plane
);
1245 fail_memop(vb
, get_userptr
);
1246 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
1249 vb
->planes
[plane
].mem_priv
= mem_priv
;
1253 * Now that everything is in order, copy relevant information
1254 * provided by userspace.
1256 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1257 vb
->v4l2_planes
[plane
] = planes
[plane
];
1261 * One or more planes changed, so we must call buf_init to do
1262 * the driver-specific initialization on the newly acquired
1263 * buffer, if provided.
1265 ret
= call_vb_qop(vb
, buf_init
, vb
);
1267 dprintk(1, "qbuf: buffer initialization failed\n");
1268 fail_vb_qop(vb
, buf_init
);
1273 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1275 dprintk(1, "qbuf: buffer preparation failed\n");
1276 fail_vb_qop(vb
, buf_prepare
);
1277 call_vb_qop(vb
, buf_cleanup
, vb
);
1283 /* In case of errors, release planes that were already acquired */
1284 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1285 if (vb
->planes
[plane
].mem_priv
)
1286 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1287 vb
->planes
[plane
].mem_priv
= NULL
;
1288 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
1289 vb
->v4l2_planes
[plane
].length
= 0;
1296 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1298 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1302 __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
1303 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1305 fail_vb_qop(vb
, buf_prepare
);
1310 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1312 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1314 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
1315 struct vb2_queue
*q
= vb
->vb2_queue
;
1319 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
1320 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1322 /* Copy relevant information provided by the userspace */
1323 __fill_vb2_buffer(vb
, b
, planes
);
1325 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1326 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1328 if (IS_ERR_OR_NULL(dbuf
)) {
1329 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1335 /* use DMABUF size if length is not provided */
1336 if (planes
[plane
].length
== 0)
1337 planes
[plane
].length
= dbuf
->size
;
1339 if (planes
[plane
].length
< planes
[plane
].data_offset
+
1340 q
->plane_sizes
[plane
]) {
1341 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1347 /* Skip the plane if already verified */
1348 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1349 vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
) {
1354 dprintk(1, "qbuf: buffer for plane %d changed\n", plane
);
1358 call_vb_qop(vb
, buf_cleanup
, vb
);
1361 /* Release previously acquired memory if present */
1362 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1363 memset(&vb
->v4l2_planes
[plane
], 0, sizeof(struct v4l2_plane
));
1365 /* Acquire each plane's memory */
1366 mem_priv
= call_memop(vb
, attach_dmabuf
, q
->alloc_ctx
[plane
],
1367 dbuf
, planes
[plane
].length
, write
);
1368 if (IS_ERR(mem_priv
)) {
1369 dprintk(1, "qbuf: failed to attach dmabuf\n");
1370 fail_memop(vb
, attach_dmabuf
);
1371 ret
= PTR_ERR(mem_priv
);
1376 vb
->planes
[plane
].dbuf
= dbuf
;
1377 vb
->planes
[plane
].mem_priv
= mem_priv
;
1380 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1381 * really we want to do this just before the DMA, not while queueing
1384 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1385 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1387 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1389 fail_memop(vb
, map_dmabuf
);
1392 vb
->planes
[plane
].dbuf_mapped
= 1;
1396 * Now that everything is in order, copy relevant information
1397 * provided by userspace.
1399 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1400 vb
->v4l2_planes
[plane
] = planes
[plane
];
1404 * Call driver-specific initialization on the newly acquired buffer,
1407 ret
= call_vb_qop(vb
, buf_init
, vb
);
1409 dprintk(1, "qbuf: buffer initialization failed\n");
1410 fail_vb_qop(vb
, buf_init
);
1415 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1417 dprintk(1, "qbuf: buffer preparation failed\n");
1418 fail_vb_qop(vb
, buf_prepare
);
1419 call_vb_qop(vb
, buf_cleanup
, vb
);
1425 /* In case of errors, release planes that were already acquired */
1426 __vb2_buf_dmabuf_put(vb
);
1432 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1434 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1436 struct vb2_queue
*q
= vb
->vb2_queue
;
1439 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1440 atomic_inc(&q
->owned_by_drv_count
);
1443 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1444 call_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1446 call_vb_qop(vb
, buf_queue
, vb
);
1449 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1451 struct vb2_queue
*q
= vb
->vb2_queue
;
1452 struct rw_semaphore
*mmap_sem
;
1455 ret
= __verify_length(vb
, b
);
1457 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1462 vb
->state
= VB2_BUF_STATE_PREPARING
;
1463 vb
->v4l2_buf
.timestamp
.tv_sec
= 0;
1464 vb
->v4l2_buf
.timestamp
.tv_usec
= 0;
1465 vb
->v4l2_buf
.sequence
= 0;
1467 switch (q
->memory
) {
1468 case V4L2_MEMORY_MMAP
:
1469 ret
= __qbuf_mmap(vb
, b
);
1471 case V4L2_MEMORY_USERPTR
:
1473 * In case of user pointer buffers vb2 allocators need to get
1474 * direct access to userspace pages. This requires getting
1475 * the mmap semaphore for read access in the current process
1476 * structure. The same semaphore is taken before calling mmap
1477 * operation, while both qbuf/prepare_buf and mmap are called
1478 * by the driver or v4l2 core with the driver's lock held.
1479 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1480 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1481 * the videobuf2 core releases the driver's lock, takes
1482 * mmap_sem and then takes the driver's lock again.
1484 mmap_sem
= ¤t
->mm
->mmap_sem
;
1485 call_qop(q
, wait_prepare
, q
);
1486 down_read(mmap_sem
);
1487 call_qop(q
, wait_finish
, q
);
1489 ret
= __qbuf_userptr(vb
, b
);
1493 case V4L2_MEMORY_DMABUF
:
1494 ret
= __qbuf_dmabuf(vb
, b
);
1497 WARN(1, "Invalid queue type\n");
1502 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
1503 vb
->state
= ret
? VB2_BUF_STATE_DEQUEUED
: VB2_BUF_STATE_PREPARED
;
1508 static int vb2_queue_or_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
,
1511 if (b
->type
!= q
->type
) {
1512 dprintk(1, "%s(): invalid buffer type\n", opname
);
1516 if (b
->index
>= q
->num_buffers
) {
1517 dprintk(1, "%s(): buffer index out of range\n", opname
);
1521 if (q
->bufs
[b
->index
] == NULL
) {
1522 /* Should never happen */
1523 dprintk(1, "%s(): buffer is NULL\n", opname
);
1527 if (b
->memory
!= q
->memory
) {
1528 dprintk(1, "%s(): invalid memory type\n", opname
);
1532 return __verify_planes_array(q
->bufs
[b
->index
], b
);
1536 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1537 * @q: videobuf2 queue
1538 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1541 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1543 * 1) verifies the passed buffer,
1544 * 2) calls buf_prepare callback in the driver (if provided), in which
1545 * driver-specific buffer initialization can be performed,
1547 * The return values from this function are intended to be directly returned
1548 * from vidioc_prepare_buf handler in driver.
1550 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1552 struct vb2_buffer
*vb
;
1556 dprintk(1, "%s(): file io in progress\n", __func__
);
1560 ret
= vb2_queue_or_prepare_buf(q
, b
, "prepare_buf");
1564 vb
= q
->bufs
[b
->index
];
1565 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1566 dprintk(1, "%s(): invalid buffer state %d\n", __func__
,
1571 ret
= __buf_prepare(vb
, b
);
1573 /* Fill buffer information for the userspace */
1574 __fill_v4l2_buffer(vb
, b
);
1576 dprintk(1, "%s() of buffer %d succeeded\n", __func__
, vb
->v4l2_buf
.index
);
1580 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1583 * vb2_start_streaming() - Attempt to start streaming.
1584 * @q: videobuf2 queue
1586 * If there are not enough buffers, then retry_start_streaming is set to
1587 * 1 and 0 is returned. The next time a buffer is queued and
1588 * retry_start_streaming is 1, this function will be called again to
1589 * retry starting the DMA engine.
1591 static int vb2_start_streaming(struct vb2_queue
*q
)
1595 /* Tell the driver to start streaming */
1596 ret
= call_qop(q
, start_streaming
, q
, atomic_read(&q
->owned_by_drv_count
));
1598 fail_qop(q
, start_streaming
);
1601 * If there are not enough buffers queued to start streaming, then
1602 * the start_streaming operation will return -ENOBUFS and you have to
1603 * retry when the next buffer is queued.
1605 if (ret
== -ENOBUFS
) {
1606 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1607 q
->retry_start_streaming
= 1;
1611 dprintk(1, "qbuf: driver refused to start streaming\n");
1613 q
->retry_start_streaming
= 0;
1617 static int vb2_internal_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1619 int ret
= vb2_queue_or_prepare_buf(q
, b
, "qbuf");
1620 struct vb2_buffer
*vb
;
1625 vb
= q
->bufs
[b
->index
];
1627 switch (vb
->state
) {
1628 case VB2_BUF_STATE_DEQUEUED
:
1629 ret
= __buf_prepare(vb
, b
);
1633 case VB2_BUF_STATE_PREPARED
:
1635 case VB2_BUF_STATE_PREPARING
:
1636 dprintk(1, "qbuf: buffer still being prepared\n");
1639 dprintk(1, "%s(): invalid buffer state %d\n", __func__
,
1645 * Add to the queued buffers list, a buffer will stay on it until
1646 * dequeued in dqbuf.
1648 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1649 vb
->state
= VB2_BUF_STATE_QUEUED
;
1650 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1652 * For output buffers copy the timestamp if needed,
1653 * and the timecode field and flag if needed.
1655 if ((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
1656 V4L2_BUF_FLAG_TIMESTAMP_COPY
)
1657 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
1658 vb
->v4l2_buf
.flags
|= b
->flags
& V4L2_BUF_FLAG_TIMECODE
;
1659 if (b
->flags
& V4L2_BUF_FLAG_TIMECODE
)
1660 vb
->v4l2_buf
.timecode
= b
->timecode
;
1664 * If already streaming, give the buffer to driver for processing.
1665 * If not, the buffer will be given to driver on next streamon.
1668 __enqueue_in_driver(vb
);
1670 /* Fill buffer information for the userspace */
1671 __fill_v4l2_buffer(vb
, b
);
1673 if (q
->retry_start_streaming
) {
1674 ret
= vb2_start_streaming(q
);
1679 dprintk(1, "%s() of buffer %d succeeded\n", __func__
, vb
->v4l2_buf
.index
);
1684 * vb2_qbuf() - Queue a buffer from userspace
1685 * @q: videobuf2 queue
1686 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1689 * Should be called from vidioc_qbuf ioctl handler of a driver.
1691 * 1) verifies the passed buffer,
1692 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1693 * which driver-specific buffer initialization can be performed,
1694 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1695 * callback for processing.
1697 * The return values from this function are intended to be directly returned
1698 * from vidioc_qbuf handler in driver.
1700 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1703 dprintk(1, "%s(): file io in progress\n", __func__
);
1707 return vb2_internal_qbuf(q
, b
);
1709 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1712 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1715 * Will sleep if required for nonblocking == false.
1717 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1720 * All operations on vb_done_list are performed under done_lock
1721 * spinlock protection. However, buffers may be removed from
1722 * it and returned to userspace only while holding both driver's
1723 * lock and the done_lock spinlock. Thus we can be sure that as
1724 * long as we hold the driver's lock, the list will remain not
1725 * empty if list_empty() check succeeds.
1731 if (!q
->streaming
) {
1732 dprintk(1, "Streaming off, will not wait for buffers\n");
1736 if (!list_empty(&q
->done_list
)) {
1738 * Found a buffer that we were waiting for.
1744 dprintk(1, "Nonblocking and no buffers to dequeue, "
1750 * We are streaming and blocking, wait for another buffer to
1751 * become ready or for streamoff. Driver's lock is released to
1752 * allow streamoff or qbuf to be called while waiting.
1754 call_qop(q
, wait_prepare
, q
);
1757 * All locks have been released, it is safe to sleep now.
1759 dprintk(3, "Will sleep waiting for buffers\n");
1760 ret
= wait_event_interruptible(q
->done_wq
,
1761 !list_empty(&q
->done_list
) || !q
->streaming
);
1764 * We need to reevaluate both conditions again after reacquiring
1765 * the locks or return an error if one occurred.
1767 call_qop(q
, wait_finish
, q
);
1769 dprintk(1, "Sleep was interrupted\n");
1777 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1779 * Will sleep if required for nonblocking == false.
1781 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1782 struct v4l2_buffer
*b
, int nonblocking
)
1784 unsigned long flags
;
1788 * Wait for at least one buffer to become available on the done_list.
1790 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1795 * Driver's lock has been held since we last verified that done_list
1796 * is not empty, so no need for another list_empty(done_list) check.
1798 spin_lock_irqsave(&q
->done_lock
, flags
);
1799 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1801 * Only remove the buffer from done_list if v4l2_buffer can handle all
1804 ret
= __verify_planes_array(*vb
, b
);
1806 list_del(&(*vb
)->done_entry
);
1807 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1813 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1814 * @q: videobuf2 queue
1816 * This function will wait until all buffers that have been given to the driver
1817 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1818 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1819 * taken, for example from stop_streaming() callback.
1821 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1823 if (!q
->streaming
) {
1824 dprintk(1, "Streaming off, will not wait for buffers\n");
1828 if (!q
->retry_start_streaming
)
1829 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1832 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1835 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1837 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1839 struct vb2_queue
*q
= vb
->vb2_queue
;
1842 /* nothing to do if the buffer is already dequeued */
1843 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1846 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1848 /* unmap DMABUF buffer */
1849 if (q
->memory
== V4L2_MEMORY_DMABUF
)
1850 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1851 if (!vb
->planes
[i
].dbuf_mapped
)
1853 call_memop(vb
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1854 vb
->planes
[i
].dbuf_mapped
= 0;
1858 static int vb2_internal_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1860 struct vb2_buffer
*vb
= NULL
;
1863 if (b
->type
!= q
->type
) {
1864 dprintk(1, "dqbuf: invalid buffer type\n");
1867 ret
= __vb2_get_done_vb(q
, &vb
, b
, nonblocking
);
1871 call_vb_qop(vb
, buf_finish
, vb
);
1873 switch (vb
->state
) {
1874 case VB2_BUF_STATE_DONE
:
1875 dprintk(3, "dqbuf: Returning done buffer\n");
1877 case VB2_BUF_STATE_ERROR
:
1878 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1881 dprintk(1, "dqbuf: Invalid buffer state\n");
1885 /* Fill buffer information for the userspace */
1886 __fill_v4l2_buffer(vb
, b
);
1887 /* Remove from videobuf queue */
1888 list_del(&vb
->queued_entry
);
1889 /* go back to dequeued state */
1892 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1893 vb
->v4l2_buf
.index
, vb
->state
);
1899 * vb2_dqbuf() - Dequeue a buffer to the userspace
1900 * @q: videobuf2 queue
1901 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1903 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1904 * buffers ready for dequeuing are present. Normally the driver
1905 * would be passing (file->f_flags & O_NONBLOCK) here
1907 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1909 * 1) verifies the passed buffer,
1910 * 2) calls buf_finish callback in the driver (if provided), in which
1911 * driver can perform any additional operations that may be required before
1912 * returning the buffer to userspace, such as cache sync,
1913 * 3) the buffer struct members are filled with relevant information for
1916 * The return values from this function are intended to be directly returned
1917 * from vidioc_dqbuf handler in driver.
1919 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1922 dprintk(1, "dqbuf: file io in progress\n");
1925 return vb2_internal_dqbuf(q
, b
, nonblocking
);
1927 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1930 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1932 * Removes all queued buffers from driver's queue and all buffers queued by
1933 * userspace from videobuf's queue. Returns to state after reqbufs.
1935 static void __vb2_queue_cancel(struct vb2_queue
*q
)
1939 if (q
->retry_start_streaming
) {
1940 q
->retry_start_streaming
= 0;
1945 * Tell driver to stop all transactions and release all queued
1949 call_qop(q
, stop_streaming
, q
);
1953 * Remove all buffers from videobuf's list...
1955 INIT_LIST_HEAD(&q
->queued_list
);
1957 * ...and done list; userspace will not receive any buffers it
1958 * has not already dequeued before initiating cancel.
1960 INIT_LIST_HEAD(&q
->done_list
);
1961 atomic_set(&q
->owned_by_drv_count
, 0);
1962 wake_up_all(&q
->done_wq
);
1965 * Reinitialize all buffers for next use.
1966 * Make sure to call buf_finish for any queued buffers. Normally
1967 * that's done in dqbuf, but that's not going to happen when we
1968 * cancel the whole queue. Note: this code belongs here, not in
1969 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
1970 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
1971 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1973 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1974 struct vb2_buffer
*vb
= q
->bufs
[i
];
1976 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1977 vb
->state
= VB2_BUF_STATE_PREPARED
;
1978 call_vb_qop(vb
, buf_finish
, vb
);
1984 static int vb2_internal_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
1986 struct vb2_buffer
*vb
;
1989 if (type
!= q
->type
) {
1990 dprintk(1, "streamon: invalid stream type\n");
1995 dprintk(3, "streamon successful: already streaming\n");
1999 if (!q
->num_buffers
) {
2000 dprintk(1, "streamon: no buffers have been allocated\n");
2004 if (!q
->num_buffers
) {
2005 dprintk(1, "streamon: no buffers have been allocated\n");
2010 * If any buffers were queued before streamon,
2011 * we can now pass them to driver for processing.
2013 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
2014 __enqueue_in_driver(vb
);
2016 /* Tell driver to start streaming. */
2017 ret
= vb2_start_streaming(q
);
2019 __vb2_queue_cancel(q
);
2025 dprintk(3, "Streamon successful\n");
2030 * vb2_streamon - start streaming
2031 * @q: videobuf2 queue
2032 * @type: type argument passed from userspace to vidioc_streamon handler
2034 * Should be called from vidioc_streamon handler of a driver.
2036 * 1) verifies current state
2037 * 2) passes any previously queued buffers to the driver and starts streaming
2039 * The return values from this function are intended to be directly returned
2040 * from vidioc_streamon handler in the driver.
2042 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2045 dprintk(1, "streamon: file io in progress\n");
2048 return vb2_internal_streamon(q
, type
);
2050 EXPORT_SYMBOL_GPL(vb2_streamon
);
2052 static int vb2_internal_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2054 if (type
!= q
->type
) {
2055 dprintk(1, "streamoff: invalid stream type\n");
2059 if (!q
->streaming
) {
2060 dprintk(3, "streamoff successful: not streaming\n");
2065 * Cancel will pause streaming and remove all buffers from the driver
2066 * and videobuf, effectively returning control over them to userspace.
2068 __vb2_queue_cancel(q
);
2070 dprintk(3, "Streamoff successful\n");
2075 * vb2_streamoff - stop streaming
2076 * @q: videobuf2 queue
2077 * @type: type argument passed from userspace to vidioc_streamoff handler
2079 * Should be called from vidioc_streamoff handler of a driver.
2081 * 1) verifies current state,
2082 * 2) stop streaming and dequeues any queued buffers, including those previously
2083 * passed to the driver (after waiting for the driver to finish).
2085 * This call can be used for pausing playback.
2086 * The return values from this function are intended to be directly returned
2087 * from vidioc_streamoff handler in the driver
2089 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2092 dprintk(1, "streamoff: file io in progress\n");
2095 return vb2_internal_streamoff(q
, type
);
2097 EXPORT_SYMBOL_GPL(vb2_streamoff
);
2100 * __find_plane_by_offset() - find plane associated with the given offset off
2102 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
2103 unsigned int *_buffer
, unsigned int *_plane
)
2105 struct vb2_buffer
*vb
;
2106 unsigned int buffer
, plane
;
2109 * Go over all buffers and their planes, comparing the given offset
2110 * with an offset assigned to each plane. If a match is found,
2111 * return its buffer and plane numbers.
2113 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
2114 vb
= q
->bufs
[buffer
];
2116 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
2117 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
2129 * vb2_expbuf() - Export a buffer as a file descriptor
2130 * @q: videobuf2 queue
2131 * @eb: export buffer structure passed from userspace to vidioc_expbuf
2134 * The return values from this function are intended to be directly returned
2135 * from vidioc_expbuf handler in driver.
2137 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
2139 struct vb2_buffer
*vb
= NULL
;
2140 struct vb2_plane
*vb_plane
;
2142 struct dma_buf
*dbuf
;
2144 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2145 dprintk(1, "Queue is not currently set up for mmap\n");
2149 if (!q
->mem_ops
->get_dmabuf
) {
2150 dprintk(1, "Queue does not support DMA buffer exporting\n");
2154 if (eb
->flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2155 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
2159 if (eb
->type
!= q
->type
) {
2160 dprintk(1, "qbuf: invalid buffer type\n");
2164 if (eb
->index
>= q
->num_buffers
) {
2165 dprintk(1, "buffer index out of range\n");
2169 vb
= q
->bufs
[eb
->index
];
2171 if (eb
->plane
>= vb
->num_planes
) {
2172 dprintk(1, "buffer plane out of range\n");
2176 vb_plane
= &vb
->planes
[eb
->plane
];
2178 dbuf
= call_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
, eb
->flags
& O_ACCMODE
);
2179 if (IS_ERR_OR_NULL(dbuf
)) {
2180 dprintk(1, "Failed to export buffer %d, plane %d\n",
2181 eb
->index
, eb
->plane
);
2182 fail_memop(vb
, get_dmabuf
);
2186 ret
= dma_buf_fd(dbuf
, eb
->flags
& ~O_ACCMODE
);
2188 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2189 eb
->index
, eb
->plane
, ret
);
2194 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2195 eb
->index
, eb
->plane
, ret
);
2200 EXPORT_SYMBOL_GPL(vb2_expbuf
);
2203 * vb2_mmap() - map video buffers into application address space
2204 * @q: videobuf2 queue
2205 * @vma: vma passed to the mmap file operation handler in the driver
2207 * Should be called from mmap file operation handler of a driver.
2208 * This function maps one plane of one of the available video buffers to
2209 * userspace. To map whole video memory allocated on reqbufs, this function
2210 * has to be called once per each plane per each buffer previously allocated.
2212 * When the userspace application calls mmap, it passes to it an offset returned
2213 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2214 * a "cookie", which is then used to identify the plane to be mapped.
2215 * This function finds a plane with a matching offset and a mapping is performed
2216 * by the means of a provided memory operation.
2218 * The return values from this function are intended to be directly returned
2219 * from the mmap handler in driver.
2221 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2223 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2224 struct vb2_buffer
*vb
;
2225 unsigned int buffer
, plane
;
2227 unsigned long length
;
2229 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2230 dprintk(1, "Queue is not currently set up for mmap\n");
2235 * Check memory area access mode.
2237 if (!(vma
->vm_flags
& VM_SHARED
)) {
2238 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2241 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
2242 if (!(vma
->vm_flags
& VM_WRITE
)) {
2243 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2247 if (!(vma
->vm_flags
& VM_READ
)) {
2248 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2254 * Find the plane corresponding to the offset passed by userspace.
2256 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2260 vb
= q
->bufs
[buffer
];
2263 * MMAP requires page_aligned buffers.
2264 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2265 * so, we need to do the same here.
2267 length
= PAGE_ALIGN(vb
->v4l2_planes
[plane
].length
);
2268 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2270 "MMAP invalid, as it would overflow buffer length\n");
2274 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2276 fail_memop(vb
, mmap
);
2280 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2283 EXPORT_SYMBOL_GPL(vb2_mmap
);
2286 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2289 unsigned long pgoff
,
2290 unsigned long flags
)
2292 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2293 struct vb2_buffer
*vb
;
2294 unsigned int buffer
, plane
;
2297 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2298 dprintk(1, "Queue is not currently set up for mmap\n");
2303 * Find the plane corresponding to the offset passed by userspace.
2305 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2309 vb
= q
->bufs
[buffer
];
2311 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
2313 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2316 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2317 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2320 * vb2_poll() - implements poll userspace operation
2321 * @q: videobuf2 queue
2322 * @file: file argument passed to the poll file operation handler
2323 * @wait: wait argument passed to the poll file operation handler
2325 * This function implements poll file operation handler for a driver.
2326 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2327 * be informed that the file descriptor of a video device is available for
2329 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2330 * will be reported as available for writing.
2332 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2335 * The return values from this function are intended to be directly returned
2336 * from poll handler in driver.
2338 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
2340 struct video_device
*vfd
= video_devdata(file
);
2341 unsigned long req_events
= poll_requested_events(wait
);
2342 struct vb2_buffer
*vb
= NULL
;
2343 unsigned int res
= 0;
2344 unsigned long flags
;
2346 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
2347 struct v4l2_fh
*fh
= file
->private_data
;
2349 if (v4l2_event_pending(fh
))
2351 else if (req_events
& POLLPRI
)
2352 poll_wait(file
, &fh
->wait
, wait
);
2355 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && !(req_events
& (POLLIN
| POLLRDNORM
)))
2357 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && !(req_events
& (POLLOUT
| POLLWRNORM
)))
2361 * Start file I/O emulator only if streaming API has not been used yet.
2363 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
2364 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
2365 (req_events
& (POLLIN
| POLLRDNORM
))) {
2366 if (__vb2_init_fileio(q
, 1))
2367 return res
| POLLERR
;
2369 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
2370 (req_events
& (POLLOUT
| POLLWRNORM
))) {
2371 if (__vb2_init_fileio(q
, 0))
2372 return res
| POLLERR
;
2374 * Write to OUTPUT queue can be done immediately.
2376 return res
| POLLOUT
| POLLWRNORM
;
2381 * There is nothing to wait for if no buffers have already been queued.
2383 if (list_empty(&q
->queued_list
))
2384 return res
| POLLERR
;
2386 if (list_empty(&q
->done_list
))
2387 poll_wait(file
, &q
->done_wq
, wait
);
2390 * Take first buffer available for dequeuing.
2392 spin_lock_irqsave(&q
->done_lock
, flags
);
2393 if (!list_empty(&q
->done_list
))
2394 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2396 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2398 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2399 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2400 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ?
2401 res
| POLLOUT
| POLLWRNORM
:
2402 res
| POLLIN
| POLLRDNORM
;
2406 EXPORT_SYMBOL_GPL(vb2_poll
);
2409 * vb2_queue_init() - initialize a videobuf2 queue
2410 * @q: videobuf2 queue; this structure should be allocated in driver
2412 * The vb2_queue structure should be allocated by the driver. The driver is
2413 * responsible of clearing it's content and setting initial values for some
2414 * required entries before calling this function.
2415 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2416 * to the struct vb2_queue description in include/media/videobuf2-core.h
2417 * for more information.
2419 int vb2_queue_init(struct vb2_queue
*q
)
2426 WARN_ON(!q
->mem_ops
) ||
2427 WARN_ON(!q
->type
) ||
2428 WARN_ON(!q
->io_modes
) ||
2429 WARN_ON(!q
->ops
->queue_setup
) ||
2430 WARN_ON(!q
->ops
->buf_queue
) ||
2431 WARN_ON(q
->timestamp_flags
&
2432 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK
|
2433 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
)))
2436 /* Warn that the driver should choose an appropriate timestamp type */
2437 WARN_ON((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
2438 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN
);
2440 INIT_LIST_HEAD(&q
->queued_list
);
2441 INIT_LIST_HEAD(&q
->done_list
);
2442 spin_lock_init(&q
->done_lock
);
2443 init_waitqueue_head(&q
->done_wq
);
2445 if (q
->buf_struct_size
== 0)
2446 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2450 EXPORT_SYMBOL_GPL(vb2_queue_init
);
2453 * vb2_queue_release() - stop streaming, release the queue and free memory
2454 * @q: videobuf2 queue
2456 * This function stops streaming and performs necessary clean ups, including
2457 * freeing video buffer memory. The driver is responsible for freeing
2458 * the vb2_queue structure itself.
2460 void vb2_queue_release(struct vb2_queue
*q
)
2462 __vb2_cleanup_fileio(q
);
2463 __vb2_queue_cancel(q
);
2464 __vb2_queue_free(q
, q
->num_buffers
);
2466 EXPORT_SYMBOL_GPL(vb2_queue_release
);
2469 * struct vb2_fileio_buf - buffer context used by file io emulator
2471 * vb2 provides a compatibility layer and emulator of file io (read and
2472 * write) calls on top of streaming API. This structure is used for
2473 * tracking context related to the buffers.
2475 struct vb2_fileio_buf
{
2479 unsigned int queued
:1;
2483 * struct vb2_fileio_data - queue context used by file io emulator
2485 * @cur_index: the index of the buffer currently being read from or
2486 * written to. If equal to q->num_buffers then a new buffer
2488 * @initial_index: in the read() case all buffers are queued up immediately
2489 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2490 * buffers. However, in the write() case no buffers are initially
2491 * queued, instead whenever a buffer is full it is queued up by
2492 * __vb2_perform_fileio(). Only once all available buffers have
2493 * been queued up will __vb2_perform_fileio() start to dequeue
2494 * buffers. This means that initially __vb2_perform_fileio()
2495 * needs to know what buffer index to use when it is queuing up
2496 * the buffers for the first time. That initial index is stored
2497 * in this field. Once it is equal to q->num_buffers all
2498 * available buffers have been queued and __vb2_perform_fileio()
2499 * should start the normal dequeue/queue cycle.
2501 * vb2 provides a compatibility layer and emulator of file io (read and
2502 * write) calls on top of streaming API. For proper operation it required
2503 * this structure to save the driver state between each call of the read
2504 * or write function.
2506 struct vb2_fileio_data
{
2507 struct v4l2_requestbuffers req
;
2508 struct v4l2_buffer b
;
2509 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
2510 unsigned int cur_index
;
2511 unsigned int initial_index
;
2512 unsigned int q_count
;
2513 unsigned int dq_count
;
2518 * __vb2_init_fileio() - initialize file io emulator
2519 * @q: videobuf2 queue
2520 * @read: mode selector (1 means read, 0 means write)
2522 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2524 struct vb2_fileio_data
*fileio
;
2526 unsigned int count
= 0;
2531 if ((read
&& !(q
->io_modes
& VB2_READ
)) ||
2532 (!read
&& !(q
->io_modes
& VB2_WRITE
)))
2536 * Check if device supports mapping buffers to kernel virtual space.
2538 if (!q
->mem_ops
->vaddr
)
2542 * Check if streaming api has not been already activated.
2544 if (q
->streaming
|| q
->num_buffers
> 0)
2548 * Start with count 1, driver can increase it in queue_setup()
2552 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2553 (read
) ? "read" : "write", count
, q
->io_flags
);
2555 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
2559 fileio
->flags
= q
->io_flags
;
2562 * Request buffers and use MMAP type to force driver
2563 * to allocate buffers by itself.
2565 fileio
->req
.count
= count
;
2566 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
2567 fileio
->req
.type
= q
->type
;
2568 ret
= vb2_reqbufs(q
, &fileio
->req
);
2573 * Check if plane_count is correct
2574 * (multiplane buffers are not supported).
2576 if (q
->bufs
[0]->num_planes
!= 1) {
2582 * Get kernel address of each buffer.
2584 for (i
= 0; i
< q
->num_buffers
; i
++) {
2585 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2586 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2590 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2594 * Read mode requires pre queuing of all buffers.
2598 * Queue all buffers.
2600 for (i
= 0; i
< q
->num_buffers
; i
++) {
2601 struct v4l2_buffer
*b
= &fileio
->b
;
2602 memset(b
, 0, sizeof(*b
));
2604 b
->memory
= q
->memory
;
2606 ret
= vb2_qbuf(q
, b
);
2609 fileio
->bufs
[i
].queued
= 1;
2612 * All buffers have been queued, so mark that by setting
2613 * initial_index to q->num_buffers
2615 fileio
->initial_index
= q
->num_buffers
;
2616 fileio
->cur_index
= q
->num_buffers
;
2622 ret
= vb2_streamon(q
, q
->type
);
2631 fileio
->req
.count
= 0;
2632 vb2_reqbufs(q
, &fileio
->req
);
2640 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2641 * @q: videobuf2 queue
2643 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2645 struct vb2_fileio_data
*fileio
= q
->fileio
;
2648 vb2_internal_streamoff(q
, q
->type
);
2650 fileio
->req
.count
= 0;
2651 vb2_reqbufs(q
, &fileio
->req
);
2653 dprintk(3, "file io emulator closed\n");
2659 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2660 * @q: videobuf2 queue
2661 * @data: pointed to target userspace buffer
2662 * @count: number of bytes to read or write
2663 * @ppos: file handle position tracking pointer
2664 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2665 * @read: access mode selector (1 means read, 0 means write)
2667 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2668 loff_t
*ppos
, int nonblock
, int read
)
2670 struct vb2_fileio_data
*fileio
;
2671 struct vb2_fileio_buf
*buf
;
2674 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2675 read
? "read" : "write", (long)*ppos
, count
,
2676 nonblock
? "non" : "");
2682 * Initialize emulator on first call.
2685 ret
= __vb2_init_fileio(q
, read
);
2686 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2693 * Check if we need to dequeue the buffer.
2695 index
= fileio
->cur_index
;
2696 if (index
>= q
->num_buffers
) {
2698 * Call vb2_dqbuf to get buffer back.
2700 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2701 fileio
->b
.type
= q
->type
;
2702 fileio
->b
.memory
= q
->memory
;
2703 ret
= vb2_internal_dqbuf(q
, &fileio
->b
, nonblock
);
2704 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2707 fileio
->dq_count
+= 1;
2709 fileio
->cur_index
= index
= fileio
->b
.index
;
2710 buf
= &fileio
->bufs
[index
];
2713 * Get number of bytes filled by the driver
2717 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2718 : vb2_plane_size(q
->bufs
[index
], 0);
2720 buf
= &fileio
->bufs
[index
];
2724 * Limit count on last few bytes of the buffer.
2726 if (buf
->pos
+ count
> buf
->size
) {
2727 count
= buf
->size
- buf
->pos
;
2728 dprintk(5, "reducing read count: %zd\n", count
);
2732 * Transfer data to userspace.
2734 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2735 count
, index
, buf
->pos
);
2737 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2739 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2741 dprintk(3, "file io: error copying data\n");
2752 * Queue next buffer if required.
2754 if (buf
->pos
== buf
->size
||
2755 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2757 * Check if this is the last buffer to read.
2759 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2760 fileio
->dq_count
== 1) {
2761 dprintk(3, "file io: read limit reached\n");
2762 return __vb2_cleanup_fileio(q
);
2766 * Call vb2_qbuf and give buffer to the driver.
2768 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2769 fileio
->b
.type
= q
->type
;
2770 fileio
->b
.memory
= q
->memory
;
2771 fileio
->b
.index
= index
;
2772 fileio
->b
.bytesused
= buf
->pos
;
2773 ret
= vb2_internal_qbuf(q
, &fileio
->b
);
2774 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2779 * Buffer has been queued, update the status
2783 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2784 fileio
->q_count
+= 1;
2786 * If we are queuing up buffers for the first time, then
2787 * increase initial_index by one.
2789 if (fileio
->initial_index
< q
->num_buffers
)
2790 fileio
->initial_index
++;
2792 * The next buffer to use is either a buffer that's going to be
2793 * queued for the first time (initial_index < q->num_buffers)
2794 * or it is equal to q->num_buffers, meaning that the next
2795 * time we need to dequeue a buffer since we've now queued up
2796 * all the 'first time' buffers.
2798 fileio
->cur_index
= fileio
->initial_index
;
2802 * Return proper number of bytes processed.
2809 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2810 loff_t
*ppos
, int nonblocking
)
2812 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2814 EXPORT_SYMBOL_GPL(vb2_read
);
2816 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2817 loff_t
*ppos
, int nonblocking
)
2819 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2820 ppos
, nonblocking
, 0);
2822 EXPORT_SYMBOL_GPL(vb2_write
);
2826 * The following functions are not part of the vb2 core API, but are helper
2827 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2828 * and struct vb2_ops.
2829 * They contain boilerplate code that most if not all drivers have to do
2830 * and so they simplify the driver code.
2833 /* The queue is busy if there is a owner and you are not that owner. */
2834 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
2836 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
2839 /* vb2 ioctl helpers */
2841 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
2842 struct v4l2_requestbuffers
*p
)
2844 struct video_device
*vdev
= video_devdata(file
);
2845 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
2849 if (vb2_queue_is_busy(vdev
, file
))
2851 res
= __reqbufs(vdev
->queue
, p
);
2852 /* If count == 0, then the owner has released all buffers and he
2853 is no longer owner of the queue. Otherwise we have a new owner. */
2855 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
2858 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
2860 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
2861 struct v4l2_create_buffers
*p
)
2863 struct video_device
*vdev
= video_devdata(file
);
2864 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->format
.type
);
2866 p
->index
= vdev
->queue
->num_buffers
;
2867 /* If count == 0, then just check if memory and type are valid.
2868 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2870 return res
!= -EBUSY
? res
: 0;
2873 if (vb2_queue_is_busy(vdev
, file
))
2875 res
= __create_bufs(vdev
->queue
, p
);
2877 vdev
->queue
->owner
= file
->private_data
;
2880 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
2882 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
2883 struct v4l2_buffer
*p
)
2885 struct video_device
*vdev
= video_devdata(file
);
2887 if (vb2_queue_is_busy(vdev
, file
))
2889 return vb2_prepare_buf(vdev
->queue
, p
);
2891 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
2893 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2895 struct video_device
*vdev
= video_devdata(file
);
2897 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2898 return vb2_querybuf(vdev
->queue
, p
);
2900 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
2902 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2904 struct video_device
*vdev
= video_devdata(file
);
2906 if (vb2_queue_is_busy(vdev
, file
))
2908 return vb2_qbuf(vdev
->queue
, p
);
2910 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
2912 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2914 struct video_device
*vdev
= video_devdata(file
);
2916 if (vb2_queue_is_busy(vdev
, file
))
2918 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
2920 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
2922 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2924 struct video_device
*vdev
= video_devdata(file
);
2926 if (vb2_queue_is_busy(vdev
, file
))
2928 return vb2_streamon(vdev
->queue
, i
);
2930 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
2932 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2934 struct video_device
*vdev
= video_devdata(file
);
2936 if (vb2_queue_is_busy(vdev
, file
))
2938 return vb2_streamoff(vdev
->queue
, i
);
2940 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
2942 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
2944 struct video_device
*vdev
= video_devdata(file
);
2946 if (vb2_queue_is_busy(vdev
, file
))
2948 return vb2_expbuf(vdev
->queue
, p
);
2950 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
2952 /* v4l2_file_operations helpers */
2954 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2956 struct video_device
*vdev
= video_devdata(file
);
2957 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2960 if (lock
&& mutex_lock_interruptible(lock
))
2961 return -ERESTARTSYS
;
2962 err
= vb2_mmap(vdev
->queue
, vma
);
2967 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
2969 int _vb2_fop_release(struct file
*file
, struct mutex
*lock
)
2971 struct video_device
*vdev
= video_devdata(file
);
2973 if (file
->private_data
== vdev
->queue
->owner
) {
2976 vb2_queue_release(vdev
->queue
);
2977 vdev
->queue
->owner
= NULL
;
2981 return v4l2_fh_release(file
);
2983 EXPORT_SYMBOL_GPL(_vb2_fop_release
);
2985 int vb2_fop_release(struct file
*file
)
2987 struct video_device
*vdev
= video_devdata(file
);
2988 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
2990 return _vb2_fop_release(file
, lock
);
2992 EXPORT_SYMBOL_GPL(vb2_fop_release
);
2994 ssize_t
vb2_fop_write(struct file
*file
, const char __user
*buf
,
2995 size_t count
, loff_t
*ppos
)
2997 struct video_device
*vdev
= video_devdata(file
);
2998 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3001 if (lock
&& mutex_lock_interruptible(lock
))
3002 return -ERESTARTSYS
;
3003 if (vb2_queue_is_busy(vdev
, file
))
3005 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
3006 file
->f_flags
& O_NONBLOCK
);
3007 if (vdev
->queue
->fileio
)
3008 vdev
->queue
->owner
= file
->private_data
;
3014 EXPORT_SYMBOL_GPL(vb2_fop_write
);
3016 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
3017 size_t count
, loff_t
*ppos
)
3019 struct video_device
*vdev
= video_devdata(file
);
3020 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3023 if (lock
&& mutex_lock_interruptible(lock
))
3024 return -ERESTARTSYS
;
3025 if (vb2_queue_is_busy(vdev
, file
))
3027 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
3028 file
->f_flags
& O_NONBLOCK
);
3029 if (vdev
->queue
->fileio
)
3030 vdev
->queue
->owner
= file
->private_data
;
3036 EXPORT_SYMBOL_GPL(vb2_fop_read
);
3038 unsigned int vb2_fop_poll(struct file
*file
, poll_table
*wait
)
3040 struct video_device
*vdev
= video_devdata(file
);
3041 struct vb2_queue
*q
= vdev
->queue
;
3042 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
3043 unsigned long req_events
= poll_requested_events(wait
);
3046 bool must_lock
= false;
3048 /* Try to be smart: only lock if polling might start fileio,
3049 otherwise locking will only introduce unwanted delays. */
3050 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
3051 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
3052 (req_events
& (POLLIN
| POLLRDNORM
)))
3054 else if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
3055 (req_events
& (POLLOUT
| POLLWRNORM
)))
3059 /* If locking is needed, but this helper doesn't know how, then you
3060 shouldn't be using this helper but you should write your own. */
3061 WARN_ON(must_lock
&& !lock
);
3063 if (must_lock
&& lock
&& mutex_lock_interruptible(lock
))
3068 res
= vb2_poll(vdev
->queue
, file
, wait
);
3070 /* If fileio was started, then we have a new queue owner. */
3071 if (must_lock
&& !fileio
&& q
->fileio
)
3072 q
->owner
= file
->private_data
;
3073 if (must_lock
&& lock
)
3077 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
3080 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
3081 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
3083 struct video_device
*vdev
= video_devdata(file
);
3084 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3087 if (lock
&& mutex_lock_interruptible(lock
))
3088 return -ERESTARTSYS
;
3089 ret
= vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
3094 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
3097 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
3099 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
3101 mutex_unlock(vq
->lock
);
3103 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
3105 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
3107 mutex_lock(vq
->lock
);
3109 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
3111 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
3112 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
3113 MODULE_LICENSE("GPL");