root/drivers/sound/gus_wave.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. reset_sample_memory
  2. gus_delay
  3. gus_poke
  4. gus_peek
  5. gus_write8
  6. gus_read8
  7. gus_look8
  8. gus_write16
  9. gus_read16
  10. gus_write_addr
  11. gus_select_voice
  12. gus_select_max_voices
  13. gus_voice_on
  14. gus_voice_off
  15. gus_voice_mode
  16. gus_voice_freq
  17. gus_voice_volume
  18. gus_voice_balance
  19. gus_ramp_range
  20. gus_ramp_rate
  21. gus_rampon
  22. gus_ramp_mode
  23. gus_rampoff
  24. gus_set_voice_pos
  25. gus_voice_init
  26. gus_voice_init2
  27. step_envelope
  28. init_envelope
  29. start_release
  30. gus_voice_fade
  31. gus_reset
  32. gus_initialize
  33. gus_wave_detect
  34. guswave_ioctl
  35. guswave_set_instr
  36. guswave_kill_note
  37. guswave_aftertouch
  38. guswave_panning
  39. guswave_volume_method
  40. compute_volume
  41. compute_and_set_volume
  42. dynamic_volume_change
  43. guswave_controller
  44. guswave_start_note2
  45. guswave_start_note
  46. guswave_reset
  47. guswave_open
  48. guswave_close
  49. guswave_load_patch
  50. guswave_hw_control
  51. gus_sampling_set_speed
  52. gus_sampling_set_channels
  53. gus_sampling_set_bits
  54. gus_sampling_ioctl
  55. gus_sampling_reset
  56. gus_sampling_open
  57. gus_sampling_close
  58. gus_sampling_update_volume
  59. play_next_pcm_block
  60. gus_transfer_output_block
  61. gus_sampling_output_block
  62. gus_sampling_start_input
  63. gus_sampling_prepare_for_input
  64. gus_sampling_prepare_for_output
  65. gus_local_qlen
  66. gus_copy_from_user
  67. guswave_setup_voice
  68. guswave_bender
  69. guswave_patchmgr
  70. guswave_alloc
  71. set_input_volumes
  72. gus_default_mixer_ioctl
  73. gus_default_mixer_init
  74. gus_wave_init
  75. do_loop_irq
  76. do_volume_irq
  77. gus_voice_irq
  78. guswave_dma_irq

   1 /*
   2  * sound/gus_wave.c
   3  *
   4  * Driver for the Gravis UltraSound wave table synth.
   5  *
   6  * Copyright by Hannu Savolainen 1993, 1994
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions are
  10  * met: 1. Redistributions of source code must retain the above copyright
  11  * notice, this list of conditions and the following disclaimer. 2.
  12  * Redistributions in binary form must reproduce the above copyright notice,
  13  * this list of conditions and the following disclaimer in the documentation
  14  * and/or other materials provided with the distribution.
  15  *
  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26  * SUCH DAMAGE.
  27  *
  28  */
  29 
  30 #include "sound_config.h"
  31 #include <linux/ultrasound.h>
  32 #include "gus_hw.h"
  33 
  34 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
  35 
  36 #define MAX_SAMPLE      150
  37 #define MAX_PATCH       256
  38 
  39 struct voice_info
  40   {
  41     unsigned long   orig_freq;
  42     unsigned long   current_freq;
  43     unsigned long   mode;
  44     int             bender;
  45     int             bender_range;
  46     int             panning;
  47     int             midi_volume;
  48     unsigned int    initial_volume;
  49     unsigned int    current_volume;
  50     int             loop_irq_mode, loop_irq_parm;
  51 #define LMODE_FINISH            1
  52 #define LMODE_PCM               2
  53 #define LMODE_PCM_STOP          3
  54     int             volume_irq_mode, volume_irq_parm;
  55 #define VMODE_HALT              1
  56 #define VMODE_ENVELOPE          2
  57 #define VMODE_START_NOTE        3
  58 
  59     int             env_phase;
  60     unsigned char   env_rate[6];
  61     unsigned char   env_offset[6];
  62 
  63     /*
  64      * Volume computation parameters for gus_adagio_vol()
  65      */
  66     int             main_vol, expression_vol, patch_vol;
  67 
  68     /* Variables for "Ultraclick" removal */
  69     int             dev_pending, note_pending, volume_pending, sample_pending;
  70     char            kill_pending;
  71     long            offset_pending;
  72 
  73   };
  74 
  75 static struct voice_alloc_info *voice_alloc;
  76 
  77 extern int      gus_base;
  78 extern int      gus_irq, gus_dma;
  79 static int      gusmax_dma = -1;
  80 static int      dual_dma_mode = 0;
  81 static long     gus_mem_size = 0;
  82 static long     free_mem_ptr = 0;
  83 static int      gus_busy = 0;
  84 static int      gus_no_dma = 0;
  85 static int      nr_voices = 0;
  86 static int      gus_devnum = 0;
  87 static int      volume_base, volume_scale, volume_method;
  88 static int      gus_recmask = SOUND_MASK_MIC;
  89 static int      recording_active = 0;
  90 static int      only_read_access = 0;
  91 
  92 int             gus_wave_volume = 60;
  93 int             gus_pcm_volume = 80;
  94 int             have_gus_max = 0;
  95 static int      gus_line_vol = 100, gus_mic_vol = 0;
  96 static unsigned char mix_image = 0x00;
  97 
  98 /*
  99  * Current version of this driver doesn't allow synth and PCM functions
 100  * at the same time. The active_device specifies the active driver
 101  */
 102 static int      active_device = 0;
 103 
 104 #define GUS_DEV_WAVE            1       /* Wave table synth */
 105 #define GUS_DEV_PCM_DONE        2       /* PCM device, transfer done */
 106 #define GUS_DEV_PCM_CONTINUE    3       /* PCM device, transfer done ch. 1/2 */
 107 
 108 static int      gus_sampling_speed;
 109 static int      gus_sampling_channels;
 110 static int      gus_sampling_bits;
 111 
 112 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
 113 
 114 /*
 115  * Variables and buffers for PCM output
 116  */
 117 #define MAX_PCM_BUFFERS         (32*MAX_REALTIME_FACTOR)        /* Don't change */
 118 
 119 static int      pcm_bsize, pcm_nblk, pcm_banksize;
 120 static int      pcm_datasize[MAX_PCM_BUFFERS];
 121 static volatile int pcm_head, pcm_tail, pcm_qlen;
 122 static volatile int pcm_active;
 123 static volatile int dma_active;
 124 static int      pcm_opened = 0;
 125 static int      pcm_current_dev;
 126 static int      pcm_current_block;
 127 static unsigned long pcm_current_buf;
 128 static int      pcm_current_count;
 129 static int      pcm_current_intrflag;
 130 
 131 struct voice_info voices[32];
 132 
 133 static int      freq_div_table[] =
 134 {
 135   44100,                        /* 14 */
 136   41160,                        /* 15 */
 137   38587,                        /* 16 */
 138   36317,                        /* 17 */
 139   34300,                        /* 18 */
 140   32494,                        /* 19 */
 141   30870,                        /* 20 */
 142   29400,                        /* 21 */
 143   28063,                        /* 22 */
 144   26843,                        /* 23 */
 145   25725,                        /* 24 */
 146   24696,                        /* 25 */
 147   23746,                        /* 26 */
 148   22866,                        /* 27 */
 149   22050,                        /* 28 */
 150   21289,                        /* 29 */
 151   20580,                        /* 30 */
 152   19916,                        /* 31 */
 153   19293                         /* 32 */
 154 };
 155 
 156 static struct patch_info *samples;
 157 static long     sample_ptrs[MAX_SAMPLE + 1];
 158 static int      sample_map[32];
 159 static int      free_sample;
 160 
 161 
 162 static int      patch_table[MAX_PATCH];
 163 static int      patch_map[32];
 164 
 165 static struct synth_info gus_info =
 166 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
 167 
 168 static void     gus_poke (long addr, unsigned char data);
 169 static void     compute_and_set_volume (int voice, int volume, int ramp_time);
 170 extern unsigned short gus_adagio_vol (int vel, int mainv, int xpn, int voicev);
 171 extern unsigned short gus_linear_vol (int vol, int mainvol);
 172 static void     compute_volume (int voice, int volume);
 173 static void     do_volume_irq (int voice);
 174 static void     set_input_volumes (void);
 175 
 176 #define INSTANT_RAMP            -1      /* Instant change. No ramping */
 177 #define FAST_RAMP               0       /* Fastest possible ramp */
 178 
 179 static void
 180 reset_sample_memory (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182   int             i;
 183 
 184   for (i = 0; i <= MAX_SAMPLE; i++)
 185     sample_ptrs[i] = -1;
 186   for (i = 0; i < 32; i++)
 187     sample_map[i] = -1;
 188   for (i = 0; i < 32; i++)
 189     patch_map[i] = -1;
 190 
 191   gus_poke (0, 0);              /* Put a silent sample to the beginning */
 192   gus_poke (1, 0);
 193   free_mem_ptr = 2;
 194 
 195   free_sample = 0;
 196 
 197   for (i = 0; i < MAX_PATCH; i++)
 198     patch_table[i] = -1;
 199 }
 200 
 201 void
 202 gus_delay (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 203 {
 204   int             i;
 205 
 206   for (i = 0; i < 7; i++)
 207     INB (u_DRAMIO);
 208 }
 209 
 210 static void
 211 gus_poke (long addr, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {                               /* Writes a byte to the DRAM */
 213   unsigned long   flags;
 214 
 215   DISABLE_INTR (flags);
 216   OUTB (0x43, u_Command);
 217   OUTB (addr & 0xff, u_DataLo);
 218   OUTB ((addr >> 8) & 0xff, u_DataHi);
 219 
 220   OUTB (0x44, u_Command);
 221   OUTB ((addr >> 16) & 0xff, u_DataHi);
 222   OUTB (data, u_DRAMIO);
 223   RESTORE_INTR (flags);
 224 }
 225 
 226 static unsigned char
 227 gus_peek (long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 228 {                               /* Reads a byte from the DRAM */
 229   unsigned long   flags;
 230   unsigned char   tmp;
 231 
 232   DISABLE_INTR (flags);
 233   OUTB (0x43, u_Command);
 234   OUTB (addr & 0xff, u_DataLo);
 235   OUTB ((addr >> 8) & 0xff, u_DataHi);
 236 
 237   OUTB (0x44, u_Command);
 238   OUTB ((addr >> 16) & 0xff, u_DataHi);
 239   tmp = INB (u_DRAMIO);
 240   RESTORE_INTR (flags);
 241 
 242   return tmp;
 243 }
 244 
 245 void
 246 gus_write8 (int reg, unsigned int data)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {                               /* Writes to an indirect register (8 bit) */
 248   unsigned long   flags;
 249 
 250   DISABLE_INTR (flags);
 251 
 252   OUTB (reg, u_Command);
 253   OUTB ((unsigned char) (data & 0xff), u_DataHi);
 254 
 255   RESTORE_INTR (flags);
 256 }
 257 
 258 unsigned char
 259 gus_read8 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 260 {                               /* Reads from an indirect register (8 bit). Offset 0x80. */
 261   unsigned long   flags;
 262   unsigned char   val;
 263 
 264   DISABLE_INTR (flags);
 265   OUTB (reg | 0x80, u_Command);
 266   val = INB (u_DataHi);
 267   RESTORE_INTR (flags);
 268 
 269   return val;
 270 }
 271 
 272 unsigned char
 273 gus_look8 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 274 {                               /* Reads from an indirect register (8 bit). No additional offset. */
 275   unsigned long   flags;
 276   unsigned char   val;
 277 
 278   DISABLE_INTR (flags);
 279   OUTB (reg, u_Command);
 280   val = INB (u_DataHi);
 281   RESTORE_INTR (flags);
 282 
 283   return val;
 284 }
 285 
 286 void
 287 gus_write16 (int reg, unsigned int data)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {                               /* Writes to an indirect register (16 bit) */
 289   unsigned long   flags;
 290 
 291   DISABLE_INTR (flags);
 292 
 293   OUTB (reg, u_Command);
 294 
 295   OUTB ((unsigned char) (data & 0xff), u_DataLo);
 296   OUTB ((unsigned char) ((data >> 8) & 0xff), u_DataHi);
 297 
 298   RESTORE_INTR (flags);
 299 }
 300 
 301 unsigned short
 302 gus_read16 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {                               /* Reads from an indirect register (16 bit). Offset 0x80. */
 304   unsigned long   flags;
 305   unsigned char   hi, lo;
 306 
 307   DISABLE_INTR (flags);
 308 
 309   OUTB (reg | 0x80, u_Command);
 310 
 311   lo = INB (u_DataLo);
 312   hi = INB (u_DataHi);
 313 
 314   RESTORE_INTR (flags);
 315 
 316   return ((hi << 8) & 0xff00) | lo;
 317 }
 318 
 319 void
 320 gus_write_addr (int reg, unsigned long address, int is16bit)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {                               /* Writes an 24 bit memory address */
 322   unsigned long   hold_address;
 323   unsigned long   flags;
 324 
 325   DISABLE_INTR (flags);
 326   if (is16bit)
 327     {
 328       /*
 329        * Special processing required for 16 bit patches
 330        */
 331 
 332       hold_address = address;
 333       address = address >> 1;
 334       address &= 0x0001ffffL;
 335       address |= (hold_address & 0x000c0000L);
 336     }
 337 
 338   gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
 339   gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
 340   /* Could writing twice fix problems with GUS_VOICE_POS() ? Lets try... */
 341   gus_delay ();
 342   gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
 343   gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
 344   RESTORE_INTR (flags);
 345 }
 346 
 347 static void
 348 gus_select_voice (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350   if (voice < 0 || voice > 31)
 351     return;
 352 
 353   OUTB (voice, u_Voice);
 354 }
 355 
 356 static void
 357 gus_select_max_voices (int nvoices)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359   if (nvoices < 14)
 360     nvoices = 14;
 361   if (nvoices > 32)
 362     nvoices = 32;
 363 
 364   voice_alloc->max_voice = nr_voices = nvoices;
 365 
 366   gus_write8 (0x0e, (nvoices - 1) | 0xc0);
 367 }
 368 
 369 static void
 370 gus_voice_on (unsigned int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 371 {
 372   gus_write8 (0x00, (unsigned char) (mode & 0xfc));
 373   gus_delay ();
 374   gus_write8 (0x00, (unsigned char) (mode & 0xfc));
 375 }
 376 
 377 static void
 378 gus_voice_off (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380   gus_write8 (0x00, gus_read8 (0x00) | 0x03);
 381 }
 382 
 383 static void
 384 gus_voice_mode (unsigned int m)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386   unsigned char   mode = (unsigned char) (m & 0xff);
 387 
 388   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) |
 389               (mode & 0xfc));   /* Don't touch last two bits */
 390   gus_delay ();
 391   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
 392 }
 393 
 394 static void
 395 gus_voice_freq (unsigned long freq)
     /* [previous][next][first][last][top][bottom][index][help] */
 396 {
 397   unsigned long   divisor = freq_div_table[nr_voices - 14];
 398   unsigned short  fc;
 399 
 400   fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
 401   fc = fc << 1;
 402 
 403   gus_write16 (0x01, fc);
 404 }
 405 
 406 static void
 407 gus_voice_volume (unsigned int vol)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409   gus_write8 (0x0d, 0x03);      /* Stop ramp before setting volume */
 410   gus_write16 (0x09, (unsigned short) (vol << 4));
 411 }
 412 
 413 static void
 414 gus_voice_balance (unsigned int balance)
     /* [previous][next][first][last][top][bottom][index][help] */
 415 {
 416   gus_write8 (0x0c, (unsigned char) (balance & 0xff));
 417 }
 418 
 419 static void
 420 gus_ramp_range (unsigned int low, unsigned int high)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422   gus_write8 (0x07, (unsigned char) ((low >> 4) & 0xff));
 423   gus_write8 (0x08, (unsigned char) ((high >> 4) & 0xff));
 424 }
 425 
 426 static void
 427 gus_ramp_rate (unsigned int scale, unsigned int rate)
     /* [previous][next][first][last][top][bottom][index][help] */
 428 {
 429   gus_write8 (0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
 430 }
 431 
 432 static void
 433 gus_rampon (unsigned int m)
     /* [previous][next][first][last][top][bottom][index][help] */
 434 {
 435   unsigned char   mode = (unsigned char) (m & 0xff);
 436 
 437   gus_write8 (0x0d, mode & 0xfc);
 438   gus_delay ();
 439   gus_write8 (0x0d, mode & 0xfc);
 440 }
 441 
 442 static void
 443 gus_ramp_mode (unsigned int m)
     /* [previous][next][first][last][top][bottom][index][help] */
 444 {
 445   unsigned char   mode = (unsigned char) (m & 0xff);
 446 
 447   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) |
 448               (mode & 0xfc));   /* Leave the last 2 bits alone */
 449   gus_delay ();
 450   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
 451 }
 452 
 453 static void
 454 gus_rampoff (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 455 {
 456   gus_write8 (0x0d, 0x03);
 457 }
 458 
 459 static void
 460 gus_set_voice_pos (int voice, long position)
     /* [previous][next][first][last][top][bottom][index][help] */
 461 {
 462   int             sample_no;
 463 
 464   if ((sample_no = sample_map[voice]) != -1)
 465     if (position < samples[sample_no].len)
 466       if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
 467         voices[voice].offset_pending = position;
 468       else
 469         gus_write_addr (0x0a, sample_ptrs[sample_no] + position,
 470                         samples[sample_no].mode & WAVE_16_BITS);
 471 }
 472 
 473 static void
 474 gus_voice_init (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476   unsigned long   flags;
 477 
 478   DISABLE_INTR (flags);
 479   gus_select_voice (voice);
 480   gus_voice_volume (0);
 481   gus_voice_off ();
 482   gus_write_addr (0x0a, 0, 0);  /* Set current position to 0 */
 483   gus_write8 (0x00, 0x03);      /* Voice off */
 484   gus_write8 (0x0d, 0x03);      /* Ramping off */
 485   voice_alloc->map[voice] = 0;
 486   voice_alloc->alloc_times[voice] = 0;
 487   RESTORE_INTR (flags);
 488 
 489 }
 490 
 491 static void
 492 gus_voice_init2 (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 493 {
 494   voices[voice].panning = 0;
 495   voices[voice].mode = 0;
 496   voices[voice].orig_freq = 20000;
 497   voices[voice].current_freq = 20000;
 498   voices[voice].bender = 0;
 499   voices[voice].bender_range = 200;
 500   voices[voice].initial_volume = 0;
 501   voices[voice].current_volume = 0;
 502   voices[voice].loop_irq_mode = 0;
 503   voices[voice].loop_irq_parm = 0;
 504   voices[voice].volume_irq_mode = 0;
 505   voices[voice].volume_irq_parm = 0;
 506   voices[voice].env_phase = 0;
 507   voices[voice].main_vol = 127;
 508   voices[voice].patch_vol = 127;
 509   voices[voice].expression_vol = 127;
 510   voices[voice].sample_pending = -1;
 511 }
 512 
 513 static void
 514 step_envelope (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 515 {
 516   unsigned        vol, prev_vol, phase;
 517   unsigned char   rate;
 518   long int        flags;
 519 
 520   if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
 521     {
 522       DISABLE_INTR (flags);
 523       gus_select_voice (voice);
 524       gus_rampoff ();
 525       RESTORE_INTR (flags);
 526       return;
 527       /*
 528        * Sustain phase begins. Continue envelope after receiving note off.
 529        */
 530     }
 531 
 532   if (voices[voice].env_phase >= 5)
 533     {                           /* Envelope finished. Shoot the voice down */
 534       gus_voice_init (voice);
 535       return;
 536     }
 537 
 538   prev_vol = voices[voice].current_volume;
 539   phase = ++voices[voice].env_phase;
 540   compute_volume (voice, voices[voice].midi_volume);
 541   vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
 542   rate = voices[voice].env_rate[phase];
 543 
 544   DISABLE_INTR (flags);
 545   gus_select_voice (voice);
 546 
 547   gus_voice_volume (prev_vol);
 548 
 549 
 550   gus_write8 (0x06, rate);      /* Ramping rate */
 551 
 552   voices[voice].volume_irq_mode = VMODE_ENVELOPE;
 553 
 554   if (((vol - prev_vol) / 64) == 0)     /* No significant volume change */
 555     {
 556       RESTORE_INTR (flags);
 557       step_envelope (voice);    /* Continue the envelope on the next step */
 558       return;
 559     }
 560 
 561   if (vol > prev_vol)
 562     {
 563       if (vol >= (4096 - 64))
 564         vol = 4096 - 65;
 565       gus_ramp_range (0, vol);
 566       gus_rampon (0x20);        /* Increasing volume, with IRQ */
 567     }
 568   else
 569     {
 570       if (vol <= 64)
 571         vol = 65;
 572       gus_ramp_range (vol, 4030);
 573       gus_rampon (0x60);        /* Decreasing volume, with IRQ */
 574     }
 575   voices[voice].current_volume = vol;
 576   RESTORE_INTR (flags);
 577 }
 578 
 579 static void
 580 init_envelope (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 581 {
 582   voices[voice].env_phase = -1;
 583   voices[voice].current_volume = 64;
 584 
 585   step_envelope (voice);
 586 }
 587 
 588 static void
 589 start_release (int voice, long int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 590 {
 591   if (gus_read8 (0x00) & 0x03)
 592     return;                     /* Voice already stopped */
 593 
 594   voices[voice].env_phase = 2;  /* Will be incremented by step_envelope */
 595 
 596   voices[voice].current_volume =
 597     voices[voice].initial_volume =
 598     gus_read16 (0x09) >> 4;     /* Get current volume */
 599 
 600   voices[voice].mode &= ~WAVE_SUSTAIN_ON;
 601   gus_rampoff ();
 602   RESTORE_INTR (flags);
 603   step_envelope (voice);
 604 }
 605 
 606 static void
 607 gus_voice_fade (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609   int             instr_no = sample_map[voice], is16bits;
 610   long int        flags;
 611 
 612   DISABLE_INTR (flags);
 613   gus_select_voice (voice);
 614 
 615   if (instr_no < 0 || instr_no > MAX_SAMPLE)
 616     {
 617       gus_write8 (0x00, 0x03);  /* Hard stop */
 618       voice_alloc->map[voice] = 0;
 619       RESTORE_INTR (flags);
 620       return;
 621     }
 622 
 623   is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0;   /* 8 or 16 bits */
 624 
 625   if (voices[voice].mode & WAVE_ENVELOPES)
 626     {
 627       start_release (voice, flags);
 628       return;
 629     }
 630 
 631   /*
 632    * Ramp the volume down but not too quickly.
 633    */
 634   if ((int) (gus_read16 (0x09) >> 4) < 100)     /* Get current volume */
 635     {
 636       gus_voice_off ();
 637       gus_rampoff ();
 638       gus_voice_init (voice);
 639       return;
 640     }
 641 
 642   gus_ramp_range (65, 4030);
 643   gus_ramp_rate (2, 4);
 644   gus_rampon (0x40 | 0x20);     /* Down, once, with IRQ */
 645   voices[voice].volume_irq_mode = VMODE_HALT;
 646   RESTORE_INTR (flags);
 647 }
 648 
 649 static void
 650 gus_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652   int             i;
 653 
 654   gus_select_max_voices (24);
 655   volume_base = 3071;
 656   volume_scale = 4;
 657   volume_method = VOL_METHOD_ADAGIO;
 658 
 659   for (i = 0; i < 32; i++)
 660     {
 661       gus_voice_init (i);       /* Turn voice off */
 662       gus_voice_init2 (i);
 663     }
 664 
 665   INB (u_Status);               /* Touch the status register */
 666 
 667   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 668   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 669 
 670   gus_read8 (0x0f);             /* Clear pending IRQs */
 671 
 672 }
 673 
 674 static void
 675 gus_initialize (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 676 {
 677   unsigned long   flags;
 678   unsigned char   dma_image, irq_image, tmp;
 679 
 680   static unsigned char gus_irq_map[16] =
 681   {0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
 682 
 683   static unsigned char gus_dma_map[8] =
 684   {0, 1, 0, 2, 0, 3, 4, 5};
 685 
 686   DISABLE_INTR (flags);
 687   gus_write8 (0x4c, 0);         /* Reset GF1 */
 688   gus_delay ();
 689   gus_delay ();
 690 
 691   gus_write8 (0x4c, 1);         /* Release Reset */
 692   gus_delay ();
 693   gus_delay ();
 694 
 695   /*
 696    * Clear all interrupts
 697    */
 698 
 699   gus_write8 (0x41, 0);         /* DMA control */
 700   gus_write8 (0x45, 0);         /* Timer control */
 701   gus_write8 (0x49, 0);         /* Sample control */
 702 
 703   gus_select_max_voices (24);
 704 
 705   INB (u_Status);               /* Touch the status register */
 706 
 707   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 708   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 709   gus_read8 (0x0f);             /* Clear pending IRQs */
 710 
 711   gus_reset ();                 /* Resets all voices */
 712 
 713   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 714   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 715   gus_read8 (0x0f);             /* Clear pending IRQs */
 716 
 717   gus_write8 (0x4c, 7);         /* Master reset | DAC enable | IRQ enable */
 718 
 719   /*
 720    * Set up for Digital ASIC
 721    */
 722 
 723   OUTB (0x05, gus_base + 0x0f);
 724 
 725   mix_image |= 0x02;            /* Disable line out (for a moment) */
 726   OUTB (mix_image, u_Mixer);
 727 
 728   OUTB (0x00, u_IRQDMAControl);
 729 
 730   OUTB (0x00, gus_base + 0x0f);
 731 
 732   /*
 733    * Now set up the DMA and IRQ interface
 734    *
 735    * The GUS supports two IRQs and two DMAs.
 736    *
 737    * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
 738    * Adding this support requires significant changes to the dmabuf.c, dsp.c
 739    * and audio.c also.
 740    */
 741 
 742   irq_image = 0;
 743   tmp = gus_irq_map[gus_irq];
 744   if (!tmp)
 745     printk ("Warning! GUS IRQ not selected\n");
 746   irq_image |= tmp;
 747   irq_image |= 0x40;            /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
 748 
 749   dual_dma_mode = 1;
 750   if (!have_gus_max || gusmax_dma == gus_dma || gusmax_dma == -1)
 751     {
 752       dual_dma_mode = 0;
 753       dma_image = 0x40;         /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
 754 
 755       tmp = gus_dma_map[gus_dma];
 756       if (!tmp)
 757         printk ("Warning! GUS DMA not selected\n");
 758 
 759       dma_image |= tmp;
 760     }
 761   else
 762     /* Setup dual DMA channel mode for GUS MAX */
 763     {
 764       dma_image = gus_dma_map[gus_dma];
 765       if (!dma_image)
 766         printk ("Warning! GUS DMA not selected\n");
 767 
 768       tmp = gus_dma_map[gusmax_dma] << 3;
 769       if (!tmp)
 770         {
 771           printk ("Warning! Invalid GUS MAX DMA\n");
 772           tmp = 0x40;           /* Combine DMA channels */
 773           dual_dma_mode = 0;
 774         }
 775 
 776       dma_image |= tmp;
 777     }
 778 
 779   /*
 780    * For some reason the IRQ and DMA addresses must be written twice
 781    */
 782 
 783   /*
 784    * Doing it first time
 785    */
 786 
 787   OUTB (mix_image, u_Mixer);    /* Select DMA control */
 788   OUTB (dma_image | 0x80, u_IRQDMAControl);     /* Set DMA address */
 789 
 790   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
 791   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
 792 
 793   /*
 794    * Doing it second time
 795    */
 796 
 797   OUTB (mix_image, u_Mixer);    /* Select DMA control */
 798   OUTB (dma_image, u_IRQDMAControl);    /* Set DMA address */
 799 
 800   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
 801   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
 802 
 803   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
 804 
 805   mix_image &= ~0x02;           /* Enable line out */
 806   mix_image |= 0x08;            /* Enable IRQ */
 807   OUTB (mix_image, u_Mixer);    /*
 808                                  * Turn mixer channels on
 809                                  * Note! Mic in is left off.
 810                                  */
 811 
 812   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
 813 
 814   gusintr (INT_HANDLER_CALL (0));       /* Serve pending interrupts */
 815   RESTORE_INTR (flags);
 816 }
 817 
 818 int
 819 gus_wave_detect (int baseaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821   unsigned long   i;
 822   unsigned long   loc;
 823 
 824   gus_base = baseaddr;
 825 
 826   gus_write8 (0x4c, 0);         /* Reset GF1 */
 827   gus_delay ();
 828   gus_delay ();
 829 
 830   gus_write8 (0x4c, 1);         /* Release Reset */
 831   gus_delay ();
 832   gus_delay ();
 833 
 834   /* See if there is first block there.... */
 835   gus_poke (0L, 0xaa);
 836   if (gus_peek (0L) != 0xaa)
 837     return (0);
 838 
 839   /* Now zero it out so that I can check for mirroring .. */
 840   gus_poke (0L, 0x00);
 841   for (i = 1L; i < 1024L; i++)
 842     {
 843       int             n, failed;
 844 
 845       /* check for mirroring ... */
 846       if (gus_peek (0L) != 0)
 847         break;
 848       loc = i << 10;
 849 
 850       for (n = loc - 1, failed = 0; n <= loc; n++)
 851         {
 852           gus_poke (loc, 0xaa);
 853           if (gus_peek (loc) != 0xaa)
 854             failed = 1;
 855 
 856           gus_poke (loc, 0x55);
 857           if (gus_peek (loc) != 0x55)
 858             failed = 1;
 859         }
 860 
 861       if (failed)
 862         break;
 863     }
 864   gus_mem_size = i << 10;
 865   return 1;
 866 }
 867 
 868 static int
 869 guswave_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 870                unsigned int cmd, unsigned int arg)
 871 {
 872 
 873   switch (cmd)
 874     {
 875     case SNDCTL_SYNTH_INFO:
 876       gus_info.nr_voices = nr_voices;
 877       IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
 878       return 0;
 879       break;
 880 
 881     case SNDCTL_SEQ_RESETSAMPLES:
 882       reset_sample_memory ();
 883       return 0;
 884       break;
 885 
 886     case SNDCTL_SEQ_PERCMODE:
 887       return 0;
 888       break;
 889 
 890     case SNDCTL_SYNTH_MEMAVL:
 891       return gus_mem_size - free_mem_ptr - 32;
 892 
 893     default:
 894       return RET_ERROR (EINVAL);
 895     }
 896 }
 897 
 898 static int
 899 guswave_set_instr (int dev, int voice, int instr_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901   int             sample_no;
 902 
 903   if (instr_no < 0 || instr_no > MAX_PATCH)
 904     return RET_ERROR (EINVAL);
 905 
 906   if (voice < 0 || voice > 31)
 907     return RET_ERROR (EINVAL);
 908 
 909   if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
 910     {
 911       voices[voice].sample_pending = instr_no;
 912       return 0;
 913     }
 914 
 915   sample_no = patch_table[instr_no];
 916   patch_map[voice] = -1;
 917 
 918   if (sample_no < 0)
 919     {
 920       printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
 921       return RET_ERROR (EINVAL);        /* Patch not defined */
 922     }
 923 
 924   if (sample_ptrs[sample_no] == -1)     /* Sample not loaded */
 925     {
 926       printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n",
 927               sample_no, instr_no, voice);
 928       return RET_ERROR (EINVAL);
 929     }
 930 
 931   sample_map[voice] = sample_no;
 932   patch_map[voice] = instr_no;
 933   return 0;
 934 }
 935 
 936 static int
 937 guswave_kill_note (int dev, int voice, int note, int velocity)
     /* [previous][next][first][last][top][bottom][index][help] */
 938 {
 939   unsigned long   flags;
 940 
 941   DISABLE_INTR (flags);
 942   /* voice_alloc->map[voice] = 0xffff; */
 943   if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
 944     {
 945       voices[voice].kill_pending = 1;
 946       RESTORE_INTR (flags);
 947     }
 948   else
 949     {
 950       RESTORE_INTR (flags);
 951       gus_voice_fade (voice);
 952     }
 953 
 954   RESTORE_INTR (flags);
 955   return 0;
 956 }
 957 
 958 static void
 959 guswave_aftertouch (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 960 {
 961 #if 0
 962   short           lo_limit, hi_limit;
 963   unsigned long   flags;
 964 
 965   if (voice < 0 || voice > 31)
 966     return;
 967 
 968   if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
 969     return;                     /* Don't mix with envelopes */
 970 
 971   if (pressure < 32)
 972     {
 973       DISABLE_INTR (flags);
 974       gus_select_voice (voice);
 975       gus_rampoff ();
 976       compute_and_set_volume (voice, 255, 0);   /* Back to original volume */
 977       RESTORE_INTR (flags);
 978       return;
 979     }
 980 
 981   hi_limit = voices[voice].current_volume;
 982   lo_limit = hi_limit * 99 / 100;
 983   if (lo_limit < 65)
 984     lo_limit = 65;
 985 
 986   DISABLE_INTR (flags);
 987   gus_select_voice (voice);
 988   if (hi_limit > (4095 - 65))
 989     {
 990       hi_limit = 4095 - 65;
 991       gus_voice_volume (hi_limit);
 992     }
 993   gus_ramp_range (lo_limit, hi_limit);
 994   gus_ramp_rate (3, 8);
 995   gus_rampon (0x58);            /* Bidirectional, dow, loop */
 996   RESTORE_INTR (flags);
 997 #endif /* 0 */
 998 }
 999 
1000 static void
1001 guswave_panning (int dev, int voice, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1002 {
1003   if (voice >= 0 || voice < 32)
1004     voices[voice].panning = value;
1005 }
1006 
1007 static void
1008 guswave_volume_method (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1009 {
1010   if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
1011     volume_method = mode;
1012 }
1013 
1014 static void
1015 compute_volume (int voice, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
1016 {
1017   if (volume < 128)
1018     voices[voice].midi_volume = volume;
1019 
1020   switch (volume_method)
1021     {
1022     case VOL_METHOD_ADAGIO:
1023       voices[voice].initial_volume =
1024         gus_adagio_vol (voices[voice].midi_volume, voices[voice].main_vol,
1025                         voices[voice].expression_vol,
1026                         voices[voice].patch_vol);
1027       break;
1028 
1029     case VOL_METHOD_LINEAR:     /* Totally ignores patch-volume and expression */
1030       voices[voice].initial_volume =
1031         gus_linear_vol (volume, voices[voice].main_vol);
1032       break;
1033 
1034     default:
1035       voices[voice].initial_volume = volume_base +
1036         (voices[voice].midi_volume * volume_scale);
1037     }
1038 
1039   if (voices[voice].initial_volume > 4030)
1040     voices[voice].initial_volume = 4030;
1041 }
1042 
1043 static void
1044 compute_and_set_volume (int voice, int volume, int ramp_time)
     /* [previous][next][first][last][top][bottom][index][help] */
1045 {
1046   int             current, target, rate;
1047   unsigned long   flags;
1048 
1049   compute_volume (voice, volume);
1050   voices[voice].current_volume = voices[voice].initial_volume;
1051 
1052   DISABLE_INTR (flags);
1053   /*
1054      * CAUTION! Interrupts disabled. Enable them before returning
1055    */
1056 
1057   gus_select_voice (voice);
1058 
1059   current = gus_read16 (0x09) >> 4;
1060   target = voices[voice].initial_volume;
1061 
1062   if (ramp_time == INSTANT_RAMP)
1063     {
1064       gus_rampoff ();
1065       gus_voice_volume (target);
1066       RESTORE_INTR (flags);
1067       return;
1068     }
1069 
1070   if (ramp_time == FAST_RAMP)
1071     rate = 63;
1072   else
1073     rate = 16;
1074   gus_ramp_rate (0, rate);
1075 
1076   if ((target - current) / 64 == 0)     /* Close enough to target. */
1077     {
1078       gus_rampoff ();
1079       gus_voice_volume (target);
1080       RESTORE_INTR (flags);
1081       return;
1082     }
1083 
1084   if (target > current)
1085     {
1086       if (target > (4095 - 65))
1087         target = 4095 - 65;
1088       gus_ramp_range (current, target);
1089       gus_rampon (0x00);        /* Ramp up, once, no IRQ */
1090     }
1091   else
1092     {
1093       if (target < 65)
1094         target = 65;
1095 
1096       gus_ramp_range (target, current);
1097       gus_rampon (0x40);        /* Ramp down, once, no irq */
1098     }
1099   RESTORE_INTR (flags);
1100 }
1101 
1102 static void
1103 dynamic_volume_change (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
1104 {
1105   unsigned char   status;
1106   unsigned long   flags;
1107 
1108   DISABLE_INTR (flags);
1109   gus_select_voice (voice);
1110   status = gus_read8 (0x00);    /* Get voice status */
1111   RESTORE_INTR (flags);
1112 
1113   if (status & 0x03)
1114     return;                     /* Voice was not running */
1115 
1116   if (!(voices[voice].mode & WAVE_ENVELOPES))
1117     {
1118       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
1119       return;
1120     }
1121 
1122   /*
1123    * Voice is running and has envelopes.
1124    */
1125 
1126   DISABLE_INTR (flags);
1127   gus_select_voice (voice);
1128   status = gus_read8 (0x0d);    /* Ramping status */
1129   RESTORE_INTR (flags);
1130 
1131   if (status & 0x03)            /* Sustain phase? */
1132     {
1133       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
1134       return;
1135     }
1136 
1137   if (voices[voice].env_phase < 0)
1138     return;
1139 
1140   compute_volume (voice, voices[voice].midi_volume);
1141 
1142 }
1143 
1144 static void
1145 guswave_controller (int dev, int voice, int ctrl_num, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1146 {
1147   unsigned long   flags;
1148   unsigned long   freq;
1149 
1150   if (voice < 0 || voice > 31)
1151     return;
1152 
1153   switch (ctrl_num)
1154     {
1155     case CTRL_PITCH_BENDER:
1156       voices[voice].bender = value;
1157 
1158       if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1159         {
1160           freq = compute_finetune (voices[voice].orig_freq, value,
1161                                    voices[voice].bender_range);
1162           voices[voice].current_freq = freq;
1163 
1164           DISABLE_INTR (flags);
1165           gus_select_voice (voice);
1166           gus_voice_freq (freq);
1167           RESTORE_INTR (flags);
1168         }
1169       break;
1170 
1171     case CTRL_PITCH_BENDER_RANGE:
1172       voices[voice].bender_range = value;
1173       break;
1174     case CTL_EXPRESSION:
1175       value /= 128;
1176     case CTRL_EXPRESSION:
1177       if (volume_method == VOL_METHOD_ADAGIO)
1178         {
1179           voices[voice].expression_vol = value;
1180           if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1181             dynamic_volume_change (voice);
1182         }
1183       break;
1184 
1185     case CTL_PAN:
1186       voices[voice].panning = (value * 2) - 128;
1187       break;
1188 
1189     case CTL_MAIN_VOLUME:
1190       value = (value * 100) / 16383;
1191 
1192     case CTRL_MAIN_VOLUME:
1193       voices[voice].main_vol = value;
1194       if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
1195         dynamic_volume_change (voice);
1196       break;
1197 
1198     default:
1199       break;
1200     }
1201 }
1202 
1203 static int
1204 guswave_start_note2 (int dev, int voice, int note_num, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
1205 {
1206   int             sample, best_sample, best_delta, delta_freq;
1207   int             is16bits, samplep, patch, pan;
1208   unsigned long   note_freq, base_note, freq, flags;
1209   unsigned char   mode = 0;
1210 
1211   if (voice < 0 || voice > 31)
1212     {
1213       printk ("GUS: Invalid voice\n");
1214       return RET_ERROR (EINVAL);
1215     }
1216 
1217   if (note_num == 255)
1218     {
1219       if (voices[voice].mode & WAVE_ENVELOPES)
1220         {
1221           voices[voice].midi_volume = volume;
1222           dynamic_volume_change (voice);
1223           return 0;
1224         }
1225 
1226       compute_and_set_volume (voice, volume, 1);
1227       return 0;
1228     }
1229 
1230   if ((patch = patch_map[voice]) == -1)
1231     {
1232       return RET_ERROR (EINVAL);
1233     }
1234 
1235   if ((samplep = patch_table[patch]) == -1)
1236     {
1237       return RET_ERROR (EINVAL);
1238     }
1239 
1240   note_freq = note_to_freq (note_num);
1241 
1242   /*
1243    * Find a sample within a patch so that the note_freq is between low_note
1244    * and high_note.
1245    */
1246   sample = -1;
1247 
1248   best_sample = samplep;
1249   best_delta = 1000000;
1250   while (samplep >= 0 && sample == -1)
1251     {
1252       delta_freq = note_freq - samples[samplep].base_note;
1253       if (delta_freq < 0)
1254         delta_freq = -delta_freq;
1255       if (delta_freq < best_delta)
1256         {
1257           best_sample = samplep;
1258           best_delta = delta_freq;
1259         }
1260       if (samples[samplep].low_note <= note_freq &&
1261           note_freq <= samples[samplep].high_note)
1262         sample = samplep;
1263       else
1264         samplep = samples[samplep].key;         /*
1265                                                    * Follow link
1266                                                  */
1267     }
1268   if (sample == -1)
1269     sample = best_sample;
1270 
1271   if (sample == -1)
1272     {
1273       printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1274       return 0;                 /* Should play default patch ??? */
1275     }
1276 
1277   is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
1278   voices[voice].mode = samples[sample].mode;
1279   voices[voice].patch_vol = samples[sample].volume;
1280 
1281   if (voices[voice].mode & WAVE_ENVELOPES)
1282     {
1283       int             i;
1284 
1285       for (i = 0; i < 6; i++)
1286         {
1287           voices[voice].env_rate[i] = samples[sample].env_rate[i];
1288           voices[voice].env_offset[i] = samples[sample].env_offset[i];
1289         }
1290     }
1291 
1292   sample_map[voice] = sample;
1293 
1294   base_note = samples[sample].base_note / 100;  /* Try to avoid overflows */
1295   note_freq /= 100;
1296 
1297   freq = samples[sample].base_freq * note_freq / base_note;
1298 
1299   voices[voice].orig_freq = freq;
1300 
1301   /*
1302    * Since the pitch bender may have been set before playing the note, we
1303    * have to calculate the bending now.
1304    */
1305 
1306   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender,
1307                            voices[voice].bender_range);
1308   voices[voice].current_freq = freq;
1309 
1310   pan = (samples[sample].panning + voices[voice].panning) / 32;
1311   pan += 7;
1312   if (pan < 0)
1313     pan = 0;
1314   if (pan > 15)
1315     pan = 15;
1316 
1317   if (samples[sample].mode & WAVE_16_BITS)
1318     {
1319       mode |= 0x04;             /* 16 bits */
1320       if ((sample_ptrs[sample] >> 18) !=
1321           ((sample_ptrs[sample] + samples[sample].len) >> 18))
1322         printk ("GUS: Sample address error\n");
1323     }
1324 
1325   /*************************************************************************
1326    *    CAUTION!        Interrupts disabled. Don't return before enabling
1327    *************************************************************************/
1328 
1329   DISABLE_INTR (flags);
1330   gus_select_voice (voice);
1331   gus_voice_off ();
1332   gus_rampoff ();
1333 
1334   RESTORE_INTR (flags);
1335 
1336   if (voices[voice].mode & WAVE_ENVELOPES)
1337     {
1338       compute_volume (voice, volume);
1339       init_envelope (voice);
1340     }
1341   else
1342     compute_and_set_volume (voice, volume, 0);
1343 
1344   DISABLE_INTR (flags);
1345   gus_select_voice (voice);
1346 
1347   if (samples[sample].mode & WAVE_LOOP_BACK)
1348     gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len -
1349                     voices[voice].offset_pending, is16bits);    /* start=end */
1350   else
1351     gus_write_addr (0x0a, sample_ptrs[sample] + voices[voice].offset_pending,
1352                     is16bits);  /* Sample start=begin */
1353 
1354   if (samples[sample].mode & WAVE_LOOPING)
1355     {
1356       mode |= 0x08;
1357 
1358       if (samples[sample].mode & WAVE_BIDIR_LOOP)
1359         mode |= 0x10;
1360 
1361       if (samples[sample].mode & WAVE_LOOP_BACK)
1362         {
1363           gus_write_addr (0x0a,
1364                           sample_ptrs[sample] + samples[sample].loop_end -
1365                           voices[voice].offset_pending, is16bits);
1366           mode |= 0x40;
1367         }
1368 
1369       gus_write_addr (0x02, sample_ptrs[sample] + samples[sample].loop_start,
1370                       is16bits);        /* Loop start location */
1371       gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].loop_end,
1372                       is16bits);        /* Loop end location */
1373     }
1374   else
1375     {
1376       mode |= 0x20;             /* Loop IRQ at the end */
1377       voices[voice].loop_irq_mode = LMODE_FINISH;       /* Ramp down at the end */
1378       voices[voice].loop_irq_parm = 1;
1379       gus_write_addr (0x02, sample_ptrs[sample],
1380                       is16bits);        /* Loop start location */
1381       gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].len - 1,
1382                       is16bits);        /* Loop end location */
1383     }
1384   gus_voice_freq (freq);
1385   gus_voice_balance (pan);
1386   gus_voice_on (mode);
1387   RESTORE_INTR (flags);
1388 
1389   return 0;
1390 }
1391 
1392 /*
1393  * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
1394  * when the note playing on the voice is changed.  It uses volume
1395  * ramping.
1396  */
1397 
1398 static int
1399 guswave_start_note (int dev, int voice, int note_num, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
1400 {
1401   long int        flags;
1402   int             mode;
1403   int             ret_val = 0;
1404 
1405   DISABLE_INTR (flags);
1406   if (note_num == 255)
1407     {
1408       if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
1409         voices[voice].volume_pending = volume;
1410       else
1411         {
1412           ret_val = guswave_start_note2 (dev, voice, note_num, volume);
1413         }
1414     }
1415   else
1416     {
1417       gus_select_voice (voice);
1418       mode = gus_read8 (0x00);
1419       if (mode & 0x20)
1420         gus_write8 (0x00, mode & 0xdf);         /* No interrupt! */
1421 
1422       voices[voice].offset_pending = 0;
1423       voices[voice].kill_pending = 0;
1424       voices[voice].volume_irq_mode = 0;
1425       voices[voice].loop_irq_mode = 0;
1426 
1427       if (voices[voice].sample_pending >= 0)
1428         {
1429           RESTORE_INTR (flags); /* Run temporarily with interrupts enabled */
1430           guswave_set_instr (voices[voice].dev_pending, voice,
1431                              voices[voice].sample_pending);
1432           voices[voice].sample_pending = -1;
1433           DISABLE_INTR (flags);
1434           gus_select_voice (voice);     /* Reselect the voice (just to be sure) */
1435         }
1436 
1437       if ((mode & 0x01) || (int) ((gus_read16 (0x09) >> 4) < 2065))
1438         {
1439           ret_val = guswave_start_note2 (dev, voice, note_num, volume);
1440         }
1441       else
1442         {
1443           voices[voice].dev_pending = dev;
1444           voices[voice].note_pending = note_num;
1445           voices[voice].volume_pending = volume;
1446           voices[voice].volume_irq_mode = VMODE_START_NOTE;
1447 
1448           gus_rampoff ();
1449           gus_ramp_range (2000, 4065);
1450           gus_ramp_rate (0, 63);        /* Fastest possible rate */
1451           gus_rampon (0x20 | 0x40);     /* Ramp down, once, irq */
1452         }
1453     }
1454   RESTORE_INTR (flags);
1455   return ret_val;
1456 }
1457 
1458 static void
1459 guswave_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1460 {
1461   int             i;
1462 
1463   for (i = 0; i < 32; i++)
1464     {
1465       gus_voice_init (i);
1466       gus_voice_init2 (i);
1467     }
1468 }
1469 
1470 static int
1471 guswave_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1472 {
1473   int             err;
1474 
1475   if (gus_busy)
1476     return RET_ERROR (EBUSY);
1477 
1478   gus_initialize ();
1479   voice_alloc->timestamp = 0;
1480 
1481   if ((err = DMAbuf_open_dma (gus_devnum)) < 0)
1482     gus_no_dma = 1;             /* Upload samples using PIO */
1483   else
1484     gus_no_dma = 0;
1485 
1486   RESET_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
1487   gus_busy = 1;
1488   active_device = GUS_DEV_WAVE;
1489 
1490   gus_reset ();
1491 
1492   return 0;
1493 }
1494 
1495 static void
1496 guswave_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1497 {
1498   gus_busy = 0;
1499   active_device = 0;
1500   gus_reset ();
1501 
1502   if (!gus_no_dma)
1503     DMAbuf_close_dma (gus_devnum);
1504 }
1505 
1506 static int
1507 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
     /* [previous][next][first][last][top][bottom][index][help] */
1508                     int offs, int count, int pmgr_flag)
1509 {
1510   struct patch_info patch;
1511   int             instr;
1512   long            sizeof_patch;
1513 
1514   unsigned long   blk_size, blk_end, left, src_offs, target;
1515 
1516   sizeof_patch = (long) &patch.data[0] - (long) &patch;         /* Header size */
1517 
1518   if (format != GUS_PATCH)
1519     {
1520       printk ("GUS Error: Invalid patch format (key) 0x%x\n", format);
1521       return RET_ERROR (EINVAL);
1522     }
1523 
1524   if (count < sizeof_patch)
1525     {
1526       printk ("GUS Error: Patch header too short\n");
1527       return RET_ERROR (EINVAL);
1528     }
1529 
1530   count -= sizeof_patch;
1531 
1532   if (free_sample >= MAX_SAMPLE)
1533     {
1534       printk ("GUS: Sample table full\n");
1535       return RET_ERROR (ENOSPC);
1536     }
1537 
1538   /*
1539    * Copy the header from user space but ignore the first bytes which have
1540    * been transferred already.
1541    */
1542 
1543   COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof_patch - offs);
1544 
1545   instr = patch.instr_no;
1546 
1547   if (instr < 0 || instr > MAX_PATCH)
1548     {
1549       printk ("GUS: Invalid patch number %d\n", instr);
1550       return RET_ERROR (EINVAL);
1551     }
1552 
1553   if (count < patch.len)
1554     {
1555       printk ("GUS Warning: Patch record too short (%d<%d)\n",
1556               count, (int) patch.len);
1557       patch.len = count;
1558     }
1559 
1560   if (patch.len <= 0 || patch.len > gus_mem_size)
1561     {
1562       printk ("GUS: Invalid sample length %d\n", (int) patch.len);
1563       return RET_ERROR (EINVAL);
1564     }
1565 
1566   if (patch.mode & WAVE_LOOPING)
1567     {
1568       if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1569         {
1570           printk ("GUS: Invalid loop start\n");
1571           return RET_ERROR (EINVAL);
1572         }
1573 
1574       if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1575         {
1576           printk ("GUS: Invalid loop end\n");
1577           return RET_ERROR (EINVAL);
1578         }
1579     }
1580 
1581   free_mem_ptr = (free_mem_ptr + 31) & ~31;     /* 32 byte alignment */
1582 
1583 #define GUS_BANK_SIZE (256*1024)
1584 
1585   if (patch.mode & WAVE_16_BITS)
1586     {
1587       /*
1588        * 16 bit samples must fit one 256k bank.
1589        */
1590       if (patch.len >= GUS_BANK_SIZE)
1591         {
1592           printk ("GUS: Sample (16 bit) too long %d\n", (int) patch.len);
1593           return RET_ERROR (ENOSPC);
1594         }
1595 
1596       if ((free_mem_ptr / GUS_BANK_SIZE) !=
1597           ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1598         {
1599           unsigned long   tmp_mem =     /* Aling to 256K */
1600           ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1601 
1602           if ((tmp_mem + patch.len) > gus_mem_size)
1603             return RET_ERROR (ENOSPC);
1604 
1605           free_mem_ptr = tmp_mem;       /* This leaves unusable memory */
1606         }
1607     }
1608 
1609   if ((free_mem_ptr + patch.len) > gus_mem_size)
1610     return RET_ERROR (ENOSPC);
1611 
1612   sample_ptrs[free_sample] = free_mem_ptr;
1613 
1614   /*
1615    * Tremolo is not possible with envelopes
1616    */
1617 
1618   if (patch.mode & WAVE_ENVELOPES)
1619     patch.mode &= ~WAVE_TREMOLO;
1620 
1621   memcpy ((char *) &samples[free_sample], &patch, sizeof_patch);
1622 
1623   /*
1624    * Link this_one sample to the list of samples for patch 'instr'.
1625    */
1626 
1627   samples[free_sample].key = patch_table[instr];
1628   patch_table[instr] = free_sample;
1629 
1630   /*
1631    * Use DMA to transfer the wave data to the DRAM
1632    */
1633 
1634   left = patch.len;
1635   src_offs = 0;
1636   target = free_mem_ptr;
1637 
1638   while (left)                  /* Not completely transferred yet */
1639     {
1640       blk_size = audio_devs[gus_devnum]->buffsize;
1641       if (blk_size > left)
1642         blk_size = left;
1643 
1644       /*
1645        * DMA cannot cross 256k bank boundaries. Check for that.
1646        */
1647       blk_end = target + blk_size;
1648 
1649       if ((target >> 18) != (blk_end >> 18))
1650         {                       /* Split the block */
1651 
1652           blk_end &= ~(256 * 1024 - 1);
1653           blk_size = blk_end - target;
1654         }
1655 
1656       if (gus_no_dma)
1657         {
1658           /*
1659            * For some reason the DMA is not possible. We have to use PIO.
1660            */
1661           long            i;
1662           unsigned char   data;
1663 
1664           for (i = 0; i < blk_size; i++)
1665             {
1666               GET_BYTE_FROM_USER (data, addr, sizeof_patch + i);
1667               if (patch.mode & WAVE_UNSIGNED)
1668 
1669                 if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
1670                   data ^= 0x80; /* Convert to signed */
1671               gus_poke (target + i, data);
1672             }
1673         }
1674       else
1675         {
1676           unsigned long   address, hold_address;
1677           unsigned char   dma_command;
1678           unsigned long   flags;
1679 
1680           /*
1681            * OK, move now. First in and then out.
1682            */
1683 
1684           COPY_FROM_USER (audio_devs[gus_devnum]->dmap->raw_buf[0],
1685                           addr, sizeof_patch + src_offs,
1686                           blk_size);
1687 
1688           DISABLE_INTR (flags);
1689 /******** INTERRUPTS DISABLED NOW ********/
1690           gus_write8 (0x41, 0); /* Disable GF1 DMA */
1691           DMAbuf_start_dma (gus_devnum,
1692                             audio_devs[gus_devnum]->dmap->raw_buf_phys[0],
1693                             blk_size, DMA_MODE_WRITE);
1694 
1695           /*
1696            * Set the DRAM address for the wave data
1697            */
1698 
1699           address = target;
1700 
1701           if (audio_devs[gus_devnum]->dmachan > 3)
1702             {
1703               hold_address = address;
1704               address = address >> 1;
1705               address &= 0x0001ffffL;
1706               address |= (hold_address & 0x000c0000L);
1707             }
1708 
1709           gus_write16 (0x42, (address >> 4) & 0xffff);  /* DRAM DMA address */
1710 
1711           /*
1712            * Start the DMA transfer
1713            */
1714 
1715           dma_command = 0x21;   /* IRQ enable, DMA start */
1716           if (patch.mode & WAVE_UNSIGNED)
1717             dma_command |= 0x80;        /* Invert MSB */
1718           if (patch.mode & WAVE_16_BITS)
1719             dma_command |= 0x40;        /* 16 bit _DATA_ */
1720           if (audio_devs[gus_devnum]->dmachan > 3)
1721             dma_command |= 0x04;        /* 16 bit DMA _channel_ */
1722 
1723           gus_write8 (0x41, dma_command);       /* Lets bo luteet (=bugs) */
1724 
1725           /*
1726            * Sleep here until the DRAM DMA done interrupt is served
1727            */
1728           active_device = GUS_DEV_WAVE;
1729 
1730           DO_SLEEP (dram_sleeper, dram_sleep_flag, HZ);
1731           if (TIMED_OUT (dram_sleeper, dram_sleep_flag))
1732             printk ("GUS: DMA Transfer timed out\n");
1733           RESTORE_INTR (flags);
1734         }
1735 
1736       /*
1737        * Now the next part
1738        */
1739 
1740       left -= blk_size;
1741       src_offs += blk_size;
1742       target += blk_size;
1743 
1744       gus_write8 (0x41, 0);     /* Stop DMA */
1745     }
1746 
1747   free_mem_ptr += patch.len;
1748 
1749   if (!pmgr_flag)
1750     pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1751   free_sample++;
1752   return 0;
1753 }
1754 
1755 static void
1756 guswave_hw_control (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
1757 {
1758   int             voice, cmd;
1759   unsigned short  p1, p2;
1760   unsigned long   plong, flags;
1761 
1762   cmd = event[2];
1763   voice = event[3];
1764   p1 = *(unsigned short *) &event[4];
1765   p2 = *(unsigned short *) &event[6];
1766   plong = *(unsigned long *) &event[4];
1767 
1768   if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
1769       (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
1770     do_volume_irq (voice);
1771 
1772   switch (cmd)
1773     {
1774 
1775     case _GUS_NUMVOICES:
1776       DISABLE_INTR (flags);
1777       gus_select_voice (voice);
1778       gus_select_max_voices (p1);
1779       RESTORE_INTR (flags);
1780       break;
1781 
1782     case _GUS_VOICESAMPLE:
1783       guswave_set_instr (dev, voice, p1);
1784       break;
1785 
1786     case _GUS_VOICEON:
1787       DISABLE_INTR (flags);
1788       gus_select_voice (voice);
1789       p1 &= ~0x20;              /* Don't allow interrupts */
1790       gus_voice_on (p1);
1791       RESTORE_INTR (flags);
1792       break;
1793 
1794     case _GUS_VOICEOFF:
1795       DISABLE_INTR (flags);
1796       gus_select_voice (voice);
1797       gus_voice_off ();
1798       RESTORE_INTR (flags);
1799       break;
1800 
1801     case _GUS_VOICEFADE:
1802       gus_voice_fade (voice);
1803       break;
1804 
1805     case _GUS_VOICEMODE:
1806       DISABLE_INTR (flags);
1807       gus_select_voice (voice);
1808       p1 &= ~0x20;              /* Don't allow interrupts */
1809       gus_voice_mode (p1);
1810       RESTORE_INTR (flags);
1811       break;
1812 
1813     case _GUS_VOICEBALA:
1814       DISABLE_INTR (flags);
1815       gus_select_voice (voice);
1816       gus_voice_balance (p1);
1817       RESTORE_INTR (flags);
1818       break;
1819 
1820     case _GUS_VOICEFREQ:
1821       DISABLE_INTR (flags);
1822       gus_select_voice (voice);
1823       gus_voice_freq (plong);
1824       RESTORE_INTR (flags);
1825       break;
1826 
1827     case _GUS_VOICEVOL:
1828       DISABLE_INTR (flags);
1829       gus_select_voice (voice);
1830       gus_voice_volume (p1);
1831       RESTORE_INTR (flags);
1832       break;
1833 
1834     case _GUS_VOICEVOL2:        /* Just update the software voice level */
1835       voices[voice].initial_volume =
1836         voices[voice].current_volume = p1;
1837       break;
1838 
1839     case _GUS_RAMPRANGE:
1840       if (voices[voice].mode & WAVE_ENVELOPES)
1841         break;                  /* NO-NO */
1842       DISABLE_INTR (flags);
1843       gus_select_voice (voice);
1844       gus_ramp_range (p1, p2);
1845       RESTORE_INTR (flags);
1846       break;
1847 
1848     case _GUS_RAMPRATE:
1849       if (voices[voice].mode & WAVE_ENVELOPES)
1850         break;                  /* NJET-NJET */
1851       DISABLE_INTR (flags);
1852       gus_select_voice (voice);
1853       gus_ramp_rate (p1, p2);
1854       RESTORE_INTR (flags);
1855       break;
1856 
1857     case _GUS_RAMPMODE:
1858       if (voices[voice].mode & WAVE_ENVELOPES)
1859         break;                  /* NO-NO */
1860       DISABLE_INTR (flags);
1861       gus_select_voice (voice);
1862       p1 &= ~0x20;              /* Don't allow interrupts */
1863       gus_ramp_mode (p1);
1864       RESTORE_INTR (flags);
1865       break;
1866 
1867     case _GUS_RAMPON:
1868       if (voices[voice].mode & WAVE_ENVELOPES)
1869         break;                  /* EI-EI */
1870       DISABLE_INTR (flags);
1871       gus_select_voice (voice);
1872       p1 &= ~0x20;              /* Don't allow interrupts */
1873       gus_rampon (p1);
1874       RESTORE_INTR (flags);
1875       break;
1876 
1877     case _GUS_RAMPOFF:
1878       if (voices[voice].mode & WAVE_ENVELOPES)
1879         break;                  /* NEJ-NEJ */
1880       DISABLE_INTR (flags);
1881       gus_select_voice (voice);
1882       gus_rampoff ();
1883       RESTORE_INTR (flags);
1884       break;
1885 
1886     case _GUS_VOLUME_SCALE:
1887       volume_base = p1;
1888       volume_scale = p2;
1889       break;
1890 
1891     case _GUS_VOICE_POS:
1892       DISABLE_INTR (flags);
1893       gus_select_voice (voice);
1894       gus_set_voice_pos (voice, plong);
1895       RESTORE_INTR (flags);
1896       break;
1897 
1898     default:;
1899     }
1900 }
1901 
1902 static int
1903 gus_sampling_set_speed (int speed)
     /* [previous][next][first][last][top][bottom][index][help] */
1904 {
1905 
1906   if (speed <= 0)
1907     speed = gus_sampling_speed;
1908 
1909   if (speed < 4000)
1910     speed = 4000;
1911 
1912   if (speed > 44100)
1913     speed = 44100;
1914 
1915   gus_sampling_speed = speed;
1916 
1917   if (only_read_access)
1918     {
1919       /* Compute nearest valid recording speed  and return it */
1920 
1921       speed = (9878400 / (gus_sampling_speed + 2)) / 16;
1922       speed = (9878400 / (speed * 16)) - 2;
1923     }
1924   return speed;
1925 }
1926 
1927 static int
1928 gus_sampling_set_channels (int channels)
     /* [previous][next][first][last][top][bottom][index][help] */
1929 {
1930   if (!channels)
1931     return gus_sampling_channels;
1932   if (channels > 2)
1933     channels = 2;
1934   if (channels < 1)
1935     channels = 1;
1936   gus_sampling_channels = channels;
1937   return channels;
1938 }
1939 
1940 static int
1941 gus_sampling_set_bits (int bits)
     /* [previous][next][first][last][top][bottom][index][help] */
1942 {
1943   if (!bits)
1944     return gus_sampling_bits;
1945 
1946   if (bits != 8 && bits != 16)
1947     bits = 8;
1948 
1949   gus_sampling_bits = bits;
1950   return bits;
1951 }
1952 
1953 static int
1954 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
1955 {
1956   switch (cmd)
1957     {
1958     case SOUND_PCM_WRITE_RATE:
1959       if (local)
1960         return gus_sampling_set_speed (arg);
1961       return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1962       break;
1963 
1964     case SOUND_PCM_READ_RATE:
1965       if (local)
1966         return gus_sampling_speed;
1967       return IOCTL_OUT (arg, gus_sampling_speed);
1968       break;
1969 
1970     case SNDCTL_DSP_STEREO:
1971       if (local)
1972         return gus_sampling_set_channels (arg + 1) - 1;
1973       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1974       break;
1975 
1976     case SOUND_PCM_WRITE_CHANNELS:
1977       if (local)
1978         return gus_sampling_set_channels (arg);
1979       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1980       break;
1981 
1982     case SOUND_PCM_READ_CHANNELS:
1983       if (local)
1984         return gus_sampling_channels;
1985       return IOCTL_OUT (arg, gus_sampling_channels);
1986       break;
1987 
1988     case SNDCTL_DSP_SETFMT:
1989       if (local)
1990         return gus_sampling_set_bits (arg);
1991       return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1992       break;
1993 
1994     case SOUND_PCM_READ_BITS:
1995       if (local)
1996         return gus_sampling_bits;
1997       return IOCTL_OUT (arg, gus_sampling_bits);
1998 
1999     case SOUND_PCM_WRITE_FILTER:        /* NOT POSSIBLE */
2000       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
2001       break;
2002 
2003     case SOUND_PCM_READ_FILTER:
2004       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
2005       break;
2006 
2007     }
2008   return RET_ERROR (EINVAL);
2009 }
2010 
2011 static void
2012 gus_sampling_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2013 {
2014   if (recording_active)
2015     {
2016       gus_write8 (0x49, 0x00);  /* Halt recording */
2017       set_input_volumes ();
2018     }
2019 }
2020 
2021 static int
2022 gus_sampling_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
2023 {
2024   if (mode & OPEN_READ && dual_dma_mode)
2025     {
2026       printk ("GUS: The 8 bit input device is disabled. Use GUS MAX\n");
2027       return RET_ERROR (ENOTTY);
2028     }
2029 
2030   if (gus_busy)
2031     return RET_ERROR (EBUSY);
2032 
2033   gus_initialize ();
2034 
2035   gus_busy = 1;
2036   active_device = 0;
2037 
2038   gus_reset ();
2039   reset_sample_memory ();
2040   gus_select_max_voices (14);
2041 
2042   pcm_active = 0;
2043   dma_active = 0;
2044   pcm_opened = 1;
2045   if (mode & OPEN_READ)
2046     {
2047       recording_active = 1;
2048       set_input_volumes ();
2049     }
2050   only_read_access = !(mode & OPEN_WRITE);
2051 
2052   return 0;
2053 }
2054 
2055 static void
2056 gus_sampling_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2057 {
2058   gus_reset ();
2059   gus_busy = 0;
2060   pcm_opened = 0;
2061   active_device = 0;
2062 
2063   if (recording_active)
2064     {
2065       gus_write8 (0x49, 0x00);  /* Halt recording */
2066       set_input_volumes ();
2067     }
2068 
2069   recording_active = 0;
2070 }
2071 
2072 static void
2073 gus_sampling_update_volume (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2074 {
2075   unsigned long   flags;
2076   int             voice;
2077 
2078   if (pcm_active && pcm_opened)
2079     for (voice = 0; voice < gus_sampling_channels; voice++)
2080       {
2081         DISABLE_INTR (flags);
2082         gus_select_voice (voice);
2083         gus_rampoff ();
2084         gus_voice_volume (1530 + (25 * gus_pcm_volume));
2085         gus_ramp_range (65, 1530 + (25 * gus_pcm_volume));
2086         RESTORE_INTR (flags);
2087       }
2088 }
2089 
2090 static void
2091 play_next_pcm_block (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2092 {
2093   unsigned long   flags;
2094   int             speed = gus_sampling_speed;
2095   int             this_one, is16bits, chn;
2096   unsigned long   dram_loc;
2097   unsigned char   mode[2], ramp_mode[2];
2098 
2099   if (!pcm_qlen)
2100     return;
2101 
2102   this_one = pcm_head;
2103 
2104   for (chn = 0; chn < gus_sampling_channels; chn++)
2105     {
2106       mode[chn] = 0x00;
2107       ramp_mode[chn] = 0x03;    /* Ramping and rollover off */
2108 
2109       if (chn == 0)
2110         {
2111           mode[chn] |= 0x20;    /* Loop IRQ */
2112           voices[chn].loop_irq_mode = LMODE_PCM;
2113         }
2114 
2115       if (gus_sampling_bits != 8)
2116         {
2117           is16bits = 1;
2118           mode[chn] |= 0x04;    /* 16 bit data */
2119         }
2120       else
2121         is16bits = 0;
2122 
2123       dram_loc = this_one * pcm_bsize;
2124       dram_loc += chn * pcm_banksize;
2125 
2126       if (this_one == (pcm_nblk - 1))   /* Last fragment of the DRAM buffer */
2127         {
2128           mode[chn] |= 0x08;    /* Enable loop */
2129           ramp_mode[chn] = 0x03;        /* Disable rollover bit */
2130         }
2131       else
2132         {
2133           if (chn == 0)
2134             ramp_mode[chn] = 0x04;      /* Enable rollover bit */
2135         }
2136 
2137       DISABLE_INTR (flags);
2138       gus_select_voice (chn);
2139       gus_voice_freq (speed);
2140 
2141       if (gus_sampling_channels == 1)
2142         gus_voice_balance (7);  /* mono */
2143       else if (chn == 0)
2144         gus_voice_balance (0);  /* left */
2145       else
2146         gus_voice_balance (15); /* right */
2147 
2148       if (!pcm_active)          /* Playback not already active */
2149         {
2150           /*
2151            * The playback was not started yet (or there has been a pause).
2152            * Start the voice (again) and ask for a rollover irq at the end of
2153            * this_one block. If this_one one is last of the buffers, use just
2154            * the normal loop with irq.
2155            */
2156 
2157           gus_voice_off ();
2158           gus_rampoff ();
2159           gus_voice_volume (1530 + (25 * gus_pcm_volume));
2160           gus_ramp_range (65, 1530 + (25 * gus_pcm_volume));
2161 
2162           gus_write_addr (0x0a, dram_loc, is16bits);    /* Starting position */
2163           gus_write_addr (0x02, chn * pcm_banksize, is16bits);  /* Loop start */
2164 
2165           if (chn != 0)
2166             gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
2167                             is16bits);  /* Loop end location */
2168         }
2169 
2170       if (chn == 0)
2171         gus_write_addr (0x04, dram_loc + pcm_datasize[this_one] - 1,
2172                         is16bits);      /* Loop end location */
2173       else
2174         mode[chn] |= 0x08;      /* Enable looping */
2175 
2176       if (pcm_datasize[this_one] != pcm_bsize)
2177         {
2178           /*
2179            * Incompletely filled block. Possibly the last one.
2180            */
2181           if (chn == 0)
2182             {
2183               mode[chn] &= ~0x08;       /* Disable looping */
2184               mode[chn] |= 0x20;        /* Enable IRQ at the end */
2185               voices[0].loop_irq_mode = LMODE_PCM_STOP;
2186               ramp_mode[chn] = 0x03;    /* No rollover bit */
2187             }
2188           else
2189             {
2190               gus_write_addr (0x04, dram_loc + pcm_datasize[this_one],
2191                               is16bits);        /* Loop end location */
2192               mode[chn] &= ~0x08;       /* Disable looping */
2193             }
2194         }
2195 
2196       RESTORE_INTR (flags);
2197     }
2198 
2199   for (chn = 0; chn < gus_sampling_channels; chn++)
2200     {
2201       DISABLE_INTR (flags);
2202       gus_select_voice (chn);
2203       gus_write8 (0x0d, ramp_mode[chn]);
2204       gus_voice_on (mode[chn]);
2205       RESTORE_INTR (flags);
2206     }
2207 
2208   pcm_active = 1;
2209 }
2210 
2211 static void
2212 gus_transfer_output_block (int dev, unsigned long buf,
     /* [previous][next][first][last][top][bottom][index][help] */
2213                            int total_count, int intrflag, int chn)
2214 {
2215   /*
2216    * This routine transfers one block of audio data to the DRAM. In mono mode
2217    * it's called just once. When in stereo mode, this_one routine is called
2218    * once for both channels.
2219    *
2220    * The left/mono channel data is transferred to the beginning of dram and the
2221    * right data to the area pointed by gus_page_size.
2222    */
2223 
2224   int             this_one, count;
2225   unsigned long   flags;
2226   unsigned char   dma_command;
2227   unsigned long   address, hold_address;
2228 
2229   DISABLE_INTR (flags);
2230 
2231   count = total_count / gus_sampling_channels;
2232 
2233   if (chn == 0)
2234     {
2235       if (pcm_qlen >= pcm_nblk)
2236         printk ("GUS Warning: PCM buffers out of sync\n");
2237 
2238       this_one = pcm_current_block = pcm_tail;
2239       pcm_qlen++;
2240       pcm_tail = (pcm_tail + 1) % pcm_nblk;
2241       pcm_datasize[this_one] = count;
2242     }
2243   else
2244     this_one = pcm_current_block;
2245 
2246   gus_write8 (0x41, 0);         /* Disable GF1 DMA */
2247   DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
2248 
2249   address = this_one * pcm_bsize;
2250   address += chn * pcm_banksize;
2251 
2252   if (audio_devs[dev]->dmachan > 3)
2253     {
2254       hold_address = address;
2255       address = address >> 1;
2256       address &= 0x0001ffffL;
2257       address |= (hold_address & 0x000c0000L);
2258     }
2259 
2260   gus_write16 (0x42, (address >> 4) & 0xffff);  /* DRAM DMA address */
2261 
2262   dma_command = 0x21;           /* IRQ enable, DMA start */
2263 
2264   if (gus_sampling_bits != 8)
2265     dma_command |= 0x40;        /* 16 bit _DATA_ */
2266   else
2267     dma_command |= 0x80;        /* Invert MSB */
2268 
2269   if (audio_devs[dev]->dmachan > 3)
2270     dma_command |= 0x04;        /* 16 bit DMA channel */
2271 
2272   gus_write8 (0x41, dma_command);       /* Kickstart */
2273 
2274   if (chn == (gus_sampling_channels - 1))       /* Last channel */
2275     {
2276       /*
2277        * Last (right or mono) channel data
2278        */
2279       dma_active = 1;           /* DMA started. There is a unacknowledged buffer */
2280       active_device = GUS_DEV_PCM_DONE;
2281       if (!pcm_active && (pcm_qlen > 0 || count < pcm_bsize))
2282         {
2283           play_next_pcm_block ();
2284         }
2285     }
2286   else
2287     {
2288       /*
2289          * Left channel data. The right channel
2290          * is transferred after DMA interrupt
2291        */
2292       active_device = GUS_DEV_PCM_CONTINUE;
2293     }
2294 
2295   RESTORE_INTR (flags);
2296 }
2297 
2298 static void
2299 gus_sampling_output_block (int dev, unsigned long buf, int total_count,
     /* [previous][next][first][last][top][bottom][index][help] */
2300                            int intrflag, int restart_dma)
2301 {
2302   pcm_current_buf = buf;
2303   pcm_current_count = total_count;
2304   pcm_current_intrflag = intrflag;
2305   pcm_current_dev = dev;
2306   gus_transfer_output_block (dev, buf, total_count, intrflag, 0);
2307 }
2308 
2309 static void
2310 gus_sampling_start_input (int dev, unsigned long buf, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
2311                           int intrflag, int restart_dma)
2312 {
2313   unsigned long   flags;
2314   unsigned char   mode;
2315 
2316   DISABLE_INTR (flags);
2317 
2318   DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
2319 
2320   mode = 0xa0;                  /* DMA IRQ enabled, invert MSB */
2321 
2322   if (audio_devs[dev]->dmachan > 3)
2323     mode |= 0x04;               /* 16 bit DMA channel */
2324   if (gus_sampling_channels > 1)
2325     mode |= 0x02;               /* Stereo */
2326   mode |= 0x01;                 /* DMA enable */
2327 
2328   gus_write8 (0x49, mode);
2329 
2330   RESTORE_INTR (flags);
2331 }
2332 
2333 static int
2334 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2335 {
2336   unsigned int    rate;
2337 
2338   rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2339 
2340   gus_write8 (0x48, rate & 0xff);       /* Set sampling rate */
2341 
2342   if (gus_sampling_bits != 8)
2343     {
2344       printk ("GUS Error: 16 bit recording not supported\n");
2345       return RET_ERROR (EINVAL);
2346     }
2347 
2348   return 0;
2349 }
2350 
2351 static int
2352 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2353 {
2354   int             i;
2355 
2356   long            mem_ptr, mem_size;
2357 
2358   mem_ptr = 0;
2359   mem_size = gus_mem_size / gus_sampling_channels;
2360 
2361   if (mem_size > (256 * 1024))
2362     mem_size = 256 * 1024;
2363 
2364   pcm_bsize = bsize / gus_sampling_channels;
2365   pcm_head = pcm_tail = pcm_qlen = 0;
2366 
2367   pcm_nblk = MAX_PCM_BUFFERS;
2368   if ((pcm_bsize * pcm_nblk) > mem_size)
2369     pcm_nblk = mem_size / pcm_bsize;
2370 
2371   for (i = 0; i < pcm_nblk; i++)
2372     pcm_datasize[i] = 0;
2373 
2374   pcm_banksize = pcm_nblk * pcm_bsize;
2375 
2376   if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2377     pcm_nblk--;
2378 
2379   return 0;
2380 }
2381 
2382 static int
2383 gus_local_qlen (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2384 {
2385   return pcm_qlen;
2386 }
2387 
2388 static void
2389 gus_copy_from_user (int dev, char *localbuf, int localoffs,
     /* [previous][next][first][last][top][bottom][index][help] */
2390                     snd_rw_buf * userbuf, int useroffs, int len)
2391 {
2392   if (gus_sampling_channels == 1)
2393     {
2394       COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2395     }
2396   else if (gus_sampling_bits == 8)
2397     {
2398       int             in_left = useroffs;
2399       int             in_right = useroffs + 1;
2400       char           *out_left, *out_right;
2401       int             i;
2402 
2403       len /= 2;
2404       localoffs /= 2;
2405       out_left = &localbuf[localoffs];
2406       out_right = out_left + pcm_bsize;
2407 
2408       for (i = 0; i < len; i++)
2409         {
2410           GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2411           in_left += 2;
2412           GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2413           in_right += 2;
2414         }
2415     }
2416   else
2417     {
2418       int             in_left = useroffs / 2;
2419       int             in_right = useroffs / 2 + 1;
2420       short          *out_left, *out_right;
2421       int             i;
2422 
2423       len /= 4;
2424       localoffs /= 4;
2425 
2426       out_left = (short *) &localbuf[localoffs];
2427       out_right = out_left + (pcm_bsize / 2);
2428 
2429       for (i = 0; i < len; i++)
2430         {
2431           GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2432           in_left += 2;
2433           GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2434           in_right += 2;
2435         }
2436     }
2437 }
2438 
2439 static struct audio_operations gus_sampling_operations =
2440 {
2441   "Gravis UltraSound",
2442   NEEDS_RESTART,
2443   AFMT_U8 | AFMT_S16_LE,
2444   NULL,
2445   gus_sampling_open,
2446   gus_sampling_close,
2447   gus_sampling_output_block,
2448   gus_sampling_start_input,
2449   gus_sampling_ioctl,
2450   gus_sampling_prepare_for_input,
2451   gus_sampling_prepare_for_output,
2452   gus_sampling_reset,
2453   gus_sampling_reset,
2454   gus_local_qlen,
2455   gus_copy_from_user
2456 };
2457 
2458 static void
2459 guswave_setup_voice (int dev, int voice, int chn)
     /* [previous][next][first][last][top][bottom][index][help] */
2460 {
2461   struct channel_info *info =
2462   &synth_devs[dev]->chn_info[chn];
2463 
2464   guswave_set_instr (dev, voice, info->pgm_num);
2465 
2466   voices[voice].expression_vol =
2467     info->controllers[CTL_EXPRESSION];  /* Just msb */
2468   voices[voice].main_vol =
2469     (info->controllers[CTL_MAIN_VOLUME] * 100) / 128;
2470   voices[voice].panning =
2471     (info->controllers[CTL_PAN] * 2) - 128;
2472   voices[voice].bender = info->bender_value;
2473 }
2474 
2475 static void
2476 guswave_bender (int dev, int voice, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
2477 {
2478   int             freq;
2479   unsigned long   flags;
2480 
2481   voices[voice].bender = value - 8192;
2482   freq = compute_finetune (voices[voice].orig_freq, value,
2483                            voices[voice].bender_range);
2484   voices[voice].current_freq = freq;
2485 
2486   DISABLE_INTR (flags);
2487   gus_select_voice (voice);
2488   gus_voice_freq (freq);
2489   RESTORE_INTR (flags);
2490 }
2491 
2492 static int
2493 guswave_patchmgr (int dev, struct patmgr_info *rec)
     /* [previous][next][first][last][top][bottom][index][help] */
2494 {
2495   int             i, n;
2496 
2497   switch (rec->command)
2498     {
2499     case PM_GET_DEVTYPE:
2500       rec->parm1 = PMTYPE_WAVE;
2501       return 0;
2502       break;
2503 
2504     case PM_GET_NRPGM:
2505       rec->parm1 = MAX_PATCH;
2506       return 0;
2507       break;
2508 
2509     case PM_GET_PGMMAP:
2510       rec->parm1 = MAX_PATCH;
2511 
2512       for (i = 0; i < MAX_PATCH; i++)
2513         {
2514           int             ptr = patch_table[i];
2515 
2516           rec->data.data8[i] = 0;
2517 
2518           while (ptr >= 0 && ptr < free_sample)
2519             {
2520               rec->data.data8[i]++;
2521               ptr = samples[ptr].key;   /* Follow link */
2522             }
2523         }
2524       return 0;
2525       break;
2526 
2527     case PM_GET_PGM_PATCHES:
2528       {
2529         int             ptr = patch_table[rec->parm1];
2530 
2531         n = 0;
2532 
2533         while (ptr >= 0 && ptr < free_sample)
2534           {
2535             rec->data.data32[n++] = ptr;
2536             ptr = samples[ptr].key;     /* Follow link */
2537           }
2538       }
2539       rec->parm1 = n;
2540       return 0;
2541       break;
2542 
2543     case PM_GET_PATCH:
2544       {
2545         int             ptr = rec->parm1;
2546         struct patch_info *pat;
2547 
2548         if (ptr < 0 || ptr >= free_sample)
2549           return RET_ERROR (EINVAL);
2550 
2551         memcpy (rec->data.data8, (char *) &samples[ptr],
2552                 sizeof (struct patch_info));
2553 
2554         pat = (struct patch_info *) rec->data.data8;
2555 
2556         pat->key = GUS_PATCH;   /* Restore patch type */
2557         rec->parm1 = sample_ptrs[ptr];  /* DRAM location */
2558         rec->parm2 = sizeof (struct patch_info);
2559       }
2560       return 0;
2561       break;
2562 
2563     case PM_SET_PATCH:
2564       {
2565         int             ptr = rec->parm1;
2566         struct patch_info *pat;
2567 
2568         if (ptr < 0 || ptr >= free_sample)
2569           return RET_ERROR (EINVAL);
2570 
2571         pat = (struct patch_info *) rec->data.data8;
2572 
2573         if (pat->len > samples[ptr].len)        /* Cannot expand sample */
2574           return RET_ERROR (EINVAL);
2575 
2576         pat->key = samples[ptr].key;    /* Ensure the link is correct */
2577 
2578         memcpy ((char *) &samples[ptr], rec->data.data8,
2579                 sizeof (struct patch_info));
2580 
2581         pat->key = GUS_PATCH;
2582       }
2583       return 0;
2584       break;
2585 
2586     case PM_READ_PATCH: /* Returns a block of wave data from the DRAM */
2587       {
2588         int             sample = rec->parm1;
2589         int             n;
2590         long            offs = rec->parm2;
2591         int             l = rec->parm3;
2592 
2593         if (sample < 0 || sample >= free_sample)
2594           return RET_ERROR (EINVAL);
2595 
2596         if (offs < 0 || offs >= samples[sample].len)
2597           return RET_ERROR (EINVAL);    /* Invalid offset */
2598 
2599         n = samples[sample].len - offs;         /* Num of bytes left */
2600 
2601         if (l > n)
2602           l = n;
2603 
2604         if (l > sizeof (rec->data.data8))
2605           l = sizeof (rec->data.data8);
2606 
2607         if (l <= 0)
2608           return RET_ERROR (EINVAL);    /*
2609                                          * Was there a bug?
2610                                          */
2611 
2612         offs += sample_ptrs[sample];    /*
2613                                          * Begin offsess + offset to DRAM
2614                                          */
2615 
2616         for (n = 0; n < l; n++)
2617           rec->data.data8[n] = gus_peek (offs++);
2618         rec->parm1 = n;         /*
2619                                  * Nr of bytes copied
2620                                  */
2621       }
2622       return 0;
2623       break;
2624 
2625     case PM_WRITE_PATCH:        /*
2626                                  * Writes a block of wave data to the DRAM
2627                                  */
2628       {
2629         int             sample = rec->parm1;
2630         int             n;
2631         long            offs = rec->parm2;
2632         int             l = rec->parm3;
2633 
2634         if (sample < 0 || sample >= free_sample)
2635           return RET_ERROR (EINVAL);
2636 
2637         if (offs < 0 || offs >= samples[sample].len)
2638           return RET_ERROR (EINVAL);    /*
2639                                          * Invalid offset
2640                                          */
2641 
2642         n = samples[sample].len - offs;         /*
2643                                                    * Nr of bytes left
2644                                                  */
2645 
2646         if (l > n)
2647           l = n;
2648 
2649         if (l > sizeof (rec->data.data8))
2650           l = sizeof (rec->data.data8);
2651 
2652         if (l <= 0)
2653           return RET_ERROR (EINVAL);    /*
2654                                          * Was there a bug?
2655                                          */
2656 
2657         offs += sample_ptrs[sample];    /*
2658                                          * Begin offsess + offset to DRAM
2659                                          */
2660 
2661         for (n = 0; n < l; n++)
2662           gus_poke (offs++, rec->data.data8[n]);
2663         rec->parm1 = n;         /*
2664                                  * Nr of bytes copied
2665                                  */
2666       }
2667       return 0;
2668       break;
2669 
2670     default:
2671       return RET_ERROR (EINVAL);
2672     }
2673 }
2674 
2675 static int
2676 guswave_alloc (int dev, int chn, int note, struct voice_alloc_info *alloc)
     /* [previous][next][first][last][top][bottom][index][help] */
2677 {
2678   int             i, p, best = -1, best_time = 0x7fffffff;
2679 
2680   p = alloc->ptr;
2681   /*
2682      * First look for a completely stopped voice
2683    */
2684 
2685   for (i = 0; i < alloc->max_voice; i++)
2686     {
2687       if (alloc->map[p] == 0)
2688         {
2689           alloc->ptr = p;
2690           return p;
2691         }
2692       if (alloc->alloc_times[p] < best_time)
2693         {
2694           best = p;
2695           best_time = alloc->alloc_times[p];
2696         }
2697       p = (p + 1) % alloc->max_voice;
2698     }
2699 
2700   /*
2701      * Then look for a releasing voice
2702    */
2703 
2704   for (i = 0; i < alloc->max_voice; i++)
2705     {
2706       if (alloc->map[p] == 0xffff)
2707         {
2708           alloc->ptr = p;
2709           return p;
2710         }
2711       p = (p + 1) % alloc->max_voice;
2712     }
2713 
2714   if (best >= 0)
2715     p = best;
2716 
2717   alloc->ptr = p;
2718   return p;
2719 }
2720 
2721 static struct synth_operations guswave_operations =
2722 {
2723   &gus_info,
2724   0,
2725   SYNTH_TYPE_SAMPLE,
2726   SAMPLE_TYPE_GUS,
2727   guswave_open,
2728   guswave_close,
2729   guswave_ioctl,
2730   guswave_kill_note,
2731   guswave_start_note,
2732   guswave_set_instr,
2733   guswave_reset,
2734   guswave_hw_control,
2735   guswave_load_patch,
2736   guswave_aftertouch,
2737   guswave_controller,
2738   guswave_panning,
2739   guswave_volume_method,
2740   guswave_patchmgr,
2741   guswave_bender,
2742   guswave_alloc,
2743   guswave_setup_voice
2744 };
2745 
2746 static void
2747 set_input_volumes (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2748 {
2749   unsigned long   flags;
2750   unsigned char   mask = 0xff & ~0x06;  /* Just line out enabled */
2751 
2752   if (have_gus_max)             /* Don't disturb GUS MAX */
2753     return;
2754 
2755   DISABLE_INTR (flags);
2756 
2757   /*
2758    *    Enable channels having vol > 10%
2759    *      Note! bit 0x01 means the line in DISABLED while 0x04 means
2760    *            the mic in ENABLED.
2761    */
2762   if (gus_line_vol > 10)
2763     mask &= ~0x01;
2764   if (gus_mic_vol > 10)
2765     mask |= 0x04;
2766 
2767   if (recording_active)
2768     {
2769       /*
2770        *    Disable channel, if not selected for recording
2771        */
2772       if (!(gus_recmask & SOUND_MASK_LINE))
2773         mask |= 0x01;
2774       if (!(gus_recmask & SOUND_MASK_MIC))
2775         mask &= ~0x04;
2776     }
2777 
2778   mix_image &= ~0x07;
2779   mix_image |= mask & 0x07;
2780   OUTB (mix_image, u_Mixer);
2781 
2782   RESTORE_INTR (flags);
2783 }
2784 
2785 int
2786 gus_default_mixer_ioctl (int dev, unsigned int cmd, unsigned int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
2787 {
2788 #define MIX_DEVS        (SOUND_MASK_MIC|SOUND_MASK_LINE| \
2789                          SOUND_MASK_SYNTH|SOUND_MASK_PCM)
2790   if (((cmd >> 8) & 0xff) == 'M')
2791     {
2792       if (cmd & IOC_IN)
2793         switch (cmd & 0xff)
2794           {
2795           case SOUND_MIXER_RECSRC:
2796             gus_recmask = IOCTL_IN (arg) & MIX_DEVS;
2797             if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
2798               gus_recmask = SOUND_MASK_MIC;
2799             /* Note! Input volumes are updated during next open for recording */
2800             return IOCTL_OUT (arg, gus_recmask);
2801             break;
2802 
2803           case SOUND_MIXER_MIC:
2804             {
2805               int             vol = IOCTL_IN (arg) & 0xff;
2806 
2807               if (vol < 0)
2808                 vol = 0;
2809               if (vol > 100)
2810                 vol = 100;
2811               gus_mic_vol = vol;
2812               set_input_volumes ();
2813               return IOCTL_OUT (arg, vol | (vol << 8));
2814             }
2815             break;
2816 
2817           case SOUND_MIXER_LINE:
2818             {
2819               int             vol = IOCTL_IN (arg) & 0xff;
2820 
2821               if (vol < 0)
2822                 vol = 0;
2823               if (vol > 100)
2824                 vol = 100;
2825               gus_line_vol = vol;
2826               set_input_volumes ();
2827               return IOCTL_OUT (arg, vol | (vol << 8));
2828             }
2829             break;
2830 
2831           case SOUND_MIXER_PCM:
2832             gus_pcm_volume = IOCTL_IN (arg) & 0xff;
2833             if (gus_pcm_volume < 0)
2834               gus_pcm_volume = 0;
2835             if (gus_pcm_volume > 100)
2836               gus_pcm_volume = 100;
2837             gus_sampling_update_volume ();
2838             return IOCTL_OUT (arg, gus_pcm_volume | (gus_pcm_volume << 8));
2839             break;
2840 
2841           case SOUND_MIXER_SYNTH:
2842             {
2843               int             voice;
2844 
2845               gus_wave_volume = IOCTL_IN (arg) & 0xff;
2846 
2847               if (gus_wave_volume < 0)
2848                 gus_wave_volume = 0;
2849               if (gus_wave_volume > 100)
2850                 gus_wave_volume = 100;
2851 
2852               if (active_device == GUS_DEV_WAVE)
2853                 for (voice = 0; voice < nr_voices; voice++)
2854                   dynamic_volume_change (voice);        /* Apply the new vol */
2855 
2856               return IOCTL_OUT (arg, gus_wave_volume | (gus_wave_volume << 8));
2857             }
2858             break;
2859 
2860           default:
2861             return RET_ERROR (EINVAL);
2862           }
2863       else
2864         switch (cmd & 0xff)     /*
2865                                  * Return parameters
2866                                  */
2867           {
2868 
2869           case SOUND_MIXER_RECSRC:
2870             return IOCTL_OUT (arg, gus_recmask);
2871             break;
2872 
2873           case SOUND_MIXER_DEVMASK:
2874             return IOCTL_OUT (arg, MIX_DEVS);
2875             break;
2876 
2877           case SOUND_MIXER_STEREODEVS:
2878             return IOCTL_OUT (arg, 0);
2879             break;
2880 
2881           case SOUND_MIXER_RECMASK:
2882             return IOCTL_OUT (arg, SOUND_MASK_MIC | SOUND_MASK_LINE);
2883             break;
2884 
2885           case SOUND_MIXER_CAPS:
2886             return IOCTL_OUT (arg, 0);
2887             break;
2888 
2889           case SOUND_MIXER_MIC:
2890             return IOCTL_OUT (arg, gus_mic_vol | (gus_mic_vol << 8));
2891             break;
2892 
2893           case SOUND_MIXER_LINE:
2894             return IOCTL_OUT (arg, gus_line_vol | (gus_line_vol << 8));
2895             break;
2896 
2897           case SOUND_MIXER_PCM:
2898             return IOCTL_OUT (arg, gus_pcm_volume | (gus_pcm_volume << 8));
2899             break;
2900 
2901           case SOUND_MIXER_SYNTH:
2902             return IOCTL_OUT (arg, gus_wave_volume | (gus_wave_volume << 8));
2903             break;
2904 
2905           default:
2906             return RET_ERROR (EINVAL);
2907           }
2908     }
2909   else
2910     return RET_ERROR (EINVAL);
2911 }
2912 
2913 static struct mixer_operations gus_mixer_operations =
2914 {
2915   "Gravis Ultrasound",
2916   gus_default_mixer_ioctl
2917 };
2918 
2919 static long
2920 gus_default_mixer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
2921 {
2922   if (num_mixers < MAX_MIXER_DEV)       /*
2923                                          * Don't install if there is another
2924                                          * mixer
2925                                          */
2926     mixer_devs[num_mixers++] = &gus_mixer_operations;
2927 
2928   if (have_gus_max)
2929     {
2930 /*
2931  *  Enable all mixer channels on the GF1 side. Otherwise recording will
2932  *  not be possible using GUS MAX.
2933  */
2934       mix_image &= ~0x07;
2935       mix_image |= 0x04;        /* All channels enabled */
2936       OUTB (mix_image, u_Mixer);
2937     }
2938   return mem_start;
2939 }
2940 
2941 long
2942 gus_wave_init (long mem_start, int irq, int dma)
     /* [previous][next][first][last][top][bottom][index][help] */
2943 {
2944   unsigned long   flags;
2945   unsigned char   val;
2946   char           *model_num = "2.4";
2947   int             gus_type = 0x24;      /* 2.4 */
2948   int             mixer_type = 0;
2949 
2950   if (irq < 0 || irq > 15)
2951     {
2952       printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2953       return mem_start;
2954     }
2955 
2956   if (dma < 0 || dma > 7)
2957     {
2958       printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2959       return mem_start;
2960     }
2961 
2962   gus_irq = irq;
2963   gus_dma = dma;
2964 #ifdef GUSMAX_DMA
2965   if (gusmax_dma == -1)         /* Not already set */
2966     gusmax_dma = GUSMAX_DMA;
2967 #else
2968   if (gusmax_dma == -1)
2969     gusmax_dma = dma;
2970 #endif
2971 
2972   /*
2973      * Try to identify the GUS model.
2974      *
2975      *  Versions < 3.6 don't have the digital ASIC. Try to probe it first.
2976    */
2977 
2978   DISABLE_INTR (flags);
2979   OUTB (0x20, gus_base + 0x0f);
2980   val = INB (gus_base + 0x0f);
2981   RESTORE_INTR (flags);
2982 
2983   if (val != 0xff && (val & 0x06))      /* Should be 0x02?? */
2984     {
2985       /*
2986          * It has the digital ASIC so the card is at least v3.4.
2987          * Next try to detect the true model.
2988        */
2989 
2990       val = INB (u_MixSelect);
2991 
2992       /*
2993          * Value 255 means pre-3.7 which don't have mixer.
2994          * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
2995          * 10 and above is GUS MAX which has the CS4231 codec/mixer.
2996          *
2997        */
2998 
2999       if (val == 255 || val < 5)
3000         {
3001           model_num = "3.4";
3002           gus_type = 0x34;
3003         }
3004       else if (val < 10)
3005         {
3006           model_num = "3.7";
3007           gus_type = 0x37;
3008           mixer_type = ICS2101;
3009         }
3010       else
3011         {
3012           model_num = "MAX";
3013           gus_type = 0x40;
3014           mixer_type = CS4231;
3015 #ifndef EXCLUDE_GUSMAX
3016           {
3017             unsigned char   max_config = 0x40;  /* Codec enable */
3018 
3019             if (gus_dma > 3)
3020               max_config |= 0x10;       /* 16 bit capture DMA */
3021 
3022             if (gusmax_dma > 3)
3023               max_config |= 0x20;       /* 16 bit playback DMA */
3024 
3025             max_config |= (gus_base >> 4) & 0x0f;       /* Extract the X from 2X0 */
3026 
3027             OUTB (max_config, gus_base + 0x106);        /* UltraMax control */
3028           }
3029 
3030           if (ad1848_detect (gus_base + 0x10c))
3031             {
3032               gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3033               gus_wave_volume = 90;
3034               have_gus_max = 1;
3035               ad1848_init ("GUS MAX", gus_base + 0x10c,
3036                            -irq,
3037                            gusmax_dma,  /* Playback DMA */
3038                            gus_dma);    /* Capture DMA */
3039             }
3040           else
3041             printk ("[Where's the CS4231?]");
3042 #else
3043           printk ("\n\n\nGUS MAX support was not compiled in!!!\n\n\n\n");
3044 #endif
3045         }
3046     }
3047   else
3048     {
3049       /*
3050          * ASIC not detected so the card must be 2.2 or 2.4.
3051          * There could still be the 16-bit/mixer daughter card.
3052        */
3053     }
3054 
3055 
3056   printk (" <Gravis UltraSound %s (%dk)>", model_num, (int) gus_mem_size / 1024);
3057 
3058   sprintf (gus_info.name, "Gravis UltraSound %s (%dk)", model_num, (int) gus_mem_size / 1024);
3059 
3060   if (num_synths >= MAX_SYNTH_DEV)
3061     printk ("GUS Error: Too many synthesizers\n");
3062   else
3063     {
3064       voice_alloc = &guswave_operations.alloc;
3065       synth_devs[num_synths++] = &guswave_operations;
3066     }
3067 
3068   PERMANENT_MALLOC (struct patch_info *, samples,
3069                            (MAX_SAMPLE + 1) * sizeof (*samples), mem_start);
3070 
3071   reset_sample_memory ();
3072 
3073   gus_initialize ();
3074 
3075   if (num_audiodevs < MAX_AUDIO_DEV)
3076     {
3077       audio_devs[gus_devnum = num_audiodevs++] = &gus_sampling_operations;
3078       audio_devs[gus_devnum]->dmachan = dma;
3079       audio_devs[gus_devnum]->buffcount = 1;
3080       audio_devs[gus_devnum]->buffsize = DSP_BUFFSIZE;
3081     }
3082   else
3083     printk ("GUS: Too many PCM devices available\n");
3084 
3085   /*
3086      *  Mixer dependent initialization.
3087    */
3088 
3089   switch (mixer_type)
3090     {
3091     case ICS2101:
3092       gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
3093       gus_wave_volume = 90;
3094       return ics2101_mixer_init (mem_start);
3095 
3096     case CS4231:
3097       /* Initialized elsewhere (ad1848.c) */
3098     default:
3099       return gus_default_mixer_init (mem_start);
3100     }
3101 }
3102 
3103 static void
3104 do_loop_irq (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
3105 {
3106   unsigned char   tmp;
3107   int             mode, parm;
3108   unsigned long   flags;
3109 
3110   DISABLE_INTR (flags);
3111   gus_select_voice (voice);
3112 
3113   tmp = gus_read8 (0x00);
3114   tmp &= ~0x20;                 /*
3115                                  * Disable wave IRQ for this_one voice
3116                                  */
3117   gus_write8 (0x00, tmp);
3118 
3119   if (tmp & 0x03)               /* Voice stopped */
3120     voice_alloc->map[voice] = 0;
3121 
3122   mode = voices[voice].loop_irq_mode;
3123   voices[voice].loop_irq_mode = 0;
3124   parm = voices[voice].loop_irq_parm;
3125 
3126   switch (mode)
3127     {
3128 
3129     case LMODE_FINISH:          /*
3130                                  * Final loop finished, shoot volume down
3131                                  */
3132 
3133       if ((int) (gus_read16 (0x09) >> 4) < 100)         /*
3134                                                          * Get current volume
3135                                                          */
3136         {
3137           gus_voice_off ();
3138           gus_rampoff ();
3139           gus_voice_init (voice);
3140           break;
3141         }
3142       gus_ramp_range (65, 4065);
3143       gus_ramp_rate (0, 63);    /*
3144                                  * Fastest possible rate
3145                                  */
3146       gus_rampon (0x20 | 0x40); /*
3147                                  * Ramp down, once, irq
3148                                  */
3149       voices[voice].volume_irq_mode = VMODE_HALT;
3150       break;
3151 
3152     case LMODE_PCM_STOP:
3153       pcm_active = 0;           /* Signal to the play_next_pcm_block routine */
3154     case LMODE_PCM:
3155       {
3156         int             flag;   /* 0 or 2 */
3157 
3158         pcm_qlen--;
3159         pcm_head = (pcm_head + 1) % pcm_nblk;
3160         if (pcm_qlen && pcm_active)
3161           {
3162             play_next_pcm_block ();
3163           }
3164         else
3165           {                     /* Underrun. Just stop the voice */
3166             gus_select_voice (0);       /* Left channel */
3167             gus_voice_off ();
3168             gus_rampoff ();
3169             gus_select_voice (1);       /* Right channel */
3170             gus_voice_off ();
3171             gus_rampoff ();
3172             pcm_active = 0;
3173           }
3174 
3175         /*
3176            * If the queue was full before this interrupt, the DMA transfer was
3177            * suspended. Let it continue now.
3178          */
3179         if (dma_active)
3180           {
3181             if (pcm_qlen == 0)
3182               flag = 1;         /* Underflow */
3183             else
3184               flag = 0;
3185             dma_active = 0;
3186           }
3187         else
3188           flag = 2;             /* Just notify the dmabuf.c */
3189         DMAbuf_outputintr (gus_devnum, flag);
3190       }
3191       break;
3192 
3193     default:;
3194     }
3195   RESTORE_INTR (flags);
3196 }
3197 
3198 static void
3199 do_volume_irq (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
3200 {
3201   unsigned char   tmp;
3202   int             mode, parm;
3203   unsigned long   flags;
3204 
3205   DISABLE_INTR (flags);
3206 
3207   gus_select_voice (voice);
3208 
3209   tmp = gus_read8 (0x0d);
3210   tmp &= ~0x20;                 /*
3211                                  * Disable volume ramp IRQ
3212                                  */
3213   gus_write8 (0x0d, tmp);
3214 
3215   mode = voices[voice].volume_irq_mode;
3216   voices[voice].volume_irq_mode = 0;
3217   parm = voices[voice].volume_irq_parm;
3218 
3219   switch (mode)
3220     {
3221     case VMODE_HALT:            /*
3222                                  * Decay phase finished
3223                                  */
3224       RESTORE_INTR (flags);
3225       gus_voice_init (voice);
3226       break;
3227 
3228     case VMODE_ENVELOPE:
3229       gus_rampoff ();
3230       RESTORE_INTR (flags);
3231       step_envelope (voice);
3232       break;
3233 
3234     case VMODE_START_NOTE:
3235       RESTORE_INTR (flags);
3236       guswave_start_note2 (voices[voice].dev_pending, voice,
3237                   voices[voice].note_pending, voices[voice].volume_pending);
3238       if (voices[voice].kill_pending)
3239         guswave_kill_note (voices[voice].dev_pending, voice,
3240                            voices[voice].note_pending, 0);
3241 
3242       if (voices[voice].sample_pending >= 0)
3243         {
3244           guswave_set_instr (voices[voice].dev_pending, voice,
3245                              voices[voice].sample_pending);
3246           voices[voice].sample_pending = -1;
3247         }
3248       break;
3249 
3250     default:;
3251     }
3252 }
3253 
3254 void
3255 gus_voice_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
3256 {
3257   unsigned long   wave_ignore = 0, volume_ignore = 0;
3258   unsigned long   voice_bit;
3259 
3260   unsigned char   src, voice;
3261 
3262   while (1)
3263     {
3264       src = gus_read8 (0x0f);   /*
3265                                  * Get source info
3266                                  */
3267       voice = src & 0x1f;
3268       src &= 0xc0;
3269 
3270       if (src == (0x80 | 0x40))
3271         return;                 /*
3272                                  * No interrupt
3273                                  */
3274 
3275       voice_bit = 1 << voice;
3276 
3277       if (!(src & 0x80))        /*
3278                                  * Wave IRQ pending
3279                                  */
3280         if (!(wave_ignore & voice_bit) && (int) voice < nr_voices)      /*
3281                                                                            * Not done
3282                                                                            * yet
3283                                                                          */
3284           {
3285             wave_ignore |= voice_bit;
3286             do_loop_irq (voice);
3287           }
3288 
3289       if (!(src & 0x40))        /*
3290                                  * Volume IRQ pending
3291                                  */
3292         if (!(volume_ignore & voice_bit) && (int) voice < nr_voices)    /*
3293                                                                            * Not done
3294                                                                            * yet
3295                                                                          */
3296           {
3297             volume_ignore |= voice_bit;
3298             do_volume_irq (voice);
3299           }
3300     }
3301 }
3302 
3303 void
3304 guswave_dma_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
3305 {
3306   unsigned char   status;
3307 
3308   status = gus_look8 (0x41);    /* Get DMA IRQ Status */
3309   if (status & 0x40)            /* DMA interrupt pending */
3310     switch (active_device)
3311       {
3312       case GUS_DEV_WAVE:
3313         if (SOMEONE_WAITING (dram_sleeper, dram_sleep_flag))
3314           WAKE_UP (dram_sleeper, dram_sleep_flag);
3315         break;
3316 
3317       case GUS_DEV_PCM_CONTINUE:        /* Left channel data transferred */
3318         gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
3319                                    pcm_current_count,
3320                                    pcm_current_intrflag, 1);
3321         break;
3322 
3323       case GUS_DEV_PCM_DONE:    /* Right or mono channel data transferred */
3324         if (pcm_qlen < pcm_nblk)
3325           {
3326             int             flag = (1 - dma_active) * 2;        /* 0 or 2 */
3327 
3328             if (pcm_qlen == 0)
3329               flag = 1;         /* Underrun */
3330             dma_active = 0;
3331             DMAbuf_outputintr (gus_devnum, flag);
3332           }
3333         break;
3334 
3335       default:;
3336       }
3337 
3338   status = gus_look8 (0x49);    /*
3339                                  * Get Sampling IRQ Status
3340                                  */
3341   if (status & 0x40)            /*
3342                                  * Sampling Irq pending
3343                                  */
3344     {
3345       DMAbuf_inputintr (gus_devnum);
3346     }
3347 
3348 }
3349 
3350 #endif

/* [previous][next][first][last][top][bottom][index][help] */