ALSA: dummy: Fix a use-after-free at closing
[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         hrtimer_cancel(&dpcm->timer);
423         tasklet_kill(&dpcm->tasklet);
424 }
425
426 static snd_pcm_uframes_t
427 dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
428 {
429         struct snd_pcm_runtime *runtime = substream->runtime;
430         struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
431         u64 delta;
432         u32 pos;
433
434         delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
435                                dpcm->base_time);
436         delta = div_u64(delta * runtime->rate + 999999, 1000000);
437         div_u64_rem(delta, runtime->buffer_size, &pos);
438         return pos;
439 }
440
441 static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
442 {
443         struct snd_pcm_runtime *runtime = substream->runtime;
444         struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
445         unsigned int period, rate;
446         long sec;
447         unsigned long nsecs;
448
449         dummy_hrtimer_sync(dpcm);
450         period = runtime->period_size;
451         rate = runtime->rate;
452         sec = period / rate;
453         period %= rate;
454         nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
455         dpcm->period_time = ktime_set(sec, nsecs);
456
457         return 0;
458 }
459
460 static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
461 {
462         struct dummy_hrtimer_pcm *dpcm;
463
464         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
465         if (!dpcm)
466                 return -ENOMEM;
467         substream->runtime->private_data = dpcm;
468         hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
469         dpcm->timer.function = dummy_hrtimer_callback;
470         dpcm->substream = substream;
471         atomic_set(&dpcm->running, 0);
472         tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
473                      (unsigned long)dpcm);
474         return 0;
475 }
476
477 static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
478 {
479         struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
480         dummy_hrtimer_sync(dpcm);
481         kfree(dpcm);
482 }
483
484 static struct dummy_timer_ops dummy_hrtimer_ops = {
485         .create =       dummy_hrtimer_create,
486         .free =         dummy_hrtimer_free,
487         .prepare =      dummy_hrtimer_prepare,
488         .start =        dummy_hrtimer_start,
489         .stop =         dummy_hrtimer_stop,
490         .pointer =      dummy_hrtimer_pointer,
491 };
492
493 #endif /* CONFIG_HIGH_RES_TIMERS */
494
495 /*
496  * PCM interface
497  */
498
499 static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
500 {
501         switch (cmd) {
502         case SNDRV_PCM_TRIGGER_START:
503         case SNDRV_PCM_TRIGGER_RESUME:
504                 return get_dummy_ops(substream)->start(substream);
505         case SNDRV_PCM_TRIGGER_STOP:
506         case SNDRV_PCM_TRIGGER_SUSPEND:
507                 return get_dummy_ops(substream)->stop(substream);
508         }
509         return -EINVAL;
510 }
511
512 static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
513 {
514         return get_dummy_ops(substream)->prepare(substream);
515 }
516
517 static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
518 {
519         return get_dummy_ops(substream)->pointer(substream);
520 }
521
522 static struct snd_pcm_hardware dummy_pcm_hardware = {
523         .info =                 (SNDRV_PCM_INFO_MMAP |
524                                  SNDRV_PCM_INFO_INTERLEAVED |
525                                  SNDRV_PCM_INFO_RESUME |
526                                  SNDRV_PCM_INFO_MMAP_VALID),
527         .formats =              USE_FORMATS,
528         .rates =                USE_RATE,
529         .rate_min =             USE_RATE_MIN,
530         .rate_max =             USE_RATE_MAX,
531         .channels_min =         USE_CHANNELS_MIN,
532         .channels_max =         USE_CHANNELS_MAX,
533         .buffer_bytes_max =     MAX_BUFFER_SIZE,
534         .period_bytes_min =     MIN_PERIOD_SIZE,
535         .period_bytes_max =     MAX_PERIOD_SIZE,
536         .periods_min =          USE_PERIODS_MIN,
537         .periods_max =          USE_PERIODS_MAX,
538         .fifo_size =            0,
539 };
540
541 static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
542                                struct snd_pcm_hw_params *hw_params)
543 {
544         if (fake_buffer) {
545                 /* runtime->dma_bytes has to be set manually to allow mmap */
546                 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
547                 return 0;
548         }
549         return snd_pcm_lib_malloc_pages(substream,
550                                         params_buffer_bytes(hw_params));
551 }
552
553 static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
554 {
555         if (fake_buffer)
556                 return 0;
557         return snd_pcm_lib_free_pages(substream);
558 }
559
560 static int dummy_pcm_open(struct snd_pcm_substream *substream)
561 {
562         struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
563         struct dummy_model *model = dummy->model;
564         struct snd_pcm_runtime *runtime = substream->runtime;
565         const struct dummy_timer_ops *ops;
566         int err;
567
568         ops = &dummy_systimer_ops;
569 #ifdef CONFIG_HIGH_RES_TIMERS
570         if (hrtimer)
571                 ops = &dummy_hrtimer_ops;
572 #endif
573
574         err = ops->create(substream);
575         if (err < 0)
576                 return err;
577         get_dummy_ops(substream) = ops;
578
579         runtime->hw = dummy->pcm_hw;
580         if (substream->pcm->device & 1) {
581                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
582                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
583         }
584         if (substream->pcm->device & 2)
585                 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
586                                       SNDRV_PCM_INFO_MMAP_VALID);
587
588         if (model == NULL)
589                 return 0;
590
591         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
592                 if (model->playback_constraints)
593                         err = model->playback_constraints(substream->runtime);
594         } else {
595                 if (model->capture_constraints)
596                         err = model->capture_constraints(substream->runtime);
597         }
598         if (err < 0) {
599                 get_dummy_ops(substream)->free(substream);
600                 return err;
601         }
602         return 0;
603 }
604
605 static int dummy_pcm_close(struct snd_pcm_substream *substream)
606 {
607         get_dummy_ops(substream)->free(substream);
608         return 0;
609 }
610
611 /*
612  * dummy buffer handling
613  */
614
615 static void *dummy_page[2];
616
617 static void free_fake_buffer(void)
618 {
619         if (fake_buffer) {
620                 int i;
621                 for (i = 0; i < 2; i++)
622                         if (dummy_page[i]) {
623                                 free_page((unsigned long)dummy_page[i]);
624                                 dummy_page[i] = NULL;
625                         }
626         }
627 }
628
629 static int alloc_fake_buffer(void)
630 {
631         int i;
632
633         if (!fake_buffer)
634                 return 0;
635         for (i = 0; i < 2; i++) {
636                 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
637                 if (!dummy_page[i]) {
638                         free_fake_buffer();
639                         return -ENOMEM;
640                 }
641         }
642         return 0;
643 }
644
645 static int dummy_pcm_copy(struct snd_pcm_substream *substream,
646                           int channel, snd_pcm_uframes_t pos,
647                           void __user *dst, snd_pcm_uframes_t count)
648 {
649         return 0; /* do nothing */
650 }
651
652 static int dummy_pcm_silence(struct snd_pcm_substream *substream,
653                              int channel, snd_pcm_uframes_t pos,
654                              snd_pcm_uframes_t count)
655 {
656         return 0; /* do nothing */
657 }
658
659 static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
660                                    unsigned long offset)
661 {
662         return virt_to_page(dummy_page[substream->stream]); /* the same page */
663 }
664
665 static struct snd_pcm_ops dummy_pcm_ops = {
666         .open =         dummy_pcm_open,
667         .close =        dummy_pcm_close,
668         .ioctl =        snd_pcm_lib_ioctl,
669         .hw_params =    dummy_pcm_hw_params,
670         .hw_free =      dummy_pcm_hw_free,
671         .prepare =      dummy_pcm_prepare,
672         .trigger =      dummy_pcm_trigger,
673         .pointer =      dummy_pcm_pointer,
674 };
675
676 static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
677         .open =         dummy_pcm_open,
678         .close =        dummy_pcm_close,
679         .ioctl =        snd_pcm_lib_ioctl,
680         .hw_params =    dummy_pcm_hw_params,
681         .hw_free =      dummy_pcm_hw_free,
682         .prepare =      dummy_pcm_prepare,
683         .trigger =      dummy_pcm_trigger,
684         .pointer =      dummy_pcm_pointer,
685         .copy =         dummy_pcm_copy,
686         .silence =      dummy_pcm_silence,
687         .page =         dummy_pcm_page,
688 };
689
690 static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
691                                         int substreams)
692 {
693         struct snd_pcm *pcm;
694         struct snd_pcm_ops *ops;
695         int err;
696
697         err = snd_pcm_new(dummy->card, "Dummy PCM", device,
698                                substreams, substreams, &pcm);
699         if (err < 0)
700                 return err;
701         dummy->pcm = pcm;
702         if (fake_buffer)
703                 ops = &dummy_pcm_ops_no_buf;
704         else
705                 ops = &dummy_pcm_ops;
706         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
707         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
708         pcm->private_data = dummy;
709         pcm->info_flags = 0;
710         strcpy(pcm->name, "Dummy PCM");
711         if (!fake_buffer) {
712                 snd_pcm_lib_preallocate_pages_for_all(pcm,
713                         SNDRV_DMA_TYPE_CONTINUOUS,
714                         snd_dma_continuous_data(GFP_KERNEL),
715                         0, 64*1024);
716         }
717         return 0;
718 }
719
720 /*
721  * mixer interface
722  */
723
724 #define DUMMY_VOLUME(xname, xindex, addr) \
725 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
726   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
727   .name = xname, .index = xindex, \
728   .info = snd_dummy_volume_info, \
729   .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
730   .private_value = addr, \
731   .tlv = { .p = db_scale_dummy } }
732
733 static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
734                                  struct snd_ctl_elem_info *uinfo)
735 {
736         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
737         uinfo->count = 2;
738         uinfo->value.integer.min = -50;
739         uinfo->value.integer.max = 100;
740         return 0;
741 }
742  
743 static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
744                                 struct snd_ctl_elem_value *ucontrol)
745 {
746         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
747         int addr = kcontrol->private_value;
748
749         spin_lock_irq(&dummy->mixer_lock);
750         ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
751         ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
752         spin_unlock_irq(&dummy->mixer_lock);
753         return 0;
754 }
755
756 static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
757                                 struct snd_ctl_elem_value *ucontrol)
758 {
759         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
760         int change, addr = kcontrol->private_value;
761         int left, right;
762
763         left = ucontrol->value.integer.value[0];
764         if (left < -50)
765                 left = -50;
766         if (left > 100)
767                 left = 100;
768         right = ucontrol->value.integer.value[1];
769         if (right < -50)
770                 right = -50;
771         if (right > 100)
772                 right = 100;
773         spin_lock_irq(&dummy->mixer_lock);
774         change = dummy->mixer_volume[addr][0] != left ||
775                  dummy->mixer_volume[addr][1] != right;
776         dummy->mixer_volume[addr][0] = left;
777         dummy->mixer_volume[addr][1] = right;
778         spin_unlock_irq(&dummy->mixer_lock);
779         return change;
780 }
781
782 static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
783
784 #define DUMMY_CAPSRC(xname, xindex, addr) \
785 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
786   .info = snd_dummy_capsrc_info, \
787   .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
788   .private_value = addr }
789
790 #define snd_dummy_capsrc_info   snd_ctl_boolean_stereo_info
791  
792 static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
793                                 struct snd_ctl_elem_value *ucontrol)
794 {
795         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
796         int addr = kcontrol->private_value;
797
798         spin_lock_irq(&dummy->mixer_lock);
799         ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
800         ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
801         spin_unlock_irq(&dummy->mixer_lock);
802         return 0;
803 }
804
805 static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
806 {
807         struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
808         int change, addr = kcontrol->private_value;
809         int left, right;
810
811         left = ucontrol->value.integer.value[0] & 1;
812         right = ucontrol->value.integer.value[1] & 1;
813         spin_lock_irq(&dummy->mixer_lock);
814         change = dummy->capture_source[addr][0] != left &&
815                  dummy->capture_source[addr][1] != right;
816         dummy->capture_source[addr][0] = left;
817         dummy->capture_source[addr][1] = right;
818         spin_unlock_irq(&dummy->mixer_lock);
819         return change;
820 }
821
822 static struct snd_kcontrol_new snd_dummy_controls[] = {
823 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
824 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
825 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
826 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
827 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
828 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
829 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
830 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
831 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
832 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
833 };
834
835 static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
836 {
837         struct snd_card *card = dummy->card;
838         unsigned int idx;
839         int err;
840
841         spin_lock_init(&dummy->mixer_lock);
842         strcpy(card->mixername, "Dummy Mixer");
843
844         for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
845                 err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
846                 if (err < 0)
847                         return err;
848         }
849         return 0;
850 }
851
852 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS)
853 /*
854  * proc interface
855  */
856 static void print_formats(struct snd_dummy *dummy,
857                           struct snd_info_buffer *buffer)
858 {
859         int i;
860
861         for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
862                 if (dummy->pcm_hw.formats & (1ULL << i))
863                         snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
864         }
865 }
866
867 static void print_rates(struct snd_dummy *dummy,
868                         struct snd_info_buffer *buffer)
869 {
870         static int rates[] = {
871                 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
872                 64000, 88200, 96000, 176400, 192000,
873         };
874         int i;
875
876         if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
877                 snd_iprintf(buffer, " continuous");
878         if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
879                 snd_iprintf(buffer, " knot");
880         for (i = 0; i < ARRAY_SIZE(rates); i++)
881                 if (dummy->pcm_hw.rates & (1 << i))
882                         snd_iprintf(buffer, " %d", rates[i]);
883 }
884
885 #define get_dummy_int_ptr(dummy, ofs) \
886         (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
887 #define get_dummy_ll_ptr(dummy, ofs) \
888         (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
889
890 struct dummy_hw_field {
891         const char *name;
892         const char *format;
893         unsigned int offset;
894         unsigned int size;
895 };
896 #define FIELD_ENTRY(item, fmt) {                   \
897         .name = #item,                             \
898         .format = fmt,                             \
899         .offset = offsetof(struct snd_pcm_hardware, item), \
900         .size = sizeof(dummy_pcm_hardware.item) }
901
902 static struct dummy_hw_field fields[] = {
903         FIELD_ENTRY(formats, "%#llx"),
904         FIELD_ENTRY(rates, "%#x"),
905         FIELD_ENTRY(rate_min, "%d"),
906         FIELD_ENTRY(rate_max, "%d"),
907         FIELD_ENTRY(channels_min, "%d"),
908         FIELD_ENTRY(channels_max, "%d"),
909         FIELD_ENTRY(buffer_bytes_max, "%ld"),
910         FIELD_ENTRY(period_bytes_min, "%ld"),
911         FIELD_ENTRY(period_bytes_max, "%ld"),
912         FIELD_ENTRY(periods_min, "%d"),
913         FIELD_ENTRY(periods_max, "%d"),
914 };
915
916 static void dummy_proc_read(struct snd_info_entry *entry,
917                             struct snd_info_buffer *buffer)
918 {
919         struct snd_dummy *dummy = entry->private_data;
920         int i;
921
922         for (i = 0; i < ARRAY_SIZE(fields); i++) {
923                 snd_iprintf(buffer, "%s ", fields[i].name);
924                 if (fields[i].size == sizeof(int))
925                         snd_iprintf(buffer, fields[i].format,
926                                 *get_dummy_int_ptr(dummy, fields[i].offset));
927                 else
928                         snd_iprintf(buffer, fields[i].format,
929                                 *get_dummy_ll_ptr(dummy, fields[i].offset));
930                 if (!strcmp(fields[i].name, "formats"))
931                         print_formats(dummy, buffer);
932                 else if (!strcmp(fields[i].name, "rates"))
933                         print_rates(dummy, buffer);
934                 snd_iprintf(buffer, "\n");
935         }
936 }
937
938 static void dummy_proc_write(struct snd_info_entry *entry,
939                              struct snd_info_buffer *buffer)
940 {
941         struct snd_dummy *dummy = entry->private_data;
942         char line[64];
943
944         while (!snd_info_get_line(buffer, line, sizeof(line))) {
945                 char item[20];
946                 const char *ptr;
947                 unsigned long long val;
948                 int i;
949
950                 ptr = snd_info_get_str(item, line, sizeof(item));
951                 for (i = 0; i < ARRAY_SIZE(fields); i++) {
952                         if (!strcmp(item, fields[i].name))
953                                 break;
954                 }
955                 if (i >= ARRAY_SIZE(fields))
956                         continue;
957                 snd_info_get_str(item, ptr, sizeof(item));
958                 if (strict_strtoull(item, 0, &val))
959                         continue;
960                 if (fields[i].size == sizeof(int))
961                         *get_dummy_int_ptr(dummy, fields[i].offset) = val;
962                 else
963                         *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
964         }
965 }
966
967 static void __devinit dummy_proc_init(struct snd_dummy *chip)
968 {
969         struct snd_info_entry *entry;
970
971         if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
972                 snd_info_set_text_ops(entry, chip, dummy_proc_read);
973                 entry->c.text.write = dummy_proc_write;
974                 entry->mode |= S_IWUSR;
975                 entry->private_data = chip;
976         }
977 }
978 #else
979 #define dummy_proc_init(x)
980 #endif /* CONFIG_SND_DEBUG && CONFIG_PROC_FS */
981
982 static int __devinit snd_dummy_probe(struct platform_device *devptr)
983 {
984         struct snd_card *card;
985         struct snd_dummy *dummy;
986         struct dummy_model *m = NULL, **mdl;
987         int idx, err;
988         int dev = devptr->id;
989
990         err = snd_card_create(index[dev], id[dev], THIS_MODULE,
991                               sizeof(struct snd_dummy), &card);
992         if (err < 0)
993                 return err;
994         dummy = card->private_data;
995         dummy->card = card;
996         for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
997                 if (strcmp(model[dev], (*mdl)->name) == 0) {
998                         printk(KERN_INFO
999                                 "snd-dummy: Using model '%s' for card %i\n",
1000                                 (*mdl)->name, card->number);
1001                         m = dummy->model = *mdl;
1002                         break;
1003                 }
1004         }
1005         for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
1006                 if (pcm_substreams[dev] < 1)
1007                         pcm_substreams[dev] = 1;
1008                 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1009                         pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1010                 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
1011                 if (err < 0)
1012                         goto __nodev;
1013         }
1014
1015         dummy->pcm_hw = dummy_pcm_hardware;
1016         if (m) {
1017                 if (m->formats)
1018                         dummy->pcm_hw.formats = m->formats;
1019                 if (m->buffer_bytes_max)
1020                         dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1021                 if (m->period_bytes_min)
1022                         dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1023                 if (m->period_bytes_max)
1024                         dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1025                 if (m->periods_min)
1026                         dummy->pcm_hw.periods_min = m->periods_min;
1027                 if (m->periods_max)
1028                         dummy->pcm_hw.periods_max = m->periods_max;
1029                 if (m->rates)
1030                         dummy->pcm_hw.rates = m->rates;
1031                 if (m->rate_min)
1032                         dummy->pcm_hw.rate_min = m->rate_min;
1033                 if (m->rate_max)
1034                         dummy->pcm_hw.rate_max = m->rate_max;
1035                 if (m->channels_min)
1036                         dummy->pcm_hw.channels_min = m->channels_min;
1037                 if (m->channels_max)
1038                         dummy->pcm_hw.channels_max = m->channels_max;
1039         }
1040
1041         err = snd_card_dummy_new_mixer(dummy);
1042         if (err < 0)
1043                 goto __nodev;
1044         strcpy(card->driver, "Dummy");
1045         strcpy(card->shortname, "Dummy");
1046         sprintf(card->longname, "Dummy %i", dev + 1);
1047
1048         dummy_proc_init(dummy);
1049
1050         snd_card_set_dev(card, &devptr->dev);
1051
1052         err = snd_card_register(card);
1053         if (err == 0) {
1054                 platform_set_drvdata(devptr, card);
1055                 return 0;
1056         }
1057       __nodev:
1058         snd_card_free(card);
1059         return err;
1060 }
1061
1062 static int __devexit snd_dummy_remove(struct platform_device *devptr)
1063 {
1064         snd_card_free(platform_get_drvdata(devptr));
1065         platform_set_drvdata(devptr, NULL);
1066         return 0;
1067 }
1068
1069 #ifdef CONFIG_PM
1070 static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
1071 {
1072         struct snd_card *card = platform_get_drvdata(pdev);
1073         struct snd_dummy *dummy = card->private_data;
1074
1075         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1076         snd_pcm_suspend_all(dummy->pcm);
1077         return 0;
1078 }
1079         
1080 static int snd_dummy_resume(struct platform_device *pdev)
1081 {
1082         struct snd_card *card = platform_get_drvdata(pdev);
1083
1084         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1085         return 0;
1086 }
1087 #endif
1088
1089 #define SND_DUMMY_DRIVER        "snd_dummy"
1090
1091 static struct platform_driver snd_dummy_driver = {
1092         .probe          = snd_dummy_probe,
1093         .remove         = __devexit_p(snd_dummy_remove),
1094 #ifdef CONFIG_PM
1095         .suspend        = snd_dummy_suspend,
1096         .resume         = snd_dummy_resume,
1097 #endif
1098         .driver         = {
1099                 .name   = SND_DUMMY_DRIVER
1100         },
1101 };
1102
1103 static void snd_dummy_unregister_all(void)
1104 {
1105         int i;
1106
1107         for (i = 0; i < ARRAY_SIZE(devices); ++i)
1108                 platform_device_unregister(devices[i]);
1109         platform_driver_unregister(&snd_dummy_driver);
1110         free_fake_buffer();
1111 }
1112
1113 static int __init alsa_card_dummy_init(void)
1114 {
1115         int i, cards, err;
1116
1117         err = platform_driver_register(&snd_dummy_driver);
1118         if (err < 0)
1119                 return err;
1120
1121         err = alloc_fake_buffer();
1122         if (err < 0) {
1123                 platform_driver_unregister(&snd_dummy_driver);
1124                 return err;
1125         }
1126
1127         cards = 0;
1128         for (i = 0; i < SNDRV_CARDS; i++) {
1129                 struct platform_device *device;
1130                 if (! enable[i])
1131                         continue;
1132                 device = platform_device_register_simple(SND_DUMMY_DRIVER,
1133                                                          i, NULL, 0);
1134                 if (IS_ERR(device))
1135                         continue;
1136                 if (!platform_get_drvdata(device)) {
1137                         platform_device_unregister(device);
1138                         continue;
1139                 }
1140                 devices[i] = device;
1141                 cards++;
1142         }
1143         if (!cards) {
1144 #ifdef MODULE
1145                 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
1146 #endif
1147                 snd_dummy_unregister_all();
1148                 return -ENODEV;
1149         }
1150         return 0;
1151 }
1152
1153 static void __exit alsa_card_dummy_exit(void)
1154 {
1155         snd_dummy_unregister_all();
1156 }
1157
1158 module_init(alsa_card_dummy_init)
1159 module_exit(alsa_card_dummy_exit)