]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/sound/gus_wave.c
This is the Linux generic soundcard driver, version 1.0c. Supports
[FreeBSD/FreeBSD.git] / sys / i386 / isa / sound / gus_wave.c
1
2 /*
3  * linux/kernel/chr_drv/sound/gus_wave.c
4  * 
5  * Driver for the Gravis UltraSound wave table synth.
6  * 
7  * (C) 1993  Hannu Savolainen (hsavolai@cs.helsinki.fi) See COPYING for further
8  * details. Should be distributed with this file.
9  */
10
11 /* #define GUS_LINEAR_VOLUME     */
12
13 #include "sound_config.h"
14 #include "ultrasound.h"
15 #include "gus_hw.h"
16
17 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
18
19 #define MAX_SAMPLE      256
20 #define MAX_PATCH       256
21
22 struct voice_info
23   {
24     unsigned long   orig_freq;
25     unsigned long   current_freq;
26     unsigned long   mode;
27     int             bender;
28     int             bender_range;
29     int             panning;
30     int             midi_volume;
31     unsigned int    initial_volume;
32     unsigned int    current_volume;
33     int             loop_irq_mode, loop_irq_parm;
34 #define LMODE_FINISH            1
35 #define LMODE_PCM               2
36 #define LMODE_PCM_STOP          3
37     int             volume_irq_mode, volume_irq_parm;
38 #define VMODE_HALT              1
39 #define VMODE_ENVELOPE          2
40
41     int             env_phase;
42     unsigned char   env_rate[6];
43     unsigned char   env_offset[6];
44
45     /*
46      * Volume computation parameters for gus_adagio_vol()
47      */
48     int             main_vol, expression_vol, patch_vol;
49
50   };
51
52 extern int      gus_base;
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;
63
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;
67
68 /*
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
71  */
72 static int      active_device = 0;
73
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
77                                          * chn */
78
79 static int      gus_sampling_speed;
80 static int      gus_sampling_channels;
81 static int      gus_sampling_bits;
82
83 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
84
85 /*
86  * Variables and buffers for PCM output
87  */
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;
100
101 struct voice_info voices[32];
102
103 static int      freq_div_table[] =
104 {
105   44100,                        /* 14 */
106   41160,                        /* 15 */
107   38587,                        /* 16 */
108   36317,                        /* 17 */
109   34300,                        /* 18 */
110   32494,                        /* 19 */
111   30870,                        /* 20 */
112   29400,                        /* 21 */
113   28063,                        /* 22 */
114   26843,                        /* 23 */
115   25725,                        /* 24 */
116   24696,                        /* 25 */
117   23746,                        /* 26 */
118   22866,                        /* 27 */
119   22050,                        /* 28 */
120   21289,                        /* 29 */
121   20580,                        /* 30 */
122   19916,                        /* 31 */
123   19293                         /* 32 */
124 };
125
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;
130
131
132 static int      patch_table[MAX_PATCH];
133 static int      patch_map[32];
134
135 static struct synth_info gus_info =
136 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
137
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);
142
143 #define INSTANT_RAMP            -1      /* Dont use ramping */
144 #define FAST_RAMP               0       /* Fastest possible ramp */
145
146 static void
147 reset_sample_memory (void)
148 {
149   int             i;
150
151   for (i = 0; i <= MAX_SAMPLE; i++)
152     sample_ptrs[i] = -1;
153   for (i = 0; i < 32; i++)
154     sample_map[i] = -1;
155   for (i = 0; i < 32; i++)
156     patch_map[i] = -1;
157
158   gus_poke (0, 0);              /* Put silence here */
159   gus_poke (1, 0);
160
161   free_mem_ptr = 2;
162   free_sample = 0;
163
164   for (i = 0; i < MAX_PATCH; i++)
165     patch_table[i] = -1;
166 }
167
168 void
169 gus_delay (void)
170 {
171   int             i;
172
173   for (i = 0; i < 7; i++)
174     INB (u_DRAMIO);
175 }
176
177 static void
178 gus_poke (long addr, unsigned char data)
179 {
180   unsigned long   flags;
181
182   DISABLE_INTR (flags);
183   OUTB (0x43, u_Command);
184   OUTB (addr & 0xff, u_DataLo);
185   OUTB ((addr >> 8) & 0xff, u_DataHi);
186
187   OUTB (0x44, u_Command);
188   OUTB ((addr >> 16) & 0xff, u_DataHi);
189   OUTB (data, u_DRAMIO);
190   RESTORE_INTR (flags);
191 }
192
193 static unsigned char
194 gus_peek (long addr)
195 {
196   unsigned long   flags;
197   unsigned char   tmp;
198
199   DISABLE_INTR (flags);
200   OUTB (0x43, u_Command);
201   OUTB (addr & 0xff, u_DataLo);
202   OUTB ((addr >> 8) & 0xff, u_DataHi);
203
204   OUTB (0x44, u_Command);
205   OUTB ((addr >> 16) & 0xff, u_DataHi);
206   tmp = INB (u_DRAMIO);
207   RESTORE_INTR (flags);
208
209   return tmp;
210 }
211
212 void
213 gus_write8 (int reg, unsigned char data)
214 {
215   unsigned long   flags;
216
217   DISABLE_INTR (flags);
218
219   OUTB (reg, u_Command);
220   OUTB (data, u_DataHi);
221
222   RESTORE_INTR (flags);
223 }
224
225 unsigned char
226 gus_read8 (int reg)
227 {
228   unsigned long   flags;
229   unsigned char   val;
230
231   DISABLE_INTR (flags);
232   OUTB (reg | 0x80, u_Command);
233   val = INB (u_DataHi);
234   RESTORE_INTR (flags);
235
236   return val;
237 }
238
239 unsigned char
240 gus_look8 (int reg)
241 {
242   unsigned long   flags;
243   unsigned char   val;
244
245   DISABLE_INTR (flags);
246   OUTB (reg, u_Command);
247   val = INB (u_DataHi);
248   RESTORE_INTR (flags);
249
250   return val;
251 }
252
253 void
254 gus_write16 (int reg, unsigned short data)
255 {
256   unsigned long   flags;
257
258   DISABLE_INTR (flags);
259
260   OUTB (reg, u_Command);
261
262   OUTB (data & 0xff, u_DataLo);
263   OUTB ((data >> 8) & 0xff, u_DataHi);
264
265   RESTORE_INTR (flags);
266 }
267
268 unsigned short
269 gus_read16 (int reg)
270 {
271   unsigned long   flags;
272   unsigned char   hi, lo;
273
274   DISABLE_INTR (flags);
275
276   OUTB (reg | 0x80, u_Command);
277
278   lo = INB (u_DataLo);
279   hi = INB (u_DataHi);
280
281   RESTORE_INTR (flags);
282
283   return ((hi << 8) & 0xff00) | lo;
284 }
285
286 void
287 gus_write_addr (int reg, unsigned long address, int is16bit)
288 {
289   unsigned long   hold_address;
290
291   if (is16bit)
292     {
293       /*
294        * Special processing required for 16 bit patches
295        */
296
297       hold_address = address;
298       address = address >> 1;
299       address &= 0x0001ffffL;
300       address |= (hold_address & 0x000c0000L);
301     }
302
303   gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
304   gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
305 }
306
307 static void
308 gus_select_voice (int voice)
309 {
310   if (voice < 0 || voice > 31)
311     return;
312
313   OUTB (voice, u_Voice);
314 }
315
316 static void
317 gus_select_max_voices (int nvoices)
318 {
319   if (nvoices < 14)
320     nvoices = 14;
321   if (nvoices > 32)
322     nvoices = 32;
323
324   nr_voices = nvoices;
325
326   gus_write8 (0x0e, (nvoices - 1) | 0xc0);
327 }
328
329 static void
330 gus_voice_on (unsigned char mode)
331 {
332   gus_write8 (0x00, mode & 0xfc);
333   gus_delay ();
334   gus_write8 (0x00, mode & 0xfc);
335 }
336
337 static void
338 gus_voice_off (void)
339 {
340   gus_write8 (0x00, gus_read8 (0x00) | 0x03);
341 }
342
343 static void
344 gus_voice_mode (unsigned char mode)
345 {
346   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc)); /* Don't start or stop
347                                                                  * voice */
348   gus_delay ();
349   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
350 }
351
352 static void
353 gus_voice_freq (unsigned long freq)
354 {
355   unsigned long   divisor = freq_div_table[nr_voices - 14];
356   unsigned short  fc;
357
358   fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
359   fc = fc << 1;
360
361   gus_write16 (0x01, fc);
362 }
363
364 static void
365 gus_voice_volume (unsigned short vol)
366 {
367   gus_write8 (0x0d, 0x03);      /* Stop ramp before setting volume */
368   gus_write16 (0x09, vol << 4);
369 }
370
371 static void
372 gus_voice_balance (unsigned char balance)
373 {
374   gus_write8 (0x0c, balance);
375 }
376
377 static void
378 gus_ramp_range (unsigned short low, unsigned short high)
379 {
380   gus_write8 (0x07, (low >> 4) & 0xff);
381   gus_write8 (0x08, (high >> 4) & 0xff);
382 }
383
384 static void
385 gus_ramp_rate (unsigned char scale, unsigned char rate)
386 {
387   gus_write8 (0x06, ((scale & 0x03) << 6) | (rate & 0x3f));
388 }
389
390 static void
391 gus_rampon (unsigned char mode)
392 {
393   gus_write8 (0x0d, mode & 0xfc);
394   gus_delay ();
395   gus_write8 (0x0d, mode & 0xfc);
396 }
397
398 static void
399 gus_ramp_mode (unsigned char mode)
400 {
401   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc)); /* Don't start or stop
402                                                                  * ramping */
403   gus_delay ();
404   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
405 }
406
407 static void
408 gus_rampoff (void)
409 {
410   gus_write8 (0x0d, 0x03);
411 }
412
413 static void
414 gus_voice_init (int voice)
415 {
416   unsigned long   flags;
417
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);
425
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;
442 }
443
444 static void
445 step_envelope (int voice)
446 {
447   unsigned        vol, prev_vol, phase;
448   unsigned char   rate;
449
450   if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
451     {
452       gus_rampoff ();
453       return;                   /* Sustain */
454     }
455
456   if (voices[voice].env_phase >= 5)
457     {
458       /*
459        * Shoot the voice off
460        */
461
462       gus_voice_init (voice);
463       return;
464     }
465
466   prev_vol = voices[voice].current_volume;
467   gus_voice_volume (prev_vol);
468   phase = ++voices[voice].env_phase;
469
470   compute_volume (voice, voices[voice].midi_volume);
471
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 */
475
476   voices[voice].volume_irq_mode = VMODE_ENVELOPE;
477
478   if (((vol - prev_vol) / 64) == 0)     /* No significant volume change */
479     {
480       step_envelope (voice);    /* Continue with the next phase */
481       return;
482     }
483
484   if (vol > prev_vol)
485     {
486       if (vol >= (4096 - 64))
487         vol = 4096 - 65;
488       gus_ramp_range (0, vol);
489       gus_rampon (0x20);        /* Increasing, irq */
490     }
491   else
492     {
493       if (vol <= 64)
494         vol = 65;
495       gus_ramp_range (vol, 4095);
496       gus_rampon (0x60);        /* Decreasing, irq */
497     }
498   voices[voice].current_volume = vol;
499 }
500
501 static void
502 init_envelope (int voice)
503 {
504   voices[voice].env_phase = -1;
505   voices[voice].current_volume = 64;
506
507   step_envelope (voice);
508 }
509
510 static void
511 start_release (int voice)
512 {
513   if (gus_read8 (0x00) & 0x03)
514     return;                     /* Voice already stopped */
515
516   voices[voice].env_phase = 2;  /* Will be incremented by step_envelope */
517
518   voices[voice].current_volume =
519     voices[voice].initial_volume =
520     gus_read16 (0x09) >> 4;     /* Get current volume */
521
522   voices[voice].mode &= ~WAVE_SUSTAIN_ON;
523   gus_rampoff ();
524   step_envelope (voice);
525 }
526
527 static void
528 gus_voice_fade (int voice)
529 {
530   int             instr_no = sample_map[voice], is16bits;
531
532   if (instr_no < 0 || instr_no > MAX_SAMPLE)
533     {
534       gus_write8 (0x00, 0x03);  /* Hard stop */
535       return;
536     }
537
538   is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0;   /* 8 or 16 bit samples */
539
540   if (voices[voice].mode & WAVE_ENVELOPES)
541     {
542       start_release (voice);
543       return;
544     }
545
546   /*
547    * Ramp the volume down but not too quickly.
548    */
549   if ((gus_read16 (0x09) >> 4) < 100)   /* Get current volume */
550     {
551       gus_voice_off ();
552       gus_rampoff ();
553       gus_voice_init (voice);
554       return;
555     }
556
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;
561 }
562
563 static void
564 gus_reset (void)
565 {
566   int             i;
567
568   gus_select_max_voices (24);
569   volume_base = 3071;
570   volume_scale = 4;
571   volume_method = VOL_METHOD_ADAGIO;
572
573   for (i = 0; i < 32; i++)
574     {
575       gus_voice_init (i);       /* Turn voice off */
576     }
577
578   INB (u_Status);               /* Touch the status register */
579
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 */
583
584 }
585
586 static void
587 gus_initialize (void)
588 {
589   unsigned long   flags;
590   unsigned char   dma_image, irq_image, tmp;
591
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};
594
595   static unsigned char gus_dma_map[8] =
596   {0, 1, 0, 2, 0, 3, 4, 5};
597
598   DISABLE_INTR (flags);
599
600   gus_write8 (0x4c, 0);         /* Reset GF1 */
601   gus_delay ();
602   gus_delay ();
603
604   gus_write8 (0x4c, 1);         /* Release Reset */
605   gus_delay ();
606   gus_delay ();
607
608   /*
609    * Clear all interrupts
610    */
611
612   gus_write8 (0x41, 0);         /* DMA control */
613   gus_write8 (0x45, 0);         /* Timer control */
614   gus_write8 (0x49, 0);         /* Sample control */
615
616   gus_select_max_voices (24);
617
618   INB (u_Status);               /* Touch the status register */
619
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 */
623
624   gus_reset ();                 /* Resets all voices */
625
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 */
629
630   gus_write8 (0x4c, 7);         /* Master reset | DAC enable | IRQ enable */
631
632   /*
633    * Set up for Digital ASIC
634    */
635
636   OUTB (0x05, gus_base + 0x0f);
637
638   mix_image |= 0x02;            /* Disable line out */
639   OUTB (mix_image, u_Mixer);
640
641   OUTB (0x00, u_IRQDMAControl);
642
643   OUTB (0x00, gus_base + 0x0f);
644
645   /*
646    * Now set up the DMA and IRQ interface
647    * 
648    * The GUS supports two IRQs and two DMAs.
649    * 
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.
652    * 
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
655    * and audio.c also.
656    */
657
658   irq_image = 0;
659   tmp = gus_irq_map[gus_irq];
660   if (!tmp)
661     printk ("Warning! GUS IRQ not selected\n");
662   irq_image |= tmp;
663
664   if (GUS_MIDI_IRQ != gus_irq)
665     {                           /* The midi irq was defined and != wave irq */
666       tmp = gus_irq_map[GUS_MIDI_IRQ];
667       tmp <<= 3;
668
669       if (!tmp)
670         printk ("Warning! GUS Midi IRQ not selected\n");
671       else
672         gus_set_midi_irq (GUS_MIDI_IRQ);
673
674       irq_image |= tmp;
675     }
676   else
677     irq_image |= 0x40;          /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
678
679   dma_image = 0x40;             /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
680   tmp = gus_dma_map[gus_dma];
681   if (!tmp)
682     printk ("Warning! GUS DMA not selected\n");
683   dma_image |= tmp;
684
685   /*
686    * For some reason the IRQ and DMA addresses must be written twice
687    */
688
689   /* Doing it first time */
690
691   OUTB (mix_image, u_Mixer);    /* Select DMA control */
692   OUTB (dma_image, u_IRQDMAControl);    /* Set DMA address */
693
694   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
695   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
696
697   /* Doing it second time */
698
699   OUTB (mix_image, u_Mixer);    /* Select DMA control */
700   OUTB (dma_image, u_IRQDMAControl);    /* Set DMA address */
701
702   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
703   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
704
705   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
706
707   mix_image &= ~0x02;           /* Enable line out */
708   mix_image |= 0x08;            /* Enable IRQ */
709   OUTB (mix_image, u_Mixer);    /* Turn mixer channels on */
710
711   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
712
713   gusintr (0);                  /* Serve pending interrupts */
714   RESTORE_INTR (flags);
715 }
716
717 int
718 gus_wave_detect (int baseaddr)
719 {
720   gus_base = baseaddr;
721
722   gus_write8 (0x4c, 0);         /* Reset GF1 */
723   gus_delay ();
724   gus_delay ();
725
726   gus_write8 (0x4c, 1);         /* Release Reset */
727   gus_delay ();
728   gus_delay ();
729
730   gus_poke (0x000, 0xaa);
731   gus_poke (0x100, 0x55);
732
733   if (gus_peek (0x000) != 0xaa)
734     return 0;
735   if (gus_peek (0x100) != 0x55)
736     return 0;
737
738   gus_mem_size = 0x40000;       /* 256k */
739   gus_poke (0x40000, 0xaa);
740   if (gus_peek (0x40000) != 0xaa)
741     return 1;
742
743   gus_mem_size = 0x80000;       /* 512k */
744   gus_poke (0x80000, 0xaa);
745   if (gus_peek (0x80000) != 0xaa)
746     return 1;
747
748   gus_mem_size = 0xc0000;       /* 768k */
749   gus_poke (0xc0000, 0xaa);
750   if (gus_peek (0xc0000) != 0xaa)
751     return 1;
752
753   gus_mem_size = 0x100000;      /* 1M */
754
755   return 1;
756 }
757
758 static int
759 guswave_ioctl (int dev,
760                unsigned int cmd, unsigned int arg)
761 {
762
763   switch (cmd)
764     {
765     case SNDCTL_SYNTH_INFO:
766       gus_info.nr_voices = nr_voices;
767       IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
768       return 0;
769       break;
770
771     case SNDCTL_SEQ_RESETSAMPLES:
772       reset_sample_memory ();
773       return 0;
774       break;
775
776     case SNDCTL_SEQ_PERCMODE:
777       return 0;
778       break;
779
780     case SNDCTL_SYNTH_MEMAVL:
781       return gus_mem_size - free_mem_ptr - 32;
782
783     default:
784       return RET_ERROR (EINVAL);
785     }
786 }
787
788 static int
789 guswave_set_instr (int dev, int voice, int instr_no)
790 {
791   int             sample_no;
792
793   if (instr_no < 0 || instr_no > MAX_PATCH)
794     return RET_ERROR (EINVAL);
795
796   if (voice < 0 || voice > 31)
797     return RET_ERROR (EINVAL);
798
799   sample_no = patch_table[instr_no];
800   patch_map[voice] = -1;
801
802   if (sample_no < 0)
803     {
804       printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
805       return RET_ERROR (EINVAL);/* Patch not defined */
806     }
807
808   if (sample_ptrs[sample_no] == -1)     /* Sample not loaded */
809     {
810       printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);
811       return RET_ERROR (EINVAL);
812     }
813
814   sample_map[voice] = sample_no;
815   patch_map[voice] = instr_no;
816   return 0;
817 }
818
819 static int
820 guswave_kill_note (int dev, int voice, int velocity)
821 {
822   unsigned long   flags;
823
824   DISABLE_INTR (flags);
825   gus_select_voice (voice);
826   gus_voice_fade (voice);
827   RESTORE_INTR (flags);
828
829   return 0;
830 }
831
832 static void
833 guswave_aftertouch (int dev, int voice, int pressure)
834 {
835   short           lo_limit, hi_limit;
836   unsigned long   flags;
837
838   return;                       /* Currently disabled */
839
840   if (voice < 0 || voice > 31)
841     return;
842
843   if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
844     return;                     /* Don't mix with envelopes */
845
846   if (pressure < 32)
847     {
848       DISABLE_INTR (flags);
849       gus_select_voice (voice);
850       gus_rampoff ();
851       compute_and_set_volume (voice, 255, 0);   /* Back to original volume */
852       RESTORE_INTR (flags);
853       return;
854     }
855
856   hi_limit = voices[voice].current_volume;
857   lo_limit = hi_limit * 99 / 100;
858   if (lo_limit < 65)
859     lo_limit = 65;
860
861   DISABLE_INTR (flags);
862   gus_select_voice (voice);
863   if (hi_limit > (4095 - 65))
864     {
865       hi_limit = 4095 - 65;
866       gus_voice_volume (hi_limit);
867     }
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);
872 }
873
874 static void
875 guswave_panning (int dev, int voice, int value)
876 {
877   if (voice >= 0 || voice < 32)
878     voices[voice].panning = value;
879 }
880
881 static void
882 compute_volume (int voice, int volume)
883 {
884   if (volume < 128)
885     {
886       voices[voice].midi_volume = volume;
887
888       switch (volume_method)
889         {
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);
895           break;
896
897         default:
898           voices[voice].initial_volume = volume_base + (volume * volume_scale);
899         }
900     }
901
902   if (voices[voice].initial_volume > 4095)
903     voices[voice].initial_volume = 4095;
904 }
905
906 static void
907 compute_and_set_volume (int voice, int volume, int ramp_time)
908 {
909   int             current, target, rate;
910
911   compute_volume (voice, volume);
912   voices[voice].current_volume = voices[voice].initial_volume;
913
914   current = gus_read16 (0x09) >> 4;
915   target = voices[voice].initial_volume;
916
917   if (ramp_time == INSTANT_RAMP)
918     {
919       gus_rampoff ();
920       gus_voice_volume (target);
921       return;
922     }
923
924   if (ramp_time == FAST_RAMP)
925     rate = 63;
926   else
927     rate = 16;
928   gus_ramp_rate (0, rate);
929
930   if ((target - current) / 64 == 0)     /* Too close */
931     {
932       gus_rampoff ();
933       gus_voice_volume (target);
934       return;
935     }
936
937   if (target > current)
938     {
939       if (target > (4095 - 65))
940         target = 4095 - 65;
941       gus_ramp_range (current, target);
942       gus_rampon (0x00);        /* Ramp up, once, no irq */
943     }
944   else
945     {
946       if (target < 65)
947         target = 65;
948
949       gus_ramp_range (target, current);
950       gus_rampon (0x40);        /* Ramp down, once, no irq */
951     }
952 }
953
954 static void
955 dynamic_volume_change (int voice)
956 {
957   unsigned char   status;
958   unsigned long   flags;
959
960   DISABLE_INTR (flags);
961   gus_select_voice (voice);
962   status = gus_read8 (0x00);    /* Voice status */
963   RESTORE_INTR (flags);
964
965   if (status & 0x03)
966     return;                     /* Voice not started */
967
968   if (!(voices[voice].mode & WAVE_ENVELOPES))
969     {
970       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
971       return;
972     }
973
974   /*
975    * Voice is running and has envelopes.
976    */
977
978   DISABLE_INTR (flags);
979   gus_select_voice (voice);
980   status = gus_read8 (0x0d);    /* Ramping status */
981   RESTORE_INTR (flags);
982
983   if (status & 0x03)            /* Sustain phase? */
984     {
985       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
986       return;
987     }
988
989   if (voices[voice].env_phase < 0)
990     return;
991
992   compute_volume (voice, voices[voice].midi_volume);
993
994 #if 0                           /* Is this really required */
995   voices[voice].current_volume =
996     gus_read16 (0x09) >> 4;     /* Get current volume */
997
998   voices[voice].env_phase--;
999   step_envelope (voice);
1000 #endif
1001 }
1002
1003 static void
1004 guswave_controller (int dev, int voice, int ctrl_num, int value)
1005 {
1006   unsigned long   flags;
1007   unsigned long   freq;
1008
1009   if (voice < 0 || voice > 31)
1010     return;
1011
1012   switch (ctrl_num)
1013     {
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;
1018
1019       DISABLE_INTR (flags);
1020       gus_select_voice (voice);
1021       gus_voice_freq (freq);
1022       RESTORE_INTR (flags);
1023       break;
1024
1025     case CTRL_PITCH_BENDER_RANGE:
1026       voices[voice].bender_range = value;
1027       break;
1028
1029     case CTRL_EXPRESSION:
1030       volume_method = VOL_METHOD_ADAGIO;
1031       voices[voice].expression_vol = value;
1032       dynamic_volume_change (voice);
1033       break;
1034
1035     case CTRL_MAIN_VOLUME:
1036       volume_method = VOL_METHOD_ADAGIO;
1037       voices[voice].main_vol = value;
1038       dynamic_volume_change (voice);
1039       break;
1040
1041     default:                    /* Ignore */
1042       break;
1043     }
1044 }
1045
1046 static int
1047 guswave_start_note (int dev, int voice, int note_num, int volume)
1048 {
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;
1053
1054   if (voice < 0 || voice > 31)
1055     {
1056       printk ("GUS: Invalid voice\n");
1057       return RET_ERROR (EINVAL);
1058     }
1059
1060   if (note_num == 255)
1061     {
1062       if (voices[voice].mode & WAVE_ENVELOPES)
1063         {
1064           voices[voice].midi_volume = volume;
1065           dynamic_volume_change (voice);
1066           return 0;
1067         }
1068
1069       compute_and_set_volume (voice, volume, 1);
1070       return 0;
1071     }
1072
1073   if ((patch = patch_map[voice]) == -1)
1074     {
1075       return RET_ERROR (EINVAL);
1076     }
1077
1078   if ((samplep = patch_table[patch]) == -1)
1079     {
1080       return RET_ERROR (EINVAL);
1081     }
1082
1083   note_freq = note_to_freq (note_num);
1084
1085   /*
1086    * Find a sample within a patch so that the note_freq is between low_note
1087    * and high_note.
1088    */
1089   sample = -1;
1090
1091   best_sample = samplep;
1092   best_delta = 1000000;
1093   while (samplep >= 0 && sample == -1)
1094     {
1095       delta_freq = note_freq - samples[samplep].base_note;
1096       if (delta_freq < 0)
1097         delta_freq = -delta_freq;
1098       if (delta_freq < best_delta)
1099         {
1100           best_sample = samplep;
1101           best_delta = delta_freq;
1102         }
1103       if (samples[samplep].low_note <= note_freq && note_freq <= samples[samplep].high_note)
1104         sample = samplep;
1105       else
1106         samplep = samples[samplep].key; /* Follow link */
1107     }
1108   if (sample == -1)
1109     sample = best_sample;
1110
1111   if (sample == -1)
1112     {
1113       printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1114       return 0;                 /* Should play default patch ??? */
1115     }
1116
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;
1120
1121   if (voices[voice].mode & WAVE_ENVELOPES)
1122     {
1123       int             i;
1124
1125       for (i = 0; i < 6; i++)
1126         {
1127           voices[voice].env_rate[i] = samples[sample].env_rate[i];
1128           voices[voice].env_offset[i] = samples[sample].env_offset[i];
1129         }
1130     }
1131
1132   sample_map[voice] = sample;
1133
1134   base_note = samples[sample].base_note / 100;  /* To avoid overflows */
1135   note_freq /= 100;
1136
1137   freq = samples[sample].base_freq * note_freq / base_note;
1138
1139   voices[voice].orig_freq = freq;
1140
1141   /*
1142    * Since the pitch bender may have been set before playing the note, we
1143    * have to calculate the bending now.
1144    */
1145
1146   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1147   voices[voice].current_freq = freq;
1148
1149   pan = (samples[sample].panning + voices[voice].panning) / 32;
1150   pan += 7;
1151   if (pan < 0)
1152     pan = 0;
1153   if (pan > 15)
1154     pan = 15;
1155
1156   if (samples[sample].mode & WAVE_16_BITS)
1157     {
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");
1162     }
1163
1164   /*************************************************************************
1165  *      CAUTION!        Interrupts disabled. Don't return before enabling
1166  *************************************************************************/
1167
1168   DISABLE_INTR (flags);
1169   gus_select_voice (voice);
1170   gus_voice_off ();             /* It may still be running */
1171   gus_rampoff ();
1172   if (voices[voice].mode & WAVE_ENVELOPES)
1173     {
1174       compute_volume (voice, volume);
1175       init_envelope (voice);
1176     }
1177   else
1178     compute_and_set_volume (voice, volume, 0);
1179
1180   if (samples[sample].mode & WAVE_LOOP_BACK)
1181     gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len, is16bits); /* Sample start=end */
1182   else
1183     gus_write_addr (0x0a, sample_ptrs[sample], is16bits);       /* Sample start=begin */
1184
1185   if (samples[sample].mode & WAVE_LOOPING)
1186     {
1187       mode |= 0x08;             /* Looping on */
1188
1189       if (samples[sample].mode & WAVE_BIDIR_LOOP)
1190         mode |= 0x10;           /* Bidirectional looping on */
1191
1192       if (samples[sample].mode & WAVE_LOOP_BACK)
1193         {
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 */
1197         }
1198
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 */
1201     }
1202   else
1203     {
1204       mode |= 0x20;             /* Loop irq at the end */
1205       voices[voice].loop_irq_mode = LMODE_FINISH;       /* Ramp it down at the
1206                                                          * end */
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 */
1210     }
1211   gus_voice_freq (freq);
1212   gus_voice_balance (pan);
1213   gus_voice_on (mode);
1214   RESTORE_INTR (flags);
1215
1216   return 0;
1217 }
1218
1219 static void
1220 guswave_reset (int dev)
1221 {
1222   int             i;
1223
1224   for (i = 0; i < 32; i++)
1225     gus_voice_init (i);
1226 }
1227
1228 static int
1229 guswave_open (int dev, int mode)
1230 {
1231   int             err;
1232
1233   if (gus_busy)
1234     return RET_ERROR (EBUSY);
1235
1236   if ((err = DMAbuf_open_dma (gus_devnum)))
1237     return err;
1238
1239   gus_busy = 1;
1240   active_device = GUS_DEV_WAVE;
1241
1242   gus_reset ();
1243
1244   return 0;
1245 }
1246
1247 static void
1248 guswave_close (int dev)
1249 {
1250   gus_busy = 0;
1251   active_device = 0;
1252   gus_reset ();
1253
1254   DMAbuf_close_dma (gus_devnum);
1255 }
1256
1257 static int
1258 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
1259                     int offs, int count, int pmgr_flag)
1260 {
1261   struct patch_info patch;
1262   int             instr;
1263
1264   unsigned long   blk_size, blk_end, left, src_offs, target;
1265
1266   if (format != GUS_PATCH)
1267     {
1268       printk ("GUS Error: Invalid patch format (key) 0x%04x\n", format);
1269 #ifndef CPU_I486
1270       if (format == OBSOLETE_GUS_PATCH)
1271         printk ("GUS Error: obsolete patch format\n");
1272 #endif
1273       return RET_ERROR (EINVAL);
1274     }
1275
1276   if (count < sizeof (patch))
1277     {
1278       printk ("GUS Error: Patch header too short\n");
1279       return RET_ERROR (EINVAL);
1280     }
1281
1282   count -= sizeof (patch);
1283
1284   if (free_sample >= MAX_SAMPLE)
1285     {
1286       printk ("GUS: Sample table full\n");
1287       return RET_ERROR (ENOSPC);
1288     }
1289
1290   /*
1291    * Copy the header from user space but ignore the first bytes which have
1292    * been transferred already.
1293    */
1294
1295   COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof (patch) - offs);
1296
1297   instr = patch.instr_no;
1298
1299   if (instr < 0 || instr > MAX_PATCH)
1300     {
1301       printk ("GUS: Invalid patch number %d\n", instr);
1302       return RET_ERROR (EINVAL);
1303     }
1304
1305   if (count < patch.len)
1306     {
1307       printk ("GUS Warning: Patch record too short (%d<%d)\n",
1308               count, patch.len);
1309       patch.len = count;
1310     }
1311
1312   if (patch.len <= 0 || patch.len > gus_mem_size)
1313     {
1314       printk ("GUS: Invalid sample length %d\n", patch.len);
1315       return RET_ERROR (EINVAL);
1316     }
1317
1318   if (patch.mode & WAVE_LOOPING)
1319     {
1320       if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1321         {
1322           printk ("GUS: Invalid loop start\n");
1323           return RET_ERROR (EINVAL);
1324         }
1325
1326       if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1327         {
1328           printk ("GUS: Invalid loop end\n");
1329           return RET_ERROR (EINVAL);
1330         }
1331     }
1332
1333   free_mem_ptr = (free_mem_ptr + 31) & ~31;     /* Alignment 32 bytes */
1334
1335 #define GUS_BANK_SIZE (256*1024)
1336
1337   if (patch.mode & WAVE_16_BITS)
1338     {
1339       /*
1340        * 16 bit samples must fit one 256k bank.
1341        */
1342       if (patch.len >= GUS_BANK_SIZE)
1343         {
1344           printk ("GUS: Sample (16 bit) too long %d\n", patch.len);
1345           return RET_ERROR (ENOSPC);
1346         }
1347
1348       if ((free_mem_ptr / GUS_BANK_SIZE) !=
1349           ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1350         {
1351           unsigned long   tmp_mem =     /* Align to 256K*N */
1352           ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1353
1354           if ((tmp_mem + patch.len) > gus_mem_size)
1355             return RET_ERROR (ENOSPC);
1356
1357           free_mem_ptr = tmp_mem;       /* This leaves unusable memory */
1358         }
1359     }
1360
1361   if ((free_mem_ptr + patch.len) > gus_mem_size)
1362     return RET_ERROR (ENOSPC);
1363
1364   sample_ptrs[free_sample] = free_mem_ptr;
1365
1366   /* Tremolo is not possible with envelopes */
1367
1368   if (patch.mode & WAVE_ENVELOPES)
1369     patch.mode &= ~WAVE_TREMOLO;
1370
1371   memcpy ((char *) &samples[free_sample], &patch, sizeof (patch));
1372
1373   /*
1374    * Link this_one sample to the list of samples for patch 'instr'.
1375    */
1376
1377   samples[free_sample].key = patch_table[instr];
1378   patch_table[instr] = free_sample;
1379
1380   /*
1381    * Use DMA to transfer the wave data to the DRAM
1382    */
1383
1384   left = patch.len;
1385   src_offs = 0;
1386   target = free_mem_ptr;
1387
1388   while (left)                  /* Not all moved */
1389     {
1390       blk_size = sound_buffsizes[gus_devnum];
1391       if (blk_size > left)
1392         blk_size = left;
1393
1394       /*
1395        * DMA cannot cross 256k bank boundaries. Check for that.
1396        */
1397       blk_end = target + blk_size;
1398
1399       if ((target >> 18) != (blk_end >> 18))
1400         {                       /* Have to split the block */
1401
1402           blk_end &= ~(256 * 1024 - 1);
1403           blk_size = blk_end - target;
1404         }
1405
1406 #ifdef GUS_NO_DMA
1407       /*
1408        * For some reason the DMA is not possible. We have to use PIO.
1409        */
1410       {
1411         long            i;
1412         unsigned char   data;
1413
1414         for (i = 0; i < blk_size; i++)
1415           {
1416             GET_BYTE_FROM_USER (data, addr, sizeof (patch) + i);
1417             gus_poke (target + i, data);
1418           }
1419       }
1420 #else /* GUS_NO_DMA */
1421       {
1422         unsigned long   address, hold_address;
1423         unsigned char   dma_command;
1424
1425         /*
1426          * OK, move now. First in and then out.
1427          */
1428
1429         COPY_FROM_USER (snd_raw_buf[gus_devnum][0],
1430                         addr, sizeof (patch) + src_offs,
1431                         blk_size);
1432
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);
1436
1437         /*
1438          * Set the DRAM address for the wave data
1439          */
1440
1441         address = target;
1442
1443         if (sound_dsp_dmachan[gus_devnum] > 3)
1444           {
1445             hold_address = address;
1446             address = address >> 1;
1447             address &= 0x0001ffffL;
1448             address |= (hold_address & 0x000c0000L);
1449           }
1450
1451         gus_write16 (0x42, (address >> 4) & 0xffff);    /* DRAM DMA address */
1452
1453         /*
1454          * Start the DMA transfer
1455          */
1456
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 */
1464
1465         gus_write8 (0x41, dma_command); /* Let's go luteet (=bugs) */
1466
1467         /*
1468          * Sleep here until the DRAM DMA done interrupt is served
1469          */
1470         active_device = GUS_DEV_WAVE;
1471
1472         INTERRUPTIBLE_SLEEP_ON (dram_sleeper, dram_sleep_flag);
1473       }
1474 #endif /* GUS_NO_DMA */
1475
1476       /*
1477        * Now the next part
1478        */
1479
1480       left -= blk_size;
1481       src_offs += blk_size;
1482       target += blk_size;
1483
1484       gus_write8 (0x41, 0);     /* Stop DMA */
1485     }
1486
1487   free_mem_ptr += patch.len;
1488
1489   if (!pmgr_flag)
1490     pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1491   free_sample++;
1492   return 0;
1493 }
1494
1495 static void
1496 guswave_hw_control (int dev, unsigned char *event)
1497 {
1498   int             voice, cmd;
1499   unsigned short  p1, p2;
1500   unsigned long   plong, flags;
1501
1502   cmd = event[2];
1503   voice = event[3];
1504   p1 = *(unsigned short *) &event[4];
1505   p2 = *(unsigned short *) &event[6];
1506   plong = *(unsigned long *) &event[4];
1507
1508   switch (cmd)
1509     {
1510
1511     case _GUS_NUMVOICES:
1512       DISABLE_INTR (flags);
1513       gus_select_voice (voice);
1514       gus_select_max_voices (p1);
1515       RESTORE_INTR (flags);
1516       break;
1517
1518     case _GUS_VOICESAMPLE:
1519       guswave_set_instr (dev, voice, p1);
1520       break;
1521
1522     case _GUS_VOICEON:
1523       DISABLE_INTR (flags);
1524       gus_select_voice (voice);
1525       p1 &= ~0x20;              /* Disable intr */
1526       gus_voice_on (p1);
1527       RESTORE_INTR (flags);
1528       break;
1529
1530     case _GUS_VOICEOFF:
1531       DISABLE_INTR (flags);
1532       gus_select_voice (voice);
1533       gus_voice_off ();
1534       RESTORE_INTR (flags);
1535       break;
1536
1537     case _GUS_VOICEFADE:
1538       DISABLE_INTR (flags);
1539       gus_select_voice (voice);
1540       gus_voice_fade (voice);
1541       RESTORE_INTR (flags);
1542       break;
1543
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);
1550       break;
1551
1552     case _GUS_VOICEBALA:
1553       DISABLE_INTR (flags);
1554       gus_select_voice (voice);
1555       gus_voice_balance (p1);
1556       RESTORE_INTR (flags);
1557       break;
1558
1559     case _GUS_VOICEFREQ:
1560       DISABLE_INTR (flags);
1561       gus_select_voice (voice);
1562       gus_voice_freq (plong);
1563       RESTORE_INTR (flags);
1564       break;
1565
1566     case _GUS_VOICEVOL:
1567       DISABLE_INTR (flags);
1568       gus_select_voice (voice);
1569       gus_voice_volume (p1);
1570       RESTORE_INTR (flags);
1571       break;
1572
1573     case _GUS_VOICEVOL2:        /* Just update the voice value */
1574       voices[voice].initial_volume =
1575         voices[voice].current_volume = p1;
1576       break;
1577
1578     case _GUS_RAMPRANGE:
1579       if (voices[voice].mode & WAVE_ENVELOPES)
1580         break;                  /* NO-NO */
1581       DISABLE_INTR (flags);
1582       gus_select_voice (voice);
1583       gus_ramp_range (p1, p2);
1584       RESTORE_INTR (flags);
1585       break;
1586
1587     case _GUS_RAMPRATE:
1588       if (voices[voice].mode & WAVE_ENVELOPES)
1589         break;                  /* NO-NO */
1590       DISABLE_INTR (flags);
1591       gus_select_voice (voice);
1592       gus_ramp_rate (p1, p2);
1593       RESTORE_INTR (flags);
1594       break;
1595
1596     case _GUS_RAMPMODE:
1597       if (voices[voice].mode & WAVE_ENVELOPES)
1598         break;                  /* NO-NO */
1599       DISABLE_INTR (flags);
1600       gus_select_voice (voice);
1601       p1 &= ~0x20;              /* Disable intr */
1602       gus_ramp_mode (p1);
1603       RESTORE_INTR (flags);
1604       break;
1605
1606     case _GUS_RAMPON:
1607       if (voices[voice].mode & WAVE_ENVELOPES)
1608         break;                  /* NO-NO */
1609       DISABLE_INTR (flags);
1610       gus_select_voice (voice);
1611       p1 &= ~0x20;              /* Disable intr */
1612       gus_rampon (p1);
1613       RESTORE_INTR (flags);
1614       break;
1615
1616     case _GUS_RAMPOFF:
1617       if (voices[voice].mode & WAVE_ENVELOPES)
1618         break;                  /* NO-NO */
1619       DISABLE_INTR (flags);
1620       gus_select_voice (voice);
1621       gus_rampoff ();
1622       RESTORE_INTR (flags);
1623       break;
1624
1625     case _GUS_VOLUME_SCALE:
1626       volume_base = p1;
1627       volume_scale = p2;
1628       break;
1629
1630     default:;
1631     }
1632 }
1633
1634 static int
1635 gus_sampling_set_speed (int speed)
1636 {
1637   if (speed <= 0)
1638     return gus_sampling_speed;
1639
1640   if (speed > 44100)
1641     speed = 44100;
1642
1643   gus_sampling_speed = speed;
1644   return speed;
1645 }
1646
1647 static int
1648 gus_sampling_set_channels (int channels)
1649 {
1650   if (!channels)
1651     return gus_sampling_channels;
1652   if (channels > 2)
1653     channels = 2;
1654   if (channels < 1)
1655     channels = 1;
1656   gus_sampling_channels = channels;
1657   return channels;
1658 }
1659
1660 static int
1661 gus_sampling_set_bits (int bits)
1662 {
1663   if (!bits)
1664     return gus_sampling_bits;
1665
1666   if (bits != 8 && bits != 16)
1667     bits = 8;
1668
1669   gus_sampling_bits = bits;
1670   return bits;
1671 }
1672
1673 static int
1674 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
1675 {
1676   switch (cmd)
1677     {
1678     case SOUND_PCM_WRITE_RATE:
1679       if (local)
1680         return gus_sampling_set_speed (arg);
1681       return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1682       break;
1683
1684     case SOUND_PCM_READ_RATE:
1685       if (local)
1686         return gus_sampling_speed;
1687       return IOCTL_OUT (arg, gus_sampling_speed);
1688       break;
1689
1690     case SNDCTL_DSP_STEREO:
1691       if (local)
1692         return gus_sampling_set_channels (arg + 1) - 1;
1693       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1694       break;
1695
1696     case SOUND_PCM_WRITE_CHANNELS:
1697       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1698       break;
1699
1700     case SOUND_PCM_READ_CHANNELS:
1701       if (local)
1702         return gus_sampling_channels;
1703       return IOCTL_OUT (arg, gus_sampling_channels);
1704       break;
1705
1706     case SNDCTL_DSP_SAMPLESIZE:
1707       if (local)
1708         return gus_sampling_set_bits (arg);
1709       return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1710       break;
1711
1712     case SOUND_PCM_READ_BITS:
1713       if (local)
1714         return gus_sampling_bits;
1715       return IOCTL_OUT (arg, gus_sampling_bits);
1716
1717     case SOUND_PCM_WRITE_FILTER:        /* NOT YET IMPLEMENTED */
1718       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1719       break;
1720
1721     case SOUND_PCM_READ_FILTER:
1722       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1723       break;
1724
1725     default:
1726       return RET_ERROR (EINVAL);
1727     }
1728   return RET_ERROR (EINVAL);
1729 }
1730
1731 static void
1732 gus_sampling_reset (int dev)
1733 {
1734 }
1735
1736 static int
1737 gus_sampling_open (int dev, int mode)
1738 {
1739 #ifdef GUS_NO_DMA
1740   printk ("GUS: DMA mode not enabled. Device not supported\n");
1741   return RET_ERROR (ENXIO);
1742 #endif
1743
1744   if (gus_busy)
1745     return RET_ERROR (EBUSY);
1746
1747   gus_busy = 1;
1748   active_device = 0;
1749
1750   gus_reset ();
1751   reset_sample_memory ();
1752   gus_select_max_voices (14);
1753
1754   gus_sampling_set_bits (8);
1755   gus_sampling_set_channels (1);
1756   gus_sampling_set_speed (DSP_DEFAULT_SPEED);
1757   pcm_active = 0;
1758
1759   return 0;
1760 }
1761
1762 static void
1763 gus_sampling_close (int dev)
1764 {
1765   gus_reset ();
1766   gus_busy = 0;
1767   active_device = 0;
1768 }
1769
1770 static void
1771 play_next_pcm_block (void)
1772 {
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];
1778
1779   if (!pcm_qlen)
1780     return;
1781
1782   this_one = pcm_head;
1783
1784   for (chn = 0; chn < gus_sampling_channels; chn++)
1785     {
1786       mode[chn] = 0x00;
1787       ramp_mode[chn] = 0x03;    /* Ramping and rollover off */
1788
1789       if (chn == 0)
1790         {
1791           mode[chn] |= 0x20;    /* Loop irq */
1792           voices[chn].loop_irq_mode = LMODE_PCM;
1793         }
1794
1795       if (gus_sampling_bits != 8)
1796         {
1797           is16bits = 1;
1798           mode[chn] |= 0x04;    /* 16 bit data */
1799         }
1800       else
1801         is16bits = 0;
1802
1803       dram_loc = this_one * pcm_bsize;
1804       dram_loc += chn * pcm_banksize;
1805
1806       if (this_one == (pcm_nblk - 1))   /* Last of the DRAM buffers */
1807         {
1808           mode[chn] |= 0x08;    /* Enable loop */
1809           ramp_mode[chn] = 0x03;/* Disable rollover */
1810         }
1811       else
1812         {
1813           if (chn == 0)
1814             ramp_mode[chn] = 0x04;      /* Enable rollover bit */
1815         }
1816
1817       DISABLE_INTR (flags);
1818       gus_select_voice (chn);
1819       gus_voice_freq (speed);
1820
1821       if (gus_sampling_channels == 1)
1822         gus_voice_balance (7);  /* mono */
1823       else if (chn == 0)
1824         gus_voice_balance (0);  /* left */
1825       else
1826         gus_voice_balance (15); /* right */
1827
1828       if (!pcm_active)          /* Voice not started yet */
1829         {
1830           /*
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.
1835            */
1836
1837           gus_voice_off ();     /* It could already be running */
1838           gus_rampoff ();
1839           gus_voice_volume (4000);
1840           gus_ramp_range (65, 4030);
1841
1842           gus_write_addr (0x0a, dram_loc, is16bits);    /* Starting position */
1843           gus_write_addr (0x02, chn * pcm_banksize, is16bits);  /* Loop start location */
1844
1845           if (chn != 0)
1846             gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk),
1847                             is16bits);  /* Loop end location */
1848         }
1849
1850       if (chn == 0)
1851         gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);     /* Loop end location */
1852       else
1853         mode[chn] |= 0x08;      /* Enable loop */
1854
1855       if (pcm_datasize[this_one] != pcm_bsize)
1856         {
1857           /* Incomplete block. Possibly the last one. */
1858           if (chn == 0)
1859             {
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 */
1864             }
1865           else
1866             {
1867               gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);       /* Loop end location */
1868               mode[chn] &= ~0x08;       /* Disable loop */
1869             }
1870         }
1871
1872       RESTORE_INTR (flags);
1873     }
1874
1875   for (chn = 0; chn < gus_sampling_channels; chn++)
1876     {
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);
1882     }
1883
1884   pcm_active = 1;
1885 }
1886
1887 static void
1888 gus_transfer_output_block (int dev, unsigned long buf,
1889                            int total_count, int intrflag, int chn)
1890 {
1891   /*
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.
1895    * 
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.
1898    */
1899
1900   int             this_one, count;
1901   unsigned long   flags;
1902   unsigned char   dma_command;
1903   unsigned long   address, hold_address;
1904
1905   DISABLE_INTR (flags);
1906
1907   count = total_count / gus_sampling_channels;
1908
1909   if (chn == 0)
1910     {
1911       if (pcm_qlen >= pcm_nblk)
1912         printk ("GUS Warning: PCM buffers out of sync\n");
1913
1914       this_one = pcm_current_block = pcm_tail;
1915       pcm_qlen++;
1916       pcm_tail = (pcm_tail + 1) % pcm_nblk;
1917       pcm_datasize[this_one] = count;
1918     }
1919   else
1920     this_one = pcm_current_block;
1921
1922   gus_write8 (0x41, 0);         /* Disable GF1 DMA */
1923   DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
1924
1925   address = this_one * pcm_bsize;
1926   address += chn * pcm_banksize;
1927
1928   if (sound_dsp_dmachan[dev] > 3)
1929     {
1930       hold_address = address;
1931       address = address >> 1;
1932       address &= 0x0001ffffL;
1933       address |= (hold_address & 0x000c0000L);
1934     }
1935
1936   gus_write16 (0x42, (address >> 4) & 0xffff);  /* DRAM DMA address */
1937
1938   dma_command = 0x21;           /* IRQ enable, DMA start */
1939
1940   if (gus_sampling_bits != 8)
1941     dma_command |= 0x40;        /* 16 bit _DATA_ */
1942   else
1943     dma_command |= 0x80;        /* Invert MSB */
1944
1945   if (sound_dsp_dmachan[dev] > 3)
1946     dma_command |= 0x04;        /* 16 bit DMA channel */
1947
1948   gus_write8 (0x41, dma_command);       /* Kick on */
1949
1950   if (chn == (gus_sampling_channels - 1))       /* Last channel */
1951     {
1952       /* Last (right or mono) channel data */
1953       active_device = GUS_DEV_PCM_DONE;
1954       if (!pcm_active && (pcm_qlen > 2 || count < pcm_bsize))
1955         {
1956           play_next_pcm_block ();
1957         }
1958     }
1959   else                          /* Left channel data. The right channel is
1960                                  * transferred after DMA interrupt */
1961     active_device = GUS_DEV_PCM_CONTINUE;
1962
1963   RESTORE_INTR (flags);
1964 }
1965
1966 static void
1967 gus_sampling_output_block (int dev, unsigned long buf, int total_count, int intrflag)
1968 {
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);
1974 }
1975
1976 static void
1977 gus_sampling_start_input (int dev, unsigned long buf, int count, int intrflag)
1978 {
1979   unsigned long   flags;
1980   unsigned char   mode;
1981
1982   DISABLE_INTR (flags);
1983
1984   DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
1985
1986   mode = 0xa0;                  /* DMA IRQ enable, invert MSB */
1987
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 */
1993
1994   gus_write8 (0x49, mode);
1995
1996   RESTORE_INTR (flags);
1997 }
1998
1999 static int
2000 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
2001 {
2002   unsigned int    rate;
2003
2004   rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2005
2006   gus_write8 (0x48, rate & 0xff);       /* Set sampling frequency */
2007
2008   if (gus_sampling_bits != 8)
2009     {
2010       printk ("GUS Error: 16 bit recording not supported\n");
2011       return RET_ERROR (EINVAL);
2012     }
2013
2014   return 0;
2015 }
2016
2017 static int
2018 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
2019 {
2020   int             i;
2021
2022   long            mem_ptr, mem_size;
2023
2024   mem_ptr = 0;
2025   mem_size = gus_mem_size / gus_sampling_channels;
2026
2027   if (mem_size > (256 * 1024))
2028     mem_size = 256 * 1024;
2029
2030   pcm_bsize = bsize / gus_sampling_channels;
2031   pcm_head = pcm_tail = pcm_qlen = 0;
2032
2033   pcm_nblk = MAX_PCM_BUFFERS;
2034   if ((pcm_bsize * pcm_nblk) > mem_size)
2035     pcm_nblk = mem_size / pcm_bsize;
2036
2037   for (i = 0; i < pcm_nblk; i++)
2038     pcm_datasize[i] = 0;
2039
2040   pcm_banksize = pcm_nblk * pcm_bsize;
2041
2042   if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2043     pcm_nblk--;
2044
2045   return 0;
2046 }
2047
2048 static int
2049 gus_has_output_drained (int dev)
2050 {
2051   return !pcm_qlen;
2052 }
2053
2054 static void
2055 gus_copy_from_user (int dev, char *localbuf, int localoffs,
2056                     snd_rw_buf * userbuf, int useroffs, int len)
2057 {
2058   if (gus_sampling_channels == 1)
2059     COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2060   else if (gus_sampling_bits == 8)
2061     {
2062       int             in_left = useroffs;
2063       int             in_right = useroffs + 1;
2064       char           *out_left, *out_right;
2065       int             i;
2066
2067       len /= 2;
2068       localoffs /= 2;
2069       out_left = &localbuf[localoffs];
2070       out_right = out_left + pcm_bsize;
2071
2072       for (i = 0; i < len; i++)
2073         {
2074           GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2075           in_left += 2;
2076           GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2077           in_right += 2;
2078         }
2079     }
2080   else
2081     {
2082       int             in_left = useroffs;
2083       int             in_right = useroffs + 1;
2084       short          *out_left, *out_right;
2085       int             i;
2086
2087       len /= 4;
2088       localoffs /= 4;
2089
2090       out_left = (short *) &localbuf[localoffs];
2091       out_right = out_left + (pcm_bsize / 2);
2092
2093       for (i = 0; i < len; i++)
2094         {
2095           GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2096           in_left += 2;
2097           GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2098           in_right += 2;
2099         }
2100     }
2101 }
2102
2103 static struct audio_operations gus_sampling_operations =
2104 {
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,
2116   gus_copy_from_user
2117 };
2118
2119 static int
2120 guswave_patchmgr (int dev, struct patmgr_info *rec)
2121 {
2122   int             i, n;
2123
2124   switch (rec->command)
2125     {
2126     case PM_GET_DEVTYPE:
2127       rec->parm1 = PMTYPE_WAVE;
2128       return 0;
2129       break;
2130
2131     case PM_GET_NRPGM:
2132       rec->parm1 = MAX_PATCH;
2133       return 0;
2134       break;
2135
2136     case PM_GET_PGMMAP:
2137       rec->parm1 = MAX_PATCH;
2138
2139       for (i = 0; i < MAX_PATCH; i++)
2140         {
2141           int             ptr = patch_table[i];
2142
2143           rec->data.data8[i] = 0;
2144
2145           while (ptr >= 0 && ptr < free_sample)
2146             {
2147               rec->data.data8[i]++;
2148               ptr = samples[ptr].key;   /* Follow link */
2149             }
2150         }
2151       return 0;
2152       break;
2153
2154     case PM_GET_PGM_PATCHES:
2155       {
2156         int             ptr = patch_table[rec->parm1];
2157
2158         n = 0;
2159
2160         while (ptr >= 0 && ptr < free_sample)
2161           {
2162             rec->data.data32[n++] = ptr;
2163             ptr = samples[ptr].key;     /* Follow link */
2164           }
2165       }
2166       rec->parm1 = n;
2167       return 0;
2168       break;
2169
2170     case PM_GET_PATCH:
2171       {
2172         int             ptr = rec->parm1;
2173         struct patch_info *pat;
2174
2175         if (ptr < 0 || ptr >= free_sample)
2176           return RET_ERROR (EINVAL);
2177
2178         memcpy (rec->data.data8, (char *) &samples[ptr],
2179                 sizeof (struct patch_info));
2180
2181         pat = (struct patch_info *) rec->data.data8;
2182
2183         pat->key = GUS_PATCH;   /* Restore patch type */
2184         rec->parm1 = sample_ptrs[ptr];  /* DRAM address */
2185         rec->parm2 = sizeof (struct patch_info);
2186       }
2187       return 0;
2188       break;
2189
2190     case PM_SET_PATCH:
2191       {
2192         int             ptr = rec->parm1;
2193         struct patch_info *pat;
2194
2195         if (ptr < 0 || ptr >= free_sample)
2196           return RET_ERROR (EINVAL);
2197
2198         pat = (struct patch_info *) rec->data.data8;
2199
2200         if (pat->len > samples[ptr].len)        /* Cannot expand sample */
2201           return RET_ERROR (EINVAL);
2202
2203         pat->key = samples[ptr].key;    /* Ensure the link is correct */
2204
2205         memcpy ((char *) &samples[ptr], rec->data.data8,
2206                 sizeof (struct patch_info));
2207
2208         pat->key = GUS_PATCH;
2209       }
2210       return 0;
2211       break;
2212
2213     case PM_READ_PATCH: /* Returns a block of wave data from the DRAM */
2214       {
2215         int             sample = rec->parm1;
2216         int             n;
2217         long            offs = rec->parm2;
2218         int             l = rec->parm3;
2219
2220         if (sample < 0 || sample >= free_sample)
2221           return RET_ERROR (EINVAL);
2222
2223         if (offs < 0 || offs >= samples[sample].len)
2224           return RET_ERROR (EINVAL);    /* Invalid offset */
2225
2226         n = samples[sample].len - offs; /* Nr of bytes left */
2227
2228         if (l > n)
2229           l = n;
2230
2231         if (l > sizeof (rec->data.data8))
2232           l = sizeof (rec->data.data8);
2233
2234         if (l <= 0)
2235           return RET_ERROR (EINVAL);    /* Was there a bug? */
2236
2237         offs += sample_ptrs[sample];    /* Begin offsess + offset to DRAM */
2238
2239         for (n = 0; n < l; n++)
2240           rec->data.data8[n] = gus_peek (offs++);
2241         rec->parm1 = n;         /* Nr of bytes copied */
2242       }
2243       return 0;
2244       break;
2245
2246     case PM_WRITE_PATCH:        /* Writes a block of wave data to the DRAM */
2247       {
2248         int             sample = rec->parm1;
2249         int             n;
2250         long            offs = rec->parm2;
2251         int             l = rec->parm3;
2252
2253         if (sample < 0 || sample >= free_sample)
2254           return RET_ERROR (EINVAL);
2255
2256         if (offs < 0 || offs >= samples[sample].len)
2257           return RET_ERROR (EINVAL);    /* Invalid offset */
2258
2259         n = samples[sample].len - offs; /* Nr of bytes left */
2260
2261         if (l > n)
2262           l = n;
2263
2264         if (l > sizeof (rec->data.data8))
2265           l = sizeof (rec->data.data8);
2266
2267         if (l <= 0)
2268           return RET_ERROR (EINVAL);    /* Was there a bug? */
2269
2270         offs += sample_ptrs[sample];    /* Begin offsess + offset to DRAM */
2271
2272         for (n = 0; n < l; n++)
2273           gus_poke (offs++, rec->data.data8[n]);
2274         rec->parm1 = n;         /* Nr of bytes copied */
2275       }
2276       return 0;
2277       break;
2278
2279     default:
2280       return RET_ERROR (EINVAL);
2281     }
2282 }
2283
2284 static struct synth_operations guswave_operations =
2285 {
2286   &gus_info,
2287   SYNTH_TYPE_SAMPLE,
2288   SAMPLE_TYPE_GUS,
2289   guswave_open,
2290   guswave_close,
2291   guswave_ioctl,
2292   guswave_kill_note,
2293   guswave_start_note,
2294   guswave_set_instr,
2295   guswave_reset,
2296   guswave_hw_control,
2297   guswave_load_patch,
2298   guswave_aftertouch,
2299   guswave_controller,
2300   guswave_panning,
2301   guswave_patchmgr
2302 };
2303
2304 long
2305 gus_wave_init (long mem_start, int irq, int dma)
2306 {
2307   printk (" <Gravis UltraSound %dk>", gus_mem_size / 1024);
2308
2309   if (irq < 0 || irq > 15)
2310     {
2311       printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2312       return mem_start;
2313     }
2314
2315   if (dma < 0 || dma > 7)
2316     {
2317       printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2318       return mem_start;
2319     }
2320
2321   gus_irq = irq;
2322   gus_dma = dma;
2323
2324   if (num_synths >= MAX_SYNTH_DEV)
2325     printk ("GUS Error: Too many synthesizers\n");
2326   else
2327     synth_devs[num_synths++] = &guswave_operations;
2328
2329   reset_sample_memory ();
2330
2331   gus_initialize ();
2332
2333   if (num_dspdevs < MAX_DSP_DEV)
2334     {
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;
2340     }
2341   else
2342     printk ("GUS: Too many PCM devices available\n");
2343
2344   return mem_start;
2345 }
2346
2347 static void
2348 do_loop_irq (int voice)
2349 {
2350   unsigned char   tmp;
2351   int             mode, parm;
2352   unsigned long   flags;
2353
2354   DISABLE_INTR (flags);
2355   gus_select_voice (voice);
2356
2357   tmp = gus_read8 (0x00);
2358   tmp &= ~0x20;                 /* Disable wave IRQ for this_one voice */
2359   gus_write8 (0x00, tmp);
2360
2361   mode = voices[voice].loop_irq_mode;
2362   voices[voice].loop_irq_mode = 0;
2363   parm = voices[voice].loop_irq_parm;
2364
2365   switch (mode)
2366     {
2367
2368     case LMODE_FINISH:          /* Final loop finished, shoot volume down */
2369
2370       if ((gus_read16 (0x09) >> 4) < 100)       /* Get current volume */
2371         {
2372           gus_voice_off ();
2373           gus_rampoff ();
2374           gus_voice_init (voice);
2375           return;
2376         }
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;
2381       break;
2382
2383     case LMODE_PCM_STOP:
2384       pcm_active = 0;           /* Requires extensive processing */
2385     case LMODE_PCM:
2386       {
2387         int             orig_qlen = pcm_qlen;
2388
2389         pcm_qlen--;
2390         pcm_head = (pcm_head + 1) % pcm_nblk;
2391         if (pcm_qlen)
2392           {
2393             play_next_pcm_block ();
2394           }
2395         else
2396           {                     /* Out of data. Just stop the voice */
2397             gus_voice_off ();
2398             gus_rampoff ();
2399             pcm_active = 0;
2400           }
2401
2402         if (orig_qlen == pcm_nblk)
2403           {
2404             DMAbuf_outputintr (gus_devnum);
2405           }
2406       }
2407       break;
2408
2409     default:;
2410     }
2411   RESTORE_INTR (flags);
2412 }
2413
2414 static void
2415 do_volume_irq (int voice)
2416 {
2417   unsigned char   tmp;
2418   int             mode, parm;
2419   unsigned long   flags;
2420
2421   DISABLE_INTR (flags);
2422
2423   gus_select_voice (voice);
2424
2425   tmp = gus_read8 (0x0d);
2426   tmp &= ~0x20;                 /* Disable volume ramp IRQ */
2427   gus_write8 (0x0d, tmp);
2428
2429   mode = voices[voice].volume_irq_mode;
2430   voices[voice].volume_irq_mode = 0;
2431   parm = voices[voice].volume_irq_parm;
2432
2433   switch (mode)
2434     {
2435     case VMODE_HALT:            /* Decay phase finished */
2436       gus_voice_init (voice);
2437       break;
2438
2439     case VMODE_ENVELOPE:
2440       gus_rampoff ();
2441       step_envelope (voice);
2442       break;
2443
2444     default:;
2445     }
2446
2447   RESTORE_INTR (flags);
2448 }
2449
2450 void
2451 gus_voice_irq (void)
2452 {
2453   unsigned long   wave_ignore = 0, volume_ignore = 0;
2454   unsigned long   voice_bit;
2455
2456   unsigned char   src, voice;
2457
2458   while (1)
2459     {
2460       src = gus_read8 (0x0f);   /* Get source info */
2461       voice = src & 0x1f;
2462       src &= 0xc0;
2463
2464       if (src == (0x80 | 0x40))
2465         return;                 /* No interrupt */
2466
2467       voice_bit = 1 << voice;
2468
2469       if (!(src & 0x80))        /* Wave IRQ pending */
2470         if (!(wave_ignore & voice_bit) && voice < nr_voices)    /* Not done yet */
2471           {
2472             wave_ignore |= voice_bit;
2473             do_loop_irq (voice);
2474           }
2475
2476       if (!(src & 0x40))        /* Volume IRQ pending */
2477         if (!(volume_ignore & voice_bit) && voice < nr_voices)  /* Not done yet */
2478           {
2479             volume_ignore |= voice_bit;
2480             do_volume_irq (voice);
2481           }
2482     }
2483 }
2484
2485 void
2486 guswave_dma_irq (void)
2487 {
2488   unsigned char   status;
2489
2490   status = gus_look8 (0x41);    /* Get DMA IRQ Status */
2491   if (status & 0x40)            /* DMA Irq pending */
2492     switch (active_device)
2493       {
2494       case GUS_DEV_WAVE:
2495         if (dram_sleep_flag)
2496           WAKE_UP (dram_sleeper);
2497         break;
2498
2499       case GUS_DEV_PCM_CONTINUE:
2500         gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
2501                                    pcm_current_count,
2502                                    pcm_current_intrflag, 1);
2503         break;
2504
2505       case GUS_DEV_PCM_DONE:
2506         if (pcm_qlen < pcm_nblk)
2507           {
2508             DMAbuf_outputintr (gus_devnum);
2509           }
2510         break;
2511
2512       default:;
2513       }
2514
2515   status = gus_look8 (0x49);    /* Get Sampling IRQ Status */
2516   if (status & 0x40)            /* Sampling Irq pending */
2517     {
2518       DMAbuf_inputintr (gus_devnum);
2519     }
2520
2521 }
2522
2523 #endif