File Index
Files and directories
Sign in
FILE

v4l2-mem2mem.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/v4l2-core/v4l2-mem2mem.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Memory-to-memory device framework for Video for Linux 2 and vb2.
   4 *
   5 * Helper functions for devices that use vb2 buffers for both their
   6 * source and destination.
   7 *
   8 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
   9 * Pawel Osciak, <pawel@osciak.com>
  10 * Marek Szyprowski, <m.szyprowski@samsung.com>
  11 */
  12#include <linux/module.h>
  13#include <linux/sched.h>
  14#include <linux/slab.h>
  15
  16#include <media/media-device.h>
  17#include <media/videobuf2-v4l2.h>
  18#include <media/v4l2-mem2mem.h>
  19#include <media/v4l2-dev.h>
  20#include <media/v4l2-device.h>
  21#include <media/v4l2-fh.h>
  22#include <media/v4l2-event.h>
  23
  24MODULE_DESCRIPTION("Mem to mem device framework for vb2");
  25MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
  26MODULE_LICENSE("GPL");
  27
  28static bool debug;
  29module_param(debug, bool, 0644);
  30
  31#define dprintk(fmt, arg...)						\
  32	do {								\
  33		if (debug)						\
  34			printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
  35	} while (0)
  36
  37
  38/* Instance is already queued on the job_queue */
  39#define TRANS_QUEUED		(1 << 0)
  40/* Instance is currently running in hardware */
  41#define TRANS_RUNNING		(1 << 1)
  42/* Instance is currently aborting */
  43#define TRANS_ABORT		(1 << 2)
  44
  45
  46/* The job queue is not running new jobs */
  47#define QUEUE_PAUSED		(1 << 0)
  48
  49
  50/* Offset base for buffers on the destination queue - used to distinguish
  51 * between source and destination buffers when mmapping - they receive the same
  52 * offsets but for different queues */
  53#define DST_QUEUE_OFF_BASE	(1 << 30)
  54
  55enum v4l2_m2m_entity_type {
  56	MEM2MEM_ENT_TYPE_SOURCE,
  57	MEM2MEM_ENT_TYPE_SINK,
  58	MEM2MEM_ENT_TYPE_PROC
  59};
  60
  61static const char * const m2m_entity_name[] = {
  62	"source",
  63	"sink",
  64	"proc"
  65};
  66
  67/**
  68 * struct v4l2_m2m_dev - per-device context
  69 * @source:		&struct media_entity pointer with the source entity
  70 *			Used only when the M2M device is registered via
  71 *			v4l2_m2m_register_media_controller().
  72 * @source_pad:		&struct media_pad with the source pad.
  73 *			Used only when the M2M device is registered via
  74 *			v4l2_m2m_register_media_controller().
  75 * @sink:		&struct media_entity pointer with the sink entity
  76 *			Used only when the M2M device is registered via
  77 *			v4l2_m2m_register_media_controller().
  78 * @sink_pad:		&struct media_pad with the sink pad.
  79 *			Used only when the M2M device is registered via
  80 *			v4l2_m2m_register_media_controller().
  81 * @proc:		&struct media_entity pointer with the M2M device itself.
  82 * @proc_pads:		&struct media_pad with the @proc pads.
  83 *			Used only when the M2M device is registered via
  84 *			v4l2_m2m_unregister_media_controller().
  85 * @intf_devnode:	&struct media_intf devnode pointer with the interface
  86 *			with controls the M2M device.
  87 * @curr_ctx:		currently running instance
  88 * @job_queue:		instances queued to run
  89 * @job_spinlock:	protects job_queue
  90 * @job_work:		worker to run queued jobs.
  91 * @job_queue_flags:	flags of the queue status, %QUEUE_PAUSED.
  92 * @m2m_ops:		driver callbacks
  93 * @kref:		device reference count
  94 */
  95struct v4l2_m2m_dev {
  96	struct v4l2_m2m_ctx	*curr_ctx;
  97#ifdef CONFIG_MEDIA_CONTROLLER
  98	struct media_entity	*source;
  99	struct media_pad	source_pad;
 100	struct media_entity	sink;
 101	struct media_pad	sink_pad;
 102	struct media_entity	proc;
 103	struct media_pad	proc_pads[2];
 104	struct media_intf_devnode *intf_devnode;
 105#endif
 106
 107	struct list_head	job_queue;
 108	spinlock_t		job_spinlock;
 109	struct work_struct	job_work;
 110	unsigned long		job_queue_flags;
 111
 112	const struct v4l2_m2m_ops *m2m_ops;
 113
 114	struct kref kref;
 115};
 116
 117static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
 118						enum v4l2_buf_type type)
 119{
 120	if (V4L2_TYPE_IS_OUTPUT(type))
 121		return &m2m_ctx->out_q_ctx;
 122	else
 123		return &m2m_ctx->cap_q_ctx;
 124}
 125
 126struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
 127				       enum v4l2_buf_type type)
 128{
 129	return &get_queue_ctx(m2m_ctx, type)->q;
 130}
 131EXPORT_SYMBOL(v4l2_m2m_get_vq);
 132
 133struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
 134{
 135	struct v4l2_m2m_buffer *b;
 136	unsigned long flags;
 137
 138	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 139
 140	if (list_empty(&q_ctx->rdy_queue)) {
 141		spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 142		return NULL;
 143	}
 144
 145	b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
 146	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 147	return &b->vb;
 148}
 149EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
 150
 151struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx)
 152{
 153	struct v4l2_m2m_buffer *b;
 154	unsigned long flags;
 155
 156	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 157
 158	if (list_empty(&q_ctx->rdy_queue)) {
 159		spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 160		return NULL;
 161	}
 162
 163	b = list_last_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
 164	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 165	return &b->vb;
 166}
 167EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf);
 168
 169struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
 170{
 171	struct v4l2_m2m_buffer *b;
 172	unsigned long flags;
 173
 174	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 175	if (list_empty(&q_ctx->rdy_queue)) {
 176		spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 177		return NULL;
 178	}
 179	b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
 180	list_del(&b->list);
 181	q_ctx->num_rdy--;
 182	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 183
 184	return &b->vb;
 185}
 186EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
 187
 188void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
 189				struct vb2_v4l2_buffer *vbuf)
 190{
 191	struct v4l2_m2m_buffer *b;
 192	unsigned long flags;
 193
 194	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 195	b = container_of(vbuf, struct v4l2_m2m_buffer, vb);
 196	list_del(&b->list);
 197	q_ctx->num_rdy--;
 198	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 199}
 200EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf);
 201
 202struct vb2_v4l2_buffer *
 203v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx)
 204
 205{
 206	struct v4l2_m2m_buffer *b, *tmp;
 207	struct vb2_v4l2_buffer *ret = NULL;
 208	unsigned long flags;
 209
 210	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 211	list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) {
 212		if (b->vb.vb2_buf.index == idx) {
 213			list_del(&b->list);
 214			q_ctx->num_rdy--;
 215			ret = &b->vb;
 216			break;
 217		}
 218	}
 219	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 220
 221	return ret;
 222}
 223EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx);
 224
 225/*
 226 * Scheduling handlers
 227 */
 228
 229void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
 230{
 231	unsigned long flags;
 232	void *ret = NULL;
 233
 234	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 235	if (m2m_dev->curr_ctx)
 236		ret = m2m_dev->curr_ctx->priv;
 237	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 238
 239	return ret;
 240}
 241EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
 242
 243/**
 244 * v4l2_m2m_try_run() - select next job to perform and run it if possible
 245 * @m2m_dev: per-device context
 246 *
 247 * Get next transaction (if present) from the waiting jobs list and run it.
 248 *
 249 * Note that this function can run on a given v4l2_m2m_ctx context,
 250 * but call .device_run for another context.
 251 */
 252static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
 253{
 254	unsigned long flags;
 255
 256	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 257	if (NULL != m2m_dev->curr_ctx) {
 258		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 259		dprintk("Another instance is running, won't run now\n");
 260		return;
 261	}
 262
 263	if (list_empty(&m2m_dev->job_queue)) {
 264		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 265		dprintk("No job pending\n");
 266		return;
 267	}
 268
 269	if (m2m_dev->job_queue_flags & QUEUE_PAUSED) {
 270		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 271		dprintk("Running new jobs is paused\n");
 272		return;
 273	}
 274
 275	m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
 276				   struct v4l2_m2m_ctx, queue);
 277	m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
 278	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 279
 280	dprintk("Running job on m2m_ctx: %p\n", m2m_dev->curr_ctx);
 281	m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
 282}
 283
 284/*
 285 * __v4l2_m2m_try_queue() - queue a job
 286 * @m2m_dev: m2m device
 287 * @m2m_ctx: m2m context
 288 *
 289 * Check if this context is ready to queue a job.
 290 *
 291 * This function can run in interrupt context.
 292 */
 293static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev,
 294				 struct v4l2_m2m_ctx *m2m_ctx)
 295{
 296	unsigned long flags_job;
 297	struct vb2_v4l2_buffer *dst, *src;
 298
 299	dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
 300
 301	if (!m2m_ctx->out_q_ctx.q.streaming ||
 302	    (!m2m_ctx->cap_q_ctx.q.streaming && !m2m_ctx->ignore_cap_streaming)) {
 303		if (!m2m_ctx->ignore_cap_streaming)
 304			dprintk("Streaming needs to be on for both queues\n");
 305		else
 306			dprintk("Streaming needs to be on for the OUTPUT queue\n");
 307		return;
 308	}
 309
 310	spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
 311
 312	/* If the context is aborted then don't schedule it */
 313	if (m2m_ctx->job_flags & TRANS_ABORT) {
 314		dprintk("Aborted context\n");
 315		goto job_unlock;
 316	}
 317
 318	if (m2m_ctx->job_flags & TRANS_QUEUED) {
 319		dprintk("On job queue already\n");
 320		goto job_unlock;
 321	}
 322
 323	src = v4l2_m2m_next_src_buf(m2m_ctx);
 324	dst = v4l2_m2m_next_dst_buf(m2m_ctx);
 325	if (!src && !m2m_ctx->out_q_ctx.buffered) {
 326		dprintk("No input buffers available\n");
 327		goto job_unlock;
 328	}
 329	if (!dst && !m2m_ctx->cap_q_ctx.buffered) {
 330		dprintk("No output buffers available\n");
 331		goto job_unlock;
 332	}
 333
 334	m2m_ctx->new_frame = true;
 335
 336	if (src && dst && dst->is_held &&
 337	    dst->vb2_buf.copied_timestamp &&
 338	    dst->vb2_buf.timestamp != src->vb2_buf.timestamp) {
 339		dprintk("Timestamp mismatch, returning held capture buffer\n");
 340		dst->is_held = false;
 341		v4l2_m2m_dst_buf_remove(m2m_ctx);
 342		v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
 343		dst = v4l2_m2m_next_dst_buf(m2m_ctx);
 344
 345		if (!dst && !m2m_ctx->cap_q_ctx.buffered) {
 346			dprintk("No output buffers available after returning held buffer\n");
 347			goto job_unlock;
 348		}
 349	}
 350
 351	if (src && dst && (m2m_ctx->out_q_ctx.q.subsystem_flags &
 352			   VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
 353		m2m_ctx->new_frame = !dst->vb2_buf.copied_timestamp ||
 354			dst->vb2_buf.timestamp != src->vb2_buf.timestamp;
 355
 356	if (m2m_ctx->has_stopped) {
 357		dprintk("Device has stopped\n");
 358		goto job_unlock;
 359	}
 360
 361	if (m2m_dev->m2m_ops->job_ready
 362		&& (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
 363		dprintk("Driver not ready\n");
 364		goto job_unlock;
 365	}
 366
 367	list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
 368	m2m_ctx->job_flags |= TRANS_QUEUED;
 369
 370job_unlock:
 371	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
 372}
 373
 374/**
 375 * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context
 376 * @m2m_ctx: m2m context
 377 *
 378 * Check if this context is ready to queue a job. If suitable,
 379 * run the next queued job on the mem2mem device.
 380 *
 381 * This function shouldn't run in interrupt context.
 382 *
 383 * Note that v4l2_m2m_try_schedule() can schedule one job for this context,
 384 * and then run another job for another context.
 385 */
 386void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
 387{
 388	struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;
 389
 390	__v4l2_m2m_try_queue(m2m_dev, m2m_ctx);
 391	v4l2_m2m_try_run(m2m_dev);
 392}
 393EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule);
 394
 395/**
 396 * v4l2_m2m_device_run_work() - run pending jobs for the context
 397 * @work: Work structure used for scheduling the execution of this function.
 398 */
 399static void v4l2_m2m_device_run_work(struct work_struct *work)
 400{
 401	struct v4l2_m2m_dev *m2m_dev =
 402		container_of(work, struct v4l2_m2m_dev, job_work);
 403
 404	v4l2_m2m_try_run(m2m_dev);
 405}
 406
 407/**
 408 * v4l2_m2m_cancel_job() - cancel pending jobs for the context
 409 * @m2m_ctx: m2m context with jobs to be canceled
 410 *
 411 * In case of streamoff or release called on any context,
 412 * 1] If the context is currently running, then abort job will be called
 413 * 2] If the context is queued, then the context will be removed from
 414 *    the job_queue
 415 */
 416static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
 417{
 418	struct v4l2_m2m_dev *m2m_dev;
 419	unsigned long flags;
 420
 421	m2m_dev = m2m_ctx->m2m_dev;
 422	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 423
 424	m2m_ctx->job_flags |= TRANS_ABORT;
 425	if (m2m_ctx->job_flags & TRANS_RUNNING) {
 426		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 427		if (m2m_dev->m2m_ops->job_abort)
 428			m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
 429		dprintk("m2m_ctx %p running, will wait to complete\n", m2m_ctx);
 430		wait_event(m2m_ctx->finished,
 431				!(m2m_ctx->job_flags & TRANS_RUNNING));
 432	} else if (m2m_ctx->job_flags & TRANS_QUEUED) {
 433		list_del(&m2m_ctx->queue);
 434		m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
 435		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 436		dprintk("m2m_ctx: %p had been on queue and was removed\n",
 437			m2m_ctx);
 438	} else {
 439		/* Do nothing, was not on queue/running */
 440		spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 441	}
 442}
 443
 444/*
 445 * Schedule the next job, called from v4l2_m2m_job_finish() or
 446 * v4l2_m2m_buf_done_and_job_finish().
 447 */
 448static void v4l2_m2m_schedule_next_job(struct v4l2_m2m_dev *m2m_dev,
 449				       struct v4l2_m2m_ctx *m2m_ctx)
 450{
 451	/*
 452	 * This instance might have more buffers ready, but since we do not
 453	 * allow more than one job on the job_queue per instance, each has
 454	 * to be scheduled separately after the previous one finishes.
 455	 */
 456	__v4l2_m2m_try_queue(m2m_dev, m2m_ctx);
 457
 458	/*
 459	 * We might be running in atomic context,
 460	 * but the job must be run in non-atomic context.
 461	 */
 462	schedule_work(&m2m_dev->job_work);
 463}
 464
 465/*
 466 * Assumes job_spinlock is held, called from v4l2_m2m_job_finish() or
 467 * v4l2_m2m_buf_done_and_job_finish().
 468 */
 469static bool _v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
 470				 struct v4l2_m2m_ctx *m2m_ctx)
 471{
 472	if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
 473		dprintk("Called by an instance not currently running\n");
 474		return false;
 475	}
 476
 477	list_del(&m2m_dev->curr_ctx->queue);
 478	m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
 479	wake_up(&m2m_dev->curr_ctx->finished);
 480	m2m_dev->curr_ctx = NULL;
 481	return true;
 482}
 483
 484void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
 485			 struct v4l2_m2m_ctx *m2m_ctx)
 486{
 487	unsigned long flags;
 488	bool schedule_next;
 489
 490	/*
 491	 * This function should not be used for drivers that support
 492	 * holding capture buffers. Those should use
 493	 * v4l2_m2m_buf_done_and_job_finish() instead.
 494	 */
 495	WARN_ON(m2m_ctx->out_q_ctx.q.subsystem_flags &
 496		VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
 497	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 498	schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx);
 499	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 500
 501	if (schedule_next)
 502		v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx);
 503}
 504EXPORT_SYMBOL(v4l2_m2m_job_finish);
 505
 506void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
 507				      struct v4l2_m2m_ctx *m2m_ctx,
 508				      enum vb2_buffer_state state)
 509{
 510	struct vb2_v4l2_buffer *src_buf, *dst_buf;
 511	bool schedule_next = false;
 512	unsigned long flags;
 513
 514	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 515	src_buf = v4l2_m2m_src_buf_remove(m2m_ctx);
 516	dst_buf = v4l2_m2m_next_dst_buf(m2m_ctx);
 517
 518	if (WARN_ON(!src_buf || !dst_buf))
 519		goto unlock;
 520	dst_buf->is_held = src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
 521	if (!dst_buf->is_held) {
 522		v4l2_m2m_dst_buf_remove(m2m_ctx);
 523		v4l2_m2m_buf_done(dst_buf, state);
 524	}
 525	/*
 526	 * If the request API is being used, returning the OUTPUT
 527	 * (src) buffer will wake-up any process waiting on the
 528	 * request file descriptor.
 529	 *
 530	 * Therefore, return the CAPTURE (dst) buffer first,
 531	 * to avoid signalling the request file descriptor
 532	 * before the CAPTURE buffer is done.
 533	 */
 534	v4l2_m2m_buf_done(src_buf, state);
 535	schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx);
 536unlock:
 537	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 538
 539	if (schedule_next)
 540		v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx);
 541}
 542EXPORT_SYMBOL(v4l2_m2m_buf_done_and_job_finish);
 543
 544void v4l2_m2m_suspend(struct v4l2_m2m_dev *m2m_dev)
 545{
 546	unsigned long flags;
 547	struct v4l2_m2m_ctx *curr_ctx;
 548
 549	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 550	m2m_dev->job_queue_flags |= QUEUE_PAUSED;
 551	curr_ctx = m2m_dev->curr_ctx;
 552	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 553
 554	if (curr_ctx)
 555		wait_event(curr_ctx->finished,
 556			   !(curr_ctx->job_flags & TRANS_RUNNING));
 557}
 558EXPORT_SYMBOL(v4l2_m2m_suspend);
 559
 560void v4l2_m2m_resume(struct v4l2_m2m_dev *m2m_dev)
 561{
 562	unsigned long flags;
 563
 564	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
 565	m2m_dev->job_queue_flags &= ~QUEUE_PAUSED;
 566	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
 567
 568	v4l2_m2m_try_run(m2m_dev);
 569}
 570EXPORT_SYMBOL(v4l2_m2m_resume);
 571
 572int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 573		     struct v4l2_requestbuffers *reqbufs)
 574{
 575	struct vb2_queue *vq;
 576	int ret;
 577
 578	vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
 579	ret = vb2_reqbufs(vq, reqbufs);
 580	/* If count == 0, then the owner has released all buffers and he
 581	   is no longer owner of the queue. Otherwise we have an owner. */
 582	if (ret == 0)
 583		vq->owner = reqbufs->count ? file->private_data : NULL;
 584
 585	return ret;
 586}
 587EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
 588
 589static void v4l2_m2m_adjust_mem_offset(struct vb2_queue *vq,
 590				       struct v4l2_buffer *buf)
 591{
 592	/* Adjust MMAP memory offsets for the CAPTURE queue */
 593	if (buf->memory == V4L2_MEMORY_MMAP && V4L2_TYPE_IS_CAPTURE(vq->type)) {
 594		if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
 595			unsigned int i;
 596
 597			for (i = 0; i < buf->length; ++i)
 598				buf->m.planes[i].m.mem_offset
 599					+= DST_QUEUE_OFF_BASE;
 600		} else {
 601			buf->m.offset += DST_QUEUE_OFF_BASE;
 602		}
 603	}
 604}
 605
 606int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 607		      struct v4l2_buffer *buf)
 608{
 609	struct vb2_queue *vq;
 610	int ret;
 611
 612	vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
 613	ret = vb2_querybuf(vq, buf);
 614	if (ret)
 615		return ret;
 616
 617	/* Adjust MMAP memory offsets for the CAPTURE queue */
 618	v4l2_m2m_adjust_mem_offset(vq, buf);
 619
 620	return 0;
 621}
 622EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
 623
 624/*
 625 * This will add the LAST flag and mark the buffer management
 626 * state as stopped.
 627 * This is called when the last capture buffer must be flagged as LAST
 628 * in draining mode from the encoder/decoder driver buf_queue() callback
 629 * or from v4l2_update_last_buf_state() when a capture buffer is available.
 630 */
 631void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx,
 632			       struct vb2_v4l2_buffer *vbuf)
 633{
 634	vbuf->flags |= V4L2_BUF_FLAG_LAST;
 635	vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
 636
 637	v4l2_m2m_mark_stopped(m2m_ctx);
 638}
 639EXPORT_SYMBOL_GPL(v4l2_m2m_last_buffer_done);
 640
 641/* When stop command is issued, update buffer management state */
 642static int v4l2_update_last_buf_state(struct v4l2_m2m_ctx *m2m_ctx)
 643{
 644	struct vb2_v4l2_buffer *next_dst_buf;
 645
 646	if (m2m_ctx->is_draining)
 647		return -EBUSY;
 648
 649	if (m2m_ctx->has_stopped)
 650		return 0;
 651
 652	m2m_ctx->last_src_buf = v4l2_m2m_last_src_buf(m2m_ctx);
 653	m2m_ctx->is_draining = true;
 654
 655	/*
 656	 * The processing of the last output buffer queued before
 657	 * the STOP command is expected to mark the buffer management
 658	 * state as stopped with v4l2_m2m_mark_stopped().
 659	 */
 660	if (m2m_ctx->last_src_buf)
 661		return 0;
 662
 663	/*
 664	 * In case the output queue is empty, try to mark the last capture
 665	 * buffer as LAST.
 666	 */
 667	next_dst_buf = v4l2_m2m_dst_buf_remove(m2m_ctx);
 668	if (!next_dst_buf) {
 669		/*
 670		 * Wait for the next queued one in encoder/decoder driver
 671		 * buf_queue() callback using the v4l2_m2m_dst_buf_is_last()
 672		 * helper or in v4l2_m2m_qbuf() if encoder/decoder is not yet
 673		 * streaming.
 674		 */
 675		m2m_ctx->next_buf_last = true;
 676		return 0;
 677	}
 678
 679	v4l2_m2m_last_buffer_done(m2m_ctx, next_dst_buf);
 680
 681	return 0;
 682}
 683
 684/*
 685 * Updates the encoding/decoding buffer management state, should
 686 * be called from encoder/decoder drivers start_streaming()
 687 */
 688void v4l2_m2m_update_start_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
 689					   struct vb2_queue *q)
 690{
 691	/* If start streaming again, untag the last output buffer */
 692	if (V4L2_TYPE_IS_OUTPUT(q->type))
 693		m2m_ctx->last_src_buf = NULL;
 694}
 695EXPORT_SYMBOL_GPL(v4l2_m2m_update_start_streaming_state);
 696
 697/*
 698 * Updates the encoding/decoding buffer management state, should
 699 * be called from encoder/decoder driver stop_streaming()
 700 */
 701void v4l2_m2m_update_stop_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
 702					  struct vb2_queue *q)
 703{
 704	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
 705		/*
 706		 * If in draining state, either mark next dst buffer as
 707		 * done or flag next one to be marked as done either
 708		 * in encoder/decoder driver buf_queue() callback using
 709		 * the v4l2_m2m_dst_buf_is_last() helper or in v4l2_m2m_qbuf()
 710		 * if encoder/decoder is not yet streaming
 711		 */
 712		if (m2m_ctx->is_draining) {
 713			struct vb2_v4l2_buffer *next_dst_buf;
 714
 715			m2m_ctx->last_src_buf = NULL;
 716			next_dst_buf = v4l2_m2m_dst_buf_remove(m2m_ctx);
 717			if (!next_dst_buf)
 718				m2m_ctx->next_buf_last = true;
 719			else
 720				v4l2_m2m_last_buffer_done(m2m_ctx,
 721							  next_dst_buf);
 722		}
 723	} else {
 724		v4l2_m2m_clear_state(m2m_ctx);
 725	}
 726}
 727EXPORT_SYMBOL_GPL(v4l2_m2m_update_stop_streaming_state);
 728
 729static void v4l2_m2m_force_last_buf_done(struct v4l2_m2m_ctx *m2m_ctx,
 730					 struct vb2_queue *q)
 731{
 732	struct vb2_buffer *vb;
 733	struct vb2_v4l2_buffer *vbuf;
 734	unsigned int i;
 735
 736	if (WARN_ON(q->is_output))
 737		return;
 738	if (list_empty(&q->queued_list))
 739		return;
 740
 741	vb = list_first_entry(&q->queued_list, struct vb2_buffer, queued_entry);
 742	for (i = 0; i < vb->num_planes; i++)
 743		vb2_set_plane_payload(vb, i, 0);
 744
 745	/*
 746	 * Since the buffer hasn't been queued to the ready queue,
 747	 * mark is active and owned before marking it LAST and DONE
 748	 */
 749	vb->state = VB2_BUF_STATE_ACTIVE;
 750	atomic_inc(&q->owned_by_drv_count);
 751
 752	vbuf = to_vb2_v4l2_buffer(vb);
 753	vbuf->field = V4L2_FIELD_NONE;
 754
 755	v4l2_m2m_last_buffer_done(m2m_ctx, vbuf);
 756}
 757
 758int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 759		  struct v4l2_buffer *buf)
 760{
 761	struct video_device *vdev = video_devdata(file);
 762	struct vb2_queue *vq;
 763	int ret;
 764
 765	vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
 766	if (V4L2_TYPE_IS_CAPTURE(vq->type) &&
 767	    (buf->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
 768		dprintk("%s: requests cannot be used with capture buffers\n",
 769			__func__);
 770		return -EPERM;
 771	}
 772
 773	ret = vb2_qbuf(vq, vdev->v4l2_dev->mdev, buf);
 774	if (ret)
 775		return ret;
 776
 777	/* Adjust MMAP memory offsets for the CAPTURE queue */
 778	v4l2_m2m_adjust_mem_offset(vq, buf);
 779
 780	/*
 781	 * If the capture queue is streaming, but streaming hasn't started
 782	 * on the device, but was asked to stop, mark the previously queued
 783	 * buffer as DONE with LAST flag since it won't be queued on the
 784	 * device.
 785	 */
 786	if (V4L2_TYPE_IS_CAPTURE(vq->type) &&
 787	    vb2_is_streaming(vq) && !vb2_start_streaming_called(vq) &&
 788	   (v4l2_m2m_has_stopped(m2m_ctx) || v4l2_m2m_dst_buf_is_last(m2m_ctx)))
 789		v4l2_m2m_force_last_buf_done(m2m_ctx, vq);
 790	else if (!(buf->flags & V4L2_BUF_FLAG_IN_REQUEST))
 791		v4l2_m2m_try_schedule(m2m_ctx);
 792
 793	return 0;
 794}
 795EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
 796
 797int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 798		   struct v4l2_buffer *buf)
 799{
 800	struct vb2_queue *vq;
 801	int ret;
 802
 803	vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
 804	ret = vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
 805	if (ret)
 806		return ret;
 807
 808	/* Adjust MMAP memory offsets for the CAPTURE queue */
 809	v4l2_m2m_adjust_mem_offset(vq, buf);
 810
 811	return 0;
 812}
 813EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
 814
 815int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 816			 struct v4l2_buffer *buf)
 817{
 818	struct video_device *vdev = video_devdata(file);
 819	struct vb2_queue *vq;
 820	int ret;
 821
 822	vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
 823	ret = vb2_prepare_buf(vq, vdev->v4l2_dev->mdev, buf);
 824	if (ret)
 825		return ret;
 826
 827	/* Adjust MMAP memory offsets for the CAPTURE queue */
 828	v4l2_m2m_adjust_mem_offset(vq, buf);
 829
 830	return 0;
 831}
 832EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
 833
 834int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 835			 struct v4l2_create_buffers *create)
 836{
 837	struct vb2_queue *vq;
 838
 839	vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
 840	return vb2_create_bufs(vq, create);
 841}
 842EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
 843
 844int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 845		  struct v4l2_exportbuffer *eb)
 846{
 847	struct vb2_queue *vq;
 848
 849	vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
 850	return vb2_expbuf(vq, eb);
 851}
 852EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
 853
 854int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 855		      enum v4l2_buf_type type)
 856{
 857	struct vb2_queue *vq;
 858	int ret;
 859
 860	vq = v4l2_m2m_get_vq(m2m_ctx, type);
 861	ret = vb2_streamon(vq, type);
 862	if (!ret)
 863		v4l2_m2m_try_schedule(m2m_ctx);
 864
 865	return ret;
 866}
 867EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
 868
 869int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 870		       enum v4l2_buf_type type)
 871{
 872	struct v4l2_m2m_dev *m2m_dev;
 873	struct v4l2_m2m_queue_ctx *q_ctx;
 874	unsigned long flags_job, flags;
 875	int ret;
 876
 877	/* wait until the current context is dequeued from job_queue */
 878	v4l2_m2m_cancel_job(m2m_ctx);
 879
 880	q_ctx = get_queue_ctx(m2m_ctx, type);
 881	ret = vb2_streamoff(&q_ctx->q, type);
 882	if (ret)
 883		return ret;
 884
 885	m2m_dev = m2m_ctx->m2m_dev;
 886	spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
 887	/* We should not be scheduled anymore, since we're dropping a queue. */
 888	if (m2m_ctx->job_flags & TRANS_QUEUED)
 889		list_del(&m2m_ctx->queue);
 890	m2m_ctx->job_flags = 0;
 891
 892	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
 893	/* Drop queue, since streamoff returns device to the same state as after
 894	 * calling reqbufs. */
 895	INIT_LIST_HEAD(&q_ctx->rdy_queue);
 896	q_ctx->num_rdy = 0;
 897	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
 898
 899	if (m2m_dev->curr_ctx == m2m_ctx) {
 900		m2m_dev->curr_ctx = NULL;
 901		wake_up(&m2m_ctx->finished);
 902	}
 903	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
 904
 905	return 0;
 906}
 907EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
 908
 909static __poll_t v4l2_m2m_poll_for_data(struct file *file,
 910				       struct v4l2_m2m_ctx *m2m_ctx,
 911				       struct poll_table_struct *wait)
 912{
 913	struct vb2_queue *src_q, *dst_q;
 914	__poll_t rc = 0;
 915	unsigned long flags;
 916
 917	src_q = v4l2_m2m_get_src_vq(m2m_ctx);
 918	dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
 919
 920	/*
 921	 * There has to be at least one buffer queued on each queued_list, which
 922	 * means either in driver already or waiting for driver to claim it
 923	 * and start processing.
 924	 */
 925	if ((!vb2_is_streaming(src_q) || src_q->error ||
 926	     list_empty(&src_q->queued_list)) &&
 927	    (!vb2_is_streaming(dst_q) || dst_q->error ||
 928	     (list_empty(&dst_q->queued_list) && !dst_q->last_buffer_dequeued)))
 929		return EPOLLERR;
 930
 931	spin_lock_irqsave(&src_q->done_lock, flags);
 932	if (!list_empty(&src_q->done_list))
 933		rc |= EPOLLOUT | EPOLLWRNORM;
 934	spin_unlock_irqrestore(&src_q->done_lock, flags);
 935
 936	spin_lock_irqsave(&dst_q->done_lock, flags);
 937	/*
 938	 * If the last buffer was dequeued from the capture queue, signal
 939	 * userspace. DQBUF(CAPTURE) will return -EPIPE.
 940	 */
 941	if (!list_empty(&dst_q->done_list) || dst_q->last_buffer_dequeued)
 942		rc |= EPOLLIN | EPOLLRDNORM;
 943	spin_unlock_irqrestore(&dst_q->done_lock, flags);
 944
 945	return rc;
 946}
 947
 948__poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 949		       struct poll_table_struct *wait)
 950{
 951	struct v4l2_fh *fh = file_to_v4l2_fh(file);
 952	struct vb2_queue *src_q = v4l2_m2m_get_src_vq(m2m_ctx);
 953	struct vb2_queue *dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
 954	__poll_t req_events = poll_requested_events(wait);
 955	__poll_t rc = 0;
 956
 957	/*
 958	 * poll_wait() MUST be called on the first invocation on all the
 959	 * potential queues of interest, even if we are not interested in their
 960	 * events during this first call. Failure to do so will result in
 961	 * queue's events to be ignored because the poll_table won't be capable
 962	 * of adding new wait queues thereafter.
 963	 */
 964	poll_wait(file, &src_q->done_wq, wait);
 965	poll_wait(file, &dst_q->done_wq, wait);
 966
 967	if (req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM))
 968		rc = v4l2_m2m_poll_for_data(file, m2m_ctx, wait);
 969
 970	poll_wait(file, &fh->wait, wait);
 971	if (v4l2_event_pending(fh))
 972		rc |= EPOLLPRI;
 973
 974	return rc;
 975}
 976EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
 977
 978int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 979			 struct vm_area_struct *vma)
 980{
 981	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
 982	struct vb2_queue *vq;
 983
 984	if (offset < DST_QUEUE_OFF_BASE) {
 985		vq = v4l2_m2m_get_src_vq(m2m_ctx);
 986	} else {
 987		vq = v4l2_m2m_get_dst_vq(m2m_ctx);
 988		vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
 989	}
 990
 991	return vb2_mmap(vq, vma);
 992}
 993EXPORT_SYMBOL(v4l2_m2m_mmap);
 994
 995#ifndef CONFIG_MMU
 996unsigned long v4l2_m2m_get_unmapped_area(struct file *file, unsigned long addr,
 997					 unsigned long len, unsigned long pgoff,
 998					 unsigned long flags)
 999{
1000	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1001	unsigned long offset = pgoff << PAGE_SHIFT;
1002	struct vb2_queue *vq;
1003
1004	if (offset < DST_QUEUE_OFF_BASE) {
1005		vq = v4l2_m2m_get_src_vq(fh->m2m_ctx);
1006	} else {
1007		vq = v4l2_m2m_get_dst_vq(fh->m2m_ctx);
1008		pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
1009	}
1010
1011	return vb2_get_unmapped_area(vq, addr, len, pgoff, flags);
1012}
1013EXPORT_SYMBOL_GPL(v4l2_m2m_get_unmapped_area);
1014#endif
1015
1016#if defined(CONFIG_MEDIA_CONTROLLER)
1017void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
1018{
1019	media_remove_intf_links(&m2m_dev->intf_devnode->intf);
1020	media_devnode_remove(m2m_dev->intf_devnode);
1021
1022	media_entity_remove_links(m2m_dev->source);
1023	media_entity_remove_links(&m2m_dev->sink);
1024	media_entity_remove_links(&m2m_dev->proc);
1025	media_device_unregister_entity(m2m_dev->source);
1026	media_device_unregister_entity(&m2m_dev->sink);
1027	media_device_unregister_entity(&m2m_dev->proc);
1028	kfree(m2m_dev->source->name);
1029	kfree(m2m_dev->sink.name);
1030	kfree(m2m_dev->proc.name);
1031}
1032EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller);
1033
1034static int v4l2_m2m_register_entity(struct media_device *mdev,
1035	struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type,
1036	struct video_device *vdev, int function)
1037{
1038	struct media_entity *entity;
1039	struct media_pad *pads;
1040	char *name;
1041	unsigned int len;
1042	int num_pads;
1043	int ret;
1044
1045	switch (type) {
1046	case MEM2MEM_ENT_TYPE_SOURCE:
1047		entity = m2m_dev->source;
1048		pads = &m2m_dev->source_pad;
1049		pads[0].flags = MEDIA_PAD_FL_SOURCE;
1050		num_pads = 1;
1051		break;
1052	case MEM2MEM_ENT_TYPE_SINK:
1053		entity = &m2m_dev->sink;
1054		pads = &m2m_dev->sink_pad;
1055		pads[0].flags = MEDIA_PAD_FL_SINK;
1056		num_pads = 1;
1057		break;
1058	case MEM2MEM_ENT_TYPE_PROC:
1059		entity = &m2m_dev->proc;
1060		pads = m2m_dev->proc_pads;
1061		pads[0].flags = MEDIA_PAD_FL_SINK;
1062		pads[1].flags = MEDIA_PAD_FL_SOURCE;
1063		num_pads = 2;
1064		break;
1065	default:
1066		return -EINVAL;
1067	}
1068
1069	entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
1070	if (type != MEM2MEM_ENT_TYPE_PROC) {
1071		entity->info.dev.major = VIDEO_MAJOR;
1072		entity->info.dev.minor = vdev->minor;
1073	}
1074	len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]);
1075	name = kmalloc(len, GFP_KERNEL);
1076	if (!name)
1077		return -ENOMEM;
1078	snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]);
1079	entity->name = name;
1080	entity->function = function;
1081
1082	ret = media_entity_pads_init(entity, num_pads, pads);
1083	if (ret) {
1084		kfree(entity->name);
1085		entity->name = NULL;
1086		return ret;
1087	}
1088	ret = media_device_register_entity(mdev, entity);
1089	if (ret) {
1090		kfree(entity->name);
1091		entity->name = NULL;
1092		return ret;
1093	}
1094
1095	return 0;
1096}
1097
1098int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
1099		struct video_device *vdev, int function)
1100{
1101	struct media_device *mdev = vdev->v4l2_dev->mdev;
1102	struct media_link *link;
1103	int ret;
1104
1105	if (!mdev)
1106		return 0;
1107
1108	/* A memory-to-memory device consists in two
1109	 * DMA engine and one video processing entities.
1110	 * The DMA engine entities are linked to a V4L interface
1111	 */
1112
1113	/* Create the three entities with their pads */
1114	m2m_dev->source = &vdev->entity;
1115	ret = v4l2_m2m_register_entity(mdev, m2m_dev,
1116			MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L);
1117	if (ret)
1118		return ret;
1119	ret = v4l2_m2m_register_entity(mdev, m2m_dev,
1120			MEM2MEM_ENT_TYPE_PROC, vdev, function);
1121	if (ret)
1122		goto err_rel_entity0;
1123	ret = v4l2_m2m_register_entity(mdev, m2m_dev,
1124			MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L);
1125	if (ret)
1126		goto err_rel_entity1;
1127
1128	/* Connect the three entities */
1129	ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 0,
1130			MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1131	if (ret)
1132		goto err_rel_entity2;
1133
1134	ret = media_create_pad_link(&m2m_dev->proc, 1, &m2m_dev->sink, 0,
1135			MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1136	if (ret)
1137		goto err_rm_links0;
1138
1139	/* Create video interface */
1140	m2m_dev->intf_devnode = media_devnode_create(mdev,
1141			MEDIA_INTF_T_V4L_VIDEO, 0,
1142			VIDEO_MAJOR, vdev->minor);
1143	if (!m2m_dev->intf_devnode) {
1144		ret = -ENOMEM;
1145		goto err_rm_links1;
1146	}
1147
1148	/* Connect the two DMA engines to the interface */
1149	link = media_create_intf_link(m2m_dev->source,
1150			&m2m_dev->intf_devnode->intf,
1151			MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1152	if (!link) {
1153		ret = -ENOMEM;
1154		goto err_rm_devnode;
1155	}
1156
1157	link = media_create_intf_link(&m2m_dev->sink,
1158			&m2m_dev->intf_devnode->intf,
1159			MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1160	if (!link) {
1161		ret = -ENOMEM;
1162		goto err_rm_intf_link;
1163	}
1164	return 0;
1165
1166err_rm_intf_link:
1167	media_remove_intf_links(&m2m_dev->intf_devnode->intf);
1168err_rm_devnode:
1169	media_devnode_remove(m2m_dev->intf_devnode);
1170err_rm_links1:
1171	media_entity_remove_links(&m2m_dev->sink);
1172err_rm_links0:
1173	media_entity_remove_links(&m2m_dev->proc);
1174	media_entity_remove_links(m2m_dev->source);
1175err_rel_entity2:
1176	media_device_unregister_entity(&m2m_dev->proc);
1177	kfree(m2m_dev->proc.name);
1178err_rel_entity1:
1179	media_device_unregister_entity(&m2m_dev->sink);
1180	kfree(m2m_dev->sink.name);
1181err_rel_entity0:
1182	media_device_unregister_entity(m2m_dev->source);
1183	kfree(m2m_dev->source->name);
1184	return ret;
1185	return 0;
1186}
1187EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller);
1188#endif
1189
1190struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
1191{
1192	struct v4l2_m2m_dev *m2m_dev;
1193
1194	if (!m2m_ops || WARN_ON(!m2m_ops->device_run))
1195		return ERR_PTR(-EINVAL);
1196
1197	m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
1198	if (!m2m_dev)
1199		return ERR_PTR(-ENOMEM);
1200
1201	m2m_dev->curr_ctx = NULL;
1202	m2m_dev->m2m_ops = m2m_ops;
1203	INIT_LIST_HEAD(&m2m_dev->job_queue);
1204	spin_lock_init(&m2m_dev->job_spinlock);
1205	INIT_WORK(&m2m_dev->job_work, v4l2_m2m_device_run_work);
1206	kref_init(&m2m_dev->kref);
1207
1208	return m2m_dev;
1209}
1210EXPORT_SYMBOL_GPL(v4l2_m2m_init);
1211
1212void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
1213{
1214	kfree(m2m_dev);
1215}
1216EXPORT_SYMBOL_GPL(v4l2_m2m_release);
1217
1218void v4l2_m2m_get(struct v4l2_m2m_dev *m2m_dev)
1219{
1220	kref_get(&m2m_dev->kref);
1221}
1222EXPORT_SYMBOL_GPL(v4l2_m2m_get);
1223
1224static void v4l2_m2m_release_from_kref(struct kref *kref)
1225{
1226	struct v4l2_m2m_dev *m2m_dev = container_of(kref, struct v4l2_m2m_dev, kref);
1227
1228	v4l2_m2m_release(m2m_dev);
1229}
1230
1231void v4l2_m2m_put(struct v4l2_m2m_dev *m2m_dev)
1232{
1233	kref_put(&m2m_dev->kref, v4l2_m2m_release_from_kref);
1234}
1235EXPORT_SYMBOL_GPL(v4l2_m2m_put);
1236
1237struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
1238		void *drv_priv,
1239		int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
1240{
1241	struct v4l2_m2m_ctx *m2m_ctx;
1242	struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
1243	int ret;
1244
1245	m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
1246	if (!m2m_ctx)
1247		return ERR_PTR(-ENOMEM);
1248
1249	m2m_ctx->priv = drv_priv;
1250	m2m_ctx->m2m_dev = m2m_dev;
1251	init_waitqueue_head(&m2m_ctx->finished);
1252
1253	out_q_ctx = &m2m_ctx->out_q_ctx;
1254	cap_q_ctx = &m2m_ctx->cap_q_ctx;
1255
1256	INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
1257	INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
1258	spin_lock_init(&out_q_ctx->rdy_spinlock);
1259	spin_lock_init(&cap_q_ctx->rdy_spinlock);
1260
1261	INIT_LIST_HEAD(&m2m_ctx->queue);
1262
1263	ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
1264
1265	if (ret)
1266		goto err;
1267	/*
1268	 * Both queues should use same the mutex to lock the m2m context.
1269	 * This lock is used in some v4l2_m2m_* helpers.
1270	 */
1271	if (WARN_ON(out_q_ctx->q.lock != cap_q_ctx->q.lock)) {
1272		ret = -EINVAL;
1273		goto err;
1274	}
1275	m2m_ctx->q_lock = out_q_ctx->q.lock;
1276
1277	return m2m_ctx;
1278err:
1279	kfree(m2m_ctx);
1280	return ERR_PTR(ret);
1281}
1282EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
1283
1284void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
1285{
1286	/* wait until the current context is dequeued from job_queue */
1287	v4l2_m2m_cancel_job(m2m_ctx);
1288
1289	vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
1290	vb2_queue_release(&m2m_ctx->out_q_ctx.q);
1291
1292	kfree(m2m_ctx);
1293}
1294EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
1295
1296void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
1297		struct vb2_v4l2_buffer *vbuf)
1298{
1299	struct v4l2_m2m_buffer *b = container_of(vbuf,
1300				struct v4l2_m2m_buffer, vb);
1301	struct v4l2_m2m_queue_ctx *q_ctx;
1302	unsigned long flags;
1303
1304	q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type);
1305
1306	spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
1307	list_add_tail(&b->list, &q_ctx->rdy_queue);
1308	q_ctx->num_rdy++;
1309	spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
1310}
1311EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
1312
1313void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb,
1314				struct vb2_v4l2_buffer *cap_vb)
1315{
1316	const u32 mask = V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1317
1318	cap_vb->vb2_buf.timestamp = out_vb->vb2_buf.timestamp;
1319
1320	if (out_vb->flags & V4L2_BUF_FLAG_TIMECODE)
1321		cap_vb->timecode = out_vb->timecode;
1322	cap_vb->field = out_vb->field;
1323	cap_vb->flags &= ~mask;
1324	cap_vb->flags |= out_vb->flags & mask;
1325	cap_vb->vb2_buf.copied_timestamp = 1;
1326}
1327EXPORT_SYMBOL_GPL(v4l2_m2m_buf_copy_metadata);
1328
1329void v4l2_m2m_request_queue(struct media_request *req)
1330{
1331	struct media_request_object *obj, *obj_safe;
1332	struct v4l2_m2m_ctx *m2m_ctx = NULL;
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		struct v4l2_m2m_ctx *m2m_ctx_obj;
1344		struct vb2_buffer *vb;
1345
1346		if (!obj->ops->queue)
1347			continue;
1348
1349		if (vb2_request_object_is_buffer(obj)) {
1350			/* Sanity checks */
1351			vb = container_of(obj, struct vb2_buffer, req_obj);
1352			WARN_ON(!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type));
1353			m2m_ctx_obj = container_of(vb->vb2_queue,
1354						   struct v4l2_m2m_ctx,
1355						   out_q_ctx.q);
1356			WARN_ON(m2m_ctx && m2m_ctx_obj != m2m_ctx);
1357			m2m_ctx = m2m_ctx_obj;
1358		}
1359
1360		/*
1361		 * The buffer we queue here can in theory be immediately
1362		 * unbound, hence the use of list_for_each_entry_safe()
1363		 * above and why we call the queue op last.
1364		 */
1365		obj->ops->queue(obj);
1366	}
1367
1368	WARN_ON(!m2m_ctx);
1369
1370	if (m2m_ctx)
1371		v4l2_m2m_try_schedule(m2m_ctx);
1372}
1373EXPORT_SYMBOL_GPL(v4l2_m2m_request_queue);
1374
1375/* Videobuf2 ioctl helpers */
1376
1377int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
1378				struct v4l2_requestbuffers *rb)
1379{
1380	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1381
1382	return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
1383}
1384EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
1385
1386int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
1387				struct v4l2_create_buffers *create)
1388{
1389	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1390
1391	return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
1392}
1393EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
1394
1395int v4l2_m2m_ioctl_remove_bufs(struct file *file, void *priv,
1396			       struct v4l2_remove_buffers *remove)
1397{
1398	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1399	struct vb2_queue *q = v4l2_m2m_get_vq(fh->m2m_ctx, remove->type);
1400
1401	if (q->type != remove->type)
1402		return -EINVAL;
1403
1404	return vb2_core_remove_bufs(q, remove->index, remove->count);
1405}
1406EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_remove_bufs);
1407
1408int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
1409				struct v4l2_buffer *buf)
1410{
1411	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1412
1413	return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
1414}
1415EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
1416
1417int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
1418				struct v4l2_buffer *buf)
1419{
1420	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1421
1422	return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
1423}
1424EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
1425
1426int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
1427				struct v4l2_buffer *buf)
1428{
1429	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1430
1431	return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
1432}
1433EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
1434
1435int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
1436			       struct v4l2_buffer *buf)
1437{
1438	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1439
1440	return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
1441}
1442EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
1443
1444int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
1445				struct v4l2_exportbuffer *eb)
1446{
1447	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1448
1449	return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
1450}
1451EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
1452
1453int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
1454				enum v4l2_buf_type type)
1455{
1456	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1457
1458	return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
1459}
1460EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
1461
1462int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
1463				enum v4l2_buf_type type)
1464{
1465	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1466
1467	return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
1468}
1469EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
1470
1471int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *priv,
1472				   struct v4l2_encoder_cmd *ec)
1473{
1474	if (ec->cmd != V4L2_ENC_CMD_STOP && ec->cmd != V4L2_ENC_CMD_START)
1475		return -EINVAL;
1476
1477	ec->flags = 0;
1478	return 0;
1479}
1480EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_encoder_cmd);
1481
1482int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *priv,
1483				   struct v4l2_decoder_cmd *dc)
1484{
1485	if (dc->cmd != V4L2_DEC_CMD_STOP && dc->cmd != V4L2_DEC_CMD_START)
1486		return -EINVAL;
1487
1488	dc->flags = 0;
1489
1490	if (dc->cmd == V4L2_DEC_CMD_STOP) {
1491		dc->stop.pts = 0;
1492	} else if (dc->cmd == V4L2_DEC_CMD_START) {
1493		dc->start.speed = 0;
1494		dc->start.format = V4L2_DEC_START_FMT_NONE;
1495	}
1496	return 0;
1497}
1498EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_decoder_cmd);
1499
1500/*
1501 * Updates the encoding state on ENC_CMD_STOP/ENC_CMD_START
1502 * Should be called from the encoder driver encoder_cmd() callback
1503 */
1504int v4l2_m2m_encoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
1505			 struct v4l2_encoder_cmd *ec)
1506{
1507	if (ec->cmd != V4L2_ENC_CMD_STOP && ec->cmd != V4L2_ENC_CMD_START)
1508		return -EINVAL;
1509
1510	if (ec->cmd == V4L2_ENC_CMD_STOP)
1511		return v4l2_update_last_buf_state(m2m_ctx);
1512
1513	if (m2m_ctx->is_draining)
1514		return -EBUSY;
1515
1516	if (m2m_ctx->has_stopped)
1517		m2m_ctx->has_stopped = false;
1518
1519	return 0;
1520}
1521EXPORT_SYMBOL_GPL(v4l2_m2m_encoder_cmd);
1522
1523/*
1524 * Updates the decoding state on DEC_CMD_STOP/DEC_CMD_START
1525 * Should be called from the decoder driver decoder_cmd() callback
1526 */
1527int v4l2_m2m_decoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
1528			 struct v4l2_decoder_cmd *dc)
1529{
1530	if (dc->cmd != V4L2_DEC_CMD_STOP && dc->cmd != V4L2_DEC_CMD_START)
1531		return -EINVAL;
1532
1533	if (dc->cmd == V4L2_DEC_CMD_STOP)
1534		return v4l2_update_last_buf_state(m2m_ctx);
1535
1536	if (m2m_ctx->is_draining)
1537		return -EBUSY;
1538
1539	if (m2m_ctx->has_stopped)
1540		m2m_ctx->has_stopped = false;
1541
1542	return 0;
1543}
1544EXPORT_SYMBOL_GPL(v4l2_m2m_decoder_cmd);
1545
1546int v4l2_m2m_ioctl_encoder_cmd(struct file *file, void *priv,
1547			       struct v4l2_encoder_cmd *ec)
1548{
1549	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1550
1551	return v4l2_m2m_encoder_cmd(file, fh->m2m_ctx, ec);
1552}
1553EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_encoder_cmd);
1554
1555int v4l2_m2m_ioctl_decoder_cmd(struct file *file, void *priv,
1556			       struct v4l2_decoder_cmd *dc)
1557{
1558	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1559
1560	return v4l2_m2m_decoder_cmd(file, fh->m2m_ctx, dc);
1561}
1562EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_decoder_cmd);
1563
1564int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *priv,
1565					     struct v4l2_decoder_cmd *dc)
1566{
1567	if (dc->cmd != V4L2_DEC_CMD_FLUSH)
1568		return -EINVAL;
1569
1570	dc->flags = 0;
1571
1572	return 0;
1573}
1574EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_try_decoder_cmd);
1575
1576int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv,
1577					 struct v4l2_decoder_cmd *dc)
1578{
1579	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1580	struct vb2_v4l2_buffer *out_vb, *cap_vb;
1581	struct v4l2_m2m_dev *m2m_dev = fh->m2m_ctx->m2m_dev;
1582	unsigned long flags;
1583	int ret;
1584
1585	ret = v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, dc);
1586	if (ret < 0)
1587		return ret;
1588
1589	spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
1590	out_vb = v4l2_m2m_last_src_buf(fh->m2m_ctx);
1591	cap_vb = v4l2_m2m_last_dst_buf(fh->m2m_ctx);
1592
1593	/*
1594	 * If there is an out buffer pending, then clear any HOLD flag.
1595	 *
1596	 * By clearing this flag we ensure that when this output
1597	 * buffer is processed any held capture buffer will be released.
1598	 */
1599	if (out_vb) {
1600		out_vb->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
1601	} else if (cap_vb && cap_vb->is_held) {
1602		/*
1603		 * If there were no output buffers, but there is a
1604		 * capture buffer that is held, then release that
1605		 * buffer.
1606		 */
1607		cap_vb->is_held = false;
1608		v4l2_m2m_dst_buf_remove(fh->m2m_ctx);
1609		v4l2_m2m_buf_done(cap_vb, VB2_BUF_STATE_DONE);
1610	}
1611	spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
1612
1613	return 0;
1614}
1615EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_decoder_cmd);
1616
1617/*
1618 * v4l2_file_operations helpers. It is assumed here same lock is used
1619 * for the output and the capture buffer queue.
1620 */
1621
1622int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
1623{
1624	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1625
1626	return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
1627}
1628EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
1629
1630__poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
1631{
1632	struct v4l2_fh *fh = file_to_v4l2_fh(file);
1633	struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
1634	__poll_t ret;
1635
1636	if (m2m_ctx->q_lock)
1637		mutex_lock(m2m_ctx->q_lock);
1638
1639	ret = v4l2_m2m_poll(file, m2m_ctx, wait);
1640
1641	if (m2m_ctx->q_lock)
1642		mutex_unlock(m2m_ctx->q_lock);
1643
1644	return ret;
1645}
1646EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);
1647
~/ Read-only file preview.