blob: 9c827b15445875e6d347d0afb99073b86a8e2182 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dummy soundcard
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <sound/driver.h>
22#include <linux/init.h>
23#include <linux/jiffies.h>
24#include <linux/slab.h>
25#include <linux/time.h>
26#include <linux/wait.h>
27#include <linux/moduleparam.h>
28#include <sound/core.h>
29#include <sound/control.h>
30#include <sound/pcm.h>
31#include <sound/rawmidi.h>
32#include <sound/initval.h>
33
34MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
36MODULE_LICENSE("GPL");
37MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
38
39#define MAX_PCM_DEVICES 4
40#define MAX_PCM_SUBSTREAMS 16
41#define MAX_MIDI_DEVICES 2
42
43#if 0 /* emu10k1 emulation */
44#define MAX_BUFFER_SIZE (128 * 1024)
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +010045static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 int err;
48 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
49 return err;
50 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
51 return err;
52 return 0;
53}
54#define add_playback_constraints emu10k1_playback_constraints
55#endif
56
57#if 0 /* RME9652 emulation */
58#define MAX_BUFFER_SIZE (26 * 64 * 1024)
59#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
60#define USE_CHANNELS_MIN 26
61#define USE_CHANNELS_MAX 26
62#define USE_PERIODS_MIN 2
63#define USE_PERIODS_MAX 2
64#endif
65
66#if 0 /* ICE1712 emulation */
67#define MAX_BUFFER_SIZE (256 * 1024)
68#define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
69#define USE_CHANNELS_MIN 10
70#define USE_CHANNELS_MAX 10
71#define USE_PERIODS_MIN 1
72#define USE_PERIODS_MAX 1024
73#endif
74
75#if 0 /* UDA1341 emulation */
76#define MAX_BUFFER_SIZE (16380)
77#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
78#define USE_CHANNELS_MIN 2
79#define USE_CHANNELS_MAX 2
80#define USE_PERIODS_MIN 2
81#define USE_PERIODS_MAX 255
82#endif
83
84#if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
85#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
86#define USE_CHANNELS_MIN 2
87#define USE_CHANNELS_MAX 2
88#define USE_RATE SNDRV_PCM_RATE_48000
89#define USE_RATE_MIN 48000
90#define USE_RATE_MAX 48000
91#endif
92
93
94/* defaults */
95#ifndef MAX_BUFFER_SIZE
96#define MAX_BUFFER_SIZE (64*1024)
97#endif
98#ifndef USE_FORMATS
99#define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
100#endif
101#ifndef USE_RATE
102#define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
103#define USE_RATE_MIN 5500
104#define USE_RATE_MAX 48000
105#endif
106#ifndef USE_CHANNELS_MIN
107#define USE_CHANNELS_MIN 1
108#endif
109#ifndef USE_CHANNELS_MAX
110#define USE_CHANNELS_MAX 2
111#endif
112#ifndef USE_PERIODS_MIN
113#define USE_PERIODS_MIN 1
114#endif
115#ifndef USE_PERIODS_MAX
116#define USE_PERIODS_MAX 1024
117#endif
118#ifndef add_playback_constraints
119#define add_playback_constraints(x) 0
120#endif
121#ifndef add_capture_constraints
122#define add_capture_constraints(x) 0
123#endif
124
125static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
126static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
127static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
128static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
129static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
130//static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
131
132module_param_array(index, int, NULL, 0444);
133MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
134module_param_array(id, charp, NULL, 0444);
135MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
136module_param_array(enable, bool, NULL, 0444);
137MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
138module_param_array(pcm_devs, int, NULL, 0444);
139MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
140module_param_array(pcm_substreams, int, NULL, 0444);
141MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
142//module_param_array(midi_devs, int, NULL, 0444);
143//MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
144
145#define MIXER_ADDR_MASTER 0
146#define MIXER_ADDR_LINE 1
147#define MIXER_ADDR_MIC 2
148#define MIXER_ADDR_SYNTH 3
149#define MIXER_ADDR_CD 4
150#define MIXER_ADDR_LAST 4
151
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100152struct snd_dummy {
153 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spinlock_t mixer_lock;
155 int mixer_volume[MIXER_ADDR_LAST+1][2];
156 int capture_source[MIXER_ADDR_LAST+1][2];
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100159struct snd_dummy_pcm {
160 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spinlock_t lock;
162 struct timer_list timer;
163 unsigned int pcm_size;
164 unsigned int pcm_count;
165 unsigned int pcm_bps; /* bytes per second */
166 unsigned int pcm_jiffie; /* bytes per one jiffie */
167 unsigned int pcm_irq_pos; /* IRQ position */
168 unsigned int pcm_buf_pos; /* position in buffer */
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100169 struct snd_pcm_substream *substream;
170};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100172static struct snd_card *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100175static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 dpcm->timer.expires = 1 + jiffies;
178 add_timer(&dpcm->timer);
179}
180
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100181static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 del_timer(&dpcm->timer);
184}
185
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100186static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100188 struct snd_pcm_runtime *runtime = substream->runtime;
189 struct snd_dummy_pcm *dpcm = runtime->private_data;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100190 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100192 spin_lock(&dpcm->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (cmd == SNDRV_PCM_TRIGGER_START) {
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100194 snd_card_dummy_pcm_timer_start(dpcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100196 snd_card_dummy_pcm_timer_stop(dpcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } else {
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100198 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100200 spin_unlock(&dpcm->lock);
201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100204static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100206 struct snd_pcm_runtime *runtime = substream->runtime;
207 struct snd_dummy_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int bps;
209
210 bps = runtime->rate * runtime->channels;
211 bps *= snd_pcm_format_width(runtime->format);
212 bps /= 8;
213 if (bps <= 0)
214 return -EINVAL;
215 dpcm->pcm_bps = bps;
216 dpcm->pcm_jiffie = bps / HZ;
217 dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
218 dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
219 dpcm->pcm_irq_pos = 0;
220 dpcm->pcm_buf_pos = 0;
221 return 0;
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static void snd_card_dummy_pcm_timer_function(unsigned long data)
225{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100226 struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100228 spin_lock(&dpcm->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 dpcm->timer.expires = 1 + jiffies;
230 add_timer(&dpcm->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
232 dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
233 dpcm->pcm_buf_pos %= dpcm->pcm_size;
234 if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
235 dpcm->pcm_irq_pos %= dpcm->pcm_count;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100236 spin_unlock(&dpcm->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 snd_pcm_period_elapsed(dpcm->substream);
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100238 spin_lock(&dpcm->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100240 spin_unlock(&dpcm->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100243static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100245 struct snd_pcm_runtime *runtime = substream->runtime;
246 struct snd_dummy_pcm *dpcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
249}
250
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100251static struct snd_pcm_hardware snd_card_dummy_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
254 SNDRV_PCM_INFO_MMAP_VALID),
255 .formats = USE_FORMATS,
256 .rates = USE_RATE,
257 .rate_min = USE_RATE_MIN,
258 .rate_max = USE_RATE_MAX,
259 .channels_min = USE_CHANNELS_MIN,
260 .channels_max = USE_CHANNELS_MAX,
261 .buffer_bytes_max = MAX_BUFFER_SIZE,
262 .period_bytes_min = 64,
263 .period_bytes_max = MAX_BUFFER_SIZE,
264 .periods_min = USE_PERIODS_MIN,
265 .periods_max = USE_PERIODS_MAX,
266 .fifo_size = 0,
267};
268
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100269static struct snd_pcm_hardware snd_card_dummy_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
272 SNDRV_PCM_INFO_MMAP_VALID),
273 .formats = USE_FORMATS,
274 .rates = USE_RATE,
275 .rate_min = USE_RATE_MIN,
276 .rate_max = USE_RATE_MAX,
277 .channels_min = USE_CHANNELS_MIN,
278 .channels_max = USE_CHANNELS_MAX,
279 .buffer_bytes_max = MAX_BUFFER_SIZE,
280 .period_bytes_min = 64,
281 .period_bytes_max = MAX_BUFFER_SIZE,
282 .periods_min = USE_PERIODS_MIN,
283 .periods_max = USE_PERIODS_MAX,
284 .fifo_size = 0,
285};
286
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100287static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100289 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100292static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream,
293 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
296}
297
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100298static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 return snd_pcm_lib_free_pages(substream);
301}
302
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100303static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream)
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100304{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100305 struct snd_dummy_pcm *dpcm;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100306
307 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
308 if (! dpcm)
309 return dpcm;
310 init_timer(&dpcm->timer);
311 dpcm->timer.data = (unsigned long) dpcm;
312 dpcm->timer.function = snd_card_dummy_pcm_timer_function;
313 spin_lock_init(&dpcm->lock);
314 dpcm->substream = substream;
315 return dpcm;
316}
317
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100318static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100320 struct snd_pcm_runtime *runtime = substream->runtime;
321 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int err;
323
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100324 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 runtime->private_data = dpcm;
327 runtime->private_free = snd_card_dummy_runtime_free;
328 runtime->hw = snd_card_dummy_playback;
329 if (substream->pcm->device & 1) {
330 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
331 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
332 }
333 if (substream->pcm->device & 2)
334 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
335 if ((err = add_playback_constraints(runtime)) < 0) {
336 kfree(dpcm);
337 return err;
338 }
339
340 return 0;
341}
342
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100343static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100345 struct snd_pcm_runtime *runtime = substream->runtime;
346 struct snd_dummy_pcm *dpcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err;
348
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100349 if ((dpcm = new_pcm_stream(substream)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 runtime->private_data = dpcm;
352 runtime->private_free = snd_card_dummy_runtime_free;
353 runtime->hw = snd_card_dummy_capture;
354 if (substream->pcm->device == 1) {
355 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
356 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
357 }
358 if (substream->pcm->device & 2)
359 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
360 if ((err = add_capture_constraints(runtime)) < 0) {
361 kfree(dpcm);
362 return err;
363 }
364
365 return 0;
366}
367
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100368static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 return 0;
371}
372
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100373static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 return 0;
376}
377
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100378static struct snd_pcm_ops snd_card_dummy_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .open = snd_card_dummy_playback_open,
380 .close = snd_card_dummy_playback_close,
381 .ioctl = snd_pcm_lib_ioctl,
382 .hw_params = snd_card_dummy_hw_params,
383 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100384 .prepare = snd_card_dummy_pcm_prepare,
385 .trigger = snd_card_dummy_pcm_trigger,
386 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100389static struct snd_pcm_ops snd_card_dummy_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .open = snd_card_dummy_capture_open,
391 .close = snd_card_dummy_capture_close,
392 .ioctl = snd_pcm_lib_ioctl,
393 .hw_params = snd_card_dummy_hw_params,
394 .hw_free = snd_card_dummy_hw_free,
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100395 .prepare = snd_card_dummy_pcm_prepare,
396 .trigger = snd_card_dummy_pcm_trigger,
397 .pointer = snd_card_dummy_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100400static int __init snd_card_dummy_pcm(struct snd_dummy *dummy, int device, int substreams)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100402 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int err;
404
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100405 if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
406 substreams, substreams, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return err;
408 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
409 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
410 pcm->private_data = dummy;
411 pcm->info_flags = 0;
412 strcpy(pcm->name, "Dummy PCM");
413 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
414 snd_dma_continuous_data(GFP_KERNEL),
415 0, 64*1024);
416 return 0;
417}
418
419#define DUMMY_VOLUME(xname, xindex, addr) \
420{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
421 .info = snd_dummy_volume_info, \
422 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
423 .private_value = addr }
424
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100425static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
429 uinfo->count = 2;
430 uinfo->value.integer.min = -50;
431 uinfo->value.integer.max = 100;
432 return 0;
433}
434
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100435static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100438 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int addr = kcontrol->private_value;
440
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100441 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
443 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100444 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100448static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
449 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100451 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int change, addr = kcontrol->private_value;
453 int left, right;
454
455 left = ucontrol->value.integer.value[0];
456 if (left < -50)
457 left = -50;
458 if (left > 100)
459 left = 100;
460 right = ucontrol->value.integer.value[1];
461 if (right < -50)
462 right = -50;
463 if (right > 100)
464 right = 100;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100465 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 change = dummy->mixer_volume[addr][0] != left ||
467 dummy->mixer_volume[addr][1] != right;
468 dummy->mixer_volume[addr][0] = left;
469 dummy->mixer_volume[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100470 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return change;
472}
473
474#define DUMMY_CAPSRC(xname, xindex, addr) \
475{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
476 .info = snd_dummy_capsrc_info, \
477 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
478 .private_value = addr }
479
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100480static int snd_dummy_capsrc_info(struct snd_kcontrol *kcontrol,
481 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
484 uinfo->count = 2;
485 uinfo->value.integer.min = 0;
486 uinfo->value.integer.max = 1;
487 return 0;
488}
489
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100490static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100493 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int addr = kcontrol->private_value;
495
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100496 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
498 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100499 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100503static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100505 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int change, addr = kcontrol->private_value;
507 int left, right;
508
509 left = ucontrol->value.integer.value[0] & 1;
510 right = ucontrol->value.integer.value[1] & 1;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100511 spin_lock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 change = dummy->capture_source[addr][0] != left &&
513 dummy->capture_source[addr][1] != right;
514 dummy->capture_source[addr][0] = left;
515 dummy->capture_source[addr][1] = right;
Takashi Iwaic8eb6ba2005-11-17 10:20:23 +0100516 spin_unlock_irq(&dummy->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return change;
518}
519
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100520static struct snd_kcontrol_new snd_dummy_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
522DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
523DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
524DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
525DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
526DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
527DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
528DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
529DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
530DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
531};
532
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100533static int __init snd_card_dummy_new_mixer(struct snd_dummy *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100535 struct snd_card *card = dummy->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 unsigned int idx;
537 int err;
538
539 snd_assert(dummy != NULL, return -EINVAL);
540 spin_lock_init(&dummy->mixer_lock);
541 strcpy(card->mixername, "Dummy Mixer");
542
543 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
544 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
545 return err;
546 }
547 return 0;
548}
549
550static int __init snd_card_dummy_probe(int dev)
551{
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100552 struct snd_card *card;
553 struct snd_dummy *dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int idx, err;
555
556 if (!enable[dev])
557 return -ENODEV;
558 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100559 sizeof(struct snd_dummy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (card == NULL)
561 return -ENOMEM;
Takashi Iwai4a4d2cf2005-11-17 14:27:28 +0100562 dummy = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 dummy->card = card;
564 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
565 if (pcm_substreams[dev] < 1)
566 pcm_substreams[dev] = 1;
567 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
568 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
569 if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
570 goto __nodev;
571 }
572 if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
573 goto __nodev;
574 strcpy(card->driver, "Dummy");
575 strcpy(card->shortname, "Dummy");
576 sprintf(card->longname, "Dummy %i", dev + 1);
Takashi Iwai16dab542005-09-05 17:17:58 +0200577
578 if ((err = snd_card_set_generic_dev(card)) < 0)
579 goto __nodev;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if ((err = snd_card_register(card)) == 0) {
582 snd_dummy_cards[dev] = card;
583 return 0;
584 }
585 __nodev:
586 snd_card_free(card);
587 return err;
588}
589
590static int __init alsa_card_dummy_init(void)
591{
592 int dev, cards;
593
594 for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
595 if (snd_card_dummy_probe(dev) < 0) {
596#ifdef MODULE
597 printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1);
598#endif
599 break;
600 }
601 cards++;
602 }
603 if (!cards) {
604#ifdef MODULE
605 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
606#endif
607 return -ENODEV;
608 }
609 return 0;
610}
611
612static void __exit alsa_card_dummy_exit(void)
613{
614 int idx;
615
616 for (idx = 0; idx < SNDRV_CARDS; idx++)
617 snd_card_free(snd_dummy_cards[idx]);
618}
619
620module_init(alsa_card_dummy_init)
621module_exit(alsa_card_dummy_exit)