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