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