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