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_voice_init
  25. step_envelope
  26. init_envelope
  27. start_release
  28. gus_voice_fade
  29. gus_reset
  30. gus_initialize
  31. gus_wave_detect
  32. guswave_ioctl
  33. guswave_set_instr
  34. guswave_kill_note
  35. guswave_aftertouch
  36. guswave_panning
  37. compute_volume
  38. compute_and_set_volume
  39. dynamic_volume_change
  40. guswave_controller
  41. guswave_start_note
  42. guswave_reset
  43. guswave_open
  44. guswave_close
  45. guswave_load_patch
  46. guswave_hw_control
  47. gus_sampling_set_speed
  48. gus_sampling_set_channels
  49. gus_sampling_set_bits
  50. gus_sampling_ioctl
  51. gus_sampling_reset
  52. gus_sampling_open
  53. gus_sampling_close
  54. play_next_pcm_block
  55. gus_transfer_output_block
  56. gus_sampling_output_block
  57. gus_sampling_start_input
  58. gus_sampling_prepare_for_input
  59. gus_sampling_prepare_for_output
  60. gus_has_output_drained
  61. gus_copy_from_user
  62. guswave_patchmgr
  63. gus_wave_init
  64. do_loop_irq
  65. do_volume_irq
  66. gus_voice_irq
  67. guswave_dma_irq

   1 
   2 /*
   3  * linux/kernel/chr_drv/sound/gus_wave.c
   4  * 
   5  * Driver for the Gravis UltraSound wave table synth.
   6  * 
   7  * Copyright by Hannu Savolainen 1993
   8  * 
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions are
  11  * met: 1. Redistributions of source code must retain the above copyright
  12  * notice, this list of conditions and the following disclaimer. 2.
  13  * Redistributions in binary form must reproduce the above copyright notice,
  14  * this list of conditions and the following disclaimer in the documentation
  15  * and/or other materials provided with the distribution.
  16  * 
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  * 
  29  */
  30 
  31 /* #define GUS_LINEAR_VOLUME     */
  32 
  33 #include "sound_config.h"
  34 #include "ultrasound.h"
  35 #include "gus_hw.h"
  36 
  37 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_GUS)
  38 
  39 #define MAX_SAMPLE      256
  40 #define MAX_PATCH       256
  41 
  42 struct voice_info
  43   {
  44     unsigned long   orig_freq;
  45     unsigned long   current_freq;
  46     unsigned long   mode;
  47     int             bender;
  48     int             bender_range;
  49     int             panning;
  50     int             midi_volume;
  51     unsigned int    initial_volume;
  52     unsigned int    current_volume;
  53     int             loop_irq_mode, loop_irq_parm;
  54 #define LMODE_FINISH            1
  55 #define LMODE_PCM               2
  56 #define LMODE_PCM_STOP          3
  57     int             volume_irq_mode, volume_irq_parm;
  58 #define VMODE_HALT              1
  59 #define VMODE_ENVELOPE          2
  60 
  61     int             env_phase;
  62     unsigned char   env_rate[6];
  63     unsigned char   env_offset[6];
  64 
  65     /*
  66      * Volume computation parameters for gus_adagio_vol()
  67      */
  68     int             main_vol, expression_vol, patch_vol;
  69 
  70   };
  71 
  72 extern int      gus_base;
  73 extern int      gus_irq, gus_dma;
  74 extern char    *snd_raw_buf[MAX_DSP_DEV][DSP_BUFFCOUNT];
  75 extern unsigned long snd_raw_buf_phys[MAX_DSP_DEV][DSP_BUFFCOUNT];
  76 extern int      snd_raw_count[MAX_DSP_DEV];
  77 static long     gus_mem_size = 0;
  78 static long     free_mem_ptr = 0;
  79 static int      gus_busy = 0;
  80 static int      nr_voices = 0;  /* Number of currently allowed voices */
  81 static int      gus_devnum = 0;
  82 static int      volume_base, volume_scale, volume_method;
  83 
  84 #define VOL_METHOD_ADAGIO       1
  85 int             gus_wave_volume = 60;   /* Master wolume for wave (0 to 100) */
  86 static unsigned char mix_image = 0x00;
  87 
  88 /*
  89  * Current version of this_one driver doesn't allow synth and PCM functions
  90  * at the same time. The active_device specifies the active driver
  91  */
  92 static int      active_device = 0;
  93 
  94 #define GUS_DEV_WAVE            1       /* Wave table synth */
  95 #define GUS_DEV_PCM_DONE        2       /* PCM device, transfer done */
  96 #define GUS_DEV_PCM_CONTINUE    3       /* PCM device, transfer the second
  97                                          * chn */
  98 
  99 static int      gus_sampling_speed;
 100 static int      gus_sampling_channels;
 101 static int      gus_sampling_bits;
 102 
 103 DEFINE_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
 104 
 105 /*
 106  * Variables and buffers for PCM output
 107  */
 108 #define MAX_PCM_BUFFERS         32      /* Don't change */
 109 static int      pcm_bsize,      /* Current blocksize */
 110                 pcm_nblk,       /* Current # of blocks */
 111                 pcm_banksize;   /* # bytes allocated for channels */
 112 static int      pcm_datasize[MAX_PCM_BUFFERS];  /* Actual # of bytes in blk */
 113 static volatile int pcm_head, pcm_tail, pcm_qlen;       /* DRAM queue */
 114 static volatile int pcm_active;
 115 static int      pcm_current_dev;
 116 static int      pcm_current_block;
 117 static unsigned long pcm_current_buf;
 118 static int      pcm_current_count;
 119 static int      pcm_current_intrflag;
 120 
 121 struct voice_info voices[32];
 122 
 123 static int      freq_div_table[] =
 124 {
 125   44100,                        /* 14 */
 126   41160,                        /* 15 */
 127   38587,                        /* 16 */
 128   36317,                        /* 17 */
 129   34300,                        /* 18 */
 130   32494,                        /* 19 */
 131   30870,                        /* 20 */
 132   29400,                        /* 21 */
 133   28063,                        /* 22 */
 134   26843,                        /* 23 */
 135   25725,                        /* 24 */
 136   24696,                        /* 25 */
 137   23746,                        /* 26 */
 138   22866,                        /* 27 */
 139   22050,                        /* 28 */
 140   21289,                        /* 29 */
 141   20580,                        /* 30 */
 142   19916,                        /* 31 */
 143   19293                         /* 32 */
 144 };
 145 
 146 static struct patch_info samples[MAX_SAMPLE + 1];
 147 static long     sample_ptrs[MAX_SAMPLE + 1];
 148 static int      sample_map[32];
 149 static int      free_sample;
 150 
 151 
 152 static int      patch_table[MAX_PATCH];
 153 static int      patch_map[32];
 154 
 155 static struct synth_info gus_info =
 156 {"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS, 0, 16, 0, MAX_PATCH};
 157 
 158 static void     gus_poke (long addr, unsigned char data);
 159 static void     compute_and_set_volume (int voice, int volume, int ramp_time);
 160 extern unsigned short gus_adagio_vol (int vel, int mainv, int xpn, int voicev);
 161 static void     compute_volume (int voice, int volume);
 162 
 163 #define INSTANT_RAMP            -1      /* Dont use ramping */
 164 #define FAST_RAMP               0       /* Fastest possible ramp */
 165 
 166 static void
 167 reset_sample_memory (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169   int             i;
 170 
 171   for (i = 0; i <= MAX_SAMPLE; i++)
 172     sample_ptrs[i] = -1;
 173   for (i = 0; i < 32; i++)
 174     sample_map[i] = -1;
 175   for (i = 0; i < 32; i++)
 176     patch_map[i] = -1;
 177 
 178   gus_poke (0, 0);              /* Put silence here */
 179   gus_poke (1, 0);
 180 
 181   free_mem_ptr = 2;
 182   free_sample = 0;
 183 
 184   for (i = 0; i < MAX_PATCH; i++)
 185     patch_table[i] = -1;
 186 }
 187 
 188 void
 189 gus_delay (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191   int             i;
 192 
 193   for (i = 0; i < 7; i++)
 194     INB (u_DRAMIO);
 195 }
 196 
 197 static void
 198 gus_poke (long addr, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200   unsigned long   flags;
 201 
 202   DISABLE_INTR (flags);
 203   OUTB (0x43, u_Command);
 204   OUTB (addr & 0xff, u_DataLo);
 205   OUTB ((addr >> 8) & 0xff, u_DataHi);
 206 
 207   OUTB (0x44, u_Command);
 208   OUTB ((addr >> 16) & 0xff, u_DataHi);
 209   OUTB (data, u_DRAMIO);
 210   RESTORE_INTR (flags);
 211 }
 212 
 213 static unsigned char
 214 gus_peek (long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216   unsigned long   flags;
 217   unsigned char   tmp;
 218 
 219   DISABLE_INTR (flags);
 220   OUTB (0x43, u_Command);
 221   OUTB (addr & 0xff, u_DataLo);
 222   OUTB ((addr >> 8) & 0xff, u_DataHi);
 223 
 224   OUTB (0x44, u_Command);
 225   OUTB ((addr >> 16) & 0xff, u_DataHi);
 226   tmp = INB (u_DRAMIO);
 227   RESTORE_INTR (flags);
 228 
 229   return tmp;
 230 }
 231 
 232 void
 233 gus_write8 (int reg, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235   unsigned long   flags;
 236 
 237   DISABLE_INTR (flags);
 238 
 239   OUTB (reg, u_Command);
 240   OUTB (data, u_DataHi);
 241 
 242   RESTORE_INTR (flags);
 243 }
 244 
 245 unsigned char
 246 gus_read8 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {
 248   unsigned long   flags;
 249   unsigned char   val;
 250 
 251   DISABLE_INTR (flags);
 252   OUTB (reg | 0x80, u_Command);
 253   val = INB (u_DataHi);
 254   RESTORE_INTR (flags);
 255 
 256   return val;
 257 }
 258 
 259 unsigned char
 260 gus_look8 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262   unsigned long   flags;
 263   unsigned char   val;
 264 
 265   DISABLE_INTR (flags);
 266   OUTB (reg, u_Command);
 267   val = INB (u_DataHi);
 268   RESTORE_INTR (flags);
 269 
 270   return val;
 271 }
 272 
 273 void
 274 gus_write16 (int reg, unsigned short data)
     /* [previous][next][first][last][top][bottom][index][help] */
 275 {
 276   unsigned long   flags;
 277 
 278   DISABLE_INTR (flags);
 279 
 280   OUTB (reg, u_Command);
 281 
 282   OUTB (data & 0xff, u_DataLo);
 283   OUTB ((data >> 8) & 0xff, u_DataHi);
 284 
 285   RESTORE_INTR (flags);
 286 }
 287 
 288 unsigned short
 289 gus_read16 (int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291   unsigned long   flags;
 292   unsigned char   hi, lo;
 293 
 294   DISABLE_INTR (flags);
 295 
 296   OUTB (reg | 0x80, u_Command);
 297 
 298   lo = INB (u_DataLo);
 299   hi = INB (u_DataHi);
 300 
 301   RESTORE_INTR (flags);
 302 
 303   return ((hi << 8) & 0xff00) | lo;
 304 }
 305 
 306 void
 307 gus_write_addr (int reg, unsigned long address, int is16bit)
     /* [previous][next][first][last][top][bottom][index][help] */
 308 {
 309   unsigned long   hold_address;
 310 
 311   if (is16bit)
 312     {
 313       /*
 314        * Special processing required for 16 bit patches
 315        */
 316 
 317       hold_address = address;
 318       address = address >> 1;
 319       address &= 0x0001ffffL;
 320       address |= (hold_address & 0x000c0000L);
 321     }
 322 
 323   gus_write16 (reg, (unsigned short) ((address >> 7) & 0xffff));
 324   gus_write16 (reg + 1, (unsigned short) ((address << 9) & 0xffff));
 325 }
 326 
 327 static void
 328 gus_select_voice (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 329 {
 330   if (voice < 0 || voice > 31)
 331     return;
 332 
 333   OUTB (voice, u_Voice);
 334 }
 335 
 336 static void
 337 gus_select_max_voices (int nvoices)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339   if (nvoices < 14)
 340     nvoices = 14;
 341   if (nvoices > 32)
 342     nvoices = 32;
 343 
 344   nr_voices = nvoices;
 345 
 346   gus_write8 (0x0e, (nvoices - 1) | 0xc0);
 347 }
 348 
 349 static void
 350 gus_voice_on (unsigned char mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352   gus_write8 (0x00, mode & 0xfc);
 353   gus_delay ();
 354   gus_write8 (0x00, mode & 0xfc);
 355 }
 356 
 357 static void
 358 gus_voice_off (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360   gus_write8 (0x00, gus_read8 (0x00) | 0x03);
 361 }
 362 
 363 static void
 364 gus_voice_mode (unsigned char mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 365 {
 366   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc)); /* Don't start or stop
 367                                                                  * voice */
 368   gus_delay ();
 369   gus_write8 (0x00, (gus_read8 (0x00) & 0x03) | (mode & 0xfc));
 370 }
 371 
 372 static void
 373 gus_voice_freq (unsigned long freq)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375   unsigned long   divisor = freq_div_table[nr_voices - 14];
 376   unsigned short  fc;
 377 
 378   fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
 379   fc = fc << 1;
 380 
 381   gus_write16 (0x01, fc);
 382 }
 383 
 384 static void
 385 gus_voice_volume (unsigned short vol)
     /* [previous][next][first][last][top][bottom][index][help] */
 386 {
 387   gus_write8 (0x0d, 0x03);      /* Stop ramp before setting volume */
 388   gus_write16 (0x09, vol << 4);
 389 }
 390 
 391 static void
 392 gus_voice_balance (unsigned char balance)
     /* [previous][next][first][last][top][bottom][index][help] */
 393 {
 394   gus_write8 (0x0c, balance);
 395 }
 396 
 397 static void
 398 gus_ramp_range (unsigned short low, unsigned short high)
     /* [previous][next][first][last][top][bottom][index][help] */
 399 {
 400   gus_write8 (0x07, (low >> 4) & 0xff);
 401   gus_write8 (0x08, (high >> 4) & 0xff);
 402 }
 403 
 404 static void
 405 gus_ramp_rate (unsigned char scale, unsigned char rate)
     /* [previous][next][first][last][top][bottom][index][help] */
 406 {
 407   gus_write8 (0x06, ((scale & 0x03) << 6) | (rate & 0x3f));
 408 }
 409 
 410 static void
 411 gus_rampon (unsigned char mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413   gus_write8 (0x0d, mode & 0xfc);
 414   gus_delay ();
 415   gus_write8 (0x0d, mode & 0xfc);
 416 }
 417 
 418 static void
 419 gus_ramp_mode (unsigned char mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc)); /* Don't start or stop
 422                                                                  * ramping */
 423   gus_delay ();
 424   gus_write8 (0x0d, (gus_read8 (0x0d) & 0x03) | (mode & 0xfc));
 425 }
 426 
 427 static void
 428 gus_rampoff (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430   gus_write8 (0x0d, 0x03);
 431 }
 432 
 433 static void
 434 gus_voice_init (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 435 {
 436   unsigned long   flags;
 437 
 438   DISABLE_INTR (flags);
 439   gus_select_voice (voice);
 440   gus_voice_volume (0);
 441   gus_write_addr (0x0a, 0, 0);  /* Set current position to 0 */
 442   gus_write8 (0x00, 0x03);      /* Voice off */
 443   gus_write8 (0x0d, 0x03);      /* Ramping off */
 444   RESTORE_INTR (flags);
 445 
 446   voices[voice].panning = 0;
 447   voices[voice].mode = 0;
 448   voices[voice].orig_freq = 20000;
 449   voices[voice].current_freq = 20000;
 450   voices[voice].bender = 0;
 451   voices[voice].bender_range = 200;
 452   voices[voice].initial_volume = 0;
 453   voices[voice].current_volume = 0;
 454   voices[voice].loop_irq_mode = 0;
 455   voices[voice].loop_irq_parm = 0;
 456   voices[voice].volume_irq_mode = 0;
 457   voices[voice].volume_irq_parm = 0;
 458   voices[voice].env_phase = 0;
 459   voices[voice].main_vol = 127;
 460   voices[voice].patch_vol = 127;
 461   voices[voice].expression_vol = 127;
 462 }
 463 
 464 static void
 465 step_envelope (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467   unsigned        vol, prev_vol, phase;
 468   unsigned char   rate;
 469 
 470   if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
 471     {
 472       gus_rampoff ();
 473       return;                   /* Sustain */
 474     }
 475 
 476   if (voices[voice].env_phase >= 5)
 477     {
 478       /*
 479        * Shoot the voice off
 480        */
 481 
 482       gus_voice_init (voice);
 483       return;
 484     }
 485 
 486   prev_vol = voices[voice].current_volume;
 487   gus_voice_volume (prev_vol);
 488   phase = ++voices[voice].env_phase;
 489 
 490   compute_volume (voice, voices[voice].midi_volume);
 491 
 492   vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
 493   rate = voices[voice].env_rate[phase];
 494   gus_write8 (0x06, rate);      /* Ramping rate */
 495 
 496   voices[voice].volume_irq_mode = VMODE_ENVELOPE;
 497 
 498   if (((vol - prev_vol) / 64) == 0)     /* No significant volume change */
 499     {
 500       step_envelope (voice);    /* Continue with the next phase */
 501       return;
 502     }
 503 
 504   if (vol > prev_vol)
 505     {
 506       if (vol >= (4096 - 64))
 507         vol = 4096 - 65;
 508       gus_ramp_range (0, vol);
 509       gus_rampon (0x20);        /* Increasing, irq */
 510     }
 511   else
 512     {
 513       if (vol <= 64)
 514         vol = 65;
 515       gus_ramp_range (vol, 4095);
 516       gus_rampon (0x60);        /* Decreasing, irq */
 517     }
 518   voices[voice].current_volume = vol;
 519 }
 520 
 521 static void
 522 init_envelope (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 523 {
 524   voices[voice].env_phase = -1;
 525   voices[voice].current_volume = 64;
 526 
 527   step_envelope (voice);
 528 }
 529 
 530 static void
 531 start_release (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 532 {
 533   if (gus_read8 (0x00) & 0x03)
 534     return;                     /* Voice already stopped */
 535 
 536   voices[voice].env_phase = 2;  /* Will be incremented by step_envelope */
 537 
 538   voices[voice].current_volume =
 539     voices[voice].initial_volume =
 540     gus_read16 (0x09) >> 4;     /* Get current volume */
 541 
 542   voices[voice].mode &= ~WAVE_SUSTAIN_ON;
 543   gus_rampoff ();
 544   step_envelope (voice);
 545 }
 546 
 547 static void
 548 gus_voice_fade (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 549 {
 550   int             instr_no = sample_map[voice], is16bits;
 551 
 552   if (instr_no < 0 || instr_no > MAX_SAMPLE)
 553     {
 554       gus_write8 (0x00, 0x03);  /* Hard stop */
 555       return;
 556     }
 557 
 558   is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0;   /* 8 or 16 bit samples */
 559 
 560   if (voices[voice].mode & WAVE_ENVELOPES)
 561     {
 562       start_release (voice);
 563       return;
 564     }
 565 
 566   /*
 567    * Ramp the volume down but not too quickly.
 568    */
 569   if ((gus_read16 (0x09) >> 4) < 100)   /* Get current volume */
 570     {
 571       gus_voice_off ();
 572       gus_rampoff ();
 573       gus_voice_init (voice);
 574       return;
 575     }
 576 
 577   gus_ramp_range (65, 4095);
 578   gus_ramp_rate (2, 4);
 579   gus_rampon (0x40 | 0x20);     /* Down, once, irq */
 580   voices[voice].volume_irq_mode = VMODE_HALT;
 581 }
 582 
 583 static void
 584 gus_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 585 {
 586   int             i;
 587 
 588   gus_select_max_voices (24);
 589   volume_base = 3071;
 590   volume_scale = 4;
 591   volume_method = VOL_METHOD_ADAGIO;
 592 
 593   for (i = 0; i < 32; i++)
 594     {
 595       gus_voice_init (i);       /* Turn voice off */
 596     }
 597 
 598   INB (u_Status);               /* Touch the status register */
 599 
 600   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 601   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 602   gus_read8 (0x0f);             /* Clear pending IRQs */
 603 
 604 }
 605 
 606 static void
 607 gus_initialize (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609   unsigned long   flags;
 610   unsigned char   dma_image, irq_image, tmp;
 611 
 612   static unsigned char gus_irq_map[16] =
 613   {0, 0, 1, 3, 0, 2, 0, 4, 0, 0, 0, 5, 6, 0, 0, 7};
 614 
 615   static unsigned char gus_dma_map[8] =
 616   {0, 1, 0, 2, 0, 3, 4, 5};
 617 
 618   DISABLE_INTR (flags);
 619 
 620   gus_write8 (0x4c, 0);         /* Reset GF1 */
 621   gus_delay ();
 622   gus_delay ();
 623 
 624   gus_write8 (0x4c, 1);         /* Release Reset */
 625   gus_delay ();
 626   gus_delay ();
 627 
 628   /*
 629    * Clear all interrupts
 630    */
 631 
 632   gus_write8 (0x41, 0);         /* DMA control */
 633   gus_write8 (0x45, 0);         /* Timer control */
 634   gus_write8 (0x49, 0);         /* Sample control */
 635 
 636   gus_select_max_voices (24);
 637 
 638   INB (u_Status);               /* Touch the status register */
 639 
 640   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 641   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 642   gus_read8 (0x0f);             /* Clear pending IRQs */
 643 
 644   gus_reset ();                 /* Resets all voices */
 645 
 646   gus_look8 (0x41);             /* Clear any pending DMA IRQs */
 647   gus_look8 (0x49);             /* Clear any pending sample IRQs */
 648   gus_read8 (0x0f);             /* Clear pending IRQs */
 649 
 650   gus_write8 (0x4c, 7);         /* Master reset | DAC enable | IRQ enable */
 651 
 652   /*
 653    * Set up for Digital ASIC
 654    */
 655 
 656   OUTB (0x05, gus_base + 0x0f);
 657 
 658   mix_image |= 0x02;            /* Disable line out */
 659   OUTB (mix_image, u_Mixer);
 660 
 661   OUTB (0x00, u_IRQDMAControl);
 662 
 663   OUTB (0x00, gus_base + 0x0f);
 664 
 665   /*
 666    * Now set up the DMA and IRQ interface
 667    * 
 668    * The GUS supports two IRQs and two DMAs.
 669    * 
 670    * If GUS_MIDI_IRQ is defined and if it's != GUS_IRQ, separate Midi IRQ is set
 671    * up. Otherwise the same IRQ is shared by the both devices.
 672    * 
 673    * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
 674    * Adding this support requires significant changes to the dmabuf.c, dsp.c
 675    * and audio.c also.
 676    */
 677 
 678   irq_image = 0;
 679   tmp = gus_irq_map[gus_irq];
 680   if (!tmp)
 681     printk ("Warning! GUS IRQ not selected\n");
 682   irq_image |= tmp;
 683 
 684   if (GUS_MIDI_IRQ != gus_irq)
 685     {                           /* The midi irq was defined and != wave irq */
 686       tmp = gus_irq_map[GUS_MIDI_IRQ];
 687       tmp <<= 3;
 688 
 689       if (!tmp)
 690         printk ("Warning! GUS Midi IRQ not selected\n");
 691       else
 692         gus_set_midi_irq (GUS_MIDI_IRQ);
 693 
 694       irq_image |= tmp;
 695     }
 696   else
 697     irq_image |= 0x40;          /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
 698 
 699   dma_image = 0x40;             /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
 700   tmp = gus_dma_map[gus_dma];
 701   if (!tmp)
 702     printk ("Warning! GUS DMA not selected\n");
 703   dma_image |= tmp;
 704 
 705   /*
 706    * For some reason the IRQ and DMA addresses must be written twice
 707    */
 708 
 709   /* Doing it first time */
 710 
 711   OUTB (mix_image, u_Mixer);    /* Select DMA control */
 712   OUTB (dma_image | 0x80, u_IRQDMAControl);     /* Set DMA address */
 713 
 714   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
 715   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
 716 
 717   /* Doing it second time */
 718 
 719   OUTB (mix_image, u_Mixer);    /* Select DMA control */
 720   OUTB (dma_image, u_IRQDMAControl);    /* Set DMA address */
 721 
 722   OUTB (mix_image | 0x40, u_Mixer);     /* Select IRQ control */
 723   OUTB (irq_image, u_IRQDMAControl);    /* Set IRQ address */
 724 
 725   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
 726 
 727   mix_image &= ~0x02;           /* Enable line out */
 728   mix_image |= 0x08;            /* Enable IRQ */
 729   OUTB (mix_image, u_Mixer);    /* Turn mixer channels on */
 730 
 731   gus_select_voice (0);         /* This disables writes to IRQ/DMA reg */
 732 
 733   gusintr (0);                  /* Serve pending interrupts */
 734   RESTORE_INTR (flags);
 735 }
 736 
 737 int
 738 gus_wave_detect (int baseaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 739 {
 740   gus_base = baseaddr;
 741 
 742   gus_write8 (0x4c, 0);         /* Reset GF1 */
 743   gus_delay ();
 744   gus_delay ();
 745 
 746   gus_write8 (0x4c, 1);         /* Release Reset */
 747   gus_delay ();
 748   gus_delay ();
 749 
 750   gus_poke (0x000, 0xaa);
 751   gus_poke (0x100, 0x55);
 752 
 753   if (gus_peek (0x000) != 0xaa)
 754     return 0;
 755   if (gus_peek (0x100) != 0x55)
 756     return 0;
 757 
 758   gus_mem_size = 0x40000;       /* 256k */
 759   gus_poke (0x40000, 0xaa);
 760   if (gus_peek (0x40000) != 0xaa)
 761     return 1;
 762 
 763   gus_mem_size = 0x80000;       /* 512k */
 764   gus_poke (0x80000, 0xaa);
 765   if (gus_peek (0x80000) != 0xaa)
 766     return 1;
 767 
 768   gus_mem_size = 0xc0000;       /* 768k */
 769   gus_poke (0xc0000, 0xaa);
 770   if (gus_peek (0xc0000) != 0xaa)
 771     return 1;
 772 
 773   gus_mem_size = 0x100000;      /* 1M */
 774 
 775   return 1;
 776 }
 777 
 778 static int
 779 guswave_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 780                unsigned int cmd, unsigned int arg)
 781 {
 782 
 783   switch (cmd)
 784     {
 785     case SNDCTL_SYNTH_INFO:
 786       gus_info.nr_voices = nr_voices;
 787       IOCTL_TO_USER ((char *) arg, 0, &gus_info, sizeof (gus_info));
 788       return 0;
 789       break;
 790 
 791     case SNDCTL_SEQ_RESETSAMPLES:
 792       reset_sample_memory ();
 793       return 0;
 794       break;
 795 
 796     case SNDCTL_SEQ_PERCMODE:
 797       return 0;
 798       break;
 799 
 800     case SNDCTL_SYNTH_MEMAVL:
 801       return gus_mem_size - free_mem_ptr - 32;
 802 
 803     default:
 804       return RET_ERROR (EINVAL);
 805     }
 806 }
 807 
 808 static int
 809 guswave_set_instr (int dev, int voice, int instr_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 810 {
 811   int             sample_no;
 812 
 813   if (instr_no < 0 || instr_no > MAX_PATCH)
 814     return RET_ERROR (EINVAL);
 815 
 816   if (voice < 0 || voice > 31)
 817     return RET_ERROR (EINVAL);
 818 
 819   sample_no = patch_table[instr_no];
 820   patch_map[voice] = -1;
 821 
 822   if (sample_no < 0)
 823     {
 824       printk ("GUS: Undefined patch %d for voice %d\n", instr_no, voice);
 825       return RET_ERROR (EINVAL);/* Patch not defined */
 826     }
 827 
 828   if (sample_ptrs[sample_no] == -1)     /* Sample not loaded */
 829     {
 830       printk ("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);
 831       return RET_ERROR (EINVAL);
 832     }
 833 
 834   sample_map[voice] = sample_no;
 835   patch_map[voice] = instr_no;
 836   return 0;
 837 }
 838 
 839 static int
 840 guswave_kill_note (int dev, int voice, int velocity)
     /* [previous][next][first][last][top][bottom][index][help] */
 841 {
 842   unsigned long   flags;
 843 
 844   DISABLE_INTR (flags);
 845   gus_select_voice (voice);
 846   gus_voice_fade (voice);
 847   RESTORE_INTR (flags);
 848 
 849   return 0;
 850 }
 851 
 852 static void
 853 guswave_aftertouch (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 854 {
 855   short           lo_limit, hi_limit;
 856   unsigned long   flags;
 857 
 858   return;                       /* Currently disabled */
 859 
 860   if (voice < 0 || voice > 31)
 861     return;
 862 
 863   if (voices[voice].mode & WAVE_ENVELOPES && voices[voice].env_phase != 2)
 864     return;                     /* Don't mix with envelopes */
 865 
 866   if (pressure < 32)
 867     {
 868       DISABLE_INTR (flags);
 869       gus_select_voice (voice);
 870       gus_rampoff ();
 871       compute_and_set_volume (voice, 255, 0);   /* Back to original volume */
 872       RESTORE_INTR (flags);
 873       return;
 874     }
 875 
 876   hi_limit = voices[voice].current_volume;
 877   lo_limit = hi_limit * 99 / 100;
 878   if (lo_limit < 65)
 879     lo_limit = 65;
 880 
 881   DISABLE_INTR (flags);
 882   gus_select_voice (voice);
 883   if (hi_limit > (4095 - 65))
 884     {
 885       hi_limit = 4095 - 65;
 886       gus_voice_volume (hi_limit);
 887     }
 888   gus_ramp_range (lo_limit, hi_limit);
 889   gus_ramp_rate (3, 8);
 890   gus_rampon (0x58);            /* Bidirectional, Down, Loop */
 891   RESTORE_INTR (flags);
 892 }
 893 
 894 static void
 895 guswave_panning (int dev, int voice, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 896 {
 897   if (voice >= 0 || voice < 32)
 898     voices[voice].panning = value;
 899 }
 900 
 901 static void
 902 compute_volume (int voice, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 903 {
 904   if (volume < 128)
 905     {
 906       voices[voice].midi_volume = volume;
 907 
 908       switch (volume_method)
 909         {
 910         case VOL_METHOD_ADAGIO:
 911           voices[voice].initial_volume =
 912             gus_adagio_vol (volume, voices[voice].main_vol,
 913                             voices[voice].expression_vol,
 914                             voices[voice].patch_vol);
 915           break;
 916 
 917         default:
 918           voices[voice].initial_volume = volume_base + (volume * volume_scale);
 919         }
 920     }
 921 
 922   if (voices[voice].initial_volume > 4095)
 923     voices[voice].initial_volume = 4095;
 924 }
 925 
 926 static void
 927 compute_and_set_volume (int voice, int volume, int ramp_time)
     /* [previous][next][first][last][top][bottom][index][help] */
 928 {
 929   int             current, target, rate;
 930 
 931   compute_volume (voice, volume);
 932   voices[voice].current_volume = voices[voice].initial_volume;
 933 
 934   current = gus_read16 (0x09) >> 4;
 935   target = voices[voice].initial_volume;
 936 
 937   if (ramp_time == INSTANT_RAMP)
 938     {
 939       gus_rampoff ();
 940       gus_voice_volume (target);
 941       return;
 942     }
 943 
 944   if (ramp_time == FAST_RAMP)
 945     rate = 63;
 946   else
 947     rate = 16;
 948   gus_ramp_rate (0, rate);
 949 
 950   if ((target - current) / 64 == 0)     /* Too close */
 951     {
 952       gus_rampoff ();
 953       gus_voice_volume (target);
 954       return;
 955     }
 956 
 957   if (target > current)
 958     {
 959       if (target > (4095 - 65))
 960         target = 4095 - 65;
 961       gus_ramp_range (current, target);
 962       gus_rampon (0x00);        /* Ramp up, once, no irq */
 963     }
 964   else
 965     {
 966       if (target < 65)
 967         target = 65;
 968 
 969       gus_ramp_range (target, current);
 970       gus_rampon (0x40);        /* Ramp down, once, no irq */
 971     }
 972 }
 973 
 974 static void
 975 dynamic_volume_change (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
 976 {
 977   unsigned char   status;
 978   unsigned long   flags;
 979 
 980   DISABLE_INTR (flags);
 981   gus_select_voice (voice);
 982   status = gus_read8 (0x00);    /* Voice status */
 983   RESTORE_INTR (flags);
 984 
 985   if (status & 0x03)
 986     return;                     /* Voice not started */
 987 
 988   if (!(voices[voice].mode & WAVE_ENVELOPES))
 989     {
 990       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
 991       return;
 992     }
 993 
 994   /*
 995    * Voice is running and has envelopes.
 996    */
 997 
 998   DISABLE_INTR (flags);
 999   gus_select_voice (voice);
1000   status = gus_read8 (0x0d);    /* Ramping status */
1001   RESTORE_INTR (flags);
1002 
1003   if (status & 0x03)            /* Sustain phase? */
1004     {
1005       compute_and_set_volume (voice, voices[voice].midi_volume, 1);
1006       return;
1007     }
1008 
1009   if (voices[voice].env_phase < 0)
1010     return;
1011 
1012   compute_volume (voice, voices[voice].midi_volume);
1013 
1014 #if 0                           /* Is this really required */
1015   voices[voice].current_volume =
1016     gus_read16 (0x09) >> 4;     /* Get current volume */
1017 
1018   voices[voice].env_phase--;
1019   step_envelope (voice);
1020 #endif
1021 }
1022 
1023 static void
1024 guswave_controller (int dev, int voice, int ctrl_num, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1025 {
1026   unsigned long   flags;
1027   unsigned long   freq;
1028 
1029   if (voice < 0 || voice > 31)
1030     return;
1031 
1032   switch (ctrl_num)
1033     {
1034     case CTRL_PITCH_BENDER:
1035       voices[voice].bender = value;
1036       freq = compute_finetune (voices[voice].orig_freq, value, voices[voice].bender_range);
1037       voices[voice].current_freq = freq;
1038 
1039       DISABLE_INTR (flags);
1040       gus_select_voice (voice);
1041       gus_voice_freq (freq);
1042       RESTORE_INTR (flags);
1043       break;
1044 
1045     case CTRL_PITCH_BENDER_RANGE:
1046       voices[voice].bender_range = value;
1047       break;
1048 
1049     case CTRL_EXPRESSION:
1050       volume_method = VOL_METHOD_ADAGIO;
1051       voices[voice].expression_vol = value;
1052       dynamic_volume_change (voice);
1053       break;
1054 
1055     case CTRL_MAIN_VOLUME:
1056       volume_method = VOL_METHOD_ADAGIO;
1057       voices[voice].main_vol = value;
1058       dynamic_volume_change (voice);
1059       break;
1060 
1061     default:                    /* Ignore */
1062       break;
1063     }
1064 }
1065 
1066 static int
1067 guswave_start_note (int dev, int voice, int note_num, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
1068 {
1069   int             sample, best_sample, best_delta, delta_freq;
1070   int             is16bits, samplep, patch, pan;
1071   unsigned long   note_freq, base_note, freq, flags;
1072   unsigned char   mode = 0;
1073 
1074   if (voice < 0 || voice > 31)
1075     {
1076       printk ("GUS: Invalid voice\n");
1077       return RET_ERROR (EINVAL);
1078     }
1079 
1080   if (note_num == 255)
1081     {
1082       if (voices[voice].mode & WAVE_ENVELOPES)
1083         {
1084           voices[voice].midi_volume = volume;
1085           dynamic_volume_change (voice);
1086           return 0;
1087         }
1088 
1089       compute_and_set_volume (voice, volume, 1);
1090       return 0;
1091     }
1092 
1093   if ((patch = patch_map[voice]) == -1)
1094     {
1095       return RET_ERROR (EINVAL);
1096     }
1097 
1098   if ((samplep = patch_table[patch]) == -1)
1099     {
1100       return RET_ERROR (EINVAL);
1101     }
1102 
1103   note_freq = note_to_freq (note_num);
1104 
1105   /*
1106    * Find a sample within a patch so that the note_freq is between low_note
1107    * and high_note.
1108    */
1109   sample = -1;
1110 
1111   best_sample = samplep;
1112   best_delta = 1000000;
1113   while (samplep >= 0 && sample == -1)
1114     {
1115       delta_freq = note_freq - samples[samplep].base_note;
1116       if (delta_freq < 0)
1117         delta_freq = -delta_freq;
1118       if (delta_freq < best_delta)
1119         {
1120           best_sample = samplep;
1121           best_delta = delta_freq;
1122         }
1123       if (samples[samplep].low_note <= note_freq && note_freq <= samples[samplep].high_note)
1124         sample = samplep;
1125       else
1126         samplep = samples[samplep].key; /* Follow link */
1127     }
1128   if (sample == -1)
1129     sample = best_sample;
1130 
1131   if (sample == -1)
1132     {
1133       printk ("GUS: Patch %d not defined for note %d\n", patch, note_num);
1134       return 0;                 /* Should play default patch ??? */
1135     }
1136 
1137   is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;     /* 8 or 16 bit samples */
1138   voices[voice].mode = samples[sample].mode;
1139   voices[voice].patch_vol = samples[sample].volume;
1140 
1141   if (voices[voice].mode & WAVE_ENVELOPES)
1142     {
1143       int             i;
1144 
1145       for (i = 0; i < 6; i++)
1146         {
1147           voices[voice].env_rate[i] = samples[sample].env_rate[i];
1148           voices[voice].env_offset[i] = samples[sample].env_offset[i];
1149         }
1150     }
1151 
1152   sample_map[voice] = sample;
1153 
1154   base_note = samples[sample].base_note / 100;  /* To avoid overflows */
1155   note_freq /= 100;
1156 
1157   freq = samples[sample].base_freq * note_freq / base_note;
1158 
1159   voices[voice].orig_freq = freq;
1160 
1161   /*
1162    * Since the pitch bender may have been set before playing the note, we
1163    * have to calculate the bending now.
1164    */
1165 
1166   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1167   voices[voice].current_freq = freq;
1168 
1169   pan = (samples[sample].panning + voices[voice].panning) / 32;
1170   pan += 7;
1171   if (pan < 0)
1172     pan = 0;
1173   if (pan > 15)
1174     pan = 15;
1175 
1176   if (samples[sample].mode & WAVE_16_BITS)
1177     {
1178       mode |= 0x04;             /* 16 bits */
1179       if ((sample_ptrs[sample] >> 18) !=
1180           ((sample_ptrs[sample] + samples[sample].len) >> 18))
1181         printk ("GUS: Sample address error\n");
1182     }
1183 
1184   /*************************************************************************
1185  *      CAUTION!        Interrupts disabled. Don't return before enabling
1186  *************************************************************************/
1187 
1188   DISABLE_INTR (flags);
1189   gus_select_voice (voice);
1190   gus_voice_off ();             /* It may still be running */
1191   gus_rampoff ();
1192   if (voices[voice].mode & WAVE_ENVELOPES)
1193     {
1194       compute_volume (voice, volume);
1195       init_envelope (voice);
1196     }
1197   else
1198     compute_and_set_volume (voice, volume, 0);
1199 
1200   if (samples[sample].mode & WAVE_LOOP_BACK)
1201     gus_write_addr (0x0a, sample_ptrs[sample] + samples[sample].len, is16bits); /* Sample start=end */
1202   else
1203     gus_write_addr (0x0a, sample_ptrs[sample], is16bits);       /* Sample start=begin */
1204 
1205   if (samples[sample].mode & WAVE_LOOPING)
1206     {
1207       mode |= 0x08;             /* Looping on */
1208 
1209       if (samples[sample].mode & WAVE_BIDIR_LOOP)
1210         mode |= 0x10;           /* Bidirectional looping on */
1211 
1212       if (samples[sample].mode & WAVE_LOOP_BACK)
1213         {
1214           gus_write_addr (0x0a, /* Put the current location = loop_end */
1215                   sample_ptrs[sample] + samples[sample].loop_end, is16bits);
1216           mode |= 0x40;         /* Loop backwards */
1217         }
1218 
1219       gus_write_addr (0x02, sample_ptrs[sample] + samples[sample].loop_start, is16bits);        /* Loop start location */
1220       gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].loop_end, is16bits);  /* Loop end location */
1221     }
1222   else
1223     {
1224       mode |= 0x20;             /* Loop irq at the end */
1225       voices[voice].loop_irq_mode = LMODE_FINISH;       /* Ramp it down at the
1226                                                          * end */
1227       voices[voice].loop_irq_parm = 1;
1228       gus_write_addr (0x02, sample_ptrs[sample], is16bits);     /* Loop start location */
1229       gus_write_addr (0x04, sample_ptrs[sample] + samples[sample].len, is16bits);       /* Loop end location */
1230     }
1231   gus_voice_freq (freq);
1232   gus_voice_balance (pan);
1233   gus_voice_on (mode);
1234   RESTORE_INTR (flags);
1235 
1236   return 0;
1237 }
1238 
1239 static void
1240 guswave_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1241 {
1242   int             i;
1243 
1244   for (i = 0; i < 32; i++)
1245     gus_voice_init (i);
1246 }
1247 
1248 static int
1249 guswave_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251   int             err;
1252 
1253   if (gus_busy)
1254     return RET_ERROR (EBUSY);
1255 
1256   if ((err = DMAbuf_open_dma (gus_devnum)))
1257     return err;
1258 
1259   RESET_WAIT_QUEUE (dram_sleeper, dram_sleep_flag);
1260   gus_busy = 1;
1261   active_device = GUS_DEV_WAVE;
1262 
1263   gus_reset ();
1264 
1265   return 0;
1266 }
1267 
1268 static void
1269 guswave_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1270 {
1271   gus_busy = 0;
1272   active_device = 0;
1273   gus_reset ();
1274 
1275   DMAbuf_close_dma (gus_devnum);
1276 }
1277 
1278 static int
1279 guswave_load_patch (int dev, int format, snd_rw_buf * addr,
     /* [previous][next][first][last][top][bottom][index][help] */
1280                     int offs, int count, int pmgr_flag)
1281 {
1282   struct patch_info patch;
1283   int             instr;
1284   long            sizeof_patch;
1285 
1286   unsigned long   blk_size, blk_end, left, src_offs, target;
1287 
1288   sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Size of the header
1289                                                          * info */
1290 
1291   if (format != GUS_PATCH)
1292     {
1293       printk ("GUS Error: Invalid patch format (key) 0x%04x\n", format);
1294       return RET_ERROR (EINVAL);
1295     }
1296 
1297   if (count < sizeof_patch)
1298     {
1299       printk ("GUS Error: Patch header too short\n");
1300       return RET_ERROR (EINVAL);
1301     }
1302 
1303   count -= sizeof_patch;
1304 
1305   if (free_sample >= MAX_SAMPLE)
1306     {
1307       printk ("GUS: Sample table full\n");
1308       return RET_ERROR (ENOSPC);
1309     }
1310 
1311   /*
1312    * Copy the header from user space but ignore the first bytes which have
1313    * been transferred already.
1314    */
1315 
1316   COPY_FROM_USER (&((char *) &patch)[offs], addr, offs, sizeof_patch - offs);
1317 
1318   instr = patch.instr_no;
1319 
1320   if (instr < 0 || instr > MAX_PATCH)
1321     {
1322       printk ("GUS: Invalid patch number %d\n", instr);
1323       return RET_ERROR (EINVAL);
1324     }
1325 
1326   if (count < patch.len)
1327     {
1328       printk ("GUS Warning: Patch record too short (%d<%d)\n",
1329               count, patch.len);
1330       patch.len = count;
1331     }
1332 
1333   if (patch.len <= 0 || patch.len > gus_mem_size)
1334     {
1335       printk ("GUS: Invalid sample length %d\n", patch.len);
1336       return RET_ERROR (EINVAL);
1337     }
1338 
1339   if (patch.mode & WAVE_LOOPING)
1340     {
1341       if (patch.loop_start < 0 || patch.loop_start >= patch.len)
1342         {
1343           printk ("GUS: Invalid loop start\n");
1344           return RET_ERROR (EINVAL);
1345         }
1346 
1347       if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
1348         {
1349           printk ("GUS: Invalid loop end\n");
1350           return RET_ERROR (EINVAL);
1351         }
1352     }
1353 
1354   free_mem_ptr = (free_mem_ptr + 31) & ~31;     /* Alignment 32 bytes */
1355 
1356 #define GUS_BANK_SIZE (256*1024)
1357 
1358   if (patch.mode & WAVE_16_BITS)
1359     {
1360       /*
1361        * 16 bit samples must fit one 256k bank.
1362        */
1363       if (patch.len >= GUS_BANK_SIZE)
1364         {
1365           printk ("GUS: Sample (16 bit) too long %d\n", patch.len);
1366           return RET_ERROR (ENOSPC);
1367         }
1368 
1369       if ((free_mem_ptr / GUS_BANK_SIZE) !=
1370           ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
1371         {
1372           unsigned long   tmp_mem =     /* Align to 256K*N */
1373           ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
1374 
1375           if ((tmp_mem + patch.len) > gus_mem_size)
1376             return RET_ERROR (ENOSPC);
1377 
1378           free_mem_ptr = tmp_mem;       /* This leaves unusable memory */
1379         }
1380     }
1381 
1382   if ((free_mem_ptr + patch.len) > gus_mem_size)
1383     return RET_ERROR (ENOSPC);
1384 
1385   sample_ptrs[free_sample] = free_mem_ptr;
1386 
1387   /* Tremolo is not possible with envelopes */
1388 
1389   if (patch.mode & WAVE_ENVELOPES)
1390     patch.mode &= ~WAVE_TREMOLO;
1391 
1392   memcpy ((char *) &samples[free_sample], &patch, sizeof_patch);
1393 
1394   /*
1395    * Link this_one sample to the list of samples for patch 'instr'.
1396    */
1397 
1398   samples[free_sample].key = patch_table[instr];
1399   patch_table[instr] = free_sample;
1400 
1401   /*
1402    * Use DMA to transfer the wave data to the DRAM
1403    */
1404 
1405   left = patch.len;
1406   src_offs = 0;
1407   target = free_mem_ptr;
1408 
1409   while (left)                  /* Not all moved */
1410     {
1411       blk_size = sound_buffsizes[gus_devnum];
1412       if (blk_size > left)
1413         blk_size = left;
1414 
1415       /*
1416        * DMA cannot cross 256k bank boundaries. Check for that.
1417        */
1418       blk_end = target + blk_size;
1419 
1420       if ((target >> 18) != (blk_end >> 18))
1421         {                       /* Have to split the block */
1422 
1423           blk_end &= ~(256 * 1024 - 1);
1424           blk_size = blk_end - target;
1425         }
1426 
1427 #ifdef GUS_NO_DMA
1428       /*
1429        * For some reason the DMA is not possible. We have to use PIO.
1430        */
1431       {
1432         long            i;
1433         unsigned char   data;
1434 
1435         for (i = 0; i < blk_size; i++)
1436           {
1437             GET_BYTE_FROM_USER (data, addr, sizeof_patch + i);
1438             gus_poke (target + i, data);
1439           }
1440       }
1441 #else /* GUS_NO_DMA */
1442       {
1443         unsigned long   address, hold_address;
1444         unsigned char   dma_command;
1445 
1446         /*
1447          * OK, move now. First in and then out.
1448          */
1449 
1450         COPY_FROM_USER (snd_raw_buf[gus_devnum][0],
1451                         addr, sizeof_patch + src_offs,
1452                         blk_size);
1453 
1454         gus_write8 (0x41, 0);   /* Disable GF1 DMA */
1455         DMAbuf_start_dma (gus_devnum, snd_raw_buf_phys[gus_devnum][0],
1456                           blk_size, DMA_MODE_WRITE);
1457 
1458         /*
1459          * Set the DRAM address for the wave data
1460          */
1461 
1462         address = target;
1463 
1464         if (sound_dsp_dmachan[gus_devnum] > 3)
1465           {
1466             hold_address = address;
1467             address = address >> 1;
1468             address &= 0x0001ffffL;
1469             address |= (hold_address & 0x000c0000L);
1470           }
1471 
1472         gus_write16 (0x42, (address >> 4) & 0xffff);    /* DRAM DMA address */
1473 
1474         /*
1475          * Start the DMA transfer
1476          */
1477 
1478         dma_command = 0x21;     /* IRQ enable, DMA start */
1479         if (patch.mode & WAVE_UNSIGNED)
1480           dma_command |= 0x80;  /* Invert MSB */
1481         if (patch.mode & WAVE_16_BITS)
1482           dma_command |= 0x40;  /* 16 bit _DATA_ */
1483         if (sound_dsp_dmachan[gus_devnum] > 3)
1484           dma_command |= 0x04;  /* 16 bit DMA channel */
1485 
1486         gus_write8 (0x41, dma_command); /* Let's go luteet (=bugs) */
1487 
1488         /*
1489          * Sleep here until the DRAM DMA done interrupt is served
1490          */
1491         active_device = GUS_DEV_WAVE;
1492 
1493         DO_SLEEP (dram_sleeper, dram_sleep_flag, HZ);
1494         if (TIMED_OUT (dram_sleeper, dram_sleep_flag))
1495           printk ("GUS: DMA Transfer timed out\n");
1496       }
1497 #endif /* GUS_NO_DMA */
1498 
1499       /*
1500        * Now the next part
1501        */
1502 
1503       left -= blk_size;
1504       src_offs += blk_size;
1505       target += blk_size;
1506 
1507       gus_write8 (0x41, 0);     /* Stop DMA */
1508     }
1509 
1510   free_mem_ptr += patch.len;
1511 
1512   if (!pmgr_flag)
1513     pmgr_inform (dev, PM_E_PATCH_LOADED, instr, free_sample, 0, 0);
1514   free_sample++;
1515   return 0;
1516 }
1517 
1518 static void
1519 guswave_hw_control (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
1520 {
1521   int             voice, cmd;
1522   unsigned short  p1, p2;
1523   unsigned long   plong, flags;
1524 
1525   cmd = event[2];
1526   voice = event[3];
1527   p1 = *(unsigned short *) &event[4];
1528   p2 = *(unsigned short *) &event[6];
1529   plong = *(unsigned long *) &event[4];
1530 
1531   switch (cmd)
1532     {
1533 
1534     case _GUS_NUMVOICES:
1535       DISABLE_INTR (flags);
1536       gus_select_voice (voice);
1537       gus_select_max_voices (p1);
1538       RESTORE_INTR (flags);
1539       break;
1540 
1541     case _GUS_VOICESAMPLE:
1542       guswave_set_instr (dev, voice, p1);
1543       break;
1544 
1545     case _GUS_VOICEON:
1546       DISABLE_INTR (flags);
1547       gus_select_voice (voice);
1548       p1 &= ~0x20;              /* Disable intr */
1549       gus_voice_on (p1);
1550       RESTORE_INTR (flags);
1551       break;
1552 
1553     case _GUS_VOICEOFF:
1554       DISABLE_INTR (flags);
1555       gus_select_voice (voice);
1556       gus_voice_off ();
1557       RESTORE_INTR (flags);
1558       break;
1559 
1560     case _GUS_VOICEFADE:
1561       DISABLE_INTR (flags);
1562       gus_select_voice (voice);
1563       gus_voice_fade (voice);
1564       RESTORE_INTR (flags);
1565       break;
1566 
1567     case _GUS_VOICEMODE:
1568       DISABLE_INTR (flags);
1569       gus_select_voice (voice);
1570       p1 &= ~0x20;              /* Disable intr */
1571       gus_voice_mode (p1);
1572       RESTORE_INTR (flags);
1573       break;
1574 
1575     case _GUS_VOICEBALA:
1576       DISABLE_INTR (flags);
1577       gus_select_voice (voice);
1578       gus_voice_balance (p1);
1579       RESTORE_INTR (flags);
1580       break;
1581 
1582     case _GUS_VOICEFREQ:
1583       DISABLE_INTR (flags);
1584       gus_select_voice (voice);
1585       gus_voice_freq (plong);
1586       RESTORE_INTR (flags);
1587       break;
1588 
1589     case _GUS_VOICEVOL:
1590       DISABLE_INTR (flags);
1591       gus_select_voice (voice);
1592       gus_voice_volume (p1);
1593       RESTORE_INTR (flags);
1594       break;
1595 
1596     case _GUS_VOICEVOL2:        /* Just update the voice value */
1597       voices[voice].initial_volume =
1598         voices[voice].current_volume = p1;
1599       break;
1600 
1601     case _GUS_RAMPRANGE:
1602       if (voices[voice].mode & WAVE_ENVELOPES)
1603         break;                  /* NO-NO */
1604       DISABLE_INTR (flags);
1605       gus_select_voice (voice);
1606       gus_ramp_range (p1, p2);
1607       RESTORE_INTR (flags);
1608       break;
1609 
1610     case _GUS_RAMPRATE:
1611       if (voices[voice].mode & WAVE_ENVELOPES)
1612         break;                  /* NO-NO */
1613       DISABLE_INTR (flags);
1614       gus_select_voice (voice);
1615       gus_ramp_rate (p1, p2);
1616       RESTORE_INTR (flags);
1617       break;
1618 
1619     case _GUS_RAMPMODE:
1620       if (voices[voice].mode & WAVE_ENVELOPES)
1621         break;                  /* NO-NO */
1622       DISABLE_INTR (flags);
1623       gus_select_voice (voice);
1624       p1 &= ~0x20;              /* Disable intr */
1625       gus_ramp_mode (p1);
1626       RESTORE_INTR (flags);
1627       break;
1628 
1629     case _GUS_RAMPON:
1630       if (voices[voice].mode & WAVE_ENVELOPES)
1631         break;                  /* NO-NO */
1632       DISABLE_INTR (flags);
1633       gus_select_voice (voice);
1634       p1 &= ~0x20;              /* Disable intr */
1635       gus_rampon (p1);
1636       RESTORE_INTR (flags);
1637       break;
1638 
1639     case _GUS_RAMPOFF:
1640       if (voices[voice].mode & WAVE_ENVELOPES)
1641         break;                  /* NO-NO */
1642       DISABLE_INTR (flags);
1643       gus_select_voice (voice);
1644       gus_rampoff ();
1645       RESTORE_INTR (flags);
1646       break;
1647 
1648     case _GUS_VOLUME_SCALE:
1649       volume_base = p1;
1650       volume_scale = p2;
1651       break;
1652 
1653     default:;
1654     }
1655 }
1656 
1657 static int
1658 gus_sampling_set_speed (int speed)
     /* [previous][next][first][last][top][bottom][index][help] */
1659 {
1660   if (speed <= 0)
1661     return gus_sampling_speed;
1662 
1663   if (speed > 44100)
1664     speed = 44100;
1665 
1666   gus_sampling_speed = speed;
1667   return speed;
1668 }
1669 
1670 static int
1671 gus_sampling_set_channels (int channels)
     /* [previous][next][first][last][top][bottom][index][help] */
1672 {
1673   if (!channels)
1674     return gus_sampling_channels;
1675   if (channels > 2)
1676     channels = 2;
1677   if (channels < 1)
1678     channels = 1;
1679   gus_sampling_channels = channels;
1680   return channels;
1681 }
1682 
1683 static int
1684 gus_sampling_set_bits (int bits)
     /* [previous][next][first][last][top][bottom][index][help] */
1685 {
1686   if (!bits)
1687     return gus_sampling_bits;
1688 
1689   if (bits != 8 && bits != 16)
1690     bits = 8;
1691 
1692   gus_sampling_bits = bits;
1693   return bits;
1694 }
1695 
1696 static int
1697 gus_sampling_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
1698 {
1699   switch (cmd)
1700     {
1701     case SOUND_PCM_WRITE_RATE:
1702       if (local)
1703         return gus_sampling_set_speed (arg);
1704       return IOCTL_OUT (arg, gus_sampling_set_speed (IOCTL_IN (arg)));
1705       break;
1706 
1707     case SOUND_PCM_READ_RATE:
1708       if (local)
1709         return gus_sampling_speed;
1710       return IOCTL_OUT (arg, gus_sampling_speed);
1711       break;
1712 
1713     case SNDCTL_DSP_STEREO:
1714       if (local)
1715         return gus_sampling_set_channels (arg + 1) - 1;
1716       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg) + 1) - 1);
1717       break;
1718 
1719     case SOUND_PCM_WRITE_CHANNELS:
1720       return IOCTL_OUT (arg, gus_sampling_set_channels (IOCTL_IN (arg)));
1721       break;
1722 
1723     case SOUND_PCM_READ_CHANNELS:
1724       if (local)
1725         return gus_sampling_channels;
1726       return IOCTL_OUT (arg, gus_sampling_channels);
1727       break;
1728 
1729     case SNDCTL_DSP_SAMPLESIZE:
1730       if (local)
1731         return gus_sampling_set_bits (arg);
1732       return IOCTL_OUT (arg, gus_sampling_set_bits (IOCTL_IN (arg)));
1733       break;
1734 
1735     case SOUND_PCM_READ_BITS:
1736       if (local)
1737         return gus_sampling_bits;
1738       return IOCTL_OUT (arg, gus_sampling_bits);
1739 
1740     case SOUND_PCM_WRITE_FILTER:        /* NOT YET IMPLEMENTED */
1741       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1742       break;
1743 
1744     case SOUND_PCM_READ_FILTER:
1745       return IOCTL_OUT (arg, RET_ERROR (EINVAL));
1746       break;
1747 
1748     default:
1749       return RET_ERROR (EINVAL);
1750     }
1751   return RET_ERROR (EINVAL);
1752 }
1753 
1754 static void
1755 gus_sampling_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1756 {
1757 }
1758 
1759 static int
1760 gus_sampling_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1761 {
1762 #ifdef GUS_NO_DMA
1763   printk ("GUS: DMA mode not enabled. Device not supported\n");
1764   return RET_ERROR (ENXIO);
1765 #endif
1766 
1767   if (gus_busy)
1768     return RET_ERROR (EBUSY);
1769 
1770   gus_busy = 1;
1771   active_device = 0;
1772 
1773   gus_reset ();
1774   reset_sample_memory ();
1775   gus_select_max_voices (14);
1776 
1777   gus_sampling_set_bits (8);
1778   gus_sampling_set_channels (1);
1779   gus_sampling_set_speed (DSP_DEFAULT_SPEED);
1780   pcm_active = 0;
1781 
1782   return 0;
1783 }
1784 
1785 static void
1786 gus_sampling_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1787 {
1788   gus_reset ();
1789   gus_busy = 0;
1790   active_device = 0;
1791 }
1792 
1793 static void
1794 play_next_pcm_block (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1795 {
1796   unsigned long   flags;
1797   int             speed = gus_sampling_speed;
1798   int             this_one, is16bits, chn;
1799   unsigned long   dram_loc;
1800   unsigned char   mode[2], ramp_mode[2];
1801 
1802   if (!pcm_qlen)
1803     return;
1804 
1805   this_one = pcm_head;
1806 
1807   for (chn = 0; chn < gus_sampling_channels; chn++)
1808     {
1809       mode[chn] = 0x00;
1810       ramp_mode[chn] = 0x03;    /* Ramping and rollover off */
1811 
1812       if (chn == 0)
1813         {
1814           mode[chn] |= 0x20;    /* Loop irq */
1815           voices[chn].loop_irq_mode = LMODE_PCM;
1816         }
1817 
1818       if (gus_sampling_bits != 8)
1819         {
1820           is16bits = 1;
1821           mode[chn] |= 0x04;    /* 16 bit data */
1822         }
1823       else
1824         is16bits = 0;
1825 
1826       dram_loc = this_one * pcm_bsize;
1827       dram_loc += chn * pcm_banksize;
1828 
1829       if (this_one == (pcm_nblk - 1))   /* Last of the DRAM buffers */
1830         {
1831           mode[chn] |= 0x08;    /* Enable loop */
1832           ramp_mode[chn] = 0x03;/* Disable rollover */
1833         }
1834       else
1835         {
1836           if (chn == 0)
1837             ramp_mode[chn] = 0x04;      /* Enable rollover bit */
1838         }
1839 
1840       DISABLE_INTR (flags);
1841       gus_select_voice (chn);
1842       gus_voice_freq (speed);
1843 
1844       if (gus_sampling_channels == 1)
1845         gus_voice_balance (7);  /* mono */
1846       else if (chn == 0)
1847         gus_voice_balance (0);  /* left */
1848       else
1849         gus_voice_balance (15); /* right */
1850 
1851       if (!pcm_active)          /* Voice not started yet */
1852         {
1853           /*
1854            * The playback was not started yet (or there has been a pause).
1855            * Start the voice (again) and ask for a rollover irq at the end of
1856            * this_one block. If this_one one is last of the buffers, use just
1857            * the normal loop with irq.
1858            */
1859 
1860           gus_voice_off ();     /* It could already be running */
1861           gus_rampoff ();
1862           gus_voice_volume (4000);
1863           gus_ramp_range (65, 4030);
1864 
1865           gus_write_addr (0x0a, dram_loc, is16bits);    /* Starting position */
1866           gus_write_addr (0x02, chn * pcm_banksize, is16bits);  /* Loop start location */
1867 
1868           if (chn != 0)
1869             gus_write_addr (0x04, pcm_banksize + (pcm_bsize * pcm_nblk),
1870                             is16bits);  /* Loop end location */
1871         }
1872 
1873       if (chn == 0)
1874         gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);     /* Loop end location */
1875       else
1876         mode[chn] |= 0x08;      /* Enable loop */
1877 
1878       if (pcm_datasize[this_one] != pcm_bsize)
1879         {
1880           /* Incomplete block. Possibly the last one. */
1881           if (chn == 0)
1882             {
1883               mode[chn] &= ~0x08;       /* Disable loop */
1884               mode[chn] |= 0x20;/* Enable loop IRQ */
1885               voices[0].loop_irq_mode = LMODE_PCM_STOP;
1886               ramp_mode[chn] = 0x03;    /* No rollover bit */
1887             }
1888           else
1889             {
1890               gus_write_addr (0x04, dram_loc + pcm_datasize[this_one], is16bits);       /* Loop end location */
1891               mode[chn] &= ~0x08;       /* Disable loop */
1892             }
1893         }
1894 
1895       RESTORE_INTR (flags);
1896     }
1897 
1898   for (chn = 0; chn < gus_sampling_channels; chn++)
1899     {
1900       DISABLE_INTR (flags);
1901       gus_select_voice (chn);
1902       gus_write8 (0x0d, ramp_mode[chn]);
1903       gus_voice_on (mode[chn]);
1904       RESTORE_INTR (flags);
1905     }
1906 
1907   pcm_active = 1;
1908 }
1909 
1910 static void
1911 gus_transfer_output_block (int dev, unsigned long buf,
     /* [previous][next][first][last][top][bottom][index][help] */
1912                            int total_count, int intrflag, int chn)
1913 {
1914   /*
1915    * This routine transfers one block of audio data to the DRAM. In mono mode
1916    * it's called just once. When in stereo mode, this_one routine is called
1917    * once for both channels.
1918    * 
1919    * The left/mono channel data is transferred to the beginning of dram and the
1920    * right data to the area pointed by gus_page_size.
1921    */
1922 
1923   int             this_one, count;
1924   unsigned long   flags;
1925   unsigned char   dma_command;
1926   unsigned long   address, hold_address;
1927 
1928   DISABLE_INTR (flags);
1929 
1930   count = total_count / gus_sampling_channels;
1931 
1932   if (chn == 0)
1933     {
1934       if (pcm_qlen >= pcm_nblk)
1935         printk ("GUS Warning: PCM buffers out of sync\n");
1936 
1937       this_one = pcm_current_block = pcm_tail;
1938       pcm_qlen++;
1939       pcm_tail = (pcm_tail + 1) % pcm_nblk;
1940       pcm_datasize[this_one] = count;
1941     }
1942   else
1943     this_one = pcm_current_block;
1944 
1945   gus_write8 (0x41, 0);         /* Disable GF1 DMA */
1946   DMAbuf_start_dma (dev, buf + (chn * count), count, DMA_MODE_WRITE);
1947 
1948   address = this_one * pcm_bsize;
1949   address += chn * pcm_banksize;
1950 
1951   if (sound_dsp_dmachan[dev] > 3)
1952     {
1953       hold_address = address;
1954       address = address >> 1;
1955       address &= 0x0001ffffL;
1956       address |= (hold_address & 0x000c0000L);
1957     }
1958 
1959   gus_write16 (0x42, (address >> 4) & 0xffff);  /* DRAM DMA address */
1960 
1961   dma_command = 0x21;           /* IRQ enable, DMA start */
1962 
1963   if (gus_sampling_bits != 8)
1964     dma_command |= 0x40;        /* 16 bit _DATA_ */
1965   else
1966     dma_command |= 0x80;        /* Invert MSB */
1967 
1968   if (sound_dsp_dmachan[dev] > 3)
1969     dma_command |= 0x04;        /* 16 bit DMA channel */
1970 
1971   gus_write8 (0x41, dma_command);       /* Kick on */
1972 
1973   if (chn == (gus_sampling_channels - 1))       /* Last channel */
1974     {
1975       /* Last (right or mono) channel data */
1976       active_device = GUS_DEV_PCM_DONE;
1977       if (!pcm_active && (pcm_qlen > 2 || count < pcm_bsize))
1978         {
1979           play_next_pcm_block ();
1980         }
1981     }
1982   else                          /* Left channel data. The right channel is
1983                                  * transferred after DMA interrupt */
1984     active_device = GUS_DEV_PCM_CONTINUE;
1985 
1986   RESTORE_INTR (flags);
1987 }
1988 
1989 static void
1990 gus_sampling_output_block (int dev, unsigned long buf, int total_count, int intrflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1991 {
1992   pcm_current_buf = buf;
1993   pcm_current_count = total_count;
1994   pcm_current_intrflag = intrflag;
1995   pcm_current_dev = dev;
1996   gus_transfer_output_block (dev, buf, total_count, intrflag, 0);
1997 }
1998 
1999 static void
2000 gus_sampling_start_input (int dev, unsigned long buf, int count, int intrflag)
     /* [previous][next][first][last][top][bottom][index][help] */
2001 {
2002   unsigned long   flags;
2003   unsigned char   mode;
2004 
2005   DISABLE_INTR (flags);
2006 
2007   DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
2008 
2009   mode = 0xa0;                  /* DMA IRQ enable, invert MSB */
2010 
2011   if (sound_dsp_dmachan[dev] > 3)
2012     mode |= 0x04;               /* 16 bit DMA channel */
2013   if (gus_sampling_channels > 1)
2014     mode |= 0x02;               /* Stereo */
2015   mode |= 0x01;                 /* DMA enable */
2016 
2017   gus_write8 (0x49, mode);
2018 
2019   RESTORE_INTR (flags);
2020 }
2021 
2022 static int
2023 gus_sampling_prepare_for_input (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2024 {
2025   unsigned int    rate;
2026 
2027   rate = (9878400 / (gus_sampling_speed + 2)) / 16;
2028 
2029   gus_write8 (0x48, rate & 0xff);       /* Set sampling frequency */
2030 
2031   if (gus_sampling_bits != 8)
2032     {
2033       printk ("GUS Error: 16 bit recording not supported\n");
2034       return RET_ERROR (EINVAL);
2035     }
2036 
2037   return 0;
2038 }
2039 
2040 static int
2041 gus_sampling_prepare_for_output (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2042 {
2043   int             i;
2044 
2045   long            mem_ptr, mem_size;
2046 
2047   mem_ptr = 0;
2048   mem_size = gus_mem_size / gus_sampling_channels;
2049 
2050   if (mem_size > (256 * 1024))
2051     mem_size = 256 * 1024;
2052 
2053   pcm_bsize = bsize / gus_sampling_channels;
2054   pcm_head = pcm_tail = pcm_qlen = 0;
2055 
2056   pcm_nblk = MAX_PCM_BUFFERS;
2057   if ((pcm_bsize * pcm_nblk) > mem_size)
2058     pcm_nblk = mem_size / pcm_bsize;
2059 
2060   for (i = 0; i < pcm_nblk; i++)
2061     pcm_datasize[i] = 0;
2062 
2063   pcm_banksize = pcm_nblk * pcm_bsize;
2064 
2065   if (gus_sampling_bits != 8 && pcm_banksize == (256 * 1024))
2066     pcm_nblk--;
2067 
2068   return 0;
2069 }
2070 
2071 static int
2072 gus_has_output_drained (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2073 {
2074   return !pcm_qlen;
2075 }
2076 
2077 static void
2078 gus_copy_from_user (int dev, char *localbuf, int localoffs,
     /* [previous][next][first][last][top][bottom][index][help] */
2079                     snd_rw_buf * userbuf, int useroffs, int len)
2080 {
2081   if (gus_sampling_channels == 1)
2082     {
2083       COPY_FROM_USER (&localbuf[localoffs], userbuf, useroffs, len);
2084     }
2085   else if (gus_sampling_bits == 8)
2086     {
2087       int             in_left = useroffs;
2088       int             in_right = useroffs + 1;
2089       char           *out_left, *out_right;
2090       int             i;
2091 
2092       len /= 2;
2093       localoffs /= 2;
2094       out_left = &localbuf[localoffs];
2095       out_right = out_left + pcm_bsize;
2096 
2097       for (i = 0; i < len; i++)
2098         {
2099           GET_BYTE_FROM_USER (*out_left++, userbuf, in_left);
2100           in_left += 2;
2101           GET_BYTE_FROM_USER (*out_right++, userbuf, in_right);
2102           in_right += 2;
2103         }
2104     }
2105   else
2106     {
2107       int             in_left = useroffs;
2108       int             in_right = useroffs + 1;
2109       short          *out_left, *out_right;
2110       int             i;
2111 
2112       len /= 4;
2113       localoffs /= 4;
2114 
2115       out_left = (short *) &localbuf[localoffs];
2116       out_right = out_left + (pcm_bsize / 2);
2117 
2118       for (i = 0; i < len; i++)
2119         {
2120           GET_SHORT_FROM_USER (*out_left++, (short *) userbuf, in_left);
2121           in_left += 2;
2122           GET_SHORT_FROM_USER (*out_right++, (short *) userbuf, in_right);
2123           in_right += 2;
2124         }
2125     }
2126 }
2127 
2128 static struct audio_operations gus_sampling_operations =
2129 {
2130   "Gravis UltraSound",
2131   gus_sampling_open,            /* */
2132   gus_sampling_close,           /* */
2133   gus_sampling_output_block,    /* */
2134   gus_sampling_start_input,     /* */
2135   gus_sampling_ioctl,           /* */
2136   gus_sampling_prepare_for_input,       /* */
2137   gus_sampling_prepare_for_output,      /* */
2138   gus_sampling_reset,           /* */
2139   gus_sampling_reset,           /* halt_xfer */
2140   gus_has_output_drained,
2141   gus_copy_from_user
2142 };
2143 
2144 static int
2145 guswave_patchmgr (int dev, struct patmgr_info *rec)
     /* [previous][next][first][last][top][bottom][index][help] */
2146 {
2147   int             i, n;
2148 
2149   switch (rec->command)
2150     {
2151     case PM_GET_DEVTYPE:
2152       rec->parm1 = PMTYPE_WAVE;
2153       return 0;
2154       break;
2155 
2156     case PM_GET_NRPGM:
2157       rec->parm1 = MAX_PATCH;
2158       return 0;
2159       break;
2160 
2161     case PM_GET_PGMMAP:
2162       rec->parm1 = MAX_PATCH;
2163 
2164       for (i = 0; i < MAX_PATCH; i++)
2165         {
2166           int             ptr = patch_table[i];
2167 
2168           rec->data.data8[i] = 0;
2169 
2170           while (ptr >= 0 && ptr < free_sample)
2171             {
2172               rec->data.data8[i]++;
2173               ptr = samples[ptr].key;   /* Follow link */
2174             }
2175         }
2176       return 0;
2177       break;
2178 
2179     case PM_GET_PGM_PATCHES:
2180       {
2181         int             ptr = patch_table[rec->parm1];
2182 
2183         n = 0;
2184 
2185         while (ptr >= 0 && ptr < free_sample)
2186           {
2187             rec->data.data32[n++] = ptr;
2188             ptr = samples[ptr].key;     /* Follow link */
2189           }
2190       }
2191       rec->parm1 = n;
2192       return 0;
2193       break;
2194 
2195     case PM_GET_PATCH:
2196       {
2197         int             ptr = rec->parm1;
2198         struct patch_info *pat;
2199 
2200         if (ptr < 0 || ptr >= free_sample)
2201           return RET_ERROR (EINVAL);
2202 
2203         memcpy (rec->data.data8, (char *) &samples[ptr],
2204                 sizeof (struct patch_info));
2205 
2206         pat = (struct patch_info *) rec->data.data8;
2207 
2208         pat->key = GUS_PATCH;   /* Restore patch type */
2209         rec->parm1 = sample_ptrs[ptr];  /* DRAM address */
2210         rec->parm2 = sizeof (struct patch_info);
2211       }
2212       return 0;
2213       break;
2214 
2215     case PM_SET_PATCH:
2216       {
2217         int             ptr = rec->parm1;
2218         struct patch_info *pat;
2219 
2220         if (ptr < 0 || ptr >= free_sample)
2221           return RET_ERROR (EINVAL);
2222 
2223         pat = (struct patch_info *) rec->data.data8;
2224 
2225         if (pat->len > samples[ptr].len)        /* Cannot expand sample */
2226           return RET_ERROR (EINVAL);
2227 
2228         pat->key = samples[ptr].key;    /* Ensure the link is correct */
2229 
2230         memcpy ((char *) &samples[ptr], rec->data.data8,
2231                 sizeof (struct patch_info));
2232 
2233         pat->key = GUS_PATCH;
2234       }
2235       return 0;
2236       break;
2237 
2238     case PM_READ_PATCH: /* Returns a block of wave data from the DRAM */
2239       {
2240         int             sample = rec->parm1;
2241         int             n;
2242         long            offs = rec->parm2;
2243         int             l = rec->parm3;
2244 
2245         if (sample < 0 || sample >= free_sample)
2246           return RET_ERROR (EINVAL);
2247 
2248         if (offs < 0 || offs >= samples[sample].len)
2249           return RET_ERROR (EINVAL);    /* Invalid offset */
2250 
2251         n = samples[sample].len - offs; /* Nr of bytes left */
2252 
2253         if (l > n)
2254           l = n;
2255 
2256         if (l > sizeof (rec->data.data8))
2257           l = sizeof (rec->data.data8);
2258 
2259         if (l <= 0)
2260           return RET_ERROR (EINVAL);    /* Was there a bug? */
2261 
2262         offs += sample_ptrs[sample];    /* Begin offsess + offset to DRAM */
2263 
2264         for (n = 0; n < l; n++)
2265           rec->data.data8[n] = gus_peek (offs++);
2266         rec->parm1 = n;         /* Nr of bytes copied */
2267       }
2268       return 0;
2269       break;
2270 
2271     case PM_WRITE_PATCH:        /* Writes a block of wave data to the DRAM */
2272       {
2273         int             sample = rec->parm1;
2274         int             n;
2275         long            offs = rec->parm2;
2276         int             l = rec->parm3;
2277 
2278         if (sample < 0 || sample >= free_sample)
2279           return RET_ERROR (EINVAL);
2280 
2281         if (offs < 0 || offs >= samples[sample].len)
2282           return RET_ERROR (EINVAL);    /* Invalid offset */
2283 
2284         n = samples[sample].len - offs; /* Nr of bytes left */
2285 
2286         if (l > n)
2287           l = n;
2288 
2289         if (l > sizeof (rec->data.data8))
2290           l = sizeof (rec->data.data8);
2291 
2292         if (l <= 0)
2293           return RET_ERROR (EINVAL);    /* Was there a bug? */
2294 
2295         offs += sample_ptrs[sample];    /* Begin offsess + offset to DRAM */
2296 
2297         for (n = 0; n < l; n++)
2298           gus_poke (offs++, rec->data.data8[n]);
2299         rec->parm1 = n;         /* Nr of bytes copied */
2300       }
2301       return 0;
2302       break;
2303 
2304     default:
2305       return RET_ERROR (EINVAL);
2306     }
2307 }
2308 
2309 static struct synth_operations guswave_operations =
2310 {
2311   &gus_info,
2312   SYNTH_TYPE_SAMPLE,
2313   SAMPLE_TYPE_GUS,
2314   guswave_open,
2315   guswave_close,
2316   guswave_ioctl,
2317   guswave_kill_note,
2318   guswave_start_note,
2319   guswave_set_instr,
2320   guswave_reset,
2321   guswave_hw_control,
2322   guswave_load_patch,
2323   guswave_aftertouch,
2324   guswave_controller,
2325   guswave_panning,
2326   guswave_patchmgr
2327 };
2328 
2329 long
2330 gus_wave_init (long mem_start, int irq, int dma)
     /* [previous][next][first][last][top][bottom][index][help] */
2331 {
2332   printk (" <Gravis UltraSound %dk>", gus_mem_size / 1024);
2333 
2334   if (irq < 0 || irq > 15)
2335     {
2336       printk ("ERROR! Invalid IRQ#%d. GUS Disabled", irq);
2337       return mem_start;
2338     }
2339 
2340   if (dma < 0 || dma > 7)
2341     {
2342       printk ("ERROR! Invalid DMA#%d. GUS Disabled", dma);
2343       return mem_start;
2344     }
2345 
2346   gus_irq = irq;
2347   gus_dma = dma;
2348 
2349   if (num_synths >= MAX_SYNTH_DEV)
2350     printk ("GUS Error: Too many synthesizers\n");
2351   else
2352     synth_devs[num_synths++] = &guswave_operations;
2353 
2354   reset_sample_memory ();
2355 
2356   gus_initialize ();
2357 
2358   if (num_dspdevs < MAX_DSP_DEV)
2359     {
2360       dsp_devs[gus_devnum = num_dspdevs++] = &gus_sampling_operations;
2361       sound_dsp_dmachan[gus_devnum] = dma;
2362       sound_buffcounts[gus_devnum] = 1;
2363       sound_buffsizes[gus_devnum] = DSP_BUFFSIZE;
2364       sound_dma_automode[gus_devnum] = 0;
2365     }
2366   else
2367     printk ("GUS: Too many PCM devices available\n");
2368 
2369   return mem_start;
2370 }
2371 
2372 static void
2373 do_loop_irq (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
2374 {
2375   unsigned char   tmp;
2376   int             mode, parm;
2377   unsigned long   flags;
2378 
2379   DISABLE_INTR (flags);
2380   gus_select_voice (voice);
2381 
2382   tmp = gus_read8 (0x00);
2383   tmp &= ~0x20;                 /* Disable wave IRQ for this_one voice */
2384   gus_write8 (0x00, tmp);
2385 
2386   mode = voices[voice].loop_irq_mode;
2387   voices[voice].loop_irq_mode = 0;
2388   parm = voices[voice].loop_irq_parm;
2389 
2390   switch (mode)
2391     {
2392 
2393     case LMODE_FINISH:          /* Final loop finished, shoot volume down */
2394 
2395       if ((gus_read16 (0x09) >> 4) < 100)       /* Get current volume */
2396         {
2397           gus_voice_off ();
2398           gus_rampoff ();
2399           gus_voice_init (voice);
2400           return;
2401         }
2402       gus_ramp_range (65, 4065);
2403       gus_ramp_rate (0, 63);    /* Fastest possible rate */
2404       gus_rampon (0x20 | 0x40); /* Ramp down, once, irq */
2405       voices[voice].volume_irq_mode = VMODE_HALT;
2406       break;
2407 
2408     case LMODE_PCM_STOP:
2409       pcm_active = 0;           /* Requires extensive processing */
2410     case LMODE_PCM:
2411       {
2412         int             orig_qlen = pcm_qlen;
2413 
2414         pcm_qlen--;
2415         pcm_head = (pcm_head + 1) % pcm_nblk;
2416         if (pcm_qlen)
2417           {
2418             play_next_pcm_block ();
2419           }
2420         else
2421           {                     /* Out of data. Just stop the voice */
2422             gus_voice_off ();
2423             gus_rampoff ();
2424             pcm_active = 0;
2425           }
2426 
2427         if (orig_qlen == pcm_nblk)
2428           {
2429             DMAbuf_outputintr (gus_devnum);
2430           }
2431       }
2432       break;
2433 
2434     default:;
2435     }
2436   RESTORE_INTR (flags);
2437 }
2438 
2439 static void
2440 do_volume_irq (int voice)
     /* [previous][next][first][last][top][bottom][index][help] */
2441 {
2442   unsigned char   tmp;
2443   int             mode, parm;
2444   unsigned long   flags;
2445 
2446   DISABLE_INTR (flags);
2447 
2448   gus_select_voice (voice);
2449 
2450   tmp = gus_read8 (0x0d);
2451   tmp &= ~0x20;                 /* Disable volume ramp IRQ */
2452   gus_write8 (0x0d, tmp);
2453 
2454   mode = voices[voice].volume_irq_mode;
2455   voices[voice].volume_irq_mode = 0;
2456   parm = voices[voice].volume_irq_parm;
2457 
2458   switch (mode)
2459     {
2460     case VMODE_HALT:            /* Decay phase finished */
2461       gus_voice_init (voice);
2462       break;
2463 
2464     case VMODE_ENVELOPE:
2465       gus_rampoff ();
2466       step_envelope (voice);
2467       break;
2468 
2469     default:;
2470     }
2471 
2472   RESTORE_INTR (flags);
2473 }
2474 
2475 void
2476 gus_voice_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2477 {
2478   unsigned long   wave_ignore = 0, volume_ignore = 0;
2479   unsigned long   voice_bit;
2480 
2481   unsigned char   src, voice;
2482 
2483   while (1)
2484     {
2485       src = gus_read8 (0x0f);   /* Get source info */
2486       voice = src & 0x1f;
2487       src &= 0xc0;
2488 
2489       if (src == (0x80 | 0x40))
2490         return;                 /* No interrupt */
2491 
2492       voice_bit = 1 << voice;
2493 
2494       if (!(src & 0x80))        /* Wave IRQ pending */
2495         if (!(wave_ignore & voice_bit) && voice < nr_voices)    /* Not done yet */
2496           {
2497             wave_ignore |= voice_bit;
2498             do_loop_irq (voice);
2499           }
2500 
2501       if (!(src & 0x40))        /* Volume IRQ pending */
2502         if (!(volume_ignore & voice_bit) && voice < nr_voices)  /* Not done yet */
2503           {
2504             volume_ignore |= voice_bit;
2505             do_volume_irq (voice);
2506           }
2507     }
2508 }
2509 
2510 void
2511 guswave_dma_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2512 {
2513   unsigned char   status;
2514 
2515   status = gus_look8 (0x41);    /* Get DMA IRQ Status */
2516   if (status & 0x40)            /* DMA Irq pending */
2517     switch (active_device)
2518       {
2519       case GUS_DEV_WAVE:
2520         if (SOMEONE_WAITING (dram_sleep_flag))
2521           WAKE_UP (dram_sleeper, dram_sleep_flag);
2522         break;
2523 
2524       case GUS_DEV_PCM_CONTINUE:
2525         gus_transfer_output_block (pcm_current_dev, pcm_current_buf,
2526                                    pcm_current_count,
2527                                    pcm_current_intrflag, 1);
2528         break;
2529 
2530       case GUS_DEV_PCM_DONE:
2531         if (pcm_qlen < pcm_nblk)
2532           {
2533             DMAbuf_outputintr (gus_devnum);
2534           }
2535         break;
2536 
2537       default:;
2538       }
2539 
2540   status = gus_look8 (0x49);    /* Get Sampling IRQ Status */
2541   if (status & 0x40)            /* Sampling Irq pending */
2542     {
2543       DMAbuf_inputintr (gus_devnum);
2544     }
2545 
2546 }
2547 
2548 #endif

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