gusclassic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Driver for Gravis UltraSound Classic 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. #define SNDRV_LEGACY_FIND_FREE_IRQ
  32. #define SNDRV_LEGACY_FIND_FREE_DMA
  33. #include <sound/initval.h>
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  35. MODULE_DESCRIPTION("Gravis UltraSound Classic");
  36. MODULE_LICENSE("GPL");
  37. MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Classic}}");
  38. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  39. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  40. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  41. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
  42. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,5,9,11,12,15 */
  43. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  44. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  45. static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
  46. /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
  47. static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
  48. static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  49. module_param_array(index, int, NULL, 0444);
  50. MODULE_PARM_DESC(index, "Index value for GUS Classic soundcard.");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "ID string for GUS Classic soundcard.");
  53. module_param_array(enable, bool, NULL, 0444);
  54. MODULE_PARM_DESC(enable, "Enable GUS Classic soundcard.");
  55. module_param_array(port, long, NULL, 0444);
  56. MODULE_PARM_DESC(port, "Port # for GUS Classic driver.");
  57. module_param_array(irq, int, NULL, 0444);
  58. MODULE_PARM_DESC(irq, "IRQ # for GUS Classic driver.");
  59. module_param_array(dma1, int, NULL, 0444);
  60. MODULE_PARM_DESC(dma1, "DMA1 # for GUS Classic driver.");
  61. module_param_array(dma2, int, NULL, 0444);
  62. MODULE_PARM_DESC(dma2, "DMA2 # for GUS Classic driver.");
  63. module_param_array(joystick_dac, int, NULL, 0444);
  64. MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Classic driver.");
  65. module_param_array(channels, int, NULL, 0444);
  66. MODULE_PARM_DESC(channels, "GF1 channels for GUS Classic driver.");
  67. module_param_array(pcm_channels, int, NULL, 0444);
  68. MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Classic driver.");
  69. static struct platform_device *devices[SNDRV_CARDS];
  70. #define PFX "gusclassic: "
  71. static int __devinit snd_gusclassic_detect(struct snd_gus_card * gus)
  72. {
  73. unsigned char d;
  74. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  75. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
  76. snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
  77. return -ENODEV;
  78. }
  79. udelay(160);
  80. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
  81. udelay(160);
  82. if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
  83. snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
  84. return -ENODEV;
  85. }
  86. return 0;
  87. }
  88. static void __devinit snd_gusclassic_init(int dev, struct snd_gus_card * gus)
  89. {
  90. gus->equal_irq = 0;
  91. gus->codec_flag = 0;
  92. gus->max_flag = 0;
  93. gus->joystick_dac = joystick_dac[dev];
  94. }
  95. static int __devinit snd_gusclassic_probe(struct platform_device *pdev)
  96. {
  97. int dev = pdev->id;
  98. static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1};
  99. static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
  100. int xirq, xdma1, xdma2;
  101. struct snd_card *card;
  102. struct snd_gus_card *gus = NULL;
  103. int err;
  104. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  105. if (card == NULL)
  106. return -ENOMEM;
  107. if (pcm_channels[dev] < 2)
  108. pcm_channels[dev] = 2;
  109. xirq = irq[dev];
  110. if (xirq == SNDRV_AUTO_IRQ) {
  111. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  112. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  113. err = -EBUSY;
  114. goto _err;
  115. }
  116. }
  117. xdma1 = dma1[dev];
  118. if (xdma1 == SNDRV_AUTO_DMA) {
  119. if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  120. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  121. err = -EBUSY;
  122. goto _err;
  123. }
  124. }
  125. xdma2 = dma2[dev];
  126. if (xdma2 == SNDRV_AUTO_DMA) {
  127. if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  128. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  129. err = -EBUSY;
  130. goto _err;
  131. }
  132. }
  133. if (port[dev] != SNDRV_AUTO_PORT) {
  134. err = snd_gus_create(card,
  135. port[dev],
  136. xirq, xdma1, xdma2,
  137. 0, channels[dev], pcm_channels[dev],
  138. 0, &gus);
  139. } else {
  140. /* auto-probe legacy ports */
  141. static unsigned long possible_ports[] = {
  142. 0x220, 0x230, 0x240, 0x250, 0x260,
  143. };
  144. int i;
  145. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  146. err = snd_gus_create(card,
  147. possible_ports[i],
  148. xirq, xdma1, xdma2,
  149. 0, channels[dev], pcm_channels[dev],
  150. 0, &gus);
  151. if (err >= 0) {
  152. port[dev] = possible_ports[i];
  153. break;
  154. }
  155. }
  156. }
  157. if (err < 0)
  158. goto _err;
  159. if ((err = snd_gusclassic_detect(gus)) < 0)
  160. goto _err;
  161. snd_gusclassic_init(dev, gus);
  162. if ((err = snd_gus_initialize(gus)) < 0)
  163. goto _err;
  164. if (gus->max_flag || gus->ess_flag) {
  165. snd_printk(KERN_ERR PFX "GUS Classic or ACE soundcard was not detected at 0x%lx\n", gus->gf1.port);
  166. err = -ENODEV;
  167. goto _err;
  168. }
  169. if ((err = snd_gf1_new_mixer(gus)) < 0)
  170. goto _err;
  171. if ((err = snd_gf1_pcm_new(gus, 0, 0, NULL)) < 0)
  172. goto _err;
  173. if (!gus->ace_flag) {
  174. if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
  175. goto _err;
  176. }
  177. sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, xirq, xdma1);
  178. if (xdma2 >= 0)
  179. sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
  180. snd_card_set_dev(card, &pdev->dev);
  181. if ((err = snd_card_register(card)) < 0)
  182. goto _err;
  183. platform_set_drvdata(pdev, card);
  184. return 0;
  185. _err:
  186. snd_card_free(card);
  187. return err;
  188. }
  189. static int __devexit snd_gusclassic_remove(struct platform_device *devptr)
  190. {
  191. snd_card_free(platform_get_drvdata(devptr));
  192. platform_set_drvdata(devptr, NULL);
  193. return 0;
  194. }
  195. #define GUSCLASSIC_DRIVER "snd_gusclassic"
  196. static struct platform_driver snd_gusclassic_driver = {
  197. .probe = snd_gusclassic_probe,
  198. .remove = __devexit_p(snd_gusclassic_remove),
  199. /* FIXME: suspend/resume */
  200. .driver = {
  201. .name = GUSCLASSIC_DRIVER
  202. },
  203. };
  204. static void __init_or_module snd_gusclassic_unregister_all(void)
  205. {
  206. int i;
  207. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  208. platform_device_unregister(devices[i]);
  209. platform_driver_unregister(&snd_gusclassic_driver);
  210. }
  211. static int __init alsa_card_gusclassic_init(void)
  212. {
  213. int i, cards, err;
  214. err = platform_driver_register(&snd_gusclassic_driver);
  215. if (err < 0)
  216. return err;
  217. cards = 0;
  218. for (i = 0; i < SNDRV_CARDS; i++) {
  219. struct platform_device *device;
  220. if (! enable[i])
  221. continue;
  222. device = platform_device_register_simple(GUSCLASSIC_DRIVER,
  223. i, NULL, 0);
  224. if (IS_ERR(device))
  225. continue;
  226. if (!platform_get_drvdata(device)) {
  227. platform_device_unregister(device);
  228. continue;
  229. }
  230. devices[i] = device;
  231. cards++;
  232. }
  233. if (!cards) {
  234. #ifdef MODULE
  235. printk(KERN_ERR "GUS Classic soundcard not found or device busy\n");
  236. #endif
  237. snd_gusclassic_unregister_all();
  238. return -ENODEV;
  239. }
  240. return 0;
  241. }
  242. static void __exit alsa_card_gusclassic_exit(void)
  243. {
  244. snd_gusclassic_unregister_all();
  245. }
  246. module_init(alsa_card_gusclassic_init)
  247. module_exit(alsa_card_gusclassic_exit)