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