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