gus_reset.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/time.h>
  23. #include <sound/core.h>
  24. #include <sound/gus.h>
  25. extern void snd_gf1_timers_init(struct snd_gus_card * gus);
  26. extern void snd_gf1_timers_done(struct snd_gus_card * gus);
  27. extern int snd_gf1_synth_init(struct snd_gus_card * gus);
  28. extern void snd_gf1_synth_done(struct snd_gus_card * gus);
  29. /*
  30. * ok.. default interrupt handlers...
  31. */
  32. static void snd_gf1_default_interrupt_handler_midi_out(struct snd_gus_card * gus)
  33. {
  34. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x20);
  35. }
  36. static void snd_gf1_default_interrupt_handler_midi_in(struct snd_gus_card * gus)
  37. {
  38. snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x80);
  39. }
  40. static void snd_gf1_default_interrupt_handler_timer1(struct snd_gus_card * gus)
  41. {
  42. snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~4);
  43. }
  44. static void snd_gf1_default_interrupt_handler_timer2(struct snd_gus_card * gus)
  45. {
  46. snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~8);
  47. }
  48. static void snd_gf1_default_interrupt_handler_wave_and_volume(struct snd_gus_card * gus, struct snd_gus_voice * voice)
  49. {
  50. snd_gf1_i_ctrl_stop(gus, 0x00);
  51. snd_gf1_i_ctrl_stop(gus, 0x0d);
  52. }
  53. static void snd_gf1_default_interrupt_handler_dma_write(struct snd_gus_card * gus)
  54. {
  55. snd_gf1_i_write8(gus, 0x41, 0x00);
  56. }
  57. static void snd_gf1_default_interrupt_handler_dma_read(struct snd_gus_card * gus)
  58. {
  59. snd_gf1_i_write8(gus, 0x49, 0x00);
  60. }
  61. void snd_gf1_set_default_handlers(struct snd_gus_card * gus, unsigned int what)
  62. {
  63. if (what & SNDRV_GF1_HANDLER_MIDI_OUT)
  64. gus->gf1.interrupt_handler_midi_out = snd_gf1_default_interrupt_handler_midi_out;
  65. if (what & SNDRV_GF1_HANDLER_MIDI_IN)
  66. gus->gf1.interrupt_handler_midi_in = snd_gf1_default_interrupt_handler_midi_in;
  67. if (what & SNDRV_GF1_HANDLER_TIMER1)
  68. gus->gf1.interrupt_handler_timer1 = snd_gf1_default_interrupt_handler_timer1;
  69. if (what & SNDRV_GF1_HANDLER_TIMER2)
  70. gus->gf1.interrupt_handler_timer2 = snd_gf1_default_interrupt_handler_timer2;
  71. if (what & SNDRV_GF1_HANDLER_VOICE) {
  72. struct snd_gus_voice *voice;
  73. voice = &gus->gf1.voices[what & 0xffff];
  74. voice->handler_wave =
  75. voice->handler_volume = snd_gf1_default_interrupt_handler_wave_and_volume;
  76. voice->handler_effect = NULL;
  77. voice->volume_change = NULL;
  78. }
  79. if (what & SNDRV_GF1_HANDLER_DMA_WRITE)
  80. gus->gf1.interrupt_handler_dma_write = snd_gf1_default_interrupt_handler_dma_write;
  81. if (what & SNDRV_GF1_HANDLER_DMA_READ)
  82. gus->gf1.interrupt_handler_dma_read = snd_gf1_default_interrupt_handler_dma_read;
  83. }
  84. /*
  85. */
  86. static void snd_gf1_clear_regs(struct snd_gus_card * gus)
  87. {
  88. unsigned long flags;
  89. spin_lock_irqsave(&gus->reg_lock, flags);
  90. inb(GUSP(gus, IRQSTAT));
  91. snd_gf1_write8(gus, 0x41, 0); /* DRAM DMA Control Register */
  92. snd_gf1_write8(gus, 0x45, 0); /* Timer Control */
  93. snd_gf1_write8(gus, 0x49, 0); /* Sampling Control Register */
  94. spin_unlock_irqrestore(&gus->reg_lock, flags);
  95. }
  96. static void snd_gf1_look_regs(struct snd_gus_card * gus)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&gus->reg_lock, flags);
  100. snd_gf1_look8(gus, 0x41); /* DRAM DMA Control Register */
  101. snd_gf1_look8(gus, 0x49); /* Sampling Control Register */
  102. inb(GUSP(gus, IRQSTAT));
  103. snd_gf1_read8(gus, 0x0f); /* IRQ Source Register */
  104. spin_unlock_irqrestore(&gus->reg_lock, flags);
  105. }
  106. /*
  107. * put selected GF1 voices to initial stage...
  108. */
  109. void snd_gf1_smart_stop_voice(struct snd_gus_card * gus, unsigned short voice)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&gus->reg_lock, flags);
  113. snd_gf1_select_voice(gus, voice);
  114. #if 0
  115. printk(KERN_DEBUG " -%i- smart stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
  116. #endif
  117. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
  118. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  119. spin_unlock_irqrestore(&gus->reg_lock, flags);
  120. }
  121. void snd_gf1_stop_voice(struct snd_gus_card * gus, unsigned short voice)
  122. {
  123. unsigned long flags;
  124. spin_lock_irqsave(&gus->reg_lock, flags);
  125. snd_gf1_select_voice(gus, voice);
  126. #if 0
  127. printk(KERN_DEBUG " -%i- stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
  128. #endif
  129. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
  130. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  131. if (gus->gf1.enh_mode)
  132. snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
  133. spin_unlock_irqrestore(&gus->reg_lock, flags);
  134. #if 0
  135. snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_VIBRATO);
  136. snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_TREMOLO);
  137. #endif
  138. }
  139. static void snd_gf1_clear_voices(struct snd_gus_card * gus, unsigned short v_min,
  140. unsigned short v_max)
  141. {
  142. unsigned long flags;
  143. unsigned int daddr;
  144. unsigned short i, w_16;
  145. daddr = gus->gf1.default_voice_address << 4;
  146. for (i = v_min; i <= v_max; i++) {
  147. #if 0
  148. if (gus->gf1.syn_voices)
  149. gus->gf1.syn_voices[i].flags = ~VFLG_DYNAMIC;
  150. #endif
  151. spin_lock_irqsave(&gus->reg_lock, flags);
  152. snd_gf1_select_voice(gus, i);
  153. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); /* Voice Control Register = voice stop */
  154. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); /* Volume Ramp Control Register = ramp off */
  155. if (gus->gf1.enh_mode)
  156. snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, gus->gf1.memory ? 0x02 : 0x82); /* Deactivate voice */
  157. w_16 = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & 0x04;
  158. snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, 0x400);
  159. snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, daddr, w_16);
  160. snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, daddr, w_16);
  161. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, 0);
  162. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, 0);
  163. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0);
  164. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, 0);
  165. snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, daddr, w_16);
  166. snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, 7);
  167. if (gus->gf1.enh_mode) {
  168. snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
  169. snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME, 0);
  170. snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME_FINAL, 0);
  171. }
  172. spin_unlock_irqrestore(&gus->reg_lock, flags);
  173. #if 0
  174. snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_VIBRATO);
  175. snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_TREMOLO);
  176. #endif
  177. }
  178. }
  179. void snd_gf1_stop_voices(struct snd_gus_card * gus, unsigned short v_min, unsigned short v_max)
  180. {
  181. unsigned long flags;
  182. short i, ramp_ok;
  183. unsigned short ramp_end;
  184. if (!in_interrupt()) { /* this can't be done in interrupt */
  185. for (i = v_min, ramp_ok = 0; i <= v_max; i++) {
  186. spin_lock_irqsave(&gus->reg_lock, flags);
  187. snd_gf1_select_voice(gus, i);
  188. ramp_end = snd_gf1_read16(gus, 9) >> 8;
  189. if (ramp_end > SNDRV_GF1_MIN_OFFSET) {
  190. ramp_ok++;
  191. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 20); /* ramp rate */
  192. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET); /* ramp start */
  193. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, ramp_end); /* ramp end */
  194. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40); /* ramp down */
  195. if (gus->gf1.enh_mode) {
  196. snd_gf1_delay(gus);
  197. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40);
  198. }
  199. }
  200. spin_unlock_irqrestore(&gus->reg_lock, flags);
  201. }
  202. msleep_interruptible(50);
  203. }
  204. snd_gf1_clear_voices(gus, v_min, v_max);
  205. }
  206. static void snd_gf1_alloc_voice_use(struct snd_gus_card * gus,
  207. struct snd_gus_voice * pvoice,
  208. int type, int client, int port)
  209. {
  210. pvoice->use = 1;
  211. switch (type) {
  212. case SNDRV_GF1_VOICE_TYPE_PCM:
  213. gus->gf1.pcm_alloc_voices++;
  214. pvoice->pcm = 1;
  215. break;
  216. case SNDRV_GF1_VOICE_TYPE_SYNTH:
  217. pvoice->synth = 1;
  218. pvoice->client = client;
  219. pvoice->port = port;
  220. break;
  221. case SNDRV_GF1_VOICE_TYPE_MIDI:
  222. pvoice->midi = 1;
  223. pvoice->client = client;
  224. pvoice->port = port;
  225. break;
  226. }
  227. }
  228. struct snd_gus_voice *snd_gf1_alloc_voice(struct snd_gus_card * gus, int type, int client, int port)
  229. {
  230. struct snd_gus_voice *pvoice;
  231. unsigned long flags;
  232. int idx;
  233. spin_lock_irqsave(&gus->voice_alloc, flags);
  234. if (type == SNDRV_GF1_VOICE_TYPE_PCM) {
  235. if (gus->gf1.pcm_alloc_voices >= gus->gf1.pcm_channels) {
  236. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  237. return NULL;
  238. }
  239. }
  240. for (idx = 0; idx < 32; idx++) {
  241. pvoice = &gus->gf1.voices[idx];
  242. if (!pvoice->use) {
  243. snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
  244. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  245. return pvoice;
  246. }
  247. }
  248. for (idx = 0; idx < 32; idx++) {
  249. pvoice = &gus->gf1.voices[idx];
  250. if (pvoice->midi && !pvoice->client) {
  251. snd_gf1_clear_voices(gus, pvoice->number, pvoice->number);
  252. snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
  253. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  254. return pvoice;
  255. }
  256. }
  257. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  258. return NULL;
  259. }
  260. void snd_gf1_free_voice(struct snd_gus_card * gus, struct snd_gus_voice *voice)
  261. {
  262. unsigned long flags;
  263. void (*private_free)(struct snd_gus_voice *voice);
  264. void *private_data;
  265. if (voice == NULL || !voice->use)
  266. return;
  267. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | voice->number);
  268. snd_gf1_clear_voices(gus, voice->number, voice->number);
  269. spin_lock_irqsave(&gus->voice_alloc, flags);
  270. private_free = voice->private_free;
  271. private_data = voice->private_data;
  272. voice->private_free = NULL;
  273. voice->private_data = NULL;
  274. if (voice->pcm)
  275. gus->gf1.pcm_alloc_voices--;
  276. voice->use = voice->pcm = 0;
  277. voice->sample_ops = NULL;
  278. spin_unlock_irqrestore(&gus->voice_alloc, flags);
  279. if (private_free)
  280. private_free(voice);
  281. }
  282. /*
  283. * call this function only by start of driver
  284. */
  285. int snd_gf1_start(struct snd_gus_card * gus)
  286. {
  287. unsigned long flags;
  288. unsigned int i;
  289. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
  290. udelay(160);
  291. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* disable IRQ & DAC */
  292. udelay(160);
  293. snd_gf1_i_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
  294. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_ALL);
  295. for (i = 0; i < 32; i++) {
  296. gus->gf1.voices[i].number = i;
  297. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | i);
  298. }
  299. snd_gf1_uart_cmd(gus, 0x03); /* huh.. this cleanup took me some time... */
  300. if (gus->gf1.enh_mode) { /* enhanced mode !!!! */
  301. snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
  302. snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
  303. }
  304. snd_gf1_clear_regs(gus);
  305. snd_gf1_select_active_voices(gus);
  306. snd_gf1_delay(gus);
  307. gus->gf1.default_voice_address = gus->gf1.memory > 0 ? 0 : 512 - 8;
  308. /* initialize LFOs & clear LFOs memory */
  309. if (gus->gf1.enh_mode && gus->gf1.memory) {
  310. gus->gf1.hw_lfo = 1;
  311. gus->gf1.default_voice_address += 1024;
  312. } else {
  313. gus->gf1.sw_lfo = 1;
  314. }
  315. #if 0
  316. snd_gf1_lfo_init(gus);
  317. #endif
  318. if (gus->gf1.memory > 0)
  319. for (i = 0; i < 4; i++)
  320. snd_gf1_poke(gus, gus->gf1.default_voice_address + i, 0);
  321. snd_gf1_clear_regs(gus);
  322. snd_gf1_clear_voices(gus, 0, 31);
  323. snd_gf1_look_regs(gus);
  324. udelay(160);
  325. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7); /* Reset Register = IRQ enable, DAC enable */
  326. udelay(160);
  327. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7); /* Reset Register = IRQ enable, DAC enable */
  328. if (gus->gf1.enh_mode) { /* enhanced mode !!!! */
  329. snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
  330. snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
  331. }
  332. while ((snd_gf1_i_read8(gus, SNDRV_GF1_GB_VOICES_IRQ) & 0xc0) != 0xc0);
  333. spin_lock_irqsave(&gus->reg_lock, flags);
  334. outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
  335. outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
  336. spin_unlock_irqrestore(&gus->reg_lock, flags);
  337. snd_gf1_timers_init(gus);
  338. snd_gf1_look_regs(gus);
  339. snd_gf1_mem_init(gus);
  340. snd_gf1_mem_proc_init(gus);
  341. #ifdef CONFIG_SND_DEBUG
  342. snd_gus_irq_profile_init(gus);
  343. #endif
  344. #if 0
  345. if (gus->pnp_flag) {
  346. if (gus->chip.playback_fifo_size > 0)
  347. snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_RECORD_BASE_ADDR, gus->chip.playback_fifo_block->ptr >> 8);
  348. if (gus->chip.record_fifo_size > 0)
  349. snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_PLAY_BASE_ADDR, gus->chip.record_fifo_block->ptr >> 8);
  350. snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_SIZE, gus->chip.interwave_fifo_reg);
  351. }
  352. #endif
  353. return 0;
  354. }
  355. /*
  356. * call this function only by shutdown of driver
  357. */
  358. int snd_gf1_stop(struct snd_gus_card * gus)
  359. {
  360. snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0); /* stop all timers */
  361. snd_gf1_stop_voices(gus, 0, 31); /* stop all voices */
  362. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* disable IRQ & DAC */
  363. snd_gf1_timers_done(gus);
  364. snd_gf1_mem_done(gus);
  365. #if 0
  366. snd_gf1_lfo_done(gus);
  367. #endif
  368. return 0;
  369. }