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