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