sb16.c 19.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Driver for SoundBlaster 16/AWE32/AWE64 soundcards
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <asm/dma.h>
#include <linux/init.h>
#include <linux/pnp.h>
25
#include <linux/err.h>
26
#include <linux/isa.h>
27
#include <linux/module.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <sound/core.h>
#include <sound/sb.h>
#include <sound/sb16_csp.h>
#include <sound/mpu401.h>
#include <sound/opl3.h>
#include <sound/emu8000.h>
#include <sound/seq_device.h>
#define SNDRV_LEGACY_FIND_FREE_IRQ
#define SNDRV_LEGACY_FIND_FREE_DMA
#include <sound/initval.h>

#ifdef SNDRV_SBAWE
#define PFX "sbawe: "
#else
#define PFX "sb16: "
#endif

45
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
MODULE_LICENSE("GPL");
#ifndef SNDRV_SBAWE
MODULE_DESCRIPTION("Sound Blaster 16");
MODULE_SUPPORTED_DEVICE("{{Creative Labs,SB 16},"
		"{Creative Labs,SB Vibra16S},"
		"{Creative Labs,SB Vibra16C},"
		"{Creative Labs,SB Vibra16CL},"
		"{Creative Labs,SB Vibra16X}}");
#else
MODULE_DESCRIPTION("Sound Blaster AWE");
MODULE_SUPPORTED_DEVICE("{{Creative Labs,SB AWE 32},"
		"{Creative Labs,SB AWE 64},"
		"{Creative Labs,SB AWE 64 Gold}}");
#endif

#if 0
#define SNDRV_DEBUG_IRQ
#endif

