gusmax.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Gravis UltraSound MAX soundcard
  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/wss.h>
  16. #define SNDRV_LEGACY_FIND_FREE_IRQ
  17. #define SNDRV_LEGACY_FIND_FREE_DMA
  18. #include <sound/initval.h>
  19. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  20. MODULE_DESCRIPTION("Gravis UltraSound MAX");
  21. MODULE_LICENSE("GPL");
  22. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  23. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  24. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  25. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
  26. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
  27. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  28. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  29. static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
  30. /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
  31. static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
  32. static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  33. module_param_array(index, int, NULL, 0444);
  34. MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
  35. module_param_array(id, charp, NULL, 0444);
  36. MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
  37. module_param_array(enable, bool, NULL, 0444);
  38. MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
  39. module_param_hw_array(port, long, ioport, NULL, 0444);
  40. MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
  41. module_param_hw_array(irq, int, irq, NULL, 0444);
  42. MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
  43. module_param_hw_array(dma1, int, dma, NULL, 0444);
  44. MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
  45. module_param_hw_array(dma2, int, dma, NULL, 0444);
  46. MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
  47. module_param_array(joystick_dac, int, NULL, 0444);
  48. MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
  49. module_param_array(channels, int, NULL, 0444);
  50. MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
  51. module_param_array(pcm_channels, int, NULL, 0444);
  52. MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
  53. struct snd_gusmax {
  54. int irq;
  55. struct snd_card *card;
  56. struct snd_gus_card *gus;
  57. struct snd_wss *wss;
  58. unsigned short gus_status_reg;
  59. unsigned short pcm_status_reg;
  60. };
  61. #define PFX "gusmax: "
  62. static int snd_gusmax_detect(struct snd_gus_card *gus)
  63. {
  64. unsigned char d;
  65. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  66. d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
  67. if ((d & 0x07) != 0) {
  68. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  69. return -ENODEV;
  70. }
  71. udelay(160);
  72. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  73. udelay(160);
  74. d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
  75. if ((d & 0x07) != 1) {
  76. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  77. return -ENODEV;
  78. }
  79. return 0;
  80. }
  81. static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id)
  82. {
  83. struct snd_gusmax *maxcard = dev_id;
  84. int loop, max = 5;
  85. int handled = 0;
  86. do {
  87. loop = 0;
  88. if (inb(maxcard->gus_status_reg)) {
  89. handled = 1;
  90. snd_gus_interrupt(irq, maxcard->gus);
  91. loop++;
  92. }
  93. if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
  94. handled = 1;
  95. snd_wss_interrupt(irq, maxcard->wss);
  96. loop++;
  97. }
  98. } while (loop && --max > 0);
  99. return IRQ_RETVAL(handled);
  100. }
  101. static void snd_gusmax_init(int dev, struct snd_card *card,
  102. struct snd_gus_card *gus)
  103. {
  104. gus->equal_irq = 1;
  105. gus->codec_flag = 1;
  106. gus->joystick_dac = joystick_dac[dev];
  107. /* init control register */
  108. gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
  109. if (gus->gf1.dma1 > 3)
  110. gus->max_cntrl_val |= 0x10;
  111. if (gus->gf1.dma2 > 3)
  112. gus->max_cntrl_val |= 0x20;
  113. gus->max_cntrl_val |= 0x40;
  114. outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
  115. }
  116. static int snd_gusmax_mixer(struct snd_wss *chip)
  117. {
  118. struct snd_card *card = chip->card;
  119. struct snd_ctl_elem_id id1, id2;
  120. int err;
  121. memset(&id1, 0, sizeof(id1));
  122. memset(&id2, 0, sizeof(id2));
  123. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  124. /* reassign AUXA to SYNTHESIZER */
  125. strcpy(id1.name, "Aux Playback Switch");
  126. strcpy(id2.name, "Synth Playback Switch");
  127. err = snd_ctl_rename_id(card, &id1, &id2);
  128. if (err < 0)
  129. return err;
  130. strcpy(id1.name, "Aux Playback Volume");
  131. strcpy(id2.name, "Synth Playback Volume");
  132. err = snd_ctl_rename_id(card, &id1, &id2);
  133. if (err < 0)
  134. return err;
  135. /* reassign AUXB to CD */
  136. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  137. strcpy(id2.name, "CD Playback Switch");
  138. err = snd_ctl_rename_id(card, &id1, &id2);
  139. if (err < 0)
  140. return err;
  141. strcpy(id1.name, "Aux Playback Volume");
  142. strcpy(id2.name, "CD Playback Volume");
  143. err = snd_ctl_rename_id(card, &id1, &id2);
  144. if (err < 0)
  145. return err;
  146. #if 0
  147. /* reassign Mono Input to MIC */
  148. if (snd_mixer_group_rename(mixer,
  149. SNDRV_MIXER_IN_MONO, 0,
  150. SNDRV_MIXER_IN_MIC, 0) < 0)
  151. goto __error;
  152. if (snd_mixer_elem_rename(mixer,
  153. SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
  154. SNDRV_MIXER_IN_MIC, 0) < 0)
  155. goto __error;
  156. if (snd_mixer_elem_rename(mixer,
  157. "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
  158. "Mic Capture Volume", 0) < 0)
  159. goto __error;
  160. if (snd_mixer_elem_rename(mixer,
  161. "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
  162. "Mic Capture Switch", 0) < 0)
  163. goto __error;
  164. #endif
  165. return 0;
  166. }
  167. static int snd_gusmax_match(struct device *pdev, unsigned int dev)
  168. {
  169. return enable[dev];
  170. }
  171. static int snd_gusmax_probe(struct device *pdev, unsigned int dev)
  172. {
  173. static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
  174. static const int possible_dmas[] = {5, 6, 7, 1, 3, -1};
  175. int xirq, xdma1, xdma2, err;
  176. struct snd_card *card;
  177. struct snd_gus_card *gus = NULL;
  178. struct snd_wss *wss;
  179. struct snd_gusmax *maxcard;
  180. err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
  181. sizeof(struct snd_gusmax), &card);
  182. if (err < 0)
  183. return err;
  184. maxcard = card->private_data;
  185. maxcard->card = card;
  186. maxcard->irq = -1;
  187. xirq = irq[dev];
  188. if (xirq == SNDRV_AUTO_IRQ) {
  189. xirq = snd_legacy_find_free_irq(possible_irqs);
  190. if (xirq < 0) {
  191. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  192. return -EBUSY;
  193. }
  194. }
  195. xdma1 = dma1[dev];
  196. if (xdma1 == SNDRV_AUTO_DMA) {
  197. xdma1 = snd_legacy_find_free_dma(possible_dmas);
  198. if (xdma1 < 0) {
  199. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  200. return -EBUSY;
  201. }
  202. }
  203. xdma2 = dma2[dev];
  204. if (xdma2 == SNDRV_AUTO_DMA) {
  205. xdma2 = snd_legacy_find_free_dma(possible_dmas);
  206. if (xdma2 < 0) {
  207. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  208. return -EBUSY;
  209. }
  210. }
  211. if (port[dev] != SNDRV_AUTO_PORT) {
  212. err = snd_gus_create(card,
  213. port[dev],
  214. -xirq, xdma1, xdma2,
  215. 0, channels[dev],
  216. pcm_channels[dev],
  217. 0, &gus);
  218. } else {
  219. static const unsigned long possible_ports[] = {
  220. 0x220, 0x230, 0x240, 0x250, 0x260
  221. };
  222. int i;
  223. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  224. err = snd_gus_create(card,
  225. possible_ports[i],
  226. -xirq, xdma1, xdma2,
  227. 0, channels[dev],
  228. pcm_channels[dev],
  229. 0, &gus);
  230. if (err >= 0) {
  231. port[dev] = possible_ports[i];
  232. break;
  233. }
  234. }
  235. }
  236. if (err < 0)
  237. return err;
  238. err = snd_gusmax_detect(gus);
  239. if (err < 0)
  240. return err;
  241. maxcard->gus_status_reg = gus->gf1.reg_irqstat;
  242. maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
  243. snd_gusmax_init(dev, card, gus);
  244. err = snd_gus_initialize(gus);
  245. if (err < 0)
  246. return err;
  247. if (!gus->max_flag) {
  248. snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
  249. return -ENODEV;
  250. }
  251. if (devm_request_irq(card->dev, xirq, snd_gusmax_interrupt, 0,
  252. "GUS MAX", (void *)maxcard)) {
  253. snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
  254. return -EBUSY;
  255. }
  256. maxcard->irq = xirq;
  257. card->sync_irq = maxcard->irq;
  258. err = snd_wss_create(card,
  259. gus->gf1.port + 0x10c, -1, xirq,
  260. xdma2 < 0 ? xdma1 : xdma2, xdma1,
  261. WSS_HW_DETECT,
  262. WSS_HWSHARE_IRQ |
  263. WSS_HWSHARE_DMA1 |
  264. WSS_HWSHARE_DMA2,
  265. &wss);
  266. if (err < 0)
  267. return err;
  268. err = snd_wss_pcm(wss, 0);
  269. if (err < 0)
  270. return err;
  271. err = snd_wss_mixer(wss);
  272. if (err < 0)
  273. return err;
  274. err = snd_wss_timer(wss, 2);
  275. if (err < 0)
  276. return err;
  277. if (pcm_channels[dev] > 0) {
  278. err = snd_gf1_pcm_new(gus, 1, 1);
  279. if (err < 0)
  280. return err;
  281. }
  282. err = snd_gusmax_mixer(wss);
  283. if (err < 0)
  284. return err;
  285. err = snd_gf1_rawmidi_new(gus, 0);
  286. if (err < 0)
  287. return err;
  288. sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
  289. if (xdma2 >= 0)
  290. sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
  291. err = snd_card_register(card);
  292. if (err < 0)
  293. return err;
  294. maxcard->gus = gus;
  295. maxcard->wss = wss;
  296. dev_set_drvdata(pdev, card);
  297. return 0;
  298. }
  299. #define DEV_NAME "gusmax"
  300. static struct isa_driver snd_gusmax_driver = {
  301. .match = snd_gusmax_match,
  302. .probe = snd_gusmax_probe,
  303. /* FIXME: suspend/resume */
  304. .driver = {
  305. .name = DEV_NAME
  306. },
  307. };
  308. module_isa_driver(snd_gusmax_driver, SNDRV_CARDS);