vserver 1.9.3
[linux-2.6.git] / sound / oss / cs4232.c
1 /*
2  * Copyright (C) by Hannu Savolainen 1993-1997
3  *
4  *      cs4232.c
5  *
6  * The low level driver for Crystal CS4232 based cards. The CS4232 is
7  * a PnP compatible chip which contains a CS4231A codec, SB emulation,
8  * a MPU401 compatible MIDI port, joystick and synthesizer and IDE CD-ROM 
9  * interfaces. This is just a temporary driver until full PnP support
10  * gets implemented. Just the WSS codec, FM synth and the MIDI ports are
11  * supported. Other interfaces are left uninitialized.
12  *
13  * ifdef ...WAVEFRONT...
14  * 
15  *   Support is provided for initializing the WaveFront synth
16  *   interface as well, which is logical device #4. Note that if
17  *   you have a Tropez+ card, you probably don't need to setup
18  *   the CS4232-supported MIDI interface, since it corresponds to
19  *   the internal 26-pin header that's hard to access. Using this
20  *   requires an additional IRQ, a resource none too plentiful in
21  *   this environment. Just don't set module parameters mpuio and
22  *   mpuirq, and the MIDI port will be left uninitialized. You can
23  *   still use the ICS2115 hosted MIDI interface which corresponds
24  *   to the 9-pin D connector on the back of the card.
25  *
26  * endif  ...WAVEFRONT...
27  *
28  * Supported chips are:
29  *      CS4232
30  *      CS4236
31  *      CS4236B
32  *
33  * Note: You will need a PnP config setup to initialise some CS4232 boards
34  * anyway.
35  *
36  * Changes
37  *      John Rood               Added Bose Sound System Support.
38  *      Toshio Spoor
39  *      Alan Cox                Modularisation, Basic cleanups.
40  *      Paul Barton-Davis       Separated MPU configuration, added
41  *                                       Tropez+ (WaveFront) support
42  *      Christoph Hellwig       Adapted to module_init/module_exit,
43  *                                      simple cleanups
44  *      Arnaldo C. de Melo      got rid of attach_uart401
45  *      Bartlomiej Zolnierkiewicz
46  *                              Added some __init/__initdata/__exit
47  *      Marcus Meissner         Added ISA PnP support.
48  */
49
50 #include <linux/config.h>
51 #include <linux/pnp.h>
52 #include <linux/module.h>
53 #include <linux/init.h>
54
55 #include "sound_config.h"
56
57 #include "cs4232.h"
58 #include "ad1848.h"
59 #include "mpu401.h"
60
61 #define KEY_PORT        0x279   /* Same as LPT1 status port */
62 #define CSN_NUM         0x99    /* Just a random number */
63 #define INDEX_ADDRESS   0x00    /* (R0) Index Address Register */
64 #define INDEX_DATA      0x01    /* (R1) Indexed Data Register */
65 #define PIN_CONTROL     0x0a    /* (I10) Pin Control */
66 #define ENABLE_PINS     0xc0    /* XCTRL0/XCTRL1 enable */
67
68 static void CS_OUT(unsigned char a)
69 {
70         outb(a, KEY_PORT);
71 }
72
73 #define CS_OUT2(a, b)           {CS_OUT(a);CS_OUT(b);}
74 #define CS_OUT3(a, b, c)        {CS_OUT(a);CS_OUT(b);CS_OUT(c);}
75
76 static int __initdata bss       = 0;
77 static int mpu_base, mpu_irq;
78 static int synth_base, synth_irq;
79 static int mpu_detected;
80
81 int probe_cs4232_mpu(struct address_info *hw_config)
82 {
83         /*
84          *      Just write down the config values.
85          */
86
87         mpu_base = hw_config->io_base;
88         mpu_irq = hw_config->irq;
89
90         return 1;
91 }
92
93 static unsigned char crystal_key[] =    /* A 32 byte magic key sequence */
94 {
95         0x96, 0x35, 0x9a, 0xcd, 0xe6, 0xf3, 0x79, 0xbc,
96         0x5e, 0xaf, 0x57, 0x2b, 0x15, 0x8a, 0xc5, 0xe2,
97         0xf1, 0xf8, 0x7c, 0x3e, 0x9f, 0x4f, 0x27, 0x13,
98         0x09, 0x84, 0x42, 0xa1, 0xd0, 0x68, 0x34, 0x1a
99 };
100
101 static void sleep(unsigned howlong)
102 {
103         current->state = TASK_INTERRUPTIBLE;
104         schedule_timeout(howlong);
105 }
106
107 static void enable_xctrl(int baseio)
108 {
109         unsigned char regd;
110                 
111         /*
112          * Some IBM Aptiva's have the Bose Sound System. By default
113          * the Bose Amplifier is disabled. The amplifier will be 
114          * activated, by setting the XCTRL0 and XCTRL1 bits.
115          * Volume of the monitor bose speakers/woofer, can then
116          * be set by changing the PCM volume.
117          *
118          */
119                 
120         printk("cs4232: enabling Bose Sound System Amplifier.\n");
121         
122         /* Switch to Pin Control Address */                   
123         regd = inb(baseio + INDEX_ADDRESS) & 0xe0;
124         outb(((unsigned char) (PIN_CONTROL | regd)), baseio + INDEX_ADDRESS );
125         
126         /* Activate the XCTRL0 and XCTRL1 Pins */
127         regd = inb(baseio + INDEX_DATA);
128         outb(((unsigned char) (ENABLE_PINS | regd)), baseio + INDEX_DATA );
129 }
130
131 static int __init probe_cs4232(struct address_info *hw_config, int isapnp_configured)
132 {
133         int i, n;
134         int base = hw_config->io_base, irq = hw_config->irq;
135         int dma1 = hw_config->dma, dma2 = hw_config->dma2;
136         struct resource *ports;
137
138         if (base == -1 || irq == -1 || dma1 == -1) {
139                 printk(KERN_ERR "cs4232: dma, irq and io must be set.\n");
140                 return 0;
141         }
142
143         /*
144          * Verify that the I/O port range is free.
145          */
146
147         ports = request_region(base, 4, "ad1848");
148         if (!ports) {
149                 printk(KERN_ERR "cs4232.c: I/O port 0x%03x not free\n", base);
150                 return 0;
151         }
152         if (ad1848_detect(ports, NULL, hw_config->osp)) {
153                 goto got_it;    /* The card is already active */
154         }
155         if (isapnp_configured) {
156                 printk(KERN_ERR "cs4232.c: ISA PnP configured, but not detected?\n");
157                 goto fail;
158         }
159
160         /*
161          * This version of the driver doesn't use the PnP method when configuring
162          * the card but a simplified method defined by Crystal. This means that
163          * just one CS4232 compatible device can exist on the system. Also this
164          * method conflicts with possible PnP support in the OS. For this reason 
165          * driver is just a temporary kludge.
166          *
167          * Also the Cirrus/Crystal method doesn't always work. Try ISA PnP first ;)
168          */
169
170         /*
171          * Repeat initialization few times since it doesn't always succeed in
172          * first time.
173          */
174
175         for (n = 0; n < 4; n++)
176         {       
177                 /*
178                  *      Wake up the card by sending a 32 byte Crystal key to the key port.
179                  */
180                 
181                 for (i = 0; i < 32; i++)
182                         CS_OUT(crystal_key[i]);
183
184                 sleep(HZ / 10);
185
186                 /*
187                  *      Now set the CSN (Card Select Number).
188                  */
189
190                 CS_OUT2(0x06, CSN_NUM);
191
192                 /*
193                  *      Then set some config bytes. First logical device 0 
194                  */
195
196                 CS_OUT2(0x15, 0x00);    /* Select logical device 0 (WSS/SB/FM) */
197                 CS_OUT3(0x47, (base >> 8) & 0xff, base & 0xff); /* WSS base */
198
199                 if (check_region(0x388, 4))     /* Not free */
200                         CS_OUT3(0x48, 0x00, 0x00)       /* FM base off */
201                 else
202                         CS_OUT3(0x48, 0x03, 0x88);      /* FM base 0x388 */
203
204                 CS_OUT3(0x42, 0x00, 0x00);      /* SB base off */
205                 CS_OUT2(0x22, irq);             /* SB+WSS IRQ */
206                 CS_OUT2(0x2a, dma1);            /* SB+WSS DMA */
207
208                 if (dma2 != -1)
209                         CS_OUT2(0x25, dma2)     /* WSS DMA2 */
210                 else
211                         CS_OUT2(0x25, 4);       /* No WSS DMA2 */
212
213                 CS_OUT2(0x33, 0x01);    /* Activate logical dev 0 */
214
215                 sleep(HZ / 10);
216
217                 /*
218                  * Initialize logical device 3 (MPU)
219                  */
220
221                 if (mpu_base != 0 && mpu_irq != 0)
222                 {
223                         CS_OUT2(0x15, 0x03);    /* Select logical device 3 (MPU) */
224                         CS_OUT3(0x47, (mpu_base >> 8) & 0xff, mpu_base & 0xff); /* MPU base */
225                         CS_OUT2(0x22, mpu_irq); /* MPU IRQ */
226                         CS_OUT2(0x33, 0x01);    /* Activate logical dev 3 */
227                 }
228
229                 if(synth_base != 0)
230                 {
231                     CS_OUT2 (0x15, 0x04);               /* logical device 4 (WaveFront) */
232                     CS_OUT3 (0x47, (synth_base >> 8) & 0xff,
233                              synth_base & 0xff);        /* base */
234                     CS_OUT2 (0x22, synth_irq);          /* IRQ */
235                     CS_OUT2 (0x33, 0x01);               /* Activate logical dev 4 */
236                 }
237
238                 /*
239                  * Finally activate the chip
240                  */
241                 
242                 CS_OUT(0x79);
243
244                 sleep(HZ / 5);
245
246                 /*
247                  * Then try to detect the codec part of the chip
248                  */
249
250                 if (ad1848_detect(ports, NULL, hw_config->osp))
251                         goto got_it;
252                 
253                 sleep(HZ);
254         }
255 fail:
256         release_region(base, 4);
257         return 0;
258
259 got_it:
260         if (dma2 == -1)
261                 dma2 = dma1;
262
263         hw_config->slots[0] = ad1848_init("Crystal audio controller", ports,
264                                           irq,
265                                           dma1,         /* Playback DMA */
266                                           dma2,         /* Capture DMA */
267                                           0,
268                                           hw_config->osp,
269                                           THIS_MODULE);
270
271         if (hw_config->slots[0] != -1 &&
272                 audio_devs[hw_config->slots[0]]->mixer_dev!=-1)
273         {       
274                 /* Assume the mixer map is as suggested in the CS4232 databook */
275                 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
276                 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
277                 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_SYNTH);           /* FM synth */
278         }
279         if (mpu_base != 0 && mpu_irq != 0)
280         {
281                 static struct address_info hw_config2 = {
282                         0
283                 };              /* Ensure it's initialized */
284
285                 hw_config2.io_base = mpu_base;
286                 hw_config2.irq = mpu_irq;
287                 hw_config2.dma = -1;
288                 hw_config2.dma2 = -1;
289                 hw_config2.always_detect = 0;
290                 hw_config2.name = NULL;
291                 hw_config2.driver_use_1 = 0;
292                 hw_config2.driver_use_2 = 0;
293                 hw_config2.card_subtype = 0;
294
295                 if (probe_uart401(&hw_config2, THIS_MODULE))
296                 {
297                         mpu_detected = 1;
298                 }
299                 else
300                 {
301                         mpu_base = mpu_irq = 0;
302                 }
303                 hw_config->slots[1] = hw_config2.slots[1];
304         }
305         
306         if (bss)
307                 enable_xctrl(base);
308
309         return 1;
310 }
311
312 static void __devexit unload_cs4232(struct address_info *hw_config)
313 {
314         int base = hw_config->io_base, irq = hw_config->irq;
315         int dma1 = hw_config->dma, dma2 = hw_config->dma2;
316
317         if (dma2 == -1)
318                 dma2 = dma1;
319
320         ad1848_unload(base,
321                       irq,
322                       dma1,     /* Playback DMA */
323                       dma2,     /* Capture DMA */
324                       0);
325
326         sound_unload_audiodev(hw_config->slots[0]);
327         if (mpu_base != 0 && mpu_irq != 0 && mpu_detected)
328         {
329                 static struct address_info hw_config2 =
330                 {
331                         0
332                 };              /* Ensure it's initialized */
333
334                 hw_config2.io_base = mpu_base;
335                 hw_config2.irq = mpu_irq;
336                 hw_config2.dma = -1;
337                 hw_config2.dma2 = -1;
338                 hw_config2.always_detect = 0;
339                 hw_config2.name = NULL;
340                 hw_config2.driver_use_1 = 0;
341                 hw_config2.driver_use_2 = 0;
342                 hw_config2.card_subtype = 0;
343                 hw_config2.slots[1] = hw_config->slots[1];
344
345                 unload_uart401(&hw_config2);
346         }
347 }
348
349 static struct address_info cfg;
350 static struct address_info cfg_mpu;
351
352 static int __initdata io        = -1;
353 static int __initdata irq       = -1;
354 static int __initdata dma       = -1;
355 static int __initdata dma2      = -1;
356 static int __initdata mpuio     = -1;
357 static int __initdata mpuirq    = -1;
358 static int __initdata synthio   = -1;
359 static int __initdata synthirq  = -1;
360 static int __initdata isapnp    = 1;
361
362 MODULE_DESCRIPTION("CS4232 based soundcard driver"); 
363 MODULE_AUTHOR("Hannu Savolainen, Paul Barton-Davis"); 
364 MODULE_LICENSE("GPL");
365
366 MODULE_PARM(io,"i");
367 MODULE_PARM_DESC(io,"base I/O port for AD1848");
368 MODULE_PARM(irq,"i");
369 MODULE_PARM_DESC(irq,"IRQ for AD1848 chip");
370 MODULE_PARM(dma,"i");
371 MODULE_PARM_DESC(dma,"8 bit DMA for AD1848 chip");
372 MODULE_PARM(dma2,"i");
373 MODULE_PARM_DESC(dma2,"16 bit DMA for AD1848 chip");
374 MODULE_PARM(mpuio,"i");
375 MODULE_PARM_DESC(mpuio,"MPU 401 base address");
376 MODULE_PARM(mpuirq,"i");
377 MODULE_PARM_DESC(mpuirq,"MPU 401 IRQ");
378 MODULE_PARM(synthio,"i");
379 MODULE_PARM_DESC(synthio,"Maui WaveTable base I/O port");
380 MODULE_PARM(synthirq,"i");
381 MODULE_PARM_DESC(synthirq,"Maui WaveTable IRQ");
382 MODULE_PARM(isapnp,"i");
383 MODULE_PARM_DESC(isapnp,"Enable ISAPnP probing (default 1)");
384 MODULE_PARM(bss,"i");
385 MODULE_PARM_DESC(bss,"Enable Bose Sound System Support (default 0)");
386
387 /*
388  *      Install a CS4232 based card. Need to have ad1848 and mpu401
389  *      loaded ready.
390  */
391
392 /* All cs4232 based cards have the main ad1848 card either as CSC0000 or
393  * CSC0100. */
394 static const struct pnp_device_id cs4232_pnp_table[] = {
395         { .id = "CSC0100", .driver_data = 0 },
396         { .id = "CSC0000", .driver_data = 0 },
397         /* Guillemot Turtlebeach something appears to be cs4232 compatible
398          * (untested) */
399         { .id = "GIM0100", .driver_data = 0 },
400         { .id = ""}
401 };
402
403 MODULE_DEVICE_TABLE(pnp, cs4232_pnp_table);
404
405 static int cs4232_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
406 {
407         struct address_info *isapnpcfg;
408
409         isapnpcfg=(struct address_info*)kmalloc(sizeof(*isapnpcfg),GFP_KERNEL);
410         if (!isapnpcfg)
411                 return -ENOMEM;
412
413         isapnpcfg->irq          = pnp_irq(dev, 0);
414         isapnpcfg->dma          = pnp_dma(dev, 0);
415         isapnpcfg->dma2         = pnp_dma(dev, 1);
416         isapnpcfg->io_base      = pnp_port_start(dev, 0);
417         if (probe_cs4232(isapnpcfg,TRUE) == 0) {
418                 printk(KERN_ERR "cs4232: ISA PnP card found, but not detected?\n");
419                 kfree(isapnpcfg);
420                 return -ENODEV;
421         }
422         pnp_set_drvdata(dev,isapnpcfg);
423         return 0;
424 }
425
426 static void __devexit cs4232_pnp_remove(struct pnp_dev *dev)
427 {
428         struct address_info *cfg = pnp_get_drvdata(dev);
429         if (cfg) {
430                 unload_cs4232(cfg);
431                 kfree(cfg);
432         }
433 }
434
435 static struct pnp_driver cs4232_driver = {
436         .name           = "cs4232",
437         .id_table       = cs4232_pnp_table,
438         .probe          = cs4232_pnp_probe,
439         .remove         = __devexit_p(cs4232_pnp_remove),
440 };
441
442 static int __init init_cs4232(void)
443 {
444 #ifdef CONFIG_SOUND_WAVEFRONT_MODULE
445         if(synthio == -1)
446                 printk(KERN_INFO "cs4232: set synthio and synthirq to use the wavefront facilities.\n");
447         else {
448                 synth_base = synthio;
449                 synth_irq =  synthirq;
450         }
451 #else
452         if(synthio != -1)
453                 printk(KERN_WARNING "cs4232: wavefront support not enabled in this driver.\n");
454 #endif
455         cfg.irq = -1;
456
457         if (isapnp &&
458             (pnp_register_driver(&cs4232_driver) > 0)
459         )
460                 return 0;
461
462         if(io==-1||irq==-1||dma==-1)
463         {
464                 printk(KERN_ERR "cs4232: Must set io, irq and dma.\n");
465                 return -ENODEV;
466         }
467
468         cfg.io_base = io;
469         cfg.irq = irq;
470         cfg.dma = dma;
471         cfg.dma2 = dma2;
472
473         cfg_mpu.io_base = -1;
474         cfg_mpu.irq = -1;
475
476         if (mpuio != -1 && mpuirq != -1) {
477                 cfg_mpu.io_base = mpuio;
478                 cfg_mpu.irq = mpuirq;
479                 probe_cs4232_mpu(&cfg_mpu); /* Bug always returns 0 not OK -- AC */
480         }
481
482         if (probe_cs4232(&cfg,FALSE) == 0)
483                 return -ENODEV;
484
485         return 0;
486 }
487
488 static void __exit cleanup_cs4232(void)
489 {
490         pnp_unregister_driver(&cs4232_driver);
491         if (cfg.irq != -1)
492                 unload_cs4232(&cfg); /* Unloads global MPU as well, if needed */
493 }
494
495 module_init(init_cs4232);
496 module_exit(cleanup_cs4232);
497
498 #ifndef MODULE
499 static int __init setup_cs4232(char *str)
500 {
501         /* io, irq, dma, dma2 mpuio, mpuirq*/
502         int ints[7];
503
504         /* If we have isapnp cards, no need for options */
505         if (pnp_register_driver(&cs4232_driver) > 0)
506                 return 1;
507         
508         str = get_options(str, ARRAY_SIZE(ints), ints);
509         
510         io      = ints[1];
511         irq     = ints[2];
512         dma     = ints[3];
513         dma2    = ints[4];
514         mpuio   = ints[5];
515         mpuirq  = ints[6];
516
517         return 1;
518 }
519
520 __setup("cs4232=", setup_cs4232);
521 #endif