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