gus_main.c 12 KB

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