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