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