gusmax.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Driver for Gravis UltraSound MAX soundcard
  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/cs4231.h>
  32. #define SNDRV_LEGACY_FIND_FREE_IRQ
  33. #define SNDRV_LEGACY_FIND_FREE_DMA
  34. #include <sound/initval.h>
  35. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  36. MODULE_DESCRIPTION("Gravis UltraSound MAX");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound MAX}}");
  39. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  40. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  41. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  42. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
  43. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */
  44. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  45. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  46. static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
  47. /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
  48. static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
  49. static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  50. module_param_array(index, int, NULL, 0444);
  51. MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard.");
  52. module_param_array(id, charp, NULL, 0444);
  53. MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard.");
  54. module_param_array(enable, bool, NULL, 0444);
  55. MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard.");
  56. module_param_array(port, long, NULL, 0444);
  57. MODULE_PARM_DESC(port, "Port # for GUS MAX driver.");
  58. module_param_array(irq, int, NULL, 0444);
  59. MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver.");
  60. module_param_array(dma1, int, NULL, 0444);
  61. MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver.");
  62. module_param_array(dma2, int, NULL, 0444);
  63. MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver.");
  64. module_param_array(joystick_dac, int, NULL, 0444);
  65. MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver.");
  66. module_param_array(channels, int, NULL, 0444);
  67. MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver.");
  68. module_param_array(pcm_channels, int, NULL, 0444);
  69. MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver.");
  70. static struct platform_device *devices[SNDRV_CARDS];
  71. struct snd_gusmax {
  72. int irq;
  73. struct snd_card *card;
  74. struct snd_gus_card *gus;
  75. struct snd_cs4231 *cs4231;
  76. unsigned short gus_status_reg;
  77. unsigned short pcm_status_reg;
  78. };
  79. #define PFX "gusmax: "
  80. static int __devinit snd_gusmax_detect(struct snd_gus_card * gus)
  81. {
  82. unsigned char d;
  83. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  84. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
  85. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  86. return -ENODEV;
  87. }
  88. udelay(160);
  89. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  90. udelay(160);
  91. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
  92. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  93. return -ENODEV;
  94. }
  95. return 0;
  96. }
  97. static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id)
  98. {
  99. struct snd_gusmax *maxcard = dev_id;
  100. int loop, max = 5;
  101. int handled = 0;
  102. do {
  103. loop = 0;
  104. if (inb(maxcard->gus_status_reg)) {
  105. handled = 1;
  106. snd_gus_interrupt(irq, maxcard->gus);
  107. loop++;
  108. }
  109. if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */
  110. handled = 1;
  111. snd_cs4231_interrupt(irq, maxcard->cs4231);
  112. loop++;
  113. }
  114. } while (loop && --max > 0);
  115. return IRQ_RETVAL(handled);
  116. }
  117. static void __devinit snd_gusmax_init(int dev, struct snd_card *card,
  118. struct snd_gus_card * gus)
  119. {
  120. gus->equal_irq = 1;
  121. gus->codec_flag = 1;
  122. gus->joystick_dac = joystick_dac[dev];
  123. /* init control register */
  124. gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f;
  125. if (gus->gf1.dma1 > 3)
  126. gus->max_cntrl_val |= 0x10;
  127. if (gus->gf1.dma2 > 3)
  128. gus->max_cntrl_val |= 0x20;
  129. gus->max_cntrl_val |= 0x40;
  130. outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT));
  131. }
  132. #define CS4231_PRIVATE( left, right, shift, mute ) \
  133. ((left << 24)|(right << 16)|(shift<<8)|mute)
  134. static int __devinit snd_gusmax_mixer(struct snd_cs4231 *chip)
  135. {
  136. struct snd_card *card = chip->card;
  137. struct snd_ctl_elem_id id1, id2;
  138. int err;
  139. memset(&id1, 0, sizeof(id1));
  140. memset(&id2, 0, sizeof(id2));
  141. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  142. /* reassign AUXA to SYNTHESIZER */
  143. strcpy(id1.name, "Aux Playback Switch");
  144. strcpy(id2.name, "Synth Playback Switch");
  145. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  146. return err;
  147. strcpy(id1.name, "Aux Playback Volume");
  148. strcpy(id2.name, "Synth Playback Volume");
  149. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  150. return err;
  151. /* reassign AUXB to CD */
  152. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  153. strcpy(id2.name, "CD Playback Switch");
  154. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  155. return err;
  156. strcpy(id1.name, "Aux Playback Volume");
  157. strcpy(id2.name, "CD Playback Volume");
  158. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  159. return err;
  160. #if 0
  161. /* reassign Mono Input to MIC */
  162. if (snd_mixer_group_rename(mixer,
  163. SNDRV_MIXER_IN_MONO, 0,
  164. SNDRV_MIXER_IN_MIC, 0) < 0)
  165. goto __error;
  166. if (snd_mixer_elem_rename(mixer,
  167. SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT,
  168. SNDRV_MIXER_IN_MIC, 0) < 0)
  169. goto __error;
  170. if (snd_mixer_elem_rename(mixer,
  171. "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1,
  172. "Mic Capture Volume", 0) < 0)
  173. goto __error;
  174. if (snd_mixer_elem_rename(mixer,
  175. "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1,
  176. "Mic Capture Switch", 0) < 0)
  177. goto __error;
  178. #endif
  179. return 0;
  180. }
  181. static void snd_gusmax_free(struct snd_card *card)
  182. {
  183. struct snd_gusmax *maxcard = (struct snd_gusmax *)card->private_data;
  184. if (maxcard == NULL)
  185. return;
  186. if (maxcard->irq >= 0)
  187. free_irq(maxcard->irq, (void *)maxcard);
  188. }
  189. static int __devinit snd_gusmax_probe(struct platform_device *pdev)
  190. {
  191. int dev = pdev->id;
  192. static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1};
  193. static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
  194. int xirq, xdma1, xdma2, err;
  195. struct snd_card *card;
  196. struct snd_gus_card *gus = NULL;
  197. struct snd_cs4231 *cs4231;
  198. struct snd_gusmax *maxcard;
  199. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  200. sizeof(struct snd_gusmax));
  201. if (card == NULL)
  202. return -ENOMEM;
  203. card->private_free = snd_gusmax_free;
  204. maxcard = (struct snd_gusmax *)card->private_data;
  205. maxcard->card = card;
  206. maxcard->irq = -1;
  207. xirq = irq[dev];
  208. if (xirq == SNDRV_AUTO_IRQ) {
  209. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  210. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  211. err = -EBUSY;
  212. goto _err;
  213. }
  214. }
  215. xdma1 = dma1[dev];
  216. if (xdma1 == SNDRV_AUTO_DMA) {
  217. if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  218. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  219. err = -EBUSY;
  220. goto _err;
  221. }
  222. }
  223. xdma2 = dma2[dev];
  224. if (xdma2 == SNDRV_AUTO_DMA) {
  225. if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  226. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  227. err = -EBUSY;
  228. goto _err;
  229. }
  230. }
  231. if (port[dev] != SNDRV_AUTO_PORT) {
  232. err = snd_gus_create(card,
  233. port[dev],
  234. -xirq, xdma1, xdma2,
  235. 0, channels[dev],
  236. pcm_channels[dev],
  237. 0, &gus);
  238. } else {
  239. static unsigned long possible_ports[] = {
  240. 0x220, 0x230, 0x240, 0x250, 0x260
  241. };
  242. int i;
  243. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  244. err = snd_gus_create(card,
  245. possible_ports[i],
  246. -xirq, xdma1, xdma2,
  247. 0, channels[dev],
  248. pcm_channels[dev],
  249. 0, &gus);
  250. if (err >= 0) {
  251. port[dev] = possible_ports[i];
  252. break;
  253. }
  254. }
  255. }
  256. if (err < 0)
  257. goto _err;
  258. if ((err = snd_gusmax_detect(gus)) < 0)
  259. goto _err;
  260. maxcard->gus_status_reg = gus->gf1.reg_irqstat;
  261. maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2;
  262. snd_gusmax_init(dev, card, gus);
  263. if ((err = snd_gus_initialize(gus)) < 0)
  264. goto _err;
  265. if (!gus->max_flag) {
  266. snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port);
  267. err = -ENODEV;
  268. goto _err;
  269. }
  270. if (request_irq(xirq, snd_gusmax_interrupt, IRQF_DISABLED, "GUS MAX", (void *)maxcard)) {
  271. snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq);
  272. err = -EBUSY;
  273. goto _err;
  274. }
  275. maxcard->irq = xirq;
  276. if ((err = snd_cs4231_create(card,
  277. gus->gf1.port + 0x10c, -1, xirq,
  278. xdma2 < 0 ? xdma1 : xdma2, xdma1,
  279. CS4231_HW_DETECT,
  280. CS4231_HWSHARE_IRQ |
  281. CS4231_HWSHARE_DMA1 |
  282. CS4231_HWSHARE_DMA2,
  283. &cs4231)) < 0)
  284. goto _err;
  285. if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
  286. goto _err;
  287. if ((err = snd_cs4231_mixer(cs4231)) < 0)
  288. goto _err;
  289. if ((err = snd_cs4231_timer(cs4231, 2, NULL)) < 0)
  290. goto _err;
  291. if (pcm_channels[dev] > 0) {
  292. if ((err = snd_gf1_pcm_new(gus, 1, 1, NULL)) < 0)
  293. goto _err;
  294. }
  295. if ((err = snd_gusmax_mixer(cs4231)) < 0)
  296. goto _err;
  297. if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
  298. goto _err;
  299. sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1);
  300. if (xdma2 >= 0)
  301. sprintf(card->longname + strlen(card->longname), "&%i", xdma2);
  302. snd_card_set_dev(card, &pdev->dev);
  303. if ((err = snd_card_register(card)) < 0)
  304. goto _err;
  305. maxcard->gus = gus;
  306. maxcard->cs4231 = cs4231;
  307. platform_set_drvdata(pdev, card);
  308. return 0;
  309. _err:
  310. snd_card_free(card);
  311. return err;
  312. }
  313. static int __devexit snd_gusmax_remove(struct platform_device *devptr)
  314. {
  315. snd_card_free(platform_get_drvdata(devptr));
  316. platform_set_drvdata(devptr, NULL);
  317. return 0;
  318. }
  319. #define GUSMAX_DRIVER "snd_gusmax"
  320. static struct platform_driver snd_gusmax_driver = {
  321. .probe = snd_gusmax_probe,
  322. .remove = __devexit_p(snd_gusmax_remove),
  323. /* FIXME: suspend/resume */
  324. .driver = {
  325. .name = GUSMAX_DRIVER
  326. },
  327. };
  328. static void __init_or_module snd_gusmax_unregister_all(void)
  329. {
  330. int i;
  331. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  332. platform_device_unregister(devices[i]);
  333. platform_driver_unregister(&snd_gusmax_driver);
  334. }
  335. static int __init alsa_card_gusmax_init(void)
  336. {
  337. int i, cards, err;
  338. err = platform_driver_register(&snd_gusmax_driver);
  339. if (err < 0)
  340. return err;
  341. cards = 0;
  342. for (i = 0; i < SNDRV_CARDS; i++) {
  343. struct platform_device *device;
  344. if (! enable[i])
  345. continue;
  346. device = platform_device_register_simple(GUSMAX_DRIVER,
  347. i, NULL, 0);
  348. if (IS_ERR(device))
  349. continue;
  350. if (!platform_get_drvdata(device)) {
  351. platform_device_unregister(device);
  352. continue;
  353. }
  354. devices[i] = device;
  355. cards++;
  356. }
  357. if (!cards) {
  358. #ifdef MODULE
  359. printk(KERN_ERR "GUS MAX soundcard not found or device busy\n");
  360. #endif
  361. snd_gusmax_unregister_all();
  362. return -ENODEV;
  363. }
  364. return 0;
  365. }
  366. static void __exit alsa_card_gusmax_exit(void)
  367. {
  368. snd_gusmax_unregister_all();
  369. }
  370. module_init(alsa_card_gusmax_init)
  371. module_exit(alsa_card_gusmax_exit)