home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / sound / opl3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  26.7 KB  |  1,226 lines

  1. /*
  2.  * sound/opl3.c
  3.  *
  4.  * A low level driver for Yamaha YM3812 and OPL-3 -chips
  5.  *
  6.  * Copyright by Hannu Savolainen 1993
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions are
  10.  * met: 1. Redistributions of source code must retain the above copyright
  11.  * notice, this list of conditions and the following disclaimer. 2.
  12.  * Redistributions in binary form must reproduce the above copyright notice,
  13.  * this list of conditions and the following disclaimer in the documentation
  14.  * and/or other materials provided with the distribution.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  *
  28.  */
  29.  
  30. /*
  31.  * Major improvements to the FM handling 30AUG92 by Rob Hooft,
  32.  */
  33. /*
  34.  * hooft@chem.ruu.nl
  35.  */
  36.  
  37. #include "sound_config.h"
  38.  
  39. #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
  40.  
  41. #include "opl3.h"
  42.  
  43. #define MAX_VOICE    18
  44. #define OFFS_4OP    11    /*
  45.                    * * * Definitions for the operators OP3 and
  46.                    * * OP4 * * begin here   */
  47.  
  48. static int      opl3_enabled = 0;
  49. static int      left_address = 0x388, right_address = 0x388, both_address = 0;
  50.  
  51. static int      nr_voices = 9;
  52. static int      logical_voices[MAX_VOICE] =
  53. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  54.  
  55. struct voice_info
  56.   {
  57.     unsigned char   keyon_byte;
  58.     long            bender;
  59.     long            bender_range;
  60.     unsigned long   orig_freq;
  61.     unsigned long   current_freq;
  62.     int             mode;
  63.   };
  64.  
  65. static struct voice_info voices[MAX_VOICE];
  66. static struct voice_alloc_info *voice_alloc;
  67. static struct channel_info *chn_info;
  68.  
  69. static struct sbi_instrument *instrmap;
  70. static struct sbi_instrument *active_instrument[MAX_VOICE] =
  71. {NULL};
  72.  
  73. static struct synth_info fm_info =
  74. {"OPL-2", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
  75.  
  76. static int      already_initialized = 0;
  77.  
  78. static int      opl3_ok = 0;
  79. static int      opl3_busy = 0;
  80. static int      fm_model = 0;    /*
  81.  
  82.  
  83.                  * *  * * 0=no fm, 1=mono, 2=SB Pro 1, 3=SB
  84.                  * Pro 2 * *    */
  85.  
  86. static int      store_instr (int instr_no, struct sbi_instrument *instr);
  87. static void     freq_to_fnum (int freq, int *block, int *fnum);
  88. static void     opl3_command (int io_addr, unsigned int addr, unsigned int val);
  89. static int      opl3_kill_note (int dev, int voice, int note, int velocity);
  90. static unsigned char connection_mask = 0x00;
  91.  
  92. void
  93. enable_opl3_mode (int left, int right, int both)
  94. {
  95.   if (opl3_enabled)
  96.     return;
  97.  
  98.   opl3_enabled = 1;
  99.   left_address = left;
  100.   right_address = right;
  101.   both_address = both;
  102.   fm_info.capabilities = SYNTH_CAP_OPL3;
  103.   fm_info.synth_subtype = FM_TYPE_OPL3;
  104. }
  105.  
  106. static void
  107. enter_4op_mode (void)
  108. {
  109.   int             i;
  110.   static int      voices_4op[MAX_VOICE] =
  111.   {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
  112.  
  113.   connection_mask = 0x3f;    /* Connect all possible 4 OP voices */
  114.   opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);
  115.  
  116.   for (i = 0; i < 3; i++)
  117.     physical_voices[i].voice_mode = 4;
  118.   for (i = 3; i < 6; i++)
  119.     physical_voices[i].voice_mode = 0;
  120.  
  121.   for (i = 9; i < 12; i++)
  122.     physical_voices[i].voice_mode = 4;
  123.   for (i = 12; i < 15; i++)
  124.     physical_voices[i].voice_mode = 0;
  125.  
  126.   for (i = 0; i < 12; i++)
  127.     logical_voices[i] = voices_4op[i];
  128.   voice_alloc->max_voice = nr_voices = 12;
  129. }
  130.  
  131. static int
  132. opl3_ioctl (int dev,
  133.         unsigned int cmd, unsigned int arg)
  134. {
  135.   switch (cmd)
  136.     {
  137.  
  138.     case SNDCTL_FM_LOAD_INSTR:
  139.       {
  140.     struct sbi_instrument ins;
  141.  
  142.     IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
  143.  
  144.     if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  145.       {
  146.         printk ("FM Error: Invalid instrument number %d\n", ins.channel);
  147.         return RET_ERROR (EINVAL);
  148.       }
  149.  
  150.     pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
  151.     return store_instr (ins.channel, &ins);
  152.       }
  153.       break;
  154.  
  155.     case SNDCTL_SYNTH_INFO:
  156.       fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
  157.  
  158.       IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
  159.       return 0;
  160.       break;
  161.  
  162.     case SNDCTL_SYNTH_MEMAVL:
  163.       return 0x7fffffff;
  164.       break;
  165.  
  166.     case SNDCTL_FM_4OP_ENABLE:
  167.       if (opl3_enabled)
  168.     enter_4op_mode ();
  169.       return 0;
  170.       break;
  171.  
  172.     default:
  173.       return RET_ERROR (EINVAL);
  174.     }
  175.  
  176. }
  177.  
  178. int
  179. opl3_detect (int ioaddr)
  180. {
  181.   /*
  182.    * This function returns 1 if the FM chicp is present at the given I/O port
  183.    * The detection algorithm plays with the timer built in the FM chip and
  184.    * looks for a change in the status register.
  185.    *
  186.    * Note! The timers of the FM chip are not connected to AdLib (and compatible)
  187.    * boards.
  188.    *
  189.    * Note2! The chip is initialized if detected.
  190.    */
  191.  
  192.   unsigned char   stat1, stat2;
  193.   int             i;
  194.  
  195.   if (already_initialized)
  196.     {
  197.       return 0;            /*
  198.                  * Do avoid duplicate initializations
  199.                  */
  200.     }
  201.  
  202.   if (opl3_enabled)
  203.     ioaddr = left_address;
  204.  
  205.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);    /*
  206.                                          * Reset
  207.                                          * timers
  208.                                          * 1
  209.                                          * and
  210.                                          * 2
  211.                                          */
  212.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);    /*
  213.                                  * Reset the
  214.                                  * IRQ of FM
  215.                                  * * chicp
  216.                                  */
  217.  
  218.   stat1 = INB (ioaddr);        /*
  219.                  * Read status register
  220.                  */
  221.  
  222.   if ((stat1 & 0xE0) != 0x00)
  223.     {
  224.       return 0;            /*
  225.                  * Should be 0x00
  226.                  */
  227.     }
  228.  
  229.   opl3_command (ioaddr, TIMER1_REGISTER, 0xff);    /*
  230.                              * Set timer 1 to
  231.                              * 0xff
  232.                              */
  233.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
  234.         TIMER2_MASK | TIMER1_START);    /*
  235.                          * Unmask and start timer 1
  236.                          */
  237.  
  238.   /*
  239.    * Now we have to delay at least 80 msec
  240.    */
  241.  
  242.   for (i = 0; i < 50; i++)
  243.     tenmicrosec ();        /*
  244.                  * To be sure
  245.                  */
  246.  
  247.   stat2 = INB (ioaddr);        /*
  248.                  * Read status after timers have expired
  249.                  */
  250.  
  251.   /*
  252.    * Stop the timers
  253.    */
  254.  
  255.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);    /*
  256.                                          * Reset
  257.                                          * timers
  258.                                          * 1
  259.                                          * and
  260.                                          * 2
  261.                                          */
  262.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);    /*
  263.                                  * Reset the
  264.                                  * IRQ of FM
  265.                                  * * chicp
  266.                                  */
  267.  
  268.   if ((stat2 & 0xE0) != 0xc0)
  269.     {
  270.       return 0;            /*
  271.                  * There is no YM3812
  272.                  */
  273.     }
  274.  
  275.   /*
  276.    * There is a FM chicp in this address. Now set some default values.
  277.    */
  278.  
  279.   for (i = 0; i < 9; i++)
  280.     opl3_command (ioaddr, KEYON_BLOCK + i, 0);    /*
  281.                          * Note off
  282.                          */
  283.  
  284.   opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
  285.   opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);    /*
  286.                              * Melodic mode.
  287.                              */
  288.  
  289.   return 1;
  290. }
  291.  
  292. static int
  293. opl3_kill_note (int dev, int voice, int note, int velocity)
  294. {
  295.   struct physical_voice_info *map;
  296.  
  297.   if (voice < 0 || voice >= nr_voices)
  298.     return 0;
  299.  
  300.   voice_alloc->map[voice] = 0;
  301.  
  302.   map = &physical_voices[logical_voices[voice]];
  303.  
  304.   DEB (printk ("Kill note %d\n", voice));
  305.  
  306.   if (map->voice_mode == 0)
  307.     return 0;
  308.  
  309.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
  310.  
  311.   voices[voice].keyon_byte = 0;
  312.   voices[voice].bender = 0;
  313.   voices[voice].bender_range = 200;    /*
  314.                      * 200 cents = 2 semitones
  315.                      */
  316.   voices[voice].orig_freq = 0;
  317.   voices[voice].current_freq = 0;
  318.   voices[voice].mode = 0;
  319.  
  320.   return 0;
  321. }
  322.  
  323. #define HIHAT            0
  324. #define CYMBAL            1
  325. #define TOMTOM            2
  326. #define SNARE            3
  327. #define BDRUM            4
  328. #define UNDEFINED        TOMTOM
  329. #define DEFAULT            TOMTOM
  330.  
  331. static int
  332. store_instr (int instr_no, struct sbi_instrument *instr)
  333. {
  334.  
  335.   if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
  336.     printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
  337.   memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
  338.  
  339.   return 0;
  340. }
  341.  
  342. static int
  343. opl3_set_instr (int dev, int voice, int instr_no)
  344. {
  345.   if (voice < 0 || voice >= nr_voices)
  346.     return 0;
  347.  
  348.   if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
  349.     return 0;
  350.  
  351.   active_instrument[voice] = &instrmap[instr_no];
  352.   return 0;
  353. }
  354.  
  355. /*
  356.  * The next table looks magical, but it certainly is not. Its values have
  357.  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  358.  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  359.  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
  360.  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
  361.  * volume -8 it was implemented as a table because it is only 128 bytes and
  362.  * it saves a lot of log() calculations. (RH)
  363.  */
  364. char            fm_volume_table[128] =
  365. {-64, -48, -40, -35, -32, -29, -27, -26,    /*
  366.                          * 0 -   7
  367.                          */
  368.  -24, -23, -21, -20, -19, -18, -18, -17,    /*
  369.                          * 8 -  15
  370.                          */
  371.  -16, -15, -15, -14, -13, -13, -12, -12,    /*
  372.                          * 16 -  23
  373.                          */
  374.  -11, -11, -10, -10, -10, -9, -9, -8,    /*
  375.                      * 24 -  31
  376.                      */
  377.  -8, -8, -7, -7, -7, -6, -6, -6,/*
  378.                      * 32 -  39
  379.                      */
  380.  -5, -5, -5, -5, -4, -4, -4, -4,/*
  381.                      * 40 -  47
  382.                      */
  383.  -3, -3, -3, -3, -2, -2, -2, -2,/*
  384.                      * 48 -  55
  385.                      */
  386.  -2, -1, -1, -1, -1, 0, 0, 0,    /*
  387.                  * 56 -  63
  388.                  */
  389.  0, 0, 0, 1, 1, 1, 1, 1,    /*
  390.                  * 64 -  71
  391.                  */
  392.  1, 2, 2, 2, 2, 2, 2, 2,    /*
  393.                  * 72 -  79
  394.                  */
  395.  3, 3, 3, 3, 3, 3, 3, 4,    /*
  396.                  * 80 -  87
  397.                  */
  398.  4, 4, 4, 4, 4, 4, 4, 5,    /*
  399.                  * 88 -  95
  400.                  */
  401.  5, 5, 5, 5, 5, 5, 5, 5,    /*
  402.                  * 96 - 103
  403.                  */
  404.  6, 6, 6, 6, 6, 6, 6, 6,    /*
  405.                  * 104 - 111
  406.                  */
  407.  6, 7, 7, 7, 7, 7, 7, 7,    /*
  408.                  * 112 - 119
  409.                  */
  410.  7, 7, 7, 8, 8, 8, 8, 8};    /*
  411.  
  412.  
  413.                  * *  * * 120 - 127   */
  414.  
  415. static void
  416. calc_vol (unsigned char *regbyte, int volume)
  417. {
  418.   int             level = (~*regbyte & 0x3f);
  419.  
  420.   if (level)
  421.     level += fm_volume_table[volume];
  422.  
  423.   if (level > 0x3f)
  424.     level = 0x3f;
  425.   if (level < 0)
  426.     level = 0;
  427.  
  428.   *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
  429. }
  430.  
  431. static void
  432. set_voice_volume (int voice, int volume)
  433. {
  434.   unsigned char   vol1, vol2, vol3, vol4;
  435.   struct sbi_instrument *instr;
  436.   struct physical_voice_info *map;
  437.  
  438.   if (voice < 0 || voice >= nr_voices)
  439.     return;
  440.  
  441.   map = &physical_voices[logical_voices[voice]];
  442.  
  443.   instr = active_instrument[voice];
  444.  
  445.   if (!instr)
  446.     instr = &instrmap[0];
  447.  
  448.   if (instr->channel < 0)
  449.     return;
  450.  
  451.   if (voices[voice].mode == 0)
  452.     return;
  453.  
  454.   if (voices[voice].mode == 2)
  455.     {                /*
  456.                  * 2 OP voice
  457.                  */
  458.  
  459.       vol1 = instr->operators[2];
  460.       vol2 = instr->operators[3];
  461.  
  462.       if ((instr->operators[10] & 0x01))
  463.     {            /*
  464.                  * Additive synthesis
  465.                  */
  466.       calc_vol (&vol1, volume);
  467.       calc_vol (&vol2, volume);
  468.     }
  469.       else
  470.     {            /*
  471.                  * FM synthesis
  472.                  */
  473.       calc_vol (&vol2, volume);
  474.     }
  475.  
  476.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);    /*
  477.                                      * Modulator
  478.                                      * volume
  479.                                      */
  480.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);    /*
  481.                                      * Carrier
  482.                                      * volume
  483.                                      */
  484.     }
  485.   else
  486.     {                /*
  487.                  * 4 OP voice
  488.                  */
  489.       int             connection;
  490.  
  491.       vol1 = instr->operators[2];
  492.       vol2 = instr->operators[3];
  493.       vol3 = instr->operators[OFFS_4OP + 2];
  494.       vol4 = instr->operators[OFFS_4OP + 3];
  495.  
  496.       /*
  497.        * The connection method for 4 OP voices is defined by the rightmost
  498.        * bits at the offsets 10 and 10+OFFS_4OP
  499.        */
  500.  
  501.       connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  502.  
  503.       switch (connection)
  504.     {
  505.     case 0:
  506.       calc_vol (&vol4, volume);    /*
  507.                      * Just the OP 4 is carrier
  508.                      */
  509.       break;
  510.  
  511.     case 1:
  512.       calc_vol (&vol2, volume);
  513.       calc_vol (&vol4, volume);
  514.       break;
  515.  
  516.     case 2:
  517.       calc_vol (&vol1, volume);
  518.       calc_vol (&vol4, volume);
  519.       break;
  520.  
  521.     case 3:
  522.       calc_vol (&vol1, volume);
  523.       calc_vol (&vol3, volume);
  524.       calc_vol (&vol4, volume);
  525.       break;
  526.  
  527.     default:        /*
  528.                  * Why ??
  529.                                                                                                                  */ ;
  530.     }
  531.  
  532.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
  533.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
  534.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
  535.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
  536.     }
  537. }
  538.  
  539. static int
  540. opl3_start_note (int dev, int voice, int note, int volume)
  541. {
  542.   unsigned char   data, fpc;
  543.   int             block, fnum, freq, voice_mode;
  544.   struct sbi_instrument *instr;
  545.   struct physical_voice_info *map;
  546.  
  547.   if (voice < 0 || voice >= nr_voices)
  548.     return 0;
  549.  
  550.   map = &physical_voices[logical_voices[voice]];
  551.  
  552.   if (map->voice_mode == 0)
  553.     return 0;
  554.  
  555.   if (note == 255)        /*
  556.                  * Just change the volume
  557.                  */
  558.     {
  559.       set_voice_volume (voice, volume);
  560.       return 0;
  561.     }
  562.  
  563.   /*
  564.    * Kill previous note before playing
  565.    */
  566.   opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);    /*
  567.                                  * Carrier
  568.                                  * volume to
  569.                                  * min
  570.                                  */
  571.   opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);    /*
  572.                                  * Modulator
  573.                                  * volume to
  574.                                  */
  575.  
  576.   if (map->voice_mode == 4)
  577.     {
  578.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
  579.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
  580.     }
  581.  
  582.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);    /*
  583.                                      * Note
  584.                                      * off
  585.                                      */
  586.  
  587.   instr = active_instrument[voice];
  588.  
  589.   if (!instr)
  590.     instr = &instrmap[0];
  591.  
  592.   if (instr->channel < 0)
  593.     {
  594.       printk (
  595.            "OPL3: Initializing voice %d with undefined instrument\n",
  596.            voice);
  597.       return 0;
  598.     }
  599.  
  600.   if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
  601.     return 0;            /*
  602.                  * Cannot play
  603.                  */
  604.  
  605.   voice_mode = map->voice_mode;
  606.  
  607.   if (voice_mode == 4)
  608.     {
  609.       int             voice_shift;
  610.  
  611.       voice_shift = (map->ioaddr == left_address) ? 0 : 3;
  612.       voice_shift += map->voice_num;
  613.  
  614.       if (instr->key != OPL3_PATCH)    /*
  615.                      * Just 2 OP patch
  616.                      */
  617.     {
  618.       voice_mode = 2;
  619.       connection_mask &= ~(1 << voice_shift);
  620.     }
  621.       else
  622.     {
  623.       connection_mask |= (1 << voice_shift);
  624.     }
  625.  
  626.       opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
  627.     }
  628.  
  629.   /*
  630.    * Set Sound Characteristics
  631.    */
  632.   opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
  633.   opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
  634.  
  635.   /*
  636.    * Set Attack/Decay
  637.    */
  638.   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
  639.   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
  640.  
  641.   /*
  642.    * Set Sustain/Release
  643.    */
  644.   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
  645.   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
  646.  
  647.   /*
  648.    * Set Wave Select
  649.    */
  650.   opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
  651.   opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
  652.  
  653.   /*
  654.    * Set Feedback/Connection
  655.    */
  656.   fpc = instr->operators[10];
  657.   if (!(fpc & 0x30))
  658.     fpc |= 0x30;        /*
  659.                  * Ensure that at least one chn is enabled
  660.                  */
  661.   opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
  662.         fpc);
  663.  
  664.   /*
  665.    * If the voice is a 4 OP one, initialize the operators 3 and 4 also
  666.    */
  667.  
  668.   if (voice_mode == 4)
  669.     {
  670.  
  671.       /*
  672.        * Set Sound Characteristics
  673.        */
  674.       opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
  675.       opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
  676.  
  677.       /*
  678.        * Set Attack/Decay
  679.        */
  680.       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
  681.       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
  682.  
  683.       /*
  684.        * Set Sustain/Release
  685.        */
  686.       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
  687.       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
  688.  
  689.       /*
  690.        * Set Wave Select
  691.        */
  692.       opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
  693.       opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
  694.  
  695.       /*
  696.        * Set Feedback/Connection
  697.        */
  698.       fpc = instr->operators[OFFS_4OP + 10];
  699.       if (!(fpc & 0x30))
  700.     fpc |= 0x30;        /*
  701.                  * Ensure that at least one chn is enabled
  702.                  */
  703.       opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
  704.     }
  705.  
  706.   voices[voice].mode = voice_mode;
  707.  
  708.   set_voice_volume (voice, volume);
  709.  
  710.   freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
  711.  
  712.   /*
  713.    * Since the pitch bender may have been set before playing the note, we
  714.    * have to calculate the bending now.
  715.    */
  716.  
  717.   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
  718.   voices[voice].current_freq = freq;
  719.  
  720.   freq_to_fnum (freq, &block, &fnum);
  721.  
  722.   /*
  723.    * Play note
  724.    */
  725.  
  726.   data = fnum & 0xff;        /*
  727.                  * Least significant bits of fnumber
  728.                  */
  729.   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
  730.  
  731.   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
  732.   voices[voice].keyon_byte = data;
  733.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  734.   if (voice_mode == 4)
  735.     opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
  736.  
  737.   return 0;
  738. }
  739.  
  740. static void
  741. freq_to_fnum (int freq, int *block, int *fnum)
  742. {
  743.   int             f, octave;
  744.  
  745.   /*
  746.    * Converts the note frequency to block and fnum values for the FM chip
  747.    */
  748.   /*
  749.    * First try to compute the block -value (octave) where the note belongs
  750.    */
  751.  
  752.   f = freq;
  753.  
  754.   octave = 5;
  755.  
  756.   if (f == 0)
  757.     octave = 0;
  758.   else if (f < 261)
  759.     {
  760.       while (f < 261)
  761.     {
  762.       octave--;
  763.       f <<= 1;
  764.     }
  765.     }
  766.   else if (f > 493)
  767.     {
  768.       while (f > 493)
  769.     {
  770.       octave++;
  771.       f >>= 1;
  772.     }
  773.     }
  774.  
  775.   if (octave > 7)
  776.     octave = 7;
  777.  
  778.   *fnum = freq * (1 << (20 - octave)) / 49716;
  779.   *block = octave;
  780. }
  781.  
  782. static void
  783. opl3_command (int io_addr, unsigned int addr, unsigned int val)
  784. {
  785.   int             i;
  786.  
  787.   /*
  788.    * The original 2-OP synth requires a quite long delay after writing to a
  789.    * register. The OPL-3 survives with just two INBs
  790.    */
  791.  
  792.   OUTB ((unsigned char) (addr & 0xff), io_addr);    /*
  793.                              * Select register
  794.                              *
  795.                              */
  796.  
  797.   if (!opl3_enabled)
  798.     tenmicrosec ();
  799.   else
  800.     for (i = 0; i < 2; i++)
  801.       INB (io_addr);
  802.  
  803.   OUTB ((unsigned char) (val & 0xff), io_addr + 1);    /*
  804.                              * Write to register
  805.                              *
  806.                              */
  807.  
  808.   if (!opl3_enabled)
  809.     {
  810.       tenmicrosec ();
  811.       tenmicrosec ();
  812.       tenmicrosec ();
  813.     }
  814.   else
  815.     for (i = 0; i < 2; i++)
  816.       INB (io_addr);
  817. }
  818.  
  819. static void
  820. opl3_reset (int dev)
  821. {
  822.   int             i;
  823.  
  824.   for (i = 0; i < nr_voices; i++)
  825.     {
  826.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  827.         KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);
  828.  
  829.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  830.         KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);
  831.  
  832.       if (physical_voices[logical_voices[i]].voice_mode == 4)
  833.     {
  834.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  835.         KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);
  836.  
  837.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  838.         KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);
  839.     }
  840.  
  841.       opl3_kill_note (dev, i, 0, 64);
  842.     }
  843.  
  844.   if (opl3_enabled)
  845.     {
  846.       voice_alloc->max_voice = nr_voices = 18;
  847.  
  848.       for (i = 0; i < 18; i++)
  849.     logical_voices[i] = i;
  850.  
  851.       for (i = 0; i < 18; i++)
  852.     physical_voices[i].voice_mode = 2;
  853.  
  854.     }
  855.  
  856. }
  857.  
  858. static int
  859. opl3_open (int dev, int mode)
  860. {
  861.   if (!opl3_ok)
  862.     return RET_ERROR (ENXIO);
  863.   if (opl3_busy)
  864.     return RET_ERROR (EBUSY);
  865.   opl3_busy = 1;
  866.  
  867.   connection_mask = 0x00;    /*
  868.                  * Just 2 OP voices
  869.                  */
  870.   if (opl3_enabled)
  871.     opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
  872.   return 0;
  873. }
  874.  
  875. static void
  876. opl3_close (int dev)
  877. {
  878.   opl3_busy = 0;
  879.   voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
  880.   fm_info.nr_drums = 0;
  881.   fm_info.perc_mode = 0;
  882.  
  883.   opl3_reset (dev);
  884. }
  885.  
  886. static void
  887. opl3_hw_control (int dev, unsigned char *event)
  888. {
  889. }
  890.  
  891. static int
  892. opl3_load_patch (int dev, int format, snd_rw_buf * addr,
  893.          int offs, int count, int pmgr_flag)
  894. {
  895.   struct sbi_instrument ins;
  896.  
  897.   if (count < sizeof (ins))
  898.     {
  899.       printk ("FM Error: Patch record too short\n");
  900.       return RET_ERROR (EINVAL);
  901.     }
  902.  
  903.   COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
  904.  
  905.   if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  906.     {
  907.       printk ("FM Error: Invalid instrument number %d\n", ins.channel);
  908.       return RET_ERROR (EINVAL);
  909.     }
  910.   ins.key = format;
  911.  
  912.   return store_instr (ins.channel, &ins);
  913. }
  914.  
  915. static void
  916. opl3_panning (int dev, int voice, int pressure)
  917. {
  918. }
  919.  
  920. static void
  921. opl3_volume_method (int dev, int mode)
  922. {
  923. }
  924.  
  925. #define SET_VIBRATO(cell) { \
  926.       tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
  927.       if (pressure > 110) \
  928.     tmp |= 0x40;        /* Vibrato on */ \
  929.       opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
  930.  
  931. static void
  932. opl3_aftertouch (int dev, int voice, int pressure)
  933. {
  934.   int             tmp;
  935.   struct sbi_instrument *instr;
  936.   struct physical_voice_info *map;
  937.  
  938.   if (voice < 0 || voice >= nr_voices)
  939.     return;
  940.  
  941.   map = &physical_voices[logical_voices[voice]];
  942.  
  943.   DEB (printk ("Aftertouch %d\n", voice));
  944.  
  945.   if (map->voice_mode == 0)
  946.     return;
  947.  
  948.   /*
  949.    * Adjust the amount of vibrato depending the pressure
  950.    */
  951.  
  952.   instr = active_instrument[voice];
  953.  
  954.   if (!instr)
  955.     instr = &instrmap[0];
  956.  
  957.   if (voices[voice].mode == 4)
  958.     {
  959.       int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  960.  
  961.       switch (connection)
  962.     {
  963.     case 0:
  964.       SET_VIBRATO (4);
  965.       break;
  966.  
  967.     case 1:
  968.       SET_VIBRATO (2);
  969.       SET_VIBRATO (4);
  970.       break;
  971.  
  972.     case 2:
  973.       SET_VIBRATO (1);
  974.       SET_VIBRATO (4);
  975.       break;
  976.  
  977.     case 3:
  978.       SET_VIBRATO (1);
  979.       SET_VIBRATO (3);
  980.       SET_VIBRATO (4);
  981.       break;
  982.  
  983.     }
  984.       /*
  985.        * Not implemented yet
  986.        */
  987.     }
  988.   else
  989.     {
  990.       SET_VIBRATO (1);
  991.  
  992.       if ((instr->operators[10] & 0x01))    /*
  993.                          * Additive synthesis
  994.                          */
  995.     SET_VIBRATO (2);
  996.     }
  997. }
  998.  
  999. #undef SET_VIBRATO
  1000.  
  1001. static void
  1002. bend_pitch (int dev, int voice, int value)
  1003. {
  1004.   unsigned char   data;
  1005.   int             block, fnum, freq;
  1006.   struct physical_voice_info *map;
  1007.  
  1008.   map = &physical_voices[logical_voices[voice]];
  1009.  
  1010.   if (map->voice_mode == 0)
  1011.     return;
  1012.  
  1013.   voices[voice].bender = value;
  1014.   if (!value)
  1015.     return;
  1016.   if (!(voices[voice].keyon_byte & 0x20))
  1017.     return;            /*
  1018.                  * Not keyed on
  1019.                  */
  1020.  
  1021.   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
  1022.   voices[voice].current_freq = freq;
  1023.  
  1024.   freq_to_fnum (freq, &block, &fnum);
  1025.  
  1026.   data = fnum & 0xff;        /*
  1027.                  * Least significant bits of fnumber
  1028.                  */
  1029.   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
  1030.  
  1031.   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);    /*
  1032.                                  * *
  1033.                                  * KEYON|OCTAVE|MS
  1034.                                  *
  1035.                                  * * bits * *
  1036.                                  * of * f-num
  1037.                                  *
  1038.                                  */
  1039.   voices[voice].keyon_byte = data;
  1040.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  1041. }
  1042.  
  1043. static void
  1044. opl3_controller (int dev, int voice, int ctrl_num, int value)
  1045. {
  1046.   if (voice < 0 || voice >= nr_voices)
  1047.     return;
  1048.  
  1049.   switch (ctrl_num)
  1050.     {
  1051.     case CTRL_PITCH_BENDER:
  1052.       bend_pitch (dev, voice, value);
  1053.       break;
  1054.  
  1055.     case CTRL_PITCH_BENDER_RANGE:
  1056.       voices[voice].bender_range = value;
  1057.       break;
  1058.     }
  1059. }
  1060.  
  1061. static int
  1062. opl3_patchmgr (int dev, struct patmgr_info *rec)
  1063. {
  1064.   return RET_ERROR (EINVAL);
  1065. }
  1066.  
  1067. static void
  1068. opl3_bender (int dev, int voice, int value)
  1069. {
  1070.   if (voice < 0 || voice >= nr_voices)
  1071.     return;
  1072.  
  1073.   bend_pitch (dev, voice, value);
  1074. }
  1075.  
  1076. static int
  1077. opl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc)
  1078. {
  1079.   int             i, p, avail_voices;
  1080.   struct sbi_instrument *instr;
  1081.   int             is4op;
  1082.   int             instr_no;
  1083.  
  1084.   if (chn < 0 || chn > 15)
  1085.     instr_no = 0;
  1086.   else
  1087.     instr_no = chn_info[chn].pgm_num;
  1088.  
  1089.   instr = &instrmap[instr_no];
  1090.   if (instr->channel < 0 ||    /* Instrument not loaded */
  1091.       nr_voices != 12)        /* Not in 4 OP mode */
  1092.     is4op = 0;
  1093.   else if (nr_voices == 12)    /* 4 OP mode */
  1094.     is4op = (instr->key == OPL3_PATCH);
  1095.   else
  1096.     is4op = 0;
  1097.  
  1098.   if (is4op)
  1099.     {
  1100.       p = 0;
  1101.       avail_voices = 6;
  1102.     }
  1103.   else
  1104.     {
  1105.       if (nr_voices == 12)    /* 4 OP mode. Use the '2 OP only' voices first */
  1106.     p = 6;
  1107.       else
  1108.     p = 0;
  1109.       avail_voices = nr_voices;
  1110.     }
  1111.  
  1112.   /*
  1113.  *    Now try to find a free voice
  1114.  */
  1115.  
  1116.   for (i = 0; i < avail_voices; i++)
  1117.     {
  1118.       if (alloc->map[p] == 0)
  1119.     {
  1120.       return p;
  1121.     }
  1122.       p = (p + 1) % nr_voices;
  1123.     }
  1124.  
  1125.   /*
  1126.  *    Insert some kind of priority mechanism here.
  1127.  */
  1128.  
  1129.   printk ("OPL3: Out of free voices\n");
  1130.   return 0;            /* All voices in use. Select the first one. */
  1131. }
  1132.  
  1133. static struct synth_operations opl3_operations =
  1134. {
  1135.   &fm_info,
  1136.   0,
  1137.   SYNTH_TYPE_FM,
  1138.   FM_TYPE_ADLIB,
  1139.   opl3_open,
  1140.   opl3_close,
  1141.   opl3_ioctl,
  1142.   opl3_kill_note,
  1143.   opl3_start_note,
  1144.   opl3_set_instr,
  1145.   opl3_reset,
  1146.   opl3_hw_control,
  1147.   opl3_load_patch,
  1148.   opl3_aftertouch,
  1149.   opl3_controller,
  1150.   opl3_panning,
  1151.   opl3_volume_method,
  1152.   opl3_patchmgr,
  1153.   opl3_bender,
  1154.   opl3_alloc_voice
  1155. };
  1156.  
  1157. long
  1158. opl3_init (long mem_start)
  1159. {
  1160.   int             i;
  1161.  
  1162.   PERMANENT_MALLOC (struct sbi_instrument *, instrmap,
  1163.             SBFM_MAXINSTR * sizeof (*instrmap), mem_start);
  1164.  
  1165.   if (num_synths >= MAX_SYNTH_DEV)
  1166.     printk ("OPL3 Error: Too many synthesizers\n");
  1167.   else
  1168.     {
  1169.       synth_devs[num_synths++] = &opl3_operations;
  1170.       voice_alloc = &opl3_operations.alloc;
  1171.       chn_info = &opl3_operations.chn_info[0];
  1172.     }
  1173.  
  1174.   fm_model = 0;
  1175.   opl3_ok = 1;
  1176.   if (opl3_enabled)
  1177.     {
  1178.       printk (" <Yamaha OPL-3 FM>");
  1179.       fm_model = 2;
  1180.       voice_alloc->max_voice = nr_voices = 18;
  1181.       fm_info.nr_drums = 0;
  1182.       fm_info.capabilities |= SYNTH_CAP_OPL3;
  1183. #ifndef SCO
  1184.       strcpy (fm_info.name, "Yamaha OPL-3");
  1185. #endif
  1186.  
  1187.       for (i = 0; i < 18; i++)
  1188.     if (physical_voices[i].ioaddr == USE_LEFT)
  1189.       physical_voices[i].ioaddr = left_address;
  1190.     else
  1191.       physical_voices[i].ioaddr = right_address;
  1192.  
  1193.  
  1194.       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);    /*
  1195.                                      * Enable
  1196.                                      * OPL-3
  1197.                                      * mode
  1198.                                      */
  1199.       opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);    /*
  1200.                                      * Select
  1201.                                      * all
  1202.                                      * 2-OP
  1203.                                      * *
  1204.                                      * voices
  1205.                                      */
  1206.     }
  1207.   else
  1208.     {
  1209.       printk (" <Yamaha 2-OP FM>");
  1210.       fm_model = 1;
  1211.       voice_alloc->max_voice = nr_voices = 9;
  1212.       fm_info.nr_drums = 0;
  1213.  
  1214.       for (i = 0; i < 18; i++)
  1215.     physical_voices[i].ioaddr = left_address;
  1216.     };
  1217.  
  1218.   already_initialized = 1;
  1219.   for (i = 0; i < SBFM_MAXINSTR; i++)
  1220.     instrmap[i].channel = -1;
  1221.  
  1222.   return mem_start;
  1223. }
  1224.  
  1225. #endif
  1226.