2 * videobuf2-v4l2.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 * The vb2_thread implementation was based on code from videobuf-dvb.c:
10 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation.
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/freezer.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
23 #include <linux/module.h>
24 #include <linux/poll.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-dev.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-event.h>
32 #include <media/v4l2-fh.h>
34 #include <media/videobuf2-v4l2.h>
37 module_param(debug
, int, 0644);
39 #define dprintk(q, level, fmt, arg...) \
42 pr_info("vb2-v4l2: [%p] %s: " fmt, \
43 (q)->name, __func__, ## arg); \
46 /* Flags that are set by us */
47 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
48 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
49 V4L2_BUF_FLAG_PREPARED | \
50 V4L2_BUF_FLAG_IN_REQUEST | \
51 V4L2_BUF_FLAG_REQUEST_FD | \
52 V4L2_BUF_FLAG_TIMESTAMP_MASK)
53 /* Output buffer flags that should be passed on to the driver */
54 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | \
55 V4L2_BUF_FLAG_BFRAME | \
56 V4L2_BUF_FLAG_KEYFRAME | \
57 V4L2_BUF_FLAG_TIMECODE | \
58 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
61 * __verify_planes_array() - verify that the planes array passed in struct
62 * v4l2_buffer from userspace can be safely used
64 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
66 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
69 /* Is memory for copying plane information present? */
70 if (b
->m
.planes
== NULL
) {
71 dprintk(vb
->vb2_queue
, 1,
72 "multi-planar buffer passed but planes array not provided\n");
76 if (b
->length
< vb
->num_planes
|| b
->length
> VB2_MAX_PLANES
) {
77 dprintk(vb
->vb2_queue
, 1,
78 "incorrect planes array length, expected %d, got %d\n",
79 vb
->num_planes
, b
->length
);
86 static int __verify_planes_array_core(struct vb2_buffer
*vb
, const void *pb
)
88 return __verify_planes_array(vb
, pb
);
92 * __verify_length() - Verify that the bytesused value for each plane fits in
93 * the plane length and that the data offset doesn't exceed the bytesused value.
95 static int __verify_length(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
98 unsigned int bytesused
;
101 if (V4L2_TYPE_IS_CAPTURE(b
->type
))
104 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
105 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
106 length
= (b
->memory
== VB2_MEMORY_USERPTR
||
107 b
->memory
== VB2_MEMORY_DMABUF
)
108 ? b
->m
.planes
[plane
].length
109 : vb
->planes
[plane
].length
;
110 bytesused
= b
->m
.planes
[plane
].bytesused
111 ? b
->m
.planes
[plane
].bytesused
: length
;
113 if (b
->m
.planes
[plane
].bytesused
> length
)
116 if (b
->m
.planes
[plane
].data_offset
> 0 &&
117 b
->m
.planes
[plane
].data_offset
>= bytesused
)
121 length
= (b
->memory
== VB2_MEMORY_USERPTR
)
122 ? b
->length
: vb
->planes
[0].length
;
124 if (b
->bytesused
> length
)
132 * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
134 static void __init_vb2_v4l2_buffer(struct vb2_buffer
*vb
)
136 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
138 vbuf
->request_fd
= -1;
141 static void __copy_timestamp(struct vb2_buffer
*vb
, const void *pb
)
143 const struct v4l2_buffer
*b
= pb
;
144 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
145 struct vb2_queue
*q
= vb
->vb2_queue
;
149 * For output buffers copy the timestamp if needed,
150 * and the timecode field and flag if needed.
152 if (q
->copy_timestamp
)
153 vb
->timestamp
= v4l2_buffer_get_timestamp(b
);
154 vbuf
->flags
|= b
->flags
& V4L2_BUF_FLAG_TIMECODE
;
155 if (b
->flags
& V4L2_BUF_FLAG_TIMECODE
)
156 vbuf
->timecode
= b
->timecode
;
160 static void vb2_warn_zero_bytesused(struct vb2_buffer
*vb
)
162 static bool check_once
;
169 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
170 if (vb
->vb2_queue
->allow_zero_bytesused
)
171 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
173 pr_warn("use the actual size instead.\n");
176 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
178 struct vb2_queue
*q
= vb
->vb2_queue
;
179 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
180 struct vb2_plane
*planes
= vbuf
->planes
;
184 ret
= __verify_length(vb
, b
);
186 dprintk(q
, 1, "plane parameters verification failed: %d\n", ret
);
189 if (b
->field
== V4L2_FIELD_ALTERNATE
&& q
->is_output
) {
191 * If the format's field is ALTERNATE, then the buffer's field
192 * should be either TOP or BOTTOM, not ALTERNATE since that
193 * makes no sense. The driver has to know whether the
194 * buffer represents a top or a bottom field in order to
195 * program any DMA correctly. Using ALTERNATE is wrong, since
196 * that just says that it is either a top or a bottom field,
197 * but not which of the two it is.
199 dprintk(q
, 1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
203 vbuf
->request_fd
= -1;
204 vbuf
->is_held
= false;
206 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
208 case VB2_MEMORY_USERPTR
:
209 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
210 planes
[plane
].m
.userptr
=
211 b
->m
.planes
[plane
].m
.userptr
;
212 planes
[plane
].length
=
213 b
->m
.planes
[plane
].length
;
216 case VB2_MEMORY_DMABUF
:
217 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
219 b
->m
.planes
[plane
].m
.fd
;
220 planes
[plane
].length
=
221 b
->m
.planes
[plane
].length
;
225 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
226 planes
[plane
].m
.offset
=
227 vb
->planes
[plane
].m
.offset
;
228 planes
[plane
].length
=
229 vb
->planes
[plane
].length
;
234 /* Fill in user-provided information for OUTPUT types */
235 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
237 * Will have to go up to b->length when API starts
238 * accepting variable number of planes.
240 * If bytesused == 0 for the output buffer, then fall
241 * back to the full buffer size. In that case
242 * userspace clearly never bothered to set it and
243 * it's a safe assumption that they really meant to
244 * use the full plane sizes.
246 * Some drivers, e.g. old codec drivers, use bytesused == 0
247 * as a way to indicate that streaming is finished.
248 * In that case, the driver should use the
249 * allow_zero_bytesused flag to keep old userspace
250 * applications working.
252 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
253 struct vb2_plane
*pdst
= &planes
[plane
];
254 struct v4l2_plane
*psrc
= &b
->m
.planes
[plane
];
256 if (psrc
->bytesused
== 0)
257 vb2_warn_zero_bytesused(vb
);
259 if (vb
->vb2_queue
->allow_zero_bytesused
)
260 pdst
->bytesused
= psrc
->bytesused
;
262 pdst
->bytesused
= psrc
->bytesused
?
263 psrc
->bytesused
: pdst
->length
;
264 pdst
->data_offset
= psrc
->data_offset
;
269 * Single-planar buffers do not use planes array,
270 * so fill in relevant v4l2_buffer struct fields instead.
271 * In vb2 we use our internal V4l2_planes struct for
272 * single-planar buffers as well, for simplicity.
274 * If bytesused == 0 for the output buffer, then fall back
275 * to the full buffer size as that's a sensible default.
277 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
278 * a way to indicate that streaming is finished. In that case,
279 * the driver should use the allow_zero_bytesused flag to keep
280 * old userspace applications working.
283 case VB2_MEMORY_USERPTR
:
284 planes
[0].m
.userptr
= b
->m
.userptr
;
285 planes
[0].length
= b
->length
;
287 case VB2_MEMORY_DMABUF
:
288 planes
[0].m
.fd
= b
->m
.fd
;
289 planes
[0].length
= b
->length
;
292 planes
[0].m
.offset
= vb
->planes
[0].m
.offset
;
293 planes
[0].length
= vb
->planes
[0].length
;
297 planes
[0].data_offset
= 0;
298 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
299 if (b
->bytesused
== 0)
300 vb2_warn_zero_bytesused(vb
);
302 if (vb
->vb2_queue
->allow_zero_bytesused
)
303 planes
[0].bytesused
= b
->bytesused
;
305 planes
[0].bytesused
= b
->bytesused
?
306 b
->bytesused
: planes
[0].length
;
308 planes
[0].bytesused
= 0;
312 /* Zero flags that we handle */
313 vbuf
->flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
314 if (!vb
->vb2_queue
->copy_timestamp
|| V4L2_TYPE_IS_CAPTURE(b
->type
)) {
316 * Non-COPY timestamps and non-OUTPUT queues will get
317 * their timestamp and timestamp source flags from the
320 vbuf
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
323 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
325 * For output buffers mask out the timecode flag:
326 * this will be handled later in vb2_qbuf().
327 * The 'field' is valid metadata for this output buffer
328 * and so that needs to be copied here.
330 vbuf
->flags
&= ~V4L2_BUF_FLAG_TIMECODE
;
331 vbuf
->field
= b
->field
;
332 if (!(q
->subsystem_flags
& VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF
))
333 vbuf
->flags
&= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF
;
335 /* Zero any output buffer flags as this is a capture buffer */
336 vbuf
->flags
&= ~V4L2_BUFFER_OUT_FLAGS
;
337 /* Zero last flag, this is a signal from driver to userspace */
338 vbuf
->flags
&= ~V4L2_BUF_FLAG_LAST
;
344 static void set_buffer_cache_hints(struct vb2_queue
*q
,
345 struct vb2_buffer
*vb
,
346 struct v4l2_buffer
*b
)
348 if (!vb2_queue_allows_cache_hints(q
)) {
350 * Clear buffer cache flags if queue does not support user
351 * space hints. That's to indicate to userspace that these
354 b
->flags
&= ~V4L2_BUF_FLAG_NO_CACHE_INVALIDATE
;
355 b
->flags
&= ~V4L2_BUF_FLAG_NO_CACHE_CLEAN
;
359 if (b
->flags
& V4L2_BUF_FLAG_NO_CACHE_INVALIDATE
)
360 vb
->skip_cache_sync_on_finish
= 1;
362 if (b
->flags
& V4L2_BUF_FLAG_NO_CACHE_CLEAN
)
363 vb
->skip_cache_sync_on_prepare
= 1;
366 static int vb2_queue_or_prepare_buf(struct vb2_queue
*q
, struct media_device
*mdev
,
367 struct vb2_buffer
*vb
, struct v4l2_buffer
*b
,
368 bool is_prepare
, struct media_request
**p_req
)
370 const char *opname
= is_prepare
? "prepare_buf" : "qbuf";
371 struct media_request
*req
;
372 struct vb2_v4l2_buffer
*vbuf
;
375 if (b
->type
!= q
->type
) {
376 dprintk(q
, 1, "%s: invalid buffer type\n", opname
);
380 if (b
->memory
!= q
->memory
) {
381 dprintk(q
, 1, "%s: invalid memory type\n", opname
);
385 vbuf
= to_vb2_v4l2_buffer(vb
);
386 ret
= __verify_planes_array(vb
, b
);
390 if (!is_prepare
&& (b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
) &&
391 vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
392 dprintk(q
, 1, "%s: buffer is not in dequeued state\n", opname
);
397 set_buffer_cache_hints(q
, vb
, b
);
398 /* Copy relevant information provided by the userspace */
399 memset(vbuf
->planes
, 0,
400 sizeof(vbuf
->planes
[0]) * vb
->num_planes
);
401 ret
= vb2_fill_vb2_v4l2_buffer(vb
, b
);
409 if (!(b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
)) {
410 if (q
->requires_requests
) {
411 dprintk(q
, 1, "%s: queue requires requests\n", opname
);
414 if (q
->uses_requests
) {
415 dprintk(q
, 1, "%s: queue uses requests\n", opname
);
419 } else if (!q
->supports_requests
) {
420 dprintk(q
, 1, "%s: queue does not support requests\n", opname
);
422 } else if (q
->uses_qbuf
) {
423 dprintk(q
, 1, "%s: queue does not use requests\n", opname
);
428 * For proper locking when queueing a request you need to be able
429 * to lock access to the vb2 queue, so check that there is a lock
430 * that we can use. In addition p_req must be non-NULL.
432 if (WARN_ON(!q
->lock
|| !p_req
))
436 * Make sure this op is implemented by the driver. It's easy to forget
437 * this callback, but is it important when canceling a buffer in a
440 if (WARN_ON(!q
->ops
->buf_request_complete
))
443 * Make sure this op is implemented by the driver for the output queue.
444 * It's easy to forget this callback, but is it important to correctly
445 * validate the 'field' value at QBUF time.
447 if (WARN_ON((q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
||
448 q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) &&
449 !q
->ops
->buf_out_validate
))
452 req
= media_request_get_by_fd(mdev
, b
->request_fd
);
454 dprintk(q
, 1, "%s: invalid request_fd\n", opname
);
459 * Early sanity check. This is checked again when the buffer
460 * is bound to the request in vb2_core_qbuf().
462 if (req
->state
!= MEDIA_REQUEST_STATE_IDLE
&&
463 req
->state
!= MEDIA_REQUEST_STATE_UPDATING
) {
464 dprintk(q
, 1, "%s: request is not idle\n", opname
);
465 media_request_put(req
);
470 vbuf
->request_fd
= b
->request_fd
;
476 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
477 * returned to userspace
479 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, void *pb
)
481 struct v4l2_buffer
*b
= pb
;
482 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
483 struct vb2_queue
*q
= vb
->vb2_queue
;
486 /* Copy back data such as timestamp, flags, etc. */
487 b
->index
= vb
->index
;
489 b
->memory
= vb
->memory
;
492 b
->flags
= vbuf
->flags
;
493 b
->field
= vbuf
->field
;
494 v4l2_buffer_set_timestamp(b
, vb
->timestamp
);
495 b
->timecode
= vbuf
->timecode
;
496 b
->sequence
= vbuf
->sequence
;
500 if (q
->is_multiplanar
) {
502 * Fill in plane-related data if userspace provided an array
503 * for it. The caller has already verified memory and size.
505 b
->length
= vb
->num_planes
;
506 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
507 struct v4l2_plane
*pdst
= &b
->m
.planes
[plane
];
508 struct vb2_plane
*psrc
= &vb
->planes
[plane
];
510 pdst
->bytesused
= psrc
->bytesused
;
511 pdst
->length
= psrc
->length
;
512 if (q
->memory
== VB2_MEMORY_MMAP
)
513 pdst
->m
.mem_offset
= psrc
->m
.offset
;
514 else if (q
->memory
== VB2_MEMORY_USERPTR
)
515 pdst
->m
.userptr
= psrc
->m
.userptr
;
516 else if (q
->memory
== VB2_MEMORY_DMABUF
)
517 pdst
->m
.fd
= psrc
->m
.fd
;
518 pdst
->data_offset
= psrc
->data_offset
;
519 memset(pdst
->reserved
, 0, sizeof(pdst
->reserved
));
523 * We use length and offset in v4l2_planes array even for
524 * single-planar buffers, but userspace does not.
526 b
->length
= vb
->planes
[0].length
;
527 b
->bytesused
= vb
->planes
[0].bytesused
;
528 if (q
->memory
== VB2_MEMORY_MMAP
)
529 b
->m
.offset
= vb
->planes
[0].m
.offset
;
530 else if (q
->memory
== VB2_MEMORY_USERPTR
)
531 b
->m
.userptr
= vb
->planes
[0].m
.userptr
;
532 else if (q
->memory
== VB2_MEMORY_DMABUF
)
533 b
->m
.fd
= vb
->planes
[0].m
.fd
;
537 * Clear any buffer state related flags.
539 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
540 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
;
541 if (!q
->copy_timestamp
) {
543 * For non-COPY timestamps, drop timestamp source bits
544 * and obtain the timestamp source from the queue.
546 b
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
547 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
551 case VB2_BUF_STATE_QUEUED
:
552 case VB2_BUF_STATE_ACTIVE
:
553 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
555 case VB2_BUF_STATE_IN_REQUEST
:
556 b
->flags
|= V4L2_BUF_FLAG_IN_REQUEST
;
558 case VB2_BUF_STATE_ERROR
:
559 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
561 case VB2_BUF_STATE_DONE
:
562 b
->flags
|= V4L2_BUF_FLAG_DONE
;
564 case VB2_BUF_STATE_PREPARING
:
565 case VB2_BUF_STATE_DEQUEUED
:
570 if ((vb
->state
== VB2_BUF_STATE_DEQUEUED
||
571 vb
->state
== VB2_BUF_STATE_IN_REQUEST
) &&
572 vb
->synced
&& vb
->prepared
)
573 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
575 if (vb2_buffer_in_use(q
, vb
))
576 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
577 if (vbuf
->request_fd
>= 0) {
578 b
->flags
|= V4L2_BUF_FLAG_REQUEST_FD
;
579 b
->request_fd
= vbuf
->request_fd
;
584 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
585 * v4l2_buffer by the userspace. It also verifies that struct
586 * v4l2_buffer has a valid number of planes.
588 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, struct vb2_plane
*planes
)
590 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
593 if (!vb
->vb2_queue
->copy_timestamp
)
596 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
597 if (vb
->vb2_queue
->memory
!= VB2_MEMORY_MMAP
) {
598 planes
[plane
].m
= vbuf
->planes
[plane
].m
;
599 planes
[plane
].length
= vbuf
->planes
[plane
].length
;
601 planes
[plane
].bytesused
= vbuf
->planes
[plane
].bytesused
;
602 planes
[plane
].data_offset
= vbuf
->planes
[plane
].data_offset
;
607 static const struct vb2_buf_ops v4l2_buf_ops
= {
608 .verify_planes_array
= __verify_planes_array_core
,
609 .init_buffer
= __init_vb2_v4l2_buffer
,
610 .fill_user_buffer
= __fill_v4l2_buffer
,
611 .fill_vb2_buffer
= __fill_vb2_buffer
,
612 .copy_timestamp
= __copy_timestamp
,
615 struct vb2_buffer
*vb2_find_buffer(struct vb2_queue
*q
, u64 timestamp
)
618 struct vb2_buffer
*vb2
;
621 * This loop doesn't scale if there is a really large number of buffers.
622 * Maybe something more efficient will be needed in this case.
624 for (i
= 0; i
< q
->max_num_buffers
; i
++) {
625 vb2
= vb2_get_buffer(q
, i
);
630 if (vb2
->copied_timestamp
&&
631 vb2
->timestamp
== timestamp
)
636 EXPORT_SYMBOL_GPL(vb2_find_buffer
);
639 * vb2_querybuf() - query video buffer information
641 * @b: buffer struct passed from userspace to vidioc_querybuf handler
644 * Should be called from vidioc_querybuf ioctl handler in driver.
645 * This function will verify the passed v4l2_buffer structure and fill the
646 * relevant information for the userspace.
648 * The return values from this function are intended to be directly returned
649 * from vidioc_querybuf handler in driver.
651 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
653 struct vb2_buffer
*vb
;
656 if (b
->type
!= q
->type
) {
657 dprintk(q
, 1, "wrong buffer type\n");
661 vb
= vb2_get_buffer(q
, b
->index
);
663 dprintk(q
, 1, "can't find the requested buffer %u\n", b
->index
);
667 ret
= __verify_planes_array(vb
, b
);
669 vb2_core_querybuf(q
, vb
, b
);
672 EXPORT_SYMBOL(vb2_querybuf
);
674 static void vb2_set_flags_and_caps(struct vb2_queue
*q
, u32 memory
,
675 u32
*flags
, u32
*caps
, u32
*max_num_bufs
)
677 if (!q
->allow_cache_hints
|| memory
!= V4L2_MEMORY_MMAP
) {
679 * This needs to clear V4L2_MEMORY_FLAG_NON_COHERENT only,
680 * but in order to avoid bugs we zero out all bits.
684 /* Clear all unknown flags. */
685 *flags
&= V4L2_MEMORY_FLAG_NON_COHERENT
;
688 *caps
|= V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
;
689 if (q
->io_modes
& VB2_MMAP
)
690 *caps
|= V4L2_BUF_CAP_SUPPORTS_MMAP
;
691 if (q
->io_modes
& VB2_USERPTR
)
692 *caps
|= V4L2_BUF_CAP_SUPPORTS_USERPTR
;
693 if (q
->io_modes
& VB2_DMABUF
)
694 *caps
|= V4L2_BUF_CAP_SUPPORTS_DMABUF
;
695 if (q
->subsystem_flags
& VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF
)
696 *caps
|= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF
;
697 if (q
->allow_cache_hints
&& q
->io_modes
& VB2_MMAP
)
698 *caps
|= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS
;
699 if (q
->supports_requests
)
700 *caps
|= V4L2_BUF_CAP_SUPPORTS_REQUESTS
;
702 *max_num_bufs
= q
->max_num_buffers
;
703 *caps
|= V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS
;
707 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
709 int ret
= vb2_verify_memory_type(q
, req
->memory
, req
->type
);
710 u32 flags
= req
->flags
;
712 vb2_set_flags_and_caps(q
, req
->memory
, &flags
,
713 &req
->capabilities
, NULL
);
715 return ret
? ret
: vb2_core_reqbufs(q
, req
->memory
,
716 req
->flags
, &req
->count
);
718 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
720 int vb2_prepare_buf(struct vb2_queue
*q
, struct media_device
*mdev
,
721 struct v4l2_buffer
*b
)
723 struct vb2_buffer
*vb
;
726 if (vb2_fileio_is_active(q
)) {
727 dprintk(q
, 1, "file io in progress\n");
731 if (b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
)
734 vb
= vb2_get_buffer(q
, b
->index
);
736 dprintk(q
, 1, "can't find the requested buffer %u\n", b
->index
);
740 ret
= vb2_queue_or_prepare_buf(q
, mdev
, vb
, b
, true, NULL
);
742 return ret
? ret
: vb2_core_prepare_buf(q
, vb
, b
);
744 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
746 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
748 unsigned requested_planes
= 1;
749 unsigned requested_sizes
[VIDEO_MAX_PLANES
];
750 struct v4l2_format
*f
= &create
->format
;
751 int ret
= vb2_verify_memory_type(q
, create
->memory
, f
->type
);
754 create
->index
= vb2_get_num_buffers(q
);
755 vb2_set_flags_and_caps(q
, create
->memory
, &create
->flags
,
756 &create
->capabilities
, &create
->max_num_buffers
);
757 if (create
->count
== 0)
758 return ret
!= -EBUSY
? ret
: 0;
761 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
762 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
763 requested_planes
= f
->fmt
.pix_mp
.num_planes
;
764 if (requested_planes
== 0 ||
765 requested_planes
> VIDEO_MAX_PLANES
)
767 for (i
= 0; i
< requested_planes
; i
++)
769 f
->fmt
.pix_mp
.plane_fmt
[i
].sizeimage
;
771 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
772 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
773 requested_sizes
[0] = f
->fmt
.pix
.sizeimage
;
775 case V4L2_BUF_TYPE_VBI_CAPTURE
:
776 case V4L2_BUF_TYPE_VBI_OUTPUT
:
777 requested_sizes
[0] = f
->fmt
.vbi
.samples_per_line
*
778 (f
->fmt
.vbi
.count
[0] + f
->fmt
.vbi
.count
[1]);
780 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
:
781 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
:
782 requested_sizes
[0] = f
->fmt
.sliced
.io_size
;
784 case V4L2_BUF_TYPE_SDR_CAPTURE
:
785 case V4L2_BUF_TYPE_SDR_OUTPUT
:
786 requested_sizes
[0] = f
->fmt
.sdr
.buffersize
;
788 case V4L2_BUF_TYPE_META_CAPTURE
:
789 case V4L2_BUF_TYPE_META_OUTPUT
:
790 requested_sizes
[0] = f
->fmt
.meta
.buffersize
;
795 for (i
= 0; i
< requested_planes
; i
++)
796 if (requested_sizes
[i
] == 0)
801 return vb2_core_create_bufs(q
, create
->memory
,
808 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
810 int vb2_qbuf(struct vb2_queue
*q
, struct media_device
*mdev
,
811 struct v4l2_buffer
*b
)
813 struct media_request
*req
= NULL
;
814 struct vb2_buffer
*vb
;
817 if (vb2_fileio_is_active(q
)) {
818 dprintk(q
, 1, "file io in progress\n");
822 vb
= vb2_get_buffer(q
, b
->index
);
824 dprintk(q
, 1, "can't find the requested buffer %u\n", b
->index
);
828 ret
= vb2_queue_or_prepare_buf(q
, mdev
, vb
, b
, false, &req
);
831 ret
= vb2_core_qbuf(q
, vb
, b
, req
);
833 media_request_put(req
);
836 EXPORT_SYMBOL_GPL(vb2_qbuf
);
838 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
842 if (vb2_fileio_is_active(q
)) {
843 dprintk(q
, 1, "file io in progress\n");
847 if (b
->type
!= q
->type
) {
848 dprintk(q
, 1, "invalid buffer type\n");
852 ret
= vb2_core_dqbuf(q
, NULL
, b
, nonblocking
);
855 b
->flags
& V4L2_BUF_FLAG_DONE
&&
856 b
->flags
& V4L2_BUF_FLAG_LAST
)
857 q
->last_buffer_dequeued
= true;
860 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
863 b
->flags
&= ~V4L2_BUF_FLAG_DONE
;
867 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
869 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
871 if (vb2_fileio_is_active(q
)) {
872 dprintk(q
, 1, "file io in progress\n");
875 return vb2_core_streamon(q
, type
);
877 EXPORT_SYMBOL_GPL(vb2_streamon
);
879 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
881 if (vb2_fileio_is_active(q
)) {
882 dprintk(q
, 1, "file io in progress\n");
885 return vb2_core_streamoff(q
, type
);
887 EXPORT_SYMBOL_GPL(vb2_streamoff
);
889 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
891 struct vb2_buffer
*vb
;
893 vb
= vb2_get_buffer(q
, eb
->index
);
895 dprintk(q
, 1, "can't find the requested buffer %u\n", eb
->index
);
899 return vb2_core_expbuf(q
, &eb
->fd
, eb
->type
, vb
,
900 eb
->plane
, eb
->flags
);
902 EXPORT_SYMBOL_GPL(vb2_expbuf
);
904 int vb2_queue_init_name(struct vb2_queue
*q
, const char *name
)
910 WARN_ON(q
->timestamp_flags
&
911 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK
|
912 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
)))
915 /* Warn that the driver should choose an appropriate timestamp type */
916 WARN_ON((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
917 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN
);
919 /* Warn that vb2_memory should match with v4l2_memory */
920 if (WARN_ON(VB2_MEMORY_MMAP
!= (int)V4L2_MEMORY_MMAP
)
921 || WARN_ON(VB2_MEMORY_USERPTR
!= (int)V4L2_MEMORY_USERPTR
)
922 || WARN_ON(VB2_MEMORY_DMABUF
!= (int)V4L2_MEMORY_DMABUF
))
925 if (q
->buf_struct_size
== 0)
926 q
->buf_struct_size
= sizeof(struct vb2_v4l2_buffer
);
928 q
->buf_ops
= &v4l2_buf_ops
;
929 q
->is_multiplanar
= V4L2_TYPE_IS_MULTIPLANAR(q
->type
);
930 q
->is_output
= V4L2_TYPE_IS_OUTPUT(q
->type
);
931 q
->copy_timestamp
= (q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
)
932 == V4L2_BUF_FLAG_TIMESTAMP_COPY
;
934 * For compatibility with vb1: if QBUF hasn't been called yet, then
935 * return EPOLLERR as well. This only affects capture queues, output
936 * queues will always initialize waiting_for_buffers to false.
938 q
->quirk_poll_must_check_waiting_for_buffers
= true;
941 strscpy(q
->name
, name
, sizeof(q
->name
));
945 return vb2_core_queue_init(q
);
947 EXPORT_SYMBOL_GPL(vb2_queue_init_name
);
949 int vb2_queue_init(struct vb2_queue
*q
)
951 return vb2_queue_init_name(q
, NULL
);
953 EXPORT_SYMBOL_GPL(vb2_queue_init
);
955 void vb2_queue_release(struct vb2_queue
*q
)
957 vb2_core_queue_release(q
);
959 EXPORT_SYMBOL_GPL(vb2_queue_release
);
961 int vb2_queue_change_type(struct vb2_queue
*q
, unsigned int type
)
973 EXPORT_SYMBOL_GPL(vb2_queue_change_type
);
975 __poll_t
vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
977 struct video_device
*vfd
= video_devdata(file
);
980 res
= vb2_core_poll(q
, file
, wait
);
982 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
983 struct v4l2_fh
*fh
= file
->private_data
;
985 poll_wait(file
, &fh
->wait
, wait
);
986 if (v4l2_event_pending(fh
))
992 EXPORT_SYMBOL_GPL(vb2_poll
);
995 * The following functions are not part of the vb2 core API, but are helper
996 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
997 * and struct vb2_ops.
998 * They contain boilerplate code that most if not all drivers have to do
999 * and so they simplify the driver code.
1002 /* vb2 ioctl helpers */
1004 int vb2_ioctl_remove_bufs(struct file
*file
, void *priv
,
1005 struct v4l2_remove_buffers
*d
)
1007 struct video_device
*vdev
= video_devdata(file
);
1009 if (vdev
->queue
->type
!= d
->type
)
1015 if (vb2_queue_is_busy(vdev
->queue
, file
))
1018 return vb2_core_remove_bufs(vdev
->queue
, d
->index
, d
->count
);
1020 EXPORT_SYMBOL_GPL(vb2_ioctl_remove_bufs
);
1022 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
1023 struct v4l2_requestbuffers
*p
)
1025 struct video_device
*vdev
= video_devdata(file
);
1026 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
1027 u32 flags
= p
->flags
;
1029 vb2_set_flags_and_caps(vdev
->queue
, p
->memory
, &flags
,
1030 &p
->capabilities
, NULL
);
1034 if (vb2_queue_is_busy(vdev
->queue
, file
))
1036 res
= vb2_core_reqbufs(vdev
->queue
, p
->memory
, p
->flags
, &p
->count
);
1037 /* If count == 0, then the owner has released all buffers and he
1038 is no longer owner of the queue. Otherwise we have a new owner. */
1040 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
1043 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
1045 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
1046 struct v4l2_create_buffers
*p
)
1048 struct video_device
*vdev
= video_devdata(file
);
1049 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
, p
->format
.type
);
1051 p
->index
= vb2_get_num_buffers(vdev
->queue
);
1052 vb2_set_flags_and_caps(vdev
->queue
, p
->memory
, &p
->flags
,
1053 &p
->capabilities
, &p
->max_num_buffers
);
1055 * If count == 0, then just check if memory and type are valid.
1056 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1059 return res
!= -EBUSY
? res
: 0;
1062 if (vb2_queue_is_busy(vdev
->queue
, file
))
1065 res
= vb2_create_bufs(vdev
->queue
, p
);
1067 vdev
->queue
->owner
= file
->private_data
;
1070 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
1072 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
1073 struct v4l2_buffer
*p
)
1075 struct video_device
*vdev
= video_devdata(file
);
1077 if (vb2_queue_is_busy(vdev
->queue
, file
))
1079 return vb2_prepare_buf(vdev
->queue
, vdev
->v4l2_dev
->mdev
, p
);
1081 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
1083 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1085 struct video_device
*vdev
= video_devdata(file
);
1087 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1088 return vb2_querybuf(vdev
->queue
, p
);
1090 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
1092 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1094 struct video_device
*vdev
= video_devdata(file
);
1096 if (vb2_queue_is_busy(vdev
->queue
, file
))
1098 return vb2_qbuf(vdev
->queue
, vdev
->v4l2_dev
->mdev
, p
);
1100 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
1102 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1104 struct video_device
*vdev
= video_devdata(file
);
1106 if (vb2_queue_is_busy(vdev
->queue
, file
))
1108 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
1110 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
1112 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1114 struct video_device
*vdev
= video_devdata(file
);
1116 if (vb2_queue_is_busy(vdev
->queue
, file
))
1118 return vb2_streamon(vdev
->queue
, i
);
1120 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
1122 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1124 struct video_device
*vdev
= video_devdata(file
);
1126 if (vb2_queue_is_busy(vdev
->queue
, file
))
1128 return vb2_streamoff(vdev
->queue
, i
);
1130 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
1132 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
1134 struct video_device
*vdev
= video_devdata(file
);
1136 if (vb2_queue_is_busy(vdev
->queue
, file
))
1138 return vb2_expbuf(vdev
->queue
, p
);
1140 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
1142 /* v4l2_file_operations helpers */
1144 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1146 struct video_device
*vdev
= video_devdata(file
);
1148 return vb2_mmap(vdev
->queue
, vma
);
1150 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
1152 int _vb2_fop_release(struct file
*file
, struct mutex
*lock
)
1154 struct video_device
*vdev
= video_devdata(file
);
1158 if (!vdev
->queue
->owner
|| file
->private_data
== vdev
->queue
->owner
) {
1159 vb2_queue_release(vdev
->queue
);
1160 vdev
->queue
->owner
= NULL
;
1164 return v4l2_fh_release(file
);
1166 EXPORT_SYMBOL_GPL(_vb2_fop_release
);
1168 int vb2_fop_release(struct file
*file
)
1170 struct video_device
*vdev
= video_devdata(file
);
1171 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1173 return _vb2_fop_release(file
, lock
);
1175 EXPORT_SYMBOL_GPL(vb2_fop_release
);
1177 ssize_t
vb2_fop_write(struct file
*file
, const char __user
*buf
,
1178 size_t count
, loff_t
*ppos
)
1180 struct video_device
*vdev
= video_devdata(file
);
1181 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1184 if (!(vdev
->queue
->io_modes
& VB2_WRITE
))
1186 if (lock
&& mutex_lock_interruptible(lock
))
1187 return -ERESTARTSYS
;
1188 if (vb2_queue_is_busy(vdev
->queue
, file
))
1190 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
1191 file
->f_flags
& O_NONBLOCK
);
1192 if (vdev
->queue
->fileio
)
1193 vdev
->queue
->owner
= file
->private_data
;
1199 EXPORT_SYMBOL_GPL(vb2_fop_write
);
1201 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
1202 size_t count
, loff_t
*ppos
)
1204 struct video_device
*vdev
= video_devdata(file
);
1205 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1208 if (!(vdev
->queue
->io_modes
& VB2_READ
))
1210 if (lock
&& mutex_lock_interruptible(lock
))
1211 return -ERESTARTSYS
;
1212 if (vb2_queue_is_busy(vdev
->queue
, file
))
1214 vdev
->queue
->owner
= file
->private_data
;
1215 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
1216 file
->f_flags
& O_NONBLOCK
);
1217 if (!vdev
->queue
->fileio
)
1218 vdev
->queue
->owner
= NULL
;
1224 EXPORT_SYMBOL_GPL(vb2_fop_read
);
1226 __poll_t
vb2_fop_poll(struct file
*file
, poll_table
*wait
)
1228 struct video_device
*vdev
= video_devdata(file
);
1229 struct vb2_queue
*q
= vdev
->queue
;
1230 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
1235 * If this helper doesn't know how to lock, then you shouldn't be using
1236 * it but you should write your own.
1240 if (lock
&& mutex_lock_interruptible(lock
))
1245 res
= vb2_poll(vdev
->queue
, file
, wait
);
1247 /* If fileio was started, then we have a new queue owner. */
1248 if (!fileio
&& q
->fileio
)
1249 q
->owner
= file
->private_data
;
1254 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
1257 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
1258 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1260 struct video_device
*vdev
= video_devdata(file
);
1262 return vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
1264 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
1267 void vb2_video_unregister_device(struct video_device
*vdev
)
1269 /* Check if vdev was ever registered at all */
1270 if (!vdev
|| !video_is_registered(vdev
))
1274 * Calling this function only makes sense if vdev->queue is set.
1275 * If it is NULL, then just call video_unregister_device() instead.
1277 WARN_ON(!vdev
->queue
);
1280 * Take a reference to the device since video_unregister_device()
1281 * calls device_unregister(), but we don't want that to release
1282 * the device since we want to clean up the queue first.
1284 get_device(&vdev
->dev
);
1285 video_unregister_device(vdev
);
1287 struct mutex
*lock
= vdev
->queue
->lock
?
1288 vdev
->queue
->lock
: vdev
->lock
;
1292 vb2_queue_release(vdev
->queue
);
1293 vdev
->queue
->owner
= NULL
;
1298 * Now we put the device, and in most cases this will release
1301 put_device(&vdev
->dev
);
1303 EXPORT_SYMBOL_GPL(vb2_video_unregister_device
);
1305 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1307 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
1309 mutex_unlock(vq
->lock
);
1311 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
1313 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
1315 mutex_lock(vq
->lock
);
1317 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
1320 * Note that this function is called during validation time and
1321 * thus the req_queue_mutex is held to ensure no request objects
1322 * can be added or deleted while validating. So there is no need
1323 * to protect the objects list.
1325 int vb2_request_validate(struct media_request
*req
)
1327 struct media_request_object
*obj
;
1330 if (!vb2_request_buffer_cnt(req
))
1333 list_for_each_entry(obj
, &req
->objects
, list
) {
1334 if (!obj
->ops
->prepare
)
1337 ret
= obj
->ops
->prepare(obj
);
1343 list_for_each_entry_continue_reverse(obj
, &req
->objects
, list
)
1344 if (obj
->ops
->unprepare
)
1345 obj
->ops
->unprepare(obj
);
1350 EXPORT_SYMBOL_GPL(vb2_request_validate
);
1352 void vb2_request_queue(struct media_request
*req
)
1354 struct media_request_object
*obj
, *obj_safe
;
1357 * Queue all objects. Note that buffer objects are at the end of the
1358 * objects list, after all other object types. Once buffer objects
1359 * are queued, the driver might delete them immediately (if the driver
1360 * processes the buffer at once), so we have to use
1361 * list_for_each_entry_safe() to handle the case where the object we
1364 list_for_each_entry_safe(obj
, obj_safe
, &req
->objects
, list
)
1365 if (obj
->ops
->queue
)
1366 obj
->ops
->queue(obj
);
1368 EXPORT_SYMBOL_GPL(vb2_request_queue
);
1370 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1371 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1372 MODULE_LICENSE("GPL");