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