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