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_has_output_drained
  66. gus_copy_from_user
  67. guswave_bender
  68. guswave_patchmgr
  69. set_input_volumes
  70. gus_default_mixer_ioctl
  71. gus_default_mixer_init
  72. gus_wave_init
  73. do_loop_irq
  74. do_volume_irq
  75. gus_voice_irq
  76. guswave_dma_irq

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

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