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

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