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