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