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