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