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