1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for SoundBlaster 1.0/2.0/Pro soundcards and compatible
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
6
7 #include <linux/init.h>
8 #include <linux/err.h>
9 #include <linux/isa.h>
10 #include <linux/ioport.h>
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include <sound/core.h>
14 #include <sound/sb.h>
15 #include <sound/opl3.h>
16 #include <sound/initval.h>
17
18 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
19 MODULE_DESCRIPTION("Sound Blaster 1.0/2.0/Pro");
20 MODULE_LICENSE("GPL");
21
22 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
23 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
24 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
25 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
26 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
27 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3 */
28
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for Sound Blaster soundcard.");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for Sound Blaster soundcard.");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable Sound Blaster soundcard.");
35 module_param_hw_array(port, long, ioport, NULL, 0444);
36 MODULE_PARM_DESC(port, "Port # for SB8 driver.");
37 module_param_hw_array(irq, int, irq, NULL, 0444);
38 MODULE_PARM_DESC(irq, "IRQ # for SB8 driver.");
39 module_param_hw_array(dma8, int, dma, NULL, 0444);
40 MODULE_PARM_DESC(dma8, "8-bit DMA # for SB8 driver.");
41
42 struct snd_sb8 {
43 struct resource *fm_res; /* used to block FM i/o region for legacy cards */
44 struct snd_sb *chip;
45 };
46
snd_sb8_interrupt(int irq,void * dev_id)47 static irqreturn_t snd_sb8_interrupt(int irq, void *dev_id)
48 {
49 struct snd_sb *chip = dev_id;
50
51 if (chip->open & SB_OPEN_PCM) {
52 return snd_sb8dsp_interrupt(chip);
53 } else {
54 return snd_sb8dsp_midi_interrupt(chip);
55 }
56 }
57
snd_sb8_match(struct device * pdev,unsigned int dev)58 static int snd_sb8_match(struct device *pdev, unsigned int dev)
59 {
60 if (!enable[dev])
61 return 0;
62 if (irq[dev] == SNDRV_AUTO_IRQ) {
63 dev_err(pdev, "please specify irq\n");
64 return 0;
65 }
66 if (dma8[dev] == SNDRV_AUTO_DMA) {
67 dev_err(pdev, "please specify dma8\n");
68 return 0;
69 }
70 return 1;
71 }
72
snd_sb8_probe(struct device * pdev,unsigned int dev)73 static int snd_sb8_probe(struct device *pdev, unsigned int dev)
74 {
75 struct snd_sb *chip;
76 struct snd_card *card;
77 struct snd_sb8 *acard;
78 struct snd_opl3 *opl3;
79 int err;
80
81 err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
82 sizeof(struct snd_sb8), &card);
83 if (err < 0)
84 return err;
85 acard = card->private_data;
86
87 /*
88 * Block the 0x388 port to avoid PnP conflicts.
89 * No need to check this value after request_region,
90 * as we never do anything with it.
91 */
92 acard->fm_res = devm_request_region(card->dev, 0x388, 4,
93 "SoundBlaster FM");
94
95 if (port[dev] != SNDRV_AUTO_PORT) {
96 err = snd_sbdsp_create(card, port[dev], irq[dev],
97 snd_sb8_interrupt, dma8[dev],
98 -1, SB_HW_AUTO, &chip);
99 if (err < 0)
100 return err;
101 } else {
102 /* auto-probe legacy ports */
103 static const unsigned long possible_ports[] = {
104 0x220, 0x240, 0x260,
105 };
106 int i;
107 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
108 err = snd_sbdsp_create(card, possible_ports[i],
109 irq[dev],
110 snd_sb8_interrupt,
111 dma8[dev],
112 -1,
113 SB_HW_AUTO,
114 &chip);
115 if (err >= 0) {
116 port[dev] = possible_ports[i];
117 break;
118 }
119 }
120 if (i >= ARRAY_SIZE(possible_ports))
121 return -EINVAL;
122 }
123 acard->chip = chip;
124
125 if (chip->hardware >= SB_HW_16) {
126 if (chip->hardware == SB_HW_ALS100)
127 dev_warn(pdev, "ALS100 chip detected at 0x%lx, try snd-als100 module\n",
128 port[dev]);
129 else
130 dev_warn(pdev, "SB 16 chip detected at 0x%lx, try snd-sb16 module\n",
131 port[dev]);
132 return -ENODEV;
133 }
134
135 err = snd_sb8dsp_pcm(chip, 0);
136 if (err < 0)
137 return err;
138
139 err = snd_sbmixer_new(chip);
140 if (err < 0)
141 return err;
142
143 if (chip->hardware == SB_HW_10 || chip->hardware == SB_HW_20) {
144 err = snd_opl3_create(card, chip->port + 8, 0,
145 OPL3_HW_AUTO, 1, &opl3);
146 if (err < 0)
147 dev_warn(pdev, "sb8: no OPL device at 0x%lx\n", chip->port + 8);
148 } else {
149 err = snd_opl3_create(card, chip->port, chip->port + 2,
150 OPL3_HW_AUTO, 1, &opl3);
151 if (err < 0) {
152 dev_warn(pdev, "sb8: no OPL device at 0x%lx-0x%lx\n",
153 chip->port, chip->port + 2);
154 }
155 }
156 if (err >= 0) {
157 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
158 if (err < 0)
159 return err;
160 }
161
162 err = snd_sb8dsp_midi(chip, 0);
163 if (err < 0)
164 return err;
165
166 strscpy(card->driver, chip->hardware == SB_HW_PRO ? "SB Pro" : "SB8");
167 strscpy(card->shortname, chip->name);
168 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
169 chip->name,
170 chip->port,
171 irq[dev], dma8[dev]);
172
173 err = snd_card_register(card);
174 if (err < 0)
175 return err;
176
177 dev_set_drvdata(pdev, card);
178 return 0;
179 }
180
181 #ifdef CONFIG_PM
snd_sb8_suspend(struct device * dev,unsigned int n,pm_message_t state)182 static int snd_sb8_suspend(struct device *dev, unsigned int n,
183 pm_message_t state)
184 {
185 struct snd_card *card = dev_get_drvdata(dev);
186 struct snd_sb8 *acard = card->private_data;
187 struct snd_sb *chip = acard->chip;
188
189 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
190 snd_sbmixer_suspend(chip);
191 return 0;
192 }
193
snd_sb8_resume(struct device * dev,unsigned int n)194 static int snd_sb8_resume(struct device *dev, unsigned int n)
195 {
196 struct snd_card *card = dev_get_drvdata(dev);
197 struct snd_sb8 *acard = card->private_data;
198 struct snd_sb *chip = acard->chip;
199
200 snd_sbdsp_reset(chip);
201 snd_sbmixer_resume(chip);
202 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
203 return 0;
204 }
205 #endif
206
207 #define DEV_NAME "sb8"
208
209 static struct isa_driver snd_sb8_driver = {
210 .match = snd_sb8_match,
211 .probe = snd_sb8_probe,
212 #ifdef CONFIG_PM
213 .suspend = snd_sb8_suspend,
214 .resume = snd_sb8_resume,
215 #endif
216 .driver = {
217 .name = DEV_NAME
218 },
219 };
220
221 module_isa_driver(snd_sb8_driver, SNDRV_CARDS);
222