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)
103 static void __vb2_queue_cancel(struct vb2_queue
*q
);
106 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
108 static int __vb2_buf_mem_alloc(struct vb2_buffer
*vb
)
110 struct vb2_queue
*q
= vb
->vb2_queue
;
115 * Allocate memory for all planes in this buffer
116 * NOTE: mmapped areas should be page aligned
118 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
119 unsigned long size
= PAGE_ALIGN(q
->plane_sizes
[plane
]);
121 mem_priv
= call_memop(vb
, alloc
, q
->alloc_ctx
[plane
],
123 if (IS_ERR_OR_NULL(mem_priv
))
126 /* Associate allocator private data with this plane */
127 vb
->planes
[plane
].mem_priv
= mem_priv
;
128 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
133 fail_memop(vb
, alloc
);
134 /* Free already allocated memory if one of the allocations failed */
135 for (; plane
> 0; --plane
) {
136 call_memop(vb
, put
, vb
->planes
[plane
- 1].mem_priv
);
137 vb
->planes
[plane
- 1].mem_priv
= NULL
;
144 * __vb2_buf_mem_free() - free memory of the given buffer
146 static void __vb2_buf_mem_free(struct vb2_buffer
*vb
)
150 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
151 call_memop(vb
, put
, vb
->planes
[plane
].mem_priv
);
152 vb
->planes
[plane
].mem_priv
= NULL
;
153 dprintk(3, "Freed plane %d of buffer %d\n", plane
,
159 * __vb2_buf_userptr_put() - release userspace memory associated with
162 static void __vb2_buf_userptr_put(struct vb2_buffer
*vb
)
166 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
167 if (vb
->planes
[plane
].mem_priv
)
168 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
169 vb
->planes
[plane
].mem_priv
= NULL
;
174 * __vb2_plane_dmabuf_put() - release memory associated with
175 * a DMABUF shared plane
177 static void __vb2_plane_dmabuf_put(struct vb2_buffer
*vb
, struct vb2_plane
*p
)
183 call_memop(vb
, unmap_dmabuf
, p
->mem_priv
);
185 call_memop(vb
, detach_dmabuf
, p
->mem_priv
);
186 dma_buf_put(p
->dbuf
);
187 memset(p
, 0, sizeof(*p
));
191 * __vb2_buf_dmabuf_put() - release memory associated with
192 * a DMABUF shared buffer
194 static void __vb2_buf_dmabuf_put(struct vb2_buffer
*vb
)
198 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
199 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
203 * __setup_lengths() - setup initial lengths for every plane in
204 * every buffer on the queue
206 static void __setup_lengths(struct vb2_queue
*q
, unsigned int n
)
208 unsigned int buffer
, plane
;
209 struct vb2_buffer
*vb
;
211 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
212 vb
= q
->bufs
[buffer
];
216 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
217 vb
->v4l2_planes
[plane
].length
= q
->plane_sizes
[plane
];
222 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
223 * every buffer on the queue
225 static void __setup_offsets(struct vb2_queue
*q
, unsigned int n
)
227 unsigned int buffer
, plane
;
228 struct vb2_buffer
*vb
;
231 if (q
->num_buffers
) {
232 struct v4l2_plane
*p
;
233 vb
= q
->bufs
[q
->num_buffers
- 1];
234 p
= &vb
->v4l2_planes
[vb
->num_planes
- 1];
235 off
= PAGE_ALIGN(p
->m
.mem_offset
+ p
->length
);
240 for (buffer
= q
->num_buffers
; buffer
< q
->num_buffers
+ n
; ++buffer
) {
241 vb
= q
->bufs
[buffer
];
245 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
246 vb
->v4l2_planes
[plane
].m
.mem_offset
= off
;
248 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
251 off
+= vb
->v4l2_planes
[plane
].length
;
252 off
= PAGE_ALIGN(off
);
258 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
259 * video buffer memory for all buffers/planes on the queue and initializes the
262 * Returns the number of buffers successfully allocated.
264 static int __vb2_queue_alloc(struct vb2_queue
*q
, enum v4l2_memory memory
,
265 unsigned int num_buffers
, unsigned int num_planes
)
268 struct vb2_buffer
*vb
;
271 for (buffer
= 0; buffer
< num_buffers
; ++buffer
) {
272 /* Allocate videobuf buffer structures */
273 vb
= kzalloc(q
->buf_struct_size
, GFP_KERNEL
);
275 dprintk(1, "Memory alloc for buffer struct failed\n");
279 /* Length stores number of planes for multiplanar buffers */
280 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
))
281 vb
->v4l2_buf
.length
= num_planes
;
283 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
285 vb
->num_planes
= num_planes
;
286 vb
->v4l2_buf
.index
= q
->num_buffers
+ buffer
;
287 vb
->v4l2_buf
.type
= q
->type
;
288 vb
->v4l2_buf
.memory
= memory
;
290 /* Allocate video buffer memory for the MMAP type */
291 if (memory
== V4L2_MEMORY_MMAP
) {
292 ret
= __vb2_buf_mem_alloc(vb
);
294 dprintk(1, "Failed allocating memory for "
295 "buffer %d\n", buffer
);
300 * Call the driver-provided buffer initialization
301 * callback, if given. An error in initialization
302 * results in queue setup failure.
304 ret
= call_vb_qop(vb
, buf_init
, vb
);
306 dprintk(1, "Buffer %d %p initialization"
307 " failed\n", buffer
, vb
);
308 fail_vb_qop(vb
, buf_init
);
309 __vb2_buf_mem_free(vb
);
315 q
->bufs
[q
->num_buffers
+ buffer
] = vb
;
318 __setup_lengths(q
, buffer
);
319 if (memory
== V4L2_MEMORY_MMAP
)
320 __setup_offsets(q
, buffer
);
322 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
329 * __vb2_free_mem() - release all video buffer memory for a given queue
331 static void __vb2_free_mem(struct vb2_queue
*q
, unsigned int buffers
)
334 struct vb2_buffer
*vb
;
336 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
338 vb
= q
->bufs
[buffer
];
342 /* Free MMAP buffers or release USERPTR buffers */
343 if (q
->memory
== V4L2_MEMORY_MMAP
)
344 __vb2_buf_mem_free(vb
);
345 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
346 __vb2_buf_dmabuf_put(vb
);
348 __vb2_buf_userptr_put(vb
);
353 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
354 * related information, if no buffers are left return the queue to an
355 * uninitialized state. Might be called even if the queue has already been freed.
357 static int __vb2_queue_free(struct vb2_queue
*q
, unsigned int buffers
)
362 * Sanity check: when preparing a buffer the queue lock is released for
363 * a short while (see __buf_prepare for the details), which would allow
364 * a race with a reqbufs which can call this function. Removing the
365 * buffers from underneath __buf_prepare is obviously a bad idea, so we
366 * check if any of the buffers is in the state PREPARING, and if so we
367 * just return -EAGAIN.
369 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
371 if (q
->bufs
[buffer
] == NULL
)
373 if (q
->bufs
[buffer
]->state
== VB2_BUF_STATE_PREPARING
) {
374 dprintk(1, "reqbufs: preparing buffers, cannot free\n");
379 /* Call driver-provided cleanup function for each buffer, if provided */
380 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
382 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
384 if (vb
&& vb
->planes
[0].mem_priv
)
385 call_vb_qop(vb
, buf_cleanup
, vb
);
388 /* Release video buffer memory */
389 __vb2_free_mem(q
, buffers
);
391 #ifdef CONFIG_VIDEO_ADV_DEBUG
393 * Check that all the calls were balances during the life-time of this
394 * queue. If not (or if the debug level is 1 or up), then dump the
395 * counters to the kernel log.
397 if (q
->num_buffers
) {
398 bool unbalanced
= q
->cnt_start_streaming
!= q
->cnt_stop_streaming
||
399 q
->cnt_wait_prepare
!= q
->cnt_wait_finish
;
401 if (unbalanced
|| debug
) {
402 pr_info("vb2: counters for queue %p:%s\n", q
,
403 unbalanced
? " UNBALANCED!" : "");
404 pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
405 q
->cnt_queue_setup
, q
->cnt_start_streaming
,
406 q
->cnt_stop_streaming
);
407 pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
408 q
->cnt_wait_prepare
, q
->cnt_wait_finish
);
410 q
->cnt_queue_setup
= 0;
411 q
->cnt_wait_prepare
= 0;
412 q
->cnt_wait_finish
= 0;
413 q
->cnt_start_streaming
= 0;
414 q
->cnt_stop_streaming
= 0;
416 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
417 struct vb2_buffer
*vb
= q
->bufs
[buffer
];
418 bool unbalanced
= vb
->cnt_mem_alloc
!= vb
->cnt_mem_put
||
419 vb
->cnt_mem_prepare
!= vb
->cnt_mem_finish
||
420 vb
->cnt_mem_get_userptr
!= vb
->cnt_mem_put_userptr
||
421 vb
->cnt_mem_attach_dmabuf
!= vb
->cnt_mem_detach_dmabuf
||
422 vb
->cnt_mem_map_dmabuf
!= vb
->cnt_mem_unmap_dmabuf
||
423 vb
->cnt_buf_queue
!= vb
->cnt_buf_done
||
424 vb
->cnt_buf_prepare
!= vb
->cnt_buf_finish
||
425 vb
->cnt_buf_init
!= vb
->cnt_buf_cleanup
;
427 if (unbalanced
|| debug
) {
428 pr_info("vb2: counters for queue %p, buffer %d:%s\n",
429 q
, buffer
, unbalanced
? " UNBALANCED!" : "");
430 pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
431 vb
->cnt_buf_init
, vb
->cnt_buf_cleanup
,
432 vb
->cnt_buf_prepare
, vb
->cnt_buf_finish
);
433 pr_info("vb2: buf_queue: %u buf_done: %u\n",
434 vb
->cnt_buf_queue
, vb
->cnt_buf_done
);
435 pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
436 vb
->cnt_mem_alloc
, vb
->cnt_mem_put
,
437 vb
->cnt_mem_prepare
, vb
->cnt_mem_finish
,
439 pr_info("vb2: get_userptr: %u put_userptr: %u\n",
440 vb
->cnt_mem_get_userptr
, vb
->cnt_mem_put_userptr
);
441 pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
442 vb
->cnt_mem_attach_dmabuf
, vb
->cnt_mem_detach_dmabuf
,
443 vb
->cnt_mem_map_dmabuf
, vb
->cnt_mem_unmap_dmabuf
);
444 pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
445 vb
->cnt_mem_get_dmabuf
,
446 vb
->cnt_mem_num_users
,
453 /* Free videobuf buffers */
454 for (buffer
= q
->num_buffers
- buffers
; buffer
< q
->num_buffers
;
456 kfree(q
->bufs
[buffer
]);
457 q
->bufs
[buffer
] = NULL
;
460 q
->num_buffers
-= buffers
;
461 if (!q
->num_buffers
) {
463 INIT_LIST_HEAD(&q
->queued_list
);
469 * __verify_planes_array() - verify that the planes array passed in struct
470 * v4l2_buffer from userspace can be safely used
472 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
474 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
477 /* Is memory for copying plane information present? */
478 if (NULL
== b
->m
.planes
) {
479 dprintk(1, "Multi-planar buffer passed but "
480 "planes array not provided\n");
484 if (b
->length
< vb
->num_planes
|| b
->length
> VIDEO_MAX_PLANES
) {
485 dprintk(1, "Incorrect planes array length, "
486 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
494 * __verify_length() - Verify that the bytesused value for each plane fits in
495 * the plane length and that the data offset doesn't exceed the bytesused value.
497 static int __verify_length(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
502 if (!V4L2_TYPE_IS_OUTPUT(b
->type
))
505 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
506 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
507 length
= (b
->memory
== V4L2_MEMORY_USERPTR
)
508 ? b
->m
.planes
[plane
].length
509 : vb
->v4l2_planes
[plane
].length
;
511 if (b
->m
.planes
[plane
].bytesused
> length
)
514 if (b
->m
.planes
[plane
].data_offset
> 0 &&
515 b
->m
.planes
[plane
].data_offset
>=
516 b
->m
.planes
[plane
].bytesused
)
520 length
= (b
->memory
== V4L2_MEMORY_USERPTR
)
521 ? b
->length
: vb
->v4l2_planes
[0].length
;
523 if (b
->bytesused
> length
)
531 * __buffer_in_use() - return true if the buffer is in use and
532 * the queue cannot be freed (by the means of REQBUFS(0)) call
534 static bool __buffer_in_use(struct vb2_queue
*q
, struct vb2_buffer
*vb
)
537 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
538 void *mem_priv
= vb
->planes
[plane
].mem_priv
;
540 * If num_users() has not been provided, call_memop
541 * will return 0, apparently nobody cares about this
542 * case anyway. If num_users() returns more than 1,
543 * we are not the only user of the plane's memory.
545 if (mem_priv
&& call_memop(vb
, num_users
, mem_priv
) > 1)
552 * __buffers_in_use() - return true if any buffers on the queue are in use and
553 * the queue cannot be freed (by the means of REQBUFS(0)) call
555 static bool __buffers_in_use(struct vb2_queue
*q
)
558 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
559 if (__buffer_in_use(q
, q
->bufs
[buffer
]))
566 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
567 * returned to userspace
569 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
571 struct vb2_queue
*q
= vb
->vb2_queue
;
573 /* Copy back data such as timestamp, flags, etc. */
574 memcpy(b
, &vb
->v4l2_buf
, offsetof(struct v4l2_buffer
, m
));
575 b
->reserved2
= vb
->v4l2_buf
.reserved2
;
576 b
->reserved
= vb
->v4l2_buf
.reserved
;
578 if (V4L2_TYPE_IS_MULTIPLANAR(q
->type
)) {
580 * Fill in plane-related data if userspace provided an array
581 * for it. The caller has already verified memory and size.
583 b
->length
= vb
->num_planes
;
584 memcpy(b
->m
.planes
, vb
->v4l2_planes
,
585 b
->length
* sizeof(struct v4l2_plane
));
588 * We use length and offset in v4l2_planes array even for
589 * single-planar buffers, but userspace does not.
591 b
->length
= vb
->v4l2_planes
[0].length
;
592 b
->bytesused
= vb
->v4l2_planes
[0].bytesused
;
593 if (q
->memory
== V4L2_MEMORY_MMAP
)
594 b
->m
.offset
= vb
->v4l2_planes
[0].m
.mem_offset
;
595 else if (q
->memory
== V4L2_MEMORY_USERPTR
)
596 b
->m
.userptr
= vb
->v4l2_planes
[0].m
.userptr
;
597 else if (q
->memory
== V4L2_MEMORY_DMABUF
)
598 b
->m
.fd
= vb
->v4l2_planes
[0].m
.fd
;
602 * Clear any buffer state related flags.
604 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
605 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
;
606 if ((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) !=
607 V4L2_BUF_FLAG_TIMESTAMP_COPY
) {
609 * For non-COPY timestamps, drop timestamp source bits
610 * and obtain the timestamp source from the queue.
612 b
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
613 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
617 case VB2_BUF_STATE_QUEUED
:
618 case VB2_BUF_STATE_ACTIVE
:
619 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
621 case VB2_BUF_STATE_ERROR
:
622 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
624 case VB2_BUF_STATE_DONE
:
625 b
->flags
|= V4L2_BUF_FLAG_DONE
;
627 case VB2_BUF_STATE_PREPARED
:
628 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
630 case VB2_BUF_STATE_PREPARING
:
631 case VB2_BUF_STATE_DEQUEUED
:
636 if (__buffer_in_use(q
, vb
))
637 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
641 * vb2_querybuf() - query video buffer information
643 * @b: buffer struct passed from userspace to vidioc_querybuf handler
646 * Should be called from vidioc_querybuf ioctl handler in driver.
647 * This function will verify the passed v4l2_buffer structure and fill the
648 * relevant information for the userspace.
650 * The return values from this function are intended to be directly returned
651 * from vidioc_querybuf handler in driver.
653 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
655 struct vb2_buffer
*vb
;
658 if (b
->type
!= q
->type
) {
659 dprintk(1, "querybuf: wrong buffer type\n");
663 if (b
->index
>= q
->num_buffers
) {
664 dprintk(1, "querybuf: buffer index out of range\n");
667 vb
= q
->bufs
[b
->index
];
668 ret
= __verify_planes_array(vb
, b
);
670 __fill_v4l2_buffer(vb
, b
);
673 EXPORT_SYMBOL(vb2_querybuf
);
676 * __verify_userptr_ops() - verify that all memory operations required for
677 * USERPTR queue type have been provided
679 static int __verify_userptr_ops(struct vb2_queue
*q
)
681 if (!(q
->io_modes
& VB2_USERPTR
) || !q
->mem_ops
->get_userptr
||
682 !q
->mem_ops
->put_userptr
)
689 * __verify_mmap_ops() - verify that all memory operations required for
690 * MMAP queue type have been provided
692 static int __verify_mmap_ops(struct vb2_queue
*q
)
694 if (!(q
->io_modes
& VB2_MMAP
) || !q
->mem_ops
->alloc
||
695 !q
->mem_ops
->put
|| !q
->mem_ops
->mmap
)
702 * __verify_dmabuf_ops() - verify that all memory operations required for
703 * DMABUF queue type have been provided
705 static int __verify_dmabuf_ops(struct vb2_queue
*q
)
707 if (!(q
->io_modes
& VB2_DMABUF
) || !q
->mem_ops
->attach_dmabuf
||
708 !q
->mem_ops
->detach_dmabuf
|| !q
->mem_ops
->map_dmabuf
||
709 !q
->mem_ops
->unmap_dmabuf
)
716 * __verify_memory_type() - Check whether the memory type and buffer type
717 * passed to a buffer operation are compatible with the queue.
719 static int __verify_memory_type(struct vb2_queue
*q
,
720 enum v4l2_memory memory
, enum v4l2_buf_type type
)
722 if (memory
!= V4L2_MEMORY_MMAP
&& memory
!= V4L2_MEMORY_USERPTR
&&
723 memory
!= V4L2_MEMORY_DMABUF
) {
724 dprintk(1, "reqbufs: unsupported memory type\n");
728 if (type
!= q
->type
) {
729 dprintk(1, "reqbufs: requested type is incorrect\n");
734 * Make sure all the required memory ops for given memory type
737 if (memory
== V4L2_MEMORY_MMAP
&& __verify_mmap_ops(q
)) {
738 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
742 if (memory
== V4L2_MEMORY_USERPTR
&& __verify_userptr_ops(q
)) {
743 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
747 if (memory
== V4L2_MEMORY_DMABUF
&& __verify_dmabuf_ops(q
)) {
748 dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
753 * Place the busy tests at the end: -EBUSY can be ignored when
754 * create_bufs is called with count == 0, but count == 0 should still
755 * do the memory and type validation.
758 dprintk(1, "reqbufs: file io in progress\n");
765 * __reqbufs() - Initiate streaming
766 * @q: videobuf2 queue
767 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
769 * Should be called from vidioc_reqbufs ioctl handler of a driver.
771 * 1) verifies streaming parameters passed from the userspace,
772 * 2) sets up the queue,
773 * 3) negotiates number of buffers and planes per buffer with the driver
774 * to be used during streaming,
775 * 4) allocates internal buffer structures (struct vb2_buffer), according to
776 * the agreed parameters,
777 * 5) for MMAP memory type, allocates actual video memory, using the
778 * memory handling/allocation routines provided during queue initialization
780 * If req->count is 0, all the memory will be freed instead.
781 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
782 * and the queue is not busy, memory will be reallocated.
784 * The return values from this function are intended to be directly returned
785 * from vidioc_reqbufs handler in driver.
787 static int __reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
789 unsigned int num_buffers
, allocated_buffers
, num_planes
= 0;
793 dprintk(1, "reqbufs: streaming active\n");
797 if (req
->count
== 0 || q
->num_buffers
!= 0 || q
->memory
!= req
->memory
) {
799 * We already have buffers allocated, so first check if they
800 * are not in use and can be freed.
802 if (q
->memory
== V4L2_MEMORY_MMAP
&& __buffers_in_use(q
)) {
803 dprintk(1, "reqbufs: memory in use, cannot free\n");
808 * Call queue_cancel to clean up any buffers in the PREPARED or
809 * QUEUED state which is possible if buffers were prepared or
810 * queued without ever calling STREAMON.
812 __vb2_queue_cancel(q
);
813 ret
= __vb2_queue_free(q
, q
->num_buffers
);
818 * In case of REQBUFS(0) return immediately without calling
819 * driver's queue_setup() callback and allocating resources.
826 * Make sure the requested values and current defaults are sane.
828 num_buffers
= min_t(unsigned int, req
->count
, VIDEO_MAX_FRAME
);
829 num_buffers
= max_t(unsigned int, req
->count
, q
->min_buffers_needed
);
830 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
831 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
832 q
->memory
= req
->memory
;
835 * Ask the driver how many buffers and planes per buffer it requires.
836 * Driver also sets the size and allocator context for each plane.
838 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
, &num_planes
,
839 q
->plane_sizes
, q
->alloc_ctx
);
841 fail_qop(q
, queue_setup
);
845 /* Finally, allocate buffers and video memory */
846 allocated_buffers
= __vb2_queue_alloc(q
, req
->memory
, num_buffers
, num_planes
);
847 if (allocated_buffers
== 0) {
848 dprintk(1, "Memory allocation failed\n");
853 * There is no point in continuing if we can't allocate the minimum
854 * number of buffers needed by this vb2_queue.
856 if (allocated_buffers
< q
->min_buffers_needed
)
860 * Check if driver can handle the allocated number of buffers.
862 if (!ret
&& allocated_buffers
< num_buffers
) {
863 num_buffers
= allocated_buffers
;
865 ret
= call_qop(q
, queue_setup
, q
, NULL
, &num_buffers
,
866 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
868 fail_qop(q
, queue_setup
);
870 if (!ret
&& allocated_buffers
< num_buffers
)
874 * Either the driver has accepted a smaller number of buffers,
875 * or .queue_setup() returned an error
879 q
->num_buffers
= allocated_buffers
;
883 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
884 * from q->num_buffers.
886 __vb2_queue_free(q
, allocated_buffers
);
891 * Return the number of successfully allocated buffers
894 req
->count
= allocated_buffers
;
900 * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
902 * @q: videobuf2 queue
903 * @req: struct passed from userspace to vidioc_reqbufs handler in driver
905 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
907 int ret
= __verify_memory_type(q
, req
->memory
, req
->type
);
909 return ret
? ret
: __reqbufs(q
, req
);
911 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
914 * __create_bufs() - Allocate buffers and any required auxiliary structs
915 * @q: videobuf2 queue
916 * @create: creation parameters, passed from userspace to vidioc_create_bufs
919 * Should be called from vidioc_create_bufs ioctl handler of a driver.
921 * 1) verifies parameter sanity
922 * 2) calls the .queue_setup() queue operation
923 * 3) performs any necessary memory allocations
925 * The return values from this function are intended to be directly returned
926 * from vidioc_create_bufs handler in driver.
928 static int __create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
930 unsigned int num_planes
= 0, num_buffers
, allocated_buffers
;
933 if (q
->num_buffers
== VIDEO_MAX_FRAME
) {
934 dprintk(1, "%s(): maximum number of buffers already allocated\n",
939 if (!q
->num_buffers
) {
940 memset(q
->plane_sizes
, 0, sizeof(q
->plane_sizes
));
941 memset(q
->alloc_ctx
, 0, sizeof(q
->alloc_ctx
));
942 q
->memory
= create
->memory
;
945 num_buffers
= min(create
->count
, VIDEO_MAX_FRAME
- q
->num_buffers
);
948 * Ask the driver, whether the requested number of buffers, planes per
949 * buffer and their sizes are acceptable
951 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
952 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
954 fail_qop(q
, queue_setup
);
958 /* Finally, allocate buffers and video memory */
959 allocated_buffers
= __vb2_queue_alloc(q
, create
->memory
, num_buffers
,
961 if (allocated_buffers
== 0) {
962 dprintk(1, "Memory allocation failed\n");
967 * Check if driver can handle the so far allocated number of buffers.
969 if (allocated_buffers
< num_buffers
) {
970 num_buffers
= allocated_buffers
;
973 * q->num_buffers contains the total number of buffers, that the
974 * queue driver has set up
976 ret
= call_qop(q
, queue_setup
, q
, &create
->format
, &num_buffers
,
977 &num_planes
, q
->plane_sizes
, q
->alloc_ctx
);
979 fail_qop(q
, queue_setup
);
981 if (!ret
&& allocated_buffers
< num_buffers
)
985 * Either the driver has accepted a smaller number of buffers,
986 * or .queue_setup() returned an error
990 q
->num_buffers
+= allocated_buffers
;
994 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
995 * from q->num_buffers.
997 __vb2_queue_free(q
, allocated_buffers
);
1002 * Return the number of successfully allocated buffers
1005 create
->count
= allocated_buffers
;
1011 * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
1012 * memory and type values.
1013 * @q: videobuf2 queue
1014 * @create: creation parameters, passed from userspace to vidioc_create_bufs
1017 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
1019 int ret
= __verify_memory_type(q
, create
->memory
, create
->format
.type
);
1021 create
->index
= q
->num_buffers
;
1022 if (create
->count
== 0)
1023 return ret
!= -EBUSY
? ret
: 0;
1024 return ret
? ret
: __create_bufs(q
, create
);
1026 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
1029 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
1030 * @vb: vb2_buffer to which the plane in question belongs to
1031 * @plane_no: plane number for which the address is to be returned
1033 * This function returns a kernel virtual address of a given plane if
1034 * such a mapping exist, NULL otherwise.
1036 void *vb2_plane_vaddr(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
, vaddr
, vb
->planes
[plane_no
].mem_priv
);
1044 EXPORT_SYMBOL_GPL(vb2_plane_vaddr
);
1047 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
1048 * @vb: vb2_buffer to which the plane in question belongs to
1049 * @plane_no: plane number for which the cookie is to be returned
1051 * This function returns an allocator specific cookie for a given plane if
1052 * available, NULL otherwise. The allocator should provide some simple static
1053 * inline function, which would convert this cookie to the allocator specific
1054 * type that can be used directly by the driver to access the buffer. This can
1055 * be for example physical address, pointer to scatter list or IOMMU mapping.
1057 void *vb2_plane_cookie(struct vb2_buffer
*vb
, unsigned int plane_no
)
1059 if (plane_no
> vb
->num_planes
|| !vb
->planes
[plane_no
].mem_priv
)
1062 return call_memop(vb
, cookie
, vb
->planes
[plane_no
].mem_priv
);
1064 EXPORT_SYMBOL_GPL(vb2_plane_cookie
);
1067 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1068 * @vb: vb2_buffer returned from the driver
1069 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1070 * or VB2_BUF_STATE_ERROR if the operation finished with an error.
1071 * If start_streaming fails then it should return buffers with state
1072 * VB2_BUF_STATE_QUEUED to put them back into the queue.
1074 * This function should be called by the driver after a hardware operation on
1075 * a buffer is finished and the buffer may be returned to userspace. The driver
1076 * cannot use this buffer anymore until it is queued back to it by videobuf
1077 * by the means of buf_queue callback. Only buffers previously queued to the
1078 * driver by buf_queue can be passed to this function.
1080 * While streaming a buffer can only be returned in state DONE or ERROR.
1081 * The start_streaming op can also return them in case the DMA engine cannot
1082 * be started for some reason. In that case the buffers should be returned with
1085 void vb2_buffer_done(struct vb2_buffer
*vb
, enum vb2_buffer_state state
)
1087 struct vb2_queue
*q
= vb
->vb2_queue
;
1088 unsigned long flags
;
1091 if (WARN_ON(vb
->state
!= VB2_BUF_STATE_ACTIVE
))
1094 if (!q
->start_streaming_called
) {
1095 if (WARN_ON(state
!= VB2_BUF_STATE_QUEUED
))
1096 state
= VB2_BUF_STATE_QUEUED
;
1097 } else if (!WARN_ON(!q
->start_streaming_called
)) {
1098 if (WARN_ON(state
!= VB2_BUF_STATE_DONE
&&
1099 state
!= VB2_BUF_STATE_ERROR
))
1100 state
= VB2_BUF_STATE_ERROR
;
1103 #ifdef CONFIG_VIDEO_ADV_DEBUG
1105 * Although this is not a callback, it still does have to balance
1106 * with the buf_queue op. So update this counter manually.
1110 dprintk(4, "Done processing on buffer %d, state: %d\n",
1111 vb
->v4l2_buf
.index
, state
);
1114 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1115 call_memop(vb
, finish
, vb
->planes
[plane
].mem_priv
);
1117 /* Add the buffer to the done buffers list */
1118 spin_lock_irqsave(&q
->done_lock
, flags
);
1120 if (state
!= VB2_BUF_STATE_QUEUED
)
1121 list_add_tail(&vb
->done_entry
, &q
->done_list
);
1122 atomic_dec(&q
->owned_by_drv_count
);
1123 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1125 if (state
== VB2_BUF_STATE_QUEUED
)
1128 /* Inform any processes that may be waiting for buffers */
1129 wake_up(&q
->done_wq
);
1131 EXPORT_SYMBOL_GPL(vb2_buffer_done
);
1134 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
1135 * v4l2_buffer by the userspace. The caller has already verified that struct
1136 * v4l2_buffer has a valid number of planes.
1138 static void __fill_vb2_buffer(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
,
1139 struct v4l2_plane
*v4l2_planes
)
1143 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
1144 /* Fill in driver-provided information for OUTPUT types */
1145 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1147 * Will have to go up to b->length when API starts
1148 * accepting variable number of planes.
1150 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1151 v4l2_planes
[plane
].bytesused
=
1152 b
->m
.planes
[plane
].bytesused
;
1153 v4l2_planes
[plane
].data_offset
=
1154 b
->m
.planes
[plane
].data_offset
;
1158 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
1159 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1160 v4l2_planes
[plane
].m
.userptr
=
1161 b
->m
.planes
[plane
].m
.userptr
;
1162 v4l2_planes
[plane
].length
=
1163 b
->m
.planes
[plane
].length
;
1166 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
1167 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1168 v4l2_planes
[plane
].m
.fd
=
1169 b
->m
.planes
[plane
].m
.fd
;
1170 v4l2_planes
[plane
].length
=
1171 b
->m
.planes
[plane
].length
;
1172 v4l2_planes
[plane
].data_offset
=
1173 b
->m
.planes
[plane
].data_offset
;
1178 * Single-planar buffers do not use planes array,
1179 * so fill in relevant v4l2_buffer struct fields instead.
1180 * In videobuf we use our internal V4l2_planes struct for
1181 * single-planar buffers as well, for simplicity.
1183 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1184 v4l2_planes
[0].bytesused
= b
->bytesused
;
1185 v4l2_planes
[0].data_offset
= 0;
1188 if (b
->memory
== V4L2_MEMORY_USERPTR
) {
1189 v4l2_planes
[0].m
.userptr
= b
->m
.userptr
;
1190 v4l2_planes
[0].length
= b
->length
;
1193 if (b
->memory
== V4L2_MEMORY_DMABUF
) {
1194 v4l2_planes
[0].m
.fd
= b
->m
.fd
;
1195 v4l2_planes
[0].length
= b
->length
;
1196 v4l2_planes
[0].data_offset
= 0;
1201 /* Zero flags that the vb2 core handles */
1202 vb
->v4l2_buf
.flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
1203 if ((vb
->vb2_queue
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) !=
1204 V4L2_BUF_FLAG_TIMESTAMP_COPY
|| !V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1206 * Non-COPY timestamps and non-OUTPUT queues will get
1207 * their timestamp and timestamp source flags from the
1210 vb
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
1213 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
1215 * For output buffers mask out the timecode flag:
1216 * this will be handled later in vb2_internal_qbuf().
1217 * The 'field' is valid metadata for this output buffer
1218 * and so that needs to be copied here.
1220 vb
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_TIMECODE
;
1221 vb
->v4l2_buf
.field
= b
->field
;
1223 /* Zero any output buffer flags as this is a capture buffer */
1224 vb
->v4l2_buf
.flags
&= ~V4L2_BUFFER_OUT_FLAGS
;
1229 * __qbuf_userptr() - handle qbuf of a USERPTR buffer
1231 static int __qbuf_userptr(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1233 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
1234 struct vb2_queue
*q
= vb
->vb2_queue
;
1238 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
1239 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1241 /* Copy relevant information provided by the userspace */
1242 __fill_vb2_buffer(vb
, b
, planes
);
1244 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1245 /* Skip the plane if already verified */
1246 if (vb
->v4l2_planes
[plane
].m
.userptr
&&
1247 vb
->v4l2_planes
[plane
].m
.userptr
== planes
[plane
].m
.userptr
1248 && vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
)
1251 dprintk(3, "qbuf: userspace address for plane %d changed, "
1252 "reacquiring memory\n", plane
);
1254 /* Check if the provided plane buffer is large enough */
1255 if (planes
[plane
].length
< q
->plane_sizes
[plane
]) {
1256 dprintk(1, "qbuf: provided buffer size %u is less than "
1257 "setup size %u for plane %d\n",
1258 planes
[plane
].length
,
1259 q
->plane_sizes
[plane
], plane
);
1264 /* Release previously acquired memory if present */
1265 if (vb
->planes
[plane
].mem_priv
) {
1268 call_vb_qop(vb
, buf_cleanup
, vb
);
1270 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1273 vb
->planes
[plane
].mem_priv
= NULL
;
1274 memset(&vb
->v4l2_planes
[plane
], 0, sizeof(struct v4l2_plane
));
1276 /* Acquire each plane's memory */
1277 mem_priv
= call_memop(vb
, get_userptr
, q
->alloc_ctx
[plane
],
1278 planes
[plane
].m
.userptr
,
1279 planes
[plane
].length
, write
);
1280 if (IS_ERR_OR_NULL(mem_priv
)) {
1281 dprintk(1, "qbuf: failed acquiring userspace "
1282 "memory for plane %d\n", plane
);
1283 fail_memop(vb
, get_userptr
);
1284 ret
= mem_priv
? PTR_ERR(mem_priv
) : -EINVAL
;
1287 vb
->planes
[plane
].mem_priv
= mem_priv
;
1291 * Now that everything is in order, copy relevant information
1292 * provided by userspace.
1294 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1295 vb
->v4l2_planes
[plane
] = planes
[plane
];
1299 * One or more planes changed, so we must call buf_init to do
1300 * the driver-specific initialization on the newly acquired
1301 * buffer, if provided.
1303 ret
= call_vb_qop(vb
, buf_init
, vb
);
1305 dprintk(1, "qbuf: buffer initialization failed\n");
1306 fail_vb_qop(vb
, buf_init
);
1311 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1313 dprintk(1, "qbuf: buffer preparation failed\n");
1314 fail_vb_qop(vb
, buf_prepare
);
1315 call_vb_qop(vb
, buf_cleanup
, vb
);
1321 /* In case of errors, release planes that were already acquired */
1322 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1323 if (vb
->planes
[plane
].mem_priv
)
1324 call_memop(vb
, put_userptr
, vb
->planes
[plane
].mem_priv
);
1325 vb
->planes
[plane
].mem_priv
= NULL
;
1326 vb
->v4l2_planes
[plane
].m
.userptr
= 0;
1327 vb
->v4l2_planes
[plane
].length
= 0;
1334 * __qbuf_mmap() - handle qbuf of an MMAP buffer
1336 static int __qbuf_mmap(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1340 __fill_vb2_buffer(vb
, b
, vb
->v4l2_planes
);
1341 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1343 fail_vb_qop(vb
, buf_prepare
);
1348 * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
1350 static int __qbuf_dmabuf(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1352 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
1353 struct vb2_queue
*q
= vb
->vb2_queue
;
1357 int write
= !V4L2_TYPE_IS_OUTPUT(q
->type
);
1358 bool reacquired
= vb
->planes
[0].mem_priv
== NULL
;
1360 /* Copy relevant information provided by the userspace */
1361 __fill_vb2_buffer(vb
, b
, planes
);
1363 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1364 struct dma_buf
*dbuf
= dma_buf_get(planes
[plane
].m
.fd
);
1366 if (IS_ERR_OR_NULL(dbuf
)) {
1367 dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
1373 /* use DMABUF size if length is not provided */
1374 if (planes
[plane
].length
== 0)
1375 planes
[plane
].length
= dbuf
->size
;
1377 if (planes
[plane
].length
< planes
[plane
].data_offset
+
1378 q
->plane_sizes
[plane
]) {
1379 dprintk(1, "qbuf: invalid dmabuf length for plane %d\n",
1385 /* Skip the plane if already verified */
1386 if (dbuf
== vb
->planes
[plane
].dbuf
&&
1387 vb
->v4l2_planes
[plane
].length
== planes
[plane
].length
) {
1392 dprintk(1, "qbuf: buffer for plane %d changed\n", plane
);
1396 call_vb_qop(vb
, buf_cleanup
, vb
);
1399 /* Release previously acquired memory if present */
1400 __vb2_plane_dmabuf_put(vb
, &vb
->planes
[plane
]);
1401 memset(&vb
->v4l2_planes
[plane
], 0, sizeof(struct v4l2_plane
));
1403 /* Acquire each plane's memory */
1404 mem_priv
= call_memop(vb
, attach_dmabuf
, q
->alloc_ctx
[plane
],
1405 dbuf
, planes
[plane
].length
, write
);
1406 if (IS_ERR(mem_priv
)) {
1407 dprintk(1, "qbuf: failed to attach dmabuf\n");
1408 fail_memop(vb
, attach_dmabuf
);
1409 ret
= PTR_ERR(mem_priv
);
1414 vb
->planes
[plane
].dbuf
= dbuf
;
1415 vb
->planes
[plane
].mem_priv
= mem_priv
;
1418 /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
1419 * really we want to do this just before the DMA, not while queueing
1422 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
1423 ret
= call_memop(vb
, map_dmabuf
, vb
->planes
[plane
].mem_priv
);
1425 dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
1427 fail_memop(vb
, map_dmabuf
);
1430 vb
->planes
[plane
].dbuf_mapped
= 1;
1434 * Now that everything is in order, copy relevant information
1435 * provided by userspace.
1437 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1438 vb
->v4l2_planes
[plane
] = planes
[plane
];
1442 * Call driver-specific initialization on the newly acquired buffer,
1445 ret
= call_vb_qop(vb
, buf_init
, vb
);
1447 dprintk(1, "qbuf: buffer initialization failed\n");
1448 fail_vb_qop(vb
, buf_init
);
1453 ret
= call_vb_qop(vb
, buf_prepare
, vb
);
1455 dprintk(1, "qbuf: buffer preparation failed\n");
1456 fail_vb_qop(vb
, buf_prepare
);
1457 call_vb_qop(vb
, buf_cleanup
, vb
);
1463 /* In case of errors, release planes that were already acquired */
1464 __vb2_buf_dmabuf_put(vb
);
1470 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
1472 static void __enqueue_in_driver(struct vb2_buffer
*vb
)
1474 struct vb2_queue
*q
= vb
->vb2_queue
;
1477 vb
->state
= VB2_BUF_STATE_ACTIVE
;
1478 atomic_inc(&q
->owned_by_drv_count
);
1481 for (plane
= 0; plane
< vb
->num_planes
; ++plane
)
1482 call_memop(vb
, prepare
, vb
->planes
[plane
].mem_priv
);
1484 call_vb_qop(vb
, buf_queue
, vb
);
1487 static int __buf_prepare(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
1489 struct vb2_queue
*q
= vb
->vb2_queue
;
1490 struct rw_semaphore
*mmap_sem
;
1493 ret
= __verify_length(vb
, b
);
1495 dprintk(1, "%s(): plane parameters verification failed: %d\n",
1500 vb
->state
= VB2_BUF_STATE_PREPARING
;
1501 vb
->v4l2_buf
.timestamp
.tv_sec
= 0;
1502 vb
->v4l2_buf
.timestamp
.tv_usec
= 0;
1503 vb
->v4l2_buf
.sequence
= 0;
1505 switch (q
->memory
) {
1506 case V4L2_MEMORY_MMAP
:
1507 ret
= __qbuf_mmap(vb
, b
);
1509 case V4L2_MEMORY_USERPTR
:
1511 * In case of user pointer buffers vb2 allocators need to get
1512 * direct access to userspace pages. This requires getting
1513 * the mmap semaphore for read access in the current process
1514 * structure. The same semaphore is taken before calling mmap
1515 * operation, while both qbuf/prepare_buf and mmap are called
1516 * by the driver or v4l2 core with the driver's lock held.
1517 * To avoid an AB-BA deadlock (mmap_sem then driver's lock in
1518 * mmap and driver's lock then mmap_sem in qbuf/prepare_buf),
1519 * the videobuf2 core releases the driver's lock, takes
1520 * mmap_sem and then takes the driver's lock again.
1522 mmap_sem
= ¤t
->mm
->mmap_sem
;
1523 call_qop(q
, wait_prepare
, q
);
1524 down_read(mmap_sem
);
1525 call_qop(q
, wait_finish
, q
);
1527 ret
= __qbuf_userptr(vb
, b
);
1531 case V4L2_MEMORY_DMABUF
:
1532 ret
= __qbuf_dmabuf(vb
, b
);
1535 WARN(1, "Invalid queue type\n");
1540 dprintk(1, "qbuf: buffer preparation failed: %d\n", ret
);
1541 vb
->state
= ret
? VB2_BUF_STATE_DEQUEUED
: VB2_BUF_STATE_PREPARED
;
1546 static int vb2_queue_or_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
,
1549 if (b
->type
!= q
->type
) {
1550 dprintk(1, "%s(): invalid buffer type\n", opname
);
1554 if (b
->index
>= q
->num_buffers
) {
1555 dprintk(1, "%s(): buffer index out of range\n", opname
);
1559 if (q
->bufs
[b
->index
] == NULL
) {
1560 /* Should never happen */
1561 dprintk(1, "%s(): buffer is NULL\n", opname
);
1565 if (b
->memory
!= q
->memory
) {
1566 dprintk(1, "%s(): invalid memory type\n", opname
);
1570 return __verify_planes_array(q
->bufs
[b
->index
], b
);
1574 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
1575 * @q: videobuf2 queue
1576 * @b: buffer structure passed from userspace to vidioc_prepare_buf
1579 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
1581 * 1) verifies the passed buffer,
1582 * 2) calls buf_prepare callback in the driver (if provided), in which
1583 * driver-specific buffer initialization can be performed,
1585 * The return values from this function are intended to be directly returned
1586 * from vidioc_prepare_buf handler in driver.
1588 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1590 struct vb2_buffer
*vb
;
1594 dprintk(1, "%s(): file io in progress\n", __func__
);
1598 ret
= vb2_queue_or_prepare_buf(q
, b
, "prepare_buf");
1602 vb
= q
->bufs
[b
->index
];
1603 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
1604 dprintk(1, "%s(): invalid buffer state %d\n", __func__
,
1609 ret
= __buf_prepare(vb
, b
);
1611 /* Fill buffer information for the userspace */
1612 __fill_v4l2_buffer(vb
, b
);
1614 dprintk(1, "%s() of buffer %d succeeded\n", __func__
, vb
->v4l2_buf
.index
);
1618 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
1621 * vb2_start_streaming() - Attempt to start streaming.
1622 * @q: videobuf2 queue
1624 * Attempt to start streaming. When this function is called there must be
1625 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1626 * number of buffers required for the DMA engine to function). If the
1627 * @start_streaming op fails it is supposed to return all the driver-owned
1628 * buffers back to vb2 in state QUEUED. Check if that happened and if
1629 * not warn and reclaim them forcefully.
1631 static int vb2_start_streaming(struct vb2_queue
*q
)
1633 struct vb2_buffer
*vb
;
1637 * If any buffers were queued before streamon,
1638 * we can now pass them to driver for processing.
1640 list_for_each_entry(vb
, &q
->queued_list
, queued_entry
)
1641 __enqueue_in_driver(vb
);
1643 /* Tell the driver to start streaming */
1644 ret
= call_qop(q
, start_streaming
, q
,
1645 atomic_read(&q
->owned_by_drv_count
));
1646 q
->start_streaming_called
= ret
== 0;
1650 fail_qop(q
, start_streaming
);
1651 dprintk(1, "qbuf: driver refused to start streaming\n");
1652 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
1656 * Forcefully reclaim buffers if the driver did not
1657 * correctly return them to vb2.
1659 for (i
= 0; i
< q
->num_buffers
; ++i
) {
1661 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1662 vb2_buffer_done(vb
, VB2_BUF_STATE_QUEUED
);
1664 /* Must be zero now */
1665 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
1670 static int vb2_internal_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1672 int ret
= vb2_queue_or_prepare_buf(q
, b
, "qbuf");
1673 struct vb2_buffer
*vb
;
1678 vb
= q
->bufs
[b
->index
];
1680 switch (vb
->state
) {
1681 case VB2_BUF_STATE_DEQUEUED
:
1682 ret
= __buf_prepare(vb
, b
);
1686 case VB2_BUF_STATE_PREPARED
:
1688 case VB2_BUF_STATE_PREPARING
:
1689 dprintk(1, "qbuf: buffer still being prepared\n");
1692 dprintk(1, "%s(): invalid buffer state %d\n", __func__
,
1698 * Add to the queued buffers list, a buffer will stay on it until
1699 * dequeued in dqbuf.
1701 list_add_tail(&vb
->queued_entry
, &q
->queued_list
);
1703 vb
->state
= VB2_BUF_STATE_QUEUED
;
1704 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
1706 * For output buffers copy the timestamp if needed,
1707 * and the timecode field and flag if needed.
1709 if ((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
1710 V4L2_BUF_FLAG_TIMESTAMP_COPY
)
1711 vb
->v4l2_buf
.timestamp
= b
->timestamp
;
1712 vb
->v4l2_buf
.flags
|= b
->flags
& V4L2_BUF_FLAG_TIMECODE
;
1713 if (b
->flags
& V4L2_BUF_FLAG_TIMECODE
)
1714 vb
->v4l2_buf
.timecode
= b
->timecode
;
1718 * If already streaming, give the buffer to driver for processing.
1719 * If not, the buffer will be given to driver on next streamon.
1721 if (q
->start_streaming_called
)
1722 __enqueue_in_driver(vb
);
1724 /* Fill buffer information for the userspace */
1725 __fill_v4l2_buffer(vb
, b
);
1728 * If streamon has been called, and we haven't yet called
1729 * start_streaming() since not enough buffers were queued, and
1730 * we now have reached the minimum number of queued buffers,
1731 * then we can finally call start_streaming().
1733 if (q
->streaming
&& !q
->start_streaming_called
&&
1734 q
->queued_count
>= q
->min_buffers_needed
) {
1735 ret
= vb2_start_streaming(q
);
1740 dprintk(1, "%s() of buffer %d succeeded\n", __func__
, vb
->v4l2_buf
.index
);
1745 * vb2_qbuf() - Queue a buffer from userspace
1746 * @q: videobuf2 queue
1747 * @b: buffer structure passed from userspace to vidioc_qbuf handler
1750 * Should be called from vidioc_qbuf ioctl handler of a driver.
1752 * 1) verifies the passed buffer,
1753 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
1754 * which driver-specific buffer initialization can be performed,
1755 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
1756 * callback for processing.
1758 * The return values from this function are intended to be directly returned
1759 * from vidioc_qbuf handler in driver.
1761 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
1764 dprintk(1, "%s(): file io in progress\n", __func__
);
1768 return vb2_internal_qbuf(q
, b
);
1770 EXPORT_SYMBOL_GPL(vb2_qbuf
);
1773 * __vb2_wait_for_done_vb() - wait for a buffer to become available
1776 * Will sleep if required for nonblocking == false.
1778 static int __vb2_wait_for_done_vb(struct vb2_queue
*q
, int nonblocking
)
1781 * All operations on vb_done_list are performed under done_lock
1782 * spinlock protection. However, buffers may be removed from
1783 * it and returned to userspace only while holding both driver's
1784 * lock and the done_lock spinlock. Thus we can be sure that as
1785 * long as we hold the driver's lock, the list will remain not
1786 * empty if list_empty() check succeeds.
1792 if (!q
->streaming
) {
1793 dprintk(1, "Streaming off, will not wait for buffers\n");
1797 if (!list_empty(&q
->done_list
)) {
1799 * Found a buffer that we were waiting for.
1805 dprintk(1, "Nonblocking and no buffers to dequeue, "
1811 * We are streaming and blocking, wait for another buffer to
1812 * become ready or for streamoff. Driver's lock is released to
1813 * allow streamoff or qbuf to be called while waiting.
1815 call_qop(q
, wait_prepare
, q
);
1818 * All locks have been released, it is safe to sleep now.
1820 dprintk(3, "Will sleep waiting for buffers\n");
1821 ret
= wait_event_interruptible(q
->done_wq
,
1822 !list_empty(&q
->done_list
) || !q
->streaming
);
1825 * We need to reevaluate both conditions again after reacquiring
1826 * the locks or return an error if one occurred.
1828 call_qop(q
, wait_finish
, q
);
1830 dprintk(1, "Sleep was interrupted\n");
1838 * __vb2_get_done_vb() - get a buffer ready for dequeuing
1840 * Will sleep if required for nonblocking == false.
1842 static int __vb2_get_done_vb(struct vb2_queue
*q
, struct vb2_buffer
**vb
,
1843 struct v4l2_buffer
*b
, int nonblocking
)
1845 unsigned long flags
;
1849 * Wait for at least one buffer to become available on the done_list.
1851 ret
= __vb2_wait_for_done_vb(q
, nonblocking
);
1856 * Driver's lock has been held since we last verified that done_list
1857 * is not empty, so no need for another list_empty(done_list) check.
1859 spin_lock_irqsave(&q
->done_lock
, flags
);
1860 *vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
, done_entry
);
1862 * Only remove the buffer from done_list if v4l2_buffer can handle all
1865 ret
= __verify_planes_array(*vb
, b
);
1867 list_del(&(*vb
)->done_entry
);
1868 spin_unlock_irqrestore(&q
->done_lock
, flags
);
1874 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
1875 * @q: videobuf2 queue
1877 * This function will wait until all buffers that have been given to the driver
1878 * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
1879 * wait_prepare, wait_finish pair. It is intended to be called with all locks
1880 * taken, for example from stop_streaming() callback.
1882 int vb2_wait_for_all_buffers(struct vb2_queue
*q
)
1884 if (!q
->streaming
) {
1885 dprintk(1, "Streaming off, will not wait for buffers\n");
1889 if (q
->start_streaming_called
)
1890 wait_event(q
->done_wq
, !atomic_read(&q
->owned_by_drv_count
));
1893 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers
);
1896 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
1898 static void __vb2_dqbuf(struct vb2_buffer
*vb
)
1900 struct vb2_queue
*q
= vb
->vb2_queue
;
1903 /* nothing to do if the buffer is already dequeued */
1904 if (vb
->state
== VB2_BUF_STATE_DEQUEUED
)
1907 vb
->state
= VB2_BUF_STATE_DEQUEUED
;
1909 /* unmap DMABUF buffer */
1910 if (q
->memory
== V4L2_MEMORY_DMABUF
)
1911 for (i
= 0; i
< vb
->num_planes
; ++i
) {
1912 if (!vb
->planes
[i
].dbuf_mapped
)
1914 call_memop(vb
, unmap_dmabuf
, vb
->planes
[i
].mem_priv
);
1915 vb
->planes
[i
].dbuf_mapped
= 0;
1919 static int vb2_internal_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1921 struct vb2_buffer
*vb
= NULL
;
1924 if (b
->type
!= q
->type
) {
1925 dprintk(1, "dqbuf: invalid buffer type\n");
1928 ret
= __vb2_get_done_vb(q
, &vb
, b
, nonblocking
);
1932 switch (vb
->state
) {
1933 case VB2_BUF_STATE_DONE
:
1934 dprintk(3, "dqbuf: Returning done buffer\n");
1936 case VB2_BUF_STATE_ERROR
:
1937 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1940 dprintk(1, "dqbuf: Invalid buffer state\n");
1944 call_vb_qop(vb
, buf_finish
, vb
);
1946 /* Fill buffer information for the userspace */
1947 __fill_v4l2_buffer(vb
, b
);
1948 /* Remove from videobuf queue */
1949 list_del(&vb
->queued_entry
);
1951 /* go back to dequeued state */
1954 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1955 vb
->v4l2_buf
.index
, vb
->state
);
1961 * vb2_dqbuf() - Dequeue a buffer to the userspace
1962 * @q: videobuf2 queue
1963 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
1965 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
1966 * buffers ready for dequeuing are present. Normally the driver
1967 * would be passing (file->f_flags & O_NONBLOCK) here
1969 * Should be called from vidioc_dqbuf ioctl handler of a driver.
1971 * 1) verifies the passed buffer,
1972 * 2) calls buf_finish callback in the driver (if provided), in which
1973 * driver can perform any additional operations that may be required before
1974 * returning the buffer to userspace, such as cache sync,
1975 * 3) the buffer struct members are filled with relevant information for
1978 * The return values from this function are intended to be directly returned
1979 * from vidioc_dqbuf handler in driver.
1981 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
1984 dprintk(1, "dqbuf: file io in progress\n");
1987 return vb2_internal_dqbuf(q
, b
, nonblocking
);
1989 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
1992 * __vb2_queue_cancel() - cancel and stop (pause) streaming
1994 * Removes all queued buffers from driver's queue and all buffers queued by
1995 * userspace from videobuf's queue. Returns to state after reqbufs.
1997 static void __vb2_queue_cancel(struct vb2_queue
*q
)
2002 * Tell driver to stop all transactions and release all queued
2005 if (q
->start_streaming_called
)
2006 call_qop(q
, stop_streaming
, q
);
2008 q
->start_streaming_called
= 0;
2009 q
->queued_count
= 0;
2011 if (WARN_ON(atomic_read(&q
->owned_by_drv_count
))) {
2012 for (i
= 0; i
< q
->num_buffers
; ++i
)
2013 if (q
->bufs
[i
]->state
== VB2_BUF_STATE_ACTIVE
)
2014 vb2_buffer_done(q
->bufs
[i
], VB2_BUF_STATE_ERROR
);
2015 /* Must be zero now */
2016 WARN_ON(atomic_read(&q
->owned_by_drv_count
));
2020 * Remove all buffers from videobuf's list...
2022 INIT_LIST_HEAD(&q
->queued_list
);
2024 * ...and done list; userspace will not receive any buffers it
2025 * has not already dequeued before initiating cancel.
2027 INIT_LIST_HEAD(&q
->done_list
);
2028 atomic_set(&q
->owned_by_drv_count
, 0);
2029 wake_up_all(&q
->done_wq
);
2032 * Reinitialize all buffers for next use.
2033 * Make sure to call buf_finish for any queued buffers. Normally
2034 * that's done in dqbuf, but that's not going to happen when we
2035 * cancel the whole queue. Note: this code belongs here, not in
2036 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
2037 * call to __fill_v4l2_buffer() after buf_finish(). That order can't
2038 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
2040 for (i
= 0; i
< q
->num_buffers
; ++i
) {
2041 struct vb2_buffer
*vb
= q
->bufs
[i
];
2043 if (vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
2044 vb
->state
= VB2_BUF_STATE_PREPARED
;
2045 call_vb_qop(vb
, buf_finish
, vb
);
2051 static int vb2_internal_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2055 if (type
!= q
->type
) {
2056 dprintk(1, "streamon: invalid stream type\n");
2061 dprintk(3, "streamon successful: already streaming\n");
2065 if (!q
->num_buffers
) {
2066 dprintk(1, "streamon: no buffers have been allocated\n");
2070 if (!q
->num_buffers
) {
2071 dprintk(1, "streamon: no buffers have been allocated\n");
2074 if (q
->num_buffers
< q
->min_buffers_needed
) {
2075 dprintk(1, "streamon: need at least %u allocated buffers\n",
2076 q
->min_buffers_needed
);
2081 * Tell driver to start streaming provided sufficient buffers
2084 if (q
->queued_count
>= q
->min_buffers_needed
) {
2085 ret
= vb2_start_streaming(q
);
2087 __vb2_queue_cancel(q
);
2094 dprintk(3, "Streamon successful\n");
2099 * vb2_streamon - start streaming
2100 * @q: videobuf2 queue
2101 * @type: type argument passed from userspace to vidioc_streamon handler
2103 * Should be called from vidioc_streamon handler of a driver.
2105 * 1) verifies current state
2106 * 2) passes any previously queued buffers to the driver and starts streaming
2108 * The return values from this function are intended to be directly returned
2109 * from vidioc_streamon handler in the driver.
2111 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2114 dprintk(1, "streamon: file io in progress\n");
2117 return vb2_internal_streamon(q
, type
);
2119 EXPORT_SYMBOL_GPL(vb2_streamon
);
2121 static int vb2_internal_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2123 if (type
!= q
->type
) {
2124 dprintk(1, "streamoff: invalid stream type\n");
2129 * Cancel will pause streaming and remove all buffers from the driver
2130 * and videobuf, effectively returning control over them to userspace.
2132 * Note that we do this even if q->streaming == 0: if you prepare or
2133 * queue buffers, and then call streamoff without ever having called
2134 * streamon, you would still expect those buffers to be returned to
2135 * their normal dequeued state.
2137 __vb2_queue_cancel(q
);
2139 dprintk(3, "Streamoff successful\n");
2144 * vb2_streamoff - stop streaming
2145 * @q: videobuf2 queue
2146 * @type: type argument passed from userspace to vidioc_streamoff handler
2148 * Should be called from vidioc_streamoff handler of a driver.
2150 * 1) verifies current state,
2151 * 2) stop streaming and dequeues any queued buffers, including those previously
2152 * passed to the driver (after waiting for the driver to finish).
2154 * This call can be used for pausing playback.
2155 * The return values from this function are intended to be directly returned
2156 * from vidioc_streamoff handler in the driver
2158 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
2161 dprintk(1, "streamoff: file io in progress\n");
2164 return vb2_internal_streamoff(q
, type
);
2166 EXPORT_SYMBOL_GPL(vb2_streamoff
);
2169 * __find_plane_by_offset() - find plane associated with the given offset off
2171 static int __find_plane_by_offset(struct vb2_queue
*q
, unsigned long off
,
2172 unsigned int *_buffer
, unsigned int *_plane
)
2174 struct vb2_buffer
*vb
;
2175 unsigned int buffer
, plane
;
2178 * Go over all buffers and their planes, comparing the given offset
2179 * with an offset assigned to each plane. If a match is found,
2180 * return its buffer and plane numbers.
2182 for (buffer
= 0; buffer
< q
->num_buffers
; ++buffer
) {
2183 vb
= q
->bufs
[buffer
];
2185 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
2186 if (vb
->v4l2_planes
[plane
].m
.mem_offset
== off
) {
2198 * vb2_expbuf() - Export a buffer as a file descriptor
2199 * @q: videobuf2 queue
2200 * @eb: export buffer structure passed from userspace to vidioc_expbuf
2203 * The return values from this function are intended to be directly returned
2204 * from vidioc_expbuf handler in driver.
2206 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
2208 struct vb2_buffer
*vb
= NULL
;
2209 struct vb2_plane
*vb_plane
;
2211 struct dma_buf
*dbuf
;
2213 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2214 dprintk(1, "Queue is not currently set up for mmap\n");
2218 if (!q
->mem_ops
->get_dmabuf
) {
2219 dprintk(1, "Queue does not support DMA buffer exporting\n");
2223 if (eb
->flags
& ~(O_CLOEXEC
| O_ACCMODE
)) {
2224 dprintk(1, "Queue does support only O_CLOEXEC and access mode flags\n");
2228 if (eb
->type
!= q
->type
) {
2229 dprintk(1, "qbuf: invalid buffer type\n");
2233 if (eb
->index
>= q
->num_buffers
) {
2234 dprintk(1, "buffer index out of range\n");
2238 vb
= q
->bufs
[eb
->index
];
2240 if (eb
->plane
>= vb
->num_planes
) {
2241 dprintk(1, "buffer plane out of range\n");
2245 vb_plane
= &vb
->planes
[eb
->plane
];
2247 dbuf
= call_memop(vb
, get_dmabuf
, vb_plane
->mem_priv
, eb
->flags
& O_ACCMODE
);
2248 if (IS_ERR_OR_NULL(dbuf
)) {
2249 dprintk(1, "Failed to export buffer %d, plane %d\n",
2250 eb
->index
, eb
->plane
);
2251 fail_memop(vb
, get_dmabuf
);
2255 ret
= dma_buf_fd(dbuf
, eb
->flags
& ~O_ACCMODE
);
2257 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2258 eb
->index
, eb
->plane
, ret
);
2263 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2264 eb
->index
, eb
->plane
, ret
);
2269 EXPORT_SYMBOL_GPL(vb2_expbuf
);
2272 * vb2_mmap() - map video buffers into application address space
2273 * @q: videobuf2 queue
2274 * @vma: vma passed to the mmap file operation handler in the driver
2276 * Should be called from mmap file operation handler of a driver.
2277 * This function maps one plane of one of the available video buffers to
2278 * userspace. To map whole video memory allocated on reqbufs, this function
2279 * has to be called once per each plane per each buffer previously allocated.
2281 * When the userspace application calls mmap, it passes to it an offset returned
2282 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
2283 * a "cookie", which is then used to identify the plane to be mapped.
2284 * This function finds a plane with a matching offset and a mapping is performed
2285 * by the means of a provided memory operation.
2287 * The return values from this function are intended to be directly returned
2288 * from the mmap handler in driver.
2290 int vb2_mmap(struct vb2_queue
*q
, struct vm_area_struct
*vma
)
2292 unsigned long off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2293 struct vb2_buffer
*vb
;
2294 unsigned int buffer
, plane
;
2296 unsigned long length
;
2298 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2299 dprintk(1, "Queue is not currently set up for mmap\n");
2304 * Check memory area access mode.
2306 if (!(vma
->vm_flags
& VM_SHARED
)) {
2307 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
2310 if (V4L2_TYPE_IS_OUTPUT(q
->type
)) {
2311 if (!(vma
->vm_flags
& VM_WRITE
)) {
2312 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
2316 if (!(vma
->vm_flags
& VM_READ
)) {
2317 dprintk(1, "Invalid vma flags, VM_READ needed\n");
2323 * Find the plane corresponding to the offset passed by userspace.
2325 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2329 vb
= q
->bufs
[buffer
];
2332 * MMAP requires page_aligned buffers.
2333 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
2334 * so, we need to do the same here.
2336 length
= PAGE_ALIGN(vb
->v4l2_planes
[plane
].length
);
2337 if (length
< (vma
->vm_end
- vma
->vm_start
)) {
2339 "MMAP invalid, as it would overflow buffer length\n");
2343 ret
= call_memop(vb
, mmap
, vb
->planes
[plane
].mem_priv
, vma
);
2345 fail_memop(vb
, mmap
);
2349 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer
, plane
);
2352 EXPORT_SYMBOL_GPL(vb2_mmap
);
2355 unsigned long vb2_get_unmapped_area(struct vb2_queue
*q
,
2358 unsigned long pgoff
,
2359 unsigned long flags
)
2361 unsigned long off
= pgoff
<< PAGE_SHIFT
;
2362 struct vb2_buffer
*vb
;
2363 unsigned int buffer
, plane
;
2366 if (q
->memory
!= V4L2_MEMORY_MMAP
) {
2367 dprintk(1, "Queue is not currently set up for mmap\n");
2372 * Find the plane corresponding to the offset passed by userspace.
2374 ret
= __find_plane_by_offset(q
, off
, &buffer
, &plane
);
2378 vb
= q
->bufs
[buffer
];
2380 return (unsigned long)vb2_plane_vaddr(vb
, plane
);
2382 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area
);
2385 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
);
2386 static int __vb2_cleanup_fileio(struct vb2_queue
*q
);
2389 * vb2_poll() - implements poll userspace operation
2390 * @q: videobuf2 queue
2391 * @file: file argument passed to the poll file operation handler
2392 * @wait: wait argument passed to the poll file operation handler
2394 * This function implements poll file operation handler for a driver.
2395 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
2396 * be informed that the file descriptor of a video device is available for
2398 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
2399 * will be reported as available for writing.
2401 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
2404 * The return values from this function are intended to be directly returned
2405 * from poll handler in driver.
2407 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
2409 struct video_device
*vfd
= video_devdata(file
);
2410 unsigned long req_events
= poll_requested_events(wait
);
2411 struct vb2_buffer
*vb
= NULL
;
2412 unsigned int res
= 0;
2413 unsigned long flags
;
2415 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
2416 struct v4l2_fh
*fh
= file
->private_data
;
2418 if (v4l2_event_pending(fh
))
2420 else if (req_events
& POLLPRI
)
2421 poll_wait(file
, &fh
->wait
, wait
);
2424 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && !(req_events
& (POLLIN
| POLLRDNORM
)))
2426 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && !(req_events
& (POLLOUT
| POLLWRNORM
)))
2430 * Start file I/O emulator only if streaming API has not been used yet.
2432 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
2433 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
2434 (req_events
& (POLLIN
| POLLRDNORM
))) {
2435 if (__vb2_init_fileio(q
, 1))
2436 return res
| POLLERR
;
2438 if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
2439 (req_events
& (POLLOUT
| POLLWRNORM
))) {
2440 if (__vb2_init_fileio(q
, 0))
2441 return res
| POLLERR
;
2443 * Write to OUTPUT queue can be done immediately.
2445 return res
| POLLOUT
| POLLWRNORM
;
2450 * There is nothing to wait for if no buffers have already been queued.
2452 if (list_empty(&q
->queued_list
))
2453 return res
| POLLERR
;
2455 if (list_empty(&q
->done_list
))
2456 poll_wait(file
, &q
->done_wq
, wait
);
2459 * Take first buffer available for dequeuing.
2461 spin_lock_irqsave(&q
->done_lock
, flags
);
2462 if (!list_empty(&q
->done_list
))
2463 vb
= list_first_entry(&q
->done_list
, struct vb2_buffer
,
2465 spin_unlock_irqrestore(&q
->done_lock
, flags
);
2467 if (vb
&& (vb
->state
== VB2_BUF_STATE_DONE
2468 || vb
->state
== VB2_BUF_STATE_ERROR
)) {
2469 return (V4L2_TYPE_IS_OUTPUT(q
->type
)) ?
2470 res
| POLLOUT
| POLLWRNORM
:
2471 res
| POLLIN
| POLLRDNORM
;
2475 EXPORT_SYMBOL_GPL(vb2_poll
);
2478 * vb2_queue_init() - initialize a videobuf2 queue
2479 * @q: videobuf2 queue; this structure should be allocated in driver
2481 * The vb2_queue structure should be allocated by the driver. The driver is
2482 * responsible of clearing it's content and setting initial values for some
2483 * required entries before calling this function.
2484 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
2485 * to the struct vb2_queue description in include/media/videobuf2-core.h
2486 * for more information.
2488 int vb2_queue_init(struct vb2_queue
*q
)
2495 WARN_ON(!q
->mem_ops
) ||
2496 WARN_ON(!q
->type
) ||
2497 WARN_ON(!q
->io_modes
) ||
2498 WARN_ON(!q
->ops
->queue_setup
) ||
2499 WARN_ON(!q
->ops
->buf_queue
) ||
2500 WARN_ON(q
->timestamp_flags
&
2501 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK
|
2502 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
)))
2505 /* Warn that the driver should choose an appropriate timestamp type */
2506 WARN_ON((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
2507 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN
);
2509 INIT_LIST_HEAD(&q
->queued_list
);
2510 INIT_LIST_HEAD(&q
->done_list
);
2511 spin_lock_init(&q
->done_lock
);
2512 init_waitqueue_head(&q
->done_wq
);
2514 if (q
->buf_struct_size
== 0)
2515 q
->buf_struct_size
= sizeof(struct vb2_buffer
);
2519 EXPORT_SYMBOL_GPL(vb2_queue_init
);
2522 * vb2_queue_release() - stop streaming, release the queue and free memory
2523 * @q: videobuf2 queue
2525 * This function stops streaming and performs necessary clean ups, including
2526 * freeing video buffer memory. The driver is responsible for freeing
2527 * the vb2_queue structure itself.
2529 void vb2_queue_release(struct vb2_queue
*q
)
2531 __vb2_cleanup_fileio(q
);
2532 __vb2_queue_cancel(q
);
2533 __vb2_queue_free(q
, q
->num_buffers
);
2535 EXPORT_SYMBOL_GPL(vb2_queue_release
);
2538 * struct vb2_fileio_buf - buffer context used by file io emulator
2540 * vb2 provides a compatibility layer and emulator of file io (read and
2541 * write) calls on top of streaming API. This structure is used for
2542 * tracking context related to the buffers.
2544 struct vb2_fileio_buf
{
2548 unsigned int queued
:1;
2552 * struct vb2_fileio_data - queue context used by file io emulator
2554 * @cur_index: the index of the buffer currently being read from or
2555 * written to. If equal to q->num_buffers then a new buffer
2557 * @initial_index: in the read() case all buffers are queued up immediately
2558 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
2559 * buffers. However, in the write() case no buffers are initially
2560 * queued, instead whenever a buffer is full it is queued up by
2561 * __vb2_perform_fileio(). Only once all available buffers have
2562 * been queued up will __vb2_perform_fileio() start to dequeue
2563 * buffers. This means that initially __vb2_perform_fileio()
2564 * needs to know what buffer index to use when it is queuing up
2565 * the buffers for the first time. That initial index is stored
2566 * in this field. Once it is equal to q->num_buffers all
2567 * available buffers have been queued and __vb2_perform_fileio()
2568 * should start the normal dequeue/queue cycle.
2570 * vb2 provides a compatibility layer and emulator of file io (read and
2571 * write) calls on top of streaming API. For proper operation it required
2572 * this structure to save the driver state between each call of the read
2573 * or write function.
2575 struct vb2_fileio_data
{
2576 struct v4l2_requestbuffers req
;
2577 struct v4l2_buffer b
;
2578 struct vb2_fileio_buf bufs
[VIDEO_MAX_FRAME
];
2579 unsigned int cur_index
;
2580 unsigned int initial_index
;
2581 unsigned int q_count
;
2582 unsigned int dq_count
;
2587 * __vb2_init_fileio() - initialize file io emulator
2588 * @q: videobuf2 queue
2589 * @read: mode selector (1 means read, 0 means write)
2591 static int __vb2_init_fileio(struct vb2_queue
*q
, int read
)
2593 struct vb2_fileio_data
*fileio
;
2595 unsigned int count
= 0;
2600 if (WARN_ON((read
&& !(q
->io_modes
& VB2_READ
)) ||
2601 (!read
&& !(q
->io_modes
& VB2_WRITE
))))
2605 * Check if device supports mapping buffers to kernel virtual space.
2607 if (!q
->mem_ops
->vaddr
)
2611 * Check if streaming api has not been already activated.
2613 if (q
->streaming
|| q
->num_buffers
> 0)
2617 * Start with count 1, driver can increase it in queue_setup()
2621 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
2622 (read
) ? "read" : "write", count
, q
->io_flags
);
2624 fileio
= kzalloc(sizeof(struct vb2_fileio_data
), GFP_KERNEL
);
2628 fileio
->flags
= q
->io_flags
;
2631 * Request buffers and use MMAP type to force driver
2632 * to allocate buffers by itself.
2634 fileio
->req
.count
= count
;
2635 fileio
->req
.memory
= V4L2_MEMORY_MMAP
;
2636 fileio
->req
.type
= q
->type
;
2637 ret
= vb2_reqbufs(q
, &fileio
->req
);
2642 * Check if plane_count is correct
2643 * (multiplane buffers are not supported).
2645 if (q
->bufs
[0]->num_planes
!= 1) {
2651 * Get kernel address of each buffer.
2653 for (i
= 0; i
< q
->num_buffers
; i
++) {
2654 fileio
->bufs
[i
].vaddr
= vb2_plane_vaddr(q
->bufs
[i
], 0);
2655 if (fileio
->bufs
[i
].vaddr
== NULL
) {
2659 fileio
->bufs
[i
].size
= vb2_plane_size(q
->bufs
[i
], 0);
2663 * Read mode requires pre queuing of all buffers.
2667 * Queue all buffers.
2669 for (i
= 0; i
< q
->num_buffers
; i
++) {
2670 struct v4l2_buffer
*b
= &fileio
->b
;
2671 memset(b
, 0, sizeof(*b
));
2673 b
->memory
= q
->memory
;
2675 ret
= vb2_qbuf(q
, b
);
2678 fileio
->bufs
[i
].queued
= 1;
2681 * All buffers have been queued, so mark that by setting
2682 * initial_index to q->num_buffers
2684 fileio
->initial_index
= q
->num_buffers
;
2685 fileio
->cur_index
= q
->num_buffers
;
2691 ret
= vb2_streamon(q
, q
->type
);
2700 fileio
->req
.count
= 0;
2701 vb2_reqbufs(q
, &fileio
->req
);
2709 * __vb2_cleanup_fileio() - free resourced used by file io emulator
2710 * @q: videobuf2 queue
2712 static int __vb2_cleanup_fileio(struct vb2_queue
*q
)
2714 struct vb2_fileio_data
*fileio
= q
->fileio
;
2717 vb2_internal_streamoff(q
, q
->type
);
2719 fileio
->req
.count
= 0;
2720 vb2_reqbufs(q
, &fileio
->req
);
2722 dprintk(3, "file io emulator closed\n");
2728 * __vb2_perform_fileio() - perform a single file io (read or write) operation
2729 * @q: videobuf2 queue
2730 * @data: pointed to target userspace buffer
2731 * @count: number of bytes to read or write
2732 * @ppos: file handle position tracking pointer
2733 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
2734 * @read: access mode selector (1 means read, 0 means write)
2736 static size_t __vb2_perform_fileio(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2737 loff_t
*ppos
, int nonblock
, int read
)
2739 struct vb2_fileio_data
*fileio
;
2740 struct vb2_fileio_buf
*buf
;
2743 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
2744 read
? "read" : "write", (long)*ppos
, count
,
2745 nonblock
? "non" : "");
2751 * Initialize emulator on first call.
2754 ret
= __vb2_init_fileio(q
, read
);
2755 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret
);
2762 * Check if we need to dequeue the buffer.
2764 index
= fileio
->cur_index
;
2765 if (index
>= q
->num_buffers
) {
2767 * Call vb2_dqbuf to get buffer back.
2769 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2770 fileio
->b
.type
= q
->type
;
2771 fileio
->b
.memory
= q
->memory
;
2772 ret
= vb2_internal_dqbuf(q
, &fileio
->b
, nonblock
);
2773 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret
);
2776 fileio
->dq_count
+= 1;
2778 fileio
->cur_index
= index
= fileio
->b
.index
;
2779 buf
= &fileio
->bufs
[index
];
2782 * Get number of bytes filled by the driver
2786 buf
->size
= read
? vb2_get_plane_payload(q
->bufs
[index
], 0)
2787 : vb2_plane_size(q
->bufs
[index
], 0);
2789 buf
= &fileio
->bufs
[index
];
2793 * Limit count on last few bytes of the buffer.
2795 if (buf
->pos
+ count
> buf
->size
) {
2796 count
= buf
->size
- buf
->pos
;
2797 dprintk(5, "reducing read count: %zd\n", count
);
2801 * Transfer data to userspace.
2803 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
2804 count
, index
, buf
->pos
);
2806 ret
= copy_to_user(data
, buf
->vaddr
+ buf
->pos
, count
);
2808 ret
= copy_from_user(buf
->vaddr
+ buf
->pos
, data
, count
);
2810 dprintk(3, "file io: error copying data\n");
2821 * Queue next buffer if required.
2823 if (buf
->pos
== buf
->size
||
2824 (!read
&& (fileio
->flags
& VB2_FILEIO_WRITE_IMMEDIATELY
))) {
2826 * Check if this is the last buffer to read.
2828 if (read
&& (fileio
->flags
& VB2_FILEIO_READ_ONCE
) &&
2829 fileio
->dq_count
== 1) {
2830 dprintk(3, "file io: read limit reached\n");
2831 return __vb2_cleanup_fileio(q
);
2835 * Call vb2_qbuf and give buffer to the driver.
2837 memset(&fileio
->b
, 0, sizeof(fileio
->b
));
2838 fileio
->b
.type
= q
->type
;
2839 fileio
->b
.memory
= q
->memory
;
2840 fileio
->b
.index
= index
;
2841 fileio
->b
.bytesused
= buf
->pos
;
2842 ret
= vb2_internal_qbuf(q
, &fileio
->b
);
2843 dprintk(5, "file io: vb2_dbuf result: %d\n", ret
);
2848 * Buffer has been queued, update the status
2852 buf
->size
= vb2_plane_size(q
->bufs
[index
], 0);
2853 fileio
->q_count
+= 1;
2855 * If we are queuing up buffers for the first time, then
2856 * increase initial_index by one.
2858 if (fileio
->initial_index
< q
->num_buffers
)
2859 fileio
->initial_index
++;
2861 * The next buffer to use is either a buffer that's going to be
2862 * queued for the first time (initial_index < q->num_buffers)
2863 * or it is equal to q->num_buffers, meaning that the next
2864 * time we need to dequeue a buffer since we've now queued up
2865 * all the 'first time' buffers.
2867 fileio
->cur_index
= fileio
->initial_index
;
2871 * Return proper number of bytes processed.
2878 size_t vb2_read(struct vb2_queue
*q
, char __user
*data
, size_t count
,
2879 loff_t
*ppos
, int nonblocking
)
2881 return __vb2_perform_fileio(q
, data
, count
, ppos
, nonblocking
, 1);
2883 EXPORT_SYMBOL_GPL(vb2_read
);
2885 size_t vb2_write(struct vb2_queue
*q
, const char __user
*data
, size_t count
,
2886 loff_t
*ppos
, int nonblocking
)
2888 return __vb2_perform_fileio(q
, (char __user
*) data
, count
,
2889 ppos
, nonblocking
, 0);
2891 EXPORT_SYMBOL_GPL(vb2_write
);
2895 * The following functions are not part of the vb2 core API, but are helper
2896 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
2897 * and struct vb2_ops.
2898 * They contain boilerplate code that most if not all drivers have to do
2899 * and so they simplify the driver code.
2902 /* The queue is busy if there is a owner and you are not that owner. */
2903 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
2905 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
2908 /* vb2 ioctl helpers */
2910 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
2911 struct v4l2_requestbuffers
*p
)
2913 struct video_device
*vdev
= video_devdata(file
);
2914 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
2918 if (vb2_queue_is_busy(vdev
, file
))
2920 res
= __reqbufs(vdev
->queue
, p
);
2921 /* If count == 0, then the owner has released all buffers and he
2922 is no longer owner of the queue. Otherwise we have a new owner. */
2924 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
2927 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
2929 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
2930 struct v4l2_create_buffers
*p
)
2932 struct video_device
*vdev
= video_devdata(file
);
2933 int res
= __verify_memory_type(vdev
->queue
, p
->memory
, p
->format
.type
);
2935 p
->index
= vdev
->queue
->num_buffers
;
2936 /* If count == 0, then just check if memory and type are valid.
2937 Any -EBUSY result from __verify_memory_type can be mapped to 0. */
2939 return res
!= -EBUSY
? res
: 0;
2942 if (vb2_queue_is_busy(vdev
, file
))
2944 res
= __create_bufs(vdev
->queue
, p
);
2946 vdev
->queue
->owner
= file
->private_data
;
2949 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
2951 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
2952 struct v4l2_buffer
*p
)
2954 struct video_device
*vdev
= video_devdata(file
);
2956 if (vb2_queue_is_busy(vdev
, file
))
2958 return vb2_prepare_buf(vdev
->queue
, p
);
2960 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
2962 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2964 struct video_device
*vdev
= video_devdata(file
);
2966 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
2967 return vb2_querybuf(vdev
->queue
, p
);
2969 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
2971 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2973 struct video_device
*vdev
= video_devdata(file
);
2975 if (vb2_queue_is_busy(vdev
, file
))
2977 return vb2_qbuf(vdev
->queue
, p
);
2979 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
2981 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
2983 struct video_device
*vdev
= video_devdata(file
);
2985 if (vb2_queue_is_busy(vdev
, file
))
2987 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
2989 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
2991 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
2993 struct video_device
*vdev
= video_devdata(file
);
2995 if (vb2_queue_is_busy(vdev
, file
))
2997 return vb2_streamon(vdev
->queue
, i
);
2999 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
3001 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
3003 struct video_device
*vdev
= video_devdata(file
);
3005 if (vb2_queue_is_busy(vdev
, file
))
3007 return vb2_streamoff(vdev
->queue
, i
);
3009 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
3011 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
3013 struct video_device
*vdev
= video_devdata(file
);
3015 if (vb2_queue_is_busy(vdev
, file
))
3017 return vb2_expbuf(vdev
->queue
, p
);
3019 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
3021 /* v4l2_file_operations helpers */
3023 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3025 struct video_device
*vdev
= video_devdata(file
);
3026 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3029 if (lock
&& mutex_lock_interruptible(lock
))
3030 return -ERESTARTSYS
;
3031 err
= vb2_mmap(vdev
->queue
, vma
);
3036 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
3038 int _vb2_fop_release(struct file
*file
, struct mutex
*lock
)
3040 struct video_device
*vdev
= video_devdata(file
);
3042 if (file
->private_data
== vdev
->queue
->owner
) {
3045 vb2_queue_release(vdev
->queue
);
3046 vdev
->queue
->owner
= NULL
;
3050 return v4l2_fh_release(file
);
3052 EXPORT_SYMBOL_GPL(_vb2_fop_release
);
3054 int vb2_fop_release(struct file
*file
)
3056 struct video_device
*vdev
= video_devdata(file
);
3057 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3059 return _vb2_fop_release(file
, lock
);
3061 EXPORT_SYMBOL_GPL(vb2_fop_release
);
3063 ssize_t
vb2_fop_write(struct file
*file
, const char __user
*buf
,
3064 size_t count
, loff_t
*ppos
)
3066 struct video_device
*vdev
= video_devdata(file
);
3067 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3070 if (lock
&& mutex_lock_interruptible(lock
))
3071 return -ERESTARTSYS
;
3072 if (vb2_queue_is_busy(vdev
, file
))
3074 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
3075 file
->f_flags
& O_NONBLOCK
);
3076 if (vdev
->queue
->fileio
)
3077 vdev
->queue
->owner
= file
->private_data
;
3083 EXPORT_SYMBOL_GPL(vb2_fop_write
);
3085 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
3086 size_t count
, loff_t
*ppos
)
3088 struct video_device
*vdev
= video_devdata(file
);
3089 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3092 if (lock
&& mutex_lock_interruptible(lock
))
3093 return -ERESTARTSYS
;
3094 if (vb2_queue_is_busy(vdev
, file
))
3096 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
3097 file
->f_flags
& O_NONBLOCK
);
3098 if (vdev
->queue
->fileio
)
3099 vdev
->queue
->owner
= file
->private_data
;
3105 EXPORT_SYMBOL_GPL(vb2_fop_read
);
3107 unsigned int vb2_fop_poll(struct file
*file
, poll_table
*wait
)
3109 struct video_device
*vdev
= video_devdata(file
);
3110 struct vb2_queue
*q
= vdev
->queue
;
3111 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
3112 unsigned long req_events
= poll_requested_events(wait
);
3115 bool must_lock
= false;
3117 /* Try to be smart: only lock if polling might start fileio,
3118 otherwise locking will only introduce unwanted delays. */
3119 if (q
->num_buffers
== 0 && q
->fileio
== NULL
) {
3120 if (!V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_READ
) &&
3121 (req_events
& (POLLIN
| POLLRDNORM
)))
3123 else if (V4L2_TYPE_IS_OUTPUT(q
->type
) && (q
->io_modes
& VB2_WRITE
) &&
3124 (req_events
& (POLLOUT
| POLLWRNORM
)))
3128 /* If locking is needed, but this helper doesn't know how, then you
3129 shouldn't be using this helper but you should write your own. */
3130 WARN_ON(must_lock
&& !lock
);
3132 if (must_lock
&& lock
&& mutex_lock_interruptible(lock
))
3137 res
= vb2_poll(vdev
->queue
, file
, wait
);
3139 /* If fileio was started, then we have a new queue owner. */
3140 if (must_lock
&& !fileio
&& q
->fileio
)
3141 q
->owner
= file
->private_data
;
3142 if (must_lock
&& lock
)
3146 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
3149 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
3150 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
3152 struct video_device
*vdev
= video_devdata(file
);
3153 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
3156 if (lock
&& mutex_lock_interruptible(lock
))
3157 return -ERESTARTSYS
;
3158 ret
= vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
3163 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
3166 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
3168 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
3170 mutex_unlock(vq
->lock
);
3172 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
3174 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
3176 mutex_lock(vq
->lock
);
3178 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
3180 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
3181 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
3182 MODULE_LICENSE("GPL");