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 | V4L2_BUF_FLAG_BFRAME | \
53 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
56 * __verify_planes_array() - verify that the planes array passed in struct
57 * v4l2_buffer from userspace can be safely used
59 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
61 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
64 /* Is memory for copying plane information present? */
65 if (b->m.planes == NULL) {
66 dprintk(1, "multi-planar buffer passed but planes array not provided\n");
70 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
71 dprintk(1, "incorrect planes array length, expected %d, got %d\n",
72 vb->num_planes, b->length);
79 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
81 return __verify_planes_array(vb, pb);
85 * __verify_length() - Verify that the bytesused value for each plane fits in
86 * the plane length and that the data offset doesn't exceed the bytesused value.
88 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
91 unsigned int bytesused;
94 if (!V4L2_TYPE_IS_OUTPUT(b->type))
97 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
98 for (plane = 0; plane < vb->num_planes; ++plane) {
99 length = (b->memory == VB2_MEMORY_USERPTR ||
100 b->memory == VB2_MEMORY_DMABUF)
101 ? b->m.planes[plane].length
102 : vb->planes[plane].length;
103 bytesused = b->m.planes[plane].bytesused
104 ? b->m.planes[plane].bytesused : length;
106 if (b->m.planes[plane].bytesused > length)
109 if (b->m.planes[plane].data_offset > 0 &&
110 b->m.planes[plane].data_offset >= bytesused)
114 length = (b->memory == VB2_MEMORY_USERPTR)
115 ? b->length : vb->planes[0].length;
117 if (b->bytesused > length)
125 * __init_v4l2_vb2_buffer() - initialize the v4l2_vb2_buffer struct
127 static void __init_v4l2_vb2_buffer(struct vb2_buffer *vb)
129 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
131 vbuf->request_fd = -1;
134 static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
136 const struct v4l2_buffer *b = pb;
137 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
138 struct vb2_queue *q = vb->vb2_queue;
142 * For output buffers copy the timestamp if needed,
143 * and the timecode field and flag if needed.
145 if (q->copy_timestamp)
146 vb->timestamp = timeval_to_ns(&b->timestamp);
147 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
148 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
149 vbuf->timecode = b->timecode;
153 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
155 static bool check_once;
163 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
164 if (vb->vb2_queue->allow_zero_bytesused)
165 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
167 pr_warn("use the actual size instead.\n");
170 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
172 struct vb2_queue *q = vb->vb2_queue;
173 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
174 struct vb2_plane *planes = vbuf->planes;
178 ret = __verify_length(vb, b);
180 dprintk(1, "plane parameters verification failed: %d\n", ret);
183 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
185 * If the format's field is ALTERNATE, then the buffer's field
186 * should be either TOP or BOTTOM, not ALTERNATE since that
187 * makes no sense. The driver has to know whether the
188 * buffer represents a top or a bottom field in order to
189 * program any DMA correctly. Using ALTERNATE is wrong, since
190 * that just says that it is either a top or a bottom field,
191 * but not which of the two it is.
193 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
197 vbuf->request_fd = -1;
199 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
201 case VB2_MEMORY_USERPTR:
202 for (plane = 0; plane < vb->num_planes; ++plane) {
203 planes[plane].m.userptr =
204 b->m.planes[plane].m.userptr;
205 planes[plane].length =
206 b->m.planes[plane].length;
209 case VB2_MEMORY_DMABUF:
210 for (plane = 0; plane < vb->num_planes; ++plane) {
212 b->m.planes[plane].m.fd;
213 planes[plane].length =
214 b->m.planes[plane].length;
218 for (plane = 0; plane < vb->num_planes; ++plane) {
219 planes[plane].m.offset =
220 vb->planes[plane].m.offset;
221 planes[plane].length =
222 vb->planes[plane].length;
227 /* Fill in driver-provided information for OUTPUT types */
228 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
230 * Will have to go up to b->length when API starts
231 * accepting variable number of planes.
233 * If bytesused == 0 for the output buffer, then fall
234 * back to the full buffer size. In that case
235 * userspace clearly never bothered to set it and
236 * it's a safe assumption that they really meant to
237 * use the full plane sizes.
239 * Some drivers, e.g. old codec drivers, use bytesused == 0
240 * as a way to indicate that streaming is finished.
241 * In that case, the driver should use the
242 * allow_zero_bytesused flag to keep old userspace
243 * applications working.
245 for (plane = 0; plane < vb->num_planes; ++plane) {
246 struct vb2_plane *pdst = &planes[plane];
247 struct v4l2_plane *psrc = &b->m.planes[plane];
249 if (psrc->bytesused == 0)
250 vb2_warn_zero_bytesused(vb);
252 if (vb->vb2_queue->allow_zero_bytesused)
253 pdst->bytesused = psrc->bytesused;
255 pdst->bytesused = psrc->bytesused ?
256 psrc->bytesused : pdst->length;
257 pdst->data_offset = psrc->data_offset;
262 * Single-planar buffers do not use planes array,
263 * so fill in relevant v4l2_buffer struct fields instead.
264 * In videobuf we use our internal V4l2_planes struct for
265 * single-planar buffers as well, for simplicity.
267 * If bytesused == 0 for the output buffer, then fall back
268 * to the full buffer size as that's a sensible default.
270 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
271 * a way to indicate that streaming is finished. In that case,
272 * the driver should use the allow_zero_bytesused flag to keep
273 * old userspace applications working.
276 case VB2_MEMORY_USERPTR:
277 planes[0].m.userptr = b->m.userptr;
278 planes[0].length = b->length;
280 case VB2_MEMORY_DMABUF:
281 planes[0].m.fd = b->m.fd;
282 planes[0].length = b->length;
285 planes[0].m.offset = vb->planes[0].m.offset;
286 planes[0].length = vb->planes[0].length;
290 planes[0].data_offset = 0;
291 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
292 if (b->bytesused == 0)
293 vb2_warn_zero_bytesused(vb);
295 if (vb->vb2_queue->allow_zero_bytesused)
296 planes[0].bytesused = b->bytesused;
298 planes[0].bytesused = b->bytesused ?
299 b->bytesused : planes[0].length;
301 planes[0].bytesused = 0;
305 /* Zero flags that we handle */
306 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
307 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
309 * Non-COPY timestamps and non-OUTPUT queues will get
310 * their timestamp and timestamp source flags from the
313 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
316 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
318 * For output buffers mask out the timecode flag:
319 * this will be handled later in vb2_qbuf().
320 * The 'field' is valid metadata for this output buffer
321 * and so that needs to be copied here.
323 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
324 vbuf->field = b->field;
326 /* Zero any output buffer flags as this is a capture buffer */
327 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
328 /* Zero last flag, this is a signal from driver to userspace */
329 vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
335 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
336 struct v4l2_buffer *b,
338 struct media_request **p_req)
340 struct media_request *req;
341 struct vb2_v4l2_buffer *vbuf;
342 struct vb2_buffer *vb;
345 if (b->type != q->type) {
346 dprintk(1, "%s: invalid buffer type\n", opname);
350 if (b->index >= q->num_buffers) {
351 dprintk(1, "%s: buffer index out of range\n", opname);
355 if (q->bufs[b->index] == NULL) {
356 /* Should never happen */
357 dprintk(1, "%s: buffer is NULL\n", opname);
361 if (b->memory != q->memory) {
362 dprintk(1, "%s: invalid memory type\n", opname);
366 vb = q->bufs[b->index];
367 vbuf = to_vb2_v4l2_buffer(vb);
368 ret = __verify_planes_array(vb, b);
373 /* Copy relevant information provided by the userspace */
374 memset(vbuf->planes, 0,
375 sizeof(vbuf->planes[0]) * vb->num_planes);
376 ret = vb2_fill_vb2_v4l2_buffer(vb, b);
381 if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
382 if (q->uses_requests) {
383 dprintk(1, "%s: queue uses requests\n", opname);
387 } else if (!q->supports_requests) {
388 dprintk(1, "%s: queue does not support requests\n", opname);
390 } else if (q->uses_qbuf) {
391 dprintk(1, "%s: queue does not use requests\n", opname);
396 * For proper locking when queueing a request you need to be able
397 * to lock access to the vb2 queue, so check that there is a lock
398 * that we can use. In addition p_req must be non-NULL.
400 if (WARN_ON(!q->lock || !p_req))
404 * Make sure this op is implemented by the driver. It's easy to forget
405 * this callback, but is it important when canceling a buffer in a
408 if (WARN_ON(!q->ops->buf_request_complete))
411 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
412 dprintk(1, "%s: buffer is not in dequeued state\n", opname);
416 if (b->request_fd < 0) {
417 dprintk(1, "%s: request_fd < 0\n", opname);
421 req = media_request_get_by_fd(mdev, b->request_fd);
423 dprintk(1, "%s: invalid request_fd\n", opname);
428 * Early sanity check. This is checked again when the buffer
429 * is bound to the request in vb2_core_qbuf().
431 if (req->state != MEDIA_REQUEST_STATE_IDLE &&
432 req->state != MEDIA_REQUEST_STATE_UPDATING) {
433 dprintk(1, "%s: request is not idle\n", opname);
434 media_request_put(req);
439 vbuf->request_fd = b->request_fd;
445 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
446 * returned to userspace
448 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
450 struct v4l2_buffer *b = pb;
451 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
452 struct vb2_queue *q = vb->vb2_queue;
455 /* Copy back data such as timestamp, flags, etc. */
456 b->index = vb->index;
458 b->memory = vb->memory;
461 b->flags = vbuf->flags;
462 b->field = vbuf->field;
463 b->timestamp = ns_to_timeval(vb->timestamp);
464 b->timecode = vbuf->timecode;
465 b->sequence = vbuf->sequence;
469 if (q->is_multiplanar) {
471 * Fill in plane-related data if userspace provided an array
472 * for it. The caller has already verified memory and size.
474 b->length = vb->num_planes;
475 for (plane = 0; plane < vb->num_planes; ++plane) {
476 struct v4l2_plane *pdst = &b->m.planes[plane];
477 struct vb2_plane *psrc = &vb->planes[plane];
479 pdst->bytesused = psrc->bytesused;
480 pdst->length = psrc->length;
481 if (q->memory == VB2_MEMORY_MMAP)
482 pdst->m.mem_offset = psrc->m.offset;
483 else if (q->memory == VB2_MEMORY_USERPTR)
484 pdst->m.userptr = psrc->m.userptr;
485 else if (q->memory == VB2_MEMORY_DMABUF)
486 pdst->m.fd = psrc->m.fd;
487 pdst->data_offset = psrc->data_offset;
488 memset(pdst->reserved, 0, sizeof(pdst->reserved));
492 * We use length and offset in v4l2_planes array even for
493 * single-planar buffers, but userspace does not.
495 b->length = vb->planes[0].length;
496 b->bytesused = vb->planes[0].bytesused;
497 if (q->memory == VB2_MEMORY_MMAP)
498 b->m.offset = vb->planes[0].m.offset;
499 else if (q->memory == VB2_MEMORY_USERPTR)
500 b->m.userptr = vb->planes[0].m.userptr;
501 else if (q->memory == VB2_MEMORY_DMABUF)
502 b->m.fd = vb->planes[0].m.fd;
506 * Clear any buffer state related flags.
508 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
509 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
510 if (!q->copy_timestamp) {
512 * For non-COPY timestamps, drop timestamp source bits
513 * and obtain the timestamp source from the queue.
515 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
516 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
520 case VB2_BUF_STATE_QUEUED:
521 case VB2_BUF_STATE_ACTIVE:
522 b->flags |= V4L2_BUF_FLAG_QUEUED;
524 case VB2_BUF_STATE_IN_REQUEST:
525 b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
527 case VB2_BUF_STATE_ERROR:
528 b->flags |= V4L2_BUF_FLAG_ERROR;
530 case VB2_BUF_STATE_DONE:
531 b->flags |= V4L2_BUF_FLAG_DONE;
533 case VB2_BUF_STATE_PREPARING:
534 case VB2_BUF_STATE_DEQUEUED:
535 case VB2_BUF_STATE_REQUEUEING:
540 if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
541 vb->state == VB2_BUF_STATE_IN_REQUEST) &&
542 vb->synced && vb->prepared)
543 b->flags |= V4L2_BUF_FLAG_PREPARED;
545 if (vb2_buffer_in_use(q, vb))
546 b->flags |= V4L2_BUF_FLAG_MAPPED;
547 if (vbuf->request_fd >= 0) {
548 b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
549 b->request_fd = vbuf->request_fd;
553 b->flags & V4L2_BUF_FLAG_DONE &&
554 b->flags & V4L2_BUF_FLAG_LAST)
555 q->last_buffer_dequeued = true;
559 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
560 * v4l2_buffer by the userspace. It also verifies that struct
561 * v4l2_buffer has a valid number of planes.
563 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
565 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
568 if (!vb->vb2_queue->is_output || !vb->vb2_queue->copy_timestamp)
571 for (plane = 0; plane < vb->num_planes; ++plane) {
572 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
573 planes[plane].m = vbuf->planes[plane].m;
574 planes[plane].length = vbuf->planes[plane].length;
576 planes[plane].bytesused = vbuf->planes[plane].bytesused;
577 planes[plane].data_offset = vbuf->planes[plane].data_offset;
582 static const struct vb2_buf_ops v4l2_buf_ops = {
583 .verify_planes_array = __verify_planes_array_core,
584 .init_buffer = __init_v4l2_vb2_buffer,
585 .fill_user_buffer = __fill_v4l2_buffer,
586 .fill_vb2_buffer = __fill_vb2_buffer,
587 .copy_timestamp = __copy_timestamp,
591 * vb2_querybuf() - query video buffer information
593 * @b: buffer struct passed from userspace to vidioc_querybuf handler
596 * Should be called from vidioc_querybuf ioctl handler in driver.
597 * This function will verify the passed v4l2_buffer structure and fill the
598 * relevant information for the userspace.
600 * The return values from this function are intended to be directly returned
601 * from vidioc_querybuf handler in driver.
603 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
605 struct vb2_buffer *vb;
608 if (b->type != q->type) {
609 dprintk(1, "wrong buffer type\n");
613 if (b->index >= q->num_buffers) {
614 dprintk(1, "buffer index out of range\n");
617 vb = q->bufs[b->index];
618 ret = __verify_planes_array(vb, b);
620 vb2_core_querybuf(q, b->index, b);
623 EXPORT_SYMBOL(vb2_querybuf);
625 static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
628 if (q->io_modes & VB2_MMAP)
629 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
630 if (q->io_modes & VB2_USERPTR)
631 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
632 if (q->io_modes & VB2_DMABUF)
633 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
634 if (q->supports_requests)
635 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
638 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
640 int ret = vb2_verify_memory_type(q, req->memory, req->type);
642 fill_buf_caps(q, &req->capabilities);
643 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
645 EXPORT_SYMBOL_GPL(vb2_reqbufs);
647 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
648 struct v4l2_buffer *b)
652 if (vb2_fileio_is_active(q)) {
653 dprintk(1, "file io in progress\n");
657 if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
660 ret = vb2_queue_or_prepare_buf(q, mdev, b, "prepare_buf", NULL);
662 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
664 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
666 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
668 unsigned requested_planes = 1;
669 unsigned requested_sizes[VIDEO_MAX_PLANES];
670 struct v4l2_format *f = &create->format;
671 int ret = vb2_verify_memory_type(q, create->memory, f->type);
674 fill_buf_caps(q, &create->capabilities);
675 create->index = q->num_buffers;
676 if (create->count == 0)
677 return ret != -EBUSY ? ret : 0;
680 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
681 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
682 requested_planes = f->fmt.pix_mp.num_planes;
683 if (requested_planes == 0 ||
684 requested_planes > VIDEO_MAX_PLANES)
686 for (i = 0; i < requested_planes; i++)
688 f->fmt.pix_mp.plane_fmt[i].sizeimage;
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
692 requested_sizes[0] = f->fmt.pix.sizeimage;
694 case V4L2_BUF_TYPE_VBI_CAPTURE:
695 case V4L2_BUF_TYPE_VBI_OUTPUT:
696 requested_sizes[0] = f->fmt.vbi.samples_per_line *
697 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
699 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
700 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
701 requested_sizes[0] = f->fmt.sliced.io_size;
703 case V4L2_BUF_TYPE_SDR_CAPTURE:
704 case V4L2_BUF_TYPE_SDR_OUTPUT:
705 requested_sizes[0] = f->fmt.sdr.buffersize;
707 case V4L2_BUF_TYPE_META_CAPTURE:
708 requested_sizes[0] = f->fmt.meta.buffersize;
713 for (i = 0; i < requested_planes; i++)
714 if (requested_sizes[i] == 0)
716 return ret ? ret : vb2_core_create_bufs(q, create->memory,
717 &create->count, requested_planes, requested_sizes);
719 EXPORT_SYMBOL_GPL(vb2_create_bufs);
721 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
722 struct v4l2_buffer *b)
724 struct media_request *req = NULL;
727 if (vb2_fileio_is_active(q)) {
728 dprintk(1, "file io in progress\n");
732 ret = vb2_queue_or_prepare_buf(q, mdev, b, "qbuf", &req);
735 ret = vb2_core_qbuf(q, b->index, b, req);
737 media_request_put(req);
740 EXPORT_SYMBOL_GPL(vb2_qbuf);
742 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
746 if (vb2_fileio_is_active(q)) {
747 dprintk(1, "file io in progress\n");
751 if (b->type != q->type) {
752 dprintk(1, "invalid buffer type\n");
756 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
759 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
762 b->flags &= ~V4L2_BUF_FLAG_DONE;
766 EXPORT_SYMBOL_GPL(vb2_dqbuf);
768 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
770 if (vb2_fileio_is_active(q)) {
771 dprintk(1, "file io in progress\n");
774 return vb2_core_streamon(q, type);
776 EXPORT_SYMBOL_GPL(vb2_streamon);
778 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
780 if (vb2_fileio_is_active(q)) {
781 dprintk(1, "file io in progress\n");
784 return vb2_core_streamoff(q, type);
786 EXPORT_SYMBOL_GPL(vb2_streamoff);
788 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
790 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
791 eb->plane, eb->flags);
793 EXPORT_SYMBOL_GPL(vb2_expbuf);
795 int vb2_queue_init(struct vb2_queue *q)
801 WARN_ON(q->timestamp_flags &
802 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
803 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
806 /* Warn that the driver should choose an appropriate timestamp type */
807 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
808 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
810 /* Warn that vb2_memory should match with v4l2_memory */
811 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
812 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
813 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
816 if (q->buf_struct_size == 0)
817 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
819 q->buf_ops = &v4l2_buf_ops;
820 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
821 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
822 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
823 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
825 * For compatibility with vb1: if QBUF hasn't been called yet, then
826 * return EPOLLERR as well. This only affects capture queues, output
827 * queues will always initialize waiting_for_buffers to false.
829 q->quirk_poll_must_check_waiting_for_buffers = true;
831 return vb2_core_queue_init(q);
833 EXPORT_SYMBOL_GPL(vb2_queue_init);
835 void vb2_queue_release(struct vb2_queue *q)
837 vb2_core_queue_release(q);
839 EXPORT_SYMBOL_GPL(vb2_queue_release);
841 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
843 struct video_device *vfd = video_devdata(file);
844 __poll_t req_events = poll_requested_events(wait);
847 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
848 struct v4l2_fh *fh = file->private_data;
850 if (v4l2_event_pending(fh))
852 else if (req_events & EPOLLPRI)
853 poll_wait(file, &fh->wait, wait);
856 return res | vb2_core_poll(q, file, wait);
858 EXPORT_SYMBOL_GPL(vb2_poll);
861 * The following functions are not part of the vb2 core API, but are helper
862 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
863 * and struct vb2_ops.
864 * They contain boilerplate code that most if not all drivers have to do
865 * and so they simplify the driver code.
868 /* The queue is busy if there is a owner and you are not that owner. */
869 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
871 return vdev->queue->owner && vdev->queue->owner != file->private_data;
874 /* vb2 ioctl helpers */
876 int vb2_ioctl_reqbufs(struct file *file, void *priv,
877 struct v4l2_requestbuffers *p)
879 struct video_device *vdev = video_devdata(file);
880 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
882 fill_buf_caps(vdev->queue, &p->capabilities);
885 if (vb2_queue_is_busy(vdev, file))
887 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
888 /* If count == 0, then the owner has released all buffers and he
889 is no longer owner of the queue. Otherwise we have a new owner. */
891 vdev->queue->owner = p->count ? file->private_data : NULL;
894 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
896 int vb2_ioctl_create_bufs(struct file *file, void *priv,
897 struct v4l2_create_buffers *p)
899 struct video_device *vdev = video_devdata(file);
900 int res = vb2_verify_memory_type(vdev->queue, p->memory,
903 p->index = vdev->queue->num_buffers;
904 fill_buf_caps(vdev->queue, &p->capabilities);
906 * If count == 0, then just check if memory and type are valid.
907 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
910 return res != -EBUSY ? res : 0;
913 if (vb2_queue_is_busy(vdev, file))
916 res = vb2_create_bufs(vdev->queue, p);
918 vdev->queue->owner = file->private_data;
921 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
923 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
924 struct v4l2_buffer *p)
926 struct video_device *vdev = video_devdata(file);
928 if (vb2_queue_is_busy(vdev, file))
930 return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
932 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
934 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
936 struct video_device *vdev = video_devdata(file);
938 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
939 return vb2_querybuf(vdev->queue, p);
941 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
943 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
945 struct video_device *vdev = video_devdata(file);
947 if (vb2_queue_is_busy(vdev, file))
949 return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
951 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
953 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
955 struct video_device *vdev = video_devdata(file);
957 if (vb2_queue_is_busy(vdev, file))
959 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
961 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
963 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
965 struct video_device *vdev = video_devdata(file);
967 if (vb2_queue_is_busy(vdev, file))
969 return vb2_streamon(vdev->queue, i);
971 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
973 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
975 struct video_device *vdev = video_devdata(file);
977 if (vb2_queue_is_busy(vdev, file))
979 return vb2_streamoff(vdev->queue, i);
981 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
983 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
985 struct video_device *vdev = video_devdata(file);
987 if (vb2_queue_is_busy(vdev, file))
989 return vb2_expbuf(vdev->queue, p);
991 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
993 /* v4l2_file_operations helpers */
995 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
997 struct video_device *vdev = video_devdata(file);
999 return vb2_mmap(vdev->queue, vma);
1001 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1003 int _vb2_fop_release(struct file *file, struct mutex *lock)
1005 struct video_device *vdev = video_devdata(file);
1009 if (file->private_data == vdev->queue->owner) {
1010 vb2_queue_release(vdev->queue);
1011 vdev->queue->owner = NULL;
1015 return v4l2_fh_release(file);
1017 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1019 int vb2_fop_release(struct file *file)
1021 struct video_device *vdev = video_devdata(file);
1022 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1024 return _vb2_fop_release(file, lock);
1026 EXPORT_SYMBOL_GPL(vb2_fop_release);
1028 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1029 size_t count, loff_t *ppos)
1031 struct video_device *vdev = video_devdata(file);
1032 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1035 if (!(vdev->queue->io_modes & VB2_WRITE))
1037 if (lock && mutex_lock_interruptible(lock))
1038 return -ERESTARTSYS;
1039 if (vb2_queue_is_busy(vdev, file))
1041 err = vb2_write(vdev->queue, buf, count, ppos,
1042 file->f_flags & O_NONBLOCK);
1043 if (vdev->queue->fileio)
1044 vdev->queue->owner = file->private_data;
1050 EXPORT_SYMBOL_GPL(vb2_fop_write);
1052 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1053 size_t count, loff_t *ppos)
1055 struct video_device *vdev = video_devdata(file);
1056 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1059 if (!(vdev->queue->io_modes & VB2_READ))
1061 if (lock && mutex_lock_interruptible(lock))
1062 return -ERESTARTSYS;
1063 if (vb2_queue_is_busy(vdev, file))
1065 err = vb2_read(vdev->queue, buf, count, ppos,
1066 file->f_flags & O_NONBLOCK);
1067 if (vdev->queue->fileio)
1068 vdev->queue->owner = file->private_data;
1074 EXPORT_SYMBOL_GPL(vb2_fop_read);
1076 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1078 struct video_device *vdev = video_devdata(file);
1079 struct vb2_queue *q = vdev->queue;
1080 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1085 * If this helper doesn't know how to lock, then you shouldn't be using
1086 * it but you should write your own.
1090 if (lock && mutex_lock_interruptible(lock))
1095 res = vb2_poll(vdev->queue, file, wait);
1097 /* If fileio was started, then we have a new queue owner. */
1098 if (!fileio && q->fileio)
1099 q->owner = file->private_data;
1104 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1107 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1108 unsigned long len, unsigned long pgoff, unsigned long flags)
1110 struct video_device *vdev = video_devdata(file);
1112 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1114 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1117 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1119 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1121 mutex_unlock(vq->lock);
1123 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1125 void vb2_ops_wait_finish(struct vb2_queue *vq)
1127 mutex_lock(vq->lock);
1129 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1132 * Note that this function is called during validation time and
1133 * thus the req_queue_mutex is held to ensure no request objects
1134 * can be added or deleted while validating. So there is no need
1135 * to protect the objects list.
1137 int vb2_request_validate(struct media_request *req)
1139 struct media_request_object *obj;
1142 if (!vb2_request_buffer_cnt(req))
1145 list_for_each_entry(obj, &req->objects, list) {
1146 if (!obj->ops->prepare)
1149 ret = obj->ops->prepare(obj);
1155 list_for_each_entry_continue_reverse(obj, &req->objects, list)
1156 if (obj->ops->unprepare)
1157 obj->ops->unprepare(obj);
1162 EXPORT_SYMBOL_GPL(vb2_request_validate);
1164 void vb2_request_queue(struct media_request *req)
1166 struct media_request_object *obj, *obj_safe;
1169 * Queue all objects. Note that buffer objects are at the end of the
1170 * objects list, after all other object types. Once buffer objects
1171 * are queued, the driver might delete them immediately (if the driver
1172 * processes the buffer at once), so we have to use
1173 * list_for_each_entry_safe() to handle the case where the object we
1176 list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1177 if (obj->ops->queue)
1178 obj->ops->queue(obj);
1180 EXPORT_SYMBOL_GPL(vb2_request_queue);
1182 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1183 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1184 MODULE_LICENSE("GPL");