gusextreme.c 11 KB

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