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