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