25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/wait.h>
28 #include <linux/hrtimer.h>
30 #include <linux/module.h>
44 #define MAX_PCM_DEVICES 4
45 #define MAX_PCM_SUBSTREAMS 128
46 #define MAX_MIDI_DEVICES 2
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
68 #ifdef CONFIG_HIGH_RES_TIMERS
71 static bool fake_buffer = 1;
84 MODULE_PARM_DESC(pcm_substreams,
"PCM substreams # (1-128) for dummy driver.");
89 #ifdef CONFIG_HIGH_RES_TIMERS
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
144 static int emu10k1_playback_constraints(
struct snd_pcm_runtime *runtime)
158 .playback_constraints = emu10k1_playback_constraints,
159 .buffer_bytes_max = 128 * 1024,
164 .buffer_bytes_max = 26 * 64 * 1024,
174 .buffer_bytes_max = 256 * 1024,
184 .buffer_bytes_max = 16380,
205 .buffer_bytes_max = ((65536-64)*8),
206 .period_bytes_max = (65536-64),
272 spin_lock(&dpcm->
lock);
274 dummy_systimer_rearm(dpcm);
275 spin_unlock(&dpcm->
lock);
282 spin_lock(&dpcm->
lock);
284 spin_unlock(&dpcm->
lock);
303 static void dummy_systimer_callback(
unsigned long data)
310 dummy_systimer_update(dpcm);
311 dummy_systimer_rearm(dpcm);
314 spin_unlock_irqrestore(&dpcm->
lock, flags);
325 spin_lock(&dpcm->
lock);
326 dummy_systimer_update(dpcm);
328 spin_unlock(&dpcm->
lock);
339 substream->
runtime->private_data = dpcm;
342 dpcm->
timer.function = dummy_systimer_callback;
354 .create = dummy_systimer_create,
355 .free = dummy_systimer_free,
356 .prepare = dummy_systimer_prepare,
357 .start = dummy_systimer_start,
358 .stop = dummy_systimer_stop,
359 .pointer = dummy_systimer_pointer,
362 #ifdef CONFIG_HIGH_RES_TIMERS
367 struct dummy_hrtimer_pcm {
371 struct hrtimer
timer;
376 static void dummy_hrtimer_pcm_elapsed(
unsigned long priv)
378 struct dummy_hrtimer_pcm *dpcm = (
struct dummy_hrtimer_pcm *)priv;
385 struct dummy_hrtimer_pcm *dpcm;
387 dpcm =
container_of(timer,
struct dummy_hrtimer_pcm, timer);
390 tasklet_schedule(&dpcm->tasklet);
391 hrtimer_forward_now(timer, dpcm->period_time);
397 struct dummy_hrtimer_pcm *dpcm = substream->
runtime->private_data;
399 dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
407 struct dummy_hrtimer_pcm *dpcm = substream->
runtime->private_data;
414 static inline void dummy_hrtimer_sync(
struct dummy_hrtimer_pcm *dpcm)
427 delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
429 delta = div_u64(delta * runtime->
rate + 999999, 1000000);
442 dummy_hrtimer_sync(dpcm);
444 rate = runtime->
rate;
447 nsecs = div_u64((
u64)period * 1000000000
UL + rate - 1, rate);
448 dpcm->period_time = ktime_set(sec, nsecs);
455 struct dummy_hrtimer_pcm *dpcm;
460 substream->
runtime->private_data = dpcm;
462 dpcm->timer.function = dummy_hrtimer_callback;
463 dpcm->substream = substream;
466 (
unsigned long)dpcm);
472 struct dummy_hrtimer_pcm *dpcm = substream->
runtime->private_data;
473 dummy_hrtimer_sync(dpcm);
478 .
create = dummy_hrtimer_create,
479 .free = dummy_hrtimer_free,
480 .prepare = dummy_hrtimer_prepare,
481 .start = dummy_hrtimer_start,
482 .stop = dummy_hrtimer_stop,
483 .pointer = dummy_hrtimer_pointer,
499 return dummy->
timer_ops->start(substream);
502 return dummy->
timer_ops->stop(substream);
511 return dummy->
timer_ops->prepare(substream);
518 return dummy->
timer_ops->pointer(substream);
567 #ifdef CONFIG_HIGH_RES_TIMERS
572 err = dummy->
timer_ops->create(substream);
577 if (substream->
pcm->device & 1) {
581 if (substream->
pcm->device & 2)
613 static void *dummy_page[2];
615 static void free_fake_buffer(
void)
619 for (i = 0; i < 2; i++)
622 dummy_page[
i] =
NULL;
627 static int alloc_fake_buffer(
void)
633 for (i = 0; i < 2; i++) {
635 if (!dummy_page[i]) {
664 .open = dummy_pcm_open,
665 .close = dummy_pcm_close,
667 .hw_params = dummy_pcm_hw_params,
668 .hw_free = dummy_pcm_hw_free,
669 .prepare = dummy_pcm_prepare,
670 .trigger = dummy_pcm_trigger,
671 .pointer = dummy_pcm_pointer,
675 .open = dummy_pcm_open,
676 .close = dummy_pcm_close,
678 .hw_params = dummy_pcm_hw_params,
679 .hw_free = dummy_pcm_hw_free,
680 .prepare = dummy_pcm_prepare,
681 .trigger = dummy_pcm_trigger,
682 .pointer = dummy_pcm_pointer,
683 .copy = dummy_pcm_copy,
684 .silence = dummy_pcm_silence,
685 .page = dummy_pcm_page,
696 substreams, substreams, &pcm);
701 ops = &dummy_pcm_ops_no_buf;
703 ops = &dummy_pcm_ops;
722 #define DUMMY_VOLUME(xname, xindex, addr) \
723 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
724 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
725 .name = xname, .index = xindex, \
726 .info = snd_dummy_volume_info, \
727 .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
728 .private_value = addr, \
729 .tlv = { .p = db_scale_dummy } }
731 static int snd_dummy_volume_info(
struct snd_kcontrol *kcontrol,
741 static int snd_dummy_volume_get(
struct snd_kcontrol *kcontrol,
754 static int snd_dummy_volume_put(
struct snd_kcontrol *kcontrol,
761 left = ucontrol->
value.integer.value[0];
766 right = ucontrol->
value.integer.value[1];
782 #define DUMMY_CAPSRC(xname, xindex, addr) \
783 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
784 .info = snd_dummy_capsrc_info, \
785 .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
786 .private_value = addr }
788 #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
790 static int snd_dummy_capsrc_get(
struct snd_kcontrol *kcontrol,
809 left = ucontrol->
value.integer.value[0] & 1;
810 right = ucontrol->
value.integer.value[1] & 1;
842 for (idx = 0; idx <
ARRAY_SIZE(snd_dummy_controls); idx++) {
850 #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS)
854 static void print_formats(
struct snd_dummy *dummy,
860 if (dummy->
pcm_hw.formats & (1ULL << i))
865 static void print_rates(
struct snd_dummy *dummy,
868 static int rates[] = {
869 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
870 64000, 88200, 96000, 176400, 192000,
875 snd_iprintf(buffer,
" continuous");
877 snd_iprintf(buffer,
" knot");
879 if (dummy->
pcm_hw.rates & (1 << i))
880 snd_iprintf(buffer,
" %d", rates[i]);
883 #define get_dummy_int_ptr(dummy, ofs) \
884 (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
885 #define get_dummy_ll_ptr(dummy, ofs) \
886 (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
888 struct dummy_hw_field {
894 #define FIELD_ENTRY(item, fmt) { \
897 .offset = offsetof(struct snd_pcm_hardware, item), \
898 .size = sizeof(dummy_pcm_hardware.item) }
900 static struct dummy_hw_field
fields[] = {
901 FIELD_ENTRY(formats,
"%#llx"),
902 FIELD_ENTRY(rates,
"%#x"),
903 FIELD_ENTRY(rate_min,
"%d"),
904 FIELD_ENTRY(rate_max,
"%d"),
905 FIELD_ENTRY(channels_min,
"%d"),
906 FIELD_ENTRY(channels_max,
"%d"),
907 FIELD_ENTRY(buffer_bytes_max,
"%ld"),
908 FIELD_ENTRY(period_bytes_min,
"%ld"),
909 FIELD_ENTRY(period_bytes_max,
"%ld"),
910 FIELD_ENTRY(periods_min,
"%d"),
911 FIELD_ENTRY(periods_max,
"%d"),
921 snd_iprintf(buffer,
"%s ", fields[i].
name);
922 if (fields[i].
size ==
sizeof(
int))
923 snd_iprintf(buffer, fields[i].
format,
924 *get_dummy_int_ptr(dummy, fields[i].
offset));
926 snd_iprintf(buffer, fields[i].format,
927 *get_dummy_ll_ptr(dummy, fields[i].
offset));
929 print_formats(dummy, buffer);
930 else if (!
strcmp(fields[i].name,
"rates"))
931 print_rates(dummy, buffer);
932 snd_iprintf(buffer,
"\n");
942 while (!snd_info_get_line(buffer, line,
sizeof(line))) {
945 unsigned long long val;
948 ptr = snd_info_get_str(item, line,
sizeof(item));
950 if (!
strcmp(item, fields[i].name))
955 snd_info_get_str(item, ptr,
sizeof(item));
958 if (fields[i].
size ==
sizeof(
int))
959 *get_dummy_int_ptr(dummy, fields[i].
offset) =
val;
961 *get_dummy_ll_ptr(dummy, fields[i].
offset) =
val;
969 if (!snd_card_proc_new(chip->
card,
"dummy_pcm", &entry)) {
970 snd_info_set_text_ops(entry, chip, dummy_proc_read);
971 entry->
c.
text.write = dummy_proc_write;
977 #define dummy_proc_init(x)
986 int dev = devptr->
id;
994 for (mdl = dummy_models; *mdl && model[
dev]; mdl++) {
995 if (
strcmp(model[dev], (*mdl)->name) == 0) {
997 "snd-dummy: Using model '%s' for card %i\n",
998 (*mdl)->name, card->
number);
999 m = dummy->
model = *mdl;
1004 if (pcm_substreams[dev] < 1)
1005 pcm_substreams[
dev] = 1;
1008 err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
1013 dummy->
pcm_hw = dummy_pcm_hardware;
1039 err = snd_card_dummy_new_mixer(dummy);
1052 platform_set_drvdata(devptr, card);
1063 platform_set_drvdata(devptr,
NULL);
1067 #ifdef CONFIG_PM_SLEEP
1068 static int snd_dummy_suspend(
struct device *pdev)
1074 snd_pcm_suspend_all(dummy->
pcm);
1078 static int snd_dummy_resume(
struct device *pdev)
1087 #define SND_DUMMY_PM_OPS &snd_dummy_pm
1089 #define SND_DUMMY_PM_OPS NULL
1092 #define SND_DUMMY_DRIVER "snd_dummy"
1095 .probe = snd_dummy_probe,
1104 static void snd_dummy_unregister_all(
void)
1114 static int __init alsa_card_dummy_init(
void)
1122 err = alloc_fake_buffer();
1137 if (!platform_get_drvdata(device)) {
1141 devices[
i] = device;
1148 snd_dummy_unregister_all();
1154 static void __exit alsa_card_dummy_exit(
void)
1156 snd_dummy_unregister_all();