au88x0.c 10 KB

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