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