gus_main.c 14 KB

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