vserver 1.9.3
[linux-2.6.git] / sound / pci / au88x0 / au88x0.c
1 /*
2  * ALSA driver for the Aureal Vortex family of soundprocessors.
3  * Author: Manuel Jander (mjander@embedded.cl)
4  *
5  *   This driver is the result of the OpenVortex Project from Savannah
6  * (savannah.nongnu.org/projects/openvortex). I would like to thank
7  * the developers of OpenVortex, Jeff Muizelar and Kester Maddock, from
8  * whom i got plenty of help, and their codebase was invaluable.
9  *   Thanks to the ALSA developers, they helped a lot working out
10  * the ALSA part.
11  *   Thanks also to Sourceforge for maintaining the old binary drivers,
12  * and the forum, where developers could comunicate.
13  *
14  * Now at least i can play Legacy DOOM with MIDI music :-)
15  */
16
17 #include "au88x0.h"
18 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/moduleparam.h>
23 #include <sound/initval.h>
24
25 // module parameters (see "Module Parameters")
26 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
27 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
28 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
29 static int pcifix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 255 };
30 static int boot_devs;
31
32 module_param_array(index, int, boot_devs, 0444);
33 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
34 module_param_array(id, charp, boot_devs, 0444);
35 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
36 module_param_array(enable, bool, boot_devs, 0444);
37 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
38 module_param_array(pcifix, int, boot_devs, 0444);
39 MODULE_PARM_DESC(pcifix, "Enable VIA-workaround for " CARD_NAME " soundcard.");
40
41 MODULE_DESCRIPTION("Aureal vortex");
42 MODULE_LICENSE("GPL");
43 MODULE_SUPPORTED_DEVICE("{{Aureal Semiconductor Inc., Aureal Vortex Sound Processor}}");
44
45 MODULE_DEVICE_TABLE(pci, snd_vortex_ids);
46
47 static void vortex_fix_latency(struct pci_dev *vortex)
48 {
49         int rc;
50         if (!(rc = pci_write_config_byte(vortex, 0x40, 0xff))) {
51                         printk(KERN_INFO CARD_NAME
52                                ": vortex latency is 0xff\n");
53         } else {
54                 printk(KERN_WARNING CARD_NAME
55                                 ": could not set vortex latency: pci error 0x%x\n", rc);
56         }
57 }
58
59 static void vortex_fix_agp_bridge(struct pci_dev *via)
60 {
61         int rc;
62         u8 value;
63
64         /*
65          * only set the bit (Extend PCI#2 Internal Master for
66          * Efficient Handling of Dummy Requests) if the can
67          * read the config and it is not already set
68          */
69
70         if (!(rc = pci_read_config_byte(via, 0x42, &value))
71                         && ((value & 0x10)
72                                 || !(rc = pci_write_config_byte(via, 0x42, value | 0x10)))) {
73                 printk(KERN_INFO CARD_NAME
74                                 ": bridge config is 0x%x\n", value | 0x10);
75         } else {
76                 printk(KERN_WARNING CARD_NAME
77                                 ": could not set vortex latency: pci error 0x%x\n", rc);
78         }
79 }
80
81 static void __devinit snd_vortex_workaround(struct pci_dev *vortex, int fix)
82 {
83         struct pci_dev *via;
84
85         /* autodetect if workarounds are required */
86         if (fix == 255) {
87                 /* VIA KT133 */
88                 via = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8365_1, NULL);
89                 /* VIA Apollo */
90                 if (via == NULL) {
91                         via = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C598_1, NULL);
92                 }
93                 /* AMD Irongate */
94                 if (via == NULL) {
95                         via = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_7007, NULL);
96                 }
97                 if (via) {
98                         printk(KERN_INFO CARD_NAME ": Activating latency workaround...\n");
99                         vortex_fix_latency(vortex);
100                         vortex_fix_agp_bridge(via);
101                 }
102         } else {
103                 if (fix & 0x1)
104                         vortex_fix_latency(vortex);
105                 if ((fix & 0x2) && (via = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8365_1, NULL)))
106                         vortex_fix_agp_bridge(via);
107                 if ((fix & 0x4) && (via = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C598_1, NULL)))
108                         vortex_fix_agp_bridge(via);
109                 if ((fix & 0x8) && (via = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_7007, NULL)))
110                         vortex_fix_agp_bridge(via);
111         }
112 }
113
114 // component-destructor
115 // (see "Management of Cards and Components")
116 static int snd_vortex_dev_free(snd_device_t * device)
117 {
118         vortex_t *vortex = device->device_data;
119
120         vortex_gameport_unregister(vortex);
121         vortex_core_shutdown(vortex);
122         // Take down PCI interface.
123         synchronize_irq(vortex->irq);
124         free_irq(vortex->irq, vortex);
125         pci_release_regions(vortex->pci_dev);
126         pci_disable_device(vortex->pci_dev);
127         kfree(vortex);
128
129         return 0;
130 }
131
132 // chip-specific constructor
133 // (see "Management of Cards and Components")
134 static int __devinit
135 snd_vortex_create(snd_card_t * card, struct pci_dev *pci, vortex_t ** rchip)
136 {
137         vortex_t *chip;
138         int err;
139         static snd_device_ops_t ops = {
140                 .dev_free = snd_vortex_dev_free,
141         };
142
143         *rchip = NULL;
144
145         // check PCI availability (DMA).
146         if ((err = pci_enable_device(pci)) < 0)
147                 return err;
148         if (!pci_dma_supported(pci, VORTEX_DMA_MASK)) {
149                 printk(KERN_ERR "error to set DMA mask\n");
150                 return -ENXIO;
151         }
152         pci_set_dma_mask(pci, VORTEX_DMA_MASK);
153
154         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
155         if (chip == NULL)
156                 return -ENOMEM;
157
158         chip->card = card;
159
160         // initialize the stuff
161         chip->pci_dev = pci;
162         chip->io = pci_resource_start(pci, 0);
163         chip->vendor = pci->vendor;
164         chip->device = pci->device;
165         chip->card = card;
166         chip->irq = -1;
167
168         // (1) PCI resource allocation
169         // Get MMIO area
170         //
171         if ((err = pci_request_regions(pci, CARD_NAME_SHORT)) != 0)
172                 goto regions_out;
173
174         chip->mmio =
175             ioremap_nocache(pci_resource_start(pci, 0),
176                             pci_resource_len(pci, 0));
177         if (!chip->mmio) {
178                 printk(KERN_ERR "MMIO area remap failed.\n");
179                 err = -ENOMEM;
180                 goto ioremap_out;
181         }
182
183         /* Init audio core.
184          * This must be done before we do request_irq otherwise we can get spurious
185          * interupts that we do not handle properly and make a mess of things */
186         if ((err = vortex_core_init(chip)) != 0) {
187                 printk(KERN_ERR "hw core init failed\n");
188                 goto core_out;
189         }
190
191         if ((err =
192              request_irq(pci->irq, vortex_interrupt,
193                          SA_INTERRUPT | SA_SHIRQ, CARD_NAME_SHORT,
194                          (void *)chip)) != 0) {
195                 printk(KERN_ERR "cannot grab irq\n");
196                 goto irq_out;
197         }
198         chip->irq = pci->irq;
199
200         pci_set_master(pci);
201         // End of PCI setup.
202
203         // Register alsa root device.
204         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
205                 goto alloc_out;
206         }
207
208         *rchip = chip;
209
210         return 0;
211
212       alloc_out:
213         synchronize_irq(chip->irq);
214         free_irq(chip->irq, chip);
215       irq_out:
216         vortex_core_shutdown(chip);
217       core_out:
218         //FIXME: the type of chip->mmio might need to be changed??
219         iounmap((void *)chip->mmio);
220       ioremap_out:
221         pci_release_regions(chip->pci_dev);
222       regions_out:
223         pci_disable_device(chip->pci_dev);
224         //FIXME: this not the right place to unregister the gameport
225         vortex_gameport_unregister(chip);
226         return err;
227 }
228
229 // constructor -- see "Constructor" sub-section
230 static int __devinit
231 snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
232 {
233         static int dev;
234         snd_card_t *card;
235         vortex_t *chip;
236         int err;
237
238         // (1)
239         if (dev >= SNDRV_CARDS)
240                 return -ENODEV;
241         if (!enable[dev]) {
242                 dev++;
243                 return -ENOENT;
244         }
245         // (2)
246         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
247         if (card == NULL)
248                 return -ENOMEM;
249
250         // (3)
251         if ((err = snd_vortex_create(card, pci, &chip)) < 0) {
252                 snd_card_free(card);
253                 return err;
254         }
255         snd_vortex_workaround(pci, pcifix[dev]);
256         // (4) Alloc components.
257         // ADB pcm.
258         if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_ADB, NR_ADB)) < 0) {
259                 snd_card_free(card);
260                 return err;
261         }
262 #ifndef CHIP_AU8820
263         // ADB SPDIF
264         if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_SPDIF, 1)) < 0) {
265                 snd_card_free(card);
266                 return err;
267         }
268         // A3D
269         if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_A3D, NR_A3D)) < 0) {
270                 snd_card_free(card);
271                 return err;
272         }
273 #endif
274         /*
275            // ADB I2S
276            if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_I2S, 1)) < 0) {
277            snd_card_free(card);
278            return err;
279            }
280          */
281 #ifndef CHIP_AU8810
282         // WT pcm.
283         if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_WT, NR_WT)) < 0) {
284                 snd_card_free(card);
285                 return err;
286         }
287 #endif
288         // snd_ac97_mixer and Vortex mixer.
289         if ((err = snd_vortex_mixer(chip)) < 0) {
290                 snd_card_free(card);
291                 return err;
292         }
293         if ((err = snd_vortex_midi(chip)) < 0) {
294                 snd_card_free(card);
295                 return err;
296         }
297         if ((err = vortex_gameport_register(chip)) < 0) {
298                 snd_card_free(card);
299                 return err;
300         }
301 #if 0
302         if (snd_seq_device_new(card, 1, SNDRV_SEQ_DEV_ID_VORTEX_SYNTH,
303                                sizeof(snd_vortex_synth_arg_t), &wave) < 0
304             || wave == NULL) {
305                 snd_printk("Can't initialize Aureal wavetable synth\n");
306         } else {
307                 snd_vortex_synth_arg_t *arg;
308
309                 arg = SNDRV_SEQ_DEVICE_ARGPTR(wave);
310                 strcpy(wave->name, "Aureal Synth");
311                 arg->hwptr = vortex;
312                 arg->index = 1;
313                 arg->seq_ports = seq_ports[dev];
314                 arg->max_voices = max_synth_voices[dev];
315         }
316 #endif
317
318         // (5)
319         strcpy(card->driver, "Aureal Vortex");
320         strcpy(card->shortname, CARD_NAME_SHORT);
321         sprintf(card->longname, "%s at 0x%lx irq %i",
322                 card->shortname, chip->io, chip->irq);
323
324         if ((err = pci_read_config_word(pci, PCI_DEVICE_ID,
325                                   &(chip->device))) < 0) {
326                 snd_card_free(card);
327                 return err;
328         }       
329         if ((err = pci_read_config_word(pci, PCI_VENDOR_ID,
330                                   &(chip->vendor))) < 0) {
331                 snd_card_free(card);
332                 return err;
333         }
334         if ((err = pci_read_config_byte(pci, PCI_REVISION_ID,
335                                   &(chip->rev))) < 0) {
336                 snd_card_free(card);
337                 return err;
338         }
339 #ifdef CHIP_AU8830
340         if ((chip->rev) != 0xfe && (chip->rev) != 0xfa) {
341                 printk(KERN_ALERT
342                        "vortex: The revision (%x) of your card has not been seen before.\n",
343                        chip->rev);
344                 printk(KERN_ALERT
345                        "vortex: Please email the results of 'lspci -vv' to openvortex-dev@nongnu.org.\n");
346                 snd_card_free(card);
347                 err = -ENODEV;
348                 return err;
349         }
350 #endif
351
352         // (6)
353         if ((err = snd_card_register(card)) < 0) {
354                 snd_card_free(card);
355                 return err;
356         }
357         // (7)
358         pci_set_drvdata(pci, card);
359         dev++;
360         vortex_connect_default(chip, 1);
361         vortex_enable_int(chip);
362         return 0;
363 }
364
365 // destructor -- see "Destructor" sub-section
366 static void __devexit snd_vortex_remove(struct pci_dev *pci)
367 {
368         snd_card_free(pci_get_drvdata(pci));
369         pci_set_drvdata(pci, NULL);
370 }
371
372 // pci_driver definition
373 static struct pci_driver driver = {
374         .name = CARD_NAME_SHORT,
375         .id_table = snd_vortex_ids,
376         .probe = snd_vortex_probe,
377         .remove = __devexit_p(snd_vortex_remove),
378 };
379
380 // initialization of the module
381 static int __init alsa_card_vortex_init(void)
382 {
383         return pci_module_init(&driver);
384 }
385
386 // clean up the module
387 static void __exit alsa_card_vortex_exit(void)
388 {
389         pci_unregister_driver(&driver);
390 }
391
392 module_init(alsa_card_vortex_init)
393 module_exit(alsa_card_vortex_exit)