T
Takashi Iwai 已提交
65
#if defined(SNDRV_SBAWE) && IS_REACHABLE(CONFIG_SND_SEQUENCER)
L
Linus Torvalds 已提交
66 67 68 69 70
#define SNDRV_SBAWE_EMU8000
#endif

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
71
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
L
Linus Torvalds 已提交
72
#ifdef CONFIG_PNP
73
static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85
#endif
static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260,0x280 */
static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x330,0x300 */
static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
#ifdef SNDRV_SBAWE_EMU8000
static long awe_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
#endif
static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 5,6,7 */
static int mic_agc[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
#ifdef CONFIG_SND_SB16_CSP
86
static int csp[SNDRV_CARDS];
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#endif
#ifdef SNDRV_SBAWE_EMU8000
static int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
#endif

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for SoundBlaster 16 soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for SoundBlaster 16 soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable SoundBlaster 16 soundcard.");
#ifdef CONFIG_PNP
module_param_array(isapnp, bool, NULL, 0444);
MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
#endif
102
module_param_hw_array(port, long, ioport, NULL, 0444);
L
Linus Torvalds 已提交
103
MODULE_PARM_DESC(port, "Port # for SB16 driver.");
104
module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
L
Linus Torvalds 已提交
105
MODULE_PARM_DESC(mpu_port, "MPU-401 port # for SB16 driver.");
106
module_param_hw_array(fm_port, long, ioport, NULL, 0444);
L
Linus Torvalds 已提交
107 108
MODULE_PARM_DESC(fm_port, "FM port # for SB16 PnP driver.");
#ifdef SNDRV_SBAWE_EMU8000
109
module_param_hw_array(awe_port, long, ioport, NULL, 0444);
L
Linus Torvalds 已提交
110 111
MODULE_PARM_DESC(awe_port, "AWE port # for SB16 PnP driver.");
#endif
112
module_param_hw_array(irq, int, irq, NULL, 0444);
L
Linus Torvalds 已提交
113
MODULE_PARM_DESC(irq, "IRQ # for SB16 driver.");
114
module_param_hw_array(dma8, int, dma, NULL, 0444);
L
Linus Torvalds 已提交
115
MODULE_PARM_DESC(dma8, "8-bit DMA # for SB16 driver.");
116
module_param_hw_array(dma16, int, dma, NULL, 0444);
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128
MODULE_PARM_DESC(dma16, "16-bit DMA # for SB16 driver.");
module_param_array(mic_agc, int, NULL, 0444);
MODULE_PARM_DESC(mic_agc, "Mic Auto-Gain-Control switch.");
#ifdef CONFIG_SND_SB16_CSP
module_param_array(csp, int, NULL, 0444);
MODULE_PARM_DESC(csp, "ASP/CSP chip support.");
#endif
#ifdef SNDRV_SBAWE_EMU8000
module_param_array(seq_ports, int, NULL, 0444);
MODULE_PARM_DESC(seq_ports, "Number of sequencer ports for WaveTable synth.");
#endif

129
#ifdef CONFIG_PNP
130
static int isa_registered;
131 132 133
static int pnp_registered;
#endif

L
Linus Torvalds 已提交
134 135
struct snd_card_sb16 {
	struct resource *fm_res;	/* used to block FM i/o region for legacy cards */
136
	struct snd_sb *chip;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#ifdef CONFIG_PNP
	int dev_no;
	struct pnp_dev *dev;
#ifdef SNDRV_SBAWE_EMU8000
	struct pnp_dev *devwt;
#endif
#endif
};

#ifdef CONFIG_PNP

static struct pnp_card_device_id snd_sb16_pnpids[] = {
#ifndef SNDRV_SBAWE
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0024", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0025", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0026", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0027", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0028", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL0029", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL002a", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
	{ .id = "CTL002b", .devs = { { "CTL0031" } } },
	/* Sound Blaster 16 PnP */
	{ .id = "CTL002c", .devs = { { "CTL0031" } } },
	/* Sound Blaster Vibra16S */
	{ .id = "CTL0051", .devs = { { "CTL0001" } } },
	/* Sound Blaster Vibra16C */
	{ .id = "CTL0070", .devs = { { "CTL0001" } } },
	/* Sound Blaster Vibra16CL - added by ctm@ardi.com */
	{ .id = "CTL0080", .devs = { { "CTL0041" } } },
	/* Sound Blaster 16 'value' PnP. It says model ct4130 on the pcb, */
	/* but ct4131 on a sticker on the board.. */
	{ .id = "CTL0086", .devs = { { "CTL0041" } } },
	/* Sound Blaster Vibra16X */
	{ .id = "CTL00f0", .devs = { { "CTL0043" } } },
180 181
	/* Sound Blaster 16 (Virtual PC 2004) */
	{ .id = "tBA03b0", .devs = { {.id="PNPb003" } } },
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#else  /* SNDRV_SBAWE defined */
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0035", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0039", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0042", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0043", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
	{ .id = "CTL0044", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	/* Note: This card has also a CTL0051:StereoEnhance device!!! */
	{ .id = "CTL0045", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0046", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0047", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0048", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL0054", .devs = { { "CTL0031" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL009a", .devs = { { "CTL0041" }, { "CTL0021" } } },
	/* Sound Blaster AWE 32 PnP */
	{ .id = "CTL009c", .devs = { { "CTL0041" }, { "CTL0021" } } },
	/* Sound Blaster 32 PnP */
	{ .id = "CTL009f", .devs = { { "CTL0041" }, { "CTL0021" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL009d", .devs = { { "CTL0042" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP Gold */
	{ .id = "CTL009e", .devs = { { "CTL0044" }, { "CTL0023" } } },
	/* Sound Blaster AWE 64 PnP Gold */
	{ .id = "CTL00b2", .devs = { { "CTL0044" }, { "CTL0023" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00c1", .devs = { { "CTL0042" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00c3", .devs = { { "CTL0045" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00c5", .devs = { { "CTL0045" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00c7", .devs = { { "CTL0045" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00e4", .devs = { { "CTL0045" }, { "CTL0022" } } },
	/* Sound Blaster AWE 64 PnP */
	{ .id = "CTL00e9", .devs = { { "CTL0045" }, { "CTL0022" } } },
	/* Sound Blaster 16 PnP (AWE) */
	{ .id = "CTL00ed", .devs = { { "CTL0041" }, { "CTL0070" } } },
	/* Generic entries */
	{ .id = "CTLXXXX" , .devs = { { "CTL0031" }, { "CTL0021" } } },
	{ .id = "CTLXXXX" , .devs = { { "CTL0041" }, { "CTL0021" } } },
	{ .id = "CTLXXXX" , .devs = { { "CTL0042" }, { "CTL0022" } } },
	{ .id = "CTLXXXX" , .devs = { { "CTL0044" }, { "CTL0023" } } },
	{ .id = "CTLXXXX" , .devs = { { "CTL0045" }, { "CTL0022" } } },
#endif /* SNDRV_SBAWE */
	{ .id = "", }
};

MODULE_DEVICE_TABLE(pnp_card, snd_sb16_pnpids);

#endif /* CONFIG_PNP */

#ifdef SNDRV_SBAWE_EMU8000
#define DRIVER_NAME	"snd-card-sbawe"
#else
#define DRIVER_NAME	"snd-card-sb16"
#endif

#ifdef CONFIG_PNP

253 254 255
static int snd_card_sb16_pnp(int dev, struct snd_card_sb16 *acard,
			     struct pnp_card_link *card,
			     const struct pnp_card_device_id *id)
L
Linus Torvalds 已提交
256 257 258 259 260
{
	struct pnp_dev *pdev;
	int err;

	acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
261
	if (acard->dev == NULL)
L
Linus Torvalds 已提交
262
		return -ENODEV; 
263

L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#ifdef SNDRV_SBAWE_EMU8000
	acard->devwt = pnp_request_card_device(card, id->devs[1].id, acard->dev);
#endif
	/* Audio initialization */
	pdev = acard->dev;

	err = pnp_activate_dev(pdev); 
	if (err < 0) { 
		snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); 
		return err; 
	} 
	port[dev] = pnp_port_start(pdev, 0);
	mpu_port[dev] = pnp_port_start(pdev, 1);
	fm_port[dev] = pnp_port_start(pdev, 2);
	dma8[dev] = pnp_dma(pdev, 0);
	dma16[dev] = pnp_dma(pdev, 1);
	irq[dev] = pnp_irq(pdev, 0);
	snd_printdd("pnp SB16: port=0x%lx, mpu port=0x%lx, fm port=0x%lx\n",
			port[dev], mpu_port[dev], fm_port[dev]);
	snd_printdd("pnp SB16: dma1=%i, dma2=%i, irq=%i\n",
			dma8[dev], dma16[dev], irq[dev]);
#ifdef SNDRV_SBAWE_EMU8000
	/* WaveTable initialization */
	pdev = acard->devwt;
	if (pdev != NULL) {
		err = pnp_activate_dev(pdev); 
		if (err < 0) { 
			goto __wt_error; 
		} 
		awe_port[dev] = pnp_port_start(pdev, 0);
294 295
		snd_printdd("pnp SB16: wavetable port=0x%llx\n",
				(unsigned long long)pnp_port_start(pdev, 0));
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	} else {
__wt_error:
		if (pdev) {
			pnp_release_card_device(pdev);
			snd_printk(KERN_ERR PFX "WaveTable pnp configure failure\n");
		}
		acard->devwt = NULL;
		awe_port[dev] = -1;
	}
#endif
	return 0;
}

#endif /* CONFIG_PNP */

311
static void snd_sb16_free(struct snd_card *card)
L
Linus Torvalds 已提交
312
{
313
	struct snd_card_sb16 *acard = card->private_data;
L
Linus Torvalds 已提交
314 315 316
        
	if (acard == NULL)
		return;
317
	release_and_free_resource(acard->fm_res);
L
Linus Torvalds 已提交
318 319
}

320 321 322 323 324 325
#ifdef CONFIG_PNP
#define is_isapnp_selected(dev)		isapnp[dev]
#else
#define is_isapnp_selected(dev)		0
#endif

326 327
static int snd_sb16_card_new(struct device *devptr, int dev,
			     struct snd_card **cardp)
328
{
329 330 331
	struct snd_card *card;
	int err;

332 333
	err = snd_card_new(devptr, index[dev], id[dev], THIS_MODULE,
			   sizeof(struct snd_card_sb16), &card);
334
	if (err < 0)
335
		return err;
336
	card->private_free = snd_sb16_free;
337 338
	*cardp = card;
	return 0;
339 340
}

341
static int snd_sb16_probe(struct snd_card *card, int dev)
L
Linus Torvalds 已提交
342 343
{
	int xirq, xdma8, xdma16;
344
	struct snd_sb *chip;
345
	struct snd_card_sb16 *acard = card->private_data;
346 347
	struct snd_opl3 *opl3;
	struct snd_hwdep *synth = NULL;
L
Linus Torvalds 已提交
348
#ifdef CONFIG_SND_SB16_CSP
349
	struct snd_hwdep *xcsp = NULL;
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
#endif
	unsigned long flags;
	int err;

	xirq = irq[dev];
	xdma8 = dma8[dev];
	xdma16 = dma16[dev];

	if ((err = snd_sbdsp_create(card,
				    port[dev],
				    xirq,
				    snd_sb16dsp_interrupt,
				    xdma8,
				    xdma16,
				    SB_HW_AUTO,
365
				    &chip)) < 0)
366
		return err;
367

368
	acard->chip = chip;
L
Linus Torvalds 已提交
369
	if (chip->hardware != SB_HW_16) {
370
		snd_printk(KERN_ERR PFX "SB 16 chip was not detected at 0x%lx\n", port[dev]);
371
		return -ENODEV;
L
Linus Torvalds 已提交
372 373
	}
	chip->mpu_port = mpu_port[dev];
374
	if (! is_isapnp_selected(dev) && (err = snd_sb16dsp_configure(chip)) < 0)
375
		return err;
376

377
	if ((err = snd_sb16dsp_pcm(chip, 0)) < 0)
378
		return err;
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

	strcpy(card->driver,
#ifdef SNDRV_SBAWE_EMU8000
			awe_port[dev] > 0 ? "SB AWE" :
#endif
			"SB16");
	strcpy(card->shortname, chip->name);
	sprintf(card->longname, "%s at 0x%lx, irq %i, dma ",
		chip->name,
		chip->port,
		xirq);
	if (xdma8 >= 0)
		sprintf(card->longname + strlen(card->longname), "%d", xdma8);
	if (xdma16 >= 0)
		sprintf(card->longname + strlen(card->longname), "%s%d",
			xdma8 >= 0 ? "&" : "", xdma16);

	if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SB,
398 399 400
					       chip->mpu_port,
					       MPU401_INFO_IRQ_HOOK, -1,
					       &chip->rmidi)) < 0)
401
			return err;
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		chip->rmidi_callback = snd_mpu401_uart_interrupt;
	}

#ifdef SNDRV_SBAWE_EMU8000
	if (awe_port[dev] == SNDRV_AUTO_PORT)
		awe_port[dev] = 0; /* disable */
#endif

	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
		if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
				    OPL3_HW_OPL3,
				    acard->fm_res != NULL || fm_port[dev] == port[dev],
				    &opl3) < 0) {
			snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
				   fm_port[dev], fm_port[dev] + 2);
		} else {
#ifdef SNDRV_SBAWE_EMU8000
			int seqdev = awe_port[dev] > 0 ? 2 : 1;
#else
			int seqdev = 1;
#endif
423
			if ((err = snd_opl3_hwdep_new(opl3, 0, seqdev, &synth)) < 0)
424
				return err;
L
Linus Torvalds 已提交
425 426 427
		}
	}

428
	if ((err = snd_sbmixer_new(chip)) < 0)
429
		return err;
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

#ifdef CONFIG_SND_SB16_CSP
	/* CSP chip on SB16ASP/AWE32 */
	if ((chip->hardware == SB_HW_16) && csp[dev]) {
		snd_sb_csp_new(chip, synth != NULL ? 1 : 0, &xcsp);
		if (xcsp) {
			chip->csp = xcsp->private_data;
			chip->hardware = SB_HW_16CSP;
		} else {
			snd_printk(KERN_INFO PFX "warning - CSP chip not detected on soundcard #%i\n", dev + 1);
		}
	}
#endif
#ifdef SNDRV_SBAWE_EMU8000
	if (awe_port[dev] > 0) {
445 446
		if ((err = snd_emu8000_new(card, 1, awe_port[dev],
					   seq_ports[dev], NULL)) < 0) {
L
Linus Torvalds 已提交
447
			snd_printk(KERN_ERR PFX "fatal error - EMU-8000 synthesizer not detected at 0x%lx\n", awe_port[dev]);
448

449
			return err;
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460
		}
	}
#endif

	/* setup Mic AGC */
	spin_lock_irqsave(&chip->mixer_lock, flags);
	snd_sbmixer_write(chip, SB_DSP4_MIC_AGC,
		(snd_sbmixer_read(chip, SB_DSP4_MIC_AGC) & 0x01) |
		(mic_agc[dev] ? 0x00 : 0x01));
	spin_unlock_irqrestore(&chip->mixer_lock, flags);

461
	if ((err = snd_card_register(card)) < 0)
462
		return err;
463

L
Linus Torvalds 已提交
464
	return 0;
465 466 467 468 469 470 471
}

#ifdef CONFIG_PM
static int snd_sb16_suspend(struct snd_card *card, pm_message_t state)
{
	struct snd_card_sb16 *acard = card->private_data;
	struct snd_sb *chip = acard->chip;
472

473 474 475 476
	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
	snd_pcm_suspend_all(chip->pcm);
	snd_sbmixer_suspend(chip);
	return 0;
L
Linus Torvalds 已提交
477 478
}

479
static int snd_sb16_resume(struct snd_card *card)
L
Linus Torvalds 已提交
480
{
481 482
	struct snd_card_sb16 *acard = card->private_data;
	struct snd_sb *chip = acard->chip;
L
Linus Torvalds 已提交
483

484 485 486 487 488 489 490
	snd_sbdsp_reset(chip);
	snd_sbmixer_resume(chip);
	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
	return 0;
}
#endif

491
static int snd_sb16_isa_probe1(int dev, struct device *pdev)
492 493 494 495 496
{
	struct snd_card_sb16 *acard;
	struct snd_card *card;
	int err;

497
	err = snd_sb16_card_new(pdev, dev, &card);
498 499
	if (err < 0)
		return err;
500 501 502 503 504 505 506 507 508 509 510 511 512 513

	acard = card->private_data;
	/* non-PnP FM port address is hardwired with base port address */
	fm_port[dev] = port[dev];
	/* block the 0x388 port to avoid PnP conflicts */
	acard->fm_res = request_region(0x388, 4, "SoundBlaster FM");
#ifdef SNDRV_SBAWE_EMU8000
	/* non-PnP AWE port address is hardwired with base port address */
	awe_port[dev] = port[dev] + 0x400;
#endif

	if ((err = snd_sb16_probe(card, dev)) < 0) {
		snd_card_free(card);
		return err;
L
Linus Torvalds 已提交
514
	}
515
	dev_set_drvdata(pdev, card);
516
	return 0;
L
Linus Torvalds 已提交
517 518
}

519

520
static int snd_sb16_isa_match(struct device *pdev, unsigned int dev)
521 522 523 524
{
	return enable[dev] && !is_isapnp_selected(dev);
}

525
static int snd_sb16_isa_probe(struct device *pdev, unsigned int dev)
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
{
	int err;
	static int possible_irqs[] = {5, 9, 10, 7, -1};
	static int possible_dmas8[] = {1, 3, 0, -1};
	static int possible_dmas16[] = {5, 6, 7, -1};

	if (irq[dev] == SNDRV_AUTO_IRQ) {
		if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
			snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
			return -EBUSY;
		}
	}
	if (dma8[dev] == SNDRV_AUTO_DMA) {
		if ((dma8[dev] = snd_legacy_find_free_dma(possible_dmas8)) < 0) {
			snd_printk(KERN_ERR PFX "unable to find a free 8-bit DMA\n");
			return -EBUSY;
		}
	}
	if (dma16[dev] == SNDRV_AUTO_DMA) {
		if ((dma16[dev] = snd_legacy_find_free_dma(possible_dmas16)) < 0) {
			snd_printk(KERN_ERR PFX "unable to find a free 16-bit DMA\n");
			return -EBUSY;
		}
	}

	if (port[dev] != SNDRV_AUTO_PORT)
552
		return snd_sb16_isa_probe1(dev, pdev);
553 554 555 556 557
	else {
		static int possible_ports[] = {0x220, 0x240, 0x260, 0x280};
		int i;
		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
			port[dev] = possible_ports[i];
558
			err = snd_sb16_isa_probe1(dev, pdev);
559 560 561 562 563 564 565
			if (! err)
				return 0;
		}
		return err;
	}
}

566
static int snd_sb16_isa_remove(struct device *pdev, unsigned int dev)
567
{
568
	snd_card_free(dev_get_drvdata(pdev));
569 570 571 572
	return 0;
}

#ifdef CONFIG_PM
573 574
static int snd_sb16_isa_suspend(struct device *dev, unsigned int n,
				pm_message_t state)
575
{
576
	return snd_sb16_suspend(dev_get_drvdata(dev), state);
577 578
}

579
static int snd_sb16_isa_resume(struct device *dev, unsigned int n)
580
{
581
	return snd_sb16_resume(dev_get_drvdata(dev));
582 583 584 585
}
#endif

#ifdef SNDRV_SBAWE
586
#define DEV_NAME "sbawe"
587
#else
588
#define DEV_NAME "sb16"
589 590
#endif

591 592 593
static struct isa_driver snd_sb16_isa_driver = {
	.match		= snd_sb16_isa_match,
	.probe		= snd_sb16_isa_probe,
594
	.remove		= snd_sb16_isa_remove,
595
#ifdef CONFIG_PM
596 597
	.suspend	= snd_sb16_isa_suspend,
	.resume		= snd_sb16_isa_resume,
598 599
#endif
	.driver		= {
600
		.name	= DEV_NAME
601 602 603 604
	},
};


L
Linus Torvalds 已提交
605
#ifdef CONFIG_PNP
606 607
static int snd_sb16_pnp_detect(struct pnp_card_link *pcard,
			       const struct pnp_card_device_id *pid)
L
Linus Torvalds 已提交
608 609
{
	static int dev;
610
	struct snd_card *card;
L
Linus Torvalds 已提交
611 612 613 614 615
	int res;

	for ( ; dev < SNDRV_CARDS; dev++) {
		if (!enable[dev] || !isapnp[dev])
			continue;
616
		res = snd_sb16_card_new(&pcard->card->dev, dev, &card);
617 618
		if (res < 0)
			return res;
619 620 621
		if ((res = snd_card_sb16_pnp(dev, card->private_data, pcard, pid)) < 0 ||
		    (res = snd_sb16_probe(card, dev)) < 0) {
			snd_card_free(card);
L
Linus Torvalds 已提交
622
			return res;
623 624
		}
		pnp_set_card_drvdata(pcard, card);
L
Linus Torvalds 已提交
625 626 627 628 629 630 631
		dev++;
		return 0;
	}

	return -ENODEV;
}

632
static void snd_sb16_pnp_remove(struct pnp_card_link *pcard)
L
Linus Torvalds 已提交
633
{
634 635 636
	snd_card_free(pnp_get_card_drvdata(pcard));
	pnp_set_card_drvdata(pcard, NULL);
}
L
Linus Torvalds 已提交
637

638 639 640 641
#ifdef CONFIG_PM
static int snd_sb16_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
{
	return snd_sb16_suspend(pnp_get_card_drvdata(pcard), state);
L
Linus Torvalds 已提交
642
}
643 644 645 646 647
static int snd_sb16_pnp_resume(struct pnp_card_link *pcard)
{
	return snd_sb16_resume(pnp_get_card_drvdata(pcard));
}
#endif
L
Linus Torvalds 已提交
648 649 650

static struct pnp_card_driver sb16_pnpc_driver = {
	.flags = PNP_DRIVER_RES_DISABLE,
651 652 653
#ifdef SNDRV_SBAWE
	.name = "sbawe",
#else
L
Linus Torvalds 已提交
654
	.name = "sb16",
655
#endif
L
Linus Torvalds 已提交
656 657
	.id_table = snd_sb16_pnpids,
	.probe = snd_sb16_pnp_detect,
658
	.remove = snd_sb16_pnp_remove,
659 660 661 662
#ifdef CONFIG_PM
	.suspend = snd_sb16_pnp_suspend,
	.resume = snd_sb16_pnp_resume,
#endif
L
Linus Torvalds 已提交
663 664 665 666 667 668
};

#endif /* CONFIG_PNP */

static int __init alsa_card_sb16_init(void)
{
669
	int err;
L
Linus Torvalds 已提交
670

671
	err = isa_register_driver(&snd_sb16_isa_driver, SNDRV_CARDS);
L
Linus Torvalds 已提交
672
#ifdef CONFIG_PNP
673 674 675
	if (!err)
		isa_registered = 1;

676
	err = pnp_register_card_driver(&sb16_pnpc_driver);
677
	if (!err)
678
		pnp_registered = 1;
679 680 681

	if (isa_registered)
		err = 0;
L
Linus Torvalds 已提交
682
#endif
683
	return err;
L
Linus Torvalds 已提交
684 685 686 687
}

static void __exit alsa_card_sb16_exit(void)
{
688 689 690
#ifdef CONFIG_PNP
	if (pnp_registered)
		pnp_unregister_card_driver(&sb16_pnpc_driver);
691
	if (isa_registered)
692
#endif
693
		isa_unregister_driver(&snd_sb16_isa_driver);
L
Linus Torvalds 已提交
694 695 696 697
}

module_init(alsa_card_sb16_init)
module_exit(alsa_card_sb16_exit)