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