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/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 #include <media/v4l2-common.h>
33 #include <media/videobuf2-v4l2.h>
36 module_param(debug
, int, 0644);
38 #define dprintk(level, fmt, arg...) \
41 pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
44 /* Flags that are set by us */
45 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
46 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
47 V4L2_BUF_FLAG_PREPARED | \
48 V4L2_BUF_FLAG_IN_REQUEST | \
49 V4L2_BUF_FLAG_REQUEST_FD | \
50 V4L2_BUF_FLAG_TIMESTAMP_MASK)
51 /* Output buffer flags that should be passed on to the driver */
52 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | \
53 V4L2_BUF_FLAG_BFRAME | \
54 V4L2_BUF_FLAG_KEYFRAME | \
55 V4L2_BUF_FLAG_TIMECODE | \
56 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
59 * __verify_planes_array() - verify that the planes array passed in struct
60 * v4l2_buffer from userspace can be safely used
62 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
64 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
67 /* Is memory for copying plane information present? */
68 if (b
->m
.planes
== NULL
) {
69 dprintk(1, "multi-planar buffer passed but planes array not provided\n");
73 if (b
->length
< vb
->num_planes
|| b
->length
> VB2_MAX_PLANES
) {
74 dprintk(1, "incorrect planes array length, expected %d, got %d\n",
75 vb
->num_planes
, b
->length
);
82 static int __verify_planes_array_core(struct vb2_buffer
*vb
, const void *pb
)
84 return __verify_planes_array(vb
, pb
);
88 * __verify_length() - Verify that the bytesused value for each plane fits in
89 * the plane length and that the data offset doesn't exceed the bytesused value.
91 static int __verify_length(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
94 unsigned int bytesused
;
97 if (!V4L2_TYPE_IS_OUTPUT(b
->type
))
100 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
101 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
102 length
= (b
->memory
== VB2_MEMORY_USERPTR
||
103 b
->memory
== VB2_MEMORY_DMABUF
)
104 ? b
->m
.planes
[plane
].length
105 : vb
->planes
[plane
].length
;
106 bytesused
= b
->m
.planes
[plane
].bytesused
107 ? b
->m
.planes
[plane
].bytesused
: length
;
109 if (b
->m
.planes
[plane
].bytesused
> length
)
112 if (b
->m
.planes
[plane
].data_offset
> 0 &&
113 b
->m
.planes
[plane
].data_offset
>= bytesused
)
117 length
= (b
->memory
== VB2_MEMORY_USERPTR
)
118 ? b
->length
: vb
->planes
[0].length
;
120 if (b
->bytesused
> length
)
128 * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
130 static void __init_vb2_v4l2_buffer(struct vb2_buffer
*vb
)
132 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
134 vbuf
->request_fd
= -1;
137 static void __copy_timestamp(struct vb2_buffer
*vb
, const void *pb
)
139 const struct v4l2_buffer
*b
= pb
;
140 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
141 struct vb2_queue
*q
= vb
->vb2_queue
;
145 * For output buffers copy the timestamp if needed,
146 * and the timecode field and flag if needed.
148 if (q
->copy_timestamp
)
149 vb
->timestamp
= v4l2_buffer_get_timestamp(b
);
150 vbuf
->flags
|= b
->flags
& V4L2_BUF_FLAG_TIMECODE
;
151 if (b
->flags
& V4L2_BUF_FLAG_TIMECODE
)
152 vbuf
->timecode
= b
->timecode
;
156 static void vb2_warn_zero_bytesused(struct vb2_buffer
*vb
)
158 static bool check_once
;
165 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
166 if (vb
->vb2_queue
->allow_zero_bytesused
)
167 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
169 pr_warn("use the actual size instead.\n");
172 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer
*vb
, struct v4l2_buffer
*b
)
174 struct vb2_queue
*q
= vb
->vb2_queue
;
175 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
176 struct vb2_plane
*planes
= vbuf
->planes
;
180 ret
= __verify_length(vb
, b
);
182 dprintk(1, "plane parameters verification failed: %d\n", ret
);
185 if (b
->field
== V4L2_FIELD_ALTERNATE
&& q
->is_output
) {
187 * If the format's field is ALTERNATE, then the buffer's field
188 * should be either TOP or BOTTOM, not ALTERNATE since that
189 * makes no sense. The driver has to know whether the
190 * buffer represents a top or a bottom field in order to
191 * program any DMA correctly. Using ALTERNATE is wrong, since
192 * that just says that it is either a top or a bottom field,
193 * but not which of the two it is.
195 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
199 vbuf
->request_fd
= -1;
200 vbuf
->is_held
= false;
202 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
204 case VB2_MEMORY_USERPTR
:
205 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
206 planes
[plane
].m
.userptr
=
207 b
->m
.planes
[plane
].m
.userptr
;
208 planes
[plane
].length
=
209 b
->m
.planes
[plane
].length
;
212 case VB2_MEMORY_DMABUF
:
213 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
215 b
->m
.planes
[plane
].m
.fd
;
216 planes
[plane
].length
=
217 b
->m
.planes
[plane
].length
;
221 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
222 planes
[plane
].m
.offset
=
223 vb
->planes
[plane
].m
.offset
;
224 planes
[plane
].length
=
225 vb
->planes
[plane
].length
;
230 /* Fill in driver-provided information for OUTPUT types */
231 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
233 * Will have to go up to b->length when API starts
234 * accepting variable number of planes.
236 * If bytesused == 0 for the output buffer, then fall
237 * back to the full buffer size. In that case
238 * userspace clearly never bothered to set it and
239 * it's a safe assumption that they really meant to
240 * use the full plane sizes.
242 * Some drivers, e.g. old codec drivers, use bytesused == 0
243 * as a way to indicate that streaming is finished.
244 * In that case, the driver should use the
245 * allow_zero_bytesused flag to keep old userspace
246 * applications working.
248 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
249 struct vb2_plane
*pdst
= &planes
[plane
];
250 struct v4l2_plane
*psrc
= &b
->m
.planes
[plane
];
252 if (psrc
->bytesused
== 0)
253 vb2_warn_zero_bytesused(vb
);
255 if (vb
->vb2_queue
->allow_zero_bytesused
)
256 pdst
->bytesused
= psrc
->bytesused
;
258 pdst
->bytesused
= psrc
->bytesused
?
259 psrc
->bytesused
: pdst
->length
;
260 pdst
->data_offset
= psrc
->data_offset
;
265 * Single-planar buffers do not use planes array,
266 * so fill in relevant v4l2_buffer struct fields instead.
267 * In videobuf we use our internal V4l2_planes struct for
268 * single-planar buffers as well, for simplicity.
270 * If bytesused == 0 for the output buffer, then fall back
271 * to the full buffer size as that's a sensible default.
273 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
274 * a way to indicate that streaming is finished. In that case,
275 * the driver should use the allow_zero_bytesused flag to keep
276 * old userspace applications working.
279 case VB2_MEMORY_USERPTR
:
280 planes
[0].m
.userptr
= b
->m
.userptr
;
281 planes
[0].length
= b
->length
;
283 case VB2_MEMORY_DMABUF
:
284 planes
[0].m
.fd
= b
->m
.fd
;
285 planes
[0].length
= b
->length
;
288 planes
[0].m
.offset
= vb
->planes
[0].m
.offset
;
289 planes
[0].length
= vb
->planes
[0].length
;
293 planes
[0].data_offset
= 0;
294 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
295 if (b
->bytesused
== 0)
296 vb2_warn_zero_bytesused(vb
);
298 if (vb
->vb2_queue
->allow_zero_bytesused
)
299 planes
[0].bytesused
= b
->bytesused
;
301 planes
[0].bytesused
= b
->bytesused
?
302 b
->bytesused
: planes
[0].length
;
304 planes
[0].bytesused
= 0;
308 /* Zero flags that we handle */
309 vbuf
->flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
310 if (!vb
->vb2_queue
->copy_timestamp
|| !V4L2_TYPE_IS_OUTPUT(b
->type
)) {
312 * Non-COPY timestamps and non-OUTPUT queues will get
313 * their timestamp and timestamp source flags from the
316 vbuf
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
319 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
321 * For output buffers mask out the timecode flag:
322 * this will be handled later in vb2_qbuf().
323 * The 'field' is valid metadata for this output buffer
324 * and so that needs to be copied here.
326 vbuf
->flags
&= ~V4L2_BUF_FLAG_TIMECODE
;
327 vbuf
->field
= b
->field
;
328 if (!(q
->subsystem_flags
& VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF
))
329 vbuf
->flags
&= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF
;
331 /* Zero any output buffer flags as this is a capture buffer */
332 vbuf
->flags
&= ~V4L2_BUFFER_OUT_FLAGS
;
333 /* Zero last flag, this is a signal from driver to userspace */
334 vbuf
->flags
&= ~V4L2_BUF_FLAG_LAST
;
340 static int vb2_queue_or_prepare_buf(struct vb2_queue
*q
, struct media_device
*mdev
,
341 struct v4l2_buffer
*b
, bool is_prepare
,
342 struct media_request
**p_req
)
344 const char *opname
= is_prepare
? "prepare_buf" : "qbuf";
345 struct media_request
*req
;
346 struct vb2_v4l2_buffer
*vbuf
;
347 struct vb2_buffer
*vb
;
350 if (b
->type
!= q
->type
) {
351 dprintk(1, "%s: invalid buffer type\n", opname
);
355 if (b
->index
>= q
->num_buffers
) {
356 dprintk(1, "%s: buffer index out of range\n", opname
);
360 if (q
->bufs
[b
->index
] == NULL
) {
361 /* Should never happen */
362 dprintk(1, "%s: buffer is NULL\n", opname
);
366 if (b
->memory
!= q
->memory
) {
367 dprintk(1, "%s: invalid memory type\n", opname
);
371 vb
= q
->bufs
[b
->index
];
372 vbuf
= to_vb2_v4l2_buffer(vb
);
373 ret
= __verify_planes_array(vb
, b
);
377 if (!is_prepare
&& (b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
) &&
378 vb
->state
!= VB2_BUF_STATE_DEQUEUED
) {
379 dprintk(1, "%s: buffer is not in dequeued state\n", opname
);
384 /* Copy relevant information provided by the userspace */
385 memset(vbuf
->planes
, 0,
386 sizeof(vbuf
->planes
[0]) * vb
->num_planes
);
387 ret
= vb2_fill_vb2_v4l2_buffer(vb
, b
);
395 if (!(b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
)) {
396 if (q
->requires_requests
) {
397 dprintk(1, "%s: queue requires requests\n", opname
);
400 if (q
->uses_requests
) {
401 dprintk(1, "%s: queue uses requests\n", opname
);
405 } else if (!q
->supports_requests
) {
406 dprintk(1, "%s: queue does not support requests\n", opname
);
408 } else if (q
->uses_qbuf
) {
409 dprintk(1, "%s: queue does not use requests\n", opname
);
414 * For proper locking when queueing a request you need to be able
415 * to lock access to the vb2 queue, so check that there is a lock
416 * that we can use. In addition p_req must be non-NULL.
418 if (WARN_ON(!q
->lock
|| !p_req
))
422 * Make sure this op is implemented by the driver. It's easy to forget
423 * this callback, but is it important when canceling a buffer in a
426 if (WARN_ON(!q
->ops
->buf_request_complete
))
429 * Make sure this op is implemented by the driver for the output queue.
430 * It's easy to forget this callback, but is it important to correctly
431 * validate the 'field' value at QBUF time.
433 if (WARN_ON((q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
||
434 q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) &&
435 !q
->ops
->buf_out_validate
))
438 if (b
->request_fd
< 0) {
439 dprintk(1, "%s: request_fd < 0\n", opname
);
443 req
= media_request_get_by_fd(mdev
, b
->request_fd
);
445 dprintk(1, "%s: invalid request_fd\n", opname
);
450 * Early sanity check. This is checked again when the buffer
451 * is bound to the request in vb2_core_qbuf().
453 if (req
->state
!= MEDIA_REQUEST_STATE_IDLE
&&
454 req
->state
!= MEDIA_REQUEST_STATE_UPDATING
) {
455 dprintk(1, "%s: request is not idle\n", opname
);
456 media_request_put(req
);
461 vbuf
->request_fd
= b
->request_fd
;
467 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
468 * returned to userspace
470 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, void *pb
)
472 struct v4l2_buffer
*b
= pb
;
473 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
474 struct vb2_queue
*q
= vb
->vb2_queue
;
477 /* Copy back data such as timestamp, flags, etc. */
478 b
->index
= vb
->index
;
480 b
->memory
= vb
->memory
;
483 b
->flags
= vbuf
->flags
;
484 b
->field
= vbuf
->field
;
485 v4l2_buffer_set_timestamp(b
, vb
->timestamp
);
486 b
->timecode
= vbuf
->timecode
;
487 b
->sequence
= vbuf
->sequence
;
491 if (q
->is_multiplanar
) {
493 * Fill in plane-related data if userspace provided an array
494 * for it. The caller has already verified memory and size.
496 b
->length
= vb
->num_planes
;
497 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
498 struct v4l2_plane
*pdst
= &b
->m
.planes
[plane
];
499 struct vb2_plane
*psrc
= &vb
->planes
[plane
];
501 pdst
->bytesused
= psrc
->bytesused
;
502 pdst
->length
= psrc
->length
;
503 if (q
->memory
== VB2_MEMORY_MMAP
)
504 pdst
->m
.mem_offset
= psrc
->m
.offset
;
505 else if (q
->memory
== VB2_MEMORY_USERPTR
)
506 pdst
->m
.userptr
= psrc
->m
.userptr
;
507 else if (q
->memory
== VB2_MEMORY_DMABUF
)
508 pdst
->m
.fd
= psrc
->m
.fd
;
509 pdst
->data_offset
= psrc
->data_offset
;
510 memset(pdst
->reserved
, 0, sizeof(pdst
->reserved
));
514 * We use length and offset in v4l2_planes array even for
515 * single-planar buffers, but userspace does not.
517 b
->length
= vb
->planes
[0].length
;
518 b
->bytesused
= vb
->planes
[0].bytesused
;
519 if (q
->memory
== VB2_MEMORY_MMAP
)
520 b
->m
.offset
= vb
->planes
[0].m
.offset
;
521 else if (q
->memory
== VB2_MEMORY_USERPTR
)
522 b
->m
.userptr
= vb
->planes
[0].m
.userptr
;
523 else if (q
->memory
== VB2_MEMORY_DMABUF
)
524 b
->m
.fd
= vb
->planes
[0].m
.fd
;
528 * Clear any buffer state related flags.
530 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
531 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
;
532 if (!q
->copy_timestamp
) {
534 * For non-COPY timestamps, drop timestamp source bits
535 * and obtain the timestamp source from the queue.
537 b
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
538 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
542 case VB2_BUF_STATE_QUEUED
:
543 case VB2_BUF_STATE_ACTIVE
:
544 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
546 case VB2_BUF_STATE_IN_REQUEST
:
547 b
->flags
|= V4L2_BUF_FLAG_IN_REQUEST
;
549 case VB2_BUF_STATE_ERROR
:
550 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
552 case VB2_BUF_STATE_DONE
:
553 b
->flags
|= V4L2_BUF_FLAG_DONE
;
555 case VB2_BUF_STATE_PREPARING
:
556 case VB2_BUF_STATE_DEQUEUED
:
561 if ((vb
->state
== VB2_BUF_STATE_DEQUEUED
||
562 vb
->state
== VB2_BUF_STATE_IN_REQUEST
) &&
563 vb
->synced
&& vb
->prepared
)
564 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
566 if (vb2_buffer_in_use(q
, vb
))
567 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
568 if (vbuf
->request_fd
>= 0) {
569 b
->flags
|= V4L2_BUF_FLAG_REQUEST_FD
;
570 b
->request_fd
= vbuf
->request_fd
;
575 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
576 * v4l2_buffer by the userspace. It also verifies that struct
577 * v4l2_buffer has a valid number of planes.
579 static int __fill_vb2_buffer(struct vb2_buffer
*vb
, struct vb2_plane
*planes
)
581 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
584 if (!vb
->vb2_queue
->copy_timestamp
)
587 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
588 if (vb
->vb2_queue
->memory
!= VB2_MEMORY_MMAP
) {
589 planes
[plane
].m
= vbuf
->planes
[plane
].m
;
590 planes
[plane
].length
= vbuf
->planes
[plane
].length
;
592 planes
[plane
].bytesused
= vbuf
->planes
[plane
].bytesused
;
593 planes
[plane
].data_offset
= vbuf
->planes
[plane
].data_offset
;
598 static const struct vb2_buf_ops v4l2_buf_ops
= {
599 .verify_planes_array
= __verify_planes_array_core
,
600 .init_buffer
= __init_vb2_v4l2_buffer
,
601 .fill_user_buffer
= __fill_v4l2_buffer
,
602 .fill_vb2_buffer
= __fill_vb2_buffer
,
603 .copy_timestamp
= __copy_timestamp
,
606 int vb2_find_timestamp(const struct vb2_queue
*q
, u64 timestamp
,
607 unsigned int start_idx
)
611 for (i
= start_idx
; i
< q
->num_buffers
; i
++)
612 if (q
->bufs
[i
]->copied_timestamp
&&
613 q
->bufs
[i
]->timestamp
== timestamp
)
617 EXPORT_SYMBOL_GPL(vb2_find_timestamp
);
620 * vb2_querybuf() - query video buffer information
622 * @b: buffer struct passed from userspace to vidioc_querybuf handler
625 * Should be called from vidioc_querybuf ioctl handler in driver.
626 * This function will verify the passed v4l2_buffer structure and fill the
627 * relevant information for the userspace.
629 * The return values from this function are intended to be directly returned
630 * from vidioc_querybuf handler in driver.
632 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
634 struct vb2_buffer
*vb
;
637 if (b
->type
!= q
->type
) {
638 dprintk(1, "wrong buffer type\n");
642 if (b
->index
>= q
->num_buffers
) {
643 dprintk(1, "buffer index out of range\n");
646 vb
= q
->bufs
[b
->index
];
647 ret
= __verify_planes_array(vb
, b
);
649 vb2_core_querybuf(q
, b
->index
, b
);
652 EXPORT_SYMBOL(vb2_querybuf
);
654 static void fill_buf_caps(struct vb2_queue
*q
, u32
*caps
)
656 *caps
= V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
;
657 if (q
->io_modes
& VB2_MMAP
)
658 *caps
|= V4L2_BUF_CAP_SUPPORTS_MMAP
;
659 if (q
->io_modes
& VB2_USERPTR
)
660 *caps
|= V4L2_BUF_CAP_SUPPORTS_USERPTR
;
661 if (q
->io_modes
& VB2_DMABUF
)
662 *caps
|= V4L2_BUF_CAP_SUPPORTS_DMABUF
;
663 if (q
->subsystem_flags
& VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF
)
664 *caps
|= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF
;
665 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
666 if (q
->supports_requests
)
667 *caps
|= V4L2_BUF_CAP_SUPPORTS_REQUESTS
;
671 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
673 int ret
= vb2_verify_memory_type(q
, req
->memory
, req
->type
);
675 fill_buf_caps(q
, &req
->capabilities
);
676 return ret
? ret
: vb2_core_reqbufs(q
, req
->memory
, &req
->count
);
678 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
680 int vb2_prepare_buf(struct vb2_queue
*q
, struct media_device
*mdev
,
681 struct v4l2_buffer
*b
)
685 if (vb2_fileio_is_active(q
)) {
686 dprintk(1, "file io in progress\n");
690 if (b
->flags
& V4L2_BUF_FLAG_REQUEST_FD
)
693 ret
= vb2_queue_or_prepare_buf(q
, mdev
, b
, true, NULL
);
695 return ret
? ret
: vb2_core_prepare_buf(q
, b
->index
, b
);
697 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
699 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
701 unsigned requested_planes
= 1;
702 unsigned requested_sizes
[VIDEO_MAX_PLANES
];
703 struct v4l2_format
*f
= &create
->format
;
704 int ret
= vb2_verify_memory_type(q
, create
->memory
, f
->type
);
707 fill_buf_caps(q
, &create
->capabilities
);
708 create
->index
= q
->num_buffers
;
709 if (create
->count
== 0)
710 return ret
!= -EBUSY
? ret
: 0;
713 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
714 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
715 requested_planes
= f
->fmt
.pix_mp
.num_planes
;
716 if (requested_planes
== 0 ||
717 requested_planes
> VIDEO_MAX_PLANES
)
719 for (i
= 0; i
< requested_planes
; i
++)
721 f
->fmt
.pix_mp
.plane_fmt
[i
].sizeimage
;
723 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
724 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
725 requested_sizes
[0] = f
->fmt
.pix
.sizeimage
;
727 case V4L2_BUF_TYPE_VBI_CAPTURE
:
728 case V4L2_BUF_TYPE_VBI_OUTPUT
:
729 requested_sizes
[0] = f
->fmt
.vbi
.samples_per_line
*
730 (f
->fmt
.vbi
.count
[0] + f
->fmt
.vbi
.count
[1]);
732 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
:
733 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
:
734 requested_sizes
[0] = f
->fmt
.sliced
.io_size
;
736 case V4L2_BUF_TYPE_SDR_CAPTURE
:
737 case V4L2_BUF_TYPE_SDR_OUTPUT
:
738 requested_sizes
[0] = f
->fmt
.sdr
.buffersize
;
740 case V4L2_BUF_TYPE_META_CAPTURE
:
741 case V4L2_BUF_TYPE_META_OUTPUT
:
742 requested_sizes
[0] = f
->fmt
.meta
.buffersize
;
747 for (i
= 0; i
< requested_planes
; i
++)
748 if (requested_sizes
[i
] == 0)
750 return ret
? ret
: vb2_core_create_bufs(q
, create
->memory
,
751 &create
->count
, requested_planes
, requested_sizes
);
753 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
755 int vb2_qbuf(struct vb2_queue
*q
, struct media_device
*mdev
,
756 struct v4l2_buffer
*b
)
758 struct media_request
*req
= NULL
;
761 if (vb2_fileio_is_active(q
)) {
762 dprintk(1, "file io in progress\n");
766 ret
= vb2_queue_or_prepare_buf(q
, mdev
, b
, false, &req
);
769 ret
= vb2_core_qbuf(q
, b
->index
, b
, req
);
771 media_request_put(req
);
774 EXPORT_SYMBOL_GPL(vb2_qbuf
);
776 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
780 if (vb2_fileio_is_active(q
)) {
781 dprintk(1, "file io in progress\n");
785 if (b
->type
!= q
->type
) {
786 dprintk(1, "invalid buffer type\n");
790 ret
= vb2_core_dqbuf(q
, NULL
, b
, nonblocking
);
793 b
->flags
& V4L2_BUF_FLAG_DONE
&&
794 b
->flags
& V4L2_BUF_FLAG_LAST
)
795 q
->last_buffer_dequeued
= true;
798 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
801 b
->flags
&= ~V4L2_BUF_FLAG_DONE
;
805 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
807 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
809 if (vb2_fileio_is_active(q
)) {
810 dprintk(1, "file io in progress\n");
813 return vb2_core_streamon(q
, type
);
815 EXPORT_SYMBOL_GPL(vb2_streamon
);
817 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
819 if (vb2_fileio_is_active(q
)) {
820 dprintk(1, "file io in progress\n");
823 return vb2_core_streamoff(q
, type
);
825 EXPORT_SYMBOL_GPL(vb2_streamoff
);
827 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
829 return vb2_core_expbuf(q
, &eb
->fd
, eb
->type
, eb
->index
,
830 eb
->plane
, eb
->flags
);
832 EXPORT_SYMBOL_GPL(vb2_expbuf
);
834 int vb2_queue_init(struct vb2_queue
*q
)
840 WARN_ON(q
->timestamp_flags
&
841 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK
|
842 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
)))
845 /* Warn that the driver should choose an appropriate timestamp type */
846 WARN_ON((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
847 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN
);
849 /* Warn that vb2_memory should match with v4l2_memory */
850 if (WARN_ON(VB2_MEMORY_MMAP
!= (int)V4L2_MEMORY_MMAP
)
851 || WARN_ON(VB2_MEMORY_USERPTR
!= (int)V4L2_MEMORY_USERPTR
)
852 || WARN_ON(VB2_MEMORY_DMABUF
!= (int)V4L2_MEMORY_DMABUF
))
855 if (q
->buf_struct_size
== 0)
856 q
->buf_struct_size
= sizeof(struct vb2_v4l2_buffer
);
858 q
->buf_ops
= &v4l2_buf_ops
;
859 q
->is_multiplanar
= V4L2_TYPE_IS_MULTIPLANAR(q
->type
);
860 q
->is_output
= V4L2_TYPE_IS_OUTPUT(q
->type
);
861 q
->copy_timestamp
= (q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
)
862 == V4L2_BUF_FLAG_TIMESTAMP_COPY
;
864 * For compatibility with vb1: if QBUF hasn't been called yet, then
865 * return EPOLLERR as well. This only affects capture queues, output
866 * queues will always initialize waiting_for_buffers to false.
868 q
->quirk_poll_must_check_waiting_for_buffers
= true;
870 return vb2_core_queue_init(q
);
872 EXPORT_SYMBOL_GPL(vb2_queue_init
);
874 void vb2_queue_release(struct vb2_queue
*q
)
876 vb2_core_queue_release(q
);
878 EXPORT_SYMBOL_GPL(vb2_queue_release
);
880 __poll_t
vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
882 struct video_device
*vfd
= video_devdata(file
);
885 res
= vb2_core_poll(q
, file
, wait
);
887 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
888 struct v4l2_fh
*fh
= file
->private_data
;
890 poll_wait(file
, &fh
->wait
, wait
);
891 if (v4l2_event_pending(fh
))
897 EXPORT_SYMBOL_GPL(vb2_poll
);
900 * The following functions are not part of the vb2 core API, but are helper
901 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
902 * and struct vb2_ops.
903 * They contain boilerplate code that most if not all drivers have to do
904 * and so they simplify the driver code.
907 /* The queue is busy if there is a owner and you are not that owner. */
908 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
910 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
913 /* vb2 ioctl helpers */
915 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
916 struct v4l2_requestbuffers
*p
)
918 struct video_device
*vdev
= video_devdata(file
);
919 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
921 fill_buf_caps(vdev
->queue
, &p
->capabilities
);
924 if (vb2_queue_is_busy(vdev
, file
))
926 res
= vb2_core_reqbufs(vdev
->queue
, p
->memory
, &p
->count
);
927 /* If count == 0, then the owner has released all buffers and he
928 is no longer owner of the queue. Otherwise we have a new owner. */
930 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
933 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
935 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
936 struct v4l2_create_buffers
*p
)
938 struct video_device
*vdev
= video_devdata(file
);
939 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
,
942 p
->index
= vdev
->queue
->num_buffers
;
943 fill_buf_caps(vdev
->queue
, &p
->capabilities
);
945 * If count == 0, then just check if memory and type are valid.
946 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
949 return res
!= -EBUSY
? res
: 0;
952 if (vb2_queue_is_busy(vdev
, file
))
955 res
= vb2_create_bufs(vdev
->queue
, p
);
957 vdev
->queue
->owner
= file
->private_data
;
960 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
962 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
963 struct v4l2_buffer
*p
)
965 struct video_device
*vdev
= video_devdata(file
);
967 if (vb2_queue_is_busy(vdev
, file
))
969 return vb2_prepare_buf(vdev
->queue
, vdev
->v4l2_dev
->mdev
, p
);
971 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
973 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
975 struct video_device
*vdev
= video_devdata(file
);
977 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
978 return vb2_querybuf(vdev
->queue
, p
);
980 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
982 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
984 struct video_device
*vdev
= video_devdata(file
);
986 if (vb2_queue_is_busy(vdev
, file
))
988 return vb2_qbuf(vdev
->queue
, vdev
->v4l2_dev
->mdev
, p
);
990 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
992 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
994 struct video_device
*vdev
= video_devdata(file
);
996 if (vb2_queue_is_busy(vdev
, file
))
998 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
1000 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
1002 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1004 struct video_device
*vdev
= video_devdata(file
);
1006 if (vb2_queue_is_busy(vdev
, file
))
1008 return vb2_streamon(vdev
->queue
, i
);
1010 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
1012 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1014 struct video_device
*vdev
= video_devdata(file
);
1016 if (vb2_queue_is_busy(vdev
, file
))
1018 return vb2_streamoff(vdev
->queue
, i
);
1020 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
1022 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
1024 struct video_device
*vdev
= video_devdata(file
);
1026 if (vb2_queue_is_busy(vdev
, file
))
1028 return vb2_expbuf(vdev
->queue
, p
);
1030 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
1032 /* v4l2_file_operations helpers */
1034 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1036 struct video_device
*vdev
= video_devdata(file
);
1038 return vb2_mmap(vdev
->queue
, vma
);
1040 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
1042 int _vb2_fop_release(struct file
*file
, struct mutex
*lock
)
1044 struct video_device
*vdev
= video_devdata(file
);
1048 if (file
->private_data
== vdev
->queue
->owner
) {
1049 vb2_queue_release(vdev
->queue
);
1050 vdev
->queue
->owner
= NULL
;
1054 return v4l2_fh_release(file
);
1056 EXPORT_SYMBOL_GPL(_vb2_fop_release
);
1058 int vb2_fop_release(struct file
*file
)
1060 struct video_device
*vdev
= video_devdata(file
);
1061 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1063 return _vb2_fop_release(file
, lock
);
1065 EXPORT_SYMBOL_GPL(vb2_fop_release
);
1067 ssize_t
vb2_fop_write(struct file
*file
, const char __user
*buf
,
1068 size_t count
, loff_t
*ppos
)
1070 struct video_device
*vdev
= video_devdata(file
);
1071 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1074 if (!(vdev
->queue
->io_modes
& VB2_WRITE
))
1076 if (lock
&& mutex_lock_interruptible(lock
))
1077 return -ERESTARTSYS
;
1078 if (vb2_queue_is_busy(vdev
, file
))
1080 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
1081 file
->f_flags
& O_NONBLOCK
);
1082 if (vdev
->queue
->fileio
)
1083 vdev
->queue
->owner
= file
->private_data
;
1089 EXPORT_SYMBOL_GPL(vb2_fop_write
);
1091 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
1092 size_t count
, loff_t
*ppos
)
1094 struct video_device
*vdev
= video_devdata(file
);
1095 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1098 if (!(vdev
->queue
->io_modes
& VB2_READ
))
1100 if (lock
&& mutex_lock_interruptible(lock
))
1101 return -ERESTARTSYS
;
1102 if (vb2_queue_is_busy(vdev
, file
))
1104 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
1105 file
->f_flags
& O_NONBLOCK
);
1106 if (vdev
->queue
->fileio
)
1107 vdev
->queue
->owner
= file
->private_data
;
1113 EXPORT_SYMBOL_GPL(vb2_fop_read
);
1115 __poll_t
vb2_fop_poll(struct file
*file
, poll_table
*wait
)
1117 struct video_device
*vdev
= video_devdata(file
);
1118 struct vb2_queue
*q
= vdev
->queue
;
1119 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
1124 * If this helper doesn't know how to lock, then you shouldn't be using
1125 * it but you should write your own.
1129 if (lock
&& mutex_lock_interruptible(lock
))
1134 res
= vb2_poll(vdev
->queue
, file
, wait
);
1136 /* If fileio was started, then we have a new queue owner. */
1137 if (!fileio
&& q
->fileio
)
1138 q
->owner
= file
->private_data
;
1143 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
1146 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
1147 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1149 struct video_device
*vdev
= video_devdata(file
);
1151 return vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
1153 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
1156 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1158 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
1160 mutex_unlock(vq
->lock
);
1162 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
1164 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
1166 mutex_lock(vq
->lock
);
1168 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
1171 * Note that this function is called during validation time and
1172 * thus the req_queue_mutex is held to ensure no request objects
1173 * can be added or deleted while validating. So there is no need
1174 * to protect the objects list.
1176 int vb2_request_validate(struct media_request
*req
)
1178 struct media_request_object
*obj
;
1181 if (!vb2_request_buffer_cnt(req
))
1184 list_for_each_entry(obj
, &req
->objects
, list
) {
1185 if (!obj
->ops
->prepare
)
1188 ret
= obj
->ops
->prepare(obj
);
1194 list_for_each_entry_continue_reverse(obj
, &req
->objects
, list
)
1195 if (obj
->ops
->unprepare
)
1196 obj
->ops
->unprepare(obj
);
1201 EXPORT_SYMBOL_GPL(vb2_request_validate
);
1203 void vb2_request_queue(struct media_request
*req
)
1205 struct media_request_object
*obj
, *obj_safe
;
1208 * Queue all objects. Note that buffer objects are at the end of the
1209 * objects list, after all other object types. Once buffer objects
1210 * are queued, the driver might delete them immediately (if the driver
1211 * processes the buffer at once), so we have to use
1212 * list_for_each_entry_safe() to handle the case where the object we
1215 list_for_each_entry_safe(obj
, obj_safe
, &req
->objects
, list
)
1216 if (obj
->ops
->queue
)
1217 obj
->ops
->queue(obj
);
1219 EXPORT_SYMBOL_GPL(vb2_request_queue
);
1221 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1222 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1223 MODULE_LICENSE("GPL");