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