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