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