gusextreme.c 12 KB

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