2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
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>
27 extern void snd_gf1_timers_init(struct snd_gus_card
* gus
);
28 extern void snd_gf1_timers_done(struct snd_gus_card
* gus
);
29 extern int snd_gf1_synth_init(struct snd_gus_card
* gus
);
30 extern void snd_gf1_synth_done(struct snd_gus_card
* gus
);
33 * ok.. default interrupt handlers...
36 static void snd_gf1_default_interrupt_handler_midi_out(struct snd_gus_card
* gus
)
38 snd_gf1_uart_cmd(gus
, gus
->gf1
.uart_cmd
&= ~0x20);
41 static void snd_gf1_default_interrupt_handler_midi_in(struct snd_gus_card
* gus
)
43 snd_gf1_uart_cmd(gus
, gus
->gf1
.uart_cmd
&= ~0x80);
46 static void snd_gf1_default_interrupt_handler_timer1(struct snd_gus_card
* gus
)
48 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL
, gus
->gf1
.timer_enabled
&= ~4);
51 static void snd_gf1_default_interrupt_handler_timer2(struct snd_gus_card
* gus
)
53 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL
, gus
->gf1
.timer_enabled
&= ~8);
56 static void snd_gf1_default_interrupt_handler_wave_and_volume(struct snd_gus_card
* gus
, struct snd_gus_voice
* voice
)
58 snd_gf1_i_ctrl_stop(gus
, 0x00);
59 snd_gf1_i_ctrl_stop(gus
, 0x0d);
62 static void snd_gf1_default_interrupt_handler_dma_write(struct snd_gus_card
* gus
)
64 snd_gf1_i_write8(gus
, 0x41, 0x00);
67 static void snd_gf1_default_interrupt_handler_dma_read(struct snd_gus_card
* gus
)
69 snd_gf1_i_write8(gus
, 0x49, 0x00);
72 void snd_gf1_set_default_handlers(struct snd_gus_card
* gus
, unsigned int what
)
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
;
85 voice
= &gus
->gf1
.voices
[what
& 0xffff];
87 voice
->handler_volume
= snd_gf1_default_interrupt_handler_wave_and_volume
;
88 voice
->handler_effect
= NULL
;
89 voice
->volume_change
= NULL
;
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
;
101 static void snd_gf1_clear_regs(struct snd_gus_card
* gus
)
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
);
113 static void snd_gf1_look_regs(struct snd_gus_card
* gus
)
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
);
126 * put selected GF1 voices to initial stage...
129 void snd_gf1_smart_stop_voice(struct snd_gus_card
* gus
, unsigned short voice
)
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
);
140 void snd_gf1_stop_voice(struct snd_gus_card
* gus
, unsigned short voice
)
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
);
153 static void snd_gf1_clear_voices(struct snd_gus_card
* gus
, unsigned short v_min
,
154 unsigned short v_max
)
158 unsigned short i
, w_16
;
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);
183 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
187 void snd_gf1_stop_voices(struct snd_gus_card
* gus
, unsigned short v_min
, unsigned short v_max
)
191 unsigned short ramp_end
;
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
) {
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
) {
206 snd_gf1_write8(gus
, SNDRV_GF1_VB_VOLUME_CONTROL
, 0x40);
209 spin_unlock_irqrestore(&gus
->reg_lock
, flags
);
211 msleep_interruptible(50);
213 snd_gf1_clear_voices(gus
, v_min
, v_max
);
216 static void snd_gf1_alloc_voice_use(struct snd_gus_card
* gus
,
217 struct snd_gus_voice
* pvoice
,
218 int type
, int client
, int port
)
222 case SNDRV_GF1_VOICE_TYPE_PCM
:
223 gus
->gf1
.pcm_alloc_voices
++;
226 case SNDRV_GF1_VOICE_TYPE_SYNTH
:
228 pvoice
->client
= client
;
231 case SNDRV_GF1_VOICE_TYPE_MIDI
:
233 pvoice
->client
= client
;
239 struct snd_gus_voice
*snd_gf1_alloc_voice(struct snd_gus_card
* gus
, int type
, int client
, int port
)
241 struct snd_gus_voice
*pvoice
;
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
);
252 for (idx
= 0; idx
< 32; idx
++) {
253 pvoice
= &gus
->gf1
.voices
[idx
];
255 snd_gf1_alloc_voice_use(gus
, pvoice
, type
, client
, port
);
256 spin_unlock_irqrestore(&gus
->voice_alloc
, flags
);
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
);
269 spin_unlock_irqrestore(&gus
->voice_alloc
, flags
);
273 void snd_gf1_free_voice(struct snd_gus_card
* gus
, struct snd_gus_voice
*voice
)
276 void (*private_free
)(struct snd_gus_voice
*voice
);
279 if (voice
== NULL
|| !voice
->use
)
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
;
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
);
298 * call this function only by start of driver
301 int snd_gf1_start(struct snd_gus_card
* gus
)
306 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_RESET
, 0); /* reset GF1 */
308 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_RESET
, 1); /* disable IRQ & DAC */
310 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL
, gus
->joystick_dac
);
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
);
318 snd_gf1_uart_cmd(gus
, 0x03); /* huh.. this cleanup took me some time... */
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);
324 snd_gf1_clear_regs(gus
);
325 snd_gf1_select_active_voices(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
) {
331 gus
->gf1
.default_voice_address
+= 1024;
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
);
342 snd_gf1_i_write8(gus
, SNDRV_GF1_GB_RESET
, 7); /* Reset Register = IRQ enable, DAC enable */
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);
349 while ((snd_gf1_i_read8(gus
, SNDRV_GF1_GB_VOICES_IRQ
) & 0xc0) != 0xc0);
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
);
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
);
369 * call this function only by shutdown of driver
372 int snd_gf1_stop(struct snd_gus_card
* gus
)
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
);