Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.9.4
   1/*
   2 * videobuf2-v4l2.c - V4L2 driver helper framework
   3 *
   4 * Copyright (C) 2010 Samsung Electronics
   5 *
   6 * Author: Pawel Osciak <pawel@osciak.com>
   7 *	   Marek Szyprowski <m.szyprowski@samsung.com>
   8 *
   9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
  10 *	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  11 *
  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.
  15 */
  16
  17#include <linux/device.h>
  18#include <linux/err.h>
  19#include <linux/freezer.h>
  20#include <linux/kernel.h>
  21#include <linux/kthread.h>
  22#include <linux/mm.h>
  23#include <linux/module.h>
 
  24#include <linux/poll.h>
  25#include <linux/sched.h>
  26#include <linux/slab.h>
 
 
 
  27
  28#include <media/v4l2-common.h>
  29#include <media/v4l2-dev.h>
  30#include <media/v4l2-device.h>
  31#include <media/v4l2-event.h>
  32#include <media/v4l2-fh.h>
 
 
  33
  34#include <media/videobuf2-v4l2.h>
  35
  36static int debug;
  37module_param(debug, int, 0644);
  38
  39#define dprintk(q, level, fmt, arg...)					      \
  40	do {								      \
  41		if (debug >= level)					      \
  42			pr_info("vb2-v4l2: [%p] %s: " fmt,		      \
  43				(q)->name, __func__, ## arg);		      \
  44	} while (0)
  45
  46/* Flags that are set by us */
  47#define V4L2_BUFFER_MASK_FLAGS	(V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  48				 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  49				 V4L2_BUF_FLAG_PREPARED | \
  50				 V4L2_BUF_FLAG_IN_REQUEST | \
  51				 V4L2_BUF_FLAG_REQUEST_FD | \
  52				 V4L2_BUF_FLAG_TIMESTAMP_MASK)
  53/* Output buffer flags that should be passed on to the driver */
  54#define V4L2_BUFFER_OUT_FLAGS	(V4L2_BUF_FLAG_PFRAME | \
  55				 V4L2_BUF_FLAG_BFRAME | \
  56				 V4L2_BUF_FLAG_KEYFRAME | \
  57				 V4L2_BUF_FLAG_TIMECODE | \
  58				 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
  59
  60/*
  61 * __verify_planes_array() - verify that the planes array passed in struct
  62 * v4l2_buffer from userspace can be safely used
  63 */
  64static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  65{
  66	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  67		return 0;
  68
  69	/* Is memory for copying plane information present? */
  70	if (b->m.planes == NULL) {
  71		dprintk(vb->vb2_queue, 1,
  72			"multi-planar buffer passed but planes array not provided\n");
  73		return -EINVAL;
  74	}
  75
  76	if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
  77		dprintk(vb->vb2_queue, 1,
  78			"incorrect planes array length, expected %d, got %d\n",
  79			vb->num_planes, b->length);
  80		return -EINVAL;
  81	}
  82
  83	return 0;
  84}
  85
  86static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
  87{
  88	return __verify_planes_array(vb, pb);
  89}
  90
  91/*
  92 * __verify_length() - Verify that the bytesused value for each plane fits in
  93 * the plane length and that the data offset doesn't exceed the bytesused value.
  94 */
  95static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  96{
  97	unsigned int length;
  98	unsigned int bytesused;
  99	unsigned int plane;
 100
 101	if (V4L2_TYPE_IS_CAPTURE(b->type))
 102		return 0;
 103
 104	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
 105		for (plane = 0; plane < vb->num_planes; ++plane) {
 106			length = (b->memory == VB2_MEMORY_USERPTR ||
 107				  b->memory == VB2_MEMORY_DMABUF)
 108			       ? b->m.planes[plane].length
 109				: vb->planes[plane].length;
 110			bytesused = b->m.planes[plane].bytesused
 111				  ? b->m.planes[plane].bytesused : length;
 112
 113			if (b->m.planes[plane].bytesused > length)
 114				return -EINVAL;
 115
 116			if (b->m.planes[plane].data_offset > 0 &&
 117			    b->m.planes[plane].data_offset >= bytesused)
 118				return -EINVAL;
 119		}
 120	} else {
 121		length = (b->memory == VB2_MEMORY_USERPTR)
 122			? b->length : vb->planes[0].length;
 123
 124		if (b->bytesused > length)
 125			return -EINVAL;
 126	}
 127
 128	return 0;
 129}
 130
 131/*
 132 * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
 133 */
 134static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb)
 135{
 136	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 137
 138	vbuf->request_fd = -1;
 139}
 140
 141static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
 142{
 143	const struct v4l2_buffer *b = pb;
 144	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 145	struct vb2_queue *q = vb->vb2_queue;
 146
 147	if (q->is_output) {
 148		/*
 149		 * For output buffers copy the timestamp if needed,
 150		 * and the timecode field and flag if needed.
 151		 */
 152		if (q->copy_timestamp)
 153			vb->timestamp = v4l2_buffer_get_timestamp(b);
 154		vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
 155		if (b->flags & V4L2_BUF_FLAG_TIMECODE)
 156			vbuf->timecode = b->timecode;
 157	}
 158};
 159
 160static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
 161{
 162	static bool check_once;
 163
 164	if (check_once)
 165		return;
 166
 167	check_once = true;
 168
 169	pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
 170	if (vb->vb2_queue->allow_zero_bytesused)
 171		pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
 172	else
 173		pr_warn("use the actual size instead.\n");
 174}
 175
 176static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
 177{
 178	struct vb2_queue *q = vb->vb2_queue;
 179	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 180	struct vb2_plane *planes = vbuf->planes;
 181	unsigned int plane;
 182	int ret;
 183
 184	ret = __verify_length(vb, b);
 185	if (ret < 0) {
 186		dprintk(q, 1, "plane parameters verification failed: %d\n", ret);
 187		return ret;
 188	}
 189	if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
 190		/*
 191		 * If the format's field is ALTERNATE, then the buffer's field
 192		 * should be either TOP or BOTTOM, not ALTERNATE since that
 193		 * makes no sense. The driver has to know whether the
 194		 * buffer represents a top or a bottom field in order to
 195		 * program any DMA correctly. Using ALTERNATE is wrong, since
 196		 * that just says that it is either a top or a bottom field,
 197		 * but not which of the two it is.
 198		 */
 199		dprintk(q, 1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
 200		return -EINVAL;
 201	}
 202	vbuf->sequence = 0;
 203	vbuf->request_fd = -1;
 204	vbuf->is_held = false;
 205
 206	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
 207		switch (b->memory) {
 208		case VB2_MEMORY_USERPTR:
 209			for (plane = 0; plane < vb->num_planes; ++plane) {
 210				planes[plane].m.userptr =
 211					b->m.planes[plane].m.userptr;
 212				planes[plane].length =
 213					b->m.planes[plane].length;
 214			}
 215			break;
 216		case VB2_MEMORY_DMABUF:
 217			for (plane = 0; plane < vb->num_planes; ++plane) {
 218				planes[plane].m.fd =
 219					b->m.planes[plane].m.fd;
 220				planes[plane].length =
 221					b->m.planes[plane].length;
 222			}
 223			break;
 224		default:
 225			for (plane = 0; plane < vb->num_planes; ++plane) {
 226				planes[plane].m.offset =
 227					vb->planes[plane].m.offset;
 228				planes[plane].length =
 229					vb->planes[plane].length;
 230			}
 231			break;
 232		}
 233
 234		/* Fill in driver-provided information for OUTPUT types */
 235		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
 236			/*
 237			 * Will have to go up to b->length when API starts
 238			 * accepting variable number of planes.
 239			 *
 240			 * If bytesused == 0 for the output buffer, then fall
 241			 * back to the full buffer size. In that case
 242			 * userspace clearly never bothered to set it and
 243			 * it's a safe assumption that they really meant to
 244			 * use the full plane sizes.
 245			 *
 246			 * Some drivers, e.g. old codec drivers, use bytesused == 0
 247			 * as a way to indicate that streaming is finished.
 248			 * In that case, the driver should use the
 249			 * allow_zero_bytesused flag to keep old userspace
 250			 * applications working.
 251			 */
 252			for (plane = 0; plane < vb->num_planes; ++plane) {
 253				struct vb2_plane *pdst = &planes[plane];
 254				struct v4l2_plane *psrc = &b->m.planes[plane];
 255
 256				if (psrc->bytesused == 0)
 257					vb2_warn_zero_bytesused(vb);
 258
 259				if (vb->vb2_queue->allow_zero_bytesused)
 260					pdst->bytesused = psrc->bytesused;
 261				else
 262					pdst->bytesused = psrc->bytesused ?
 263						psrc->bytesused : pdst->length;
 264				pdst->data_offset = psrc->data_offset;
 265			}
 266		}
 267	} else {
 268		/*
 269		 * Single-planar buffers do not use planes array,
 270		 * so fill in relevant v4l2_buffer struct fields instead.
 271		 * In vb2 we use our internal V4l2_planes struct for
 272		 * single-planar buffers as well, for simplicity.
 273		 *
 274		 * If bytesused == 0 for the output buffer, then fall back
 275		 * to the full buffer size as that's a sensible default.
 276		 *
 277		 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
 278		 * a way to indicate that streaming is finished. In that case,
 279		 * the driver should use the allow_zero_bytesused flag to keep
 280		 * old userspace applications working.
 281		 */
 282		switch (b->memory) {
 283		case VB2_MEMORY_USERPTR:
 284			planes[0].m.userptr = b->m.userptr;
 285			planes[0].length = b->length;
 286			break;
 287		case VB2_MEMORY_DMABUF:
 288			planes[0].m.fd = b->m.fd;
 289			planes[0].length = b->length;
 290			break;
 291		default:
 292			planes[0].m.offset = vb->planes[0].m.offset;
 293			planes[0].length = vb->planes[0].length;
 294			break;
 295		}
 296
 297		planes[0].data_offset = 0;
 298		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
 299			if (b->bytesused == 0)
 300				vb2_warn_zero_bytesused(vb);
 301
 302			if (vb->vb2_queue->allow_zero_bytesused)
 303				planes[0].bytesused = b->bytesused;
 304			else
 305				planes[0].bytesused = b->bytesused ?
 306					b->bytesused : planes[0].length;
 307		} else
 308			planes[0].bytesused = 0;
 309
 310	}
 311
 312	/* Zero flags that we handle */
 313	vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
 314	if (!vb->vb2_queue->copy_timestamp || V4L2_TYPE_IS_CAPTURE(b->type)) {
 315		/*
 316		 * Non-COPY timestamps and non-OUTPUT queues will get
 317		 * their timestamp and timestamp source flags from the
 318		 * queue.
 319		 */
 320		vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 321	}
 322
 323	if (V4L2_TYPE_IS_OUTPUT(b->type)) {
 324		/*
 325		 * For output buffers mask out the timecode flag:
 326		 * this will be handled later in vb2_qbuf().
 327		 * The 'field' is valid metadata for this output buffer
 328		 * and so that needs to be copied here.
 329		 */
 330		vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
 331		vbuf->field = b->field;
 332		if (!(q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
 333			vbuf->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
 334	} else {
 335		/* Zero any output buffer flags as this is a capture buffer */
 336		vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
 337		/* Zero last flag, this is a signal from driver to userspace */
 338		vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
 339	}
 340
 341	return 0;
 342}
 343
 344static void set_buffer_cache_hints(struct vb2_queue *q,
 345				   struct vb2_buffer *vb,
 346				   struct v4l2_buffer *b)
 347{
 348	if (!vb2_queue_allows_cache_hints(q)) {
 349		/*
 350		 * Clear buffer cache flags if queue does not support user
 351		 * space hints. That's to indicate to userspace that these
 352		 * flags won't work.
 353		 */
 354		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
 355		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_CLEAN;
 356		return;
 357	}
 358
 359	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE)
 360		vb->skip_cache_sync_on_finish = 1;
 361
 362	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN)
 363		vb->skip_cache_sync_on_prepare = 1;
 364}
 365
 366static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
 367				    struct vb2_buffer *vb, struct v4l2_buffer *b,
 368				    bool is_prepare, struct media_request **p_req)
 369{
 370	const char *opname = is_prepare ? "prepare_buf" : "qbuf";
 371	struct media_request *req;
 372	struct vb2_v4l2_buffer *vbuf;
 
 373	int ret;
 374
 375	if (b->type != q->type) {
 376		dprintk(q, 1, "%s: invalid buffer type\n", opname);
 
 
 
 
 
 
 
 
 
 
 
 377		return -EINVAL;
 378	}
 379
 380	if (b->memory != q->memory) {
 381		dprintk(q, 1, "%s: invalid memory type\n", opname);
 382		return -EINVAL;
 383	}
 384
 
 385	vbuf = to_vb2_v4l2_buffer(vb);
 386	ret = __verify_planes_array(vb, b);
 387	if (ret)
 388		return ret;
 389
 390	if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
 391	    vb->state != VB2_BUF_STATE_DEQUEUED) {
 392		dprintk(q, 1, "%s: buffer is not in dequeued state\n", opname);
 393		return -EINVAL;
 394	}
 395
 396	if (!vb->prepared) {
 397		set_buffer_cache_hints(q, vb, b);
 398		/* Copy relevant information provided by the userspace */
 399		memset(vbuf->planes, 0,
 400		       sizeof(vbuf->planes[0]) * vb->num_planes);
 401		ret = vb2_fill_vb2_v4l2_buffer(vb, b);
 402		if (ret)
 403			return ret;
 404	}
 405
 406	if (is_prepare)
 407		return 0;
 408
 409	if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
 410		if (q->requires_requests) {
 411			dprintk(q, 1, "%s: queue requires requests\n", opname);
 412			return -EBADR;
 413		}
 414		if (q->uses_requests) {
 415			dprintk(q, 1, "%s: queue uses requests\n", opname);
 416			return -EBUSY;
 417		}
 418		return 0;
 419	} else if (!q->supports_requests) {
 420		dprintk(q, 1, "%s: queue does not support requests\n", opname);
 421		return -EBADR;
 422	} else if (q->uses_qbuf) {
 423		dprintk(q, 1, "%s: queue does not use requests\n", opname);
 424		return -EBUSY;
 425	}
 426
 427	/*
 428	 * For proper locking when queueing a request you need to be able
 429	 * to lock access to the vb2 queue, so check that there is a lock
 430	 * that we can use. In addition p_req must be non-NULL.
 431	 */
 432	if (WARN_ON(!q->lock || !p_req))
 433		return -EINVAL;
 434
 435	/*
 436	 * Make sure this op is implemented by the driver. It's easy to forget
 437	 * this callback, but is it important when canceling a buffer in a
 438	 * queued request.
 439	 */
 440	if (WARN_ON(!q->ops->buf_request_complete))
 441		return -EINVAL;
 442	/*
 443	 * Make sure this op is implemented by the driver for the output queue.
 444	 * It's easy to forget this callback, but is it important to correctly
 445	 * validate the 'field' value at QBUF time.
 446	 */
 447	if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 448		     q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
 449		    !q->ops->buf_out_validate))
 450		return -EINVAL;
 451
 
 
 
 
 
 452	req = media_request_get_by_fd(mdev, b->request_fd);
 453	if (IS_ERR(req)) {
 454		dprintk(q, 1, "%s: invalid request_fd\n", opname);
 455		return PTR_ERR(req);
 456	}
 457
 458	/*
 459	 * Early sanity check. This is checked again when the buffer
 460	 * is bound to the request in vb2_core_qbuf().
 461	 */
 462	if (req->state != MEDIA_REQUEST_STATE_IDLE &&
 463	    req->state != MEDIA_REQUEST_STATE_UPDATING) {
 464		dprintk(q, 1, "%s: request is not idle\n", opname);
 465		media_request_put(req);
 466		return -EBUSY;
 467	}
 468
 469	*p_req = req;
 470	vbuf->request_fd = b->request_fd;
 471
 472	return 0;
 473}
 474
 475/*
 476 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
 477 * returned to userspace
 478 */
 479static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
 480{
 481	struct v4l2_buffer *b = pb;
 482	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 483	struct vb2_queue *q = vb->vb2_queue;
 484	unsigned int plane;
 485
 486	/* Copy back data such as timestamp, flags, etc. */
 487	b->index = vb->index;
 488	b->type = vb->type;
 489	b->memory = vb->memory;
 490	b->bytesused = 0;
 491
 492	b->flags = vbuf->flags;
 493	b->field = vbuf->field;
 494	v4l2_buffer_set_timestamp(b, vb->timestamp);
 495	b->timecode = vbuf->timecode;
 496	b->sequence = vbuf->sequence;
 497	b->reserved2 = 0;
 498	b->request_fd = 0;
 499
 500	if (q->is_multiplanar) {
 501		/*
 502		 * Fill in plane-related data if userspace provided an array
 503		 * for it. The caller has already verified memory and size.
 504		 */
 505		b->length = vb->num_planes;
 506		for (plane = 0; plane < vb->num_planes; ++plane) {
 507			struct v4l2_plane *pdst = &b->m.planes[plane];
 508			struct vb2_plane *psrc = &vb->planes[plane];
 509
 510			pdst->bytesused = psrc->bytesused;
 511			pdst->length = psrc->length;
 512			if (q->memory == VB2_MEMORY_MMAP)
 513				pdst->m.mem_offset = psrc->m.offset;
 514			else if (q->memory == VB2_MEMORY_USERPTR)
 515				pdst->m.userptr = psrc->m.userptr;
 516			else if (q->memory == VB2_MEMORY_DMABUF)
 517				pdst->m.fd = psrc->m.fd;
 518			pdst->data_offset = psrc->data_offset;
 519			memset(pdst->reserved, 0, sizeof(pdst->reserved));
 520		}
 521	} else {
 522		/*
 523		 * We use length and offset in v4l2_planes array even for
 524		 * single-planar buffers, but userspace does not.
 525		 */
 526		b->length = vb->planes[0].length;
 527		b->bytesused = vb->planes[0].bytesused;
 528		if (q->memory == VB2_MEMORY_MMAP)
 529			b->m.offset = vb->planes[0].m.offset;
 530		else if (q->memory == VB2_MEMORY_USERPTR)
 531			b->m.userptr = vb->planes[0].m.userptr;
 532		else if (q->memory == VB2_MEMORY_DMABUF)
 533			b->m.fd = vb->planes[0].m.fd;
 534	}
 535
 536	/*
 537	 * Clear any buffer state related flags.
 538	 */
 539	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
 540	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
 541	if (!q->copy_timestamp) {
 542		/*
 543		 * For non-COPY timestamps, drop timestamp source bits
 544		 * and obtain the timestamp source from the queue.
 545		 */
 546		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 547		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 548	}
 549
 550	switch (vb->state) {
 551	case VB2_BUF_STATE_QUEUED:
 552	case VB2_BUF_STATE_ACTIVE:
 553		b->flags |= V4L2_BUF_FLAG_QUEUED;
 554		break;
 555	case VB2_BUF_STATE_IN_REQUEST:
 556		b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
 557		break;
 558	case VB2_BUF_STATE_ERROR:
 559		b->flags |= V4L2_BUF_FLAG_ERROR;
 560		fallthrough;
 561	case VB2_BUF_STATE_DONE:
 562		b->flags |= V4L2_BUF_FLAG_DONE;
 563		break;
 564	case VB2_BUF_STATE_PREPARING:
 565	case VB2_BUF_STATE_DEQUEUED:
 566		/* nothing */
 567		break;
 568	}
 569
 570	if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
 571	     vb->state == VB2_BUF_STATE_IN_REQUEST) &&
 572	    vb->synced && vb->prepared)
 573		b->flags |= V4L2_BUF_FLAG_PREPARED;
 574
 575	if (vb2_buffer_in_use(q, vb))
 576		b->flags |= V4L2_BUF_FLAG_MAPPED;
 577	if (vbuf->request_fd >= 0) {
 578		b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
 579		b->request_fd = vbuf->request_fd;
 580	}
 581}
 582
 583/*
 584 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
 585 * v4l2_buffer by the userspace. It also verifies that struct
 586 * v4l2_buffer has a valid number of planes.
 587 */
 588static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
 589{
 590	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 591	unsigned int plane;
 592
 593	if (!vb->vb2_queue->copy_timestamp)
 594		vb->timestamp = 0;
 595
 596	for (plane = 0; plane < vb->num_planes; ++plane) {
 597		if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
 598			planes[plane].m = vbuf->planes[plane].m;
 599			planes[plane].length = vbuf->planes[plane].length;
 600		}
 601		planes[plane].bytesused = vbuf->planes[plane].bytesused;
 602		planes[plane].data_offset = vbuf->planes[plane].data_offset;
 603	}
 604	return 0;
 605}
 606
 607static const struct vb2_buf_ops v4l2_buf_ops = {
 608	.verify_planes_array	= __verify_planes_array_core,
 609	.init_buffer		= __init_vb2_v4l2_buffer,
 610	.fill_user_buffer	= __fill_v4l2_buffer,
 611	.fill_vb2_buffer	= __fill_vb2_buffer,
 612	.copy_timestamp		= __copy_timestamp,
 613};
 614
 615struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp)
 
 616{
 617	unsigned int i;
 618	struct vb2_buffer *vb2;
 619
 620	/*
 621	 * This loop doesn't scale if there is a really large number of buffers.
 622	 * Maybe something more efficient will be needed in this case.
 623	 */
 624	for (i = 0; i < q->max_num_buffers; i++) {
 625		vb2 = vb2_get_buffer(q, i);
 626
 627		if (!vb2)
 628			continue;
 629
 630		if (vb2->copied_timestamp &&
 631		    vb2->timestamp == timestamp)
 632			return vb2;
 633	}
 634	return NULL;
 635}
 636EXPORT_SYMBOL_GPL(vb2_find_buffer);
 637
 638/*
 639 * vb2_querybuf() - query video buffer information
 640 * @q:		vb2 queue
 641 * @b:		buffer struct passed from userspace to vidioc_querybuf handler
 642 *		in driver
 643 *
 644 * Should be called from vidioc_querybuf ioctl handler in driver.
 645 * This function will verify the passed v4l2_buffer structure and fill the
 646 * relevant information for the userspace.
 647 *
 648 * The return values from this function are intended to be directly returned
 649 * from vidioc_querybuf handler in driver.
 650 */
 651int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
 652{
 653	struct vb2_buffer *vb;
 654	int ret;
 655
 656	if (b->type != q->type) {
 657		dprintk(q, 1, "wrong buffer type\n");
 658		return -EINVAL;
 659	}
 660
 661	vb = vb2_get_buffer(q, b->index);
 662	if (!vb) {
 663		dprintk(q, 1, "can't find the requested buffer %u\n", b->index);
 664		return -EINVAL;
 665	}
 666
 667	ret = __verify_planes_array(vb, b);
 668	if (!ret)
 669		vb2_core_querybuf(q, vb, b);
 670	return ret;
 671}
 672EXPORT_SYMBOL(vb2_querybuf);
 673
 674static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory,
 675				   u32 *flags, u32 *caps, u32 *max_num_bufs)
 676{
 677	if (!q->allow_cache_hints || memory != V4L2_MEMORY_MMAP) {
 678		/*
 679		 * This needs to clear V4L2_MEMORY_FLAG_NON_COHERENT only,
 680		 * but in order to avoid bugs we zero out all bits.
 681		 */
 682		*flags = 0;
 683	} else {
 684		/* Clear all unknown flags. */
 685		*flags &= V4L2_MEMORY_FLAG_NON_COHERENT;
 686	}
 687
 688	*caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
 689	if (q->io_modes & VB2_MMAP)
 690		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
 691	if (q->io_modes & VB2_USERPTR)
 692		*caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
 693	if (q->io_modes & VB2_DMABUF)
 694		*caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
 695	if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
 696		*caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
 697	if (q->allow_cache_hints && q->io_modes & VB2_MMAP)
 698		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS;
 699	if (q->supports_requests)
 700		*caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
 701	if (max_num_bufs) {
 702		*max_num_bufs = q->max_num_buffers;
 703		*caps |= V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS;
 704	}
 705}
 706
 707int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
 708{
 709	int ret = vb2_verify_memory_type(q, req->memory, req->type);
 710	u32 flags = req->flags;
 711
 712	vb2_set_flags_and_caps(q, req->memory, &flags,
 713			       &req->capabilities, NULL);
 714	req->flags = flags;
 715	return ret ? ret : vb2_core_reqbufs(q, req->memory,
 716					    req->flags, &req->count);
 717}
 718EXPORT_SYMBOL_GPL(vb2_reqbufs);
 719
 720int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
 721		    struct v4l2_buffer *b)
 722{
 723	struct vb2_buffer *vb;
 724	int ret;
 725
 726	if (vb2_fileio_is_active(q)) {
 727		dprintk(q, 1, "file io in progress\n");
 728		return -EBUSY;
 729	}
 730
 731	if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
 732		return -EINVAL;
 733
 734	vb = vb2_get_buffer(q, b->index);
 735	if (!vb) {
 736		dprintk(q, 1, "can't find the requested buffer %u\n", b->index);
 737		return -EINVAL;
 738	}
 739
 740	ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, true, NULL);
 741
 742	return ret ? ret : vb2_core_prepare_buf(q, vb, b);
 743}
 744EXPORT_SYMBOL_GPL(vb2_prepare_buf);
 745
 746int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
 747{
 748	unsigned requested_planes = 1;
 749	unsigned requested_sizes[VIDEO_MAX_PLANES];
 750	struct v4l2_format *f = &create->format;
 751	int ret = vb2_verify_memory_type(q, create->memory, f->type);
 752	unsigned i;
 753
 754	create->index = vb2_get_num_buffers(q);
 755	vb2_set_flags_and_caps(q, create->memory, &create->flags,
 756			       &create->capabilities, &create->max_num_buffers);
 757	if (create->count == 0)
 758		return ret != -EBUSY ? ret : 0;
 759
 760	switch (f->type) {
 761	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 762	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 763		requested_planes = f->fmt.pix_mp.num_planes;
 764		if (requested_planes == 0 ||
 765		    requested_planes > VIDEO_MAX_PLANES)
 766			return -EINVAL;
 767		for (i = 0; i < requested_planes; i++)
 768			requested_sizes[i] =
 769				f->fmt.pix_mp.plane_fmt[i].sizeimage;
 770		break;
 771	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 772	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 773		requested_sizes[0] = f->fmt.pix.sizeimage;
 774		break;
 775	case V4L2_BUF_TYPE_VBI_CAPTURE:
 776	case V4L2_BUF_TYPE_VBI_OUTPUT:
 777		requested_sizes[0] = f->fmt.vbi.samples_per_line *
 778			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
 779		break;
 780	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 781	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 782		requested_sizes[0] = f->fmt.sliced.io_size;
 783		break;
 784	case V4L2_BUF_TYPE_SDR_CAPTURE:
 785	case V4L2_BUF_TYPE_SDR_OUTPUT:
 786		requested_sizes[0] = f->fmt.sdr.buffersize;
 787		break;
 788	case V4L2_BUF_TYPE_META_CAPTURE:
 789	case V4L2_BUF_TYPE_META_OUTPUT:
 790		requested_sizes[0] = f->fmt.meta.buffersize;
 791		break;
 792	default:
 793		return -EINVAL;
 794	}
 795	for (i = 0; i < requested_planes; i++)
 796		if (requested_sizes[i] == 0)
 797			return -EINVAL;
 798	return ret ? ret : vb2_core_create_bufs(q, create->memory,
 799						create->flags,
 800						&create->count,
 801						requested_planes,
 802						requested_sizes);
 803}
 804EXPORT_SYMBOL_GPL(vb2_create_bufs);
 805
 806int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
 807	     struct v4l2_buffer *b)
 808{
 809	struct media_request *req = NULL;
 810	struct vb2_buffer *vb;
 811	int ret;
 812
 813	if (vb2_fileio_is_active(q)) {
 814		dprintk(q, 1, "file io in progress\n");
 815		return -EBUSY;
 816	}
 817
 818	vb = vb2_get_buffer(q, b->index);
 819	if (!vb) {
 820		dprintk(q, 1, "can't find the requested buffer %u\n", b->index);
 821		return -EINVAL;
 822	}
 823
 824	ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, false, &req);
 825	if (ret)
 826		return ret;
 827	ret = vb2_core_qbuf(q, vb, b, req);
 828	if (req)
 829		media_request_put(req);
 830	return ret;
 831}
 832EXPORT_SYMBOL_GPL(vb2_qbuf);
 833
 834int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
 835{
 836	int ret;
 837
 838	if (vb2_fileio_is_active(q)) {
 839		dprintk(q, 1, "file io in progress\n");
 840		return -EBUSY;
 841	}
 842
 843	if (b->type != q->type) {
 844		dprintk(q, 1, "invalid buffer type\n");
 845		return -EINVAL;
 846	}
 847
 848	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
 849
 850	if (!q->is_output &&
 851	    b->flags & V4L2_BUF_FLAG_DONE &&
 852	    b->flags & V4L2_BUF_FLAG_LAST)
 853		q->last_buffer_dequeued = true;
 854
 855	/*
 856	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
 857	 *  cleared.
 858	 */
 859	b->flags &= ~V4L2_BUF_FLAG_DONE;
 860
 861	return ret;
 862}
 863EXPORT_SYMBOL_GPL(vb2_dqbuf);
 864
 865int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
 866{
 867	if (vb2_fileio_is_active(q)) {
 868		dprintk(q, 1, "file io in progress\n");
 869		return -EBUSY;
 870	}
 871	return vb2_core_streamon(q, type);
 872}
 873EXPORT_SYMBOL_GPL(vb2_streamon);
 874
 875int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
 876{
 877	if (vb2_fileio_is_active(q)) {
 878		dprintk(q, 1, "file io in progress\n");
 879		return -EBUSY;
 880	}
 881	return vb2_core_streamoff(q, type);
 882}
 883EXPORT_SYMBOL_GPL(vb2_streamoff);
 884
 885int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
 886{
 887	struct vb2_buffer *vb;
 888
 889	vb = vb2_get_buffer(q, eb->index);
 890	if (!vb) {
 891		dprintk(q, 1, "can't find the requested buffer %u\n", eb->index);
 892		return -EINVAL;
 893	}
 894
 895	return vb2_core_expbuf(q, &eb->fd, eb->type, vb,
 896				eb->plane, eb->flags);
 897}
 898EXPORT_SYMBOL_GPL(vb2_expbuf);
 899
 900int vb2_queue_init_name(struct vb2_queue *q, const char *name)
 901{
 902	/*
 903	 * Sanity check
 904	 */
 905	if (WARN_ON(!q)			  ||
 906	    WARN_ON(q->timestamp_flags &
 907		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
 908		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
 909		return -EINVAL;
 910
 911	/* Warn that the driver should choose an appropriate timestamp type */
 912	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
 913		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
 914
 915	/* Warn that vb2_memory should match with v4l2_memory */
 916	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
 917		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
 918		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
 919		return -EINVAL;
 920
 921	if (q->buf_struct_size == 0)
 922		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
 923
 924	q->buf_ops = &v4l2_buf_ops;
 925	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
 926	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
 927	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
 928			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
 929	/*
 930	 * For compatibility with vb1: if QBUF hasn't been called yet, then
 931	 * return EPOLLERR as well. This only affects capture queues, output
 932	 * queues will always initialize waiting_for_buffers to false.
 933	 */
 934	q->quirk_poll_must_check_waiting_for_buffers = true;
 935
 936	if (name)
 937		strscpy(q->name, name, sizeof(q->name));
 938	else
 939		q->name[0] = '\0';
 940
 941	return vb2_core_queue_init(q);
 942}
 943EXPORT_SYMBOL_GPL(vb2_queue_init_name);
 944
 945int vb2_queue_init(struct vb2_queue *q)
 946{
 947	return vb2_queue_init_name(q, NULL);
 948}
 949EXPORT_SYMBOL_GPL(vb2_queue_init);
 950
 951void vb2_queue_release(struct vb2_queue *q)
 952{
 953	vb2_core_queue_release(q);
 954}
 955EXPORT_SYMBOL_GPL(vb2_queue_release);
 956
 957int vb2_queue_change_type(struct vb2_queue *q, unsigned int type)
 958{
 959	if (type == q->type)
 960		return 0;
 961
 962	if (vb2_is_busy(q))
 963		return -EBUSY;
 964
 965	q->type = type;
 966
 967	return 0;
 968}
 969EXPORT_SYMBOL_GPL(vb2_queue_change_type);
 970
 971__poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
 972{
 973	struct video_device *vfd = video_devdata(file);
 974	__poll_t res;
 975
 976	res = vb2_core_poll(q, file, wait);
 977
 978	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
 979		struct v4l2_fh *fh = file->private_data;
 980
 981		poll_wait(file, &fh->wait, wait);
 982		if (v4l2_event_pending(fh))
 983			res |= EPOLLPRI;
 984	}
 985
 986	return res;
 987}
 988EXPORT_SYMBOL_GPL(vb2_poll);
 989
 990/*
 991 * The following functions are not part of the vb2 core API, but are helper
 992 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
 993 * and struct vb2_ops.
 994 * They contain boilerplate code that most if not all drivers have to do
 995 * and so they simplify the driver code.
 996 */
 997
 
 
 
 
 
 
 998/* vb2 ioctl helpers */
 999
1000int vb2_ioctl_reqbufs(struct file *file, void *priv,
1001			  struct v4l2_requestbuffers *p)
1002{
1003	struct video_device *vdev = video_devdata(file);
1004	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
1005	u32 flags = p->flags;
1006
1007	vb2_set_flags_and_caps(vdev->queue, p->memory, &flags,
1008			       &p->capabilities, NULL);
1009	p->flags = flags;
1010	if (res)
1011		return res;
1012	if (vb2_queue_is_busy(vdev->queue, file))
1013		return -EBUSY;
1014	res = vb2_core_reqbufs(vdev->queue, p->memory, p->flags, &p->count);
1015	/* If count == 0, then the owner has released all buffers and he
1016	   is no longer owner of the queue. Otherwise we have a new owner. */
1017	if (res == 0)
1018		vdev->queue->owner = p->count ? file->private_data : NULL;
1019	return res;
1020}
1021EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1022
1023int vb2_ioctl_create_bufs(struct file *file, void *priv,
1024			  struct v4l2_create_buffers *p)
1025{
1026	struct video_device *vdev = video_devdata(file);
1027	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->format.type);
 
1028
1029	p->index = vb2_get_num_buffers(vdev->queue);
1030	vb2_set_flags_and_caps(vdev->queue, p->memory, &p->flags,
1031			       &p->capabilities, &p->max_num_buffers);
1032	/*
1033	 * If count == 0, then just check if memory and type are valid.
1034	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1035	 */
1036	if (p->count == 0)
1037		return res != -EBUSY ? res : 0;
1038	if (res)
1039		return res;
1040	if (vb2_queue_is_busy(vdev->queue, file))
1041		return -EBUSY;
1042
1043	res = vb2_create_bufs(vdev->queue, p);
1044	if (res == 0)
1045		vdev->queue->owner = file->private_data;
1046	return res;
1047}
1048EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1049
1050int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1051			  struct v4l2_buffer *p)
1052{
1053	struct video_device *vdev = video_devdata(file);
1054
1055	if (vb2_queue_is_busy(vdev->queue, file))
1056		return -EBUSY;
1057	return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
1058}
1059EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1060
1061int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1062{
1063	struct video_device *vdev = video_devdata(file);
1064
1065	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1066	return vb2_querybuf(vdev->queue, p);
1067}
1068EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1069
1070int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1071{
1072	struct video_device *vdev = video_devdata(file);
1073
1074	if (vb2_queue_is_busy(vdev->queue, file))
1075		return -EBUSY;
1076	return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
1077}
1078EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1079
1080int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1081{
1082	struct video_device *vdev = video_devdata(file);
1083
1084	if (vb2_queue_is_busy(vdev->queue, file))
1085		return -EBUSY;
1086	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1087}
1088EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1089
1090int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1091{
1092	struct video_device *vdev = video_devdata(file);
1093
1094	if (vb2_queue_is_busy(vdev->queue, file))
1095		return -EBUSY;
1096	return vb2_streamon(vdev->queue, i);
1097}
1098EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1099
1100int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1101{
1102	struct video_device *vdev = video_devdata(file);
1103
1104	if (vb2_queue_is_busy(vdev->queue, file))
1105		return -EBUSY;
1106	return vb2_streamoff(vdev->queue, i);
1107}
1108EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1109
1110int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1111{
1112	struct video_device *vdev = video_devdata(file);
1113
1114	if (vb2_queue_is_busy(vdev->queue, file))
1115		return -EBUSY;
1116	return vb2_expbuf(vdev->queue, p);
1117}
1118EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1119
1120/* v4l2_file_operations helpers */
1121
1122int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1123{
1124	struct video_device *vdev = video_devdata(file);
1125
1126	return vb2_mmap(vdev->queue, vma);
1127}
1128EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1129
1130int _vb2_fop_release(struct file *file, struct mutex *lock)
1131{
1132	struct video_device *vdev = video_devdata(file);
1133
1134	if (lock)
1135		mutex_lock(lock);
1136	if (!vdev->queue->owner || file->private_data == vdev->queue->owner) {
1137		vb2_queue_release(vdev->queue);
1138		vdev->queue->owner = NULL;
1139	}
1140	if (lock)
1141		mutex_unlock(lock);
1142	return v4l2_fh_release(file);
1143}
1144EXPORT_SYMBOL_GPL(_vb2_fop_release);
1145
1146int vb2_fop_release(struct file *file)
1147{
1148	struct video_device *vdev = video_devdata(file);
1149	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1150
1151	return _vb2_fop_release(file, lock);
1152}
1153EXPORT_SYMBOL_GPL(vb2_fop_release);
1154
1155ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1156		size_t count, loff_t *ppos)
1157{
1158	struct video_device *vdev = video_devdata(file);
1159	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1160	int err = -EBUSY;
1161
1162	if (!(vdev->queue->io_modes & VB2_WRITE))
1163		return -EINVAL;
1164	if (lock && mutex_lock_interruptible(lock))
1165		return -ERESTARTSYS;
1166	if (vb2_queue_is_busy(vdev->queue, file))
1167		goto exit;
1168	err = vb2_write(vdev->queue, buf, count, ppos,
1169		       file->f_flags & O_NONBLOCK);
1170	if (vdev->queue->fileio)
1171		vdev->queue->owner = file->private_data;
1172exit:
1173	if (lock)
1174		mutex_unlock(lock);
1175	return err;
1176}
1177EXPORT_SYMBOL_GPL(vb2_fop_write);
1178
1179ssize_t vb2_fop_read(struct file *file, char __user *buf,
1180		size_t count, loff_t *ppos)
1181{
1182	struct video_device *vdev = video_devdata(file);
1183	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1184	int err = -EBUSY;
1185
1186	if (!(vdev->queue->io_modes & VB2_READ))
1187		return -EINVAL;
1188	if (lock && mutex_lock_interruptible(lock))
1189		return -ERESTARTSYS;
1190	if (vb2_queue_is_busy(vdev->queue, file))
1191		goto exit;
1192	vdev->queue->owner = file->private_data;
1193	err = vb2_read(vdev->queue, buf, count, ppos,
1194		       file->f_flags & O_NONBLOCK);
1195	if (!vdev->queue->fileio)
1196		vdev->queue->owner = NULL;
1197exit:
1198	if (lock)
1199		mutex_unlock(lock);
1200	return err;
1201}
1202EXPORT_SYMBOL_GPL(vb2_fop_read);
1203
1204__poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1205{
1206	struct video_device *vdev = video_devdata(file);
1207	struct vb2_queue *q = vdev->queue;
1208	struct mutex *lock = q->lock ? q->lock : vdev->lock;
1209	__poll_t res;
1210	void *fileio;
1211
1212	/*
1213	 * If this helper doesn't know how to lock, then you shouldn't be using
1214	 * it but you should write your own.
1215	 */
1216	WARN_ON(!lock);
1217
1218	if (lock && mutex_lock_interruptible(lock))
1219		return EPOLLERR;
1220
1221	fileio = q->fileio;
1222
1223	res = vb2_poll(vdev->queue, file, wait);
1224
1225	/* If fileio was started, then we have a new queue owner. */
1226	if (!fileio && q->fileio)
1227		q->owner = file->private_data;
1228	if (lock)
1229		mutex_unlock(lock);
1230	return res;
1231}
1232EXPORT_SYMBOL_GPL(vb2_fop_poll);
1233
1234#ifndef CONFIG_MMU
1235unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1236		unsigned long len, unsigned long pgoff, unsigned long flags)
1237{
1238	struct video_device *vdev = video_devdata(file);
1239
1240	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1241}
1242EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1243#endif
1244
1245void vb2_video_unregister_device(struct video_device *vdev)
1246{
1247	/* Check if vdev was ever registered at all */
1248	if (!vdev || !video_is_registered(vdev))
1249		return;
1250
1251	/*
1252	 * Calling this function only makes sense if vdev->queue is set.
1253	 * If it is NULL, then just call video_unregister_device() instead.
1254	 */
1255	WARN_ON(!vdev->queue);
1256
1257	/*
1258	 * Take a reference to the device since video_unregister_device()
1259	 * calls device_unregister(), but we don't want that to release
1260	 * the device since we want to clean up the queue first.
1261	 */
1262	get_device(&vdev->dev);
1263	video_unregister_device(vdev);
1264	if (vdev->queue) {
1265		struct mutex *lock = vdev->queue->lock ?
1266			vdev->queue->lock : vdev->lock;
1267
1268		if (lock)
1269			mutex_lock(lock);
1270		vb2_queue_release(vdev->queue);
1271		vdev->queue->owner = NULL;
1272		if (lock)
1273			mutex_unlock(lock);
1274	}
1275	/*
1276	 * Now we put the device, and in most cases this will release
1277	 * everything.
1278	 */
1279	put_device(&vdev->dev);
1280}
1281EXPORT_SYMBOL_GPL(vb2_video_unregister_device);
1282
1283/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1284
1285void vb2_ops_wait_prepare(struct vb2_queue *vq)
1286{
1287	mutex_unlock(vq->lock);
1288}
1289EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1290
1291void vb2_ops_wait_finish(struct vb2_queue *vq)
1292{
1293	mutex_lock(vq->lock);
1294}
1295EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1296
1297/*
1298 * Note that this function is called during validation time and
1299 * thus the req_queue_mutex is held to ensure no request objects
1300 * can be added or deleted while validating. So there is no need
1301 * to protect the objects list.
1302 */
1303int vb2_request_validate(struct media_request *req)
1304{
1305	struct media_request_object *obj;
1306	int ret = 0;
1307
1308	if (!vb2_request_buffer_cnt(req))
1309		return -ENOENT;
1310
1311	list_for_each_entry(obj, &req->objects, list) {
1312		if (!obj->ops->prepare)
1313			continue;
1314
1315		ret = obj->ops->prepare(obj);
1316		if (ret)
1317			break;
1318	}
1319
1320	if (ret) {
1321		list_for_each_entry_continue_reverse(obj, &req->objects, list)
1322			if (obj->ops->unprepare)
1323				obj->ops->unprepare(obj);
1324		return ret;
1325	}
1326	return 0;
1327}
1328EXPORT_SYMBOL_GPL(vb2_request_validate);
1329
1330void vb2_request_queue(struct media_request *req)
1331{
1332	struct media_request_object *obj, *obj_safe;
1333
1334	/*
1335	 * Queue all objects. Note that buffer objects are at the end of the
1336	 * objects list, after all other object types. Once buffer objects
1337	 * are queued, the driver might delete them immediately (if the driver
1338	 * processes the buffer at once), so we have to use
1339	 * list_for_each_entry_safe() to handle the case where the object we
1340	 * queue is deleted.
1341	 */
1342	list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1343		if (obj->ops->queue)
1344			obj->ops->queue(obj);
1345}
1346EXPORT_SYMBOL_GPL(vb2_request_queue);
1347
1348MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1349MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1350MODULE_LICENSE("GPL");
v5.4
   1/*
   2 * videobuf2-v4l2.c - V4L2 driver helper framework
   3 *
   4 * Copyright (C) 2010 Samsung Electronics
   5 *
   6 * Author: Pawel Osciak <pawel@osciak.com>
   7 *	   Marek Szyprowski <m.szyprowski@samsung.com>
   8 *
   9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
  10 *	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  11 *
  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.
  15 */
  16
 
  17#include <linux/err.h>
 
  18#include <linux/kernel.h>
 
 
  19#include <linux/module.h>
  20#include <linux/mm.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>
  26
 
  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>
  32
  33#include <media/videobuf2-v4l2.h>
  34
  35static int debug;
  36module_param(debug, int, 0644);
  37
  38#define dprintk(level, fmt, arg...)					      \
  39	do {								      \
  40		if (debug >= level)					      \
  41			pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
 
  42	} while (0)
  43
  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)
 
 
 
  54
  55/*
  56 * __verify_planes_array() - verify that the planes array passed in struct
  57 * v4l2_buffer from userspace can be safely used
  58 */
  59static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  60{
  61	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  62		return 0;
  63
  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");
 
  67		return -EINVAL;
  68	}
  69
  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);
  73		return -EINVAL;
  74	}
  75
  76	return 0;
  77}
  78
  79static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
  80{
  81	return __verify_planes_array(vb, pb);
  82}
  83
  84/*
  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.
  87 */
  88static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  89{
  90	unsigned int length;
  91	unsigned int bytesused;
  92	unsigned int plane;
  93
  94	if (!V4L2_TYPE_IS_OUTPUT(b->type))
  95		return 0;
  96
  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;
 105
 106			if (b->m.planes[plane].bytesused > length)
 107				return -EINVAL;
 108
 109			if (b->m.planes[plane].data_offset > 0 &&
 110			    b->m.planes[plane].data_offset >= bytesused)
 111				return -EINVAL;
 112		}
 113	} else {
 114		length = (b->memory == VB2_MEMORY_USERPTR)
 115			? b->length : vb->planes[0].length;
 116
 117		if (b->bytesused > length)
 118			return -EINVAL;
 119	}
 120
 121	return 0;
 122}
 123
 124/*
 125 * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
 126 */
 127static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb)
 128{
 129	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 130
 131	vbuf->request_fd = -1;
 132}
 133
 134static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
 135{
 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;
 139
 140	if (q->is_output) {
 141		/*
 142		 * For output buffers copy the timestamp if needed,
 143		 * and the timecode field and flag if needed.
 144		 */
 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;
 150	}
 151};
 152
 153static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
 154{
 155	static bool check_once;
 156
 157	if (check_once)
 158		return;
 159
 160	check_once = true;
 161
 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");
 165	else
 166		pr_warn("use the actual size instead.\n");
 167}
 168
 169static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
 170{
 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;
 174	unsigned int plane;
 175	int ret;
 176
 177	ret = __verify_length(vb, b);
 178	if (ret < 0) {
 179		dprintk(1, "plane parameters verification failed: %d\n", ret);
 180		return ret;
 181	}
 182	if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
 183		/*
 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.
 191		 */
 192		dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
 193		return -EINVAL;
 194	}
 195	vbuf->sequence = 0;
 196	vbuf->request_fd = -1;
 
 197
 198	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
 199		switch (b->memory) {
 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;
 206			}
 207			break;
 208		case VB2_MEMORY_DMABUF:
 209			for (plane = 0; plane < vb->num_planes; ++plane) {
 210				planes[plane].m.fd =
 211					b->m.planes[plane].m.fd;
 212				planes[plane].length =
 213					b->m.planes[plane].length;
 214			}
 215			break;
 216		default:
 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;
 222			}
 223			break;
 224		}
 225
 226		/* Fill in driver-provided information for OUTPUT types */
 227		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
 228			/*
 229			 * Will have to go up to b->length when API starts
 230			 * accepting variable number of planes.
 231			 *
 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.
 237			 *
 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.
 243			 */
 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];
 247
 248				if (psrc->bytesused == 0)
 249					vb2_warn_zero_bytesused(vb);
 250
 251				if (vb->vb2_queue->allow_zero_bytesused)
 252					pdst->bytesused = psrc->bytesused;
 253				else
 254					pdst->bytesused = psrc->bytesused ?
 255						psrc->bytesused : pdst->length;
 256				pdst->data_offset = psrc->data_offset;
 257			}
 258		}
 259	} else {
 260		/*
 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.
 265		 *
 266		 * If bytesused == 0 for the output buffer, then fall back
 267		 * to the full buffer size as that's a sensible default.
 268		 *
 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.
 273		 */
 274		switch (b->memory) {
 275		case VB2_MEMORY_USERPTR:
 276			planes[0].m.userptr = b->m.userptr;
 277			planes[0].length = b->length;
 278			break;
 279		case VB2_MEMORY_DMABUF:
 280			planes[0].m.fd = b->m.fd;
 281			planes[0].length = b->length;
 282			break;
 283		default:
 284			planes[0].m.offset = vb->planes[0].m.offset;
 285			planes[0].length = vb->planes[0].length;
 286			break;
 287		}
 288
 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);
 293
 294			if (vb->vb2_queue->allow_zero_bytesused)
 295				planes[0].bytesused = b->bytesused;
 296			else
 297				planes[0].bytesused = b->bytesused ?
 298					b->bytesused : planes[0].length;
 299		} else
 300			planes[0].bytesused = 0;
 301
 302	}
 303
 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)) {
 307		/*
 308		 * Non-COPY timestamps and non-OUTPUT queues will get
 309		 * their timestamp and timestamp source flags from the
 310		 * queue.
 311		 */
 312		vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 313	}
 314
 315	if (V4L2_TYPE_IS_OUTPUT(b->type)) {
 316		/*
 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.
 321		 */
 322		vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
 323		vbuf->field = b->field;
 
 
 324	} else {
 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;
 329	}
 330
 331	return 0;
 332}
 333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 334static 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)
 337{
 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;
 342	int ret;
 343
 344	if (b->type != q->type) {
 345		dprintk(1, "%s: invalid buffer type\n", opname);
 346		return -EINVAL;
 347	}
 348
 349	if (b->index >= q->num_buffers) {
 350		dprintk(1, "%s: buffer index out of range\n", opname);
 351		return -EINVAL;
 352	}
 353
 354	if (q->bufs[b->index] == NULL) {
 355		/* Should never happen */
 356		dprintk(1, "%s: buffer is NULL\n", opname);
 357		return -EINVAL;
 358	}
 359
 360	if (b->memory != q->memory) {
 361		dprintk(1, "%s: invalid memory type\n", opname);
 362		return -EINVAL;
 363	}
 364
 365	vb = q->bufs[b->index];
 366	vbuf = to_vb2_v4l2_buffer(vb);
 367	ret = __verify_planes_array(vb, b);
 368	if (ret)
 369		return ret;
 370
 371	if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
 372	    vb->state != VB2_BUF_STATE_DEQUEUED) {
 373		dprintk(1, "%s: buffer is not in dequeued state\n", opname);
 374		return -EINVAL;
 375	}
 376
 377	if (!vb->prepared) {
 
 378		/* Copy relevant information provided by the userspace */
 379		memset(vbuf->planes, 0,
 380		       sizeof(vbuf->planes[0]) * vb->num_planes);
 381		ret = vb2_fill_vb2_v4l2_buffer(vb, b);
 382		if (ret)
 383			return ret;
 384	}
 385
 386	if (is_prepare)
 387		return 0;
 388
 389	if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
 390		if (q->requires_requests) {
 391			dprintk(1, "%s: queue requires requests\n", opname);
 392			return -EBADR;
 393		}
 394		if (q->uses_requests) {
 395			dprintk(1, "%s: queue uses requests\n", opname);
 396			return -EBUSY;
 397		}
 398		return 0;
 399	} else if (!q->supports_requests) {
 400		dprintk(1, "%s: queue does not support requests\n", opname);
 401		return -EBADR;
 402	} else if (q->uses_qbuf) {
 403		dprintk(1, "%s: queue does not use requests\n", opname);
 404		return -EBUSY;
 405	}
 406
 407	/*
 408	 * For proper locking when queueing a request you need to be able
 409	 * to lock access to the vb2 queue, so check that there is a lock
 410	 * that we can use. In addition p_req must be non-NULL.
 411	 */
 412	if (WARN_ON(!q->lock || !p_req))
 413		return -EINVAL;
 414
 415	/*
 416	 * Make sure this op is implemented by the driver. It's easy to forget
 417	 * this callback, but is it important when canceling a buffer in a
 418	 * queued request.
 419	 */
 420	if (WARN_ON(!q->ops->buf_request_complete))
 421		return -EINVAL;
 422	/*
 423	 * Make sure this op is implemented by the driver for the output queue.
 424	 * It's easy to forget this callback, but is it important to correctly
 425	 * validate the 'field' value at QBUF time.
 426	 */
 427	if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 428		     q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
 429		    !q->ops->buf_out_validate))
 430		return -EINVAL;
 431
 432	if (b->request_fd < 0) {
 433		dprintk(1, "%s: request_fd < 0\n", opname);
 434		return -EINVAL;
 435	}
 436
 437	req = media_request_get_by_fd(mdev, b->request_fd);
 438	if (IS_ERR(req)) {
 439		dprintk(1, "%s: invalid request_fd\n", opname);
 440		return PTR_ERR(req);
 441	}
 442
 443	/*
 444	 * Early sanity check. This is checked again when the buffer
 445	 * is bound to the request in vb2_core_qbuf().
 446	 */
 447	if (req->state != MEDIA_REQUEST_STATE_IDLE &&
 448	    req->state != MEDIA_REQUEST_STATE_UPDATING) {
 449		dprintk(1, "%s: request is not idle\n", opname);
 450		media_request_put(req);
 451		return -EBUSY;
 452	}
 453
 454	*p_req = req;
 455	vbuf->request_fd = b->request_fd;
 456
 457	return 0;
 458}
 459
 460/*
 461 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
 462 * returned to userspace
 463 */
 464static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
 465{
 466	struct v4l2_buffer *b = pb;
 467	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 468	struct vb2_queue *q = vb->vb2_queue;
 469	unsigned int plane;
 470
 471	/* Copy back data such as timestamp, flags, etc. */
 472	b->index = vb->index;
 473	b->type = vb->type;
 474	b->memory = vb->memory;
 475	b->bytesused = 0;
 476
 477	b->flags = vbuf->flags;
 478	b->field = vbuf->field;
 479	b->timestamp = ns_to_timeval(vb->timestamp);
 480	b->timecode = vbuf->timecode;
 481	b->sequence = vbuf->sequence;
 482	b->reserved2 = 0;
 483	b->request_fd = 0;
 484
 485	if (q->is_multiplanar) {
 486		/*
 487		 * Fill in plane-related data if userspace provided an array
 488		 * for it. The caller has already verified memory and size.
 489		 */
 490		b->length = vb->num_planes;
 491		for (plane = 0; plane < vb->num_planes; ++plane) {
 492			struct v4l2_plane *pdst = &b->m.planes[plane];
 493			struct vb2_plane *psrc = &vb->planes[plane];
 494
 495			pdst->bytesused = psrc->bytesused;
 496			pdst->length = psrc->length;
 497			if (q->memory == VB2_MEMORY_MMAP)
 498				pdst->m.mem_offset = psrc->m.offset;
 499			else if (q->memory == VB2_MEMORY_USERPTR)
 500				pdst->m.userptr = psrc->m.userptr;
 501			else if (q->memory == VB2_MEMORY_DMABUF)
 502				pdst->m.fd = psrc->m.fd;
 503			pdst->data_offset = psrc->data_offset;
 504			memset(pdst->reserved, 0, sizeof(pdst->reserved));
 505		}
 506	} else {
 507		/*
 508		 * We use length and offset in v4l2_planes array even for
 509		 * single-planar buffers, but userspace does not.
 510		 */
 511		b->length = vb->planes[0].length;
 512		b->bytesused = vb->planes[0].bytesused;
 513		if (q->memory == VB2_MEMORY_MMAP)
 514			b->m.offset = vb->planes[0].m.offset;
 515		else if (q->memory == VB2_MEMORY_USERPTR)
 516			b->m.userptr = vb->planes[0].m.userptr;
 517		else if (q->memory == VB2_MEMORY_DMABUF)
 518			b->m.fd = vb->planes[0].m.fd;
 519	}
 520
 521	/*
 522	 * Clear any buffer state related flags.
 523	 */
 524	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
 525	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
 526	if (!q->copy_timestamp) {
 527		/*
 528		 * For non-COPY timestamps, drop timestamp source bits
 529		 * and obtain the timestamp source from the queue.
 530		 */
 531		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 532		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 533	}
 534
 535	switch (vb->state) {
 536	case VB2_BUF_STATE_QUEUED:
 537	case VB2_BUF_STATE_ACTIVE:
 538		b->flags |= V4L2_BUF_FLAG_QUEUED;
 539		break;
 540	case VB2_BUF_STATE_IN_REQUEST:
 541		b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
 542		break;
 543	case VB2_BUF_STATE_ERROR:
 544		b->flags |= V4L2_BUF_FLAG_ERROR;
 545		/* fall through */
 546	case VB2_BUF_STATE_DONE:
 547		b->flags |= V4L2_BUF_FLAG_DONE;
 548		break;
 549	case VB2_BUF_STATE_PREPARING:
 550	case VB2_BUF_STATE_DEQUEUED:
 551		/* nothing */
 552		break;
 553	}
 554
 555	if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
 556	     vb->state == VB2_BUF_STATE_IN_REQUEST) &&
 557	    vb->synced && vb->prepared)
 558		b->flags |= V4L2_BUF_FLAG_PREPARED;
 559
 560	if (vb2_buffer_in_use(q, vb))
 561		b->flags |= V4L2_BUF_FLAG_MAPPED;
 562	if (vbuf->request_fd >= 0) {
 563		b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
 564		b->request_fd = vbuf->request_fd;
 565	}
 566}
 567
 568/*
 569 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
 570 * v4l2_buffer by the userspace. It also verifies that struct
 571 * v4l2_buffer has a valid number of planes.
 572 */
 573static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
 574{
 575	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 576	unsigned int plane;
 577
 578	if (!vb->vb2_queue->copy_timestamp)
 579		vb->timestamp = 0;
 580
 581	for (plane = 0; plane < vb->num_planes; ++plane) {
 582		if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
 583			planes[plane].m = vbuf->planes[plane].m;
 584			planes[plane].length = vbuf->planes[plane].length;
 585		}
 586		planes[plane].bytesused = vbuf->planes[plane].bytesused;
 587		planes[plane].data_offset = vbuf->planes[plane].data_offset;
 588	}
 589	return 0;
 590}
 591
 592static const struct vb2_buf_ops v4l2_buf_ops = {
 593	.verify_planes_array	= __verify_planes_array_core,
 594	.init_buffer		= __init_vb2_v4l2_buffer,
 595	.fill_user_buffer	= __fill_v4l2_buffer,
 596	.fill_vb2_buffer	= __fill_vb2_buffer,
 597	.copy_timestamp		= __copy_timestamp,
 598};
 599
 600int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
 601		       unsigned int start_idx)
 602{
 603	unsigned int i;
 
 
 
 
 
 
 
 
 
 
 
 604
 605	for (i = start_idx; i < q->num_buffers; i++)
 606		if (q->bufs[i]->copied_timestamp &&
 607		    q->bufs[i]->timestamp == timestamp)
 608			return i;
 609	return -1;
 610}
 611EXPORT_SYMBOL_GPL(vb2_find_timestamp);
 612
 613/*
 614 * vb2_querybuf() - query video buffer information
 615 * @q:		videobuf queue
 616 * @b:		buffer struct passed from userspace to vidioc_querybuf handler
 617 *		in driver
 618 *
 619 * Should be called from vidioc_querybuf ioctl handler in driver.
 620 * This function will verify the passed v4l2_buffer structure and fill the
 621 * relevant information for the userspace.
 622 *
 623 * The return values from this function are intended to be directly returned
 624 * from vidioc_querybuf handler in driver.
 625 */
 626int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
 627{
 628	struct vb2_buffer *vb;
 629	int ret;
 630
 631	if (b->type != q->type) {
 632		dprintk(1, "wrong buffer type\n");
 633		return -EINVAL;
 634	}
 635
 636	if (b->index >= q->num_buffers) {
 637		dprintk(1, "buffer index out of range\n");
 
 638		return -EINVAL;
 639	}
 640	vb = q->bufs[b->index];
 641	ret = __verify_planes_array(vb, b);
 642	if (!ret)
 643		vb2_core_querybuf(q, b->index, b);
 644	return ret;
 645}
 646EXPORT_SYMBOL(vb2_querybuf);
 647
 648static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
 
 649{
 
 
 
 
 
 
 
 
 
 
 
 650	*caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
 651	if (q->io_modes & VB2_MMAP)
 652		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
 653	if (q->io_modes & VB2_USERPTR)
 654		*caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
 655	if (q->io_modes & VB2_DMABUF)
 656		*caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
 657#ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
 
 
 
 658	if (q->supports_requests)
 659		*caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
 660#endif
 
 
 
 661}
 662
 663int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
 664{
 665	int ret = vb2_verify_memory_type(q, req->memory, req->type);
 
 666
 667	fill_buf_caps(q, &req->capabilities);
 668	return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
 
 
 
 669}
 670EXPORT_SYMBOL_GPL(vb2_reqbufs);
 671
 672int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
 673		    struct v4l2_buffer *b)
 674{
 
 675	int ret;
 676
 677	if (vb2_fileio_is_active(q)) {
 678		dprintk(1, "file io in progress\n");
 679		return -EBUSY;
 680	}
 681
 682	if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
 683		return -EINVAL;
 684
 685	ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
 
 
 
 
 
 
 686
 687	return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
 688}
 689EXPORT_SYMBOL_GPL(vb2_prepare_buf);
 690
 691int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
 692{
 693	unsigned requested_planes = 1;
 694	unsigned requested_sizes[VIDEO_MAX_PLANES];
 695	struct v4l2_format *f = &create->format;
 696	int ret = vb2_verify_memory_type(q, create->memory, f->type);
 697	unsigned i;
 698
 699	fill_buf_caps(q, &create->capabilities);
 700	create->index = q->num_buffers;
 
 701	if (create->count == 0)
 702		return ret != -EBUSY ? ret : 0;
 703
 704	switch (f->type) {
 705	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 706	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 707		requested_planes = f->fmt.pix_mp.num_planes;
 708		if (requested_planes == 0 ||
 709		    requested_planes > VIDEO_MAX_PLANES)
 710			return -EINVAL;
 711		for (i = 0; i < requested_planes; i++)
 712			requested_sizes[i] =
 713				f->fmt.pix_mp.plane_fmt[i].sizeimage;
 714		break;
 715	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 716	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 717		requested_sizes[0] = f->fmt.pix.sizeimage;
 718		break;
 719	case V4L2_BUF_TYPE_VBI_CAPTURE:
 720	case V4L2_BUF_TYPE_VBI_OUTPUT:
 721		requested_sizes[0] = f->fmt.vbi.samples_per_line *
 722			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
 723		break;
 724	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
 725	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
 726		requested_sizes[0] = f->fmt.sliced.io_size;
 727		break;
 728	case V4L2_BUF_TYPE_SDR_CAPTURE:
 729	case V4L2_BUF_TYPE_SDR_OUTPUT:
 730		requested_sizes[0] = f->fmt.sdr.buffersize;
 731		break;
 732	case V4L2_BUF_TYPE_META_CAPTURE:
 733	case V4L2_BUF_TYPE_META_OUTPUT:
 734		requested_sizes[0] = f->fmt.meta.buffersize;
 735		break;
 736	default:
 737		return -EINVAL;
 738	}
 739	for (i = 0; i < requested_planes; i++)
 740		if (requested_sizes[i] == 0)
 741			return -EINVAL;
 742	return ret ? ret : vb2_core_create_bufs(q, create->memory,
 743		&create->count, requested_planes, requested_sizes);
 
 
 
 744}
 745EXPORT_SYMBOL_GPL(vb2_create_bufs);
 746
 747int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
 748	     struct v4l2_buffer *b)
 749{
 750	struct media_request *req = NULL;
 
 751	int ret;
 752
 753	if (vb2_fileio_is_active(q)) {
 754		dprintk(1, "file io in progress\n");
 755		return -EBUSY;
 756	}
 757
 758	ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
 
 
 
 
 
 
 759	if (ret)
 760		return ret;
 761	ret = vb2_core_qbuf(q, b->index, b, req);
 762	if (req)
 763		media_request_put(req);
 764	return ret;
 765}
 766EXPORT_SYMBOL_GPL(vb2_qbuf);
 767
 768int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
 769{
 770	int ret;
 771
 772	if (vb2_fileio_is_active(q)) {
 773		dprintk(1, "file io in progress\n");
 774		return -EBUSY;
 775	}
 776
 777	if (b->type != q->type) {
 778		dprintk(1, "invalid buffer type\n");
 779		return -EINVAL;
 780	}
 781
 782	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
 783
 784	if (!q->is_output &&
 785	    b->flags & V4L2_BUF_FLAG_DONE &&
 786	    b->flags & V4L2_BUF_FLAG_LAST)
 787		q->last_buffer_dequeued = true;
 788
 789	/*
 790	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
 791	 *  cleared.
 792	 */
 793	b->flags &= ~V4L2_BUF_FLAG_DONE;
 794
 795	return ret;
 796}
 797EXPORT_SYMBOL_GPL(vb2_dqbuf);
 798
 799int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
 800{
 801	if (vb2_fileio_is_active(q)) {
 802		dprintk(1, "file io in progress\n");
 803		return -EBUSY;
 804	}
 805	return vb2_core_streamon(q, type);
 806}
 807EXPORT_SYMBOL_GPL(vb2_streamon);
 808
 809int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
 810{
 811	if (vb2_fileio_is_active(q)) {
 812		dprintk(1, "file io in progress\n");
 813		return -EBUSY;
 814	}
 815	return vb2_core_streamoff(q, type);
 816}
 817EXPORT_SYMBOL_GPL(vb2_streamoff);
 818
 819int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
 820{
 821	return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
 
 
 
 
 
 
 
 
 822				eb->plane, eb->flags);
 823}
 824EXPORT_SYMBOL_GPL(vb2_expbuf);
 825
 826int vb2_queue_init(struct vb2_queue *q)
 827{
 828	/*
 829	 * Sanity check
 830	 */
 831	if (WARN_ON(!q)			  ||
 832	    WARN_ON(q->timestamp_flags &
 833		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
 834		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
 835		return -EINVAL;
 836
 837	/* Warn that the driver should choose an appropriate timestamp type */
 838	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
 839		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
 840
 841	/* Warn that vb2_memory should match with v4l2_memory */
 842	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
 843		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
 844		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
 845		return -EINVAL;
 846
 847	if (q->buf_struct_size == 0)
 848		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
 849
 850	q->buf_ops = &v4l2_buf_ops;
 851	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
 852	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
 853	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
 854			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
 855	/*
 856	 * For compatibility with vb1: if QBUF hasn't been called yet, then
 857	 * return EPOLLERR as well. This only affects capture queues, output
 858	 * queues will always initialize waiting_for_buffers to false.
 859	 */
 860	q->quirk_poll_must_check_waiting_for_buffers = true;
 861
 
 
 
 
 
 862	return vb2_core_queue_init(q);
 863}
 
 
 
 
 
 
 864EXPORT_SYMBOL_GPL(vb2_queue_init);
 865
 866void vb2_queue_release(struct vb2_queue *q)
 867{
 868	vb2_core_queue_release(q);
 869}
 870EXPORT_SYMBOL_GPL(vb2_queue_release);
 871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 872__poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
 873{
 874	struct video_device *vfd = video_devdata(file);
 875	__poll_t res;
 876
 877	res = vb2_core_poll(q, file, wait);
 878
 879	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
 880		struct v4l2_fh *fh = file->private_data;
 881
 882		poll_wait(file, &fh->wait, wait);
 883		if (v4l2_event_pending(fh))
 884			res |= EPOLLPRI;
 885	}
 886
 887	return res;
 888}
 889EXPORT_SYMBOL_GPL(vb2_poll);
 890
 891/*
 892 * The following functions are not part of the vb2 core API, but are helper
 893 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
 894 * and struct vb2_ops.
 895 * They contain boilerplate code that most if not all drivers have to do
 896 * and so they simplify the driver code.
 897 */
 898
 899/* The queue is busy if there is a owner and you are not that owner. */
 900static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
 901{
 902	return vdev->queue->owner && vdev->queue->owner != file->private_data;
 903}
 904
 905/* vb2 ioctl helpers */
 906
 907int vb2_ioctl_reqbufs(struct file *file, void *priv,
 908			  struct v4l2_requestbuffers *p)
 909{
 910	struct video_device *vdev = video_devdata(file);
 911	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
 
 912
 913	fill_buf_caps(vdev->queue, &p->capabilities);
 
 
 914	if (res)
 915		return res;
 916	if (vb2_queue_is_busy(vdev, file))
 917		return -EBUSY;
 918	res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
 919	/* If count == 0, then the owner has released all buffers and he
 920	   is no longer owner of the queue. Otherwise we have a new owner. */
 921	if (res == 0)
 922		vdev->queue->owner = p->count ? file->private_data : NULL;
 923	return res;
 924}
 925EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
 926
 927int vb2_ioctl_create_bufs(struct file *file, void *priv,
 928			  struct v4l2_create_buffers *p)
 929{
 930	struct video_device *vdev = video_devdata(file);
 931	int res = vb2_verify_memory_type(vdev->queue, p->memory,
 932			p->format.type);
 933
 934	p->index = vdev->queue->num_buffers;
 935	fill_buf_caps(vdev->queue, &p->capabilities);
 
 936	/*
 937	 * If count == 0, then just check if memory and type are valid.
 938	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
 939	 */
 940	if (p->count == 0)
 941		return res != -EBUSY ? res : 0;
 942	if (res)
 943		return res;
 944	if (vb2_queue_is_busy(vdev, file))
 945		return -EBUSY;
 946
 947	res = vb2_create_bufs(vdev->queue, p);
 948	if (res == 0)
 949		vdev->queue->owner = file->private_data;
 950	return res;
 951}
 952EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
 953
 954int vb2_ioctl_prepare_buf(struct file *file, void *priv,
 955			  struct v4l2_buffer *p)
 956{
 957	struct video_device *vdev = video_devdata(file);
 958
 959	if (vb2_queue_is_busy(vdev, file))
 960		return -EBUSY;
 961	return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
 962}
 963EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
 964
 965int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
 966{
 967	struct video_device *vdev = video_devdata(file);
 968
 969	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
 970	return vb2_querybuf(vdev->queue, p);
 971}
 972EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
 973
 974int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
 975{
 976	struct video_device *vdev = video_devdata(file);
 977
 978	if (vb2_queue_is_busy(vdev, file))
 979		return -EBUSY;
 980	return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
 981}
 982EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
 983
 984int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
 985{
 986	struct video_device *vdev = video_devdata(file);
 987
 988	if (vb2_queue_is_busy(vdev, file))
 989		return -EBUSY;
 990	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
 991}
 992EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
 993
 994int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 995{
 996	struct video_device *vdev = video_devdata(file);
 997
 998	if (vb2_queue_is_busy(vdev, file))
 999		return -EBUSY;
1000	return vb2_streamon(vdev->queue, i);
1001}
1002EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1003
1004int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1005{
1006	struct video_device *vdev = video_devdata(file);
1007
1008	if (vb2_queue_is_busy(vdev, file))
1009		return -EBUSY;
1010	return vb2_streamoff(vdev->queue, i);
1011}
1012EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1013
1014int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1015{
1016	struct video_device *vdev = video_devdata(file);
1017
1018	if (vb2_queue_is_busy(vdev, file))
1019		return -EBUSY;
1020	return vb2_expbuf(vdev->queue, p);
1021}
1022EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1023
1024/* v4l2_file_operations helpers */
1025
1026int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1027{
1028	struct video_device *vdev = video_devdata(file);
1029
1030	return vb2_mmap(vdev->queue, vma);
1031}
1032EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1033
1034int _vb2_fop_release(struct file *file, struct mutex *lock)
1035{
1036	struct video_device *vdev = video_devdata(file);
1037
1038	if (lock)
1039		mutex_lock(lock);
1040	if (file->private_data == vdev->queue->owner) {
1041		vb2_queue_release(vdev->queue);
1042		vdev->queue->owner = NULL;
1043	}
1044	if (lock)
1045		mutex_unlock(lock);
1046	return v4l2_fh_release(file);
1047}
1048EXPORT_SYMBOL_GPL(_vb2_fop_release);
1049
1050int vb2_fop_release(struct file *file)
1051{
1052	struct video_device *vdev = video_devdata(file);
1053	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1054
1055	return _vb2_fop_release(file, lock);
1056}
1057EXPORT_SYMBOL_GPL(vb2_fop_release);
1058
1059ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1060		size_t count, loff_t *ppos)
1061{
1062	struct video_device *vdev = video_devdata(file);
1063	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1064	int err = -EBUSY;
1065
1066	if (!(vdev->queue->io_modes & VB2_WRITE))
1067		return -EINVAL;
1068	if (lock && mutex_lock_interruptible(lock))
1069		return -ERESTARTSYS;
1070	if (vb2_queue_is_busy(vdev, file))
1071		goto exit;
1072	err = vb2_write(vdev->queue, buf, count, ppos,
1073		       file->f_flags & O_NONBLOCK);
1074	if (vdev->queue->fileio)
1075		vdev->queue->owner = file->private_data;
1076exit:
1077	if (lock)
1078		mutex_unlock(lock);
1079	return err;
1080}
1081EXPORT_SYMBOL_GPL(vb2_fop_write);
1082
1083ssize_t vb2_fop_read(struct file *file, char __user *buf,
1084		size_t count, loff_t *ppos)
1085{
1086	struct video_device *vdev = video_devdata(file);
1087	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1088	int err = -EBUSY;
1089
1090	if (!(vdev->queue->io_modes & VB2_READ))
1091		return -EINVAL;
1092	if (lock && mutex_lock_interruptible(lock))
1093		return -ERESTARTSYS;
1094	if (vb2_queue_is_busy(vdev, file))
1095		goto exit;
 
1096	err = vb2_read(vdev->queue, buf, count, ppos,
1097		       file->f_flags & O_NONBLOCK);
1098	if (vdev->queue->fileio)
1099		vdev->queue->owner = file->private_data;
1100exit:
1101	if (lock)
1102		mutex_unlock(lock);
1103	return err;
1104}
1105EXPORT_SYMBOL_GPL(vb2_fop_read);
1106
1107__poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1108{
1109	struct video_device *vdev = video_devdata(file);
1110	struct vb2_queue *q = vdev->queue;
1111	struct mutex *lock = q->lock ? q->lock : vdev->lock;
1112	__poll_t res;
1113	void *fileio;
1114
1115	/*
1116	 * If this helper doesn't know how to lock, then you shouldn't be using
1117	 * it but you should write your own.
1118	 */
1119	WARN_ON(!lock);
1120
1121	if (lock && mutex_lock_interruptible(lock))
1122		return EPOLLERR;
1123
1124	fileio = q->fileio;
1125
1126	res = vb2_poll(vdev->queue, file, wait);
1127
1128	/* If fileio was started, then we have a new queue owner. */
1129	if (!fileio && q->fileio)
1130		q->owner = file->private_data;
1131	if (lock)
1132		mutex_unlock(lock);
1133	return res;
1134}
1135EXPORT_SYMBOL_GPL(vb2_fop_poll);
1136
1137#ifndef CONFIG_MMU
1138unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1139		unsigned long len, unsigned long pgoff, unsigned long flags)
1140{
1141	struct video_device *vdev = video_devdata(file);
1142
1143	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1144}
1145EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1146#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1147
1148/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1149
1150void vb2_ops_wait_prepare(struct vb2_queue *vq)
1151{
1152	mutex_unlock(vq->lock);
1153}
1154EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1155
1156void vb2_ops_wait_finish(struct vb2_queue *vq)
1157{
1158	mutex_lock(vq->lock);
1159}
1160EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1161
1162/*
1163 * Note that this function is called during validation time and
1164 * thus the req_queue_mutex is held to ensure no request objects
1165 * can be added or deleted while validating. So there is no need
1166 * to protect the objects list.
1167 */
1168int vb2_request_validate(struct media_request *req)
1169{
1170	struct media_request_object *obj;
1171	int ret = 0;
1172
1173	if (!vb2_request_buffer_cnt(req))
1174		return -ENOENT;
1175
1176	list_for_each_entry(obj, &req->objects, list) {
1177		if (!obj->ops->prepare)
1178			continue;
1179
1180		ret = obj->ops->prepare(obj);
1181		if (ret)
1182			break;
1183	}
1184
1185	if (ret) {
1186		list_for_each_entry_continue_reverse(obj, &req->objects, list)
1187			if (obj->ops->unprepare)
1188				obj->ops->unprepare(obj);
1189		return ret;
1190	}
1191	return 0;
1192}
1193EXPORT_SYMBOL_GPL(vb2_request_validate);
1194
1195void vb2_request_queue(struct media_request *req)
1196{
1197	struct media_request_object *obj, *obj_safe;
1198
1199	/*
1200	 * Queue all objects. Note that buffer objects are at the end of the
1201	 * objects list, after all other object types. Once buffer objects
1202	 * are queued, the driver might delete them immediately (if the driver
1203	 * processes the buffer at once), so we have to use
1204	 * list_for_each_entry_safe() to handle the case where the object we
1205	 * queue is deleted.
1206	 */
1207	list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1208		if (obj->ops->queue)
1209			obj->ops->queue(obj);
1210}
1211EXPORT_SYMBOL_GPL(vb2_request_queue);
1212
1213MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1214MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1215MODULE_LICENSE("GPL");