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