| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * The driver for the EMU10K1 (SB Live!) based soundcards |
| 4 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
| 5 | * James Courtier-Dutton <James@superbug.co.uk> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/pci.h> |
| 10 | #include <linux/string.h> |
| 11 | #include <linux/time.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <sound/core.h> |
| 14 | #include <sound/emu10k1.h> |
| 15 | #include <sound/initval.h> |
| 16 | |
| 17 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>" ); |
| 18 | MODULE_DESCRIPTION("EMU10K1" ); |
| 19 | MODULE_LICENSE("GPL" ); |
| 20 | |
| 21 | #if IS_ENABLED(CONFIG_SND_SEQUENCER) |
| 22 | #define ENABLE_SYNTH |
| 23 | #include <sound/emu10k1_synth.h> |
| 24 | #endif |
| 25 | |
| 26 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 27 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 28 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ |
| 29 | static int extin[SNDRV_CARDS]; |
| 30 | static int extout[SNDRV_CARDS]; |
| 31 | static int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4}; |
| 32 | static int max_synth_voices[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 64}; |
| 33 | static int max_buffer_size[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 128}; |
| 34 | static bool enable_ir[SNDRV_CARDS]; |
| 35 | static uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */ |
| 36 | |
| 37 | module_param_array(index, int, NULL, 0444); |
| 38 | MODULE_PARM_DESC(index, "Index value for the EMU10K1 soundcard." ); |
| 39 | module_param_array(id, charp, NULL, 0444); |
| 40 | MODULE_PARM_DESC(id, "ID string for the EMU10K1 soundcard." ); |
| 41 | module_param_array(enable, bool, NULL, 0444); |
| 42 | MODULE_PARM_DESC(enable, "Enable the EMU10K1 soundcard." ); |
| 43 | module_param_array(extin, int, NULL, 0444); |
| 44 | MODULE_PARM_DESC(extin, "Available external inputs for FX8010. Zero=default." ); |
| 45 | module_param_array(extout, int, NULL, 0444); |
| 46 | MODULE_PARM_DESC(extout, "Available external outputs for FX8010. Zero=default." ); |
| 47 | module_param_array(seq_ports, int, NULL, 0444); |
| 48 | MODULE_PARM_DESC(seq_ports, "Allocated sequencer ports for internal synthesizer." ); |
| 49 | module_param_array(max_synth_voices, int, NULL, 0444); |
| 50 | MODULE_PARM_DESC(max_synth_voices, "Maximum number of voices for WaveTable." ); |
| 51 | module_param_array(max_buffer_size, int, NULL, 0444); |
| 52 | MODULE_PARM_DESC(max_buffer_size, "Maximum sample buffer size in MB." ); |
| 53 | module_param_array(enable_ir, bool, NULL, 0444); |
| 54 | MODULE_PARM_DESC(enable_ir, "Enable IR." ); |
| 55 | module_param_array(subsystem, uint, NULL, 0444); |
| 56 | MODULE_PARM_DESC(subsystem, "Force card subsystem model." ); |
| 57 | /* |
| 58 | * Class 0401: 1102:0008 (rev 00) Subsystem: 1102:1001 -> Audigy2 Value Model:SB0400 |
| 59 | */ |
| 60 | static const struct pci_device_id snd_emu10k1_ids[] = { |
| 61 | { PCI_VDEVICE(CREATIVE, 0x0002), 0 }, /* EMU10K1 */ |
| 62 | { PCI_VDEVICE(CREATIVE, 0x0004), 1 }, /* Audigy */ |
| 63 | { PCI_VDEVICE(CREATIVE, 0x0008), 1 }, /* Audigy 2 Value SB0400 */ |
| 64 | { 0, } |
| 65 | }; |
| 66 | |
| 67 | MODULE_DEVICE_TABLE(pci, snd_emu10k1_ids); |
| 68 | |
| 69 | static int snd_card_emu10k1_probe(struct pci_dev *pci, |
| 70 | const struct pci_device_id *pci_id) |
| 71 | { |
| 72 | static int dev; |
| 73 | struct snd_card *card; |
| 74 | struct snd_emu10k1 *emu; |
| 75 | #ifdef ENABLE_SYNTH |
| 76 | struct snd_seq_device *wave = NULL; |
| 77 | #endif |
| 78 | int err; |
| 79 | |
| 80 | if (dev >= SNDRV_CARDS) |
| 81 | return -ENODEV; |
| 82 | if (!enable[dev]) { |
| 83 | dev++; |
| 84 | return -ENOENT; |
| 85 | } |
| 86 | |
| 87 | err = snd_devm_card_new(parent: &pci->dev, idx: index[dev], xid: id[dev], THIS_MODULE, |
| 88 | extra_size: sizeof(*emu), card_ret: &card); |
| 89 | if (err < 0) |
| 90 | return err; |
| 91 | emu = card->private_data; |
| 92 | |
| 93 | if (max_buffer_size[dev] < 32) |
| 94 | max_buffer_size[dev] = 32; |
| 95 | else if (max_buffer_size[dev] > 1024) |
| 96 | max_buffer_size[dev] = 1024; |
| 97 | err = snd_emu10k1_create(card, pci, extin_mask: extin[dev], extout_mask: extout[dev], |
| 98 | max_cache_bytes: (long)max_buffer_size[dev] * 1024 * 1024, |
| 99 | enable_ir: enable_ir[dev], subsystem: subsystem[dev]); |
| 100 | if (err < 0) |
| 101 | return err; |
| 102 | err = snd_emu10k1_pcm(emu, device: 0); |
| 103 | if (err < 0) |
| 104 | return err; |
| 105 | if (emu->card_capabilities->ac97_chip) { |
| 106 | err = snd_emu10k1_pcm_mic(emu, device: 1); |
| 107 | if (err < 0) |
| 108 | return err; |
| 109 | } |
| 110 | err = snd_emu10k1_pcm_efx(emu, device: 2); |
| 111 | if (err < 0) |
| 112 | return err; |
| 113 | /* This stores the periods table. */ |
| 114 | if (emu->card_capabilities->ca0151_chip) { /* P16V */ |
| 115 | emu->p16v_buffer = |
| 116 | snd_devm_alloc_pages(dev: &pci->dev, SNDRV_DMA_TYPE_DEV, size: 1024); |
| 117 | if (!emu->p16v_buffer) |
| 118 | return -ENOMEM; |
| 119 | } |
| 120 | |
| 121 | err = snd_emu10k1_mixer(emu, pcm_device: 0, multi_device: 3); |
| 122 | if (err < 0) |
| 123 | return err; |
| 124 | |
| 125 | err = snd_emu10k1_timer(emu, device: 0); |
| 126 | if (err < 0) |
| 127 | return err; |
| 128 | |
| 129 | err = snd_emu10k1_pcm_multi(emu, device: 3); |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | if (emu->card_capabilities->ca0151_chip) { /* P16V */ |
| 133 | err = snd_p16v_pcm(emu, device: 4); |
| 134 | if (err < 0) |
| 135 | return err; |
| 136 | } |
| 137 | if (emu->audigy) { |
| 138 | err = snd_emu10k1_audigy_midi(emu); |
| 139 | if (err < 0) |
| 140 | return err; |
| 141 | } else { |
| 142 | err = snd_emu10k1_midi(emu); |
| 143 | if (err < 0) |
| 144 | return err; |
| 145 | } |
| 146 | err = snd_emu10k1_fx8010_new(emu, device: 0); |
| 147 | if (err < 0) |
| 148 | return err; |
| 149 | #ifdef ENABLE_SYNTH |
| 150 | if (snd_seq_device_new(card, device: 1, SNDRV_SEQ_DEV_ID_EMU10K1_SYNTH, |
| 151 | argsize: sizeof(struct snd_emu10k1_synth_arg), result: &wave) < 0 || |
| 152 | wave == NULL) { |
| 153 | dev_warn(emu->card->dev, |
| 154 | "can't initialize Emu10k1 wavetable synth\n" ); |
| 155 | } else { |
| 156 | struct snd_emu10k1_synth_arg *arg; |
| 157 | arg = SNDRV_SEQ_DEVICE_ARGPTR(wave); |
| 158 | strscpy(wave->name, "Emu-10k1 Synth" ); |
| 159 | arg->hwptr = emu; |
| 160 | arg->index = 1; |
| 161 | arg->seq_ports = seq_ports[dev]; |
| 162 | arg->max_voices = max_synth_voices[dev]; |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | strscpy(card->driver, emu->card_capabilities->driver, |
| 167 | sizeof(card->driver)); |
| 168 | strscpy(card->shortname, emu->card_capabilities->name, |
| 169 | sizeof(card->shortname)); |
| 170 | snprintf(buf: card->longname, size: sizeof(card->longname), |
| 171 | fmt: "%s (rev.%d, serial:0x%x) at 0x%lx, irq %i" , |
| 172 | card->shortname, emu->revision, emu->serial, emu->port, emu->irq); |
| 173 | |
| 174 | err = snd_card_register(card); |
| 175 | if (err < 0) |
| 176 | return err; |
| 177 | |
| 178 | pci_set_drvdata(pdev: pci, data: card); |
| 179 | dev++; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | #ifdef CONFIG_PM_SLEEP |
| 184 | static int snd_emu10k1_suspend(struct device *dev) |
| 185 | { |
| 186 | struct snd_card *card = dev_get_drvdata(dev); |
| 187 | struct snd_emu10k1 *emu = card->private_data; |
| 188 | |
| 189 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
| 190 | |
| 191 | emu->suspend = 1; |
| 192 | |
| 193 | cancel_work_sync(work: &emu->emu1010.work); |
| 194 | |
| 195 | snd_ac97_suspend(ac97: emu->ac97); |
| 196 | |
| 197 | snd_emu10k1_efx_suspend(emu); |
| 198 | snd_emu10k1_suspend_regs(emu); |
| 199 | if (emu->card_capabilities->ca0151_chip) |
| 200 | snd_p16v_suspend(emu); |
| 201 | |
| 202 | snd_emu10k1_done(emu); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int snd_emu10k1_resume(struct device *dev) |
| 207 | { |
| 208 | struct snd_card *card = dev_get_drvdata(dev); |
| 209 | struct snd_emu10k1 *emu = card->private_data; |
| 210 | |
| 211 | snd_emu10k1_resume_init(emu); |
| 212 | snd_emu10k1_efx_resume(emu); |
| 213 | snd_ac97_resume(ac97: emu->ac97); |
| 214 | snd_emu10k1_resume_regs(emu); |
| 215 | |
| 216 | if (emu->card_capabilities->ca0151_chip) |
| 217 | snd_p16v_resume(emu); |
| 218 | |
| 219 | emu->suspend = 0; |
| 220 | |
| 221 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static SIMPLE_DEV_PM_OPS(snd_emu10k1_pm, snd_emu10k1_suspend, snd_emu10k1_resume); |
| 227 | #define SND_EMU10K1_PM_OPS &snd_emu10k1_pm |
| 228 | #else |
| 229 | #define SND_EMU10K1_PM_OPS NULL |
| 230 | #endif /* CONFIG_PM_SLEEP */ |
| 231 | |
| 232 | static struct pci_driver emu10k1_driver = { |
| 233 | .name = KBUILD_MODNAME, |
| 234 | .id_table = snd_emu10k1_ids, |
| 235 | .probe = snd_card_emu10k1_probe, |
| 236 | .driver = { |
| 237 | .pm = SND_EMU10K1_PM_OPS, |
| 238 | }, |
| 239 | }; |
| 240 | |
| 241 | module_pci_driver(emu10k1_driver); |
| 242 | |