2 * videobuf2-v4l2.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
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 = v4l2_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;
162 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
163 if (vb->vb2_queue->allow_zero_bytesused)
164 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
166 pr_warn("use the actual size instead.\n");
169 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
171 struct vb2_queue *q = vb->vb2_queue;
172 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
173 struct vb2_plane *planes = vbuf->planes;
177 ret = __verify_length(vb, b);
179 dprintk(1, "plane parameters verification failed: %d\n", ret);
182 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
184 * If the format's field is ALTERNATE, then the buffer's field
185 * should be either TOP or BOTTOM, not ALTERNATE since that
186 * makes no sense. The driver has to know whether the
187 * buffer represents a top or a bottom field in order to
188 * program any DMA correctly. Using ALTERNATE is wrong, since
189 * that just says that it is either a top or a bottom field,
190 * but not which of the two it is.
192 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
196 vbuf->request_fd = -1;
198 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
200 case VB2_MEMORY_USERPTR:
201 for (plane = 0; plane < vb->num_planes; ++plane) {
202 planes[plane].m.userptr =
203 b->m.planes[plane].m.userptr;
204 planes[plane].length =
205 b->m.planes[plane].length;
208 case VB2_MEMORY_DMABUF:
209 for (plane = 0; plane < vb->num_planes; ++plane) {
211 b->m.planes[plane].m.fd;
212 planes[plane].length =
213 b->m.planes[plane].length;
217 for (plane = 0; plane < vb->num_planes; ++plane) {
218 planes[plane].m.offset =
219 vb->planes[plane].m.offset;
220 planes[plane].length =
221 vb->planes[plane].length;
226 /* Fill in driver-provided information for OUTPUT types */
227 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
229 * Will have to go up to b->length when API starts
230 * accepting variable number of planes.
232 * If bytesused == 0 for the output buffer, then fall
233 * back to the full buffer size. In that case
234 * userspace clearly never bothered to set it and
235 * it's a safe assumption that they really meant to
236 * use the full plane sizes.
238 * Some drivers, e.g. old codec drivers, use bytesused == 0
239 * as a way to indicate that streaming is finished.
240 * In that case, the driver should use the
241 * allow_zero_bytesused flag to keep old userspace
242 * applications working.
244 for (plane = 0; plane < vb->num_planes; ++plane) {
245 struct vb2_plane *pdst = &planes[plane];
246 struct v4l2_plane *psrc = &b->m.planes[plane];
248 if (psrc->bytesused == 0)
249 vb2_warn_zero_bytesused(vb);
251 if (vb->vb2_queue->allow_zero_bytesused)
252 pdst->bytesused = psrc->bytesused;
254 pdst->bytesused = psrc->bytesused ?
255 psrc->bytesused : pdst->length;
256 pdst->data_offset = psrc->data_offset;
261 * Single-planar buffers do not use planes array,
262 * so fill in relevant v4l2_buffer struct fields instead.
263 * In videobuf we use our internal V4l2_planes struct for
264 * single-planar buffers as well, for simplicity.
266 * If bytesused == 0 for the output buffer, then fall back
267 * to the full buffer size as that's a sensible default.
269 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
270 * a way to indicate that streaming is finished. In that case,
271 * the driver should use the allow_zero_bytesused flag to keep
272 * old userspace applications working.
275 case VB2_MEMORY_USERPTR:
276 planes[0].m.userptr = b->m.userptr;
277 planes[0].length = b->length;
279 case VB2_MEMORY_DMABUF:
280 planes[0].m.fd = b->m.fd;
281 planes[0].length = b->length;
284 planes[0].m.offset = vb->planes[0].m.offset;
285 planes[0].length = vb->planes[0].length;
289 planes[0].data_offset = 0;
290 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
291 if (b->bytesused == 0)
292 vb2_warn_zero_bytesused(vb);
294 if (vb->vb2_queue->allow_zero_bytesused)
295 planes[0].bytesused = b->bytesused;
297 planes[0].bytesused = b->bytesused ?
298 b->bytesused : planes[0].length;
300 planes[0].bytesused = 0;
304 /* Zero flags that we handle */
305 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
306 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
308 * Non-COPY timestamps and non-OUTPUT queues will get
309 * their timestamp and timestamp source flags from the
312 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
315 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
317 * For output buffers mask out the timecode flag:
318 * this will be handled later in vb2_qbuf().
319 * The 'field' is valid metadata for this output buffer
320 * and so that needs to be copied here.
322 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
323 vbuf->field = b->field;
325 /* Zero any output buffer flags as this is a capture buffer */
326 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
327 /* Zero last flag, this is a signal from driver to userspace */
328 vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
334 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
335 struct v4l2_buffer *b, bool is_prepare,
336 struct media_request **p_req)
338 const char *opname = is_prepare ? "prepare_buf" : "qbuf";
339 struct media_request *req;
340 struct vb2_v4l2_buffer *vbuf;
341 struct vb2_buffer *vb;
344 if (b->type != q->type) {
345 dprintk(1, "%s: invalid buffer type\n", opname);
349 if (b->index >= q->num_buffers) {
350 dprintk(1, "%s: buffer index out of range\n", opname);
354 if (q->bufs[b->index] == NULL) {
355 /* Should never happen */
356 dprintk(1, "%s: buffer is NULL\n", opname);
360 if (b->memory != q->memory) {
361 dprintk(1, "%s: invalid memory type\n", opname);
365 vb = q->bufs[b->index];
366 vbuf = to_vb2_v4l2_buffer(vb);
367 ret = __verify_planes_array(vb, b);
372 /* Copy relevant information provided by the userspace */
373 memset(vbuf->planes, 0,
374 sizeof(vbuf->planes[0]) * vb->num_planes);
375 ret = vb2_fill_vb2_v4l2_buffer(vb, b);
383 if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
384 if (q->uses_requests) {
385 dprintk(1, "%s: queue uses requests\n", opname);
389 } else if (!q->supports_requests) {
390 dprintk(1, "%s: queue does not support requests\n", opname);
392 } else if (q->uses_qbuf) {
393 dprintk(1, "%s: queue does not use requests\n", opname);
398 * For proper locking when queueing a request you need to be able
399 * to lock access to the vb2 queue, so check that there is a lock
400 * that we can use. In addition p_req must be non-NULL.
402 if (WARN_ON(!q->lock || !p_req))
406 * Make sure this op is implemented by the driver. It's easy to forget
407 * this callback, but is it important when canceling a buffer in a
410 if (WARN_ON(!q->ops->buf_request_complete))
413 * Make sure this op is implemented by the driver for the output queue.
414 * It's easy to forget this callback, but is it important to correctly
415 * validate the 'field' value at QBUF time.
417 if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
418 q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
419 !q->ops->buf_out_validate))
422 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
423 dprintk(1, "%s: buffer is not in dequeued state\n", opname);
427 if (b->request_fd < 0) {
428 dprintk(1, "%s: request_fd < 0\n", opname);
432 req = media_request_get_by_fd(mdev, b->request_fd);
434 dprintk(1, "%s: invalid request_fd\n", opname);
439 * Early sanity check. This is checked again when the buffer
440 * is bound to the request in vb2_core_qbuf().
442 if (req->state != MEDIA_REQUEST_STATE_IDLE &&
443 req->state != MEDIA_REQUEST_STATE_UPDATING) {
444 dprintk(1, "%s: request is not idle\n", opname);
445 media_request_put(req);
450 vbuf->request_fd = b->request_fd;
456 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
457 * returned to userspace
459 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
461 struct v4l2_buffer *b = pb;
462 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
463 struct vb2_queue *q = vb->vb2_queue;
466 /* Copy back data such as timestamp, flags, etc. */
467 b->index = vb->index;
469 b->memory = vb->memory;
472 b->flags = vbuf->flags;
473 b->field = vbuf->field;
474 b->timestamp = ns_to_timeval(vb->timestamp);
475 b->timecode = vbuf->timecode;
476 b->sequence = vbuf->sequence;
480 if (q->is_multiplanar) {
482 * Fill in plane-related data if userspace provided an array
483 * for it. The caller has already verified memory and size.
485 b->length = vb->num_planes;
486 for (plane = 0; plane < vb->num_planes; ++plane) {
487 struct v4l2_plane *pdst = &b->m.planes[plane];
488 struct vb2_plane *psrc = &vb->planes[plane];
490 pdst->bytesused = psrc->bytesused;
491 pdst->length = psrc->length;
492 if (q->memory == VB2_MEMORY_MMAP)
493 pdst->m.mem_offset = psrc->m.offset;
494 else if (q->memory == VB2_MEMORY_USERPTR)
495 pdst->m.userptr = psrc->m.userptr;
496 else if (q->memory == VB2_MEMORY_DMABUF)
497 pdst->m.fd = psrc->m.fd;
498 pdst->data_offset = psrc->data_offset;
499 memset(pdst->reserved, 0, sizeof(pdst->reserved));
503 * We use length and offset in v4l2_planes array even for
504 * single-planar buffers, but userspace does not.
506 b->length = vb->planes[0].length;
507 b->bytesused = vb->planes[0].bytesused;
508 if (q->memory == VB2_MEMORY_MMAP)
509 b->m.offset = vb->planes[0].m.offset;
510 else if (q->memory == VB2_MEMORY_USERPTR)
511 b->m.userptr = vb->planes[0].m.userptr;
512 else if (q->memory == VB2_MEMORY_DMABUF)
513 b->m.fd = vb->planes[0].m.fd;
517 * Clear any buffer state related flags.
519 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
520 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
521 if (!q->copy_timestamp) {
523 * For non-COPY timestamps, drop timestamp source bits
524 * and obtain the timestamp source from the queue.
526 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
527 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
531 case VB2_BUF_STATE_QUEUED:
532 case VB2_BUF_STATE_ACTIVE:
533 b->flags |= V4L2_BUF_FLAG_QUEUED;
535 case VB2_BUF_STATE_IN_REQUEST:
536 b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
538 case VB2_BUF_STATE_ERROR:
539 b->flags |= V4L2_BUF_FLAG_ERROR;
541 case VB2_BUF_STATE_DONE:
542 b->flags |= V4L2_BUF_FLAG_DONE;
544 case VB2_BUF_STATE_PREPARING:
545 case VB2_BUF_STATE_DEQUEUED:
546 case VB2_BUF_STATE_REQUEUEING:
551 if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
552 vb->state == VB2_BUF_STATE_IN_REQUEST) &&
553 vb->synced && vb->prepared)
554 b->flags |= V4L2_BUF_FLAG_PREPARED;
556 if (vb2_buffer_in_use(q, vb))
557 b->flags |= V4L2_BUF_FLAG_MAPPED;
558 if (vbuf->request_fd >= 0) {
559 b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
560 b->request_fd = vbuf->request_fd;
564 b->flags & V4L2_BUF_FLAG_DONE &&
565 b->flags & V4L2_BUF_FLAG_LAST)
566 q->last_buffer_dequeued = true;
570 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
571 * v4l2_buffer by the userspace. It also verifies that struct
572 * v4l2_buffer has a valid number of planes.
574 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
576 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
579 if (!vb->vb2_queue->copy_timestamp)
582 for (plane = 0; plane < vb->num_planes; ++plane) {
583 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
584 planes[plane].m = vbuf->planes[plane].m;
585 planes[plane].length = vbuf->planes[plane].length;
587 planes[plane].bytesused = vbuf->planes[plane].bytesused;
588 planes[plane].data_offset = vbuf->planes[plane].data_offset;
593 static const struct vb2_buf_ops v4l2_buf_ops = {
594 .verify_planes_array = __verify_planes_array_core,
595 .init_buffer = __init_v4l2_vb2_buffer,
596 .fill_user_buffer = __fill_v4l2_buffer,
597 .fill_vb2_buffer = __fill_vb2_buffer,
598 .copy_timestamp = __copy_timestamp,
601 int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
602 unsigned int start_idx)
606 for (i = start_idx; i < q->num_buffers; i++)
607 if (q->bufs[i]->copied_timestamp &&
608 q->bufs[i]->timestamp == timestamp)
612 EXPORT_SYMBOL_GPL(vb2_find_timestamp);
615 * vb2_querybuf() - query video buffer information
617 * @b: buffer struct passed from userspace to vidioc_querybuf handler
620 * Should be called from vidioc_querybuf ioctl handler in driver.
621 * This function will verify the passed v4l2_buffer structure and fill the
622 * relevant information for the userspace.
624 * The return values from this function are intended to be directly returned
625 * from vidioc_querybuf handler in driver.
627 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
629 struct vb2_buffer *vb;
632 if (b->type != q->type) {
633 dprintk(1, "wrong buffer type\n");
637 if (b->index >= q->num_buffers) {
638 dprintk(1, "buffer index out of range\n");
641 vb = q->bufs[b->index];
642 ret = __verify_planes_array(vb, b);
644 vb2_core_querybuf(q, b->index, b);
647 EXPORT_SYMBOL(vb2_querybuf);
649 static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
651 *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
652 if (q->io_modes & VB2_MMAP)
653 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
654 if (q->io_modes & VB2_USERPTR)
655 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
656 if (q->io_modes & VB2_DMABUF)
657 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
658 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
659 if (q->supports_requests)
660 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
664 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
666 int ret = vb2_verify_memory_type(q, req->memory, req->type);
668 fill_buf_caps(q, &req->capabilities);
669 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
671 EXPORT_SYMBOL_GPL(vb2_reqbufs);
673 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
674 struct v4l2_buffer *b)
678 if (vb2_fileio_is_active(q)) {
679 dprintk(1, "file io in progress\n");
683 if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
686 ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
688 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
690 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
692 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
694 unsigned requested_planes = 1;
695 unsigned requested_sizes[VIDEO_MAX_PLANES];
696 struct v4l2_format *f = &create->format;
697 int ret = vb2_verify_memory_type(q, create->memory, f->type);
700 fill_buf_caps(q, &create->capabilities);
701 create->index = q->num_buffers;
702 if (create->count == 0)
703 return ret != -EBUSY ? ret : 0;
706 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
707 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
708 requested_planes = f->fmt.pix_mp.num_planes;
709 if (requested_planes == 0 ||
710 requested_planes > VIDEO_MAX_PLANES)
712 for (i = 0; i < requested_planes; i++)
714 f->fmt.pix_mp.plane_fmt[i].sizeimage;
716 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
717 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
718 requested_sizes[0] = f->fmt.pix.sizeimage;
720 case V4L2_BUF_TYPE_VBI_CAPTURE:
721 case V4L2_BUF_TYPE_VBI_OUTPUT:
722 requested_sizes[0] = f->fmt.vbi.samples_per_line *
723 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
725 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
726 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
727 requested_sizes[0] = f->fmt.sliced.io_size;
729 case V4L2_BUF_TYPE_SDR_CAPTURE:
730 case V4L2_BUF_TYPE_SDR_OUTPUT:
731 requested_sizes[0] = f->fmt.sdr.buffersize;
733 case V4L2_BUF_TYPE_META_CAPTURE:
734 case V4L2_BUF_TYPE_META_OUTPUT:
735 requested_sizes[0] = f->fmt.meta.buffersize;
740 for (i = 0; i < requested_planes; i++)
741 if (requested_sizes[i] == 0)
743 return ret ? ret : vb2_core_create_bufs(q, create->memory,
744 &create->count, requested_planes, requested_sizes);
746 EXPORT_SYMBOL_GPL(vb2_create_bufs);
748 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
749 struct v4l2_buffer *b)
751 struct media_request *req = NULL;
754 if (vb2_fileio_is_active(q)) {
755 dprintk(1, "file io in progress\n");
759 ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
762 ret = vb2_core_qbuf(q, b->index, b, req);
764 media_request_put(req);
767 EXPORT_SYMBOL_GPL(vb2_qbuf);
769 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
773 if (vb2_fileio_is_active(q)) {
774 dprintk(1, "file io in progress\n");
778 if (b->type != q->type) {
779 dprintk(1, "invalid buffer type\n");
783 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
786 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
789 b->flags &= ~V4L2_BUF_FLAG_DONE;
793 EXPORT_SYMBOL_GPL(vb2_dqbuf);
795 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
797 if (vb2_fileio_is_active(q)) {
798 dprintk(1, "file io in progress\n");
801 return vb2_core_streamon(q, type);
803 EXPORT_SYMBOL_GPL(vb2_streamon);
805 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
807 if (vb2_fileio_is_active(q)) {
808 dprintk(1, "file io in progress\n");
811 return vb2_core_streamoff(q, type);
813 EXPORT_SYMBOL_GPL(vb2_streamoff);
815 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
817 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
818 eb->plane, eb->flags);
820 EXPORT_SYMBOL_GPL(vb2_expbuf);
822 int vb2_queue_init(struct vb2_queue *q)
828 WARN_ON(q->timestamp_flags &
829 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
830 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
833 /* Warn that the driver should choose an appropriate timestamp type */
834 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
835 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
837 /* Warn that vb2_memory should match with v4l2_memory */
838 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
839 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
840 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
843 if (q->buf_struct_size == 0)
844 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
846 q->buf_ops = &v4l2_buf_ops;
847 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
848 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
849 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
850 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
852 * For compatibility with vb1: if QBUF hasn't been called yet, then
853 * return EPOLLERR as well. This only affects capture queues, output
854 * queues will always initialize waiting_for_buffers to false.
856 q->quirk_poll_must_check_waiting_for_buffers = true;
858 return vb2_core_queue_init(q);
860 EXPORT_SYMBOL_GPL(vb2_queue_init);
862 void vb2_queue_release(struct vb2_queue *q)
864 vb2_core_queue_release(q);
866 EXPORT_SYMBOL_GPL(vb2_queue_release);
868 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
870 struct video_device *vfd = video_devdata(file);
873 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
874 struct v4l2_fh *fh = file->private_data;
876 poll_wait(file, &fh->wait, wait);
877 if (v4l2_event_pending(fh))
881 return res | vb2_core_poll(q, file, wait);
883 EXPORT_SYMBOL_GPL(vb2_poll);
886 * The following functions are not part of the vb2 core API, but are helper
887 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
888 * and struct vb2_ops.
889 * They contain boilerplate code that most if not all drivers have to do
890 * and so they simplify the driver code.
893 /* The queue is busy if there is a owner and you are not that owner. */
894 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
896 return vdev->queue->owner && vdev->queue->owner != file->private_data;
899 /* vb2 ioctl helpers */
901 int vb2_ioctl_reqbufs(struct file *file, void *priv,
902 struct v4l2_requestbuffers *p)
904 struct video_device *vdev = video_devdata(file);
905 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
907 fill_buf_caps(vdev->queue, &p->capabilities);
910 if (vb2_queue_is_busy(vdev, file))
912 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
913 /* If count == 0, then the owner has released all buffers and he
914 is no longer owner of the queue. Otherwise we have a new owner. */
916 vdev->queue->owner = p->count ? file->private_data : NULL;
919 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
921 int vb2_ioctl_create_bufs(struct file *file, void *priv,
922 struct v4l2_create_buffers *p)
924 struct video_device *vdev = video_devdata(file);
925 int res = vb2_verify_memory_type(vdev->queue, p->memory,
928 p->index = vdev->queue->num_buffers;
929 fill_buf_caps(vdev->queue, &p->capabilities);
931 * If count == 0, then just check if memory and type are valid.
932 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
935 return res != -EBUSY ? res : 0;
938 if (vb2_queue_is_busy(vdev, file))
941 res = vb2_create_bufs(vdev->queue, p);
943 vdev->queue->owner = file->private_data;
946 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
948 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
949 struct v4l2_buffer *p)
951 struct video_device *vdev = video_devdata(file);
953 if (vb2_queue_is_busy(vdev, file))
955 return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
957 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
959 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
961 struct video_device *vdev = video_devdata(file);
963 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
964 return vb2_querybuf(vdev->queue, p);
966 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
968 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
970 struct video_device *vdev = video_devdata(file);
972 if (vb2_queue_is_busy(vdev, file))
974 return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
976 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
978 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
980 struct video_device *vdev = video_devdata(file);
982 if (vb2_queue_is_busy(vdev, file))
984 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
986 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
988 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
990 struct video_device *vdev = video_devdata(file);
992 if (vb2_queue_is_busy(vdev, file))
994 return vb2_streamon(vdev->queue, i);
996 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
998 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1000 struct video_device *vdev = video_devdata(file);
1002 if (vb2_queue_is_busy(vdev, file))
1004 return vb2_streamoff(vdev->queue, i);
1006 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1008 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1010 struct video_device *vdev = video_devdata(file);
1012 if (vb2_queue_is_busy(vdev, file))
1014 return vb2_expbuf(vdev->queue, p);
1016 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1018 /* v4l2_file_operations helpers */
1020 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1022 struct video_device *vdev = video_devdata(file);
1024 return vb2_mmap(vdev->queue, vma);
1026 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1028 int _vb2_fop_release(struct file *file, struct mutex *lock)
1030 struct video_device *vdev = video_devdata(file);
1034 if (file->private_data == vdev->queue->owner) {
1035 vb2_queue_release(vdev->queue);
1036 vdev->queue->owner = NULL;
1040 return v4l2_fh_release(file);
1042 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1044 int vb2_fop_release(struct file *file)
1046 struct video_device *vdev = video_devdata(file);
1047 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1049 return _vb2_fop_release(file, lock);
1051 EXPORT_SYMBOL_GPL(vb2_fop_release);
1053 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1054 size_t count, loff_t *ppos)
1056 struct video_device *vdev = video_devdata(file);
1057 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1060 if (!(vdev->queue->io_modes & VB2_WRITE))
1062 if (lock && mutex_lock_interruptible(lock))
1063 return -ERESTARTSYS;
1064 if (vb2_queue_is_busy(vdev, file))
1066 err = vb2_write(vdev->queue, buf, count, ppos,
1067 file->f_flags & O_NONBLOCK);
1068 if (vdev->queue->fileio)
1069 vdev->queue->owner = file->private_data;
1075 EXPORT_SYMBOL_GPL(vb2_fop_write);
1077 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1078 size_t count, loff_t *ppos)
1080 struct video_device *vdev = video_devdata(file);
1081 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1084 if (!(vdev->queue->io_modes & VB2_READ))
1086 if (lock && mutex_lock_interruptible(lock))
1087 return -ERESTARTSYS;
1088 if (vb2_queue_is_busy(vdev, file))
1090 err = vb2_read(vdev->queue, buf, count, ppos,
1091 file->f_flags & O_NONBLOCK);
1092 if (vdev->queue->fileio)
1093 vdev->queue->owner = file->private_data;
1099 EXPORT_SYMBOL_GPL(vb2_fop_read);
1101 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1103 struct video_device *vdev = video_devdata(file);
1104 struct vb2_queue *q = vdev->queue;
1105 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1110 * If this helper doesn't know how to lock, then you shouldn't be using
1111 * it but you should write your own.
1115 if (lock && mutex_lock_interruptible(lock))
1120 res = vb2_poll(vdev->queue, file, wait);
1122 /* If fileio was started, then we have a new queue owner. */
1123 if (!fileio && q->fileio)
1124 q->owner = file->private_data;
1129 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1132 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1133 unsigned long len, unsigned long pgoff, unsigned long flags)
1135 struct video_device *vdev = video_devdata(file);
1137 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1139 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1142 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1144 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1146 mutex_unlock(vq->lock);
1148 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1150 void vb2_ops_wait_finish(struct vb2_queue *vq)
1152 mutex_lock(vq->lock);
1154 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1157 * Note that this function is called during validation time and
1158 * thus the req_queue_mutex is held to ensure no request objects
1159 * can be added or deleted while validating. So there is no need
1160 * to protect the objects list.
1162 int vb2_request_validate(struct media_request *req)
1164 struct media_request_object *obj;
1167 if (!vb2_request_buffer_cnt(req))
1170 list_for_each_entry(obj, &req->objects, list) {
1171 if (!obj->ops->prepare)
1174 ret = obj->ops->prepare(obj);
1180 list_for_each_entry_continue_reverse(obj, &req->objects, list)
1181 if (obj->ops->unprepare)
1182 obj->ops->unprepare(obj);
1187 EXPORT_SYMBOL_GPL(vb2_request_validate);
1189 void vb2_request_queue(struct media_request *req)
1191 struct media_request_object *obj, *obj_safe;
1194 * Queue all objects. Note that buffer objects are at the end of the
1195 * objects list, after all other object types. Once buffer objects
1196 * are queued, the driver might delete them immediately (if the driver
1197 * processes the buffer at once), so we have to use
1198 * list_for_each_entry_safe() to handle the case where the object we
1201 list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1202 if (obj->ops->queue)
1203 obj->ops->queue(obj);
1205 EXPORT_SYMBOL_GPL(vb2_request_queue);
1207 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1209 MODULE_LICENSE("GPL");