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