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