Linux Audio

Check our new training course

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