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