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