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