gusmax.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Driver for Gravis UltraSound MAX soundcard
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/isa.h>
  24. #include <linux/delay.h>
  25. #include <linux/time.h>
  26. #include <linux/moduleparam.h>
  27. #include <asm/dma.h>
  28. #include <sound/core.h>
  29. #include <sound/gus.h>
  30. #include <sound/cs4231.h>
  31. #define SNDRV_LEGACY_FIND_FREE_IRQ
  32. #define SNDRV_LEGACY_FIND_FREE_DMA
  33. #include <sound/initval.h>
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  35. MODULE_DESCRIPTION("Gravis UltraSound MAX");
  36. MODULE_LICENSE("GPL");
  37. MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}");
  38. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  39. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  40. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  41. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
  42. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
  43. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  44. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  45. static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
  46. /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
  47. static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
  48. static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  49. module_param_array(index, int, NULL, 0444);
  50. MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
  53. module_param_array(enable, bool, NULL, 0444);
  54. MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
  55. module_param_array(port, long, NULL, 0444);
  56. MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
  57. module_param_array(irq, int, NULL, 0444);
  58. MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
  59. module_param_array(dma1, int, NULL, 0444);
  60. MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
  61. module_param_array(dma2, int, NULL, 0444);
  62. MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
  63. module_param_array(joystick_dac, int, NULL, 0444);
  64. MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
  65. module_param_array(channels, int, NULL, 0444);
  66. MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
  67. module_param_array(pcm_channels, int, NULL, 0444);
  68. MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
  69. struct snd_gusmax {
  70. int irq;
  71. struct snd_card *card;
  72. struct snd_gus_card *gus;
  73. struct snd_cs4231 *cs4231;
  74. unsigned short gus_status_reg;
  75. unsigned short pcm_status_reg;
  76. };
  77. #define PFX "gusmax: "
  78. static int __devinit snd_gusmax_detect(struct snd_gus_card * gus)
  79. {
  80. unsigned char d;
  81. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  82. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
  83. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  84. return -ENODEV;
  85. }
  86. udelay(160);
  87. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  88. udelay(160);
  89. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
  90. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  91. return -ENODEV;
  92. }
  93. return 0;
  94. }
  95. static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id)
  96. {
  97. struct snd_gusmax *maxcard = dev_id;
  98. int loop, max = 5;
  99. int handled = 0;
  100. do {
  101. loop = 0;
  102. if (inb(maxcard->gus_status_reg)) {
  103. handled = 1;
  104. snd_gus_interrupt(irq, maxcard->gus);
  105. loop++;
  106. }
  107. if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
  108. handled = 1;
  109. snd_cs4231_interrupt(irq, maxcard->cs4231);
  110. loop++;
  111. }
  112. } while (loop && --max > 0);
  113. return IRQ_RETVAL(handled);
  114. }
  115. static void __devinit snd_gusmax_init(int dev, struct snd_card *card,
  116. struct snd_gus_card * gus)
  117. {
  118. gus->equal_irq = 1;
  119. gus->codec_flag = 1;
  120. gus->joystick_dac = joystick_dac[dev];
  121. /* init control register */
  122. gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
  123. if (gus->gf1.dma1 > 3)
  124. gus->max_cntrl_val |= 0x10;
  125. if (gus->gf1.dma2 > 3)
  126. gus->max_cntrl_val |= 0x20;
  127. gus->max_cntrl_val |= 0x40;
  128. outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
  129. }
  130. #define CS4231_PRIVATE( left, right, shift, mute ) \
  131. ((left << 24)|(right << 16)|(shift<<8)|mute)
  132. static int __devinit snd_gusmax_mixer(struct snd_cs4231 *chip)
  133. {
  134. struct snd_card *card = chip->card;
  135. struct snd_ctl_elem_id id1, id2;
  136. int err;
  137. memset(&id1, 0, sizeof(id1));
  138. memset(&id2, 0, sizeof(id2));
  139. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  140. /* reassign AUXA to SYNTHESIZER */
  141. strcpy(id1.name, "Aux Playback Switch");
  142. strcpy(id2.name, "Synth Playback Switch");
  143. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  144. return err;
  145. strcpy(id1.name, "Aux Playback Volume");
  146. strcpy(id2.name, "Synth Playback Volume");
  147. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  148. return err;
  149. /* reassign AUXB to CD */
  150. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  151. strcpy(id2.name, "CD Playback Switch");
  152. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  153. return err;
  154. strcpy(id1.name, "Aux Playback Volume");
  155. strcpy(id2.name, "CD Playback Volume");
  156. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  157. return err;
  158. #if 0
  159. /* reassign Mono Input to MIC */
  160. if (snd_mixer_group_rename(mixer,
  161. SNDRV_MIXER_IN_MONO, 0,
  162. SNDRV_MIXER_IN_MIC, 0) < 0)
  163. goto __error;
  164. if (snd_mixer_elem_rename(mixer,
  165. SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
  166. SNDRV_MIXER_IN_MIC, 0) < 0)
  167. goto __error;
  168. if (snd_mixer_elem_rename(mixer,
  169. "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
  170. "Mic Capture Volume", 0) < 0)
  171. goto __error;
  172. if (snd_mixer_elem_rename(mixer,
  173. "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
  174. "Mic Capture Switch", 0) < 0)
  175. goto __error;
  176. #endif
  177. return 0;
  178. }
  179. static void snd_gusmax_free(struct snd_card *card)
  180. {
  181. struct snd_gusmax *maxcard = (struct snd_gusmax *)card->private_data;
  182. if (maxcard == NULL)
  183. return;
  184. if (maxcard->irq >= 0)
  185. free_irq(maxcard->irq, (void *)maxcard);
  186. }
  187. static int __devinit snd_gusmax_match(struct device *pdev, unsigned int dev)
  188. {
  189. return enable[dev];
  190. }
  191. static int __devinit snd_gusmax_probe(struct device *pdev, unsigned int dev)
  192. {
  193. static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
  194. static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
  195. int xirq, xdma1, xdma2, err;
  196. struct snd_card *card;
  197. struct snd_gus_card *gus = NULL;
  198. struct snd_cs4231 *cs4231;
  199. struct snd_gusmax *maxcard;
  200. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  201. sizeof(struct snd_gusmax));
  202. if (card == NULL)
  203. return -ENOMEM;
  204. card->private_free = snd_gusmax_free;
  205. maxcard = (struct snd_gusmax *)card->private_data;
  206. maxcard->card = card;
  207. maxcard->irq = -1;
  208. xirq = irq[dev];
  209. if (xirq == SNDRV_AUTO_IRQ) {
  210. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  211. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  212. err = -EBUSY;
  213. goto _err;
  214. }
  215. }
  216. xdma1 = dma1[dev];
  217. if (xdma1 == SNDRV_AUTO_DMA) {
  218. if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  219. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  220. err = -EBUSY;
  221. goto _err;
  222. }
  223. }
  224. xdma2 = dma2[dev];
  225. if (xdma2 == SNDRV_AUTO_DMA) {
  226. if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  227. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  228. err = -EBUSY;
  229. goto _err;
  230. }
  231. }
  232. if (port[dev] != SNDRV_AUTO_PORT) {
  233. err = snd_gus_create(card,
  234. port[dev],
  235. -xirq, xdma1, xdma2,
  236. 0, channels[dev],
  237. pcm_channels[dev],
  238. 0, &gus);
  239. } else {
  240. static unsigned long possible_ports[] = {
  241. 0x220, 0x230, 0x240, 0x250, 0x260
  242. };
  243. int i;
  244. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  245. err = snd_gus_create(card,
  246. possible_ports[i],
  247. -xirq, xdma1, xdma2,
  248. 0, channels[dev],
  249. pcm_channels[dev],
  250. 0, &gus);
  251. if (err >= 0) {
  252. port[dev] = possible_ports[i];
  253. break;
  254. }
  255. }
  256. }
  257. if (err < 0)
  258. goto _err;
  259. if ((err = snd_gusmax_detect(gus)) < 0)
  260. goto _err;
  261. maxcard->gus_status_reg = gus->gf1.reg_irqstat;
  262. maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
  263. snd_gusmax_init(dev, card, gus);
  264. if ((err = snd_gus_initialize(gus)) < 0)
  265. goto _err;
  266. if (!gus->max_flag) {
  267. snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
  268. err = -ENODEV;
  269. goto _err;
  270. }
  271. if (request_irq(xirq, snd_gusmax_interrupt, IRQF_DISABLED, "GUS MAX", (void *)maxcard)) {
  272. snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
  273. err = -EBUSY;
  274. goto _err;
  275. }
  276. maxcard->irq = xirq;
  277. if ((err = snd_cs4231_create(card,
  278. gus->gf1.port + 0x10c, -1, xirq,
  279. xdma2 < 0 ? xdma1 : xdma2, xdma1,
  280. CS4231_HW_DETECT,
  281. CS4231_HWSHARE_IRQ |
  282. CS4231_HWSHARE_DMA1 |
  283. CS4231_HWSHARE_DMA2,
  284. &cs4231)) < 0)
  285. goto _err;
  286. if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
  287. goto _err;
  288. if ((err = snd_cs4231_mixer(cs4231)) < 0)
  289. goto _err;
  290. if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
  291. goto _err;
  292. if (pcm_channels[dev] > 0) {
  293. if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
  294. goto _err;
  295. }
  296. if ((err = snd_gusmax_mixer(cs4231)) < 0)
  297. goto _err;
  298. if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
  299. goto _err;
  300. sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
  301. if (xdma2 >= 0)
  302. sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
  303. snd_card_set_dev(card, pdev);
  304. if ((err = snd_card_register(card)) < 0)
  305. goto _err;
  306. maxcard->gus = gus;
  307. maxcard->cs4231 = cs4231;
  308. dev_set_drvdata(pdev, card);
  309. return 0;
  310. _err:
  311. snd_card_free(card);
  312. return err;
  313. }
  314. static int __devexit snd_gusmax_remove(struct device *devptr, unsigned int dev)
  315. {
  316. snd_card_free(dev_get_drvdata(devptr));
  317. dev_set_drvdata(devptr, NULL);
  318. return 0;
  319. }
  320. #define DEV_NAME "gusmax"
  321. static struct isa_driver snd_gusmax_driver = {
  322. .match = snd_gusmax_match,
  323. .probe = snd_gusmax_probe,
  324. .remove = __devexit_p(snd_gusmax_remove),
  325. /* FIXME: suspend/resume */
  326. .driver = {
  327. .name = DEV_NAME
  328. },
  329. };
  330. static int __init alsa_card_gusmax_init(void)
  331. {
  332. return isa_register_driver(&snd_gusmax_driver, SNDRV_CARDS);
  333. }
  334. static void __exit alsa_card_gusmax_exit(void)
  335. {
  336. isa_unregister_driver(&snd_gusmax_driver);
  337. }
  338. module_init(alsa_card_gusmax_init)
  339. module_exit(alsa_card_gusmax_exit)