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