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