gusextreme.c 11 KB

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