root/drivers/sound/gus_wave.c

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

DEFINITIONS

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

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

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