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