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