a04a9d3253f88df0afece5520002571e5ab392a1
[releases.git] / interwave.c
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_gus(struct snd_card *card, int dev,
622                                    struct snd_gus_card **gusp)
623 {
624         return snd_gus_create(card, port[dev], -irq[dev], dma1[dev], dma2[dev],
625                               0, 32, pcm_channels[dev], effect[dev], gusp);
626 }
627
628 static int snd_interwave_probe(struct snd_card *card, int dev,
629                                struct snd_gus_card *gus)
630 {
631         int xirq, xdma1, xdma2;
632         struct snd_interwave *iwcard = card->private_data;
633         struct snd_wss *wss;
634 #ifdef SNDRV_STB
635         struct snd_i2c_bus *i2c_bus;
636 #endif
637         char *str;
638         int err;
639
640         xirq = irq[dev];
641         xdma1 = dma1[dev];
642         xdma2 = dma2[dev];
643
644         err = snd_interwave_detect(iwcard, gus, dev
645 #ifdef SNDRV_STB
646                                    , &i2c_bus
647 #endif
648                                    );
649         if (err < 0)
650                 return err;
651
652         iwcard->gus_status_reg = gus->gf1.reg_irqstat;
653         iwcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
654
655         snd_interwave_init(dev, gus);
656         snd_interwave_detect_memory(gus);
657         err = snd_gus_initialize(gus);
658         if (err < 0)
659                 return err;
660
661         if (devm_request_irq(card->dev, xirq, snd_interwave_interrupt, 0,
662                              "InterWave", iwcard)) {
663                 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
664                 return -EBUSY;
665         }
666         iwcard->irq = xirq;
667         card->sync_irq = iwcard->irq;
668
669         err = snd_wss_create(card,
670                              gus->gf1.port + 0x10c, -1, xirq,
671                              xdma2 < 0 ? xdma1 : xdma2, xdma1,
672                              WSS_HW_INTERWAVE,
673                              WSS_HWSHARE_IRQ |
674                              WSS_HWSHARE_DMA1 |
675                              WSS_HWSHARE_DMA2,
676                              &wss);
677         if (err < 0)
678                 return err;
679
680         err = snd_wss_pcm(wss, 0);
681         if (err < 0)
682                 return err;
683
684         sprintf(wss->pcm->name + strlen(wss->pcm->name), " rev %c",
685                 gus->revision + 'A');
686         strcat(wss->pcm->name, " (codec)");
687
688         err = snd_wss_timer(wss, 2);
689         if (err < 0)
690                 return err;
691
692         err = snd_wss_mixer(wss);
693         if (err < 0)
694                 return err;
695
696         if (pcm_channels[dev] > 0) {
697                 err = snd_gf1_pcm_new(gus, 1, 1);
698                 if (err < 0)
699                         return err;
700         }
701         err = snd_interwave_mixer(wss);
702         if (err < 0)
703                 return err;
704
705 #ifdef SNDRV_STB
706         {
707                 struct snd_ctl_elem_id id1, id2;
708                 memset(&id1, 0, sizeof(id1));
709                 memset(&id2, 0, sizeof(id2));
710                 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
711                 strcpy(id1.name, "Master Playback Switch");
712                 strcpy(id2.name, id1.name);
713                 id2.index = 1;
714                 err = snd_ctl_rename_id(card, &id1, &id2);
715                 if (err < 0)
716                         return err;
717                 strcpy(id1.name, "Master Playback Volume");
718                 strcpy(id2.name, id1.name);
719                 err = snd_ctl_rename_id(card, &id1, &id2);
720                 if (err < 0)
721                         return err;
722                 err = snd_tea6330t_update_mixer(card, i2c_bus, 0, 1);
723                 if (err < 0)
724                         return err;
725         }
726 #endif
727
728         gus->uart_enable = midi[dev];
729         err = snd_gf1_rawmidi_new(gus, 0);
730         if (err < 0)
731                 return err;
732
733 #ifndef SNDRV_STB
734         str = "AMD InterWave";
735         if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8)
736                 str = "Dynasonic 3-D";
737 #else
738         str = "InterWave STB";
739 #endif
740         strcpy(card->driver, str);
741         strcpy(card->shortname, str);
742         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %d",
743                 str,
744                 gus->gf1.port,
745                 xirq,
746                 xdma1);
747         if (xdma2 >= 0)
748                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
749
750         err = snd_card_register(card);
751         if (err < 0)
752                 return err;
753         
754         iwcard->wss = wss;
755         iwcard->gus = gus;
756         return 0;
757 }
758
759 static int snd_interwave_isa_match(struct device *pdev,
760                                    unsigned int dev)
761 {
762         if (!enable[dev])
763                 return 0;
764 #ifdef CONFIG_PNP
765         if (isapnp[dev])
766                 return 0;
767 #endif
768         return 1;
769 }
770
771 static int snd_interwave_isa_probe(struct device *pdev,
772                                    unsigned int dev)
773 {
774         struct snd_card *card;
775         struct snd_gus_card *gus;
776         int err;
777         static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
778         static const int possible_dmas[] = {0, 1, 3, 5, 6, 7, -1};
779
780         if (irq[dev] == SNDRV_AUTO_IRQ) {
781                 irq[dev] = snd_legacy_find_free_irq(possible_irqs);
782                 if (irq[dev] < 0) {
783                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
784                         return -EBUSY;
785                 }
786         }
787         if (dma1[dev] == SNDRV_AUTO_DMA) {
788                 dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
789                 if (dma1[dev] < 0) {
790                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
791                         return -EBUSY;
792                 }
793         }
794         if (dma2[dev] == SNDRV_AUTO_DMA) {
795                 dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
796                 if (dma2[dev] < 0) {
797                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
798                         return -EBUSY;
799                 }
800         }
801
802         err = snd_interwave_card_new(pdev, dev, &card);
803         if (err < 0)
804                 return err;
805
806         if (port[dev] != SNDRV_AUTO_PORT)
807                 err = snd_interwave_probe_gus(card, dev, &gus);
808         else {
809                 static const long possible_ports[] = {0x210, 0x220, 0x230, 0x240, 0x250, 0x260};
810                 int i;
811                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
812                         port[dev] = possible_ports[i];
813                         err = snd_interwave_probe_gus(card, dev, &gus);
814                         if (! err)
815                                 return 0;
816                 }
817         }
818         if (err < 0)
819                 return err;
820
821         err = snd_interwave_probe(card, dev, gus);
822         if (err < 0)
823                 return err;
824
825         dev_set_drvdata(pdev, card);
826         return 0;
827 }
828
829 static struct isa_driver snd_interwave_driver = {
830         .match          = snd_interwave_isa_match,
831         .probe          = snd_interwave_isa_probe,
832         /* FIXME: suspend,resume */
833         .driver         = {
834                 .name   = INTERWAVE_DRIVER
835         },
836 };
837
838 #ifdef CONFIG_PNP
839 static int snd_interwave_pnp_detect(struct pnp_card_link *pcard,
840                                     const struct pnp_card_device_id *pid)
841 {
842         static int dev;
843         struct snd_card *card;
844         struct snd_gus_card *gus;
845         int res;
846
847         for ( ; dev < SNDRV_CARDS; dev++) {
848                 if (enable[dev] && isapnp[dev])
849                         break;
850         }
851         if (dev >= SNDRV_CARDS)
852                 return -ENODEV;
853                                 
854         res = snd_interwave_card_new(&pcard->card->dev, dev, &card);
855         if (res < 0)
856                 return res;
857
858         res = snd_interwave_pnp(dev, card->private_data, pcard, pid);
859         if (res < 0)
860                 return res;
861         res = snd_interwave_probe_gus(card, dev, &gus);
862         if (res < 0)
863                 return res;
864         res = snd_interwave_probe(card, dev, gus);
865         if (res < 0)
866                 return res;
867         pnp_set_card_drvdata(pcard, card);
868         dev++;
869         return 0;
870 }
871
872 static struct pnp_card_driver interwave_pnpc_driver = {
873         .flags = PNP_DRIVER_RES_DISABLE,
874         .name = INTERWAVE_PNP_DRIVER,
875         .id_table = snd_interwave_pnpids,
876         .probe = snd_interwave_pnp_detect,
877         /* FIXME: suspend,resume */
878 };
879
880 #endif /* CONFIG_PNP */
881
882 static int __init alsa_card_interwave_init(void)
883 {
884         int err;
885
886         err = isa_register_driver(&snd_interwave_driver, SNDRV_CARDS);
887 #ifdef CONFIG_PNP
888         if (!err)
889                 isa_registered = 1;
890
891         err = pnp_register_card_driver(&interwave_pnpc_driver);
892         if (!err)
893                 pnp_registered = 1;
894
895         if (isa_registered)
896                 err = 0;
897 #endif
898         return err;
899 }
900
901 static void __exit alsa_card_interwave_exit(void)
902 {
903 #ifdef CONFIG_PNP
904         if (pnp_registered)
905                 pnp_unregister_card_driver(&interwave_pnpc_driver);
906         if (isa_registered)
907 #endif
908                 isa_unregister_driver(&snd_interwave_driver);
909 }
910
911 module_init(alsa_card_interwave_init)
912 module_exit(alsa_card_interwave_exit)