gusextreme.c 11 KB

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