xref: /openbmc/linux/sound/drivers/dummy.c (revision 8bf01d8abc55eaf8e19a2d48911c8e49ee6f5bab)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Dummy soundcard
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *   GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/init.h>
226e65c1ccSTakashi Iwai #include <linux/err.h>
236e65c1ccSTakashi Iwai #include <linux/platform_device.h>
241da177e4SLinus Torvalds #include <linux/jiffies.h>
251da177e4SLinus Torvalds #include <linux/slab.h>
261da177e4SLinus Torvalds #include <linux/time.h>
271da177e4SLinus Torvalds #include <linux/wait.h>
28c631d03cSTakashi Iwai #include <linux/hrtimer.h>
29c631d03cSTakashi Iwai #include <linux/math64.h>
3065a77217SPaul Gortmaker #include <linux/module.h>
311da177e4SLinus Torvalds #include <sound/core.h>
321da177e4SLinus Torvalds #include <sound/control.h>
33fb567a8eSTakashi Iwai #include <sound/tlv.h>
341da177e4SLinus Torvalds #include <sound/pcm.h>
351da177e4SLinus Torvalds #include <sound/rawmidi.h>
369b151fecSTakashi Iwai #include <sound/info.h>
371da177e4SLinus Torvalds #include <sound/initval.h>
381da177e4SLinus Torvalds 
39c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
401da177e4SLinus Torvalds MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
421da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define MAX_PCM_DEVICES		4
45b888d1ceSTakashi Iwai #define MAX_PCM_SUBSTREAMS	128
461da177e4SLinus Torvalds #define MAX_MIDI_DEVICES	2
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* defaults */
491da177e4SLinus Torvalds #define MAX_BUFFER_SIZE		(64*1024)
50d5e1ca05SJaroslav Kysela #define MIN_PERIOD_SIZE		64
512ad5dd8dSJaroslav Kysela #define MAX_PERIOD_SIZE		MAX_BUFFER_SIZE
521da177e4SLinus Torvalds #define USE_FORMATS 		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
531da177e4SLinus Torvalds #define USE_RATE		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
541da177e4SLinus Torvalds #define USE_RATE_MIN		5500
551da177e4SLinus Torvalds #define USE_RATE_MAX		48000
561da177e4SLinus Torvalds #define USE_CHANNELS_MIN 	1
571da177e4SLinus Torvalds #define USE_CHANNELS_MAX 	2
581da177e4SLinus Torvalds #define USE_PERIODS_MIN 	1
591da177e4SLinus Torvalds #define USE_PERIODS_MAX 	1024
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
621da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
63a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
64d5e1ca05SJaroslav Kysela static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
651da177e4SLinus Torvalds static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
661da177e4SLinus Torvalds static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
671da177e4SLinus Torvalds //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
68c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
69a67ff6a5SRusty Russell static bool hrtimer = 1;
70c631d03cSTakashi Iwai #endif
71a67ff6a5SRusty Russell static bool fake_buffer = 1;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
741da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
751da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
761da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
771da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
781da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
79d5e1ca05SJaroslav Kysela module_param_array(model, charp, NULL, 0444);
80d5e1ca05SJaroslav Kysela MODULE_PARM_DESC(model, "Soundcard model.");
811da177e4SLinus Torvalds module_param_array(pcm_devs, int, NULL, 0444);
821da177e4SLinus Torvalds MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
831da177e4SLinus Torvalds module_param_array(pcm_substreams, int, NULL, 0444);
8423aebca4STakashi Iwai MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
851da177e4SLinus Torvalds //module_param_array(midi_devs, int, NULL, 0444);
861da177e4SLinus Torvalds //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
87a68c4d11STakashi Iwai module_param(fake_buffer, bool, 0444);
88a68c4d11STakashi Iwai MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
89c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
90c631d03cSTakashi Iwai module_param(hrtimer, bool, 0644);
91c631d03cSTakashi Iwai MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
92c631d03cSTakashi Iwai #endif
931da177e4SLinus Torvalds 
94f7a9275dSClemens Ladisch static struct platform_device *devices[SNDRV_CARDS];
95f7a9275dSClemens Ladisch 
961da177e4SLinus Torvalds #define MIXER_ADDR_MASTER	0
971da177e4SLinus Torvalds #define MIXER_ADDR_LINE		1
981da177e4SLinus Torvalds #define MIXER_ADDR_MIC		2
991da177e4SLinus Torvalds #define MIXER_ADDR_SYNTH	3
1001da177e4SLinus Torvalds #define MIXER_ADDR_CD		4
1011da177e4SLinus Torvalds #define MIXER_ADDR_LAST		4
1021da177e4SLinus Torvalds 
103c631d03cSTakashi Iwai struct dummy_timer_ops {
104c631d03cSTakashi Iwai 	int (*create)(struct snd_pcm_substream *);
105c631d03cSTakashi Iwai 	void (*free)(struct snd_pcm_substream *);
106c631d03cSTakashi Iwai 	int (*prepare)(struct snd_pcm_substream *);
107c631d03cSTakashi Iwai 	int (*start)(struct snd_pcm_substream *);
108c631d03cSTakashi Iwai 	int (*stop)(struct snd_pcm_substream *);
109c631d03cSTakashi Iwai 	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
110c631d03cSTakashi Iwai };
111c631d03cSTakashi Iwai 
112d5e1ca05SJaroslav Kysela struct dummy_model {
113d5e1ca05SJaroslav Kysela 	const char *name;
114d5e1ca05SJaroslav Kysela 	int (*playback_constraints)(struct snd_pcm_runtime *runtime);
115d5e1ca05SJaroslav Kysela 	int (*capture_constraints)(struct snd_pcm_runtime *runtime);
116d5e1ca05SJaroslav Kysela 	u64 formats;
117d5e1ca05SJaroslav Kysela 	size_t buffer_bytes_max;
118d5e1ca05SJaroslav Kysela 	size_t period_bytes_min;
119d5e1ca05SJaroslav Kysela 	size_t period_bytes_max;
120d5e1ca05SJaroslav Kysela 	unsigned int periods_min;
121d5e1ca05SJaroslav Kysela 	unsigned int periods_max;
122d5e1ca05SJaroslav Kysela 	unsigned int rates;
123d5e1ca05SJaroslav Kysela 	unsigned int rate_min;
124d5e1ca05SJaroslav Kysela 	unsigned int rate_max;
125d5e1ca05SJaroslav Kysela 	unsigned int channels_min;
126d5e1ca05SJaroslav Kysela 	unsigned int channels_max;
127d5e1ca05SJaroslav Kysela };
128d5e1ca05SJaroslav Kysela 
1294a4d2cfdSTakashi Iwai struct snd_dummy {
1304a4d2cfdSTakashi Iwai 	struct snd_card *card;
131d5e1ca05SJaroslav Kysela 	struct dummy_model *model;
1326e65c1ccSTakashi Iwai 	struct snd_pcm *pcm;
133d5e1ca05SJaroslav Kysela 	struct snd_pcm_hardware pcm_hw;
1341da177e4SLinus Torvalds 	spinlock_t mixer_lock;
1351da177e4SLinus Torvalds 	int mixer_volume[MIXER_ADDR_LAST+1][2];
1361da177e4SLinus Torvalds 	int capture_source[MIXER_ADDR_LAST+1][2];
137c631d03cSTakashi Iwai 	const struct dummy_timer_ops *timer_ops;
1384a4d2cfdSTakashi Iwai };
1391da177e4SLinus Torvalds 
140c631d03cSTakashi Iwai /*
141d5e1ca05SJaroslav Kysela  * card models
142d5e1ca05SJaroslav Kysela  */
143d5e1ca05SJaroslav Kysela 
144d5e1ca05SJaroslav Kysela static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
145d5e1ca05SJaroslav Kysela {
146d5e1ca05SJaroslav Kysela 	int err;
147d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
148d5e1ca05SJaroslav Kysela 	if (err < 0)
149d5e1ca05SJaroslav Kysela 		return err;
150d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
151d5e1ca05SJaroslav Kysela 	if (err < 0)
152d5e1ca05SJaroslav Kysela 		return err;
153d5e1ca05SJaroslav Kysela 	return 0;
154d5e1ca05SJaroslav Kysela }
155d5e1ca05SJaroslav Kysela 
156d5e1ca05SJaroslav Kysela struct dummy_model model_emu10k1 = {
157d5e1ca05SJaroslav Kysela 	.name = "emu10k1",
158d5e1ca05SJaroslav Kysela 	.playback_constraints = emu10k1_playback_constraints,
159d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 128 * 1024,
160d5e1ca05SJaroslav Kysela };
161d5e1ca05SJaroslav Kysela 
162d5e1ca05SJaroslav Kysela struct dummy_model model_rme9652 = {
163d5e1ca05SJaroslav Kysela 	.name = "rme9652",
164d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 26 * 64 * 1024,
165d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
166d5e1ca05SJaroslav Kysela 	.channels_min = 26,
167d5e1ca05SJaroslav Kysela 	.channels_max = 26,
168d5e1ca05SJaroslav Kysela 	.periods_min = 2,
169d5e1ca05SJaroslav Kysela 	.periods_max = 2,
170d5e1ca05SJaroslav Kysela };
171d5e1ca05SJaroslav Kysela 
172d5e1ca05SJaroslav Kysela struct dummy_model model_ice1712 = {
173d5e1ca05SJaroslav Kysela 	.name = "ice1712",
174d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 256 * 1024,
175d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
176d5e1ca05SJaroslav Kysela 	.channels_min = 10,
177d5e1ca05SJaroslav Kysela 	.channels_max = 10,
178d5e1ca05SJaroslav Kysela 	.periods_min = 1,
179d5e1ca05SJaroslav Kysela 	.periods_max = 1024,
180d5e1ca05SJaroslav Kysela };
181d5e1ca05SJaroslav Kysela 
182d5e1ca05SJaroslav Kysela struct dummy_model model_uda1341 = {
183d5e1ca05SJaroslav Kysela 	.name = "uda1341",
184d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 16380,
185d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
186d5e1ca05SJaroslav Kysela 	.channels_min = 2,
187d5e1ca05SJaroslav Kysela 	.channels_max = 2,
188d5e1ca05SJaroslav Kysela 	.periods_min = 2,
189d5e1ca05SJaroslav Kysela 	.periods_max = 255,
190d5e1ca05SJaroslav Kysela };
191d5e1ca05SJaroslav Kysela 
192d5e1ca05SJaroslav Kysela struct dummy_model model_ac97 = {
193d5e1ca05SJaroslav Kysela 	.name = "ac97",
194d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
195d5e1ca05SJaroslav Kysela 	.channels_min = 2,
196d5e1ca05SJaroslav Kysela 	.channels_max = 2,
197d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000,
198d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
199d5e1ca05SJaroslav Kysela 	.rate_max = 48000,
200d5e1ca05SJaroslav Kysela };
201d5e1ca05SJaroslav Kysela 
202d5e1ca05SJaroslav Kysela struct dummy_model model_ca0106 = {
203d5e1ca05SJaroslav Kysela 	.name = "ca0106",
204d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
205d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = ((65536-64)*8),
206d5e1ca05SJaroslav Kysela 	.period_bytes_max = (65536-64),
207d5e1ca05SJaroslav Kysela 	.periods_min = 2,
208d5e1ca05SJaroslav Kysela 	.periods_max = 8,
209d5e1ca05SJaroslav Kysela 	.channels_min = 2,
210d5e1ca05SJaroslav Kysela 	.channels_max = 2,
211d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
212d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
213d5e1ca05SJaroslav Kysela 	.rate_max = 192000,
214d5e1ca05SJaroslav Kysela };
215d5e1ca05SJaroslav Kysela 
216d5e1ca05SJaroslav Kysela struct dummy_model *dummy_models[] = {
217d5e1ca05SJaroslav Kysela 	&model_emu10k1,
218d5e1ca05SJaroslav Kysela 	&model_rme9652,
219d5e1ca05SJaroslav Kysela 	&model_ice1712,
220d5e1ca05SJaroslav Kysela 	&model_uda1341,
221d5e1ca05SJaroslav Kysela 	&model_ac97,
222d5e1ca05SJaroslav Kysela 	&model_ca0106,
223d5e1ca05SJaroslav Kysela 	NULL
224d5e1ca05SJaroslav Kysela };
225d5e1ca05SJaroslav Kysela 
226d5e1ca05SJaroslav Kysela /*
227c631d03cSTakashi Iwai  * system timer interface
228c631d03cSTakashi Iwai  */
229c631d03cSTakashi Iwai 
230c631d03cSTakashi Iwai struct dummy_systimer_pcm {
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 
243b142037bSTakashi Iwai static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
244b142037bSTakashi Iwai {
245b142037bSTakashi Iwai 	dpcm->timer.expires = jiffies +
246b5d10781STakashi Iwai 		(dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
247b142037bSTakashi Iwai 	add_timer(&dpcm->timer);
248b142037bSTakashi Iwai }
249b142037bSTakashi Iwai 
250b142037bSTakashi Iwai static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
251b142037bSTakashi Iwai {
252b142037bSTakashi Iwai 	unsigned long delta;
253b142037bSTakashi Iwai 
254b142037bSTakashi Iwai 	delta = jiffies - dpcm->base_time;
255b142037bSTakashi Iwai 	if (!delta)
256b142037bSTakashi Iwai 		return;
257b5d10781STakashi Iwai 	dpcm->base_time += delta;
258b5d10781STakashi Iwai 	delta *= dpcm->rate;
259b5d10781STakashi Iwai 	dpcm->frac_pos += delta;
260b142037bSTakashi Iwai 	while (dpcm->frac_pos >= dpcm->frac_buffer_size)
261b142037bSTakashi Iwai 		dpcm->frac_pos -= dpcm->frac_buffer_size;
262b5d10781STakashi Iwai 	while (dpcm->frac_period_rest <= delta) {
263b5d10781STakashi Iwai 		dpcm->elapsed++;
264b5d10781STakashi Iwai 		dpcm->frac_period_rest += dpcm->frac_period_size;
265b5d10781STakashi Iwai 	}
266b5d10781STakashi Iwai 	dpcm->frac_period_rest -= delta;
267b142037bSTakashi Iwai }
268b142037bSTakashi Iwai 
269c631d03cSTakashi Iwai static int dummy_systimer_start(struct snd_pcm_substream *substream)
2701da177e4SLinus Torvalds {
271c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
272c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
273b142037bSTakashi Iwai 	dpcm->base_time = jiffies;
274b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
275c8eb6ba1STakashi Iwai 	spin_unlock(&dpcm->lock);
2766e65c1ccSTakashi Iwai 	return 0;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279c631d03cSTakashi Iwai static int dummy_systimer_stop(struct snd_pcm_substream *substream)
280c631d03cSTakashi Iwai {
281c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
282c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
283c631d03cSTakashi Iwai 	del_timer(&dpcm->timer);
284c631d03cSTakashi Iwai 	spin_unlock(&dpcm->lock);
285c631d03cSTakashi Iwai 	return 0;
286c631d03cSTakashi Iwai }
287c631d03cSTakashi Iwai 
288c631d03cSTakashi Iwai static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
2891da177e4SLinus Torvalds {
2904a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
291c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = runtime->private_data;
2921da177e4SLinus Torvalds 
293b142037bSTakashi Iwai 	dpcm->frac_pos = 0;
294b142037bSTakashi Iwai 	dpcm->rate = runtime->rate;
295b142037bSTakashi Iwai 	dpcm->frac_buffer_size = runtime->buffer_size * HZ;
296b142037bSTakashi Iwai 	dpcm->frac_period_size = runtime->period_size * HZ;
297b5d10781STakashi Iwai 	dpcm->frac_period_rest = dpcm->frac_period_size;
298b5d10781STakashi Iwai 	dpcm->elapsed = 0;
29953463a83SAhmet İnan 
3001da177e4SLinus Torvalds 	return 0;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
303c631d03cSTakashi Iwai static void dummy_systimer_callback(unsigned long data)
3041da177e4SLinus Torvalds {
305c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
306b32425acSTakashi Iwai 	unsigned long flags;
307b5d10781STakashi Iwai 	int elapsed = 0;
3081da177e4SLinus Torvalds 
309b32425acSTakashi Iwai 	spin_lock_irqsave(&dpcm->lock, flags);
310b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
311b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
312b5d10781STakashi Iwai 	elapsed = dpcm->elapsed;
313b5d10781STakashi Iwai 	dpcm->elapsed = 0;
314b32425acSTakashi Iwai 	spin_unlock_irqrestore(&dpcm->lock, flags);
315b5d10781STakashi Iwai 	if (elapsed)
3161da177e4SLinus Torvalds 		snd_pcm_period_elapsed(dpcm->substream);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
319c631d03cSTakashi Iwai static snd_pcm_uframes_t
320c631d03cSTakashi Iwai dummy_systimer_pointer(struct snd_pcm_substream *substream)
3211da177e4SLinus Torvalds {
322b142037bSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
323b5d10781STakashi Iwai 	snd_pcm_uframes_t pos;
3241da177e4SLinus Torvalds 
325b142037bSTakashi Iwai 	spin_lock(&dpcm->lock);
326b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
327b5d10781STakashi Iwai 	pos = dpcm->frac_pos / HZ;
328b142037bSTakashi Iwai 	spin_unlock(&dpcm->lock);
329b5d10781STakashi Iwai 	return pos;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
332c631d03cSTakashi Iwai static int dummy_systimer_create(struct snd_pcm_substream *substream)
3331da177e4SLinus Torvalds {
334c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm;
3351da177e4SLinus Torvalds 
336c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
337c631d03cSTakashi Iwai 	if (!dpcm)
338c631d03cSTakashi Iwai 		return -ENOMEM;
339c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
340c631d03cSTakashi Iwai 	init_timer(&dpcm->timer);
341c631d03cSTakashi Iwai 	dpcm->timer.data = (unsigned long) dpcm;
342c631d03cSTakashi Iwai 	dpcm->timer.function = dummy_systimer_callback;
343c631d03cSTakashi Iwai 	spin_lock_init(&dpcm->lock);
344c631d03cSTakashi Iwai 	dpcm->substream = substream;
345c631d03cSTakashi Iwai 	return 0;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348c631d03cSTakashi Iwai static void dummy_systimer_free(struct snd_pcm_substream *substream)
349c631d03cSTakashi Iwai {
350c631d03cSTakashi Iwai 	kfree(substream->runtime->private_data);
351c631d03cSTakashi Iwai }
352c631d03cSTakashi Iwai 
353c631d03cSTakashi Iwai static struct dummy_timer_ops dummy_systimer_ops = {
354c631d03cSTakashi Iwai 	.create =	dummy_systimer_create,
355c631d03cSTakashi Iwai 	.free =		dummy_systimer_free,
356c631d03cSTakashi Iwai 	.prepare =	dummy_systimer_prepare,
357c631d03cSTakashi Iwai 	.start =	dummy_systimer_start,
358c631d03cSTakashi Iwai 	.stop =		dummy_systimer_stop,
359c631d03cSTakashi Iwai 	.pointer =	dummy_systimer_pointer,
360c631d03cSTakashi Iwai };
361c631d03cSTakashi Iwai 
362c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
363c631d03cSTakashi Iwai /*
364c631d03cSTakashi Iwai  * hrtimer interface
365c631d03cSTakashi Iwai  */
366c631d03cSTakashi Iwai 
367c631d03cSTakashi Iwai struct dummy_hrtimer_pcm {
368c631d03cSTakashi Iwai 	ktime_t base_time;
369c631d03cSTakashi Iwai 	ktime_t period_time;
370c631d03cSTakashi Iwai 	atomic_t running;
371c631d03cSTakashi Iwai 	struct hrtimer timer;
372c631d03cSTakashi Iwai 	struct tasklet_struct tasklet;
373c631d03cSTakashi Iwai 	struct snd_pcm_substream *substream;
374c631d03cSTakashi Iwai };
375c631d03cSTakashi Iwai 
376c631d03cSTakashi Iwai static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
377c631d03cSTakashi Iwai {
378c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
379c631d03cSTakashi Iwai 	if (atomic_read(&dpcm->running))
380c631d03cSTakashi Iwai 		snd_pcm_period_elapsed(dpcm->substream);
381c631d03cSTakashi Iwai }
382c631d03cSTakashi Iwai 
383c631d03cSTakashi Iwai static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
384c631d03cSTakashi Iwai {
385c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
386c631d03cSTakashi Iwai 
387c631d03cSTakashi Iwai 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
388c631d03cSTakashi Iwai 	if (!atomic_read(&dpcm->running))
389c631d03cSTakashi Iwai 		return HRTIMER_NORESTART;
390c631d03cSTakashi Iwai 	tasklet_schedule(&dpcm->tasklet);
391c631d03cSTakashi Iwai 	hrtimer_forward_now(timer, dpcm->period_time);
392c631d03cSTakashi Iwai 	return HRTIMER_RESTART;
393c631d03cSTakashi Iwai }
394c631d03cSTakashi Iwai 
395c631d03cSTakashi Iwai static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
396c631d03cSTakashi Iwai {
397c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
398c631d03cSTakashi Iwai 
399c631d03cSTakashi Iwai 	dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
400c631d03cSTakashi Iwai 	hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
401c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 1);
402c631d03cSTakashi Iwai 	return 0;
403c631d03cSTakashi Iwai }
404c631d03cSTakashi Iwai 
405c631d03cSTakashi Iwai static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
406c631d03cSTakashi Iwai {
407c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
408c631d03cSTakashi Iwai 
409c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
410c631d03cSTakashi Iwai 	hrtimer_cancel(&dpcm->timer);
411c631d03cSTakashi Iwai 	return 0;
412c631d03cSTakashi Iwai }
413c631d03cSTakashi Iwai 
414c631d03cSTakashi Iwai static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
415c631d03cSTakashi Iwai {
416c631d03cSTakashi Iwai 	tasklet_kill(&dpcm->tasklet);
417c631d03cSTakashi Iwai }
418c631d03cSTakashi Iwai 
419c631d03cSTakashi Iwai static snd_pcm_uframes_t
420c631d03cSTakashi Iwai dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
421c631d03cSTakashi Iwai {
422c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
423c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
424c631d03cSTakashi Iwai 	u64 delta;
425c631d03cSTakashi Iwai 	u32 pos;
426c631d03cSTakashi Iwai 
427c631d03cSTakashi Iwai 	delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
428c631d03cSTakashi Iwai 			       dpcm->base_time);
429c631d03cSTakashi Iwai 	delta = div_u64(delta * runtime->rate + 999999, 1000000);
430c631d03cSTakashi Iwai 	div_u64_rem(delta, runtime->buffer_size, &pos);
431c631d03cSTakashi Iwai 	return pos;
432c631d03cSTakashi Iwai }
433c631d03cSTakashi Iwai 
434c631d03cSTakashi Iwai static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
435c631d03cSTakashi Iwai {
436c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
437c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
438c631d03cSTakashi Iwai 	unsigned int period, rate;
439c631d03cSTakashi Iwai 	long sec;
440c631d03cSTakashi Iwai 	unsigned long nsecs;
441c631d03cSTakashi Iwai 
442c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
443c631d03cSTakashi Iwai 	period = runtime->period_size;
444c631d03cSTakashi Iwai 	rate = runtime->rate;
445c631d03cSTakashi Iwai 	sec = period / rate;
446c631d03cSTakashi Iwai 	period %= rate;
447c631d03cSTakashi Iwai 	nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
448c631d03cSTakashi Iwai 	dpcm->period_time = ktime_set(sec, nsecs);
449c631d03cSTakashi Iwai 
450c631d03cSTakashi Iwai 	return 0;
451c631d03cSTakashi Iwai }
452c631d03cSTakashi Iwai 
453c631d03cSTakashi Iwai static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
454c631d03cSTakashi Iwai {
455c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
456c631d03cSTakashi Iwai 
457c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
458c631d03cSTakashi Iwai 	if (!dpcm)
459c631d03cSTakashi Iwai 		return -ENOMEM;
460c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
461c631d03cSTakashi Iwai 	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
462c631d03cSTakashi Iwai 	dpcm->timer.function = dummy_hrtimer_callback;
463c631d03cSTakashi Iwai 	dpcm->substream = substream;
464c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
465c631d03cSTakashi Iwai 	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
466c631d03cSTakashi Iwai 		     (unsigned long)dpcm);
467c631d03cSTakashi Iwai 	return 0;
468c631d03cSTakashi Iwai }
469c631d03cSTakashi Iwai 
470c631d03cSTakashi Iwai static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
471c631d03cSTakashi Iwai {
472c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
473c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
474c631d03cSTakashi Iwai 	kfree(dpcm);
475c631d03cSTakashi Iwai }
476c631d03cSTakashi Iwai 
477c631d03cSTakashi Iwai static struct dummy_timer_ops dummy_hrtimer_ops = {
478c631d03cSTakashi Iwai 	.create =	dummy_hrtimer_create,
479c631d03cSTakashi Iwai 	.free =		dummy_hrtimer_free,
480c631d03cSTakashi Iwai 	.prepare =	dummy_hrtimer_prepare,
481c631d03cSTakashi Iwai 	.start =	dummy_hrtimer_start,
482c631d03cSTakashi Iwai 	.stop =		dummy_hrtimer_stop,
483c631d03cSTakashi Iwai 	.pointer =	dummy_hrtimer_pointer,
484c631d03cSTakashi Iwai };
485c631d03cSTakashi Iwai 
486c631d03cSTakashi Iwai #endif /* CONFIG_HIGH_RES_TIMERS */
487c631d03cSTakashi Iwai 
488c631d03cSTakashi Iwai /*
489c631d03cSTakashi Iwai  * PCM interface
490c631d03cSTakashi Iwai  */
491c631d03cSTakashi Iwai 
492c631d03cSTakashi Iwai static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
493c631d03cSTakashi Iwai {
494c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
495c631d03cSTakashi Iwai 
496c631d03cSTakashi Iwai 	switch (cmd) {
497c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_START:
498c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
499c631d03cSTakashi Iwai 		return dummy->timer_ops->start(substream);
500c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_STOP:
501c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
502c631d03cSTakashi Iwai 		return dummy->timer_ops->stop(substream);
503c631d03cSTakashi Iwai 	}
504c631d03cSTakashi Iwai 	return -EINVAL;
505c631d03cSTakashi Iwai }
506c631d03cSTakashi Iwai 
507c631d03cSTakashi Iwai static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
508c631d03cSTakashi Iwai {
509c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
510c631d03cSTakashi Iwai 
511c631d03cSTakashi Iwai 	return dummy->timer_ops->prepare(substream);
512c631d03cSTakashi Iwai }
513c631d03cSTakashi Iwai 
514c631d03cSTakashi Iwai static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
515c631d03cSTakashi Iwai {
516c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
517c631d03cSTakashi Iwai 
518c631d03cSTakashi Iwai 	return dummy->timer_ops->pointer(substream);
519c631d03cSTakashi Iwai }
520c631d03cSTakashi Iwai 
521c631d03cSTakashi Iwai static struct snd_pcm_hardware dummy_pcm_hardware = {
522c631d03cSTakashi Iwai 	.info =			(SNDRV_PCM_INFO_MMAP |
523c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_INTERLEAVED |
524c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_RESUME |
525c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_MMAP_VALID),
526c631d03cSTakashi Iwai 	.formats =		USE_FORMATS,
527c631d03cSTakashi Iwai 	.rates =		USE_RATE,
528c631d03cSTakashi Iwai 	.rate_min =		USE_RATE_MIN,
529c631d03cSTakashi Iwai 	.rate_max =		USE_RATE_MAX,
530c631d03cSTakashi Iwai 	.channels_min =		USE_CHANNELS_MIN,
531c631d03cSTakashi Iwai 	.channels_max =		USE_CHANNELS_MAX,
532c631d03cSTakashi Iwai 	.buffer_bytes_max =	MAX_BUFFER_SIZE,
533d5e1ca05SJaroslav Kysela 	.period_bytes_min =	MIN_PERIOD_SIZE,
534c631d03cSTakashi Iwai 	.period_bytes_max =	MAX_PERIOD_SIZE,
535c631d03cSTakashi Iwai 	.periods_min =		USE_PERIODS_MIN,
536c631d03cSTakashi Iwai 	.periods_max =		USE_PERIODS_MAX,
537c631d03cSTakashi Iwai 	.fifo_size =		0,
538c631d03cSTakashi Iwai };
539c631d03cSTakashi Iwai 
540c631d03cSTakashi Iwai static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
5414a4d2cfdSTakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
5421da177e4SLinus Torvalds {
543a68c4d11STakashi Iwai 	if (fake_buffer) {
544a68c4d11STakashi Iwai 		/* runtime->dma_bytes has to be set manually to allow mmap */
545a68c4d11STakashi Iwai 		substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
546a68c4d11STakashi Iwai 		return 0;
547a68c4d11STakashi Iwai 	}
548c631d03cSTakashi Iwai 	return snd_pcm_lib_malloc_pages(substream,
549c631d03cSTakashi Iwai 					params_buffer_bytes(hw_params));
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
552c631d03cSTakashi Iwai static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
5531da177e4SLinus Torvalds {
554a68c4d11STakashi Iwai 	if (fake_buffer)
555a68c4d11STakashi Iwai 		return 0;
5561da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
559c631d03cSTakashi Iwai static int dummy_pcm_open(struct snd_pcm_substream *substream)
560c8eb6ba1STakashi Iwai {
561c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
562d5e1ca05SJaroslav Kysela 	struct dummy_model *model = dummy->model;
5634a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
5641da177e4SLinus Torvalds 	int err;
5651da177e4SLinus Torvalds 
566c631d03cSTakashi Iwai 	dummy->timer_ops = &dummy_systimer_ops;
567c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
568c631d03cSTakashi Iwai 	if (hrtimer)
569c631d03cSTakashi Iwai 		dummy->timer_ops = &dummy_hrtimer_ops;
570c631d03cSTakashi Iwai #endif
571c631d03cSTakashi Iwai 
572c631d03cSTakashi Iwai 	err = dummy->timer_ops->create(substream);
573c631d03cSTakashi Iwai 	if (err < 0)
574c631d03cSTakashi Iwai 		return err;
575c631d03cSTakashi Iwai 
576d5e1ca05SJaroslav Kysela 	runtime->hw = dummy->pcm_hw;
5771da177e4SLinus Torvalds 	if (substream->pcm->device & 1) {
5781da177e4SLinus Torvalds 		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
5791da177e4SLinus Torvalds 		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	if (substream->pcm->device & 2)
582c631d03cSTakashi Iwai 		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
583c631d03cSTakashi Iwai 				      SNDRV_PCM_INFO_MMAP_VALID);
584c631d03cSTakashi Iwai 
585d5e1ca05SJaroslav Kysela 	if (model == NULL)
586d5e1ca05SJaroslav Kysela 		return 0;
587d5e1ca05SJaroslav Kysela 
588d5e1ca05SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
589d5e1ca05SJaroslav Kysela 		if (model->playback_constraints)
590d5e1ca05SJaroslav Kysela 			err = model->playback_constraints(substream->runtime);
591d5e1ca05SJaroslav Kysela 	} else {
592d5e1ca05SJaroslav Kysela 		if (model->capture_constraints)
593d5e1ca05SJaroslav Kysela 			err = model->capture_constraints(substream->runtime);
594d5e1ca05SJaroslav Kysela 	}
595c631d03cSTakashi Iwai 	if (err < 0) {
596c631d03cSTakashi Iwai 		dummy->timer_ops->free(substream);
5971da177e4SLinus Torvalds 		return err;
598c631d03cSTakashi Iwai 	}
5991da177e4SLinus Torvalds 	return 0;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602c631d03cSTakashi Iwai static int dummy_pcm_close(struct snd_pcm_substream *substream)
6031da177e4SLinus Torvalds {
604c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
605c631d03cSTakashi Iwai 	dummy->timer_ops->free(substream);
6061da177e4SLinus Torvalds 	return 0;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
609a68c4d11STakashi Iwai /*
610a68c4d11STakashi Iwai  * dummy buffer handling
611a68c4d11STakashi Iwai  */
612a68c4d11STakashi Iwai 
613a68c4d11STakashi Iwai static void *dummy_page[2];
614a68c4d11STakashi Iwai 
615a68c4d11STakashi Iwai static void free_fake_buffer(void)
616a68c4d11STakashi Iwai {
617a68c4d11STakashi Iwai 	if (fake_buffer) {
618a68c4d11STakashi Iwai 		int i;
619a68c4d11STakashi Iwai 		for (i = 0; i < 2; i++)
620a68c4d11STakashi Iwai 			if (dummy_page[i]) {
621a68c4d11STakashi Iwai 				free_page((unsigned long)dummy_page[i]);
622a68c4d11STakashi Iwai 				dummy_page[i] = NULL;
623a68c4d11STakashi Iwai 			}
624a68c4d11STakashi Iwai 	}
625a68c4d11STakashi Iwai }
626a68c4d11STakashi Iwai 
627a68c4d11STakashi Iwai static int alloc_fake_buffer(void)
628a68c4d11STakashi Iwai {
629a68c4d11STakashi Iwai 	int i;
630a68c4d11STakashi Iwai 
631a68c4d11STakashi Iwai 	if (!fake_buffer)
632a68c4d11STakashi Iwai 		return 0;
633a68c4d11STakashi Iwai 	for (i = 0; i < 2; i++) {
634a68c4d11STakashi Iwai 		dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
635a68c4d11STakashi Iwai 		if (!dummy_page[i]) {
636a68c4d11STakashi Iwai 			free_fake_buffer();
637a68c4d11STakashi Iwai 			return -ENOMEM;
638a68c4d11STakashi Iwai 		}
639a68c4d11STakashi Iwai 	}
640a68c4d11STakashi Iwai 	return 0;
641a68c4d11STakashi Iwai }
642a68c4d11STakashi Iwai 
643a68c4d11STakashi Iwai static int dummy_pcm_copy(struct snd_pcm_substream *substream,
644a68c4d11STakashi Iwai 			  int channel, snd_pcm_uframes_t pos,
645a68c4d11STakashi Iwai 			  void __user *dst, snd_pcm_uframes_t count)
646a68c4d11STakashi Iwai {
647a68c4d11STakashi Iwai 	return 0; /* do nothing */
648a68c4d11STakashi Iwai }
649a68c4d11STakashi Iwai 
650a68c4d11STakashi Iwai static int dummy_pcm_silence(struct snd_pcm_substream *substream,
651a68c4d11STakashi Iwai 			     int channel, snd_pcm_uframes_t pos,
652a68c4d11STakashi Iwai 			     snd_pcm_uframes_t count)
653a68c4d11STakashi Iwai {
654a68c4d11STakashi Iwai 	return 0; /* do nothing */
655a68c4d11STakashi Iwai }
656a68c4d11STakashi Iwai 
657a68c4d11STakashi Iwai static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
658a68c4d11STakashi Iwai 				   unsigned long offset)
659a68c4d11STakashi Iwai {
660a68c4d11STakashi Iwai 	return virt_to_page(dummy_page[substream->stream]); /* the same page */
661a68c4d11STakashi Iwai }
662a68c4d11STakashi Iwai 
663c631d03cSTakashi Iwai static struct snd_pcm_ops dummy_pcm_ops = {
664c631d03cSTakashi Iwai 	.open =		dummy_pcm_open,
665c631d03cSTakashi Iwai 	.close =	dummy_pcm_close,
6661da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
667c631d03cSTakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
668c631d03cSTakashi Iwai 	.hw_free =	dummy_pcm_hw_free,
669c631d03cSTakashi Iwai 	.prepare =	dummy_pcm_prepare,
670c631d03cSTakashi Iwai 	.trigger =	dummy_pcm_trigger,
671c631d03cSTakashi Iwai 	.pointer =	dummy_pcm_pointer,
6721da177e4SLinus Torvalds };
6731da177e4SLinus Torvalds 
674a68c4d11STakashi Iwai static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
675a68c4d11STakashi Iwai 	.open =		dummy_pcm_open,
676a68c4d11STakashi Iwai 	.close =	dummy_pcm_close,
677a68c4d11STakashi Iwai 	.ioctl =	snd_pcm_lib_ioctl,
678a68c4d11STakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
679a68c4d11STakashi Iwai 	.hw_free =	dummy_pcm_hw_free,
680a68c4d11STakashi Iwai 	.prepare =	dummy_pcm_prepare,
681a68c4d11STakashi Iwai 	.trigger =	dummy_pcm_trigger,
682a68c4d11STakashi Iwai 	.pointer =	dummy_pcm_pointer,
683a68c4d11STakashi Iwai 	.copy =		dummy_pcm_copy,
684a68c4d11STakashi Iwai 	.silence =	dummy_pcm_silence,
685a68c4d11STakashi Iwai 	.page =		dummy_pcm_page,
686a68c4d11STakashi Iwai };
687a68c4d11STakashi Iwai 
688788c6043SPrarit Bhargava static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
689788c6043SPrarit Bhargava 					int substreams)
6901da177e4SLinus Torvalds {
6914a4d2cfdSTakashi Iwai 	struct snd_pcm *pcm;
692a68c4d11STakashi Iwai 	struct snd_pcm_ops *ops;
6931da177e4SLinus Torvalds 	int err;
6941da177e4SLinus Torvalds 
6951a11cb64SJaroslav Kysela 	err = snd_pcm_new(dummy->card, "Dummy PCM", device,
6961a11cb64SJaroslav Kysela 			       substreams, substreams, &pcm);
6971a11cb64SJaroslav Kysela 	if (err < 0)
6981da177e4SLinus Torvalds 		return err;
6996e65c1ccSTakashi Iwai 	dummy->pcm = pcm;
700a68c4d11STakashi Iwai 	if (fake_buffer)
701a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops_no_buf;
702a68c4d11STakashi Iwai 	else
703a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops;
704a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
705a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
7061da177e4SLinus Torvalds 	pcm->private_data = dummy;
7071da177e4SLinus Torvalds 	pcm->info_flags = 0;
7081da177e4SLinus Torvalds 	strcpy(pcm->name, "Dummy PCM");
709a68c4d11STakashi Iwai 	if (!fake_buffer) {
710a68c4d11STakashi Iwai 		snd_pcm_lib_preallocate_pages_for_all(pcm,
711a68c4d11STakashi Iwai 			SNDRV_DMA_TYPE_CONTINUOUS,
7121da177e4SLinus Torvalds 			snd_dma_continuous_data(GFP_KERNEL),
7131da177e4SLinus Torvalds 			0, 64*1024);
714a68c4d11STakashi Iwai 	}
7151da177e4SLinus Torvalds 	return 0;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7189b151fecSTakashi Iwai /*
7199b151fecSTakashi Iwai  * mixer interface
7209b151fecSTakashi Iwai  */
7219b151fecSTakashi Iwai 
7221da177e4SLinus Torvalds #define DUMMY_VOLUME(xname, xindex, addr) \
723fb567a8eSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
724fb567a8eSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
725fb567a8eSTakashi Iwai   .name = xname, .index = xindex, \
7261da177e4SLinus Torvalds   .info = snd_dummy_volume_info, \
7271da177e4SLinus Torvalds   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
728fb567a8eSTakashi Iwai   .private_value = addr, \
729fb567a8eSTakashi Iwai   .tlv = { .p = db_scale_dummy } }
7301da177e4SLinus Torvalds 
7314a4d2cfdSTakashi Iwai static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
7324a4d2cfdSTakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7351da177e4SLinus Torvalds 	uinfo->count = 2;
7361da177e4SLinus Torvalds 	uinfo->value.integer.min = -50;
7371da177e4SLinus Torvalds 	uinfo->value.integer.max = 100;
7381da177e4SLinus Torvalds 	return 0;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7414a4d2cfdSTakashi Iwai static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
7424a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7431da177e4SLinus Torvalds {
7444a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7451da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7461da177e4SLinus Torvalds 
747c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7481da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
7491da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
750c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7511da177e4SLinus Torvalds 	return 0;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7544a4d2cfdSTakashi Iwai static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
7554a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7561da177e4SLinus Torvalds {
7574a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7581da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7591da177e4SLinus Torvalds 	int left, right;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0];
7621da177e4SLinus Torvalds 	if (left < -50)
7631da177e4SLinus Torvalds 		left = -50;
7641da177e4SLinus Torvalds 	if (left > 100)
7651da177e4SLinus Torvalds 		left = 100;
7661da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1];
7671da177e4SLinus Torvalds 	if (right < -50)
7681da177e4SLinus Torvalds 		right = -50;
7691da177e4SLinus Torvalds 	if (right > 100)
7701da177e4SLinus Torvalds 		right = 100;
771c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7721da177e4SLinus Torvalds 	change = dummy->mixer_volume[addr][0] != left ||
7731da177e4SLinus Torvalds 	         dummy->mixer_volume[addr][1] != right;
7741da177e4SLinus Torvalds 	dummy->mixer_volume[addr][0] = left;
7751da177e4SLinus Torvalds 	dummy->mixer_volume[addr][1] = right;
776c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7771da177e4SLinus Torvalds 	return change;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7800cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
781fb567a8eSTakashi Iwai 
7821da177e4SLinus Torvalds #define DUMMY_CAPSRC(xname, xindex, addr) \
7831da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
7841da177e4SLinus Torvalds   .info = snd_dummy_capsrc_info, \
7851da177e4SLinus Torvalds   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
7861da177e4SLinus Torvalds   .private_value = addr }
7871da177e4SLinus Torvalds 
788a5ce8890STakashi Iwai #define snd_dummy_capsrc_info	snd_ctl_boolean_stereo_info
7891da177e4SLinus Torvalds 
7904a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
7914a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7921da177e4SLinus Torvalds {
7934a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7941da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7951da177e4SLinus Torvalds 
796c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7971da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
7981da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
799c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
8001da177e4SLinus Torvalds 	return 0;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8034a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
8041da177e4SLinus Torvalds {
8054a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
8061da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
8071da177e4SLinus Torvalds 	int left, right;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0] & 1;
8101da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1] & 1;
811c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
8121da177e4SLinus Torvalds 	change = dummy->capture_source[addr][0] != left &&
8131da177e4SLinus Torvalds 	         dummy->capture_source[addr][1] != right;
8141da177e4SLinus Torvalds 	dummy->capture_source[addr][0] = left;
8151da177e4SLinus Torvalds 	dummy->capture_source[addr][1] = right;
816c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
8171da177e4SLinus Torvalds 	return change;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8204a4d2cfdSTakashi Iwai static struct snd_kcontrol_new snd_dummy_controls[] = {
8211da177e4SLinus Torvalds DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
8221da177e4SLinus Torvalds DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
8231da177e4SLinus Torvalds DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
824e05d6964STakashi Iwai DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
8251da177e4SLinus Torvalds DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
826e05d6964STakashi Iwai DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
8271da177e4SLinus Torvalds DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
828e05d6964STakashi Iwai DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
8291da177e4SLinus Torvalds DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
830e05d6964STakashi Iwai DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
8311da177e4SLinus Torvalds };
8321da177e4SLinus Torvalds 
833788c6043SPrarit Bhargava static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
8341da177e4SLinus Torvalds {
8354a4d2cfdSTakashi Iwai 	struct snd_card *card = dummy->card;
8361da177e4SLinus Torvalds 	unsigned int idx;
8371da177e4SLinus Torvalds 	int err;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	spin_lock_init(&dummy->mixer_lock);
8401da177e4SLinus Torvalds 	strcpy(card->mixername, "Dummy Mixer");
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
8431a11cb64SJaroslav Kysela 		err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
8441a11cb64SJaroslav Kysela 		if (err < 0)
8451da177e4SLinus Torvalds 			return err;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 	return 0;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8509b151fecSTakashi Iwai #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS)
8519b151fecSTakashi Iwai /*
8529b151fecSTakashi Iwai  * proc interface
8539b151fecSTakashi Iwai  */
854d5e1ca05SJaroslav Kysela static void print_formats(struct snd_dummy *dummy,
855d5e1ca05SJaroslav Kysela 			  struct snd_info_buffer *buffer)
8569b151fecSTakashi Iwai {
8579b151fecSTakashi Iwai 	int i;
8589b151fecSTakashi Iwai 
8599b151fecSTakashi Iwai 	for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
860d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.formats & (1ULL << i))
8619b151fecSTakashi Iwai 			snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
8629b151fecSTakashi Iwai 	}
8639b151fecSTakashi Iwai }
8649b151fecSTakashi Iwai 
865d5e1ca05SJaroslav Kysela static void print_rates(struct snd_dummy *dummy,
866d5e1ca05SJaroslav Kysela 			struct snd_info_buffer *buffer)
8679b151fecSTakashi Iwai {
8689b151fecSTakashi Iwai 	static int rates[] = {
8699b151fecSTakashi Iwai 		5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
8709b151fecSTakashi Iwai 		64000, 88200, 96000, 176400, 192000,
8719b151fecSTakashi Iwai 	};
8729b151fecSTakashi Iwai 	int i;
8739b151fecSTakashi Iwai 
874d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
8759b151fecSTakashi Iwai 		snd_iprintf(buffer, " continuous");
876d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
8779b151fecSTakashi Iwai 		snd_iprintf(buffer, " knot");
8789b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(rates); i++)
879d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.rates & (1 << i))
8809b151fecSTakashi Iwai 			snd_iprintf(buffer, " %d", rates[i]);
8819b151fecSTakashi Iwai }
8829b151fecSTakashi Iwai 
883d5e1ca05SJaroslav Kysela #define get_dummy_int_ptr(dummy, ofs) \
884d5e1ca05SJaroslav Kysela 	(unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
885d5e1ca05SJaroslav Kysela #define get_dummy_ll_ptr(dummy, ofs) \
886d5e1ca05SJaroslav Kysela 	(unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
8879b151fecSTakashi Iwai 
8889b151fecSTakashi Iwai struct dummy_hw_field {
8899b151fecSTakashi Iwai 	const char *name;
8909b151fecSTakashi Iwai 	const char *format;
8919b151fecSTakashi Iwai 	unsigned int offset;
8929b151fecSTakashi Iwai 	unsigned int size;
8939b151fecSTakashi Iwai };
8949b151fecSTakashi Iwai #define FIELD_ENTRY(item, fmt) {		   \
8959b151fecSTakashi Iwai 	.name = #item,				   \
8969b151fecSTakashi Iwai 	.format = fmt,				   \
8979b151fecSTakashi Iwai 	.offset = offsetof(struct snd_pcm_hardware, item), \
8989b151fecSTakashi Iwai 	.size = sizeof(dummy_pcm_hardware.item) }
8999b151fecSTakashi Iwai 
9009b151fecSTakashi Iwai static struct dummy_hw_field fields[] = {
9019b151fecSTakashi Iwai 	FIELD_ENTRY(formats, "%#llx"),
9029b151fecSTakashi Iwai 	FIELD_ENTRY(rates, "%#x"),
9039b151fecSTakashi Iwai 	FIELD_ENTRY(rate_min, "%d"),
9049b151fecSTakashi Iwai 	FIELD_ENTRY(rate_max, "%d"),
9059b151fecSTakashi Iwai 	FIELD_ENTRY(channels_min, "%d"),
9069b151fecSTakashi Iwai 	FIELD_ENTRY(channels_max, "%d"),
9079b151fecSTakashi Iwai 	FIELD_ENTRY(buffer_bytes_max, "%ld"),
9089b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_min, "%ld"),
9099b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_max, "%ld"),
9109b151fecSTakashi Iwai 	FIELD_ENTRY(periods_min, "%d"),
9119b151fecSTakashi Iwai 	FIELD_ENTRY(periods_max, "%d"),
9129b151fecSTakashi Iwai };
9139b151fecSTakashi Iwai 
9149b151fecSTakashi Iwai static void dummy_proc_read(struct snd_info_entry *entry,
9159b151fecSTakashi Iwai 			    struct snd_info_buffer *buffer)
9169b151fecSTakashi Iwai {
917d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9189b151fecSTakashi Iwai 	int i;
9199b151fecSTakashi Iwai 
9209b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(fields); i++) {
9219b151fecSTakashi Iwai 		snd_iprintf(buffer, "%s ", fields[i].name);
9229b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
9239b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
924d5e1ca05SJaroslav Kysela 				*get_dummy_int_ptr(dummy, fields[i].offset));
9259b151fecSTakashi Iwai 		else
9269b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
927d5e1ca05SJaroslav Kysela 				*get_dummy_ll_ptr(dummy, fields[i].offset));
9289b151fecSTakashi Iwai 		if (!strcmp(fields[i].name, "formats"))
929d5e1ca05SJaroslav Kysela 			print_formats(dummy, buffer);
9309b151fecSTakashi Iwai 		else if (!strcmp(fields[i].name, "rates"))
931d5e1ca05SJaroslav Kysela 			print_rates(dummy, buffer);
9329b151fecSTakashi Iwai 		snd_iprintf(buffer, "\n");
9339b151fecSTakashi Iwai 	}
9349b151fecSTakashi Iwai }
9359b151fecSTakashi Iwai 
9369b151fecSTakashi Iwai static void dummy_proc_write(struct snd_info_entry *entry,
9379b151fecSTakashi Iwai 			     struct snd_info_buffer *buffer)
9389b151fecSTakashi Iwai {
939d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9409b151fecSTakashi Iwai 	char line[64];
9419b151fecSTakashi Iwai 
9429b151fecSTakashi Iwai 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
9439b151fecSTakashi Iwai 		char item[20];
9449b151fecSTakashi Iwai 		const char *ptr;
9459b151fecSTakashi Iwai 		unsigned long long val;
9469b151fecSTakashi Iwai 		int i;
9479b151fecSTakashi Iwai 
9489b151fecSTakashi Iwai 		ptr = snd_info_get_str(item, line, sizeof(item));
9499b151fecSTakashi Iwai 		for (i = 0; i < ARRAY_SIZE(fields); i++) {
9509b151fecSTakashi Iwai 			if (!strcmp(item, fields[i].name))
9519b151fecSTakashi Iwai 				break;
9529b151fecSTakashi Iwai 		}
9539b151fecSTakashi Iwai 		if (i >= ARRAY_SIZE(fields))
9549b151fecSTakashi Iwai 			continue;
9559b151fecSTakashi Iwai 		snd_info_get_str(item, ptr, sizeof(item));
9569b151fecSTakashi Iwai 		if (strict_strtoull(item, 0, &val))
9579b151fecSTakashi Iwai 			continue;
9589b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
959d5e1ca05SJaroslav Kysela 			*get_dummy_int_ptr(dummy, fields[i].offset) = val;
9609b151fecSTakashi Iwai 		else
961d5e1ca05SJaroslav Kysela 			*get_dummy_ll_ptr(dummy, fields[i].offset) = val;
9629b151fecSTakashi Iwai 	}
9639b151fecSTakashi Iwai }
9649b151fecSTakashi Iwai 
9659b151fecSTakashi Iwai static void __devinit dummy_proc_init(struct snd_dummy *chip)
9669b151fecSTakashi Iwai {
9679b151fecSTakashi Iwai 	struct snd_info_entry *entry;
9689b151fecSTakashi Iwai 
9699b151fecSTakashi Iwai 	if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
9709b151fecSTakashi Iwai 		snd_info_set_text_ops(entry, chip, dummy_proc_read);
9719b151fecSTakashi Iwai 		entry->c.text.write = dummy_proc_write;
9729b151fecSTakashi Iwai 		entry->mode |= S_IWUSR;
973d5e1ca05SJaroslav Kysela 		entry->private_data = chip;
9749b151fecSTakashi Iwai 	}
9759b151fecSTakashi Iwai }
9769b151fecSTakashi Iwai #else
9779b151fecSTakashi Iwai #define dummy_proc_init(x)
9789b151fecSTakashi Iwai #endif /* CONFIG_SND_DEBUG && CONFIG_PROC_FS */
9799b151fecSTakashi Iwai 
980788c6043SPrarit Bhargava static int __devinit snd_dummy_probe(struct platform_device *devptr)
9811da177e4SLinus Torvalds {
9824a4d2cfdSTakashi Iwai 	struct snd_card *card;
9834a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy;
984d5e1ca05SJaroslav Kysela 	struct dummy_model *m = NULL, **mdl;
9851da177e4SLinus Torvalds 	int idx, err;
9866e65c1ccSTakashi Iwai 	int dev = devptr->id;
9871da177e4SLinus Torvalds 
988bd7dd77cSTakashi Iwai 	err = snd_card_create(index[dev], id[dev], THIS_MODULE,
989bd7dd77cSTakashi Iwai 			      sizeof(struct snd_dummy), &card);
990bd7dd77cSTakashi Iwai 	if (err < 0)
991bd7dd77cSTakashi Iwai 		return err;
9924a4d2cfdSTakashi Iwai 	dummy = card->private_data;
9931da177e4SLinus Torvalds 	dummy->card = card;
994d5e1ca05SJaroslav Kysela 	for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
995d5e1ca05SJaroslav Kysela 		if (strcmp(model[dev], (*mdl)->name) == 0) {
996d5e1ca05SJaroslav Kysela 			printk(KERN_INFO
997d5e1ca05SJaroslav Kysela 				"snd-dummy: Using model '%s' for card %i\n",
998d5e1ca05SJaroslav Kysela 				(*mdl)->name, card->number);
999d5e1ca05SJaroslav Kysela 			m = dummy->model = *mdl;
1000d5e1ca05SJaroslav Kysela 			break;
1001d5e1ca05SJaroslav Kysela 		}
1002d5e1ca05SJaroslav Kysela 	}
10031da177e4SLinus Torvalds 	for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
10041da177e4SLinus Torvalds 		if (pcm_substreams[dev] < 1)
10051da177e4SLinus Torvalds 			pcm_substreams[dev] = 1;
10061da177e4SLinus Torvalds 		if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
10071da177e4SLinus Torvalds 			pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
10081a11cb64SJaroslav Kysela 		err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
10091a11cb64SJaroslav Kysela 		if (err < 0)
10101da177e4SLinus Torvalds 			goto __nodev;
10111da177e4SLinus Torvalds 	}
1012d5e1ca05SJaroslav Kysela 
1013d5e1ca05SJaroslav Kysela 	dummy->pcm_hw = dummy_pcm_hardware;
1014d5e1ca05SJaroslav Kysela 	if (m) {
1015d5e1ca05SJaroslav Kysela 		if (m->formats)
1016d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.formats = m->formats;
1017d5e1ca05SJaroslav Kysela 		if (m->buffer_bytes_max)
1018d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1019d5e1ca05SJaroslav Kysela 		if (m->period_bytes_min)
1020d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1021d5e1ca05SJaroslav Kysela 		if (m->period_bytes_max)
1022d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1023d5e1ca05SJaroslav Kysela 		if (m->periods_min)
1024d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_min = m->periods_min;
1025d5e1ca05SJaroslav Kysela 		if (m->periods_max)
1026d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_max = m->periods_max;
1027d5e1ca05SJaroslav Kysela 		if (m->rates)
1028d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rates = m->rates;
1029d5e1ca05SJaroslav Kysela 		if (m->rate_min)
1030d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_min = m->rate_min;
1031d5e1ca05SJaroslav Kysela 		if (m->rate_max)
1032d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_max = m->rate_max;
1033d5e1ca05SJaroslav Kysela 		if (m->channels_min)
1034d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_min = m->channels_min;
1035d5e1ca05SJaroslav Kysela 		if (m->channels_max)
1036d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_max = m->channels_max;
1037d5e1ca05SJaroslav Kysela 	}
1038d5e1ca05SJaroslav Kysela 
10391a11cb64SJaroslav Kysela 	err = snd_card_dummy_new_mixer(dummy);
10401a11cb64SJaroslav Kysela 	if (err < 0)
10411da177e4SLinus Torvalds 		goto __nodev;
10421da177e4SLinus Torvalds 	strcpy(card->driver, "Dummy");
10431da177e4SLinus Torvalds 	strcpy(card->shortname, "Dummy");
10441da177e4SLinus Torvalds 	sprintf(card->longname, "Dummy %i", dev + 1);
104516dab54bSTakashi Iwai 
10469b151fecSTakashi Iwai 	dummy_proc_init(dummy);
10479b151fecSTakashi Iwai 
10486e65c1ccSTakashi Iwai 	snd_card_set_dev(card, &devptr->dev);
104916dab54bSTakashi Iwai 
10501a11cb64SJaroslav Kysela 	err = snd_card_register(card);
10511a11cb64SJaroslav Kysela 	if (err == 0) {
10526e65c1ccSTakashi Iwai 		platform_set_drvdata(devptr, card);
10531da177e4SLinus Torvalds 		return 0;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds       __nodev:
10561da177e4SLinus Torvalds 	snd_card_free(card);
10571da177e4SLinus Torvalds 	return err;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
1060788c6043SPrarit Bhargava static int __devexit snd_dummy_remove(struct platform_device *devptr)
10616e65c1ccSTakashi Iwai {
10626e65c1ccSTakashi Iwai 	snd_card_free(platform_get_drvdata(devptr));
10636e65c1ccSTakashi Iwai 	platform_set_drvdata(devptr, NULL);
10646e65c1ccSTakashi Iwai 	return 0;
10656e65c1ccSTakashi Iwai }
10666e65c1ccSTakashi Iwai 
10676e65c1ccSTakashi Iwai #ifdef CONFIG_PM
10686e65c1ccSTakashi Iwai static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
10696e65c1ccSTakashi Iwai {
10706e65c1ccSTakashi Iwai 	struct snd_card *card = platform_get_drvdata(pdev);
10716e65c1ccSTakashi Iwai 	struct snd_dummy *dummy = card->private_data;
10726e65c1ccSTakashi Iwai 
10736e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
10746e65c1ccSTakashi Iwai 	snd_pcm_suspend_all(dummy->pcm);
10756e65c1ccSTakashi Iwai 	return 0;
10766e65c1ccSTakashi Iwai }
10776e65c1ccSTakashi Iwai 
10786e65c1ccSTakashi Iwai static int snd_dummy_resume(struct platform_device *pdev)
10796e65c1ccSTakashi Iwai {
10806e65c1ccSTakashi Iwai 	struct snd_card *card = platform_get_drvdata(pdev);
10816e65c1ccSTakashi Iwai 
10826e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
10836e65c1ccSTakashi Iwai 	return 0;
10846e65c1ccSTakashi Iwai }
10856e65c1ccSTakashi Iwai #endif
10866e65c1ccSTakashi Iwai 
10876e65c1ccSTakashi Iwai #define SND_DUMMY_DRIVER	"snd_dummy"
10886e65c1ccSTakashi Iwai 
10896e65c1ccSTakashi Iwai static struct platform_driver snd_dummy_driver = {
10906e65c1ccSTakashi Iwai 	.probe		= snd_dummy_probe,
1091788c6043SPrarit Bhargava 	.remove		= __devexit_p(snd_dummy_remove),
10926e65c1ccSTakashi Iwai #ifdef CONFIG_PM
10936e65c1ccSTakashi Iwai 	.suspend	= snd_dummy_suspend,
10946e65c1ccSTakashi Iwai 	.resume		= snd_dummy_resume,
10956e65c1ccSTakashi Iwai #endif
10966e65c1ccSTakashi Iwai 	.driver		= {
1097*8bf01d8aSTakashi Iwai 		.name	= SND_DUMMY_DRIVER,
1098*8bf01d8aSTakashi Iwai 		.owner	= THIS_MODULE,
10996e65c1ccSTakashi Iwai 	},
11006e65c1ccSTakashi Iwai };
11016e65c1ccSTakashi Iwai 
1102c12aad6eSRandy Dunlap static void snd_dummy_unregister_all(void)
1103f7a9275dSClemens Ladisch {
1104f7a9275dSClemens Ladisch 	int i;
1105f7a9275dSClemens Ladisch 
1106f7a9275dSClemens Ladisch 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
1107f7a9275dSClemens Ladisch 		platform_device_unregister(devices[i]);
1108f7a9275dSClemens Ladisch 	platform_driver_unregister(&snd_dummy_driver);
1109a68c4d11STakashi Iwai 	free_fake_buffer();
1110f7a9275dSClemens Ladisch }
1111f7a9275dSClemens Ladisch 
11121da177e4SLinus Torvalds static int __init alsa_card_dummy_init(void)
11131da177e4SLinus Torvalds {
11146e65c1ccSTakashi Iwai 	int i, cards, err;
11151da177e4SLinus Torvalds 
11161a11cb64SJaroslav Kysela 	err = platform_driver_register(&snd_dummy_driver);
11171a11cb64SJaroslav Kysela 	if (err < 0)
11186e65c1ccSTakashi Iwai 		return err;
11196e65c1ccSTakashi Iwai 
1120a68c4d11STakashi Iwai 	err = alloc_fake_buffer();
1121a68c4d11STakashi Iwai 	if (err < 0) {
1122a68c4d11STakashi Iwai 		platform_driver_unregister(&snd_dummy_driver);
1123a68c4d11STakashi Iwai 		return err;
1124a68c4d11STakashi Iwai 	}
1125a68c4d11STakashi Iwai 
11266e65c1ccSTakashi Iwai 	cards = 0;
11278278ca8fSTakashi Iwai 	for (i = 0; i < SNDRV_CARDS; i++) {
11286e65c1ccSTakashi Iwai 		struct platform_device *device;
11298278ca8fSTakashi Iwai 		if (! enable[i])
11308278ca8fSTakashi Iwai 			continue;
11316e65c1ccSTakashi Iwai 		device = platform_device_register_simple(SND_DUMMY_DRIVER,
11326e65c1ccSTakashi Iwai 							 i, NULL, 0);
1133a182ee98SRene Herman 		if (IS_ERR(device))
1134a182ee98SRene Herman 			continue;
11357152447dSRene Herman 		if (!platform_get_drvdata(device)) {
11367152447dSRene Herman 			platform_device_unregister(device);
11377152447dSRene Herman 			continue;
11387152447dSRene Herman 		}
1139f7a9275dSClemens Ladisch 		devices[i] = device;
11401da177e4SLinus Torvalds 		cards++;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 	if (!cards) {
11431da177e4SLinus Torvalds #ifdef MODULE
11441da177e4SLinus Torvalds 		printk(KERN_ERR "Dummy soundcard not found or device busy\n");
11451da177e4SLinus Torvalds #endif
1146a182ee98SRene Herman 		snd_dummy_unregister_all();
1147a182ee98SRene Herman 		return -ENODEV;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 	return 0;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds static void __exit alsa_card_dummy_exit(void)
11531da177e4SLinus Torvalds {
1154f7a9275dSClemens Ladisch 	snd_dummy_unregister_all();
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds module_init(alsa_card_dummy_init)
11581da177e4SLinus Torvalds module_exit(alsa_card_dummy_exit)
1159