Import 2.3.18pre1
[davej-history.git] / drivers / sound / gus_wave.c
blob0dddaacaaa0c074c73bc10d7231fb8a700edce01
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)
15 * Frank van de Pol : Fixed GUS MAX interrupt handling. Enabled simultanious
16 * usage of CS4231A codec, GUS wave and MIDI for GUS MAX.
20 #include <linux/config.h>
22 #define GUSPNP_AUTODETECT
24 #include "sound_config.h"
25 #include <linux/ultrasound.h>
26 #include "gus_hw.h"
28 #ifdef CONFIG_GUS
30 #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
32 #define MAX_SAMPLE 150
33 #define MAX_PATCH 256
35 #define NOT_SAMPLE 0xffff
37 struct voice_info
39 unsigned long orig_freq;
40 unsigned long current_freq;
41 unsigned long mode;
42 int fixed_pitch;
43 int bender;
44 int bender_range;
45 int panning;
46 int midi_volume;
47 unsigned int initial_volume;
48 unsigned int current_volume;
49 int loop_irq_mode, loop_irq_parm;
50 #define LMODE_FINISH 1
51 #define LMODE_PCM 2
52 #define LMODE_PCM_STOP 3
53 int volume_irq_mode, volume_irq_parm;
54 #define VMODE_HALT 1
55 #define VMODE_ENVELOPE 2
56 #define VMODE_START_NOTE 3
58 int env_phase;
59 unsigned char env_rate[6];
60 unsigned char env_offset[6];
63 * Volume computation parameters for gus_adagio_vol()
65 int main_vol, expression_vol, patch_vol;
67 /* Variables for "Ultraclick" removal */
68 int dev_pending, note_pending, volume_pending,
69 sample_pending;
70 char kill_pending;
71 long offset_pending;
75 static struct voice_alloc_info *voice_alloc;
76 static struct address_info *gus_hw_config;
77 extern int gus_base;
78 extern int gus_irq, gus_dma;
79 extern int gus_pnp_flag;
80 extern int gus_no_wave_dma;
81 static int gus_dma2 = -1;
82 static int dual_dma_mode = 0;
83 static long gus_mem_size = 0;
84 static long free_mem_ptr = 0;
85 static int gus_busy = 0;
86 static int gus_no_dma = 0;
87 static int nr_voices = 0;
88 static int gus_devnum = 0;
89 static int volume_base, volume_scale, volume_method;
90 static int gus_recmask = SOUND_MASK_MIC;
91 static int recording_active = 0;
92 static int only_read_access = 0;
93 static int only_8_bits = 0;
95 int iw_mode = 0;
96 int gus_wave_volume = 60;
97 int gus_pcm_volume = 80;
98 int have_gus_max = 0;
99 static int gus_line_vol = 100, gus_mic_vol = 0;
100 static unsigned char mix_image = 0x00;
102 int gus_timer_enabled = 0;
105 * Current version of this driver doesn't allow synth and PCM functions
106 * at the same time. The active_device specifies the active driver
109 static int active_device = 0;
111 #define GUS_DEV_WAVE 1 /* Wave table synth */
112 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
113 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
115 static int gus_audio_speed;
116 static int gus_audio_channels;
117 static int gus_audio_bits;
118 static int gus_audio_bsize;
119 static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
121 static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
124 * Variables and buffers for PCM output
127 #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
129 static int pcm_bsize, pcm_nblk, pcm_banksize;
130 static int pcm_datasize[MAX_PCM_BUFFERS];
131 static volatile int pcm_head, pcm_tail, pcm_qlen;
132 static volatile int pcm_active;
133 static volatile int dma_active;
134 static int pcm_opened = 0;
135 static int pcm_current_dev;
136 static int pcm_current_block;
137 static unsigned long pcm_current_buf;
138 static int pcm_current_count;
139 static int pcm_current_intrflag;
141 extern int *gus_osp;
143 static struct voice_info voices[32];
145 static int freq_div_table[] =
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()? Let's 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);
512 static void gus_voice_init(int voice)
514 unsigned long flags;
516 save_flags(flags);
517 cli();
518 gus_select_voice(voice);
519 gus_voice_volume(0);
520 gus_voice_off();
521 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
522 gus_write8(0x00, 0x03); /* Voice off */
523 gus_write8(0x0d, 0x03); /* Ramping off */
524 voice_alloc->map[voice] = 0;
525 voice_alloc->alloc_times[voice] = 0;
526 restore_flags(flags);
530 static void gus_voice_init2(int voice)
532 voices[voice].panning = 0;
533 voices[voice].mode = 0;
534 voices[voice].orig_freq = 20000;
535 voices[voice].current_freq = 20000;
536 voices[voice].bender = 0;
537 voices[voice].bender_range = 200;
538 voices[voice].initial_volume = 0;
539 voices[voice].current_volume = 0;
540 voices[voice].loop_irq_mode = 0;
541 voices[voice].loop_irq_parm = 0;
542 voices[voice].volume_irq_mode = 0;
543 voices[voice].volume_irq_parm = 0;
544 voices[voice].env_phase = 0;
545 voices[voice].main_vol = 127;
546 voices[voice].patch_vol = 127;
547 voices[voice].expression_vol = 127;
548 voices[voice].sample_pending = -1;
549 voices[voice].fixed_pitch = 0;
552 static void step_envelope(int voice)
554 unsigned vol, prev_vol, phase;
555 unsigned char rate;
556 long int flags;
558 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
560 save_flags(flags);
561 cli();
562 gus_select_voice(voice);
563 gus_rampoff();
564 restore_flags(flags);
565 return;
567 * Sustain phase begins. Continue envelope after receiving note off.
570 if (voices[voice].env_phase >= 5)
572 /* Envelope finished. Shoot the voice down */
573 gus_voice_init(voice);
574 return;
576 prev_vol = voices[voice].current_volume;
577 phase = ++voices[voice].env_phase;
578 compute_volume(voice, voices[voice].midi_volume);
579 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
580 rate = voices[voice].env_rate[phase];
582 save_flags(flags);
583 cli();
584 gus_select_voice(voice);
586 gus_voice_volume(prev_vol);
589 gus_write8(0x06, rate); /* Ramping rate */
591 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
593 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
595 restore_flags(flags);
596 step_envelope(voice); /* Continue the envelope on the next step */
597 return;
599 if (vol > prev_vol)
601 if (vol >= (4096 - 64))
602 vol = 4096 - 65;
603 gus_ramp_range(0, vol);
604 gus_rampon(0x20); /* Increasing volume, with IRQ */
606 else
608 if (vol <= 64)
609 vol = 65;
610 gus_ramp_range(vol, 4030);
611 gus_rampon(0x60); /* Decreasing volume, with IRQ */
613 voices[voice].current_volume = vol;
614 restore_flags(flags);
617 static void init_envelope(int voice)
619 voices[voice].env_phase = -1;
620 voices[voice].current_volume = 64;
622 step_envelope(voice);
625 static void start_release(int voice, long int flags)
627 if (gus_read8(0x00) & 0x03)
628 return; /* Voice already stopped */
630 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
632 voices[voice].current_volume = voices[voice].initial_volume =
633 gus_read16(0x09) >> 4; /* Get current volume */
635 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
636 gus_rampoff();
637 restore_flags(flags);
638 step_envelope(voice);
641 static void gus_voice_fade(int voice)
643 int instr_no = sample_map[voice], is16bits;
644 long int flags;
646 save_flags(flags);
647 cli();
648 gus_select_voice(voice);
650 if (instr_no < 0 || instr_no > MAX_SAMPLE)
652 gus_write8(0x00, 0x03); /* Hard stop */
653 voice_alloc->map[voice] = 0;
654 restore_flags(flags);
655 return;
657 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
659 if (voices[voice].mode & WAVE_ENVELOPES)
661 start_release(voice, flags);
662 restore_flags(flags);
663 return;
666 * Ramp the volume down but not too quickly.
668 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
670 gus_voice_off();
671 gus_rampoff();
672 gus_voice_init(voice);
673 restore_flags(flags);
674 return;
676 gus_ramp_range(65, 4030);
677 gus_ramp_rate(2, 4);
678 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
679 voices[voice].volume_irq_mode = VMODE_HALT;
680 restore_flags(flags);
683 static void gus_reset(void)
685 int i;
687 gus_select_max_voices(24);
688 volume_base = 3071;
689 volume_scale = 4;
690 volume_method = VOL_METHOD_ADAGIO;
692 for (i = 0; i < 32; i++)
694 gus_voice_init(i); /* Turn voice off */
695 gus_voice_init2(i);
699 static void gus_initialize(void)
701 unsigned long flags;
702 unsigned char dma_image, irq_image, tmp;
704 static unsigned char gus_irq_map[16] = {
705 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
708 static unsigned char gus_dma_map[8] = {
709 0, 1, 0, 2, 0, 3, 4, 5
712 save_flags(flags);
713 cli();
714 gus_write8(0x4c, 0); /* Reset GF1 */
715 gus_delay();
716 gus_delay();
718 gus_write8(0x4c, 1); /* Release Reset */
719 gus_delay();
720 gus_delay();
723 * Clear all interrupts
726 gus_write8(0x41, 0); /* DMA control */
727 gus_write8(0x45, 0); /* Timer control */
728 gus_write8(0x49, 0); /* Sample control */
730 gus_select_max_voices(24);
732 inb(u_Status); /* Touch the status register */
734 gus_look8(0x41); /* Clear any pending DMA IRQs */
735 gus_look8(0x49); /* Clear any pending sample IRQs */
736 gus_read8(0x0f); /* Clear pending IRQs */
738 gus_reset(); /* Resets all voices */
740 gus_look8(0x41); /* Clear any pending DMA IRQs */
741 gus_look8(0x49); /* Clear any pending sample IRQs */
742 gus_read8(0x0f); /* Clear pending IRQs */
744 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
747 * Set up for Digital ASIC
750 outb((0x05), gus_base + 0x0f);
752 mix_image |= 0x02; /* Disable line out (for a moment) */
753 outb((mix_image), u_Mixer);
755 outb((0x00), u_IRQDMAControl);
757 outb((0x00), gus_base + 0x0f);
760 * Now set up the DMA and IRQ interface
762 * The GUS supports two IRQs and two DMAs.
764 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
765 * Adding this support requires significant changes to the dmabuf.c, dsp.c
766 * and audio.c also.
769 irq_image = 0;
770 tmp = gus_irq_map[gus_irq];
771 if (!gus_pnp_flag && !tmp)
772 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
773 irq_image |= tmp;
774 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
776 dual_dma_mode = 1;
777 if (gus_dma2 == gus_dma || gus_dma2 == -1)
779 dual_dma_mode = 0;
780 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
782 tmp = gus_dma_map[gus_dma];
783 if (!tmp)
784 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
786 dma_image |= tmp;
788 else
790 /* Setup dual DMA channel mode for GUS MAX */
792 dma_image = gus_dma_map[gus_dma];
793 if (!dma_image)
794 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
796 tmp = gus_dma_map[gus_dma2] << 3;
797 if (!tmp)
799 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
800 tmp = 0x40; /* Combine DMA channels */
801 dual_dma_mode = 0;
803 dma_image |= tmp;
807 * For some reason the IRQ and DMA addresses must be written twice
811 * Doing it first time
814 outb((mix_image), u_Mixer); /* Select DMA control */
815 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
817 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
818 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
821 * Doing it second time
824 outb((mix_image), u_Mixer); /* Select DMA control */
825 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
827 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
828 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
830 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
832 mix_image &= ~0x02; /* Enable line out */
833 mix_image |= 0x08; /* Enable IRQ */
834 outb((mix_image), u_Mixer); /*
835 * Turn mixer channels on
836 * Note! Mic in is left off.
839 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
841 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
843 inb(u_Status); /* Touch the status register */
845 gus_look8(0x41); /* Clear any pending DMA IRQs */
846 gus_look8(0x49); /* Clear any pending sample IRQs */
848 gus_read8(0x0f); /* Clear pending IRQs */
850 if (iw_mode)
851 gus_write8(0x19, gus_read8(0x19) | 0x01);
852 restore_flags(flags);
856 static void pnp_mem_init(void)
858 #include "iwmem.h"
859 #define CHUNK_SIZE (256*1024)
860 #define BANK_SIZE (4*1024*1024)
861 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
863 int bank, chunk, addr, total = 0;
864 int bank_sizes[4];
865 int i, j, bits = -1, testbits = -1, nbanks = 0;
868 * This routine determines what kind of RAM is installed in each of the four
869 * SIMM banks and configures the DRAM address decode logic accordingly.
873 * Place the chip into enhanced mode
875 gus_write8(0x19, gus_read8(0x19) | 0x01);
876 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
879 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
882 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
885 * Perform the DRAM size detection for each bank individually.
887 for (bank = 0; bank < 4; bank++)
889 int size = 0;
891 addr = bank * BANK_SIZE;
893 /* Clean check points of each chunk */
894 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
896 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
897 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
900 /* Write a value to each chunk point and verify the result */
901 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
903 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
904 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
906 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
907 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
909 /* OK. There is RAM. Now check for possible shadows */
910 int ok = 1, chunk2;
912 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
913 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
914 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
915 ok = 0; /* Addressing wraps */
917 if (ok)
918 size = (chunk + 1) * CHUNK_SIZE;
920 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
921 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
923 bank_sizes[bank] = size;
924 if (size)
925 nbanks = bank + 1;
926 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
929 if (nbanks == 0) /* No RAM - Give up */
931 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
932 printk(KERN_ERR "Sound: Unable to work with this card.\n");
933 gus_write8(0x19, gus_read8(0x19) & ~0x01);
934 gus_mem_size = 0;
935 return;
939 * Now we know how much DRAM there is in each bank. The next step is
940 * to find a DRAM size encoding (0 to 12) which is best for the combination
941 * we have.
943 * First try if any of the possible alternatives matches exactly the amount
944 * of memory we have.
947 for (i = 0; bits == -1 && i < 13; i++)
949 bits = i;
951 for (j = 0; bits != -1 && j < 4; j++)
952 if (mem_decode[i][j] != bank_sizes[j])
953 bits = -1; /* No hit */
957 * If necessary, try to find a combination where other than the last
958 * bank matches our configuration and the last bank is left oversized.
959 * In this way we don't leave holes in the middle of memory.
962 if (bits == -1) /* No luck yet */
964 for (i = 0; bits == -1 && i < 13; i++)
966 bits = i;
968 for (j = 0; bits != -1 && j < nbanks - 1; j++)
969 if (mem_decode[i][j] != bank_sizes[j])
970 bits = -1; /* No hit */
971 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
972 bits = -1; /* The last bank is too small */
976 * The last resort is to search for a combination where the banks are
977 * smaller than the actual SIMMs. This leaves some memory in the banks
978 * unused but doesn't leave holes in the DRAM address space.
980 if (bits == -1) /* No luck yet */
982 for (i = 0; i < 13; i++)
984 testbits = i;
985 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
986 if (mem_decode[i][j] > bank_sizes[j]) {
987 testbits = -1;
989 if(testbits > bits) bits = testbits;
991 if (bits != -1)
993 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
994 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
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 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1106 return -EFAULT;
1107 return 0;
1109 case SNDCTL_SEQ_RESETSAMPLES:
1110 reset_sample_memory();
1111 return 0;
1113 case SNDCTL_SEQ_PERCMODE:
1114 return 0;
1116 case SNDCTL_SYNTH_MEMAVL:
1117 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1119 default:
1120 return -EINVAL;
1124 static int guswave_set_instr(int dev, int voice, int instr_no)
1126 int sample_no;
1128 if (instr_no < 0 || instr_no > MAX_PATCH)
1129 instr_no = 0; /* Default to acoustic piano */
1131 if (voice < 0 || voice > 31)
1132 return -EINVAL;
1134 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1136 voices[voice].sample_pending = instr_no;
1137 return 0;
1139 sample_no = patch_table[instr_no];
1140 patch_map[voice] = -1;
1142 if (sample_no == NOT_SAMPLE)
1144 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1145 return -EINVAL; /* Patch not defined */
1147 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1149 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1150 return -EINVAL;
1152 sample_map[voice] = sample_no;
1153 patch_map[voice] = instr_no;
1154 return 0;
1157 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1159 unsigned long flags;
1161 save_flags(flags);
1162 cli();
1163 /* voice_alloc->map[voice] = 0xffff; */
1164 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1166 voices[voice].kill_pending = 1;
1167 restore_flags(flags);
1169 else
1171 restore_flags(flags);
1172 gus_voice_fade(voice);
1175 return 0;
1178 static void guswave_aftertouch(int dev, int voice, int pressure)
1182 static void guswave_panning(int dev, int voice, int value)
1184 if (voice >= 0 || voice < 32)
1185 voices[voice].panning = value;
1188 static void guswave_volume_method(int dev, int mode)
1190 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1191 volume_method = mode;
1194 static void compute_volume(int voice, int volume)
1196 if (volume < 128)
1197 voices[voice].midi_volume = volume;
1199 switch (volume_method)
1201 case VOL_METHOD_ADAGIO:
1202 voices[voice].initial_volume =
1203 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1204 voices[voice].expression_vol,
1205 voices[voice].patch_vol);
1206 break;
1208 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1209 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1210 break;
1212 default:
1213 voices[voice].initial_volume = volume_base +
1214 (voices[voice].midi_volume * volume_scale);
1217 if (voices[voice].initial_volume > 4030)
1218 voices[voice].initial_volume = 4030;
1221 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1223 int curr, target, rate;
1224 unsigned long flags;
1226 compute_volume(voice, volume);
1227 voices[voice].current_volume = voices[voice].initial_volume;
1229 save_flags(flags);
1230 cli();
1232 * CAUTION! Interrupts disabled. Enable them before returning
1235 gus_select_voice(voice);
1237 curr = gus_read16(0x09) >> 4;
1238 target = voices[voice].initial_volume;
1240 if (ramp_time == INSTANT_RAMP)
1242 gus_rampoff();
1243 gus_voice_volume(target);
1244 restore_flags(flags);
1245 return;
1247 if (ramp_time == FAST_RAMP)
1248 rate = 63;
1249 else
1250 rate = 16;
1251 gus_ramp_rate(0, rate);
1253 if ((target - curr) / 64 == 0) /* Close enough to target. */
1255 gus_rampoff();
1256 gus_voice_volume(target);
1257 restore_flags(flags);
1258 return;
1260 if (target > curr)
1262 if (target > (4095 - 65))
1263 target = 4095 - 65;
1264 gus_ramp_range(curr, target);
1265 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1267 else
1269 if (target < 65)
1270 target = 65;
1272 gus_ramp_range(target, curr);
1273 gus_rampon(0x40); /* Ramp down, once, no irq */
1275 restore_flags(flags);
1278 static void dynamic_volume_change(int voice)
1280 unsigned char status;
1281 unsigned long flags;
1283 save_flags(flags);
1284 cli();
1285 gus_select_voice(voice);
1286 status = gus_read8(0x00); /* Get voice status */
1287 restore_flags(flags);
1289 if (status & 0x03)
1290 return; /* Voice was not running */
1292 if (!(voices[voice].mode & WAVE_ENVELOPES))
1294 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1295 return;
1299 * Voice is running and has envelopes.
1302 save_flags(flags);
1303 cli();
1304 gus_select_voice(voice);
1305 status = gus_read8(0x0d); /* Ramping status */
1306 restore_flags(flags);
1308 if (status & 0x03) /* Sustain phase? */
1310 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1311 return;
1313 if (voices[voice].env_phase < 0)
1314 return;
1316 compute_volume(voice, voices[voice].midi_volume);
1320 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1322 unsigned long flags;
1323 unsigned long freq;
1325 if (voice < 0 || voice > 31)
1326 return;
1328 switch (ctrl_num)
1330 case CTRL_PITCH_BENDER:
1331 voices[voice].bender = value;
1333 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1335 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1336 voices[voice].current_freq = freq;
1338 save_flags(flags);
1339 cli();
1340 gus_select_voice(voice);
1341 gus_voice_freq(freq);
1342 restore_flags(flags);
1344 break;
1346 case CTRL_PITCH_BENDER_RANGE:
1347 voices[voice].bender_range = value;
1348 break;
1349 case CTL_EXPRESSION:
1350 value /= 128;
1351 case CTRL_EXPRESSION:
1352 if (volume_method == VOL_METHOD_ADAGIO)
1354 voices[voice].expression_vol = value;
1355 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1356 dynamic_volume_change(voice);
1358 break;
1360 case CTL_PAN:
1361 voices[voice].panning = (value * 2) - 128;
1362 break;
1364 case CTL_MAIN_VOLUME:
1365 value = (value * 100) / 16383;
1367 case CTRL_MAIN_VOLUME:
1368 voices[voice].main_vol = value;
1369 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1370 dynamic_volume_change(voice);
1371 break;
1373 default:
1374 break;
1378 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1380 int sample, best_sample, best_delta, delta_freq;
1381 int is16bits, samplep, patch, pan;
1382 unsigned long note_freq, base_note, freq, flags;
1383 unsigned char mode = 0;
1385 if (voice < 0 || voice > 31)
1387 /* printk("GUS: Invalid voice\n");*/
1388 return -EINVAL;
1390 if (note_num == 255)
1392 if (voices[voice].mode & WAVE_ENVELOPES)
1394 voices[voice].midi_volume = volume;
1395 dynamic_volume_change(voice);
1396 return 0;
1398 compute_and_set_volume(voice, volume, 1);
1399 return 0;
1401 if ((patch = patch_map[voice]) == -1)
1402 return -EINVAL;
1403 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1405 return -EINVAL;
1407 note_freq = note_to_freq(note_num);
1410 * Find a sample within a patch so that the note_freq is between low_note
1411 * and high_note.
1413 sample = -1;
1415 best_sample = samplep;
1416 best_delta = 1000000;
1417 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1419 delta_freq = note_freq - samples[samplep].base_note;
1420 if (delta_freq < 0)
1421 delta_freq = -delta_freq;
1422 if (delta_freq < best_delta)
1424 best_sample = samplep;
1425 best_delta = delta_freq;
1427 if (samples[samplep].low_note <= note_freq &&
1428 note_freq <= samples[samplep].high_note)
1430 sample = samplep;
1432 else
1433 samplep = samples[samplep].key; /* Link to next sample */
1435 if (sample == -1)
1436 sample = best_sample;
1438 if (sample == -1)
1440 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1441 return 0; /* Should play default patch ??? */
1443 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1444 voices[voice].mode = samples[sample].mode;
1445 voices[voice].patch_vol = samples[sample].volume;
1447 if (iw_mode)
1448 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1450 if (voices[voice].mode & WAVE_ENVELOPES)
1452 int i;
1454 for (i = 0; i < 6; i++)
1456 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1457 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1460 sample_map[voice] = sample;
1462 if (voices[voice].fixed_pitch) /* Fixed pitch */
1464 freq = samples[sample].base_freq;
1466 else
1468 base_note = samples[sample].base_note / 100;
1469 note_freq /= 100;
1471 freq = samples[sample].base_freq * note_freq / base_note;
1474 voices[voice].orig_freq = freq;
1477 * Since the pitch bender may have been set before playing the note, we
1478 * have to calculate the bending now.
1481 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1482 voices[voice].bender_range, 0);
1483 voices[voice].current_freq = freq;
1485 pan = (samples[sample].panning + voices[voice].panning) / 32;
1486 pan += 7;
1487 if (pan < 0)
1488 pan = 0;
1489 if (pan > 15)
1490 pan = 15;
1492 if (samples[sample].mode & WAVE_16_BITS)
1494 mode |= 0x04; /* 16 bits */
1495 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1496 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1497 printk(KERN_ERR "GUS: Sample address error\n");
1499 /*************************************************************************
1500 * CAUTION! Interrupts disabled. Don't return before enabling
1501 *************************************************************************/
1503 save_flags(flags);
1504 cli();
1505 gus_select_voice(voice);
1506 gus_voice_off();
1507 gus_rampoff();
1509 restore_flags(flags);
1511 if (voices[voice].mode & WAVE_ENVELOPES)
1513 compute_volume(voice, volume);
1514 init_envelope(voice);
1516 else
1518 compute_and_set_volume(voice, volume, 0);
1521 save_flags(flags);
1522 cli();
1523 gus_select_voice(voice);
1525 if (samples[sample].mode & WAVE_LOOP_BACK)
1526 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1527 voices[voice].offset_pending, 0, is16bits); /* start=end */
1528 else
1529 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1531 if (samples[sample].mode & WAVE_LOOPING)
1533 mode |= 0x08;
1535 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1536 mode |= 0x10;
1538 if (samples[sample].mode & WAVE_LOOP_BACK)
1540 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1541 voices[voice].offset_pending,
1542 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1543 mode |= 0x40;
1545 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1546 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1547 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1548 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1550 else
1552 mode |= 0x20; /* Loop IRQ at the end */
1553 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1554 voices[voice].loop_irq_parm = 1;
1555 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1556 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1557 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1559 gus_voice_freq(freq);
1560 gus_voice_balance(pan);
1561 gus_voice_on(mode);
1562 restore_flags(flags);
1564 return 0;
1568 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1569 * when the note playing on the voice is changed. It uses volume
1570 * ramping.
1573 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1575 long int flags;
1576 int mode;
1577 int ret_val = 0;
1579 save_flags(flags);
1580 cli();
1581 if (note_num == 255)
1583 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1585 voices[voice].volume_pending = volume;
1587 else
1589 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1592 else
1594 gus_select_voice(voice);
1595 mode = gus_read8(0x00);
1596 if (mode & 0x20)
1597 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1599 voices[voice].offset_pending = 0;
1600 voices[voice].kill_pending = 0;
1601 voices[voice].volume_irq_mode = 0;
1602 voices[voice].loop_irq_mode = 0;
1604 if (voices[voice].sample_pending >= 0)
1606 restore_flags(flags); /* Run temporarily with interrupts enabled */
1607 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1608 voices[voice].sample_pending = -1;
1609 save_flags(flags);
1610 cli();
1611 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1613 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1615 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1617 else
1619 voices[voice].dev_pending = dev;
1620 voices[voice].note_pending = note_num;
1621 voices[voice].volume_pending = volume;
1622 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1624 gus_rampoff();
1625 gus_ramp_range(2000, 4065);
1626 gus_ramp_rate(0, 63); /* Fastest possible rate */
1627 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1630 restore_flags(flags);
1631 return ret_val;
1634 static void guswave_reset(int dev)
1636 int i;
1638 for (i = 0; i < 32; i++)
1640 gus_voice_init(i);
1641 gus_voice_init2(i);
1645 static int guswave_open(int dev, int mode)
1647 int err;
1649 if (gus_busy)
1650 return -EBUSY;
1652 voice_alloc->timestamp = 0;
1654 if (gus_no_wave_dma) {
1655 gus_no_dma = 1;
1656 } else {
1657 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1659 /* printk( "GUS: Loading samples without DMA\n"); */
1660 gus_no_dma = 1; /* Upload samples using PIO */
1662 else
1663 gus_no_dma = 0;
1666 init_waitqueue_head(&dram_sleeper);
1667 gus_busy = 1;
1668 active_device = GUS_DEV_WAVE;
1670 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1671 gus_initialize();
1672 gus_reset();
1673 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1675 return 0;
1678 static void guswave_close(int dev)
1680 gus_busy = 0;
1681 active_device = 0;
1682 gus_reset();
1684 if (!gus_no_dma)
1685 DMAbuf_close_dma(gus_devnum);
1688 static int guswave_load_patch(int dev, int format, const char *addr,
1689 int offs, int count, int pmgr_flag)
1691 struct patch_info patch;
1692 int instr;
1693 long sizeof_patch;
1695 unsigned long blk_sz, blk_end, left, src_offs, target;
1697 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1699 if (format != GUS_PATCH)
1701 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1702 return -EINVAL;
1704 if (count < sizeof_patch)
1706 /* printk("GUS Error: Patch header too short\n");*/
1707 return -EINVAL;
1709 count -= sizeof_patch;
1711 if (free_sample >= MAX_SAMPLE)
1713 /* printk("GUS: Sample table full\n");*/
1714 return -ENOSPC;
1717 * Copy the header from user space but ignore the first bytes which have
1718 * been transferred already.
1721 copy_from_user(&((char *) &patch)[offs], &(addr)[offs], sizeof_patch - offs);
1723 if (patch.mode & WAVE_ROM)
1724 return -EINVAL;
1725 if (gus_mem_size == 0)
1726 return -ENOSPC;
1728 instr = patch.instr_no;
1730 if (instr < 0 || instr > MAX_PATCH)
1732 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1733 return -EINVAL;
1735 if (count < patch.len)
1737 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1738 patch.len = count;
1740 if (patch.len <= 0 || patch.len > gus_mem_size)
1742 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1743 return -EINVAL;
1745 if (patch.mode & WAVE_LOOPING)
1747 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1749 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1750 return -EINVAL;
1752 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1754 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1755 return -EINVAL;
1758 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1760 if (patch.mode & WAVE_16_BITS)
1763 * 16 bit samples must fit one 256k bank.
1765 if (patch.len >= GUS_BANK_SIZE)
1767 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1768 return -ENOSPC;
1770 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1771 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1773 unsigned long tmp_mem =
1774 /* Align to 256K */
1775 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1777 if ((tmp_mem + patch.len) > gus_mem_size)
1778 return -ENOSPC;
1780 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1783 if ((free_mem_ptr + patch.len) > gus_mem_size)
1784 return -ENOSPC;
1786 sample_ptrs[free_sample] = free_mem_ptr;
1789 * Tremolo is not possible with envelopes
1792 if (patch.mode & WAVE_ENVELOPES)
1793 patch.mode &= ~WAVE_TREMOLO;
1795 if (!(patch.mode & WAVE_FRACTIONS))
1797 patch.fractions = 0;
1799 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1802 * Link this_one sample to the list of samples for patch 'instr'.
1805 samples[free_sample].key = patch_table[instr];
1806 patch_table[instr] = free_sample;
1809 * Use DMA to transfer the wave data to the DRAM
1812 left = patch.len;
1813 src_offs = 0;
1814 target = free_mem_ptr;
1816 while (left) /* Not completely transferred yet */
1818 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1819 if (blk_sz > left)
1820 blk_sz = left;
1823 * DMA cannot cross bank (256k) boundaries. Check for that.
1826 blk_end = target + blk_sz;
1828 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1830 /* Split the block */
1831 blk_end &= ~(GUS_BANK_SIZE - 1);
1832 blk_sz = blk_end - target;
1834 if (gus_no_dma)
1837 * For some reason the DMA is not possible. We have to use PIO.
1839 long i;
1840 unsigned char data;
1842 for (i = 0; i < blk_sz; i++)
1844 get_user(*(unsigned char *) &data, (unsigned char *) &((addr)[sizeof_patch + i]));
1845 if (patch.mode & WAVE_UNSIGNED)
1846 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1847 data ^= 0x80; /* Convert to signed */
1848 gus_poke(target + i, data);
1851 else
1853 unsigned long address, hold_address;
1854 unsigned char dma_command;
1855 unsigned long flags;
1857 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1859 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1860 return -ENOSPC;
1863 * OK, move now. First in and then out.
1866 copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf, &(addr)[sizeof_patch + src_offs], blk_sz);
1868 save_flags(flags);
1869 cli();
1870 /******** INTERRUPTS DISABLED NOW ********/
1871 gus_write8(0x41, 0); /* Disable GF1 DMA */
1872 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1873 blk_sz, DMA_MODE_WRITE);
1876 * Set the DRAM address for the wave data
1879 if (iw_mode)
1881 /* Different address translation in enhanced mode */
1883 unsigned char hi;
1885 if (gus_dma > 4)
1886 address = target >> 1; /* Convert to 16 bit word address */
1887 else
1888 address = target;
1890 hi = (unsigned char) ((address >> 16) & 0xf0);
1891 hi += (unsigned char) (address & 0x0f);
1893 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1894 gus_write8(0x50, hi);
1896 else
1898 address = target;
1899 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1901 hold_address = address;
1902 address = address >> 1;
1903 address &= 0x0001ffffL;
1904 address |= (hold_address & 0x000c0000L);
1906 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1910 * Start the DMA transfer
1913 dma_command = 0x21; /* IRQ enable, DMA start */
1914 if (patch.mode & WAVE_UNSIGNED)
1915 dma_command |= 0x80; /* Invert MSB */
1916 if (patch.mode & WAVE_16_BITS)
1917 dma_command |= 0x40; /* 16 bit _DATA_ */
1918 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1919 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1921 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1924 * Sleep here until the DRAM DMA done interrupt is served
1926 active_device = GUS_DEV_WAVE;
1928 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1929 printk("GUS: DMA Transfer timed out\n");
1930 restore_flags(flags);
1934 * Now the next part
1937 left -= blk_sz;
1938 src_offs += blk_sz;
1939 target += blk_sz;
1941 gus_write8(0x41, 0); /* Stop DMA */
1944 free_mem_ptr += patch.len;
1945 free_sample++;
1946 return 0;
1949 static void guswave_hw_control(int dev, unsigned char *event_rec)
1951 int voice, cmd;
1952 unsigned short p1, p2;
1953 unsigned int plong;
1954 unsigned flags;
1956 cmd = event_rec[2];
1957 voice = event_rec[3];
1958 p1 = *(unsigned short *) &event_rec[4];
1959 p2 = *(unsigned short *) &event_rec[6];
1960 plong = *(unsigned int *) &event_rec[4];
1962 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1963 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1964 do_volume_irq(voice);
1966 switch (cmd)
1968 case _GUS_NUMVOICES:
1969 save_flags(flags);
1970 cli();
1971 gus_select_voice(voice);
1972 gus_select_max_voices(p1);
1973 restore_flags(flags);
1974 break;
1976 case _GUS_VOICESAMPLE:
1977 guswave_set_instr(dev, voice, p1);
1978 break;
1980 case _GUS_VOICEON:
1981 save_flags(flags);
1982 cli();
1983 gus_select_voice(voice);
1984 p1 &= ~0x20; /* Don't allow interrupts */
1985 gus_voice_on(p1);
1986 restore_flags(flags);
1987 break;
1989 case _GUS_VOICEOFF:
1990 save_flags(flags);
1991 cli();
1992 gus_select_voice(voice);
1993 gus_voice_off();
1994 restore_flags(flags);
1995 break;
1997 case _GUS_VOICEFADE:
1998 gus_voice_fade(voice);
1999 break;
2001 case _GUS_VOICEMODE:
2002 save_flags(flags);
2003 cli();
2004 gus_select_voice(voice);
2005 p1 &= ~0x20; /* Don't allow interrupts */
2006 gus_voice_mode(p1);
2007 restore_flags(flags);
2008 break;
2010 case _GUS_VOICEBALA:
2011 save_flags(flags);
2012 cli();
2013 gus_select_voice(voice);
2014 gus_voice_balance(p1);
2015 restore_flags(flags);
2016 break;
2018 case _GUS_VOICEFREQ:
2019 save_flags(flags);
2020 cli();
2021 gus_select_voice(voice);
2022 gus_voice_freq(plong);
2023 restore_flags(flags);
2024 break;
2026 case _GUS_VOICEVOL:
2027 save_flags(flags);
2028 cli();
2029 gus_select_voice(voice);
2030 gus_voice_volume(p1);
2031 restore_flags(flags);
2032 break;
2034 case _GUS_VOICEVOL2: /* Just update the software voice level */
2035 voices[voice].initial_volume = voices[voice].current_volume = p1;
2036 break;
2038 case _GUS_RAMPRANGE:
2039 if (voices[voice].mode & WAVE_ENVELOPES)
2040 break; /* NO-NO */
2041 save_flags(flags);
2042 cli();
2043 gus_select_voice(voice);
2044 gus_ramp_range(p1, p2);
2045 restore_flags(flags);
2046 break;
2048 case _GUS_RAMPRATE:
2049 if (voices[voice].mode & WAVE_ENVELOPES)
2050 break; /* NJET-NJET */
2051 save_flags(flags);
2052 cli();
2053 gus_select_voice(voice);
2054 gus_ramp_rate(p1, p2);
2055 restore_flags(flags);
2056 break;
2058 case _GUS_RAMPMODE:
2059 if (voices[voice].mode & WAVE_ENVELOPES)
2060 break; /* NO-NO */
2061 save_flags(flags);
2062 cli();
2063 gus_select_voice(voice);
2064 p1 &= ~0x20; /* Don't allow interrupts */
2065 gus_ramp_mode(p1);
2066 restore_flags(flags);
2067 break;
2069 case _GUS_RAMPON:
2070 if (voices[voice].mode & WAVE_ENVELOPES)
2071 break; /* EI-EI */
2072 save_flags(flags);
2073 cli();
2074 gus_select_voice(voice);
2075 p1 &= ~0x20; /* Don't allow interrupts */
2076 gus_rampon(p1);
2077 restore_flags(flags);
2078 break;
2080 case _GUS_RAMPOFF:
2081 if (voices[voice].mode & WAVE_ENVELOPES)
2082 break; /* NEJ-NEJ */
2083 save_flags(flags);
2084 cli();
2085 gus_select_voice(voice);
2086 gus_rampoff();
2087 restore_flags(flags);
2088 break;
2090 case _GUS_VOLUME_SCALE:
2091 volume_base = p1;
2092 volume_scale = p2;
2093 break;
2095 case _GUS_VOICE_POS:
2096 save_flags(flags);
2097 cli();
2098 gus_select_voice(voice);
2099 gus_set_voice_pos(voice, plong);
2100 restore_flags(flags);
2101 break;
2103 default:
2107 static int gus_audio_set_speed(int speed)
2109 if (speed <= 0)
2110 speed = gus_audio_speed;
2112 if (speed < 4000)
2113 speed = 4000;
2115 if (speed > 44100)
2116 speed = 44100;
2118 gus_audio_speed = speed;
2120 if (only_read_access)
2122 /* Compute nearest valid recording speed and return it */
2124 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2125 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2126 speed = (9878400 / (speed * 16)) - 2;
2128 return speed;
2131 static int gus_audio_set_channels(int channels)
2133 if (!channels)
2134 return gus_audio_channels;
2135 if (channels > 2)
2136 channels = 2;
2137 if (channels < 1)
2138 channels = 1;
2139 gus_audio_channels = channels;
2140 return channels;
2143 static int gus_audio_set_bits(int bits)
2145 if (!bits)
2146 return gus_audio_bits;
2148 if (bits != 8 && bits != 16)
2149 bits = 8;
2151 if (only_8_bits)
2152 bits = 8;
2154 gus_audio_bits = bits;
2155 return bits;
2158 static int gus_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
2160 int val;
2162 switch (cmd)
2164 case SOUND_PCM_WRITE_RATE:
2165 if (get_user(val, (int *)arg))
2166 return -EFAULT;
2167 val = gus_audio_set_speed(val);
2168 break;
2170 case SOUND_PCM_READ_RATE:
2171 val = gus_audio_speed;
2172 break;
2174 case SNDCTL_DSP_STEREO:
2175 if (get_user(val, (int *)arg))
2176 return -EFAULT;
2177 val = gus_audio_set_channels(val + 1) - 1;
2178 break;
2180 case SOUND_PCM_WRITE_CHANNELS:
2181 if (get_user(val, (int *)arg))
2182 return -EFAULT;
2183 val = gus_audio_set_channels(val);
2184 break;
2186 case SOUND_PCM_READ_CHANNELS:
2187 val = gus_audio_channels;
2188 break;
2190 case SNDCTL_DSP_SETFMT:
2191 if (get_user(val, (int *)arg))
2192 return -EFAULT;
2193 val = gus_audio_set_bits(val);
2194 break;
2196 case SOUND_PCM_READ_BITS:
2197 val = gus_audio_bits;
2198 break;
2200 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2201 case SOUND_PCM_READ_FILTER:
2202 val = -EINVAL;
2203 break;
2204 default:
2205 return -EINVAL;
2207 return put_user(val, (int *)arg);
2210 static void gus_audio_reset(int dev)
2212 if (recording_active)
2214 gus_write8(0x49, 0x00); /* Halt recording */
2215 set_input_volumes();
2219 static int saved_iw_mode; /* A hack hack hack */
2221 static int gus_audio_open(int dev, int mode)
2223 if (gus_busy)
2224 return -EBUSY;
2226 if (gus_pnp_flag && mode & OPEN_READ)
2228 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2229 return -EIO;
2231 gus_initialize();
2233 gus_busy = 1;
2234 active_device = 0;
2236 saved_iw_mode = iw_mode;
2237 if (iw_mode)
2239 /* There are some problems with audio in enhanced mode so disable it */
2240 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2241 iw_mode = 0;
2244 gus_reset();
2245 reset_sample_memory();
2246 gus_select_max_voices(14);
2248 pcm_active = 0;
2249 dma_active = 0;
2250 pcm_opened = 1;
2251 if (mode & OPEN_READ)
2253 recording_active = 1;
2254 set_input_volumes();
2256 only_read_access = !(mode & OPEN_WRITE);
2257 only_8_bits = mode & OPEN_READ;
2258 if (only_8_bits)
2259 audio_devs[dev]->format_mask = AFMT_U8;
2260 else
2261 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2263 return 0;
2266 static void gus_audio_close(int dev)
2268 iw_mode = saved_iw_mode;
2269 gus_reset();
2270 gus_busy = 0;
2271 pcm_opened = 0;
2272 active_device = 0;
2274 if (recording_active)
2276 gus_write8(0x49, 0x00); /* Halt recording */
2277 set_input_volumes();
2279 recording_active = 0;
2282 static void gus_audio_update_volume(void)
2284 unsigned long flags;
2285 int voice;
2287 if (pcm_active && pcm_opened)
2288 for (voice = 0; voice < gus_audio_channels; voice++)
2290 save_flags(flags);
2291 cli();
2292 gus_select_voice(voice);
2293 gus_rampoff();
2294 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2295 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2296 restore_flags(flags);
2300 static void play_next_pcm_block(void)
2302 unsigned long flags;
2303 int speed = gus_audio_speed;
2304 int this_one, is16bits, chn;
2305 unsigned long dram_loc;
2306 unsigned char mode[2], ramp_mode[2];
2308 if (!pcm_qlen)
2309 return;
2311 this_one = pcm_head;
2313 for (chn = 0; chn < gus_audio_channels; chn++)
2315 mode[chn] = 0x00;
2316 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2318 if (chn == 0)
2320 mode[chn] |= 0x20; /* Loop IRQ */
2321 voices[chn].loop_irq_mode = LMODE_PCM;
2323 if (gus_audio_bits != 8)
2325 is16bits = 1;
2326 mode[chn] |= 0x04; /* 16 bit data */
2328 else
2329 is16bits = 0;
2331 dram_loc = this_one * pcm_bsize;
2332 dram_loc += chn * pcm_banksize;
2334 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2336 mode[chn] |= 0x08; /* Enable loop */
2337 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2339 else
2341 if (chn == 0)
2342 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2344 save_flags(flags);
2345 cli();
2346 gus_select_voice(chn);
2347 gus_voice_freq(speed);
2349 if (gus_audio_channels == 1)
2350 gus_voice_balance(7); /* mono */
2351 else if (chn == 0)
2352 gus_voice_balance(0); /* left */
2353 else
2354 gus_voice_balance(15); /* right */
2356 if (!pcm_active) /* Playback not already active */
2359 * The playback was not started yet (or there has been a pause).
2360 * Start the voice (again) and ask for a rollover irq at the end of
2361 * this_one block. If this_one one is last of the buffers, use just
2362 * the normal loop with irq.
2365 gus_voice_off();
2366 gus_rampoff();
2367 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2368 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2370 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2371 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2373 if (chn != 0)
2374 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2375 0, is16bits); /* Loop end location */
2377 if (chn == 0)
2378 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2379 0, is16bits); /* Loop end location */
2380 else
2381 mode[chn] |= 0x08; /* Enable looping */
2382 restore_flags(flags);
2384 for (chn = 0; chn < gus_audio_channels; chn++)
2386 save_flags(flags);
2387 cli();
2388 gus_select_voice(chn);
2389 gus_write8(0x0d, ramp_mode[chn]);
2390 if (iw_mode)
2391 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2392 gus_voice_on(mode[chn]);
2393 restore_flags(flags);
2395 pcm_active = 1;
2398 static void gus_transfer_output_block(int dev, unsigned long buf,
2399 int total_count, int intrflag, int chn)
2402 * This routine transfers one block of audio data to the DRAM. In mono mode
2403 * it's called just once. When in stereo mode, this_one routine is called
2404 * once for both channels.
2406 * The left/mono channel data is transferred to the beginning of dram and the
2407 * right data to the area pointed by gus_page_size.
2410 int this_one, count;
2411 unsigned long flags;
2412 unsigned char dma_command;
2413 unsigned long address, hold_address;
2415 save_flags(flags);
2416 cli();
2418 count = total_count / gus_audio_channels;
2420 if (chn == 0)
2422 if (pcm_qlen >= pcm_nblk)
2423 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2425 this_one = pcm_current_block = pcm_tail;
2426 pcm_qlen++;
2427 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2428 pcm_datasize[this_one] = count;
2430 else
2431 this_one = pcm_current_block;
2433 gus_write8(0x41, 0); /* Disable GF1 DMA */
2434 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2436 address = this_one * pcm_bsize;
2437 address += chn * pcm_banksize;
2439 if (audio_devs[dev]->dmap_out->dma > 3)
2441 hold_address = address;
2442 address = address >> 1;
2443 address &= 0x0001ffffL;
2444 address |= (hold_address & 0x000c0000L);
2446 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2448 dma_command = 0x21; /* IRQ enable, DMA start */
2450 if (gus_audio_bits != 8)
2451 dma_command |= 0x40; /* 16 bit _DATA_ */
2452 else
2453 dma_command |= 0x80; /* Invert MSB */
2455 if (audio_devs[dev]->dmap_out->dma > 3)
2456 dma_command |= 0x04; /* 16 bit DMA channel */
2458 gus_write8(0x41, dma_command); /* Kick start */
2460 if (chn == (gus_audio_channels - 1)) /* Last channel */
2463 * Last (right or mono) channel data
2465 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2466 active_device = GUS_DEV_PCM_DONE;
2467 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2469 play_next_pcm_block();
2472 else
2475 * Left channel data. The right channel
2476 * is transferred after DMA interrupt
2478 active_device = GUS_DEV_PCM_CONTINUE;
2481 restore_flags(flags);
2484 static void gus_uninterleave8(char *buf, int l)
2486 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2487 int i, p = 0, halfsize = l / 2;
2488 char *buf2 = buf + halfsize, *src = bounce_buf;
2490 memcpy(bounce_buf, buf, l);
2492 for (i = 0; i < halfsize; i++)
2494 buf[i] = src[p++]; /* Left channel */
2495 buf2[i] = src[p++]; /* Right channel */
2499 static void gus_uninterleave16(short *buf, int l)
2501 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2502 int i, p = 0, halfsize = l / 2;
2503 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2505 memcpy(bounce_buf, (char *) buf, l * 2);
2507 for (i = 0; i < halfsize; i++)
2509 buf[i] = src[p++]; /* Left channel */
2510 buf2[i] = src[p++]; /* Right channel */
2514 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2515 int intrflag)
2517 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2519 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2521 pcm_current_buf = buf;
2522 pcm_current_count = total_count;
2523 pcm_current_intrflag = intrflag;
2524 pcm_current_dev = dev;
2525 if (gus_audio_channels == 2)
2527 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2529 if (gus_audio_bits == 8)
2530 gus_uninterleave8(b, total_count);
2531 else
2532 gus_uninterleave16((short *) b, total_count / 2);
2534 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2537 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2538 int intrflag)
2540 unsigned long flags;
2541 unsigned char mode;
2543 save_flags(flags);
2544 cli();
2546 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2547 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2549 if (audio_devs[dev]->dmap_in->dma > 3)
2550 mode |= 0x04; /* 16 bit DMA channel */
2551 if (gus_audio_channels > 1)
2552 mode |= 0x02; /* Stereo */
2553 mode |= 0x01; /* DMA enable */
2555 gus_write8(0x49, mode);
2556 restore_flags(flags);
2559 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2561 unsigned int rate;
2563 gus_audio_bsize = bsize;
2564 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2565 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2567 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2569 if (gus_audio_bits != 8)
2571 /* printk("GUS Error: 16 bit recording not supported\n");*/
2572 return -EINVAL;
2574 return 0;
2577 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2579 int i;
2581 long mem_ptr, mem_size;
2583 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2584 mem_ptr = 0;
2585 mem_size = gus_mem_size / gus_audio_channels;
2587 if (mem_size > (256 * 1024))
2588 mem_size = 256 * 1024;
2590 pcm_bsize = bsize / gus_audio_channels;
2591 pcm_head = pcm_tail = pcm_qlen = 0;
2593 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2594 if ((pcm_bsize * pcm_nblk) > mem_size)
2595 pcm_nblk = mem_size / pcm_bsize;
2597 for (i = 0; i < pcm_nblk; i++)
2598 pcm_datasize[i] = 0;
2600 pcm_banksize = pcm_nblk * pcm_bsize;
2602 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2603 pcm_nblk--;
2604 gus_write8(0x41, 0); /* Disable GF1 DMA */
2605 return 0;
2608 static int gus_local_qlen(int dev)
2610 return pcm_qlen;
2614 static struct audio_driver gus_audio_driver =
2616 gus_audio_open,
2617 gus_audio_close,
2618 gus_audio_output_block,
2619 gus_audio_start_input,
2620 gus_audio_ioctl,
2621 gus_audio_prepare_for_input,
2622 gus_audio_prepare_for_output,
2623 gus_audio_reset,
2624 gus_local_qlen,
2625 NULL
2628 static void guswave_setup_voice(int dev, int voice, int chn)
2630 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2632 guswave_set_instr(dev, voice, info->pgm_num);
2633 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2634 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2635 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2636 voices[voice].bender = 0;
2637 voices[voice].bender_range = info->bender_range;
2639 if (chn == 9)
2640 voices[voice].fixed_pitch = 1;
2643 static void guswave_bender(int dev, int voice, int value)
2645 int freq;
2646 unsigned long flags;
2648 voices[voice].bender = value - 8192;
2649 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2650 voices[voice].current_freq = freq;
2652 save_flags(flags);
2653 cli();
2654 gus_select_voice(voice);
2655 gus_voice_freq(freq);
2656 restore_flags(flags);
2659 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2661 int i, p, best = -1, best_time = 0x7fffffff;
2663 p = alloc->ptr;
2665 * First look for a completely stopped voice
2668 for (i = 0; i < alloc->max_voice; i++)
2670 if (alloc->map[p] == 0)
2672 alloc->ptr = p;
2673 return p;
2675 if (alloc->alloc_times[p] < best_time)
2677 best = p;
2678 best_time = alloc->alloc_times[p];
2680 p = (p + 1) % alloc->max_voice;
2684 * Then look for a releasing voice
2687 for (i = 0; i < alloc->max_voice; i++)
2689 if (alloc->map[p] == 0xffff)
2691 alloc->ptr = p;
2692 return p;
2694 p = (p + 1) % alloc->max_voice;
2696 if (best >= 0)
2697 p = best;
2699 alloc->ptr = p;
2700 return p;
2703 static struct synth_operations guswave_operations =
2705 "GUS",
2706 &gus_info,
2708 SYNTH_TYPE_SAMPLE,
2709 SAMPLE_TYPE_GUS,
2710 guswave_open,
2711 guswave_close,
2712 guswave_ioctl,
2713 guswave_kill_note,
2714 guswave_start_note,
2715 guswave_set_instr,
2716 guswave_reset,
2717 guswave_hw_control,
2718 guswave_load_patch,
2719 guswave_aftertouch,
2720 guswave_controller,
2721 guswave_panning,
2722 guswave_volume_method,
2723 guswave_bender,
2724 guswave_alloc,
2725 guswave_setup_voice
2728 static void set_input_volumes(void)
2730 unsigned long flags;
2731 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2733 if (have_gus_max) /* Don't disturb GUS MAX */
2734 return;
2736 save_flags(flags);
2737 cli();
2740 * Enable channels having vol > 10%
2741 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2742 * the mic in ENABLED.
2744 if (gus_line_vol > 10)
2745 mask &= ~0x01;
2746 if (gus_mic_vol > 10)
2747 mask |= 0x04;
2749 if (recording_active)
2752 * Disable channel, if not selected for recording
2754 if (!(gus_recmask & SOUND_MASK_LINE))
2755 mask |= 0x01;
2756 if (!(gus_recmask & SOUND_MASK_MIC))
2757 mask &= ~0x04;
2759 mix_image &= ~0x07;
2760 mix_image |= mask & 0x07;
2761 outb((mix_image), u_Mixer);
2763 restore_flags(flags);
2766 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2767 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2769 int gus_default_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
2771 int vol, val;
2773 if (((cmd >> 8) & 0xff) != 'M')
2774 return -EINVAL;
2776 if (!access_ok(VERIFY_WRITE, (int *)arg, sizeof(int)))
2777 return -EFAULT;
2779 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2781 if (__get_user(val, (int *) arg))
2782 return -EFAULT;
2784 switch (cmd & 0xff)
2786 case SOUND_MIXER_RECSRC:
2787 gus_recmask = val & MIX_DEVS;
2788 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2789 gus_recmask = SOUND_MASK_MIC;
2790 /* Note! Input volumes are updated during next open for recording */
2791 val = gus_recmask;
2792 break;
2794 case SOUND_MIXER_MIC:
2795 vol = val & 0xff;
2796 if (vol < 0)
2797 vol = 0;
2798 if (vol > 100)
2799 vol = 100;
2800 gus_mic_vol = vol;
2801 set_input_volumes();
2802 val = vol | (vol << 8);
2803 break;
2805 case SOUND_MIXER_LINE:
2806 vol = val & 0xff;
2807 if (vol < 0)
2808 vol = 0;
2809 if (vol > 100)
2810 vol = 100;
2811 gus_line_vol = vol;
2812 set_input_volumes();
2813 val = vol | (vol << 8);
2814 break;
2816 case SOUND_MIXER_PCM:
2817 gus_pcm_volume = val & 0xff;
2818 if (gus_pcm_volume < 0)
2819 gus_pcm_volume = 0;
2820 if (gus_pcm_volume > 100)
2821 gus_pcm_volume = 100;
2822 gus_audio_update_volume();
2823 val = gus_pcm_volume | (gus_pcm_volume << 8);
2824 break;
2826 case SOUND_MIXER_SYNTH:
2827 gus_wave_volume = val & 0xff;
2828 if (gus_wave_volume < 0)
2829 gus_wave_volume = 0;
2830 if (gus_wave_volume > 100)
2831 gus_wave_volume = 100;
2832 if (active_device == GUS_DEV_WAVE)
2834 int voice;
2835 for (voice = 0; voice < nr_voices; voice++)
2836 dynamic_volume_change(voice); /* Apply the new vol */
2838 val = gus_wave_volume | (gus_wave_volume << 8);
2839 break;
2841 default:
2842 return -EINVAL;
2845 else
2847 switch (cmd & 0xff)
2850 * Return parameters
2852 case SOUND_MIXER_RECSRC:
2853 val = gus_recmask;
2854 break;
2856 case SOUND_MIXER_DEVMASK:
2857 val = MIX_DEVS;
2858 break;
2860 case SOUND_MIXER_STEREODEVS:
2861 val = 0;
2862 break;
2864 case SOUND_MIXER_RECMASK:
2865 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2866 break;
2868 case SOUND_MIXER_CAPS:
2869 val = 0;
2870 break;
2872 case SOUND_MIXER_MIC:
2873 val = gus_mic_vol | (gus_mic_vol << 8);
2874 break;
2876 case SOUND_MIXER_LINE:
2877 val = gus_line_vol | (gus_line_vol << 8);
2878 break;
2880 case SOUND_MIXER_PCM:
2881 val = gus_pcm_volume | (gus_pcm_volume << 8);
2882 break;
2884 case SOUND_MIXER_SYNTH:
2885 val = gus_wave_volume | (gus_wave_volume << 8);
2886 break;
2888 default:
2889 return -EINVAL;
2892 return __put_user(val, (int *)arg);
2895 static struct mixer_operations gus_mixer_operations =
2897 "GUS",
2898 "Gravis Ultrasound",
2899 gus_default_mixer_ioctl
2902 static int gus_default_mixer_init(void)
2904 int n;
2906 if ((n = sound_alloc_mixerdev()) != -1)
2909 * Don't install if there is another
2910 * mixer
2912 mixer_devs[n] = &gus_mixer_operations;
2914 if (have_gus_max)
2917 * Enable all mixer channels on the GF1 side. Otherwise recording will
2918 * not be possible using GUS MAX.
2920 mix_image &= ~0x07;
2921 mix_image |= 0x04; /* All channels enabled */
2922 outb((mix_image), u_Mixer);
2924 return n;
2927 void gus_wave_init(struct address_info *hw_config)
2929 unsigned long flags;
2930 unsigned char val;
2931 char *model_num = "2.4";
2932 char tmp[64], tmp2[64];
2933 int gus_type = 0x24; /* 2.4 */
2935 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2936 int sdev;
2938 hw_config->slots[0] = -1; /* No wave */
2939 hw_config->slots[1] = -1; /* No ad1848 */
2940 hw_config->slots[4] = -1; /* No audio */
2941 hw_config->slots[5] = -1; /* No mixer */
2943 if (!gus_pnp_flag)
2945 if (irq < 0 || irq > 15)
2947 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2948 return;
2952 if (dma < 0 || dma > 7 || dma == 4)
2954 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2955 return;
2957 gus_irq = irq;
2958 gus_dma = dma;
2959 gus_dma2 = dma2;
2960 gus_hw_config = hw_config;
2962 if (gus_dma2 == -1)
2963 gus_dma2 = dma;
2966 * Try to identify the GUS model.
2968 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2971 save_flags(flags);
2972 cli();
2973 outb((0x20), gus_base + 0x0f);
2974 val = inb(gus_base + 0x0f);
2975 restore_flags(flags);
2977 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2979 int ad_flags = 0;
2981 if (gus_pnp_flag)
2982 ad_flags = 0x12345678; /* Interwave "magic" */
2984 * It has the digital ASIC so the card is at least v3.4.
2985 * Next try to detect the true model.
2988 if (gus_pnp_flag) /* Hack hack hack */
2989 val = 10;
2990 else
2991 val = inb(u_MixSelect);
2994 * Value 255 means pre-3.7 which don't have mixer.
2995 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2996 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
3000 if (val == 255 || val < 5)
3002 model_num = "3.4";
3003 gus_type = 0x34;
3005 else if (val < 10)
3007 model_num = "3.7";
3008 gus_type = 0x37;
3009 mixer_type = ICS2101;
3010 request_region(u_MixSelect, 1, "GUS mixer");
3012 else
3014 model_num = "MAX";
3015 gus_type = 0x40;
3016 mixer_type = CS4231;
3017 #ifdef CONFIG_GUSMAX
3019 unsigned char max_config = 0x40; /* Codec enable */
3021 if (gus_dma2 == -1)
3022 gus_dma2 = gus_dma;
3024 if (gus_dma > 3)
3025 max_config |= 0x10; /* 16 bit capture DMA */
3027 if (gus_dma2 > 3)
3028 max_config |= 0x20; /* 16 bit playback DMA */
3030 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
3032 outb((max_config), gus_base + 0x106); /* UltraMax control */
3035 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
3037 char *name = "GUS MAX";
3038 int old_num_mixers = num_mixers;
3040 if (gus_pnp_flag)
3041 name = "GUS PnP";
3043 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3044 gus_wave_volume = 90;
3045 have_gus_max = 1;
3046 if (hw_config->name)
3047 name = hw_config->name;
3049 hw_config->slots[1] = ad1848_init(name, gus_base + 0x10c,
3050 -irq, gus_dma2, /* Playback DMA */
3051 gus_dma, /* Capture DMA */
3052 1, /* Share DMA channels with GF1 */
3053 hw_config->osp);
3055 if (num_mixers > old_num_mixers)
3057 /* GUS has it's own mixer map */
3058 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
3059 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
3060 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3063 else
3064 printk(KERN_WARNING "GUS: No CS4231 ??");
3065 #else
3066 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3067 #endif
3070 else
3073 * ASIC not detected so the card must be 2.2 or 2.4.
3074 * There could still be the 16-bit/mixer daughter card.
3078 if (hw_config->name)
3080 strncpy(tmp, hw_config->name, 45);
3081 tmp[45] = 0;
3082 sprintf(tmp2, "%s (%dk)", tmp, (int) gus_mem_size / 1024);
3083 tmp2[sizeof(tmp2) - 1] = 0;
3085 else if (gus_pnp_flag)
3087 sprintf(tmp2, "Gravis UltraSound PnP (%dk)",
3088 (int) gus_mem_size / 1024);
3090 else
3091 sprintf(tmp2, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3094 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3095 if (samples == NULL)
3097 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3098 return;
3100 conf_printf(tmp2, hw_config);
3101 tmp2[sizeof(gus_info.name) - 1] = 0;
3102 strcpy(gus_info.name, tmp2);
3104 if ((sdev = sound_alloc_synthdev()) == -1)
3105 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3106 else
3108 voice_alloc = &guswave_operations.alloc;
3109 if (iw_mode)
3110 guswave_operations.id = "IWAVE";
3111 hw_config->slots[0] = sdev;
3112 synth_devs[sdev] = &guswave_operations;
3113 sequencer_init();
3114 #ifdef CONFIG_SEQUENCER
3115 gus_tmr_install(gus_base + 8);
3116 #endif
3119 reset_sample_memory();
3121 gus_initialize();
3123 if ((gus_mem_size > 0) & !gus_no_wave_dma)
3125 hw_config->slots[4] = -1;
3126 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3127 "Ultrasound",
3128 &gus_audio_driver,
3129 sizeof(struct audio_driver),
3130 NEEDS_RESTART |
3131 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3132 DMA_DUPLEX : 0),
3133 AFMT_U8 | AFMT_S16_LE,
3134 NULL, dma, dma2)) < 0)
3136 return;
3139 hw_config->slots[4] = gus_devnum;
3140 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3141 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3142 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3143 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3147 * Mixer dependent initialization.
3150 switch (mixer_type)
3152 case ICS2101:
3153 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3154 gus_wave_volume = 90;
3155 request_region(u_MixSelect, 1, "GUS mixer");
3156 hw_config->slots[5] = ics2101_mixer_init();
3157 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3158 return;
3160 case CS4231:
3161 /* Initialized elsewhere (ad1848.c) */
3162 default:
3163 hw_config->slots[5] = gus_default_mixer_init();
3164 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3165 return;
3169 void gus_wave_unload(struct address_info *hw_config)
3171 #ifdef CONFIG_GUSMAX
3172 if (have_gus_max)
3174 ad1848_unload(gus_base + 0x10c,
3175 -gus_irq,
3176 gus_dma2, /* Playback DMA */
3177 gus_dma, /* Capture DMA */
3178 1); /* Share DMA channels with GF1 */
3180 #endif
3182 if (mixer_type == ICS2101)
3184 release_region(u_MixSelect, 1);
3186 if (hw_config->slots[0] != -1)
3187 sound_unload_synthdev(hw_config->slots[0]);
3188 if (hw_config->slots[1] != -1)
3189 sound_unload_audiodev(hw_config->slots[1]);
3190 if (hw_config->slots[2] != -1)
3191 sound_unload_mididev(hw_config->slots[2]);
3192 if (hw_config->slots[4] != -1)
3193 sound_unload_audiodev(hw_config->slots[4]);
3194 if (hw_config->slots[5] != -1)
3195 sound_unload_mixerdev(hw_config->slots[5]);
3197 if(samples)
3198 vfree(samples);
3199 samples=NULL;
3202 static void do_loop_irq(int voice)
3204 unsigned char tmp;
3205 int mode, parm;
3206 unsigned long flags;
3208 save_flags(flags);
3209 cli();
3210 gus_select_voice(voice);
3212 tmp = gus_read8(0x00);
3213 tmp &= ~0x20; /*
3214 * Disable wave IRQ for this_one voice
3216 gus_write8(0x00, tmp);
3218 if (tmp & 0x03) /* Voice stopped */
3219 voice_alloc->map[voice] = 0;
3221 mode = voices[voice].loop_irq_mode;
3222 voices[voice].loop_irq_mode = 0;
3223 parm = voices[voice].loop_irq_parm;
3225 switch (mode)
3227 case LMODE_FINISH: /*
3228 * Final loop finished, shoot volume down
3231 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3232 * Get current volume
3235 gus_voice_off();
3236 gus_rampoff();
3237 gus_voice_init(voice);
3238 break;
3240 gus_ramp_range(65, 4065);
3241 gus_ramp_rate(0, 63); /*
3242 * Fastest possible rate
3244 gus_rampon(0x20 | 0x40); /*
3245 * Ramp down, once, irq
3247 voices[voice].volume_irq_mode = VMODE_HALT;
3248 break;
3250 case LMODE_PCM_STOP:
3251 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3252 case LMODE_PCM:
3254 pcm_qlen--;
3255 pcm_head = (pcm_head + 1) % pcm_nblk;
3256 if (pcm_qlen && pcm_active)
3258 play_next_pcm_block();
3260 else
3262 /* Underrun. Just stop the voice */
3263 gus_select_voice(0); /* Left channel */
3264 gus_voice_off();
3265 gus_rampoff();
3266 gus_select_voice(1); /* Right channel */
3267 gus_voice_off();
3268 gus_rampoff();
3269 pcm_active = 0;
3273 * If the queue was full before this interrupt, the DMA transfer was
3274 * suspended. Let it continue now.
3277 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3278 DMAbuf_outputintr(gus_devnum, 0);
3280 break;
3282 default:
3284 restore_flags(flags);
3287 static void do_volume_irq(int voice)
3289 unsigned char tmp;
3290 int mode, parm;
3291 unsigned long flags;
3293 save_flags(flags);
3294 cli();
3296 gus_select_voice(voice);
3297 tmp = gus_read8(0x0d);
3298 tmp &= ~0x20; /*
3299 * Disable volume ramp IRQ
3301 gus_write8(0x0d, tmp);
3303 mode = voices[voice].volume_irq_mode;
3304 voices[voice].volume_irq_mode = 0;
3305 parm = voices[voice].volume_irq_parm;
3307 switch (mode)
3309 case VMODE_HALT: /* Decay phase finished */
3310 if (iw_mode)
3311 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3312 restore_flags(flags);
3313 gus_voice_init(voice);
3314 break;
3316 case VMODE_ENVELOPE:
3317 gus_rampoff();
3318 restore_flags(flags);
3319 step_envelope(voice);
3320 break;
3322 case VMODE_START_NOTE:
3323 restore_flags(flags);
3324 guswave_start_note2(voices[voice].dev_pending, voice,
3325 voices[voice].note_pending, voices[voice].volume_pending);
3326 if (voices[voice].kill_pending)
3327 guswave_kill_note(voices[voice].dev_pending, voice,
3328 voices[voice].note_pending, 0);
3330 if (voices[voice].sample_pending >= 0)
3332 guswave_set_instr(voices[voice].dev_pending, voice,
3333 voices[voice].sample_pending);
3334 voices[voice].sample_pending = -1;
3336 break;
3338 default:
3339 restore_flags(flags);
3341 restore_flags(flags);
3344 void gus_voice_irq(void)
3346 unsigned long wave_ignore = 0, volume_ignore = 0;
3347 unsigned long voice_bit;
3349 unsigned char src, voice;
3351 while (1)
3353 src = gus_read8(0x0f); /*
3354 * Get source info
3356 voice = src & 0x1f;
3357 src &= 0xc0;
3359 if (src == (0x80 | 0x40))
3360 return; /*
3361 * No interrupt
3364 voice_bit = 1 << voice;
3366 if (!(src & 0x80)) /*
3367 * Wave IRQ pending
3369 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3370 * Not done
3371 * yet
3374 wave_ignore |= voice_bit;
3375 do_loop_irq(voice);
3377 if (!(src & 0x40)) /*
3378 * Volume IRQ pending
3380 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3381 * Not done
3382 * yet
3385 volume_ignore |= voice_bit;
3386 do_volume_irq(voice);
3391 void guswave_dma_irq(void)
3393 unsigned char status;
3395 status = gus_look8(0x41); /* Get DMA IRQ Status */
3396 if (status & 0x40) /* DMA interrupt pending */
3397 switch (active_device)
3399 case GUS_DEV_WAVE:
3400 wake_up(&dram_sleeper);
3401 break;
3403 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3404 gus_write8(0x41, 0); /* Disable GF1 DMA */
3405 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3406 pcm_current_count,
3407 pcm_current_intrflag, 1);
3408 break;
3410 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3411 gus_write8(0x41, 0); /* Disable GF1 DMA */
3412 if (pcm_qlen < pcm_nblk)
3414 dma_active = 0;
3415 if (gus_busy)
3417 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3418 DMAbuf_outputintr(gus_devnum, 0);
3421 break;
3423 default:
3425 status = gus_look8(0x49); /*
3426 * Get Sampling IRQ Status
3428 if (status & 0x40) /*
3429 * Sampling Irq pending
3432 DMAbuf_inputintr(gus_devnum);
3436 #ifdef CONFIG_SEQUENCER
3439 * Timer stuff
3442 static volatile int select_addr, data_addr;
3443 static volatile int curr_timer = 0;
3445 void gus_timer_command(unsigned int addr, unsigned int val)
3447 int i;
3449 outb(((unsigned char) (addr & 0xff)), select_addr);
3451 for (i = 0; i < 2; i++)
3452 inb(select_addr);
3454 outb(((unsigned char) (val & 0xff)), data_addr);
3456 for (i = 0; i < 2; i++)
3457 inb(select_addr);
3460 static void arm_timer(int timer, unsigned int interval)
3462 curr_timer = timer;
3464 if (timer == 1)
3466 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3467 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3468 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3470 else
3472 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3473 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3474 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3477 gus_timer_enabled = 1;
3480 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3482 int timer_no, resolution;
3483 int divisor;
3485 if (usecs_per_tick > (256 * 80))
3487 timer_no = 2;
3488 resolution = 320; /* usec */
3490 else
3492 timer_no = 1;
3493 resolution = 80; /* usec */
3495 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3496 arm_timer(timer_no, divisor);
3498 return divisor * resolution;
3501 static void gus_tmr_disable(int dev)
3503 gus_write8(0x45, 0); /* Disable both timers */
3504 gus_timer_enabled = 0;
3507 static void gus_tmr_restart(int dev)
3509 if (curr_timer == 1)
3510 gus_write8(0x45, 0x04); /* Start timer 1 again */
3511 else
3512 gus_write8(0x45, 0x08); /* Start timer 2 again */
3513 gus_timer_enabled = 1;
3516 static struct sound_lowlev_timer gus_tmr =
3520 gus_tmr_start,
3521 gus_tmr_disable,
3522 gus_tmr_restart
3525 static void gus_tmr_install(int io_base)
3527 struct sound_lowlev_timer *tmr;
3529 select_addr = io_base;
3530 data_addr = io_base + 1;
3532 tmr = &gus_tmr;
3534 #ifdef THIS_GETS_FIXED
3535 sound_timer_init(&gus_tmr, "GUS");
3536 #endif
3538 #endif
3540 #endif