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