au88x0.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. dev_info(&vortex->dev, "vortex latency is 0xff\n");
  46. } else {
  47. dev_warn(&vortex->dev,
  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. dev_info(&via->dev, "bridge config is 0x%x\n", value | 0x10);
  64. } else {
  65. dev_warn(&via->dev,
  66. "could not set vortex latency: pci error 0x%x\n", rc);
  67. }
  68. }
  69. static void snd_vortex_workaround(struct pci_dev *vortex, int fix)
  70. {
  71. struct pci_dev *via = NULL;
  72. /* autodetect if workarounds are required */
  73. if (fix == 255) {
  74. /* VIA KT133 */
  75. via = pci_get_device(PCI_VENDOR_ID_VIA,
  76. PCI_DEVICE_ID_VIA_8365_1, NULL);
  77. /* VIA Apollo */
  78. if (via == NULL) {
  79. via = pci_get_device(PCI_VENDOR_ID_VIA,
  80. PCI_DEVICE_ID_VIA_82C598_1, NULL);
  81. /* AMD Irongate */
  82. if (via == NULL)
  83. via = pci_get_device(PCI_VENDOR_ID_AMD,
  84. PCI_DEVICE_ID_AMD_FE_GATE_7007, NULL);
  85. }
  86. if (via) {
  87. dev_info(&vortex->dev,
  88. "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(struct snd_device *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. free_irq(vortex->irq, vortex);
  116. iounmap(vortex->mmio);
  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
  125. snd_vortex_create(struct snd_card *card, struct pci_dev *pci, vortex_t ** rchip)
  126. {
  127. vortex_t *chip;
  128. int err;
  129. static struct snd_device_ops 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 (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
  137. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
  138. dev_err(card->dev, "error to set DMA mask\n");
  139. pci_disable_device(pci);
  140. return -ENXIO;
  141. }
  142. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  143. if (chip == NULL) {
  144. pci_disable_device(pci);
  145. return -ENOMEM;
  146. }
  147. chip->card = card;
  148. // initialize the stuff
  149. chip->pci_dev = pci;
  150. chip->io = pci_resource_start(pci, 0);
  151. chip->vendor = pci->vendor;
  152. chip->device = pci->device;
  153. chip->card = card;
  154. chip->irq = -1;
  155. // (1) PCI resource allocation
  156. // Get MMIO area
  157. //
  158. if ((err = pci_request_regions(pci, CARD_NAME_SHORT)) != 0)
  159. goto regions_out;
  160. chip->mmio = pci_ioremap_bar(pci, 0);
  161. if (!chip->mmio) {
  162. dev_err(card->dev, "MMIO area remap failed.\n");
  163. err = -ENOMEM;
  164. goto ioremap_out;
  165. }
  166. /* Init audio core.
  167. * This must be done before we do request_irq otherwise we can get spurious
  168. * interrupts that we do not handle properly and make a mess of things */
  169. if ((err = vortex_core_init(chip)) != 0) {
  170. dev_err(card->dev, "hw core init failed\n");
  171. goto core_out;
  172. }
  173. if ((err = request_irq(pci->irq, vortex_interrupt,
  174. IRQF_SHARED, KBUILD_MODNAME,
  175. chip)) != 0) {
  176. dev_err(card->dev, "cannot grab irq\n");
  177. goto irq_out;
  178. }
  179. chip->irq = pci->irq;
  180. pci_set_master(pci);
  181. // End of PCI setup.
  182. // Register alsa root device.
  183. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  184. goto alloc_out;
  185. }
  186. *rchip = chip;
  187. return 0;
  188. alloc_out:
  189. free_irq(chip->irq, chip);
  190. irq_out:
  191. vortex_core_shutdown(chip);
  192. core_out:
  193. iounmap(chip->mmio);
  194. ioremap_out:
  195. pci_release_regions(chip->pci_dev);
  196. regions_out:
  197. pci_disable_device(chip->pci_dev);
  198. //FIXME: this not the right place to unregister the gameport
  199. vortex_gameport_unregister(chip);
  200. kfree(chip);
  201. return err;
  202. }
  203. // constructor -- see "Constructor" sub-section
  204. static int
  205. snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  206. {
  207. static int dev;
  208. struct snd_card *card;
  209. vortex_t *chip;
  210. int err;
  211. // (1)
  212. if (dev >= SNDRV_CARDS)
  213. return -ENODEV;
  214. if (!enable[dev]) {
  215. dev++;
  216. return -ENOENT;
  217. }
  218. // (2)
  219. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  220. 0, &card);
  221. if (err < 0)
  222. return err;
  223. // (3)
  224. if ((err = snd_vortex_create(card, pci, &chip)) < 0) {
  225. snd_card_free(card);
  226. return err;
  227. }
  228. snd_vortex_workaround(pci, pcifix[dev]);
  229. // Card details needed in snd_vortex_midi
  230. strcpy(card->driver, CARD_NAME_SHORT);
  231. sprintf(card->shortname, "Aureal Vortex %s", CARD_NAME_SHORT);
  232. sprintf(card->longname, "%s at 0x%lx irq %i",
  233. card->shortname, chip->io, chip->irq);
  234. // (4) Alloc components.
  235. err = snd_vortex_mixer(chip);
  236. if (err < 0) {
  237. snd_card_free(card);
  238. return err;
  239. }
  240. // ADB pcm.
  241. err = snd_vortex_new_pcm(chip, VORTEX_PCM_ADB, NR_PCM);
  242. if (err < 0) {
  243. snd_card_free(card);
  244. return err;
  245. }
  246. #ifndef CHIP_AU8820
  247. // ADB SPDIF
  248. if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_SPDIF, 1)) < 0) {
  249. snd_card_free(card);
  250. return err;
  251. }
  252. // A3D
  253. if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_A3D, NR_A3D)) < 0) {
  254. snd_card_free(card);
  255. return err;
  256. }
  257. #endif
  258. /*
  259. // ADB I2S
  260. if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_I2S, 1)) < 0) {
  261. snd_card_free(card);
  262. return err;
  263. }
  264. */
  265. #ifndef CHIP_AU8810
  266. // WT pcm.
  267. if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_WT, NR_WT)) < 0) {
  268. snd_card_free(card);
  269. return err;
  270. }
  271. #endif
  272. if ((err = snd_vortex_midi(chip)) < 0) {
  273. snd_card_free(card);
  274. return err;
  275. }
  276. vortex_gameport_register(chip);
  277. #if 0
  278. if (snd_seq_device_new(card, 1, SNDRV_SEQ_DEV_ID_VORTEX_SYNTH,
  279. sizeof(snd_vortex_synth_arg_t), &wave) < 0
  280. || wave == NULL) {
  281. dev_err(card->dev, "Can't initialize Aureal wavetable synth\n");
  282. } else {
  283. snd_vortex_synth_arg_t *arg;
  284. arg = SNDRV_SEQ_DEVICE_ARGPTR(wave);
  285. strcpy(wave->name, "Aureal Synth");
  286. arg->hwptr = vortex;
  287. arg->index = 1;
  288. arg->seq_ports = seq_ports[dev];
  289. arg->max_voices = max_synth_voices[dev];
  290. }
  291. #endif
  292. // (5)
  293. if ((err = pci_read_config_word(pci, PCI_DEVICE_ID,
  294. &(chip->device))) < 0) {
  295. snd_card_free(card);
  296. return err;
  297. }
  298. if ((err = pci_read_config_word(pci, PCI_VENDOR_ID,
  299. &(chip->vendor))) < 0) {
  300. snd_card_free(card);
  301. return err;
  302. }
  303. chip->rev = pci->revision;
  304. #ifdef CHIP_AU8830
  305. if ((chip->rev) != 0xfe && (chip->rev) != 0xfa) {
  306. dev_alert(card->dev,
  307. "The revision (%x) of your card has not been seen before.\n",
  308. chip->rev);
  309. dev_alert(card->dev,
  310. "Please email the results of 'lspci -vv' to openvortex-dev@nongnu.org.\n");
  311. snd_card_free(card);
  312. err = -ENODEV;
  313. return err;
  314. }
  315. #endif
  316. // (6)
  317. if ((err = snd_card_register(card)) < 0) {
  318. snd_card_free(card);
  319. return err;
  320. }
  321. // (7)
  322. pci_set_drvdata(pci, card);
  323. dev++;
  324. vortex_connect_default(chip, 1);
  325. vortex_enable_int(chip);
  326. return 0;
  327. }
  328. // destructor -- see "Destructor" sub-section
  329. static void snd_vortex_remove(struct pci_dev *pci)
  330. {
  331. snd_card_free(pci_get_drvdata(pci));
  332. }
  333. // pci_driver definition
  334. static struct pci_driver vortex_driver = {
  335. .name = KBUILD_MODNAME,
  336. .id_table = snd_vortex_ids,
  337. .probe = snd_vortex_probe,
  338. .remove = snd_vortex_remove,
  339. };
  340. module_pci_driver(vortex_driver);