xref: /linux/sound/isa/gus/interwave.c (revision 06a130e42a5bfc84795464bff023bff4c16f58c5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for AMD InterWave soundcard
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  *
6  *   1999/07/22		Erik Inge Bolso <knan@mo.himolde.no>
7  *			* mixer group handlers
8  */
9 
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/isa.h>
13 #include <linux/delay.h>
14 #include <linux/pnp.h>
15 #include <linux/module.h>
16 #include <asm/dma.h>
17 #include <sound/core.h>
18 #include <sound/gus.h>
19 #include <sound/wss.h>
20 #ifdef SNDRV_STB
21 #include <sound/tea6330t.h>
22 #endif
23 #define SNDRV_LEGACY_FIND_FREE_IRQ
24 #define SNDRV_LEGACY_FIND_FREE_DMA
25 #include <sound/initval.h>
26 
27 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
28 MODULE_LICENSE("GPL");
29 #ifndef SNDRV_STB
30 MODULE_DESCRIPTION("AMD InterWave");
31 #else
32 MODULE_DESCRIPTION("AMD InterWave STB with TEA6330T");
33 #endif
34 
35 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
36 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
37 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
38 #ifdef CONFIG_PNP
39 static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
40 #endif
41 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x210,0x220,0x230,0x240,0x250,0x260 */
42 #ifdef SNDRV_STB
43 static long port_tc[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x350,0x360,0x370,0x380 */
44 #endif
45 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 2,3,5,9,11,12,15 */
46 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3,5,6,7 */
47 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3,5,6,7 */
48 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
49 				/* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
50 static int midi[SNDRV_CARDS];
51 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
52 static int effect[SNDRV_CARDS];
53 
54 #ifdef SNDRV_STB
55 #define INTERWAVE_DRIVER	"snd_interwave_stb"
56 #define INTERWAVE_PNP_DRIVER	"interwave-stb"
57 #else
58 #define INTERWAVE_DRIVER	"snd_interwave"
59 #define INTERWAVE_PNP_DRIVER	"interwave"
60 #endif
61 
62 module_param_array(index, int, NULL, 0444);
63 MODULE_PARM_DESC(index, "Index value for InterWave soundcard.");
64 module_param_array(id, charp, NULL, 0444);
65 MODULE_PARM_DESC(id, "ID string for InterWave soundcard.");
66 module_param_array(enable, bool, NULL, 0444);
67 MODULE_PARM_DESC(enable, "Enable InterWave soundcard.");
68 #ifdef CONFIG_PNP
69 module_param_array(isapnp, bool, NULL, 0444);
70 MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
71 #endif
72 module_param_hw_array(port, long, ioport, NULL, 0444);
73 MODULE_PARM_DESC(port, "Port # for InterWave driver.");
74 #ifdef SNDRV_STB
75 module_param_hw_array(port_tc, long, ioport, NULL, 0444);
76 MODULE_PARM_DESC(port_tc, "Tone control (TEA6330T - i2c bus) port # for InterWave driver.");
77 #endif
78 module_param_hw_array(irq, int, irq, NULL, 0444);
79 MODULE_PARM_DESC(irq, "IRQ # for InterWave driver.");
80 module_param_hw_array(dma1, int, dma, NULL, 0444);
81 MODULE_PARM_DESC(dma1, "DMA1 # for InterWave driver.");
82 module_param_hw_array(dma2, int, dma, NULL, 0444);
83 MODULE_PARM_DESC(dma2, "DMA2 # for InterWave driver.");
84 module_param_array(joystick_dac, int, NULL, 0444);
85 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for InterWave driver.");
86 module_param_array(midi, int, NULL, 0444);
87 MODULE_PARM_DESC(midi, "MIDI UART enable for InterWave driver.");
88 module_param_array(pcm_channels, int, NULL, 0444);
89 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for InterWave driver.");
90 module_param_array(effect, int, NULL, 0444);
91 MODULE_PARM_DESC(effect, "Effects enable for InterWave driver.");
92 
93 struct snd_interwave {
94 	int irq;
95 	struct snd_card *card;
96 	struct snd_gus_card *gus;
97 	struct snd_wss *wss;
98 #ifdef SNDRV_STB
99 	struct resource *i2c_res;
100 #endif
101 	unsigned short gus_status_reg;
102 	unsigned short pcm_status_reg;
103 #ifdef CONFIG_PNP
104 	struct pnp_dev *dev;
105 #ifdef SNDRV_STB
106 	struct pnp_dev *devtc;
107 #endif
108 #endif
109 };
110 
111 
112 #ifdef CONFIG_PNP
113 static int isa_registered;
114 static int pnp_registered;
115 
116 static const struct pnp_card_device_id snd_interwave_pnpids[] = {
117 #ifndef SNDRV_STB
118 	/* Gravis UltraSound Plug & Play */
119 	{ .id = "GRV0001", .devs = { { .id = "GRV0000" } } },
120 	/* STB SoundRage32 */
121 	{ .id = "STB011a", .devs = { { .id = "STB0010" } } },
122 	/* MED3210 */
123 	{ .id = "DXP3201", .devs = { { .id = "DXP0010" } } },
124 	/* Dynasonic Pro */
125 	/* This device also have CDC1117:DynaSonix Pro Audio Effects Processor */
126 	{ .id = "CDC1111", .devs = { { .id = "CDC1112" } } },
127 	/* Panasonic PCA761AW Audio Card */
128 	{ .id = "ADV55ff", .devs = { { .id = "ADV0010" } } },
129 	/* InterWave STB without TEA6330T */
130 	{ .id = "ADV550a", .devs = { { .id = "ADV0010" } } },
131 #else
132 	/* InterWave STB with TEA6330T */
133 	{ .id = "ADV550a", .devs = { { .id = "ADV0010" }, { .id = "ADV0015" } } },
134 #endif
135 	{ .id = "" }
136 };
137 
138 MODULE_DEVICE_TABLE(pnp_card, snd_interwave_pnpids);
139 
140 #endif /* CONFIG_PNP */
141 
142 
143 #ifdef SNDRV_STB
144 static void snd_interwave_i2c_setlines(struct snd_i2c_bus *bus, int ctrl, int data)
145 {
146 	unsigned long port = bus->private_value;
147 
148 #if 0
149 	dev_dbg(bus->card->dev, "i2c_setlines - 0x%lx <- %i,%i\n", port, ctrl, data);
150 #endif
151 	outb((data << 1) | ctrl, port);
152 	udelay(10);
153 }
154 
155 static int snd_interwave_i2c_getclockline(struct snd_i2c_bus *bus)
156 {
157 	unsigned long port = bus->private_value;
158 	unsigned char res;
159 
160 	res = inb(port) & 1;
161 #if 0
162 	dev_dbg(bus->card->dev, "i2c_getclockline - 0x%lx -> %i\n", port, res);
163 #endif
164 	return res;
165 }
166 
167 static int snd_interwave_i2c_getdataline(struct snd_i2c_bus *bus, int ack)
168 {
169 	unsigned long port = bus->private_value;
170 	unsigned char res;
171 
172 	if (ack)
173 		udelay(10);
174 	res = (inb(port) & 2) >> 1;
175 #if 0
176 	dev_dbg(bus->card->dev, "i2c_getdataline - 0x%lx -> %i\n", port, res);
177 #endif
178 	return res;
179 }
180 
181 static struct snd_i2c_bit_ops snd_interwave_i2c_bit_ops = {
182 	.setlines = snd_interwave_i2c_setlines,
183 	.getclock = snd_interwave_i2c_getclockline,
184 	.getdata  = snd_interwave_i2c_getdataline,
185 };
186 
187 static int snd_interwave_detect_stb(struct snd_interwave *iwcard,
188 				    struct snd_gus_card *gus, int dev,
189 				    struct snd_i2c_bus **rbus)
190 {
191 	unsigned long port;
192 	struct snd_i2c_bus *bus;
193 	struct snd_card *card = iwcard->card;
194 	char name[32];
195 	int err;
196 
197 	*rbus = NULL;
198 	port = port_tc[dev];
199 	if (port == SNDRV_AUTO_PORT) {
200 		port = 0x350;
201 		if (gus->gf1.port == 0x250) {
202 			port = 0x360;
203 		}
204 		while (port <= 0x380) {
205 			iwcard->i2c_res = devm_request_region(card->dev, port, 1,
206 							      "InterWave (I2C bus)");
207 			if (iwcard->i2c_res)
208 				break;
209 			port += 0x10;
210 		}
211 	} else {
212 		iwcard->i2c_res = devm_request_region(card->dev, port, 1,
213 						      "InterWave (I2C bus)");
214 	}
215 	if (iwcard->i2c_res == NULL) {
216 		dev_err(card->dev, "interwave: can't grab i2c bus port\n");
217 		return -ENODEV;
218 	}
219 
220 	sprintf(name, "InterWave-%i", card->number);
221 	err = snd_i2c_bus_create(card, name, NULL, &bus);
222 	if (err < 0)
223 		return err;
224 	bus->private_value = port;
225 	bus->hw_ops.bit = &snd_interwave_i2c_bit_ops;
226 	err = snd_tea6330t_detect(bus, 0);
227 	if (err < 0)
228 		return err;
229 	*rbus = bus;
230 	return 0;
231 }
232 #endif
233 
234 static int snd_interwave_detect(struct snd_interwave *iwcard,
235 				struct snd_gus_card *gus,
236 				int dev
237 #ifdef SNDRV_STB
238 				, struct snd_i2c_bus **rbus
239 #endif
240 				          )
241 {
242 	unsigned long flags;
243 	unsigned char rev1, rev2;
244 	int d;
245 
246 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);	/* reset GF1 */
247 	d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
248 	if ((d & 0x07) != 0) {
249 		dev_dbg(gus->card->dev, "[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
250 		return -ENODEV;
251 	}
252 	udelay(160);
253 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* release reset */
254 	udelay(160);
255 	d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
256 	if ((d & 0x07) != 1) {
257 		dev_dbg(gus->card->dev, "[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
258 		return -ENODEV;
259 	}
260 	spin_lock_irqsave(&gus->reg_lock, flags);
261 	rev1 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER);
262 	snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, ~rev1);
263 	rev2 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER);
264 	snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, rev1);
265 	spin_unlock_irqrestore(&gus->reg_lock, flags);
266 	dev_dbg(gus->card->dev,
267 		"[0x%lx] InterWave check - rev1=0x%x, rev2=0x%x\n",
268 		gus->gf1.port, rev1, rev2);
269 	if ((rev1 & 0xf0) == (rev2 & 0xf0) &&
270 	    (rev1 & 0x0f) != (rev2 & 0x0f)) {
271 		dev_dbg(gus->card->dev,
272 			"[0x%lx] InterWave check - passed\n", gus->gf1.port);
273 		gus->interwave = 1;
274 		strcpy(gus->card->shortname, "AMD InterWave");
275 		gus->revision = rev1 >> 4;
276 #ifndef SNDRV_STB
277 		return 0;	/* ok.. We have an InterWave board */
278 #else
279 		return snd_interwave_detect_stb(iwcard, gus, dev, rbus);
280 #endif
281 	}
282 	dev_dbg(gus->card->dev, "[0x%lx] InterWave check - failed\n", gus->gf1.port);
283 	return -ENODEV;
284 }
285 
286 static irqreturn_t snd_interwave_interrupt(int irq, void *dev_id)
287 {
288 	struct snd_interwave *iwcard = dev_id;
289 	int loop, max = 5;
290 	int handled = 0;
291 
292 	do {
293 		loop = 0;
294 		if (inb(iwcard->gus_status_reg)) {
295 			handled = 1;
296 			snd_gus_interrupt(irq, iwcard->gus);
297 			loop++;
298 		}
299 		if (inb(iwcard->pcm_status_reg) & 0x01) {	/* IRQ bit is set? */
300 			handled = 1;
301 			snd_wss_interrupt(irq, iwcard->wss);
302 			loop++;
303 		}
304 	} while (loop && --max > 0);
305 	return IRQ_RETVAL(handled);
306 }
307 
308 static void snd_interwave_reset(struct snd_gus_card *gus)
309 {
310 	snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x00);
311 	udelay(160);
312 	snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x01);
313 	udelay(160);
314 }
315 
316 static void snd_interwave_bank_sizes(struct snd_gus_card *gus, int *sizes)
317 {
318 	unsigned int idx;
319 	unsigned int local;
320 	unsigned char d;
321 
322 	for (idx = 0; idx < 4; idx++) {
323 		sizes[idx] = 0;
324 		d = 0x55;
325 		for (local = idx << 22;
326 		     local < (idx << 22) + 0x400000;
327 		     local += 0x40000, d++) {
328 			snd_gf1_poke(gus, local, d);
329 			snd_gf1_poke(gus, local + 1, d + 1);
330 #if 0
331 			dev_dbg(gus->card->dev, "d = 0x%x, local = 0x%x, "
332 			       "local + 1 = 0x%x, idx << 22 = 0x%x\n",
333 			       d,
334 			       snd_gf1_peek(gus, local),
335 			       snd_gf1_peek(gus, local + 1),
336 			       snd_gf1_peek(gus, idx << 22));
337 #endif
338 			if (snd_gf1_peek(gus, local) != d ||
339 			    snd_gf1_peek(gus, local + 1) != d + 1 ||
340 			    snd_gf1_peek(gus, idx << 22) != 0x55)
341 				break;
342 			sizes[idx]++;
343 		}
344 	}
345 #if 0
346 	dev_dbg(gus->card->dev, "sizes: %i %i %i %i\n",
347 	       sizes[0], sizes[1], sizes[2], sizes[3]);
348 #endif
349 }
350 
351 struct rom_hdr {
352 	/* 000 */ unsigned char iwave[8];
353 	/* 008 */ unsigned char rom_hdr_revision;
354 	/* 009 */ unsigned char series_number;
355 	/* 010 */ unsigned char series_name[16];
356 	/* 026 */ unsigned char date[10];
357 	/* 036 */ unsigned short vendor_revision_major;
358 	/* 038 */ unsigned short vendor_revision_minor;
359 	/* 040 */ unsigned int rom_size;
360 	/* 044 */ unsigned char copyright[128];
361 	/* 172 */ unsigned char vendor_name[64];
362 	/* 236 */ unsigned char rom_description[128];
363 	/* 364 */ unsigned char pad[147];
364 	/* 511 */ unsigned char csum;
365 };
366 
367 static void snd_interwave_detect_memory(struct snd_gus_card *gus)
368 {
369 	static const unsigned int lmc[13] =
370 	{
371 		0x00000001, 0x00000101, 0x01010101, 0x00000401,
372 		0x04040401, 0x00040101, 0x04040101, 0x00000004,
373 		0x00000404, 0x04040404, 0x00000010, 0x00001010,
374 		0x10101010
375 	};
376 
377 	int bank_pos, pages;
378 	unsigned int i, lmct;
379 	int psizes[4];
380 	unsigned char iwave[8];
381 	unsigned char csum;
382 
383 	snd_interwave_reset(gus);
384 	snd_gf1_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_read8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);		/* enhanced mode */
385 	snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);	/* DRAM I/O cycles selected */
386 	snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff10) | 0x004c);
387 	/* ok.. simple test of memory size */
388 	pages = 0;
389 	snd_gf1_poke(gus, 0, 0x55);
390 	snd_gf1_poke(gus, 1, 0xaa);
391 #if 1
392 	if (snd_gf1_peek(gus, 0) == 0x55 && snd_gf1_peek(gus, 1) == 0xaa)
393 #else
394 	if (0)			/* ok.. for testing of 0k RAM */
395 #endif
396 	{
397 		snd_interwave_bank_sizes(gus, psizes);
398 		lmct = (psizes[3] << 24) | (psizes[2] << 16) |
399 		    (psizes[1] << 8) | psizes[0];
400 #if 0
401 		dev_dbg(gus->card->dev, "lmct = 0x%08x\n", lmct);
402 #endif
403 		for (i = 0; i < ARRAY_SIZE(lmc); i++)
404 			if (lmct == lmc[i]) {
405 #if 0
406 				dev_dbg(gus->card->dev, "found !!! %i\n", i);
407 #endif
408 				snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | i);
409 				snd_interwave_bank_sizes(gus, psizes);
410 				break;
411 			}
412 		if (i >= ARRAY_SIZE(lmc) && !gus->gf1.enh_mode)
413 			 snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | 2);
414 		for (i = 0; i < 4; i++) {
415 			gus->gf1.mem_alloc.banks_8[i].address =
416 			    gus->gf1.mem_alloc.banks_16[i].address = i << 22;
417 			gus->gf1.mem_alloc.banks_8[i].size =
418 			    gus->gf1.mem_alloc.banks_16[i].size = psizes[i] << 18;
419 			pages += psizes[i];
420 		}
421 	}
422 	pages <<= 18;
423 	gus->gf1.memory = pages;
424 
425 	snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x03);	/* select ROM */
426 	snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff1f) | (4 << 5));
427 	gus->gf1.rom_banks = 0;
428 	gus->gf1.rom_memory = 0;
429 	for (bank_pos = 0; bank_pos < 16L * 1024L * 1024L; bank_pos += 4L * 1024L * 1024L) {
430 		for (i = 0; i < 8; ++i)
431 			iwave[i] = snd_gf1_peek(gus, bank_pos + i);
432 		if (strncmp(iwave, "INTRWAVE", 8))
433 			continue;	/* first check */
434 		csum = 0;
435 		for (i = 0; i < sizeof(struct rom_hdr); i++)
436 			csum += snd_gf1_peek(gus, bank_pos + i);
437 		if (csum != 0)
438 			continue;	/* not valid rom */
439 		gus->gf1.rom_banks++;
440 		gus->gf1.rom_present |= 1 << (bank_pos >> 22);
441 		gus->gf1.rom_memory = snd_gf1_peek(gus, bank_pos + 40) |
442 				     (snd_gf1_peek(gus, bank_pos + 41) << 8) |
443 				     (snd_gf1_peek(gus, bank_pos + 42) << 16) |
444 				     (snd_gf1_peek(gus, bank_pos + 43) << 24);
445 	}
446 #if 0
447 	if (gus->gf1.rom_memory > 0) {
448 		if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8)
449 			gus->card->type = SNDRV_CARD_TYPE_IW_DYNASONIC;
450 	}
451 #endif
452 	snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x00);	/* select RAM */
453 
454 	if (!gus->gf1.enh_mode)
455 		snd_interwave_reset(gus);
456 }
457 
458 static void snd_interwave_init(int dev, struct snd_gus_card *gus)
459 {
460 	unsigned long flags;
461 
462 	/* ok.. some InterWave specific initialization */
463 	spin_lock_irqsave(&gus->reg_lock, flags);
464 	snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0x00);
465 	snd_gf1_write8(gus, SNDRV_GF1_GB_COMPATIBILITY, 0x1f);
466 	snd_gf1_write8(gus, SNDRV_GF1_GB_DECODE_CONTROL, 0x49);
467 	snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, 0x11);
468 	snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_A, 0x00);
469 	snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_B, 0x30);
470 	snd_gf1_write8(gus, SNDRV_GF1_GB_EMULATION_IRQ, 0x00);
471 	spin_unlock_irqrestore(&gus->reg_lock, flags);
472 	gus->equal_irq = 1;
473 	gus->codec_flag = 1;
474 	gus->interwave = 1;
475 	gus->max_flag = 1;
476 	gus->joystick_dac = joystick_dac[dev];
477 
478 }
479 
480 static const struct snd_kcontrol_new snd_interwave_controls[] = {
481 WSS_DOUBLE("Master Playback Switch", 0,
482 		CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 7, 7, 1, 1),
483 WSS_DOUBLE("Master Playback Volume", 0,
484 		CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 0, 0, 31, 1),
485 WSS_DOUBLE("Mic Playback Switch", 0,
486 		CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 7, 7, 1, 1),
487 WSS_DOUBLE("Mic Playback Volume", 0,
488 		CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 0, 0, 31, 1)
489 };
490 
491 static int snd_interwave_mixer(struct snd_wss *chip)
492 {
493 	struct snd_card *card = chip->card;
494 	struct snd_ctl_elem_id id1, id2;
495 	unsigned int idx;
496 	int err;
497 
498 	memset(&id1, 0, sizeof(id1));
499 	memset(&id2, 0, sizeof(id2));
500 	id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
501 #if 0
502 	/* remove mono microphone controls */
503 	strcpy(id1.name, "Mic Playback Switch");
504 	err = snd_ctl_remove_id(card, &id1);
505 	if (err < 0)
506 		return err;
507 	strcpy(id1.name, "Mic Playback Volume");
508 	err = snd_ctl_remove_id(card, &id1);
509 	if (err < 0)
510 		return err;
511 #endif
512 	/* add new master and mic controls */
513 	for (idx = 0; idx < ARRAY_SIZE(snd_interwave_controls); idx++) {
514 		err = snd_ctl_add(card, snd_ctl_new1(&snd_interwave_controls[idx], chip));
515 		if (err < 0)
516 			return err;
517 	}
518 	snd_wss_out(chip, CS4231_LINE_LEFT_OUTPUT, 0x9f);
519 	snd_wss_out(chip, CS4231_LINE_RIGHT_OUTPUT, 0x9f);
520 	snd_wss_out(chip, CS4231_LEFT_MIC_INPUT, 0x9f);
521 	snd_wss_out(chip, CS4231_RIGHT_MIC_INPUT, 0x9f);
522 	/* reassign AUXA to SYNTHESIZER */
523 	strcpy(id1.name, "Aux Playback Switch");
524 	strcpy(id2.name, "Synth Playback Switch");
525 	err = snd_ctl_rename_id(card, &id1, &id2);
526 	if (err < 0)
527 		return err;
528 	strcpy(id1.name, "Aux Playback Volume");
529 	strcpy(id2.name, "Synth Playback Volume");
530 	err = snd_ctl_rename_id(card, &id1, &id2);
531 	if (err < 0)
532 		return err;
533 	/* reassign AUXB to CD */
534 	strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
535 	strcpy(id2.name, "CD Playback Switch");
536 	err = snd_ctl_rename_id(card, &id1, &id2);
537 	if (err < 0)
538 		return err;
539 	strcpy(id1.name, "Aux Playback Volume");
540 	strcpy(id2.name, "CD Playback Volume");
541 	err = snd_ctl_rename_id(card, &id1, &id2);
542 	if (err < 0)
543 		return err;
544 	return 0;
545 }
546 
547 #ifdef CONFIG_PNP
548 
549 static int snd_interwave_pnp(int dev, struct snd_interwave *iwcard,
550 			     struct pnp_card_link *card,
551 			     const struct pnp_card_device_id *id)
552 {
553 	struct pnp_dev *pdev;
554 	int err;
555 
556 	iwcard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
557 	if (iwcard->dev == NULL)
558 		return -EBUSY;
559 
560 #ifdef SNDRV_STB
561 	iwcard->devtc = pnp_request_card_device(card, id->devs[1].id, NULL);
562 	if (iwcard->devtc == NULL)
563 		return -EBUSY;
564 #endif
565 	/* Synth & Codec initialization */
566 	pdev = iwcard->dev;
567 
568 	err = pnp_activate_dev(pdev);
569 	if (err < 0) {
570 		dev_err(&pdev->dev, "InterWave PnP configure failure (out of resources?)\n");
571 		return err;
572 	}
573 	if (pnp_port_start(pdev, 0) + 0x100 != pnp_port_start(pdev, 1) ||
574 	    pnp_port_start(pdev, 0) + 0x10c != pnp_port_start(pdev, 2)) {
575 		dev_err(&pdev->dev, "PnP configure failure (wrong ports)\n");
576 		return -ENOENT;
577 	}
578 	port[dev] = pnp_port_start(pdev, 0);
579 	dma1[dev] = pnp_dma(pdev, 0);
580 	if (dma2[dev] >= 0)
581 		dma2[dev] = pnp_dma(pdev, 1);
582 	irq[dev] = pnp_irq(pdev, 0);
583 	dev_dbg(&pdev->dev,
584 		"isapnp IW: sb port=0x%llx, gf1 port=0x%llx, codec port=0x%llx\n",
585 		(unsigned long long)pnp_port_start(pdev, 0),
586 		(unsigned long long)pnp_port_start(pdev, 1),
587 		(unsigned long long)pnp_port_start(pdev, 2));
588 	dev_dbg(&pdev->dev,
589 		"isapnp IW: dma1=%i, dma2=%i, irq=%i\n",
590 		dma1[dev], dma2[dev], irq[dev]);
591 #ifdef SNDRV_STB
592 	/* Tone Control initialization */
593 	pdev = iwcard->devtc;
594 
595 	err = pnp_activate_dev(pdev);
596 	if (err < 0) {
597 		dev_err(&pdev->dev,
598 			"InterWave ToneControl PnP configure failure (out of resources?)\n");
599 		return err;
600 	}
601 	port_tc[dev] = pnp_port_start(pdev, 0);
602 	dev_dbg(&pdev->dev, "isapnp IW: tone control port=0x%lx\n", port_tc[dev]);
603 #endif
604 	return 0;
605 }
606 #endif /* CONFIG_PNP */
607 
608 static int snd_interwave_card_new(struct device *pdev, int dev,
609 				  struct snd_card **cardp)
610 {
611 	struct snd_card *card;
612 	struct snd_interwave *iwcard;
613 	int err;
614 
615 	err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
616 				sizeof(struct snd_interwave), &card);
617 	if (err < 0)
618 		return err;
619 	iwcard = card->private_data;
620 	iwcard->card = card;
621 	iwcard->irq = -1;
622 	*cardp = card;
623 	return 0;
624 }
625 
626 static int snd_interwave_probe_gus(struct snd_card *card, int dev,
627 				   struct snd_gus_card **gusp)
628 {
629 	return snd_gus_create(card, port[dev], -irq[dev], dma1[dev], dma2[dev],
630 			      0, 32, pcm_channels[dev], effect[dev], gusp);
631 }
632 
633 static int snd_interwave_probe(struct snd_card *card, int dev,
634 			       struct snd_gus_card *gus)
635 {
636 	int xirq, xdma1, xdma2;
637 	struct snd_interwave *iwcard = card->private_data;
638 	struct snd_wss *wss;
639 #ifdef SNDRV_STB
640 	struct snd_i2c_bus *i2c_bus;
641 #endif
642 	char *str;
643 	int err;
644 
645 	xirq = irq[dev];
646 	xdma1 = dma1[dev];
647 	xdma2 = dma2[dev];
648 
649 	err = snd_interwave_detect(iwcard, gus, dev
650 #ifdef SNDRV_STB
651 				   , &i2c_bus
652 #endif
653 				   );
654 	if (err < 0)
655 		return err;
656 
657 	iwcard->gus_status_reg = gus->gf1.reg_irqstat;
658 	iwcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
659 
660 	snd_interwave_init(dev, gus);
661 	snd_interwave_detect_memory(gus);
662 	err = snd_gus_initialize(gus);
663 	if (err < 0)
664 		return err;
665 
666 	if (devm_request_irq(card->dev, xirq, snd_interwave_interrupt, 0,
667 			     "InterWave", iwcard)) {
668 		dev_err(card->dev, "unable to grab IRQ %d\n", xirq);
669 		return -EBUSY;
670 	}
671 	iwcard->irq = xirq;
672 	card->sync_irq = iwcard->irq;
673 
674 	err = snd_wss_create(card,
675 			     gus->gf1.port + 0x10c, -1, xirq,
676 			     xdma2 < 0 ? xdma1 : xdma2, xdma1,
677 			     WSS_HW_INTERWAVE,
678 			     WSS_HWSHARE_IRQ |
679 			     WSS_HWSHARE_DMA1 |
680 			     WSS_HWSHARE_DMA2,
681 			     &wss);
682 	if (err < 0)
683 		return err;
684 
685 	err = snd_wss_pcm(wss, 0);
686 	if (err < 0)
687 		return err;
688 
689 	sprintf(wss->pcm->name + strlen(wss->pcm->name), " rev %c",
690 		gus->revision + 'A');
691 	strcat(wss->pcm->name, " (codec)");
692 
693 	err = snd_wss_timer(wss, 2);
694 	if (err < 0)
695 		return err;
696 
697 	err = snd_wss_mixer(wss);
698 	if (err < 0)
699 		return err;
700 
701 	if (pcm_channels[dev] > 0) {
702 		err = snd_gf1_pcm_new(gus, 1, 1);
703 		if (err < 0)
704 			return err;
705 	}
706 	err = snd_interwave_mixer(wss);
707 	if (err < 0)
708 		return err;
709 
710 #ifdef SNDRV_STB
711 	{
712 		struct snd_ctl_elem_id id1, id2;
713 		memset(&id1, 0, sizeof(id1));
714 		memset(&id2, 0, sizeof(id2));
715 		id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
716 		strcpy(id1.name, "Master Playback Switch");
717 		strcpy(id2.name, id1.name);
718 		id2.index = 1;
719 		err = snd_ctl_rename_id(card, &id1, &id2);
720 		if (err < 0)
721 			return err;
722 		strcpy(id1.name, "Master Playback Volume");
723 		strcpy(id2.name, id1.name);
724 		err = snd_ctl_rename_id(card, &id1, &id2);
725 		if (err < 0)
726 			return err;
727 		err = snd_tea6330t_update_mixer(card, i2c_bus, 0, 1);
728 		if (err < 0)
729 			return err;
730 	}
731 #endif
732 
733 	gus->uart_enable = midi[dev];
734 	err = snd_gf1_rawmidi_new(gus, 0);
735 	if (err < 0)
736 		return err;
737 
738 #ifndef SNDRV_STB
739 	str = "AMD InterWave";
740 	if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8)
741 		str = "Dynasonic 3-D";
742 #else
743 	str = "InterWave STB";
744 #endif
745 	strcpy(card->driver, str);
746 	strcpy(card->shortname, str);
747 	sprintf(card->longname, "%s at 0x%lx, irq %i, dma %d",
748 		str,
749 		gus->gf1.port,
750 		xirq,
751 		xdma1);
752 	if (xdma2 >= 0)
753 		sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
754 
755 	err = snd_card_register(card);
756 	if (err < 0)
757 		return err;
758 
759 	iwcard->wss = wss;
760 	iwcard->gus = gus;
761 	return 0;
762 }
763 
764 static int snd_interwave_isa_match(struct device *pdev,
765 				   unsigned int dev)
766 {
767 	if (!enable[dev])
768 		return 0;
769 #ifdef CONFIG_PNP
770 	if (isapnp[dev])
771 		return 0;
772 #endif
773 	return 1;
774 }
775 
776 static int snd_interwave_isa_probe(struct device *pdev,
777 				   unsigned int dev)
778 {
779 	struct snd_card *card;
780 	struct snd_gus_card *gus;
781 	int err;
782 	static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
783 	static const int possible_dmas[] = {0, 1, 3, 5, 6, 7, -1};
784 
785 	if (irq[dev] == SNDRV_AUTO_IRQ) {
786 		irq[dev] = snd_legacy_find_free_irq(possible_irqs);
787 		if (irq[dev] < 0) {
788 			dev_err(pdev, "unable to find a free IRQ\n");
789 			return -EBUSY;
790 		}
791 	}
792 	if (dma1[dev] == SNDRV_AUTO_DMA) {
793 		dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
794 		if (dma1[dev] < 0) {
795 			dev_err(pdev, "unable to find a free DMA1\n");
796 			return -EBUSY;
797 		}
798 	}
799 	if (dma2[dev] == SNDRV_AUTO_DMA) {
800 		dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
801 		if (dma2[dev] < 0) {
802 			dev_err(pdev, "unable to find a free DMA2\n");
803 			return -EBUSY;
804 		}
805 	}
806 
807 	err = snd_interwave_card_new(pdev, dev, &card);
808 	if (err < 0)
809 		return err;
810 
811 	if (port[dev] != SNDRV_AUTO_PORT)
812 		err = snd_interwave_probe_gus(card, dev, &gus);
813 	else {
814 		static const long possible_ports[] = {0x210, 0x220, 0x230, 0x240, 0x250, 0x260};
815 		int i;
816 		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
817 			port[dev] = possible_ports[i];
818 			err = snd_interwave_probe_gus(card, dev, &gus);
819 			if (! err)
820 				return 0;
821 		}
822 	}
823 	if (err < 0)
824 		return err;
825 
826 	err = snd_interwave_probe(card, dev, gus);
827 	if (err < 0)
828 		return err;
829 
830 	dev_set_drvdata(pdev, card);
831 	return 0;
832 }
833 
834 static struct isa_driver snd_interwave_driver = {
835 	.match		= snd_interwave_isa_match,
836 	.probe		= snd_interwave_isa_probe,
837 	/* FIXME: suspend,resume */
838 	.driver		= {
839 		.name	= INTERWAVE_DRIVER
840 	},
841 };
842 
843 #ifdef CONFIG_PNP
844 static int snd_interwave_pnp_detect(struct pnp_card_link *pcard,
845 				    const struct pnp_card_device_id *pid)
846 {
847 	static int dev;
848 	struct snd_card *card;
849 	struct snd_gus_card *gus;
850 	int res;
851 
852 	for ( ; dev < SNDRV_CARDS; dev++) {
853 		if (enable[dev] && isapnp[dev])
854 			break;
855 	}
856 	if (dev >= SNDRV_CARDS)
857 		return -ENODEV;
858 
859 	res = snd_interwave_card_new(&pcard->card->dev, dev, &card);
860 	if (res < 0)
861 		return res;
862 
863 	res = snd_interwave_pnp(dev, card->private_data, pcard, pid);
864 	if (res < 0)
865 		return res;
866 	res = snd_interwave_probe_gus(card, dev, &gus);
867 	if (res < 0)
868 		return res;
869 	res = snd_interwave_probe(card, dev, gus);
870 	if (res < 0)
871 		return res;
872 	pnp_set_card_drvdata(pcard, card);
873 	dev++;
874 	return 0;
875 }
876 
877 static struct pnp_card_driver interwave_pnpc_driver = {
878 	.flags = PNP_DRIVER_RES_DISABLE,
879 	.name = INTERWAVE_PNP_DRIVER,
880 	.id_table = snd_interwave_pnpids,
881 	.probe = snd_interwave_pnp_detect,
882 	/* FIXME: suspend,resume */
883 };
884 
885 #endif /* CONFIG_PNP */
886 
887 static int __init alsa_card_interwave_init(void)
888 {
889 	int err;
890 
891 	err = isa_register_driver(&snd_interwave_driver, SNDRV_CARDS);
892 #ifdef CONFIG_PNP
893 	if (!err)
894 		isa_registered = 1;
895 
896 	err = pnp_register_card_driver(&interwave_pnpc_driver);
897 	if (!err)
898 		pnp_registered = 1;
899 
900 	if (isa_registered)
901 		err = 0;
902 #endif
903 	return err;
904 }
905 
906 static void __exit alsa_card_interwave_exit(void)
907 {
908 #ifdef CONFIG_PNP
909 	if (pnp_registered)
910 		pnp_unregister_card_driver(&interwave_pnpc_driver);
911 	if (isa_registered)
912 #endif
913 		isa_unregister_driver(&snd_interwave_driver);
914 }
915 
916 module_init(alsa_card_interwave_init)
917 module_exit(alsa_card_interwave_exit)
918