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