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