patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / drivers / dummy.c
1 /*
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
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35 MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
36 MODULE_LICENSE("GPL");
37 MODULE_CLASSES("{sound}");
38 MODULE_DEVICES("{{ALSA,Dummy soundcard}}");
39
40 #define MAX_PCM_DEVICES         4
41 #define MAX_PCM_SUBSTREAMS      16
42 #define MAX_MIDI_DEVICES        2
43
44 #if 0 /* emu10k1 emulation */
45 #define MAX_BUFFER_SIZE         (128 * 1024)
46 static int emu10k1_playback_constraints(snd_pcm_runtime_t *runtime)
47 {
48         int err;
49         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
50                 return err;
51         if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
52                 return err;
53         return 0;
54 }
55 #define add_playback_constraints emu10k1_playback_constraints
56 #endif
57
58 #if 0 /* RME9652 emulation */
59 #define MAX_BUFFER_SIZE         (26 * 64 * 1024)
60 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S32_LE
61 #define USE_CHANNELS_MIN        26
62 #define USE_CHANNELS_MAX        26
63 #define USE_PERIODS_MIN         2
64 #define USE_PERIODS_MAX         2
65 #endif
66
67 #if 0 /* ICE1712 emulation */
68 #define MAX_BUFFER_SIZE         (256 * 1024)
69 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S32_LE
70 #define USE_CHANNELS_MIN        10
71 #define USE_CHANNELS_MAX        10
72 #define USE_PERIODS_MIN         1
73 #define USE_PERIODS_MAX         1024
74 #endif
75
76 #if 0 /* UDA1341 emulation */
77 #define MAX_BUFFER_SIZE         (16380)
78 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE
79 #define USE_CHANNELS_MIN        2
80 #define USE_CHANNELS_MAX        2
81 #define USE_PERIODS_MIN         2
82 #define USE_PERIODS_MAX         255
83 #endif
84
85 #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
86 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE
87 #define USE_CHANNELS_MIN        2
88 #define USE_CHANNELS_MAX        2
89 #define USE_RATE                SNDRV_PCM_RATE_48000
90 #define USE_RATE_MIN            48000
91 #define USE_RATE_MAX            48000
92 #endif
93
94
95 /* defaults */
96 #ifndef MAX_BUFFER_SIZE
97 #define MAX_BUFFER_SIZE         (64*1024)
98 #endif
99 #ifndef USE_FORMATS
100 #define USE_FORMATS             (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
101 #endif
102 #ifndef USE_RATE
103 #define USE_RATE                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
104 #define USE_RATE_MIN            5500
105 #define USE_RATE_MAX            48000
106 #endif
107 #ifndef USE_CHANNELS_MIN
108 #define USE_CHANNELS_MIN        1
109 #endif
110 #ifndef USE_CHANNELS_MAX
111 #define USE_CHANNELS_MAX        2
112 #endif
113 #ifndef USE_PERIODS_MIN
114 #define USE_PERIODS_MIN         1
115 #endif
116 #ifndef USE_PERIODS_MAX
117 #define USE_PERIODS_MAX         1024
118 #endif
119 #ifndef add_playback_constraints
120 #define add_playback_constraints(x) 0
121 #endif
122 #ifndef add_capture_constraints
123 #define add_capture_constraints(x) 0
124 #endif
125
126 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
127 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
128 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
129 static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
130 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
131 //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
132 static int boot_devs;
133
134 module_param_array(index, int, boot_devs, 0444);
135 MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
136 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
137 module_param_array(id, charp, boot_devs, 0444);
138 MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
139 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
140 module_param_array(enable, bool, boot_devs, 0444);
141 MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
142 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
143 module_param_array(pcm_devs, int, boot_devs, 0444);
144 MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
145 MODULE_PARM_SYNTAX(pcm_devs, SNDRV_ENABLED ",allows:{{0,4}},default:1,dialog:list");
146 module_param_array(pcm_substreams, int, boot_devs, 0444);
147 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
148 MODULE_PARM_SYNTAX(pcm_substreams, SNDRV_ENABLED ",allows:{{1,16}},default:8,dialog:list");
149 //module_param_array(midi_devs, int, boot_devs, 0444);
150 //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
151 //MODULE_PARM_SYNTAX(midi_devs, SNDRV_ENABLED ",allows:{{0,2}},default:8,dialog:list");
152
153 #define MIXER_ADDR_MASTER       0
154 #define MIXER_ADDR_LINE         1
155 #define MIXER_ADDR_MIC          2
156 #define MIXER_ADDR_SYNTH        3
157 #define MIXER_ADDR_CD           4
158 #define MIXER_ADDR_LAST         4
159
160 typedef struct snd_card_dummy {
161         snd_card_t *card;
162         spinlock_t mixer_lock;
163         int mixer_volume[MIXER_ADDR_LAST+1][2];
164         int capture_source[MIXER_ADDR_LAST+1][2];
165 } snd_card_dummy_t;
166
167 typedef struct snd_card_dummy_pcm {
168         snd_card_dummy_t *dummy;
169         spinlock_t lock;
170         struct timer_list timer;
171         unsigned int pcm_size;
172         unsigned int pcm_count;
173         unsigned int pcm_bps;           /* bytes per second */
174         unsigned int pcm_jiffie;        /* bytes per one jiffie */
175         unsigned int pcm_irq_pos;       /* IRQ position */
176         unsigned int pcm_buf_pos;       /* position in buffer */
177         snd_pcm_substream_t *substream;
178 } snd_card_dummy_pcm_t;
179
180 static snd_card_t *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
181
182
183 static int snd_card_dummy_playback_ioctl(snd_pcm_substream_t * substream,
184                                          unsigned int cmd,
185                                          void *arg)
186 {
187         return snd_pcm_lib_ioctl(substream, cmd, arg);
188 }
189
190 static int snd_card_dummy_capture_ioctl(snd_pcm_substream_t * substream,
191                                         unsigned int cmd,
192                                         void *arg)
193 {
194         return snd_pcm_lib_ioctl(substream, cmd, arg);
195 }
196
197 static void snd_card_dummy_pcm_timer_start(snd_pcm_substream_t * substream)
198 {
199         snd_pcm_runtime_t *runtime = substream->runtime;
200         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return);
201
202         dpcm->timer.expires = 1 + jiffies;
203         add_timer(&dpcm->timer);
204 }
205
206 static void snd_card_dummy_pcm_timer_stop(snd_pcm_substream_t * substream)
207 {
208         snd_pcm_runtime_t *runtime = substream->runtime;
209         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return);
210
211         del_timer(&dpcm->timer);
212 }
213
214 static int snd_card_dummy_playback_trigger(snd_pcm_substream_t * substream,
215                                            int cmd)
216 {
217         if (cmd == SNDRV_PCM_TRIGGER_START) {
218                 snd_card_dummy_pcm_timer_start(substream);
219         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
220                 snd_card_dummy_pcm_timer_stop(substream);
221         } else {
222                 return -EINVAL;
223         }
224         return 0;
225 }
226
227 static int snd_card_dummy_capture_trigger(snd_pcm_substream_t * substream,
228                                           int cmd)
229 {
230         if (cmd == SNDRV_PCM_TRIGGER_START) {
231                 snd_card_dummy_pcm_timer_start(substream);
232         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
233                 snd_card_dummy_pcm_timer_stop(substream);
234         } else {
235                 return -EINVAL;
236         }
237         return 0;
238 }
239
240 static int snd_card_dummy_pcm_prepare(snd_pcm_substream_t * substream)
241 {
242         snd_pcm_runtime_t *runtime = substream->runtime;
243         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return -ENXIO);
244         unsigned int bps;
245
246         bps = runtime->rate * runtime->channels;
247         bps *= snd_pcm_format_width(runtime->format);
248         bps /= 8;
249         if (bps <= 0)
250                 return -EINVAL;
251         dpcm->pcm_bps = bps;
252         dpcm->pcm_jiffie = bps / HZ;
253         dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
254         dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
255         dpcm->pcm_irq_pos = 0;
256         dpcm->pcm_buf_pos = 0;
257         return 0;
258 }
259
260 static int snd_card_dummy_playback_prepare(snd_pcm_substream_t * substream)
261 {
262         return snd_card_dummy_pcm_prepare(substream);
263 }
264
265 static int snd_card_dummy_capture_prepare(snd_pcm_substream_t * substream)
266 {
267         return snd_card_dummy_pcm_prepare(substream);
268 }
269
270 static void snd_card_dummy_pcm_timer_function(unsigned long data)
271 {
272         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, (void *)data, return);
273         
274         dpcm->timer.expires = 1 + jiffies;
275         add_timer(&dpcm->timer);
276         spin_lock_irq(&dpcm->lock);
277         dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
278         dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
279         dpcm->pcm_buf_pos %= dpcm->pcm_size;
280         if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
281                 dpcm->pcm_irq_pos %= dpcm->pcm_count;
282                 snd_pcm_period_elapsed(dpcm->substream);
283         }
284         spin_unlock_irq(&dpcm->lock);   
285 }
286
287 static snd_pcm_uframes_t snd_card_dummy_playback_pointer(snd_pcm_substream_t * substream)
288 {
289         snd_pcm_runtime_t *runtime = substream->runtime;
290         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return -ENXIO);
291
292         return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
293 }
294
295 static snd_pcm_uframes_t snd_card_dummy_capture_pointer(snd_pcm_substream_t * substream)
296 {
297         snd_pcm_runtime_t *runtime = substream->runtime;
298         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return -ENXIO);
299
300         return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
301 }
302
303 static snd_pcm_hardware_t snd_card_dummy_playback =
304 {
305         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
306                                  SNDRV_PCM_INFO_MMAP_VALID),
307         .formats =              USE_FORMATS,
308         .rates =                USE_RATE,
309         .rate_min =             USE_RATE_MIN,
310         .rate_max =             USE_RATE_MAX,
311         .channels_min =         USE_CHANNELS_MIN,
312         .channels_max =         USE_CHANNELS_MAX,
313         .buffer_bytes_max =     MAX_BUFFER_SIZE,
314         .period_bytes_min =     64,
315         .period_bytes_max =     MAX_BUFFER_SIZE,
316         .periods_min =          USE_PERIODS_MIN,
317         .periods_max =          USE_PERIODS_MAX,
318         .fifo_size =            0,
319 };
320
321 static snd_pcm_hardware_t snd_card_dummy_capture =
322 {
323         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
324                                  SNDRV_PCM_INFO_MMAP_VALID),
325         .formats =              USE_FORMATS,
326         .rates =                USE_RATE,
327         .rate_min =             USE_RATE_MIN,
328         .rate_max =             USE_RATE_MAX,
329         .channels_min =         USE_CHANNELS_MIN,
330         .channels_max =         USE_CHANNELS_MAX,
331         .buffer_bytes_max =     MAX_BUFFER_SIZE,
332         .period_bytes_min =     64,
333         .period_bytes_max =     MAX_BUFFER_SIZE,
334         .periods_min =          USE_PERIODS_MIN,
335         .periods_max =          USE_PERIODS_MAX,
336         .fifo_size =            0,
337 };
338
339 static void snd_card_dummy_runtime_free(snd_pcm_runtime_t *runtime)
340 {
341         snd_card_dummy_pcm_t *dpcm = snd_magic_cast(snd_card_dummy_pcm_t, runtime->private_data, return);
342         snd_magic_kfree(dpcm);
343 }
344
345 static int snd_card_dummy_playback_open(snd_pcm_substream_t * substream)
346 {
347         snd_pcm_runtime_t *runtime = substream->runtime;
348         snd_card_dummy_pcm_t *dpcm;
349         int err;
350
351         dpcm = snd_magic_kcalloc(snd_card_dummy_pcm_t, 0, GFP_KERNEL);
352         if (dpcm == NULL)
353                 return -ENOMEM;
354         if ((runtime->dma_area = snd_malloc_pages_fallback(MAX_BUFFER_SIZE, GFP_KERNEL, &runtime->dma_bytes)) == NULL) {
355                 snd_magic_kfree(dpcm);
356                 return -ENOMEM;
357         }
358         init_timer(&dpcm->timer);
359         dpcm->timer.data = (unsigned long) dpcm;
360         dpcm->timer.function = snd_card_dummy_pcm_timer_function;
361         spin_lock_init(&dpcm->lock);
362         dpcm->substream = substream;
363         runtime->private_data = dpcm;
364         runtime->private_free = snd_card_dummy_runtime_free;
365         runtime->hw = snd_card_dummy_playback;
366         if (substream->pcm->device & 1) {
367                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
368                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
369         }
370         if (substream->pcm->device & 2)
371                 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
372         if ((err = add_playback_constraints(runtime)) < 0) {
373                 snd_magic_kfree(dpcm);
374                 return err;
375         }
376
377         return 0;
378 }
379
380 static int snd_card_dummy_capture_open(snd_pcm_substream_t * substream)
381 {
382         snd_pcm_runtime_t *runtime = substream->runtime;
383         snd_card_dummy_pcm_t *dpcm;
384         int err;
385
386         dpcm = snd_magic_kcalloc(snd_card_dummy_pcm_t, 0, GFP_KERNEL);
387         if (dpcm == NULL)
388                 return -ENOMEM;
389         if ((runtime->dma_area = snd_malloc_pages_fallback(MAX_BUFFER_SIZE, GFP_KERNEL, &runtime->dma_bytes)) == NULL) {
390                 snd_magic_kfree(dpcm);
391                 return -ENOMEM;
392         }
393         memset(runtime->dma_area, 0, runtime->dma_bytes);
394         init_timer(&dpcm->timer);
395         dpcm->timer.data = (unsigned long) dpcm;
396         dpcm->timer.function = snd_card_dummy_pcm_timer_function;
397         spin_lock_init(&dpcm->lock);
398         dpcm->substream = substream;
399         runtime->private_data = dpcm;
400         runtime->private_free = snd_card_dummy_runtime_free;
401         runtime->hw = snd_card_dummy_capture;
402         if (substream->pcm->device == 1) {
403                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
404                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
405         }
406         if (substream->pcm->device & 2)
407                 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
408         if ((err = add_capture_constraints(runtime)) < 0) {
409                 snd_magic_kfree(dpcm);
410                 return err;
411         }
412
413         return 0;
414 }
415
416 static int snd_card_dummy_playback_close(snd_pcm_substream_t * substream)
417 {
418         snd_pcm_runtime_t *runtime = substream->runtime;
419
420         snd_free_pages(runtime->dma_area, runtime->dma_bytes);
421         return 0;
422 }
423
424 static int snd_card_dummy_capture_close(snd_pcm_substream_t * substream)
425 {
426         snd_pcm_runtime_t *runtime = substream->runtime;
427
428         snd_free_pages(runtime->dma_area, runtime->dma_bytes);
429         return 0;
430 }
431
432 static snd_pcm_ops_t snd_card_dummy_playback_ops = {
433         .open =                 snd_card_dummy_playback_open,
434         .close =                snd_card_dummy_playback_close,
435         .ioctl =                snd_card_dummy_playback_ioctl,
436         .prepare =              snd_card_dummy_playback_prepare,
437         .trigger =              snd_card_dummy_playback_trigger,
438         .pointer =              snd_card_dummy_playback_pointer,
439 };
440
441 static snd_pcm_ops_t snd_card_dummy_capture_ops = {
442         .open =                 snd_card_dummy_capture_open,
443         .close =                snd_card_dummy_capture_close,
444         .ioctl =                snd_card_dummy_capture_ioctl,
445         .prepare =              snd_card_dummy_capture_prepare,
446         .trigger =              snd_card_dummy_capture_trigger,
447         .pointer =              snd_card_dummy_capture_pointer,
448 };
449
450 static int __init snd_card_dummy_pcm(snd_card_dummy_t *dummy, int device, int substreams)
451 {
452         snd_pcm_t *pcm;
453         int err;
454
455         if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device, substreams, substreams, &pcm)) < 0)
456                 return err;
457         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
458         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
459         pcm->private_data = dummy;
460         pcm->info_flags = 0;
461         strcpy(pcm->name, "Dummy PCM");
462         return 0;
463 }
464
465 #define DUMMY_VOLUME(xname, xindex, addr) \
466 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
467   .info = snd_dummy_volume_info, \
468   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
469   .private_value = addr }
470
471 static int snd_dummy_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
472 {
473         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
474         uinfo->count = 2;
475         uinfo->value.integer.min = -50;
476         uinfo->value.integer.max = 100;
477         return 0;
478 }
479  
480 static int snd_dummy_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
481 {
482         snd_card_dummy_t *dummy = _snd_kcontrol_chip(kcontrol);
483         unsigned long flags;
484         int addr = kcontrol->private_value;
485
486         spin_lock_irqsave(&dummy->mixer_lock, flags);
487         ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
488         ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
489         spin_unlock_irqrestore(&dummy->mixer_lock, flags);
490         return 0;
491 }                                                                                                                                                                                                                                                                                                            
492
493 static int snd_dummy_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
494 {
495         snd_card_dummy_t *dummy = _snd_kcontrol_chip(kcontrol);
496         unsigned long flags;
497         int change, addr = kcontrol->private_value;
498         int left, right;
499
500         left = ucontrol->value.integer.value[0];
501         if (left < -50)
502                 left = -50;
503         if (left > 100)
504                 left = 100;
505         right = ucontrol->value.integer.value[1];
506         if (right < -50)
507                 right = -50;
508         if (right > 100)
509                 right = 100;
510         spin_lock_irqsave(&dummy->mixer_lock, flags);
511         change = dummy->mixer_volume[addr][0] != left ||
512                  dummy->mixer_volume[addr][1] != right;
513         dummy->mixer_volume[addr][0] = left;
514         dummy->mixer_volume[addr][1] = right;
515         spin_unlock_irqrestore(&dummy->mixer_lock, flags);
516         return change;
517 }                                                                                                                                                                                                                                                                                                            
518
519 #define DUMMY_CAPSRC(xname, xindex, addr) \
520 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
521   .info = snd_dummy_capsrc_info, \
522   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
523   .private_value = addr }
524
525 static int snd_dummy_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
526 {
527         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
528         uinfo->count = 2;
529         uinfo->value.integer.min = 0;
530         uinfo->value.integer.max = 1;
531         return 0;
532 }
533  
534 static int snd_dummy_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
535 {
536         snd_card_dummy_t *dummy = _snd_kcontrol_chip(kcontrol);
537         unsigned long flags;
538         int addr = kcontrol->private_value;
539
540         spin_lock_irqsave(&dummy->mixer_lock, flags);
541         ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
542         ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
543         spin_unlock_irqrestore(&dummy->mixer_lock, flags);
544         return 0;
545 }
546
547 static int snd_dummy_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
548 {
549         snd_card_dummy_t *dummy = _snd_kcontrol_chip(kcontrol);
550         unsigned long flags;
551         int change, addr = kcontrol->private_value;
552         int left, right;
553
554         left = ucontrol->value.integer.value[0] & 1;
555         right = ucontrol->value.integer.value[1] & 1;
556         spin_lock_irqsave(&dummy->mixer_lock, flags);
557         change = dummy->capture_source[addr][0] != left &&
558                  dummy->capture_source[addr][1] != right;
559         dummy->capture_source[addr][0] = left;
560         dummy->capture_source[addr][1] = right;
561         spin_unlock_irqrestore(&dummy->mixer_lock, flags);
562         return change;
563 }                                                                                                                                                                                                                                                                                                            
564
565 #define DUMMY_CONTROLS (sizeof(snd_dummy_controls)/sizeof(snd_kcontrol_new_t))
566
567 static snd_kcontrol_new_t snd_dummy_controls[] = {
568 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
569 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
570 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
571 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
572 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
573 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
574 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
575 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
576 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
577 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
578 };
579
580 int __init snd_card_dummy_new_mixer(snd_card_dummy_t * dummy)
581 {
582         snd_card_t *card = dummy->card;
583         unsigned int idx;
584         int err;
585
586         snd_assert(dummy != NULL, return -EINVAL);
587         spin_lock_init(&dummy->mixer_lock);
588         strcpy(card->mixername, "Dummy Mixer");
589
590         for (idx = 0; idx < DUMMY_CONTROLS; idx++) {
591                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
592                         return err;
593         }
594         return 0;
595 }
596
597 static int __init snd_card_dummy_probe(int dev)
598 {
599         snd_card_t *card;
600         struct snd_card_dummy *dummy;
601         int idx, err;
602
603         if (!enable[dev])
604                 return -ENODEV;
605         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
606                             sizeof(struct snd_card_dummy));
607         if (card == NULL)
608                 return -ENOMEM;
609         dummy = (struct snd_card_dummy *)card->private_data;
610         dummy->card = card;
611         for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
612                 if (pcm_substreams[dev] < 1)
613                         pcm_substreams[dev] = 1;
614                 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
615                         pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
616                 if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
617                         goto __nodev;
618         }
619         if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
620                 goto __nodev;
621         strcpy(card->driver, "Dummy");
622         strcpy(card->shortname, "Dummy");
623         sprintf(card->longname, "Dummy %i", dev + 1);
624         if ((err = snd_card_register(card)) == 0) {
625                 snd_dummy_cards[dev] = card;
626                 return 0;
627         }
628       __nodev:
629         snd_card_free(card);
630         return err;
631 }
632
633 static int __init alsa_card_dummy_init(void)
634 {
635         int dev, cards;
636
637         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
638                 if (snd_card_dummy_probe(dev) < 0) {
639 #ifdef MODULE
640                         printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1);
641 #endif
642                         break;
643                 }
644                 cards++;
645         }
646         if (!cards) {
647 #ifdef MODULE
648                 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
649 #endif
650                 return -ENODEV;
651         }
652         return 0;
653 }
654
655 static void __exit alsa_card_dummy_exit(void)
656 {
657         int idx;
658
659         for (idx = 0; idx < SNDRV_CARDS; idx++)
660                 snd_card_free(snd_dummy_cards[idx]);
661 }
662
663 module_init(alsa_card_dummy_init)
664 module_exit(alsa_card_dummy_exit)