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