Import 2.1.81
[davej-history.git] / drivers / sound / gus_wave.c
blob6f3f4a6890607bf4225e1c2289a0b642381a11e4
1 /*
2 * sound/gus_wave.c
4 * Driver for the Gravis UltraSound wave table synth.
5 */
6 /*
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
16 #include <linux/config.h>
19 #define GUSPNP_AUTODETECT
21 #include "sound_config.h"
22 #include <linux/ultrasound.h>
23 #include "gus_hw.h"
25 #if defined(CONFIG_GUSHW) || defined(MODULE)
27 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
29 #define MAX_SAMPLE 150
30 #define MAX_PATCH 256
32 #define NOT_SAMPLE 0xffff
34 struct voice_info
36 unsigned long orig_freq;
37 unsigned long current_freq;
38 unsigned long mode;
39 int fixed_pitch;
40 int bender;
41 int bender_range;
42 int panning;
43 int midi_volume;
44 unsigned int initial_volume;
45 unsigned int current_volume;
46 int loop_irq_mode, loop_irq_parm;
47 #define LMODE_FINISH 1
48 #define LMODE_PCM 2
49 #define LMODE_PCM_STOP 3
50 int volume_irq_mode, volume_irq_parm;
51 #define VMODE_HALT 1
52 #define VMODE_ENVELOPE 2
53 #define VMODE_START_NOTE 3
55 int env_phase;
56 unsigned char env_rate[6];
57 unsigned char env_offset[6];
60 * Volume computation parameters for gus_adagio_vol()
62 int main_vol, expression_vol, patch_vol;
64 /* Variables for "Ultraclick" removal */
65 int dev_pending, note_pending, volume_pending,
66 sample_pending;
67 char kill_pending;
68 long offset_pending;
72 static struct voice_alloc_info *voice_alloc;
74 extern int gus_base;
75 extern int gus_irq, gus_dma;
76 extern int gus_pnp_flag;
77 static int gus_dma2 = -1;
78 static int dual_dma_mode = 0;
79 static long gus_mem_size = 0;
80 static long free_mem_ptr = 0;
81 static int gus_busy = 0;
82 static int gus_no_dma = 0;
83 static int nr_voices = 0;
84 static int gus_devnum = 0;
85 static int volume_base, volume_scale, volume_method;
86 static int gus_recmask = SOUND_MASK_MIC;
87 static int recording_active = 0;
88 static int only_read_access = 0;
89 static int only_8_bits = 0;
91 int iw_mode = 0;
92 int gus_wave_volume = 60;
93 int gus_pcm_volume = 80;
94 int have_gus_max = 0;
95 static int gus_line_vol = 100, gus_mic_vol = 0;
96 static unsigned char mix_image = 0x00;
98 int gus_timer_enabled = 0;
101 * Current version of this driver doesn't allow synth and PCM functions
102 * at the same time. The active_device specifies the active driver
105 static int active_device = 0;
107 #define GUS_DEV_WAVE 1 /* Wave table synth */
108 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
109 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
111 static int gus_audio_speed;
112 static int gus_audio_channels;
113 static int gus_audio_bits;
114 static int gus_audio_bsize;
115 static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
117 static struct wait_queue *dram_sleeper = NULL;
118 static volatile struct snd_wait dram_sleep_flag = {
123 * Variables and buffers for PCM output
126 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
128 static int pcm_bsize, pcm_nblk, pcm_banksize;
129 static int pcm_datasize[MAX_PCM_BUFFERS];
130 static volatile int pcm_head, pcm_tail, pcm_qlen;
131 static volatile int pcm_active;
132 static volatile int dma_active;
133 static int pcm_opened = 0;
134 static int pcm_current_dev;
135 static int pcm_current_block;
136 static unsigned long pcm_current_buf;
137 static int pcm_current_count;
138 static int pcm_current_intrflag;
140 extern int *gus_osp;
142 static struct voice_info voices[32];
144 static int freq_div_table[] =
146 44100,
147 44100, /* 14 */
148 41160, /* 15 */
149 38587, /* 16 */
150 36317, /* 17 */
151 34300, /* 18 */
152 32494, /* 19 */
153 30870, /* 20 */
154 29400, /* 21 */
155 28063, /* 22 */
156 26843, /* 23 */
157 25725, /* 24 */
158 24696, /* 25 */
159 23746, /* 26 */
160 22866, /* 27 */
161 22050, /* 28 */
162 21289, /* 29 */
163 20580, /* 30 */
164 19916, /* 31 */
165 19293 /* 32 */
168 static struct patch_info *samples = NULL;
169 static long sample_ptrs[MAX_SAMPLE + 1];
170 static int sample_map[32];
171 static int free_sample;
172 static int mixer_type = 0;
175 static int patch_table[MAX_PATCH];
176 static int patch_map[32];
178 static struct synth_info gus_info = {
179 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
180 0, 16, 0, MAX_PATCH
183 static void gus_poke(long addr, unsigned char data);
184 static void compute_and_set_volume(int voice, int volume, int ramp_time);
185 extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
186 extern unsigned short gus_linear_vol(int vol, int mainvol);
187 static void compute_volume(int voice, int volume);
188 static void do_volume_irq(int voice);
189 static void set_input_volumes(void);
190 static void gus_tmr_install(int io_base);
192 #define INSTANT_RAMP -1 /* Instant change. No ramping */
193 #define FAST_RAMP 0 /* Fastest possible ramp */
195 static void reset_sample_memory(void)
197 int i;
199 for (i = 0; i <= MAX_SAMPLE; i++)
200 sample_ptrs[i] = -1;
201 for (i = 0; i < 32; i++)
202 sample_map[i] = -1;
203 for (i = 0; i < 32; i++)
204 patch_map[i] = -1;
206 gus_poke(0, 0); /* Put a silent sample to the beginning */
207 gus_poke(1, 0);
208 free_mem_ptr = 2;
210 free_sample = 0;
212 for (i = 0; i < MAX_PATCH; i++)
213 patch_table[i] = NOT_SAMPLE;
216 void gus_delay(void)
218 int i;
220 for (i = 0; i < 7; i++)
221 inb(u_DRAMIO);
224 static void gus_poke(long addr, unsigned char data)
225 { /* Writes a byte to the DRAM */
226 unsigned long flags;
228 save_flags(flags);
229 cli();
230 outb((0x43), u_Command);
231 outb((addr & 0xff), u_DataLo);
232 outb(((addr >> 8) & 0xff), u_DataHi);
234 outb((0x44), u_Command);
235 outb(((addr >> 16) & 0xff), u_DataHi);
236 outb((data), u_DRAMIO);
237 restore_flags(flags);
240 static unsigned char gus_peek(long addr)
241 { /* Reads a byte from the DRAM */
242 unsigned long flags;
243 unsigned char tmp;
245 save_flags(flags);
246 cli();
247 outb((0x43), u_Command);
248 outb((addr & 0xff), u_DataLo);
249 outb(((addr >> 8) & 0xff), u_DataHi);
251 outb((0x44), u_Command);
252 outb(((addr >> 16) & 0xff), u_DataHi);
253 tmp = inb(u_DRAMIO);
254 restore_flags(flags);
256 return tmp;
259 void gus_write8(int reg, unsigned int data)
260 { /* Writes to an indirect register (8 bit) */
261 unsigned long flags;
263 save_flags(flags);
264 cli();
266 outb((reg), u_Command);
267 outb(((unsigned char) (data & 0xff)), u_DataHi);
269 restore_flags(flags);
272 static unsigned char gus_read8(int reg)
274 /* Reads from an indirect register (8 bit). Offset 0x80. */
275 unsigned long flags;
276 unsigned char val;
278 save_flags(flags);
279 cli();
280 outb((reg | 0x80), u_Command);
281 val = inb(u_DataHi);
282 restore_flags(flags);
284 return val;
287 static unsigned char gus_look8(int reg)
289 /* Reads from an indirect register (8 bit). No additional offset. */
290 unsigned long flags;
291 unsigned char val;
293 save_flags(flags);
294 cli();
295 outb((reg), u_Command);
296 val = inb(u_DataHi);
297 restore_flags(flags);
299 return val;
302 static void gus_write16(int reg, unsigned int data)
304 /* Writes to an indirect register (16 bit) */
305 unsigned long flags;
307 save_flags(flags);
308 cli();
310 outb((reg), u_Command);
312 outb(((unsigned char) (data & 0xff)), u_DataLo);
313 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
315 restore_flags(flags);
318 static unsigned short gus_read16(int reg)
320 /* Reads from an indirect register (16 bit). Offset 0x80. */
321 unsigned long flags;
322 unsigned char hi, lo;
324 save_flags(flags);
325 cli();
327 outb((reg | 0x80), u_Command);
329 lo = inb(u_DataLo);
330 hi = inb(u_DataHi);
332 restore_flags(flags);
334 return ((hi << 8) & 0xff00) | lo;
337 static unsigned short gus_look16(int reg)
339 /* Reads from an indirect register (16 bit). No additional offset. */
340 unsigned long flags;
341 unsigned char hi, lo;
343 save_flags(flags);
344 cli();
346 outb((reg), u_Command);
348 lo = inb(u_DataLo);
349 hi = inb(u_DataHi);
351 restore_flags(flags);
353 return ((hi << 8) & 0xff00) | lo;
356 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
358 /* Writes an 24 bit memory address */
359 unsigned long hold_address;
360 unsigned long flags;
362 save_flags(flags);
363 cli();
364 if (is16bit)
366 if (iw_mode)
368 /* Interwave spesific address translations */
369 address >>= 1;
371 else
374 * Special processing required for 16 bit patches
377 hold_address = address;
378 address = address >> 1;
379 address &= 0x0001ffffL;
380 address |= (hold_address & 0x000c0000L);
383 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
384 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
385 + (frac << 5));
386 /* Could writing twice fix problems with GUS_VOICE_POS() ? Lets try... */
387 gus_delay();
388 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
389 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
390 + (frac << 5));
391 restore_flags(flags);
394 static void gus_select_voice(int voice)
396 if (voice < 0 || voice > 31)
397 return;
398 outb((voice), u_Voice);
401 static void gus_select_max_voices(int nvoices)
403 if (iw_mode)
404 nvoices = 32;
405 if (nvoices < 14)
406 nvoices = 14;
407 if (nvoices > 32)
408 nvoices = 32;
410 voice_alloc->max_voice = nr_voices = nvoices;
411 gus_write8(0x0e, (nvoices - 1) | 0xc0);
414 static void gus_voice_on(unsigned int mode)
416 gus_write8(0x00, (unsigned char) (mode & 0xfc));
417 gus_delay();
418 gus_write8(0x00, (unsigned char) (mode & 0xfc));
421 static void gus_voice_off(void)
423 gus_write8(0x00, gus_read8(0x00) | 0x03);
426 static void gus_voice_mode(unsigned int m)
428 unsigned char mode = (unsigned char) (m & 0xff);
430 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
431 (mode & 0xfc)); /* Don't touch last two bits */
432 gus_delay();
433 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
436 static void gus_voice_freq(unsigned long freq)
438 unsigned long divisor = freq_div_table[nr_voices - 14];
439 unsigned short fc;
441 /* Interwave plays at 44100 Hz with any number of voices */
442 if (iw_mode)
443 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
444 else
445 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
446 fc = fc << 1;
448 gus_write16(0x01, fc);
451 static void gus_voice_volume(unsigned int vol)
453 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
454 gus_write16(0x09, (unsigned short) (vol << 4));
457 static void gus_voice_balance(unsigned int balance)
459 gus_write8(0x0c, (unsigned char) (balance & 0xff));
462 static void gus_ramp_range(unsigned int low, unsigned int high)
464 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
465 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
468 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
470 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
473 static void gus_rampon(unsigned int m)
475 unsigned char mode = (unsigned char) (m & 0xff);
477 gus_write8(0x0d, mode & 0xfc);
478 gus_delay();
479 gus_write8(0x0d, mode & 0xfc);
482 static void gus_ramp_mode(unsigned int m)
484 unsigned char mode = (unsigned char) (m & 0xff);
486 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
487 (mode & 0xfc)); /* Leave the last 2 bits alone */
488 gus_delay();
489 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
492 static void gus_rampoff(void)
494 gus_write8(0x0d, 0x03);
497 static void gus_set_voice_pos(int voice, long position)
499 int sample_no;
501 if ((sample_no = sample_map[voice]) != -1)
502 if (position < samples[sample_no].len)
503 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
504 voices[voice].offset_pending = position;
505 else
506 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
507 samples[sample_no].mode & WAVE_16_BITS);
510 static void gus_voice_init(int voice)
512 unsigned long flags;
514 save_flags(flags);
515 cli();
516 gus_select_voice(voice);
517 gus_voice_volume(0);
518 gus_voice_off();
519 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
520 gus_write8(0x00, 0x03); /* Voice off */
521 gus_write8(0x0d, 0x03); /* Ramping off */
522 voice_alloc->map[voice] = 0;
523 voice_alloc->alloc_times[voice] = 0;
524 restore_flags(flags);
528 static void gus_voice_init2(int voice)
530 voices[voice].panning = 0;
531 voices[voice].mode = 0;
532 voices[voice].orig_freq = 20000;
533 voices[voice].current_freq = 20000;
534 voices[voice].bender = 0;
535 voices[voice].bender_range = 200;
536 voices[voice].initial_volume = 0;
537 voices[voice].current_volume = 0;
538 voices[voice].loop_irq_mode = 0;
539 voices[voice].loop_irq_parm = 0;
540 voices[voice].volume_irq_mode = 0;
541 voices[voice].volume_irq_parm = 0;
542 voices[voice].env_phase = 0;
543 voices[voice].main_vol = 127;
544 voices[voice].patch_vol = 127;
545 voices[voice].expression_vol = 127;
546 voices[voice].sample_pending = -1;
547 voices[voice].fixed_pitch = 0;
550 static void step_envelope(int voice)
552 unsigned vol, prev_vol, phase;
553 unsigned char rate;
554 long int flags;
556 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
558 save_flags(flags);
559 cli();
560 gus_select_voice(voice);
561 gus_rampoff();
562 restore_flags(flags);
563 return;
565 * Sustain phase begins. Continue envelope after receiving note off.
568 if (voices[voice].env_phase >= 5)
570 /* Envelope finished. Shoot the voice down */
571 gus_voice_init(voice);
572 return;
574 prev_vol = voices[voice].current_volume;
575 phase = ++voices[voice].env_phase;
576 compute_volume(voice, voices[voice].midi_volume);
577 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
578 rate = voices[voice].env_rate[phase];
580 save_flags(flags);
581 cli();
582 gus_select_voice(voice);
584 gus_voice_volume(prev_vol);
587 gus_write8(0x06, rate); /* Ramping rate */
589 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
591 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
593 restore_flags(flags);
594 step_envelope(voice); /* Continue the envelope on the next step */
595 return;
597 if (vol > prev_vol)
599 if (vol >= (4096 - 64))
600 vol = 4096 - 65;
601 gus_ramp_range(0, vol);
602 gus_rampon(0x20); /* Increasing volume, with IRQ */
604 else
606 if (vol <= 64)
607 vol = 65;
608 gus_ramp_range(vol, 4030);
609 gus_rampon(0x60); /* Decreasing volume, with IRQ */
611 voices[voice].current_volume = vol;
612 restore_flags(flags);
615 static void init_envelope(int voice)
617 voices[voice].env_phase = -1;
618 voices[voice].current_volume = 64;
620 step_envelope(voice);
623 static void start_release(int voice, long int flags)
625 if (gus_read8(0x00) & 0x03)
626 return; /* Voice already stopped */
628 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
630 voices[voice].current_volume = voices[voice].initial_volume =
631 gus_read16(0x09) >> 4; /* Get current volume */
633 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
634 gus_rampoff();
635 restore_flags(flags);
636 step_envelope(voice);
639 static void gus_voice_fade(int voice)
641 int instr_no = sample_map[voice], is16bits;
642 long int flags;
644 save_flags(flags);
645 cli();
646 gus_select_voice(voice);
648 if (instr_no < 0 || instr_no > MAX_SAMPLE)
650 gus_write8(0x00, 0x03); /* Hard stop */
651 voice_alloc->map[voice] = 0;
652 restore_flags(flags);
653 return;
655 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
657 if (voices[voice].mode & WAVE_ENVELOPES)
659 start_release(voice, flags);
660 restore_flags(flags);
661 return;
664 * Ramp the volume down but not too quickly.
666 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
668 gus_voice_off();
669 gus_rampoff();
670 gus_voice_init(voice);
671 restore_flags(flags);
672 return;
674 gus_ramp_range(65, 4030);
675 gus_ramp_rate(2, 4);
676 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
677 voices[voice].volume_irq_mode = VMODE_HALT;
678 restore_flags(flags);
681 static void gus_reset(void)
683 int i;
685 gus_select_max_voices(24);
686 volume_base = 3071;
687 volume_scale = 4;
688 volume_method = VOL_METHOD_ADAGIO;
690 for (i = 0; i < 32; i++)
692 gus_voice_init(i); /* Turn voice off */
693 gus_voice_init2(i);
697 static void gus_initialize(void)
699 unsigned long flags;
700 unsigned char dma_image, irq_image, tmp;
702 static unsigned char gus_irq_map[16] = {
703 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
706 static unsigned char gus_dma_map[8] = {
707 0, 1, 0, 2, 0, 3, 4, 5
710 save_flags(flags);
711 cli();
712 gus_write8(0x4c, 0); /* Reset GF1 */
713 gus_delay();
714 gus_delay();
716 gus_write8(0x4c, 1); /* Release Reset */
717 gus_delay();
718 gus_delay();
721 * Clear all interrupts
724 gus_write8(0x41, 0); /* DMA control */
725 gus_write8(0x45, 0); /* Timer control */
726 gus_write8(0x49, 0); /* Sample control */
728 gus_select_max_voices(24);
730 inb(u_Status); /* Touch the status register */
732 gus_look8(0x41); /* Clear any pending DMA IRQs */
733 gus_look8(0x49); /* Clear any pending sample IRQs */
734 gus_read8(0x0f); /* Clear pending IRQs */
736 gus_reset(); /* Resets all voices */
738 gus_look8(0x41); /* Clear any pending DMA IRQs */
739 gus_look8(0x49); /* Clear any pending sample IRQs */
740 gus_read8(0x0f); /* Clear pending IRQs */
742 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
745 * Set up for Digital ASIC
748 outb((0x05), gus_base + 0x0f);
750 mix_image |= 0x02; /* Disable line out (for a moment) */
751 outb((mix_image), u_Mixer);
753 outb((0x00), u_IRQDMAControl);
755 outb((0x00), gus_base + 0x0f);
758 * Now set up the DMA and IRQ interface
760 * The GUS supports two IRQs and two DMAs.
762 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
763 * Adding this support requires significant changes to the dmabuf.c, dsp.c
764 * and audio.c also.
767 irq_image = 0;
768 tmp = gus_irq_map[gus_irq];
769 if (!gus_pnp_flag && !tmp)
770 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
771 irq_image |= tmp;
772 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
774 dual_dma_mode = 1;
775 if (gus_dma2 == gus_dma || gus_dma2 == -1)
777 dual_dma_mode = 0;
778 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
780 tmp = gus_dma_map[gus_dma];
781 if (!tmp)
782 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
784 dma_image |= tmp;
786 else
788 /* Setup dual DMA channel mode for GUS MAX */
790 dma_image = gus_dma_map[gus_dma];
791 if (!dma_image)
792 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
794 tmp = gus_dma_map[gus_dma2] << 3;
795 if (!tmp)
797 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
798 tmp = 0x40; /* Combine DMA channels */
799 dual_dma_mode = 0;
801 dma_image |= tmp;
805 * For some reason the IRQ and DMA addresses must be written twice
809 * Doing it first time
812 outb((mix_image), u_Mixer); /* Select DMA control */
813 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
815 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
816 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
819 * Doing it second time
822 outb((mix_image), u_Mixer); /* Select DMA control */
823 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
825 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
826 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
828 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
830 mix_image &= ~0x02; /* Enable line out */
831 mix_image |= 0x08; /* Enable IRQ */
832 outb((mix_image), u_Mixer); /*
833 * Turn mixer channels on
834 * Note! Mic in is left off.
837 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
839 gusintr(gus_irq, NULL, NULL); /* Serve pending interrupts */
841 inb(u_Status); /* Touch the status register */
843 gus_look8(0x41); /* Clear any pending DMA IRQs */
844 gus_look8(0x49); /* Clear any pending sample IRQs */
846 gus_read8(0x0f); /* Clear pending IRQs */
848 if (iw_mode)
849 gus_write8(0x19, gus_read8(0x19) | 0x01);
850 restore_flags(flags);
854 static void pnp_mem_init(void)
856 #include "iwmem.h"
857 #define CHUNK_SIZE (256*1024)
858 #define BANK_SIZE (4*1024*1024)
859 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
861 int bank, chunk, addr, total = 0;
862 int bank_sizes[4];
863 int i, j, bits = -1, nbanks = 0;
866 * This routine determines what kind of RAM is installed in each of the four
867 * SIMM banks and configures the DRAM address decode logic accordingly.
871 * Place the chip into enhanced mode
873 gus_write8(0x19, gus_read8(0x19) | 0x01);
874 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
877 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
880 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
883 * Perform the DRAM size detection for each bank individually.
885 for (bank = 0; bank < 4; bank++)
887 int size = 0;
889 addr = bank * BANK_SIZE;
891 /* Clean check points of each chunk */
892 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
894 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
895 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
898 /* Write a value to each chunk point and verify the result */
899 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
901 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
902 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
904 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
905 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
907 /* OK. There is RAM. Now check for possible shadows */
908 int ok = 1, chunk2;
910 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
911 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
912 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
913 ok = 0; /* Addressing wraps */
915 if (ok)
916 size = (chunk + 1) * CHUNK_SIZE;
918 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
919 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
921 bank_sizes[bank] = size;
922 if (size)
923 nbanks = bank + 1;
924 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
927 if (nbanks == 0) /* No RAM - Give up */
929 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
930 printk(KERN_ERR "Sound: Unable to work with this card.\n");
931 gus_write8(0x19, gus_read8(0x19) & ~0x01);
932 gus_mem_size = 0;
933 return;
937 * Now we know how much DRAM there is in each bank. The next step is
938 * to find a DRAM size encoding (0 to 12) which is best for the combination
939 * we have.
941 * First try if any of the possible alternatives matches exactly the amount
942 * of memory we have.
945 for (i = 0; bits == -1 && i < 13; i++)
947 bits = i;
949 for (j = 0; bits != -1 && j < 4; j++)
950 if (mem_decode[i][j] != bank_sizes[j])
951 bits = -1; /* No hit */
955 * If necessary, try to find a combination where other than the last
956 * bank matches our configuration and the last bank is left oversized.
957 * In this way we don't leave holes in the middle of memory.
960 if (bits == -1) /* No luck yet */
962 for (i = 0; bits == -1 && i < 13; i++)
964 bits = i;
966 for (j = 0; bits != -1 && j < nbanks - 1; j++)
967 if (mem_decode[i][j] != bank_sizes[j])
968 bits = -1; /* No hit */
969 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
970 bits = -1; /* The last bank is too small */
974 * The last resort is to search for a combination where the last bank is
975 * smaller than the actual SIMM. This leaves some memory in the last bank
976 * unused but doesn't leave holes in the DRAM address space.
978 if (bits == -1) /* No luck yet */
980 for (i = 0; bits == -1 && i < 13; i++)
982 bits = i;
984 for (j = 0; bits != -1 && j < nbanks - 1; j++)
985 if (mem_decode[i][j] != bank_sizes[j])
986 bits = -1; /* No hit */
988 if (bits != -1)
990 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
991 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
994 if (bits == -1)
996 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
997 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
998 bits = 0;
1000 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
1002 for (bank = 0; bank < 4; bank++)
1004 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
1006 if (bank_sizes[bank] > mem_decode[bits][bank])
1007 total += mem_decode[bits][bank];
1008 else
1009 total += bank_sizes[bank];
1012 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
1015 * Set the memory addressing mode.
1017 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
1019 /* Leave the chip into enhanced mode. Disable LFO */
1020 gus_mem_size = total;
1021 iw_mode = 1;
1022 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1025 int gus_wave_detect(int baseaddr)
1027 unsigned long i, max_mem = 1024L;
1028 unsigned long loc;
1029 unsigned char val;
1031 gus_base = baseaddr;
1033 gus_write8(0x4c, 0); /* Reset GF1 */
1034 gus_delay();
1035 gus_delay();
1037 gus_write8(0x4c, 1); /* Release Reset */
1038 gus_delay();
1039 gus_delay();
1041 #ifdef GUSPNP_AUTODETECT
1042 val = gus_look8(0x5b); /* Version number register */
1043 gus_write8(0x5b, ~val); /* Invert all bits */
1045 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
1047 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
1049 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1050 gus_pnp_flag = 1;
1052 else
1054 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1055 gus_pnp_flag = 0;
1058 gus_write8(0x5b, val); /* Restore all bits */
1059 #endif
1061 if (gus_pnp_flag)
1062 pnp_mem_init();
1063 if (iw_mode)
1064 return 1;
1066 /* See if there is first block there.... */
1067 gus_poke(0L, 0xaa);
1068 if (gus_peek(0L) != 0xaa)
1069 return (0);
1071 /* Now zero it out so that I can check for mirroring .. */
1072 gus_poke(0L, 0x00);
1073 for (i = 1L; i < max_mem; i++)
1075 int n, failed;
1077 /* check for mirroring ... */
1078 if (gus_peek(0L) != 0)
1079 break;
1080 loc = i << 10;
1082 for (n = loc - 1, failed = 0; n <= loc; n++)
1084 gus_poke(loc, 0xaa);
1085 if (gus_peek(loc) != 0xaa)
1086 failed = 1;
1087 gus_poke(loc, 0x55);
1088 if (gus_peek(loc) != 0x55)
1089 failed = 1;
1091 if (failed)
1092 break;
1094 gus_mem_size = i << 10;
1095 return 1;
1098 static int guswave_ioctl(int dev, unsigned int cmd, caddr_t arg)
1101 switch (cmd)
1103 case SNDCTL_SYNTH_INFO:
1104 gus_info.nr_voices = nr_voices;
1105 return copy_to_user(arg, &gus_info, sizeof(gus_info));
1107 case SNDCTL_SEQ_RESETSAMPLES:
1108 reset_sample_memory();
1109 return 0;
1111 case SNDCTL_SEQ_PERCMODE:
1112 return 0;
1114 case SNDCTL_SYNTH_MEMAVL:
1115 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1117 default:
1118 return -EINVAL;
1122 static int guswave_set_instr(int dev, int voice, int instr_no)
1124 int sample_no;
1126 if (instr_no < 0 || instr_no > MAX_PATCH)
1127 instr_no = 0; /* Default to acoustic piano */
1129 if (voice < 0 || voice > 31)
1130 return -EINVAL;
1132 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1134 voices[voice].sample_pending = instr_no;
1135 return 0;
1137 sample_no = patch_table[instr_no];
1138 patch_map[voice] = -1;
1140 if (sample_no == NOT_SAMPLE)
1142 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1143 return -EINVAL; /* Patch not defined */
1145 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1147 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1148 return -EINVAL;
1150 sample_map[voice] = sample_no;
1151 patch_map[voice] = instr_no;
1152 return 0;
1155 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1157 unsigned long flags;
1159 save_flags(flags);
1160 cli();
1161 /* voice_alloc->map[voice] = 0xffff; */
1162 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1164 voices[voice].kill_pending = 1;
1165 restore_flags(flags);
1167 else
1169 restore_flags(flags);
1170 gus_voice_fade(voice);
1173 restore_flags(flags);
1174 return 0;
1177 static void guswave_aftertouch(int dev, int voice, int pressure)
1181 static void guswave_panning(int dev, int voice, int value)
1183 if (voice >= 0 || voice < 32)
1184 voices[voice].panning = value;
1187 static void guswave_volume_method(int dev, int mode)
1189 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1190 volume_method = mode;
1193 static void compute_volume(int voice, int volume)
1195 if (volume < 128)
1196 voices[voice].midi_volume = volume;
1198 switch (volume_method)
1200 case VOL_METHOD_ADAGIO:
1201 voices[voice].initial_volume =
1202 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1203 voices[voice].expression_vol,
1204 voices[voice].patch_vol);
1205 break;
1207 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1208 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1209 break;
1211 default:
1212 voices[voice].initial_volume = volume_base +
1213 (voices[voice].midi_volume * volume_scale);
1216 if (voices[voice].initial_volume > 4030)
1217 voices[voice].initial_volume = 4030;
1220 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1222 int curr, target, rate;
1223 unsigned long flags;
1225 compute_volume(voice, volume);
1226 voices[voice].current_volume = voices[voice].initial_volume;
1228 save_flags(flags);
1229 cli();
1231 * CAUTION! Interrupts disabled. Enable them before returning
1234 gus_select_voice(voice);
1236 curr = gus_read16(0x09) >> 4;
1237 target = voices[voice].initial_volume;
1239 if (ramp_time == INSTANT_RAMP)
1241 gus_rampoff();
1242 gus_voice_volume(target);
1243 restore_flags(flags);
1244 return;
1246 if (ramp_time == FAST_RAMP)
1247 rate = 63;
1248 else
1249 rate = 16;
1250 gus_ramp_rate(0, rate);
1252 if ((target - curr) / 64 == 0) /* Close enough to target. */
1254 gus_rampoff();
1255 gus_voice_volume(target);
1256 restore_flags(flags);
1257 return;
1259 if (target > curr)
1261 if (target > (4095 - 65))
1262 target = 4095 - 65;
1263 gus_ramp_range(curr, target);
1264 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1266 else
1268 if (target < 65)
1269 target = 65;
1271 gus_ramp_range(target, curr);
1272 gus_rampon(0x40); /* Ramp down, once, no irq */
1274 restore_flags(flags);
1277 static void dynamic_volume_change(int voice)
1279 unsigned char status;
1280 unsigned long flags;
1282 save_flags(flags);
1283 cli();
1284 gus_select_voice(voice);
1285 status = gus_read8(0x00); /* Get voice status */
1286 restore_flags(flags);
1288 if (status & 0x03)
1289 return; /* Voice was not running */
1291 if (!(voices[voice].mode & WAVE_ENVELOPES))
1293 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1294 return;
1298 * Voice is running and has envelopes.
1301 save_flags(flags);
1302 cli();
1303 gus_select_voice(voice);
1304 status = gus_read8(0x0d); /* Ramping status */
1305 restore_flags(flags);
1307 if (status & 0x03) /* Sustain phase? */
1309 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1310 return;
1312 if (voices[voice].env_phase < 0)
1313 return;
1315 compute_volume(voice, voices[voice].midi_volume);
1319 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1321 unsigned long flags;
1322 unsigned long freq;
1324 if (voice < 0 || voice > 31)
1325 return;
1327 switch (ctrl_num)
1329 case CTRL_PITCH_BENDER:
1330 voices[voice].bender = value;
1332 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1334 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1335 voices[voice].current_freq = freq;
1337 save_flags(flags);
1338 cli();
1339 gus_select_voice(voice);
1340 gus_voice_freq(freq);
1341 restore_flags(flags);
1343 break;
1345 case CTRL_PITCH_BENDER_RANGE:
1346 voices[voice].bender_range = value;
1347 break;
1348 case CTL_EXPRESSION:
1349 value /= 128;
1350 case CTRL_EXPRESSION:
1351 if (volume_method == VOL_METHOD_ADAGIO)
1353 voices[voice].expression_vol = value;
1354 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1355 dynamic_volume_change(voice);
1357 break;
1359 case CTL_PAN:
1360 voices[voice].panning = (value * 2) - 128;
1361 break;
1363 case CTL_MAIN_VOLUME:
1364 value = (value * 100) / 16383;
1366 case CTRL_MAIN_VOLUME:
1367 voices[voice].main_vol = value;
1368 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1369 dynamic_volume_change(voice);
1370 break;
1372 default:
1373 break;
1377 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1379 int sample, best_sample, best_delta, delta_freq;
1380 int is16bits, samplep, patch, pan;
1381 unsigned long note_freq, base_note, freq, flags;
1382 unsigned char mode = 0;
1384 if (voice < 0 || voice > 31)
1386 /* printk("GUS: Invalid voice\n");*/
1387 return -EINVAL;
1389 if (note_num == 255)
1391 if (voices[voice].mode & WAVE_ENVELOPES)
1393 voices[voice].midi_volume = volume;
1394 dynamic_volume_change(voice);
1395 return 0;
1397 compute_and_set_volume(voice, volume, 1);
1398 return 0;
1400 if ((patch = patch_map[voice]) == -1)
1401 return -EINVAL;
1402 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1404 return -EINVAL;
1406 note_freq = note_to_freq(note_num);
1409 * Find a sample within a patch so that the note_freq is between low_note
1410 * and high_note.
1412 sample = -1;
1414 best_sample = samplep;
1415 best_delta = 1000000;
1416 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1418 delta_freq = note_freq - samples[samplep].base_note;
1419 if (delta_freq < 0)
1420 delta_freq = -delta_freq;
1421 if (delta_freq < best_delta)
1423 best_sample = samplep;
1424 best_delta = delta_freq;
1426 if (samples[samplep].low_note <= note_freq &&
1427 note_freq <= samples[samplep].high_note)
1429 sample = samplep;
1431 else
1432 samplep = samples[samplep].key; /* Link to next sample */
1434 if (sample == -1)
1435 sample = best_sample;
1437 if (sample == -1)
1439 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1440 return 0; /* Should play default patch ??? */
1442 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1443 voices[voice].mode = samples[sample].mode;
1444 voices[voice].patch_vol = samples[sample].volume;
1446 if (iw_mode)
1447 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1449 if (voices[voice].mode & WAVE_ENVELOPES)
1451 int i;
1453 for (i = 0; i < 6; i++)
1455 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1456 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1459 sample_map[voice] = sample;
1461 if (voices[voice].fixed_pitch) /* Fixed pitch */
1463 freq = samples[sample].base_freq;
1465 else
1467 base_note = samples[sample].base_note / 100;
1468 note_freq /= 100;
1470 freq = samples[sample].base_freq * note_freq / base_note;
1473 voices[voice].orig_freq = freq;
1476 * Since the pitch bender may have been set before playing the note, we
1477 * have to calculate the bending now.
1480 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1481 voices[voice].bender_range, 0);
1482 voices[voice].current_freq = freq;
1484 pan = (samples[sample].panning + voices[voice].panning) / 32;
1485 pan += 7;
1486 if (pan < 0)
1487 pan = 0;
1488 if (pan > 15)
1489 pan = 15;
1491 if (samples[sample].mode & WAVE_16_BITS)
1493 mode |= 0x04; /* 16 bits */
1494 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1495 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1496 printk(KERN_ERR "GUS: Sample address error\n");
1498 /*************************************************************************
1499 * CAUTION! Interrupts disabled. Don't return before enabling
1500 *************************************************************************/
1502 save_flags(flags);
1503 cli();
1504 gus_select_voice(voice);
1505 gus_voice_off();
1506 gus_rampoff();
1508 restore_flags(flags);
1510 if (voices[voice].mode & WAVE_ENVELOPES)
1512 compute_volume(voice, volume);
1513 init_envelope(voice);
1515 else
1517 compute_and_set_volume(voice, volume, 0);
1520 save_flags(flags);
1521 cli();
1522 gus_select_voice(voice);
1524 if (samples[sample].mode & WAVE_LOOP_BACK)
1525 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1526 voices[voice].offset_pending, 0, is16bits); /* start=end */
1527 else
1528 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1530 if (samples[sample].mode & WAVE_LOOPING)
1532 mode |= 0x08;
1534 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1535 mode |= 0x10;
1537 if (samples[sample].mode & WAVE_LOOP_BACK)
1539 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1540 voices[voice].offset_pending,
1541 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1542 mode |= 0x40;
1544 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1545 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1546 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1547 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1549 else
1551 mode |= 0x20; /* Loop IRQ at the end */
1552 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1553 voices[voice].loop_irq_parm = 1;
1554 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1555 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1556 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1558 gus_voice_freq(freq);
1559 gus_voice_balance(pan);
1560 gus_voice_on(mode);
1561 restore_flags(flags);
1563 return 0;
1567 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1568 * when the note playing on the voice is changed. It uses volume
1569 * ramping.
1572 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1574 long int flags;
1575 int mode;
1576 int ret_val = 0;
1578 save_flags(flags);
1579 cli();
1580 if (note_num == 255)
1582 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1584 voices[voice].volume_pending = volume;
1586 else
1588 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1591 else
1593 gus_select_voice(voice);
1594 mode = gus_read8(0x00);
1595 if (mode & 0x20)
1596 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1598 voices[voice].offset_pending = 0;
1599 voices[voice].kill_pending = 0;
1600 voices[voice].volume_irq_mode = 0;
1601 voices[voice].loop_irq_mode = 0;
1603 if (voices[voice].sample_pending >= 0)
1605 restore_flags(flags); /* Run temporarily with interrupts enabled */
1606 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1607 voices[voice].sample_pending = -1;
1608 save_flags(flags);
1609 cli();
1610 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1612 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1614 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1616 else
1618 voices[voice].dev_pending = dev;
1619 voices[voice].note_pending = note_num;
1620 voices[voice].volume_pending = volume;
1621 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1623 gus_rampoff();
1624 gus_ramp_range(2000, 4065);
1625 gus_ramp_rate(0, 63); /* Fastest possible rate */
1626 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1629 restore_flags(flags);
1630 return ret_val;
1633 static void guswave_reset(int dev)
1635 int i;
1637 for (i = 0; i < 32; i++)
1639 gus_voice_init(i);
1640 gus_voice_init2(i);
1644 static int guswave_open(int dev, int mode)
1646 int err;
1648 if (gus_busy)
1649 return -EBUSY;
1651 voice_alloc->timestamp = 0;
1653 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1655 /* printk( "GUS: Loading samples without DMA\n"); */
1656 gus_no_dma = 1; /* Upload samples using PIO */
1658 else
1659 gus_no_dma = 0;
1661 dram_sleep_flag.opts = WK_NONE;
1662 gus_busy = 1;
1663 active_device = GUS_DEV_WAVE;
1665 gusintr(gus_irq, NULL, NULL); /* Serve pending interrupts */
1666 gus_initialize();
1667 gus_reset();
1668 gusintr(gus_irq, NULL, NULL); /* Serve pending interrupts */
1670 return 0;
1673 static void guswave_close(int dev)
1675 gus_busy = 0;
1676 active_device = 0;
1677 gus_reset();
1679 if (!gus_no_dma)
1680 DMAbuf_close_dma(gus_devnum);
1683 static int guswave_load_patch(int dev, int format, const char *addr,
1684 int offs, int count, int pmgr_flag)
1686 struct patch_info patch;
1687 int instr;
1688 long sizeof_patch;
1690 unsigned long blk_sz, blk_end, left, src_offs, target;
1692 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1694 if (format != GUS_PATCH)
1696 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1697 return -EINVAL;
1699 if (count < sizeof_patch)
1701 /* printk("GUS Error: Patch header too short\n");*/
1702 return -EINVAL;
1704 count -= sizeof_patch;
1706 if (free_sample >= MAX_SAMPLE)
1708 /* printk("GUS: Sample table full\n");*/
1709 return -ENOSPC;
1712 * Copy the header from user space but ignore the first bytes which have
1713 * been transferred already.
1716 copy_from_user(&((char *) &patch)[offs], &(addr)[offs], sizeof_patch - offs);
1718 if (patch.mode & WAVE_ROM)
1719 return -EINVAL;
1720 if (gus_mem_size == 0)
1721 return -ENOSPC;
1723 instr = patch.instr_no;
1725 if (instr < 0 || instr > MAX_PATCH)
1727 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1728 return -EINVAL;
1730 if (count < patch.len)
1732 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1733 patch.len = count;
1735 if (patch.len <= 0 || patch.len > gus_mem_size)
1737 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1738 return -EINVAL;
1740 if (patch.mode & WAVE_LOOPING)
1742 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1744 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1745 return -EINVAL;
1747 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1749 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1750 return -EINVAL;
1753 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1755 if (patch.mode & WAVE_16_BITS)
1758 * 16 bit samples must fit one 256k bank.
1760 if (patch.len >= GUS_BANK_SIZE)
1762 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1763 return -ENOSPC;
1765 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1766 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1768 unsigned long tmp_mem =
1769 /* Align to 256K */
1770 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1772 if ((tmp_mem + patch.len) > gus_mem_size)
1773 return -ENOSPC;
1775 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1778 if ((free_mem_ptr + patch.len) > gus_mem_size)
1779 return -ENOSPC;
1781 sample_ptrs[free_sample] = free_mem_ptr;
1784 * Tremolo is not possible with envelopes
1787 if (patch.mode & WAVE_ENVELOPES)
1788 patch.mode &= ~WAVE_TREMOLO;
1790 if (!(patch.mode & WAVE_FRACTIONS))
1792 patch.fractions = 0;
1794 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1797 * Link this_one sample to the list of samples for patch 'instr'.
1800 samples[free_sample].key = patch_table[instr];
1801 patch_table[instr] = free_sample;
1804 * Use DMA to transfer the wave data to the DRAM
1807 left = patch.len;
1808 src_offs = 0;
1809 target = free_mem_ptr;
1811 while (left) /* Not completely transferred yet */
1813 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1814 if (blk_sz > left)
1815 blk_sz = left;
1818 * DMA cannot cross bank (256k) boundaries. Check for that.
1821 blk_end = target + blk_sz;
1823 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1825 /* Split the block */
1826 blk_end &= ~(GUS_BANK_SIZE - 1);
1827 blk_sz = blk_end - target;
1829 if (gus_no_dma)
1832 * For some reason the DMA is not possible. We have to use PIO.
1834 long i;
1835 unsigned char data;
1837 for (i = 0; i < blk_sz; i++)
1839 get_user(*(unsigned char *) &data, (unsigned char *) &((addr)[sizeof_patch + i]));
1840 if (patch.mode & WAVE_UNSIGNED)
1841 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1842 data ^= 0x80; /* Convert to signed */
1843 gus_poke(target + i, data);
1846 else
1848 unsigned long address, hold_address;
1849 unsigned char dma_command;
1850 unsigned long flags;
1851 unsigned long tlimit;
1853 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1855 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1856 return -ENOSPC;
1859 * OK, move now. First in and then out.
1862 copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf, &(addr)[sizeof_patch + src_offs], blk_sz);
1864 save_flags(flags);
1865 cli();
1866 /******** INTERRUPTS DISABLED NOW ********/
1867 gus_write8(0x41, 0); /* Disable GF1 DMA */
1868 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1869 blk_sz, DMA_MODE_WRITE);
1872 * Set the DRAM address for the wave data
1875 if (iw_mode)
1877 /* Different address translation in enhanced mode */
1879 unsigned char hi;
1881 if (gus_dma > 4)
1882 address = target >> 1; /* Convert to 16 bit word address */
1883 else
1884 address = target;
1886 hi = (unsigned char) ((address >> 16) & 0xf0);
1887 hi += (unsigned char) (address & 0x0f);
1889 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1890 gus_write8(0x50, hi);
1892 else
1894 address = target;
1895 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1897 hold_address = address;
1898 address = address >> 1;
1899 address &= 0x0001ffffL;
1900 address |= (hold_address & 0x000c0000L);
1902 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1906 * Start the DMA transfer
1909 dma_command = 0x21; /* IRQ enable, DMA start */
1910 if (patch.mode & WAVE_UNSIGNED)
1911 dma_command |= 0x80; /* Invert MSB */
1912 if (patch.mode & WAVE_16_BITS)
1913 dma_command |= 0x40; /* 16 bit _DATA_ */
1914 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1915 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1917 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1920 * Sleep here until the DRAM DMA done interrupt is served
1922 active_device = GUS_DEV_WAVE;
1924 current->timeout = tlimit = jiffies + HZ;
1925 dram_sleep_flag.opts = WK_SLEEP;
1926 interruptible_sleep_on(&dram_sleeper);
1927 if (!(dram_sleep_flag.opts & WK_WAKEUP))
1929 if (jiffies >= tlimit)
1930 dram_sleep_flag.opts |= WK_TIMEOUT;
1932 dram_sleep_flag.opts &= ~WK_SLEEP;
1933 if ((dram_sleep_flag.opts & WK_TIMEOUT))
1934 printk(KERN_WARNING "GUS: DMA Transfer timed out\n");
1935 restore_flags(flags);
1939 * Now the next part
1942 left -= blk_sz;
1943 src_offs += blk_sz;
1944 target += blk_sz;
1946 gus_write8(0x41, 0); /* Stop DMA */
1949 free_mem_ptr += patch.len;
1950 free_sample++;
1951 return 0;
1954 static void guswave_hw_control(int dev, unsigned char *event_rec)
1956 int voice, cmd;
1957 unsigned short p1, p2;
1958 unsigned int plong;
1959 unsigned flags;
1961 cmd = event_rec[2];
1962 voice = event_rec[3];
1963 p1 = *(unsigned short *) &event_rec[4];
1964 p2 = *(unsigned short *) &event_rec[6];
1965 plong = *(unsigned int *) &event_rec[4];
1967 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1968 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1969 do_volume_irq(voice);
1971 switch (cmd)
1973 case _GUS_NUMVOICES:
1974 save_flags(flags);
1975 cli();
1976 gus_select_voice(voice);
1977 gus_select_max_voices(p1);
1978 restore_flags(flags);
1979 break;
1981 case _GUS_VOICESAMPLE:
1982 guswave_set_instr(dev, voice, p1);
1983 break;
1985 case _GUS_VOICEON:
1986 save_flags(flags);
1987 cli();
1988 gus_select_voice(voice);
1989 p1 &= ~0x20; /* Don't allow interrupts */
1990 gus_voice_on(p1);
1991 restore_flags(flags);
1992 break;
1994 case _GUS_VOICEOFF:
1995 save_flags(flags);
1996 cli();
1997 gus_select_voice(voice);
1998 gus_voice_off();
1999 restore_flags(flags);
2000 break;
2002 case _GUS_VOICEFADE:
2003 gus_voice_fade(voice);
2004 break;
2006 case _GUS_VOICEMODE:
2007 save_flags(flags);
2008 cli();
2009 gus_select_voice(voice);
2010 p1 &= ~0x20; /* Don't allow interrupts */
2011 gus_voice_mode(p1);
2012 restore_flags(flags);
2013 break;
2015 case _GUS_VOICEBALA:
2016 save_flags(flags);
2017 cli();
2018 gus_select_voice(voice);
2019 gus_voice_balance(p1);
2020 restore_flags(flags);
2021 break;
2023 case _GUS_VOICEFREQ:
2024 save_flags(flags);
2025 cli();
2026 gus_select_voice(voice);
2027 gus_voice_freq(plong);
2028 restore_flags(flags);
2029 break;
2031 case _GUS_VOICEVOL:
2032 save_flags(flags);
2033 cli();
2034 gus_select_voice(voice);
2035 gus_voice_volume(p1);
2036 restore_flags(flags);
2037 break;
2039 case _GUS_VOICEVOL2: /* Just update the software voice level */
2040 voices[voice].initial_volume = voices[voice].current_volume = p1;
2041 break;
2043 case _GUS_RAMPRANGE:
2044 if (voices[voice].mode & WAVE_ENVELOPES)
2045 break; /* NO-NO */
2046 save_flags(flags);
2047 cli();
2048 gus_select_voice(voice);
2049 gus_ramp_range(p1, p2);
2050 restore_flags(flags);
2051 break;
2053 case _GUS_RAMPRATE:
2054 if (voices[voice].mode & WAVE_ENVELOPES)
2055 break; /* NJET-NJET */
2056 save_flags(flags);
2057 cli();
2058 gus_select_voice(voice);
2059 gus_ramp_rate(p1, p2);
2060 restore_flags(flags);
2061 break;
2063 case _GUS_RAMPMODE:
2064 if (voices[voice].mode & WAVE_ENVELOPES)
2065 break; /* NO-NO */
2066 save_flags(flags);
2067 cli();
2068 gus_select_voice(voice);
2069 p1 &= ~0x20; /* Don't allow interrupts */
2070 gus_ramp_mode(p1);
2071 restore_flags(flags);
2072 break;
2074 case _GUS_RAMPON:
2075 if (voices[voice].mode & WAVE_ENVELOPES)
2076 break; /* EI-EI */
2077 save_flags(flags);
2078 cli();
2079 gus_select_voice(voice);
2080 p1 &= ~0x20; /* Don't allow interrupts */
2081 gus_rampon(p1);
2082 restore_flags(flags);
2083 break;
2085 case _GUS_RAMPOFF:
2086 if (voices[voice].mode & WAVE_ENVELOPES)
2087 break; /* NEJ-NEJ */
2088 save_flags(flags);
2089 cli();
2090 gus_select_voice(voice);
2091 gus_rampoff();
2092 restore_flags(flags);
2093 break;
2095 case _GUS_VOLUME_SCALE:
2096 volume_base = p1;
2097 volume_scale = p2;
2098 break;
2100 case _GUS_VOICE_POS:
2101 save_flags(flags);
2102 cli();
2103 gus_select_voice(voice);
2104 gus_set_voice_pos(voice, plong);
2105 restore_flags(flags);
2106 break;
2108 default:
2112 static int gus_audio_set_speed(int speed)
2114 if (speed <= 0)
2115 speed = gus_audio_speed;
2117 if (speed < 4000)
2118 speed = 4000;
2120 if (speed > 44100)
2121 speed = 44100;
2123 gus_audio_speed = speed;
2125 if (only_read_access)
2127 /* Compute nearest valid recording speed and return it */
2129 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2130 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2131 speed = (9878400 / (speed * 16)) - 2;
2133 return speed;
2136 static int gus_audio_set_channels(int channels)
2138 if (!channels)
2139 return gus_audio_channels;
2140 if (channels > 2)
2141 channels = 2;
2142 if (channels < 1)
2143 channels = 1;
2144 gus_audio_channels = channels;
2145 return channels;
2148 static int gus_audio_set_bits(int bits)
2150 if (!bits)
2151 return gus_audio_bits;
2153 if (bits != 8 && bits != 16)
2154 bits = 8;
2156 if (only_8_bits)
2157 bits = 8;
2159 gus_audio_bits = bits;
2160 return bits;
2163 static int gus_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
2165 int val;
2167 switch (cmd)
2169 case SOUND_PCM_WRITE_RATE:
2170 if (get_user(val, (int *)arg))
2171 return -EFAULT;
2172 val = gus_audio_set_speed(val);
2173 break;
2175 case SOUND_PCM_READ_RATE:
2176 val = gus_audio_speed;
2177 break;
2179 case SNDCTL_DSP_STEREO:
2180 if (get_user(val, (int *)arg))
2181 return -EFAULT;
2182 val = gus_audio_set_channels(val + 1) - 1;
2183 break;
2185 case SOUND_PCM_WRITE_CHANNELS:
2186 if (get_user(val, (int *)arg))
2187 return -EFAULT;
2188 val = gus_audio_set_channels(val);
2189 break;
2191 case SOUND_PCM_READ_CHANNELS:
2192 val = gus_audio_channels;
2193 break;
2195 case SNDCTL_DSP_SETFMT:
2196 if (get_user(val, (int *)arg))
2197 return -EFAULT;
2198 val = gus_audio_set_bits(val);
2199 break;
2201 case SOUND_PCM_READ_BITS:
2202 val = gus_audio_bits;
2203 break;
2205 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2206 case SOUND_PCM_READ_FILTER:
2207 val = -EINVAL;
2208 break;
2209 default:
2210 return -EINVAL;
2212 return put_user(val, (int *)arg);
2215 static void gus_audio_reset(int dev)
2217 if (recording_active)
2219 gus_write8(0x49, 0x00); /* Halt recording */
2220 set_input_volumes();
2224 static int saved_iw_mode; /* A hack hack hack */
2226 static int gus_audio_open(int dev, int mode)
2228 if (gus_busy)
2229 return -EBUSY;
2231 if (gus_pnp_flag && mode & OPEN_READ)
2233 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2234 return -EIO;
2236 gus_initialize();
2238 gus_busy = 1;
2239 active_device = 0;
2241 gus_reset();
2242 reset_sample_memory();
2243 gus_select_max_voices(14);
2244 saved_iw_mode = iw_mode;
2245 if (iw_mode)
2247 /* There are some problems with audio in enhanced mode so disable it */
2248 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2249 iw_mode = 0;
2251 pcm_active = 0;
2252 dma_active = 0;
2253 pcm_opened = 1;
2254 if (mode & OPEN_READ)
2256 recording_active = 1;
2257 set_input_volumes();
2259 only_read_access = !(mode & OPEN_WRITE);
2260 only_8_bits = mode & OPEN_READ;
2261 if (only_8_bits)
2262 audio_devs[dev]->format_mask = AFMT_U8;
2263 else
2264 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2266 return 0;
2269 static void gus_audio_close(int dev)
2271 iw_mode = saved_iw_mode;
2272 gus_reset();
2273 gus_busy = 0;
2274 pcm_opened = 0;
2275 active_device = 0;
2277 if (recording_active)
2279 gus_write8(0x49, 0x00); /* Halt recording */
2280 set_input_volumes();
2282 recording_active = 0;
2285 static void gus_audio_update_volume(void)
2287 unsigned long flags;
2288 int voice;
2290 if (pcm_active && pcm_opened)
2291 for (voice = 0; voice < gus_audio_channels; voice++)
2293 save_flags(flags);
2294 cli();
2295 gus_select_voice(voice);
2296 gus_rampoff();
2297 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2298 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2299 restore_flags(flags);
2303 static void play_next_pcm_block(void)
2305 unsigned long flags;
2306 int speed = gus_audio_speed;
2307 int this_one, is16bits, chn;
2308 unsigned long dram_loc;
2309 unsigned char mode[2], ramp_mode[2];
2311 if (!pcm_qlen)
2312 return;
2314 this_one = pcm_head;
2316 for (chn = 0; chn < gus_audio_channels; chn++)
2318 mode[chn] = 0x00;
2319 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2321 if (chn == 0)
2323 mode[chn] |= 0x20; /* Loop IRQ */
2324 voices[chn].loop_irq_mode = LMODE_PCM;
2326 if (gus_audio_bits != 8)
2328 is16bits = 1;
2329 mode[chn] |= 0x04; /* 16 bit data */
2331 else
2332 is16bits = 0;
2334 dram_loc = this_one * pcm_bsize;
2335 dram_loc += chn * pcm_banksize;
2337 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2339 mode[chn] |= 0x08; /* Enable loop */
2340 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2342 else
2344 if (chn == 0)
2345 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2347 save_flags(flags);
2348 cli();
2349 gus_select_voice(chn);
2350 gus_voice_freq(speed);
2352 if (gus_audio_channels == 1)
2353 gus_voice_balance(7); /* mono */
2354 else if (chn == 0)
2355 gus_voice_balance(0); /* left */
2356 else
2357 gus_voice_balance(15); /* right */
2359 if (!pcm_active) /* Playback not already active */
2362 * The playback was not started yet (or there has been a pause).
2363 * Start the voice (again) and ask for a rollover irq at the end of
2364 * this_one block. If this_one one is last of the buffers, use just
2365 * the normal loop with irq.
2368 gus_voice_off();
2369 gus_rampoff();
2370 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2371 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2373 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2374 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2376 if (chn != 0)
2377 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2378 0, is16bits); /* Loop end location */
2380 if (chn == 0)
2381 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2382 0, is16bits); /* Loop end location */
2383 else
2384 mode[chn] |= 0x08; /* Enable looping */
2385 restore_flags(flags);
2387 for (chn = 0; chn < gus_audio_channels; chn++)
2389 save_flags(flags);
2390 cli();
2391 gus_select_voice(chn);
2392 gus_write8(0x0d, ramp_mode[chn]);
2393 if (iw_mode)
2394 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2395 gus_voice_on(mode[chn]);
2396 restore_flags(flags);
2398 pcm_active = 1;
2401 static void gus_transfer_output_block(int dev, unsigned long buf,
2402 int total_count, int intrflag, int chn)
2405 * This routine transfers one block of audio data to the DRAM. In mono mode
2406 * it's called just once. When in stereo mode, this_one routine is called
2407 * once for both channels.
2409 * The left/mono channel data is transferred to the beginning of dram and the
2410 * right data to the area pointed by gus_page_size.
2413 int this_one, count;
2414 unsigned long flags;
2415 unsigned char dma_command;
2416 unsigned long address, hold_address;
2418 save_flags(flags);
2419 cli();
2421 count = total_count / gus_audio_channels;
2423 if (chn == 0)
2425 if (pcm_qlen >= pcm_nblk)
2426 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2428 this_one = pcm_current_block = pcm_tail;
2429 pcm_qlen++;
2430 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2431 pcm_datasize[this_one] = count;
2433 else
2434 this_one = pcm_current_block;
2436 gus_write8(0x41, 0); /* Disable GF1 DMA */
2437 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2439 address = this_one * pcm_bsize;
2440 address += chn * pcm_banksize;
2442 if (audio_devs[dev]->dmap_out->dma > 3)
2444 hold_address = address;
2445 address = address >> 1;
2446 address &= 0x0001ffffL;
2447 address |= (hold_address & 0x000c0000L);
2449 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2451 dma_command = 0x21; /* IRQ enable, DMA start */
2453 if (gus_audio_bits != 8)
2454 dma_command |= 0x40; /* 16 bit _DATA_ */
2455 else
2456 dma_command |= 0x80; /* Invert MSB */
2458 if (audio_devs[dev]->dmap_out->dma > 3)
2459 dma_command |= 0x04; /* 16 bit DMA channel */
2461 gus_write8(0x41, dma_command); /* Kick start */
2463 if (chn == (gus_audio_channels - 1)) /* Last channel */
2466 * Last (right or mono) channel data
2468 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2469 active_device = GUS_DEV_PCM_DONE;
2470 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2472 play_next_pcm_block();
2475 else
2478 * Left channel data. The right channel
2479 * is transferred after DMA interrupt
2481 active_device = GUS_DEV_PCM_CONTINUE;
2484 restore_flags(flags);
2487 static void gus_uninterleave8(char *buf, int l)
2489 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2490 int i, p = 0, halfsize = l / 2;
2491 char *buf2 = buf + halfsize, *src = bounce_buf;
2493 memcpy(bounce_buf, buf, l);
2495 for (i = 0; i < halfsize; i++)
2497 buf[i] = src[p++]; /* Left channel */
2498 buf2[i] = src[p++]; /* Right channel */
2502 static void gus_uninterleave16(short *buf, int l)
2504 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2505 int i, p = 0, halfsize = l / 2;
2506 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2508 memcpy(bounce_buf, (char *) buf, l * 2);
2510 for (i = 0; i < halfsize; i++)
2512 buf[i] = src[p++]; /* Left channel */
2513 buf2[i] = src[p++]; /* Right channel */
2517 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2518 int intrflag)
2520 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2522 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2524 pcm_current_buf = buf;
2525 pcm_current_count = total_count;
2526 pcm_current_intrflag = intrflag;
2527 pcm_current_dev = dev;
2528 if (gus_audio_channels == 2)
2530 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2532 if (gus_audio_bits == 8)
2533 gus_uninterleave8(b, total_count);
2534 else
2535 gus_uninterleave16((short *) b, total_count / 2);
2537 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2540 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2541 int intrflag)
2543 unsigned long flags;
2544 unsigned char mode;
2546 save_flags(flags);
2547 cli();
2549 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2550 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2552 if (audio_devs[dev]->dmap_in->dma > 3)
2553 mode |= 0x04; /* 16 bit DMA channel */
2554 if (gus_audio_channels > 1)
2555 mode |= 0x02; /* Stereo */
2556 mode |= 0x01; /* DMA enable */
2558 gus_write8(0x49, mode);
2559 restore_flags(flags);
2562 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2564 unsigned int rate;
2566 gus_audio_bsize = bsize;
2567 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2568 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2570 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2572 if (gus_audio_bits != 8)
2574 /* printk("GUS Error: 16 bit recording not supported\n");*/
2575 return -EINVAL;
2577 return 0;
2580 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2582 int i;
2584 long mem_ptr, mem_size;
2586 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2587 mem_ptr = 0;
2588 mem_size = gus_mem_size / gus_audio_channels;
2590 if (mem_size > (256 * 1024))
2591 mem_size = 256 * 1024;
2593 pcm_bsize = bsize / gus_audio_channels;
2594 pcm_head = pcm_tail = pcm_qlen = 0;
2596 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2597 if ((pcm_bsize * pcm_nblk) > mem_size)
2598 pcm_nblk = mem_size / pcm_bsize;
2600 for (i = 0; i < pcm_nblk; i++)
2601 pcm_datasize[i] = 0;
2603 pcm_banksize = pcm_nblk * pcm_bsize;
2605 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2606 pcm_nblk--;
2607 gus_write8(0x41, 0); /* Disable GF1 DMA */
2608 return 0;
2611 static int gus_local_qlen(int dev)
2613 return pcm_qlen;
2617 static struct audio_driver gus_audio_driver =
2619 gus_audio_open,
2620 gus_audio_close,
2621 gus_audio_output_block,
2622 gus_audio_start_input,
2623 gus_audio_ioctl,
2624 gus_audio_prepare_for_input,
2625 gus_audio_prepare_for_output,
2626 gus_audio_reset,
2627 gus_local_qlen,
2628 NULL
2631 static void guswave_setup_voice(int dev, int voice, int chn)
2633 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2635 guswave_set_instr(dev, voice, info->pgm_num);
2636 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2637 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2638 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2639 voices[voice].bender = 0;
2640 voices[voice].bender_range = info->bender_range;
2642 if (chn == 9)
2643 voices[voice].fixed_pitch = 1;
2646 static void guswave_bender(int dev, int voice, int value)
2648 int freq;
2649 unsigned long flags;
2651 voices[voice].bender = value - 8192;
2652 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2653 voices[voice].current_freq = freq;
2655 save_flags(flags);
2656 cli();
2657 gus_select_voice(voice);
2658 gus_voice_freq(freq);
2659 restore_flags(flags);
2662 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2664 int i, p, best = -1, best_time = 0x7fffffff;
2666 p = alloc->ptr;
2668 * First look for a completely stopped voice
2671 for (i = 0; i < alloc->max_voice; i++)
2673 if (alloc->map[p] == 0)
2675 alloc->ptr = p;
2676 return p;
2678 if (alloc->alloc_times[p] < best_time)
2680 best = p;
2681 best_time = alloc->alloc_times[p];
2683 p = (p + 1) % alloc->max_voice;
2687 * Then look for a releasing voice
2690 for (i = 0; i < alloc->max_voice; i++)
2692 if (alloc->map[p] == 0xffff)
2694 alloc->ptr = p;
2695 return p;
2697 p = (p + 1) % alloc->max_voice;
2699 if (best >= 0)
2700 p = best;
2702 alloc->ptr = p;
2703 return p;
2706 static struct synth_operations guswave_operations =
2708 "GUS",
2709 &gus_info,
2711 SYNTH_TYPE_SAMPLE,
2712 SAMPLE_TYPE_GUS,
2713 guswave_open,
2714 guswave_close,
2715 guswave_ioctl,
2716 guswave_kill_note,
2717 guswave_start_note,
2718 guswave_set_instr,
2719 guswave_reset,
2720 guswave_hw_control,
2721 guswave_load_patch,
2722 guswave_aftertouch,
2723 guswave_controller,
2724 guswave_panning,
2725 guswave_volume_method,
2726 guswave_bender,
2727 guswave_alloc,
2728 guswave_setup_voice
2731 static void set_input_volumes(void)
2733 unsigned long flags;
2734 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2736 if (have_gus_max) /* Don't disturb GUS MAX */
2737 return;
2739 save_flags(flags);
2740 cli();
2743 * Enable channels having vol > 10%
2744 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2745 * the mic in ENABLED.
2747 if (gus_line_vol > 10)
2748 mask &= ~0x01;
2749 if (gus_mic_vol > 10)
2750 mask |= 0x04;
2752 if (recording_active)
2755 * Disable channel, if not selected for recording
2757 if (!(gus_recmask & SOUND_MASK_LINE))
2758 mask |= 0x01;
2759 if (!(gus_recmask & SOUND_MASK_MIC))
2760 mask &= ~0x04;
2762 mix_image &= ~0x07;
2763 mix_image |= mask & 0x07;
2764 outb((mix_image), u_Mixer);
2766 restore_flags(flags);
2769 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2770 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2772 int gus_default_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
2774 int vol, val;
2776 if (((cmd >> 8) & 0xff) != 'M')
2777 return -EINVAL;
2779 if (!access_ok(VERIFY_WRITE, (int *)arg, sizeof(int)))
2780 return -EFAULT;
2782 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2784 if (__get_user(val, (int *) arg))
2785 return -EFAULT;
2787 switch (cmd & 0xff)
2789 case SOUND_MIXER_RECSRC:
2790 gus_recmask = val & MIX_DEVS;
2791 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2792 gus_recmask = SOUND_MASK_MIC;
2793 /* Note! Input volumes are updated during next open for recording */
2794 val = gus_recmask;
2795 break;
2797 case SOUND_MIXER_MIC:
2798 vol = val & 0xff;
2799 if (vol < 0)
2800 vol = 0;
2801 if (vol > 100)
2802 vol = 100;
2803 gus_mic_vol = vol;
2804 set_input_volumes();
2805 val = vol | (vol << 8);
2806 break;
2808 case SOUND_MIXER_LINE:
2809 vol = val & 0xff;
2810 if (vol < 0)
2811 vol = 0;
2812 if (vol > 100)
2813 vol = 100;
2814 gus_line_vol = vol;
2815 set_input_volumes();
2816 val = vol | (vol << 8);
2817 break;
2819 case SOUND_MIXER_PCM:
2820 gus_pcm_volume = val & 0xff;
2821 if (gus_pcm_volume < 0)
2822 gus_pcm_volume = 0;
2823 if (gus_pcm_volume > 100)
2824 gus_pcm_volume = 100;
2825 gus_audio_update_volume();
2826 val = gus_pcm_volume | (gus_pcm_volume << 8);
2827 break;
2829 case SOUND_MIXER_SYNTH:
2830 gus_wave_volume = val & 0xff;
2831 if (gus_wave_volume < 0)
2832 gus_wave_volume = 0;
2833 if (gus_wave_volume > 100)
2834 gus_wave_volume = 100;
2835 if (active_device == GUS_DEV_WAVE)
2837 int voice;
2838 for (voice = 0; voice < nr_voices; voice++)
2839 dynamic_volume_change(voice); /* Apply the new vol */
2841 val = gus_wave_volume | (gus_wave_volume << 8);
2842 break;
2844 default:
2845 return -EINVAL;
2848 else
2850 switch (cmd & 0xff)
2853 * Return parameters
2855 case SOUND_MIXER_RECSRC:
2856 val = gus_recmask;
2857 break;
2859 case SOUND_MIXER_DEVMASK:
2860 val = MIX_DEVS;
2861 break;
2863 case SOUND_MIXER_STEREODEVS:
2864 val = 0;
2865 break;
2867 case SOUND_MIXER_RECMASK:
2868 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2869 break;
2871 case SOUND_MIXER_CAPS:
2872 val = 0;
2873 break;
2875 case SOUND_MIXER_MIC:
2876 val = gus_mic_vol | (gus_mic_vol << 8);
2877 break;
2879 case SOUND_MIXER_LINE:
2880 val = gus_line_vol | (gus_line_vol << 8);
2881 break;
2883 case SOUND_MIXER_PCM:
2884 val = gus_pcm_volume | (gus_pcm_volume << 8);
2885 break;
2887 case SOUND_MIXER_SYNTH:
2888 val = gus_wave_volume | (gus_wave_volume << 8);
2889 break;
2891 default:
2892 return -EINVAL;
2895 return __put_user(val, (int *)arg);
2898 static struct mixer_operations gus_mixer_operations =
2900 "GUS",
2901 "Gravis Ultrasound",
2902 gus_default_mixer_ioctl
2905 static int gus_default_mixer_init(void)
2907 int n;
2909 if ((n = sound_alloc_mixerdev()) != -1)
2912 * Don't install if there is another
2913 * mixer
2915 mixer_devs[n] = &gus_mixer_operations;
2917 if (have_gus_max)
2920 * Enable all mixer channels on the GF1 side. Otherwise recording will
2921 * not be possible using GUS MAX.
2923 mix_image &= ~0x07;
2924 mix_image |= 0x04; /* All channels enabled */
2925 outb((mix_image), u_Mixer);
2927 return n;
2930 void gus_wave_init(struct address_info *hw_config)
2932 unsigned long flags;
2933 unsigned char val;
2934 char *model_num = "2.4";
2935 char tmp[64], tmp2[64];
2936 int gus_type = 0x24; /* 2.4 */
2938 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2939 int dev;
2940 int sdev;
2942 hw_config->slots[0] = -1; /* No wave */
2943 hw_config->slots[1] = -1; /* No ad1848 */
2944 hw_config->slots[4] = -1; /* No audio */
2945 hw_config->slots[5] = -1; /* No mixer */
2947 if (!gus_pnp_flag)
2949 if (irq < 0 || irq > 15)
2951 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2952 return;
2956 if (dma < 0 || dma > 7 || dma == 4)
2958 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2959 return;
2961 gus_irq = irq;
2962 gus_dma = dma;
2963 gus_dma2 = dma2;
2965 if (gus_dma2 == -1)
2966 gus_dma2 = dma;
2969 * Try to identify the GUS model.
2971 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2974 save_flags(flags);
2975 cli();
2976 outb((0x20), gus_base + 0x0f);
2977 val = inb(gus_base + 0x0f);
2978 restore_flags(flags);
2980 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2982 int ad_flags = 0;
2984 if (gus_pnp_flag)
2985 ad_flags = 0x12345678; /* Interwave "magic" */
2987 * It has the digital ASIC so the card is at least v3.4.
2988 * Next try to detect the true model.
2991 if (gus_pnp_flag) /* Hack hack hack */
2992 val = 10;
2993 else
2994 val = inb(u_MixSelect);
2997 * Value 255 means pre-3.7 which don't have mixer.
2998 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2999 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
3003 if (val == 255 || val < 5)
3005 model_num = "3.4";
3006 gus_type = 0x34;
3008 else if (val < 10)
3010 model_num = "3.7";
3011 gus_type = 0x37;
3012 mixer_type = ICS2101;
3013 request_region(u_MixSelect, 1, "GUS mixer");
3015 else
3017 model_num = "MAX";
3018 gus_type = 0x40;
3019 mixer_type = CS4231;
3020 #ifdef CONFIG_GUSMAX
3022 unsigned char max_config = 0x40; /* Codec enable */
3024 if (gus_dma2 == -1)
3025 gus_dma2 = gus_dma;
3027 if (gus_dma > 3)
3028 max_config |= 0x10; /* 16 bit capture DMA */
3030 if (gus_dma2 > 3)
3031 max_config |= 0x20; /* 16 bit playback DMA */
3033 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
3035 outb((max_config), gus_base + 0x106); /* UltraMax control */
3038 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
3040 char *name = "GUS MAX";
3041 int old_num_mixers = num_mixers;
3043 if (gus_pnp_flag)
3044 name = "GUS PnP";
3046 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3047 gus_wave_volume = 90;
3048 have_gus_max = 1;
3049 if (hw_config->name)
3050 name = hw_config->name;
3052 hw_config->slots[1] = ad1848_init(name, gus_base + 0x10c,
3053 -irq, gus_dma2, /* Playback DMA */
3054 gus_dma, /* Capture DMA */
3055 1, /* Share DMA channels with GF1 */
3056 hw_config->osp);
3058 if (num_mixers > old_num_mixers)
3060 /* GUS has it's own mixer map */
3061 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
3062 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
3063 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3066 else
3067 printk(KERN_WARNING "GUS: No CS4231 ??");
3068 #else
3069 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3070 #endif
3073 else
3076 * ASIC not detected so the card must be 2.2 or 2.4.
3077 * There could still be the 16-bit/mixer daughter card.
3081 if (hw_config->name)
3083 strncpy(tmp, hw_config->name, 45);
3084 tmp[45] = 0;
3085 sprintf(tmp2, "%s (%dk)", tmp, (int) gus_mem_size / 1024);
3086 tmp2[sizeof(tmp2) - 1] = 0;
3088 else if (gus_pnp_flag)
3090 sprintf(tmp2, "Gravis UltraSound PnP (%dk)",
3091 (int) gus_mem_size / 1024);
3093 else
3094 sprintf(tmp2, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3097 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3098 if (samples == NULL)
3100 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3101 return;
3103 conf_printf(tmp2, hw_config);
3104 tmp2[sizeof(gus_info.name) - 1] = 0;
3105 strcpy(gus_info.name, tmp2);
3107 if ((sdev = sound_alloc_synthdev()) == -1)
3108 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3109 else
3111 voice_alloc = &guswave_operations.alloc;
3112 if (iw_mode)
3113 guswave_operations.id = "IWAVE";
3114 hw_config->slots[0] = sdev;
3115 synth_devs[sdev] = &guswave_operations;
3116 sequencer_init();
3117 #if defined(CONFIG_SEQUENCER) || defined(MODULE)
3118 gus_tmr_install(gus_base + 8);
3119 #endif
3122 reset_sample_memory();
3124 gus_initialize();
3126 if (gus_mem_size > 0)
3128 if ((dev = sound_alloc_audiodev()) != -1)
3130 hw_config->slots[4] = dev;
3131 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3132 "Ultrasound",
3133 &gus_audio_driver,
3134 sizeof(struct audio_driver),
3135 NEEDS_RESTART |
3136 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3137 DMA_DUPLEX : 0),
3138 AFMT_U8 | AFMT_S16_LE,
3139 NULL,
3140 dma,
3141 dma2)) < 0)
3143 return;
3146 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3147 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3148 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3149 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3150 } else
3151 printk(KERN_WARNING "GUS: Too many audio devices available\n");
3155 * Mixer dependent initialization.
3158 switch (mixer_type)
3160 case ICS2101:
3161 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3162 gus_wave_volume = 90;
3163 request_region(u_MixSelect, 1, "GUS mixer");
3164 hw_config->slots[5] = ics2101_mixer_init();
3165 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3166 return;
3168 case CS4231:
3169 /* Initialized elsewhere (ad1848.c) */
3170 default:
3171 hw_config->slots[5] = gus_default_mixer_init();
3172 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3173 return;
3177 void gus_wave_unload(struct address_info *hw_config)
3179 #ifdef CONFIG_GUSMAX
3180 if (have_gus_max)
3182 ad1848_unload(gus_base + 0x10c,
3183 -gus_irq,
3184 gus_dma2, /* Playback DMA */
3185 gus_dma, /* Capture DMA */
3186 1); /* Share DMA channels with GF1 */
3188 #endif
3190 if (mixer_type == ICS2101)
3192 release_region(u_MixSelect, 1);
3194 if (hw_config->slots[0] != -1)
3195 sound_unload_synthdev(hw_config->slots[0]);
3196 if (hw_config->slots[1] != -1)
3197 sound_unload_audiodev(hw_config->slots[1]);
3198 if (hw_config->slots[2] != -1)
3199 sound_unload_mididev(hw_config->slots[2]);
3200 if (hw_config->slots[4] != -1)
3201 sound_unload_audiodev(hw_config->slots[4]);
3202 if (hw_config->slots[5] != -1)
3203 sound_unload_mixerdev(hw_config->slots[5]);
3205 if(samples)
3206 vfree(samples);
3207 samples=NULL;
3210 static void do_loop_irq(int voice)
3212 unsigned char tmp;
3213 int mode, parm;
3214 unsigned long flags;
3216 save_flags(flags);
3217 cli();
3218 gus_select_voice(voice);
3220 tmp = gus_read8(0x00);
3221 tmp &= ~0x20; /*
3222 * Disable wave IRQ for this_one voice
3224 gus_write8(0x00, tmp);
3226 if (tmp & 0x03) /* Voice stopped */
3227 voice_alloc->map[voice] = 0;
3229 mode = voices[voice].loop_irq_mode;
3230 voices[voice].loop_irq_mode = 0;
3231 parm = voices[voice].loop_irq_parm;
3233 switch (mode)
3235 case LMODE_FINISH: /*
3236 * Final loop finished, shoot volume down
3239 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3240 * Get current volume
3243 gus_voice_off();
3244 gus_rampoff();
3245 gus_voice_init(voice);
3246 break;
3248 gus_ramp_range(65, 4065);
3249 gus_ramp_rate(0, 63); /*
3250 * Fastest possible rate
3252 gus_rampon(0x20 | 0x40); /*
3253 * Ramp down, once, irq
3255 voices[voice].volume_irq_mode = VMODE_HALT;
3256 break;
3258 case LMODE_PCM_STOP:
3259 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3260 case LMODE_PCM:
3262 pcm_qlen--;
3263 pcm_head = (pcm_head + 1) % pcm_nblk;
3264 if (pcm_qlen && pcm_active)
3266 play_next_pcm_block();
3268 else
3270 /* Underrun. Just stop the voice */
3271 gus_select_voice(0); /* Left channel */
3272 gus_voice_off();
3273 gus_rampoff();
3274 gus_select_voice(1); /* Right channel */
3275 gus_voice_off();
3276 gus_rampoff();
3277 pcm_active = 0;
3281 * If the queue was full before this interrupt, the DMA transfer was
3282 * suspended. Let it continue now.
3285 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3286 DMAbuf_outputintr(gus_devnum, 0);
3288 break;
3290 default:
3292 restore_flags(flags);
3295 static void do_volume_irq(int voice)
3297 unsigned char tmp;
3298 int mode, parm;
3299 unsigned long flags;
3301 save_flags(flags);
3302 cli();
3304 gus_select_voice(voice);
3305 tmp = gus_read8(0x0d);
3306 tmp &= ~0x20; /*
3307 * Disable volume ramp IRQ
3309 gus_write8(0x0d, tmp);
3311 mode = voices[voice].volume_irq_mode;
3312 voices[voice].volume_irq_mode = 0;
3313 parm = voices[voice].volume_irq_parm;
3315 switch (mode)
3317 case VMODE_HALT: /* Decay phase finished */
3318 if (iw_mode)
3319 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3320 restore_flags(flags);
3321 gus_voice_init(voice);
3322 break;
3324 case VMODE_ENVELOPE:
3325 gus_rampoff();
3326 restore_flags(flags);
3327 step_envelope(voice);
3328 break;
3330 case VMODE_START_NOTE:
3331 restore_flags(flags);
3332 guswave_start_note2(voices[voice].dev_pending, voice,
3333 voices[voice].note_pending, voices[voice].volume_pending);
3334 if (voices[voice].kill_pending)
3335 guswave_kill_note(voices[voice].dev_pending, voice,
3336 voices[voice].note_pending, 0);
3338 if (voices[voice].sample_pending >= 0)
3340 guswave_set_instr(voices[voice].dev_pending, voice,
3341 voices[voice].sample_pending);
3342 voices[voice].sample_pending = -1;
3344 break;
3346 default:
3347 restore_flags(flags);
3349 restore_flags(flags);
3352 void gus_voice_irq(void)
3354 unsigned long wave_ignore = 0, volume_ignore = 0;
3355 unsigned long voice_bit;
3357 unsigned char src, voice;
3359 while (1)
3361 src = gus_read8(0x0f); /*
3362 * Get source info
3364 voice = src & 0x1f;
3365 src &= 0xc0;
3367 if (src == (0x80 | 0x40))
3368 return; /*
3369 * No interrupt
3372 voice_bit = 1 << voice;
3374 if (!(src & 0x80)) /*
3375 * Wave IRQ pending
3377 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3378 * Not done
3379 * yet
3382 wave_ignore |= voice_bit;
3383 do_loop_irq(voice);
3385 if (!(src & 0x40)) /*
3386 * Volume IRQ pending
3388 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3389 * Not done
3390 * yet
3393 volume_ignore |= voice_bit;
3394 do_volume_irq(voice);
3399 void guswave_dma_irq(void)
3401 unsigned char status;
3403 status = gus_look8(0x41); /* Get DMA IRQ Status */
3404 if (status & 0x40) /* DMA interrupt pending */
3405 switch (active_device)
3407 case GUS_DEV_WAVE:
3408 if ((dram_sleep_flag.opts & WK_SLEEP))
3410 dram_sleep_flag.opts = WK_WAKEUP;
3411 wake_up(&dram_sleeper);
3413 break;
3415 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3416 gus_write8(0x41, 0); /* Disable GF1 DMA */
3417 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3418 pcm_current_count,
3419 pcm_current_intrflag, 1);
3420 break;
3422 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3423 gus_write8(0x41, 0); /* Disable GF1 DMA */
3424 if (pcm_qlen < pcm_nblk)
3426 dma_active = 0;
3427 if (gus_busy)
3429 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3430 DMAbuf_outputintr(gus_devnum, 0);
3433 break;
3435 default:
3437 status = gus_look8(0x49); /*
3438 * Get Sampling IRQ Status
3440 if (status & 0x40) /*
3441 * Sampling Irq pending
3444 DMAbuf_inputintr(gus_devnum);
3448 #if defined(CONFIG_SEQUENCER) || defined(MODULE)
3451 * Timer stuff
3454 static volatile int select_addr, data_addr;
3455 static volatile int curr_timer = 0;
3457 void gus_timer_command(unsigned int addr, unsigned int val)
3459 int i;
3461 outb(((unsigned char) (addr & 0xff)), select_addr);
3463 for (i = 0; i < 2; i++)
3464 inb(select_addr);
3466 outb(((unsigned char) (val & 0xff)), data_addr);
3468 for (i = 0; i < 2; i++)
3469 inb(select_addr);
3472 static void arm_timer(int timer, unsigned int interval)
3474 curr_timer = timer;
3476 if (timer == 1)
3478 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3479 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3480 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3482 else
3484 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3485 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3486 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3489 gus_timer_enabled = 1;
3492 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3494 int timer_no, resolution;
3495 int divisor;
3497 if (usecs_per_tick > (256 * 80))
3499 timer_no = 2;
3500 resolution = 320; /* usec */
3502 else
3504 timer_no = 1;
3505 resolution = 80; /* usec */
3507 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3508 arm_timer(timer_no, divisor);
3510 return divisor * resolution;
3513 static void gus_tmr_disable(int dev)
3515 gus_write8(0x45, 0); /* Disable both timers */
3516 gus_timer_enabled = 0;
3519 static void gus_tmr_restart(int dev)
3521 if (curr_timer == 1)
3522 gus_write8(0x45, 0x04); /* Start timer 1 again */
3523 else
3524 gus_write8(0x45, 0x08); /* Start timer 2 again */
3525 gus_timer_enabled = 1;
3528 static struct sound_lowlev_timer gus_tmr =
3532 gus_tmr_start,
3533 gus_tmr_disable,
3534 gus_tmr_restart
3537 static void gus_tmr_install(int io_base)
3539 struct sound_lowlev_timer *tmr;
3541 select_addr = io_base;
3542 data_addr = io_base + 1;
3544 tmr = &gus_tmr;
3546 #ifdef THIS_GETS_FIXED
3547 sound_timer_init(&gus_tmr, "GUS");
3548 #endif
3550 #endif
3552 #endif