xref: /linux/sound/isa/sb/sb16.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for SoundBlaster 16/AWE32/AWE64 soundcards
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6 
7 #include <asm/dma.h>
8 #include <linux/init.h>
9 #include <linux/pnp.h>
10 #include <linux/err.h>
11 #include <linux/isa.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/sb.h>
15 #include <sound/sb16_csp.h>
16 #include <sound/mpu401.h>
17 #include <sound/opl3.h>
18 #include <sound/emu8000.h>
19 #include <sound/seq_device.h>
20 #define SNDRV_LEGACY_FIND_FREE_IRQ
21 #define SNDRV_LEGACY_FIND_FREE_DMA
22 #include <sound/initval.h>
23 
24 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
25 MODULE_LICENSE("GPL");
26 #ifndef SNDRV_SBAWE
27 MODULE_DESCRIPTION("Sound Blaster 16");
28 #else
29 MODULE_DESCRIPTION("Sound Blaster AWE");
30 #endif
31 
32 #if 0
33 #define SNDRV_DEBUG_IRQ
34 #endif
35 
36 #if defined(SNDRV_SBAWE) && IS_ENABLED(CONFIG_SND_SEQUENCER)
37 #define SNDRV_SBAWE_EMU8000
38 #endif
39 
40 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
41 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
42 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
43 #ifdef CONFIG_PNP
44 static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
45 #endif
46 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260,0x280 */
47 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x330,0x300 */
48 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
49 #ifdef SNDRV_SBAWE_EMU8000
50 static long awe_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
51 #endif
52 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
53 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
54 static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 5,6,7 */
55 static int mic_agc[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
56 #ifdef CONFIG_SND_SB16_CSP
57 static int csp[SNDRV_CARDS];
58 #endif
59 #ifdef SNDRV_SBAWE_EMU8000
60 static int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
61 #endif
62 
63 module_param_array(index, int, NULL, 0444);
64 MODULE_PARM_DESC(index, "Index value for SoundBlaster 16 soundcard.");
65 module_param_array(id, charp, NULL, 0444);
66 MODULE_PARM_DESC(id, "ID string for SoundBlaster 16 soundcard.");
67 module_param_array(enable, bool, NULL, 0444);
68 MODULE_PARM_DESC(enable, "Enable SoundBlaster 16 soundcard.");
69 #ifdef CONFIG_PNP
70 module_param_array(isapnp, bool, NULL, 0444);
71 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
72 #endif
73 module_param_hw_array(port, long, ioport, NULL, 0444);
74 MODULE_PARM_DESC(port, "Port # for SB16 driver.");
75 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
76 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for SB16 driver.");
77 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
78 MODULE_PARM_DESC(fm_port, "FM port # for SB16 PnP driver.");
79 #ifdef SNDRV_SBAWE_EMU8000
80 module_param_hw_array(awe_port, long, ioport, NULL, 0444);
81 MODULE_PARM_DESC(awe_port, "AWE port # for SB16 PnP driver.");
82 #endif
83 module_param_hw_array(irq, int, irq, NULL, 0444);
84 MODULE_PARM_DESC(irq, "IRQ # for SB16 driver.");
85 module_param_hw_array(dma8, int, dma, NULL, 0444);
86 MODULE_PARM_DESC(dma8, "8-bit DMA # for SB16 driver.");
87 module_param_hw_array(dma16, int, dma, NULL, 0444);
88 MODULE_PARM_DESC(dma16, "16-bit DMA # for SB16 driver.");
89 module_param_array(mic_agc, int, NULL, 0444);
90 MODULE_PARM_DESC(mic_agc, "Mic Auto-Gain-Control switch.");
91 #ifdef CONFIG_SND_SB16_CSP
92 module_param_array(csp, int, NULL, 0444);
93 MODULE_PARM_DESC(csp, "ASP/CSP chip support.");
94 #endif
95 #ifdef SNDRV_SBAWE_EMU8000
96 module_param_array(seq_ports, int, NULL, 0444);
97 MODULE_PARM_DESC(seq_ports, "Number of sequencer ports for WaveTable synth.");
98 #endif
99 
100 #ifdef CONFIG_PNP
101 static int isa_registered;
102 static int pnp_registered;
103 #endif
104 
105 struct snd_card_sb16 {
106 	struct resource *fm_res;	/* used to block FM i/o region for legacy cards */
107 	struct snd_sb *chip;
108 #ifdef CONFIG_PNP
109 	int dev_no;
110 	struct pnp_dev *dev;
111 #ifdef SNDRV_SBAWE_EMU8000
112 	struct pnp_dev *devwt;
113 #endif
114 #endif
115 };
116 
117 #ifdef CONFIG_PNP
118 
119 static const struct pnp_card_device_id snd_sb16_pnpids[] = {
120 #ifndef SNDRV_SBAWE
121 	/* Sound Blaster 16 PnP */
122 	{ .id = "CTL0024", .devs = { { "CTL0031" } } },
123 	/* Sound Blaster 16 PnP */
124 	{ .id = "CTL0025", .devs = { { "CTL0031" } } },
125 	/* Sound Blaster 16 PnP */
126 	{ .id = "CTL0026", .devs = { { "CTL0031" } } },
127 	/* Sound Blaster 16 PnP */
128 	{ .id = "CTL0027", .devs = { { "CTL0031" } } },
129 	/* Sound Blaster 16 PnP */
130 	{ .id = "CTL0028", .devs = { { "CTL0031" } } },
131 	/* Sound Blaster 16 PnP */
132 	{ .id = "CTL0029", .devs = { { "CTL0031" } } },
133 	/* Sound Blaster 16 PnP */
134 	{ .id = "CTL002a", .devs = { { "CTL0031" } } },
135 	/* Sound Blaster 16 PnP */
136 	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
137 	{ .id = "CTL002b", .devs = { { "CTL0031" } } },
138 	/* Sound Blaster 16 PnP */
139 	{ .id = "CTL002c", .devs = { { "CTL0031" } } },
140 	/* Sound Blaster Vibra16S */
141 	{ .id = "CTL0051", .devs = { { "CTL0001" } } },
142 	/* Sound Blaster Vibra16C */
143 	{ .id = "CTL0070", .devs = { { "CTL0001" } } },
144 	/* Sound Blaster Vibra16CL - added by ctm@ardi.com */
145 	{ .id = "CTL0080", .devs = { { "CTL0041" } } },
146 	/* Sound Blaster 16 'value' PnP. It says model ct4130 on the pcb, */
147 	/* but ct4131 on a sticker on the board.. */
148 	{ .id = "CTL0086", .devs = { { "CTL0041" } } },
149 	/* Sound Blaster Vibra16X */
150 	{ .id = "CTL00f0", .devs = { { "CTL0043" } } },
151 	/* Sound Blaster 16 (Virtual PC 2004) */
152 	{ .id = "tBA03b0", .devs = { {.id="PNPb003" } } },
153 #else  /* SNDRV_SBAWE defined */
154 	/* Sound Blaster AWE 32 PnP */
155 	{ .id = "CTL0035", .devs = { { "CTL0031" }, { "CTL0021" } } },
156 	/* Sound Blaster AWE 32 PnP */
157 	{ .id = "CTL0039", .devs = { { "CTL0031" }, { "CTL0021" } } },
158 	/* Sound Blaster AWE 32 PnP */
159 	{ .id = "CTL0042", .devs = { { "CTL0031" }, { "CTL0021" } } },
160 	/* Sound Blaster AWE 32 PnP */
161 	{ .id = "CTL0043", .devs = { { "CTL0031" }, { "CTL0021" } } },
162 	/* Sound Blaster AWE 32 PnP */
163 	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
164 	{ .id = "CTL0044", .devs = { { "CTL0031" }, { "CTL0021" } } },
165 	/* Sound Blaster AWE 32 PnP */
166 	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
167 	{ .id = "CTL0045", .devs = { { "CTL0031" }, { "CTL0021" } } },
168 	/* Sound Blaster AWE 32 PnP */
169 	{ .id = "CTL0046", .devs = { { "CTL0031" }, { "CTL0021" } } },
170 	/* Sound Blaster AWE 32 PnP */
171 	{ .id = "CTL0047", .devs = { { "CTL0031" }, { "CTL0021" } } },
172 	/* Sound Blaster AWE 32 PnP */
173 	{ .id = "CTL0048", .devs = { { "CTL0031" }, { "CTL0021" } } },
174 	/* Sound Blaster AWE 32 PnP */
175 	{ .id = "CTL0054", .devs = { { "CTL0031" }, { "CTL0021" } } },
176 	/* Sound Blaster AWE 32 PnP */
177 	{ .id = "CTL009a", .devs = { { "CTL0041" }, { "CTL0021" } } },
178 	/* Sound Blaster AWE 32 PnP */
179 	{ .id = "CTL009c", .devs = { { "CTL0041" }, { "CTL0021" } } },
180 	/* Sound Blaster 32 PnP */
181 	{ .id = "CTL009f", .devs = { { "CTL0041" }, { "CTL0021" } } },
182 	/* Sound Blaster AWE 64 PnP */
183 	{ .id = "CTL009d", .devs = { { "CTL0042" }, { "CTL0022" } } },
184 	/* Sound Blaster AWE 64 PnP Gold */
185 	{ .id = "CTL009e", .devs = { { "CTL0044" }, { "CTL0023" } } },
186 	/* Sound Blaster AWE 64 PnP Gold */
187 	{ .id = "CTL00b2", .devs = { { "CTL0044" }, { "CTL0023" } } },
188 	/* Sound Blaster AWE 64 PnP */
189 	{ .id = "CTL00c1", .devs = { { "CTL0042" }, { "CTL0022" } } },
190 	/* Sound Blaster AWE 64 PnP */
191 	{ .id = "CTL00c3", .devs = { { "CTL0045" }, { "CTL0022" } } },
192 	/* Sound Blaster AWE 64 PnP */
193 	{ .id = "CTL00c5", .devs = { { "CTL0045" }, { "CTL0022" } } },
194 	/* Sound Blaster AWE 64 PnP */
195 	{ .id = "CTL00c7", .devs = { { "CTL0045" }, { "CTL0022" } } },
196 	/* Sound Blaster AWE 64 PnP */
197 	{ .id = "CTL00e4", .devs = { { "CTL0045" }, { "CTL0022" } } },
198 	/* Sound Blaster AWE 64 PnP */
199 	{ .id = "CTL00e9", .devs = { { "CTL0045" }, { "CTL0022" } } },
200 	/* Sound Blaster 16 PnP (AWE) */
201 	{ .id = "CTL00ed", .devs = { { "CTL0041" }, { "CTL0070" } } },
202 	/* Generic entries */
203 	{ .id = "CTLXXXX" , .devs = { { "CTL0031" }, { "CTL0021" } } },
204 	{ .id = "CTLXXXX" , .devs = { { "CTL0041" }, { "CTL0021" } } },
205 	{ .id = "CTLXXXX" , .devs = { { "CTL0042" }, { "CTL0022" } } },
206 	{ .id = "CTLXXXX" , .devs = { { "CTL0044" }, { "CTL0023" } } },
207 	{ .id = "CTLXXXX" , .devs = { { "CTL0045" }, { "CTL0022" } } },
208 #endif /* SNDRV_SBAWE */
209 	{ .id = "", }
210 };
211 
212 MODULE_DEVICE_TABLE(pnp_card, snd_sb16_pnpids);
213 
214 #endif /* CONFIG_PNP */
215 
216 #ifdef SNDRV_SBAWE_EMU8000
217 #define DRIVER_NAME	"snd-card-sbawe"
218 #else
219 #define DRIVER_NAME	"snd-card-sb16"
220 #endif
221 
222 #ifdef CONFIG_PNP
223 
snd_card_sb16_pnp(int dev,struct snd_card_sb16 * acard,struct pnp_card_link * card,const struct pnp_card_device_id * id)224 static int snd_card_sb16_pnp(int dev, struct snd_card_sb16 *acard,
225 			     struct pnp_card_link *card,
226 			     const struct pnp_card_device_id *id)
227 {
228 	struct pnp_dev *pdev;
229 	int err;
230 
231 	acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
232 	if (acard->dev == NULL)
233 		return -ENODEV;
234 
235 #ifdef SNDRV_SBAWE_EMU8000
236 	acard->devwt = pnp_request_card_device(card, id->devs[1].id, acard->dev);
237 #endif
238 	/* Audio initialization */
239 	pdev = acard->dev;
240 
241 	err = pnp_activate_dev(pdev);
242 	if (err < 0) {
243 		dev_err(&pdev->dev, "AUDIO pnp configure failure\n");
244 		return err;
245 	}
246 	port[dev] = pnp_port_start(pdev, 0);
247 	mpu_port[dev] = pnp_port_start(pdev, 1);
248 	fm_port[dev] = pnp_port_start(pdev, 2);
249 	dma8[dev] = pnp_dma(pdev, 0);
250 	dma16[dev] = pnp_dma(pdev, 1);
251 	irq[dev] = pnp_irq(pdev, 0);
252 	dev_dbg(&pdev->dev, "pnp SB16: port=0x%lx, mpu port=0x%lx, fm port=0x%lx\n",
253 		port[dev], mpu_port[dev], fm_port[dev]);
254 	dev_dbg(&pdev->dev, "pnp SB16: dma1=%i, dma2=%i, irq=%i\n",
255 		dma8[dev], dma16[dev], irq[dev]);
256 #ifdef SNDRV_SBAWE_EMU8000
257 	/* WaveTable initialization */
258 	pdev = acard->devwt;
259 	if (pdev != NULL) {
260 		err = pnp_activate_dev(pdev);
261 		if (err < 0) {
262 			goto __wt_error;
263 		}
264 		awe_port[dev] = pnp_port_start(pdev, 0);
265 		dev_dbg(&pdev->dev, "pnp SB16: wavetable port=0x%llx\n",
266 			(unsigned long long)pnp_port_start(pdev, 0));
267 	} else {
268 __wt_error:
269 		if (pdev) {
270 			pnp_release_card_device(pdev);
271 			dev_err(&pdev->dev, "WaveTable pnp configure failure\n");
272 		}
273 		acard->devwt = NULL;
274 		awe_port[dev] = -1;
275 	}
276 #endif
277 	return 0;
278 }
279 
280 #endif /* CONFIG_PNP */
281 
282 #ifdef CONFIG_PNP
283 #define is_isapnp_selected(dev)		isapnp[dev]
284 #else
285 #define is_isapnp_selected(dev)		0
286 #endif
287 
snd_sb16_card_new(struct device * devptr,int dev,struct snd_card ** cardp)288 static int snd_sb16_card_new(struct device *devptr, int dev,
289 			     struct snd_card **cardp)
290 {
291 	struct snd_card *card;
292 	int err;
293 
294 	err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE,
295 				sizeof(struct snd_card_sb16), &card);
296 	if (err < 0)
297 		return err;
298 	*cardp = card;
299 	return 0;
300 }
301 
snd_sb16_probe(struct snd_card * card,int dev)302 static int snd_sb16_probe(struct snd_card *card, int dev)
303 {
304 	int xirq, xdma8, xdma16;
305 	struct snd_sb *chip;
306 	struct snd_card_sb16 *acard = card->private_data;
307 	struct snd_opl3 *opl3;
308 	struct snd_hwdep *synth = NULL;
309 #ifdef CONFIG_SND_SB16_CSP
310 	struct snd_hwdep *xcsp = NULL;
311 #endif
312 	unsigned long flags;
313 	int err;
314 
315 	xirq = irq[dev];
316 	xdma8 = dma8[dev];
317 	xdma16 = dma16[dev];
318 
319 	err = snd_sbdsp_create(card, port[dev], xirq, snd_sb16dsp_interrupt,
320 			       xdma8, xdma16, SB_HW_AUTO, &chip);
321 	if (err < 0)
322 		return err;
323 
324 	acard->chip = chip;
325 	if (chip->hardware != SB_HW_16) {
326 		dev_err(card->dev, "SB 16 chip was not detected at 0x%lx\n", port[dev]);
327 		return -ENODEV;
328 	}
329 	chip->mpu_port = mpu_port[dev];
330 	if (!is_isapnp_selected(dev)) {
331 		err = snd_sb16dsp_configure(chip);
332 		if (err < 0)
333 			return err;
334 	}
335 
336 	err = snd_sb16dsp_pcm(chip, 0);
337 	if (err < 0)
338 		return err;
339 
340 	strcpy(card->driver,
341 #ifdef SNDRV_SBAWE_EMU8000
342 			awe_port[dev] > 0 ? "SB AWE" :
343 #endif
344 			"SB16");
345 	strcpy(card->shortname, chip->name);
346 	sprintf(card->longname, "%s at 0x%lx, irq %i, dma ",
347 		chip->name,
348 		chip->port,
349 		xirq);
350 	if (xdma8 >= 0)
351 		sprintf(card->longname + strlen(card->longname), "%d", xdma8);
352 	if (xdma16 >= 0)
353 		sprintf(card->longname + strlen(card->longname), "%s%d",
354 			xdma8 >= 0 ? "&" : "", xdma16);
355 
356 	if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
357 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_SB,
358 					  chip->mpu_port,
359 					  MPU401_INFO_IRQ_HOOK, -1,
360 					  &chip->rmidi);
361 		if (err < 0)
362 			return err;
363 		chip->rmidi_callback = snd_mpu401_uart_interrupt;
364 	}
365 
366 #ifdef SNDRV_SBAWE_EMU8000
367 	if (awe_port[dev] == SNDRV_AUTO_PORT)
368 		awe_port[dev] = 0; /* disable */
369 #endif
370 
371 	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
372 		if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
373 				    OPL3_HW_OPL3,
374 				    acard->fm_res != NULL || fm_port[dev] == port[dev],
375 				    &opl3) < 0) {
376 			dev_err(card->dev, "no OPL device at 0x%lx-0x%lx\n",
377 				fm_port[dev], fm_port[dev] + 2);
378 		} else {
379 #ifdef SNDRV_SBAWE_EMU8000
380 			int seqdev = awe_port[dev] > 0 ? 2 : 1;
381 #else
382 			int seqdev = 1;
383 #endif
384 			err = snd_opl3_hwdep_new(opl3, 0, seqdev, &synth);
385 			if (err < 0)
386 				return err;
387 		}
388 	}
389 
390 	err = snd_sbmixer_new(chip);
391 	if (err < 0)
392 		return err;
393 
394 #ifdef CONFIG_SND_SB16_CSP
395 	/* CSP chip on SB16ASP/AWE32 */
396 	if ((chip->hardware == SB_HW_16) && csp[dev]) {
397 		snd_sb_csp_new(chip, synth != NULL ? 1 : 0, &xcsp);
398 		if (xcsp) {
399 			chip->csp = xcsp->private_data;
400 			chip->hardware = SB_HW_16CSP;
401 		} else {
402 			dev_info(card->dev,
403 				 "warning - CSP chip not detected on soundcard #%i\n",
404 				 dev + 1);
405 		}
406 	}
407 #endif
408 #ifdef SNDRV_SBAWE_EMU8000
409 	if (awe_port[dev] > 0) {
410 		err = snd_emu8000_new(card, 1, awe_port[dev],
411 				      seq_ports[dev], NULL);
412 		if (err < 0) {
413 			dev_err(card->dev,
414 				"fatal error - EMU-8000 synthesizer not detected at 0x%lx\n",
415 				awe_port[dev]);
416 
417 			return err;
418 		}
419 	}
420 #endif
421 
422 	/* setup Mic AGC */
423 	spin_lock_irqsave(&chip->mixer_lock, flags);
424 	snd_sbmixer_write(chip, SB_DSP4_MIC_AGC,
425 		(snd_sbmixer_read(chip, SB_DSP4_MIC_AGC) & 0x01) |
426 		(mic_agc[dev] ? 0x00 : 0x01));
427 	spin_unlock_irqrestore(&chip->mixer_lock, flags);
428 
429 	err = snd_card_register(card);
430 	if (err < 0)
431 		return err;
432 
433 	return 0;
434 }
435 
436 #ifdef CONFIG_PM
snd_sb16_suspend(struct snd_card * card,pm_message_t state)437 static int snd_sb16_suspend(struct snd_card *card, pm_message_t state)
438 {
439 	struct snd_card_sb16 *acard = card->private_data;
440 	struct snd_sb *chip = acard->chip;
441 
442 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
443 	snd_sbmixer_suspend(chip);
444 	return 0;
445 }
446 
snd_sb16_resume(struct snd_card * card)447 static int snd_sb16_resume(struct snd_card *card)
448 {
449 	struct snd_card_sb16 *acard = card->private_data;
450 	struct snd_sb *chip = acard->chip;
451 
452 	snd_sbdsp_reset(chip);
453 	snd_sbmixer_resume(chip);
454 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
455 	return 0;
456 }
457 #endif
458 
snd_sb16_isa_probe1(int dev,struct device * pdev)459 static int snd_sb16_isa_probe1(int dev, struct device *pdev)
460 {
461 	struct snd_card_sb16 *acard;
462 	struct snd_card *card;
463 	int err;
464 
465 	err = snd_sb16_card_new(pdev, dev, &card);
466 	if (err < 0)
467 		return err;
468 
469 	acard = card->private_data;
470 	/* non-PnP FM port address is hardwired with base port address */
471 	fm_port[dev] = port[dev];
472 	/* block the 0x388 port to avoid PnP conflicts */
473 	acard->fm_res = devm_request_region(card->dev, 0x388, 4,
474 					    "SoundBlaster FM");
475 #ifdef SNDRV_SBAWE_EMU8000
476 	/* non-PnP AWE port address is hardwired with base port address */
477 	awe_port[dev] = port[dev] + 0x400;
478 #endif
479 
480 	err = snd_sb16_probe(card, dev);
481 	if (err < 0)
482 		return err;
483 	dev_set_drvdata(pdev, card);
484 	return 0;
485 }
486 
487 
snd_sb16_isa_match(struct device * pdev,unsigned int dev)488 static int snd_sb16_isa_match(struct device *pdev, unsigned int dev)
489 {
490 	return enable[dev] && !is_isapnp_selected(dev);
491 }
492 
snd_sb16_isa_probe(struct device * pdev,unsigned int dev)493 static int snd_sb16_isa_probe(struct device *pdev, unsigned int dev)
494 {
495 	int err;
496 	static const int possible_irqs[] = {5, 9, 10, 7, -1};
497 	static const int possible_dmas8[] = {1, 3, 0, -1};
498 	static const int possible_dmas16[] = {5, 6, 7, -1};
499 
500 	if (irq[dev] == SNDRV_AUTO_IRQ) {
501 		irq[dev] = snd_legacy_find_free_irq(possible_irqs);
502 		if (irq[dev] < 0) {
503 			dev_err(pdev, "unable to find a free IRQ\n");
504 			return -EBUSY;
505 		}
506 	}
507 	if (dma8[dev] == SNDRV_AUTO_DMA) {
508 		dma8[dev] = snd_legacy_find_free_dma(possible_dmas8);
509 		if (dma8[dev] < 0) {
510 			dev_err(pdev, "unable to find a free 8-bit DMA\n");
511 			return -EBUSY;
512 		}
513 	}
514 	if (dma16[dev] == SNDRV_AUTO_DMA) {
515 		dma16[dev] = snd_legacy_find_free_dma(possible_dmas16);
516 		if (dma16[dev] < 0) {
517 			dev_err(pdev, "unable to find a free 16-bit DMA\n");
518 			return -EBUSY;
519 		}
520 	}
521 
522 	if (port[dev] != SNDRV_AUTO_PORT)
523 		return snd_sb16_isa_probe1(dev, pdev);
524 	else {
525 		static const int possible_ports[] = {0x220, 0x240, 0x260, 0x280};
526 		int i;
527 		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
528 			port[dev] = possible_ports[i];
529 			err = snd_sb16_isa_probe1(dev, pdev);
530 			if (! err)
531 				return 0;
532 		}
533 		return err;
534 	}
535 }
536 
537 #ifdef CONFIG_PM
snd_sb16_isa_suspend(struct device * dev,unsigned int n,pm_message_t state)538 static int snd_sb16_isa_suspend(struct device *dev, unsigned int n,
539 				pm_message_t state)
540 {
541 	return snd_sb16_suspend(dev_get_drvdata(dev), state);
542 }
543 
snd_sb16_isa_resume(struct device * dev,unsigned int n)544 static int snd_sb16_isa_resume(struct device *dev, unsigned int n)
545 {
546 	return snd_sb16_resume(dev_get_drvdata(dev));
547 }
548 #endif
549 
550 #ifdef SNDRV_SBAWE
551 #define DEV_NAME "sbawe"
552 #else
553 #define DEV_NAME "sb16"
554 #endif
555 
556 static struct isa_driver snd_sb16_isa_driver = {
557 	.match		= snd_sb16_isa_match,
558 	.probe		= snd_sb16_isa_probe,
559 #ifdef CONFIG_PM
560 	.suspend	= snd_sb16_isa_suspend,
561 	.resume		= snd_sb16_isa_resume,
562 #endif
563 	.driver		= {
564 		.name	= DEV_NAME
565 	},
566 };
567 
568 
569 #ifdef CONFIG_PNP
snd_sb16_pnp_detect(struct pnp_card_link * pcard,const struct pnp_card_device_id * pid)570 static int snd_sb16_pnp_detect(struct pnp_card_link *pcard,
571 			       const struct pnp_card_device_id *pid)
572 {
573 	static int dev;
574 	struct snd_card *card;
575 	int res;
576 
577 	for ( ; dev < SNDRV_CARDS; dev++) {
578 		if (!enable[dev] || !isapnp[dev])
579 			continue;
580 		res = snd_sb16_card_new(&pcard->card->dev, dev, &card);
581 		if (res < 0)
582 			return res;
583 		res = snd_card_sb16_pnp(dev, card->private_data, pcard, pid);
584 		if (res < 0)
585 			return res;
586 		res = snd_sb16_probe(card, dev);
587 		if (res < 0)
588 			return res;
589 		pnp_set_card_drvdata(pcard, card);
590 		dev++;
591 		return 0;
592 	}
593 
594 	return -ENODEV;
595 }
596 
597 #ifdef CONFIG_PM
snd_sb16_pnp_suspend(struct pnp_card_link * pcard,pm_message_t state)598 static int snd_sb16_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
599 {
600 	return snd_sb16_suspend(pnp_get_card_drvdata(pcard), state);
601 }
snd_sb16_pnp_resume(struct pnp_card_link * pcard)602 static int snd_sb16_pnp_resume(struct pnp_card_link *pcard)
603 {
604 	return snd_sb16_resume(pnp_get_card_drvdata(pcard));
605 }
606 #endif
607 
608 static struct pnp_card_driver sb16_pnpc_driver = {
609 	.flags = PNP_DRIVER_RES_DISABLE,
610 #ifdef SNDRV_SBAWE
611 	.name = "sbawe",
612 #else
613 	.name = "sb16",
614 #endif
615 	.id_table = snd_sb16_pnpids,
616 	.probe = snd_sb16_pnp_detect,
617 #ifdef CONFIG_PM
618 	.suspend = snd_sb16_pnp_suspend,
619 	.resume = snd_sb16_pnp_resume,
620 #endif
621 };
622 
623 #endif /* CONFIG_PNP */
624 
alsa_card_sb16_init(void)625 static int __init alsa_card_sb16_init(void)
626 {
627 	int err;
628 
629 	err = isa_register_driver(&snd_sb16_isa_driver, SNDRV_CARDS);
630 #ifdef CONFIG_PNP
631 	if (!err)
632 		isa_registered = 1;
633 
634 	err = pnp_register_card_driver(&sb16_pnpc_driver);
635 	if (!err)
636 		pnp_registered = 1;
637 
638 	if (isa_registered)
639 		err = 0;
640 #endif
641 	return err;
642 }
643 
alsa_card_sb16_exit(void)644 static void __exit alsa_card_sb16_exit(void)
645 {
646 #ifdef CONFIG_PNP
647 	if (pnp_registered)
648 		pnp_unregister_card_driver(&sb16_pnpc_driver);
649 	if (isa_registered)
650 #endif
651 		isa_unregister_driver(&snd_sb16_isa_driver);
652 }
653 
654 module_init(alsa_card_sb16_init)
655 module_exit(alsa_card_sb16_exit)
656