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-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-common.h>
32 #include <media/videobuf2-v4l2.h>
35 module_param(debug
, int, 0644);
37 #define dprintk(level, fmt, arg...) \
40 pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
43 /* Flags that are set by the vb2 core */
44 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
45 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
46 V4L2_BUF_FLAG_PREPARED | \
47 V4L2_BUF_FLAG_TIMESTAMP_MASK)
48 /* Output buffer flags that should be passed on to the driver */
49 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
50 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
53 * __verify_planes_array() - verify that the planes array passed in struct
54 * v4l2_buffer from userspace can be safely used
56 static int __verify_planes_array(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
58 if (!V4L2_TYPE_IS_MULTIPLANAR(b
->type
))
61 /* Is memory for copying plane information present? */
62 if (b
->m
.planes
== NULL
) {
63 dprintk(1, "multi-planar buffer passed but "
64 "planes array not provided\n");
68 if (b
->length
< vb
->num_planes
|| b
->length
> VB2_MAX_PLANES
) {
69 dprintk(1, "incorrect planes array length, "
70 "expected %d, got %d\n", vb
->num_planes
, b
->length
);
78 * __verify_length() - Verify that the bytesused value for each plane fits in
79 * the plane length and that the data offset doesn't exceed the bytesused value.
81 static int __verify_length(struct vb2_buffer
*vb
, const struct v4l2_buffer
*b
)
84 unsigned int bytesused
;
87 if (!V4L2_TYPE_IS_OUTPUT(b
->type
))
90 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
91 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
92 length
= (b
->memory
== VB2_MEMORY_USERPTR
||
93 b
->memory
== VB2_MEMORY_DMABUF
)
94 ? b
->m
.planes
[plane
].length
95 : vb
->planes
[plane
].length
;
96 bytesused
= b
->m
.planes
[plane
].bytesused
97 ? b
->m
.planes
[plane
].bytesused
: length
;
99 if (b
->m
.planes
[plane
].bytesused
> length
)
102 if (b
->m
.planes
[plane
].data_offset
> 0 &&
103 b
->m
.planes
[plane
].data_offset
>= bytesused
)
107 length
= (b
->memory
== VB2_MEMORY_USERPTR
)
108 ? b
->length
: vb
->planes
[0].length
;
110 if (b
->bytesused
> length
)
117 static void __copy_timestamp(struct vb2_buffer
*vb
, const void *pb
)
119 const struct v4l2_buffer
*b
= pb
;
120 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
121 struct vb2_queue
*q
= vb
->vb2_queue
;
125 * For output buffers copy the timestamp if needed,
126 * and the timecode field and flag if needed.
128 if (q
->copy_timestamp
)
129 vb
->timestamp
= timeval_to_ns(&b
->timestamp
);
130 vbuf
->flags
|= b
->flags
& V4L2_BUF_FLAG_TIMECODE
;
131 if (b
->flags
& V4L2_BUF_FLAG_TIMECODE
)
132 vbuf
->timecode
= b
->timecode
;
136 static void vb2_warn_zero_bytesused(struct vb2_buffer
*vb
)
138 static bool check_once
;
146 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
147 if (vb
->vb2_queue
->allow_zero_bytesused
)
148 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
150 pr_warn("use the actual size instead.\n");
153 static int vb2_queue_or_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
,
156 if (b
->type
!= q
->type
) {
157 dprintk(1, "%s: invalid buffer type\n", opname
);
161 if (b
->index
>= q
->num_buffers
) {
162 dprintk(1, "%s: buffer index out of range\n", opname
);
166 if (q
->bufs
[b
->index
] == NULL
) {
167 /* Should never happen */
168 dprintk(1, "%s: buffer is NULL\n", opname
);
172 if (b
->memory
!= q
->memory
) {
173 dprintk(1, "%s: invalid memory type\n", opname
);
177 return __verify_planes_array(q
->bufs
[b
->index
], b
);
181 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
182 * returned to userspace
184 static void __fill_v4l2_buffer(struct vb2_buffer
*vb
, void *pb
)
186 struct v4l2_buffer
*b
= pb
;
187 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
188 struct vb2_queue
*q
= vb
->vb2_queue
;
191 /* Copy back data such as timestamp, flags, etc. */
192 b
->index
= vb
->index
;
194 b
->memory
= vb
->memory
;
197 b
->flags
= vbuf
->flags
;
198 b
->field
= vbuf
->field
;
199 b
->timestamp
= ns_to_timeval(vb
->timestamp
);
200 b
->timecode
= vbuf
->timecode
;
201 b
->sequence
= vbuf
->sequence
;
205 if (q
->is_multiplanar
) {
207 * Fill in plane-related data if userspace provided an array
208 * for it. The caller has already verified memory and size.
210 b
->length
= vb
->num_planes
;
211 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
212 struct v4l2_plane
*pdst
= &b
->m
.planes
[plane
];
213 struct vb2_plane
*psrc
= &vb
->planes
[plane
];
215 pdst
->bytesused
= psrc
->bytesused
;
216 pdst
->length
= psrc
->length
;
217 if (q
->memory
== VB2_MEMORY_MMAP
)
218 pdst
->m
.mem_offset
= psrc
->m
.offset
;
219 else if (q
->memory
== VB2_MEMORY_USERPTR
)
220 pdst
->m
.userptr
= psrc
->m
.userptr
;
221 else if (q
->memory
== VB2_MEMORY_DMABUF
)
222 pdst
->m
.fd
= psrc
->m
.fd
;
223 pdst
->data_offset
= psrc
->data_offset
;
224 memset(pdst
->reserved
, 0, sizeof(pdst
->reserved
));
228 * We use length and offset in v4l2_planes array even for
229 * single-planar buffers, but userspace does not.
231 b
->length
= vb
->planes
[0].length
;
232 b
->bytesused
= vb
->planes
[0].bytesused
;
233 if (q
->memory
== VB2_MEMORY_MMAP
)
234 b
->m
.offset
= vb
->planes
[0].m
.offset
;
235 else if (q
->memory
== VB2_MEMORY_USERPTR
)
236 b
->m
.userptr
= vb
->planes
[0].m
.userptr
;
237 else if (q
->memory
== VB2_MEMORY_DMABUF
)
238 b
->m
.fd
= vb
->planes
[0].m
.fd
;
242 * Clear any buffer state related flags.
244 b
->flags
&= ~V4L2_BUFFER_MASK_FLAGS
;
245 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
;
246 if (!q
->copy_timestamp
) {
248 * For non-COPY timestamps, drop timestamp source bits
249 * and obtain the timestamp source from the queue.
251 b
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
252 b
->flags
|= q
->timestamp_flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
256 case VB2_BUF_STATE_QUEUED
:
257 case VB2_BUF_STATE_ACTIVE
:
258 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
260 case VB2_BUF_STATE_ERROR
:
261 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
263 case VB2_BUF_STATE_DONE
:
264 b
->flags
|= V4L2_BUF_FLAG_DONE
;
266 case VB2_BUF_STATE_PREPARED
:
267 b
->flags
|= V4L2_BUF_FLAG_PREPARED
;
269 case VB2_BUF_STATE_PREPARING
:
270 case VB2_BUF_STATE_DEQUEUED
:
271 case VB2_BUF_STATE_REQUEUEING
:
276 if (vb2_buffer_in_use(q
, vb
))
277 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
280 b
->flags
& V4L2_BUF_FLAG_DONE
&&
281 b
->flags
& V4L2_BUF_FLAG_LAST
)
282 q
->last_buffer_dequeued
= true;
286 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
287 * v4l2_buffer by the userspace. It also verifies that struct
288 * v4l2_buffer has a valid number of planes.
290 static int __fill_vb2_buffer(struct vb2_buffer
*vb
,
291 const void *pb
, struct vb2_plane
*planes
)
293 struct vb2_queue
*q
= vb
->vb2_queue
;
294 const struct v4l2_buffer
*b
= pb
;
295 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
299 ret
= __verify_length(vb
, b
);
301 dprintk(1, "plane parameters verification failed: %d\n", ret
);
304 if (b
->field
== V4L2_FIELD_ALTERNATE
&& q
->is_output
) {
306 * If the format's field is ALTERNATE, then the buffer's field
307 * should be either TOP or BOTTOM, not ALTERNATE since that
308 * makes no sense. The driver has to know whether the
309 * buffer represents a top or a bottom field in order to
310 * program any DMA correctly. Using ALTERNATE is wrong, since
311 * that just says that it is either a top or a bottom field,
312 * but not which of the two it is.
314 dprintk(1, "the field is incorrectly set to ALTERNATE "
315 "for an output buffer\n");
321 if (V4L2_TYPE_IS_MULTIPLANAR(b
->type
)) {
322 if (b
->memory
== VB2_MEMORY_USERPTR
) {
323 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
324 planes
[plane
].m
.userptr
=
325 b
->m
.planes
[plane
].m
.userptr
;
326 planes
[plane
].length
=
327 b
->m
.planes
[plane
].length
;
330 if (b
->memory
== VB2_MEMORY_DMABUF
) {
331 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
333 b
->m
.planes
[plane
].m
.fd
;
334 planes
[plane
].length
=
335 b
->m
.planes
[plane
].length
;
339 /* Fill in driver-provided information for OUTPUT types */
340 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
342 * Will have to go up to b->length when API starts
343 * accepting variable number of planes.
345 * If bytesused == 0 for the output buffer, then fall
346 * back to the full buffer size. In that case
347 * userspace clearly never bothered to set it and
348 * it's a safe assumption that they really meant to
349 * use the full plane sizes.
351 * Some drivers, e.g. old codec drivers, use bytesused == 0
352 * as a way to indicate that streaming is finished.
353 * In that case, the driver should use the
354 * allow_zero_bytesused flag to keep old userspace
355 * applications working.
357 for (plane
= 0; plane
< vb
->num_planes
; ++plane
) {
358 struct vb2_plane
*pdst
= &planes
[plane
];
359 struct v4l2_plane
*psrc
= &b
->m
.planes
[plane
];
361 if (psrc
->bytesused
== 0)
362 vb2_warn_zero_bytesused(vb
);
364 if (vb
->vb2_queue
->allow_zero_bytesused
)
365 pdst
->bytesused
= psrc
->bytesused
;
367 pdst
->bytesused
= psrc
->bytesused
?
368 psrc
->bytesused
: pdst
->length
;
369 pdst
->data_offset
= psrc
->data_offset
;
374 * Single-planar buffers do not use planes array,
375 * so fill in relevant v4l2_buffer struct fields instead.
376 * In videobuf we use our internal V4l2_planes struct for
377 * single-planar buffers as well, for simplicity.
379 * If bytesused == 0 for the output buffer, then fall back
380 * to the full buffer size as that's a sensible default.
382 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
383 * a way to indicate that streaming is finished. In that case,
384 * the driver should use the allow_zero_bytesused flag to keep
385 * old userspace applications working.
387 if (b
->memory
== VB2_MEMORY_USERPTR
) {
388 planes
[0].m
.userptr
= b
->m
.userptr
;
389 planes
[0].length
= b
->length
;
392 if (b
->memory
== VB2_MEMORY_DMABUF
) {
393 planes
[0].m
.fd
= b
->m
.fd
;
394 planes
[0].length
= b
->length
;
397 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
398 if (b
->bytesused
== 0)
399 vb2_warn_zero_bytesused(vb
);
401 if (vb
->vb2_queue
->allow_zero_bytesused
)
402 planes
[0].bytesused
= b
->bytesused
;
404 planes
[0].bytesused
= b
->bytesused
?
405 b
->bytesused
: planes
[0].length
;
407 planes
[0].bytesused
= 0;
411 /* Zero flags that the vb2 core handles */
412 vbuf
->flags
= b
->flags
& ~V4L2_BUFFER_MASK_FLAGS
;
413 if (!vb
->vb2_queue
->copy_timestamp
|| !V4L2_TYPE_IS_OUTPUT(b
->type
)) {
415 * Non-COPY timestamps and non-OUTPUT queues will get
416 * their timestamp and timestamp source flags from the
419 vbuf
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
422 if (V4L2_TYPE_IS_OUTPUT(b
->type
)) {
424 * For output buffers mask out the timecode flag:
425 * this will be handled later in vb2_internal_qbuf().
426 * The 'field' is valid metadata for this output buffer
427 * and so that needs to be copied here.
429 vbuf
->flags
&= ~V4L2_BUF_FLAG_TIMECODE
;
430 vbuf
->field
= b
->field
;
432 /* Zero any output buffer flags as this is a capture buffer */
433 vbuf
->flags
&= ~V4L2_BUFFER_OUT_FLAGS
;
439 static const struct vb2_buf_ops v4l2_buf_ops
= {
440 .fill_user_buffer
= __fill_v4l2_buffer
,
441 .fill_vb2_buffer
= __fill_vb2_buffer
,
442 .copy_timestamp
= __copy_timestamp
,
446 * vb2_querybuf() - query video buffer information
448 * @b: buffer struct passed from userspace to vidioc_querybuf handler
451 * Should be called from vidioc_querybuf ioctl handler in driver.
452 * This function will verify the passed v4l2_buffer structure and fill the
453 * relevant information for the userspace.
455 * The return values from this function are intended to be directly returned
456 * from vidioc_querybuf handler in driver.
458 int vb2_querybuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
460 struct vb2_buffer
*vb
;
463 if (b
->type
!= q
->type
) {
464 dprintk(1, "wrong buffer type\n");
468 if (b
->index
>= q
->num_buffers
) {
469 dprintk(1, "buffer index out of range\n");
472 vb
= q
->bufs
[b
->index
];
473 ret
= __verify_planes_array(vb
, b
);
475 vb2_core_querybuf(q
, b
->index
, b
);
478 EXPORT_SYMBOL(vb2_querybuf
);
481 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
482 * the memory and type values.
483 * @q: videobuf2 queue
484 * @req: struct passed from userspace to vidioc_reqbufs handler
487 int vb2_reqbufs(struct vb2_queue
*q
, struct v4l2_requestbuffers
*req
)
489 int ret
= vb2_verify_memory_type(q
, req
->memory
, req
->type
);
491 return ret
? ret
: vb2_core_reqbufs(q
, req
->memory
, &req
->count
);
493 EXPORT_SYMBOL_GPL(vb2_reqbufs
);
496 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
497 * @q: videobuf2 queue
498 * @b: buffer structure passed from userspace to vidioc_prepare_buf
501 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
503 * 1) verifies the passed buffer,
504 * 2) calls buf_prepare callback in the driver (if provided), in which
505 * driver-specific buffer initialization can be performed,
507 * The return values from this function are intended to be directly returned
508 * from vidioc_prepare_buf handler in driver.
510 int vb2_prepare_buf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
514 if (vb2_fileio_is_active(q
)) {
515 dprintk(1, "file io in progress\n");
519 ret
= vb2_queue_or_prepare_buf(q
, b
, "prepare_buf");
521 return ret
? ret
: vb2_core_prepare_buf(q
, b
->index
, b
);
523 EXPORT_SYMBOL_GPL(vb2_prepare_buf
);
526 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
527 * the memory and type values.
528 * @q: videobuf2 queue
529 * @create: creation parameters, passed from userspace to vidioc_create_bufs
532 int vb2_create_bufs(struct vb2_queue
*q
, struct v4l2_create_buffers
*create
)
534 unsigned requested_planes
= 1;
535 unsigned requested_sizes
[VIDEO_MAX_PLANES
];
536 struct v4l2_format
*f
= &create
->format
;
537 int ret
= vb2_verify_memory_type(q
, create
->memory
, f
->type
);
540 create
->index
= q
->num_buffers
;
541 if (create
->count
== 0)
542 return ret
!= -EBUSY
? ret
: 0;
545 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
546 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
547 requested_planes
= f
->fmt
.pix_mp
.num_planes
;
548 if (requested_planes
== 0 ||
549 requested_planes
> VIDEO_MAX_PLANES
)
551 for (i
= 0; i
< requested_planes
; i
++)
553 f
->fmt
.pix_mp
.plane_fmt
[i
].sizeimage
;
555 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
556 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
557 requested_sizes
[0] = f
->fmt
.pix
.sizeimage
;
559 case V4L2_BUF_TYPE_VBI_CAPTURE
:
560 case V4L2_BUF_TYPE_VBI_OUTPUT
:
561 requested_sizes
[0] = f
->fmt
.vbi
.samples_per_line
*
562 (f
->fmt
.vbi
.count
[0] + f
->fmt
.vbi
.count
[1]);
564 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
:
565 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
:
566 requested_sizes
[0] = f
->fmt
.sliced
.io_size
;
568 case V4L2_BUF_TYPE_SDR_CAPTURE
:
569 case V4L2_BUF_TYPE_SDR_OUTPUT
:
570 requested_sizes
[0] = f
->fmt
.sdr
.buffersize
;
575 for (i
= 0; i
< requested_planes
; i
++)
576 if (requested_sizes
[i
] == 0)
578 return ret
? ret
: vb2_core_create_bufs(q
, create
->memory
,
579 &create
->count
, requested_planes
, requested_sizes
);
581 EXPORT_SYMBOL_GPL(vb2_create_bufs
);
583 static int vb2_internal_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
585 int ret
= vb2_queue_or_prepare_buf(q
, b
, "qbuf");
587 return ret
? ret
: vb2_core_qbuf(q
, b
->index
, b
);
591 * vb2_qbuf() - Queue a buffer from userspace
592 * @q: videobuf2 queue
593 * @b: buffer structure passed from userspace to vidioc_qbuf handler
596 * Should be called from vidioc_qbuf ioctl handler of a driver.
598 * 1) verifies the passed buffer,
599 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
600 * which driver-specific buffer initialization can be performed,
601 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
602 * callback for processing.
604 * The return values from this function are intended to be directly returned
605 * from vidioc_qbuf handler in driver.
607 int vb2_qbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
)
609 if (vb2_fileio_is_active(q
)) {
610 dprintk(1, "file io in progress\n");
614 return vb2_internal_qbuf(q
, b
);
616 EXPORT_SYMBOL_GPL(vb2_qbuf
);
618 static int vb2_internal_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
,
623 if (b
->type
!= q
->type
) {
624 dprintk(1, "invalid buffer type\n");
628 ret
= vb2_core_dqbuf(q
, NULL
, b
, nonblocking
);
634 * vb2_dqbuf() - Dequeue a buffer to the userspace
635 * @q: videobuf2 queue
636 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
638 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
639 * buffers ready for dequeuing are present. Normally the driver
640 * would be passing (file->f_flags & O_NONBLOCK) here
642 * Should be called from vidioc_dqbuf ioctl handler of a driver.
644 * 1) verifies the passed buffer,
645 * 2) calls buf_finish callback in the driver (if provided), in which
646 * driver can perform any additional operations that may be required before
647 * returning the buffer to userspace, such as cache sync,
648 * 3) the buffer struct members are filled with relevant information for
651 * The return values from this function are intended to be directly returned
652 * from vidioc_dqbuf handler in driver.
654 int vb2_dqbuf(struct vb2_queue
*q
, struct v4l2_buffer
*b
, bool nonblocking
)
656 if (vb2_fileio_is_active(q
)) {
657 dprintk(1, "file io in progress\n");
660 return vb2_internal_dqbuf(q
, b
, nonblocking
);
662 EXPORT_SYMBOL_GPL(vb2_dqbuf
);
665 * vb2_streamon - start streaming
666 * @q: videobuf2 queue
667 * @type: type argument passed from userspace to vidioc_streamon handler
669 * Should be called from vidioc_streamon handler of a driver.
671 * 1) verifies current state
672 * 2) passes any previously queued buffers to the driver and starts streaming
674 * The return values from this function are intended to be directly returned
675 * from vidioc_streamon handler in the driver.
677 int vb2_streamon(struct vb2_queue
*q
, enum v4l2_buf_type type
)
679 if (vb2_fileio_is_active(q
)) {
680 dprintk(1, "file io in progress\n");
683 return vb2_core_streamon(q
, type
);
685 EXPORT_SYMBOL_GPL(vb2_streamon
);
688 * vb2_streamoff - stop streaming
689 * @q: videobuf2 queue
690 * @type: type argument passed from userspace to vidioc_streamoff handler
692 * Should be called from vidioc_streamoff handler of a driver.
694 * 1) verifies current state,
695 * 2) stop streaming and dequeues any queued buffers, including those previously
696 * passed to the driver (after waiting for the driver to finish).
698 * This call can be used for pausing playback.
699 * The return values from this function are intended to be directly returned
700 * from vidioc_streamoff handler in the driver
702 int vb2_streamoff(struct vb2_queue
*q
, enum v4l2_buf_type type
)
704 if (vb2_fileio_is_active(q
)) {
705 dprintk(1, "file io in progress\n");
708 return vb2_core_streamoff(q
, type
);
710 EXPORT_SYMBOL_GPL(vb2_streamoff
);
713 * vb2_expbuf() - Export a buffer as a file descriptor
714 * @q: videobuf2 queue
715 * @eb: export buffer structure passed from userspace to vidioc_expbuf
718 * The return values from this function are intended to be directly returned
719 * from vidioc_expbuf handler in driver.
721 int vb2_expbuf(struct vb2_queue
*q
, struct v4l2_exportbuffer
*eb
)
723 return vb2_core_expbuf(q
, &eb
->fd
, eb
->type
, eb
->index
,
724 eb
->plane
, eb
->flags
);
726 EXPORT_SYMBOL_GPL(vb2_expbuf
);
729 * vb2_queue_init() - initialize a videobuf2 queue
730 * @q: videobuf2 queue; this structure should be allocated in driver
732 * The vb2_queue structure should be allocated by the driver. The driver is
733 * responsible of clearing it's content and setting initial values for some
734 * required entries before calling this function.
735 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
736 * to the struct vb2_queue description in include/media/videobuf2-core.h
737 * for more information.
739 int vb2_queue_init(struct vb2_queue
*q
)
745 WARN_ON(q
->timestamp_flags
&
746 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK
|
747 V4L2_BUF_FLAG_TSTAMP_SRC_MASK
)))
750 /* Warn that the driver should choose an appropriate timestamp type */
751 WARN_ON((q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
) ==
752 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN
);
754 /* Warn that vb2_memory should match with v4l2_memory */
755 if (WARN_ON(VB2_MEMORY_MMAP
!= (int)V4L2_MEMORY_MMAP
)
756 || WARN_ON(VB2_MEMORY_USERPTR
!= (int)V4L2_MEMORY_USERPTR
)
757 || WARN_ON(VB2_MEMORY_DMABUF
!= (int)V4L2_MEMORY_DMABUF
))
760 if (q
->buf_struct_size
== 0)
761 q
->buf_struct_size
= sizeof(struct vb2_v4l2_buffer
);
763 q
->buf_ops
= &v4l2_buf_ops
;
764 q
->is_multiplanar
= V4L2_TYPE_IS_MULTIPLANAR(q
->type
);
765 q
->is_output
= V4L2_TYPE_IS_OUTPUT(q
->type
);
766 q
->copy_timestamp
= (q
->timestamp_flags
& V4L2_BUF_FLAG_TIMESTAMP_MASK
)
767 == V4L2_BUF_FLAG_TIMESTAMP_COPY
;
769 return vb2_core_queue_init(q
);
771 EXPORT_SYMBOL_GPL(vb2_queue_init
);
774 * vb2_queue_release() - stop streaming, release the queue and free memory
775 * @q: videobuf2 queue
777 * This function stops streaming and performs necessary clean ups, including
778 * freeing video buffer memory. The driver is responsible for freeing
779 * the vb2_queue structure itself.
781 void vb2_queue_release(struct vb2_queue
*q
)
783 vb2_core_queue_release(q
);
785 EXPORT_SYMBOL_GPL(vb2_queue_release
);
788 * vb2_poll() - implements poll userspace operation
789 * @q: videobuf2 queue
790 * @file: file argument passed to the poll file operation handler
791 * @wait: wait argument passed to the poll file operation handler
793 * This function implements poll file operation handler for a driver.
794 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
795 * be informed that the file descriptor of a video device is available for
797 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
798 * will be reported as available for writing.
800 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
803 * The return values from this function are intended to be directly returned
804 * from poll handler in driver.
806 unsigned int vb2_poll(struct vb2_queue
*q
, struct file
*file
, poll_table
*wait
)
808 struct video_device
*vfd
= video_devdata(file
);
809 unsigned long req_events
= poll_requested_events(wait
);
810 unsigned int res
= 0;
812 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
813 struct v4l2_fh
*fh
= file
->private_data
;
815 if (v4l2_event_pending(fh
))
817 else if (req_events
& POLLPRI
)
818 poll_wait(file
, &fh
->wait
, wait
);
822 * For compatibility with vb1: if QBUF hasn't been called yet, then
823 * return POLLERR as well. This only affects capture queues, output
824 * queues will always initialize waiting_for_buffers to false.
826 if (q
->waiting_for_buffers
&& (req_events
& (POLLIN
| POLLRDNORM
)))
829 return res
| vb2_core_poll(q
, file
, wait
);
831 EXPORT_SYMBOL_GPL(vb2_poll
);
834 * The following functions are not part of the vb2 core API, but are helper
835 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
836 * and struct vb2_ops.
837 * They contain boilerplate code that most if not all drivers have to do
838 * and so they simplify the driver code.
841 /* The queue is busy if there is a owner and you are not that owner. */
842 static inline bool vb2_queue_is_busy(struct video_device
*vdev
, struct file
*file
)
844 return vdev
->queue
->owner
&& vdev
->queue
->owner
!= file
->private_data
;
847 /* vb2 ioctl helpers */
849 int vb2_ioctl_reqbufs(struct file
*file
, void *priv
,
850 struct v4l2_requestbuffers
*p
)
852 struct video_device
*vdev
= video_devdata(file
);
853 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
, p
->type
);
857 if (vb2_queue_is_busy(vdev
, file
))
859 res
= vb2_core_reqbufs(vdev
->queue
, p
->memory
, &p
->count
);
860 /* If count == 0, then the owner has released all buffers and he
861 is no longer owner of the queue. Otherwise we have a new owner. */
863 vdev
->queue
->owner
= p
->count
? file
->private_data
: NULL
;
866 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs
);
868 int vb2_ioctl_create_bufs(struct file
*file
, void *priv
,
869 struct v4l2_create_buffers
*p
)
871 struct video_device
*vdev
= video_devdata(file
);
872 int res
= vb2_verify_memory_type(vdev
->queue
, p
->memory
,
875 p
->index
= vdev
->queue
->num_buffers
;
877 * If count == 0, then just check if memory and type are valid.
878 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
881 return res
!= -EBUSY
? res
: 0;
884 if (vb2_queue_is_busy(vdev
, file
))
887 res
= vb2_create_bufs(vdev
->queue
, p
);
889 vdev
->queue
->owner
= file
->private_data
;
892 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs
);
894 int vb2_ioctl_prepare_buf(struct file
*file
, void *priv
,
895 struct v4l2_buffer
*p
)
897 struct video_device
*vdev
= video_devdata(file
);
899 if (vb2_queue_is_busy(vdev
, file
))
901 return vb2_prepare_buf(vdev
->queue
, p
);
903 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf
);
905 int vb2_ioctl_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
907 struct video_device
*vdev
= video_devdata(file
);
909 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
910 return vb2_querybuf(vdev
->queue
, p
);
912 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf
);
914 int vb2_ioctl_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
916 struct video_device
*vdev
= video_devdata(file
);
918 if (vb2_queue_is_busy(vdev
, file
))
920 return vb2_qbuf(vdev
->queue
, p
);
922 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf
);
924 int vb2_ioctl_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
926 struct video_device
*vdev
= video_devdata(file
);
928 if (vb2_queue_is_busy(vdev
, file
))
930 return vb2_dqbuf(vdev
->queue
, p
, file
->f_flags
& O_NONBLOCK
);
932 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf
);
934 int vb2_ioctl_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
936 struct video_device
*vdev
= video_devdata(file
);
938 if (vb2_queue_is_busy(vdev
, file
))
940 return vb2_streamon(vdev
->queue
, i
);
942 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon
);
944 int vb2_ioctl_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
946 struct video_device
*vdev
= video_devdata(file
);
948 if (vb2_queue_is_busy(vdev
, file
))
950 return vb2_streamoff(vdev
->queue
, i
);
952 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff
);
954 int vb2_ioctl_expbuf(struct file
*file
, void *priv
, struct v4l2_exportbuffer
*p
)
956 struct video_device
*vdev
= video_devdata(file
);
958 if (vb2_queue_is_busy(vdev
, file
))
960 return vb2_expbuf(vdev
->queue
, p
);
962 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf
);
964 /* v4l2_file_operations helpers */
966 int vb2_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
968 struct video_device
*vdev
= video_devdata(file
);
970 return vb2_mmap(vdev
->queue
, vma
);
972 EXPORT_SYMBOL_GPL(vb2_fop_mmap
);
974 int _vb2_fop_release(struct file
*file
, struct mutex
*lock
)
976 struct video_device
*vdev
= video_devdata(file
);
980 if (file
->private_data
== vdev
->queue
->owner
) {
981 vb2_queue_release(vdev
->queue
);
982 vdev
->queue
->owner
= NULL
;
986 return v4l2_fh_release(file
);
988 EXPORT_SYMBOL_GPL(_vb2_fop_release
);
990 int vb2_fop_release(struct file
*file
)
992 struct video_device
*vdev
= video_devdata(file
);
993 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
995 return _vb2_fop_release(file
, lock
);
997 EXPORT_SYMBOL_GPL(vb2_fop_release
);
999 ssize_t
vb2_fop_write(struct file
*file
, const char __user
*buf
,
1000 size_t count
, loff_t
*ppos
)
1002 struct video_device
*vdev
= video_devdata(file
);
1003 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1006 if (!(vdev
->queue
->io_modes
& VB2_WRITE
))
1008 if (lock
&& mutex_lock_interruptible(lock
))
1009 return -ERESTARTSYS
;
1010 if (vb2_queue_is_busy(vdev
, file
))
1012 err
= vb2_write(vdev
->queue
, buf
, count
, ppos
,
1013 file
->f_flags
& O_NONBLOCK
);
1014 if (vdev
->queue
->fileio
)
1015 vdev
->queue
->owner
= file
->private_data
;
1021 EXPORT_SYMBOL_GPL(vb2_fop_write
);
1023 ssize_t
vb2_fop_read(struct file
*file
, char __user
*buf
,
1024 size_t count
, loff_t
*ppos
)
1026 struct video_device
*vdev
= video_devdata(file
);
1027 struct mutex
*lock
= vdev
->queue
->lock
? vdev
->queue
->lock
: vdev
->lock
;
1030 if (!(vdev
->queue
->io_modes
& VB2_READ
))
1032 if (lock
&& mutex_lock_interruptible(lock
))
1033 return -ERESTARTSYS
;
1034 if (vb2_queue_is_busy(vdev
, file
))
1036 err
= vb2_read(vdev
->queue
, buf
, count
, ppos
,
1037 file
->f_flags
& O_NONBLOCK
);
1038 if (vdev
->queue
->fileio
)
1039 vdev
->queue
->owner
= file
->private_data
;
1045 EXPORT_SYMBOL_GPL(vb2_fop_read
);
1047 unsigned int vb2_fop_poll(struct file
*file
, poll_table
*wait
)
1049 struct video_device
*vdev
= video_devdata(file
);
1050 struct vb2_queue
*q
= vdev
->queue
;
1051 struct mutex
*lock
= q
->lock
? q
->lock
: vdev
->lock
;
1056 * If this helper doesn't know how to lock, then you shouldn't be using
1057 * it but you should write your own.
1061 if (lock
&& mutex_lock_interruptible(lock
))
1066 res
= vb2_poll(vdev
->queue
, file
, wait
);
1068 /* If fileio was started, then we have a new queue owner. */
1069 if (!fileio
&& q
->fileio
)
1070 q
->owner
= file
->private_data
;
1075 EXPORT_SYMBOL_GPL(vb2_fop_poll
);
1078 unsigned long vb2_fop_get_unmapped_area(struct file
*file
, unsigned long addr
,
1079 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1081 struct video_device
*vdev
= video_devdata(file
);
1083 return vb2_get_unmapped_area(vdev
->queue
, addr
, len
, pgoff
, flags
);
1085 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area
);
1088 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1090 void vb2_ops_wait_prepare(struct vb2_queue
*vq
)
1092 mutex_unlock(vq
->lock
);
1094 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare
);
1096 void vb2_ops_wait_finish(struct vb2_queue
*vq
)
1098 mutex_lock(vq
->lock
);
1100 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish
);
1102 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1103 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1104 MODULE_LICENSE("GPL");