Import 2.2.0pre6
[davej-history.git] / drivers / sound / gus_wave.c
blob3b15a961602daac63c3cebd38fe430ac7c705648
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 struct wait_queue *dram_sleeper = NULL;
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,
148 44100, /* 14 */
149 41160, /* 15 */
150 38587, /* 16 */
151 36317, /* 17 */
152 34300, /* 18 */
153 32494, /* 19 */
154 30870, /* 20 */
155 29400, /* 21 */
156 28063, /* 22 */
157 26843, /* 23 */
158 25725, /* 24 */
159 24696, /* 25 */
160 23746, /* 26 */
161 22866, /* 27 */
162 22050, /* 28 */
163 21289, /* 29 */
164 20580, /* 30 */
165 19916, /* 31 */
166 19293 /* 32 */
169 static struct patch_info *samples = NULL;
170 static long sample_ptrs[MAX_SAMPLE + 1];
171 static int sample_map[32];
172 static int free_sample;
173 static int mixer_type = 0;
176 static int patch_table[MAX_PATCH];
177 static int patch_map[32];
179 static struct synth_info gus_info = {
180 "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
181 0, 16, 0, MAX_PATCH
184 static void gus_poke(long addr, unsigned char data);
185 static void compute_and_set_volume(int voice, int volume, int ramp_time);
186 extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
187 extern unsigned short gus_linear_vol(int vol, int mainvol);
188 static void compute_volume(int voice, int volume);
189 static void do_volume_irq(int voice);
190 static void set_input_volumes(void);
191 static void gus_tmr_install(int io_base);
193 #define INSTANT_RAMP -1 /* Instant change. No ramping */
194 #define FAST_RAMP 0 /* Fastest possible ramp */
196 static void reset_sample_memory(void)
198 int i;
200 for (i = 0; i <= MAX_SAMPLE; i++)
201 sample_ptrs[i] = -1;
202 for (i = 0; i < 32; i++)
203 sample_map[i] = -1;
204 for (i = 0; i < 32; i++)
205 patch_map[i] = -1;
207 gus_poke(0, 0); /* Put a silent sample to the beginning */
208 gus_poke(1, 0);
209 free_mem_ptr = 2;
211 free_sample = 0;
213 for (i = 0; i < MAX_PATCH; i++)
214 patch_table[i] = NOT_SAMPLE;
217 void gus_delay(void)
219 int i;
221 for (i = 0; i < 7; i++)
222 inb(u_DRAMIO);
225 static void gus_poke(long addr, unsigned char data)
226 { /* Writes a byte to the DRAM */
227 unsigned long flags;
229 save_flags(flags);
230 cli();
231 outb((0x43), u_Command);
232 outb((addr & 0xff), u_DataLo);
233 outb(((addr >> 8) & 0xff), u_DataHi);
235 outb((0x44), u_Command);
236 outb(((addr >> 16) & 0xff), u_DataHi);
237 outb((data), u_DRAMIO);
238 restore_flags(flags);
241 static unsigned char gus_peek(long addr)
242 { /* Reads a byte from the DRAM */
243 unsigned long flags;
244 unsigned char tmp;
246 save_flags(flags);
247 cli();
248 outb((0x43), u_Command);
249 outb((addr & 0xff), u_DataLo);
250 outb(((addr >> 8) & 0xff), u_DataHi);
252 outb((0x44), u_Command);
253 outb(((addr >> 16) & 0xff), u_DataHi);
254 tmp = inb(u_DRAMIO);
255 restore_flags(flags);
257 return tmp;
260 void gus_write8(int reg, unsigned int data)
261 { /* Writes to an indirect register (8 bit) */
262 unsigned long flags;
264 save_flags(flags);
265 cli();
267 outb((reg), u_Command);
268 outb(((unsigned char) (data & 0xff)), u_DataHi);
270 restore_flags(flags);
273 static unsigned char gus_read8(int reg)
275 /* Reads from an indirect register (8 bit). Offset 0x80. */
276 unsigned long flags;
277 unsigned char val;
279 save_flags(flags);
280 cli();
281 outb((reg | 0x80), u_Command);
282 val = inb(u_DataHi);
283 restore_flags(flags);
285 return val;
288 static unsigned char gus_look8(int reg)
290 /* Reads from an indirect register (8 bit). No additional offset. */
291 unsigned long flags;
292 unsigned char val;
294 save_flags(flags);
295 cli();
296 outb((reg), u_Command);
297 val = inb(u_DataHi);
298 restore_flags(flags);
300 return val;
303 static void gus_write16(int reg, unsigned int data)
305 /* Writes to an indirect register (16 bit) */
306 unsigned long flags;
308 save_flags(flags);
309 cli();
311 outb((reg), u_Command);
313 outb(((unsigned char) (data & 0xff)), u_DataLo);
314 outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
316 restore_flags(flags);
319 static unsigned short gus_read16(int reg)
321 /* Reads from an indirect register (16 bit). Offset 0x80. */
322 unsigned long flags;
323 unsigned char hi, lo;
325 save_flags(flags);
326 cli();
328 outb((reg | 0x80), u_Command);
330 lo = inb(u_DataLo);
331 hi = inb(u_DataHi);
333 restore_flags(flags);
335 return ((hi << 8) & 0xff00) | lo;
338 static unsigned short gus_look16(int reg)
340 /* Reads from an indirect register (16 bit). No additional offset. */
341 unsigned long flags;
342 unsigned char hi, lo;
344 save_flags(flags);
345 cli();
347 outb((reg), u_Command);
349 lo = inb(u_DataLo);
350 hi = inb(u_DataHi);
352 restore_flags(flags);
354 return ((hi << 8) & 0xff00) | lo;
357 static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
359 /* Writes an 24 bit memory address */
360 unsigned long hold_address;
361 unsigned long flags;
363 save_flags(flags);
364 cli();
365 if (is16bit)
367 if (iw_mode)
369 /* Interwave spesific address translations */
370 address >>= 1;
372 else
375 * Special processing required for 16 bit patches
378 hold_address = address;
379 address = address >> 1;
380 address &= 0x0001ffffL;
381 address |= (hold_address & 0x000c0000L);
384 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
385 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
386 + (frac << 5));
387 /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
388 gus_delay();
389 gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
390 gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
391 + (frac << 5));
392 restore_flags(flags);
395 static void gus_select_voice(int voice)
397 if (voice < 0 || voice > 31)
398 return;
399 outb((voice), u_Voice);
402 static void gus_select_max_voices(int nvoices)
404 if (iw_mode)
405 nvoices = 32;
406 if (nvoices < 14)
407 nvoices = 14;
408 if (nvoices > 32)
409 nvoices = 32;
411 voice_alloc->max_voice = nr_voices = nvoices;
412 gus_write8(0x0e, (nvoices - 1) | 0xc0);
415 static void gus_voice_on(unsigned int mode)
417 gus_write8(0x00, (unsigned char) (mode & 0xfc));
418 gus_delay();
419 gus_write8(0x00, (unsigned char) (mode & 0xfc));
422 static void gus_voice_off(void)
424 gus_write8(0x00, gus_read8(0x00) | 0x03);
427 static void gus_voice_mode(unsigned int m)
429 unsigned char mode = (unsigned char) (m & 0xff);
431 gus_write8(0x00, (gus_read8(0x00) & 0x03) |
432 (mode & 0xfc)); /* Don't touch last two bits */
433 gus_delay();
434 gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
437 static void gus_voice_freq(unsigned long freq)
439 unsigned long divisor = freq_div_table[nr_voices - 14];
440 unsigned short fc;
442 /* Interwave plays at 44100 Hz with any number of voices */
443 if (iw_mode)
444 fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
445 else
446 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
447 fc = fc << 1;
449 gus_write16(0x01, fc);
452 static void gus_voice_volume(unsigned int vol)
454 gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
455 gus_write16(0x09, (unsigned short) (vol << 4));
458 static void gus_voice_balance(unsigned int balance)
460 gus_write8(0x0c, (unsigned char) (balance & 0xff));
463 static void gus_ramp_range(unsigned int low, unsigned int high)
465 gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
466 gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
469 static void gus_ramp_rate(unsigned int scale, unsigned int rate)
471 gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
474 static void gus_rampon(unsigned int m)
476 unsigned char mode = (unsigned char) (m & 0xff);
478 gus_write8(0x0d, mode & 0xfc);
479 gus_delay();
480 gus_write8(0x0d, mode & 0xfc);
483 static void gus_ramp_mode(unsigned int m)
485 unsigned char mode = (unsigned char) (m & 0xff);
487 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
488 (mode & 0xfc)); /* Leave the last 2 bits alone */
489 gus_delay();
490 gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
493 static void gus_rampoff(void)
495 gus_write8(0x0d, 0x03);
498 static void gus_set_voice_pos(int voice, long position)
500 int sample_no;
502 if ((sample_no = sample_map[voice]) != -1) {
503 if (position < samples[sample_no].len) {
504 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
505 voices[voice].offset_pending = position;
506 else
507 gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
508 samples[sample_no].mode & WAVE_16_BITS);
513 static void gus_voice_init(int voice)
515 unsigned long flags;
517 save_flags(flags);
518 cli();
519 gus_select_voice(voice);
520 gus_voice_volume(0);
521 gus_voice_off();
522 gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
523 gus_write8(0x00, 0x03); /* Voice off */
524 gus_write8(0x0d, 0x03); /* Ramping off */
525 voice_alloc->map[voice] = 0;
526 voice_alloc->alloc_times[voice] = 0;
527 restore_flags(flags);
531 static void gus_voice_init2(int voice)
533 voices[voice].panning = 0;
534 voices[voice].mode = 0;
535 voices[voice].orig_freq = 20000;
536 voices[voice].current_freq = 20000;
537 voices[voice].bender = 0;
538 voices[voice].bender_range = 200;
539 voices[voice].initial_volume = 0;
540 voices[voice].current_volume = 0;
541 voices[voice].loop_irq_mode = 0;
542 voices[voice].loop_irq_parm = 0;
543 voices[voice].volume_irq_mode = 0;
544 voices[voice].volume_irq_parm = 0;
545 voices[voice].env_phase = 0;
546 voices[voice].main_vol = 127;
547 voices[voice].patch_vol = 127;
548 voices[voice].expression_vol = 127;
549 voices[voice].sample_pending = -1;
550 voices[voice].fixed_pitch = 0;
553 static void step_envelope(int voice)
555 unsigned vol, prev_vol, phase;
556 unsigned char rate;
557 long int flags;
559 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
561 save_flags(flags);
562 cli();
563 gus_select_voice(voice);
564 gus_rampoff();
565 restore_flags(flags);
566 return;
568 * Sustain phase begins. Continue envelope after receiving note off.
571 if (voices[voice].env_phase >= 5)
573 /* Envelope finished. Shoot the voice down */
574 gus_voice_init(voice);
575 return;
577 prev_vol = voices[voice].current_volume;
578 phase = ++voices[voice].env_phase;
579 compute_volume(voice, voices[voice].midi_volume);
580 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
581 rate = voices[voice].env_rate[phase];
583 save_flags(flags);
584 cli();
585 gus_select_voice(voice);
587 gus_voice_volume(prev_vol);
590 gus_write8(0x06, rate); /* Ramping rate */
592 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
594 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
596 restore_flags(flags);
597 step_envelope(voice); /* Continue the envelope on the next step */
598 return;
600 if (vol > prev_vol)
602 if (vol >= (4096 - 64))
603 vol = 4096 - 65;
604 gus_ramp_range(0, vol);
605 gus_rampon(0x20); /* Increasing volume, with IRQ */
607 else
609 if (vol <= 64)
610 vol = 65;
611 gus_ramp_range(vol, 4030);
612 gus_rampon(0x60); /* Decreasing volume, with IRQ */
614 voices[voice].current_volume = vol;
615 restore_flags(flags);
618 static void init_envelope(int voice)
620 voices[voice].env_phase = -1;
621 voices[voice].current_volume = 64;
623 step_envelope(voice);
626 static void start_release(int voice, long int flags)
628 if (gus_read8(0x00) & 0x03)
629 return; /* Voice already stopped */
631 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
633 voices[voice].current_volume = voices[voice].initial_volume =
634 gus_read16(0x09) >> 4; /* Get current volume */
636 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
637 gus_rampoff();
638 restore_flags(flags);
639 step_envelope(voice);
642 static void gus_voice_fade(int voice)
644 int instr_no = sample_map[voice], is16bits;
645 long int flags;
647 save_flags(flags);
648 cli();
649 gus_select_voice(voice);
651 if (instr_no < 0 || instr_no > MAX_SAMPLE)
653 gus_write8(0x00, 0x03); /* Hard stop */
654 voice_alloc->map[voice] = 0;
655 restore_flags(flags);
656 return;
658 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
660 if (voices[voice].mode & WAVE_ENVELOPES)
662 start_release(voice, flags);
663 restore_flags(flags);
664 return;
667 * Ramp the volume down but not too quickly.
669 if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
671 gus_voice_off();
672 gus_rampoff();
673 gus_voice_init(voice);
674 restore_flags(flags);
675 return;
677 gus_ramp_range(65, 4030);
678 gus_ramp_rate(2, 4);
679 gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
680 voices[voice].volume_irq_mode = VMODE_HALT;
681 restore_flags(flags);
684 static void gus_reset(void)
686 int i;
688 gus_select_max_voices(24);
689 volume_base = 3071;
690 volume_scale = 4;
691 volume_method = VOL_METHOD_ADAGIO;
693 for (i = 0; i < 32; i++)
695 gus_voice_init(i); /* Turn voice off */
696 gus_voice_init2(i);
700 static void gus_initialize(void)
702 unsigned long flags;
703 unsigned char dma_image, irq_image, tmp;
705 static unsigned char gus_irq_map[16] = {
706 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
709 static unsigned char gus_dma_map[8] = {
710 0, 1, 0, 2, 0, 3, 4, 5
713 save_flags(flags);
714 cli();
715 gus_write8(0x4c, 0); /* Reset GF1 */
716 gus_delay();
717 gus_delay();
719 gus_write8(0x4c, 1); /* Release Reset */
720 gus_delay();
721 gus_delay();
724 * Clear all interrupts
727 gus_write8(0x41, 0); /* DMA control */
728 gus_write8(0x45, 0); /* Timer control */
729 gus_write8(0x49, 0); /* Sample control */
731 gus_select_max_voices(24);
733 inb(u_Status); /* Touch the status register */
735 gus_look8(0x41); /* Clear any pending DMA IRQs */
736 gus_look8(0x49); /* Clear any pending sample IRQs */
737 gus_read8(0x0f); /* Clear pending IRQs */
739 gus_reset(); /* Resets all voices */
741 gus_look8(0x41); /* Clear any pending DMA IRQs */
742 gus_look8(0x49); /* Clear any pending sample IRQs */
743 gus_read8(0x0f); /* Clear pending IRQs */
745 gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
748 * Set up for Digital ASIC
751 outb((0x05), gus_base + 0x0f);
753 mix_image |= 0x02; /* Disable line out (for a moment) */
754 outb((mix_image), u_Mixer);
756 outb((0x00), u_IRQDMAControl);
758 outb((0x00), gus_base + 0x0f);
761 * Now set up the DMA and IRQ interface
763 * The GUS supports two IRQs and two DMAs.
765 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
766 * Adding this support requires significant changes to the dmabuf.c, dsp.c
767 * and audio.c also.
770 irq_image = 0;
771 tmp = gus_irq_map[gus_irq];
772 if (!gus_pnp_flag && !tmp)
773 printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
774 irq_image |= tmp;
775 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
777 dual_dma_mode = 1;
778 if (gus_dma2 == gus_dma || gus_dma2 == -1)
780 dual_dma_mode = 0;
781 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
783 tmp = gus_dma_map[gus_dma];
784 if (!tmp)
785 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
787 dma_image |= tmp;
789 else
791 /* Setup dual DMA channel mode for GUS MAX */
793 dma_image = gus_dma_map[gus_dma];
794 if (!dma_image)
795 printk(KERN_WARNING "Warning! GUS DMA not selected\n");
797 tmp = gus_dma_map[gus_dma2] << 3;
798 if (!tmp)
800 printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
801 tmp = 0x40; /* Combine DMA channels */
802 dual_dma_mode = 0;
804 dma_image |= tmp;
808 * For some reason the IRQ and DMA addresses must be written twice
812 * Doing it first time
815 outb((mix_image), u_Mixer); /* Select DMA control */
816 outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
818 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
819 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
822 * Doing it second time
825 outb((mix_image), u_Mixer); /* Select DMA control */
826 outb((dma_image), u_IRQDMAControl); /* Set DMA address */
828 outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
829 outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
831 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
833 mix_image &= ~0x02; /* Enable line out */
834 mix_image |= 0x08; /* Enable IRQ */
835 outb((mix_image), u_Mixer); /*
836 * Turn mixer channels on
837 * Note! Mic in is left off.
840 gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
842 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
844 inb(u_Status); /* Touch the status register */
846 gus_look8(0x41); /* Clear any pending DMA IRQs */
847 gus_look8(0x49); /* Clear any pending sample IRQs */
849 gus_read8(0x0f); /* Clear pending IRQs */
851 if (iw_mode)
852 gus_write8(0x19, gus_read8(0x19) | 0x01);
853 restore_flags(flags);
857 static void pnp_mem_init(void)
859 #include "iwmem.h"
860 #define CHUNK_SIZE (256*1024)
861 #define BANK_SIZE (4*1024*1024)
862 #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
864 int bank, chunk, addr, total = 0;
865 int bank_sizes[4];
866 int i, j, bits = -1, testbits = -1, nbanks = 0;
869 * This routine determines what kind of RAM is installed in each of the four
870 * SIMM banks and configures the DRAM address decode logic accordingly.
874 * Place the chip into enhanced mode
876 gus_write8(0x19, gus_read8(0x19) | 0x01);
877 gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
880 * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
883 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
886 * Perform the DRAM size detection for each bank individually.
888 for (bank = 0; bank < 4; bank++)
890 int size = 0;
892 addr = bank * BANK_SIZE;
894 /* Clean check points of each chunk */
895 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
897 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
898 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
901 /* Write a value to each chunk point and verify the result */
902 for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
904 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
905 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
907 if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
908 gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
910 /* OK. There is RAM. Now check for possible shadows */
911 int ok = 1, chunk2;
913 for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
914 if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
915 gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
916 ok = 0; /* Addressing wraps */
918 if (ok)
919 size = (chunk + 1) * CHUNK_SIZE;
921 gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
922 gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
924 bank_sizes[bank] = size;
925 if (size)
926 nbanks = bank + 1;
927 DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
930 if (nbanks == 0) /* No RAM - Give up */
932 printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
933 printk(KERN_ERR "Sound: Unable to work with this card.\n");
934 gus_write8(0x19, gus_read8(0x19) & ~0x01);
935 gus_mem_size = 0;
936 return;
940 * Now we know how much DRAM there is in each bank. The next step is
941 * to find a DRAM size encoding (0 to 12) which is best for the combination
942 * we have.
944 * First try if any of the possible alternatives matches exactly the amount
945 * of memory we have.
948 for (i = 0; bits == -1 && i < 13; i++)
950 bits = i;
952 for (j = 0; bits != -1 && j < 4; j++)
953 if (mem_decode[i][j] != bank_sizes[j])
954 bits = -1; /* No hit */
958 * If necessary, try to find a combination where other than the last
959 * bank matches our configuration and the last bank is left oversized.
960 * In this way we don't leave holes in the middle of memory.
963 if (bits == -1) /* No luck yet */
965 for (i = 0; bits == -1 && i < 13; i++)
967 bits = i;
969 for (j = 0; bits != -1 && j < nbanks - 1; j++)
970 if (mem_decode[i][j] != bank_sizes[j])
971 bits = -1; /* No hit */
972 if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
973 bits = -1; /* The last bank is too small */
977 * The last resort is to search for a combination where the banks are
978 * smaller than the actual SIMMs. This leaves some memory in the banks
979 * unused but doesn't leave holes in the DRAM address space.
981 if (bits == -1) /* No luck yet */
983 for (i = 0; i < 13; i++)
985 testbits = i;
986 for (j = 0; testbits != -1 && j < nbanks - 1; j++)
987 if (mem_decode[i][j] > bank_sizes[j]) {
988 testbits = -1;
990 if(testbits > bits) bits = testbits;
992 if (bits != -1)
994 printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
995 printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
997 printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
998 printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
999 bits = 0;
1001 DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
1003 for (bank = 0; bank < 4; bank++)
1005 DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
1007 if (bank_sizes[bank] > mem_decode[bits][bank])
1008 total += mem_decode[bits][bank];
1009 else
1010 total += bank_sizes[bank];
1013 DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
1016 * Set the memory addressing mode.
1018 gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
1020 /* Leave the chip into enhanced mode. Disable LFO */
1021 gus_mem_size = total;
1022 iw_mode = 1;
1023 gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
1026 int gus_wave_detect(int baseaddr)
1028 unsigned long i, max_mem = 1024L;
1029 unsigned long loc;
1030 unsigned char val;
1032 gus_base = baseaddr;
1034 gus_write8(0x4c, 0); /* Reset GF1 */
1035 gus_delay();
1036 gus_delay();
1038 gus_write8(0x4c, 1); /* Release Reset */
1039 gus_delay();
1040 gus_delay();
1042 #ifdef GUSPNP_AUTODETECT
1043 val = gus_look8(0x5b); /* Version number register */
1044 gus_write8(0x5b, ~val); /* Invert all bits */
1046 if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
1048 if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
1050 DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
1051 gus_pnp_flag = 1;
1053 else
1055 DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
1056 gus_pnp_flag = 0;
1059 gus_write8(0x5b, val); /* Restore all bits */
1060 #endif
1062 if (gus_pnp_flag)
1063 pnp_mem_init();
1064 if (iw_mode)
1065 return 1;
1067 /* See if there is first block there.... */
1068 gus_poke(0L, 0xaa);
1069 if (gus_peek(0L) != 0xaa)
1070 return (0);
1072 /* Now zero it out so that I can check for mirroring .. */
1073 gus_poke(0L, 0x00);
1074 for (i = 1L; i < max_mem; i++)
1076 int n, failed;
1078 /* check for mirroring ... */
1079 if (gus_peek(0L) != 0)
1080 break;
1081 loc = i << 10;
1083 for (n = loc - 1, failed = 0; n <= loc; n++)
1085 gus_poke(loc, 0xaa);
1086 if (gus_peek(loc) != 0xaa)
1087 failed = 1;
1088 gus_poke(loc, 0x55);
1089 if (gus_peek(loc) != 0x55)
1090 failed = 1;
1092 if (failed)
1093 break;
1095 gus_mem_size = i << 10;
1096 return 1;
1099 static int guswave_ioctl(int dev, unsigned int cmd, caddr_t arg)
1102 switch (cmd)
1104 case SNDCTL_SYNTH_INFO:
1105 gus_info.nr_voices = nr_voices;
1106 if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
1107 return -EFAULT;
1108 return 0;
1110 case SNDCTL_SEQ_RESETSAMPLES:
1111 reset_sample_memory();
1112 return 0;
1114 case SNDCTL_SEQ_PERCMODE:
1115 return 0;
1117 case SNDCTL_SYNTH_MEMAVL:
1118 return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
1120 default:
1121 return -EINVAL;
1125 static int guswave_set_instr(int dev, int voice, int instr_no)
1127 int sample_no;
1129 if (instr_no < 0 || instr_no > MAX_PATCH)
1130 instr_no = 0; /* Default to acoustic piano */
1132 if (voice < 0 || voice > 31)
1133 return -EINVAL;
1135 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1137 voices[voice].sample_pending = instr_no;
1138 return 0;
1140 sample_no = patch_table[instr_no];
1141 patch_map[voice] = -1;
1143 if (sample_no == NOT_SAMPLE)
1145 /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
1146 return -EINVAL; /* Patch not defined */
1148 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
1150 /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
1151 return -EINVAL;
1153 sample_map[voice] = sample_no;
1154 patch_map[voice] = instr_no;
1155 return 0;
1158 static int guswave_kill_note(int dev, int voice, int note, int velocity)
1160 unsigned long flags;
1162 save_flags(flags);
1163 cli();
1164 /* voice_alloc->map[voice] = 0xffff; */
1165 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1167 voices[voice].kill_pending = 1;
1168 restore_flags(flags);
1170 else
1172 restore_flags(flags);
1173 gus_voice_fade(voice);
1176 return 0;
1179 static void guswave_aftertouch(int dev, int voice, int pressure)
1183 static void guswave_panning(int dev, int voice, int value)
1185 if (voice >= 0 || voice < 32)
1186 voices[voice].panning = value;
1189 static void guswave_volume_method(int dev, int mode)
1191 if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1192 volume_method = mode;
1195 static void compute_volume(int voice, int volume)
1197 if (volume < 128)
1198 voices[voice].midi_volume = volume;
1200 switch (volume_method)
1202 case VOL_METHOD_ADAGIO:
1203 voices[voice].initial_volume =
1204 gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
1205 voices[voice].expression_vol,
1206 voices[voice].patch_vol);
1207 break;
1209 case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
1210 voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
1211 break;
1213 default:
1214 voices[voice].initial_volume = volume_base +
1215 (voices[voice].midi_volume * volume_scale);
1218 if (voices[voice].initial_volume > 4030)
1219 voices[voice].initial_volume = 4030;
1222 static void compute_and_set_volume(int voice, int volume, int ramp_time)
1224 int curr, target, rate;
1225 unsigned long flags;
1227 compute_volume(voice, volume);
1228 voices[voice].current_volume = voices[voice].initial_volume;
1230 save_flags(flags);
1231 cli();
1233 * CAUTION! Interrupts disabled. Enable them before returning
1236 gus_select_voice(voice);
1238 curr = gus_read16(0x09) >> 4;
1239 target = voices[voice].initial_volume;
1241 if (ramp_time == INSTANT_RAMP)
1243 gus_rampoff();
1244 gus_voice_volume(target);
1245 restore_flags(flags);
1246 return;
1248 if (ramp_time == FAST_RAMP)
1249 rate = 63;
1250 else
1251 rate = 16;
1252 gus_ramp_rate(0, rate);
1254 if ((target - curr) / 64 == 0) /* Close enough to target. */
1256 gus_rampoff();
1257 gus_voice_volume(target);
1258 restore_flags(flags);
1259 return;
1261 if (target > curr)
1263 if (target > (4095 - 65))
1264 target = 4095 - 65;
1265 gus_ramp_range(curr, target);
1266 gus_rampon(0x00); /* Ramp up, once, no IRQ */
1268 else
1270 if (target < 65)
1271 target = 65;
1273 gus_ramp_range(target, curr);
1274 gus_rampon(0x40); /* Ramp down, once, no irq */
1276 restore_flags(flags);
1279 static void dynamic_volume_change(int voice)
1281 unsigned char status;
1282 unsigned long flags;
1284 save_flags(flags);
1285 cli();
1286 gus_select_voice(voice);
1287 status = gus_read8(0x00); /* Get voice status */
1288 restore_flags(flags);
1290 if (status & 0x03)
1291 return; /* Voice was not running */
1293 if (!(voices[voice].mode & WAVE_ENVELOPES))
1295 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1296 return;
1300 * Voice is running and has envelopes.
1303 save_flags(flags);
1304 cli();
1305 gus_select_voice(voice);
1306 status = gus_read8(0x0d); /* Ramping status */
1307 restore_flags(flags);
1309 if (status & 0x03) /* Sustain phase? */
1311 compute_and_set_volume(voice, voices[voice].midi_volume, 1);
1312 return;
1314 if (voices[voice].env_phase < 0)
1315 return;
1317 compute_volume(voice, voices[voice].midi_volume);
1321 static void guswave_controller(int dev, int voice, int ctrl_num, int value)
1323 unsigned long flags;
1324 unsigned long freq;
1326 if (voice < 0 || voice > 31)
1327 return;
1329 switch (ctrl_num)
1331 case CTRL_PITCH_BENDER:
1332 voices[voice].bender = value;
1334 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1336 freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
1337 voices[voice].current_freq = freq;
1339 save_flags(flags);
1340 cli();
1341 gus_select_voice(voice);
1342 gus_voice_freq(freq);
1343 restore_flags(flags);
1345 break;
1347 case CTRL_PITCH_BENDER_RANGE:
1348 voices[voice].bender_range = value;
1349 break;
1350 case CTL_EXPRESSION:
1351 value /= 128;
1352 case CTRL_EXPRESSION:
1353 if (volume_method == VOL_METHOD_ADAGIO)
1355 voices[voice].expression_vol = value;
1356 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1357 dynamic_volume_change(voice);
1359 break;
1361 case CTL_PAN:
1362 voices[voice].panning = (value * 2) - 128;
1363 break;
1365 case CTL_MAIN_VOLUME:
1366 value = (value * 100) / 16383;
1368 case CTRL_MAIN_VOLUME:
1369 voices[voice].main_vol = value;
1370 if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1371 dynamic_volume_change(voice);
1372 break;
1374 default:
1375 break;
1379 static int guswave_start_note2(int dev, int voice, int note_num, int volume)
1381 int sample, best_sample, best_delta, delta_freq;
1382 int is16bits, samplep, patch, pan;
1383 unsigned long note_freq, base_note, freq, flags;
1384 unsigned char mode = 0;
1386 if (voice < 0 || voice > 31)
1388 /* printk("GUS: Invalid voice\n");*/
1389 return -EINVAL;
1391 if (note_num == 255)
1393 if (voices[voice].mode & WAVE_ENVELOPES)
1395 voices[voice].midi_volume = volume;
1396 dynamic_volume_change(voice);
1397 return 0;
1399 compute_and_set_volume(voice, volume, 1);
1400 return 0;
1402 if ((patch = patch_map[voice]) == -1)
1403 return -EINVAL;
1404 if ((samplep = patch_table[patch]) == NOT_SAMPLE)
1406 return -EINVAL;
1408 note_freq = note_to_freq(note_num);
1411 * Find a sample within a patch so that the note_freq is between low_note
1412 * and high_note.
1414 sample = -1;
1416 best_sample = samplep;
1417 best_delta = 1000000;
1418 while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
1420 delta_freq = note_freq - samples[samplep].base_note;
1421 if (delta_freq < 0)
1422 delta_freq = -delta_freq;
1423 if (delta_freq < best_delta)
1425 best_sample = samplep;
1426 best_delta = delta_freq;
1428 if (samples[samplep].low_note <= note_freq &&
1429 note_freq <= samples[samplep].high_note)
1431 sample = samplep;
1433 else
1434 samplep = samples[samplep].key; /* Link to next sample */
1436 if (sample == -1)
1437 sample = best_sample;
1439 if (sample == -1)
1441 /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
1442 return 0; /* Should play default patch ??? */
1444 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1445 voices[voice].mode = samples[sample].mode;
1446 voices[voice].patch_vol = samples[sample].volume;
1448 if (iw_mode)
1449 gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
1451 if (voices[voice].mode & WAVE_ENVELOPES)
1453 int i;
1455 for (i = 0; i < 6; i++)
1457 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1458 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1461 sample_map[voice] = sample;
1463 if (voices[voice].fixed_pitch) /* Fixed pitch */
1465 freq = samples[sample].base_freq;
1467 else
1469 base_note = samples[sample].base_note / 100;
1470 note_freq /= 100;
1472 freq = samples[sample].base_freq * note_freq / base_note;
1475 voices[voice].orig_freq = freq;
1478 * Since the pitch bender may have been set before playing the note, we
1479 * have to calculate the bending now.
1482 freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
1483 voices[voice].bender_range, 0);
1484 voices[voice].current_freq = freq;
1486 pan = (samples[sample].panning + voices[voice].panning) / 32;
1487 pan += 7;
1488 if (pan < 0)
1489 pan = 0;
1490 if (pan > 15)
1491 pan = 15;
1493 if (samples[sample].mode & WAVE_16_BITS)
1495 mode |= 0x04; /* 16 bits */
1496 if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
1497 ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
1498 printk(KERN_ERR "GUS: Sample address error\n");
1500 /*************************************************************************
1501 * CAUTION! Interrupts disabled. Don't return before enabling
1502 *************************************************************************/
1504 save_flags(flags);
1505 cli();
1506 gus_select_voice(voice);
1507 gus_voice_off();
1508 gus_rampoff();
1510 restore_flags(flags);
1512 if (voices[voice].mode & WAVE_ENVELOPES)
1514 compute_volume(voice, volume);
1515 init_envelope(voice);
1517 else
1519 compute_and_set_volume(voice, volume, 0);
1522 save_flags(flags);
1523 cli();
1524 gus_select_voice(voice);
1526 if (samples[sample].mode & WAVE_LOOP_BACK)
1527 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
1528 voices[voice].offset_pending, 0, is16bits); /* start=end */
1529 else
1530 gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
1532 if (samples[sample].mode & WAVE_LOOPING)
1534 mode |= 0x08;
1536 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1537 mode |= 0x10;
1539 if (samples[sample].mode & WAVE_LOOP_BACK)
1541 gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
1542 voices[voice].offset_pending,
1543 (samples[sample].fractions >> 4) & 0x0f, is16bits);
1544 mode |= 0x40;
1546 gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
1547 samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
1548 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
1549 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1551 else
1553 mode |= 0x20; /* Loop IRQ at the end */
1554 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
1555 voices[voice].loop_irq_parm = 1;
1556 gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
1557 gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
1558 (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
1560 gus_voice_freq(freq);
1561 gus_voice_balance(pan);
1562 gus_voice_on(mode);
1563 restore_flags(flags);
1565 return 0;
1569 * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1570 * when the note playing on the voice is changed. It uses volume
1571 * ramping.
1574 static int guswave_start_note(int dev, int voice, int note_num, int volume)
1576 long int flags;
1577 int mode;
1578 int ret_val = 0;
1580 save_flags(flags);
1581 cli();
1582 if (note_num == 255)
1584 if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1586 voices[voice].volume_pending = volume;
1588 else
1590 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1593 else
1595 gus_select_voice(voice);
1596 mode = gus_read8(0x00);
1597 if (mode & 0x20)
1598 gus_write8(0x00, mode & 0xdf); /* No interrupt! */
1600 voices[voice].offset_pending = 0;
1601 voices[voice].kill_pending = 0;
1602 voices[voice].volume_irq_mode = 0;
1603 voices[voice].loop_irq_mode = 0;
1605 if (voices[voice].sample_pending >= 0)
1607 restore_flags(flags); /* Run temporarily with interrupts enabled */
1608 guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
1609 voices[voice].sample_pending = -1;
1610 save_flags(flags);
1611 cli();
1612 gus_select_voice(voice); /* Reselect the voice (just to be sure) */
1614 if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
1616 ret_val = guswave_start_note2(dev, voice, note_num, volume);
1618 else
1620 voices[voice].dev_pending = dev;
1621 voices[voice].note_pending = note_num;
1622 voices[voice].volume_pending = volume;
1623 voices[voice].volume_irq_mode = VMODE_START_NOTE;
1625 gus_rampoff();
1626 gus_ramp_range(2000, 4065);
1627 gus_ramp_rate(0, 63); /* Fastest possible rate */
1628 gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
1631 restore_flags(flags);
1632 return ret_val;
1635 static void guswave_reset(int dev)
1637 int i;
1639 for (i = 0; i < 32; i++)
1641 gus_voice_init(i);
1642 gus_voice_init2(i);
1646 static int guswave_open(int dev, int mode)
1648 int err;
1650 if (gus_busy)
1651 return -EBUSY;
1653 voice_alloc->timestamp = 0;
1655 if (gus_no_wave_dma) {
1656 gus_no_dma = 1;
1657 } else {
1658 if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
1660 /* printk( "GUS: Loading samples without DMA\n"); */
1661 gus_no_dma = 1; /* Upload samples using PIO */
1663 else
1664 gus_no_dma = 0;
1667 init_waitqueue(&dram_sleeper);
1668 gus_busy = 1;
1669 active_device = GUS_DEV_WAVE;
1671 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1672 gus_initialize();
1673 gus_reset();
1674 gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
1676 return 0;
1679 static void guswave_close(int dev)
1681 gus_busy = 0;
1682 active_device = 0;
1683 gus_reset();
1685 if (!gus_no_dma)
1686 DMAbuf_close_dma(gus_devnum);
1689 static int guswave_load_patch(int dev, int format, const char *addr,
1690 int offs, int count, int pmgr_flag)
1692 struct patch_info patch;
1693 int instr;
1694 long sizeof_patch;
1696 unsigned long blk_sz, blk_end, left, src_offs, target;
1698 sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
1700 if (format != GUS_PATCH)
1702 /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
1703 return -EINVAL;
1705 if (count < sizeof_patch)
1707 /* printk("GUS Error: Patch header too short\n");*/
1708 return -EINVAL;
1710 count -= sizeof_patch;
1712 if (free_sample >= MAX_SAMPLE)
1714 /* printk("GUS: Sample table full\n");*/
1715 return -ENOSPC;
1718 * Copy the header from user space but ignore the first bytes which have
1719 * been transferred already.
1722 copy_from_user(&((char *) &patch)[offs], &(addr)[offs], sizeof_patch - offs);
1724 if (patch.mode & WAVE_ROM)
1725 return -EINVAL;
1726 if (gus_mem_size == 0)
1727 return -ENOSPC;
1729 instr = patch.instr_no;
1731 if (instr < 0 || instr > MAX_PATCH)
1733 /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
1734 return -EINVAL;
1736 if (count < patch.len)
1738 /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
1739 patch.len = count;
1741 if (patch.len <= 0 || patch.len > gus_mem_size)
1743 /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
1744 return -EINVAL;
1746 if (patch.mode & WAVE_LOOPING)
1748 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1750 /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
1751 return -EINVAL;
1753 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1755 /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
1756 return -EINVAL;
1759 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
1761 if (patch.mode & WAVE_16_BITS)
1764 * 16 bit samples must fit one 256k bank.
1766 if (patch.len >= GUS_BANK_SIZE)
1768 /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
1769 return -ENOSPC;
1771 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1772 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1774 unsigned long tmp_mem =
1775 /* Align to 256K */
1776 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1778 if ((tmp_mem + patch.len) > gus_mem_size)
1779 return -ENOSPC;
1781 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1784 if ((free_mem_ptr + patch.len) > gus_mem_size)
1785 return -ENOSPC;
1787 sample_ptrs[free_sample] = free_mem_ptr;
1790 * Tremolo is not possible with envelopes
1793 if (patch.mode & WAVE_ENVELOPES)
1794 patch.mode &= ~WAVE_TREMOLO;
1796 if (!(patch.mode & WAVE_FRACTIONS))
1798 patch.fractions = 0;
1800 memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
1803 * Link this_one sample to the list of samples for patch 'instr'.
1806 samples[free_sample].key = patch_table[instr];
1807 patch_table[instr] = free_sample;
1810 * Use DMA to transfer the wave data to the DRAM
1813 left = patch.len;
1814 src_offs = 0;
1815 target = free_mem_ptr;
1817 while (left) /* Not completely transferred yet */
1819 blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
1820 if (blk_sz > left)
1821 blk_sz = left;
1824 * DMA cannot cross bank (256k) boundaries. Check for that.
1827 blk_end = target + blk_sz;
1829 if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
1831 /* Split the block */
1832 blk_end &= ~(GUS_BANK_SIZE - 1);
1833 blk_sz = blk_end - target;
1835 if (gus_no_dma)
1838 * For some reason the DMA is not possible. We have to use PIO.
1840 long i;
1841 unsigned char data;
1843 for (i = 0; i < blk_sz; i++)
1845 get_user(*(unsigned char *) &data, (unsigned char *) &((addr)[sizeof_patch + i]));
1846 if (patch.mode & WAVE_UNSIGNED)
1847 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1848 data ^= 0x80; /* Convert to signed */
1849 gus_poke(target + i, data);
1852 else
1854 unsigned long address, hold_address;
1855 unsigned char dma_command;
1856 unsigned long flags;
1858 if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
1860 printk(KERN_ERR "GUS: DMA buffer == NULL\n");
1861 return -ENOSPC;
1864 * OK, move now. First in and then out.
1867 copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf, &(addr)[sizeof_patch + src_offs], blk_sz);
1869 save_flags(flags);
1870 cli();
1871 /******** INTERRUPTS DISABLED NOW ********/
1872 gus_write8(0x41, 0); /* Disable GF1 DMA */
1873 DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
1874 blk_sz, DMA_MODE_WRITE);
1877 * Set the DRAM address for the wave data
1880 if (iw_mode)
1882 /* Different address translation in enhanced mode */
1884 unsigned char hi;
1886 if (gus_dma > 4)
1887 address = target >> 1; /* Convert to 16 bit word address */
1888 else
1889 address = target;
1891 hi = (unsigned char) ((address >> 16) & 0xf0);
1892 hi += (unsigned char) (address & 0x0f);
1894 gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
1895 gus_write8(0x50, hi);
1897 else
1899 address = target;
1900 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1902 hold_address = address;
1903 address = address >> 1;
1904 address &= 0x0001ffffL;
1905 address |= (hold_address & 0x000c0000L);
1907 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1911 * Start the DMA transfer
1914 dma_command = 0x21; /* IRQ enable, DMA start */
1915 if (patch.mode & WAVE_UNSIGNED)
1916 dma_command |= 0x80; /* Invert MSB */
1917 if (patch.mode & WAVE_16_BITS)
1918 dma_command |= 0x40; /* 16 bit _DATA_ */
1919 if (audio_devs[gus_devnum]->dmap_out->dma > 3)
1920 dma_command |= 0x04; /* 16 bit DMA _channel_ */
1922 gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
1925 * Sleep here until the DRAM DMA done interrupt is served
1927 active_device = GUS_DEV_WAVE;
1929 if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
1930 printk("GUS: DMA Transfer timed out\n");
1931 restore_flags(flags);
1935 * Now the next part
1938 left -= blk_sz;
1939 src_offs += blk_sz;
1940 target += blk_sz;
1942 gus_write8(0x41, 0); /* Stop DMA */
1945 free_mem_ptr += patch.len;
1946 free_sample++;
1947 return 0;
1950 static void guswave_hw_control(int dev, unsigned char *event_rec)
1952 int voice, cmd;
1953 unsigned short p1, p2;
1954 unsigned int plong;
1955 unsigned flags;
1957 cmd = event_rec[2];
1958 voice = event_rec[3];
1959 p1 = *(unsigned short *) &event_rec[4];
1960 p2 = *(unsigned short *) &event_rec[6];
1961 plong = *(unsigned int *) &event_rec[4];
1963 if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1964 (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1965 do_volume_irq(voice);
1967 switch (cmd)
1969 case _GUS_NUMVOICES:
1970 save_flags(flags);
1971 cli();
1972 gus_select_voice(voice);
1973 gus_select_max_voices(p1);
1974 restore_flags(flags);
1975 break;
1977 case _GUS_VOICESAMPLE:
1978 guswave_set_instr(dev, voice, p1);
1979 break;
1981 case _GUS_VOICEON:
1982 save_flags(flags);
1983 cli();
1984 gus_select_voice(voice);
1985 p1 &= ~0x20; /* Don't allow interrupts */
1986 gus_voice_on(p1);
1987 restore_flags(flags);
1988 break;
1990 case _GUS_VOICEOFF:
1991 save_flags(flags);
1992 cli();
1993 gus_select_voice(voice);
1994 gus_voice_off();
1995 restore_flags(flags);
1996 break;
1998 case _GUS_VOICEFADE:
1999 gus_voice_fade(voice);
2000 break;
2002 case _GUS_VOICEMODE:
2003 save_flags(flags);
2004 cli();
2005 gus_select_voice(voice);
2006 p1 &= ~0x20; /* Don't allow interrupts */
2007 gus_voice_mode(p1);
2008 restore_flags(flags);
2009 break;
2011 case _GUS_VOICEBALA:
2012 save_flags(flags);
2013 cli();
2014 gus_select_voice(voice);
2015 gus_voice_balance(p1);
2016 restore_flags(flags);
2017 break;
2019 case _GUS_VOICEFREQ:
2020 save_flags(flags);
2021 cli();
2022 gus_select_voice(voice);
2023 gus_voice_freq(plong);
2024 restore_flags(flags);
2025 break;
2027 case _GUS_VOICEVOL:
2028 save_flags(flags);
2029 cli();
2030 gus_select_voice(voice);
2031 gus_voice_volume(p1);
2032 restore_flags(flags);
2033 break;
2035 case _GUS_VOICEVOL2: /* Just update the software voice level */
2036 voices[voice].initial_volume = voices[voice].current_volume = p1;
2037 break;
2039 case _GUS_RAMPRANGE:
2040 if (voices[voice].mode & WAVE_ENVELOPES)
2041 break; /* NO-NO */
2042 save_flags(flags);
2043 cli();
2044 gus_select_voice(voice);
2045 gus_ramp_range(p1, p2);
2046 restore_flags(flags);
2047 break;
2049 case _GUS_RAMPRATE:
2050 if (voices[voice].mode & WAVE_ENVELOPES)
2051 break; /* NJET-NJET */
2052 save_flags(flags);
2053 cli();
2054 gus_select_voice(voice);
2055 gus_ramp_rate(p1, p2);
2056 restore_flags(flags);
2057 break;
2059 case _GUS_RAMPMODE:
2060 if (voices[voice].mode & WAVE_ENVELOPES)
2061 break; /* NO-NO */
2062 save_flags(flags);
2063 cli();
2064 gus_select_voice(voice);
2065 p1 &= ~0x20; /* Don't allow interrupts */
2066 gus_ramp_mode(p1);
2067 restore_flags(flags);
2068 break;
2070 case _GUS_RAMPON:
2071 if (voices[voice].mode & WAVE_ENVELOPES)
2072 break; /* EI-EI */
2073 save_flags(flags);
2074 cli();
2075 gus_select_voice(voice);
2076 p1 &= ~0x20; /* Don't allow interrupts */
2077 gus_rampon(p1);
2078 restore_flags(flags);
2079 break;
2081 case _GUS_RAMPOFF:
2082 if (voices[voice].mode & WAVE_ENVELOPES)
2083 break; /* NEJ-NEJ */
2084 save_flags(flags);
2085 cli();
2086 gus_select_voice(voice);
2087 gus_rampoff();
2088 restore_flags(flags);
2089 break;
2091 case _GUS_VOLUME_SCALE:
2092 volume_base = p1;
2093 volume_scale = p2;
2094 break;
2096 case _GUS_VOICE_POS:
2097 save_flags(flags);
2098 cli();
2099 gus_select_voice(voice);
2100 gus_set_voice_pos(voice, plong);
2101 restore_flags(flags);
2102 break;
2104 default:
2108 static int gus_audio_set_speed(int speed)
2110 if (speed <= 0)
2111 speed = gus_audio_speed;
2113 if (speed < 4000)
2114 speed = 4000;
2116 if (speed > 44100)
2117 speed = 44100;
2119 gus_audio_speed = speed;
2121 if (only_read_access)
2123 /* Compute nearest valid recording speed and return it */
2125 /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
2126 speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2127 speed = (9878400 / (speed * 16)) - 2;
2129 return speed;
2132 static int gus_audio_set_channels(int channels)
2134 if (!channels)
2135 return gus_audio_channels;
2136 if (channels > 2)
2137 channels = 2;
2138 if (channels < 1)
2139 channels = 1;
2140 gus_audio_channels = channels;
2141 return channels;
2144 static int gus_audio_set_bits(int bits)
2146 if (!bits)
2147 return gus_audio_bits;
2149 if (bits != 8 && bits != 16)
2150 bits = 8;
2152 if (only_8_bits)
2153 bits = 8;
2155 gus_audio_bits = bits;
2156 return bits;
2159 static int gus_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
2161 int val;
2163 switch (cmd)
2165 case SOUND_PCM_WRITE_RATE:
2166 if (get_user(val, (int *)arg))
2167 return -EFAULT;
2168 val = gus_audio_set_speed(val);
2169 break;
2171 case SOUND_PCM_READ_RATE:
2172 val = gus_audio_speed;
2173 break;
2175 case SNDCTL_DSP_STEREO:
2176 if (get_user(val, (int *)arg))
2177 return -EFAULT;
2178 val = gus_audio_set_channels(val + 1) - 1;
2179 break;
2181 case SOUND_PCM_WRITE_CHANNELS:
2182 if (get_user(val, (int *)arg))
2183 return -EFAULT;
2184 val = gus_audio_set_channels(val);
2185 break;
2187 case SOUND_PCM_READ_CHANNELS:
2188 val = gus_audio_channels;
2189 break;
2191 case SNDCTL_DSP_SETFMT:
2192 if (get_user(val, (int *)arg))
2193 return -EFAULT;
2194 val = gus_audio_set_bits(val);
2195 break;
2197 case SOUND_PCM_READ_BITS:
2198 val = gus_audio_bits;
2199 break;
2201 case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
2202 case SOUND_PCM_READ_FILTER:
2203 val = -EINVAL;
2204 break;
2205 default:
2206 return -EINVAL;
2208 return put_user(val, (int *)arg);
2211 static void gus_audio_reset(int dev)
2213 if (recording_active)
2215 gus_write8(0x49, 0x00); /* Halt recording */
2216 set_input_volumes();
2220 static int saved_iw_mode; /* A hack hack hack */
2222 static int gus_audio_open(int dev, int mode)
2224 if (gus_busy)
2225 return -EBUSY;
2227 if (gus_pnp_flag && mode & OPEN_READ)
2229 /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
2230 return -EIO;
2232 gus_initialize();
2234 gus_busy = 1;
2235 active_device = 0;
2237 gus_reset();
2238 reset_sample_memory();
2239 gus_select_max_voices(14);
2240 saved_iw_mode = iw_mode;
2241 if (iw_mode)
2243 /* There are some problems with audio in enhanced mode so disable it */
2244 gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
2245 iw_mode = 0;
2247 pcm_active = 0;
2248 dma_active = 0;
2249 pcm_opened = 1;
2250 if (mode & OPEN_READ)
2252 recording_active = 1;
2253 set_input_volumes();
2255 only_read_access = !(mode & OPEN_WRITE);
2256 only_8_bits = mode & OPEN_READ;
2257 if (only_8_bits)
2258 audio_devs[dev]->format_mask = AFMT_U8;
2259 else
2260 audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
2262 return 0;
2265 static void gus_audio_close(int dev)
2267 iw_mode = saved_iw_mode;
2268 gus_reset();
2269 gus_busy = 0;
2270 pcm_opened = 0;
2271 active_device = 0;
2273 if (recording_active)
2275 gus_write8(0x49, 0x00); /* Halt recording */
2276 set_input_volumes();
2278 recording_active = 0;
2281 static void gus_audio_update_volume(void)
2283 unsigned long flags;
2284 int voice;
2286 if (pcm_active && pcm_opened)
2287 for (voice = 0; voice < gus_audio_channels; voice++)
2289 save_flags(flags);
2290 cli();
2291 gus_select_voice(voice);
2292 gus_rampoff();
2293 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2294 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2295 restore_flags(flags);
2299 static void play_next_pcm_block(void)
2301 unsigned long flags;
2302 int speed = gus_audio_speed;
2303 int this_one, is16bits, chn;
2304 unsigned long dram_loc;
2305 unsigned char mode[2], ramp_mode[2];
2307 if (!pcm_qlen)
2308 return;
2310 this_one = pcm_head;
2312 for (chn = 0; chn < gus_audio_channels; chn++)
2314 mode[chn] = 0x00;
2315 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
2317 if (chn == 0)
2319 mode[chn] |= 0x20; /* Loop IRQ */
2320 voices[chn].loop_irq_mode = LMODE_PCM;
2322 if (gus_audio_bits != 8)
2324 is16bits = 1;
2325 mode[chn] |= 0x04; /* 16 bit data */
2327 else
2328 is16bits = 0;
2330 dram_loc = this_one * pcm_bsize;
2331 dram_loc += chn * pcm_banksize;
2333 if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
2335 mode[chn] |= 0x08; /* Enable loop */
2336 ramp_mode[chn] = 0x03; /* Disable rollover bit */
2338 else
2340 if (chn == 0)
2341 ramp_mode[chn] = 0x04; /* Enable rollover bit */
2343 save_flags(flags);
2344 cli();
2345 gus_select_voice(chn);
2346 gus_voice_freq(speed);
2348 if (gus_audio_channels == 1)
2349 gus_voice_balance(7); /* mono */
2350 else if (chn == 0)
2351 gus_voice_balance(0); /* left */
2352 else
2353 gus_voice_balance(15); /* right */
2355 if (!pcm_active) /* Playback not already active */
2358 * The playback was not started yet (or there has been a pause).
2359 * Start the voice (again) and ask for a rollover irq at the end of
2360 * this_one block. If this_one one is last of the buffers, use just
2361 * the normal loop with irq.
2364 gus_voice_off();
2365 gus_rampoff();
2366 gus_voice_volume(1530 + (25 * gus_pcm_volume));
2367 gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
2369 gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
2370 gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
2372 if (chn != 0)
2373 gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2374 0, is16bits); /* Loop end location */
2376 if (chn == 0)
2377 gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
2378 0, is16bits); /* Loop end location */
2379 else
2380 mode[chn] |= 0x08; /* Enable looping */
2381 restore_flags(flags);
2383 for (chn = 0; chn < gus_audio_channels; chn++)
2385 save_flags(flags);
2386 cli();
2387 gus_select_voice(chn);
2388 gus_write8(0x0d, ramp_mode[chn]);
2389 if (iw_mode)
2390 gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
2391 gus_voice_on(mode[chn]);
2392 restore_flags(flags);
2394 pcm_active = 1;
2397 static void gus_transfer_output_block(int dev, unsigned long buf,
2398 int total_count, int intrflag, int chn)
2401 * This routine transfers one block of audio data to the DRAM. In mono mode
2402 * it's called just once. When in stereo mode, this_one routine is called
2403 * once for both channels.
2405 * The left/mono channel data is transferred to the beginning of dram and the
2406 * right data to the area pointed by gus_page_size.
2409 int this_one, count;
2410 unsigned long flags;
2411 unsigned char dma_command;
2412 unsigned long address, hold_address;
2414 save_flags(flags);
2415 cli();
2417 count = total_count / gus_audio_channels;
2419 if (chn == 0)
2421 if (pcm_qlen >= pcm_nblk)
2422 printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
2424 this_one = pcm_current_block = pcm_tail;
2425 pcm_qlen++;
2426 pcm_tail = (pcm_tail + 1) % pcm_nblk;
2427 pcm_datasize[this_one] = count;
2429 else
2430 this_one = pcm_current_block;
2432 gus_write8(0x41, 0); /* Disable GF1 DMA */
2433 DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
2435 address = this_one * pcm_bsize;
2436 address += chn * pcm_banksize;
2438 if (audio_devs[dev]->dmap_out->dma > 3)
2440 hold_address = address;
2441 address = address >> 1;
2442 address &= 0x0001ffffL;
2443 address |= (hold_address & 0x000c0000L);
2445 gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
2447 dma_command = 0x21; /* IRQ enable, DMA start */
2449 if (gus_audio_bits != 8)
2450 dma_command |= 0x40; /* 16 bit _DATA_ */
2451 else
2452 dma_command |= 0x80; /* Invert MSB */
2454 if (audio_devs[dev]->dmap_out->dma > 3)
2455 dma_command |= 0x04; /* 16 bit DMA channel */
2457 gus_write8(0x41, dma_command); /* Kick start */
2459 if (chn == (gus_audio_channels - 1)) /* Last channel */
2462 * Last (right or mono) channel data
2464 dma_active = 1; /* DMA started. There is a unacknowledged buffer */
2465 active_device = GUS_DEV_PCM_DONE;
2466 if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
2468 play_next_pcm_block();
2471 else
2474 * Left channel data. The right channel
2475 * is transferred after DMA interrupt
2477 active_device = GUS_DEV_PCM_CONTINUE;
2480 restore_flags(flags);
2483 static void gus_uninterleave8(char *buf, int l)
2485 /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
2486 int i, p = 0, halfsize = l / 2;
2487 char *buf2 = buf + halfsize, *src = bounce_buf;
2489 memcpy(bounce_buf, buf, l);
2491 for (i = 0; i < halfsize; i++)
2493 buf[i] = src[p++]; /* Left channel */
2494 buf2[i] = src[p++]; /* Right channel */
2498 static void gus_uninterleave16(short *buf, int l)
2500 /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
2501 int i, p = 0, halfsize = l / 2;
2502 short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
2504 memcpy(bounce_buf, (char *) buf, l * 2);
2506 for (i = 0; i < halfsize; i++)
2508 buf[i] = src[p++]; /* Left channel */
2509 buf2[i] = src[p++]; /* Right channel */
2513 static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
2514 int intrflag)
2516 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
2518 dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2520 pcm_current_buf = buf;
2521 pcm_current_count = total_count;
2522 pcm_current_intrflag = intrflag;
2523 pcm_current_dev = dev;
2524 if (gus_audio_channels == 2)
2526 char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
2528 if (gus_audio_bits == 8)
2529 gus_uninterleave8(b, total_count);
2530 else
2531 gus_uninterleave16((short *) b, total_count / 2);
2533 gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
2536 static void gus_audio_start_input(int dev, unsigned long buf, int count,
2537 int intrflag)
2539 unsigned long flags;
2540 unsigned char mode;
2542 save_flags(flags);
2543 cli();
2545 DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
2546 mode = 0xa0; /* DMA IRQ enabled, invert MSB */
2548 if (audio_devs[dev]->dmap_in->dma > 3)
2549 mode |= 0x04; /* 16 bit DMA channel */
2550 if (gus_audio_channels > 1)
2551 mode |= 0x02; /* Stereo */
2552 mode |= 0x01; /* DMA enable */
2554 gus_write8(0x49, mode);
2555 restore_flags(flags);
2558 static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
2560 unsigned int rate;
2562 gus_audio_bsize = bsize;
2563 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
2564 rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
2566 gus_write8(0x48, rate & 0xff); /* Set sampling rate */
2568 if (gus_audio_bits != 8)
2570 /* printk("GUS Error: 16 bit recording not supported\n");*/
2571 return -EINVAL;
2573 return 0;
2576 static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
2578 int i;
2580 long mem_ptr, mem_size;
2582 audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
2583 mem_ptr = 0;
2584 mem_size = gus_mem_size / gus_audio_channels;
2586 if (mem_size > (256 * 1024))
2587 mem_size = 256 * 1024;
2589 pcm_bsize = bsize / gus_audio_channels;
2590 pcm_head = pcm_tail = pcm_qlen = 0;
2592 pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
2593 if ((pcm_bsize * pcm_nblk) > mem_size)
2594 pcm_nblk = mem_size / pcm_bsize;
2596 for (i = 0; i < pcm_nblk; i++)
2597 pcm_datasize[i] = 0;
2599 pcm_banksize = pcm_nblk * pcm_bsize;
2601 if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
2602 pcm_nblk--;
2603 gus_write8(0x41, 0); /* Disable GF1 DMA */
2604 return 0;
2607 static int gus_local_qlen(int dev)
2609 return pcm_qlen;
2613 static struct audio_driver gus_audio_driver =
2615 gus_audio_open,
2616 gus_audio_close,
2617 gus_audio_output_block,
2618 gus_audio_start_input,
2619 gus_audio_ioctl,
2620 gus_audio_prepare_for_input,
2621 gus_audio_prepare_for_output,
2622 gus_audio_reset,
2623 gus_local_qlen,
2624 NULL
2627 static void guswave_setup_voice(int dev, int voice, int chn)
2629 struct channel_info *info = &synth_devs[dev]->chn_info[chn];
2631 guswave_set_instr(dev, voice, info->pgm_num);
2632 voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
2633 voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
2634 voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
2635 voices[voice].bender = 0;
2636 voices[voice].bender_range = info->bender_range;
2638 if (chn == 9)
2639 voices[voice].fixed_pitch = 1;
2642 static void guswave_bender(int dev, int voice, int value)
2644 int freq;
2645 unsigned long flags;
2647 voices[voice].bender = value - 8192;
2648 freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
2649 voices[voice].current_freq = freq;
2651 save_flags(flags);
2652 cli();
2653 gus_select_voice(voice);
2654 gus_voice_freq(freq);
2655 restore_flags(flags);
2658 static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
2660 int i, p, best = -1, best_time = 0x7fffffff;
2662 p = alloc->ptr;
2664 * First look for a completely stopped voice
2667 for (i = 0; i < alloc->max_voice; i++)
2669 if (alloc->map[p] == 0)
2671 alloc->ptr = p;
2672 return p;
2674 if (alloc->alloc_times[p] < best_time)
2676 best = p;
2677 best_time = alloc->alloc_times[p];
2679 p = (p + 1) % alloc->max_voice;
2683 * Then look for a releasing voice
2686 for (i = 0; i < alloc->max_voice; i++)
2688 if (alloc->map[p] == 0xffff)
2690 alloc->ptr = p;
2691 return p;
2693 p = (p + 1) % alloc->max_voice;
2695 if (best >= 0)
2696 p = best;
2698 alloc->ptr = p;
2699 return p;
2702 static struct synth_operations guswave_operations =
2704 "GUS",
2705 &gus_info,
2707 SYNTH_TYPE_SAMPLE,
2708 SAMPLE_TYPE_GUS,
2709 guswave_open,
2710 guswave_close,
2711 guswave_ioctl,
2712 guswave_kill_note,
2713 guswave_start_note,
2714 guswave_set_instr,
2715 guswave_reset,
2716 guswave_hw_control,
2717 guswave_load_patch,
2718 guswave_aftertouch,
2719 guswave_controller,
2720 guswave_panning,
2721 guswave_volume_method,
2722 guswave_bender,
2723 guswave_alloc,
2724 guswave_setup_voice
2727 static void set_input_volumes(void)
2729 unsigned long flags;
2730 unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
2732 if (have_gus_max) /* Don't disturb GUS MAX */
2733 return;
2735 save_flags(flags);
2736 cli();
2739 * Enable channels having vol > 10%
2740 * Note! bit 0x01 means the line in DISABLED while 0x04 means
2741 * the mic in ENABLED.
2743 if (gus_line_vol > 10)
2744 mask &= ~0x01;
2745 if (gus_mic_vol > 10)
2746 mask |= 0x04;
2748 if (recording_active)
2751 * Disable channel, if not selected for recording
2753 if (!(gus_recmask & SOUND_MASK_LINE))
2754 mask |= 0x01;
2755 if (!(gus_recmask & SOUND_MASK_MIC))
2756 mask &= ~0x04;
2758 mix_image &= ~0x07;
2759 mix_image |= mask & 0x07;
2760 outb((mix_image), u_Mixer);
2762 restore_flags(flags);
2765 #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2766 SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2768 int gus_default_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
2770 int vol, val;
2772 if (((cmd >> 8) & 0xff) != 'M')
2773 return -EINVAL;
2775 if (!access_ok(VERIFY_WRITE, (int *)arg, sizeof(int)))
2776 return -EFAULT;
2778 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
2780 if (__get_user(val, (int *) arg))
2781 return -EFAULT;
2783 switch (cmd & 0xff)
2785 case SOUND_MIXER_RECSRC:
2786 gus_recmask = val & MIX_DEVS;
2787 if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2788 gus_recmask = SOUND_MASK_MIC;
2789 /* Note! Input volumes are updated during next open for recording */
2790 val = gus_recmask;
2791 break;
2793 case SOUND_MIXER_MIC:
2794 vol = val & 0xff;
2795 if (vol < 0)
2796 vol = 0;
2797 if (vol > 100)
2798 vol = 100;
2799 gus_mic_vol = vol;
2800 set_input_volumes();
2801 val = vol | (vol << 8);
2802 break;
2804 case SOUND_MIXER_LINE:
2805 vol = val & 0xff;
2806 if (vol < 0)
2807 vol = 0;
2808 if (vol > 100)
2809 vol = 100;
2810 gus_line_vol = vol;
2811 set_input_volumes();
2812 val = vol | (vol << 8);
2813 break;
2815 case SOUND_MIXER_PCM:
2816 gus_pcm_volume = val & 0xff;
2817 if (gus_pcm_volume < 0)
2818 gus_pcm_volume = 0;
2819 if (gus_pcm_volume > 100)
2820 gus_pcm_volume = 100;
2821 gus_audio_update_volume();
2822 val = gus_pcm_volume | (gus_pcm_volume << 8);
2823 break;
2825 case SOUND_MIXER_SYNTH:
2826 gus_wave_volume = val & 0xff;
2827 if (gus_wave_volume < 0)
2828 gus_wave_volume = 0;
2829 if (gus_wave_volume > 100)
2830 gus_wave_volume = 100;
2831 if (active_device == GUS_DEV_WAVE)
2833 int voice;
2834 for (voice = 0; voice < nr_voices; voice++)
2835 dynamic_volume_change(voice); /* Apply the new vol */
2837 val = gus_wave_volume | (gus_wave_volume << 8);
2838 break;
2840 default:
2841 return -EINVAL;
2844 else
2846 switch (cmd & 0xff)
2849 * Return parameters
2851 case SOUND_MIXER_RECSRC:
2852 val = gus_recmask;
2853 break;
2855 case SOUND_MIXER_DEVMASK:
2856 val = MIX_DEVS;
2857 break;
2859 case SOUND_MIXER_STEREODEVS:
2860 val = 0;
2861 break;
2863 case SOUND_MIXER_RECMASK:
2864 val = SOUND_MASK_MIC | SOUND_MASK_LINE;
2865 break;
2867 case SOUND_MIXER_CAPS:
2868 val = 0;
2869 break;
2871 case SOUND_MIXER_MIC:
2872 val = gus_mic_vol | (gus_mic_vol << 8);
2873 break;
2875 case SOUND_MIXER_LINE:
2876 val = gus_line_vol | (gus_line_vol << 8);
2877 break;
2879 case SOUND_MIXER_PCM:
2880 val = gus_pcm_volume | (gus_pcm_volume << 8);
2881 break;
2883 case SOUND_MIXER_SYNTH:
2884 val = gus_wave_volume | (gus_wave_volume << 8);
2885 break;
2887 default:
2888 return -EINVAL;
2891 return __put_user(val, (int *)arg);
2894 static struct mixer_operations gus_mixer_operations =
2896 "GUS",
2897 "Gravis Ultrasound",
2898 gus_default_mixer_ioctl
2901 static int gus_default_mixer_init(void)
2903 int n;
2905 if ((n = sound_alloc_mixerdev()) != -1)
2908 * Don't install if there is another
2909 * mixer
2911 mixer_devs[n] = &gus_mixer_operations;
2913 if (have_gus_max)
2916 * Enable all mixer channels on the GF1 side. Otherwise recording will
2917 * not be possible using GUS MAX.
2919 mix_image &= ~0x07;
2920 mix_image |= 0x04; /* All channels enabled */
2921 outb((mix_image), u_Mixer);
2923 return n;
2926 void gus_wave_init(struct address_info *hw_config)
2928 unsigned long flags;
2929 unsigned char val;
2930 char *model_num = "2.4";
2931 char tmp[64], tmp2[64];
2932 int gus_type = 0x24; /* 2.4 */
2934 int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
2935 int sdev;
2937 hw_config->slots[0] = -1; /* No wave */
2938 hw_config->slots[1] = -1; /* No ad1848 */
2939 hw_config->slots[4] = -1; /* No audio */
2940 hw_config->slots[5] = -1; /* No mixer */
2942 if (!gus_pnp_flag)
2944 if (irq < 0 || irq > 15)
2946 printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2947 return;
2951 if (dma < 0 || dma > 7 || dma == 4)
2953 printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
2954 return;
2956 gus_irq = irq;
2957 gus_dma = dma;
2958 gus_dma2 = dma2;
2959 gus_hw_config = hw_config;
2961 if (gus_dma2 == -1)
2962 gus_dma2 = dma;
2965 * Try to identify the GUS model.
2967 * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2970 save_flags(flags);
2971 cli();
2972 outb((0x20), gus_base + 0x0f);
2973 val = inb(gus_base + 0x0f);
2974 restore_flags(flags);
2976 if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
2978 int ad_flags = 0;
2980 if (gus_pnp_flag)
2981 ad_flags = 0x12345678; /* Interwave "magic" */
2983 * It has the digital ASIC so the card is at least v3.4.
2984 * Next try to detect the true model.
2987 if (gus_pnp_flag) /* Hack hack hack */
2988 val = 10;
2989 else
2990 val = inb(u_MixSelect);
2993 * Value 255 means pre-3.7 which don't have mixer.
2994 * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2995 * 10 and above is GUS MAX which has the CS4231 codec/mixer.
2999 if (val == 255 || val < 5)
3001 model_num = "3.4";
3002 gus_type = 0x34;
3004 else if (val < 10)
3006 model_num = "3.7";
3007 gus_type = 0x37;
3008 mixer_type = ICS2101;
3009 request_region(u_MixSelect, 1, "GUS mixer");
3011 else
3013 model_num = "MAX";
3014 gus_type = 0x40;
3015 mixer_type = CS4231;
3016 #ifdef CONFIG_GUSMAX
3018 unsigned char max_config = 0x40; /* Codec enable */
3020 if (gus_dma2 == -1)
3021 gus_dma2 = gus_dma;
3023 if (gus_dma > 3)
3024 max_config |= 0x10; /* 16 bit capture DMA */
3026 if (gus_dma2 > 3)
3027 max_config |= 0x20; /* 16 bit playback DMA */
3029 max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
3031 outb((max_config), gus_base + 0x106); /* UltraMax control */
3034 if (ad1848_detect(gus_base + 0x10c, &ad_flags, hw_config->osp))
3036 char *name = "GUS MAX";
3037 int old_num_mixers = num_mixers;
3039 if (gus_pnp_flag)
3040 name = "GUS PnP";
3042 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3043 gus_wave_volume = 90;
3044 have_gus_max = 1;
3045 if (hw_config->name)
3046 name = hw_config->name;
3048 hw_config->slots[1] = ad1848_init(name, gus_base + 0x10c,
3049 -irq, gus_dma2, /* Playback DMA */
3050 gus_dma, /* Capture DMA */
3051 1, /* Share DMA channels with GF1 */
3052 hw_config->osp);
3054 if (num_mixers > old_num_mixers)
3056 /* GUS has it's own mixer map */
3057 AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
3058 AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
3059 AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
3062 else
3063 printk(KERN_WARNING "GUS: No CS4231 ??");
3064 #else
3065 printk(KERN_ERR "GUS MAX found, but not compiled in\n");
3066 #endif
3069 else
3072 * ASIC not detected so the card must be 2.2 or 2.4.
3073 * There could still be the 16-bit/mixer daughter card.
3077 if (hw_config->name)
3079 strncpy(tmp, hw_config->name, 45);
3080 tmp[45] = 0;
3081 sprintf(tmp2, "%s (%dk)", tmp, (int) gus_mem_size / 1024);
3082 tmp2[sizeof(tmp2) - 1] = 0;
3084 else if (gus_pnp_flag)
3086 sprintf(tmp2, "Gravis UltraSound PnP (%dk)",
3087 (int) gus_mem_size / 1024);
3089 else
3090 sprintf(tmp2, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3093 samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
3094 if (samples == NULL)
3096 printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
3097 return;
3099 conf_printf(tmp2, hw_config);
3100 tmp2[sizeof(gus_info.name) - 1] = 0;
3101 strcpy(gus_info.name, tmp2);
3103 if ((sdev = sound_alloc_synthdev()) == -1)
3104 printk(KERN_WARNING "gus_init: Too many synthesizers\n");
3105 else
3107 voice_alloc = &guswave_operations.alloc;
3108 if (iw_mode)
3109 guswave_operations.id = "IWAVE";
3110 hw_config->slots[0] = sdev;
3111 synth_devs[sdev] = &guswave_operations;
3112 sequencer_init();
3113 #ifdef CONFIG_SEQUENCER
3114 gus_tmr_install(gus_base + 8);
3115 #endif
3118 reset_sample_memory();
3120 gus_initialize();
3122 if ((gus_mem_size > 0) & !gus_no_wave_dma)
3124 hw_config->slots[4] = -1;
3125 if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
3126 "Ultrasound",
3127 &gus_audio_driver,
3128 sizeof(struct audio_driver),
3129 NEEDS_RESTART |
3130 ((!iw_mode && dma2 != dma && dma2 != -1) ?
3131 DMA_DUPLEX : 0),
3132 AFMT_U8 | AFMT_S16_LE,
3133 NULL, dma, dma2)) < 0)
3135 return;
3138 hw_config->slots[4] = gus_devnum;
3139 audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
3140 audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
3141 audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
3142 audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
3146 * Mixer dependent initialization.
3149 switch (mixer_type)
3151 case ICS2101:
3152 gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3153 gus_wave_volume = 90;
3154 request_region(u_MixSelect, 1, "GUS mixer");
3155 hw_config->slots[5] = ics2101_mixer_init();
3156 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3157 return;
3159 case CS4231:
3160 /* Initialized elsewhere (ad1848.c) */
3161 default:
3162 hw_config->slots[5] = gus_default_mixer_init();
3163 audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
3164 return;
3168 void gus_wave_unload(struct address_info *hw_config)
3170 #ifdef CONFIG_GUSMAX
3171 if (have_gus_max)
3173 ad1848_unload(gus_base + 0x10c,
3174 -gus_irq,
3175 gus_dma2, /* Playback DMA */
3176 gus_dma, /* Capture DMA */
3177 1); /* Share DMA channels with GF1 */
3179 #endif
3181 if (mixer_type == ICS2101)
3183 release_region(u_MixSelect, 1);
3185 if (hw_config->slots[0] != -1)
3186 sound_unload_synthdev(hw_config->slots[0]);
3187 if (hw_config->slots[1] != -1)
3188 sound_unload_audiodev(hw_config->slots[1]);
3189 if (hw_config->slots[2] != -1)
3190 sound_unload_mididev(hw_config->slots[2]);
3191 if (hw_config->slots[4] != -1)
3192 sound_unload_audiodev(hw_config->slots[4]);
3193 if (hw_config->slots[5] != -1)
3194 sound_unload_mixerdev(hw_config->slots[5]);
3196 if(samples)
3197 vfree(samples);
3198 samples=NULL;
3201 static void do_loop_irq(int voice)
3203 unsigned char tmp;
3204 int mode, parm;
3205 unsigned long flags;
3207 save_flags(flags);
3208 cli();
3209 gus_select_voice(voice);
3211 tmp = gus_read8(0x00);
3212 tmp &= ~0x20; /*
3213 * Disable wave IRQ for this_one voice
3215 gus_write8(0x00, tmp);
3217 if (tmp & 0x03) /* Voice stopped */
3218 voice_alloc->map[voice] = 0;
3220 mode = voices[voice].loop_irq_mode;
3221 voices[voice].loop_irq_mode = 0;
3222 parm = voices[voice].loop_irq_parm;
3224 switch (mode)
3226 case LMODE_FINISH: /*
3227 * Final loop finished, shoot volume down
3230 if ((int) (gus_read16(0x09) >> 4) < 100) /*
3231 * Get current volume
3234 gus_voice_off();
3235 gus_rampoff();
3236 gus_voice_init(voice);
3237 break;
3239 gus_ramp_range(65, 4065);
3240 gus_ramp_rate(0, 63); /*
3241 * Fastest possible rate
3243 gus_rampon(0x20 | 0x40); /*
3244 * Ramp down, once, irq
3246 voices[voice].volume_irq_mode = VMODE_HALT;
3247 break;
3249 case LMODE_PCM_STOP:
3250 pcm_active = 0; /* Signal to the play_next_pcm_block routine */
3251 case LMODE_PCM:
3253 pcm_qlen--;
3254 pcm_head = (pcm_head + 1) % pcm_nblk;
3255 if (pcm_qlen && pcm_active)
3257 play_next_pcm_block();
3259 else
3261 /* Underrun. Just stop the voice */
3262 gus_select_voice(0); /* Left channel */
3263 gus_voice_off();
3264 gus_rampoff();
3265 gus_select_voice(1); /* Right channel */
3266 gus_voice_off();
3267 gus_rampoff();
3268 pcm_active = 0;
3272 * If the queue was full before this interrupt, the DMA transfer was
3273 * suspended. Let it continue now.
3276 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3277 DMAbuf_outputintr(gus_devnum, 0);
3279 break;
3281 default:
3283 restore_flags(flags);
3286 static void do_volume_irq(int voice)
3288 unsigned char tmp;
3289 int mode, parm;
3290 unsigned long flags;
3292 save_flags(flags);
3293 cli();
3295 gus_select_voice(voice);
3296 tmp = gus_read8(0x0d);
3297 tmp &= ~0x20; /*
3298 * Disable volume ramp IRQ
3300 gus_write8(0x0d, tmp);
3302 mode = voices[voice].volume_irq_mode;
3303 voices[voice].volume_irq_mode = 0;
3304 parm = voices[voice].volume_irq_parm;
3306 switch (mode)
3308 case VMODE_HALT: /* Decay phase finished */
3309 if (iw_mode)
3310 gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
3311 restore_flags(flags);
3312 gus_voice_init(voice);
3313 break;
3315 case VMODE_ENVELOPE:
3316 gus_rampoff();
3317 restore_flags(flags);
3318 step_envelope(voice);
3319 break;
3321 case VMODE_START_NOTE:
3322 restore_flags(flags);
3323 guswave_start_note2(voices[voice].dev_pending, voice,
3324 voices[voice].note_pending, voices[voice].volume_pending);
3325 if (voices[voice].kill_pending)
3326 guswave_kill_note(voices[voice].dev_pending, voice,
3327 voices[voice].note_pending, 0);
3329 if (voices[voice].sample_pending >= 0)
3331 guswave_set_instr(voices[voice].dev_pending, voice,
3332 voices[voice].sample_pending);
3333 voices[voice].sample_pending = -1;
3335 break;
3337 default:
3338 restore_flags(flags);
3340 restore_flags(flags);
3343 void gus_voice_irq(void)
3345 unsigned long wave_ignore = 0, volume_ignore = 0;
3346 unsigned long voice_bit;
3348 unsigned char src, voice;
3350 while (1)
3352 src = gus_read8(0x0f); /*
3353 * Get source info
3355 voice = src & 0x1f;
3356 src &= 0xc0;
3358 if (src == (0x80 | 0x40))
3359 return; /*
3360 * No interrupt
3363 voice_bit = 1 << voice;
3365 if (!(src & 0x80)) /*
3366 * Wave IRQ pending
3368 if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
3369 * Not done
3370 * yet
3373 wave_ignore |= voice_bit;
3374 do_loop_irq(voice);
3376 if (!(src & 0x40)) /*
3377 * Volume IRQ pending
3379 if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
3380 * Not done
3381 * yet
3384 volume_ignore |= voice_bit;
3385 do_volume_irq(voice);
3390 void guswave_dma_irq(void)
3392 unsigned char status;
3394 status = gus_look8(0x41); /* Get DMA IRQ Status */
3395 if (status & 0x40) /* DMA interrupt pending */
3396 switch (active_device)
3398 case GUS_DEV_WAVE:
3399 wake_up(&dram_sleeper);
3400 break;
3402 case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
3403 gus_write8(0x41, 0); /* Disable GF1 DMA */
3404 gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
3405 pcm_current_count,
3406 pcm_current_intrflag, 1);
3407 break;
3409 case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
3410 gus_write8(0x41, 0); /* Disable GF1 DMA */
3411 if (pcm_qlen < pcm_nblk)
3413 dma_active = 0;
3414 if (gus_busy)
3416 if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
3417 DMAbuf_outputintr(gus_devnum, 0);
3420 break;
3422 default:
3424 status = gus_look8(0x49); /*
3425 * Get Sampling IRQ Status
3427 if (status & 0x40) /*
3428 * Sampling Irq pending
3431 DMAbuf_inputintr(gus_devnum);
3435 #ifdef CONFIG_SEQUENCER
3438 * Timer stuff
3441 static volatile int select_addr, data_addr;
3442 static volatile int curr_timer = 0;
3444 void gus_timer_command(unsigned int addr, unsigned int val)
3446 int i;
3448 outb(((unsigned char) (addr & 0xff)), select_addr);
3450 for (i = 0; i < 2; i++)
3451 inb(select_addr);
3453 outb(((unsigned char) (val & 0xff)), data_addr);
3455 for (i = 0; i < 2; i++)
3456 inb(select_addr);
3459 static void arm_timer(int timer, unsigned int interval)
3461 curr_timer = timer;
3463 if (timer == 1)
3465 gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
3466 gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
3467 gus_timer_command(0x04, 0x01); /* Start timer 1 */
3469 else
3471 gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
3472 gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
3473 gus_timer_command(0x04, 0x02); /* Start timer 2 */
3476 gus_timer_enabled = 1;
3479 static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
3481 int timer_no, resolution;
3482 int divisor;
3484 if (usecs_per_tick > (256 * 80))
3486 timer_no = 2;
3487 resolution = 320; /* usec */
3489 else
3491 timer_no = 1;
3492 resolution = 80; /* usec */
3494 divisor = (usecs_per_tick + (resolution / 2)) / resolution;
3495 arm_timer(timer_no, divisor);
3497 return divisor * resolution;
3500 static void gus_tmr_disable(int dev)
3502 gus_write8(0x45, 0); /* Disable both timers */
3503 gus_timer_enabled = 0;
3506 static void gus_tmr_restart(int dev)
3508 if (curr_timer == 1)
3509 gus_write8(0x45, 0x04); /* Start timer 1 again */
3510 else
3511 gus_write8(0x45, 0x08); /* Start timer 2 again */
3512 gus_timer_enabled = 1;
3515 static struct sound_lowlev_timer gus_tmr =
3519 gus_tmr_start,
3520 gus_tmr_disable,
3521 gus_tmr_restart
3524 static void gus_tmr_install(int io_base)
3526 struct sound_lowlev_timer *tmr;
3528 select_addr = io_base;
3529 data_addr = io_base + 1;
3531 tmr = &gus_tmr;
3533 #ifdef THIS_GETS_FIXED
3534 sound_timer_init(&gus_tmr, "GUS");
3535 #endif
3537 #endif
3539 #endif