xref: /openbmc/linux/sound/drivers/dummy.c (revision 7095be6d2cc2870299c9e6c6c2df3148be410c4a)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Dummy soundcard
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/init.h>
86e65c1ccSTakashi Iwai #include <linux/err.h>
96e65c1ccSTakashi Iwai #include <linux/platform_device.h>
101da177e4SLinus Torvalds #include <linux/jiffies.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/time.h>
131da177e4SLinus Torvalds #include <linux/wait.h>
14c631d03cSTakashi Iwai #include <linux/hrtimer.h>
15c631d03cSTakashi Iwai #include <linux/math64.h>
1665a77217SPaul Gortmaker #include <linux/module.h>
171da177e4SLinus Torvalds #include <sound/core.h>
181da177e4SLinus Torvalds #include <sound/control.h>
19fb567a8eSTakashi Iwai #include <sound/tlv.h>
201da177e4SLinus Torvalds #include <sound/pcm.h>
211da177e4SLinus Torvalds #include <sound/rawmidi.h>
229b151fecSTakashi Iwai #include <sound/info.h>
231da177e4SLinus Torvalds #include <sound/initval.h>
241da177e4SLinus Torvalds 
25c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
261da177e4SLinus Torvalds MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
271da177e4SLinus Torvalds MODULE_LICENSE("GPL");
281da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define MAX_PCM_DEVICES		4
31b888d1ceSTakashi Iwai #define MAX_PCM_SUBSTREAMS	128
321da177e4SLinus Torvalds #define MAX_MIDI_DEVICES	2
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /* defaults */
351da177e4SLinus Torvalds #define MAX_BUFFER_SIZE		(64*1024)
36d5e1ca05SJaroslav Kysela #define MIN_PERIOD_SIZE		64
372ad5dd8dSJaroslav Kysela #define MAX_PERIOD_SIZE		MAX_BUFFER_SIZE
381da177e4SLinus Torvalds #define USE_FORMATS 		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
391da177e4SLinus Torvalds #define USE_RATE		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
401da177e4SLinus Torvalds #define USE_RATE_MIN		5500
411da177e4SLinus Torvalds #define USE_RATE_MAX		48000
421da177e4SLinus Torvalds #define USE_CHANNELS_MIN 	1
431da177e4SLinus Torvalds #define USE_CHANNELS_MAX 	2
441da177e4SLinus Torvalds #define USE_PERIODS_MIN 	1
451da177e4SLinus Torvalds #define USE_PERIODS_MAX 	1024
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
481da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
49a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
50d5e1ca05SJaroslav Kysela static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
511da177e4SLinus Torvalds static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
521da177e4SLinus Torvalds static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
531da177e4SLinus Torvalds //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
54c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
55a67ff6a5SRusty Russell static bool hrtimer = 1;
56c631d03cSTakashi Iwai #endif
57a67ff6a5SRusty Russell static bool fake_buffer = 1;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
601da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
611da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
621da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
631da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
641da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
65d5e1ca05SJaroslav Kysela module_param_array(model, charp, NULL, 0444);
66d5e1ca05SJaroslav Kysela MODULE_PARM_DESC(model, "Soundcard model.");
671da177e4SLinus Torvalds module_param_array(pcm_devs, int, NULL, 0444);
681da177e4SLinus Torvalds MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
691da177e4SLinus Torvalds module_param_array(pcm_substreams, int, NULL, 0444);
7023aebca4STakashi Iwai MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
711da177e4SLinus Torvalds //module_param_array(midi_devs, int, NULL, 0444);
721da177e4SLinus Torvalds //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
73a68c4d11STakashi Iwai module_param(fake_buffer, bool, 0444);
74a68c4d11STakashi Iwai MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
75c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
76ddce57a6STakashi Iwai module_param(hrtimer, bool, 0644);
77c631d03cSTakashi Iwai MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
78c631d03cSTakashi Iwai #endif
791da177e4SLinus Torvalds 
80f7a9275dSClemens Ladisch static struct platform_device *devices[SNDRV_CARDS];
81f7a9275dSClemens Ladisch 
821da177e4SLinus Torvalds #define MIXER_ADDR_MASTER	0
831da177e4SLinus Torvalds #define MIXER_ADDR_LINE		1
841da177e4SLinus Torvalds #define MIXER_ADDR_MIC		2
851da177e4SLinus Torvalds #define MIXER_ADDR_SYNTH	3
861da177e4SLinus Torvalds #define MIXER_ADDR_CD		4
871da177e4SLinus Torvalds #define MIXER_ADDR_LAST		4
881da177e4SLinus Torvalds 
89c631d03cSTakashi Iwai struct dummy_timer_ops {
90c631d03cSTakashi Iwai 	int (*create)(struct snd_pcm_substream *);
91c631d03cSTakashi Iwai 	void (*free)(struct snd_pcm_substream *);
92c631d03cSTakashi Iwai 	int (*prepare)(struct snd_pcm_substream *);
93c631d03cSTakashi Iwai 	int (*start)(struct snd_pcm_substream *);
94c631d03cSTakashi Iwai 	int (*stop)(struct snd_pcm_substream *);
95c631d03cSTakashi Iwai 	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
96c631d03cSTakashi Iwai };
97c631d03cSTakashi Iwai 
98ddce57a6STakashi Iwai #define get_dummy_ops(substream) \
99ddce57a6STakashi Iwai 	(*(const struct dummy_timer_ops **)(substream)->runtime->private_data)
100ddce57a6STakashi Iwai 
101d5e1ca05SJaroslav Kysela struct dummy_model {
102d5e1ca05SJaroslav Kysela 	const char *name;
103d5e1ca05SJaroslav Kysela 	int (*playback_constraints)(struct snd_pcm_runtime *runtime);
104d5e1ca05SJaroslav Kysela 	int (*capture_constraints)(struct snd_pcm_runtime *runtime);
105d5e1ca05SJaroslav Kysela 	u64 formats;
106d5e1ca05SJaroslav Kysela 	size_t buffer_bytes_max;
107d5e1ca05SJaroslav Kysela 	size_t period_bytes_min;
108d5e1ca05SJaroslav Kysela 	size_t period_bytes_max;
109d5e1ca05SJaroslav Kysela 	unsigned int periods_min;
110d5e1ca05SJaroslav Kysela 	unsigned int periods_max;
111d5e1ca05SJaroslav Kysela 	unsigned int rates;
112d5e1ca05SJaroslav Kysela 	unsigned int rate_min;
113d5e1ca05SJaroslav Kysela 	unsigned int rate_max;
114d5e1ca05SJaroslav Kysela 	unsigned int channels_min;
115d5e1ca05SJaroslav Kysela 	unsigned int channels_max;
116d5e1ca05SJaroslav Kysela };
117d5e1ca05SJaroslav Kysela 
1184a4d2cfdSTakashi Iwai struct snd_dummy {
1194a4d2cfdSTakashi Iwai 	struct snd_card *card;
120d64e7f7cSTakashi Iwai 	const struct dummy_model *model;
1216e65c1ccSTakashi Iwai 	struct snd_pcm *pcm;
122d5e1ca05SJaroslav Kysela 	struct snd_pcm_hardware pcm_hw;
1231da177e4SLinus Torvalds 	spinlock_t mixer_lock;
1241da177e4SLinus Torvalds 	int mixer_volume[MIXER_ADDR_LAST+1][2];
1251da177e4SLinus Torvalds 	int capture_source[MIXER_ADDR_LAST+1][2];
12616e43467SClemens Ladisch 	int iobox;
12716e43467SClemens Ladisch 	struct snd_kcontrol *cd_volume_ctl;
12816e43467SClemens Ladisch 	struct snd_kcontrol *cd_switch_ctl;
1294a4d2cfdSTakashi Iwai };
1301da177e4SLinus Torvalds 
131c631d03cSTakashi Iwai /*
132d5e1ca05SJaroslav Kysela  * card models
133d5e1ca05SJaroslav Kysela  */
134d5e1ca05SJaroslav Kysela 
135d5e1ca05SJaroslav Kysela static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
136d5e1ca05SJaroslav Kysela {
137d5e1ca05SJaroslav Kysela 	int err;
138d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
139d5e1ca05SJaroslav Kysela 	if (err < 0)
140d5e1ca05SJaroslav Kysela 		return err;
141d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
142d5e1ca05SJaroslav Kysela 	if (err < 0)
143d5e1ca05SJaroslav Kysela 		return err;
144d5e1ca05SJaroslav Kysela 	return 0;
145d5e1ca05SJaroslav Kysela }
146d5e1ca05SJaroslav Kysela 
147d64e7f7cSTakashi Iwai static const struct dummy_model model_emu10k1 = {
148d5e1ca05SJaroslav Kysela 	.name = "emu10k1",
149d5e1ca05SJaroslav Kysela 	.playback_constraints = emu10k1_playback_constraints,
150d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 128 * 1024,
151d5e1ca05SJaroslav Kysela };
152d5e1ca05SJaroslav Kysela 
153d64e7f7cSTakashi Iwai static const struct dummy_model model_rme9652 = {
154d5e1ca05SJaroslav Kysela 	.name = "rme9652",
155d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 26 * 64 * 1024,
156d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
157d5e1ca05SJaroslav Kysela 	.channels_min = 26,
158d5e1ca05SJaroslav Kysela 	.channels_max = 26,
159d5e1ca05SJaroslav Kysela 	.periods_min = 2,
160d5e1ca05SJaroslav Kysela 	.periods_max = 2,
161d5e1ca05SJaroslav Kysela };
162d5e1ca05SJaroslav Kysela 
163d64e7f7cSTakashi Iwai static const struct dummy_model model_ice1712 = {
164d5e1ca05SJaroslav Kysela 	.name = "ice1712",
165d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 256 * 1024,
166d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
167d5e1ca05SJaroslav Kysela 	.channels_min = 10,
168d5e1ca05SJaroslav Kysela 	.channels_max = 10,
169d5e1ca05SJaroslav Kysela 	.periods_min = 1,
170d5e1ca05SJaroslav Kysela 	.periods_max = 1024,
171d5e1ca05SJaroslav Kysela };
172d5e1ca05SJaroslav Kysela 
173d64e7f7cSTakashi Iwai static const struct dummy_model model_uda1341 = {
174d5e1ca05SJaroslav Kysela 	.name = "uda1341",
175d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 16380,
176d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
177d5e1ca05SJaroslav Kysela 	.channels_min = 2,
178d5e1ca05SJaroslav Kysela 	.channels_max = 2,
179d5e1ca05SJaroslav Kysela 	.periods_min = 2,
180d5e1ca05SJaroslav Kysela 	.periods_max = 255,
181d5e1ca05SJaroslav Kysela };
182d5e1ca05SJaroslav Kysela 
183d64e7f7cSTakashi Iwai static const struct dummy_model model_ac97 = {
184d5e1ca05SJaroslav Kysela 	.name = "ac97",
185d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
186d5e1ca05SJaroslav Kysela 	.channels_min = 2,
187d5e1ca05SJaroslav Kysela 	.channels_max = 2,
188d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000,
189d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
190d5e1ca05SJaroslav Kysela 	.rate_max = 48000,
191d5e1ca05SJaroslav Kysela };
192d5e1ca05SJaroslav Kysela 
193d64e7f7cSTakashi Iwai static const struct dummy_model model_ca0106 = {
194d5e1ca05SJaroslav Kysela 	.name = "ca0106",
195d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
196d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = ((65536-64)*8),
197d5e1ca05SJaroslav Kysela 	.period_bytes_max = (65536-64),
198d5e1ca05SJaroslav Kysela 	.periods_min = 2,
199d5e1ca05SJaroslav Kysela 	.periods_max = 8,
200d5e1ca05SJaroslav Kysela 	.channels_min = 2,
201d5e1ca05SJaroslav Kysela 	.channels_max = 2,
202d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
203d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
204d5e1ca05SJaroslav Kysela 	.rate_max = 192000,
205d5e1ca05SJaroslav Kysela };
206d5e1ca05SJaroslav Kysela 
207d64e7f7cSTakashi Iwai static const struct dummy_model *dummy_models[] = {
208d5e1ca05SJaroslav Kysela 	&model_emu10k1,
209d5e1ca05SJaroslav Kysela 	&model_rme9652,
210d5e1ca05SJaroslav Kysela 	&model_ice1712,
211d5e1ca05SJaroslav Kysela 	&model_uda1341,
212d5e1ca05SJaroslav Kysela 	&model_ac97,
213d5e1ca05SJaroslav Kysela 	&model_ca0106,
214d5e1ca05SJaroslav Kysela 	NULL
215d5e1ca05SJaroslav Kysela };
216d5e1ca05SJaroslav Kysela 
217d5e1ca05SJaroslav Kysela /*
218c631d03cSTakashi Iwai  * system timer interface
219c631d03cSTakashi Iwai  */
220c631d03cSTakashi Iwai 
221c631d03cSTakashi Iwai struct dummy_systimer_pcm {
222ddce57a6STakashi Iwai 	/* ops must be the first item */
223ddce57a6STakashi Iwai 	const struct dummy_timer_ops *timer_ops;
2241da177e4SLinus Torvalds 	spinlock_t lock;
2251da177e4SLinus Torvalds 	struct timer_list timer;
226b142037bSTakashi Iwai 	unsigned long base_time;
227b142037bSTakashi Iwai 	unsigned int frac_pos;	/* fractional sample position (based HZ) */
228b5d10781STakashi Iwai 	unsigned int frac_period_rest;
229b142037bSTakashi Iwai 	unsigned int frac_buffer_size;	/* buffer_size * HZ */
230b142037bSTakashi Iwai 	unsigned int frac_period_size;	/* period_size * HZ */
231b142037bSTakashi Iwai 	unsigned int rate;
232b5d10781STakashi Iwai 	int elapsed;
2334a4d2cfdSTakashi Iwai 	struct snd_pcm_substream *substream;
2344a4d2cfdSTakashi Iwai };
2351da177e4SLinus Torvalds 
236b142037bSTakashi Iwai static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
237b142037bSTakashi Iwai {
2382a52b6eeSRoman Kollar 	mod_timer(&dpcm->timer, jiffies +
239*7095be6dSLars-Peter Clausen 		DIV_ROUND_UP(dpcm->frac_period_rest, dpcm->rate));
240b142037bSTakashi Iwai }
241b142037bSTakashi Iwai 
242b142037bSTakashi Iwai static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
243b142037bSTakashi Iwai {
244b142037bSTakashi Iwai 	unsigned long delta;
245b142037bSTakashi Iwai 
246b142037bSTakashi Iwai 	delta = jiffies - dpcm->base_time;
247b142037bSTakashi Iwai 	if (!delta)
248b142037bSTakashi Iwai 		return;
249b5d10781STakashi Iwai 	dpcm->base_time += delta;
250b5d10781STakashi Iwai 	delta *= dpcm->rate;
251b5d10781STakashi Iwai 	dpcm->frac_pos += delta;
252b142037bSTakashi Iwai 	while (dpcm->frac_pos >= dpcm->frac_buffer_size)
253b142037bSTakashi Iwai 		dpcm->frac_pos -= dpcm->frac_buffer_size;
254b5d10781STakashi Iwai 	while (dpcm->frac_period_rest <= delta) {
255b5d10781STakashi Iwai 		dpcm->elapsed++;
256b5d10781STakashi Iwai 		dpcm->frac_period_rest += dpcm->frac_period_size;
257b5d10781STakashi Iwai 	}
258b5d10781STakashi Iwai 	dpcm->frac_period_rest -= delta;
259b142037bSTakashi Iwai }
260b142037bSTakashi Iwai 
261c631d03cSTakashi Iwai static int dummy_systimer_start(struct snd_pcm_substream *substream)
2621da177e4SLinus Torvalds {
263c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
264c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
265b142037bSTakashi Iwai 	dpcm->base_time = jiffies;
266b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
267c8eb6ba1STakashi Iwai 	spin_unlock(&dpcm->lock);
2686e65c1ccSTakashi Iwai 	return 0;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
271c631d03cSTakashi Iwai static int dummy_systimer_stop(struct snd_pcm_substream *substream)
272c631d03cSTakashi Iwai {
273c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
274c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
275c631d03cSTakashi Iwai 	del_timer(&dpcm->timer);
276c631d03cSTakashi Iwai 	spin_unlock(&dpcm->lock);
277c631d03cSTakashi Iwai 	return 0;
278c631d03cSTakashi Iwai }
279c631d03cSTakashi Iwai 
280c631d03cSTakashi Iwai static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
2811da177e4SLinus Torvalds {
2824a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
283c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = runtime->private_data;
2841da177e4SLinus Torvalds 
285b142037bSTakashi Iwai 	dpcm->frac_pos = 0;
286b142037bSTakashi Iwai 	dpcm->rate = runtime->rate;
287b142037bSTakashi Iwai 	dpcm->frac_buffer_size = runtime->buffer_size * HZ;
288b142037bSTakashi Iwai 	dpcm->frac_period_size = runtime->period_size * HZ;
289b5d10781STakashi Iwai 	dpcm->frac_period_rest = dpcm->frac_period_size;
290b5d10781STakashi Iwai 	dpcm->elapsed = 0;
29153463a83SAhmet İnan 
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
295bc47ba90SKees Cook static void dummy_systimer_callback(struct timer_list *t)
2961da177e4SLinus Torvalds {
297bc47ba90SKees Cook 	struct dummy_systimer_pcm *dpcm = from_timer(dpcm, t, timer);
298b32425acSTakashi Iwai 	unsigned long flags;
299b5d10781STakashi Iwai 	int elapsed = 0;
3001da177e4SLinus Torvalds 
301b32425acSTakashi Iwai 	spin_lock_irqsave(&dpcm->lock, flags);
302b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
303b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
304b5d10781STakashi Iwai 	elapsed = dpcm->elapsed;
305b5d10781STakashi Iwai 	dpcm->elapsed = 0;
306b32425acSTakashi Iwai 	spin_unlock_irqrestore(&dpcm->lock, flags);
307b5d10781STakashi Iwai 	if (elapsed)
3081da177e4SLinus Torvalds 		snd_pcm_period_elapsed(dpcm->substream);
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
311c631d03cSTakashi Iwai static snd_pcm_uframes_t
312c631d03cSTakashi Iwai dummy_systimer_pointer(struct snd_pcm_substream *substream)
3131da177e4SLinus Torvalds {
314b142037bSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
315b5d10781STakashi Iwai 	snd_pcm_uframes_t pos;
3161da177e4SLinus Torvalds 
317b142037bSTakashi Iwai 	spin_lock(&dpcm->lock);
318b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
319b5d10781STakashi Iwai 	pos = dpcm->frac_pos / HZ;
320b142037bSTakashi Iwai 	spin_unlock(&dpcm->lock);
321b5d10781STakashi Iwai 	return pos;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324c631d03cSTakashi Iwai static int dummy_systimer_create(struct snd_pcm_substream *substream)
3251da177e4SLinus Torvalds {
326c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm;
3271da177e4SLinus Torvalds 
328c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
329c631d03cSTakashi Iwai 	if (!dpcm)
330c631d03cSTakashi Iwai 		return -ENOMEM;
331c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
332bc47ba90SKees Cook 	timer_setup(&dpcm->timer, dummy_systimer_callback, 0);
333c631d03cSTakashi Iwai 	spin_lock_init(&dpcm->lock);
334c631d03cSTakashi Iwai 	dpcm->substream = substream;
335c631d03cSTakashi Iwai 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
338c631d03cSTakashi Iwai static void dummy_systimer_free(struct snd_pcm_substream *substream)
339c631d03cSTakashi Iwai {
340c631d03cSTakashi Iwai 	kfree(substream->runtime->private_data);
341c631d03cSTakashi Iwai }
342c631d03cSTakashi Iwai 
343d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_systimer_ops = {
344c631d03cSTakashi Iwai 	.create =	dummy_systimer_create,
345c631d03cSTakashi Iwai 	.free =		dummy_systimer_free,
346c631d03cSTakashi Iwai 	.prepare =	dummy_systimer_prepare,
347c631d03cSTakashi Iwai 	.start =	dummy_systimer_start,
348c631d03cSTakashi Iwai 	.stop =		dummy_systimer_stop,
349c631d03cSTakashi Iwai 	.pointer =	dummy_systimer_pointer,
350c631d03cSTakashi Iwai };
351c631d03cSTakashi Iwai 
352c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
353c631d03cSTakashi Iwai /*
354c631d03cSTakashi Iwai  * hrtimer interface
355c631d03cSTakashi Iwai  */
356c631d03cSTakashi Iwai 
357c631d03cSTakashi Iwai struct dummy_hrtimer_pcm {
358ddce57a6STakashi Iwai 	/* ops must be the first item */
359ddce57a6STakashi Iwai 	const struct dummy_timer_ops *timer_ops;
360c631d03cSTakashi Iwai 	ktime_t base_time;
361c631d03cSTakashi Iwai 	ktime_t period_time;
362c631d03cSTakashi Iwai 	atomic_t running;
363c631d03cSTakashi Iwai 	struct hrtimer timer;
364c631d03cSTakashi Iwai 	struct snd_pcm_substream *substream;
365c631d03cSTakashi Iwai };
366c631d03cSTakashi Iwai 
367c631d03cSTakashi Iwai static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
368c631d03cSTakashi Iwai {
369c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
370c631d03cSTakashi Iwai 
371c631d03cSTakashi Iwai 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
372c631d03cSTakashi Iwai 	if (!atomic_read(&dpcm->running))
373c631d03cSTakashi Iwai 		return HRTIMER_NORESTART;
374b03bbbe0SThomas Gleixner 	/*
375b03bbbe0SThomas Gleixner 	 * In cases of XRUN and draining, this calls .trigger to stop PCM
376b03bbbe0SThomas Gleixner 	 * substream.
377b03bbbe0SThomas Gleixner 	 */
378b03bbbe0SThomas Gleixner 	snd_pcm_period_elapsed(dpcm->substream);
379b03bbbe0SThomas Gleixner 	if (!atomic_read(&dpcm->running))
380b03bbbe0SThomas Gleixner 		return HRTIMER_NORESTART;
381b03bbbe0SThomas Gleixner 
382c631d03cSTakashi Iwai 	hrtimer_forward_now(timer, dpcm->period_time);
383c631d03cSTakashi Iwai 	return HRTIMER_RESTART;
384c631d03cSTakashi Iwai }
385c631d03cSTakashi Iwai 
386c631d03cSTakashi Iwai static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
387c631d03cSTakashi Iwai {
388c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
389c631d03cSTakashi Iwai 
390c631d03cSTakashi Iwai 	dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
391b03bbbe0SThomas Gleixner 	hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT);
392c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 1);
393c631d03cSTakashi Iwai 	return 0;
394c631d03cSTakashi Iwai }
395c631d03cSTakashi Iwai 
396c631d03cSTakashi Iwai static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
397c631d03cSTakashi Iwai {
398c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
399c631d03cSTakashi Iwai 
400c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
401b03bbbe0SThomas Gleixner 	if (!hrtimer_callback_running(&dpcm->timer))
402c631d03cSTakashi Iwai 		hrtimer_cancel(&dpcm->timer);
403c631d03cSTakashi Iwai 	return 0;
404c631d03cSTakashi Iwai }
405c631d03cSTakashi Iwai 
406c631d03cSTakashi Iwai static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
407c631d03cSTakashi Iwai {
408d5dbbe65STakashi Iwai 	hrtimer_cancel(&dpcm->timer);
409c631d03cSTakashi Iwai }
410c631d03cSTakashi Iwai 
411c631d03cSTakashi Iwai static snd_pcm_uframes_t
412c631d03cSTakashi Iwai dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
413c631d03cSTakashi Iwai {
414c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
415c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
416c631d03cSTakashi Iwai 	u64 delta;
417c631d03cSTakashi Iwai 	u32 pos;
418c631d03cSTakashi Iwai 
419c631d03cSTakashi Iwai 	delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
420c631d03cSTakashi Iwai 			       dpcm->base_time);
421c631d03cSTakashi Iwai 	delta = div_u64(delta * runtime->rate + 999999, 1000000);
422c631d03cSTakashi Iwai 	div_u64_rem(delta, runtime->buffer_size, &pos);
423c631d03cSTakashi Iwai 	return pos;
424c631d03cSTakashi Iwai }
425c631d03cSTakashi Iwai 
426c631d03cSTakashi Iwai static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
427c631d03cSTakashi Iwai {
428c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
429c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
430c631d03cSTakashi Iwai 	unsigned int period, rate;
431c631d03cSTakashi Iwai 	long sec;
432c631d03cSTakashi Iwai 	unsigned long nsecs;
433c631d03cSTakashi Iwai 
434c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
435c631d03cSTakashi Iwai 	period = runtime->period_size;
436c631d03cSTakashi Iwai 	rate = runtime->rate;
437c631d03cSTakashi Iwai 	sec = period / rate;
438c631d03cSTakashi Iwai 	period %= rate;
439c631d03cSTakashi Iwai 	nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
440c631d03cSTakashi Iwai 	dpcm->period_time = ktime_set(sec, nsecs);
441c631d03cSTakashi Iwai 
442c631d03cSTakashi Iwai 	return 0;
443c631d03cSTakashi Iwai }
444c631d03cSTakashi Iwai 
445c631d03cSTakashi Iwai static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
446c631d03cSTakashi Iwai {
447c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
448c631d03cSTakashi Iwai 
449c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
450c631d03cSTakashi Iwai 	if (!dpcm)
451c631d03cSTakashi Iwai 		return -ENOMEM;
452c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
453b03bbbe0SThomas Gleixner 	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
454c631d03cSTakashi Iwai 	dpcm->timer.function = dummy_hrtimer_callback;
455c631d03cSTakashi Iwai 	dpcm->substream = substream;
456c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
457c631d03cSTakashi Iwai 	return 0;
458c631d03cSTakashi Iwai }
459c631d03cSTakashi Iwai 
460c631d03cSTakashi Iwai static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
461c631d03cSTakashi Iwai {
462c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
463c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
464c631d03cSTakashi Iwai 	kfree(dpcm);
465c631d03cSTakashi Iwai }
466c631d03cSTakashi Iwai 
467d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_hrtimer_ops = {
468c631d03cSTakashi Iwai 	.create =	dummy_hrtimer_create,
469c631d03cSTakashi Iwai 	.free =		dummy_hrtimer_free,
470c631d03cSTakashi Iwai 	.prepare =	dummy_hrtimer_prepare,
471c631d03cSTakashi Iwai 	.start =	dummy_hrtimer_start,
472c631d03cSTakashi Iwai 	.stop =		dummy_hrtimer_stop,
473c631d03cSTakashi Iwai 	.pointer =	dummy_hrtimer_pointer,
474c631d03cSTakashi Iwai };
475c631d03cSTakashi Iwai 
476c631d03cSTakashi Iwai #endif /* CONFIG_HIGH_RES_TIMERS */
477c631d03cSTakashi Iwai 
478c631d03cSTakashi Iwai /*
479c631d03cSTakashi Iwai  * PCM interface
480c631d03cSTakashi Iwai  */
481c631d03cSTakashi Iwai 
482c631d03cSTakashi Iwai static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
483c631d03cSTakashi Iwai {
484c631d03cSTakashi Iwai 	switch (cmd) {
485c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_START:
486c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
487ddce57a6STakashi Iwai 		return get_dummy_ops(substream)->start(substream);
488c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_STOP:
489c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
490ddce57a6STakashi Iwai 		return get_dummy_ops(substream)->stop(substream);
491c631d03cSTakashi Iwai 	}
492c631d03cSTakashi Iwai 	return -EINVAL;
493c631d03cSTakashi Iwai }
494c631d03cSTakashi Iwai 
495c631d03cSTakashi Iwai static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
496c631d03cSTakashi Iwai {
497ddce57a6STakashi Iwai 	return get_dummy_ops(substream)->prepare(substream);
498c631d03cSTakashi Iwai }
499c631d03cSTakashi Iwai 
500c631d03cSTakashi Iwai static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
501c631d03cSTakashi Iwai {
502ddce57a6STakashi Iwai 	return get_dummy_ops(substream)->pointer(substream);
503c631d03cSTakashi Iwai }
504c631d03cSTakashi Iwai 
505b6c0b715SBhumika Goyal static const struct snd_pcm_hardware dummy_pcm_hardware = {
506c631d03cSTakashi Iwai 	.info =			(SNDRV_PCM_INFO_MMAP |
507c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_INTERLEAVED |
508c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_RESUME |
509c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_MMAP_VALID),
510c631d03cSTakashi Iwai 	.formats =		USE_FORMATS,
511c631d03cSTakashi Iwai 	.rates =		USE_RATE,
512c631d03cSTakashi Iwai 	.rate_min =		USE_RATE_MIN,
513c631d03cSTakashi Iwai 	.rate_max =		USE_RATE_MAX,
514c631d03cSTakashi Iwai 	.channels_min =		USE_CHANNELS_MIN,
515c631d03cSTakashi Iwai 	.channels_max =		USE_CHANNELS_MAX,
516c631d03cSTakashi Iwai 	.buffer_bytes_max =	MAX_BUFFER_SIZE,
517d5e1ca05SJaroslav Kysela 	.period_bytes_min =	MIN_PERIOD_SIZE,
518c631d03cSTakashi Iwai 	.period_bytes_max =	MAX_PERIOD_SIZE,
519c631d03cSTakashi Iwai 	.periods_min =		USE_PERIODS_MIN,
520c631d03cSTakashi Iwai 	.periods_max =		USE_PERIODS_MAX,
521c631d03cSTakashi Iwai 	.fifo_size =		0,
522c631d03cSTakashi Iwai };
523c631d03cSTakashi Iwai 
524c631d03cSTakashi Iwai static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
5254a4d2cfdSTakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
5261da177e4SLinus Torvalds {
527a68c4d11STakashi Iwai 	if (fake_buffer) {
528a68c4d11STakashi Iwai 		/* runtime->dma_bytes has to be set manually to allow mmap */
529a68c4d11STakashi Iwai 		substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
530a68c4d11STakashi Iwai 		return 0;
531a68c4d11STakashi Iwai 	}
532a68c4d11STakashi Iwai 	return 0;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
535c631d03cSTakashi Iwai static int dummy_pcm_open(struct snd_pcm_substream *substream)
536c8eb6ba1STakashi Iwai {
537c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
538d64e7f7cSTakashi Iwai 	const struct dummy_model *model = dummy->model;
5394a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
540ddce57a6STakashi Iwai 	const struct dummy_timer_ops *ops;
5411da177e4SLinus Torvalds 	int err;
5421da177e4SLinus Torvalds 
543ddce57a6STakashi Iwai 	ops = &dummy_systimer_ops;
544c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
545c631d03cSTakashi Iwai 	if (hrtimer)
546ddce57a6STakashi Iwai 		ops = &dummy_hrtimer_ops;
547c631d03cSTakashi Iwai #endif
548c631d03cSTakashi Iwai 
549ddce57a6STakashi Iwai 	err = ops->create(substream);
550c631d03cSTakashi Iwai 	if (err < 0)
551c631d03cSTakashi Iwai 		return err;
552ddce57a6STakashi Iwai 	get_dummy_ops(substream) = ops;
553c631d03cSTakashi Iwai 
554d5e1ca05SJaroslav Kysela 	runtime->hw = dummy->pcm_hw;
5551da177e4SLinus Torvalds 	if (substream->pcm->device & 1) {
5561da177e4SLinus Torvalds 		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
5571da177e4SLinus Torvalds 		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 	if (substream->pcm->device & 2)
560c631d03cSTakashi Iwai 		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
561c631d03cSTakashi Iwai 				      SNDRV_PCM_INFO_MMAP_VALID);
562c631d03cSTakashi Iwai 
563d5e1ca05SJaroslav Kysela 	if (model == NULL)
564d5e1ca05SJaroslav Kysela 		return 0;
565d5e1ca05SJaroslav Kysela 
566d5e1ca05SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567d5e1ca05SJaroslav Kysela 		if (model->playback_constraints)
568d5e1ca05SJaroslav Kysela 			err = model->playback_constraints(substream->runtime);
569d5e1ca05SJaroslav Kysela 	} else {
570d5e1ca05SJaroslav Kysela 		if (model->capture_constraints)
571d5e1ca05SJaroslav Kysela 			err = model->capture_constraints(substream->runtime);
572d5e1ca05SJaroslav Kysela 	}
573c631d03cSTakashi Iwai 	if (err < 0) {
574ddce57a6STakashi Iwai 		get_dummy_ops(substream)->free(substream);
5751da177e4SLinus Torvalds 		return err;
576c631d03cSTakashi Iwai 	}
5771da177e4SLinus Torvalds 	return 0;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
580c631d03cSTakashi Iwai static int dummy_pcm_close(struct snd_pcm_substream *substream)
5811da177e4SLinus Torvalds {
582ddce57a6STakashi Iwai 	get_dummy_ops(substream)->free(substream);
5831da177e4SLinus Torvalds 	return 0;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
586a68c4d11STakashi Iwai /*
587a68c4d11STakashi Iwai  * dummy buffer handling
588a68c4d11STakashi Iwai  */
589a68c4d11STakashi Iwai 
590a68c4d11STakashi Iwai static void *dummy_page[2];
591a68c4d11STakashi Iwai 
592a68c4d11STakashi Iwai static void free_fake_buffer(void)
593a68c4d11STakashi Iwai {
594a68c4d11STakashi Iwai 	if (fake_buffer) {
595a68c4d11STakashi Iwai 		int i;
596a68c4d11STakashi Iwai 		for (i = 0; i < 2; i++)
597a68c4d11STakashi Iwai 			if (dummy_page[i]) {
598a68c4d11STakashi Iwai 				free_page((unsigned long)dummy_page[i]);
599a68c4d11STakashi Iwai 				dummy_page[i] = NULL;
600a68c4d11STakashi Iwai 			}
601a68c4d11STakashi Iwai 	}
602a68c4d11STakashi Iwai }
603a68c4d11STakashi Iwai 
604a68c4d11STakashi Iwai static int alloc_fake_buffer(void)
605a68c4d11STakashi Iwai {
606a68c4d11STakashi Iwai 	int i;
607a68c4d11STakashi Iwai 
608a68c4d11STakashi Iwai 	if (!fake_buffer)
609a68c4d11STakashi Iwai 		return 0;
610a68c4d11STakashi Iwai 	for (i = 0; i < 2; i++) {
611a68c4d11STakashi Iwai 		dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
612a68c4d11STakashi Iwai 		if (!dummy_page[i]) {
613a68c4d11STakashi Iwai 			free_fake_buffer();
614a68c4d11STakashi Iwai 			return -ENOMEM;
615a68c4d11STakashi Iwai 		}
616a68c4d11STakashi Iwai 	}
617a68c4d11STakashi Iwai 	return 0;
618a68c4d11STakashi Iwai }
619a68c4d11STakashi Iwai 
620a68c4d11STakashi Iwai static int dummy_pcm_copy(struct snd_pcm_substream *substream,
621d53611d2STakashi Iwai 			  int channel, unsigned long pos,
622d53611d2STakashi Iwai 			  void __user *dst, unsigned long bytes)
623d53611d2STakashi Iwai {
624d53611d2STakashi Iwai 	return 0; /* do nothing */
625d53611d2STakashi Iwai }
626d53611d2STakashi Iwai 
627d53611d2STakashi Iwai static int dummy_pcm_copy_kernel(struct snd_pcm_substream *substream,
628d53611d2STakashi Iwai 				 int channel, unsigned long pos,
629d53611d2STakashi Iwai 				 void *dst, unsigned long bytes)
630a68c4d11STakashi Iwai {
631a68c4d11STakashi Iwai 	return 0; /* do nothing */
632a68c4d11STakashi Iwai }
633a68c4d11STakashi Iwai 
634a68c4d11STakashi Iwai static int dummy_pcm_silence(struct snd_pcm_substream *substream,
635d53611d2STakashi Iwai 			     int channel, unsigned long pos,
636d53611d2STakashi Iwai 			     unsigned long bytes)
637a68c4d11STakashi Iwai {
638a68c4d11STakashi Iwai 	return 0; /* do nothing */
639a68c4d11STakashi Iwai }
640a68c4d11STakashi Iwai 
641a68c4d11STakashi Iwai static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
642a68c4d11STakashi Iwai 				   unsigned long offset)
643a68c4d11STakashi Iwai {
644a68c4d11STakashi Iwai 	return virt_to_page(dummy_page[substream->stream]); /* the same page */
645a68c4d11STakashi Iwai }
646a68c4d11STakashi Iwai 
6471da7f0c5STakashi Iwai static const struct snd_pcm_ops dummy_pcm_ops = {
648c631d03cSTakashi Iwai 	.open =		dummy_pcm_open,
649c631d03cSTakashi Iwai 	.close =	dummy_pcm_close,
650c631d03cSTakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
651c631d03cSTakashi Iwai 	.prepare =	dummy_pcm_prepare,
652c631d03cSTakashi Iwai 	.trigger =	dummy_pcm_trigger,
653c631d03cSTakashi Iwai 	.pointer =	dummy_pcm_pointer,
6541da177e4SLinus Torvalds };
6551da177e4SLinus Torvalds 
6561da7f0c5STakashi Iwai static const struct snd_pcm_ops dummy_pcm_ops_no_buf = {
657a68c4d11STakashi Iwai 	.open =		dummy_pcm_open,
658a68c4d11STakashi Iwai 	.close =	dummy_pcm_close,
659a68c4d11STakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
660a68c4d11STakashi Iwai 	.prepare =	dummy_pcm_prepare,
661a68c4d11STakashi Iwai 	.trigger =	dummy_pcm_trigger,
662a68c4d11STakashi Iwai 	.pointer =	dummy_pcm_pointer,
663d53611d2STakashi Iwai 	.copy_user =	dummy_pcm_copy,
664d53611d2STakashi Iwai 	.copy_kernel =	dummy_pcm_copy_kernel,
665d53611d2STakashi Iwai 	.fill_silence =	dummy_pcm_silence,
666a68c4d11STakashi Iwai 	.page =		dummy_pcm_page,
667a68c4d11STakashi Iwai };
668a68c4d11STakashi Iwai 
669fbbb01a1SBill Pemberton static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
670788c6043SPrarit Bhargava 			      int substreams)
6711da177e4SLinus Torvalds {
6724a4d2cfdSTakashi Iwai 	struct snd_pcm *pcm;
6731da7f0c5STakashi Iwai 	const struct snd_pcm_ops *ops;
6741da177e4SLinus Torvalds 	int err;
6751da177e4SLinus Torvalds 
6761a11cb64SJaroslav Kysela 	err = snd_pcm_new(dummy->card, "Dummy PCM", device,
6771a11cb64SJaroslav Kysela 			       substreams, substreams, &pcm);
6781a11cb64SJaroslav Kysela 	if (err < 0)
6791da177e4SLinus Torvalds 		return err;
6806e65c1ccSTakashi Iwai 	dummy->pcm = pcm;
681a68c4d11STakashi Iwai 	if (fake_buffer)
682a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops_no_buf;
683a68c4d11STakashi Iwai 	else
684a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops;
685a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
686a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
6871da177e4SLinus Torvalds 	pcm->private_data = dummy;
6881da177e4SLinus Torvalds 	pcm->info_flags = 0;
6891da177e4SLinus Torvalds 	strcpy(pcm->name, "Dummy PCM");
690a68c4d11STakashi Iwai 	if (!fake_buffer) {
691ef1545b8STakashi Iwai 		snd_pcm_set_managed_buffer_all(pcm,
692a68c4d11STakashi Iwai 			SNDRV_DMA_TYPE_CONTINUOUS,
6938fd9da75STakashi Iwai 			NULL,
6941da177e4SLinus Torvalds 			0, 64*1024);
695a68c4d11STakashi Iwai 	}
6961da177e4SLinus Torvalds 	return 0;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6999b151fecSTakashi Iwai /*
7009b151fecSTakashi Iwai  * mixer interface
7019b151fecSTakashi Iwai  */
7029b151fecSTakashi Iwai 
7031da177e4SLinus Torvalds #define DUMMY_VOLUME(xname, xindex, addr) \
704fb567a8eSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
705fb567a8eSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
706fb567a8eSTakashi Iwai   .name = xname, .index = xindex, \
7071da177e4SLinus Torvalds   .info = snd_dummy_volume_info, \
7081da177e4SLinus Torvalds   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
709fb567a8eSTakashi Iwai   .private_value = addr, \
710fb567a8eSTakashi Iwai   .tlv = { .p = db_scale_dummy } }
7111da177e4SLinus Torvalds 
7124a4d2cfdSTakashi Iwai static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
7134a4d2cfdSTakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7161da177e4SLinus Torvalds 	uinfo->count = 2;
7171da177e4SLinus Torvalds 	uinfo->value.integer.min = -50;
7181da177e4SLinus Torvalds 	uinfo->value.integer.max = 100;
7191da177e4SLinus Torvalds 	return 0;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7224a4d2cfdSTakashi Iwai static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
7234a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7241da177e4SLinus Torvalds {
7254a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7261da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7271da177e4SLinus Torvalds 
728c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7291da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
7301da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
731c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7321da177e4SLinus Torvalds 	return 0;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7354a4d2cfdSTakashi Iwai static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
7364a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7371da177e4SLinus Torvalds {
7384a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7391da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7401da177e4SLinus Torvalds 	int left, right;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0];
7431da177e4SLinus Torvalds 	if (left < -50)
7441da177e4SLinus Torvalds 		left = -50;
7451da177e4SLinus Torvalds 	if (left > 100)
7461da177e4SLinus Torvalds 		left = 100;
7471da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1];
7481da177e4SLinus Torvalds 	if (right < -50)
7491da177e4SLinus Torvalds 		right = -50;
7501da177e4SLinus Torvalds 	if (right > 100)
7511da177e4SLinus Torvalds 		right = 100;
752c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7531da177e4SLinus Torvalds 	change = dummy->mixer_volume[addr][0] != left ||
7541da177e4SLinus Torvalds 	         dummy->mixer_volume[addr][1] != right;
7551da177e4SLinus Torvalds 	dummy->mixer_volume[addr][0] = left;
7561da177e4SLinus Torvalds 	dummy->mixer_volume[addr][1] = right;
757c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7581da177e4SLinus Torvalds 	return change;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7610cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
762fb567a8eSTakashi Iwai 
7631da177e4SLinus Torvalds #define DUMMY_CAPSRC(xname, xindex, addr) \
7641da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
7651da177e4SLinus Torvalds   .info = snd_dummy_capsrc_info, \
7661da177e4SLinus Torvalds   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
7671da177e4SLinus Torvalds   .private_value = addr }
7681da177e4SLinus Torvalds 
769a5ce8890STakashi Iwai #define snd_dummy_capsrc_info	snd_ctl_boolean_stereo_info
7701da177e4SLinus Torvalds 
7714a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
7724a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7731da177e4SLinus Torvalds {
7744a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7751da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7761da177e4SLinus Torvalds 
777c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7781da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
7791da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
780c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7811da177e4SLinus Torvalds 	return 0;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7844a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7851da177e4SLinus Torvalds {
7864a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7871da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7881da177e4SLinus Torvalds 	int left, right;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0] & 1;
7911da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1] & 1;
792c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7931da177e4SLinus Torvalds 	change = dummy->capture_source[addr][0] != left &&
7941da177e4SLinus Torvalds 	         dummy->capture_source[addr][1] != right;
7951da177e4SLinus Torvalds 	dummy->capture_source[addr][0] = left;
7961da177e4SLinus Torvalds 	dummy->capture_source[addr][1] = right;
797c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7981da177e4SLinus Torvalds 	return change;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
80116e43467SClemens Ladisch static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
80216e43467SClemens Ladisch 				struct snd_ctl_elem_info *info)
80316e43467SClemens Ladisch {
804a4a1b737SColin Ian King 	static const char *const names[] = { "None", "CD Player" };
80516e43467SClemens Ladisch 
80616e43467SClemens Ladisch 	return snd_ctl_enum_info(info, 1, 2, names);
80716e43467SClemens Ladisch }
80816e43467SClemens Ladisch 
80916e43467SClemens Ladisch static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
81016e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
81116e43467SClemens Ladisch {
81216e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
81316e43467SClemens Ladisch 
81416e43467SClemens Ladisch 	value->value.enumerated.item[0] = dummy->iobox;
81516e43467SClemens Ladisch 	return 0;
81616e43467SClemens Ladisch }
81716e43467SClemens Ladisch 
81816e43467SClemens Ladisch static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
81916e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
82016e43467SClemens Ladisch {
82116e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
82216e43467SClemens Ladisch 	int changed;
82316e43467SClemens Ladisch 
82416e43467SClemens Ladisch 	if (value->value.enumerated.item[0] > 1)
82516e43467SClemens Ladisch 		return -EINVAL;
82616e43467SClemens Ladisch 
82716e43467SClemens Ladisch 	changed = value->value.enumerated.item[0] != dummy->iobox;
82816e43467SClemens Ladisch 	if (changed) {
82916e43467SClemens Ladisch 		dummy->iobox = value->value.enumerated.item[0];
83016e43467SClemens Ladisch 
83116e43467SClemens Ladisch 		if (dummy->iobox) {
83216e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access &=
83316e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83416e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access &=
83516e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83616e43467SClemens Ladisch 		} else {
83716e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access |=
83816e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83916e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access |=
84016e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
84116e43467SClemens Ladisch 		}
84216e43467SClemens Ladisch 
84316e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
84416e43467SClemens Ladisch 			       &dummy->cd_volume_ctl->id);
84516e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
84616e43467SClemens Ladisch 			       &dummy->cd_switch_ctl->id);
84716e43467SClemens Ladisch 	}
84816e43467SClemens Ladisch 
84916e43467SClemens Ladisch 	return changed;
85016e43467SClemens Ladisch }
85116e43467SClemens Ladisch 
8522eccd408STakashi Iwai static const struct snd_kcontrol_new snd_dummy_controls[] = {
8531da177e4SLinus Torvalds DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
8541da177e4SLinus Torvalds DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
8551da177e4SLinus Torvalds DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
856e05d6964STakashi Iwai DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
8571da177e4SLinus Torvalds DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
858e05d6964STakashi Iwai DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
8591da177e4SLinus Torvalds DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
860e05d6964STakashi Iwai DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
8611da177e4SLinus Torvalds DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
86216e43467SClemens Ladisch DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
86316e43467SClemens Ladisch {
86416e43467SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
86516e43467SClemens Ladisch 	.name  = "External I/O Box",
86616e43467SClemens Ladisch 	.info  = snd_dummy_iobox_info,
86716e43467SClemens Ladisch 	.get   = snd_dummy_iobox_get,
86816e43467SClemens Ladisch 	.put   = snd_dummy_iobox_put,
86916e43467SClemens Ladisch },
8701da177e4SLinus Torvalds };
8711da177e4SLinus Torvalds 
872fbbb01a1SBill Pemberton static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
8731da177e4SLinus Torvalds {
8744a4d2cfdSTakashi Iwai 	struct snd_card *card = dummy->card;
87516e43467SClemens Ladisch 	struct snd_kcontrol *kcontrol;
8761da177e4SLinus Torvalds 	unsigned int idx;
8771da177e4SLinus Torvalds 	int err;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	spin_lock_init(&dummy->mixer_lock);
8801da177e4SLinus Torvalds 	strcpy(card->mixername, "Dummy Mixer");
88116e43467SClemens Ladisch 	dummy->iobox = 1;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
88416e43467SClemens Ladisch 		kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
88516e43467SClemens Ladisch 		err = snd_ctl_add(card, kcontrol);
8861a11cb64SJaroslav Kysela 		if (err < 0)
8871da177e4SLinus Torvalds 			return err;
88816e43467SClemens Ladisch 		if (!strcmp(kcontrol->id.name, "CD Volume"))
88916e43467SClemens Ladisch 			dummy->cd_volume_ctl = kcontrol;
89016e43467SClemens Ladisch 		else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
89116e43467SClemens Ladisch 			dummy->cd_switch_ctl = kcontrol;
89216e43467SClemens Ladisch 
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 	return 0;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
897129a4c9fSTakashi Iwai #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
8989b151fecSTakashi Iwai /*
8999b151fecSTakashi Iwai  * proc interface
9009b151fecSTakashi Iwai  */
901d5e1ca05SJaroslav Kysela static void print_formats(struct snd_dummy *dummy,
902d5e1ca05SJaroslav Kysela 			  struct snd_info_buffer *buffer)
9039b151fecSTakashi Iwai {
904c5f72ef1STakashi Iwai 	snd_pcm_format_t i;
9059b151fecSTakashi Iwai 
906c5f72ef1STakashi Iwai 	pcm_for_each_format(i) {
907c5f72ef1STakashi Iwai 		if (dummy->pcm_hw.formats & pcm_format_to_bits(i))
9089b151fecSTakashi Iwai 			snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
9099b151fecSTakashi Iwai 	}
9109b151fecSTakashi Iwai }
9119b151fecSTakashi Iwai 
912d5e1ca05SJaroslav Kysela static void print_rates(struct snd_dummy *dummy,
913d5e1ca05SJaroslav Kysela 			struct snd_info_buffer *buffer)
9149b151fecSTakashi Iwai {
915d64e7f7cSTakashi Iwai 	static const int rates[] = {
9169b151fecSTakashi Iwai 		5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
9179b151fecSTakashi Iwai 		64000, 88200, 96000, 176400, 192000,
9189b151fecSTakashi Iwai 	};
9199b151fecSTakashi Iwai 	int i;
9209b151fecSTakashi Iwai 
921d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
9229b151fecSTakashi Iwai 		snd_iprintf(buffer, " continuous");
923d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
9249b151fecSTakashi Iwai 		snd_iprintf(buffer, " knot");
9259b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(rates); i++)
926d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.rates & (1 << i))
9279b151fecSTakashi Iwai 			snd_iprintf(buffer, " %d", rates[i]);
9289b151fecSTakashi Iwai }
9299b151fecSTakashi Iwai 
930d5e1ca05SJaroslav Kysela #define get_dummy_int_ptr(dummy, ofs) \
931d5e1ca05SJaroslav Kysela 	(unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
932d5e1ca05SJaroslav Kysela #define get_dummy_ll_ptr(dummy, ofs) \
933d5e1ca05SJaroslav Kysela 	(unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
9349b151fecSTakashi Iwai 
9359b151fecSTakashi Iwai struct dummy_hw_field {
9369b151fecSTakashi Iwai 	const char *name;
9379b151fecSTakashi Iwai 	const char *format;
9389b151fecSTakashi Iwai 	unsigned int offset;
9399b151fecSTakashi Iwai 	unsigned int size;
9409b151fecSTakashi Iwai };
9419b151fecSTakashi Iwai #define FIELD_ENTRY(item, fmt) {		   \
9429b151fecSTakashi Iwai 	.name = #item,				   \
9439b151fecSTakashi Iwai 	.format = fmt,				   \
9449b151fecSTakashi Iwai 	.offset = offsetof(struct snd_pcm_hardware, item), \
9459b151fecSTakashi Iwai 	.size = sizeof(dummy_pcm_hardware.item) }
9469b151fecSTakashi Iwai 
947d64e7f7cSTakashi Iwai static const struct dummy_hw_field fields[] = {
9489b151fecSTakashi Iwai 	FIELD_ENTRY(formats, "%#llx"),
9499b151fecSTakashi Iwai 	FIELD_ENTRY(rates, "%#x"),
9509b151fecSTakashi Iwai 	FIELD_ENTRY(rate_min, "%d"),
9519b151fecSTakashi Iwai 	FIELD_ENTRY(rate_max, "%d"),
9529b151fecSTakashi Iwai 	FIELD_ENTRY(channels_min, "%d"),
9539b151fecSTakashi Iwai 	FIELD_ENTRY(channels_max, "%d"),
9549b151fecSTakashi Iwai 	FIELD_ENTRY(buffer_bytes_max, "%ld"),
9559b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_min, "%ld"),
9569b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_max, "%ld"),
9579b151fecSTakashi Iwai 	FIELD_ENTRY(periods_min, "%d"),
9589b151fecSTakashi Iwai 	FIELD_ENTRY(periods_max, "%d"),
9599b151fecSTakashi Iwai };
9609b151fecSTakashi Iwai 
9619b151fecSTakashi Iwai static void dummy_proc_read(struct snd_info_entry *entry,
9629b151fecSTakashi Iwai 			    struct snd_info_buffer *buffer)
9639b151fecSTakashi Iwai {
964d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9659b151fecSTakashi Iwai 	int i;
9669b151fecSTakashi Iwai 
9679b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(fields); i++) {
9689b151fecSTakashi Iwai 		snd_iprintf(buffer, "%s ", fields[i].name);
9699b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
9709b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
971d5e1ca05SJaroslav Kysela 				*get_dummy_int_ptr(dummy, fields[i].offset));
9729b151fecSTakashi Iwai 		else
9739b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
974d5e1ca05SJaroslav Kysela 				*get_dummy_ll_ptr(dummy, fields[i].offset));
9759b151fecSTakashi Iwai 		if (!strcmp(fields[i].name, "formats"))
976d5e1ca05SJaroslav Kysela 			print_formats(dummy, buffer);
9779b151fecSTakashi Iwai 		else if (!strcmp(fields[i].name, "rates"))
978d5e1ca05SJaroslav Kysela 			print_rates(dummy, buffer);
9799b151fecSTakashi Iwai 		snd_iprintf(buffer, "\n");
9809b151fecSTakashi Iwai 	}
9819b151fecSTakashi Iwai }
9829b151fecSTakashi Iwai 
9839b151fecSTakashi Iwai static void dummy_proc_write(struct snd_info_entry *entry,
9849b151fecSTakashi Iwai 			     struct snd_info_buffer *buffer)
9859b151fecSTakashi Iwai {
986d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9879b151fecSTakashi Iwai 	char line[64];
9889b151fecSTakashi Iwai 
9899b151fecSTakashi Iwai 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
9909b151fecSTakashi Iwai 		char item[20];
9919b151fecSTakashi Iwai 		const char *ptr;
9929b151fecSTakashi Iwai 		unsigned long long val;
9939b151fecSTakashi Iwai 		int i;
9949b151fecSTakashi Iwai 
9959b151fecSTakashi Iwai 		ptr = snd_info_get_str(item, line, sizeof(item));
9969b151fecSTakashi Iwai 		for (i = 0; i < ARRAY_SIZE(fields); i++) {
9979b151fecSTakashi Iwai 			if (!strcmp(item, fields[i].name))
9989b151fecSTakashi Iwai 				break;
9999b151fecSTakashi Iwai 		}
10009b151fecSTakashi Iwai 		if (i >= ARRAY_SIZE(fields))
10019b151fecSTakashi Iwai 			continue;
10029b151fecSTakashi Iwai 		snd_info_get_str(item, ptr, sizeof(item));
1003b785a492SJingoo Han 		if (kstrtoull(item, 0, &val))
10049b151fecSTakashi Iwai 			continue;
10059b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
1006d5e1ca05SJaroslav Kysela 			*get_dummy_int_ptr(dummy, fields[i].offset) = val;
10079b151fecSTakashi Iwai 		else
1008d5e1ca05SJaroslav Kysela 			*get_dummy_ll_ptr(dummy, fields[i].offset) = val;
10099b151fecSTakashi Iwai 	}
10109b151fecSTakashi Iwai }
10119b151fecSTakashi Iwai 
1012fbbb01a1SBill Pemberton static void dummy_proc_init(struct snd_dummy *chip)
10139b151fecSTakashi Iwai {
1014815d808cSTakashi Iwai 	snd_card_rw_proc_new(chip->card, "dummy_pcm", chip,
1015815d808cSTakashi Iwai 			     dummy_proc_read, dummy_proc_write);
10169b151fecSTakashi Iwai }
10179b151fecSTakashi Iwai #else
10189b151fecSTakashi Iwai #define dummy_proc_init(x)
1019129a4c9fSTakashi Iwai #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
10209b151fecSTakashi Iwai 
1021fbbb01a1SBill Pemberton static int snd_dummy_probe(struct platform_device *devptr)
10221da177e4SLinus Torvalds {
10234a4d2cfdSTakashi Iwai 	struct snd_card *card;
10244a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy;
1025d64e7f7cSTakashi Iwai 	const struct dummy_model *m = NULL, **mdl;
10261da177e4SLinus Torvalds 	int idx, err;
10276e65c1ccSTakashi Iwai 	int dev = devptr->id;
10281da177e4SLinus Torvalds 
10295872f3f6STakashi Iwai 	err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1030bd7dd77cSTakashi Iwai 			   sizeof(struct snd_dummy), &card);
1031bd7dd77cSTakashi Iwai 	if (err < 0)
1032bd7dd77cSTakashi Iwai 		return err;
10334a4d2cfdSTakashi Iwai 	dummy = card->private_data;
10341da177e4SLinus Torvalds 	dummy->card = card;
1035d5e1ca05SJaroslav Kysela 	for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
1036d5e1ca05SJaroslav Kysela 		if (strcmp(model[dev], (*mdl)->name) == 0) {
1037d5e1ca05SJaroslav Kysela 			printk(KERN_INFO
1038d5e1ca05SJaroslav Kysela 				"snd-dummy: Using model '%s' for card %i\n",
1039d5e1ca05SJaroslav Kysela 				(*mdl)->name, card->number);
1040d5e1ca05SJaroslav Kysela 			m = dummy->model = *mdl;
1041d5e1ca05SJaroslav Kysela 			break;
1042d5e1ca05SJaroslav Kysela 		}
1043d5e1ca05SJaroslav Kysela 	}
10441da177e4SLinus Torvalds 	for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
10451da177e4SLinus Torvalds 		if (pcm_substreams[dev] < 1)
10461da177e4SLinus Torvalds 			pcm_substreams[dev] = 1;
10471da177e4SLinus Torvalds 		if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
10481da177e4SLinus Torvalds 			pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
10491a11cb64SJaroslav Kysela 		err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
10501a11cb64SJaroslav Kysela 		if (err < 0)
10511da177e4SLinus Torvalds 			goto __nodev;
10521da177e4SLinus Torvalds 	}
1053d5e1ca05SJaroslav Kysela 
1054d5e1ca05SJaroslav Kysela 	dummy->pcm_hw = dummy_pcm_hardware;
1055d5e1ca05SJaroslav Kysela 	if (m) {
1056d5e1ca05SJaroslav Kysela 		if (m->formats)
1057d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.formats = m->formats;
1058d5e1ca05SJaroslav Kysela 		if (m->buffer_bytes_max)
1059d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1060d5e1ca05SJaroslav Kysela 		if (m->period_bytes_min)
1061d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1062d5e1ca05SJaroslav Kysela 		if (m->period_bytes_max)
1063d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1064d5e1ca05SJaroslav Kysela 		if (m->periods_min)
1065d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_min = m->periods_min;
1066d5e1ca05SJaroslav Kysela 		if (m->periods_max)
1067d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_max = m->periods_max;
1068d5e1ca05SJaroslav Kysela 		if (m->rates)
1069d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rates = m->rates;
1070d5e1ca05SJaroslav Kysela 		if (m->rate_min)
1071d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_min = m->rate_min;
1072d5e1ca05SJaroslav Kysela 		if (m->rate_max)
1073d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_max = m->rate_max;
1074d5e1ca05SJaroslav Kysela 		if (m->channels_min)
1075d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_min = m->channels_min;
1076d5e1ca05SJaroslav Kysela 		if (m->channels_max)
1077d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_max = m->channels_max;
1078d5e1ca05SJaroslav Kysela 	}
1079d5e1ca05SJaroslav Kysela 
10801a11cb64SJaroslav Kysela 	err = snd_card_dummy_new_mixer(dummy);
10811a11cb64SJaroslav Kysela 	if (err < 0)
10821da177e4SLinus Torvalds 		goto __nodev;
10831da177e4SLinus Torvalds 	strcpy(card->driver, "Dummy");
10841da177e4SLinus Torvalds 	strcpy(card->shortname, "Dummy");
10851da177e4SLinus Torvalds 	sprintf(card->longname, "Dummy %i", dev + 1);
108616dab54bSTakashi Iwai 
10879b151fecSTakashi Iwai 	dummy_proc_init(dummy);
10889b151fecSTakashi Iwai 
10891a11cb64SJaroslav Kysela 	err = snd_card_register(card);
10901a11cb64SJaroslav Kysela 	if (err == 0) {
10916e65c1ccSTakashi Iwai 		platform_set_drvdata(devptr, card);
10921da177e4SLinus Torvalds 		return 0;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds       __nodev:
10951da177e4SLinus Torvalds 	snd_card_free(card);
10961da177e4SLinus Torvalds 	return err;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
1099fbbb01a1SBill Pemberton static int snd_dummy_remove(struct platform_device *devptr)
11006e65c1ccSTakashi Iwai {
11016e65c1ccSTakashi Iwai 	snd_card_free(platform_get_drvdata(devptr));
11026e65c1ccSTakashi Iwai 	return 0;
11036e65c1ccSTakashi Iwai }
11046e65c1ccSTakashi Iwai 
1105d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP
1106284e7ca7STakashi Iwai static int snd_dummy_suspend(struct device *pdev)
11076e65c1ccSTakashi Iwai {
1108284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11096e65c1ccSTakashi Iwai 
11106e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
11116e65c1ccSTakashi Iwai 	return 0;
11126e65c1ccSTakashi Iwai }
11136e65c1ccSTakashi Iwai 
1114284e7ca7STakashi Iwai static int snd_dummy_resume(struct device *pdev)
11156e65c1ccSTakashi Iwai {
1116284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11176e65c1ccSTakashi Iwai 
11186e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
11196e65c1ccSTakashi Iwai 	return 0;
11206e65c1ccSTakashi Iwai }
1121284e7ca7STakashi Iwai 
1122284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
1123284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	&snd_dummy_pm
1124284e7ca7STakashi Iwai #else
1125284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	NULL
11266e65c1ccSTakashi Iwai #endif
11276e65c1ccSTakashi Iwai 
11286e65c1ccSTakashi Iwai #define SND_DUMMY_DRIVER	"snd_dummy"
11296e65c1ccSTakashi Iwai 
11306e65c1ccSTakashi Iwai static struct platform_driver snd_dummy_driver = {
11316e65c1ccSTakashi Iwai 	.probe		= snd_dummy_probe,
1132fbbb01a1SBill Pemberton 	.remove		= snd_dummy_remove,
11336e65c1ccSTakashi Iwai 	.driver		= {
11348bf01d8aSTakashi Iwai 		.name	= SND_DUMMY_DRIVER,
1135284e7ca7STakashi Iwai 		.pm	= SND_DUMMY_PM_OPS,
11366e65c1ccSTakashi Iwai 	},
11376e65c1ccSTakashi Iwai };
11386e65c1ccSTakashi Iwai 
1139c12aad6eSRandy Dunlap static void snd_dummy_unregister_all(void)
1140f7a9275dSClemens Ladisch {
1141f7a9275dSClemens Ladisch 	int i;
1142f7a9275dSClemens Ladisch 
1143f7a9275dSClemens Ladisch 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
1144f7a9275dSClemens Ladisch 		platform_device_unregister(devices[i]);
1145f7a9275dSClemens Ladisch 	platform_driver_unregister(&snd_dummy_driver);
1146a68c4d11STakashi Iwai 	free_fake_buffer();
1147f7a9275dSClemens Ladisch }
1148f7a9275dSClemens Ladisch 
11491da177e4SLinus Torvalds static int __init alsa_card_dummy_init(void)
11501da177e4SLinus Torvalds {
11516e65c1ccSTakashi Iwai 	int i, cards, err;
11521da177e4SLinus Torvalds 
11531a11cb64SJaroslav Kysela 	err = platform_driver_register(&snd_dummy_driver);
11541a11cb64SJaroslav Kysela 	if (err < 0)
11556e65c1ccSTakashi Iwai 		return err;
11566e65c1ccSTakashi Iwai 
1157a68c4d11STakashi Iwai 	err = alloc_fake_buffer();
1158a68c4d11STakashi Iwai 	if (err < 0) {
1159a68c4d11STakashi Iwai 		platform_driver_unregister(&snd_dummy_driver);
1160a68c4d11STakashi Iwai 		return err;
1161a68c4d11STakashi Iwai 	}
1162a68c4d11STakashi Iwai 
11636e65c1ccSTakashi Iwai 	cards = 0;
11648278ca8fSTakashi Iwai 	for (i = 0; i < SNDRV_CARDS; i++) {
11656e65c1ccSTakashi Iwai 		struct platform_device *device;
11668278ca8fSTakashi Iwai 		if (! enable[i])
11678278ca8fSTakashi Iwai 			continue;
11686e65c1ccSTakashi Iwai 		device = platform_device_register_simple(SND_DUMMY_DRIVER,
11696e65c1ccSTakashi Iwai 							 i, NULL, 0);
1170a182ee98SRene Herman 		if (IS_ERR(device))
1171a182ee98SRene Herman 			continue;
11727152447dSRene Herman 		if (!platform_get_drvdata(device)) {
11737152447dSRene Herman 			platform_device_unregister(device);
11747152447dSRene Herman 			continue;
11757152447dSRene Herman 		}
1176f7a9275dSClemens Ladisch 		devices[i] = device;
11771da177e4SLinus Torvalds 		cards++;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 	if (!cards) {
11801da177e4SLinus Torvalds #ifdef MODULE
11811da177e4SLinus Torvalds 		printk(KERN_ERR "Dummy soundcard not found or device busy\n");
11821da177e4SLinus Torvalds #endif
1183a182ee98SRene Herman 		snd_dummy_unregister_all();
1184a182ee98SRene Herman 		return -ENODEV;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 	return 0;
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds static void __exit alsa_card_dummy_exit(void)
11901da177e4SLinus Torvalds {
1191f7a9275dSClemens Ladisch 	snd_dummy_unregister_all();
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds module_init(alsa_card_dummy_init)
11951da177e4SLinus Torvalds module_exit(alsa_card_dummy_exit)
1196