3 * linux/kernel/chr_drv/sound/gus_wave.c
5 * Driver for the Gravis UltraSound wave table synth.
7 * (C) 1993 Hannu Savolainen (hsavolai@cs.helsinki.fi) See COPYING for further
8 * details. Should be distributed with this file.
11 /* #define GUS_LINEAR_VOLUME */
13 #include "sound_config.h"
14 #include "ultrasound.h"
17 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
19 #define MAX_SAMPLE 256
24 unsigned long orig_freq;
25 unsigned long current_freq;
31 unsigned int initial_volume;
32 unsigned int current_volume;
33 int loop_irq_mode, loop_irq_parm;
34 #define LMODE_FINISH 1
36 #define LMODE_PCM_STOP 3
37 int volume_irq_mode, volume_irq_parm;
39 #define VMODE_ENVELOPE 2
42 unsigned char env_rate[6];
43 unsigned char env_offset[6];
46 * Volume computation parameters for gus_adagio_vol()
48 int main_vol, expression_vol, patch_vol;
53 extern int gus_irq, gus_dma;
54 extern char *snd_raw_buf[MAX_DSP_DEV][DSP_BUFFCOUNT];
55 extern unsigned long snd_raw_buf_phys[MAX_DSP_DEV][DSP_BUFFCOUNT];
56 extern int snd_raw_count[MAX_DSP_DEV];
57 static long gus_mem_size = 0;
58 static long free_mem_ptr = 0;
59 static int gus_busy = 0;
60 static int nr_voices = 0; /* Number of currently allowed voices */
61 static int gus_devnum = 0;
62 static int volume_base, volume_scale, volume_method;
64 #define VOL_METHOD_ADAGIO 1
65 int gus_wave_volume = 60; /* Master wolume for wave (0 to 100) */
66 static unsigned char mix_image = 0x00;
69 * Current version of this_one driver doesn't allow synth and PCM functions
70 * at the same time. The active_device specifies the active driver
72 static int active_device = 0;
74 #define GUS_DEV_WAVE 1 /* Wave table synth */
75 #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
76 #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer the second
79 static int gus_sampling_speed;
80 static int gus_sampling_channels;
81 static int gus_sampling_bits;
83 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
86 * Variables and buffers for PCM output
88 #define MAX_PCM_BUFFERS 32 /* Don't change */
89 static int pcm_bsize, /* Current blocksize */
90 pcm_nblk, /* Current # of blocks */
91 pcm_banksize; /* # bytes allocated for channels */
92 static int pcm_datasize[MAX_PCM_BUFFERS]; /* Actual # of bytes in blk */
93 static volatile int pcm_head, pcm_tail, pcm_qlen; /* DRAM queue */
94 static volatile int pcm_active;
95 static int pcm_current_dev;
96 static int pcm_current_block;
97 static unsigned long pcm_current_buf;
98 static int pcm_current_count;
99 static int pcm_current_intrflag;
101 struct voice_info voices[32];
103 static int freq_div_table[] =
126 static struct patch_info samples[MAX_SAMPLE + 1];
127 static long sample_ptrs[MAX_SAMPLE + 1];
128 static int sample_map[32];
129 static int free_sample;
132 static int patch_table[MAX_PATCH];
133 static int patch_map[32];
135 static struct synth_info gus_info =
136 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
138 static void gus_poke (long addr, unsigned char data);
139 static void compute_and_set_volume (int voice, int volume, int ramp_time);
140 extern unsigned short gus_adagio_vol (int vel, int mainv, int xpn, int voicev);
141 static void compute_volume (int voice, int volume);
143 #define INSTANT_RAMP -1 /* Dont use ramping */
144 #define FAST_RAMP 0 /* Fastest possible ramp */
147 reset_sample_memory (void)
151 for (i = 0; i <= MAX_SAMPLE; i++)
153 for (i = 0; i < 32; i++)
155 for (i = 0; i < 32; i++)
158 gus_poke (0, 0); /* Put silence here */
164 for (i = 0; i < MAX_PATCH; i++)
173 for (i = 0; i < 7; i++)
178 gus_poke (long addr, unsigned char data)
182 DISABLE_INTR (flags);
183 OUTB (0x43, u_Command);
184 OUTB (addr & 0xff, u_DataLo);
185 OUTB ((addr >> 8) & 0xff, u_DataHi);
187 OUTB (0x44, u_Command);
188 OUTB ((addr >> 16) & 0xff, u_DataHi);
189 OUTB (data, u_DRAMIO);
190 RESTORE_INTR (flags);
199 DISABLE_INTR (flags);
200 OUTB (0x43, u_Command);
201 OUTB (addr & 0xff, u_DataLo);
202 OUTB ((addr >> 8) & 0xff, u_DataHi);
204 OUTB (0x44, u_Command);
205 OUTB ((addr >> 16) & 0xff, u_DataHi);
206 tmp = INB (u_DRAMIO);
207 RESTORE_INTR (flags);
213 gus_write8 (int reg, unsigned char data)
217 DISABLE_INTR (flags);
219 OUTB (reg, u_Command);
220 OUTB (data, u_DataHi);
222 RESTORE_INTR (flags);
231 DISABLE_INTR (flags);
232 OUTB (reg | 0x80, u_Command);
233 val = INB (u_DataHi);
234 RESTORE_INTR (flags);
245 DISABLE_INTR (flags);
246 OUTB (reg, u_Command);
247 val = INB (u_DataHi);
248 RESTORE_INTR (flags);
254 gus_write16 (int reg, unsigned short data)
258 DISABLE_INTR (flags);
260 OUTB (reg, u_Command);
262 OUTB (data & 0xff, u_DataLo);
263 OUTB ((data >> 8) & 0xff, u_DataHi);
265 RESTORE_INTR (flags);
272 unsigned char hi, lo;
274 DISABLE_INTR (flags);
276 OUTB (reg | 0x80, u_Command);
281 RESTORE_INTR (flags);
283 return ((hi << 8) & 0xff00) | lo;
287 gus_write_addr (int reg, unsigned long address, int is16bit)
289 unsigned long hold_address;
294 * Special processing required for 16 bit patches
297 hold_address = address;
298 address = address >> 1;
299 address &= 0x0001ffffL;
300 address |= (hold_address & 0x000c0000L);
303 gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
304 gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
308 gus_select_voice (int voice)
310 if (voice < 0 || voice > 31)
313 OUTB (voice, u_Voice);
317 gus_select_max_voices (int nvoices)
326 gus_write8 (0x0e, (nvoices - 1) | 0xc0);
330 gus_voice_on (unsigned char mode)
332 gus_write8 (0x00, mode & 0xfc);
334 gus_write8 (0x00, mode & 0xfc);
340 gus_write8 (0x00, gus_read8 (0x00) | 0x03);
344 gus_voice_mode (unsigned char mode)
346 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc)); /* Don't start or stop
349 gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
353 gus_voice_freq (unsigned long freq)
355 unsigned long divisor = freq_div_table[nr_voices - 14];
358 fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
361 gus_write16 (0x01, fc);
365 gus_voice_volume (unsigned short vol)
367 gus_write8 (0x0d, 0x03); /* Stop ramp before setting volume */
368 gus_write16 (0x09, vol << 4);
372 gus_voice_balance (unsigned char balance)
374 gus_write8 (0x0c, balance);
378 gus_ramp_range (unsigned short low, unsigned short high)
380 gus_write8 (0x07, (low >> 4) & 0xff);
381 gus_write8 (0x08, (high >> 4) & 0xff);
385 gus_ramp_rate (unsigned char scale, unsigned char rate)
387 gus_write8 (0x06, ((scale & 0x03) << 6) | (rate & 0x3f));
391 gus_rampon (unsigned char mode)
393 gus_write8 (0x0d, mode & 0xfc);
395 gus_write8 (0x0d, mode & 0xfc);
399 gus_ramp_mode (unsigned char mode)
401 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc)); /* Don't start or stop
404 gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
410 gus_write8 (0x0d, 0x03);
414 gus_voice_init (int voice)
418 DISABLE_INTR (flags);
419 gus_select_voice (voice);
420 gus_voice_volume (0);
421 gus_write_addr (0x0a, 0, 0); /* Set current position to 0 */
422 gus_write8 (0x00, 0x03); /* Voice off */
423 gus_write8 (0x0d, 0x03); /* Ramping off */
424 RESTORE_INTR (flags);
426 voices[voice].panning = 0;
427 voices[voice].mode = 0;
428 voices[voice].orig_freq = 20000;
429 voices[voice].current_freq = 20000;
430 voices[voice].bender = 0;
431 voices[voice].bender_range = 200;
432 voices[voice].initial_volume = 0;
433 voices[voice].current_volume = 0;
434 voices[voice].loop_irq_mode = 0;
435 voices[voice].loop_irq_parm = 0;
436 voices[voice].volume_irq_mode = 0;
437 voices[voice].volume_irq_parm = 0;
438 voices[voice].env_phase = 0;
439 voices[voice].main_vol = 127;
440 voices[voice].patch_vol = 127;
441 voices[voice].expression_vol = 127;
445 step_envelope (int voice)
447 unsigned vol, prev_vol, phase;
450 if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
453 return; /* Sustain */
456 if (voices[voice].env_phase >= 5)
459 * Shoot the voice off
462 gus_voice_init (voice);
466 prev_vol = voices[voice].current_volume;
467 gus_voice_volume (prev_vol);
468 phase = ++voices[voice].env_phase;
470 compute_volume (voice, voices[voice].midi_volume);
472 vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
473 rate = voices[voice].env_rate[phase];
474 gus_write8 (0x06, rate); /* Ramping rate */
476 voices[voice].volume_irq_mode = VMODE_ENVELOPE;
478 if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
480 step_envelope (voice); /* Continue with the next phase */
486 if (vol >= (4096 - 64))
488 gus_ramp_range (0, vol);
489 gus_rampon (0x20); /* Increasing, irq */
495 gus_ramp_range (vol, 4095);
496 gus_rampon (0x60); /* Decreasing, irq */
498 voices[voice].current_volume = vol;
502 init_envelope (int voice)
504 voices[voice].env_phase = -1;
505 voices[voice].current_volume = 64;
507 step_envelope (voice);
511 start_release (int voice)
513 if (gus_read8 (0x00) & 0x03)
514 return; /* Voice already stopped */
516 voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
518 voices[voice].current_volume =
519 voices[voice].initial_volume =
520 gus_read16 (0x09) >> 4; /* Get current volume */
522 voices[voice].mode &= ~WAVE_SUSTAIN_ON;
524 step_envelope (voice);
528 gus_voice_fade (int voice)
530 int instr_no = sample_map[voice], is16bits;
532 if (instr_no < 0 || instr_no > MAX_SAMPLE)
534 gus_write8 (0x00, 0x03); /* Hard stop */
538 is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bit samples */
540 if (voices[voice].mode & WAVE_ENVELOPES)
542 start_release (voice);
547 * Ramp the volume down but not too quickly.
549 if ((gus_read16 (0x09) >> 4) < 100) /* Get current volume */
553 gus_voice_init (voice);
557 gus_ramp_range (65, 4095);
558 gus_ramp_rate (2, 4);
559 gus_rampon (0x40 | 0x20); /* Down, once, irq */
560 voices[voice].volume_irq_mode = VMODE_HALT;
568 gus_select_max_voices (24);
571 volume_method = VOL_METHOD_ADAGIO;
573 for (i = 0; i < 32; i++)
575 gus_voice_init (i); /* Turn voice off */
578 INB (u_Status); /* Touch the status register */
580 gus_look8 (0x41); /* Clear any pending DMA IRQs */
581 gus_look8 (0x49); /* Clear any pending sample IRQs */
582 gus_read8 (0x0f); /* Clear pending IRQs */
587 gus_initialize (void)
590 unsigned char dma_image, irq_image, tmp;
592 static unsigned char gus_irq_map[16] =
593 {0, 0, 1, 3, 0, 2, 0, 4, 0, 0, 0, 5, 6, 0, 0, 7};
595 static unsigned char gus_dma_map[8] =
596 {0, 1, 0, 2, 0, 3, 4, 5};
598 DISABLE_INTR (flags);
600 gus_write8 (0x4c, 0); /* Reset GF1 */
604 gus_write8 (0x4c, 1); /* Release Reset */
609 * Clear all interrupts
612 gus_write8 (0x41, 0); /* DMA control */
613 gus_write8 (0x45, 0); /* Timer control */
614 gus_write8 (0x49, 0); /* Sample control */
616 gus_select_max_voices (24);
618 INB (u_Status); /* Touch the status register */
620 gus_look8 (0x41); /* Clear any pending DMA IRQs */
621 gus_look8 (0x49); /* Clear any pending sample IRQs */
622 gus_read8 (0x0f); /* Clear pending IRQs */
624 gus_reset (); /* Resets all voices */
626 gus_look8 (0x41); /* Clear any pending DMA IRQs */
627 gus_look8 (0x49); /* Clear any pending sample IRQs */
628 gus_read8 (0x0f); /* Clear pending IRQs */
630 gus_write8 (0x4c, 7); /* Master reset | DAC enable | IRQ enable */
633 * Set up for Digital ASIC
636 OUTB (0x05, gus_base + 0x0f);
638 mix_image |= 0x02; /* Disable line out */
639 OUTB (mix_image, u_Mixer);
641 OUTB (0x00, u_IRQDMAControl);
643 OUTB (0x00, gus_base + 0x0f);
646 * Now set up the DMA and IRQ interface
648 * The GUS supports two IRQs and two DMAs.
650 * If GUS_MIDI_IRQ is defined and if it's != GUS_IRQ, separate Midi IRQ is set
651 * up. Otherwise the same IRQ is shared by the both devices.
653 * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
654 * Adding this support requires significant changes to the dmabuf.c, dsp.c
659 tmp = gus_irq_map[gus_irq];
661 printk ("Warning! GUS IRQ not selected\n");
664 if (GUS_MIDI_IRQ != gus_irq)
665 { /* The midi irq was defined and != wave irq */
666 tmp = gus_irq_map[GUS_MIDI_IRQ];
670 printk ("Warning! GUS Midi IRQ not selected\n");
672 gus_set_midi_irq (GUS_MIDI_IRQ);
677 irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
679 dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
680 tmp = gus_dma_map[gus_dma];
682 printk ("Warning! GUS DMA not selected\n");
686 * For some reason the IRQ and DMA addresses must be written twice
689 /* Doing it first time */
691 OUTB (mix_image, u_Mixer); /* Select DMA control */
692 OUTB (dma_image, u_IRQDMAControl); /* Set DMA address */
694 OUTB (mix_image | 0x40, u_Mixer); /* Select IRQ control */
695 OUTB (irq_image, u_IRQDMAControl); /* Set IRQ address */
697 /* Doing it second time */
699 OUTB (mix_image, u_Mixer); /* Select DMA control */
700 OUTB (dma_image, u_IRQDMAControl); /* Set DMA address */
702 OUTB (mix_image | 0x40, u_Mixer); /* Select IRQ control */
703 OUTB (irq_image, u_IRQDMAControl); /* Set IRQ address */
705 gus_select_voice (0); /* This disables writes to IRQ/DMA reg */
707 mix_image &= ~0x02; /* Enable line out */
708 mix_image |= 0x08; /* Enable IRQ */
709 OUTB (mix_image, u_Mixer); /* Turn mixer channels on */
711 gus_select_voice (0); /* This disables writes to IRQ/DMA reg */
713 gusintr (0); /* Serve pending interrupts */
714 RESTORE_INTR (flags);
718 gus_wave_detect (int baseaddr)
722 gus_write8 (0x4c, 0); /* Reset GF1 */
726 gus_write8 (0x4c, 1); /* Release Reset */
730 gus_poke (0x000, 0xaa);
731 gus_poke (0x100, 0x55);
733 if (gus_peek (0x000) != 0xaa)
735 if (gus_peek (0x100) != 0x55)
738 gus_mem_size = 0x40000; /* 256k */
739 gus_poke (0x40000, 0xaa);
740 if (gus_peek (0x40000) != 0xaa)
743 gus_mem_size = 0x80000; /* 512k */
744 gus_poke (0x80000, 0xaa);
745 if (gus_peek (0x80000) != 0xaa)
748 gus_mem_size = 0xc0000; /* 768k */
749 gus_poke (0xc0000, 0xaa);
750 if (gus_peek (0xc0000) != 0xaa)
753 gus_mem_size = 0x100000; /* 1M */
759 guswave_ioctl (int dev,
760 unsigned int cmd, unsigned int arg)
765 case SNDCTL_SYNTH_INFO:
766 gus_info.nr_voices = nr_voices;
767 IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
771 case SNDCTL_SEQ_RESETSAMPLES:
772 reset_sample_memory ();
776 case SNDCTL_SEQ_PERCMODE:
780 case SNDCTL_SYNTH_MEMAVL:
781 return gus_mem_size - free_mem_ptr - 32;
784 return RET_ERROR (EINVAL);
789 guswave_set_instr (int dev, int voice, int instr_no)
793 if (instr_no < 0 || instr_no > MAX_PATCH)
794 return RET_ERROR (EINVAL);
796 if (voice < 0 || voice > 31)
797 return RET_ERROR (EINVAL);
799 sample_no = patch_table[instr_no];
800 patch_map[voice] = -1;
804 printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
805 return RET_ERROR (EINVAL);/* Patch not defined */
808 if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
810 printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);
811 return RET_ERROR (EINVAL);
814 sample_map[voice] = sample_no;
815 patch_map[voice] = instr_no;
820 guswave_kill_note (int dev, int voice, int velocity)
824 DISABLE_INTR (flags);
825 gus_select_voice (voice);
826 gus_voice_fade (voice);
827 RESTORE_INTR (flags);
833 guswave_aftertouch (int dev, int voice, int pressure)
835 short lo_limit, hi_limit;
838 return; /* Currently disabled */
840 if (voice < 0 || voice > 31)
843 if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
844 return; /* Don't mix with envelopes */
848 DISABLE_INTR (flags);
849 gus_select_voice (voice);
851 compute_and_set_volume (voice, 255, 0); /* Back to original volume */
852 RESTORE_INTR (flags);
856 hi_limit = voices[voice].current_volume;
857 lo_limit = hi_limit * 99 / 100;
861 DISABLE_INTR (flags);
862 gus_select_voice (voice);
863 if (hi_limit > (4095 - 65))
865 hi_limit = 4095 - 65;
866 gus_voice_volume (hi_limit);
868 gus_ramp_range (lo_limit, hi_limit);
869 gus_ramp_rate (3, 8);
870 gus_rampon (0x58); /* Bidirectional, Down, Loop */
871 RESTORE_INTR (flags);
875 guswave_panning (int dev, int voice, int value)
877 if (voice >= 0 || voice < 32)
878 voices[voice].panning = value;
882 compute_volume (int voice, int volume)
886 voices[voice].midi_volume = volume;
888 switch (volume_method)
890 case VOL_METHOD_ADAGIO:
891 voices[voice].initial_volume =
892 gus_adagio_vol (volume, voices[voice].main_vol,
893 voices[voice].expression_vol,
894 voices[voice].patch_vol);
898 voices[voice].initial_volume = volume_base + (volume * volume_scale);
902 if (voices[voice].initial_volume > 4095)
903 voices[voice].initial_volume = 4095;
907 compute_and_set_volume (int voice, int volume, int ramp_time)
909 int current, target, rate;
911 compute_volume (voice, volume);
912 voices[voice].current_volume = voices[voice].initial_volume;
914 current = gus_read16 (0x09) >> 4;
915 target = voices[voice].initial_volume;
917 if (ramp_time == INSTANT_RAMP)
920 gus_voice_volume (target);
924 if (ramp_time == FAST_RAMP)
928 gus_ramp_rate (0, rate);
930 if ((target - current) / 64 == 0) /* Too close */
933 gus_voice_volume (target);
937 if (target > current)
939 if (target > (4095 - 65))
941 gus_ramp_range (current, target);
942 gus_rampon (0x00); /* Ramp up, once, no irq */
949 gus_ramp_range (target, current);
950 gus_rampon (0x40); /* Ramp down, once, no irq */
955 dynamic_volume_change (int voice)
957 unsigned char status;
960 DISABLE_INTR (flags);
961 gus_select_voice (voice);
962 status = gus_read8 (0x00); /* Voice status */
963 RESTORE_INTR (flags);
966 return; /* Voice not started */
968 if (!(voices[voice].mode & WAVE_ENVELOPES))
970 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
975 * Voice is running and has envelopes.
978 DISABLE_INTR (flags);
979 gus_select_voice (voice);
980 status = gus_read8 (0x0d); /* Ramping status */
981 RESTORE_INTR (flags);
983 if (status & 0x03) /* Sustain phase? */
985 compute_and_set_volume (voice, voices[voice].midi_volume, 1);
989 if (voices[voice].env_phase < 0)
992 compute_volume (voice, voices[voice].midi_volume);
994 #if 0 /* Is this really required */
995 voices[voice].current_volume =
996 gus_read16 (0x09) >> 4; /* Get current volume */
998 voices[voice].env_phase--;
999 step_envelope (voice);
1004 guswave_controller (int dev, int voice, int ctrl_num, int value)
1006 unsigned long flags;
1009 if (voice < 0 || voice > 31)
1014 case CTRL_PITCH_BENDER:
1015 voices[voice].bender = value;
1016 freq = compute_finetune (voices[voice].orig_freq, value, voices[voice].bender_range);
1017 voices[voice].current_freq = freq;
1019 DISABLE_INTR (flags);
1020 gus_select_voice (voice);
1021 gus_voice_freq (freq);
1022 RESTORE_INTR (flags);
1025 case CTRL_PITCH_BENDER_RANGE:
1026 voices[voice].bender_range = value;
1029 case CTRL_EXPRESSION:
1030 volume_method = VOL_METHOD_ADAGIO;
1031 voices[voice].expression_vol = value;
1032 dynamic_volume_change (voice);
1035 case CTRL_MAIN_VOLUME:
1036 volume_method = VOL_METHOD_ADAGIO;
1037 voices[voice].main_vol = value;
1038 dynamic_volume_change (voice);
1041 default: /* Ignore */
1047 guswave_start_note (int dev, int voice, int note_num, int volume)
1049 int sample, best_sample, best_delta, delta_freq;
1050 int is16bits, samplep, patch, pan;
1051 unsigned long note_freq, base_note, freq, flags;
1052 unsigned char mode = 0;
1054 if (voice < 0 || voice > 31)
1056 printk ("GUS: Invalid voice\n");
1057 return RET_ERROR (EINVAL);
1060 if (note_num == 255)
1062 if (voices[voice].mode & WAVE_ENVELOPES)
1064 voices[voice].midi_volume = volume;
1065 dynamic_volume_change (voice);
1069 compute_and_set_volume (voice, volume, 1);
1073 if ((patch = patch_map[voice]) == -1)
1075 return RET_ERROR (EINVAL);
1078 if ((samplep = patch_table[patch]) == -1)
1080 return RET_ERROR (EINVAL);
1083 note_freq = note_to_freq (note_num);
1086 * Find a sample within a patch so that the note_freq is between low_note
1091 best_sample = samplep;
1092 best_delta = 1000000;
1093 while (samplep >= 0 && sample == -1)
1095 delta_freq = note_freq - samples[samplep].base_note;
1097 delta_freq = -delta_freq;
1098 if (delta_freq < best_delta)
1100 best_sample = samplep;
1101 best_delta = delta_freq;
1103 if (samples[samplep].low_note <= note_freq && note_freq <= samples[samplep].high_note)
1106 samplep = samples[samplep].key; /* Follow link */
1109 sample = best_sample;
1113 printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1114 return 0; /* Should play default patch ??? */
1117 is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bit samples */
1118 voices[voice].mode = samples[sample].mode;
1119 voices[voice].patch_vol = samples[sample].volume;
1121 if (voices[voice].mode & WAVE_ENVELOPES)
1125 for (i = 0; i < 6; i++)
1127 voices[voice].env_rate[i] = samples[sample].env_rate[i];
1128 voices[voice].env_offset[i] = samples[sample].env_offset[i];
1132 sample_map[voice] = sample;
1134 base_note = samples[sample].base_note / 100; /* To avoid overflows */
1137 freq = samples[sample].base_freq * note_freq / base_note;
1139 voices[voice].orig_freq = freq;
1142 * Since the pitch bender may have been set before playing the note, we
1143 * have to calculate the bending now.
1146 freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1147 voices[voice].current_freq = freq;
1149 pan = (samples[sample].panning + voices[voice].panning) / 32;
1156 if (samples[sample].mode & WAVE_16_BITS)
1158 mode |= 0x04; /* 16 bits */
1159 if ((sample_ptrs[sample] >> 18) !=
1160 ((sample_ptrs[sample] + samples[sample].len) >> 18))
1161 printk ("GUS: Sample address error\n");
1164 /*************************************************************************
1165 * CAUTION! Interrupts disabled. Don't return before enabling
1166 *************************************************************************/
1168 DISABLE_INTR (flags);
1169 gus_select_voice (voice);
1170 gus_voice_off (); /* It may still be running */
1172 if (voices[voice].mode & WAVE_ENVELOPES)
1174 compute_volume (voice, volume);
1175 init_envelope (voice);
1178 compute_and_set_volume (voice, volume, 0);
1180 if (samples[sample].mode & WAVE_LOOP_BACK)
1181 gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len, is16bits); /* Sample start=end */
1183 gus_write_addr (0x0a, sample_ptrs[sample], is16bits); /* Sample start=begin */
1185 if (samples[sample].mode & WAVE_LOOPING)
1187 mode |= 0x08; /* Looping on */
1189 if (samples[sample].mode & WAVE_BIDIR_LOOP)
1190 mode |= 0x10; /* Bidirectional looping on */
1192 if (samples[sample].mode & WAVE_LOOP_BACK)
1194 gus_write_addr (0x0a, /* Put the current location = loop_end */
1195 sample_ptrs[sample] + samples[sample].loop_end, is16bits);
1196 mode |= 0x40; /* Loop backwards */
1199 gus_write_addr (0x02, sample_ptrs[sample] + samples[sample].loop_start, is16bits); /* Loop start location */
1200 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].loop_end, is16bits); /* Loop end location */
1204 mode |= 0x20; /* Loop irq at the end */
1205 voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp it down at the
1207 voices[voice].loop_irq_parm = 1;
1208 gus_write_addr (0x02, sample_ptrs[sample], is16bits); /* Loop start location */
1209 gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].len, is16bits); /* Loop end location */
1211 gus_voice_freq (freq);
1212 gus_voice_balance (pan);
1213 gus_voice_on (mode);
1214 RESTORE_INTR (flags);
1220 guswave_reset (int dev)
1224 for (i = 0; i < 32; i++)
1229 guswave_open (int dev, int mode)
1234 return RET_ERROR (EBUSY);
1236 if ((err = DMAbuf_open_dma (gus_devnum)))
1240 active_device = GUS_DEV_WAVE;
1248 guswave_close (int dev)
1254 DMAbuf_close_dma (gus_devnum);
1258 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
1259 int offs, int count, int pmgr_flag)
1261 struct patch_info patch;
1264 unsigned long blk_size, blk_end, left, src_offs, target;
1266 if (format != GUS_PATCH)
1268 printk ("GUS Error: Invalid patch format (key) 0x%04x\n", format);
1270 if (format == OBSOLETE_GUS_PATCH)
1271 printk ("GUS Error: obsolete patch format\n");
1273 return RET_ERROR (EINVAL);
1276 if (count < sizeof (patch))
1278 printk ("GUS Error: Patch header too short\n");
1279 return RET_ERROR (EINVAL);
1282 count -= sizeof (patch);
1284 if (free_sample >= MAX_SAMPLE)
1286 printk ("GUS: Sample table full\n");
1287 return RET_ERROR (ENOSPC);
1291 * Copy the header from user space but ignore the first bytes which have
1292 * been transferred already.
1295 COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof (patch) - offs);
1297 instr = patch.instr_no;
1299 if (instr < 0 || instr > MAX_PATCH)
1301 printk ("GUS: Invalid patch number %d\n", instr);
1302 return RET_ERROR (EINVAL);
1305 if (count < patch.len)
1307 printk ("GUS Warning: Patch record too short (%d<%d)\n",
1312 if (patch.len <= 0 || patch.len > gus_mem_size)
1314 printk ("GUS: Invalid sample length %d\n", patch.len);
1315 return RET_ERROR (EINVAL);
1318 if (patch.mode & WAVE_LOOPING)
1320 if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1322 printk ("GUS: Invalid loop start\n");
1323 return RET_ERROR (EINVAL);
1326 if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1328 printk ("GUS: Invalid loop end\n");
1329 return RET_ERROR (EINVAL);
1333 free_mem_ptr = (free_mem_ptr + 31) & ~31; /* Alignment 32 bytes */
1335 #define GUS_BANK_SIZE (256*1024)
1337 if (patch.mode & WAVE_16_BITS)
1340 * 16 bit samples must fit one 256k bank.
1342 if (patch.len >= GUS_BANK_SIZE)
1344 printk ("GUS: Sample (16 bit) too long %d\n", patch.len);
1345 return RET_ERROR (ENOSPC);
1348 if ((free_mem_ptr / GUS_BANK_SIZE) !=
1349 ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1351 unsigned long tmp_mem = /* Align to 256K*N */
1352 ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1354 if ((tmp_mem + patch.len) > gus_mem_size)
1355 return RET_ERROR (ENOSPC);
1357 free_mem_ptr = tmp_mem; /* This leaves unusable memory */
1361 if ((free_mem_ptr + patch.len) > gus_mem_size)
1362 return RET_ERROR (ENOSPC);
1364 sample_ptrs[free_sample] = free_mem_ptr;
1366 /* Tremolo is not possible with envelopes */
1368 if (patch.mode & WAVE_ENVELOPES)
1369 patch.mode &= ~WAVE_TREMOLO;
1371 memcpy ((char *) &samples[free_sample], &patch, sizeof (patch));
1374 * Link this_one sample to the list of samples for patch 'instr'.
1377 samples[free_sample].key = patch_table[instr];
1378 patch_table[instr] = free_sample;
1381 * Use DMA to transfer the wave data to the DRAM
1386 target = free_mem_ptr;
1388 while (left) /* Not all moved */
1390 blk_size = sound_buffsizes[gus_devnum];
1391 if (blk_size > left)
1395 * DMA cannot cross 256k bank boundaries. Check for that.
1397 blk_end = target + blk_size;
1399 if ((target >> 18) != (blk_end >> 18))
1400 { /* Have to split the block */
1402 blk_end &= ~(256 * 1024 - 1);
1403 blk_size = blk_end - target;
1408 * For some reason the DMA is not possible. We have to use PIO.
1414 for (i = 0; i < blk_size; i++)
1416 GET_BYTE_FROM_USER (data, addr, sizeof (patch) + i);
1417 gus_poke (target + i, data);
1420 #else /* GUS_NO_DMA */
1422 unsigned long address, hold_address;
1423 unsigned char dma_command;
1426 * OK, move now. First in and then out.
1429 COPY_FROM_USER (snd_raw_buf[gus_devnum][0],
1430 addr, sizeof (patch) + src_offs,
1433 gus_write8 (0x41, 0); /* Disable GF1 DMA */
1434 DMAbuf_start_dma (gus_devnum, snd_raw_buf_phys[gus_devnum][0],
1435 blk_size, DMA_MODE_WRITE);
1438 * Set the DRAM address for the wave data
1443 if (sound_dsp_dmachan[gus_devnum] > 3)
1445 hold_address = address;
1446 address = address >> 1;
1447 address &= 0x0001ffffL;
1448 address |= (hold_address & 0x000c0000L);
1451 gus_write16 (0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1454 * Start the DMA transfer
1457 dma_command = 0x21; /* IRQ enable, DMA start */
1458 if (patch.mode & WAVE_UNSIGNED)
1459 dma_command |= 0x80; /* Invert MSB */
1460 if (patch.mode & WAVE_16_BITS)
1461 dma_command |= 0x40; /* 16 bit _DATA_ */
1462 if (sound_dsp_dmachan[gus_devnum] > 3)
1463 dma_command |= 0x04; /* 16 bit DMA channel */
1465 gus_write8 (0x41, dma_command); /* Let's go luteet (=bugs) */
1468 * Sleep here until the DRAM DMA done interrupt is served
1470 active_device = GUS_DEV_WAVE;
1472 INTERRUPTIBLE_SLEEP_ON (dram_sleeper, dram_sleep_flag);
1474 #endif /* GUS_NO_DMA */
1481 src_offs += blk_size;
1484 gus_write8 (0x41, 0); /* Stop DMA */
1487 free_mem_ptr += patch.len;
1490 pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1496 guswave_hw_control (int dev, unsigned char *event)
1499 unsigned short p1, p2;
1500 unsigned long plong, flags;
1504 p1 = *(unsigned short *) &event[4];
1505 p2 = *(unsigned short *) &event[6];
1506 plong = *(unsigned long *) &event[4];
1511 case _GUS_NUMVOICES:
1512 DISABLE_INTR (flags);
1513 gus_select_voice (voice);
1514 gus_select_max_voices (p1);
1515 RESTORE_INTR (flags);
1518 case _GUS_VOICESAMPLE:
1519 guswave_set_instr (dev, voice, p1);
1523 DISABLE_INTR (flags);
1524 gus_select_voice (voice);
1525 p1 &= ~0x20; /* Disable intr */
1527 RESTORE_INTR (flags);
1531 DISABLE_INTR (flags);
1532 gus_select_voice (voice);
1534 RESTORE_INTR (flags);
1537 case _GUS_VOICEFADE:
1538 DISABLE_INTR (flags);
1539 gus_select_voice (voice);
1540 gus_voice_fade (voice);
1541 RESTORE_INTR (flags);
1544 case _GUS_VOICEMODE:
1545 DISABLE_INTR (flags);
1546 gus_select_voice (voice);
1547 p1 &= ~0x20; /* Disable intr */
1548 gus_voice_mode (p1);
1549 RESTORE_INTR (flags);
1552 case _GUS_VOICEBALA:
1553 DISABLE_INTR (flags);
1554 gus_select_voice (voice);
1555 gus_voice_balance (p1);
1556 RESTORE_INTR (flags);
1559 case _GUS_VOICEFREQ:
1560 DISABLE_INTR (flags);
1561 gus_select_voice (voice);
1562 gus_voice_freq (plong);
1563 RESTORE_INTR (flags);
1567 DISABLE_INTR (flags);
1568 gus_select_voice (voice);
1569 gus_voice_volume (p1);
1570 RESTORE_INTR (flags);
1573 case _GUS_VOICEVOL2: /* Just update the voice value */
1574 voices[voice].initial_volume =
1575 voices[voice].current_volume = p1;
1578 case _GUS_RAMPRANGE:
1579 if (voices[voice].mode & WAVE_ENVELOPES)
1581 DISABLE_INTR (flags);
1582 gus_select_voice (voice);
1583 gus_ramp_range (p1, p2);
1584 RESTORE_INTR (flags);
1588 if (voices[voice].mode & WAVE_ENVELOPES)
1590 DISABLE_INTR (flags);
1591 gus_select_voice (voice);
1592 gus_ramp_rate (p1, p2);
1593 RESTORE_INTR (flags);
1597 if (voices[voice].mode & WAVE_ENVELOPES)
1599 DISABLE_INTR (flags);
1600 gus_select_voice (voice);
1601 p1 &= ~0x20; /* Disable intr */
1603 RESTORE_INTR (flags);
1607 if (voices[voice].mode & WAVE_ENVELOPES)
1609 DISABLE_INTR (flags);
1610 gus_select_voice (voice);
1611 p1 &= ~0x20; /* Disable intr */
1613 RESTORE_INTR (flags);
1617 if (voices[voice].mode & WAVE_ENVELOPES)
1619 DISABLE_INTR (flags);
1620 gus_select_voice (voice);
1622 RESTORE_INTR (flags);
1625 case _GUS_VOLUME_SCALE:
1635 gus_sampling_set_speed (int speed)
1638 return gus_sampling_speed;
1643 gus_sampling_speed = speed;
1648 gus_sampling_set_channels (int channels)
1651 return gus_sampling_channels;
1656 gus_sampling_channels = channels;
1661 gus_sampling_set_bits (int bits)
1664 return gus_sampling_bits;
1666 if (bits != 8 && bits != 16)
1669 gus_sampling_bits = bits;
1674 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
1678 case SOUND_PCM_WRITE_RATE:
1680 return gus_sampling_set_speed (arg);
1681 return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1684 case SOUND_PCM_READ_RATE:
1686 return gus_sampling_speed;
1687 return IOCTL_OUT (arg, gus_sampling_speed);
1690 case SNDCTL_DSP_STEREO:
1692 return gus_sampling_set_channels (arg + 1) - 1;
1693 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1696 case SOUND_PCM_WRITE_CHANNELS:
1697 return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1700 case SOUND_PCM_READ_CHANNELS:
1702 return gus_sampling_channels;
1703 return IOCTL_OUT (arg, gus_sampling_channels);
1706 case SNDCTL_DSP_SAMPLESIZE:
1708 return gus_sampling_set_bits (arg);
1709 return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1712 case SOUND_PCM_READ_BITS:
1714 return gus_sampling_bits;
1715 return IOCTL_OUT (arg, gus_sampling_bits);
1717 case SOUND_PCM_WRITE_FILTER: /* NOT YET IMPLEMENTED */
1718 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1721 case SOUND_PCM_READ_FILTER:
1722 return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1726 return RET_ERROR (EINVAL);
1728 return RET_ERROR (EINVAL);
1732 gus_sampling_reset (int dev)
1737 gus_sampling_open (int dev, int mode)
1740 printk ("GUS: DMA mode not enabled. Device not supported\n");
1741 return RET_ERROR (ENXIO);
1745 return RET_ERROR (EBUSY);
1751 reset_sample_memory ();
1752 gus_select_max_voices (14);
1754 gus_sampling_set_bits (8);
1755 gus_sampling_set_channels (1);
1756 gus_sampling_set_speed (DSP_DEFAULT_SPEED);
1763 gus_sampling_close (int dev)
1771 play_next_pcm_block (void)
1773 unsigned long flags;
1774 int speed = gus_sampling_speed;
1775 int this_one, is16bits, chn;
1776 unsigned long dram_loc;
1777 unsigned char mode[2], ramp_mode[2];
1782 this_one = pcm_head;
1784 for (chn = 0; chn < gus_sampling_channels; chn++)
1787 ramp_mode[chn] = 0x03; /* Ramping and rollover off */
1791 mode[chn] |= 0x20; /* Loop irq */
1792 voices[chn].loop_irq_mode = LMODE_PCM;
1795 if (gus_sampling_bits != 8)
1798 mode[chn] |= 0x04; /* 16 bit data */
1803 dram_loc = this_one * pcm_bsize;
1804 dram_loc += chn * pcm_banksize;
1806 if (this_one == (pcm_nblk - 1)) /* Last of the DRAM buffers */
1808 mode[chn] |= 0x08; /* Enable loop */
1809 ramp_mode[chn] = 0x03;/* Disable rollover */
1814 ramp_mode[chn] = 0x04; /* Enable rollover bit */
1817 DISABLE_INTR (flags);
1818 gus_select_voice (chn);
1819 gus_voice_freq (speed);
1821 if (gus_sampling_channels == 1)
1822 gus_voice_balance (7); /* mono */
1824 gus_voice_balance (0); /* left */
1826 gus_voice_balance (15); /* right */
1828 if (!pcm_active) /* Voice not started yet */
1831 * The playback was not started yet (or there has been a pause).
1832 * Start the voice (again) and ask for a rollover irq at the end of
1833 * this_one block. If this_one one is last of the buffers, use just
1834 * the normal loop with irq.
1837 gus_voice_off (); /* It could already be running */
1839 gus_voice_volume (4000);
1840 gus_ramp_range (65, 4030);
1842 gus_write_addr (0x0a, dram_loc, is16bits); /* Starting position */
1843 gus_write_addr (0x02, chn * pcm_banksize, is16bits); /* Loop start location */
1846 gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk),
1847 is16bits); /* Loop end location */
1851 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits); /* Loop end location */
1853 mode[chn] |= 0x08; /* Enable loop */
1855 if (pcm_datasize[this_one] != pcm_bsize)
1857 /* Incomplete block. Possibly the last one. */
1860 mode[chn] &= ~0x08; /* Disable loop */
1861 mode[chn] |= 0x20;/* Enable loop IRQ */
1862 voices[0].loop_irq_mode = LMODE_PCM_STOP;
1863 ramp_mode[chn] = 0x03; /* No rollover bit */
1867 gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits); /* Loop end location */
1868 mode[chn] &= ~0x08; /* Disable loop */
1872 RESTORE_INTR (flags);
1875 for (chn = 0; chn < gus_sampling_channels; chn++)
1877 DISABLE_INTR (flags);
1878 gus_select_voice (chn);
1879 gus_write8 (0x0d, ramp_mode[chn]);
1880 gus_voice_on (mode[chn]);
1881 RESTORE_INTR (flags);
1888 gus_transfer_output_block (int dev, unsigned long buf,
1889 int total_count, int intrflag, int chn)
1892 * This routine transfers one block of audio data to the DRAM. In mono mode
1893 * it's called just once. When in stereo mode, this_one routine is called
1894 * once for both channels.
1896 * The left/mono channel data is transferred to the beginning of dram and the
1897 * right data to the area pointed by gus_page_size.
1900 int this_one, count;
1901 unsigned long flags;
1902 unsigned char dma_command;
1903 unsigned long address, hold_address;
1905 DISABLE_INTR (flags);
1907 count = total_count / gus_sampling_channels;
1911 if (pcm_qlen >= pcm_nblk)
1912 printk ("GUS Warning: PCM buffers out of sync\n");
1914 this_one = pcm_current_block = pcm_tail;
1916 pcm_tail = (pcm_tail + 1) % pcm_nblk;
1917 pcm_datasize[this_one] = count;
1920 this_one = pcm_current_block;
1922 gus_write8 (0x41, 0); /* Disable GF1 DMA */
1923 DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
1925 address = this_one * pcm_bsize;
1926 address += chn * pcm_banksize;
1928 if (sound_dsp_dmachan[dev] > 3)
1930 hold_address = address;
1931 address = address >> 1;
1932 address &= 0x0001ffffL;
1933 address |= (hold_address & 0x000c0000L);
1936 gus_write16 (0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
1938 dma_command = 0x21; /* IRQ enable, DMA start */
1940 if (gus_sampling_bits != 8)
1941 dma_command |= 0x40; /* 16 bit _DATA_ */
1943 dma_command |= 0x80; /* Invert MSB */
1945 if (sound_dsp_dmachan[dev] > 3)
1946 dma_command |= 0x04; /* 16 bit DMA channel */
1948 gus_write8 (0x41, dma_command); /* Kick on */
1950 if (chn == (gus_sampling_channels - 1)) /* Last channel */
1952 /* Last (right or mono) channel data */
1953 active_device = GUS_DEV_PCM_DONE;
1954 if (!pcm_active && (pcm_qlen > 2 || count < pcm_bsize))
1956 play_next_pcm_block ();
1959 else /* Left channel data. The right channel is
1960 * transferred after DMA interrupt */
1961 active_device = GUS_DEV_PCM_CONTINUE;
1963 RESTORE_INTR (flags);
1967 gus_sampling_output_block (int dev, unsigned long buf, int total_count, int intrflag)
1969 pcm_current_buf = buf;
1970 pcm_current_count = total_count;
1971 pcm_current_intrflag = intrflag;
1972 pcm_current_dev = dev;
1973 gus_transfer_output_block (dev, buf, total_count, intrflag, 0);
1977 gus_sampling_start_input (int dev, unsigned long buf, int count, int intrflag)
1979 unsigned long flags;
1982 DISABLE_INTR (flags);
1984 DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
1986 mode = 0xa0; /* DMA IRQ enable, invert MSB */
1988 if (sound_dsp_dmachan[dev] > 3)
1989 mode |= 0x04; /* 16 bit DMA channel */
1990 if (gus_sampling_channels > 1)
1991 mode |= 0x02; /* Stereo */
1992 mode |= 0x01; /* DMA enable */
1994 gus_write8 (0x49, mode);
1996 RESTORE_INTR (flags);
2000 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
2004 rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2006 gus_write8 (0x48, rate & 0xff); /* Set sampling frequency */
2008 if (gus_sampling_bits != 8)
2010 printk ("GUS Error: 16 bit recording not supported\n");
2011 return RET_ERROR (EINVAL);
2018 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
2022 long mem_ptr, mem_size;
2025 mem_size = gus_mem_size / gus_sampling_channels;
2027 if (mem_size > (256 * 1024))
2028 mem_size = 256 * 1024;
2030 pcm_bsize = bsize / gus_sampling_channels;
2031 pcm_head = pcm_tail = pcm_qlen = 0;
2033 pcm_nblk = MAX_PCM_BUFFERS;
2034 if ((pcm_bsize * pcm_nblk) > mem_size)
2035 pcm_nblk = mem_size / pcm_bsize;
2037 for (i = 0; i < pcm_nblk; i++)
2038 pcm_datasize[i] = 0;
2040 pcm_banksize = pcm_nblk * pcm_bsize;
2042 if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2049 gus_has_output_drained (int dev)
2055 gus_copy_from_user (int dev, char *localbuf, int localoffs,
2056 snd_rw_buf * userbuf, int useroffs, int len)
2058 if (gus_sampling_channels == 1)
2059 COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2060 else if (gus_sampling_bits == 8)
2062 int in_left = useroffs;
2063 int in_right = useroffs + 1;
2064 char *out_left, *out_right;
2069 out_left = &localbuf[localoffs];
2070 out_right = out_left + pcm_bsize;
2072 for (i = 0; i < len; i++)
2074 GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2076 GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2082 int in_left = useroffs;
2083 int in_right = useroffs + 1;
2084 short *out_left, *out_right;
2090 out_left = (short *) &localbuf[localoffs];
2091 out_right = out_left + (pcm_bsize / 2);
2093 for (i = 0; i < len; i++)
2095 GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2097 GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2103 static struct audio_operations gus_sampling_operations =
2105 "Gravis UltraSound",
2106 gus_sampling_open, /* */
2107 gus_sampling_close, /* */
2108 gus_sampling_output_block, /* */
2109 gus_sampling_start_input, /* */
2110 gus_sampling_ioctl, /* */
2111 gus_sampling_prepare_for_input, /* */
2112 gus_sampling_prepare_for_output, /* */
2113 gus_sampling_reset, /* */
2114 gus_sampling_reset, /* halt_xfer */
2115 gus_has_output_drained,
2120 guswave_patchmgr (int dev, struct patmgr_info *rec)
2124 switch (rec->command)
2126 case PM_GET_DEVTYPE:
2127 rec->parm1 = PMTYPE_WAVE;
2132 rec->parm1 = MAX_PATCH;
2137 rec->parm1 = MAX_PATCH;
2139 for (i = 0; i < MAX_PATCH; i++)
2141 int ptr = patch_table[i];
2143 rec->data.data8[i] = 0;
2145 while (ptr >= 0 && ptr < free_sample)
2147 rec->data.data8[i]++;
2148 ptr = samples[ptr].key; /* Follow link */
2154 case PM_GET_PGM_PATCHES:
2156 int ptr = patch_table[rec->parm1];
2160 while (ptr >= 0 && ptr < free_sample)
2162 rec->data.data32[n++] = ptr;
2163 ptr = samples[ptr].key; /* Follow link */
2172 int ptr = rec->parm1;
2173 struct patch_info *pat;
2175 if (ptr < 0 || ptr >= free_sample)
2176 return RET_ERROR (EINVAL);
2178 memcpy (rec->data.data8, (char *) &samples[ptr],
2179 sizeof (struct patch_info));
2181 pat = (struct patch_info *) rec->data.data8;
2183 pat->key = GUS_PATCH; /* Restore patch type */
2184 rec->parm1 = sample_ptrs[ptr]; /* DRAM address */
2185 rec->parm2 = sizeof (struct patch_info);
2192 int ptr = rec->parm1;
2193 struct patch_info *pat;
2195 if (ptr < 0 || ptr >= free_sample)
2196 return RET_ERROR (EINVAL);
2198 pat = (struct patch_info *) rec->data.data8;
2200 if (pat->len > samples[ptr].len) /* Cannot expand sample */
2201 return RET_ERROR (EINVAL);
2203 pat->key = samples[ptr].key; /* Ensure the link is correct */
2205 memcpy ((char *) &samples[ptr], rec->data.data8,
2206 sizeof (struct patch_info));
2208 pat->key = GUS_PATCH;
2213 case PM_READ_PATCH: /* Returns a block of wave data from the DRAM */
2215 int sample = rec->parm1;
2217 long offs = rec->parm2;
2220 if (sample < 0 || sample >= free_sample)
2221 return RET_ERROR (EINVAL);
2223 if (offs < 0 || offs >= samples[sample].len)
2224 return RET_ERROR (EINVAL); /* Invalid offset */
2226 n = samples[sample].len - offs; /* Nr of bytes left */
2231 if (l > sizeof (rec->data.data8))
2232 l = sizeof (rec->data.data8);
2235 return RET_ERROR (EINVAL); /* Was there a bug? */
2237 offs += sample_ptrs[sample]; /* Begin offsess + offset to DRAM */
2239 for (n = 0; n < l; n++)
2240 rec->data.data8[n] = gus_peek (offs++);
2241 rec->parm1 = n; /* Nr of bytes copied */
2246 case PM_WRITE_PATCH: /* Writes a block of wave data to the DRAM */
2248 int sample = rec->parm1;
2250 long offs = rec->parm2;
2253 if (sample < 0 || sample >= free_sample)
2254 return RET_ERROR (EINVAL);
2256 if (offs < 0 || offs >= samples[sample].len)
2257 return RET_ERROR (EINVAL); /* Invalid offset */
2259 n = samples[sample].len - offs; /* Nr of bytes left */
2264 if (l > sizeof (rec->data.data8))
2265 l = sizeof (rec->data.data8);
2268 return RET_ERROR (EINVAL); /* Was there a bug? */
2270 offs += sample_ptrs[sample]; /* Begin offsess + offset to DRAM */
2272 for (n = 0; n < l; n++)
2273 gus_poke (offs++, rec->data.data8[n]);
2274 rec->parm1 = n; /* Nr of bytes copied */
2280 return RET_ERROR (EINVAL);
2284 static struct synth_operations guswave_operations =
2305 gus_wave_init (long mem_start, int irq, int dma)
2307 printk (" <Gravis UltraSound %dk>", gus_mem_size / 1024);
2309 if (irq < 0 || irq > 15)
2311 printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2315 if (dma < 0 || dma > 7)
2317 printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2324 if (num_synths >= MAX_SYNTH_DEV)
2325 printk ("GUS Error: Too many synthesizers\n");
2327 synth_devs[num_synths++] = &guswave_operations;
2329 reset_sample_memory ();
2333 if (num_dspdevs < MAX_DSP_DEV)
2335 dsp_devs[gus_devnum = num_dspdevs++] = &gus_sampling_operations;
2336 sound_dsp_dmachan[gus_devnum] = dma;
2337 sound_buffcounts[gus_devnum] = 1;
2338 sound_buffsizes[gus_devnum] = DSP_BUFFSIZE;
2339 sound_dma_automode[gus_devnum] = 0;
2342 printk ("GUS: Too many PCM devices available\n");
2348 do_loop_irq (int voice)
2352 unsigned long flags;
2354 DISABLE_INTR (flags);
2355 gus_select_voice (voice);
2357 tmp = gus_read8 (0x00);
2358 tmp &= ~0x20; /* Disable wave IRQ for this_one voice */
2359 gus_write8 (0x00, tmp);
2361 mode = voices[voice].loop_irq_mode;
2362 voices[voice].loop_irq_mode = 0;
2363 parm = voices[voice].loop_irq_parm;
2368 case LMODE_FINISH: /* Final loop finished, shoot volume down */
2370 if ((gus_read16 (0x09) >> 4) < 100) /* Get current volume */
2374 gus_voice_init (voice);
2377 gus_ramp_range (65, 4065);
2378 gus_ramp_rate (0, 63); /* Fastest possible rate */
2379 gus_rampon (0x20 | 0x40); /* Ramp down, once, irq */
2380 voices[voice].volume_irq_mode = VMODE_HALT;
2383 case LMODE_PCM_STOP:
2384 pcm_active = 0; /* Requires extensive processing */
2387 int orig_qlen = pcm_qlen;
2390 pcm_head = (pcm_head + 1) % pcm_nblk;
2393 play_next_pcm_block ();
2396 { /* Out of data. Just stop the voice */
2402 if (orig_qlen == pcm_nblk)
2404 DMAbuf_outputintr (gus_devnum);
2411 RESTORE_INTR (flags);
2415 do_volume_irq (int voice)
2419 unsigned long flags;
2421 DISABLE_INTR (flags);
2423 gus_select_voice (voice);
2425 tmp = gus_read8 (0x0d);
2426 tmp &= ~0x20; /* Disable volume ramp IRQ */
2427 gus_write8 (0x0d, tmp);
2429 mode = voices[voice].volume_irq_mode;
2430 voices[voice].volume_irq_mode = 0;
2431 parm = voices[voice].volume_irq_parm;
2435 case VMODE_HALT: /* Decay phase finished */
2436 gus_voice_init (voice);
2439 case VMODE_ENVELOPE:
2441 step_envelope (voice);
2447 RESTORE_INTR (flags);
2451 gus_voice_irq (void)
2453 unsigned long wave_ignore = 0, volume_ignore = 0;
2454 unsigned long voice_bit;
2456 unsigned char src, voice;
2460 src = gus_read8 (0x0f); /* Get source info */
2464 if (src == (0x80 | 0x40))
2465 return; /* No interrupt */
2467 voice_bit = 1 << voice;
2469 if (!(src & 0x80)) /* Wave IRQ pending */
2470 if (!(wave_ignore & voice_bit) && voice < nr_voices) /* Not done yet */
2472 wave_ignore |= voice_bit;
2473 do_loop_irq (voice);
2476 if (!(src & 0x40)) /* Volume IRQ pending */
2477 if (!(volume_ignore & voice_bit) && voice < nr_voices) /* Not done yet */
2479 volume_ignore |= voice_bit;
2480 do_volume_irq (voice);
2486 guswave_dma_irq (void)
2488 unsigned char status;
2490 status = gus_look8 (0x41); /* Get DMA IRQ Status */
2491 if (status & 0x40) /* DMA Irq pending */
2492 switch (active_device)
2495 if (dram_sleep_flag)
2496 WAKE_UP (dram_sleeper);
2499 case GUS_DEV_PCM_CONTINUE:
2500 gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
2502 pcm_current_intrflag, 1);
2505 case GUS_DEV_PCM_DONE:
2506 if (pcm_qlen < pcm_nblk)
2508 DMAbuf_outputintr (gus_devnum);
2515 status = gus_look8 (0x49); /* Get Sampling IRQ Status */
2516 if (status & 0x40) /* Sampling Irq pending */
2518 DMAbuf_inputintr (gus_devnum);