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. gus_wave_unload
  76. do_loop_irq
  77. do_volume_irq
  78. gus_voice_irq
  79. guswave_dma_irq
  80. gus_timer_command
  81. arm_timer
  82. gus_tmr_start
  83. gus_tmr_disable
  84. gus_tmr_restart
  85. gus_tmr_install

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

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