gus_main.c 14 KB

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