au88x0.c 10 KB

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