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