gusextreme.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/module.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 bool 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 snd_gusextreme_match(struct device *dev, unsigned int n)
  87. {
  88. return enable[n];
  89. }
  90. static int snd_gusextreme_es1688_create(struct snd_card *card,
  91. struct snd_es1688 *chip,
  92. struct device *dev, unsigned int n)
  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. dev_err(dev, "unable to find a free IRQ for ES1688\n");
  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. dev_err(dev, "unable to find a free DMA for ES1688\n");
  109. return -EBUSY;
  110. }
  111. }
  112. if (port[n] != SNDRV_AUTO_PORT)
  113. return snd_es1688_create(card, chip, port[n], mpu_port[n],
  114. irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
  115. i = 0;
  116. do {
  117. port[n] = possible_ports[i];
  118. error = snd_es1688_create(card, chip, port[n], mpu_port[n],
  119. irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
  120. } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
  121. return error;
  122. }
  123. static int snd_gusextreme_gus_card_create(struct snd_card *card,
  124. struct device *dev, unsigned int n,
  125. 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. dev_err(dev, "unable to find a free IRQ for GF1\n");
  133. return -EBUSY;
  134. }
  135. }
  136. if (dma1[n] == SNDRV_AUTO_DMA) {
  137. dma1[n] = snd_legacy_find_free_dma(possible_dmas);
  138. if (dma1[n] < 0) {
  139. dev_err(dev, "unable to find a free DMA for GF1\n");
  140. return -EBUSY;
  141. }
  142. }
  143. return snd_gus_create(card, gf1_port[n], gf1_irq[n], dma1[n], -1,
  144. 0, channels[n], pcm_channels[n], 0, rgus);
  145. }
  146. static int snd_gusextreme_detect(struct snd_gus_card *gus,
  147. struct snd_es1688 *es1688)
  148. {
  149. unsigned long flags;
  150. unsigned char d;
  151. /*
  152. * This is main stuff - enable access to GF1 chip...
  153. * I'm not sure, if this will work for card which have
  154. * ES1688 chip in another place than 0x220.
  155. *
  156. * I used reverse-engineering in DOSEMU. [--jk]
  157. *
  158. * ULTRINIT.EXE:
  159. * 0x230 = 0,2,3
  160. * 0x240 = 2,0,1
  161. * 0x250 = 2,0,3
  162. * 0x260 = 2,2,1
  163. */
  164. spin_lock_irqsave(&es1688->mixer_lock, flags);
  165. snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */
  166. spin_unlock_irqrestore(&es1688->mixer_lock, flags);
  167. spin_lock_irqsave(&es1688->reg_lock, flags);
  168. outb(gus->gf1.port & 0x040 ? 2 : 0, ES1688P(es1688, INIT1));
  169. outb(0, 0x201);
  170. outb(gus->gf1.port & 0x020 ? 2 : 0, ES1688P(es1688, INIT1));
  171. outb(0, 0x201);
  172. outb(gus->gf1.port & 0x010 ? 3 : 1, ES1688P(es1688, INIT1));
  173. spin_unlock_irqrestore(&es1688->reg_lock, flags);
  174. udelay(100);
  175. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  176. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
  177. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  178. return -EIO;
  179. }
  180. udelay(160);
  181. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  182. udelay(160);
  183. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
  184. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  185. return -EIO;
  186. }
  187. return 0;
  188. }
  189. static int snd_gusextreme_mixer(struct snd_card *card)
  190. {
  191. struct snd_ctl_elem_id id1, id2;
  192. int error;
  193. memset(&id1, 0, sizeof(id1));
  194. memset(&id2, 0, sizeof(id2));
  195. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  196. /* reassign AUX to SYNTHESIZER */
  197. strcpy(id1.name, "Aux Playback Volume");
  198. strcpy(id2.name, "Synth Playback Volume");
  199. error = snd_ctl_rename_id(card, &id1, &id2);
  200. if (error < 0)
  201. return error;
  202. /* reassign Master Playback Switch to Synth Playback Switch */
  203. strcpy(id1.name, "Master Playback Switch");
  204. strcpy(id2.name, "Synth Playback Switch");
  205. error = snd_ctl_rename_id(card, &id1, &id2);
  206. if (error < 0)
  207. return error;
  208. return 0;
  209. }
  210. static int snd_gusextreme_probe(struct device *dev, unsigned int n)
  211. {
  212. struct snd_card *card;
  213. struct snd_gus_card *gus;
  214. struct snd_es1688 *es1688;
  215. struct snd_opl3 *opl3;
  216. int error;
  217. error = snd_card_new(dev, index[n], id[n], THIS_MODULE,
  218. sizeof(struct snd_es1688), &card);
  219. if (error < 0)
  220. return error;
  221. es1688 = card->private_data;
  222. if (mpu_port[n] == SNDRV_AUTO_PORT)
  223. mpu_port[n] = 0;
  224. if (mpu_irq[n] > 15)
  225. mpu_irq[n] = -1;
  226. error = snd_gusextreme_es1688_create(card, es1688, dev, n);
  227. if (error < 0)
  228. goto out;
  229. if (gf1_port[n] < 0)
  230. gf1_port[n] = es1688->port + 0x20;
  231. error = snd_gusextreme_gus_card_create(card, dev, n, &gus);
  232. if (error < 0)
  233. goto out;
  234. error = snd_gusextreme_detect(gus, es1688);
  235. if (error < 0)
  236. goto out;
  237. gus->joystick_dac = joystick_dac[n];
  238. error = snd_gus_initialize(gus);
  239. if (error < 0)
  240. goto out;
  241. error = -ENODEV;
  242. if (!gus->ess_flag) {
  243. dev_err(dev, "GUS Extreme soundcard was not "
  244. "detected at 0x%lx\n", gus->gf1.port);
  245. goto out;
  246. }
  247. gus->codec_flag = 1;
  248. error = snd_es1688_pcm(card, es1688, 0);
  249. if (error < 0)
  250. goto out;
  251. error = snd_es1688_mixer(card, es1688);
  252. if (error < 0)
  253. goto out;
  254. snd_component_add(card, "ES1688");
  255. if (pcm_channels[n] > 0) {
  256. error = snd_gf1_pcm_new(gus, 1, 1);
  257. if (error < 0)
  258. goto out;
  259. }
  260. error = snd_gf1_new_mixer(gus);
  261. if (error < 0)
  262. goto out;
  263. error = snd_gusextreme_mixer(card);
  264. if (error < 0)
  265. goto out;
  266. if (snd_opl3_create(card, es1688->port, es1688->port + 2,
  267. OPL3_HW_OPL3, 0, &opl3) < 0)
  268. dev_warn(dev, "opl3 not detected at 0x%lx\n", es1688->port);
  269. else {
  270. error = snd_opl3_hwdep_new(opl3, 0, 2, NULL);
  271. if (error < 0)
  272. goto out;
  273. }
  274. if (es1688->mpu_port >= 0x300) {
  275. error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
  276. es1688->mpu_port, 0, mpu_irq[n], NULL);
  277. if (error < 0)
  278. goto out;
  279. }
  280. sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, "
  281. "irq %i&%i, dma %i&%i", es1688->port,
  282. gus->gf1.irq, es1688->irq, gus->gf1.dma1, es1688->dma8);
  283. error = snd_card_register(card);
  284. if (error < 0)
  285. goto out;
  286. dev_set_drvdata(dev, card);
  287. return 0;
  288. out: snd_card_free(card);
  289. return error;
  290. }
  291. static int snd_gusextreme_remove(struct device *dev, unsigned int n)
  292. {
  293. snd_card_free(dev_get_drvdata(dev));
  294. return 0;
  295. }
  296. static struct isa_driver snd_gusextreme_driver = {
  297. .match = snd_gusextreme_match,
  298. .probe = snd_gusextreme_probe,
  299. .remove = snd_gusextreme_remove,
  300. #if 0 /* FIXME */
  301. .suspend = snd_gusextreme_suspend,
  302. .resume = snd_gusextreme_resume,
  303. #endif
  304. .driver = {
  305. .name = DEV_NAME
  306. }
  307. };
  308. static int __init alsa_card_gusextreme_init(void)
  309. {
  310. return isa_register_driver(&snd_gusextreme_driver, SNDRV_CARDS);
  311. }
  312. static void __exit alsa_card_gusextreme_exit(void)
  313. {
  314. isa_unregister_driver(&snd_gusextreme_driver);
  315. }
  316. module_init(alsa_card_gusextreme_init);
  317. module_exit(alsa_card_gusextreme_exit);