gusextreme.c 11 KB

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