gus_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Routines for Gravis UltraSound soundcards
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/gus.h>
  29. #include <sound/control.h>
  30. #include <asm/dma.h>
  31. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  32. MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
  33. MODULE_LICENSE("GPL");
  34. static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches);
  35. int snd_gus_use_inc(struct snd_gus_card * gus)
  36. {
  37. if (!try_module_get(gus->card->module))
  38. return 0;
  39. return 1;
  40. }
  41. void snd_gus_use_dec(struct snd_gus_card * gus)
  42. {
  43. module_put(gus->card->module);
  44. }
  45. static int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  46. {
  47. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  48. uinfo->count = 1;
  49. uinfo->value.integer.min = 0;
  50. uinfo->value.integer.max = 31;
  51. return 0;
  52. }
  53. static int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  54. {
  55. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  56. ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
  57. return 0;
  58. }
  59. static int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  60. {
  61. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  62. unsigned long flags;
  63. int change;
  64. unsigned char nval;
  65. nval = ucontrol->value.integer.value[0] & 31;
  66. spin_lock_irqsave(&gus->reg_lock, flags);
  67. change = gus->joystick_dac != nval;
  68. gus->joystick_dac = nval;
  69. snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
  70. spin_unlock_irqrestore(&gus->reg_lock, flags);
  71. return change;
  72. }
  73. static struct snd_kcontrol_new snd_gus_joystick_control = {
  74. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  75. .name = "Joystick Speed",
  76. .info = snd_gus_joystick_info,
  77. .get = snd_gus_joystick_get,
  78. .put = snd_gus_joystick_put
  79. };
  80. static void snd_gus_init_control(struct snd_gus_card *gus)
  81. {
  82. if (!gus->ace_flag)
  83. snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus));
  84. }
  85. /*
  86. *
  87. */
  88. static int snd_gus_free(struct snd_gus_card *gus)
  89. {
  90. if (gus->gf1.res_port2 == NULL)
  91. goto __hw_end;
  92. snd_gf1_stop(gus);
  93. snd_gus_init_dma_irq(gus, 0);
  94. __hw_end:
  95. release_and_free_resource(gus->gf1.res_port1);
  96. release_and_free_resource(gus->gf1.res_port2);
  97. if (gus->gf1.irq >= 0)
  98. free_irq(gus->gf1.irq, (void *) gus);
  99. if (gus->gf1.dma1 >= 0) {
  100. disable_dma(gus->gf1.dma1);
  101. free_dma(gus->gf1.dma1);
  102. }
  103. if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
  104. disable_dma(gus->gf1.dma2);
  105. free_dma(gus->gf1.dma2);
  106. }
  107. kfree(gus);
  108. return 0;
  109. }
  110. static int snd_gus_dev_free(struct snd_device *device)
  111. {
  112. struct snd_gus_card *gus = device->device_data;
  113. return snd_gus_free(gus);
  114. }
  115. int snd_gus_create(struct snd_card *card,
  116. unsigned long port,
  117. int irq, int dma1, int dma2,
  118. int timer_dev,
  119. int voices,
  120. int pcm_channels,
  121. int effect,
  122. struct snd_gus_card **rgus)
  123. {
  124. struct snd_gus_card *gus;
  125. int err;
  126. static struct snd_device_ops ops = {
  127. .dev_free = snd_gus_dev_free,
  128. };
  129. *rgus = NULL;
  130. gus = kzalloc(sizeof(*gus), GFP_KERNEL);
  131. if (gus == NULL)
  132. return -ENOMEM;
  133. spin_lock_init(&gus->reg_lock);
  134. spin_lock_init(&gus->voice_alloc);
  135. spin_lock_init(&gus->active_voice_lock);
  136. spin_lock_init(&gus->event_lock);
  137. spin_lock_init(&gus->dma_lock);
  138. spin_lock_init(&gus->pcm_volume_level_lock);
  139. spin_lock_init(&gus->uart_cmd_lock);
  140. mutex_init(&gus->dma_mutex);
  141. gus->gf1.irq = -1;
  142. gus->gf1.dma1 = -1;
  143. gus->gf1.dma2 = -1;
  144. gus->card = card;
  145. gus->gf1.port = port;
  146. /* fill register variables for speedup */
  147. gus->gf1.reg_page = GUSP(gus, GF1PAGE);
  148. gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
  149. gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
  150. gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
  151. gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
  152. gus->gf1.reg_dram = GUSP(gus, DRAM);
  153. gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
  154. gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
  155. /* allocate resources */
  156. if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) {
  157. snd_printk(KERN_ERR "gus: can't grab SB port 0x%lx\n", port);
  158. snd_gus_free(gus);
  159. return -EBUSY;
  160. }
  161. if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) {
  162. snd_printk(KERN_ERR "gus: can't grab synth port 0x%lx\n", port + 0x100);
  163. snd_gus_free(gus);
  164. return -EBUSY;
  165. }
  166. if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) {
  167. snd_printk(KERN_ERR "gus: can't grab irq %d\n", irq);
  168. snd_gus_free(gus);
  169. return -EBUSY;
  170. }
  171. gus->gf1.irq = irq;
  172. if (request_dma(dma1, "GUS - 1")) {
  173. snd_printk(KERN_ERR "gus: can't grab DMA1 %d\n", dma1);
  174. snd_gus_free(gus);
  175. return -EBUSY;
  176. }
  177. gus->gf1.dma1 = dma1;
  178. if (dma2 >= 0 && dma1 != dma2) {
  179. if (request_dma(dma2, "GUS - 2")) {
  180. snd_printk(KERN_ERR "gus: can't grab DMA2 %d\n", dma2);
  181. snd_gus_free(gus);
  182. return -EBUSY;
  183. }
  184. gus->gf1.dma2 = dma2;
  185. } else {
  186. gus->gf1.dma2 = gus->gf1.dma1;
  187. gus->equal_dma = 1;
  188. }
  189. gus->timer_dev = timer_dev;
  190. if (voices < 14)
  191. voices = 14;
  192. if (voices > 32)
  193. voices = 32;
  194. if (pcm_channels < 0)
  195. pcm_channels = 0;
  196. if (pcm_channels > 8)
  197. pcm_channels = 8;
  198. pcm_channels++;
  199. pcm_channels &= ~1;
  200. gus->gf1.effect = effect ? 1 : 0;
  201. gus->gf1.active_voices = voices;
  202. gus->gf1.pcm_channels = pcm_channels;
  203. gus->gf1.volume_ramp = 25;
  204. gus->gf1.smooth_pan = 1;
  205. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
  206. snd_gus_free(gus);
  207. return err;
  208. }
  209. *rgus = gus;
  210. return 0;
  211. }
  212. /*
  213. * Memory detection routine for plain GF1 soundcards
  214. */
  215. static int snd_gus_detect_memory(struct snd_gus_card * gus)
  216. {
  217. int l, idx, local;
  218. unsigned char d;
  219. snd_gf1_poke(gus, 0L, 0xaa);
  220. snd_gf1_poke(gus, 1L, 0x55);
  221. if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
  222. snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
  223. return -ENOMEM;
  224. }
  225. for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
  226. local = idx << 18;
  227. snd_gf1_poke(gus, local, d);
  228. snd_gf1_poke(gus, local + 1, d + 1);
  229. if (snd_gf1_peek(gus, local) != d ||
  230. snd_gf1_peek(gus, local + 1) != d + 1 ||
  231. snd_gf1_peek(gus, 0L) != 0xaa)
  232. break;
  233. }
  234. #if 1
  235. gus->gf1.memory = idx << 18;
  236. #else
  237. gus->gf1.memory = 256 * 1024;
  238. #endif
  239. for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
  240. gus->gf1.mem_alloc.banks_8[l].address =
  241. gus->gf1.mem_alloc.banks_8[l].size = 0;
  242. gus->gf1.mem_alloc.banks_16[l].address = l << 18;
  243. gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
  244. }
  245. gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
  246. return 0; /* some memory were detected */
  247. }
  248. static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
  249. {
  250. struct snd_card *card;
  251. unsigned long flags;
  252. int irq, dma1, dma2;
  253. static unsigned char irqs[16] =
  254. {0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
  255. static unsigned char dmas[8] =
  256. {6, 1, 0, 2, 0, 3, 4, 5};
  257. if (snd_BUG_ON(!gus))
  258. return -EINVAL;
  259. card = gus->card;
  260. if (snd_BUG_ON(!card))
  261. return -EINVAL;
  262. gus->mix_cntrl_reg &= 0xf8;
  263. gus->mix_cntrl_reg |= 0x01; /* disable MIC, LINE IN, enable LINE OUT */
  264. if (gus->codec_flag || gus->ess_flag) {
  265. gus->mix_cntrl_reg &= ~1; /* enable LINE IN */
  266. gus->mix_cntrl_reg |= 4; /* enable MIC */
  267. }
  268. dma1 = gus->gf1.dma1;
  269. dma1 = abs(dma1);
  270. dma1 = dmas[dma1 & 7];
  271. dma2 = gus->gf1.dma2;
  272. dma2 = abs(dma2);
  273. dma2 = dmas[dma2 & 7];
  274. dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
  275. if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
  276. snd_printk(KERN_ERR "Error! DMA isn't defined.\n");
  277. return -EINVAL;
  278. }
  279. irq = gus->gf1.irq;
  280. irq = abs(irq);
  281. irq = irqs[irq & 0x0f];
  282. if (irq == 0) {
  283. snd_printk(KERN_ERR "Error! IRQ isn't defined.\n");
  284. return -EINVAL;
  285. }
  286. irq |= 0x40;
  287. #if 0
  288. card->mixer.mix_ctrl_reg |= 0x10;
  289. #endif
  290. spin_lock_irqsave(&gus->reg_lock, flags);
  291. outb(5, GUSP(gus, REGCNTRLS));
  292. outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  293. outb(0x00, GUSP(gus, IRQDMACNTRLREG));
  294. outb(0, GUSP(gus, REGCNTRLS));
  295. spin_unlock_irqrestore(&gus->reg_lock, flags);
  296. udelay(100);
  297. spin_lock_irqsave(&gus->reg_lock, flags);
  298. outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  299. outb(dma1, GUSP(gus, IRQDMACNTRLREG));
  300. if (latches) {
  301. outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  302. outb(irq, GUSP(gus, IRQDMACNTRLREG));
  303. }
  304. spin_unlock_irqrestore(&gus->reg_lock, flags);
  305. udelay(100);
  306. spin_lock_irqsave(&gus->reg_lock, flags);
  307. outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  308. outb(dma1, GUSP(gus, IRQDMACNTRLREG));
  309. if (latches) {
  310. outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  311. outb(irq, GUSP(gus, IRQDMACNTRLREG));
  312. }
  313. spin_unlock_irqrestore(&gus->reg_lock, flags);
  314. snd_gf1_delay(gus);
  315. if (latches)
  316. gus->mix_cntrl_reg |= 0x08; /* enable latches */
  317. else
  318. gus->mix_cntrl_reg &= ~0x08; /* disable latches */
  319. spin_lock_irqsave(&gus->reg_lock, flags);
  320. outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  321. outb(0, GUSP(gus, GF1PAGE));
  322. spin_unlock_irqrestore(&gus->reg_lock, flags);
  323. return 0;
  324. }
  325. static int snd_gus_check_version(struct snd_gus_card * gus)
  326. {
  327. unsigned long flags;
  328. unsigned char val, rev;
  329. struct snd_card *card;
  330. card = gus->card;
  331. spin_lock_irqsave(&gus->reg_lock, flags);
  332. outb(0x20, GUSP(gus, REGCNTRLS));
  333. val = inb(GUSP(gus, REGCNTRLS));
  334. rev = inb(GUSP(gus, BOARDVERSION));
  335. spin_unlock_irqrestore(&gus->reg_lock, flags);
  336. snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
  337. strcpy(card->driver, "GUS");
  338. strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
  339. if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
  340. if (rev >= 5 && rev <= 9) {
  341. gus->ics_flag = 1;
  342. if (rev == 5)
  343. gus->ics_flipped = 1;
  344. card->longname[27] = '3';
  345. card->longname[29] = rev == 5 ? '5' : '7';
  346. }
  347. if (rev >= 10 && rev != 255) {
  348. if (rev >= 10 && rev <= 11) {
  349. strcpy(card->driver, "GUS MAX");
  350. strcpy(card->longname, "Gravis UltraSound MAX");
  351. gus->max_flag = 1;
  352. } else if (rev == 0x30) {
  353. strcpy(card->driver, "GUS ACE");
  354. strcpy(card->longname, "Gravis UltraSound Ace");
  355. gus->ace_flag = 1;
  356. } else if (rev == 0x50) {
  357. strcpy(card->driver, "GUS Extreme");
  358. strcpy(card->longname, "Gravis UltraSound Extreme");
  359. gus->ess_flag = 1;
  360. } else {
  361. snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
  362. snd_printk(KERN_ERR " please - report to <perex@perex.cz>\n");
  363. }
  364. }
  365. }
  366. strcpy(card->shortname, card->longname);
  367. gus->uart_enable = 1; /* standard GUSes doesn't have midi uart trouble */
  368. snd_gus_init_control(gus);
  369. return 0;
  370. }
  371. int snd_gus_initialize(struct snd_gus_card *gus)
  372. {
  373. int err;
  374. if (!gus->interwave) {
  375. if ((err = snd_gus_check_version(gus)) < 0) {
  376. snd_printk(KERN_ERR "version check failed\n");
  377. return err;
  378. }
  379. if ((err = snd_gus_detect_memory(gus)) < 0)
  380. return err;
  381. }
  382. if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
  383. return err;
  384. snd_gf1_start(gus);
  385. gus->initialized = 1;
  386. return 0;
  387. }
  388. /* gus_io.c */
  389. EXPORT_SYMBOL(snd_gf1_delay);
  390. EXPORT_SYMBOL(snd_gf1_write8);
  391. EXPORT_SYMBOL(snd_gf1_look8);
  392. EXPORT_SYMBOL(snd_gf1_write16);
  393. EXPORT_SYMBOL(snd_gf1_look16);
  394. EXPORT_SYMBOL(snd_gf1_i_write8);
  395. EXPORT_SYMBOL(snd_gf1_i_look8);
  396. EXPORT_SYMBOL(snd_gf1_i_look16);
  397. EXPORT_SYMBOL(snd_gf1_dram_addr);
  398. EXPORT_SYMBOL(snd_gf1_write_addr);
  399. EXPORT_SYMBOL(snd_gf1_poke);
  400. EXPORT_SYMBOL(snd_gf1_peek);
  401. /* gus_reset.c */
  402. EXPORT_SYMBOL(snd_gf1_alloc_voice);
  403. EXPORT_SYMBOL(snd_gf1_free_voice);
  404. EXPORT_SYMBOL(snd_gf1_ctrl_stop);
  405. EXPORT_SYMBOL(snd_gf1_stop_voice);
  406. /* gus_mixer.c */
  407. EXPORT_SYMBOL(snd_gf1_new_mixer);
  408. /* gus_pcm.c */
  409. EXPORT_SYMBOL(snd_gf1_pcm_new);
  410. /* gus.c */
  411. EXPORT_SYMBOL(snd_gus_use_inc);
  412. EXPORT_SYMBOL(snd_gus_use_dec);
  413. EXPORT_SYMBOL(snd_gus_create);
  414. EXPORT_SYMBOL(snd_gus_initialize);
  415. /* gus_irq.c */
  416. EXPORT_SYMBOL(snd_gus_interrupt);
  417. /* gus_uart.c */
  418. EXPORT_SYMBOL(snd_gf1_rawmidi_new);
  419. /* gus_dram.c */
  420. EXPORT_SYMBOL(snd_gus_dram_write);
  421. EXPORT_SYMBOL(snd_gus_dram_read);
  422. /* gus_volume.c */
  423. EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
  424. EXPORT_SYMBOL(snd_gf1_translate_freq);
  425. /* gus_mem.c */
  426. EXPORT_SYMBOL(snd_gf1_mem_alloc);
  427. EXPORT_SYMBOL(snd_gf1_mem_xfree);
  428. EXPORT_SYMBOL(snd_gf1_mem_free);
  429. EXPORT_SYMBOL(snd_gf1_mem_lock);
  430. /*
  431. * INIT part
  432. */
  433. static int __init alsa_gus_init(void)
  434. {
  435. return 0;
  436. }
  437. static void __exit alsa_gus_exit(void)
  438. {
  439. }
  440. module_init(alsa_gus_init)
  441. module_exit(alsa_gus_exit)