1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/init.h>
9 #include <linux/platform_device.h>
10 #include <linux/jiffies.h>
11 #include <linux/slab.h>
12 #include <linux/time.h>
13 #include <linux/wait.h>
14 #include <linux/hrtimer.h>
15 #include <linux/math64.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/tlv.h>
20 #include <sound/pcm.h>
21 #include <sound/rawmidi.h>
22 #include <sound/info.h>
23 #include <sound/initval.h>
25 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
26 MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
27 MODULE_LICENSE("GPL");
28 MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
30 #define MAX_PCM_DEVICES 4
31 #define MAX_PCM_SUBSTREAMS 128
32 #define MAX_MIDI_DEVICES 2
35 #define MAX_BUFFER_SIZE (64*1024)
36 #define MIN_PERIOD_SIZE 64
37 #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
38 #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
39 #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
40 #define USE_RATE_MIN 5500
41 #define USE_RATE_MAX 48000
42 #define USE_CHANNELS_MIN 1
43 #define USE_CHANNELS_MAX 2
44 #define USE_PERIODS_MIN 1
45 #define USE_PERIODS_MAX 1024
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
50 static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
51 static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
52 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
53 //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
54 #ifdef CONFIG_HIGH_RES_TIMERS
55 static bool hrtimer = 1;
57 static bool fake_buffer = 1;
59 module_param_array(index, int, NULL, 0444);
60 MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
61 module_param_array(id, charp, NULL, 0444);
62 MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
63 module_param_array(enable, bool, NULL, 0444);
64 MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
65 module_param_array(model, charp, NULL, 0444);
66 MODULE_PARM_DESC(model, "Soundcard model.");
67 module_param_array(pcm_devs, int, NULL, 0444);
68 MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
69 module_param_array(pcm_substreams, int, NULL, 0444);
70 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
71 //module_param_array(midi_devs, int, NULL, 0444);
72 //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
73 module_param(fake_buffer, bool, 0444);
74 MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
75 #ifdef CONFIG_HIGH_RES_TIMERS
76 module_param(hrtimer, bool, 0644);
77 MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
80 static struct platform_device *devices[SNDRV_CARDS];
82 #define MIXER_ADDR_MASTER 0
83 #define MIXER_ADDR_LINE 1
84 #define MIXER_ADDR_MIC 2
85 #define MIXER_ADDR_SYNTH 3
86 #define MIXER_ADDR_CD 4
87 #define MIXER_ADDR_LAST 4
89 struct dummy_timer_ops {
90 int (*create)(struct snd_pcm_substream *);
91 void (*free)(struct snd_pcm_substream *);
92 int (*prepare)(struct snd_pcm_substream *);
93 int (*start)(struct snd_pcm_substream *);
94 int (*stop)(struct snd_pcm_substream *);
95 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
98 #define get_dummy_ops(substream) \
99 (*(const struct dummy_timer_ops **)(substream)->runtime->private_data)
103 int (*playback_constraints)(struct snd_pcm_runtime *runtime);
104 int (*capture_constraints)(struct snd_pcm_runtime *runtime);
106 size_t buffer_bytes_max;
107 size_t period_bytes_min;
108 size_t period_bytes_max;
109 unsigned int periods_min;
110 unsigned int periods_max;
112 unsigned int rate_min;
113 unsigned int rate_max;
114 unsigned int channels_min;
115 unsigned int channels_max;
119 struct snd_card *card;
120 struct dummy_model *model;
122 struct snd_pcm_hardware pcm_hw;
123 spinlock_t mixer_lock;
124 int mixer_volume[MIXER_ADDR_LAST+1][2];
125 int capture_source[MIXER_ADDR_LAST+1][2];
127 struct snd_kcontrol *cd_volume_ctl;
128 struct snd_kcontrol *cd_switch_ctl;
135 static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
138 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
141 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
147 static struct dummy_model model_emu10k1 = {
149 .playback_constraints = emu10k1_playback_constraints,
150 .buffer_bytes_max = 128 * 1024,
153 static struct dummy_model model_rme9652 = {
155 .buffer_bytes_max = 26 * 64 * 1024,
156 .formats = SNDRV_PCM_FMTBIT_S32_LE,
163 static struct dummy_model model_ice1712 = {
165 .buffer_bytes_max = 256 * 1024,
166 .formats = SNDRV_PCM_FMTBIT_S32_LE,
173 static struct dummy_model model_uda1341 = {
175 .buffer_bytes_max = 16380,
176 .formats = SNDRV_PCM_FMTBIT_S16_LE,
183 static struct dummy_model model_ac97 = {
185 .formats = SNDRV_PCM_FMTBIT_S16_LE,
188 .rates = SNDRV_PCM_RATE_48000,
193 static struct dummy_model model_ca0106 = {
195 .formats = SNDRV_PCM_FMTBIT_S16_LE,
196 .buffer_bytes_max = ((65536-64)*8),
197 .period_bytes_max = (65536-64),
202 .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
207 static struct dummy_model *dummy_models[] = {
218 * system timer interface
221 struct dummy_systimer_pcm {
222 /* ops must be the first item */
223 const struct dummy_timer_ops *timer_ops;
225 struct timer_list timer;
226 unsigned long base_time;
227 unsigned int frac_pos; /* fractional sample position (based HZ) */
228 unsigned int frac_period_rest;
229 unsigned int frac_buffer_size; /* buffer_size * HZ */
230 unsigned int frac_period_size; /* period_size * HZ */
233 struct snd_pcm_substream *substream;
236 static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
238 mod_timer(&dpcm->timer, jiffies +
239 (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate);
242 static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
246 delta = jiffies - dpcm->base_time;
249 dpcm->base_time += delta;
251 dpcm->frac_pos += delta;
252 while (dpcm->frac_pos >= dpcm->frac_buffer_size)
253 dpcm->frac_pos -= dpcm->frac_buffer_size;
254 while (dpcm->frac_period_rest <= delta) {
256 dpcm->frac_period_rest += dpcm->frac_period_size;
258 dpcm->frac_period_rest -= delta;
261 static int dummy_systimer_start(struct snd_pcm_substream *substream)
263 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
264 spin_lock(&dpcm->lock);
265 dpcm->base_time = jiffies;
266 dummy_systimer_rearm(dpcm);
267 spin_unlock(&dpcm->lock);
271 static int dummy_systimer_stop(struct snd_pcm_substream *substream)
273 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
274 spin_lock(&dpcm->lock);
275 del_timer(&dpcm->timer);
276 spin_unlock(&dpcm->lock);
280 static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
282 struct snd_pcm_runtime *runtime = substream->runtime;
283 struct dummy_systimer_pcm *dpcm = runtime->private_data;
286 dpcm->rate = runtime->rate;
287 dpcm->frac_buffer_size = runtime->buffer_size * HZ;
288 dpcm->frac_period_size = runtime->period_size * HZ;
289 dpcm->frac_period_rest = dpcm->frac_period_size;
295 static void dummy_systimer_callback(struct timer_list *t)
297 struct dummy_systimer_pcm *dpcm = from_timer(dpcm, t, timer);
301 spin_lock_irqsave(&dpcm->lock, flags);
302 dummy_systimer_update(dpcm);
303 dummy_systimer_rearm(dpcm);
304 elapsed = dpcm->elapsed;
306 spin_unlock_irqrestore(&dpcm->lock, flags);
308 snd_pcm_period_elapsed(dpcm->substream);
311 static snd_pcm_uframes_t
312 dummy_systimer_pointer(struct snd_pcm_substream *substream)
314 struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
315 snd_pcm_uframes_t pos;
317 spin_lock(&dpcm->lock);
318 dummy_systimer_update(dpcm);
319 pos = dpcm->frac_pos / HZ;
320 spin_unlock(&dpcm->lock);
324 static int dummy_systimer_create(struct snd_pcm_substream *substream)
326 struct dummy_systimer_pcm *dpcm;
328 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
331 substream->runtime->private_data = dpcm;
332 timer_setup(&dpcm->timer, dummy_systimer_callback, 0);
333 spin_lock_init(&dpcm->lock);
334 dpcm->substream = substream;
338 static void dummy_systimer_free(struct snd_pcm_substream *substream)
340 kfree(substream->runtime->private_data);
343 static const struct dummy_timer_ops dummy_systimer_ops = {
344 .create = dummy_systimer_create,
345 .free = dummy_systimer_free,
346 .prepare = dummy_systimer_prepare,
347 .start = dummy_systimer_start,
348 .stop = dummy_systimer_stop,
349 .pointer = dummy_systimer_pointer,
352 #ifdef CONFIG_HIGH_RES_TIMERS
357 struct dummy_hrtimer_pcm {
358 /* ops must be the first item */
359 const struct dummy_timer_ops *timer_ops;
363 struct hrtimer timer;
364 struct snd_pcm_substream *substream;
367 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
369 struct dummy_hrtimer_pcm *dpcm;
371 dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
372 if (!atomic_read(&dpcm->running))
373 return HRTIMER_NORESTART;
375 * In cases of XRUN and draining, this calls .trigger to stop PCM
378 snd_pcm_period_elapsed(dpcm->substream);
379 if (!atomic_read(&dpcm->running))
380 return HRTIMER_NORESTART;
382 hrtimer_forward_now(timer, dpcm->period_time);
383 return HRTIMER_RESTART;
386 static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
388 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
390 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
391 hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT);
392 atomic_set(&dpcm->running, 1);
396 static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
398 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
400 atomic_set(&dpcm->running, 0);
401 if (!hrtimer_callback_running(&dpcm->timer))
402 hrtimer_cancel(&dpcm->timer);
406 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
408 hrtimer_cancel(&dpcm->timer);
411 static snd_pcm_uframes_t
412 dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
414 struct snd_pcm_runtime *runtime = substream->runtime;
415 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
419 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
421 delta = div_u64(delta * runtime->rate + 999999, 1000000);
422 div_u64_rem(delta, runtime->buffer_size, &pos);
426 static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
428 struct snd_pcm_runtime *runtime = substream->runtime;
429 struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
430 unsigned int period, rate;
434 dummy_hrtimer_sync(dpcm);
435 period = runtime->period_size;
436 rate = runtime->rate;
439 nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
440 dpcm->period_time = ktime_set(sec, nsecs);
445 static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
447 struct dummy_hrtimer_pcm *dpcm;
449 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
452 substream->runtime->private_data = dpcm;
453 hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
454 dpcm->timer.function = dummy_hrtimer_callback;
455 dpcm->substream = substream;
456 atomic_set(&dpcm->running, 0);
460 static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
462 struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
463 dummy_hrtimer_sync(dpcm);
467 static const struct dummy_timer_ops dummy_hrtimer_ops = {
468 .create = dummy_hrtimer_create,
469 .free = dummy_hrtimer_free,
470 .prepare = dummy_hrtimer_prepare,
471 .start = dummy_hrtimer_start,
472 .stop = dummy_hrtimer_stop,
473 .pointer = dummy_hrtimer_pointer,
476 #endif /* CONFIG_HIGH_RES_TIMERS */
482 static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
485 case SNDRV_PCM_TRIGGER_START:
486 case SNDRV_PCM_TRIGGER_RESUME:
487 return get_dummy_ops(substream)->start(substream);
488 case SNDRV_PCM_TRIGGER_STOP:
489 case SNDRV_PCM_TRIGGER_SUSPEND:
490 return get_dummy_ops(substream)->stop(substream);
495 static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
497 return get_dummy_ops(substream)->prepare(substream);
500 static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
502 return get_dummy_ops(substream)->pointer(substream);
505 static const struct snd_pcm_hardware dummy_pcm_hardware = {
506 .info = (SNDRV_PCM_INFO_MMAP |
507 SNDRV_PCM_INFO_INTERLEAVED |
508 SNDRV_PCM_INFO_RESUME |
509 SNDRV_PCM_INFO_MMAP_VALID),
510 .formats = USE_FORMATS,
512 .rate_min = USE_RATE_MIN,
513 .rate_max = USE_RATE_MAX,
514 .channels_min = USE_CHANNELS_MIN,
515 .channels_max = USE_CHANNELS_MAX,
516 .buffer_bytes_max = MAX_BUFFER_SIZE,
517 .period_bytes_min = MIN_PERIOD_SIZE,
518 .period_bytes_max = MAX_PERIOD_SIZE,
519 .periods_min = USE_PERIODS_MIN,
520 .periods_max = USE_PERIODS_MAX,
524 static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
525 struct snd_pcm_hw_params *hw_params)
528 /* runtime->dma_bytes has to be set manually to allow mmap */
529 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
532 return snd_pcm_lib_malloc_pages(substream,
533 params_buffer_bytes(hw_params));
536 static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
540 return snd_pcm_lib_free_pages(substream);
543 static int dummy_pcm_open(struct snd_pcm_substream *substream)
545 struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
546 struct dummy_model *model = dummy->model;
547 struct snd_pcm_runtime *runtime = substream->runtime;
548 const struct dummy_timer_ops *ops;
551 ops = &dummy_systimer_ops;
552 #ifdef CONFIG_HIGH_RES_TIMERS
554 ops = &dummy_hrtimer_ops;
557 err = ops->create(substream);
560 get_dummy_ops(substream) = ops;
562 runtime->hw = dummy->pcm_hw;
563 if (substream->pcm->device & 1) {
564 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
565 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
567 if (substream->pcm->device & 2)
568 runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
569 SNDRV_PCM_INFO_MMAP_VALID);
574 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
575 if (model->playback_constraints)
576 err = model->playback_constraints(substream->runtime);
578 if (model->capture_constraints)
579 err = model->capture_constraints(substream->runtime);
582 get_dummy_ops(substream)->free(substream);
588 static int dummy_pcm_close(struct snd_pcm_substream *substream)
590 get_dummy_ops(substream)->free(substream);
595 * dummy buffer handling
598 static void *dummy_page[2];
600 static void free_fake_buffer(void)
604 for (i = 0; i < 2; i++)
606 free_page((unsigned long)dummy_page[i]);
607 dummy_page[i] = NULL;
612 static int alloc_fake_buffer(void)
618 for (i = 0; i < 2; i++) {
619 dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
620 if (!dummy_page[i]) {
628 static int dummy_pcm_copy(struct snd_pcm_substream *substream,
629 int channel, unsigned long pos,
630 void __user *dst, unsigned long bytes)
632 return 0; /* do nothing */
635 static int dummy_pcm_copy_kernel(struct snd_pcm_substream *substream,
636 int channel, unsigned long pos,
637 void *dst, unsigned long bytes)
639 return 0; /* do nothing */
642 static int dummy_pcm_silence(struct snd_pcm_substream *substream,
643 int channel, unsigned long pos,
646 return 0; /* do nothing */
649 static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
650 unsigned long offset)
652 return virt_to_page(dummy_page[substream->stream]); /* the same page */
655 static struct snd_pcm_ops dummy_pcm_ops = {
656 .open = dummy_pcm_open,
657 .close = dummy_pcm_close,
658 .ioctl = snd_pcm_lib_ioctl,
659 .hw_params = dummy_pcm_hw_params,
660 .hw_free = dummy_pcm_hw_free,
661 .prepare = dummy_pcm_prepare,
662 .trigger = dummy_pcm_trigger,
663 .pointer = dummy_pcm_pointer,
666 static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
667 .open = dummy_pcm_open,
668 .close = dummy_pcm_close,
669 .ioctl = snd_pcm_lib_ioctl,
670 .hw_params = dummy_pcm_hw_params,
671 .hw_free = dummy_pcm_hw_free,
672 .prepare = dummy_pcm_prepare,
673 .trigger = dummy_pcm_trigger,
674 .pointer = dummy_pcm_pointer,
675 .copy_user = dummy_pcm_copy,
676 .copy_kernel = dummy_pcm_copy_kernel,
677 .fill_silence = dummy_pcm_silence,
678 .page = dummy_pcm_page,
681 static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
685 struct snd_pcm_ops *ops;
688 err = snd_pcm_new(dummy->card, "Dummy PCM", device,
689 substreams, substreams, &pcm);
694 ops = &dummy_pcm_ops_no_buf;
696 ops = &dummy_pcm_ops;
697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
698 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
699 pcm->private_data = dummy;
701 strcpy(pcm->name, "Dummy PCM");
703 snd_pcm_lib_preallocate_pages_for_all(pcm,
704 SNDRV_DMA_TYPE_CONTINUOUS,
715 #define DUMMY_VOLUME(xname, xindex, addr) \
716 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
717 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
718 .name = xname, .index = xindex, \
719 .info = snd_dummy_volume_info, \
720 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
721 .private_value = addr, \
722 .tlv = { .p = db_scale_dummy } }
724 static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
725 struct snd_ctl_elem_info *uinfo)
727 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
729 uinfo->value.integer.min = -50;
730 uinfo->value.integer.max = 100;
734 static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
735 struct snd_ctl_elem_value *ucontrol)
737 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
738 int addr = kcontrol->private_value;
740 spin_lock_irq(&dummy->mixer_lock);
741 ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
742 ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
743 spin_unlock_irq(&dummy->mixer_lock);
747 static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
748 struct snd_ctl_elem_value *ucontrol)
750 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
751 int change, addr = kcontrol->private_value;
754 left = ucontrol->value.integer.value[0];
759 right = ucontrol->value.integer.value[1];
764 spin_lock_irq(&dummy->mixer_lock);
765 change = dummy->mixer_volume[addr][0] != left ||
766 dummy->mixer_volume[addr][1] != right;
767 dummy->mixer_volume[addr][0] = left;
768 dummy->mixer_volume[addr][1] = right;
769 spin_unlock_irq(&dummy->mixer_lock);
773 static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
775 #define DUMMY_CAPSRC(xname, xindex, addr) \
776 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
777 .info = snd_dummy_capsrc_info, \
778 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
779 .private_value = addr }
781 #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
783 static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
784 struct snd_ctl_elem_value *ucontrol)
786 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
787 int addr = kcontrol->private_value;
789 spin_lock_irq(&dummy->mixer_lock);
790 ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
791 ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
792 spin_unlock_irq(&dummy->mixer_lock);
796 static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
798 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
799 int change, addr = kcontrol->private_value;
802 left = ucontrol->value.integer.value[0] & 1;
803 right = ucontrol->value.integer.value[1] & 1;
804 spin_lock_irq(&dummy->mixer_lock);
805 change = dummy->capture_source[addr][0] != left &&
806 dummy->capture_source[addr][1] != right;
807 dummy->capture_source[addr][0] = left;
808 dummy->capture_source[addr][1] = right;
809 spin_unlock_irq(&dummy->mixer_lock);
813 static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
814 struct snd_ctl_elem_info *info)
816 static const char *const names[] = { "None", "CD Player" };
818 return snd_ctl_enum_info(info, 1, 2, names);
821 static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
822 struct snd_ctl_elem_value *value)
824 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
826 value->value.enumerated.item[0] = dummy->iobox;
830 static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
831 struct snd_ctl_elem_value *value)
833 struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
836 if (value->value.enumerated.item[0] > 1)
839 changed = value->value.enumerated.item[0] != dummy->iobox;
841 dummy->iobox = value->value.enumerated.item[0];
844 dummy->cd_volume_ctl->vd[0].access &=
845 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
846 dummy->cd_switch_ctl->vd[0].access &=
847 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
849 dummy->cd_volume_ctl->vd[0].access |=
850 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
851 dummy->cd_switch_ctl->vd[0].access |=
852 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
855 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
856 &dummy->cd_volume_ctl->id);
857 snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
858 &dummy->cd_switch_ctl->id);
864 static struct snd_kcontrol_new snd_dummy_controls[] = {
865 DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
866 DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
867 DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
868 DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
869 DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
870 DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
871 DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
872 DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
873 DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
874 DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
877 .name = "External I/O Box",
878 .info = snd_dummy_iobox_info,
879 .get = snd_dummy_iobox_get,
880 .put = snd_dummy_iobox_put,
884 static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
886 struct snd_card *card = dummy->card;
887 struct snd_kcontrol *kcontrol;
891 spin_lock_init(&dummy->mixer_lock);
892 strcpy(card->mixername, "Dummy Mixer");
895 for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
896 kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
897 err = snd_ctl_add(card, kcontrol);
900 if (!strcmp(kcontrol->id.name, "CD Volume"))
901 dummy->cd_volume_ctl = kcontrol;
902 else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
903 dummy->cd_switch_ctl = kcontrol;
909 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
913 static void print_formats(struct snd_dummy *dummy,
914 struct snd_info_buffer *buffer)
918 for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
919 if (dummy->pcm_hw.formats & (1ULL << i))
920 snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
924 static void print_rates(struct snd_dummy *dummy,
925 struct snd_info_buffer *buffer)
927 static int rates[] = {
928 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
929 64000, 88200, 96000, 176400, 192000,
933 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
934 snd_iprintf(buffer, " continuous");
935 if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
936 snd_iprintf(buffer, " knot");
937 for (i = 0; i < ARRAY_SIZE(rates); i++)
938 if (dummy->pcm_hw.rates & (1 << i))
939 snd_iprintf(buffer, " %d", rates[i]);
942 #define get_dummy_int_ptr(dummy, ofs) \
943 (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
944 #define get_dummy_ll_ptr(dummy, ofs) \
945 (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
947 struct dummy_hw_field {
953 #define FIELD_ENTRY(item, fmt) { \
956 .offset = offsetof(struct snd_pcm_hardware, item), \
957 .size = sizeof(dummy_pcm_hardware.item) }
959 static struct dummy_hw_field fields[] = {
960 FIELD_ENTRY(formats, "%#llx"),
961 FIELD_ENTRY(rates, "%#x"),
962 FIELD_ENTRY(rate_min, "%d"),
963 FIELD_ENTRY(rate_max, "%d"),
964 FIELD_ENTRY(channels_min, "%d"),
965 FIELD_ENTRY(channels_max, "%d"),
966 FIELD_ENTRY(buffer_bytes_max, "%ld"),
967 FIELD_ENTRY(period_bytes_min, "%ld"),
968 FIELD_ENTRY(period_bytes_max, "%ld"),
969 FIELD_ENTRY(periods_min, "%d"),
970 FIELD_ENTRY(periods_max, "%d"),
973 static void dummy_proc_read(struct snd_info_entry *entry,
974 struct snd_info_buffer *buffer)
976 struct snd_dummy *dummy = entry->private_data;
979 for (i = 0; i < ARRAY_SIZE(fields); i++) {
980 snd_iprintf(buffer, "%s ", fields[i].name);
981 if (fields[i].size == sizeof(int))
982 snd_iprintf(buffer, fields[i].format,
983 *get_dummy_int_ptr(dummy, fields[i].offset));
985 snd_iprintf(buffer, fields[i].format,
986 *get_dummy_ll_ptr(dummy, fields[i].offset));
987 if (!strcmp(fields[i].name, "formats"))
988 print_formats(dummy, buffer);
989 else if (!strcmp(fields[i].name, "rates"))
990 print_rates(dummy, buffer);
991 snd_iprintf(buffer, "\n");
995 static void dummy_proc_write(struct snd_info_entry *entry,
996 struct snd_info_buffer *buffer)
998 struct snd_dummy *dummy = entry->private_data;
1001 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1004 unsigned long long val;
1007 ptr = snd_info_get_str(item, line, sizeof(item));
1008 for (i = 0; i < ARRAY_SIZE(fields); i++) {
1009 if (!strcmp(item, fields[i].name))
1012 if (i >= ARRAY_SIZE(fields))
1014 snd_info_get_str(item, ptr, sizeof(item));
1015 if (kstrtoull(item, 0, &val))
1017 if (fields[i].size == sizeof(int))
1018 *get_dummy_int_ptr(dummy, fields[i].offset) = val;
1020 *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
1024 static void dummy_proc_init(struct snd_dummy *chip)
1026 snd_card_rw_proc_new(chip->card, "dummy_pcm", chip,
1027 dummy_proc_read, dummy_proc_write);
1030 #define dummy_proc_init(x)
1031 #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
1033 static int snd_dummy_probe(struct platform_device *devptr)
1035 struct snd_card *card;
1036 struct snd_dummy *dummy;
1037 struct dummy_model *m = NULL, **mdl;
1039 int dev = devptr->id;
1041 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1042 sizeof(struct snd_dummy), &card);
1045 dummy = card->private_data;
1047 for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
1048 if (strcmp(model[dev], (*mdl)->name) == 0) {
1050 "snd-dummy: Using model '%s' for card %i\n",
1051 (*mdl)->name, card->number);
1052 m = dummy->model = *mdl;
1056 for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
1057 if (pcm_substreams[dev] < 1)
1058 pcm_substreams[dev] = 1;
1059 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1060 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1061 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
1066 dummy->pcm_hw = dummy_pcm_hardware;
1069 dummy->pcm_hw.formats = m->formats;
1070 if (m->buffer_bytes_max)
1071 dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
1072 if (m->period_bytes_min)
1073 dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
1074 if (m->period_bytes_max)
1075 dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
1077 dummy->pcm_hw.periods_min = m->periods_min;
1079 dummy->pcm_hw.periods_max = m->periods_max;
1081 dummy->pcm_hw.rates = m->rates;
1083 dummy->pcm_hw.rate_min = m->rate_min;
1085 dummy->pcm_hw.rate_max = m->rate_max;
1086 if (m->channels_min)
1087 dummy->pcm_hw.channels_min = m->channels_min;
1088 if (m->channels_max)
1089 dummy->pcm_hw.channels_max = m->channels_max;
1092 err = snd_card_dummy_new_mixer(dummy);
1095 strcpy(card->driver, "Dummy");
1096 strcpy(card->shortname, "Dummy");
1097 sprintf(card->longname, "Dummy %i", dev + 1);
1099 dummy_proc_init(dummy);
1101 err = snd_card_register(card);
1103 platform_set_drvdata(devptr, card);
1107 snd_card_free(card);
1111 static int snd_dummy_remove(struct platform_device *devptr)
1113 snd_card_free(platform_get_drvdata(devptr));
1117 #ifdef CONFIG_PM_SLEEP
1118 static int snd_dummy_suspend(struct device *pdev)
1120 struct snd_card *card = dev_get_drvdata(pdev);
1122 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1126 static int snd_dummy_resume(struct device *pdev)
1128 struct snd_card *card = dev_get_drvdata(pdev);
1130 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1134 static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
1135 #define SND_DUMMY_PM_OPS &snd_dummy_pm
1137 #define SND_DUMMY_PM_OPS NULL
1140 #define SND_DUMMY_DRIVER "snd_dummy"
1142 static struct platform_driver snd_dummy_driver = {
1143 .probe = snd_dummy_probe,
1144 .remove = snd_dummy_remove,
1146 .name = SND_DUMMY_DRIVER,
1147 .pm = SND_DUMMY_PM_OPS,
1151 static void snd_dummy_unregister_all(void)
1155 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1156 platform_device_unregister(devices[i]);
1157 platform_driver_unregister(&snd_dummy_driver);
1161 static int __init alsa_card_dummy_init(void)
1165 err = platform_driver_register(&snd_dummy_driver);
1169 err = alloc_fake_buffer();
1171 platform_driver_unregister(&snd_dummy_driver);
1176 for (i = 0; i < SNDRV_CARDS; i++) {
1177 struct platform_device *device;
1180 device = platform_device_register_simple(SND_DUMMY_DRIVER,
1184 if (!platform_get_drvdata(device)) {
1185 platform_device_unregister(device);
1188 devices[i] = device;
1193 printk(KERN_ERR "Dummy soundcard not found or device busy\n");
1195 snd_dummy_unregister_all();
1201 static void __exit alsa_card_dummy_exit(void)
1203 snd_dummy_unregister_all();
1206 module_init(alsa_card_dummy_init)
1207 module_exit(alsa_card_dummy_exit)