gusextreme.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Gravis UltraSound Extreme soundcards
  4. * Copyright (c) by Jaroslav Kysela <[email protected]>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/err.h>
  8. #include <linux/isa.h>
  9. #include <linux/delay.h>
  10. #include <linux/time.h>
  11. #include <linux/module.h>
  12. #include <asm/dma.h>
  13. #include <sound/core.h>
  14. #include <sound/gus.h>
  15. #include <sound/es1688.h>
  16. #include <sound/mpu401.h>
  17. #include <sound/opl3.h>
  18. #define SNDRV_LEGACY_AUTO_PROBE
  19. #define SNDRV_LEGACY_FIND_FREE_IRQ
  20. #define SNDRV_LEGACY_FIND_FREE_DMA
  21. #include <sound/initval.h>
  22. #define CRD_NAME "Gravis UltraSound Extreme"
  23. #define DEV_NAME "gusextreme"
  24. MODULE_DESCRIPTION(CRD_NAME);
  25. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  26. MODULE_LICENSE("GPL");
  27. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  28. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  29. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  30. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
  31. static long gf1_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x210,0x220,0x230,0x240,0x250,0x260,0x270 */
  32. static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x300,0x310,0x320 */
  33. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  34. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  35. static int gf1_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
  36. static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  37. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  38. static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
  39. /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
  40. static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
  41. static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  42. module_param_array(index, int, NULL, 0444);
  43. MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
  44. module_param_array(id, charp, NULL, 0444);
  45. MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
  46. module_param_array(enable, bool, NULL, 0444);
  47. MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
  48. module_param_hw_array(port, long, ioport, NULL, 0444);
  49. MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
  50. module_param_hw_array(gf1_port, long, ioport, NULL, 0444);
  51. MODULE_PARM_DESC(gf1_port, "GF1 port # for " CRD_NAME " driver (optional).");
  52. module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
  53. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
  54. module_param_hw_array(irq, int, irq, NULL, 0444);
  55. MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
  56. module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
  57. MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
  58. module_param_hw_array(gf1_irq, int, irq, NULL, 0444);
  59. MODULE_PARM_DESC(gf1_irq, "GF1 IRQ # for " CRD_NAME " driver.");
  60. module_param_hw_array(dma8, int, dma, NULL, 0444);
  61. MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
  62. module_param_hw_array(dma1, int, dma, NULL, 0444);
  63. MODULE_PARM_DESC(dma1, "GF1 DMA # for " CRD_NAME " driver.");
  64. module_param_array(joystick_dac, int, NULL, 0444);
  65. MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver.");
  66. module_param_array(channels, int, NULL, 0444);
  67. MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver.");
  68. module_param_array(pcm_channels, int, NULL, 0444);
  69. MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver.");
  70. static int snd_gusextreme_match(struct device *dev, unsigned int n)
  71. {
  72. return enable[n];
  73. }
  74. static int snd_gusextreme_es1688_create(struct snd_card *card,
  75. struct snd_es1688 *chip,
  76. struct device *dev, unsigned int n)
  77. {
  78. static const long possible_ports[] = {0x220, 0x240, 0x260};
  79. static const int possible_irqs[] = {5, 9, 10, 7, -1};
  80. static const int possible_dmas[] = {1, 3, 0, -1};
  81. int i, error;
  82. if (irq[n] == SNDRV_AUTO_IRQ) {
  83. irq[n] = snd_legacy_find_free_irq(possible_irqs);
  84. if (irq[n] < 0) {
  85. dev_err(dev, "unable to find a free IRQ for ES1688\n");
  86. return -EBUSY;
  87. }
  88. }
  89. if (dma8[n] == SNDRV_AUTO_DMA) {
  90. dma8[n] = snd_legacy_find_free_dma(possible_dmas);
  91. if (dma8[n] < 0) {
  92. dev_err(dev, "unable to find a free DMA for ES1688\n");
  93. return -EBUSY;
  94. }
  95. }
  96. if (port[n] != SNDRV_AUTO_PORT)
  97. return snd_es1688_create(card, chip, port[n], mpu_port[n],
  98. irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
  99. i = 0;
  100. do {
  101. port[n] = possible_ports[i];
  102. error = snd_es1688_create(card, chip, port[n], mpu_port[n],
  103. irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
  104. } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
  105. return error;
  106. }
  107. static int snd_gusextreme_gus_card_create(struct snd_card *card,
  108. struct device *dev, unsigned int n,
  109. struct snd_gus_card **rgus)
  110. {
  111. static const int possible_irqs[] = {11, 12, 15, 9, 5, 7, 3, -1};
  112. static const int possible_dmas[] = {5, 6, 7, 3, 1, -1};
  113. if (gf1_irq[n] == SNDRV_AUTO_IRQ) {
  114. gf1_irq[n] = snd_legacy_find_free_irq(possible_irqs);
  115. if (gf1_irq[n] < 0) {
  116. dev_err(dev, "unable to find a free IRQ for GF1\n");
  117. return -EBUSY;
  118. }
  119. }
  120. if (dma1[n] == SNDRV_AUTO_DMA) {
  121. dma1[n] = snd_legacy_find_free_dma(possible_dmas);
  122. if (dma1[n] < 0) {
  123. dev_err(dev, "unable to find a free DMA for GF1\n");
  124. return -EBUSY;
  125. }
  126. }
  127. return snd_gus_create(card, gf1_port[n], gf1_irq[n], dma1[n], -1,
  128. 0, channels[n], pcm_channels[n], 0, rgus);
  129. }
  130. static int snd_gusextreme_detect(struct snd_gus_card *gus,
  131. struct snd_es1688 *es1688)
  132. {
  133. unsigned long flags;
  134. unsigned char d;
  135. /*
  136. * This is main stuff - enable access to GF1 chip...
  137. * I'm not sure, if this will work for card which have
  138. * ES1688 chip in another place than 0x220.
  139. *
  140. * I used reverse-engineering in DOSEMU. [--jk]
  141. *
  142. * ULTRINIT.EXE:
  143. * 0x230 = 0,2,3
  144. * 0x240 = 2,0,1
  145. * 0x250 = 2,0,3
  146. * 0x260 = 2,2,1
  147. */
  148. spin_lock_irqsave(&es1688->mixer_lock, flags);
  149. snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */
  150. spin_unlock_irqrestore(&es1688->mixer_lock, flags);
  151. spin_lock_irqsave(&es1688->reg_lock, flags);
  152. outb(gus->gf1.port & 0x040 ? 2 : 0, ES1688P(es1688, INIT1));
  153. outb(0, 0x201);
  154. outb(gus->gf1.port & 0x020 ? 2 : 0, ES1688P(es1688, INIT1));
  155. outb(0, 0x201);
  156. outb(gus->gf1.port & 0x010 ? 3 : 1, ES1688P(es1688, INIT1));
  157. spin_unlock_irqrestore(&es1688->reg_lock, flags);
  158. udelay(100);
  159. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  160. d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
  161. if ((d & 0x07) != 0) {
  162. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  163. return -EIO;
  164. }
  165. udelay(160);
  166. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  167. udelay(160);
  168. d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
  169. if ((d & 0x07) != 1) {
  170. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  171. return -EIO;
  172. }
  173. return 0;
  174. }
  175. static int snd_gusextreme_mixer(struct snd_card *card)
  176. {
  177. struct snd_ctl_elem_id id1, id2;
  178. int error;
  179. memset(&id1, 0, sizeof(id1));
  180. memset(&id2, 0, sizeof(id2));
  181. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  182. /* reassign AUX to SYNTHESIZER */
  183. strcpy(id1.name, "Aux Playback Volume");
  184. strcpy(id2.name, "Synth Playback Volume");
  185. error = snd_ctl_rename_id(card, &id1, &id2);
  186. if (error < 0)
  187. return error;
  188. /* reassign Master Playback Switch to Synth Playback Switch */
  189. strcpy(id1.name, "Master Playback Switch");
  190. strcpy(id2.name, "Synth Playback Switch");
  191. error = snd_ctl_rename_id(card, &id1, &id2);
  192. if (error < 0)
  193. return error;
  194. return 0;
  195. }
  196. static int snd_gusextreme_probe(struct device *dev, unsigned int n)
  197. {
  198. struct snd_card *card;
  199. struct snd_gus_card *gus;
  200. struct snd_es1688 *es1688;
  201. struct snd_opl3 *opl3;
  202. int error;
  203. error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE,
  204. sizeof(struct snd_es1688), &card);
  205. if (error < 0)
  206. return error;
  207. es1688 = card->private_data;
  208. if (mpu_port[n] == SNDRV_AUTO_PORT)
  209. mpu_port[n] = 0;
  210. if (mpu_irq[n] > 15)
  211. mpu_irq[n] = -1;
  212. error = snd_gusextreme_es1688_create(card, es1688, dev, n);
  213. if (error < 0)
  214. return error;
  215. if (gf1_port[n] < 0)
  216. gf1_port[n] = es1688->port + 0x20;
  217. error = snd_gusextreme_gus_card_create(card, dev, n, &gus);
  218. if (error < 0)
  219. return error;
  220. error = snd_gusextreme_detect(gus, es1688);
  221. if (error < 0)
  222. return error;
  223. gus->joystick_dac = joystick_dac[n];
  224. error = snd_gus_initialize(gus);
  225. if (error < 0)
  226. return error;
  227. error = -ENODEV;
  228. if (!gus->ess_flag) {
  229. dev_err(dev, "GUS Extreme soundcard was not "
  230. "detected at 0x%lx\n", gus->gf1.port);
  231. return error;
  232. }
  233. gus->codec_flag = 1;
  234. error = snd_es1688_pcm(card, es1688, 0);
  235. if (error < 0)
  236. return error;
  237. error = snd_es1688_mixer(card, es1688);
  238. if (error < 0)
  239. return error;
  240. snd_component_add(card, "ES1688");
  241. if (pcm_channels[n] > 0) {
  242. error = snd_gf1_pcm_new(gus, 1, 1);
  243. if (error < 0)
  244. return error;
  245. }
  246. error = snd_gf1_new_mixer(gus);
  247. if (error < 0)
  248. return error;
  249. error = snd_gusextreme_mixer(card);
  250. if (error < 0)
  251. return error;
  252. if (snd_opl3_create(card, es1688->port, es1688->port + 2,
  253. OPL3_HW_OPL3, 0, &opl3) < 0)
  254. dev_warn(dev, "opl3 not detected at 0x%lx\n", es1688->port);
  255. else {
  256. error = snd_opl3_hwdep_new(opl3, 0, 2, NULL);
  257. if (error < 0)
  258. return error;
  259. }
  260. if (es1688->mpu_port >= 0x300) {
  261. error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
  262. es1688->mpu_port, 0, mpu_irq[n], NULL);
  263. if (error < 0)
  264. return error;
  265. }
  266. sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, "
  267. "irq %i&%i, dma %i&%i", es1688->port,
  268. gus->gf1.irq, es1688->irq, gus->gf1.dma1, es1688->dma8);
  269. error = snd_card_register(card);
  270. if (error < 0)
  271. return error;
  272. dev_set_drvdata(dev, card);
  273. return 0;
  274. }
  275. static struct isa_driver snd_gusextreme_driver = {
  276. .match = snd_gusextreme_match,
  277. .probe = snd_gusextreme_probe,
  278. #if 0 /* FIXME */
  279. .suspend = snd_gusextreme_suspend,
  280. .resume = snd_gusextreme_resume,
  281. #endif
  282. .driver = {
  283. .name = DEV_NAME
  284. }
  285. };
  286. module_isa_driver(snd_gusextreme_driver, SNDRV_CARDS);