gusmax.c 11 KB

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