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