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