xref: /openbmc/linux/sound/drivers/dummy.c (revision d8c5ed752e5b9aabad9ea8b53272b6abb4fa5235)
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];
13716e43467SClemens Ladisch 	int iobox;
13816e43467SClemens Ladisch 	struct snd_kcontrol *cd_volume_ctl;
13916e43467SClemens Ladisch 	struct snd_kcontrol *cd_switch_ctl;
140c631d03cSTakashi Iwai 	const struct dummy_timer_ops *timer_ops;
1414a4d2cfdSTakashi Iwai };
1421da177e4SLinus Torvalds 
143c631d03cSTakashi Iwai /*
144d5e1ca05SJaroslav Kysela  * card models
145d5e1ca05SJaroslav Kysela  */
146d5e1ca05SJaroslav Kysela 
147d5e1ca05SJaroslav Kysela static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
148d5e1ca05SJaroslav Kysela {
149d5e1ca05SJaroslav Kysela 	int err;
150d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
151d5e1ca05SJaroslav Kysela 	if (err < 0)
152d5e1ca05SJaroslav Kysela 		return err;
153d5e1ca05SJaroslav Kysela 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
154d5e1ca05SJaroslav Kysela 	if (err < 0)
155d5e1ca05SJaroslav Kysela 		return err;
156d5e1ca05SJaroslav Kysela 	return 0;
157d5e1ca05SJaroslav Kysela }
158d5e1ca05SJaroslav Kysela 
159e4c28688STakashi Iwai static struct dummy_model model_emu10k1 = {
160d5e1ca05SJaroslav Kysela 	.name = "emu10k1",
161d5e1ca05SJaroslav Kysela 	.playback_constraints = emu10k1_playback_constraints,
162d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 128 * 1024,
163d5e1ca05SJaroslav Kysela };
164d5e1ca05SJaroslav Kysela 
165e4c28688STakashi Iwai static struct dummy_model model_rme9652 = {
166d5e1ca05SJaroslav Kysela 	.name = "rme9652",
167d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 26 * 64 * 1024,
168d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
169d5e1ca05SJaroslav Kysela 	.channels_min = 26,
170d5e1ca05SJaroslav Kysela 	.channels_max = 26,
171d5e1ca05SJaroslav Kysela 	.periods_min = 2,
172d5e1ca05SJaroslav Kysela 	.periods_max = 2,
173d5e1ca05SJaroslav Kysela };
174d5e1ca05SJaroslav Kysela 
175e4c28688STakashi Iwai static struct dummy_model model_ice1712 = {
176d5e1ca05SJaroslav Kysela 	.name = "ice1712",
177d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 256 * 1024,
178d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
179d5e1ca05SJaroslav Kysela 	.channels_min = 10,
180d5e1ca05SJaroslav Kysela 	.channels_max = 10,
181d5e1ca05SJaroslav Kysela 	.periods_min = 1,
182d5e1ca05SJaroslav Kysela 	.periods_max = 1024,
183d5e1ca05SJaroslav Kysela };
184d5e1ca05SJaroslav Kysela 
185e4c28688STakashi Iwai static struct dummy_model model_uda1341 = {
186d5e1ca05SJaroslav Kysela 	.name = "uda1341",
187d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = 16380,
188d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
189d5e1ca05SJaroslav Kysela 	.channels_min = 2,
190d5e1ca05SJaroslav Kysela 	.channels_max = 2,
191d5e1ca05SJaroslav Kysela 	.periods_min = 2,
192d5e1ca05SJaroslav Kysela 	.periods_max = 255,
193d5e1ca05SJaroslav Kysela };
194d5e1ca05SJaroslav Kysela 
195e4c28688STakashi Iwai static struct dummy_model model_ac97 = {
196d5e1ca05SJaroslav Kysela 	.name = "ac97",
197d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
198d5e1ca05SJaroslav Kysela 	.channels_min = 2,
199d5e1ca05SJaroslav Kysela 	.channels_max = 2,
200d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000,
201d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
202d5e1ca05SJaroslav Kysela 	.rate_max = 48000,
203d5e1ca05SJaroslav Kysela };
204d5e1ca05SJaroslav Kysela 
205e4c28688STakashi Iwai static struct dummy_model model_ca0106 = {
206d5e1ca05SJaroslav Kysela 	.name = "ca0106",
207d5e1ca05SJaroslav Kysela 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
208d5e1ca05SJaroslav Kysela 	.buffer_bytes_max = ((65536-64)*8),
209d5e1ca05SJaroslav Kysela 	.period_bytes_max = (65536-64),
210d5e1ca05SJaroslav Kysela 	.periods_min = 2,
211d5e1ca05SJaroslav Kysela 	.periods_max = 8,
212d5e1ca05SJaroslav Kysela 	.channels_min = 2,
213d5e1ca05SJaroslav Kysela 	.channels_max = 2,
214d5e1ca05SJaroslav Kysela 	.rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
215d5e1ca05SJaroslav Kysela 	.rate_min = 48000,
216d5e1ca05SJaroslav Kysela 	.rate_max = 192000,
217d5e1ca05SJaroslav Kysela };
218d5e1ca05SJaroslav Kysela 
219e4c28688STakashi Iwai static struct dummy_model *dummy_models[] = {
220d5e1ca05SJaroslav Kysela 	&model_emu10k1,
221d5e1ca05SJaroslav Kysela 	&model_rme9652,
222d5e1ca05SJaroslav Kysela 	&model_ice1712,
223d5e1ca05SJaroslav Kysela 	&model_uda1341,
224d5e1ca05SJaroslav Kysela 	&model_ac97,
225d5e1ca05SJaroslav Kysela 	&model_ca0106,
226d5e1ca05SJaroslav Kysela 	NULL
227d5e1ca05SJaroslav Kysela };
228d5e1ca05SJaroslav Kysela 
229d5e1ca05SJaroslav Kysela /*
230c631d03cSTakashi Iwai  * system timer interface
231c631d03cSTakashi Iwai  */
232c631d03cSTakashi Iwai 
233c631d03cSTakashi Iwai struct dummy_systimer_pcm {
2341da177e4SLinus Torvalds 	spinlock_t lock;
2351da177e4SLinus Torvalds 	struct timer_list timer;
236b142037bSTakashi Iwai 	unsigned long base_time;
237b142037bSTakashi Iwai 	unsigned int frac_pos;	/* fractional sample position (based HZ) */
238b5d10781STakashi Iwai 	unsigned int frac_period_rest;
239b142037bSTakashi Iwai 	unsigned int frac_buffer_size;	/* buffer_size * HZ */
240b142037bSTakashi Iwai 	unsigned int frac_period_size;	/* period_size * HZ */
241b142037bSTakashi Iwai 	unsigned int rate;
242b5d10781STakashi Iwai 	int elapsed;
2434a4d2cfdSTakashi Iwai 	struct snd_pcm_substream *substream;
2444a4d2cfdSTakashi Iwai };
2451da177e4SLinus Torvalds 
246b142037bSTakashi Iwai static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
247b142037bSTakashi Iwai {
2482a52b6eeSRoman Kollar 	mod_timer(&dpcm->timer, jiffies +
2492a52b6eeSRoman Kollar 		(dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate);
250b142037bSTakashi Iwai }
251b142037bSTakashi Iwai 
252b142037bSTakashi Iwai static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
253b142037bSTakashi Iwai {
254b142037bSTakashi Iwai 	unsigned long delta;
255b142037bSTakashi Iwai 
256b142037bSTakashi Iwai 	delta = jiffies - dpcm->base_time;
257b142037bSTakashi Iwai 	if (!delta)
258b142037bSTakashi Iwai 		return;
259b5d10781STakashi Iwai 	dpcm->base_time += delta;
260b5d10781STakashi Iwai 	delta *= dpcm->rate;
261b5d10781STakashi Iwai 	dpcm->frac_pos += delta;
262b142037bSTakashi Iwai 	while (dpcm->frac_pos >= dpcm->frac_buffer_size)
263b142037bSTakashi Iwai 		dpcm->frac_pos -= dpcm->frac_buffer_size;
264b5d10781STakashi Iwai 	while (dpcm->frac_period_rest <= delta) {
265b5d10781STakashi Iwai 		dpcm->elapsed++;
266b5d10781STakashi Iwai 		dpcm->frac_period_rest += dpcm->frac_period_size;
267b5d10781STakashi Iwai 	}
268b5d10781STakashi Iwai 	dpcm->frac_period_rest -= delta;
269b142037bSTakashi Iwai }
270b142037bSTakashi Iwai 
271c631d03cSTakashi Iwai static int dummy_systimer_start(struct snd_pcm_substream *substream)
2721da177e4SLinus Torvalds {
273c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
274c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
275b142037bSTakashi Iwai 	dpcm->base_time = jiffies;
276b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
277c8eb6ba1STakashi Iwai 	spin_unlock(&dpcm->lock);
2786e65c1ccSTakashi Iwai 	return 0;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
281c631d03cSTakashi Iwai static int dummy_systimer_stop(struct snd_pcm_substream *substream)
282c631d03cSTakashi Iwai {
283c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
284c631d03cSTakashi Iwai 	spin_lock(&dpcm->lock);
285c631d03cSTakashi Iwai 	del_timer(&dpcm->timer);
286c631d03cSTakashi Iwai 	spin_unlock(&dpcm->lock);
287c631d03cSTakashi Iwai 	return 0;
288c631d03cSTakashi Iwai }
289c631d03cSTakashi Iwai 
290c631d03cSTakashi Iwai static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
2911da177e4SLinus Torvalds {
2924a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
293c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = runtime->private_data;
2941da177e4SLinus Torvalds 
295b142037bSTakashi Iwai 	dpcm->frac_pos = 0;
296b142037bSTakashi Iwai 	dpcm->rate = runtime->rate;
297b142037bSTakashi Iwai 	dpcm->frac_buffer_size = runtime->buffer_size * HZ;
298b142037bSTakashi Iwai 	dpcm->frac_period_size = runtime->period_size * HZ;
299b5d10781STakashi Iwai 	dpcm->frac_period_rest = dpcm->frac_period_size;
300b5d10781STakashi Iwai 	dpcm->elapsed = 0;
30153463a83SAhmet İnan 
3021da177e4SLinus Torvalds 	return 0;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305c631d03cSTakashi Iwai static void dummy_systimer_callback(unsigned long data)
3061da177e4SLinus Torvalds {
307c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
308b32425acSTakashi Iwai 	unsigned long flags;
309b5d10781STakashi Iwai 	int elapsed = 0;
3101da177e4SLinus Torvalds 
311b32425acSTakashi Iwai 	spin_lock_irqsave(&dpcm->lock, flags);
312b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
313b142037bSTakashi Iwai 	dummy_systimer_rearm(dpcm);
314b5d10781STakashi Iwai 	elapsed = dpcm->elapsed;
315b5d10781STakashi Iwai 	dpcm->elapsed = 0;
316b32425acSTakashi Iwai 	spin_unlock_irqrestore(&dpcm->lock, flags);
317b5d10781STakashi Iwai 	if (elapsed)
3181da177e4SLinus Torvalds 		snd_pcm_period_elapsed(dpcm->substream);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
321c631d03cSTakashi Iwai static snd_pcm_uframes_t
322c631d03cSTakashi Iwai dummy_systimer_pointer(struct snd_pcm_substream *substream)
3231da177e4SLinus Torvalds {
324b142037bSTakashi Iwai 	struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
325b5d10781STakashi Iwai 	snd_pcm_uframes_t pos;
3261da177e4SLinus Torvalds 
327b142037bSTakashi Iwai 	spin_lock(&dpcm->lock);
328b142037bSTakashi Iwai 	dummy_systimer_update(dpcm);
329b5d10781STakashi Iwai 	pos = dpcm->frac_pos / HZ;
330b142037bSTakashi Iwai 	spin_unlock(&dpcm->lock);
331b5d10781STakashi Iwai 	return pos;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
334c631d03cSTakashi Iwai static int dummy_systimer_create(struct snd_pcm_substream *substream)
3351da177e4SLinus Torvalds {
336c631d03cSTakashi Iwai 	struct dummy_systimer_pcm *dpcm;
3371da177e4SLinus Torvalds 
338c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
339c631d03cSTakashi Iwai 	if (!dpcm)
340c631d03cSTakashi Iwai 		return -ENOMEM;
341c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
3422a52b6eeSRoman Kollar 	setup_timer(&dpcm->timer, dummy_systimer_callback,
3432a52b6eeSRoman Kollar 			(unsigned long) dpcm);
344c631d03cSTakashi Iwai 	spin_lock_init(&dpcm->lock);
345c631d03cSTakashi Iwai 	dpcm->substream = substream;
346c631d03cSTakashi Iwai 	return 0;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
349c631d03cSTakashi Iwai static void dummy_systimer_free(struct snd_pcm_substream *substream)
350c631d03cSTakashi Iwai {
351c631d03cSTakashi Iwai 	kfree(substream->runtime->private_data);
352c631d03cSTakashi Iwai }
353c631d03cSTakashi Iwai 
354*d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_systimer_ops = {
355c631d03cSTakashi Iwai 	.create =	dummy_systimer_create,
356c631d03cSTakashi Iwai 	.free =		dummy_systimer_free,
357c631d03cSTakashi Iwai 	.prepare =	dummy_systimer_prepare,
358c631d03cSTakashi Iwai 	.start =	dummy_systimer_start,
359c631d03cSTakashi Iwai 	.stop =		dummy_systimer_stop,
360c631d03cSTakashi Iwai 	.pointer =	dummy_systimer_pointer,
361c631d03cSTakashi Iwai };
362c631d03cSTakashi Iwai 
363c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
364c631d03cSTakashi Iwai /*
365c631d03cSTakashi Iwai  * hrtimer interface
366c631d03cSTakashi Iwai  */
367c631d03cSTakashi Iwai 
368c631d03cSTakashi Iwai struct dummy_hrtimer_pcm {
369c631d03cSTakashi Iwai 	ktime_t base_time;
370c631d03cSTakashi Iwai 	ktime_t period_time;
371c631d03cSTakashi Iwai 	atomic_t running;
372c631d03cSTakashi Iwai 	struct hrtimer timer;
373c631d03cSTakashi Iwai 	struct tasklet_struct tasklet;
374c631d03cSTakashi Iwai 	struct snd_pcm_substream *substream;
375c631d03cSTakashi Iwai };
376c631d03cSTakashi Iwai 
377c631d03cSTakashi Iwai static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
378c631d03cSTakashi Iwai {
379c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
380c631d03cSTakashi Iwai 	if (atomic_read(&dpcm->running))
381c631d03cSTakashi Iwai 		snd_pcm_period_elapsed(dpcm->substream);
382c631d03cSTakashi Iwai }
383c631d03cSTakashi Iwai 
384c631d03cSTakashi Iwai static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
385c631d03cSTakashi Iwai {
386c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
387c631d03cSTakashi Iwai 
388c631d03cSTakashi Iwai 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
389c631d03cSTakashi Iwai 	if (!atomic_read(&dpcm->running))
390c631d03cSTakashi Iwai 		return HRTIMER_NORESTART;
391c631d03cSTakashi Iwai 	tasklet_schedule(&dpcm->tasklet);
392c631d03cSTakashi Iwai 	hrtimer_forward_now(timer, dpcm->period_time);
393c631d03cSTakashi Iwai 	return HRTIMER_RESTART;
394c631d03cSTakashi Iwai }
395c631d03cSTakashi Iwai 
396c631d03cSTakashi Iwai static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
397c631d03cSTakashi Iwai {
398c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
399c631d03cSTakashi Iwai 
400c631d03cSTakashi Iwai 	dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
401c631d03cSTakashi Iwai 	hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
402c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 1);
403c631d03cSTakashi Iwai 	return 0;
404c631d03cSTakashi Iwai }
405c631d03cSTakashi Iwai 
406c631d03cSTakashi Iwai static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
407c631d03cSTakashi Iwai {
408c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
409c631d03cSTakashi Iwai 
410c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
411c631d03cSTakashi Iwai 	hrtimer_cancel(&dpcm->timer);
412c631d03cSTakashi Iwai 	return 0;
413c631d03cSTakashi Iwai }
414c631d03cSTakashi Iwai 
415c631d03cSTakashi Iwai static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
416c631d03cSTakashi Iwai {
417c631d03cSTakashi Iwai 	tasklet_kill(&dpcm->tasklet);
418c631d03cSTakashi Iwai }
419c631d03cSTakashi Iwai 
420c631d03cSTakashi Iwai static snd_pcm_uframes_t
421c631d03cSTakashi Iwai dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
422c631d03cSTakashi Iwai {
423c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
424c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
425c631d03cSTakashi Iwai 	u64 delta;
426c631d03cSTakashi Iwai 	u32 pos;
427c631d03cSTakashi Iwai 
428c631d03cSTakashi Iwai 	delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
429c631d03cSTakashi Iwai 			       dpcm->base_time);
430c631d03cSTakashi Iwai 	delta = div_u64(delta * runtime->rate + 999999, 1000000);
431c631d03cSTakashi Iwai 	div_u64_rem(delta, runtime->buffer_size, &pos);
432c631d03cSTakashi Iwai 	return pos;
433c631d03cSTakashi Iwai }
434c631d03cSTakashi Iwai 
435c631d03cSTakashi Iwai static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
436c631d03cSTakashi Iwai {
437c631d03cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
438c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
439c631d03cSTakashi Iwai 	unsigned int period, rate;
440c631d03cSTakashi Iwai 	long sec;
441c631d03cSTakashi Iwai 	unsigned long nsecs;
442c631d03cSTakashi Iwai 
443c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
444c631d03cSTakashi Iwai 	period = runtime->period_size;
445c631d03cSTakashi Iwai 	rate = runtime->rate;
446c631d03cSTakashi Iwai 	sec = period / rate;
447c631d03cSTakashi Iwai 	period %= rate;
448c631d03cSTakashi Iwai 	nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
449c631d03cSTakashi Iwai 	dpcm->period_time = ktime_set(sec, nsecs);
450c631d03cSTakashi Iwai 
451c631d03cSTakashi Iwai 	return 0;
452c631d03cSTakashi Iwai }
453c631d03cSTakashi Iwai 
454c631d03cSTakashi Iwai static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
455c631d03cSTakashi Iwai {
456c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm;
457c631d03cSTakashi Iwai 
458c631d03cSTakashi Iwai 	dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
459c631d03cSTakashi Iwai 	if (!dpcm)
460c631d03cSTakashi Iwai 		return -ENOMEM;
461c631d03cSTakashi Iwai 	substream->runtime->private_data = dpcm;
462c631d03cSTakashi Iwai 	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
463c631d03cSTakashi Iwai 	dpcm->timer.function = dummy_hrtimer_callback;
464c631d03cSTakashi Iwai 	dpcm->substream = substream;
465c631d03cSTakashi Iwai 	atomic_set(&dpcm->running, 0);
466c631d03cSTakashi Iwai 	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
467c631d03cSTakashi Iwai 		     (unsigned long)dpcm);
468c631d03cSTakashi Iwai 	return 0;
469c631d03cSTakashi Iwai }
470c631d03cSTakashi Iwai 
471c631d03cSTakashi Iwai static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
472c631d03cSTakashi Iwai {
473c631d03cSTakashi Iwai 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
474c631d03cSTakashi Iwai 	dummy_hrtimer_sync(dpcm);
475c631d03cSTakashi Iwai 	kfree(dpcm);
476c631d03cSTakashi Iwai }
477c631d03cSTakashi Iwai 
478*d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_hrtimer_ops = {
479c631d03cSTakashi Iwai 	.create =	dummy_hrtimer_create,
480c631d03cSTakashi Iwai 	.free =		dummy_hrtimer_free,
481c631d03cSTakashi Iwai 	.prepare =	dummy_hrtimer_prepare,
482c631d03cSTakashi Iwai 	.start =	dummy_hrtimer_start,
483c631d03cSTakashi Iwai 	.stop =		dummy_hrtimer_stop,
484c631d03cSTakashi Iwai 	.pointer =	dummy_hrtimer_pointer,
485c631d03cSTakashi Iwai };
486c631d03cSTakashi Iwai 
487c631d03cSTakashi Iwai #endif /* CONFIG_HIGH_RES_TIMERS */
488c631d03cSTakashi Iwai 
489c631d03cSTakashi Iwai /*
490c631d03cSTakashi Iwai  * PCM interface
491c631d03cSTakashi Iwai  */
492c631d03cSTakashi Iwai 
493c631d03cSTakashi Iwai static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
494c631d03cSTakashi Iwai {
495c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
496c631d03cSTakashi Iwai 
497c631d03cSTakashi Iwai 	switch (cmd) {
498c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_START:
499c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
500c631d03cSTakashi Iwai 		return dummy->timer_ops->start(substream);
501c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_STOP:
502c631d03cSTakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
503c631d03cSTakashi Iwai 		return dummy->timer_ops->stop(substream);
504c631d03cSTakashi Iwai 	}
505c631d03cSTakashi Iwai 	return -EINVAL;
506c631d03cSTakashi Iwai }
507c631d03cSTakashi Iwai 
508c631d03cSTakashi Iwai static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
509c631d03cSTakashi Iwai {
510c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
511c631d03cSTakashi Iwai 
512c631d03cSTakashi Iwai 	return dummy->timer_ops->prepare(substream);
513c631d03cSTakashi Iwai }
514c631d03cSTakashi Iwai 
515c631d03cSTakashi Iwai static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
516c631d03cSTakashi Iwai {
517c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
518c631d03cSTakashi Iwai 
519c631d03cSTakashi Iwai 	return dummy->timer_ops->pointer(substream);
520c631d03cSTakashi Iwai }
521c631d03cSTakashi Iwai 
522c631d03cSTakashi Iwai static struct snd_pcm_hardware dummy_pcm_hardware = {
523c631d03cSTakashi Iwai 	.info =			(SNDRV_PCM_INFO_MMAP |
524c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_INTERLEAVED |
525c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_RESUME |
526c631d03cSTakashi Iwai 				 SNDRV_PCM_INFO_MMAP_VALID),
527c631d03cSTakashi Iwai 	.formats =		USE_FORMATS,
528c631d03cSTakashi Iwai 	.rates =		USE_RATE,
529c631d03cSTakashi Iwai 	.rate_min =		USE_RATE_MIN,
530c631d03cSTakashi Iwai 	.rate_max =		USE_RATE_MAX,
531c631d03cSTakashi Iwai 	.channels_min =		USE_CHANNELS_MIN,
532c631d03cSTakashi Iwai 	.channels_max =		USE_CHANNELS_MAX,
533c631d03cSTakashi Iwai 	.buffer_bytes_max =	MAX_BUFFER_SIZE,
534d5e1ca05SJaroslav Kysela 	.period_bytes_min =	MIN_PERIOD_SIZE,
535c631d03cSTakashi Iwai 	.period_bytes_max =	MAX_PERIOD_SIZE,
536c631d03cSTakashi Iwai 	.periods_min =		USE_PERIODS_MIN,
537c631d03cSTakashi Iwai 	.periods_max =		USE_PERIODS_MAX,
538c631d03cSTakashi Iwai 	.fifo_size =		0,
539c631d03cSTakashi Iwai };
540c631d03cSTakashi Iwai 
541c631d03cSTakashi Iwai static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
5424a4d2cfdSTakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
5431da177e4SLinus Torvalds {
544a68c4d11STakashi Iwai 	if (fake_buffer) {
545a68c4d11STakashi Iwai 		/* runtime->dma_bytes has to be set manually to allow mmap */
546a68c4d11STakashi Iwai 		substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
547a68c4d11STakashi Iwai 		return 0;
548a68c4d11STakashi Iwai 	}
549c631d03cSTakashi Iwai 	return snd_pcm_lib_malloc_pages(substream,
550c631d03cSTakashi Iwai 					params_buffer_bytes(hw_params));
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
553c631d03cSTakashi Iwai static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
5541da177e4SLinus Torvalds {
555a68c4d11STakashi Iwai 	if (fake_buffer)
556a68c4d11STakashi Iwai 		return 0;
5571da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
560c631d03cSTakashi Iwai static int dummy_pcm_open(struct snd_pcm_substream *substream)
561c8eb6ba1STakashi Iwai {
562c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
563d5e1ca05SJaroslav Kysela 	struct dummy_model *model = dummy->model;
5644a4d2cfdSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
5651da177e4SLinus Torvalds 	int err;
5661da177e4SLinus Torvalds 
567c631d03cSTakashi Iwai 	dummy->timer_ops = &dummy_systimer_ops;
568c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS
569c631d03cSTakashi Iwai 	if (hrtimer)
570c631d03cSTakashi Iwai 		dummy->timer_ops = &dummy_hrtimer_ops;
571c631d03cSTakashi Iwai #endif
572c631d03cSTakashi Iwai 
573c631d03cSTakashi Iwai 	err = dummy->timer_ops->create(substream);
574c631d03cSTakashi Iwai 	if (err < 0)
575c631d03cSTakashi Iwai 		return err;
576c631d03cSTakashi Iwai 
577d5e1ca05SJaroslav Kysela 	runtime->hw = dummy->pcm_hw;
5781da177e4SLinus Torvalds 	if (substream->pcm->device & 1) {
5791da177e4SLinus Torvalds 		runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
5801da177e4SLinus Torvalds 		runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	if (substream->pcm->device & 2)
583c631d03cSTakashi Iwai 		runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
584c631d03cSTakashi Iwai 				      SNDRV_PCM_INFO_MMAP_VALID);
585c631d03cSTakashi Iwai 
586d5e1ca05SJaroslav Kysela 	if (model == NULL)
587d5e1ca05SJaroslav Kysela 		return 0;
588d5e1ca05SJaroslav Kysela 
589d5e1ca05SJaroslav Kysela 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
590d5e1ca05SJaroslav Kysela 		if (model->playback_constraints)
591d5e1ca05SJaroslav Kysela 			err = model->playback_constraints(substream->runtime);
592d5e1ca05SJaroslav Kysela 	} else {
593d5e1ca05SJaroslav Kysela 		if (model->capture_constraints)
594d5e1ca05SJaroslav Kysela 			err = model->capture_constraints(substream->runtime);
595d5e1ca05SJaroslav Kysela 	}
596c631d03cSTakashi Iwai 	if (err < 0) {
597c631d03cSTakashi Iwai 		dummy->timer_ops->free(substream);
5981da177e4SLinus Torvalds 		return err;
599c631d03cSTakashi Iwai 	}
6001da177e4SLinus Torvalds 	return 0;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
603c631d03cSTakashi Iwai static int dummy_pcm_close(struct snd_pcm_substream *substream)
6041da177e4SLinus Torvalds {
605c631d03cSTakashi Iwai 	struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
606c631d03cSTakashi Iwai 	dummy->timer_ops->free(substream);
6071da177e4SLinus Torvalds 	return 0;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610a68c4d11STakashi Iwai /*
611a68c4d11STakashi Iwai  * dummy buffer handling
612a68c4d11STakashi Iwai  */
613a68c4d11STakashi Iwai 
614a68c4d11STakashi Iwai static void *dummy_page[2];
615a68c4d11STakashi Iwai 
616a68c4d11STakashi Iwai static void free_fake_buffer(void)
617a68c4d11STakashi Iwai {
618a68c4d11STakashi Iwai 	if (fake_buffer) {
619a68c4d11STakashi Iwai 		int i;
620a68c4d11STakashi Iwai 		for (i = 0; i < 2; i++)
621a68c4d11STakashi Iwai 			if (dummy_page[i]) {
622a68c4d11STakashi Iwai 				free_page((unsigned long)dummy_page[i]);
623a68c4d11STakashi Iwai 				dummy_page[i] = NULL;
624a68c4d11STakashi Iwai 			}
625a68c4d11STakashi Iwai 	}
626a68c4d11STakashi Iwai }
627a68c4d11STakashi Iwai 
628a68c4d11STakashi Iwai static int alloc_fake_buffer(void)
629a68c4d11STakashi Iwai {
630a68c4d11STakashi Iwai 	int i;
631a68c4d11STakashi Iwai 
632a68c4d11STakashi Iwai 	if (!fake_buffer)
633a68c4d11STakashi Iwai 		return 0;
634a68c4d11STakashi Iwai 	for (i = 0; i < 2; i++) {
635a68c4d11STakashi Iwai 		dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
636a68c4d11STakashi Iwai 		if (!dummy_page[i]) {
637a68c4d11STakashi Iwai 			free_fake_buffer();
638a68c4d11STakashi Iwai 			return -ENOMEM;
639a68c4d11STakashi Iwai 		}
640a68c4d11STakashi Iwai 	}
641a68c4d11STakashi Iwai 	return 0;
642a68c4d11STakashi Iwai }
643a68c4d11STakashi Iwai 
644a68c4d11STakashi Iwai static int dummy_pcm_copy(struct snd_pcm_substream *substream,
645a68c4d11STakashi Iwai 			  int channel, snd_pcm_uframes_t pos,
646a68c4d11STakashi Iwai 			  void __user *dst, snd_pcm_uframes_t count)
647a68c4d11STakashi Iwai {
648a68c4d11STakashi Iwai 	return 0; /* do nothing */
649a68c4d11STakashi Iwai }
650a68c4d11STakashi Iwai 
651a68c4d11STakashi Iwai static int dummy_pcm_silence(struct snd_pcm_substream *substream,
652a68c4d11STakashi Iwai 			     int channel, snd_pcm_uframes_t pos,
653a68c4d11STakashi Iwai 			     snd_pcm_uframes_t count)
654a68c4d11STakashi Iwai {
655a68c4d11STakashi Iwai 	return 0; /* do nothing */
656a68c4d11STakashi Iwai }
657a68c4d11STakashi Iwai 
658a68c4d11STakashi Iwai static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
659a68c4d11STakashi Iwai 				   unsigned long offset)
660a68c4d11STakashi Iwai {
661a68c4d11STakashi Iwai 	return virt_to_page(dummy_page[substream->stream]); /* the same page */
662a68c4d11STakashi Iwai }
663a68c4d11STakashi Iwai 
664c631d03cSTakashi Iwai static struct snd_pcm_ops dummy_pcm_ops = {
665c631d03cSTakashi Iwai 	.open =		dummy_pcm_open,
666c631d03cSTakashi Iwai 	.close =	dummy_pcm_close,
6671da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
668c631d03cSTakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
669c631d03cSTakashi Iwai 	.hw_free =	dummy_pcm_hw_free,
670c631d03cSTakashi Iwai 	.prepare =	dummy_pcm_prepare,
671c631d03cSTakashi Iwai 	.trigger =	dummy_pcm_trigger,
672c631d03cSTakashi Iwai 	.pointer =	dummy_pcm_pointer,
6731da177e4SLinus Torvalds };
6741da177e4SLinus Torvalds 
675a68c4d11STakashi Iwai static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
676a68c4d11STakashi Iwai 	.open =		dummy_pcm_open,
677a68c4d11STakashi Iwai 	.close =	dummy_pcm_close,
678a68c4d11STakashi Iwai 	.ioctl =	snd_pcm_lib_ioctl,
679a68c4d11STakashi Iwai 	.hw_params =	dummy_pcm_hw_params,
680a68c4d11STakashi Iwai 	.hw_free =	dummy_pcm_hw_free,
681a68c4d11STakashi Iwai 	.prepare =	dummy_pcm_prepare,
682a68c4d11STakashi Iwai 	.trigger =	dummy_pcm_trigger,
683a68c4d11STakashi Iwai 	.pointer =	dummy_pcm_pointer,
684a68c4d11STakashi Iwai 	.copy =		dummy_pcm_copy,
685a68c4d11STakashi Iwai 	.silence =	dummy_pcm_silence,
686a68c4d11STakashi Iwai 	.page =		dummy_pcm_page,
687a68c4d11STakashi Iwai };
688a68c4d11STakashi Iwai 
689fbbb01a1SBill Pemberton static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
690788c6043SPrarit Bhargava 			      int substreams)
6911da177e4SLinus Torvalds {
6924a4d2cfdSTakashi Iwai 	struct snd_pcm *pcm;
693a68c4d11STakashi Iwai 	struct snd_pcm_ops *ops;
6941da177e4SLinus Torvalds 	int err;
6951da177e4SLinus Torvalds 
6961a11cb64SJaroslav Kysela 	err = snd_pcm_new(dummy->card, "Dummy PCM", device,
6971a11cb64SJaroslav Kysela 			       substreams, substreams, &pcm);
6981a11cb64SJaroslav Kysela 	if (err < 0)
6991da177e4SLinus Torvalds 		return err;
7006e65c1ccSTakashi Iwai 	dummy->pcm = pcm;
701a68c4d11STakashi Iwai 	if (fake_buffer)
702a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops_no_buf;
703a68c4d11STakashi Iwai 	else
704a68c4d11STakashi Iwai 		ops = &dummy_pcm_ops;
705a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
706a68c4d11STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
7071da177e4SLinus Torvalds 	pcm->private_data = dummy;
7081da177e4SLinus Torvalds 	pcm->info_flags = 0;
7091da177e4SLinus Torvalds 	strcpy(pcm->name, "Dummy PCM");
710a68c4d11STakashi Iwai 	if (!fake_buffer) {
711a68c4d11STakashi Iwai 		snd_pcm_lib_preallocate_pages_for_all(pcm,
712a68c4d11STakashi Iwai 			SNDRV_DMA_TYPE_CONTINUOUS,
7131da177e4SLinus Torvalds 			snd_dma_continuous_data(GFP_KERNEL),
7141da177e4SLinus Torvalds 			0, 64*1024);
715a68c4d11STakashi Iwai 	}
7161da177e4SLinus Torvalds 	return 0;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7199b151fecSTakashi Iwai /*
7209b151fecSTakashi Iwai  * mixer interface
7219b151fecSTakashi Iwai  */
7229b151fecSTakashi Iwai 
7231da177e4SLinus Torvalds #define DUMMY_VOLUME(xname, xindex, addr) \
724fb567a8eSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
725fb567a8eSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
726fb567a8eSTakashi Iwai   .name = xname, .index = xindex, \
7271da177e4SLinus Torvalds   .info = snd_dummy_volume_info, \
7281da177e4SLinus Torvalds   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
729fb567a8eSTakashi Iwai   .private_value = addr, \
730fb567a8eSTakashi Iwai   .tlv = { .p = db_scale_dummy } }
7311da177e4SLinus Torvalds 
7324a4d2cfdSTakashi Iwai static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
7334a4d2cfdSTakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7361da177e4SLinus Torvalds 	uinfo->count = 2;
7371da177e4SLinus Torvalds 	uinfo->value.integer.min = -50;
7381da177e4SLinus Torvalds 	uinfo->value.integer.max = 100;
7391da177e4SLinus Torvalds 	return 0;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7424a4d2cfdSTakashi Iwai static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
7434a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7441da177e4SLinus Torvalds {
7454a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7461da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7471da177e4SLinus Torvalds 
748c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7491da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
7501da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
751c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7521da177e4SLinus Torvalds 	return 0;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7554a4d2cfdSTakashi Iwai static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
7564a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7571da177e4SLinus Torvalds {
7584a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7591da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
7601da177e4SLinus Torvalds 	int left, right;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0];
7631da177e4SLinus Torvalds 	if (left < -50)
7641da177e4SLinus Torvalds 		left = -50;
7651da177e4SLinus Torvalds 	if (left > 100)
7661da177e4SLinus Torvalds 		left = 100;
7671da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1];
7681da177e4SLinus Torvalds 	if (right < -50)
7691da177e4SLinus Torvalds 		right = -50;
7701da177e4SLinus Torvalds 	if (right > 100)
7711da177e4SLinus Torvalds 		right = 100;
772c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7731da177e4SLinus Torvalds 	change = dummy->mixer_volume[addr][0] != left ||
7741da177e4SLinus Torvalds 	         dummy->mixer_volume[addr][1] != right;
7751da177e4SLinus Torvalds 	dummy->mixer_volume[addr][0] = left;
7761da177e4SLinus Torvalds 	dummy->mixer_volume[addr][1] = right;
777c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
7781da177e4SLinus Torvalds 	return change;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7810cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
782fb567a8eSTakashi Iwai 
7831da177e4SLinus Torvalds #define DUMMY_CAPSRC(xname, xindex, addr) \
7841da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
7851da177e4SLinus Torvalds   .info = snd_dummy_capsrc_info, \
7861da177e4SLinus Torvalds   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
7871da177e4SLinus Torvalds   .private_value = addr }
7881da177e4SLinus Torvalds 
789a5ce8890STakashi Iwai #define snd_dummy_capsrc_info	snd_ctl_boolean_stereo_info
7901da177e4SLinus Torvalds 
7914a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
7924a4d2cfdSTakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
7931da177e4SLinus Torvalds {
7944a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
7951da177e4SLinus Torvalds 	int addr = kcontrol->private_value;
7961da177e4SLinus Torvalds 
797c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
7981da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
7991da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
800c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
8011da177e4SLinus Torvalds 	return 0;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8044a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
8051da177e4SLinus Torvalds {
8064a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
8071da177e4SLinus Torvalds 	int change, addr = kcontrol->private_value;
8081da177e4SLinus Torvalds 	int left, right;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0] & 1;
8111da177e4SLinus Torvalds 	right = ucontrol->value.integer.value[1] & 1;
812c8eb6ba1STakashi Iwai 	spin_lock_irq(&dummy->mixer_lock);
8131da177e4SLinus Torvalds 	change = dummy->capture_source[addr][0] != left &&
8141da177e4SLinus Torvalds 	         dummy->capture_source[addr][1] != right;
8151da177e4SLinus Torvalds 	dummy->capture_source[addr][0] = left;
8161da177e4SLinus Torvalds 	dummy->capture_source[addr][1] = right;
817c8eb6ba1STakashi Iwai 	spin_unlock_irq(&dummy->mixer_lock);
8181da177e4SLinus Torvalds 	return change;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
82116e43467SClemens Ladisch static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
82216e43467SClemens Ladisch 				struct snd_ctl_elem_info *info)
82316e43467SClemens Ladisch {
82416e43467SClemens Ladisch 	const char *const names[] = { "None", "CD Player" };
82516e43467SClemens Ladisch 
82616e43467SClemens Ladisch 	return snd_ctl_enum_info(info, 1, 2, names);
82716e43467SClemens Ladisch }
82816e43467SClemens Ladisch 
82916e43467SClemens Ladisch static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
83016e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
83116e43467SClemens Ladisch {
83216e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
83316e43467SClemens Ladisch 
83416e43467SClemens Ladisch 	value->value.enumerated.item[0] = dummy->iobox;
83516e43467SClemens Ladisch 	return 0;
83616e43467SClemens Ladisch }
83716e43467SClemens Ladisch 
83816e43467SClemens Ladisch static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
83916e43467SClemens Ladisch 			       struct snd_ctl_elem_value *value)
84016e43467SClemens Ladisch {
84116e43467SClemens Ladisch 	struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
84216e43467SClemens Ladisch 	int changed;
84316e43467SClemens Ladisch 
84416e43467SClemens Ladisch 	if (value->value.enumerated.item[0] > 1)
84516e43467SClemens Ladisch 		return -EINVAL;
84616e43467SClemens Ladisch 
84716e43467SClemens Ladisch 	changed = value->value.enumerated.item[0] != dummy->iobox;
84816e43467SClemens Ladisch 	if (changed) {
84916e43467SClemens Ladisch 		dummy->iobox = value->value.enumerated.item[0];
85016e43467SClemens Ladisch 
85116e43467SClemens Ladisch 		if (dummy->iobox) {
85216e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access &=
85316e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
85416e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access &=
85516e43467SClemens Ladisch 				~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
85616e43467SClemens Ladisch 		} else {
85716e43467SClemens Ladisch 			dummy->cd_volume_ctl->vd[0].access |=
85816e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
85916e43467SClemens Ladisch 			dummy->cd_switch_ctl->vd[0].access |=
86016e43467SClemens Ladisch 				SNDRV_CTL_ELEM_ACCESS_INACTIVE;
86116e43467SClemens Ladisch 		}
86216e43467SClemens Ladisch 
86316e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
86416e43467SClemens Ladisch 			       &dummy->cd_volume_ctl->id);
86516e43467SClemens Ladisch 		snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
86616e43467SClemens Ladisch 			       &dummy->cd_switch_ctl->id);
86716e43467SClemens Ladisch 	}
86816e43467SClemens Ladisch 
86916e43467SClemens Ladisch 	return changed;
87016e43467SClemens Ladisch }
87116e43467SClemens Ladisch 
8724a4d2cfdSTakashi Iwai static struct snd_kcontrol_new snd_dummy_controls[] = {
8731da177e4SLinus Torvalds DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
8741da177e4SLinus Torvalds DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
8751da177e4SLinus Torvalds DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
876e05d6964STakashi Iwai DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
8771da177e4SLinus Torvalds DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
878e05d6964STakashi Iwai DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
8791da177e4SLinus Torvalds DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
880e05d6964STakashi Iwai DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
8811da177e4SLinus Torvalds DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
88216e43467SClemens Ladisch DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
88316e43467SClemens Ladisch {
88416e43467SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
88516e43467SClemens Ladisch 	.name  = "External I/O Box",
88616e43467SClemens Ladisch 	.info  = snd_dummy_iobox_info,
88716e43467SClemens Ladisch 	.get   = snd_dummy_iobox_get,
88816e43467SClemens Ladisch 	.put   = snd_dummy_iobox_put,
88916e43467SClemens Ladisch },
8901da177e4SLinus Torvalds };
8911da177e4SLinus Torvalds 
892fbbb01a1SBill Pemberton static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
8931da177e4SLinus Torvalds {
8944a4d2cfdSTakashi Iwai 	struct snd_card *card = dummy->card;
89516e43467SClemens Ladisch 	struct snd_kcontrol *kcontrol;
8961da177e4SLinus Torvalds 	unsigned int idx;
8971da177e4SLinus Torvalds 	int err;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	spin_lock_init(&dummy->mixer_lock);
9001da177e4SLinus Torvalds 	strcpy(card->mixername, "Dummy Mixer");
90116e43467SClemens Ladisch 	dummy->iobox = 1;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
90416e43467SClemens Ladisch 		kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
90516e43467SClemens Ladisch 		err = snd_ctl_add(card, kcontrol);
9061a11cb64SJaroslav Kysela 		if (err < 0)
9071da177e4SLinus Torvalds 			return err;
90816e43467SClemens Ladisch 		if (!strcmp(kcontrol->id.name, "CD Volume"))
90916e43467SClemens Ladisch 			dummy->cd_volume_ctl = kcontrol;
91016e43467SClemens Ladisch 		else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
91116e43467SClemens Ladisch 			dummy->cd_switch_ctl = kcontrol;
91216e43467SClemens Ladisch 
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 	return 0;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
917129a4c9fSTakashi Iwai #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
9189b151fecSTakashi Iwai /*
9199b151fecSTakashi Iwai  * proc interface
9209b151fecSTakashi Iwai  */
921d5e1ca05SJaroslav Kysela static void print_formats(struct snd_dummy *dummy,
922d5e1ca05SJaroslav Kysela 			  struct snd_info_buffer *buffer)
9239b151fecSTakashi Iwai {
9249b151fecSTakashi Iwai 	int i;
9259b151fecSTakashi Iwai 
9269b151fecSTakashi Iwai 	for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
927d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.formats & (1ULL << i))
9289b151fecSTakashi Iwai 			snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
9299b151fecSTakashi Iwai 	}
9309b151fecSTakashi Iwai }
9319b151fecSTakashi Iwai 
932d5e1ca05SJaroslav Kysela static void print_rates(struct snd_dummy *dummy,
933d5e1ca05SJaroslav Kysela 			struct snd_info_buffer *buffer)
9349b151fecSTakashi Iwai {
9359b151fecSTakashi Iwai 	static int rates[] = {
9369b151fecSTakashi Iwai 		5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
9379b151fecSTakashi Iwai 		64000, 88200, 96000, 176400, 192000,
9389b151fecSTakashi Iwai 	};
9399b151fecSTakashi Iwai 	int i;
9409b151fecSTakashi Iwai 
941d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
9429b151fecSTakashi Iwai 		snd_iprintf(buffer, " continuous");
943d5e1ca05SJaroslav Kysela 	if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
9449b151fecSTakashi Iwai 		snd_iprintf(buffer, " knot");
9459b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(rates); i++)
946d5e1ca05SJaroslav Kysela 		if (dummy->pcm_hw.rates & (1 << i))
9479b151fecSTakashi Iwai 			snd_iprintf(buffer, " %d", rates[i]);
9489b151fecSTakashi Iwai }
9499b151fecSTakashi Iwai 
950d5e1ca05SJaroslav Kysela #define get_dummy_int_ptr(dummy, ofs) \
951d5e1ca05SJaroslav Kysela 	(unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
952d5e1ca05SJaroslav Kysela #define get_dummy_ll_ptr(dummy, ofs) \
953d5e1ca05SJaroslav Kysela 	(unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
9549b151fecSTakashi Iwai 
9559b151fecSTakashi Iwai struct dummy_hw_field {
9569b151fecSTakashi Iwai 	const char *name;
9579b151fecSTakashi Iwai 	const char *format;
9589b151fecSTakashi Iwai 	unsigned int offset;
9599b151fecSTakashi Iwai 	unsigned int size;
9609b151fecSTakashi Iwai };
9619b151fecSTakashi Iwai #define FIELD_ENTRY(item, fmt) {		   \
9629b151fecSTakashi Iwai 	.name = #item,				   \
9639b151fecSTakashi Iwai 	.format = fmt,				   \
9649b151fecSTakashi Iwai 	.offset = offsetof(struct snd_pcm_hardware, item), \
9659b151fecSTakashi Iwai 	.size = sizeof(dummy_pcm_hardware.item) }
9669b151fecSTakashi Iwai 
9679b151fecSTakashi Iwai static struct dummy_hw_field fields[] = {
9689b151fecSTakashi Iwai 	FIELD_ENTRY(formats, "%#llx"),
9699b151fecSTakashi Iwai 	FIELD_ENTRY(rates, "%#x"),
9709b151fecSTakashi Iwai 	FIELD_ENTRY(rate_min, "%d"),
9719b151fecSTakashi Iwai 	FIELD_ENTRY(rate_max, "%d"),
9729b151fecSTakashi Iwai 	FIELD_ENTRY(channels_min, "%d"),
9739b151fecSTakashi Iwai 	FIELD_ENTRY(channels_max, "%d"),
9749b151fecSTakashi Iwai 	FIELD_ENTRY(buffer_bytes_max, "%ld"),
9759b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_min, "%ld"),
9769b151fecSTakashi Iwai 	FIELD_ENTRY(period_bytes_max, "%ld"),
9779b151fecSTakashi Iwai 	FIELD_ENTRY(periods_min, "%d"),
9789b151fecSTakashi Iwai 	FIELD_ENTRY(periods_max, "%d"),
9799b151fecSTakashi Iwai };
9809b151fecSTakashi Iwai 
9819b151fecSTakashi Iwai static void dummy_proc_read(struct snd_info_entry *entry,
9829b151fecSTakashi Iwai 			    struct snd_info_buffer *buffer)
9839b151fecSTakashi Iwai {
984d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
9859b151fecSTakashi Iwai 	int i;
9869b151fecSTakashi Iwai 
9879b151fecSTakashi Iwai 	for (i = 0; i < ARRAY_SIZE(fields); i++) {
9889b151fecSTakashi Iwai 		snd_iprintf(buffer, "%s ", fields[i].name);
9899b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
9909b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
991d5e1ca05SJaroslav Kysela 				*get_dummy_int_ptr(dummy, fields[i].offset));
9929b151fecSTakashi Iwai 		else
9939b151fecSTakashi Iwai 			snd_iprintf(buffer, fields[i].format,
994d5e1ca05SJaroslav Kysela 				*get_dummy_ll_ptr(dummy, fields[i].offset));
9959b151fecSTakashi Iwai 		if (!strcmp(fields[i].name, "formats"))
996d5e1ca05SJaroslav Kysela 			print_formats(dummy, buffer);
9979b151fecSTakashi Iwai 		else if (!strcmp(fields[i].name, "rates"))
998d5e1ca05SJaroslav Kysela 			print_rates(dummy, buffer);
9999b151fecSTakashi Iwai 		snd_iprintf(buffer, "\n");
10009b151fecSTakashi Iwai 	}
10019b151fecSTakashi Iwai }
10029b151fecSTakashi Iwai 
10039b151fecSTakashi Iwai static void dummy_proc_write(struct snd_info_entry *entry,
10049b151fecSTakashi Iwai 			     struct snd_info_buffer *buffer)
10059b151fecSTakashi Iwai {
1006d5e1ca05SJaroslav Kysela 	struct snd_dummy *dummy = entry->private_data;
10079b151fecSTakashi Iwai 	char line[64];
10089b151fecSTakashi Iwai 
10099b151fecSTakashi Iwai 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
10109b151fecSTakashi Iwai 		char item[20];
10119b151fecSTakashi Iwai 		const char *ptr;
10129b151fecSTakashi Iwai 		unsigned long long val;
10139b151fecSTakashi Iwai 		int i;
10149b151fecSTakashi Iwai 
10159b151fecSTakashi Iwai 		ptr = snd_info_get_str(item, line, sizeof(item));
10169b151fecSTakashi Iwai 		for (i = 0; i < ARRAY_SIZE(fields); i++) {
10179b151fecSTakashi Iwai 			if (!strcmp(item, fields[i].name))
10189b151fecSTakashi Iwai 				break;
10199b151fecSTakashi Iwai 		}
10209b151fecSTakashi Iwai 		if (i >= ARRAY_SIZE(fields))
10219b151fecSTakashi Iwai 			continue;
10229b151fecSTakashi Iwai 		snd_info_get_str(item, ptr, sizeof(item));
1023b785a492SJingoo Han 		if (kstrtoull(item, 0, &val))
10249b151fecSTakashi Iwai 			continue;
10259b151fecSTakashi Iwai 		if (fields[i].size == sizeof(int))
1026d5e1ca05SJaroslav Kysela 			*get_dummy_int_ptr(dummy, fields[i].offset) = val;
10279b151fecSTakashi Iwai 		else
1028d5e1ca05SJaroslav Kysela 			*get_dummy_ll_ptr(dummy, fields[i].offset) = val;
10299b151fecSTakashi Iwai 	}
10309b151fecSTakashi Iwai }
10319b151fecSTakashi Iwai 
1032fbbb01a1SBill Pemberton static void dummy_proc_init(struct snd_dummy *chip)
10339b151fecSTakashi Iwai {
10349b151fecSTakashi Iwai 	struct snd_info_entry *entry;
10359b151fecSTakashi Iwai 
10369b151fecSTakashi Iwai 	if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
10379b151fecSTakashi Iwai 		snd_info_set_text_ops(entry, chip, dummy_proc_read);
10389b151fecSTakashi Iwai 		entry->c.text.write = dummy_proc_write;
10399b151fecSTakashi Iwai 		entry->mode |= S_IWUSR;
1040d5e1ca05SJaroslav Kysela 		entry->private_data = chip;
10419b151fecSTakashi Iwai 	}
10429b151fecSTakashi Iwai }
10439b151fecSTakashi Iwai #else
10449b151fecSTakashi Iwai #define dummy_proc_init(x)
1045129a4c9fSTakashi Iwai #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
10469b151fecSTakashi Iwai 
1047fbbb01a1SBill Pemberton static int snd_dummy_probe(struct platform_device *devptr)
10481da177e4SLinus Torvalds {
10494a4d2cfdSTakashi Iwai 	struct snd_card *card;
10504a4d2cfdSTakashi Iwai 	struct snd_dummy *dummy;
1051d5e1ca05SJaroslav Kysela 	struct dummy_model *m = NULL, **mdl;
10521da177e4SLinus Torvalds 	int idx, err;
10536e65c1ccSTakashi Iwai 	int dev = devptr->id;
10541da177e4SLinus Torvalds 
10555872f3f6STakashi Iwai 	err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1056bd7dd77cSTakashi Iwai 			   sizeof(struct snd_dummy), &card);
1057bd7dd77cSTakashi Iwai 	if (err < 0)
1058bd7dd77cSTakashi Iwai 		return err;
10594a4d2cfdSTakashi Iwai 	dummy = card->private_data;
10601da177e4SLinus Torvalds 	dummy->card = card;
1061d5e1ca05SJaroslav Kysela 	for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
1062d5e1ca05SJaroslav Kysela 		if (strcmp(model[dev], (*mdl)->name) == 0) {
1063d5e1ca05SJaroslav Kysela 			printk(KERN_INFO
1064d5e1ca05SJaroslav Kysela 				"snd-dummy: Using model '%s' for card %i\n",
1065d5e1ca05SJaroslav Kysela 				(*mdl)->name, card->number);
1066d5e1ca05SJaroslav Kysela 			m = dummy->model = *mdl;
1067d5e1ca05SJaroslav Kysela 			break;
1068d5e1ca05SJaroslav Kysela 		}
1069d5e1ca05SJaroslav Kysela 	}
10701da177e4SLinus Torvalds 	for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
10711da177e4SLinus Torvalds 		if (pcm_substreams[dev] < 1)
10721da177e4SLinus Torvalds 			pcm_substreams[dev] = 1;
10731da177e4SLinus Torvalds 		if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
10741da177e4SLinus Torvalds 			pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
10751a11cb64SJaroslav Kysela 		err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
10761a11cb64SJaroslav Kysela 		if (err < 0)
10771da177e4SLinus Torvalds 			goto __nodev;
10781da177e4SLinus Torvalds 	}
1079d5e1ca05SJaroslav Kysela 
1080d5e1ca05SJaroslav Kysela 	dummy->pcm_hw = dummy_pcm_hardware;
1081d5e1ca05SJaroslav Kysela 	if (m) {
1082d5e1ca05SJaroslav Kysela 		if (m->formats)
1083d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.formats = m->formats;
1084d5e1ca05SJaroslav Kysela 		if (m->buffer_bytes_max)
1085d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1086d5e1ca05SJaroslav Kysela 		if (m->period_bytes_min)
1087d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1088d5e1ca05SJaroslav Kysela 		if (m->period_bytes_max)
1089d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1090d5e1ca05SJaroslav Kysela 		if (m->periods_min)
1091d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_min = m->periods_min;
1092d5e1ca05SJaroslav Kysela 		if (m->periods_max)
1093d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.periods_max = m->periods_max;
1094d5e1ca05SJaroslav Kysela 		if (m->rates)
1095d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rates = m->rates;
1096d5e1ca05SJaroslav Kysela 		if (m->rate_min)
1097d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_min = m->rate_min;
1098d5e1ca05SJaroslav Kysela 		if (m->rate_max)
1099d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.rate_max = m->rate_max;
1100d5e1ca05SJaroslav Kysela 		if (m->channels_min)
1101d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_min = m->channels_min;
1102d5e1ca05SJaroslav Kysela 		if (m->channels_max)
1103d5e1ca05SJaroslav Kysela 			dummy->pcm_hw.channels_max = m->channels_max;
1104d5e1ca05SJaroslav Kysela 	}
1105d5e1ca05SJaroslav Kysela 
11061a11cb64SJaroslav Kysela 	err = snd_card_dummy_new_mixer(dummy);
11071a11cb64SJaroslav Kysela 	if (err < 0)
11081da177e4SLinus Torvalds 		goto __nodev;
11091da177e4SLinus Torvalds 	strcpy(card->driver, "Dummy");
11101da177e4SLinus Torvalds 	strcpy(card->shortname, "Dummy");
11111da177e4SLinus Torvalds 	sprintf(card->longname, "Dummy %i", dev + 1);
111216dab54bSTakashi Iwai 
11139b151fecSTakashi Iwai 	dummy_proc_init(dummy);
11149b151fecSTakashi Iwai 
11151a11cb64SJaroslav Kysela 	err = snd_card_register(card);
11161a11cb64SJaroslav Kysela 	if (err == 0) {
11176e65c1ccSTakashi Iwai 		platform_set_drvdata(devptr, card);
11181da177e4SLinus Torvalds 		return 0;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds       __nodev:
11211da177e4SLinus Torvalds 	snd_card_free(card);
11221da177e4SLinus Torvalds 	return err;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
1125fbbb01a1SBill Pemberton static int snd_dummy_remove(struct platform_device *devptr)
11266e65c1ccSTakashi Iwai {
11276e65c1ccSTakashi Iwai 	snd_card_free(platform_get_drvdata(devptr));
11286e65c1ccSTakashi Iwai 	return 0;
11296e65c1ccSTakashi Iwai }
11306e65c1ccSTakashi Iwai 
1131d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP
1132284e7ca7STakashi Iwai static int snd_dummy_suspend(struct device *pdev)
11336e65c1ccSTakashi Iwai {
1134284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11356e65c1ccSTakashi Iwai 	struct snd_dummy *dummy = card->private_data;
11366e65c1ccSTakashi Iwai 
11376e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
11386e65c1ccSTakashi Iwai 	snd_pcm_suspend_all(dummy->pcm);
11396e65c1ccSTakashi Iwai 	return 0;
11406e65c1ccSTakashi Iwai }
11416e65c1ccSTakashi Iwai 
1142284e7ca7STakashi Iwai static int snd_dummy_resume(struct device *pdev)
11436e65c1ccSTakashi Iwai {
1144284e7ca7STakashi Iwai 	struct snd_card *card = dev_get_drvdata(pdev);
11456e65c1ccSTakashi Iwai 
11466e65c1ccSTakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
11476e65c1ccSTakashi Iwai 	return 0;
11486e65c1ccSTakashi Iwai }
1149284e7ca7STakashi Iwai 
1150284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
1151284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	&snd_dummy_pm
1152284e7ca7STakashi Iwai #else
1153284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS	NULL
11546e65c1ccSTakashi Iwai #endif
11556e65c1ccSTakashi Iwai 
11566e65c1ccSTakashi Iwai #define SND_DUMMY_DRIVER	"snd_dummy"
11576e65c1ccSTakashi Iwai 
11586e65c1ccSTakashi Iwai static struct platform_driver snd_dummy_driver = {
11596e65c1ccSTakashi Iwai 	.probe		= snd_dummy_probe,
1160fbbb01a1SBill Pemberton 	.remove		= snd_dummy_remove,
11616e65c1ccSTakashi Iwai 	.driver		= {
11628bf01d8aSTakashi Iwai 		.name	= SND_DUMMY_DRIVER,
1163284e7ca7STakashi Iwai 		.pm	= SND_DUMMY_PM_OPS,
11646e65c1ccSTakashi Iwai 	},
11656e65c1ccSTakashi Iwai };
11666e65c1ccSTakashi Iwai 
1167c12aad6eSRandy Dunlap static void snd_dummy_unregister_all(void)
1168f7a9275dSClemens Ladisch {
1169f7a9275dSClemens Ladisch 	int i;
1170f7a9275dSClemens Ladisch 
1171f7a9275dSClemens Ladisch 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
1172f7a9275dSClemens Ladisch 		platform_device_unregister(devices[i]);
1173f7a9275dSClemens Ladisch 	platform_driver_unregister(&snd_dummy_driver);
1174a68c4d11STakashi Iwai 	free_fake_buffer();
1175f7a9275dSClemens Ladisch }
1176f7a9275dSClemens Ladisch 
11771da177e4SLinus Torvalds static int __init alsa_card_dummy_init(void)
11781da177e4SLinus Torvalds {
11796e65c1ccSTakashi Iwai 	int i, cards, err;
11801da177e4SLinus Torvalds 
11811a11cb64SJaroslav Kysela 	err = platform_driver_register(&snd_dummy_driver);
11821a11cb64SJaroslav Kysela 	if (err < 0)
11836e65c1ccSTakashi Iwai 		return err;
11846e65c1ccSTakashi Iwai 
1185a68c4d11STakashi Iwai 	err = alloc_fake_buffer();
1186a68c4d11STakashi Iwai 	if (err < 0) {
1187a68c4d11STakashi Iwai 		platform_driver_unregister(&snd_dummy_driver);
1188a68c4d11STakashi Iwai 		return err;
1189a68c4d11STakashi Iwai 	}
1190a68c4d11STakashi Iwai 
11916e65c1ccSTakashi Iwai 	cards = 0;
11928278ca8fSTakashi Iwai 	for (i = 0; i < SNDRV_CARDS; i++) {
11936e65c1ccSTakashi Iwai 		struct platform_device *device;
11948278ca8fSTakashi Iwai 		if (! enable[i])
11958278ca8fSTakashi Iwai 			continue;
11966e65c1ccSTakashi Iwai 		device = platform_device_register_simple(SND_DUMMY_DRIVER,
11976e65c1ccSTakashi Iwai 							 i, NULL, 0);
1198a182ee98SRene Herman 		if (IS_ERR(device))
1199a182ee98SRene Herman 			continue;
12007152447dSRene Herman 		if (!platform_get_drvdata(device)) {
12017152447dSRene Herman 			platform_device_unregister(device);
12027152447dSRene Herman 			continue;
12037152447dSRene Herman 		}
1204f7a9275dSClemens Ladisch 		devices[i] = device;
12051da177e4SLinus Torvalds 		cards++;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 	if (!cards) {
12081da177e4SLinus Torvalds #ifdef MODULE
12091da177e4SLinus Torvalds 		printk(KERN_ERR "Dummy soundcard not found or device busy\n");
12101da177e4SLinus Torvalds #endif
1211a182ee98SRene Herman 		snd_dummy_unregister_all();
1212a182ee98SRene Herman 		return -ENODEV;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 	return 0;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds static void __exit alsa_card_dummy_exit(void)
12181da177e4SLinus Torvalds {
1219f7a9275dSClemens Ladisch 	snd_dummy_unregister_all();
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds module_init(alsa_card_dummy_init)
12231da177e4SLinus Torvalds module_exit(alsa_card_dummy_exit)
1224