home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / sound / gus_wave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-21  |  70.4 KB  |  3,421 lines

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