au88x0.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA driver for the Aureal Vortex family of soundprocessors.
  4. * Author: Manuel Jander ([email protected])
  5. *
  6. * This driver is the result of the OpenVortex Project from Savannah
  7. * (savannah.nongnu.org/projects/openvortex). I would like to thank
  8. * the developers of OpenVortex, Jeff Muizelaar and Kester Maddock, from
  9. * whom i got plenty of help, and their codebase was invaluable.
  10. * Thanks to the ALSA developers, they helped a lot working out
  11. * the ALSA part.
  12. * Thanks also to Sourceforge for maintaining the old binary drivers,
  13. * and the forum, where developers could comunicate.
  14. *
  15. * Now at least i can play Legacy DOOM with MIDI music :-)
  16. */
  17. #include "au88x0.h"
  18. #include <linux/init.h>
  19. #include <linux/pci.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/dma-mapping.h>
  24. #include <sound/initval.h>
  25. // module parameters (see "Module Parameters")
  26. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  27. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  28. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  29. static int pcifix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 255 };
  30. module_param_array(index, int, NULL, 0444);
  31. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  32. module_param_array(id, charp, NULL, 0444);
  33. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  34. module_param_array(enable, bool, NULL, 0444);
  35. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  36. module_param_array(pcifix, int, NULL, 0444);
  37. MODULE_PARM_DESC(pcifix, "Enable VIA-workaround for " CARD_NAME " soundcard.");
  38. MODULE_DESCRIPTION("Aureal vortex");
  39. MODULE_LICENSE("GPL");
  40. MODULE_DEVICE_TABLE(pci, snd_vortex_ids);
  41. static void vortex_fix_latency(struct pci_dev *vortex)
  42. {
  43. int rc;
  44. rc = pci_write_config_byte(vortex, 0x40, 0xff);
  45. if (!rc) {
  46. dev_info(&vortex->dev, "vortex latency is 0xff\n");
  47. } else {
  48. dev_warn(&vortex->dev,
  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. rc = pci_read_config_byte(via, 0x42, &value);
  62. if (!rc) {
  63. if (!(value & 0x10))
  64. rc = pci_write_config_byte(via, 0x42, value | 0x10);
  65. }
  66. if (!rc) {
  67. dev_info(&via->dev, "bridge config is 0x%x\n", value | 0x10);
  68. } else {
  69. dev_warn(&via->dev,
  70. "could not set vortex latency: pci error 0x%x\n", rc);
  71. }
  72. }
  73. static void snd_vortex_workaround(struct pci_dev *vortex, int fix)
  74. {
  75. struct pci_dev *via = NULL;
  76. /* autodetect if workarounds are required */
  77. if (fix == 255) {
  78. /* VIA KT133 */
  79. via = pci_get_device(PCI_VENDOR_ID_VIA,
  80. PCI_DEVICE_ID_VIA_8365_1, NULL);
  81. /* VIA Apollo */
  82. if (via == NULL) {
  83. via = pci_get_device(PCI_VENDOR_ID_VIA,
  84. PCI_DEVICE_ID_VIA_82C598_1, NULL);
  85. /* AMD Irongate */
  86. if (via == NULL)
  87. via = pci_get_device(PCI_VENDOR_ID_AMD,
  88. PCI_DEVICE_ID_AMD_FE_GATE_7007, NULL);
  89. }
  90. if (via) {
  91. dev_info(&vortex->dev,
  92. "Activating latency workaround...\n");
  93. vortex_fix_latency(vortex);
  94. vortex_fix_agp_bridge(via);
  95. }
  96. } else {
  97. if (fix & 0x1)
  98. vortex_fix_latency(vortex);
  99. if (fix & 0x2)
  100. via = pci_get_device(PCI_VENDOR_ID_VIA,
  101. PCI_DEVICE_ID_VIA_8365_1, NULL);
  102. else if (fix & 0x4)
  103. via = pci_get_device(PCI_VENDOR_ID_VIA,
  104. PCI_DEVICE_ID_VIA_82C598_1, NULL);
  105. else if (fix & 0x8)
  106. via = pci_get_device(PCI_VENDOR_ID_AMD,
  107. PCI_DEVICE_ID_AMD_FE_GATE_7007, NULL);
  108. if (via)
  109. vortex_fix_agp_bridge(via);
  110. }
  111. pci_dev_put(via);
  112. }
  113. // component-destructor
  114. // (see "Management of Cards and Components")
  115. static void snd_vortex_free(struct snd_card *card)
  116. {
  117. vortex_t *vortex = card->private_data;
  118. vortex_gameport_unregister(vortex);
  119. vortex_core_shutdown(vortex);
  120. }
  121. // chip-specific constructor
  122. // (see "Management of Cards and Components")
  123. static int
  124. snd_vortex_create(struct snd_card *card, struct pci_dev *pci)
  125. {
  126. vortex_t *chip = card->private_data;
  127. int err;
  128. // check PCI availability (DMA).
  129. err = pcim_enable_device(pci);
  130. if (err < 0)
  131. return err;
  132. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
  133. dev_err(card->dev, "error to set DMA mask\n");
  134. return -ENXIO;
  135. }
  136. chip->card = card;
  137. // initialize the stuff
  138. chip->pci_dev = pci;
  139. chip->vendor = pci->vendor;
  140. chip->device = pci->device;
  141. chip->card = card;
  142. chip->irq = -1;
  143. // (1) PCI resource allocation
  144. // Get MMIO area
  145. //
  146. err = pcim_iomap_regions(pci, 1 << 0, CARD_NAME_SHORT);
  147. if (err)
  148. return err;
  149. chip->io = pci_resource_start(pci, 0);
  150. chip->mmio = pcim_iomap_table(pci)[0];
  151. /* Init audio core.
  152. * This must be done before we do request_irq otherwise we can get spurious
  153. * interrupts that we do not handle properly and make a mess of things */
  154. err = vortex_core_init(chip);
  155. if (err) {
  156. dev_err(card->dev, "hw core init failed\n");
  157. return err;
  158. }
  159. err = devm_request_irq(&pci->dev, pci->irq, vortex_interrupt,
  160. IRQF_SHARED, KBUILD_MODNAME, chip);
  161. if (err) {
  162. dev_err(card->dev, "cannot grab irq\n");
  163. return err;
  164. }
  165. chip->irq = pci->irq;
  166. card->sync_irq = chip->irq;
  167. card->private_free = snd_vortex_free;
  168. pci_set_master(pci);
  169. // End of PCI setup.
  170. return 0;
  171. }
  172. // constructor -- see "Constructor" sub-section
  173. static int
  174. __snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  175. {
  176. static int dev;
  177. struct snd_card *card;
  178. vortex_t *chip;
  179. int err;
  180. // (1)
  181. if (dev >= SNDRV_CARDS)
  182. return -ENODEV;
  183. if (!enable[dev]) {
  184. dev++;
  185. return -ENOENT;
  186. }
  187. // (2)
  188. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  189. sizeof(*chip), &card);
  190. if (err < 0)
  191. return err;
  192. chip = card->private_data;
  193. // (3)
  194. err = snd_vortex_create(card, pci);
  195. if (err < 0)
  196. return err;
  197. snd_vortex_workaround(pci, pcifix[dev]);
  198. // Card details needed in snd_vortex_midi
  199. strcpy(card->driver, CARD_NAME_SHORT);
  200. sprintf(card->shortname, "Aureal Vortex %s", CARD_NAME_SHORT);
  201. sprintf(card->longname, "%s at 0x%lx irq %i",
  202. card->shortname, chip->io, chip->irq);
  203. // (4) Alloc components.
  204. err = snd_vortex_mixer(chip);
  205. if (err < 0)
  206. return err;
  207. // ADB pcm.
  208. err = snd_vortex_new_pcm(chip, VORTEX_PCM_ADB, NR_PCM);
  209. if (err < 0)
  210. return err;
  211. #ifndef CHIP_AU8820
  212. // ADB SPDIF
  213. err = snd_vortex_new_pcm(chip, VORTEX_PCM_SPDIF, 1);
  214. if (err < 0)
  215. return err;
  216. // A3D
  217. err = snd_vortex_new_pcm(chip, VORTEX_PCM_A3D, NR_A3D);
  218. if (err < 0)
  219. return err;
  220. #endif
  221. /*
  222. // ADB I2S
  223. if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_I2S, 1)) < 0) {
  224. return err;
  225. }
  226. */
  227. #ifndef CHIP_AU8810
  228. // WT pcm.
  229. err = snd_vortex_new_pcm(chip, VORTEX_PCM_WT, NR_WT);
  230. if (err < 0)
  231. return err;
  232. #endif
  233. err = snd_vortex_midi(chip);
  234. if (err < 0)
  235. return err;
  236. vortex_gameport_register(chip);
  237. #if 0
  238. if (snd_seq_device_new(card, 1, SNDRV_SEQ_DEV_ID_VORTEX_SYNTH,
  239. sizeof(snd_vortex_synth_arg_t), &wave) < 0
  240. || wave == NULL) {
  241. dev_err(card->dev, "Can't initialize Aureal wavetable synth\n");
  242. } else {
  243. snd_vortex_synth_arg_t *arg;
  244. arg = SNDRV_SEQ_DEVICE_ARGPTR(wave);
  245. strcpy(wave->name, "Aureal Synth");
  246. arg->hwptr = vortex;
  247. arg->index = 1;
  248. arg->seq_ports = seq_ports[dev];
  249. arg->max_voices = max_synth_voices[dev];
  250. }
  251. #endif
  252. // (5)
  253. err = pci_read_config_word(pci, PCI_DEVICE_ID, &chip->device);
  254. if (err < 0)
  255. return err;
  256. err = pci_read_config_word(pci, PCI_VENDOR_ID, &chip->vendor);
  257. if (err < 0)
  258. return err;
  259. chip->rev = pci->revision;
  260. #ifdef CHIP_AU8830
  261. if ((chip->rev) != 0xfe && (chip->rev) != 0xfa) {
  262. dev_alert(card->dev,
  263. "The revision (%x) of your card has not been seen before.\n",
  264. chip->rev);
  265. dev_alert(card->dev,
  266. "Please email the results of 'lspci -vv' to [email protected].\n");
  267. return -ENODEV;
  268. }
  269. #endif
  270. // (6)
  271. err = snd_card_register(card);
  272. if (err < 0)
  273. return err;
  274. // (7)
  275. pci_set_drvdata(pci, card);
  276. dev++;
  277. vortex_connect_default(chip, 1);
  278. vortex_enable_int(chip);
  279. return 0;
  280. }
  281. static int
  282. snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  283. {
  284. return snd_card_free_on_error(&pci->dev, __snd_vortex_probe(pci, pci_id));
  285. }
  286. // pci_driver definition
  287. static struct pci_driver vortex_driver = {
  288. .name = KBUILD_MODNAME,
  289. .id_table = snd_vortex_ids,
  290. .probe = snd_vortex_probe,
  291. };
  292. module_pci_driver(vortex_driver);