au88x0.c 10.0 KB

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