xref: /openbmc/linux/sound/drivers/dummy.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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 
291da177e4SLinus Torvalds #define MAX_PCM_DEVICES		4
30b888d1ceSTakashi Iwai #define MAX_PCM_SUBSTREAMS	128
311da177e4SLinus Torvalds #define MAX_MIDI_DEVICES	2
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /* defaults */
341da177e4SLinus Torvalds #define MAX_BUFFER_SIZE		(64*1024)
35d5e1ca05SJaroslav Kysela #define MIN_PERIOD_SIZE		64
362ad5dd8dSJaroslav Kysela #define MAX_PERIOD_SIZE		MAX_BUFFER_SIZE
371da177e4SLinus Torvalds #define USE_FORMATS 		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
381da177e4SLinus Torvalds #define USE_RATE		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
391da177e4SLinus Torvalds #define USE_RATE_MIN		5500
401da177e4SLinus Torvalds #define USE_RATE_MAX		48000
411da177e4SLinus Torvalds #define USE_CHANNELS_MIN 	1
421da177e4SLinus Torvalds #define USE_CHANNELS_MAX 	2
431da177e4SLinus Torvalds #define USE_PERIODS_MIN 	1
441da177e4SLinus Torvalds #define USE_PERIODS_MAX 	1024
45446bc11fSYJ Lee #define USE_MIXER_VOLUME_LEVEL_MIN	-50
46446bc11fSYJ Lee #define USE_MIXER_VOLUME_LEVEL_MAX	100
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
491da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
50a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
51d5e1ca05SJaroslav Kysela static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
521da177e4SLinus Torvalds static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
531da177e4SLinus Torvalds static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
541da177e4SLinus Torvalds //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
55446bc11fSYJ Lee static int mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN;
56446bc11fSYJ Lee static int mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX;
57c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
58a67ff6a5SRusty Russell static bool hrtimer = 1;
59c631d03cSTakashi Iwai #endif
60a67ff6a5SRusty Russell static bool fake_buffer = 1;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
631da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
641da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
651da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
661da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
671da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
68d5e1ca05SJaroslav Kysela module_param_array(model, charp, NULL, 0444);
69d5e1ca05SJaroslav Kysela MODULE_PARM_DESC(model, "Soundcard model.");
701da177e4SLinus Torvalds module_param_array(pcm_devs, int, NULL, 0444);
711da177e4SLinus Torvalds MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
721da177e4SLinus Torvalds module_param_array(pcm_substreams, int, NULL, 0444);
7323aebca4STakashi Iwai MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
741da177e4SLinus Torvalds //module_param_array(midi_devs, int, NULL, 0444);
751da177e4SLinus Torvalds //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
76446bc11fSYJ Lee module_param(mixer_volume_level_min, int, 0444);
77446bc11fSYJ Lee MODULE_PARM_DESC(mixer_volume_level_min, "Minimum mixer volume level for dummy driver. Default: -50");
78446bc11fSYJ Lee module_param(mixer_volume_level_max, int, 0444);
79446bc11fSYJ Lee MODULE_PARM_DESC(mixer_volume_level_max, "Maximum mixer volume level for dummy driver. Default: 100");
80a68c4d11STakashi Iwai module_param(fake_buffer, bool, 0444);
81a68c4d11STakashi Iwai MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
82c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
83ddce57a6STakashi Iwai module_param(hrtimer, bool, 0644);
84c631d03cSTakashi Iwai MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
85c631d03cSTakashi Iwai #endif
861da177e4SLinus Torvalds 
87f7a9275dSClemens Ladisch static struct platform_device *devices[SNDRV_CARDS];
88f7a9275dSClemens Ladisch 
891da177e4SLinus Torvalds #define MIXER_ADDR_MASTER	0
901da177e4SLinus Torvalds #define MIXER_ADDR_LINE		1
911da177e4SLinus Torvalds #define MIXER_ADDR_MIC		2
921da177e4SLinus Torvalds #define MIXER_ADDR_SYNTH	3
931da177e4SLinus Torvalds #define MIXER_ADDR_CD		4
941da177e4SLinus Torvalds #define MIXER_ADDR_LAST		4
951da177e4SLinus Torvalds 
96c631d03cSTakashi Iwai struct dummy_timer_ops {
97c631d03cSTakashi Iwai 	int (*create)(struct snd_pcm_substream *);
98c631d03cSTakashi Iwai 	void (*free)(struct snd_pcm_substream *);
99c631d03cSTakashi Iwai 	int (*prepare)(struct snd_pcm_substream *);
100c631d03cSTakashi Iwai 	int (*start)(struct snd_pcm_substream *);
101c631d03cSTakashi Iwai 	int (*stop)(struct snd_pcm_substream *);
102c631d03cSTakashi Iwai 	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
103c631d03cSTakashi Iwai };
104c631d03cSTakashi Iwai 
105ddce57a6STakashi Iwai #define get_dummy_ops(substream) \
106ddce57a6STakashi Iwai 	(*(const struct dummy_timer_ops **)(substream)->runtime->private_data)
107ddce57a6STakashi Iwai 
108d5e1ca05SJaroslav Kysela struct dummy_model {
109d5e1ca05SJaroslav Kysela 	const char *name;
110d5e1ca05SJaroslav Kysela 	int (*playback_constraints)(struct snd_pcm_runtime *runtime);
111d5e1ca05SJaroslav Kysela 	int (*capture_constraints)(struct snd_pcm_runtime *runtime);
112d5e1ca05SJaroslav Kysela 	u64 formats;
113d5e1ca05SJaroslav Kysela 	size_t buffer_bytes_max;
114d5e1ca05SJaroslav Kysela 	size_t period_bytes_min;
115d5e1ca05SJaroslav Kysela 	size_t period_bytes_max;
116d5e1ca05SJaroslav Kysela 	unsigned int periods_min;
117d5e1ca05SJaroslav Kysela 	unsigned int periods_max;
118d5e1ca05SJaroslav Kysela 	unsigned int rates;
119d5e1ca05SJaroslav Kysela 	unsigned int rate_min;
120d5e1ca05SJaroslav Kysela 	unsigned int rate_max;
121d5e1ca05SJaroslav Kysela 	unsigned int channels_min;
122d5e1ca05SJaroslav Kysela 	unsigned int channels_max;
123d5e1ca05SJaroslav Kysela };
124d5e1ca05SJaroslav Kysela 
1254a4d2cfdSTakashi Iwai struct snd_dummy {
1264a4d2cfdSTakashi Iwai 	struct snd_card *card;
127d64e7f7cSTakashi Iwai 	const struct dummy_model *model;
1286e65c1ccSTakashi Iwai 	struct snd_pcm *pcm;
129d5e1ca05SJaroslav Kysela 	struct snd_pcm_hardware pcm_hw;
1301da177e4SLinus Torvalds 	spinlock_t mixer_lock;
1311da177e4SLinus Torvalds 	int mixer_volume[MIXER_ADDR_LAST+1][2];
1321da177e4SLinus Torvalds 	int capture_source[MIXER_ADDR_LAST+1][2];
13316e43467SClemens Ladisch 	int iobox;
13416e43467SClemens Ladisch 	struct snd_kcontrol *cd_volume_ctl;
13516e43467SClemens Ladisch 	struct snd_kcontrol *cd_switch_ctl;
1364a4d2cfdSTakashi Iwai };
1371da177e4SLinus Torvalds 
138c631d03cSTakashi Iwai /*
139d5e1ca05SJaroslav Kysela  * card models
140d5e1ca05SJaroslav Kysela  */
141d5e1ca05SJaroslav Kysela 
emu10k1_playback_constraints(struct snd_pcm_runtime * runtime)142d5e1ca05SJaroslav Kysela static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
143d5e1ca05SJaroslav Kysela {
144d5e1ca05SJaroslav Kysela 	int err;
145d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
146d5e1ca05SJaroslav Kysela 	if (err < 0)
147d5e1ca05SJaroslav Kysela 		return err;
148d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
149d5e1ca05SJaroslav Kysela 	if (err < 0)
150d5e1ca05SJaroslav Kysela 		return err;
151d5e1ca05SJaroslav Kysela 	return 0;
152d5e1ca05SJaroslav Kysela }
153d5e1ca05SJaroslav Kysela 
154d64e7f7cSTakashi Iwai static const struct dummy_model model_emu10k1 = {
155d5e1ca05SJaroslav Kysela 	.name = "emu10k1",
156d5e1ca05SJaroslav Kysela 	.playback_constraints = emu10k1_playback_constraints,
157d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 128 * 1024,
158d5e1ca05SJaroslav Kysela };
159d5e1ca05SJaroslav Kysela 
160d64e7f7cSTakashi Iwai static const struct dummy_model model_rme9652 = {
161d5e1ca05SJaroslav Kysela 	.name = "rme9652",
162d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 26 * 64 * 1024,
163d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
164d5e1ca05SJaroslav Kysela 	.channels_min = 26,
165d5e1ca05SJaroslav Kysela 	.channels_max = 26,
166d5e1ca05SJaroslav Kysela 	.periods_min = 2,
167d5e1ca05SJaroslav Kysela 	.periods_max = 2,
168d5e1ca05SJaroslav Kysela };
169d5e1ca05SJaroslav Kysela 
170d64e7f7cSTakashi Iwai static const struct dummy_model model_ice1712 = {
171d5e1ca05SJaroslav Kysela 	.name = "ice1712",
172d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 256 * 1024,
173d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
174d5e1ca05SJaroslav Kysela 	.channels_min = 10,
175d5e1ca05SJaroslav Kysela 	.channels_max = 10,
176d5e1ca05SJaroslav Kysela 	.periods_min = 1,
177d5e1ca05SJaroslav Kysela 	.periods_max = 1024,
178d5e1ca05SJaroslav Kysela };
179d5e1ca05SJaroslav Kysela 
180d64e7f7cSTakashi Iwai static const struct dummy_model model_uda1341 = {
181d5e1ca05SJaroslav Kysela 	.name = "uda1341",
182d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 16380,
183d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
184d5e1ca05SJaroslav Kysela 	.channels_min = 2,
185d5e1ca05SJaroslav Kysela 	.channels_max = 2,
186d5e1ca05SJaroslav Kysela 	.periods_min = 2,
187d5e1ca05SJaroslav Kysela 	.periods_max = 255,
188d5e1ca05SJaroslav Kysela };
189d5e1ca05SJaroslav Kysela 
190d64e7f7cSTakashi Iwai static const struct dummy_model model_ac97 = {
191d5e1ca05SJaroslav Kysela 	.name = "ac97",
192d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
193d5e1ca05SJaroslav Kysela 	.channels_min = 2,
194d5e1ca05SJaroslav Kysela 	.channels_max = 2,
195d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000,
196d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
197d5e1ca05SJaroslav Kysela 	.rate_max = 48000,
198d5e1ca05SJaroslav Kysela };
199d5e1ca05SJaroslav Kysela 
200d64e7f7cSTakashi Iwai static const struct dummy_model model_ca0106 = {
201d5e1ca05SJaroslav Kysela 	.name = "ca0106",
202d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
203d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = ((65536-64)*8),
204d5e1ca05SJaroslav Kysela 	.period_bytes_max = (65536-64),
205d5e1ca05SJaroslav Kysela 	.periods_min = 2,
206d5e1ca05SJaroslav Kysela 	.periods_max = 8,
207d5e1ca05SJaroslav Kysela 	.channels_min = 2,
208d5e1ca05SJaroslav Kysela 	.channels_max = 2,
209d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
210d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
211d5e1ca05SJaroslav Kysela 	.rate_max = 192000,
212d5e1ca05SJaroslav Kysela };
213d5e1ca05SJaroslav Kysela 
214d64e7f7cSTakashi Iwai static const struct dummy_model *dummy_models[] = {
215d5e1ca05SJaroslav Kysela 	&model_emu10k1,
216d5e1ca05SJaroslav Kysela 	&model_rme9652,
217d5e1ca05SJaroslav Kysela 	&model_ice1712,
218d5e1ca05SJaroslav Kysela 	&model_uda1341,
219d5e1ca05SJaroslav Kysela 	&model_ac97,
220d5e1ca05SJaroslav Kysela 	&model_ca0106,
221d5e1ca05SJaroslav Kysela 	NULL
222d5e1ca05SJaroslav Kysela };
223d5e1ca05SJaroslav Kysela 
224d5e1ca05SJaroslav Kysela /*
225c631d03cSTakashi Iwai  * system timer interface
226c631d03cSTakashi Iwai  */
227c631d03cSTakashi Iwai 
228c631d03cSTakashi Iwai struct dummy_systimer_pcm {
229ddce57a6STakashi Iwai 	/* ops must be the first item */
230ddce57a6STakashi Iwai 	const struct dummy_timer_ops *timer_ops;
2311da177e4SLinus Torvalds 	spinlock_t lock;
2321da177e4SLinus Torvalds 	struct timer_list timer;
233b142037bSTakashi Iwai 	unsigned long base_time;
234b142037bSTakashi Iwai 	unsigned int frac_pos;	/* fractional sample position (based HZ) */
235b5d10781STakashi Iwai 	unsigned int frac_period_rest;
236b142037bSTakashi Iwai 	unsigned int frac_buffer_size;	/* buffer_size * HZ */
237b142037bSTakashi Iwai 	unsigned int frac_period_size;	/* period_size * HZ */
238b142037bSTakashi Iwai 	unsigned int rate;
239b5d10781STakashi Iwai 	int elapsed;
2404a4d2cfdSTakashi Iwai 	struct snd_pcm_substream *substream;
2414a4d2cfdSTakashi Iwai };
2421da177e4SLinus Torvalds 
dummy_systimer_rearm(struct dummy_systimer_pcm * dpcm)243b142037bSTakashi Iwai static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
244b142037bSTakashi Iwai {
2452a52b6eeSRoman Kollar 	mod_timer(&dpcm->timer, jiffies +
2467095be6dSLars-Peter Clausen 		DIV_ROUND_UP(dpcm->frac_period_rest, dpcm->rate));
247b142037bSTakashi Iwai }
248b142037bSTakashi Iwai 
dummy_systimer_update(struct dummy_systimer_pcm * dpcm)249b142037bSTakashi Iwai static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
250b142037bSTakashi Iwai {
251b142037bSTakashi Iwai 	unsigned long delta;
252b142037bSTakashi Iwai 
253b142037bSTakashi Iwai 	delta = jiffies - dpcm->base_time;
254b142037bSTakashi Iwai 	if (!delta)
255b142037bSTakashi Iwai 		return;
256b5d10781STakashi Iwai 	dpcm->base_time += delta;
257b5d10781STakashi Iwai 	delta *= dpcm->rate;
258b5d10781STakashi Iwai 	dpcm->frac_pos += delta;
259b142037bSTakashi Iwai 	while (dpcm->frac_pos >= dpcm->frac_buffer_size)
260b142037bSTakashi Iwai 		dpcm->frac_pos -= dpcm->frac_buffer_size;
261b5d10781STakashi Iwai 	while (dpcm->frac_period_rest <= delta) {
262b5d10781STakashi Iwai 		dpcm->elapsed++;
263b5d10781STakashi Iwai 		dpcm->frac_period_rest += dpcm->frac_period_size;
264b5d10781STakashi Iwai 	}
265b5d10781STakashi Iwai 	dpcm->frac_period_rest -= delta;
266b142037bSTakashi Iwai }
267b142037bSTakashi Iwai 
dummy_systimer_start(struct snd_pcm_substream * substream)268c631d03cSTakashi Iwai static int dummy_systimer_start(struct snd_pcm_substream *substream)
2691da177e4SLinus Torvalds {
270c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
271c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
272b142037bSTakashi Iwai 	dpcm->base_time = jiffies;
273b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
274c8eb6ba1STakashi Iwai 	spin_unlock(&dpcm->lock);
2756e65c1ccSTakashi Iwai 	return 0;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
dummy_systimer_stop(struct snd_pcm_substream * substream)278c631d03cSTakashi Iwai static int dummy_systimer_stop(struct snd_pcm_substream *substream)
279c631d03cSTakashi Iwai {
280c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
281c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
282c631d03cSTakashi Iwai 	del_timer(&dpcm->timer);
283c631d03cSTakashi Iwai 	spin_unlock(&dpcm->lock);
284c631d03cSTakashi Iwai 	return 0;
285c631d03cSTakashi Iwai }
286c631d03cSTakashi Iwai 
dummy_systimer_prepare(struct snd_pcm_substream * substream)287c631d03cSTakashi Iwai static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
2881da177e4SLinus Torvalds {
2894a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
290c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = runtime->private_data;
2911da177e4SLinus Torvalds 
292b142037bSTakashi Iwai 	dpcm->frac_pos = 0;
293b142037bSTakashi Iwai 	dpcm->rate = runtime->rate;
294b142037bSTakashi Iwai 	dpcm->frac_buffer_size = runtime->buffer_size * HZ;
295b142037bSTakashi Iwai 	dpcm->frac_period_size = runtime->period_size * HZ;
296b5d10781STakashi Iwai 	dpcm->frac_period_rest = dpcm->frac_period_size;
297b5d10781STakashi Iwai 	dpcm->elapsed = 0;
29853463a83SAhmet İnan 
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
dummy_systimer_callback(struct timer_list * t)302bc47ba90SKees Cook static void dummy_systimer_callback(struct timer_list *t)
3031da177e4SLinus Torvalds {
304bc47ba90SKees Cook 	struct dummy_systimer_pcm *dpcm = from_timer(dpcm, t, timer);
305b32425acSTakashi Iwai 	unsigned long flags;
306b5d10781STakashi Iwai 	int elapsed = 0;
3071da177e4SLinus Torvalds 
308b32425acSTakashi Iwai 	spin_lock_irqsave(&dpcm->lock, flags);
309b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
310b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
311b5d10781STakashi Iwai 	elapsed = dpcm->elapsed;
312b5d10781STakashi Iwai 	dpcm->elapsed = 0;
313b32425acSTakashi Iwai 	spin_unlock_irqrestore(&dpcm->lock, flags);
314b5d10781STakashi Iwai 	if (elapsed)
3151da177e4SLinus Torvalds 		snd_pcm_period_elapsed(dpcm->substream);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318c631d03cSTakashi Iwai static snd_pcm_uframes_t
dummy_systimer_pointer(struct snd_pcm_substream * substream)319c631d03cSTakashi Iwai dummy_systimer_pointer(struct snd_pcm_substream *substream)
3201da177e4SLinus Torvalds {
321b142037bSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
322b5d10781STakashi Iwai 	snd_pcm_uframes_t pos;
3231da177e4SLinus Torvalds 
324b142037bSTakashi Iwai 	spin_lock(&dpcm->lock);
325b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
326b5d10781STakashi Iwai 	pos = dpcm->frac_pos / HZ;
327b142037bSTakashi Iwai 	spin_unlock(&dpcm->lock);
328b5d10781STakashi Iwai 	return pos;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
dummy_systimer_create(struct snd_pcm_substream * substream)331c631d03cSTakashi Iwai static int dummy_systimer_create(struct snd_pcm_substream *substream)
3321da177e4SLinus Torvalds {
333c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm;
3341da177e4SLinus Torvalds 
335c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
336c631d03cSTakashi Iwai 	if (!dpcm)
337c631d03cSTakashi Iwai 		return -ENOMEM;
338c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
339bc47ba90SKees Cook 	timer_setup(&dpcm->timer, dummy_systimer_callback, 0);
340c631d03cSTakashi Iwai 	spin_lock_init(&dpcm->lock);
341c631d03cSTakashi Iwai 	dpcm->substream = substream;
342c631d03cSTakashi Iwai 	return 0;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
dummy_systimer_free(struct snd_pcm_substream * substream)345c631d03cSTakashi Iwai static void dummy_systimer_free(struct snd_pcm_substream *substream)
346c631d03cSTakashi Iwai {
347c631d03cSTakashi Iwai 	kfree(substream->runtime->private_data);
348c631d03cSTakashi Iwai }
349c631d03cSTakashi Iwai 
350d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_systimer_ops = {
351c631d03cSTakashi Iwai 	.create =	dummy_systimer_create,
352c631d03cSTakashi Iwai 	.free =		dummy_systimer_free,
353c631d03cSTakashi Iwai 	.prepare =	dummy_systimer_prepare,
354c631d03cSTakashi Iwai 	.start =	dummy_systimer_start,
355c631d03cSTakashi Iwai 	.stop =		dummy_systimer_stop,
356c631d03cSTakashi Iwai 	.pointer =	dummy_systimer_pointer,
357c631d03cSTakashi Iwai };
358c631d03cSTakashi Iwai 
359c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
360c631d03cSTakashi Iwai /*
361c631d03cSTakashi Iwai  * hrtimer interface
362c631d03cSTakashi Iwai  */
363c631d03cSTakashi Iwai 
364c631d03cSTakashi Iwai struct dummy_hrtimer_pcm {
365ddce57a6STakashi Iwai 	/* ops must be the first item */
366ddce57a6STakashi Iwai 	const struct dummy_timer_ops *timer_ops;
367c631d03cSTakashi Iwai 	ktime_t base_time;
368c631d03cSTakashi Iwai 	ktime_t period_time;
369c631d03cSTakashi Iwai 	atomic_t running;
370c631d03cSTakashi Iwai 	struct hrtimer timer;
371c631d03cSTakashi Iwai 	struct snd_pcm_substream *substream;
372c631d03cSTakashi Iwai };
373c631d03cSTakashi Iwai 
dummy_hrtimer_callback(struct hrtimer * timer)374c631d03cSTakashi Iwai static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
375c631d03cSTakashi Iwai {
376c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
377c631d03cSTakashi Iwai 
378c631d03cSTakashi Iwai 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
379c631d03cSTakashi Iwai 	if (!atomic_read(&dpcm->running))
380c631d03cSTakashi Iwai 		return HRTIMER_NORESTART;
381b03bbbe0SThomas Gleixner 	/*
382b03bbbe0SThomas Gleixner 	 * In cases of XRUN and draining, this calls .trigger to stop PCM
383b03bbbe0SThomas Gleixner 	 * substream.
384b03bbbe0SThomas Gleixner 	 */
385b03bbbe0SThomas Gleixner 	snd_pcm_period_elapsed(dpcm->substream);
386b03bbbe0SThomas Gleixner 	if (!atomic_read(&dpcm->running))
387b03bbbe0SThomas Gleixner 		return HRTIMER_NORESTART;
388b03bbbe0SThomas Gleixner 
389c631d03cSTakashi Iwai 	hrtimer_forward_now(timer, dpcm->period_time);
390c631d03cSTakashi Iwai 	return HRTIMER_RESTART;
391c631d03cSTakashi Iwai }
392c631d03cSTakashi Iwai 
dummy_hrtimer_start(struct snd_pcm_substream * substream)393c631d03cSTakashi Iwai static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
394c631d03cSTakashi Iwai {
395c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
396c631d03cSTakashi Iwai 
397c631d03cSTakashi Iwai 	dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
398b03bbbe0SThomas Gleixner 	hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT);
399c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 1);
400c631d03cSTakashi Iwai 	return 0;
401c631d03cSTakashi Iwai }
402c631d03cSTakashi Iwai 
dummy_hrtimer_stop(struct snd_pcm_substream * substream)403c631d03cSTakashi Iwai static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
404c631d03cSTakashi Iwai {
405c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
406c631d03cSTakashi Iwai 
407c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
408b03bbbe0SThomas Gleixner 	if (!hrtimer_callback_running(&dpcm->timer))
409c631d03cSTakashi Iwai 		hrtimer_cancel(&dpcm->timer);
410c631d03cSTakashi Iwai 	return 0;
411c631d03cSTakashi Iwai }
412c631d03cSTakashi Iwai 
dummy_hrtimer_sync(struct dummy_hrtimer_pcm * dpcm)413c631d03cSTakashi Iwai static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
414c631d03cSTakashi Iwai {
415d5dbbe65STakashi Iwai 	hrtimer_cancel(&dpcm->timer);
416c631d03cSTakashi Iwai }
417c631d03cSTakashi Iwai 
418c631d03cSTakashi Iwai static snd_pcm_uframes_t
dummy_hrtimer_pointer(struct snd_pcm_substream * substream)419c631d03cSTakashi Iwai dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
420c631d03cSTakashi Iwai {
421c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
422c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
423c631d03cSTakashi Iwai 	u64 delta;
424c631d03cSTakashi Iwai 	u32 pos;
425c631d03cSTakashi Iwai 
426c631d03cSTakashi Iwai 	delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
427c631d03cSTakashi Iwai 			       dpcm->base_time);
428c631d03cSTakashi Iwai 	delta = div_u64(delta * runtime->rate + 999999, 1000000);
429c631d03cSTakashi Iwai 	div_u64_rem(delta, runtime->buffer_size, &pos);
430c631d03cSTakashi Iwai 	return pos;
431c631d03cSTakashi Iwai }
432c631d03cSTakashi Iwai 
dummy_hrtimer_prepare(struct snd_pcm_substream * substream)433c631d03cSTakashi Iwai static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
434c631d03cSTakashi Iwai {
435c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
436c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
437c631d03cSTakashi Iwai 	unsigned int period, rate;
438c631d03cSTakashi Iwai 	long sec;
439c631d03cSTakashi Iwai 	unsigned long nsecs;
440c631d03cSTakashi Iwai 
441c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
442c631d03cSTakashi Iwai 	period = runtime->period_size;
443c631d03cSTakashi Iwai 	rate = runtime->rate;
444c631d03cSTakashi Iwai 	sec = period / rate;
445c631d03cSTakashi Iwai 	period %= rate;
446c631d03cSTakashi Iwai 	nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
447c631d03cSTakashi Iwai 	dpcm->period_time = ktime_set(sec, nsecs);
448c631d03cSTakashi Iwai 
449c631d03cSTakashi Iwai 	return 0;
450c631d03cSTakashi Iwai }
451c631d03cSTakashi Iwai 
dummy_hrtimer_create(struct snd_pcm_substream * substream)452c631d03cSTakashi Iwai static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
453c631d03cSTakashi Iwai {
454c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
455c631d03cSTakashi Iwai 
456c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
457c631d03cSTakashi Iwai 	if (!dpcm)
458c631d03cSTakashi Iwai 		return -ENOMEM;
459c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
460b03bbbe0SThomas Gleixner 	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
461c631d03cSTakashi Iwai 	dpcm->timer.function = dummy_hrtimer_callback;
462c631d03cSTakashi Iwai 	dpcm->substream = substream;
463c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
464c631d03cSTakashi Iwai 	return 0;
465c631d03cSTakashi Iwai }
466c631d03cSTakashi Iwai 
dummy_hrtimer_free(struct snd_pcm_substream * substream)467c631d03cSTakashi Iwai static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
468c631d03cSTakashi Iwai {
469c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
470c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
471c631d03cSTakashi Iwai 	kfree(dpcm);
472c631d03cSTakashi Iwai }
473c631d03cSTakashi Iwai 
474d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_hrtimer_ops = {
475c631d03cSTakashi Iwai 	.create =	dummy_hrtimer_create,
476c631d03cSTakashi Iwai 	.free =		dummy_hrtimer_free,
477c631d03cSTakashi Iwai 	.prepare =	dummy_hrtimer_prepare,
478c631d03cSTakashi Iwai 	.start =	dummy_hrtimer_start,
479c631d03cSTakashi Iwai 	.stop =		dummy_hrtimer_stop,
480c631d03cSTakashi Iwai 	.pointer =	dummy_hrtimer_pointer,
481c631d03cSTakashi Iwai };
482c631d03cSTakashi Iwai 
483c631d03cSTakashi Iwai #endif /* CONFIG_HIGH_RES_TIMERS */
484c631d03cSTakashi Iwai 
485c631d03cSTakashi Iwai /*
486c631d03cSTakashi Iwai  * PCM interface
487c631d03cSTakashi Iwai  */
488c631d03cSTakashi Iwai 
dummy_pcm_trigger(struct snd_pcm_substream * substream,int cmd)489c631d03cSTakashi Iwai static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
490c631d03cSTakashi Iwai {
491c631d03cSTakashi Iwai 	switch (cmd) {
492c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_START:
493c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
494ddce57a6STakashi Iwai 		return get_dummy_ops(substream)->start(substream);
495c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_STOP:
496c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
497ddce57a6STakashi Iwai 		return get_dummy_ops(substream)->stop(substream);
498c631d03cSTakashi Iwai 	}
499c631d03cSTakashi Iwai 	return -EINVAL;
500c631d03cSTakashi Iwai }
501c631d03cSTakashi Iwai 
dummy_pcm_prepare(struct snd_pcm_substream * substream)502c631d03cSTakashi Iwai static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
503c631d03cSTakashi Iwai {
504ddce57a6STakashi Iwai 	return get_dummy_ops(substream)->prepare(substream);
505c631d03cSTakashi Iwai }
506c631d03cSTakashi Iwai 
dummy_pcm_pointer(struct snd_pcm_substream * substream)507c631d03cSTakashi Iwai static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
508c631d03cSTakashi Iwai {
509ddce57a6STakashi Iwai 	return get_dummy_ops(substream)->pointer(substream);
510c631d03cSTakashi Iwai }
511c631d03cSTakashi Iwai 
512b6c0b715SBhumika Goyal static const struct snd_pcm_hardware dummy_pcm_hardware = {
513c631d03cSTakashi Iwai 	.info =			(SNDRV_PCM_INFO_MMAP |
514c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_INTERLEAVED |
515c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_RESUME |
516c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_MMAP_VALID),
517c631d03cSTakashi Iwai 	.formats =		USE_FORMATS,
518c631d03cSTakashi Iwai 	.rates =		USE_RATE,
519c631d03cSTakashi Iwai 	.rate_min =		USE_RATE_MIN,
520c631d03cSTakashi Iwai 	.rate_max =		USE_RATE_MAX,
521c631d03cSTakashi Iwai 	.channels_min =		USE_CHANNELS_MIN,
522c631d03cSTakashi Iwai 	.channels_max =		USE_CHANNELS_MAX,
523c631d03cSTakashi Iwai 	.buffer_bytes_max =	MAX_BUFFER_SIZE,
524d5e1ca05SJaroslav Kysela 	.period_bytes_min =	MIN_PERIOD_SIZE,
525c631d03cSTakashi Iwai 	.period_bytes_max =	MAX_PERIOD_SIZE,
526c631d03cSTakashi Iwai 	.periods_min =		USE_PERIODS_MIN,
527c631d03cSTakashi Iwai 	.periods_max =		USE_PERIODS_MAX,
528c631d03cSTakashi Iwai 	.fifo_size =		0,
529c631d03cSTakashi Iwai };
530c631d03cSTakashi Iwai 
dummy_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)531c631d03cSTakashi Iwai static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
5324a4d2cfdSTakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
5331da177e4SLinus Torvalds {
534a68c4d11STakashi Iwai 	if (fake_buffer) {
535a68c4d11STakashi Iwai 		/* runtime->dma_bytes has to be set manually to allow mmap */
536a68c4d11STakashi Iwai 		substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
537a68c4d11STakashi Iwai 		return 0;
538a68c4d11STakashi Iwai 	}
539a68c4d11STakashi Iwai 	return 0;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
dummy_pcm_open(struct snd_pcm_substream * substream)542c631d03cSTakashi Iwai static int dummy_pcm_open(struct snd_pcm_substream *substream)
543c8eb6ba1STakashi Iwai {
544c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
545d64e7f7cSTakashi Iwai 	const struct dummy_model *model = dummy->model;
5464a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
547ddce57a6STakashi Iwai 	const struct dummy_timer_ops *ops;
5481da177e4SLinus Torvalds 	int err;
5491da177e4SLinus Torvalds 
550ddce57a6STakashi Iwai 	ops = &dummy_systimer_ops;
551c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
552c631d03cSTakashi Iwai 	if (hrtimer)
553ddce57a6STakashi Iwai 		ops = &dummy_hrtimer_ops;
554c631d03cSTakashi Iwai #endif
555c631d03cSTakashi Iwai 
556ddce57a6STakashi Iwai 	err = ops->create(substream);
557c631d03cSTakashi Iwai 	if (err < 0)
558c631d03cSTakashi Iwai 		return err;
559ddce57a6STakashi Iwai 	get_dummy_ops(substream) = ops;
560c631d03cSTakashi Iwai 
561d5e1ca05SJaroslav Kysela 	runtime->hw = dummy->pcm_hw;
5621da177e4SLinus Torvalds 	if (substream->pcm->device & 1) {
5631da177e4SLinus Torvalds 		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
5641da177e4SLinus Torvalds 		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	if (substream->pcm->device & 2)
567c631d03cSTakashi Iwai 		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
568c631d03cSTakashi Iwai 				      SNDRV_PCM_INFO_MMAP_VALID);
569c631d03cSTakashi Iwai 
570d5e1ca05SJaroslav Kysela 	if (model == NULL)
571d5e1ca05SJaroslav Kysela 		return 0;
572d5e1ca05SJaroslav Kysela 
573d5e1ca05SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
574d5e1ca05SJaroslav Kysela 		if (model->playback_constraints)
575d5e1ca05SJaroslav Kysela 			err = model->playback_constraints(substream->runtime);
576d5e1ca05SJaroslav Kysela 	} else {
577d5e1ca05SJaroslav Kysela 		if (model->capture_constraints)
578d5e1ca05SJaroslav Kysela 			err = model->capture_constraints(substream->runtime);
579d5e1ca05SJaroslav Kysela 	}
580c631d03cSTakashi Iwai 	if (err < 0) {
581ddce57a6STakashi Iwai 		get_dummy_ops(substream)->free(substream);
5821da177e4SLinus Torvalds 		return err;
583c631d03cSTakashi Iwai 	}
5841da177e4SLinus Torvalds 	return 0;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
dummy_pcm_close(struct snd_pcm_substream * substream)587c631d03cSTakashi Iwai static int dummy_pcm_close(struct snd_pcm_substream *substream)
5881da177e4SLinus Torvalds {
589ddce57a6STakashi Iwai 	get_dummy_ops(substream)->free(substream);
5901da177e4SLinus Torvalds 	return 0;
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
593a68c4d11STakashi Iwai /*
594a68c4d11STakashi Iwai  * dummy buffer handling
595a68c4d11STakashi Iwai  */
596a68c4d11STakashi Iwai 
597a68c4d11STakashi Iwai static void *dummy_page[2];
598a68c4d11STakashi Iwai 
free_fake_buffer(void)599a68c4d11STakashi Iwai static void free_fake_buffer(void)
600a68c4d11STakashi Iwai {
601a68c4d11STakashi Iwai 	if (fake_buffer) {
602a68c4d11STakashi Iwai 		int i;
603a68c4d11STakashi Iwai 		for (i = 0; i < 2; i++)
604a68c4d11STakashi Iwai 			if (dummy_page[i]) {
605a68c4d11STakashi Iwai 				free_page((unsigned long)dummy_page[i]);
606a68c4d11STakashi Iwai 				dummy_page[i] = NULL;
607a68c4d11STakashi Iwai 			}
608a68c4d11STakashi Iwai 	}
609a68c4d11STakashi Iwai }
610a68c4d11STakashi Iwai 
alloc_fake_buffer(void)611a68c4d11STakashi Iwai static int alloc_fake_buffer(void)
612a68c4d11STakashi Iwai {
613a68c4d11STakashi Iwai 	int i;
614a68c4d11STakashi Iwai 
615a68c4d11STakashi Iwai 	if (!fake_buffer)
616a68c4d11STakashi Iwai 		return 0;
617a68c4d11STakashi Iwai 	for (i = 0; i < 2; i++) {
618a68c4d11STakashi Iwai 		dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
619a68c4d11STakashi Iwai 		if (!dummy_page[i]) {
620a68c4d11STakashi Iwai 			free_fake_buffer();
621a68c4d11STakashi Iwai 			return -ENOMEM;
622a68c4d11STakashi Iwai 		}
623a68c4d11STakashi Iwai 	}
624a68c4d11STakashi Iwai 	return 0;
625a68c4d11STakashi Iwai }
626a68c4d11STakashi Iwai 
dummy_pcm_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * iter,unsigned long bytes)627a68c4d11STakashi Iwai static int dummy_pcm_copy(struct snd_pcm_substream *substream,
628d53611d2STakashi Iwai 			  int channel, unsigned long pos,
629*526a19b3STakashi Iwai 			  struct iov_iter *iter, unsigned long bytes)
630a68c4d11STakashi Iwai {
631a68c4d11STakashi Iwai 	return 0; /* do nothing */
632a68c4d11STakashi Iwai }
633a68c4d11STakashi Iwai 
dummy_pcm_silence(struct snd_pcm_substream * substream,int channel,unsigned long pos,unsigned long bytes)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 
dummy_pcm_page(struct snd_pcm_substream * substream,unsigned long offset)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,
663*526a19b3STakashi Iwai 	.copy =		dummy_pcm_copy,
664d53611d2STakashi Iwai 	.fill_silence =	dummy_pcm_silence,
665a68c4d11STakashi Iwai 	.page =		dummy_pcm_page,
666a68c4d11STakashi Iwai };
667a68c4d11STakashi Iwai 
snd_card_dummy_pcm(struct snd_dummy * dummy,int device,int substreams)668fbbb01a1SBill Pemberton static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
669788c6043SPrarit Bhargava 			      int substreams)
6701da177e4SLinus Torvalds {
6714a4d2cfdSTakashi Iwai 	struct snd_pcm *pcm;
6721da7f0c5STakashi Iwai 	const struct snd_pcm_ops *ops;
6731da177e4SLinus Torvalds 	int err;
6741da177e4SLinus Torvalds 
6751a11cb64SJaroslav Kysela 	err = snd_pcm_new(dummy->card, "Dummy PCM", device,
6761a11cb64SJaroslav Kysela 			       substreams, substreams, &pcm);
6771a11cb64SJaroslav Kysela 	if (err < 0)
6781da177e4SLinus Torvalds 		return err;
6796e65c1ccSTakashi Iwai 	dummy->pcm = pcm;
680a68c4d11STakashi Iwai 	if (fake_buffer)
681a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops_no_buf;
682a68c4d11STakashi Iwai 	else
683a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops;
684a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
685a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
6861da177e4SLinus Torvalds 	pcm->private_data = dummy;
6871da177e4SLinus Torvalds 	pcm->info_flags = 0;
6881da177e4SLinus Torvalds 	strcpy(pcm->name, "Dummy PCM");
689a68c4d11STakashi Iwai 	if (!fake_buffer) {
690ef1545b8STakashi Iwai 		snd_pcm_set_managed_buffer_all(pcm,
691a68c4d11STakashi Iwai 			SNDRV_DMA_TYPE_CONTINUOUS,
6928fd9da75STakashi Iwai 			NULL,
6931da177e4SLinus Torvalds 			0, 64*1024);
694a68c4d11STakashi Iwai 	}
6951da177e4SLinus Torvalds 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6989b151fecSTakashi Iwai /*
6999b151fecSTakashi Iwai  * mixer interface
7009b151fecSTakashi Iwai  */
7019b151fecSTakashi Iwai 
7021da177e4SLinus Torvalds #define DUMMY_VOLUME(xname, xindex, addr) \
703fb567a8eSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
704fb567a8eSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
705fb567a8eSTakashi Iwai   .name = xname, .index = xindex, \
7061da177e4SLinus Torvalds   .info = snd_dummy_volume_info, \
7071da177e4SLinus Torvalds   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
708fb567a8eSTakashi Iwai   .private_value = addr, \
709fb567a8eSTakashi Iwai   .tlv = { .p = db_scale_dummy } }
7101da177e4SLinus Torvalds 
snd_dummy_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)7114a4d2cfdSTakashi Iwai static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
7124a4d2cfdSTakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7151da177e4SLinus Torvalds 	uinfo->count = 2;
716446bc11fSYJ Lee 	uinfo->value.integer.min = mixer_volume_level_min;
717446bc11fSYJ Lee 	uinfo->value.integer.max = mixer_volume_level_max;
7181da177e4SLinus Torvalds 	return 0;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
snd_dummy_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7214a4d2cfdSTakashi Iwai static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
7224a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7231da177e4SLinus Torvalds {
7244a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7251da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7261da177e4SLinus Torvalds 
727c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7281da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
7291da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
730c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7311da177e4SLinus Torvalds 	return 0;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
snd_dummy_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7344a4d2cfdSTakashi Iwai static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
7354a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7361da177e4SLinus Torvalds {
7374a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7381da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7391da177e4SLinus Torvalds 	int left, right;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0];
742446bc11fSYJ Lee 	if (left < mixer_volume_level_min)
743446bc11fSYJ Lee 		left = mixer_volume_level_min;
744446bc11fSYJ Lee 	if (left > mixer_volume_level_max)
745446bc11fSYJ Lee 		left = mixer_volume_level_max;
7461da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1];
747446bc11fSYJ Lee 	if (right < mixer_volume_level_min)
748446bc11fSYJ Lee 		right = mixer_volume_level_min;
749446bc11fSYJ Lee 	if (right > mixer_volume_level_max)
750446bc11fSYJ Lee 		right = mixer_volume_level_max;
751c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7521da177e4SLinus Torvalds 	change = dummy->mixer_volume[addr][0] != left ||
7531da177e4SLinus Torvalds 	         dummy->mixer_volume[addr][1] != right;
7541da177e4SLinus Torvalds 	dummy->mixer_volume[addr][0] = left;
7551da177e4SLinus Torvalds 	dummy->mixer_volume[addr][1] = right;
756c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7571da177e4SLinus Torvalds 	return change;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7600cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
761fb567a8eSTakashi Iwai 
7621da177e4SLinus Torvalds #define DUMMY_CAPSRC(xname, xindex, addr) \
7631da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
7641da177e4SLinus Torvalds   .info = snd_dummy_capsrc_info, \
7651da177e4SLinus Torvalds   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
7661da177e4SLinus Torvalds   .private_value = addr }
7671da177e4SLinus Torvalds 
768a5ce8890STakashi Iwai #define snd_dummy_capsrc_info	snd_ctl_boolean_stereo_info
7691da177e4SLinus Torvalds 
snd_dummy_capsrc_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7704a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
7714a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7721da177e4SLinus Torvalds {
7734a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7741da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7751da177e4SLinus Torvalds 
776c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7771da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
7781da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
779c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7801da177e4SLinus Torvalds 	return 0;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
snd_dummy_capsrc_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7834a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
7841da177e4SLinus Torvalds {
7854a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7861da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7871da177e4SLinus Torvalds 	int left, right;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0] & 1;
7901da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1] & 1;
791c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7921da177e4SLinus Torvalds 	change = dummy->capture_source[addr][0] != left &&
7931da177e4SLinus Torvalds 	         dummy->capture_source[addr][1] != right;
7941da177e4SLinus Torvalds 	dummy->capture_source[addr][0] = left;
7951da177e4SLinus Torvalds 	dummy->capture_source[addr][1] = right;
796c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7971da177e4SLinus Torvalds 	return change;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
snd_dummy_iobox_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * info)80016e43467SClemens Ladisch static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
80116e43467SClemens Ladisch 				struct snd_ctl_elem_info *info)
80216e43467SClemens Ladisch {
803a4a1b737SColin Ian King 	static const char *const names[] = { "None", "CD Player" };
80416e43467SClemens Ladisch 
80516e43467SClemens Ladisch 	return snd_ctl_enum_info(info, 1, 2, names);
80616e43467SClemens Ladisch }
80716e43467SClemens Ladisch 
snd_dummy_iobox_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)80816e43467SClemens Ladisch static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
80916e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
81016e43467SClemens Ladisch {
81116e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
81216e43467SClemens Ladisch 
81316e43467SClemens Ladisch 	value->value.enumerated.item[0] = dummy->iobox;
81416e43467SClemens Ladisch 	return 0;
81516e43467SClemens Ladisch }
81616e43467SClemens Ladisch 
snd_dummy_iobox_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)81716e43467SClemens Ladisch static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
81816e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
81916e43467SClemens Ladisch {
82016e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
82116e43467SClemens Ladisch 	int changed;
82216e43467SClemens Ladisch 
82316e43467SClemens Ladisch 	if (value->value.enumerated.item[0] > 1)
82416e43467SClemens Ladisch 		return -EINVAL;
82516e43467SClemens Ladisch 
82616e43467SClemens Ladisch 	changed = value->value.enumerated.item[0] != dummy->iobox;
82716e43467SClemens Ladisch 	if (changed) {
82816e43467SClemens Ladisch 		dummy->iobox = value->value.enumerated.item[0];
82916e43467SClemens Ladisch 
83016e43467SClemens Ladisch 		if (dummy->iobox) {
83116e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access &=
83216e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83316e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access &=
83416e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83516e43467SClemens Ladisch 		} else {
83616e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access |=
83716e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
83816e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access |=
83916e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
84016e43467SClemens Ladisch 		}
84116e43467SClemens Ladisch 
84216e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
84316e43467SClemens Ladisch 			       &dummy->cd_volume_ctl->id);
84416e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
84516e43467SClemens Ladisch 			       &dummy->cd_switch_ctl->id);
84616e43467SClemens Ladisch 	}
84716e43467SClemens Ladisch 
84816e43467SClemens Ladisch 	return changed;
84916e43467SClemens Ladisch }
85016e43467SClemens Ladisch 
8512eccd408STakashi Iwai static const struct snd_kcontrol_new snd_dummy_controls[] = {
8521da177e4SLinus Torvalds DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
8531da177e4SLinus Torvalds DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
8541da177e4SLinus Torvalds DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
855e05d6964STakashi Iwai DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
8561da177e4SLinus Torvalds DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
857e05d6964STakashi Iwai DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
8581da177e4SLinus Torvalds DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
859e05d6964STakashi Iwai DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
8601da177e4SLinus Torvalds DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
86116e43467SClemens Ladisch DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
86216e43467SClemens Ladisch {
86316e43467SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
86416e43467SClemens Ladisch 	.name  = "External I/O Box",
86516e43467SClemens Ladisch 	.info  = snd_dummy_iobox_info,
86616e43467SClemens Ladisch 	.get   = snd_dummy_iobox_get,
86716e43467SClemens Ladisch 	.put   = snd_dummy_iobox_put,
86816e43467SClemens Ladisch },
8691da177e4SLinus Torvalds };
8701da177e4SLinus Torvalds 
snd_card_dummy_new_mixer(struct snd_dummy * dummy)871fbbb01a1SBill Pemberton static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
8721da177e4SLinus Torvalds {
8734a4d2cfdSTakashi Iwai 	struct snd_card *card = dummy->card;
87416e43467SClemens Ladisch 	struct snd_kcontrol *kcontrol;
8751da177e4SLinus Torvalds 	unsigned int idx;
8761da177e4SLinus Torvalds 	int err;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	spin_lock_init(&dummy->mixer_lock);
8791da177e4SLinus Torvalds 	strcpy(card->mixername, "Dummy Mixer");
88016e43467SClemens Ladisch 	dummy->iobox = 1;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
88316e43467SClemens Ladisch 		kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
88416e43467SClemens Ladisch 		err = snd_ctl_add(card, kcontrol);
8851a11cb64SJaroslav Kysela 		if (err < 0)
8861da177e4SLinus Torvalds 			return err;
88716e43467SClemens Ladisch 		if (!strcmp(kcontrol->id.name, "CD Volume"))
88816e43467SClemens Ladisch 			dummy->cd_volume_ctl = kcontrol;
88916e43467SClemens Ladisch 		else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
89016e43467SClemens Ladisch 			dummy->cd_switch_ctl = kcontrol;
89116e43467SClemens Ladisch 
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 	return 0;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
896129a4c9fSTakashi Iwai #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
8979b151fecSTakashi Iwai /*
8989b151fecSTakashi Iwai  * proc interface
8999b151fecSTakashi Iwai  */
print_formats(struct snd_dummy * dummy,struct snd_info_buffer * buffer)900d5e1ca05SJaroslav Kysela static void print_formats(struct snd_dummy *dummy,
901d5e1ca05SJaroslav Kysela 			  struct snd_info_buffer *buffer)
9029b151fecSTakashi Iwai {
903c5f72ef1STakashi Iwai 	snd_pcm_format_t i;
9049b151fecSTakashi Iwai 
905c5f72ef1STakashi Iwai 	pcm_for_each_format(i) {
906c5f72ef1STakashi Iwai 		if (dummy->pcm_hw.formats & pcm_format_to_bits(i))
9079b151fecSTakashi Iwai 			snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
9089b151fecSTakashi Iwai 	}
9099b151fecSTakashi Iwai }
9109b151fecSTakashi Iwai 
print_rates(struct snd_dummy * dummy,struct snd_info_buffer * buffer)911d5e1ca05SJaroslav Kysela static void print_rates(struct snd_dummy *dummy,
912d5e1ca05SJaroslav Kysela 			struct snd_info_buffer *buffer)
9139b151fecSTakashi Iwai {
914d64e7f7cSTakashi Iwai 	static const int rates[] = {
9159b151fecSTakashi Iwai 		5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
9169b151fecSTakashi Iwai 		64000, 88200, 96000, 176400, 192000,
9179b151fecSTakashi Iwai 	};
9189b151fecSTakashi Iwai 	int i;
9199b151fecSTakashi Iwai 
920d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
9219b151fecSTakashi Iwai 		snd_iprintf(buffer, " continuous");
922d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
9239b151fecSTakashi Iwai 		snd_iprintf(buffer, " knot");
9249b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(rates); i++)
925d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.rates & (1 << i))
9269b151fecSTakashi Iwai 			snd_iprintf(buffer, " %d", rates[i]);
9279b151fecSTakashi Iwai }
9289b151fecSTakashi Iwai 
929d5e1ca05SJaroslav Kysela #define get_dummy_int_ptr(dummy, ofs) \
930d5e1ca05SJaroslav Kysela 	(unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
931d5e1ca05SJaroslav Kysela #define get_dummy_ll_ptr(dummy, ofs) \
932d5e1ca05SJaroslav Kysela 	(unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
9339b151fecSTakashi Iwai 
9349b151fecSTakashi Iwai struct dummy_hw_field {
9359b151fecSTakashi Iwai 	const char *name;
9369b151fecSTakashi Iwai 	const char *format;
9379b151fecSTakashi Iwai 	unsigned int offset;
9389b151fecSTakashi Iwai 	unsigned int size;
9399b151fecSTakashi Iwai };
9409b151fecSTakashi Iwai #define FIELD_ENTRY(item, fmt) {		   \
9419b151fecSTakashi Iwai 	.name = #item,				   \
9429b151fecSTakashi Iwai 	.format = fmt,				   \
9439b151fecSTakashi Iwai 	.offset = offsetof(struct snd_pcm_hardware, item), \
9449b151fecSTakashi Iwai 	.size = sizeof(dummy_pcm_hardware.item) }
9459b151fecSTakashi Iwai 
946d64e7f7cSTakashi Iwai static const struct dummy_hw_field fields[] = {
9479b151fecSTakashi Iwai 	FIELD_ENTRY(formats, "%#llx"),
9489b151fecSTakashi Iwai 	FIELD_ENTRY(rates, "%#x"),
9499b151fecSTakashi Iwai 	FIELD_ENTRY(rate_min, "%d"),
9509b151fecSTakashi Iwai 	FIELD_ENTRY(rate_max, "%d"),
9519b151fecSTakashi Iwai 	FIELD_ENTRY(channels_min, "%d"),
9529b151fecSTakashi Iwai 	FIELD_ENTRY(channels_max, "%d"),
9539b151fecSTakashi Iwai 	FIELD_ENTRY(buffer_bytes_max, "%ld"),
9549b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_min, "%ld"),
9559b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_max, "%ld"),
9569b151fecSTakashi Iwai 	FIELD_ENTRY(periods_min, "%d"),
9579b151fecSTakashi Iwai 	FIELD_ENTRY(periods_max, "%d"),
9589b151fecSTakashi Iwai };
9599b151fecSTakashi Iwai 
dummy_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)9609b151fecSTakashi Iwai static void dummy_proc_read(struct snd_info_entry *entry,
9619b151fecSTakashi Iwai 			    struct snd_info_buffer *buffer)
9629b151fecSTakashi Iwai {
963d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9649b151fecSTakashi Iwai 	int i;
9659b151fecSTakashi Iwai 
9669b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(fields); i++) {
9679b151fecSTakashi Iwai 		snd_iprintf(buffer, "%s ", fields[i].name);
9689b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
9699b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
970d5e1ca05SJaroslav Kysela 				*get_dummy_int_ptr(dummy, fields[i].offset));
9719b151fecSTakashi Iwai 		else
9729b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
973d5e1ca05SJaroslav Kysela 				*get_dummy_ll_ptr(dummy, fields[i].offset));
9749b151fecSTakashi Iwai 		if (!strcmp(fields[i].name, "formats"))
975d5e1ca05SJaroslav Kysela 			print_formats(dummy, buffer);
9769b151fecSTakashi Iwai 		else if (!strcmp(fields[i].name, "rates"))
977d5e1ca05SJaroslav Kysela 			print_rates(dummy, buffer);
9789b151fecSTakashi Iwai 		snd_iprintf(buffer, "\n");
9799b151fecSTakashi Iwai 	}
9809b151fecSTakashi Iwai }
9819b151fecSTakashi Iwai 
dummy_proc_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)9829b151fecSTakashi Iwai static void dummy_proc_write(struct snd_info_entry *entry,
9839b151fecSTakashi Iwai 			     struct snd_info_buffer *buffer)
9849b151fecSTakashi Iwai {
985d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9869b151fecSTakashi Iwai 	char line[64];
9879b151fecSTakashi Iwai 
9889b151fecSTakashi Iwai 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
9899b151fecSTakashi Iwai 		char item[20];
9909b151fecSTakashi Iwai 		const char *ptr;
9919b151fecSTakashi Iwai 		unsigned long long val;
9929b151fecSTakashi Iwai 		int i;
9939b151fecSTakashi Iwai 
9949b151fecSTakashi Iwai 		ptr = snd_info_get_str(item, line, sizeof(item));
9959b151fecSTakashi Iwai 		for (i = 0; i < ARRAY_SIZE(fields); i++) {
9969b151fecSTakashi Iwai 			if (!strcmp(item, fields[i].name))
9979b151fecSTakashi Iwai 				break;
9989b151fecSTakashi Iwai 		}
9999b151fecSTakashi Iwai 		if (i >= ARRAY_SIZE(fields))
10009b151fecSTakashi Iwai 			continue;
10019b151fecSTakashi Iwai 		snd_info_get_str(item, ptr, sizeof(item));
1002b785a492SJingoo Han 		if (kstrtoull(item, 0, &val))
10039b151fecSTakashi Iwai 			continue;
10049b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
1005d5e1ca05SJaroslav Kysela 			*get_dummy_int_ptr(dummy, fields[i].offset) = val;
10069b151fecSTakashi Iwai 		else
1007d5e1ca05SJaroslav Kysela 			*get_dummy_ll_ptr(dummy, fields[i].offset) = val;
10089b151fecSTakashi Iwai 	}
10099b151fecSTakashi Iwai }
10109b151fecSTakashi Iwai 
dummy_proc_init(struct snd_dummy * chip)1011fbbb01a1SBill Pemberton static void dummy_proc_init(struct snd_dummy *chip)
10129b151fecSTakashi Iwai {
1013815d808cSTakashi Iwai 	snd_card_rw_proc_new(chip->card, "dummy_pcm", chip,
1014815d808cSTakashi Iwai 			     dummy_proc_read, dummy_proc_write);
10159b151fecSTakashi Iwai }
10169b151fecSTakashi Iwai #else
10179b151fecSTakashi Iwai #define dummy_proc_init(x)
1018129a4c9fSTakashi Iwai #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
10199b151fecSTakashi Iwai 
snd_dummy_probe(struct platform_device * devptr)1020fbbb01a1SBill Pemberton static int snd_dummy_probe(struct platform_device *devptr)
10211da177e4SLinus Torvalds {
10224a4d2cfdSTakashi Iwai 	struct snd_card *card;
10234a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy;
1024d64e7f7cSTakashi Iwai 	const struct dummy_model *m = NULL, **mdl;
10251da177e4SLinus Torvalds 	int idx, err;
10266e65c1ccSTakashi Iwai 	int dev = devptr->id;
10271da177e4SLinus Torvalds 
1028ed16a22bSTakashi Iwai 	err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1029bd7dd77cSTakashi Iwai 				sizeof(struct snd_dummy), &card);
1030bd7dd77cSTakashi Iwai 	if (err < 0)
1031bd7dd77cSTakashi Iwai 		return err;
10324a4d2cfdSTakashi Iwai 	dummy = card->private_data;
10331da177e4SLinus Torvalds 	dummy->card = card;
1034d5e1ca05SJaroslav Kysela 	for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
1035d5e1ca05SJaroslav Kysela 		if (strcmp(model[dev], (*mdl)->name) == 0) {
1036d5e1ca05SJaroslav Kysela 			printk(KERN_INFO
1037d5e1ca05SJaroslav Kysela 				"snd-dummy: Using model '%s' for card %i\n",
1038d5e1ca05SJaroslav Kysela 				(*mdl)->name, card->number);
1039d5e1ca05SJaroslav Kysela 			m = dummy->model = *mdl;
1040d5e1ca05SJaroslav Kysela 			break;
1041d5e1ca05SJaroslav Kysela 		}
1042d5e1ca05SJaroslav Kysela 	}
10431da177e4SLinus Torvalds 	for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
10441da177e4SLinus Torvalds 		if (pcm_substreams[dev] < 1)
10451da177e4SLinus Torvalds 			pcm_substreams[dev] = 1;
10461da177e4SLinus Torvalds 		if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
10471da177e4SLinus Torvalds 			pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
10481a11cb64SJaroslav Kysela 		err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
10491a11cb64SJaroslav Kysela 		if (err < 0)
1050ed16a22bSTakashi Iwai 			return err;
10511da177e4SLinus Torvalds 	}
1052d5e1ca05SJaroslav Kysela 
1053d5e1ca05SJaroslav Kysela 	dummy->pcm_hw = dummy_pcm_hardware;
1054d5e1ca05SJaroslav Kysela 	if (m) {
1055d5e1ca05SJaroslav Kysela 		if (m->formats)
1056d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.formats = m->formats;
1057d5e1ca05SJaroslav Kysela 		if (m->buffer_bytes_max)
1058d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1059d5e1ca05SJaroslav Kysela 		if (m->period_bytes_min)
1060d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1061d5e1ca05SJaroslav Kysela 		if (m->period_bytes_max)
1062d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1063d5e1ca05SJaroslav Kysela 		if (m->periods_min)
1064d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_min = m->periods_min;
1065d5e1ca05SJaroslav Kysela 		if (m->periods_max)
1066d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_max = m->periods_max;
1067d5e1ca05SJaroslav Kysela 		if (m->rates)
1068d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rates = m->rates;
1069d5e1ca05SJaroslav Kysela 		if (m->rate_min)
1070d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_min = m->rate_min;
1071d5e1ca05SJaroslav Kysela 		if (m->rate_max)
1072d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_max = m->rate_max;
1073d5e1ca05SJaroslav Kysela 		if (m->channels_min)
1074d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_min = m->channels_min;
1075d5e1ca05SJaroslav Kysela 		if (m->channels_max)
1076d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_max = m->channels_max;
1077d5e1ca05SJaroslav Kysela 	}
1078d5e1ca05SJaroslav Kysela 
1079446bc11fSYJ Lee 	if (mixer_volume_level_min > mixer_volume_level_max) {
1080446bc11fSYJ Lee 		pr_warn("snd-dummy: Invalid mixer volume level: min=%d, max=%d. Fall back to default value.\n",
1081446bc11fSYJ Lee 		mixer_volume_level_min, mixer_volume_level_max);
1082446bc11fSYJ Lee 		mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN;
1083446bc11fSYJ Lee 		mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX;
1084446bc11fSYJ Lee 	}
10851a11cb64SJaroslav Kysela 	err = snd_card_dummy_new_mixer(dummy);
10861a11cb64SJaroslav Kysela 	if (err < 0)
1087ed16a22bSTakashi Iwai 		return err;
10881da177e4SLinus Torvalds 	strcpy(card->driver, "Dummy");
10891da177e4SLinus Torvalds 	strcpy(card->shortname, "Dummy");
10901da177e4SLinus Torvalds 	sprintf(card->longname, "Dummy %i", dev + 1);
109116dab54bSTakashi Iwai 
10929b151fecSTakashi Iwai 	dummy_proc_init(dummy);
10939b151fecSTakashi Iwai 
10941a11cb64SJaroslav Kysela 	err = snd_card_register(card);
1095ed16a22bSTakashi Iwai 	if (err < 0)
10961da177e4SLinus Torvalds 		return err;
1097ed16a22bSTakashi Iwai 	platform_set_drvdata(devptr, card);
10986e65c1ccSTakashi Iwai 	return 0;
10996e65c1ccSTakashi Iwai }
11006e65c1ccSTakashi Iwai 
1101d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP
snd_dummy_suspend(struct device * pdev)1102284e7ca7STakashi Iwai static int snd_dummy_suspend(struct device *pdev)
11036e65c1ccSTakashi Iwai {
1104284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11056e65c1ccSTakashi Iwai 
11066e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
11076e65c1ccSTakashi Iwai 	return 0;
11086e65c1ccSTakashi Iwai }
11096e65c1ccSTakashi Iwai 
snd_dummy_resume(struct device * pdev)1110284e7ca7STakashi Iwai static int snd_dummy_resume(struct device *pdev)
11116e65c1ccSTakashi Iwai {
1112284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11136e65c1ccSTakashi Iwai 
11146e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
11156e65c1ccSTakashi Iwai 	return 0;
11166e65c1ccSTakashi Iwai }
1117284e7ca7STakashi Iwai 
1118284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
1119284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	&snd_dummy_pm
1120284e7ca7STakashi Iwai #else
1121284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	NULL
11226e65c1ccSTakashi Iwai #endif
11236e65c1ccSTakashi Iwai 
11246e65c1ccSTakashi Iwai #define SND_DUMMY_DRIVER	"snd_dummy"
11256e65c1ccSTakashi Iwai 
11266e65c1ccSTakashi Iwai static struct platform_driver snd_dummy_driver = {
11276e65c1ccSTakashi Iwai 	.probe		= snd_dummy_probe,
11286e65c1ccSTakashi Iwai 	.driver		= {
11298bf01d8aSTakashi Iwai 		.name	= SND_DUMMY_DRIVER,
1130284e7ca7STakashi Iwai 		.pm	= SND_DUMMY_PM_OPS,
11316e65c1ccSTakashi Iwai 	},
11326e65c1ccSTakashi Iwai };
11336e65c1ccSTakashi Iwai 
snd_dummy_unregister_all(void)1134c12aad6eSRandy Dunlap static void snd_dummy_unregister_all(void)
1135f7a9275dSClemens Ladisch {
1136f7a9275dSClemens Ladisch 	int i;
1137f7a9275dSClemens Ladisch 
1138f7a9275dSClemens Ladisch 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
1139f7a9275dSClemens Ladisch 		platform_device_unregister(devices[i]);
1140f7a9275dSClemens Ladisch 	platform_driver_unregister(&snd_dummy_driver);
1141a68c4d11STakashi Iwai 	free_fake_buffer();
1142f7a9275dSClemens Ladisch }
1143f7a9275dSClemens Ladisch 
alsa_card_dummy_init(void)11441da177e4SLinus Torvalds static int __init alsa_card_dummy_init(void)
11451da177e4SLinus Torvalds {
11466e65c1ccSTakashi Iwai 	int i, cards, err;
11471da177e4SLinus Torvalds 
11481a11cb64SJaroslav Kysela 	err = platform_driver_register(&snd_dummy_driver);
11491a11cb64SJaroslav Kysela 	if (err < 0)
11506e65c1ccSTakashi Iwai 		return err;
11516e65c1ccSTakashi Iwai 
1152a68c4d11STakashi Iwai 	err = alloc_fake_buffer();
1153a68c4d11STakashi Iwai 	if (err < 0) {
1154a68c4d11STakashi Iwai 		platform_driver_unregister(&snd_dummy_driver);
1155a68c4d11STakashi Iwai 		return err;
1156a68c4d11STakashi Iwai 	}
1157a68c4d11STakashi Iwai 
11586e65c1ccSTakashi Iwai 	cards = 0;
11598278ca8fSTakashi Iwai 	for (i = 0; i < SNDRV_CARDS; i++) {
11606e65c1ccSTakashi Iwai 		struct platform_device *device;
11618278ca8fSTakashi Iwai 		if (! enable[i])
11628278ca8fSTakashi Iwai 			continue;
11636e65c1ccSTakashi Iwai 		device = platform_device_register_simple(SND_DUMMY_DRIVER,
11646e65c1ccSTakashi Iwai 							 i, NULL, 0);
1165a182ee98SRene Herman 		if (IS_ERR(device))
1166a182ee98SRene Herman 			continue;
11677152447dSRene Herman 		if (!platform_get_drvdata(device)) {
11687152447dSRene Herman 			platform_device_unregister(device);
11697152447dSRene Herman 			continue;
11707152447dSRene Herman 		}
1171f7a9275dSClemens Ladisch 		devices[i] = device;
11721da177e4SLinus Torvalds 		cards++;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 	if (!cards) {
11751da177e4SLinus Torvalds #ifdef MODULE
11761da177e4SLinus Torvalds 		printk(KERN_ERR "Dummy soundcard not found or device busy\n");
11771da177e4SLinus Torvalds #endif
1178a182ee98SRene Herman 		snd_dummy_unregister_all();
1179a182ee98SRene Herman 		return -ENODEV;
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 	return 0;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
alsa_card_dummy_exit(void)11841da177e4SLinus Torvalds static void __exit alsa_card_dummy_exit(void)
11851da177e4SLinus Torvalds {
1186f7a9275dSClemens Ladisch 	snd_dummy_unregister_all();
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds module_init(alsa_card_dummy_init)
11901da177e4SLinus Torvalds module_exit(alsa_card_dummy_exit)
1191