ALSA: dummy - Fake buffer allocations
[linux-2.6.git] / 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/hrtimer.h>
29 #include <linux/math64.h>
30 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/tlv.h>
34 #include <sound/pcm.h>
35 #include <sound/rawmidi.h>
36 #include <sound/initval.h>
37
38 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
39 MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
40 MODULE_LICENSE("GPL");
41 MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
42
43 #define MAX_PCM_DEVICES         4
44 #define MAX_PCM_SUBSTREAMS      16
45 #define MAX_MIDI_DEVICES        2
46
47 #if 0 /* emu10k1 emulation */
48 #define MAX_BUFFER_SIZE         (128 * 1024)
49 static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
50 {
51         int err;
52         err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
53         if (err < 0)
54                 return err;
55         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
56         if (err < 0)
57                 return err;
58         return 0;
59 }
60 #define add_playback_constraints emu10k1_playback_constraints
61 #endif
62
63 #if 0 /* RME9652 emulation */
64 #define MAX_BUFFER_SIZE         (26 * 64 * 1024)
65 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S32_LE
66 #define USE_CHANNELS_MIN        26
67 #define USE_CHANNELS_MAX        26
68 #define USE_PERIODS_MIN         2
69 #define USE_PERIODS_MAX         2
70 #endif
71
72 #if 0 /* ICE1712 emulation */
73 #define MAX_BUFFER_SIZE         (256 * 1024)
74 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S32_LE
75 #define USE_CHANNELS_MIN        10
76 #define USE_CHANNELS_MAX        10
77 #define USE_PERIODS_MIN         1
78 #define USE_PERIODS_MAX         1024
79 #endif
80
81 #if 0 /* UDA1341 emulation */
82 #define MAX_BUFFER_SIZE         (16380)
83 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE
84 #define USE_CHANNELS_MIN        2
85 #define USE_CHANNELS_MAX        2
86 #define USE_PERIODS_MIN         2
87 #define USE_PERIODS_MAX         255
88 #endif
89
90 #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
91 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE
92 #define USE_CHANNELS_MIN        2
93 #define USE_CHANNELS_MAX        2
94 #define USE_RATE                SNDRV_PCM_RATE_48000
95 #define USE_RATE_MIN            48000
96 #define USE_RATE_MAX            48000
97 #endif
98
99 #if 0 /* CA0106 */
100 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE
101 #define USE_CHANNELS_MIN        2
102 #define USE_CHANNELS_MAX        2
103 #define USE_RATE                (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000) 
104 #define USE_RATE_MIN            48000 
105 #define USE_RATE_MAX            192000
106 #define MAX_BUFFER_SIZE         ((65536-64)*8)
107 #define MAX_PERIOD_SIZE         (65536-64)
108 #define USE_PERIODS_MIN         2
109 #define USE_PERIODS_MAX         8
110 #endif
111
112
113 /* defaults */
114 #ifndef MAX_BUFFER_SIZE
115 #define MAX_BUFFER_SIZE         (64*1024)
116 #endif
117 #ifndef MAX_PERIOD_SIZE
118 #define MAX_PERIOD_SIZE         MAX_BUFFER_SIZE
119 #endif
120 #ifndef USE_FORMATS
121 #define USE_FORMATS             (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
122 #endif
123 #ifndef USE_RATE
124 #define USE_RATE                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
125 #define USE_RATE_MIN            5500
126 #define USE_RATE_MAX            48000
127 #endif
128 #ifndef USE_CHANNELS_MIN
129 #define USE_CHANNELS_MIN        1
130 #endif
131 #ifndef USE_CHANNELS_MAX
132 #define USE_CHANNELS_MAX        2
133 #endif
134 #ifndef USE_PERIODS_MIN
135 #define USE_PERIODS_MIN         1
136 #endif
137 #ifndef USE_PERIODS_MAX
138 #define USE_PERIODS_MAX         1024
139 #endif
140 #ifndef add_playback_constraints
141 #define add_playback_constraints(x) 0
142 #endif
143 #ifndef add_capture_constraints
144 #define add_capture_constraints(x) 0
145 #endif
146
147 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
148 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
149 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
150 static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
151 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
152 //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
153 #ifdef CONFIG_HIGH_RES_TIMERS
154 static int hrtimer = 1;
155 #endif
156 static int fake_buffer = 1;
157
158 module_param_array(index, int, NULL, 0444);
159 MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
160 module_param_array(id, charp, NULL, 0444);
161 MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
162 module_param_array(enable, bool, NULL, 0444);
163 MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
164 module_param_array(pcm_devs, int, NULL, 0444);
165 MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
166 module_param_array(pcm_substreams, int, NULL, 0444);
167 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
168 //module_param_array(midi_devs, int, NULL, 0444);
169 //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
170 module_param(fake_buffer, bool, 0444);
171 MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
172 #ifdef CONFIG_HIGH_RES_TIMERS
173 module_param(hrtimer, bool, 0644);
174 MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
175 #endif
176
177 static struct platform_device *devices[SNDRV_CARDS];
178
179 #define MIXER_ADDR_MASTER       0
180 #define MIXER_ADDR_LINE         1
181 #define MIXER_ADDR_MIC          2
182 #define MIXER_ADDR_SYNTH        3
183 #define MIXER_ADDR_CD           4
184 #define MIXER_ADDR_LAST         4
185
186 struct dummy_timer_ops {
187         int (*create)(struct snd_pcm_substream *);
188         void (*free)(struct snd_pcm_substream *);
189         int (*prepare)(struct snd_pcm_substream *);
190         int (*start)(struct snd_pcm_substream *);
191         int (*stop)(struct snd_pcm_substream *);
192         snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
193 };
194
195 struct snd_dummy {
196         struct snd_card *card;
197         struct snd_pcm *pcm;
198         spinlock_t mixer_lock;
199         int mixer_volume[MIXER_ADDR_LAST+1][2];
200         int capture_source[MIXER_ADDR_LAST+1][2];
201         const struct dummy_timer_ops *timer_ops;
202 };
203
204 /*
205  * system timer interface
206  */
207
208 struct dummy_systimer_pcm {
209         spinlock_t lock;
210         struct timer_list timer;
211         unsigned long base_time;
212         unsigned int frac_pos;  /* fractional sample position (based HZ) */
213         unsigned int frac_period_rest;
214         unsigned int frac_buffer_size;  /* buffer_size * HZ */
215         unsigned int frac_period_size;  /* period_size * HZ */
216         unsigned int rate;
217         int elapsed;
218         struct snd_pcm_substream *substream;
219 };
220
221 static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
222 {
223         dpcm->timer.expires = jiffies +
224                 (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
225         add_timer(&dpcm->timer);
226 }
227
228 static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
229 {
230         unsigned long delta;
231
232         delta = jiffies - dpcm->base_time;
233         if (!delta)
234                 return;
235         dpcm->base_time += delta;
236         delta *= dpcm->rate;
237         dpcm->frac_pos += delta;
238         while (dpcm->frac_pos >= dpcm->frac_buffer_size)
239                 dpcm->frac_pos -= dpcm->frac_buffer_size;
240         while (dpcm->frac_period_rest <= delta) {
241                 dpcm->elapsed++;
242                 dpcm->frac_period_rest += dpcm->frac_period_size;
243         }
244         dpcm->frac_period_rest -= delta;
245 }
246
247 static int dummy_systimer_start(struct snd_pcm_substream *substream)
248 {
249         struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
250         spin_lock(&dpcm->lock);
251         dpcm->base_time = jiffies;
252         dummy_systimer_rearm(dpcm);
253         spin_unlock(&dpcm->lock);
254         return 0;
255 }
256
257 static int dummy_systimer_stop(struct snd_pcm_substream *substream)
258 {
259         struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
260         spin_lock(&dpcm->lock);
261         del_timer(&dpcm->timer);
262         spin_unlock(&dpcm->lock);
263         return 0;
264 }
265
266 static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
267 {
268         struct snd_pcm_runtime *runtime = substream->runtime;
269         struct dummy_systimer_pcm *dpcm = runtime->private_data;
270
271         dpcm->frac_pos = 0;
272         dpcm->rate = runtime->rate;
273         dpcm->frac_buffer_size = runtime->buffer_size * HZ;
274         dpcm->frac_period_size = runtime->period_size * HZ;
275         dpcm->frac_period_rest = dpcm->frac_period_size;
276         dpcm->elapsed = 0;
277
278         return 0;
279 }
280
281 static void dummy_systimer_callback(unsigned long data)
282 {
283         struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
284         unsigned long flags;
285         int elapsed = 0;
286         
287         spin_lock_irqsave(&dpcm->lock, flags);
288         dummy_systimer_update(dpcm);
289         dummy_systimer_rearm(dpcm);
290         elapsed = dpcm->elapsed;
291         dpcm->elapsed = 0;
292         spin_unlock_irqrestore(&dpcm->lock, flags);
293         if (elapsed)
294                 snd_pcm_period_elapsed(dpcm->substream);
295 }
296
297 static snd_pcm_uframes_t
298 dummy_systimer_pointer(struct snd_pcm_substream *substream)
299 {
300         struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
301         snd_pcm_uframes_t pos;
302
303         spin_lock(&dpcm->lock);
304         dummy_systimer_update(dpcm);
305         pos = dpcm->frac_pos / HZ;
306         spin_unlock(&dpcm->lock);
307         return pos;
308 }
309
310 static int dummy_systimer_create(struct snd_pcm_substream *substream)
311 {
312         struct dummy_systimer_pcm *dpcm;
313
314         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
315         if (!dpcm)
316                 return -ENOMEM;
317         substream->runtime->private_data = dpcm;
318         init_timer(&dpcm->timer);
319         dpcm->timer.data = (unsigned long) dpcm;
320         dpcm->timer.function = dummy_systimer_callback;
321         spin_lock_init(&dpcm->lock);
322         dpcm->substream = substream;
323         return 0;
324 }
325
326 static void dummy_systimer_free(struct snd_pcm_substream *substream)
327 {
328         kfree(substream->runtime->private_data);
329 }
330
331 static struct dummy_timer_ops dummy_systimer_ops = {
332         .create =       dummy_systimer_create,
333         .free =         dummy_systimer_free,
334         .prepare =      dummy_systimer_prepare,
335         .start =        dummy_systimer_start,
336         .stop =         dummy_systimer_stop,
337         .pointer =      dummy_systimer_pointer,
338 };
339
340 #ifdef CONFIG_HIGH_RES_TIMERS
341 /*
342  * hrtimer interface
343  */
344
345 struct dummy_hrtimer_pcm {
346         ktime_t base_time;
347         ktime_t period_time;
348         atomic_t running;
349         struct hrtimer timer;
350         struct tasklet_struct tasklet;
351         struct snd_pcm_substream *substream;
352 };
353
354 static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
355 {
356         struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
357         if (atomic_read(&dpcm->running))
358                 snd_pcm_period_elapsed(dpcm->substream);
359 }
360
361 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
362 {
363         struct dummy_hrtimer_pcm *dpcm;
364
365         dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
366         if (!atomic_read(&dpcm->running))
367                 return HRTIMER_NORESTART;
368         tasklet_schedule(&dpcm->tasklet);
369         hrtimer_forward_now(timer, dpcm->period_time);
370         return HRTIMER_RESTART;
371 }
372
373 static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
374 {
375         struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
376
377         dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
378         hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
379         atomic_set(&dpcm->running, 1);
380         return 0;
381 }
382
383 static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
384 {
385         struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
386
387         atomic_set(&dpcm->running, 0);
388         hrtimer_cancel(&dpcm->timer);
389         return 0;
390 }
391
392 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
393 {
394         tasklet_kill(&dpcm->tasklet);
395 }
396
397 static snd_pcm_uframes_t
398 dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
399 {
400         struct snd_pcm_runtime *runtime = substream->runtime;
401         struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
402         u64 delta;
403         u32 pos;
404
405         delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
406                                dpcm->base_time);
407         delta = div_u64(delta * runtime->rate + 999999, 1000000);
408         div_u64_rem(delta, runtime->buffer_size, &pos);
409         return pos;
410 }
411
412 static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
413 {
414         struct snd_pcm_runtime *runtime = substream->runtime;
415         struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
416         unsigned int period, rate;
417         long sec;
418         unsigned long nsecs;
419
420         dummy_hrtimer_sync(dpcm);
421         period = runtime->period_size;
422         rate = runtime->rate;
423         sec = period / rate;
424         period %= rate;
425         nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
426         dpcm->period_time = ktime_set(sec, nsecs);
427
428         return 0;
429 }
430
431 static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
432 {
433         struct dummy_hrtimer_pcm *dpcm;
434
435         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
436         if (!dpcm)
437                 return -ENOMEM;
438         substream->runtime->private_data = dpcm;
439         hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
440         dpcm->timer.function = dummy_hrtimer_callback;
441         dpcm->substream = substream;
442         atomic_set(&dpcm->running, 0);
443         tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
444                      (unsigned long)dpcm);
445         return 0;
446 }
447
448 static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
449 {
450         struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
451         dummy_hrtimer_sync(dpcm);
452         kfree(dpcm);
453 }
454
455 static struct dummy_timer_ops dummy_hrtimer_ops = {
456         .create =       dummy_hrtimer_create,
457         .free =         dummy_hrtimer_free,
458         .prepare =      dummy_hrtimer_prepare,
459         .start =        dummy_hrtimer_start,
460         .stop =         dummy_hrtimer_stop,
461         .pointer =      dummy_hrtimer_pointer,
462 };
463
464 #endif /* CONFIG_HIGH_RES_TIMERS */
465
466 /*
467  * PCM interface
468  */
469
470 static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
471 {
472         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
473
474         switch (cmd) {
475         case SNDRV_PCM_TRIGGER_START:
476         case SNDRV_PCM_TRIGGER_RESUME:
477                 return dummy->timer_ops->start(substream);
478         case SNDRV_PCM_TRIGGER_STOP:
479         case SNDRV_PCM_TRIGGER_SUSPEND:
480                 return dummy->timer_ops->stop(substream);
481         }
482         return -EINVAL;
483 }
484
485 static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
486 {
487         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
488
489         return dummy->timer_ops->prepare(substream);
490 }
491
492 static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
493 {
494         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
495
496         return dummy->timer_ops->pointer(substream);
497 }
498
499 static struct snd_pcm_hardware dummy_pcm_hardware = {
500         .info =                 (SNDRV_PCM_INFO_MMAP |
501                                  SNDRV_PCM_INFO_INTERLEAVED |
502                                  SNDRV_PCM_INFO_RESUME |
503                                  SNDRV_PCM_INFO_MMAP_VALID),
504         .formats =              USE_FORMATS,
505         .rates =                USE_RATE,
506         .rate_min =             USE_RATE_MIN,
507         .rate_max =             USE_RATE_MAX,
508         .channels_min =         USE_CHANNELS_MIN,
509         .channels_max =         USE_CHANNELS_MAX,
510         .buffer_bytes_max =     MAX_BUFFER_SIZE,
511         .period_bytes_min =     64,
512         .period_bytes_max =     MAX_PERIOD_SIZE,
513         .periods_min =          USE_PERIODS_MIN,
514         .periods_max =          USE_PERIODS_MAX,
515         .fifo_size =            0,
516 };
517
518 static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
519                                struct snd_pcm_hw_params *hw_params)
520 {
521         if (fake_buffer) {
522                 /* runtime->dma_bytes has to be set manually to allow mmap */
523                 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
524                 return 0;
525         }
526         return snd_pcm_lib_malloc_pages(substream,
527                                         params_buffer_bytes(hw_params));
528 }
529
530 static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
531 {
532         if (fake_buffer)
533                 return 0;
534         return snd_pcm_lib_free_pages(substream);
535 }
536
537 static int dummy_pcm_open(struct snd_pcm_substream *substream)
538 {
539         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
540         struct snd_pcm_runtime *runtime = substream->runtime;
541         int err;
542
543         dummy->timer_ops = &dummy_systimer_ops;
544 #ifdef CONFIG_HIGH_RES_TIMERS
545         if (hrtimer)
546                 dummy->timer_ops = &dummy_hrtimer_ops;
547 #endif
548
549         err = dummy->timer_ops->create(substream);
550         if (err < 0)
551                 return err;
552
553         runtime->hw = dummy_pcm_hardware;
554         if (substream->pcm->device & 1) {
555                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
556                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
557         }
558         if (substream->pcm->device & 2)
559                 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
560                                       SNDRV_PCM_INFO_MMAP_VALID);
561
562         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
563                 err = add_playback_constraints(substream->runtime);
564         else
565                 err = add_capture_constraints(substream->runtime);
566         if (err < 0) {
567                 dummy->timer_ops->free(substream);
568                 return err;
569         }
570         return 0;
571 }
572
573 static int dummy_pcm_close(struct snd_pcm_substream *substream)
574 {
575         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
576         dummy->timer_ops->free(substream);
577         return 0;
578 }
579
580 /*
581  * dummy buffer handling
582  */
583
584 static void *dummy_page[2];
585
586 static void free_fake_buffer(void)
587 {
588         if (fake_buffer) {
589                 int i;
590                 for (i = 0; i < 2; i++)
591                         if (dummy_page[i]) {
592                                 free_page((unsigned long)dummy_page[i]);
593                                 dummy_page[i] = NULL;
594                         }
595         }
596 }
597
598 static int alloc_fake_buffer(void)
599 {
600         int i;
601
602         if (!fake_buffer)
603                 return 0;
604         for (i = 0; i < 2; i++) {
605                 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
606                 if (!dummy_page[i]) {
607                         free_fake_buffer();
608                         return -ENOMEM;
609                 }
610         }
611         return 0;
612 }
613
614 static int dummy_pcm_copy(struct snd_pcm_substream *substream,
615                           int channel, snd_pcm_uframes_t pos,
616                           void __user *dst, snd_pcm_uframes_t count)
617 {
618         return 0; /* do nothing */
619 }
620
621 static int dummy_pcm_silence(struct snd_pcm_substream *substream,
622                              int channel, snd_pcm_uframes_t pos,
623                              snd_pcm_uframes_t count)
624 {
625         return 0; /* do nothing */
626 }
627
628 static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
629                                    unsigned long offset)
630 {
631         return virt_to_page(dummy_page[substream->stream]); /* the same page */
632 }
633
634 static struct snd_pcm_ops dummy_pcm_ops = {
635         .open =         dummy_pcm_open,
636         .close =        dummy_pcm_close,
637         .ioctl =        snd_pcm_lib_ioctl,
638         .hw_params =    dummy_pcm_hw_params,
639         .hw_free =      dummy_pcm_hw_free,
640         .prepare =      dummy_pcm_prepare,
641         .trigger =      dummy_pcm_trigger,
642         .pointer =      dummy_pcm_pointer,
643 };
644
645 static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
646         .open =         dummy_pcm_open,
647         .close =        dummy_pcm_close,
648         .ioctl =        snd_pcm_lib_ioctl,
649         .hw_params =    dummy_pcm_hw_params,
650         .hw_free =      dummy_pcm_hw_free,
651         .prepare =      dummy_pcm_prepare,
652         .trigger =      dummy_pcm_trigger,
653         .pointer =      dummy_pcm_pointer,
654         .copy =         dummy_pcm_copy,
655         .silence =      dummy_pcm_silence,
656         .page =         dummy_pcm_page,
657 };
658
659 static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
660                                         int substreams)
661 {
662         struct snd_pcm *pcm;
663         struct snd_pcm_ops *ops;
664         int err;
665
666         err = snd_pcm_new(dummy->card, "Dummy PCM", device,
667                                substreams, substreams, &pcm);
668         if (err < 0)
669                 return err;
670         dummy->pcm = pcm;
671         if (fake_buffer)
672                 ops = &dummy_pcm_ops_no_buf;
673         else
674                 ops = &dummy_pcm_ops;
675         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
676         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
677         pcm->private_data = dummy;
678         pcm->info_flags = 0;
679         strcpy(pcm->name, "Dummy PCM");
680         if (!fake_buffer) {
681                 snd_pcm_lib_preallocate_pages_for_all(pcm,
682                         SNDRV_DMA_TYPE_CONTINUOUS,
683                         snd_dma_continuous_data(GFP_KERNEL),
684                         0, 64*1024);
685         }
686         return 0;
687 }
688
689 #define DUMMY_VOLUME(xname, xindex, addr) \
690 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
691   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
692   .name = xname, .index = xindex, \
693   .info = snd_dummy_volume_info, \
694   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
695   .private_value = addr, \
696   .tlv = { .p = db_scale_dummy } }
697
698 static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
699                                  struct snd_ctl_elem_info *uinfo)
700 {
701         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
702         uinfo->count = 2;
703         uinfo->value.integer.min = -50;
704         uinfo->value.integer.max = 100;
705         return 0;
706 }
707  
708 static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
709                                 struct snd_ctl_elem_value *ucontrol)
710 {
711         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
712         int addr = kcontrol->private_value;
713
714         spin_lock_irq(&dummy->mixer_lock);
715         ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
716         ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
717         spin_unlock_irq(&dummy->mixer_lock);
718         return 0;
719 }
720
721 static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
722                                 struct snd_ctl_elem_value *ucontrol)
723 {
724         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
725         int change, addr = kcontrol->private_value;
726         int left, right;
727
728         left = ucontrol->value.integer.value[0];
729         if (left < -50)
730                 left = -50;
731         if (left > 100)
732                 left = 100;
733         right = ucontrol->value.integer.value[1];
734         if (right < -50)
735                 right = -50;
736         if (right > 100)
737                 right = 100;
738         spin_lock_irq(&dummy->mixer_lock);
739         change = dummy->mixer_volume[addr][0] != left ||
740                  dummy->mixer_volume[addr][1] != right;
741         dummy->mixer_volume[addr][0] = left;
742         dummy->mixer_volume[addr][1] = right;
743         spin_unlock_irq(&dummy->mixer_lock);
744         return change;
745 }
746
747 static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
748
749 #define DUMMY_CAPSRC(xname, xindex, addr) \
750 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
751   .info = snd_dummy_capsrc_info, \
752   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
753   .private_value = addr }
754
755 #define snd_dummy_capsrc_info   snd_ctl_boolean_stereo_info
756  
757 static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
758                                 struct snd_ctl_elem_value *ucontrol)
759 {
760         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
761         int addr = kcontrol->private_value;
762
763         spin_lock_irq(&dummy->mixer_lock);
764         ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
765         ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
766         spin_unlock_irq(&dummy->mixer_lock);
767         return 0;
768 }
769
770 static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
771 {
772         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
773         int change, addr = kcontrol->private_value;
774         int left, right;
775
776         left = ucontrol->value.integer.value[0] & 1;
777         right = ucontrol->value.integer.value[1] & 1;
778         spin_lock_irq(&dummy->mixer_lock);
779         change = dummy->capture_source[addr][0] != left &&
780                  dummy->capture_source[addr][1] != right;
781         dummy->capture_source[addr][0] = left;
782         dummy->capture_source[addr][1] = right;
783         spin_unlock_irq(&dummy->mixer_lock);
784         return change;
785 }
786
787 static struct snd_kcontrol_new snd_dummy_controls[] = {
788 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
789 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
790 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
791 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
792 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
793 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
794 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
795 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
796 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
797 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
798 };
799
800 static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
801 {
802         struct snd_card *card = dummy->card;
803         unsigned int idx;
804         int err;
805
806         if (snd_BUG_ON(!dummy))
807                 return -EINVAL;
808         spin_lock_init(&dummy->mixer_lock);
809         strcpy(card->mixername, "Dummy Mixer");
810
811         for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
812                 err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
813                 if (err < 0)
814                         return err;
815         }
816         return 0;
817 }
818
819 static int __devinit snd_dummy_probe(struct platform_device *devptr)
820 {
821         struct snd_card *card;
822         struct snd_dummy *dummy;
823         int idx, err;
824         int dev = devptr->id;
825
826         err = snd_card_create(index[dev], id[dev], THIS_MODULE,
827                               sizeof(struct snd_dummy), &card);
828         if (err < 0)
829                 return err;
830         dummy = card->private_data;
831         dummy->card = card;
832         for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
833                 if (pcm_substreams[dev] < 1)
834                         pcm_substreams[dev] = 1;
835                 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
836                         pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
837                 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
838                 if (err < 0)
839                         goto __nodev;
840         }
841         err = snd_card_dummy_new_mixer(dummy);
842         if (err < 0)
843                 goto __nodev;
844         strcpy(card->driver, "Dummy");
845         strcpy(card->shortname, "Dummy");
846         sprintf(card->longname, "Dummy %i", dev + 1);
847
848         snd_card_set_dev(card, &devptr->dev);
849
850         err = snd_card_register(card);
851         if (err == 0) {
852                 platform_set_drvdata(devptr, card);
853                 return 0;
854         }
855       __nodev:
856         snd_card_free(card);
857         return err;
858 }
859
860 static int __devexit snd_dummy_remove(struct platform_device *devptr)
861 {
862         snd_card_free(platform_get_drvdata(devptr));
863         platform_set_drvdata(devptr, NULL);
864         return 0;
865 }
866
867 #ifdef CONFIG_PM
868 static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
869 {
870         struct snd_card *card = platform_get_drvdata(pdev);
871         struct snd_dummy *dummy = card->private_data;
872
873         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
874         snd_pcm_suspend_all(dummy->pcm);
875         return 0;
876 }
877         
878 static int snd_dummy_resume(struct platform_device *pdev)
879 {
880         struct snd_card *card = platform_get_drvdata(pdev);
881
882         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
883         return 0;
884 }
885 #endif
886
887 #define SND_DUMMY_DRIVER        "snd_dummy"
888
889 static struct platform_driver snd_dummy_driver = {
890         .probe          = snd_dummy_probe,
891         .remove         = __devexit_p(snd_dummy_remove),
892 #ifdef CONFIG_PM
893         .suspend        = snd_dummy_suspend,
894         .resume         = snd_dummy_resume,
895 #endif
896         .driver         = {
897                 .name   = SND_DUMMY_DRIVER
898         },
899 };
900
901 static void snd_dummy_unregister_all(void)
902 {
903         int i;
904
905         for (i = 0; i < ARRAY_SIZE(devices); ++i)
906                 platform_device_unregister(devices[i]);
907         platform_driver_unregister(&snd_dummy_driver);
908         free_fake_buffer();
909 }
910
911 static int __init alsa_card_dummy_init(void)
912 {
913         int i, cards, err;
914
915         err = platform_driver_register(&snd_dummy_driver);
916         if (err < 0)
917                 return err;
918
919         err = alloc_fake_buffer();
920         if (err < 0) {
921                 platform_driver_unregister(&snd_dummy_driver);
922                 return err;
923         }
924
925         cards = 0;
926         for (i = 0; i < SNDRV_CARDS; i++) {
927                 struct platform_device *device;
928                 if (! enable[i])
929                         continue;
930                 device = platform_device_register_simple(SND_DUMMY_DRIVER,
931                                                          i, NULL, 0);
932                 if (IS_ERR(device))
933                         continue;
934                 if (!platform_get_drvdata(device)) {
935                         platform_device_unregister(device);
936                         continue;
937                 }
938                 devices[i] = device;
939                 cards++;
940         }
941         if (!cards) {
942 #ifdef MODULE
943                 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
944 #endif
945                 snd_dummy_unregister_all();
946                 return -ENODEV;
947         }
948         return 0;
949 }
950
951 static void __exit alsa_card_dummy_exit(void)
952 {
953         snd_dummy_unregister_all();
954 }
955
956 module_init(alsa_card_dummy_init)
957 module_exit(alsa_card_dummy_exit)