au88x0.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <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 int 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. synchronize_irq(vortex->irq);
  117. free_irq(vortex->irq, vortex);
  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_32BIT_MASK)) {
  138. printk(KERN_ERR "error to set DMA mask\n");
  139. return -ENXIO;
  140. }
  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. struct snd_card *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(KERN_ERR "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. .id_table = snd_vortex_ids,
  337. .probe = snd_vortex_probe,
  338. .remove = __devexit_p(snd_vortex_remove),
  339. };
  340. // initialization of the module
  341. static int __init alsa_card_vortex_init(void)
  342. {
  343. return pci_register_driver(&driver);
  344. }
  345. // clean up the module
  346. static void __exit alsa_card_vortex_exit(void)
  347. {
  348. pci_unregister_driver(&driver);
  349. }
  350. module_init(alsa_card_vortex_init)
  351. module_exit(alsa_card_vortex_exit)