File Index
Files and directories
Sign in
FILE

pcm_native.c

File preview with syntax highlighting.

linux-6.19.14/sound/core/pcm_native.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Digital Audio (PCM) abstract layer
   4 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   5 */
   6
   7#include <linux/compat.h>
   8#include <linux/mm.h>
   9#include <linux/module.h>
  10#include <linux/file.h>
  11#include <linux/slab.h>
  12#include <linux/sched/signal.h>
  13#include <linux/time.h>
  14#include <linux/pm_qos.h>
  15#include <linux/io.h>
  16#include <linux/dma-mapping.h>
  17#include <linux/vmalloc.h>
  18#include <sound/core.h>
  19#include <sound/control.h>
  20#include <sound/info.h>
  21#include <sound/pcm.h>
  22#include <sound/pcm_params.h>
  23#include <sound/timer.h>
  24#include <sound/minors.h>
  25#include <linux/uio.h>
  26#include <linux/delay.h>
  27#include <linux/bitops.h>
  28
  29#include "pcm_local.h"
  30
  31#ifdef CONFIG_SND_DEBUG
  32#define CREATE_TRACE_POINTS
  33#include "pcm_param_trace.h"
  34#else
  35#define trace_hw_mask_param_enabled()		0
  36#define trace_hw_interval_param_enabled()	0
  37#define trace_hw_mask_param(substream, type, index, prev, curr)
  38#define trace_hw_interval_param(substream, type, index, prev, curr)
  39#endif
  40
  41/*
  42 *  Compatibility
  43 */
  44
  45struct snd_pcm_hw_params_old {
  46	unsigned int flags;
  47	unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
  48			   SNDRV_PCM_HW_PARAM_ACCESS + 1];
  49	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
  50					SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
  51	unsigned int rmask;
  52	unsigned int cmask;
  53	unsigned int info;
  54	unsigned int msbits;
  55	unsigned int rate_num;
  56	unsigned int rate_den;
  57	snd_pcm_uframes_t fifo_size;
  58	unsigned char reserved[64];
  59};
  60
  61#ifdef CONFIG_SND_SUPPORT_OLD_API
  62#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
  63#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
  64
  65static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  66				      struct snd_pcm_hw_params_old __user * _oparams);
  67static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  68				      struct snd_pcm_hw_params_old __user * _oparams);
  69#endif
  70static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
  71
  72/*
  73 *
  74 */
  75
  76static DECLARE_RWSEM(snd_pcm_link_rwsem);
  77
  78void snd_pcm_group_init(struct snd_pcm_group *group)
  79{
  80	spin_lock_init(&group->lock);
  81	mutex_init(&group->mutex);
  82	INIT_LIST_HEAD(&group->substreams);
  83	refcount_set(&group->refs, 1);
  84}
  85
  86/* define group lock helpers */
  87#define DEFINE_PCM_GROUP_LOCK(action, bh_lock, bh_unlock, mutex_action) \
  88static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
  89{ \
  90	if (nonatomic) { \
  91		mutex_ ## mutex_action(&group->mutex); \
  92	} else { \
  93		if (IS_ENABLED(CONFIG_PREEMPT_RT) && bh_lock)   \
  94			local_bh_disable();			\
  95		spin_ ## action(&group->lock);			\
  96		if (IS_ENABLED(CONFIG_PREEMPT_RT) && bh_unlock) \
  97			local_bh_enable();                      \
  98	}							\
  99}
 100
 101DEFINE_PCM_GROUP_LOCK(lock, false, false, lock);
 102DEFINE_PCM_GROUP_LOCK(unlock, false, false, unlock);
 103DEFINE_PCM_GROUP_LOCK(lock_irq, true, false, lock);
 104DEFINE_PCM_GROUP_LOCK(unlock_irq, false, true, unlock);
 105
 106/**
 107 * snd_pcm_stream_lock - Lock the PCM stream
 108 * @substream: PCM substream
 109 *
 110 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
 111 * flag of the given substream.  This also takes the global link rw lock
 112 * (or rw sem), too, for avoiding the race with linked streams.
 113 */
 114void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
 115{
 116	snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
 117}
 118EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
 119
 120/**
 121 * snd_pcm_stream_unlock - Unlock the PCM stream
 122 * @substream: PCM substream
 123 *
 124 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
 125 */
 126void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
 127{
 128	snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
 129}
 130EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
 131
 132/**
 133 * snd_pcm_stream_lock_irq - Lock the PCM stream
 134 * @substream: PCM substream
 135 *
 136 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
 137 * IRQ (only when nonatomic is false).  In nonatomic case, this is identical
 138 * as snd_pcm_stream_lock().
 139 */
 140void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
 141{
 142	snd_pcm_group_lock_irq(&substream->self_group,
 143			       substream->pcm->nonatomic);
 144}
 145EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
 146
 147static void snd_pcm_stream_lock_nested(struct snd_pcm_substream *substream)
 148{
 149	struct snd_pcm_group *group = &substream->self_group;
 150
 151	if (substream->pcm->nonatomic)
 152		mutex_lock_nested(&group->mutex, SINGLE_DEPTH_NESTING);
 153	else
 154		spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
 155}
 156
 157/**
 158 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
 159 * @substream: PCM substream
 160 *
 161 * This is a counter-part of snd_pcm_stream_lock_irq().
 162 */
 163void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
 164{
 165	snd_pcm_group_unlock_irq(&substream->self_group,
 166				 substream->pcm->nonatomic);
 167}
 168EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
 169
 170unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
 171{
 172	unsigned long flags = 0;
 173	if (substream->pcm->nonatomic)
 174		mutex_lock(&substream->self_group.mutex);
 175	else
 176		spin_lock_irqsave(&substream->self_group.lock, flags);
 177	return flags;
 178}
 179EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
 180
 181unsigned long _snd_pcm_stream_lock_irqsave_nested(struct snd_pcm_substream *substream)
 182{
 183	unsigned long flags = 0;
 184	if (substream->pcm->nonatomic)
 185		mutex_lock_nested(&substream->self_group.mutex,
 186				  SINGLE_DEPTH_NESTING);
 187	else
 188		spin_lock_irqsave_nested(&substream->self_group.lock, flags,
 189					 SINGLE_DEPTH_NESTING);
 190	return flags;
 191}
 192EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave_nested);
 193
 194/**
 195 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
 196 * @substream: PCM substream
 197 * @flags: irq flags
 198 *
 199 * This is a counter-part of snd_pcm_stream_lock_irqsave().
 200 */
 201void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
 202				      unsigned long flags)
 203{
 204	if (substream->pcm->nonatomic)
 205		mutex_unlock(&substream->self_group.mutex);
 206	else
 207		spin_unlock_irqrestore(&substream->self_group.lock, flags);
 208}
 209EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
 210
 211/* Run PCM ioctl ops */
 212static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
 213			     unsigned cmd, void *arg)
 214{
 215	if (substream->ops->ioctl)
 216		return substream->ops->ioctl(substream, cmd, arg);
 217	else
 218		return snd_pcm_lib_ioctl(substream, cmd, arg);
 219}
 220
 221int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
 222{
 223	struct snd_pcm *pcm = substream->pcm;
 224	struct snd_pcm_str *pstr = substream->pstr;
 225
 226	memset(info, 0, sizeof(*info));
 227	info->card = pcm->card->number;
 228	info->device = pcm->device;
 229	info->stream = substream->stream;
 230	info->subdevice = substream->number;
 231	strscpy(info->id, pcm->id, sizeof(info->id));
 232	strscpy(info->name, pcm->name, sizeof(info->name));
 233	info->dev_class = pcm->dev_class;
 234	info->dev_subclass = pcm->dev_subclass;
 235	info->subdevices_count = pstr->substream_count;
 236	info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
 237	strscpy(info->subname, substream->name, sizeof(info->subname));
 238
 239	return 0;
 240}
 241
 242int snd_pcm_info_user(struct snd_pcm_substream *substream,
 243		      struct snd_pcm_info __user * _info)
 244{
 245	int err;
 246	struct snd_pcm_info *info __free(kfree) =
 247		kmalloc(sizeof(*info), GFP_KERNEL);
 248
 249	if (! info)
 250		return -ENOMEM;
 251	err = snd_pcm_info(substream, info);
 252	if (err >= 0) {
 253		if (copy_to_user(_info, info, sizeof(*info)))
 254			err = -EFAULT;
 255	}
 256	return err;
 257}
 258
 259/* macro for simplified cast */
 260#define PARAM_MASK_BIT(b)	(1U << (__force int)(b))
 261
 262static bool hw_support_mmap(struct snd_pcm_substream *substream)
 263{
 264	struct snd_dma_buffer *dmabuf;
 265
 266	if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
 267		return false;
 268
 269	if (substream->ops->mmap || substream->ops->page)
 270		return true;
 271
 272	dmabuf = snd_pcm_get_dma_buf(substream);
 273	if (!dmabuf)
 274		dmabuf = &substream->dma_buffer;
 275	switch (dmabuf->dev.type) {
 276	case SNDRV_DMA_TYPE_UNKNOWN:
 277		/* we can't know the device, so just assume that the driver does
 278		 * everything right
 279		 */
 280		return true;
 281	case SNDRV_DMA_TYPE_CONTINUOUS:
 282	case SNDRV_DMA_TYPE_VMALLOC:
 283		return true;
 284	default:
 285		return dma_can_mmap(dmabuf->dev.dev);
 286	}
 287}
 288
 289static int constrain_mask_params(struct snd_pcm_substream *substream,
 290				 struct snd_pcm_hw_params *params)
 291{
 292	struct snd_pcm_hw_constraints *constrs =
 293					&substream->runtime->hw_constraints;
 294	struct snd_mask *m;
 295	unsigned int k;
 296	struct snd_mask old_mask __maybe_unused;
 297	int changed;
 298
 299	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
 300		m = hw_param_mask(params, k);
 301		if (snd_mask_empty(m))
 302			return -EINVAL;
 303
 304		/* This parameter is not requested to change by a caller. */
 305		if (!(params->rmask & PARAM_MASK_BIT(k)))
 306			continue;
 307
 308		if (trace_hw_mask_param_enabled())
 309			old_mask = *m;
 310
 311		changed = snd_mask_refine(m, constrs_mask(constrs, k));
 312		if (changed < 0)
 313			return changed;
 314		if (changed == 0)
 315			continue;
 316
 317		/* Set corresponding flag so that the caller gets it. */
 318		trace_hw_mask_param(substream, k, 0, &old_mask, m);
 319		params->cmask |= PARAM_MASK_BIT(k);
 320	}
 321
 322	return 0;
 323}
 324
 325static int constrain_interval_params(struct snd_pcm_substream *substream,
 326				     struct snd_pcm_hw_params *params)
 327{
 328	struct snd_pcm_hw_constraints *constrs =
 329					&substream->runtime->hw_constraints;
 330	struct snd_interval *i;
 331	unsigned int k;
 332	struct snd_interval old_interval __maybe_unused;
 333	int changed;
 334
 335	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
 336		i = hw_param_interval(params, k);
 337		if (snd_interval_empty(i))
 338			return -EINVAL;
 339
 340		/* This parameter is not requested to change by a caller. */
 341		if (!(params->rmask & PARAM_MASK_BIT(k)))
 342			continue;
 343
 344		if (trace_hw_interval_param_enabled())
 345			old_interval = *i;
 346
 347		changed = snd_interval_refine(i, constrs_interval(constrs, k));
 348		if (changed < 0)
 349			return changed;
 350		if (changed == 0)
 351			continue;
 352
 353		/* Set corresponding flag so that the caller gets it. */
 354		trace_hw_interval_param(substream, k, 0, &old_interval, i);
 355		params->cmask |= PARAM_MASK_BIT(k);
 356	}
 357
 358	return 0;
 359}
 360
 361static int constrain_params_by_rules(struct snd_pcm_substream *substream,
 362				     struct snd_pcm_hw_params *params)
 363{
 364	struct snd_pcm_hw_constraints *constrs =
 365					&substream->runtime->hw_constraints;
 366	unsigned int k;
 367	unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
 368	unsigned int stamp;
 369	struct snd_pcm_hw_rule *r;
 370	unsigned int d;
 371	struct snd_mask old_mask __maybe_unused;
 372	struct snd_interval old_interval __maybe_unused;
 373	bool again;
 374	int changed, err = 0;
 375
 376	/*
 377	 * Each application of rule has own sequence number.
 378	 *
 379	 * Each member of 'rstamps' array represents the sequence number of
 380	 * recent application of corresponding rule.
 381	 */
 382	unsigned int *rstamps __free(kfree) =
 383		kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
 384	if (!rstamps)
 385		return -ENOMEM;
 386
 387	/*
 388	 * Each member of 'vstamps' array represents the sequence number of
 389	 * recent application of rule in which corresponding parameters were
 390	 * changed.
 391	 *
 392	 * In initial state, elements corresponding to parameters requested by
 393	 * a caller is 1. For unrequested parameters, corresponding members
 394	 * have 0 so that the parameters are never changed anymore.
 395	 */
 396	for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
 397		vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
 398
 399	/* Due to the above design, actual sequence number starts at 2. */
 400	stamp = 2;
 401retry:
 402	/* Apply all rules in order. */
 403	again = false;
 404	for (k = 0; k < constrs->rules_num; k++) {
 405		r = &constrs->rules[k];
 406
 407		/*
 408		 * Check condition bits of this rule. When the rule has
 409		 * some condition bits, parameter without the bits is
 410		 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
 411		 * is an example of the condition bits.
 412		 */
 413		if (r->cond && !(r->cond & params->flags))
 414			continue;
 415
 416		/*
 417		 * The 'deps' array includes maximum four dependencies
 418		 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
 419		 * member of this array is a sentinel and should be
 420		 * negative value.
 421		 *
 422		 * This rule should be processed in this time when dependent
 423		 * parameters were changed at former applications of the other
 424		 * rules.
 425		 */
 426		for (d = 0; r->deps[d] >= 0; d++) {
 427			if (vstamps[r->deps[d]] > rstamps[k])
 428				break;
 429		}
 430		if (r->deps[d] < 0)
 431			continue;
 432
 433		if (trace_hw_mask_param_enabled()) {
 434			if (hw_is_mask(r->var))
 435				old_mask = *hw_param_mask(params, r->var);
 436		}
 437		if (trace_hw_interval_param_enabled()) {
 438			if (hw_is_interval(r->var))
 439				old_interval = *hw_param_interval(params, r->var);
 440		}
 441
 442		changed = r->func(params, r);
 443		if (changed < 0)
 444			return changed;
 445
 446		/*
 447		 * When the parameter is changed, notify it to the caller
 448		 * by corresponding returned bit, then preparing for next
 449		 * iteration.
 450		 */
 451		if (changed && r->var >= 0) {
 452			if (hw_is_mask(r->var)) {
 453				trace_hw_mask_param(substream, r->var,
 454					k + 1, &old_mask,
 455					hw_param_mask(params, r->var));
 456			}
 457			if (hw_is_interval(r->var)) {
 458				trace_hw_interval_param(substream, r->var,
 459					k + 1, &old_interval,
 460					hw_param_interval(params, r->var));
 461			}
 462
 463			params->cmask |= PARAM_MASK_BIT(r->var);
 464			vstamps[r->var] = stamp;
 465			again = true;
 466		}
 467
 468		rstamps[k] = stamp++;
 469	}
 470
 471	/* Iterate to evaluate all rules till no parameters are changed. */
 472	if (again)
 473		goto retry;
 474
 475	return err;
 476}
 477
 478static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
 479				     struct snd_pcm_hw_params *params)
 480{
 481	const struct snd_interval *i;
 482	const struct snd_mask *m;
 483	struct snd_mask *m_rw;
 484	int err;
 485
 486	if (!params->msbits) {
 487		i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
 488		if (snd_interval_single(i))
 489			params->msbits = snd_interval_value(i);
 490		m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
 491		if (snd_mask_single(m)) {
 492			snd_pcm_format_t format = (__force snd_pcm_format_t)snd_mask_min(m);
 493			params->msbits = snd_pcm_format_width(format);
 494		}
 495	}
 496
 497	if (params->msbits) {
 498		m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
 499		if (snd_mask_single(m)) {
 500			snd_pcm_format_t format = (__force snd_pcm_format_t)snd_mask_min(m);
 501
 502			if (snd_pcm_format_linear(format) &&
 503			    snd_pcm_format_width(format) != params->msbits) {
 504				m_rw = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT);
 505				snd_mask_reset(m_rw,
 506					       (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX);
 507				if (snd_mask_empty(m_rw))
 508					return -EINVAL;
 509			}
 510		}
 511	}
 512
 513	if (!params->rate_den) {
 514		i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
 515		if (snd_interval_single(i)) {
 516			params->rate_num = snd_interval_value(i);
 517			params->rate_den = 1;
 518		}
 519	}
 520
 521	if (!params->fifo_size) {
 522		m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
 523		i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 524		if (snd_mask_single(m) && snd_interval_single(i)) {
 525			err = snd_pcm_ops_ioctl(substream,
 526						SNDRV_PCM_IOCTL1_FIFO_SIZE,
 527						params);
 528			if (err < 0)
 529				return err;
 530		}
 531	}
 532
 533	if (!params->info) {
 534		params->info = substream->runtime->hw.info;
 535		params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
 536				  SNDRV_PCM_INFO_DRAIN_TRIGGER);
 537		if (!hw_support_mmap(substream))
 538			params->info &= ~(SNDRV_PCM_INFO_MMAP |
 539					  SNDRV_PCM_INFO_MMAP_VALID);
 540	}
 541
 542	err = snd_pcm_ops_ioctl(substream,
 543				SNDRV_PCM_IOCTL1_SYNC_ID,
 544				params);
 545	if (err < 0)
 546		return err;
 547
 548	return 0;
 549}
 550
 551int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
 552		      struct snd_pcm_hw_params *params)
 553{
 554	int err;
 555
 556	params->info = 0;
 557	params->fifo_size = 0;
 558	if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
 559		params->msbits = 0;
 560	if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
 561		params->rate_num = 0;
 562		params->rate_den = 0;
 563	}
 564
 565	err = constrain_mask_params(substream, params);
 566	if (err < 0)
 567		return err;
 568
 569	err = constrain_interval_params(substream, params);
 570	if (err < 0)
 571		return err;
 572
 573	err = constrain_params_by_rules(substream, params);
 574	if (err < 0)
 575		return err;
 576
 577	params->rmask = 0;
 578
 579	return 0;
 580}
 581EXPORT_SYMBOL(snd_pcm_hw_refine);
 582
 583static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
 584				  struct snd_pcm_hw_params __user * _params)
 585{
 586	int err;
 587	struct snd_pcm_hw_params *params __free(kfree) =
 588		memdup_user(_params, sizeof(*params));
 589
 590	if (IS_ERR(params))
 591		return PTR_ERR(params);
 592
 593	err = snd_pcm_hw_refine(substream, params);
 594	if (err < 0)
 595		return err;
 596
 597	err = fixup_unreferenced_params(substream, params);
 598	if (err < 0)
 599		return err;
 600
 601	if (copy_to_user(_params, params, sizeof(*params)))
 602		return -EFAULT;
 603	return 0;
 604}
 605
 606static int period_to_usecs(struct snd_pcm_runtime *runtime)
 607{
 608	int usecs;
 609
 610	if (! runtime->rate)
 611		return -1; /* invalid */
 612
 613	/* take 75% of period time as the deadline */
 614	usecs = (750000 / runtime->rate) * runtime->period_size;
 615	usecs += ((750000 % runtime->rate) * runtime->period_size) /
 616		runtime->rate;
 617
 618	return usecs;
 619}
 620
 621static void snd_pcm_set_state(struct snd_pcm_substream *substream,
 622			      snd_pcm_state_t state)
 623{
 624	guard(pcm_stream_lock_irq)(substream);
 625	if (substream->runtime->state != SNDRV_PCM_STATE_DISCONNECTED)
 626		__snd_pcm_set_state(substream->runtime, state);
 627}
 628
 629static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
 630					int event)
 631{
 632#ifdef CONFIG_SND_PCM_TIMER
 633	if (substream->timer)
 634		snd_timer_notify(substream->timer, event,
 635					&substream->runtime->trigger_tstamp);
 636#endif
 637}
 638
 639void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
 640{
 641	if (substream->runtime && substream->runtime->stop_operating) {
 642		substream->runtime->stop_operating = false;
 643		if (substream->ops && substream->ops->sync_stop)
 644			substream->ops->sync_stop(substream);
 645		else if (sync_irq && substream->pcm->card->sync_irq > 0)
 646			synchronize_irq(substream->pcm->card->sync_irq);
 647	}
 648}
 649
 650/**
 651 * snd_pcm_hw_params_choose - choose a configuration defined by @params
 652 * @pcm: PCM instance
 653 * @params: the hw_params instance
 654 *
 655 * Choose one configuration from configuration space defined by @params.
 656 * The configuration chosen is that obtained fixing in this order:
 657 * first access, first format, first subformat, min channels,
 658 * min rate, min period time, max buffer size, min tick time
 659 *
 660 * Return: Zero if successful, or a negative error code on failure.
 661 */
 662static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
 663				    struct snd_pcm_hw_params *params)
 664{
 665	static const int vars[] = {
 666		SNDRV_PCM_HW_PARAM_ACCESS,
 667		SNDRV_PCM_HW_PARAM_FORMAT,
 668		SNDRV_PCM_HW_PARAM_SUBFORMAT,
 669		SNDRV_PCM_HW_PARAM_CHANNELS,
 670		SNDRV_PCM_HW_PARAM_RATE,
 671		SNDRV_PCM_HW_PARAM_PERIOD_TIME,
 672		SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 673		SNDRV_PCM_HW_PARAM_TICK_TIME,
 674		-1
 675	};
 676	const int *v;
 677	struct snd_mask old_mask __maybe_unused;
 678	struct snd_interval old_interval __maybe_unused;
 679	int changed;
 680
 681	for (v = vars; *v != -1; v++) {
 682		/* Keep old parameter to trace. */
 683		if (trace_hw_mask_param_enabled()) {
 684			if (hw_is_mask(*v))
 685				old_mask = *hw_param_mask(params, *v);
 686		}
 687		if (trace_hw_interval_param_enabled()) {
 688			if (hw_is_interval(*v))
 689				old_interval = *hw_param_interval(params, *v);
 690		}
 691		if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
 692			changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
 693		else
 694			changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
 695		if (changed < 0)
 696			return changed;
 697		if (changed == 0)
 698			continue;
 699
 700		/* Trace the changed parameter. */
 701		if (hw_is_mask(*v)) {
 702			trace_hw_mask_param(pcm, *v, 0, &old_mask,
 703					    hw_param_mask(params, *v));
 704		}
 705		if (hw_is_interval(*v)) {
 706			trace_hw_interval_param(pcm, *v, 0, &old_interval,
 707						hw_param_interval(params, *v));
 708		}
 709	}
 710
 711	return 0;
 712}
 713
 714/* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise
 715 * block the further r/w operations
 716 */
 717static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime)
 718{
 719	if (!atomic_dec_unless_positive(&runtime->buffer_accessing))
 720		return -EBUSY;
 721	mutex_lock(&runtime->buffer_mutex);
 722	return 0; /* keep buffer_mutex, unlocked by below */
 723}
 724
 725/* release buffer_mutex and clear r/w access flag */
 726static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime)
 727{
 728	mutex_unlock(&runtime->buffer_mutex);
 729	atomic_inc(&runtime->buffer_accessing);
 730}
 731
 732/* fill the PCM buffer with the current silence format; called from pcm_oss.c */
 733int snd_pcm_runtime_buffer_set_silence(struct snd_pcm_runtime *runtime)
 734{
 735	int err;
 736
 737	err = snd_pcm_buffer_access_lock(runtime);
 738	if (err < 0)
 739		return err;
 740	if (runtime->dma_area)
 741		snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
 742					   bytes_to_samples(runtime, runtime->dma_bytes));
 743	snd_pcm_buffer_access_unlock(runtime);
 744	return 0;
 745}
 746EXPORT_SYMBOL_GPL(snd_pcm_runtime_buffer_set_silence);
 747
 748#if IS_ENABLED(CONFIG_SND_PCM_OSS)
 749#define is_oss_stream(substream)	((substream)->oss.oss)
 750#else
 751#define is_oss_stream(substream)	false
 752#endif
 753
 754static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 755			     struct snd_pcm_hw_params *params)
 756{
 757	struct snd_pcm_runtime *runtime;
 758	int err, usecs;
 759	unsigned int bits;
 760	snd_pcm_uframes_t frames;
 761
 762	if (PCM_RUNTIME_CHECK(substream))
 763		return -ENXIO;
 764	runtime = substream->runtime;
 765	err = snd_pcm_buffer_access_lock(runtime);
 766	if (err < 0)
 767		return err;
 768	scoped_guard(pcm_stream_lock_irq, substream) {
 769		switch (runtime->state) {
 770		case SNDRV_PCM_STATE_OPEN:
 771		case SNDRV_PCM_STATE_SETUP:
 772		case SNDRV_PCM_STATE_PREPARED:
 773			if (!is_oss_stream(substream) &&
 774			    atomic_read(&substream->mmap_count))
 775				err = -EBADFD;
 776			break;
 777		default:
 778			err = -EBADFD;
 779			break;
 780		}
 781	}
 782	if (err)
 783		goto unlock;
 784
 785	snd_pcm_sync_stop(substream, true);
 786
 787	params->rmask = ~0U;
 788	err = snd_pcm_hw_refine(substream, params);
 789	if (err < 0)
 790		goto _error;
 791
 792	err = snd_pcm_hw_params_choose(substream, params);
 793	if (err < 0)
 794		goto _error;
 795
 796	err = fixup_unreferenced_params(substream, params);
 797	if (err < 0)
 798		goto _error;
 799
 800	if (substream->managed_buffer_alloc) {
 801		err = snd_pcm_lib_malloc_pages(substream,
 802					       params_buffer_bytes(params));
 803		if (err < 0)
 804			goto _error;
 805		runtime->buffer_changed = err > 0;
 806	}
 807
 808	if (substream->ops->hw_params != NULL) {
 809		err = substream->ops->hw_params(substream, params);
 810		if (err < 0)
 811			goto _error;
 812	}
 813
 814	runtime->access = params_access(params);
 815	runtime->format = params_format(params);
 816	runtime->subformat = params_subformat(params);
 817	runtime->channels = params_channels(params);
 818	runtime->rate = params_rate(params);
 819	runtime->period_size = params_period_size(params);
 820	runtime->periods = params_periods(params);
 821	runtime->buffer_size = params_buffer_size(params);
 822	runtime->info = params->info;
 823	runtime->rate_num = params->rate_num;
 824	runtime->rate_den = params->rate_den;
 825	runtime->no_period_wakeup =
 826			(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
 827			(params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
 828
 829	bits = snd_pcm_format_physical_width(runtime->format);
 830	runtime->sample_bits = bits;
 831	bits *= runtime->channels;
 832	runtime->frame_bits = bits;
 833	frames = 1;
 834	while (bits % 8 != 0) {
 835		bits *= 2;
 836		frames *= 2;
 837	}
 838	runtime->byte_align = bits / 8;
 839	runtime->min_align = frames;
 840
 841	/* Default sw params */
 842	runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
 843	runtime->period_step = 1;
 844	runtime->control->avail_min = runtime->period_size;
 845	runtime->start_threshold = 1;
 846	runtime->stop_threshold = runtime->buffer_size;
 847	runtime->silence_threshold = 0;
 848	runtime->silence_size = 0;
 849	runtime->boundary = runtime->buffer_size;
 850	while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
 851		runtime->boundary *= 2;
 852
 853	/* clear the buffer for avoiding possible kernel info leaks */
 854	if (runtime->dma_area && !substream->ops->copy) {
 855		size_t size = runtime->dma_bytes;
 856
 857		if (runtime->info & SNDRV_PCM_INFO_MMAP)
 858			size = PAGE_ALIGN(size);
 859		memset(runtime->dma_area, 0, size);
 860	}
 861
 862	snd_pcm_timer_resolution_change(substream);
 863	snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
 864
 865	if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
 866		cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
 867	usecs = period_to_usecs(runtime);
 868	if (usecs >= 0)
 869		cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
 870					    usecs);
 871	err = 0;
 872 _error:
 873	if (err) {
 874		/* hardware might be unusable from this time,
 875		 * so we force application to retry to set
 876		 * the correct hardware parameter settings
 877		 */
 878		snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 879		if (substream->ops->hw_free != NULL)
 880			substream->ops->hw_free(substream);
 881		if (substream->managed_buffer_alloc)
 882			snd_pcm_lib_free_pages(substream);
 883	}
 884 unlock:
 885	snd_pcm_buffer_access_unlock(runtime);
 886	return err;
 887}
 888
 889static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
 890				  struct snd_pcm_hw_params __user * _params)
 891{
 892	int err;
 893	struct snd_pcm_hw_params *params __free(kfree) =
 894		memdup_user(_params, sizeof(*params));
 895
 896	if (IS_ERR(params))
 897		return PTR_ERR(params);
 898
 899	err = snd_pcm_hw_params(substream, params);
 900	if (err < 0)
 901		return err;
 902
 903	if (copy_to_user(_params, params, sizeof(*params)))
 904		return -EFAULT;
 905	return err;
 906}
 907
 908static int do_hw_free(struct snd_pcm_substream *substream)
 909{
 910	int result = 0;
 911
 912	snd_pcm_sync_stop(substream, true);
 913	if (substream->ops->hw_free)
 914		result = substream->ops->hw_free(substream);
 915	if (substream->managed_buffer_alloc)
 916		snd_pcm_lib_free_pages(substream);
 917	return result;
 918}
 919
 920static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 921{
 922	struct snd_pcm_runtime *runtime;
 923	int result = 0;
 924
 925	if (PCM_RUNTIME_CHECK(substream))
 926		return -ENXIO;
 927	runtime = substream->runtime;
 928	result = snd_pcm_buffer_access_lock(runtime);
 929	if (result < 0)
 930		return result;
 931	scoped_guard(pcm_stream_lock_irq, substream) {
 932		switch (runtime->state) {
 933		case SNDRV_PCM_STATE_SETUP:
 934		case SNDRV_PCM_STATE_PREPARED:
 935			if (atomic_read(&substream->mmap_count))
 936				result = -EBADFD;
 937			break;
 938		default:
 939			result = -EBADFD;
 940			break;
 941		}
 942	}
 943	if (result)
 944		goto unlock;
 945	result = do_hw_free(substream);
 946	snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 947	cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
 948 unlock:
 949	snd_pcm_buffer_access_unlock(runtime);
 950	return result;
 951}
 952
 953static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
 954			     struct snd_pcm_sw_params *params)
 955{
 956	struct snd_pcm_runtime *runtime;
 957	int err;
 958
 959	if (PCM_RUNTIME_CHECK(substream))
 960		return -ENXIO;
 961	runtime = substream->runtime;
 962	scoped_guard(pcm_stream_lock_irq, substream) {
 963		if (runtime->state == SNDRV_PCM_STATE_OPEN)
 964			return -EBADFD;
 965	}
 966
 967	if (params->tstamp_mode < 0 ||
 968	    params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
 969		return -EINVAL;
 970	if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
 971	    params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
 972		return -EINVAL;
 973	if (params->avail_min == 0)
 974		return -EINVAL;
 975	if (params->silence_size >= runtime->boundary) {
 976		if (params->silence_threshold != 0)
 977			return -EINVAL;
 978	} else {
 979		if (params->silence_size > params->silence_threshold)
 980			return -EINVAL;
 981		if (params->silence_threshold > runtime->buffer_size)
 982			return -EINVAL;
 983	}
 984	err = 0;
 985	scoped_guard(pcm_stream_lock_irq, substream) {
 986		runtime->tstamp_mode = params->tstamp_mode;
 987		if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
 988			runtime->tstamp_type = params->tstamp_type;
 989		runtime->period_step = params->period_step;
 990		runtime->control->avail_min = params->avail_min;
 991		runtime->start_threshold = params->start_threshold;
 992		runtime->stop_threshold = params->stop_threshold;
 993		runtime->silence_threshold = params->silence_threshold;
 994		runtime->silence_size = params->silence_size;
 995		params->boundary = runtime->boundary;
 996		if (snd_pcm_running(substream)) {
 997			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
 998			    runtime->silence_size > 0)
 999				snd_pcm_playback_silence(substream, ULONG_MAX);
1000			err = snd_pcm_update_state(substream, runtime);
1001		}
1002	}
1003	return err;
1004}
1005
1006static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
1007				  struct snd_pcm_sw_params __user * _params)
1008{
1009	struct snd_pcm_sw_params params;
1010	int err;
1011	if (copy_from_user(&params, _params, sizeof(params)))
1012		return -EFAULT;
1013	err = snd_pcm_sw_params(substream, &params);
1014	if (copy_to_user(_params, &params, sizeof(params)))
1015		return -EFAULT;
1016	return err;
1017}
1018
1019static inline snd_pcm_uframes_t
1020snd_pcm_calc_delay(struct snd_pcm_substream *substream)
1021{
1022	snd_pcm_uframes_t delay;
1023
1024	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1025		delay = snd_pcm_playback_hw_avail(substream->runtime);
1026	else
1027		delay = snd_pcm_capture_avail(substream->runtime);
1028	return delay + substream->runtime->delay;
1029}
1030
1031int snd_pcm_status64(struct snd_pcm_substream *substream,
1032		     struct snd_pcm_status64 *status)
1033{
1034	struct snd_pcm_runtime *runtime = substream->runtime;
1035
1036	guard(pcm_stream_lock_irq)(substream);
1037
1038	snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
1039					&runtime->audio_tstamp_config);
1040
1041	/* backwards compatible behavior */
1042	if (runtime->audio_tstamp_config.type_requested ==
1043		SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
1044		if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
1045			runtime->audio_tstamp_config.type_requested =
1046				SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
1047		else
1048			runtime->audio_tstamp_config.type_requested =
1049				SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
1050		runtime->audio_tstamp_report.valid = 0;
1051	} else
1052		runtime->audio_tstamp_report.valid = 1;
1053
1054	status->state = runtime->state;
1055	status->suspended_state = runtime->suspended_state;
1056	if (status->state == SNDRV_PCM_STATE_OPEN)
1057		return 0;
1058	status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
1059	status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
1060	if (snd_pcm_running(substream)) {
1061		snd_pcm_update_hw_ptr(substream);
1062		if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
1063			status->tstamp_sec = runtime->status->tstamp.tv_sec;
1064			status->tstamp_nsec =
1065				runtime->status->tstamp.tv_nsec;
1066			status->driver_tstamp_sec =
1067				runtime->driver_tstamp.tv_sec;
1068			status->driver_tstamp_nsec =
1069				runtime->driver_tstamp.tv_nsec;
1070			status->audio_tstamp_sec =
1071				runtime->status->audio_tstamp.tv_sec;
1072			status->audio_tstamp_nsec =
1073				runtime->status->audio_tstamp.tv_nsec;
1074			if (runtime->audio_tstamp_report.valid == 1)
1075				/* backwards compatibility, no report provided in COMPAT mode */
1076				snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
1077								&status->audio_tstamp_accuracy,
1078								&runtime->audio_tstamp_report);
1079
1080			goto _tstamp_end;
1081		}
1082	} else {
1083		/* get tstamp only in fallback mode and only if enabled */
1084		if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
1085			struct timespec64 tstamp;
1086
1087			snd_pcm_gettime(runtime, &tstamp);
1088			status->tstamp_sec = tstamp.tv_sec;
1089			status->tstamp_nsec = tstamp.tv_nsec;
1090		}
1091	}
1092 _tstamp_end:
1093	status->appl_ptr = runtime->control->appl_ptr;
1094	status->hw_ptr = runtime->status->hw_ptr;
1095	status->avail = snd_pcm_avail(substream);
1096	status->delay = snd_pcm_running(substream) ?
1097		snd_pcm_calc_delay(substream) : 0;
1098	status->avail_max = runtime->avail_max;
1099	status->overrange = runtime->overrange;
1100	runtime->avail_max = 0;
1101	runtime->overrange = 0;
1102	return 0;
1103}
1104
1105static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1106				 struct snd_pcm_status64 __user * _status,
1107				 bool ext)
1108{
1109	struct snd_pcm_status64 status;
1110	int res;
1111
1112	memset(&status, 0, sizeof(status));
1113	/*
1114	 * with extension, parameters are read/write,
1115	 * get audio_tstamp_data from user,
1116	 * ignore rest of status structure
1117	 */
1118	if (ext && get_user(status.audio_tstamp_data,
1119				(u32 __user *)(&_status->audio_tstamp_data)))
1120		return -EFAULT;
1121	res = snd_pcm_status64(substream, &status);
1122	if (res < 0)
1123		return res;
1124	if (copy_to_user(_status, &status, sizeof(status)))
1125		return -EFAULT;
1126	return 0;
1127}
1128
1129static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1130				 struct snd_pcm_status32 __user * _status,
1131				 bool ext)
1132{
1133	struct snd_pcm_status64 status64;
1134	struct snd_pcm_status32 status32;
1135	int res;
1136
1137	memset(&status64, 0, sizeof(status64));
1138	memset(&status32, 0, sizeof(status32));
1139	/*
1140	 * with extension, parameters are read/write,
1141	 * get audio_tstamp_data from user,
1142	 * ignore rest of status structure
1143	 */
1144	if (ext && get_user(status64.audio_tstamp_data,
1145			    (u32 __user *)(&_status->audio_tstamp_data)))
1146		return -EFAULT;
1147	res = snd_pcm_status64(substream, &status64);
1148	if (res < 0)
1149		return res;
1150
1151	status32 = (struct snd_pcm_status32) {
1152		.state = status64.state,
1153		.trigger_tstamp_sec = status64.trigger_tstamp_sec,
1154		.trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1155		.tstamp_sec = status64.tstamp_sec,
1156		.tstamp_nsec = status64.tstamp_nsec,
1157		.appl_ptr = status64.appl_ptr,
1158		.hw_ptr = status64.hw_ptr,
1159		.delay = status64.delay,
1160		.avail = status64.avail,
1161		.avail_max = status64.avail_max,
1162		.overrange = status64.overrange,
1163		.suspended_state = status64.suspended_state,
1164		.audio_tstamp_data = status64.audio_tstamp_data,
1165		.audio_tstamp_sec = status64.audio_tstamp_sec,
1166		.audio_tstamp_nsec = status64.audio_tstamp_nsec,
1167		.driver_tstamp_sec = status64.audio_tstamp_sec,
1168		.driver_tstamp_nsec = status64.audio_tstamp_nsec,
1169		.audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1170	};
1171
1172	if (copy_to_user(_status, &status32, sizeof(status32)))
1173		return -EFAULT;
1174
1175	return 0;
1176}
1177
1178static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1179				struct snd_pcm_channel_info * info)
1180{
1181	struct snd_pcm_runtime *runtime;
1182	unsigned int channel;
1183	
1184	channel = info->channel;
1185	runtime = substream->runtime;
1186	scoped_guard(pcm_stream_lock_irq, substream) {
1187		if (runtime->state == SNDRV_PCM_STATE_OPEN)
1188			return -EBADFD;
1189	}
1190	if (channel >= runtime->channels)
1191		return -EINVAL;
1192	memset(info, 0, sizeof(*info));
1193	info->channel = channel;
1194	return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1195}
1196
1197static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1198				     struct snd_pcm_channel_info __user * _info)
1199{
1200	struct snd_pcm_channel_info info;
1201	int res;
1202	
1203	if (copy_from_user(&info, _info, sizeof(info)))
1204		return -EFAULT;
1205	res = snd_pcm_channel_info(substream, &info);
1206	if (res < 0)
1207		return res;
1208	if (copy_to_user(_info, &info, sizeof(info)))
1209		return -EFAULT;
1210	return 0;
1211}
1212
1213static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1214{
1215	struct snd_pcm_runtime *runtime = substream->runtime;
1216	if (runtime->trigger_master == NULL)
1217		return;
1218	if (runtime->trigger_master == substream) {
1219		if (!runtime->trigger_tstamp_latched)
1220			snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1221	} else {
1222		snd_pcm_trigger_tstamp(runtime->trigger_master);
1223		runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1224	}
1225	runtime->trigger_master = NULL;
1226}
1227
1228#define ACTION_ARG_IGNORE	(__force snd_pcm_state_t)0
1229
1230struct action_ops {
1231	int (*pre_action)(struct snd_pcm_substream *substream,
1232			  snd_pcm_state_t state);
1233	int (*do_action)(struct snd_pcm_substream *substream,
1234			 snd_pcm_state_t state);
1235	void (*undo_action)(struct snd_pcm_substream *substream,
1236			    snd_pcm_state_t state);
1237	void (*post_action)(struct snd_pcm_substream *substream,
1238			    snd_pcm_state_t state);
1239};
1240
1241/*
1242 *  this functions is core for handling of linked stream
1243 *  Note: the stream state might be changed also on failure
1244 *  Note2: call with calling stream lock + link lock
1245 */
1246static int snd_pcm_action_group(const struct action_ops *ops,
1247				struct snd_pcm_substream *substream,
1248				snd_pcm_state_t state,
1249				bool stream_lock)
1250{
1251	struct snd_pcm_substream *s = NULL;
1252	struct snd_pcm_substream *s1;
1253	int res = 0, depth = 1;
1254
1255	snd_pcm_group_for_each_entry(s, substream) {
1256		if (s != substream) {
1257			if (!stream_lock)
1258				mutex_lock_nested(&s->runtime->buffer_mutex, depth);
1259			else if (s->pcm->nonatomic)
1260				mutex_lock_nested(&s->self_group.mutex, depth);
1261			else
1262				spin_lock_nested(&s->self_group.lock, depth);
1263			depth++;
1264		}
1265		res = ops->pre_action(s, state);
1266		if (res < 0)
1267			goto _unlock;
1268	}
1269	snd_pcm_group_for_each_entry(s, substream) {
1270		res = ops->do_action(s, state);
1271		if (res < 0) {
1272			if (ops->undo_action) {
1273				snd_pcm_group_for_each_entry(s1, substream) {
1274					if (s1 == s) /* failed stream */
1275						break;
1276					ops->undo_action(s1, state);
1277				}
1278			}
1279			s = NULL; /* unlock all */
1280			goto _unlock;
1281		}
1282	}
1283	snd_pcm_group_for_each_entry(s, substream) {
1284		ops->post_action(s, state);
1285	}
1286 _unlock:
1287	/* unlock streams */
1288	snd_pcm_group_for_each_entry(s1, substream) {
1289		if (s1 != substream) {
1290			if (!stream_lock)
1291				mutex_unlock(&s1->runtime->buffer_mutex);
1292			else if (s1->pcm->nonatomic)
1293				mutex_unlock(&s1->self_group.mutex);
1294			else
1295				spin_unlock(&s1->self_group.lock);
1296		}
1297		if (s1 == s)	/* end */
1298			break;
1299	}
1300	return res;
1301}
1302
1303/*
1304 *  Note: call with stream lock
1305 */
1306static int snd_pcm_action_single(const struct action_ops *ops,
1307				 struct snd_pcm_substream *substream,
1308				 snd_pcm_state_t state)
1309{
1310	int res;
1311	
1312	res = ops->pre_action(substream, state);
1313	if (res < 0)
1314		return res;
1315	res = ops->do_action(substream, state);
1316	if (res == 0)
1317		ops->post_action(substream, state);
1318	else if (ops->undo_action)
1319		ops->undo_action(substream, state);
1320	return res;
1321}
1322
1323static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1324				 struct snd_pcm_group *new_group)
1325{
1326	substream->group = new_group;
1327	list_move(&substream->link_list, &new_group->substreams);
1328}
1329
1330/*
1331 * Unref and unlock the group, but keep the stream lock;
1332 * when the group becomes empty and no longer referred, destroy itself
1333 */
1334static void snd_pcm_group_unref(struct snd_pcm_group *group,
1335				struct snd_pcm_substream *substream)
1336{
1337	bool do_free;
1338
1339	if (!group)
1340		return;
1341	do_free = refcount_dec_and_test(&group->refs);
1342	snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1343	if (do_free)
1344		kfree(group);
1345}
1346
1347/*
1348 * Lock the group inside a stream lock and reference it;
1349 * return the locked group object, or NULL if not linked
1350 */
1351static struct snd_pcm_group *
1352snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1353{
1354	bool nonatomic = substream->pcm->nonatomic;
1355	struct snd_pcm_group *group;
1356	bool trylock;
1357
1358	for (;;) {
1359		if (!snd_pcm_stream_linked(substream))
1360			return NULL;
1361		group = substream->group;
1362		/* block freeing the group object */
1363		refcount_inc(&group->refs);
1364
1365		trylock = nonatomic ? mutex_trylock(&group->mutex) :
1366			spin_trylock(&group->lock);
1367		if (trylock)
1368			break; /* OK */
1369
1370		/* re-lock for avoiding ABBA deadlock */
1371		snd_pcm_stream_unlock(substream);
1372		snd_pcm_group_lock(group, nonatomic);
1373		snd_pcm_stream_lock(substream);
1374
1375		/* check the group again; the above opens a small race window */
1376		if (substream->group == group)
1377			break; /* OK */
1378		/* group changed, try again */
1379		snd_pcm_group_unref(group, substream);
1380	}
1381	return group;
1382}
1383
1384/*
1385 *  Note: call with stream lock
1386 */
1387static int snd_pcm_action(const struct action_ops *ops,
1388			  struct snd_pcm_substream *substream,
1389			  snd_pcm_state_t state)
1390{
1391	struct snd_pcm_group *group;
1392	int res;
1393
1394	group = snd_pcm_stream_group_ref(substream);
1395	if (group)
1396		res = snd_pcm_action_group(ops, substream, state, true);
1397	else
1398		res = snd_pcm_action_single(ops, substream, state);
1399	snd_pcm_group_unref(group, substream);
1400	return res;
1401}
1402
1403/*
1404 *  Note: don't use any locks before
1405 */
1406static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1407				   struct snd_pcm_substream *substream,
1408				   snd_pcm_state_t state)
1409{
1410	guard(pcm_stream_lock_irq)(substream);
1411	return snd_pcm_action(ops, substream, state);
1412}
1413
1414/*
1415 */
1416static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1417				    struct snd_pcm_substream *substream,
1418				    snd_pcm_state_t state)
1419{
1420	int res;
1421
1422	/* Guarantee the group members won't change during non-atomic action */
1423	guard(rwsem_read)(&snd_pcm_link_rwsem);
1424	res = snd_pcm_buffer_access_lock(substream->runtime);
1425	if (res < 0)
1426		return res;
1427	if (snd_pcm_stream_linked(substream))
1428		res = snd_pcm_action_group(ops, substream, state, false);
1429	else
1430		res = snd_pcm_action_single(ops, substream, state);
1431	snd_pcm_buffer_access_unlock(substream->runtime);
1432	return res;
1433}
1434
1435/*
1436 * start callbacks
1437 */
1438static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1439			     snd_pcm_state_t state)
1440{
1441	struct snd_pcm_runtime *runtime = substream->runtime;
1442	if (runtime->state != SNDRV_PCM_STATE_PREPARED)
1443		return -EBADFD;
1444	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1445	    !snd_pcm_playback_data(substream))
1446		return -EPIPE;
1447	runtime->trigger_tstamp_latched = false;
1448	runtime->trigger_master = substream;
1449	return 0;
1450}
1451
1452static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1453			    snd_pcm_state_t state)
1454{
1455	int err;
1456
1457	if (substream->runtime->trigger_master != substream)
1458		return 0;
1459	err = substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1460	/* XRUN happened during the start */
1461	if (err == -EPIPE)
1462		__snd_pcm_set_state(substream->runtime, SNDRV_PCM_STATE_XRUN);
1463	return err;
1464}
1465
1466static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1467			       snd_pcm_state_t state)
1468{
1469	if (substream->runtime->trigger_master == substream) {
1470		substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1471		substream->runtime->stop_operating = true;
1472	}
1473}
1474
1475static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1476			       snd_pcm_state_t state)
1477{
1478	struct snd_pcm_runtime *runtime = substream->runtime;
1479	snd_pcm_trigger_tstamp(substream);
1480	runtime->hw_ptr_jiffies = jiffies;
1481	runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / 
1482							    runtime->rate;
1483	__snd_pcm_set_state(runtime, state);
1484	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1485	    runtime->silence_size > 0)
1486		snd_pcm_playback_silence(substream, ULONG_MAX);
1487	snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1488}
1489
1490static const struct action_ops snd_pcm_action_start = {
1491	.pre_action = snd_pcm_pre_start,
1492	.do_action = snd_pcm_do_start,
1493	.undo_action = snd_pcm_undo_start,
1494	.post_action = snd_pcm_post_start
1495};
1496
1497/**
1498 * snd_pcm_start - start all linked streams
1499 * @substream: the PCM substream instance
1500 *
1501 * Return: Zero if successful, or a negative error code.
1502 * The stream lock must be acquired before calling this function.
1503 */
1504int snd_pcm_start(struct snd_pcm_substream *substream)
1505{
1506	return snd_pcm_action(&snd_pcm_action_start, substream,
1507			      SNDRV_PCM_STATE_RUNNING);
1508}
1509
1510/* take the stream lock and start the streams */
1511static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1512{
1513	return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1514				       SNDRV_PCM_STATE_RUNNING);
1515}
1516
1517/*
1518 * stop callbacks
1519 */
1520static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1521			    snd_pcm_state_t state)
1522{
1523	struct snd_pcm_runtime *runtime = substream->runtime;
1524	if (runtime->state == SNDRV_PCM_STATE_OPEN)
1525		return -EBADFD;
1526	runtime->trigger_master = substream;
1527	return 0;
1528}
1529
1530static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1531			   snd_pcm_state_t state)
1532{
1533	if (substream->runtime->trigger_master == substream &&
1534	    snd_pcm_running(substream)) {
1535		substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1536		substream->runtime->stop_operating = true;
1537	}
1538	return 0; /* unconditionally stop all substreams */
1539}
1540
1541static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1542			      snd_pcm_state_t state)
1543{
1544	struct snd_pcm_runtime *runtime = substream->runtime;
1545	if (runtime->state != state) {
1546		snd_pcm_trigger_tstamp(substream);
1547		__snd_pcm_set_state(runtime, state);
1548		snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1549	}
1550	wake_up(&runtime->sleep);
1551	wake_up(&runtime->tsleep);
1552}
1553
1554static const struct action_ops snd_pcm_action_stop = {
1555	.pre_action = snd_pcm_pre_stop,
1556	.do_action = snd_pcm_do_stop,
1557	.post_action = snd_pcm_post_stop
1558};
1559
1560/**
1561 * snd_pcm_stop - try to stop all running streams in the substream group
1562 * @substream: the PCM substream instance
1563 * @state: PCM state after stopping the stream
1564 *
1565 * The state of each stream is then changed to the given state unconditionally.
1566 *
1567 * Return: Zero if successful, or a negative error code.
1568 */
1569int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1570{
1571	return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1572}
1573EXPORT_SYMBOL(snd_pcm_stop);
1574
1575/**
1576 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1577 * @substream: the PCM substream
1578 *
1579 * After stopping, the state is changed to SETUP.
1580 * Unlike snd_pcm_stop(), this affects only the given stream.
1581 *
1582 * Return: Zero if successful, or a negative error code.
1583 */
1584int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1585{
1586	return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1587				     SNDRV_PCM_STATE_SETUP);
1588}
1589
1590/**
1591 * snd_pcm_stop_xrun - stop the running streams as XRUN
1592 * @substream: the PCM substream instance
1593 *
1594 * This stops the given running substream (and all linked substreams) as XRUN.
1595 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1596 *
1597 * Return: Zero if successful, or a negative error code.
1598 */
1599int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1600{
1601	guard(pcm_stream_lock_irqsave)(substream);
1602	if (substream->runtime && snd_pcm_running(substream))
1603		__snd_pcm_xrun(substream);
1604	return 0;
1605}
1606EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1607
1608/*
1609 * pause callbacks: pass boolean (to start pause or resume) as state argument
1610 */
1611#define pause_pushed(state)	(__force bool)(state)
1612
1613static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1614			     snd_pcm_state_t state)
1615{
1616	struct snd_pcm_runtime *runtime = substream->runtime;
1617	if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1618		return -ENOSYS;
1619	if (pause_pushed(state)) {
1620		if (runtime->state != SNDRV_PCM_STATE_RUNNING)
1621			return -EBADFD;
1622	} else if (runtime->state != SNDRV_PCM_STATE_PAUSED)
1623		return -EBADFD;
1624	runtime->trigger_master = substream;
1625	return 0;
1626}
1627
1628static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1629			    snd_pcm_state_t state)
1630{
1631	if (substream->runtime->trigger_master != substream)
1632		return 0;
1633	/* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1634	 * a delta between the current jiffies, this gives a large enough
1635	 * delta, effectively to skip the check once.
1636	 */
1637	substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1638	return substream->ops->trigger(substream,
1639				       pause_pushed(state) ?
1640				       SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1641				       SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1642}
1643
1644static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1645			       snd_pcm_state_t state)
1646{
1647	if (substream->runtime->trigger_master == substream)
1648		substream->ops->trigger(substream,
1649					pause_pushed(state) ?
1650					SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1651					SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1652}
1653
1654static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1655			       snd_pcm_state_t state)
1656{
1657	struct snd_pcm_runtime *runtime = substream->runtime;
1658	snd_pcm_trigger_tstamp(substream);
1659	if (pause_pushed(state)) {
1660		__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_PAUSED);
1661		snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1662		wake_up(&runtime->sleep);
1663		wake_up(&runtime->tsleep);
1664	} else {
1665		__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_RUNNING);
1666		snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1667	}
1668}
1669
1670static const struct action_ops snd_pcm_action_pause = {
1671	.pre_action = snd_pcm_pre_pause,
1672	.do_action = snd_pcm_do_pause,
1673	.undo_action = snd_pcm_undo_pause,
1674	.post_action = snd_pcm_post_pause
1675};
1676
1677/*
1678 * Push/release the pause for all linked streams.
1679 */
1680static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1681{
1682	return snd_pcm_action(&snd_pcm_action_pause, substream,
1683			      (__force snd_pcm_state_t)push);
1684}
1685
1686static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1687				  bool push)
1688{
1689	return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1690				       (__force snd_pcm_state_t)push);
1691}
1692
1693#ifdef CONFIG_PM
1694/* suspend callback: state argument ignored */
1695
1696static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1697			       snd_pcm_state_t state)
1698{
1699	struct snd_pcm_runtime *runtime = substream->runtime;
1700	switch (runtime->state) {
1701	case SNDRV_PCM_STATE_SUSPENDED:
1702		return -EBUSY;
1703	/* unresumable PCM state; return -EBUSY for skipping suspend */
1704	case SNDRV_PCM_STATE_OPEN:
1705	case SNDRV_PCM_STATE_SETUP:
1706	case SNDRV_PCM_STATE_DISCONNECTED:
1707		return -EBUSY;
1708	}
1709	runtime->trigger_master = substream;
1710	return 0;
1711}
1712
1713static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1714			      snd_pcm_state_t state)
1715{
1716	struct snd_pcm_runtime *runtime = substream->runtime;
1717	if (runtime->trigger_master != substream)
1718		return 0;
1719	if (! snd_pcm_running(substream))
1720		return 0;
1721	substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1722	runtime->stop_operating = true;
1723	return 0; /* suspend unconditionally */
1724}
1725
1726static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1727				 snd_pcm_state_t state)
1728{
1729	struct snd_pcm_runtime *runtime = substream->runtime;
1730	snd_pcm_trigger_tstamp(substream);
1731	runtime->suspended_state = runtime->state;
1732	runtime->status->suspended_state = runtime->suspended_state;
1733	__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SUSPENDED);
1734	snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1735	wake_up(&runtime->sleep);
1736	wake_up(&runtime->tsleep);
1737}
1738
1739static const struct action_ops snd_pcm_action_suspend = {
1740	.pre_action = snd_pcm_pre_suspend,
1741	.do_action = snd_pcm_do_suspend,
1742	.post_action = snd_pcm_post_suspend
1743};
1744
1745/*
1746 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1747 * @substream: the PCM substream
1748 *
1749 * After this call, all streams are changed to SUSPENDED state.
1750 *
1751 * Return: Zero if successful, or a negative error code.
1752 */
1753static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1754{
1755	guard(pcm_stream_lock_irqsave)(substream);
1756	return snd_pcm_action(&snd_pcm_action_suspend, substream,
1757			      ACTION_ARG_IGNORE);
1758}
1759
1760/**
1761 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1762 * @pcm: the PCM instance
1763 *
1764 * After this call, all streams are changed to SUSPENDED state.
1765 *
1766 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1767 */
1768int snd_pcm_suspend_all(struct snd_pcm *pcm)
1769{
1770	struct snd_pcm_substream *substream;
1771	int stream, err = 0;
1772
1773	if (! pcm)
1774		return 0;
1775
1776	for_each_pcm_substream(pcm, stream, substream) {
1777		/* FIXME: the open/close code should lock this as well */
1778		if (!substream->runtime)
1779			continue;
1780
1781		/*
1782		 * Skip BE dai link PCM's that are internal and may
1783		 * not have their substream ops set.
1784		 */
1785		if (!substream->ops)
1786			continue;
1787
1788		err = snd_pcm_suspend(substream);
1789		if (err < 0 && err != -EBUSY)
1790			return err;
1791	}
1792
1793	for_each_pcm_substream(pcm, stream, substream)
1794		snd_pcm_sync_stop(substream, false);
1795
1796	return 0;
1797}
1798EXPORT_SYMBOL(snd_pcm_suspend_all);
1799
1800/* resume callbacks: state argument ignored */
1801
1802static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1803			      snd_pcm_state_t state)
1804{
1805	struct snd_pcm_runtime *runtime = substream->runtime;
1806	if (runtime->state != SNDRV_PCM_STATE_SUSPENDED)
1807		return -EBADFD;
1808	if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1809		return -ENOSYS;
1810	runtime->trigger_master = substream;
1811	return 0;
1812}
1813
1814static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1815			     snd_pcm_state_t state)
1816{
1817	struct snd_pcm_runtime *runtime = substream->runtime;
1818	if (runtime->trigger_master != substream)
1819		return 0;
1820	/* DMA not running previously? */
1821	if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1822	    (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1823	     substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1824		return 0;
1825	return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1826}
1827
1828static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1829				snd_pcm_state_t state)
1830{
1831	if (substream->runtime->trigger_master == substream &&
1832	    snd_pcm_running(substream))
1833		substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1834}
1835
1836static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1837				snd_pcm_state_t state)
1838{
1839	struct snd_pcm_runtime *runtime = substream->runtime;
1840	snd_pcm_trigger_tstamp(substream);
1841	__snd_pcm_set_state(runtime, runtime->suspended_state);
1842	snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1843}
1844
1845static const struct action_ops snd_pcm_action_resume = {
1846	.pre_action = snd_pcm_pre_resume,
1847	.do_action = snd_pcm_do_resume,
1848	.undo_action = snd_pcm_undo_resume,
1849	.post_action = snd_pcm_post_resume
1850};
1851
1852static int snd_pcm_resume(struct snd_pcm_substream *substream)
1853{
1854	return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1855				       ACTION_ARG_IGNORE);
1856}
1857
1858#else
1859
1860static int snd_pcm_resume(struct snd_pcm_substream *substream)
1861{
1862	return -ENOSYS;
1863}
1864
1865#endif /* CONFIG_PM */
1866
1867/*
1868 * xrun ioctl
1869 *
1870 * Change the RUNNING stream(s) to XRUN state.
1871 */
1872static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1873{
1874	struct snd_pcm_runtime *runtime = substream->runtime;
1875
1876	guard(pcm_stream_lock_irq)(substream);
1877	switch (runtime->state) {
1878	case SNDRV_PCM_STATE_XRUN:
1879		return 0;	/* already there */
1880	case SNDRV_PCM_STATE_RUNNING:
1881		__snd_pcm_xrun(substream);
1882		return 0;
1883	default:
1884		return -EBADFD;
1885	}
1886}
1887
1888/*
1889 * reset ioctl
1890 */
1891/* reset callbacks:  state argument ignored */
1892static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1893			     snd_pcm_state_t state)
1894{
1895	struct snd_pcm_runtime *runtime = substream->runtime;
1896	switch (runtime->state) {
1897	case SNDRV_PCM_STATE_RUNNING:
1898	case SNDRV_PCM_STATE_PREPARED:
1899	case SNDRV_PCM_STATE_PAUSED:
1900	case SNDRV_PCM_STATE_SUSPENDED:
1901		return 0;
1902	default:
1903		return -EBADFD;
1904	}
1905}
1906
1907static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1908			    snd_pcm_state_t state)
1909{
1910	struct snd_pcm_runtime *runtime = substream->runtime;
1911	int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1912	if (err < 0)
1913		return err;
1914	guard(pcm_stream_lock_irq)(substream);
1915	runtime->hw_ptr_base = 0;
1916	runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1917		runtime->status->hw_ptr % runtime->period_size;
1918	runtime->silence_start = runtime->status->hw_ptr;
1919	runtime->silence_filled = 0;
1920	return 0;
1921}
1922
1923static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1924			       snd_pcm_state_t state)
1925{
1926	struct snd_pcm_runtime *runtime = substream->runtime;
1927	guard(pcm_stream_lock_irq)(substream);
1928	runtime->control->appl_ptr = runtime->status->hw_ptr;
1929	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1930	    runtime->silence_size > 0)
1931		snd_pcm_playback_silence(substream, ULONG_MAX);
1932}
1933
1934static const struct action_ops snd_pcm_action_reset = {
1935	.pre_action = snd_pcm_pre_reset,
1936	.do_action = snd_pcm_do_reset,
1937	.post_action = snd_pcm_post_reset
1938};
1939
1940static int snd_pcm_reset(struct snd_pcm_substream *substream)
1941{
1942	return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1943					ACTION_ARG_IGNORE);
1944}
1945
1946/*
1947 * prepare ioctl
1948 */
1949/* pass f_flags as state argument */
1950static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1951			       snd_pcm_state_t state)
1952{
1953	struct snd_pcm_runtime *runtime = substream->runtime;
1954	int f_flags = (__force int)state;
1955
1956	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
1957	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
1958		return -EBADFD;
1959	if (snd_pcm_running(substream))
1960		return -EBUSY;
1961	substream->f_flags = f_flags;
1962	return 0;
1963}
1964
1965static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1966			      snd_pcm_state_t state)
1967{
1968	int err;
1969	snd_pcm_sync_stop(substream, true);
1970	err = substream->ops->prepare(substream);
1971	if (err < 0)
1972		return err;
1973	return snd_pcm_do_reset(substream, state);
1974}
1975
1976static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1977				 snd_pcm_state_t state)
1978{
1979	struct snd_pcm_runtime *runtime = substream->runtime;
1980	runtime->control->appl_ptr = runtime->status->hw_ptr;
1981	snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1982}
1983
1984static const struct action_ops snd_pcm_action_prepare = {
1985	.pre_action = snd_pcm_pre_prepare,
1986	.do_action = snd_pcm_do_prepare,
1987	.post_action = snd_pcm_post_prepare
1988};
1989
1990/**
1991 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1992 * @substream: the PCM substream instance
1993 * @file: file to refer f_flags
1994 *
1995 * Return: Zero if successful, or a negative error code.
1996 */
1997static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1998			   struct file *file)
1999{
2000	int f_flags;
2001
2002	if (file)
2003		f_flags = file->f_flags;
2004	else
2005		f_flags = substream->f_flags;
2006
2007	scoped_guard(pcm_stream_lock_irq, substream) {
2008		switch (substream->runtime->state) {
2009		case SNDRV_PCM_STATE_PAUSED:
2010			snd_pcm_pause(substream, false);
2011			fallthrough;
2012		case SNDRV_PCM_STATE_SUSPENDED:
2013			snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2014			break;
2015		}
2016	}
2017
2018	return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
2019					substream,
2020					(__force snd_pcm_state_t)f_flags);
2021}
2022
2023/*
2024 * drain ioctl
2025 */
2026
2027/* drain init callbacks: state argument ignored */
2028static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
2029				  snd_pcm_state_t state)
2030{
2031	struct snd_pcm_runtime *runtime = substream->runtime;
2032	switch (runtime->state) {
2033	case SNDRV_PCM_STATE_OPEN:
2034	case SNDRV_PCM_STATE_DISCONNECTED:
2035	case SNDRV_PCM_STATE_SUSPENDED:
2036		return -EBADFD;
2037	}
2038	runtime->trigger_master = substream;
2039	return 0;
2040}
2041
2042static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
2043				 snd_pcm_state_t state)
2044{
2045	struct snd_pcm_runtime *runtime = substream->runtime;
2046	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2047		switch (runtime->state) {
2048		case SNDRV_PCM_STATE_PREPARED:
2049			/* start playback stream if possible */
2050			if (! snd_pcm_playback_empty(substream)) {
2051				snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
2052				snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
2053			} else {
2054				__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP);
2055			}
2056			break;
2057		case SNDRV_PCM_STATE_RUNNING:
2058			__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_DRAINING);
2059			break;
2060		case SNDRV_PCM_STATE_XRUN:
2061			__snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP);
2062			break;
2063		default:
2064			break;
2065		}
2066	} else {
2067		/* stop running stream */
2068		if (runtime->state == SNDRV_PCM_STATE_RUNNING) {
2069			snd_pcm_state_t new_state;
2070
2071			new_state = snd_pcm_capture_avail(runtime) > 0 ?
2072				SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
2073			snd_pcm_do_stop(substream, new_state);
2074			snd_pcm_post_stop(substream, new_state);
2075		}
2076	}
2077
2078	if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
2079	    runtime->trigger_master == substream &&
2080	    (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
2081		return substream->ops->trigger(substream,
2082					       SNDRV_PCM_TRIGGER_DRAIN);
2083
2084	return 0;
2085}
2086
2087static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
2088				    snd_pcm_state_t state)
2089{
2090}
2091
2092static const struct action_ops snd_pcm_action_drain_init = {
2093	.pre_action = snd_pcm_pre_drain_init,
2094	.do_action = snd_pcm_do_drain_init,
2095	.post_action = snd_pcm_post_drain_init
2096};
2097
2098/*
2099 * Drain the stream(s).
2100 * When the substream is linked, sync until the draining of all playback streams
2101 * is finished.
2102 * After this call, all streams are supposed to be either SETUP or DRAINING
2103 * (capture only) state.
2104 */
2105static int snd_pcm_drain(struct snd_pcm_substream *substream,
2106			 struct file *file)
2107{
2108	struct snd_card *card;
2109	struct snd_pcm_runtime *runtime;
2110	struct snd_pcm_substream *s;
2111	struct snd_pcm_group *group;
2112	wait_queue_entry_t wait;
2113	int result = 0;
2114	int nonblock = 0;
2115
2116	card = substream->pcm->card;
2117	runtime = substream->runtime;
2118
2119	if (runtime->state == SNDRV_PCM_STATE_OPEN)
2120		return -EBADFD;
2121
2122	if (file) {
2123		if (file->f_flags & O_NONBLOCK)
2124			nonblock = 1;
2125	} else if (substream->f_flags & O_NONBLOCK)
2126		nonblock = 1;
2127
2128	snd_pcm_stream_lock_irq(substream);
2129	/* resume pause */
2130	if (runtime->state == SNDRV_PCM_STATE_PAUSED)
2131		snd_pcm_pause(substream, false);
2132
2133	/* pre-start/stop - all running streams are changed to DRAINING state */
2134	result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2135				ACTION_ARG_IGNORE);
2136	if (result < 0)
2137		goto unlock;
2138	/* in non-blocking, we don't wait in ioctl but let caller poll */
2139	if (nonblock) {
2140		result = -EAGAIN;
2141		goto unlock;
2142	}
2143
2144	for (;;) {
2145		long tout;
2146		struct snd_pcm_runtime *to_check;
2147		unsigned int drain_rate;
2148		snd_pcm_uframes_t drain_bufsz;
2149		bool drain_no_period_wakeup;
2150
2151		if (signal_pending(current)) {
2152			result = -ERESTARTSYS;
2153			break;
2154		}
2155		/* find a substream to drain */
2156		to_check = NULL;
2157		group = snd_pcm_stream_group_ref(substream);
2158		snd_pcm_group_for_each_entry(s, substream) {
2159			if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2160				continue;
2161			runtime = s->runtime;
2162			if (runtime->state == SNDRV_PCM_STATE_DRAINING) {
2163				to_check = runtime;
2164				break;
2165			}
2166		}
2167		snd_pcm_group_unref(group, substream);
2168		if (!to_check)
2169			break; /* all drained */
2170		/*
2171		 * Cache the runtime fields needed after unlock.
2172		 * A concurrent close() on the linked stream may free
2173		 * its runtime via snd_pcm_detach_substream() once we
2174		 * release the stream lock below.
2175		 */
2176		drain_no_period_wakeup = to_check->no_period_wakeup;
2177		drain_rate = to_check->rate;
2178		drain_bufsz = to_check->buffer_size;
2179		init_waitqueue_entry(&wait, current);
2180		set_current_state(TASK_INTERRUPTIBLE);
2181		add_wait_queue(&to_check->sleep, &wait);
2182		snd_pcm_stream_unlock_irq(substream);
2183		if (drain_no_period_wakeup)
2184			tout = MAX_SCHEDULE_TIMEOUT;
2185		else {
2186			tout = 100;
2187			if (drain_rate) {
2188				long t = drain_bufsz * 1100 / drain_rate;
2189				tout = max(t, tout);
2190			}
2191			tout = msecs_to_jiffies(tout);
2192		}
2193		tout = schedule_timeout(tout);
2194
2195		snd_pcm_stream_lock_irq(substream);
2196		group = snd_pcm_stream_group_ref(substream);
2197		snd_pcm_group_for_each_entry(s, substream) {
2198			if (s->runtime == to_check) {
2199				remove_wait_queue(&to_check->sleep, &wait);
2200				break;
2201			}
2202		}
2203		snd_pcm_group_unref(group, substream);
2204
2205		if (card->shutdown) {
2206			result = -ENODEV;
2207			break;
2208		}
2209		if (tout == 0) {
2210			if (substream->runtime->state == SNDRV_PCM_STATE_SUSPENDED)
2211				result = -ESTRPIPE;
2212			else {
2213				dev_dbg(substream->pcm->card->dev,
2214					"playback drain timeout (DMA or IRQ trouble?)\n");
2215				snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2216				result = -EIO;
2217			}
2218			break;
2219		}
2220	}
2221
2222 unlock:
2223	snd_pcm_stream_unlock_irq(substream);
2224
2225	return result;
2226}
2227
2228/*
2229 * drop ioctl
2230 *
2231 * Immediately put all linked substreams into SETUP state.
2232 */
2233static int snd_pcm_drop(struct snd_pcm_substream *substream)
2234{
2235	struct snd_pcm_runtime *runtime;
2236	int result = 0;
2237	
2238	if (PCM_RUNTIME_CHECK(substream))
2239		return -ENXIO;
2240	runtime = substream->runtime;
2241
2242	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
2243	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
2244		return -EBADFD;
2245
2246	guard(pcm_stream_lock_irq)(substream);
2247	/* resume pause */
2248	if (runtime->state == SNDRV_PCM_STATE_PAUSED)
2249		snd_pcm_pause(substream, false);
2250
2251	snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2252	/* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2253
2254	return result;
2255}
2256
2257
2258static bool is_pcm_file(struct file *file)
2259{
2260	struct inode *inode = file_inode(file);
2261	struct snd_pcm *pcm;
2262	unsigned int minor;
2263
2264	if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2265		return false;
2266	minor = iminor(inode);
2267	pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2268	if (!pcm)
2269		pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2270	if (!pcm)
2271		return false;
2272	snd_card_unref(pcm->card);
2273	return true;
2274}
2275
2276/*
2277 * PCM link handling
2278 */
2279static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2280{
2281	struct snd_pcm_file *pcm_file;
2282	struct snd_pcm_substream *substream1;
2283	struct snd_pcm_group *target_group;
2284	bool nonatomic = substream->pcm->nonatomic;
2285	CLASS(fd, f)(fd);
2286
2287	if (fd_empty(f))
2288		return -EBADFD;
2289	if (!is_pcm_file(fd_file(f)))
2290		return -EBADFD;
2291
2292	pcm_file = fd_file(f)->private_data;
2293	substream1 = pcm_file->substream;
2294
2295	if (substream == substream1)
2296		return -EINVAL;
2297
2298	struct snd_pcm_group *group __free(kfree) =
2299		kzalloc(sizeof(*group), GFP_KERNEL);
2300	if (!group)
2301		return -ENOMEM;
2302	snd_pcm_group_init(group);
2303
2304	guard(rwsem_write)(&snd_pcm_link_rwsem);
2305	if (substream->runtime->state == SNDRV_PCM_STATE_OPEN ||
2306	    substream->runtime->state != substream1->runtime->state ||
2307	    substream->pcm->nonatomic != substream1->pcm->nonatomic)
2308		return -EBADFD;
2309	if (snd_pcm_stream_linked(substream1))
2310		return -EALREADY;
2311
2312	scoped_guard(pcm_stream_lock_irq, substream) {
2313		if (!snd_pcm_stream_linked(substream)) {
2314			snd_pcm_group_assign(substream, group);
2315			group = NULL; /* assigned, don't free this one below */
2316		}
2317		target_group = substream->group;
2318	}
2319
2320	snd_pcm_group_lock_irq(target_group, nonatomic);
2321	snd_pcm_stream_lock_nested(substream1);
2322	snd_pcm_group_assign(substream1, target_group);
2323	refcount_inc(&target_group->refs);
2324	snd_pcm_stream_unlock(substream1);
2325	snd_pcm_group_unlock_irq(target_group, nonatomic);
2326	return 0;
2327}
2328
2329static void relink_to_local(struct snd_pcm_substream *substream)
2330{
2331	snd_pcm_stream_lock_nested(substream);
2332	snd_pcm_group_assign(substream, &substream->self_group);
2333	snd_pcm_stream_unlock(substream);
2334}
2335
2336static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2337{
2338	struct snd_pcm_group *group;
2339	bool nonatomic = substream->pcm->nonatomic;
2340	bool do_free = false;
2341
2342	guard(rwsem_write)(&snd_pcm_link_rwsem);
2343
2344	if (!snd_pcm_stream_linked(substream))
2345		return -EALREADY;
2346
2347	group = substream->group;
2348	snd_pcm_group_lock_irq(group, nonatomic);
2349
2350	relink_to_local(substream);
2351	refcount_dec(&group->refs);
2352
2353	/* detach the last stream, too */
2354	if (list_is_singular(&group->substreams)) {
2355		relink_to_local(list_first_entry(&group->substreams,
2356						 struct snd_pcm_substream,
2357						 link_list));
2358		do_free = refcount_dec_and_test(&group->refs);
2359	}
2360
2361	snd_pcm_group_unlock_irq(group, nonatomic);
2362	if (do_free)
2363		kfree(group);
2364	return 0;
2365}
2366
2367/*
2368 * hw configurator
2369 */
2370static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2371			       struct snd_pcm_hw_rule *rule)
2372{
2373	struct snd_interval t;
2374	snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2375		     hw_param_interval_c(params, rule->deps[1]), &t);
2376	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2377}
2378
2379static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2380			       struct snd_pcm_hw_rule *rule)
2381{
2382	struct snd_interval t;
2383	snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2384		     hw_param_interval_c(params, rule->deps[1]), &t);
2385	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2386}
2387
2388static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2389				   struct snd_pcm_hw_rule *rule)
2390{
2391	struct snd_interval t;
2392	snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2393			 hw_param_interval_c(params, rule->deps[1]),
2394			 (unsigned long) rule->private, &t);
2395	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2396}
2397
2398static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2399				   struct snd_pcm_hw_rule *rule)
2400{
2401	struct snd_interval t;
2402	snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2403			 (unsigned long) rule->private,
2404			 hw_param_interval_c(params, rule->deps[1]), &t);
2405	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2406}
2407
2408static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2409				  struct snd_pcm_hw_rule *rule)
2410{
2411	snd_pcm_format_t k;
2412	const struct snd_interval *i =
2413				hw_param_interval_c(params, rule->deps[0]);
2414	struct snd_mask m;
2415	struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2416	snd_mask_any(&m);
2417	pcm_for_each_format(k) {
2418		int bits;
2419		if (!snd_mask_test_format(mask, k))
2420			continue;
2421		bits = snd_pcm_format_physical_width(k);
2422		if (bits <= 0)
2423			continue; /* ignore invalid formats */
2424		if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2425			snd_mask_reset(&m, (__force unsigned)k);
2426	}
2427	return snd_mask_refine(mask, &m);
2428}
2429
2430static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2431				       struct snd_pcm_hw_rule *rule)
2432{
2433	struct snd_interval t;
2434	snd_pcm_format_t k;
2435
2436	t.min = UINT_MAX;
2437	t.max = 0;
2438	t.openmin = 0;
2439	t.openmax = 0;
2440	pcm_for_each_format(k) {
2441		int bits;
2442		if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2443			continue;
2444		bits = snd_pcm_format_physical_width(k);
2445		if (bits <= 0)
2446			continue; /* ignore invalid formats */
2447		if (t.min > (unsigned)bits)
2448			t.min = bits;
2449		if (t.max < (unsigned)bits)
2450			t.max = bits;
2451	}
2452	t.integer = 1;
2453	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2454}
2455
2456#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 ||\
2457	SNDRV_PCM_RATE_128000 != 1 << 19
2458#error "Change this table"
2459#endif
2460
2461/* NOTE: the list is unsorted! */
2462static const unsigned int rates[] = {
2463	5512, 8000, 11025, 16000, 22050, 32000, 44100,
2464	48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000, 705600, 768000,
2465	/* extended */
2466	12000, 24000, 128000
2467};
2468
2469const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2470	.count = ARRAY_SIZE(rates),
2471	.list = rates,
2472};
2473
2474static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2475				struct snd_pcm_hw_rule *rule)
2476{
2477	struct snd_pcm_hardware *hw = rule->private;
2478	return snd_interval_list(hw_param_interval(params, rule->var),
2479				 snd_pcm_known_rates.count,
2480				 snd_pcm_known_rates.list, hw->rates);
2481}		
2482
2483static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2484					    struct snd_pcm_hw_rule *rule)
2485{
2486	struct snd_interval t;
2487	struct snd_pcm_substream *substream = rule->private;
2488	t.min = 0;
2489	t.max = substream->buffer_bytes_max;
2490	t.openmin = 0;
2491	t.openmax = 0;
2492	t.integer = 1;
2493	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2494}		
2495
2496static int snd_pcm_hw_rule_subformats(struct snd_pcm_hw_params *params,
2497				      struct snd_pcm_hw_rule *rule)
2498{
2499	struct snd_mask *sfmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT);
2500	struct snd_mask *fmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2501	u32 *subformats = rule->private;
2502	snd_pcm_format_t f;
2503	struct snd_mask m;
2504
2505	snd_mask_none(&m);
2506	/* All PCMs support at least the default STD subformat. */
2507	snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_STD);
2508
2509	pcm_for_each_format(f) {
2510		if (!snd_mask_test(fmask, (__force unsigned)f))
2511			continue;
2512
2513		if (f == SNDRV_PCM_FORMAT_S32_LE && *subformats)
2514			m.bits[0] |= *subformats;
2515		else if (snd_pcm_format_linear(f))
2516			snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX);
2517	}
2518
2519	return snd_mask_refine(sfmask, &m);
2520}
2521
2522static int snd_pcm_hw_constraint_subformats(struct snd_pcm_runtime *runtime,
2523					   unsigned int cond, u32 *subformats)
2524{
2525	return snd_pcm_hw_rule_add(runtime, cond, -1,
2526				   snd_pcm_hw_rule_subformats, (void *)subformats,
2527				   SNDRV_PCM_HW_PARAM_SUBFORMAT,
2528				   SNDRV_PCM_HW_PARAM_FORMAT, -1);
2529}
2530
2531static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2532{
2533	struct snd_pcm_runtime *runtime = substream->runtime;
2534	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2535	int k, err;
2536
2537	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2538		snd_mask_any(constrs_mask(constrs, k));
2539	}
2540
2541	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2542		snd_interval_any(constrs_interval(constrs, k));
2543	}
2544
2545	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2546	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2547	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2548	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2549	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2550
2551	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2552				   snd_pcm_hw_rule_format, NULL,
2553				   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2554	if (err < 0)
2555		return err;
2556	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2557				  snd_pcm_hw_rule_sample_bits, NULL,
2558				  SNDRV_PCM_HW_PARAM_FORMAT, 
2559				  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2560	if (err < 0)
2561		return err;
2562	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2563				  snd_pcm_hw_rule_div, NULL,
2564				  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2565	if (err < 0)
2566		return err;
2567	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2568				  snd_pcm_hw_rule_mul, NULL,
2569				  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2570	if (err < 0)
2571		return err;
2572	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2573				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2574				  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2575	if (err < 0)
2576		return err;
2577	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2578				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2579				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2580	if (err < 0)
2581		return err;
2582	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 
2583				  snd_pcm_hw_rule_div, NULL,
2584				  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2585	if (err < 0)
2586		return err;
2587	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2588				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2589				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2590	if (err < 0)
2591		return err;
2592	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2593				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2594				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2595	if (err < 0)
2596		return err;
2597	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 
2598				  snd_pcm_hw_rule_div, NULL,
2599				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2600	if (err < 0)
2601		return err;
2602	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2603				  snd_pcm_hw_rule_div, NULL,
2604				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2605	if (err < 0)
2606		return err;
2607	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2608				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2609				  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2610	if (err < 0)
2611		return err;
2612	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2613				  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2614				  SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2615	if (err < 0)
2616		return err;
2617	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2618				  snd_pcm_hw_rule_mul, NULL,
2619				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2620	if (err < 0)
2621		return err;
2622	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2623				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2624				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2625	if (err < 0)
2626		return err;
2627	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2628				  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2629				  SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2630	if (err < 0)
2631		return err;
2632	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 
2633				  snd_pcm_hw_rule_muldivk, (void*) 8,
2634				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2635	if (err < 0)
2636		return err;
2637	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2638				  snd_pcm_hw_rule_muldivk, (void*) 8,
2639				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2640	if (err < 0)
2641		return err;
2642	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 
2643				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2644				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2645	if (err < 0)
2646		return err;
2647	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 
2648				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2649				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2650	if (err < 0)
2651		return err;
2652	return 0;
2653}
2654
2655static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2656{
2657	struct snd_pcm_runtime *runtime = substream->runtime;
2658	struct snd_pcm_hardware *hw = &runtime->hw;
2659	int err;
2660	unsigned int mask = 0;
2661
2662        if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2663		mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2664        if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2665		mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2666	if (hw_support_mmap(substream)) {
2667		if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2668			mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2669		if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2670			mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2671		if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2672			mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2673	}
2674	err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2675	if (err < 0)
2676		return err;
2677
2678	err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2679	if (err < 0)
2680		return err;
2681
2682	err = snd_pcm_hw_constraint_subformats(runtime, 0, &hw->subformats);
2683	if (err < 0)
2684		return err;
2685
2686	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2687					   hw->channels_min, hw->channels_max);
2688	if (err < 0)
2689		return err;
2690
2691	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2692					   hw->rate_min, hw->rate_max);
2693	if (err < 0)
2694		return err;
2695
2696	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2697					   hw->period_bytes_min, hw->period_bytes_max);
2698	if (err < 0)
2699		return err;
2700
2701	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2702					   hw->periods_min, hw->periods_max);
2703	if (err < 0)
2704		return err;
2705
2706	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2707					   hw->period_bytes_min, hw->buffer_bytes_max);
2708	if (err < 0)
2709		return err;
2710
2711	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2712				  snd_pcm_hw_rule_buffer_bytes_max, substream,
2713				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2714	if (err < 0)
2715		return err;
2716
2717	/* FIXME: remove */
2718	if (runtime->dma_bytes) {
2719		err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2720		if (err < 0)
2721			return err;
2722	}
2723
2724	if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2725		err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2726					  snd_pcm_hw_rule_rate, hw,
2727					  SNDRV_PCM_HW_PARAM_RATE, -1);
2728		if (err < 0)
2729			return err;
2730	}
2731
2732	/* FIXME: this belong to lowlevel */
2733	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2734
2735	return 0;
2736}
2737
2738static void pcm_release_private(struct snd_pcm_substream *substream)
2739{
2740	if (snd_pcm_stream_linked(substream))
2741		snd_pcm_unlink(substream);
2742}
2743
2744void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2745{
2746	substream->ref_count--;
2747	if (substream->ref_count > 0)
2748		return;
2749
2750	snd_pcm_drop(substream);
2751	if (substream->hw_opened) {
2752		if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
2753			do_hw_free(substream);
2754		substream->ops->close(substream);
2755		substream->hw_opened = 0;
2756	}
2757	if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2758		cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2759	if (substream->pcm_release) {
2760		substream->pcm_release(substream);
2761		substream->pcm_release = NULL;
2762	}
2763	snd_pcm_detach_substream(substream);
2764}
2765EXPORT_SYMBOL(snd_pcm_release_substream);
2766
2767int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2768			   struct file *file,
2769			   struct snd_pcm_substream **rsubstream)
2770{
2771	struct snd_pcm_substream *substream;
2772	int err;
2773
2774	err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2775	if (err < 0)
2776		return err;
2777	if (substream->ref_count > 1) {
2778		*rsubstream = substream;
2779		return 0;
2780	}
2781
2782	err = snd_pcm_hw_constraints_init(substream);
2783	if (err < 0) {
2784		pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2785		goto error;
2786	}
2787
2788	err = substream->ops->open(substream);
2789	if (err < 0)
2790		goto error;
2791
2792	substream->hw_opened = 1;
2793
2794	err = snd_pcm_hw_constraints_complete(substream);
2795	if (err < 0) {
2796		pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2797		goto error;
2798	}
2799
2800	/* automatically set EXPLICIT_SYNC flag in the managed mode whenever
2801	 * the DMA buffer requires it
2802	 */
2803	if (substream->managed_buffer_alloc &&
2804	    substream->dma_buffer.dev.need_sync)
2805		substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC;
2806
2807	*rsubstream = substream;
2808	return 0;
2809
2810 error:
2811	snd_pcm_release_substream(substream);
2812	return err;
2813}
2814EXPORT_SYMBOL(snd_pcm_open_substream);
2815
2816static int snd_pcm_open_file(struct file *file,
2817			     struct snd_pcm *pcm,
2818			     int stream)
2819{
2820	struct snd_pcm_file *pcm_file;
2821	struct snd_pcm_substream *substream;
2822	int err;
2823
2824	err = snd_pcm_open_substream(pcm, stream, file, &substream);
2825	if (err < 0)
2826		return err;
2827
2828	pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2829	if (pcm_file == NULL) {
2830		snd_pcm_release_substream(substream);
2831		return -ENOMEM;
2832	}
2833	pcm_file->substream = substream;
2834	if (substream->ref_count == 1)
2835		substream->pcm_release = pcm_release_private;
2836	file->private_data = pcm_file;
2837
2838	return 0;
2839}
2840
2841static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2842{
2843	struct snd_pcm *pcm;
2844	int err = nonseekable_open(inode, file);
2845	if (err < 0)
2846		return err;
2847	pcm = snd_lookup_minor_data(iminor(inode),
2848				    SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2849	err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2850	if (pcm)
2851		snd_card_unref(pcm->card);
2852	return err;
2853}
2854
2855static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2856{
2857	struct snd_pcm *pcm;
2858	int err = nonseekable_open(inode, file);
2859	if (err < 0)
2860		return err;
2861	pcm = snd_lookup_minor_data(iminor(inode),
2862				    SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2863	err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2864	if (pcm)
2865		snd_card_unref(pcm->card);
2866	return err;
2867}
2868
2869static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2870{
2871	int err;
2872	wait_queue_entry_t wait;
2873
2874	if (pcm == NULL) {
2875		err = -ENODEV;
2876		goto __error1;
2877	}
2878	err = snd_card_file_add(pcm->card, file);
2879	if (err < 0)
2880		goto __error1;
2881	if (!try_module_get(pcm->card->module)) {
2882		err = -EFAULT;
2883		goto __error2;
2884	}
2885	init_waitqueue_entry(&wait, current);
2886	add_wait_queue(&pcm->open_wait, &wait);
2887	mutex_lock(&pcm->open_mutex);
2888	while (1) {
2889		err = snd_pcm_open_file(file, pcm, stream);
2890		if (err >= 0)
2891			break;
2892		if (err == -EAGAIN) {
2893			if (file->f_flags & O_NONBLOCK) {
2894				err = -EBUSY;
2895				break;
2896			}
2897		} else
2898			break;
2899		set_current_state(TASK_INTERRUPTIBLE);
2900		mutex_unlock(&pcm->open_mutex);
2901		schedule();
2902		mutex_lock(&pcm->open_mutex);
2903		if (pcm->card->shutdown) {
2904			err = -ENODEV;
2905			break;
2906		}
2907		if (signal_pending(current)) {
2908			err = -ERESTARTSYS;
2909			break;
2910		}
2911	}
2912	remove_wait_queue(&pcm->open_wait, &wait);
2913	mutex_unlock(&pcm->open_mutex);
2914	if (err < 0)
2915		goto __error;
2916	return err;
2917
2918      __error:
2919	module_put(pcm->card->module);
2920      __error2:
2921      	snd_card_file_remove(pcm->card, file);
2922      __error1:
2923      	return err;
2924}
2925
2926static int snd_pcm_release(struct inode *inode, struct file *file)
2927{
2928	struct snd_pcm *pcm;
2929	struct snd_pcm_substream *substream;
2930	struct snd_pcm_file *pcm_file;
2931
2932	pcm_file = file->private_data;
2933	substream = pcm_file->substream;
2934	if (snd_BUG_ON(!substream))
2935		return -ENXIO;
2936	pcm = substream->pcm;
2937
2938	/* block until the device gets woken up as it may touch the hardware */
2939	snd_power_wait(pcm->card);
2940
2941	scoped_guard(mutex, &pcm->open_mutex) {
2942		snd_pcm_release_substream(substream);
2943		kfree(pcm_file);
2944	}
2945	wake_up(&pcm->open_wait);
2946	module_put(pcm->card->module);
2947	snd_card_file_remove(pcm->card, file);
2948	return 0;
2949}
2950
2951/* check and update PCM state; return 0 or a negative error
2952 * call this inside PCM lock
2953 */
2954static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2955{
2956	switch (substream->runtime->state) {
2957	case SNDRV_PCM_STATE_DRAINING:
2958		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2959			return -EBADFD;
2960		fallthrough;
2961	case SNDRV_PCM_STATE_RUNNING:
2962		return snd_pcm_update_hw_ptr(substream);
2963	case SNDRV_PCM_STATE_PREPARED:
2964	case SNDRV_PCM_STATE_PAUSED:
2965		return 0;
2966	case SNDRV_PCM_STATE_SUSPENDED:
2967		return -ESTRPIPE;
2968	case SNDRV_PCM_STATE_XRUN:
2969		return -EPIPE;
2970	default:
2971		return -EBADFD;
2972	}
2973}
2974
2975/* increase the appl_ptr; returns the processed frames or a negative error */
2976static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2977					  snd_pcm_uframes_t frames,
2978					   snd_pcm_sframes_t avail)
2979{
2980	struct snd_pcm_runtime *runtime = substream->runtime;
2981	snd_pcm_sframes_t appl_ptr;
2982	int ret;
2983
2984	if (avail <= 0)
2985		return 0;
2986	if (frames > (snd_pcm_uframes_t)avail)
2987		frames = avail;
2988	appl_ptr = runtime->control->appl_ptr + frames;
2989	if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2990		appl_ptr -= runtime->boundary;
2991	ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2992	return ret < 0 ? ret : frames;
2993}
2994
2995/* decrease the appl_ptr; returns the processed frames or zero for error */
2996static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2997					 snd_pcm_uframes_t frames,
2998					 snd_pcm_sframes_t avail)
2999{
3000	struct snd_pcm_runtime *runtime = substream->runtime;
3001	snd_pcm_sframes_t appl_ptr;
3002	int ret;
3003
3004	if (avail <= 0)
3005		return 0;
3006	if (frames > (snd_pcm_uframes_t)avail)
3007		frames = avail;
3008	appl_ptr = runtime->control->appl_ptr - frames;
3009	if (appl_ptr < 0)
3010		appl_ptr += runtime->boundary;
3011	ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
3012	/* NOTE: we return zero for errors because PulseAudio gets depressed
3013	 * upon receiving an error from rewind ioctl and stops processing
3014	 * any longer.  Returning zero means that no rewind is done, so
3015	 * it's not absolutely wrong to answer like that.
3016	 */
3017	return ret < 0 ? 0 : frames;
3018}
3019
3020static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
3021					snd_pcm_uframes_t frames)
3022{
3023	snd_pcm_sframes_t ret;
3024
3025	if (frames == 0)
3026		return 0;
3027
3028	scoped_guard(pcm_stream_lock_irq, substream) {
3029		ret = do_pcm_hwsync(substream);
3030		if (!ret)
3031			ret = rewind_appl_ptr(substream, frames,
3032					      snd_pcm_hw_avail(substream));
3033	}
3034	if (ret >= 0)
3035		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3036	return ret;
3037}
3038
3039static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
3040					 snd_pcm_uframes_t frames)
3041{
3042	snd_pcm_sframes_t ret;
3043
3044	if (frames == 0)
3045		return 0;
3046
3047	scoped_guard(pcm_stream_lock_irq, substream) {
3048		ret = do_pcm_hwsync(substream);
3049		if (!ret)
3050			ret = forward_appl_ptr(substream, frames,
3051					       snd_pcm_avail(substream));
3052	}
3053	if (ret >= 0)
3054		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3055	return ret;
3056}
3057
3058static int snd_pcm_delay(struct snd_pcm_substream *substream,
3059			 snd_pcm_sframes_t *delay)
3060{
3061	int err;
3062
3063	scoped_guard(pcm_stream_lock_irq, substream) {
3064		err = do_pcm_hwsync(substream);
3065		if (delay && !err)
3066			*delay = snd_pcm_calc_delay(substream);
3067	}
3068	snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
3069
3070	return err;
3071}
3072		
3073static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream)
3074{
3075	return snd_pcm_delay(substream, NULL);
3076}
3077
3078#define snd_pcm_sync_ptr_get_user(__f, __c, __ptr) ({				\
3079	__label__ failed, failed_begin;						\
3080	int __err = -EFAULT;							\
3081	typeof(*(__ptr)) __user *__src = (__ptr);				\
3082										\
3083	if (!user_read_access_begin(__src, sizeof(*__src)))			\
3084		goto failed_begin;						\
3085	unsafe_get_user(__f, &__src->flags, failed);				\
3086	unsafe_get_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed);	\
3087	unsafe_get_user(__c.avail_min, &__src->c.control.avail_min, failed);	\
3088	__err = 0;								\
3089failed:										\
3090	user_read_access_end();							\
3091failed_begin:									\
3092	__err;									\
3093})
3094
3095#define snd_pcm_sync_ptr_put_user(__s, __c, __ptr) ({				\
3096	__label__ failed, failed_begin;						\
3097	int __err = -EFAULT;							\
3098	typeof(*(__ptr)) __user *__src = (__ptr);				\
3099										\
3100	if (!user_write_access_begin(__src, sizeof(*__src)))			\
3101		goto failed_begin;						\
3102	unsafe_put_user(__s.state, &__src->s.status.state, failed);		\
3103	unsafe_put_user(__s.hw_ptr, &__src->s.status.hw_ptr, failed);		\
3104	unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp.tv_sec, failed);		\
3105	unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp.tv_nsec, failed);		\
3106	unsafe_put_user(__s.suspended_state, &__src->s.status.suspended_state, failed);		\
3107	unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp.tv_sec, failed);	\
3108	unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp.tv_nsec, failed);\
3109	unsafe_put_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed);	\
3110	unsafe_put_user(__c.avail_min, &__src->c.control.avail_min, failed);	\
3111	__err = 0;								\
3112failed:										\
3113	user_write_access_end();						\
3114failed_begin:									\
3115	__err;									\
3116})
3117
3118static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
3119			    struct snd_pcm_sync_ptr __user *_sync_ptr)
3120{
3121	struct snd_pcm_runtime *runtime = substream->runtime;
3122	volatile struct snd_pcm_mmap_status *status;
3123	volatile struct snd_pcm_mmap_control *control;
3124	u32 sflags;
3125	struct snd_pcm_mmap_control scontrol;
3126	struct snd_pcm_mmap_status sstatus;
3127	int err;
3128
3129	if (snd_pcm_sync_ptr_get_user(sflags, scontrol, _sync_ptr))
3130		return -EFAULT;
3131	status = runtime->status;
3132	control = runtime->control;
3133	if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3134		err = snd_pcm_hwsync(substream);
3135		if (err < 0)
3136			return err;
3137	}
3138	scoped_guard(pcm_stream_lock_irq, substream) {
3139		if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3140			err = pcm_lib_apply_appl_ptr(substream, scontrol.appl_ptr);
3141			if (err < 0)
3142				return err;
3143		} else {
3144			scontrol.appl_ptr = control->appl_ptr;
3145		}
3146		if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3147			control->avail_min = scontrol.avail_min;
3148		else
3149			scontrol.avail_min = control->avail_min;
3150		sstatus.state = status->state;
3151		sstatus.hw_ptr = status->hw_ptr;
3152		sstatus.tstamp = status->tstamp;
3153		sstatus.suspended_state = status->suspended_state;
3154		sstatus.audio_tstamp = status->audio_tstamp;
3155	}
3156	if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3157		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3158	if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, _sync_ptr))
3159		return -EFAULT;
3160	return 0;
3161}
3162
3163struct snd_pcm_mmap_status32 {
3164	snd_pcm_state_t state;
3165	s32 pad1;
3166	u32 hw_ptr;
3167	struct __snd_timespec tstamp;
3168	snd_pcm_state_t suspended_state;
3169	struct __snd_timespec audio_tstamp;
3170} __packed;
3171
3172struct snd_pcm_mmap_control32 {
3173	u32 appl_ptr;
3174	u32 avail_min;
3175};
3176
3177struct snd_pcm_sync_ptr32 {
3178	u32 flags;
3179	union {
3180		struct snd_pcm_mmap_status32 status;
3181		unsigned char reserved[64];
3182	} s;
3183	union {
3184		struct snd_pcm_mmap_control32 control;
3185		unsigned char reserved[64];
3186	} c;
3187} __packed;
3188
3189/* recalculate the boundary within 32bit */
3190static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3191{
3192	snd_pcm_uframes_t boundary;
3193	snd_pcm_uframes_t border;
3194	int order;
3195
3196	if (! runtime->buffer_size)
3197		return 0;
3198
3199	border = 0x7fffffffUL - runtime->buffer_size;
3200	if (runtime->buffer_size > border)
3201		return runtime->buffer_size;
3202
3203	order = __fls(border) - __fls(runtime->buffer_size);
3204	boundary = runtime->buffer_size << order;
3205
3206	if (boundary <= border)
3207		return boundary;
3208	else
3209		return boundary / 2;
3210}
3211
3212static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3213					 struct snd_pcm_sync_ptr32 __user *src)
3214{
3215	struct snd_pcm_runtime *runtime = substream->runtime;
3216	volatile struct snd_pcm_mmap_status *status;
3217	volatile struct snd_pcm_mmap_control *control;
3218	u32 sflags;
3219	struct snd_pcm_mmap_control scontrol;
3220	struct snd_pcm_mmap_status sstatus;
3221	snd_pcm_uframes_t boundary;
3222	int err;
3223
3224	if (snd_BUG_ON(!runtime))
3225		return -EINVAL;
3226
3227	if (snd_pcm_sync_ptr_get_user(sflags, scontrol, src))
3228		return -EFAULT;
3229	if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3230		err = snd_pcm_hwsync(substream);
3231		if (err < 0)
3232			return err;
3233	}
3234	status = runtime->status;
3235	control = runtime->control;
3236	boundary = recalculate_boundary(runtime);
3237	if (! boundary)
3238		boundary = 0x7fffffff;
3239	scoped_guard(pcm_stream_lock_irq, substream) {
3240		/* FIXME: we should consider the boundary for the sync from app */
3241		if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3242			err = pcm_lib_apply_appl_ptr(substream,
3243						     scontrol.appl_ptr);
3244			if (err < 0)
3245				return err;
3246		} else
3247			scontrol.appl_ptr = control->appl_ptr % boundary;
3248		if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3249			control->avail_min = scontrol.avail_min;
3250		else
3251			scontrol.avail_min = control->avail_min;
3252		sstatus.state = status->state;
3253		sstatus.hw_ptr = status->hw_ptr % boundary;
3254		sstatus.tstamp = status->tstamp;
3255		sstatus.suspended_state = status->suspended_state;
3256		sstatus.audio_tstamp = status->audio_tstamp;
3257	}
3258	if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3259		snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3260	if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, src))
3261		return -EFAULT;
3262
3263	return 0;
3264}
3265#define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3266
3267static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3268{
3269	struct snd_pcm_runtime *runtime = substream->runtime;
3270	int arg;
3271	
3272	if (get_user(arg, _arg))
3273		return -EFAULT;
3274	if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3275		return -EINVAL;
3276	runtime->tstamp_type = arg;
3277	return 0;
3278}
3279
3280static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3281				      struct snd_xferi __user *_xferi)
3282{
3283	struct snd_xferi xferi;
3284	struct snd_pcm_runtime *runtime = substream->runtime;
3285	snd_pcm_sframes_t result;
3286
3287	if (runtime->state == SNDRV_PCM_STATE_OPEN)
3288		return -EBADFD;
3289	if (put_user(0, &_xferi->result))
3290		return -EFAULT;
3291	if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3292		return -EFAULT;
3293	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3294		result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3295	else
3296		result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3297	if (put_user(result, &_xferi->result))
3298		return -EFAULT;
3299	return result < 0 ? result : 0;
3300}
3301
3302static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3303				      struct snd_xfern __user *_xfern)
3304{
3305	struct snd_xfern xfern;
3306	struct snd_pcm_runtime *runtime = substream->runtime;
3307	void *bufs __free(kfree) = NULL;
3308	snd_pcm_sframes_t result;
3309
3310	if (runtime->state == SNDRV_PCM_STATE_OPEN)
3311		return -EBADFD;
3312	if (runtime->channels > 128)
3313		return -EINVAL;
3314	if (put_user(0, &_xfern->result))
3315		return -EFAULT;
3316	if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3317		return -EFAULT;
3318
3319	bufs = memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *));
3320	if (IS_ERR(bufs))
3321		return PTR_ERR(bufs);
3322	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3323		result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3324	else
3325		result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3326	if (put_user(result, &_xfern->result))
3327		return -EFAULT;
3328	return result < 0 ? result : 0;
3329}
3330
3331static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3332				snd_pcm_uframes_t __user *_frames)
3333{
3334	snd_pcm_uframes_t frames;
3335	snd_pcm_sframes_t result;
3336
3337	if (get_user(frames, _frames))
3338		return -EFAULT;
3339	if (put_user(0, _frames))
3340		return -EFAULT;
3341	result = snd_pcm_rewind(substream, frames);
3342	if (put_user(result, _frames))
3343		return -EFAULT;
3344	return result < 0 ? result : 0;
3345}
3346
3347static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3348				 snd_pcm_uframes_t __user *_frames)
3349{
3350	snd_pcm_uframes_t frames;
3351	snd_pcm_sframes_t result;
3352
3353	if (get_user(frames, _frames))
3354		return -EFAULT;
3355	if (put_user(0, _frames))
3356		return -EFAULT;
3357	result = snd_pcm_forward(substream, frames);
3358	if (put_user(result, _frames))
3359		return -EFAULT;
3360	return result < 0 ? result : 0;
3361}
3362
3363static int snd_pcm_common_ioctl(struct file *file,
3364				 struct snd_pcm_substream *substream,
3365				 unsigned int cmd, void __user *arg)
3366{
3367	struct snd_pcm_file *pcm_file = file->private_data;
3368	int res;
3369
3370	if (PCM_RUNTIME_CHECK(substream))
3371		return -ENXIO;
3372
3373	if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3374		return -EBADFD;
3375
3376	res = snd_power_wait(substream->pcm->card);
3377	if (res < 0)
3378		return res;
3379
3380	switch (cmd) {
3381	case SNDRV_PCM_IOCTL_PVERSION:
3382		return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3383	case SNDRV_PCM_IOCTL_INFO:
3384		return snd_pcm_info_user(substream, arg);
3385	case SNDRV_PCM_IOCTL_TSTAMP:	/* just for compatibility */
3386		return 0;
3387	case SNDRV_PCM_IOCTL_TTSTAMP:
3388		return snd_pcm_tstamp(substream, arg);
3389	case SNDRV_PCM_IOCTL_USER_PVERSION:
3390		if (get_user(pcm_file->user_pversion,
3391			     (unsigned int __user *)arg))
3392			return -EFAULT;
3393		return 0;
3394	case SNDRV_PCM_IOCTL_HW_REFINE:
3395		return snd_pcm_hw_refine_user(substream, arg);
3396	case SNDRV_PCM_IOCTL_HW_PARAMS:
3397		return snd_pcm_hw_params_user(substream, arg);
3398	case SNDRV_PCM_IOCTL_HW_FREE:
3399		return snd_pcm_hw_free(substream);
3400	case SNDRV_PCM_IOCTL_SW_PARAMS:
3401		return snd_pcm_sw_params_user(substream, arg);
3402	case SNDRV_PCM_IOCTL_STATUS32:
3403		return snd_pcm_status_user32(substream, arg, false);
3404	case SNDRV_PCM_IOCTL_STATUS_EXT32:
3405		return snd_pcm_status_user32(substream, arg, true);
3406	case SNDRV_PCM_IOCTL_STATUS64:
3407		return snd_pcm_status_user64(substream, arg, false);
3408	case SNDRV_PCM_IOCTL_STATUS_EXT64:
3409		return snd_pcm_status_user64(substream, arg, true);
3410	case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3411		return snd_pcm_channel_info_user(substream, arg);
3412	case SNDRV_PCM_IOCTL_PREPARE:
3413		return snd_pcm_prepare(substream, file);
3414	case SNDRV_PCM_IOCTL_RESET:
3415		return snd_pcm_reset(substream);
3416	case SNDRV_PCM_IOCTL_START:
3417		return snd_pcm_start_lock_irq(substream);
3418	case SNDRV_PCM_IOCTL_LINK:
3419		return snd_pcm_link(substream, (int)(unsigned long) arg);
3420	case SNDRV_PCM_IOCTL_UNLINK:
3421		return snd_pcm_unlink(substream);
3422	case SNDRV_PCM_IOCTL_RESUME:
3423		return snd_pcm_resume(substream);
3424	case SNDRV_PCM_IOCTL_XRUN:
3425		return snd_pcm_xrun(substream);
3426	case SNDRV_PCM_IOCTL_HWSYNC:
3427		return snd_pcm_hwsync(substream);
3428	case SNDRV_PCM_IOCTL_DELAY:
3429	{
3430		snd_pcm_sframes_t delay = 0;
3431		snd_pcm_sframes_t __user *res = arg;
3432		int err;
3433
3434		err = snd_pcm_delay(substream, &delay);
3435		if (err)
3436			return err;
3437		if (put_user(delay, res))
3438			return -EFAULT;
3439		return 0;
3440	}
3441	case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3442		return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3443	case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3444		return snd_pcm_sync_ptr(substream, arg);
3445#ifdef CONFIG_SND_SUPPORT_OLD_API
3446	case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3447		return snd_pcm_hw_refine_old_user(substream, arg);
3448	case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3449		return snd_pcm_hw_params_old_user(substream, arg);
3450#endif
3451	case SNDRV_PCM_IOCTL_DRAIN:
3452		return snd_pcm_drain(substream, file);
3453	case SNDRV_PCM_IOCTL_DROP:
3454		return snd_pcm_drop(substream);
3455	case SNDRV_PCM_IOCTL_PAUSE:
3456		return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3457	case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3458	case SNDRV_PCM_IOCTL_READI_FRAMES:
3459		return snd_pcm_xferi_frames_ioctl(substream, arg);
3460	case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3461	case SNDRV_PCM_IOCTL_READN_FRAMES:
3462		return snd_pcm_xfern_frames_ioctl(substream, arg);
3463	case SNDRV_PCM_IOCTL_REWIND:
3464		return snd_pcm_rewind_ioctl(substream, arg);
3465	case SNDRV_PCM_IOCTL_FORWARD:
3466		return snd_pcm_forward_ioctl(substream, arg);
3467	}
3468	pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3469	return -ENOTTY;
3470}
3471
3472static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3473			  unsigned long arg)
3474{
3475	struct snd_pcm_file *pcm_file;
3476
3477	pcm_file = file->private_data;
3478
3479	if (((cmd >> 8) & 0xff) != 'A')
3480		return -ENOTTY;
3481
3482	return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3483				     (void __user *)arg);
3484}
3485
3486/**
3487 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3488 * @substream: PCM substream
3489 * @cmd: IOCTL cmd
3490 * @arg: IOCTL argument
3491 *
3492 * The function is provided primarily for OSS layer and USB gadget drivers,
3493 * and it allows only the limited set of ioctls (hw_params, sw_params,
3494 * prepare, start, drain, drop, forward).
3495 *
3496 * Return: zero if successful, or a negative error code
3497 */
3498int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3499			 unsigned int cmd, void *arg)
3500{
3501	snd_pcm_uframes_t *frames = arg;
3502	snd_pcm_sframes_t result;
3503	
3504	if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3505		return -EBADFD;
3506
3507	switch (cmd) {
3508	case SNDRV_PCM_IOCTL_FORWARD:
3509	{
3510		/* provided only for OSS; capture-only and no value returned */
3511		if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3512			return -EINVAL;
3513		result = snd_pcm_forward(substream, *frames);
3514		return result < 0 ? result : 0;
3515	}
3516	case SNDRV_PCM_IOCTL_HW_PARAMS:
3517		return snd_pcm_hw_params(substream, arg);
3518	case SNDRV_PCM_IOCTL_SW_PARAMS:
3519		return snd_pcm_sw_params(substream, arg);
3520	case SNDRV_PCM_IOCTL_PREPARE:
3521		return snd_pcm_prepare(substream, NULL);
3522	case SNDRV_PCM_IOCTL_START:
3523		return snd_pcm_start_lock_irq(substream);
3524	case SNDRV_PCM_IOCTL_DRAIN:
3525		return snd_pcm_drain(substream, NULL);
3526	case SNDRV_PCM_IOCTL_DROP:
3527		return snd_pcm_drop(substream);
3528	case SNDRV_PCM_IOCTL_DELAY:
3529		return snd_pcm_delay(substream, frames);
3530	default:
3531		return -EINVAL;
3532	}
3533}
3534EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3535
3536static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3537			    loff_t * offset)
3538{
3539	struct snd_pcm_file *pcm_file;
3540	struct snd_pcm_substream *substream;
3541	struct snd_pcm_runtime *runtime;
3542	snd_pcm_sframes_t result;
3543
3544	pcm_file = file->private_data;
3545	substream = pcm_file->substream;
3546	if (PCM_RUNTIME_CHECK(substream))
3547		return -ENXIO;
3548	runtime = substream->runtime;
3549	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
3550	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3551		return -EBADFD;
3552	if (!frame_aligned(runtime, count))
3553		return -EINVAL;
3554	count = bytes_to_frames(runtime, count);
3555	result = snd_pcm_lib_read(substream, buf, count);
3556	if (result > 0)
3557		result = frames_to_bytes(runtime, result);
3558	return result;
3559}
3560
3561static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3562			     size_t count, loff_t * offset)
3563{
3564	struct snd_pcm_file *pcm_file;
3565	struct snd_pcm_substream *substream;
3566	struct snd_pcm_runtime *runtime;
3567	snd_pcm_sframes_t result;
3568
3569	pcm_file = file->private_data;
3570	substream = pcm_file->substream;
3571	if (PCM_RUNTIME_CHECK(substream))
3572		return -ENXIO;
3573	runtime = substream->runtime;
3574	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
3575	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3576		return -EBADFD;
3577	if (!frame_aligned(runtime, count))
3578		return -EINVAL;
3579	count = bytes_to_frames(runtime, count);
3580	result = snd_pcm_lib_write(substream, buf, count);
3581	if (result > 0)
3582		result = frames_to_bytes(runtime, result);
3583	return result;
3584}
3585
3586static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3587{
3588	struct snd_pcm_file *pcm_file;
3589	struct snd_pcm_substream *substream;
3590	struct snd_pcm_runtime *runtime;
3591	snd_pcm_sframes_t result;
3592	unsigned long i;
3593	snd_pcm_uframes_t frames;
3594	const struct iovec *iov = iter_iov(to);
3595
3596	pcm_file = iocb->ki_filp->private_data;
3597	substream = pcm_file->substream;
3598	if (PCM_RUNTIME_CHECK(substream))
3599		return -ENXIO;
3600	runtime = substream->runtime;
3601	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
3602	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3603		return -EBADFD;
3604	if (!user_backed_iter(to))
3605		return -EINVAL;
3606	if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3607		return -EINVAL;
3608	if (!frame_aligned(runtime, iov->iov_len))
3609		return -EINVAL;
3610	frames = bytes_to_samples(runtime, iov->iov_len);
3611
3612	void __user **bufs __free(kfree) =
3613		kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3614	if (bufs == NULL)
3615		return -ENOMEM;
3616	for (i = 0; i < to->nr_segs; ++i) {
3617		bufs[i] = iov->iov_base;
3618		iov++;
3619	}
3620	result = snd_pcm_lib_readv(substream, bufs, frames);
3621	if (result > 0)
3622		result = frames_to_bytes(runtime, result);
3623	return result;
3624}
3625
3626static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3627{
3628	struct snd_pcm_file *pcm_file;
3629	struct snd_pcm_substream *substream;
3630	struct snd_pcm_runtime *runtime;
3631	snd_pcm_sframes_t result;
3632	unsigned long i;
3633	snd_pcm_uframes_t frames;
3634	const struct iovec *iov = iter_iov(from);
3635
3636	pcm_file = iocb->ki_filp->private_data;
3637	substream = pcm_file->substream;
3638	if (PCM_RUNTIME_CHECK(substream))
3639		return -ENXIO;
3640	runtime = substream->runtime;
3641	if (runtime->state == SNDRV_PCM_STATE_OPEN ||
3642	    runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3643		return -EBADFD;
3644	if (!user_backed_iter(from))
3645		return -EINVAL;
3646	if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3647	    !frame_aligned(runtime, iov->iov_len))
3648		return -EINVAL;
3649	frames = bytes_to_samples(runtime, iov->iov_len);
3650
3651	void __user **bufs __free(kfree) =
3652		kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3653	if (bufs == NULL)
3654		return -ENOMEM;
3655	for (i = 0; i < from->nr_segs; ++i) {
3656		bufs[i] = iov->iov_base;
3657		iov++;
3658	}
3659	result = snd_pcm_lib_writev(substream, bufs, frames);
3660	if (result > 0)
3661		result = frames_to_bytes(runtime, result);
3662	return result;
3663}
3664
3665static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3666{
3667	struct snd_pcm_file *pcm_file;
3668	struct snd_pcm_substream *substream;
3669	struct snd_pcm_runtime *runtime;
3670	__poll_t mask, ok;
3671	snd_pcm_uframes_t avail;
3672
3673	pcm_file = file->private_data;
3674
3675	substream = pcm_file->substream;
3676	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3677		ok = EPOLLOUT | EPOLLWRNORM;
3678	else
3679		ok = EPOLLIN | EPOLLRDNORM;
3680	if (PCM_RUNTIME_CHECK(substream))
3681		return ok | EPOLLERR;
3682
3683	runtime = substream->runtime;
3684	if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
3685		return ok | EPOLLERR;
3686
3687	poll_wait(file, &runtime->sleep, wait);
3688
3689	mask = 0;
3690	guard(pcm_stream_lock_irq)(substream);
3691	avail = snd_pcm_avail(substream);
3692	switch (runtime->state) {
3693	case SNDRV_PCM_STATE_RUNNING:
3694	case SNDRV_PCM_STATE_PREPARED:
3695	case SNDRV_PCM_STATE_PAUSED:
3696		if (avail >= runtime->control->avail_min)
3697			mask = ok;
3698		break;
3699	case SNDRV_PCM_STATE_DRAINING:
3700		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3701			mask = ok;
3702			if (!avail)
3703				mask |= EPOLLERR;
3704		}
3705		break;
3706	default:
3707		mask = ok | EPOLLERR;
3708		break;
3709	}
3710	return mask;
3711}
3712
3713/*
3714 * mmap support
3715 */
3716
3717/*
3718 * Only on coherent architectures, we can mmap the status and the control records
3719 * for effcient data transfer.  On others, we have to use HWSYNC ioctl...
3720 */
3721#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3722/*
3723 * mmap status record
3724 */
3725static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3726{
3727	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3728	struct snd_pcm_runtime *runtime;
3729	
3730	if (substream == NULL)
3731		return VM_FAULT_SIGBUS;
3732	runtime = substream->runtime;
3733	vmf->page = virt_to_page(runtime->status);
3734	get_page(vmf->page);
3735	return 0;
3736}
3737
3738static const struct vm_operations_struct snd_pcm_vm_ops_status =
3739{
3740	.fault =	snd_pcm_mmap_status_fault,
3741};
3742
3743static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3744			       struct vm_area_struct *area)
3745{
3746	long size;
3747	if (!(area->vm_flags & VM_READ))
3748		return -EINVAL;
3749	size = area->vm_end - area->vm_start;
3750	if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3751		return -EINVAL;
3752	area->vm_ops = &snd_pcm_vm_ops_status;
3753	area->vm_private_data = substream;
3754	vm_flags_mod(area, VM_DONTEXPAND | VM_DONTDUMP,
3755		     VM_WRITE | VM_MAYWRITE);
3756
3757	return 0;
3758}
3759
3760/*
3761 * mmap control record
3762 */
3763static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3764{
3765	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3766	struct snd_pcm_runtime *runtime;
3767	
3768	if (substream == NULL)
3769		return VM_FAULT_SIGBUS;
3770	runtime = substream->runtime;
3771	vmf->page = virt_to_page(runtime->control);
3772	get_page(vmf->page);
3773	return 0;
3774}
3775
3776static const struct vm_operations_struct snd_pcm_vm_ops_control =
3777{
3778	.fault =	snd_pcm_mmap_control_fault,
3779};
3780
3781static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3782				struct vm_area_struct *area)
3783{
3784	long size;
3785	if (!(area->vm_flags & VM_READ))
3786		return -EINVAL;
3787	size = area->vm_end - area->vm_start;
3788	if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3789		return -EINVAL;
3790	area->vm_ops = &snd_pcm_vm_ops_control;
3791	area->vm_private_data = substream;
3792	vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP);
3793	return 0;
3794}
3795
3796static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3797{
3798	/* If drivers require the explicit sync (typically for non-coherent
3799	 * pages), we have to disable the mmap of status and control data
3800	 * to enforce the control via SYNC_PTR ioctl.
3801	 */
3802	if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3803		return false;
3804	/* See pcm_control_mmap_allowed() below.
3805	 * Since older alsa-lib requires both status and control mmaps to be
3806	 * coupled, we have to disable the status mmap for old alsa-lib, too.
3807	 */
3808	if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3809	    (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3810		return false;
3811	return true;
3812}
3813
3814static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3815{
3816	if (pcm_file->no_compat_mmap)
3817		return false;
3818	/* see above */
3819	if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3820		return false;
3821	/* Disallow the control mmap when SYNC_APPLPTR flag is set;
3822	 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3823	 * thus it effectively assures the manual update of appl_ptr.
3824	 */
3825	if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3826		return false;
3827	return true;
3828}
3829
3830#else /* ! coherent mmap */
3831/*
3832 * don't support mmap for status and control records.
3833 */
3834#define pcm_status_mmap_allowed(pcm_file)	false
3835#define pcm_control_mmap_allowed(pcm_file)	false
3836
3837static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3838			       struct vm_area_struct *area)
3839{
3840	return -ENXIO;
3841}
3842static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3843				struct vm_area_struct *area)
3844{
3845	return -ENXIO;
3846}
3847#endif /* coherent mmap */
3848
3849/*
3850 * snd_pcm_mmap_data_open - increase the mmap counter
3851 */
3852static void snd_pcm_mmap_data_open(struct vm_area_struct *area)
3853{
3854	struct snd_pcm_substream *substream = area->vm_private_data;
3855
3856	atomic_inc(&substream->mmap_count);
3857}
3858
3859/*
3860 * snd_pcm_mmap_data_close - decrease the mmap counter
3861 */
3862static void snd_pcm_mmap_data_close(struct vm_area_struct *area)
3863{
3864	struct snd_pcm_substream *substream = area->vm_private_data;
3865
3866	atomic_dec(&substream->mmap_count);
3867}
3868
3869/*
3870 * fault callback for mmapping a RAM page
3871 */
3872static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3873{
3874	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3875	struct snd_pcm_runtime *runtime;
3876	unsigned long offset;
3877	struct page * page;
3878	size_t dma_bytes;
3879	
3880	if (substream == NULL)
3881		return VM_FAULT_SIGBUS;
3882	runtime = substream->runtime;
3883	offset = vmf->pgoff << PAGE_SHIFT;
3884	dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3885	if (offset > dma_bytes - PAGE_SIZE)
3886		return VM_FAULT_SIGBUS;
3887	if (substream->ops->page)
3888		page = substream->ops->page(substream, offset);
3889	else if (!snd_pcm_get_dma_buf(substream)) {
3890		if (WARN_ON_ONCE(!runtime->dma_area))
3891			return VM_FAULT_SIGBUS;
3892		page = virt_to_page(runtime->dma_area + offset);
3893	} else
3894		page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset);
3895	if (!page)
3896		return VM_FAULT_SIGBUS;
3897	get_page(page);
3898	vmf->page = page;
3899	return 0;
3900}
3901
3902static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3903	.open =		snd_pcm_mmap_data_open,
3904	.close =	snd_pcm_mmap_data_close,
3905};
3906
3907static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3908	.open =		snd_pcm_mmap_data_open,
3909	.close =	snd_pcm_mmap_data_close,
3910	.fault =	snd_pcm_mmap_data_fault,
3911};
3912
3913/*
3914 * mmap the DMA buffer on RAM
3915 */
3916
3917/**
3918 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3919 * @substream: PCM substream
3920 * @area: VMA
3921 *
3922 * This is the default mmap handler for PCM data.  When mmap pcm_ops is NULL,
3923 * this function is invoked implicitly.
3924 *
3925 * Return: zero if successful, or a negative error code
3926 */
3927int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3928			     struct vm_area_struct *area)
3929{
3930	vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP);
3931	if (!substream->ops->page &&
3932	    !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area))
3933		return 0;
3934	/* mmap with fault handler */
3935	area->vm_ops = &snd_pcm_vm_ops_data_fault;
3936	return 0;
3937}
3938EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3939
3940/*
3941 * mmap the DMA buffer on I/O memory area
3942 */
3943#if SNDRV_PCM_INFO_MMAP_IOMEM
3944/**
3945 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3946 * @substream: PCM substream
3947 * @area: VMA
3948 *
3949 * When your hardware uses the iomapped pages as the hardware buffer and
3950 * wants to mmap it, pass this function as mmap pcm_ops.  Note that this
3951 * is supposed to work only on limited architectures.
3952 *
3953 * Return: zero if successful, or a negative error code
3954 */
3955int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3956			   struct vm_area_struct *area)
3957{
3958	struct snd_pcm_runtime *runtime = substream->runtime;
3959
3960	area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3961	return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3962}
3963EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3964#endif /* SNDRV_PCM_INFO_MMAP */
3965
3966/*
3967 * mmap DMA buffer
3968 */
3969int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3970		      struct vm_area_struct *area)
3971{
3972	struct snd_pcm_runtime *runtime;
3973	long size;
3974	unsigned long offset;
3975	size_t dma_bytes;
3976	int err;
3977
3978	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3979		if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3980			return -EINVAL;
3981	} else {
3982		if (!(area->vm_flags & VM_READ))
3983			return -EINVAL;
3984	}
3985	runtime = substream->runtime;
3986	if (runtime->state == SNDRV_PCM_STATE_OPEN)
3987		return -EBADFD;
3988	if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3989		return -ENXIO;
3990	if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3991	    runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3992		return -EINVAL;
3993	size = area->vm_end - area->vm_start;
3994	offset = area->vm_pgoff << PAGE_SHIFT;
3995	dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3996	if ((size_t)size > dma_bytes)
3997		return -EINVAL;
3998	if (offset > dma_bytes - size)
3999		return -EINVAL;
4000
4001	area->vm_ops = &snd_pcm_vm_ops_data;
4002	area->vm_private_data = substream;
4003	if (substream->ops->mmap)
4004		err = substream->ops->mmap(substream, area);
4005	else
4006		err = snd_pcm_lib_default_mmap(substream, area);
4007	if (!err)
4008		atomic_inc(&substream->mmap_count);
4009	return err;
4010}
4011EXPORT_SYMBOL(snd_pcm_mmap_data);
4012
4013static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
4014{
4015	struct snd_pcm_file * pcm_file;
4016	struct snd_pcm_substream *substream;	
4017	unsigned long offset;
4018	
4019	pcm_file = file->private_data;
4020	substream = pcm_file->substream;
4021	if (PCM_RUNTIME_CHECK(substream))
4022		return -ENXIO;
4023	if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
4024		return -EBADFD;
4025
4026	offset = area->vm_pgoff << PAGE_SHIFT;
4027	switch (offset) {
4028	case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
4029		if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
4030			return -ENXIO;
4031		fallthrough;
4032	case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4033		if (!pcm_status_mmap_allowed(pcm_file))
4034			return -ENXIO;
4035		return snd_pcm_mmap_status(substream, file, area);
4036	case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
4037		if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
4038			return -ENXIO;
4039		fallthrough;
4040	case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4041		if (!pcm_control_mmap_allowed(pcm_file))
4042			return -ENXIO;
4043		return snd_pcm_mmap_control(substream, file, area);
4044	default:
4045		return snd_pcm_mmap_data(substream, file, area);
4046	}
4047	return 0;
4048}
4049
4050static int snd_pcm_fasync(int fd, struct file * file, int on)
4051{
4052	struct snd_pcm_file * pcm_file;
4053	struct snd_pcm_substream *substream;
4054	struct snd_pcm_runtime *runtime;
4055
4056	pcm_file = file->private_data;
4057	substream = pcm_file->substream;
4058	if (PCM_RUNTIME_CHECK(substream))
4059		return -ENXIO;
4060	runtime = substream->runtime;
4061	if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
4062		return -EBADFD;
4063	return snd_fasync_helper(fd, file, on, &runtime->fasync);
4064}
4065
4066/*
4067 * ioctl32 compat
4068 */
4069#ifdef CONFIG_COMPAT
4070#include "pcm_compat.c"
4071#else
4072#define snd_pcm_ioctl_compat	NULL
4073#endif
4074
4075/*
4076 *  To be removed helpers to keep binary compatibility
4077 */
4078
4079#ifdef CONFIG_SND_SUPPORT_OLD_API
4080#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
4081#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
4082
4083static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
4084					       struct snd_pcm_hw_params_old *oparams)
4085{
4086	unsigned int i;
4087
4088	memset(params, 0, sizeof(*params));
4089	params->flags = oparams->flags;
4090	for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
4091		params->masks[i].bits[0] = oparams->masks[i];
4092	memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
4093	params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
4094	params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
4095	params->info = oparams->info;
4096	params->msbits = oparams->msbits;
4097	params->rate_num = oparams->rate_num;
4098	params->rate_den = oparams->rate_den;
4099	params->fifo_size = oparams->fifo_size;
4100}
4101
4102static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
4103					     struct snd_pcm_hw_params *params)
4104{
4105	unsigned int i;
4106
4107	memset(oparams, 0, sizeof(*oparams));
4108	oparams->flags = params->flags;
4109	for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
4110		oparams->masks[i] = params->masks[i].bits[0];
4111	memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
4112	oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
4113	oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
4114	oparams->info = params->info;
4115	oparams->msbits = params->msbits;
4116	oparams->rate_num = params->rate_num;
4117	oparams->rate_den = params->rate_den;
4118	oparams->fifo_size = params->fifo_size;
4119}
4120
4121static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
4122				      struct snd_pcm_hw_params_old __user * _oparams)
4123{
4124	int err;
4125
4126	struct snd_pcm_hw_params *params __free(kfree) =
4127		kmalloc(sizeof(*params), GFP_KERNEL);
4128	if (!params)
4129		return -ENOMEM;
4130
4131	struct snd_pcm_hw_params_old *oparams __free(kfree) =
4132		memdup_user(_oparams, sizeof(*oparams));
4133	if (IS_ERR(oparams))
4134		return PTR_ERR(oparams);
4135	snd_pcm_hw_convert_from_old_params(params, oparams);
4136	err = snd_pcm_hw_refine(substream, params);
4137	if (err < 0)
4138		return err;
4139
4140	err = fixup_unreferenced_params(substream, params);
4141	if (err < 0)
4142		return err;
4143
4144	snd_pcm_hw_convert_to_old_params(oparams, params);
4145	if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
4146		return -EFAULT;
4147	return 0;
4148}
4149
4150static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
4151				      struct snd_pcm_hw_params_old __user * _oparams)
4152{
4153	int err;
4154
4155	struct snd_pcm_hw_params *params __free(kfree) =
4156		kmalloc(sizeof(*params), GFP_KERNEL);
4157	if (!params)
4158		return -ENOMEM;
4159
4160	struct snd_pcm_hw_params_old *oparams __free(kfree) =
4161		memdup_user(_oparams, sizeof(*oparams));
4162	if (IS_ERR(oparams))
4163		return PTR_ERR(oparams);
4164
4165	snd_pcm_hw_convert_from_old_params(params, oparams);
4166	err = snd_pcm_hw_params(substream, params);
4167	if (err < 0)
4168		return err;
4169
4170	snd_pcm_hw_convert_to_old_params(oparams, params);
4171	if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
4172		return -EFAULT;
4173	return 0;
4174}
4175#endif /* CONFIG_SND_SUPPORT_OLD_API */
4176
4177#ifndef CONFIG_MMU
4178static unsigned long snd_pcm_get_unmapped_area(struct file *file,
4179					       unsigned long addr,
4180					       unsigned long len,
4181					       unsigned long pgoff,
4182					       unsigned long flags)
4183{
4184	struct snd_pcm_file *pcm_file = file->private_data;
4185	struct snd_pcm_substream *substream = pcm_file->substream;
4186	struct snd_pcm_runtime *runtime = substream->runtime;
4187	unsigned long offset = pgoff << PAGE_SHIFT;
4188
4189	switch (offset) {
4190	case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4191		return (unsigned long)runtime->status;
4192	case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4193		return (unsigned long)runtime->control;
4194	default:
4195		return (unsigned long)runtime->dma_area + offset;
4196	}
4197}
4198#else
4199# define snd_pcm_get_unmapped_area NULL
4200#endif
4201
4202/*
4203 *  Register section
4204 */
4205
4206const struct file_operations snd_pcm_f_ops[2] = {
4207	{
4208		.owner =		THIS_MODULE,
4209		.write =		snd_pcm_write,
4210		.write_iter =		snd_pcm_writev,
4211		.open =			snd_pcm_playback_open,
4212		.release =		snd_pcm_release,
4213		.poll =			snd_pcm_poll,
4214		.unlocked_ioctl =	snd_pcm_ioctl,
4215		.compat_ioctl = 	snd_pcm_ioctl_compat,
4216		.mmap =			snd_pcm_mmap,
4217		.fasync =		snd_pcm_fasync,
4218		.get_unmapped_area =	snd_pcm_get_unmapped_area,
4219	},
4220	{
4221		.owner =		THIS_MODULE,
4222		.read =			snd_pcm_read,
4223		.read_iter =		snd_pcm_readv,
4224		.open =			snd_pcm_capture_open,
4225		.release =		snd_pcm_release,
4226		.poll =			snd_pcm_poll,
4227		.unlocked_ioctl =	snd_pcm_ioctl,
4228		.compat_ioctl = 	snd_pcm_ioctl_compat,
4229		.mmap =			snd_pcm_mmap,
4230		.fasync =		snd_pcm_fasync,
4231		.get_unmapped_area =	snd_pcm_get_unmapped_area,
4232	}
4233};
~/ Read-only file preview.