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