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