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