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