3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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>
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35 MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
36 MODULE_LICENSE("GPL");
37 MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
39 #define MAX_PCM_DEVICES 4
40 #define MAX_PCM_SUBSTREAMS 16
41 #define MAX_MIDI_DEVICES 2
43 #if 0 /* emu10k1 emulation */
44 #define MAX_BUFFER_SIZE (128 * 1024)
45 static int emu10k1_playback_constraints(snd_pcm_runtime_t *runtime)
48 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
50 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
54 #define add_playback_constraints emu10k1_playback_constraints
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
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
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
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
95 #ifndef MAX_BUFFER_SIZE
96 #define MAX_BUFFER_SIZE (64*1024)
99 #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
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
106 #ifndef USE_CHANNELS_MIN
107 #define USE_CHANNELS_MIN 1
109 #ifndef USE_CHANNELS_MAX
110 #define USE_CHANNELS_MAX 2
112 #ifndef USE_PERIODS_MIN
113 #define USE_PERIODS_MIN 1
115 #ifndef USE_PERIODS_MAX
116 #define USE_PERIODS_MAX 1024
118 #ifndef add_playback_constraints
119 #define add_playback_constraints(x) 0
121 #ifndef add_capture_constraints
122 #define add_capture_constraints(x) 0
125 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
126 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
127 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
128 static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
129 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
130 //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
132 module_param_array(index, int, NULL, 0444);
133 MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
134 module_param_array(id, charp, NULL, 0444);
135 MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
136 module_param_array(enable, bool, NULL, 0444);
137 MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
138 module_param_array(pcm_devs, int, NULL, 0444);
139 MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
140 module_param_array(pcm_substreams, int, NULL, 0444);
141 MODULE_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.");
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
152 typedef struct snd_card_dummy {
154 spinlock_t mixer_lock;
155 int mixer_volume[MIXER_ADDR_LAST+1][2];
156 int capture_source[MIXER_ADDR_LAST+1][2];
159 typedef struct snd_card_dummy_pcm {
160 snd_card_dummy_t *dummy;
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 */
169 snd_pcm_substream_t *substream;
170 } snd_card_dummy_pcm_t;
172 static snd_card_t *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
175 static inline void snd_card_dummy_pcm_timer_start(snd_card_dummy_pcm_t *dpcm)
177 dpcm->timer.expires = 1 + jiffies;
178 add_timer(&dpcm->timer);
181 static inline void snd_card_dummy_pcm_timer_stop(snd_card_dummy_pcm_t *dpcm)
183 del_timer(&dpcm->timer);
186 static int snd_card_dummy_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
188 snd_pcm_runtime_t *runtime = substream->runtime;
189 snd_dummy_card_pcm_t *dpcm = runtime->private_data;
192 spin_lock(&dpcm->lock);
193 if (cmd == SNDRV_PCM_TRIGGER_START) {
194 snd_card_dummy_pcm_timer_start(dpcm);
195 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
196 snd_card_dummy_pcm_timer_stop(dpcm);
200 spin_unlock(&dpcm->lock);
204 static int snd_card_dummy_pcm_prepare(snd_pcm_substream_t * substream)
206 snd_pcm_runtime_t *runtime = substream->runtime;
207 snd_card_dummy_pcm_t *dpcm = runtime->private_data;
210 bps = runtime->rate * runtime->channels;
211 bps *= snd_pcm_format_width(runtime->format);
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;
224 static void snd_card_dummy_pcm_timer_function(unsigned long data)
226 snd_card_dummy_pcm_t *dpcm = (snd_card_dummy_pcm_t *)data;
228 spin_lock(&dpcm->lock);
229 dpcm->timer.expires = 1 + jiffies;
230 add_timer(&dpcm->timer);
231 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;
236 spin_unlock(&dpcm->lock);
237 snd_pcm_period_elapsed(dpcm->substream);
238 spin_lock(&dpcm->lock);
240 spin_unlock(&dpcm->lock);
243 static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(snd_pcm_substream_t * substream)
245 snd_pcm_runtime_t *runtime = substream->runtime;
246 snd_card_dummy_pcm_t *dpcm = runtime->private_data;
248 return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
251 static snd_pcm_hardware_t snd_card_dummy_playback =
253 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
254 SNDRV_PCM_INFO_MMAP_VALID),
255 .formats = USE_FORMATS,
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,
269 static snd_pcm_hardware_t snd_card_dummy_capture =
271 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
272 SNDRV_PCM_INFO_MMAP_VALID),
273 .formats = USE_FORMATS,
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,
287 static void snd_card_dummy_runtime_free(snd_pcm_runtime_t *runtime)
289 kfree(runtime->private_data);
292 static int snd_card_dummy_hw_params(snd_pcm_substream_t * substream,
293 snd_pcm_hw_params_t * hw_params)
295 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
298 static int snd_card_dummy_hw_free(snd_pcm_substream_t * substream)
300 return snd_pcm_lib_free_pages(substream);
303 static snd_card_dummy_pcm_t *new_pcm_stream(snd_pcm_substream_t *substream)
305 snd_card_dummy_pcm_t *dpcm;
307 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
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;
318 static int snd_card_dummy_playback_open(snd_pcm_substream_t * substream)
320 snd_pcm_runtime_t *runtime = substream->runtime;
321 snd_card_dummy_pcm_t *dpcm;
324 if ((dpcm = new_pcm_stream(substream)) == NULL)
326 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;
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) {
343 static int snd_card_dummy_capture_open(snd_pcm_substream_t * substream)
345 snd_pcm_runtime_t *runtime = substream->runtime;
346 snd_card_dummy_pcm_t *dpcm;
349 if ((dpcm = new_pcm_stream(substream)) == NULL)
351 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;
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) {
368 static int snd_card_dummy_playback_close(snd_pcm_substream_t * substream)
373 static int snd_card_dummy_capture_close(snd_pcm_substream_t * substream)
378 static snd_pcm_ops_t snd_card_dummy_playback_ops = {
379 .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,
384 .prepare = snd_card_dummy_pcm_prepare,
385 .trigger = snd_card_dummy_pcm_trigger,
386 .pointer = snd_card_dummy_pcm_pointer,
389 static snd_pcm_ops_t snd_card_dummy_capture_ops = {
390 .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,
395 .prepare = snd_card_dummy_pcm_prepare,
396 .trigger = snd_card_dummy_pcm_trigger,
397 .pointer = snd_card_dummy_pcm_pointer,
400 static int __init snd_card_dummy_pcm(snd_card_dummy_t *dummy, int device, int substreams)
405 if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device, substreams, substreams, &pcm)) < 0)
407 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
408 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
409 pcm->private_data = dummy;
411 strcpy(pcm->name, "Dummy PCM");
412 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
413 snd_dma_continuous_data(GFP_KERNEL),
418 #define DUMMY_VOLUME(xname, xindex, addr) \
419 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
420 .info = snd_dummy_volume_info, \
421 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
422 .private_value = addr }
424 static int snd_dummy_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
428 uinfo->value.integer.min = -50;
429 uinfo->value.integer.max = 100;
433 static int snd_dummy_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
435 snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
436 int addr = kcontrol->private_value;
438 spin_lock_irq(&dummy->mixer_lock);
439 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
440 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
441 spin_unlock_irq(&dummy->mixer_lock);
445 static int snd_dummy_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
447 snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
448 int change, addr = kcontrol->private_value;
451 left = ucontrol->value.integer.value[0];
456 right = ucontrol->value.integer.value[1];
461 spin_lock_irq(&dummy->mixer_lock);
462 change = dummy->mixer_volume[addr][0] != left ||
463 dummy->mixer_volume[addr][1] != right;
464 dummy->mixer_volume[addr][0] = left;
465 dummy->mixer_volume[addr][1] = right;
466 spin_unlock_irq(&dummy->mixer_lock);
470 #define DUMMY_CAPSRC(xname, xindex, addr) \
471 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
472 .info = snd_dummy_capsrc_info, \
473 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
474 .private_value = addr }
476 static int snd_dummy_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
478 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
480 uinfo->value.integer.min = 0;
481 uinfo->value.integer.max = 1;
485 static int snd_dummy_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
487 snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
488 int addr = kcontrol->private_value;
490 spin_lock_irq(&dummy->mixer_lock);
491 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
492 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
493 spin_unlock_irq(&dummy->mixer_lock);
497 static int snd_dummy_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
499 snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
500 int change, addr = kcontrol->private_value;
503 left = ucontrol->value.integer.value[0] & 1;
504 right = ucontrol->value.integer.value[1] & 1;
505 spin_lock_irq(&dummy->mixer_lock);
506 change = dummy->capture_source[addr][0] != left &&
507 dummy->capture_source[addr][1] != right;
508 dummy->capture_source[addr][0] = left;
509 dummy->capture_source[addr][1] = right;
510 spin_unlock_irq(&dummy->mixer_lock);
514 static snd_kcontrol_new_t snd_dummy_controls[] = {
515 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
516 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
517 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
518 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
519 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
520 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
521 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
522 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
523 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
524 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
527 static int __init snd_card_dummy_new_mixer(snd_card_dummy_t * dummy)
529 snd_card_t *card = dummy->card;
533 snd_assert(dummy != NULL, return -EINVAL);
534 spin_lock_init(&dummy->mixer_lock);
535 strcpy(card->mixername, "Dummy Mixer");
537 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
538 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
544 static int __init snd_card_dummy_probe(int dev)
547 struct snd_card_dummy *dummy;
552 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
553 sizeof(struct snd_card_dummy));
556 dummy = (struct snd_card_dummy *)card->private_data;
558 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
559 if (pcm_substreams[dev] < 1)
560 pcm_substreams[dev] = 1;
561 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
562 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
563 if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
566 if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
568 strcpy(card->driver, "Dummy");
569 strcpy(card->shortname, "Dummy");
570 sprintf(card->longname, "Dummy %i", dev + 1);
572 if ((err = snd_card_set_generic_dev(card)) < 0)
575 if ((err = snd_card_register(card)) == 0) {
576 snd_dummy_cards[dev] = card;
584 static int __init alsa_card_dummy_init(void)
588 for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
589 if (snd_card_dummy_probe(dev) < 0) {
591 printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1);
599 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
606 static void __exit alsa_card_dummy_exit(void)
610 for (idx = 0; idx < SNDRV_CARDS; idx++)
611 snd_card_free(snd_dummy_cards[idx]);
614 module_init(alsa_card_dummy_init)
615 module_exit(alsa_card_dummy_exit)