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