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