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