The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/sound/isa/opl.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * A low level driver for Yamaha YM3812 and OPL-3 -chips
    3  * 
    4  * Copyright by Hannu Savolainen 1993
    5  * 
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions are
    8  * met: 1. Redistributions of source code must retain the above copyright
    9  * notice, this list of conditions and the following disclaimer. 2.
   10  * Redistributions in binary form must reproduce the above copyright notice,
   11  * this list of conditions and the following disclaimer in the documentation
   12  * and/or other materials provided with the distribution.
   13  * 
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
   15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  * 
   26  */
   27 
   28 /*
   29  * Major improvements to the FM handling 30AUG92 by Rob Hooft,
   30  */
   31 /*
   32  * hooft@chem.ruu.nl
   33  */
   34 /*
   35  *
   36  * Ported to the new Audio Driver by Luigi Rizzo:
   37  * (C) 1999 Seigo Tanimura
   38  *
   39  * This is the OPL2/3/4 chip driver for FreeBSD, based on the Luigi Sound Driver.
   40  * This handles io against /dev/midi, the midi {in, out}put event queues
   41  * and the event/message operation to the OPL chip.
   42  *
   43  * $FreeBSD: releng/5.1/sys/dev/sound/isa/opl.c 93818 2002-04-04 21:03:38Z jhb $
   44  *
   45  */
   46 
   47 #include <dev/sound/midi/midi.h>
   48 #include <dev/sound/chip.h>
   49 
   50 #include <isa/isavar.h>
   51 
   52 static devclass_t midi_devclass;
   53 
   54 #ifndef DDB
   55 #undef DDB
   56 #define DDB(x)
   57 #endif /* DDB */
   58 
   59 /*
   60  *      The OPL-3 mode is switched on by writing 0x01, to the offset 5
   61  *      of the right side.
   62  *
   63  *      Another special register at the right side is at offset 4. It contains
   64  *      a bit mask defining which voices are used as 4 OP voices.
   65  *
   66  *      The percussive mode is implemented in the left side only.
   67  *
   68  *      With the above exeptions the both sides can be operated independently.
   69  *      
   70  *      A 4 OP voice can be created by setting the corresponding
   71  *      bit at offset 4 of the right side.
   72  *
   73  *      For example setting the rightmost bit (0x01) changes the
   74  *      first voice on the right side to the 4 OP mode. The fourth
   75  *      voice is made inaccessible.
   76  *
   77  *      If a voice is set to the 2 OP mode, it works like 2 OP modes
   78  *      of the original YM3812 (AdLib). In addition the voice can 
   79  *      be connected the left, right or both stereo channels. It can
   80  *      even be left unconnected. This works with 4 OP voices also.
   81  *
   82  *      The stereo connection bits are located in the FEEDBACK_CONNECTION
   83  *      register of the voice (0xC0-0xC8). In 4 OP voices these bits are
   84  *      in the second half of the voice.
   85  */
   86 
   87 /*
   88  *      Register numbers for the global registers
   89  */
   90 
   91 #define TEST_REGISTER                           0x01
   92 #define   ENABLE_WAVE_SELECT            0x20
   93 
   94 #define TIMER1_REGISTER                         0x02
   95 #define TIMER2_REGISTER                         0x03
   96 #define TIMER_CONTROL_REGISTER                  0x04    /* Left side */
   97 #define   IRQ_RESET                     0x80
   98 #define   TIMER1_MASK                   0x40
   99 #define   TIMER2_MASK                   0x20
  100 #define   TIMER1_START                  0x01
  101 #define   TIMER2_START                  0x02
  102 
  103 #define CONNECTION_SELECT_REGISTER              0x04    /* Right side */
  104 #define   RIGHT_4OP_0                   0x01
  105 #define   RIGHT_4OP_1                   0x02
  106 #define   RIGHT_4OP_2                   0x04
  107 #define   LEFT_4OP_0                    0x08
  108 #define   LEFT_4OP_1                    0x10
  109 #define   LEFT_4OP_2                    0x20
  110 
  111 #define OPL3_MODE_REGISTER                      0x05    /* Right side */
  112 #define   OPL3_ENABLE                   0x01
  113 #define   OPL4_ENABLE                   0x02
  114 
  115 #define KBD_SPLIT_REGISTER                      0x08    /* Left side */
  116 #define   COMPOSITE_SINE_WAVE_MODE      0x80            /* Don't use with OPL-3? */
  117 #define   KEYBOARD_SPLIT                0x40
  118 
  119 #define PERCUSSION_REGISTER                     0xbd    /* Left side only */
  120 #define   TREMOLO_DEPTH                 0x80
  121 #define   VIBRATO_DEPTH                 0x40
  122 #define   PERCUSSION_ENABLE             0x20
  123 #define   BASSDRUM_ON                   0x10
  124 #define   SNAREDRUM_ON                  0x08
  125 #define   TOMTOM_ON                     0x04
  126 #define   CYMBAL_ON                     0x02
  127 #define   HIHAT_ON                      0x01
  128 
  129 /*
  130  *      Offsets to the register banks for operators. To get the
  131  *      register number just add the operator offset to the bank offset
  132  *
  133  *      AM/VIB/EG/KSR/Multiple (0x20 to 0x35)
  134  */
  135 #define AM_VIB                                  0x20
  136 #define   TREMOLO_ON                    0x80
  137 #define   VIBRATO_ON                    0x40
  138 #define   SUSTAIN_ON                    0x20
  139 #define   KSR                           0x10    /* Key scaling rate */
  140 #define   MULTIPLE_MASK         0x0f    /* Frequency multiplier */
  141 
  142 /*
  143  *      KSL/Total level (0x40 to 0x55)
  144  */
  145 #define KSL_LEVEL                               0x40
  146 #define   KSL_MASK                      0xc0    /* Envelope scaling bits */
  147 #define   TOTAL_LEVEL_MASK              0x3f    /* Strength (volume) of OP */
  148 
  149 /*
  150  *      Attack / Decay rate (0x60 to 0x75)
  151  */
  152 #define ATTACK_DECAY                            0x60
  153 #define   ATTACK_MASK                   0xf0
  154 #define   DECAY_MASK                    0x0f
  155 
  156 /*
  157  * Sustain level / Release rate (0x80 to 0x95)
  158  */
  159 #define SUSTAIN_RELEASE                         0x80
  160 #define   SUSTAIN_MASK                  0xf0
  161 #define   RELEASE_MASK                  0x0f
  162 
  163 /*
  164  * Wave select (0xE0 to 0xF5)
  165  */
  166 #define WAVE_SELECT                     0xe0
  167 
  168 /*
  169  *      Offsets to the register banks for voices. Just add to the
  170  *      voice number to get the register number.
  171  *
  172  *      F-Number low bits (0xA0 to 0xA8).
  173  */
  174 #define FNUM_LOW                                0xa0
  175 
  176 /*
  177  *      F-number high bits / Key on / Block (octave) (0xB0 to 0xB8)
  178  */
  179 #define KEYON_BLOCK                                     0xb0
  180 #define   KEYON_BIT                             0x20
  181 #define   BLOCKNUM_MASK                         0x1c
  182 #define   FNUM_HIGH_MASK                        0x03
  183 
  184 /*
  185  *      Feedback / Connection (0xc0 to 0xc8)
  186  *
  187  *      These registers have two new bits when the OPL-3 mode
  188  *      is selected. These bits controls connecting the voice
  189  *      to the stereo channels. For 4 OP voices this bit is
  190  *      defined in the second half of the voice (add 3 to the
  191  *      register offset).
  192  *
  193  *      For 4 OP voices the connection bit is used in the
  194  *      both halfs (gives 4 ways to connect the operators).
  195  */
  196 #define FEEDBACK_CONNECTION                             0xc0
  197 #define   FEEDBACK_MASK                         0x0e    /* Valid just for 1st OP of a voice */
  198 #define   CONNECTION_BIT                        0x01
  199 /*
  200  *      In the 4 OP mode there is four possible configurations how the
  201  *      operators can be connected together (in 2 OP modes there is just
  202  *      AM or FM). The 4 OP connection mode is defined by the rightmost
  203  *      bit of the FEEDBACK_CONNECTION (0xC0-0xC8) on the both halfs.
  204  *
  205  *      First half      Second half     Mode
  206  *
  207  *                                       +---+
  208  *                                       v   |
  209  *      0               0               >+-1-+--2--3--4-->
  210  *
  211  *
  212  *                                      
  213  *                                       +---+
  214  *                                       |   |
  215  *      0               1               >+-1-+--2-+
  216  *                                                |->
  217  *                                      >--3----4-+
  218  *                                      
  219  *                                       +---+
  220  *                                       |   |
  221  *      1               0               >+-1-+-----+
  222  *                                                 |->
  223  *                                      >--2--3--4-+
  224  *
  225  *                                       +---+
  226  *                                       |   |
  227  *      1               1               >+-1-+--+
  228  *                                              |
  229  *                                      >--2--3-+->
  230  *                                              |
  231  *                                      >--4----+
  232  */
  233 #define   STEREO_BITS                           0x30    /* OPL-3 only */
  234 #define     VOICE_TO_LEFT               0x10
  235 #define     VOICE_TO_RIGHT              0x20
  236 
  237 /*
  238  *      Definition table for the physical voices
  239  */
  240 
  241 struct physical_voice_info {
  242         unsigned char voice_num;
  243         unsigned char voice_mode; /* 0=unavailable, 2=2 OP, 4=4 OP */
  244         int ch; /* channel (left=USE_LEFT, right=USE_RIGHT) */
  245         unsigned char op[4]; /* Operator offsets */
  246 };
  247 
  248 /*
  249  *      There is 18 possible 2 OP voices
  250  *      (9 in the left and 9 in the right).
  251  *      The first OP is the modulator and 2nd is the carrier.
  252  *
  253  *      The first three voices in the both sides may be connected
  254  *      with another voice to a 4 OP voice. For example voice 0
  255  *      can be connected with voice 3. The operators of voice 3 are
  256  *      used as operators 3 and 4 of the new 4 OP voice.
  257  *      In this case the 2 OP voice number 0 is the 'first half' and
  258  *      voice 3 is the second.
  259  */
  260 
  261 #define USE_LEFT        0
  262 #define USE_RIGHT       1
  263 
  264 static struct physical_voice_info pv_map[18] =
  265 {
  266 /*       No Mode Side           OP1     OP2     OP3   OP4       */
  267 /*      ---------------------------------------------------     */
  268         { 0,  2, USE_LEFT,      {0x00,  0x03,   0x08, 0x0b}},
  269         { 1,  2, USE_LEFT,      {0x01,  0x04,   0x09, 0x0c}},
  270         { 2,  2, USE_LEFT,      {0x02,  0x05,   0x0a, 0x0d}},
  271 
  272         { 3,  2, USE_LEFT,      {0x08,  0x0b,   0x00, 0x00}},
  273         { 4,  2, USE_LEFT,      {0x09,  0x0c,   0x00, 0x00}},
  274         { 5,  2, USE_LEFT,      {0x0a,  0x0d,   0x00, 0x00}},
  275 
  276         { 6,  2, USE_LEFT,      {0x10,  0x13,   0x00, 0x00}}, /* Used by percussive voices */
  277         { 7,  2, USE_LEFT,      {0x11,  0x14,   0x00, 0x00}}, /* if the percussive mode */
  278         { 8,  2, USE_LEFT,      {0x12,  0x15,   0x00, 0x00}}, /* is selected */
  279 
  280         { 0,  2, USE_RIGHT,     {0x00,  0x03,   0x08, 0x0b}},
  281         { 1,  2, USE_RIGHT,     {0x01,  0x04,   0x09, 0x0c}},
  282         { 2,  2, USE_RIGHT,     {0x02,  0x05,   0x0a, 0x0d}},
  283 
  284         { 3,  2, USE_RIGHT,     {0x08,  0x0b,   0x00, 0x00}},
  285         { 4,  2, USE_RIGHT,     {0x09,  0x0c,   0x00, 0x00}},
  286         { 5,  2, USE_RIGHT,     {0x0a,  0x0d,   0x00, 0x00}},
  287 
  288         { 6,  2, USE_RIGHT,     {0x10,  0x13,   0x00, 0x00}},
  289         { 7,  2, USE_RIGHT,     {0x11,  0x14,   0x00, 0x00}},
  290         { 8,  2, USE_RIGHT,     {0x12,  0x15,   0x00, 0x00}}
  291 };
  292 
  293 /* These are the tuning parameters. */
  294 static unsigned short semitone_tuning[24] = 
  295 {
  296 /*   0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983, 
  297 /*   8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784, 
  298 /*  16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
  299 };
  300 
  301 static unsigned short cent_tuning[100] =
  302 {
  303 /*   0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041, 
  304 /*   8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087, 
  305 /*  16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134, 
  306 /*  24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181, 
  307 /*  32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228, 
  308 /*  40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275, 
  309 /*  48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323, 
  310 /*  56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371, 
  311 /*  64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419, 
  312 /*  72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467, 
  313 /*  80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515, 
  314 /*  88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564, 
  315 /*  96 */ 10570, 10576, 10582, 10589
  316 };
  317 
  318 /*
  319  * The next table looks magical, but it certainly is not. Its values have
  320  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  321  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  322  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
  323  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
  324  * volume -8 it was implemented as a table because it is only 128 bytes and
  325  * it saves a lot of log() calculations. (RH)
  326  */
  327 static char         opl_volumetable[128] =
  328 {
  329         -64, -48, -40, -35, -32, -29, -27, -26,
  330         -24, -23, -21, -20, -19, -18, -18, -17,
  331         -16, -15, -15, -14, -13, -13, -12, -12,
  332         -11, -11, -10, -10, -10, -9, -9, -8,
  333         -8, -8, -7, -7, -7, -6, -6, -6,
  334         -5, -5, -5, -5, -4, -4, -4, -4,
  335         -3, -3, -3, -3, -2, -2, -2, -2,
  336         -2, -1, -1, -1, -1, 0, 0, 0,
  337         0, 0, 0, 1, 1, 1, 1, 1,
  338         1, 2, 2, 2, 2, 2, 2, 2,
  339         3, 3, 3, 3, 3, 3, 3, 4,
  340         4, 4, 4, 4, 4, 4, 4, 5,
  341         5, 5, 5, 5, 5, 5, 5, 5,
  342         6, 6, 6, 6, 6, 6, 6, 6,
  343         6, 7, 7, 7, 7, 7, 7, 7,
  344         7, 7, 7, 8, 8, 8, 8, 8};
  345 
  346 #define MAX_VOICE 18
  347 #define OFFS_4OP 11
  348 #define SBFM_MAXINSTR 256
  349 
  350 /* These are the OPL Models. */
  351 #define MODEL_NONE 0
  352 #define MODEL_OPL2 2
  353 #define MODEL_OPL3 3
  354 #define MODEL_OPL4 4
  355 
  356 /* These are the OPL Voice modes. */
  357 #define VOICE_NONE 0
  358 #define VOICE_2OP 2
  359 #define VOICE_4OP 4
  360 
  361 /* PnP IDs */
  362 static struct isa_pnp_id opl_ids[] = {
  363         {0x01200001, "@H@2001 FM Synthesizer"}, /* @H@2001 */
  364         {0x01100001, "@H@1001 FM Synthesizer"}, /* @H@1001 */
  365 #if notdef
  366         /* TODO: write bridge drivers for these devices. */
  367         {0x0000630e, "CSC0000 FM Synthesizer"}, /* CSC0000 */
  368         {0x68187316, "ESS1868 FM Synthesizer"}, /* ESS1868 */
  369         {0x79187316, "ESS1879 FM Synthesizer"}, /* ESS1879 */
  370         {0x2100a865, "YMH0021 FM Synthesizer"}, /* YMH0021 */
  371         {0x80719304, "ADS7180 FM Synthesizer"}, /* ADS7180 */
  372         {0x0300561e, "GRV0003 FM Synthesizer"}, /* GRV0003 */
  373 #endif /* notdef */
  374 };
  375 
  376 /* These are the default io bases. */
  377 static int opl_defaultiobase[] = {
  378         0x388,
  379         0x380,
  380 };
  381 
  382 /* These are the per-voice information. */
  383 struct voice_info {
  384         u_char   keyon_byte;
  385         long            bender;
  386         long            bender_range;
  387         u_long   orig_freq;
  388         u_long   current_freq;
  389         int             volume;
  390         int             mode;
  391 };
  392 
  393 /* These are the synthesizer and the midi device information. */
  394 static struct synth_info opl_synthinfo = {
  395         "OPL FM Synthesizer",
  396         0,
  397         SYNTH_TYPE_FM,
  398         FM_TYPE_ADLIB,
  399         0,
  400         9,
  401         0,
  402         SBFM_MAXINSTR,
  403         0,
  404 };
  405 
  406 static struct midi_info opl_midiinfo = {
  407         "OPL FM Synthesizer",
  408         0,
  409         0,
  410         0,
  411 };
  412 
  413 /*
  414  * These functions goes into oplsynthdev_op_desc.
  415  */
  416 static mdsy_killnote_t opl_killnote;
  417 static mdsy_setinstr_t opl_setinstr;
  418 static mdsy_startnote_t opl_startnote;
  419 static mdsy_reset_t opl_reset;
  420 static mdsy_hwcontrol_t opl_hwcontrol;
  421 static mdsy_loadpatch_t opl_loadpatch;
  422 static mdsy_panning_t opl_panning;
  423 static mdsy_aftertouch_t opl_aftertouch;
  424 static mdsy_controller_t opl_controller;
  425 static mdsy_patchmgr_t opl_patchmgr;
  426 static mdsy_bender_t opl_bender;
  427 static mdsy_allocvoice_t opl_allocvoice;
  428 static mdsy_setupvoice_t opl_setupvoice;
  429 static mdsy_sendsysex_t opl_sendsysex;
  430 static mdsy_prefixcmd_t opl_prefixcmd;
  431 static mdsy_volumemethod_t opl_volumemethod;
  432 
  433 /*
  434  * This is the synthdev_info for an OPL3 chip.
  435  */
  436 static synthdev_info oplsynth_op_desc = {
  437         opl_killnote,
  438         opl_setinstr,
  439         opl_startnote,
  440         opl_reset,
  441         opl_hwcontrol,
  442         opl_loadpatch,
  443         opl_panning,
  444         opl_aftertouch,
  445         opl_controller,
  446         opl_patchmgr,
  447         opl_bender,
  448         opl_allocvoice,
  449         opl_setupvoice,
  450         opl_sendsysex,
  451         opl_prefixcmd,
  452         opl_volumemethod,
  453 };
  454 
  455 /* Here is the parameter structure per a device. */
  456 struct opl_softc {
  457         device_t dev; /* device information */
  458         mididev_info *devinfo; /* midi device information */
  459 
  460         struct mtx mtx; /* Mutex to protect the device. */
  461 
  462         struct resource *io; /* Base of io port */
  463         int io_rid; /* Io resource ID */
  464 
  465         int model; /* OPL model */
  466         struct synth_info synthinfo; /* Synthesizer information */
  467 
  468         struct sbi_instrument i_map[SBFM_MAXINSTR]; /* Instrument map */
  469         struct sbi_instrument *act_i[SBFM_MAXINSTR]; /* Active instruments */
  470         struct physical_voice_info pv_map[MAX_VOICE]; /* Physical voice map */
  471         int cmask; /* Connection mask */
  472         int lv_map[MAX_VOICE]; /* Level map */
  473         struct voice_info voc[MAX_VOICE]; /* Voice information */
  474 };
  475 
  476 typedef struct opl_softc *sc_p;
  477 
  478 /*
  479  * These functions goes into opl_op_desc to get called
  480  * from sound.c.
  481  */
  482 
  483 static int opl_probe(device_t dev);
  484 static int opl_probe1(sc_p scp);
  485 static int opl_attach(device_t dev);
  486 static int oplsbc_probe(device_t dev);
  487 static int oplsbc_attach(device_t dev);
  488 
  489 static d_open_t opl_open;
  490 static d_close_t opl_close;
  491 static d_ioctl_t opl_ioctl;
  492 static midi_callback_t opl_callback;
  493 
  494 /* These go to snddev_info. */
  495 static mdsy_readraw_t opl_readraw;
  496 static mdsy_writeraw_t opl_writeraw;
  497 
  498 /* These functions are local. */
  499 static void opl_command(sc_p scp, int ch, int addr, u_int val);
  500 static int opl_status(sc_p scp);
  501 static void opl_enter4opmode(sc_p scp);
  502 static void opl_storeinstr(sc_p scp, int instr_no, struct sbi_instrument *instr);
  503 static void opl_calcvol(u_char *regbyte, int volume, int main_vol);
  504 static void opl_setvoicevolume(sc_p scp, int voice, int volume, int main_vol);
  505 static void opl_freqtofnum(int freq, int *block, int *fnum);
  506 static int opl_bendpitch(sc_p scp, int voice, int val);
  507 static int opl_notetofreq(int note_num);
  508 static u_long opl_computefinetune(u_long base_freq, int bend, int range);
  509 static int opl_allocres(sc_p scp, device_t dev);
  510 static void opl_releaseres(sc_p scp, device_t dev);
  511 
  512 /*
  513  * This is the device descriptor for the midi device.
  514  */
  515 static mididev_info opl_op_desc = {
  516         "OPL FM Synthesizer",
  517 
  518         SNDCARD_OPL,
  519 
  520         opl_open,
  521         opl_close,
  522         opl_ioctl,
  523 
  524         opl_callback,
  525 
  526         MIDI_BUFFSIZE, /* Queue Length */
  527 
  528         0, /* XXX This is not an *audio* device! */
  529 };
  530 
  531 /*
  532  * Here are the main functions to interact to the user process.
  533  */
  534 
  535 static int
  536 opl_probe(device_t dev)
  537 {
  538         sc_p scp;
  539         int unit, i;
  540 
  541         /* Check isapnp ids */
  542         if (isa_get_logicalid(dev) != 0)
  543                 return (ISA_PNP_PROBE(device_get_parent(dev), dev, opl_ids));
  544 
  545         scp = device_get_softc(dev);
  546         unit = device_get_unit(dev);
  547 
  548         device_set_desc(dev, opl_op_desc.name);
  549         bzero(scp, sizeof(*scp));
  550 
  551         MIDI_DEBUG(printf("opl%d: probing.\n", unit));
  552 
  553         scp->io_rid = 0;
  554         scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, 0, ~0, 4, RF_ACTIVE);
  555         if (opl_allocres(scp, dev)) {
  556                 /* We try the defaults in opl_defaultiobase. */
  557                 MIDI_DEBUG(printf("opl%d: port is omitted, trying the defaults.\n", unit));
  558                 for (i = 0 ; i < sizeof(opl_defaultiobase) / sizeof(*opl_defaultiobase) ; i++) {
  559                         scp->io_rid = 0;
  560                         scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, opl_defaultiobase[i], opl_defaultiobase[i] + 1, 4, RF_ACTIVE);
  561                         if (scp->io != NULL) {
  562                                 if (opl_probe1(scp))
  563                                         opl_releaseres(scp, dev);
  564                                 else
  565                                         break;
  566                         }
  567                 }
  568                 if (scp->io == NULL)
  569                         return (ENXIO);
  570         } else if(opl_probe1(scp)) {
  571                 opl_releaseres(scp, dev);
  572                 return (ENXIO);
  573         }
  574 
  575         /* We now have some kind of OPL. */
  576 
  577         MIDI_DEBUG(printf("opl%d: probed.\n", unit));
  578 
  579         return (0);
  580 }
  581 
  582 /* We do probe in this function. */
  583 static int
  584 opl_probe1(sc_p scp)
  585 {
  586         u_char stat1, stat2;
  587 
  588         /* Reset the timers and the interrupt. */
  589         opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
  590         opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, IRQ_RESET);
  591 
  592         /* Read the status. */
  593         stat1 = opl_status(scp);
  594         if ((stat1 & 0xe0) != 0)
  595                 return (1);
  596 
  597         /* Try firing the timer1. */
  598         opl_command(scp, USE_LEFT, TIMER1_REGISTER, 0xff); /* Set the timer value. */
  599         opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_START | TIMER2_MASK); /* Start the timer. */
  600         DELAY(150); /* Wait for the timer. */
  601 
  602         /* Read the status. */
  603         stat2 = opl_status(scp);
  604 
  605         /* Reset the timers and the interrupt. */
  606         opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
  607         opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, IRQ_RESET);
  608 
  609         if ((stat2 & 0xe0) != 0xc0)
  610                 return (1);
  611 
  612         return (0);
  613 }
  614 
  615 static int
  616 oplsbc_probe(device_t dev)
  617 {
  618         char *s;
  619         sc_p scp;
  620         struct sndcard_func *func;
  621 
  622         /* The parent device has already been probed. */
  623 
  624         func = device_get_ivars(dev);
  625         if (func == NULL || func->func != SCF_SYNTH)
  626                 return (ENXIO);
  627 
  628         s = "SB OPL FM Synthesizer";
  629 
  630         scp = device_get_softc(dev);
  631         bzero(scp, sizeof(*scp));
  632         scp->io_rid = 2;
  633         device_set_desc(dev, s);
  634         return (0);
  635 }
  636 
  637 static int
  638 opl_attach(device_t dev)
  639 {
  640         sc_p scp;
  641         mididev_info *devinfo;
  642         int i, opl4_io, opl4_id;
  643         struct resource *opl4;
  644         u_char signature, tmp;
  645 
  646         scp = device_get_softc(dev);
  647 
  648         MIDI_DEBUG(printf("opl: attaching.\n"));
  649 
  650         /* Fill the softc for this unit. */
  651         scp->dev = dev;
  652 
  653         /* Allocate other resources. */
  654         if (opl_allocres(scp, dev)) {
  655                 opl_releaseres(scp, dev);
  656                 return (ENXIO);
  657         }
  658 
  659         /* Detect the OPL type. */
  660         signature = opl_status(scp);
  661         if (signature == 0x06)
  662                 scp->model = MODEL_OPL2;
  663         else {
  664                 /* OPL3 or later, might be OPL4. */
  665 
  666                 /* Enable OPL3 and OPL4. */
  667                 opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, 0);
  668                 opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
  669 
  670                 tmp = opl_status(scp);
  671                 if (tmp != 0x02)
  672                         scp->model = MODEL_OPL3;
  673 #if notdef
  674                 else {
  675 #endif /* notdef */
  676                         /* Alloc OPL4 ID register. */
  677                         opl4_id = 2;
  678                         opl4_io = rman_get_start(scp->io) - 8;
  679                         opl4 = bus_alloc_resource(dev, SYS_RES_IOPORT, &opl4_id, opl4_io, opl4_io + 1, 2, RF_ACTIVE);
  680                         if (opl4 != NULL) {
  681                                 /* Select OPL4 ID register. */
  682                                 bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 0, 0x02);
  683                                 DELAY(10);
  684                                 tmp = bus_space_read_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 1);
  685                                 DELAY(10);
  686 
  687                                 if (tmp != 0x20)
  688                                         scp->model = MODEL_OPL3;
  689                                 else {
  690                                         scp->model = MODEL_OPL4;
  691 
  692                                         /* Select back OPL4 FM mixer control. */
  693                                         bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 0, 0xf8);
  694                                         DELAY(10);
  695                                         bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 1, 0x1b);
  696                                         DELAY(10);
  697                                 }
  698                                 bus_release_resource(dev, SYS_RES_IOPORT, opl4_id, opl4);
  699                         }
  700 #if notdef
  701                 }
  702 #endif /* notdef */
  703                 opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, 0);
  704         }
  705 
  706         /* Kill any previous notes. */
  707         for (i = 0 ; i < 9 ; i++)
  708                 opl_command(scp, USE_RIGHT, KEYON_BLOCK + i, 0);
  709 
  710         /* Select melodic mode. */
  711         opl_command(scp, USE_LEFT, TEST_REGISTER, ENABLE_WAVE_SELECT);
  712         opl_command(scp, USE_LEFT, PERCUSSION_REGISTER, 0);
  713 
  714         for (i = 0 ; i < SBFM_MAXINSTR ; i++)
  715                 scp->i_map[i].channel = -1;
  716 
  717         /* Fill the softc. */
  718         bcopy(&opl_synthinfo, &scp->synthinfo, sizeof(opl_synthinfo));
  719         snprintf(scp->synthinfo.name, 64, "Yamaha OPL%d FM", scp->model);
  720         mtx_init(&scp->mtx, "oplmid", NULL, MTX_DEF);
  721         bcopy(pv_map, scp->pv_map, sizeof(pv_map));
  722         if (scp->model < MODEL_OPL3) { /* OPL2. */
  723                 scp->synthinfo.nr_voices = 9;
  724                 scp->synthinfo.nr_drums = 0;
  725                 for (i = 0 ; i < MAX_VOICE ; i++)
  726                         scp->pv_map[i].ch = USE_LEFT;
  727         } else { /* OPL3 or later. */
  728                 scp->synthinfo.capabilities |= SYNTH_CAP_OPL3;
  729                 scp->synthinfo.nr_voices = 18;
  730                 scp->synthinfo.nr_drums = 0;
  731 #if notdef
  732                 for (i = 0 ; i < MAX_VOICE ; i++) {
  733                         if (scp->pv_map[i].ch == USE_LEFT)
  734                                 scp->pv_map[i].ch = USE_LEFT;
  735                         else
  736                                 scp->pv_map[i].ch = USE_RIGHT;
  737                 }
  738 #endif /* notdef */
  739                 opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, OPL3_ENABLE);
  740                 opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, 0);
  741         }
  742 
  743         scp->devinfo = devinfo = create_mididev_info_unit(MDT_SYNTH, &opl_op_desc, &oplsynth_op_desc);
  744 
  745         /* Fill the midi info. */
  746         devinfo->synth.readraw = opl_readraw;
  747         devinfo->synth.writeraw = opl_writeraw;
  748         devinfo->synth.alloc.max_voice = scp->synthinfo.nr_voices;
  749         strcpy(devinfo->name, scp->synthinfo.name);
  750         snprintf(devinfo->midistat, sizeof(devinfo->midistat), "at 0x%x", (u_int)rman_get_start(scp->io));
  751 
  752         midiinit(devinfo, dev);
  753 
  754         MIDI_DEBUG(printf("opl: attached.\n"));
  755         MIDI_DEBUG(printf("opl: the chip is OPL%d.\n", scp->model));
  756 
  757         return (0);
  758 }
  759 
  760 static int
  761 oplsbc_attach(device_t dev)
  762 {
  763         return (opl_attach(dev));
  764 }
  765 
  766 static int
  767 opl_open(dev_t i_dev, int flags, int mode, struct thread *td)
  768 {
  769         sc_p scp;
  770         mididev_info *devinfo;
  771         int unit, i;
  772 
  773         unit = MIDIUNIT(i_dev);
  774 
  775         MIDI_DEBUG(printf("opl%d: opening.\n", unit));
  776 
  777         devinfo = get_mididev_info(i_dev, &unit);
  778         if (devinfo == NULL) {
  779                 MIDI_DEBUG(printf("opl_open: unit %d is not configured.\n", unit));
  780                 return (ENXIO);
  781         }
  782         scp = devinfo->softc;
  783 
  784         mtx_lock(&devinfo->synth.vc_mtx);
  785         if (scp->model < MODEL_OPL3)
  786                 devinfo->synth.alloc.max_voice = 9;
  787         else
  788                 devinfo->synth.alloc.max_voice = 18;
  789         devinfo->synth.alloc.timestamp = 0;
  790         for (i = 0 ; i < MAX_VOICE ; i++) {
  791                 devinfo->synth.alloc.map[i] = 0;
  792                 devinfo->synth.alloc.alloc_times[i] = 0;
  793         }
  794         mtx_unlock(&devinfo->synth.vc_mtx);
  795         scp->cmask = 0; /* We are in 2 OP mode initially. */
  796         if (scp->model >= MODEL_OPL3) {
  797                 mtx_lock(&scp->mtx);
  798                 opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
  799                 mtx_unlock(&scp->mtx);
  800         }
  801 
  802         MIDI_DEBUG(printf("opl%d: opened.\n", unit));
  803 
  804         return (0);
  805 }
  806 
  807 static int
  808 opl_close(dev_t i_dev, int flags, int mode, struct thread *td)
  809 {
  810         sc_p scp;
  811         mididev_info *devinfo;
  812         int unit;
  813 
  814         unit = MIDIUNIT(i_dev);
  815 
  816         MIDI_DEBUG(printf("opl%d: closing.\n", unit));
  817 
  818         devinfo = get_mididev_info(i_dev, &unit);
  819         if (devinfo == NULL) {
  820                 MIDI_DEBUG(printf("opl_close: unit %d is not configured.\n", unit));
  821                 return (ENXIO);
  822         }
  823         scp = devinfo->softc;
  824 
  825         mtx_lock(&devinfo->synth.vc_mtx);
  826         if (scp->model < MODEL_OPL3)
  827                 devinfo->synth.alloc.max_voice = 9;
  828         else
  829                 devinfo->synth.alloc.max_voice = 18;
  830         mtx_unlock(&devinfo->synth.vc_mtx);
  831 
  832         /* Stop the OPL. */
  833         opl_reset(scp->devinfo);
  834 
  835         MIDI_DEBUG(printf("opl%d: closed.\n", unit));
  836 
  837         return (0);
  838 }
  839 
  840 static int
  841 opl_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
  842 {
  843         sc_p scp;
  844         mididev_info *devinfo;
  845         int unit;
  846         struct synth_info *synthinfo;
  847         struct midi_info *midiinfo;
  848         struct sbi_instrument *ins;
  849 
  850         unit = MIDIUNIT(i_dev);
  851 
  852         MIDI_DEBUG(printf("opl_ioctl: unit %d, cmd %s.\n", unit, midi_cmdname(cmd, cmdtab_midiioctl)));
  853 
  854         devinfo = get_mididev_info(i_dev, &unit);
  855         if (devinfo == NULL) {
  856                 MIDI_DEBUG(printf("opl_ioctl: unit %d is not configured.\n", unit));
  857                 return (ENXIO);
  858         }
  859         scp = devinfo->softc;
  860 
  861         switch (cmd) {
  862         case SNDCTL_SYNTH_INFO:
  863                 synthinfo = (struct synth_info *)arg;
  864                 if (synthinfo->device != unit)
  865                         return (ENXIO);
  866                 bcopy(&scp->synthinfo, synthinfo, sizeof(scp->synthinfo));
  867                 synthinfo->device = unit;
  868                 synthinfo->nr_voices = devinfo->synth.alloc.max_voice;
  869                 if (synthinfo->nr_voices == 12)
  870                         synthinfo->nr_voices = 6;
  871                 return (0);
  872                 break;
  873         case SNDCTL_MIDI_INFO:
  874                 midiinfo = (struct midi_info *)arg;
  875                 if (midiinfo->device != unit)
  876                         return (ENXIO);
  877                 bcopy(&opl_midiinfo, midiinfo, sizeof(opl_midiinfo));
  878                 strcpy(midiinfo->name, scp->synthinfo.name);
  879                 midiinfo->device = unit;
  880                 return (0);
  881                 break;
  882         case SNDCTL_FM_LOAD_INSTR:
  883                 ins = (struct sbi_instrument *)arg;
  884                 if (ins->channel < 0 || ins->channel >= SBFM_MAXINSTR) {
  885                         printf("opl_ioctl: Instrument number %d is not valid.\n", ins->channel);
  886                         return (EINVAL);
  887                 }
  888 #if notyet
  889                 pmgr_inform(scp, PM_E_PATCH_LOADED, inc->channel, 0, 0, 0);
  890 #endif /* notyet */
  891                 opl_storeinstr(scp, ins->channel, ins);
  892                 return (0);
  893                 break;
  894         case SNDCTL_SYNTH_MEMAVL:
  895                 *(int *)arg = 0x7fffffff;
  896                 return (0);
  897                 break;
  898         case SNDCTL_FM_4OP_ENABLE:
  899                 if (scp->model >= MODEL_OPL3)
  900                         opl_enter4opmode(scp);
  901                 return (0);
  902                 break;
  903         default:
  904                 return (ENOSYS);
  905         }
  906         /* NOTREACHED */
  907         return (EINVAL);
  908 }
  909 
  910 static int
  911 opl_callback(void *d, int reason)
  912 {
  913         int unit;
  914         sc_p scp;
  915         mididev_info *devinfo;
  916 
  917         devinfo = (mididev_info *)d;
  918 
  919         mtx_assert(&devinfo->flagqueue_mtx, MA_OWNED);
  920 
  921         if (devinfo == NULL) {
  922                 MIDI_DEBUG(printf("opl_callback: device not configured.\n"));
  923                 return (ENXIO);
  924         }
  925 
  926         unit = devinfo->unit;
  927         scp = devinfo->softc;
  928 
  929         MIDI_DEBUG(printf("opl%d: callback, reason 0x%x.\n", unit, reason));
  930 
  931         switch (reason & MIDI_CB_REASON_MASK) {
  932         case MIDI_CB_START:
  933                 if ((reason & MIDI_CB_RD) != 0 && (devinfo->flags & MIDI_F_READING) == 0)
  934                         /* Begin recording. */
  935                         devinfo->flags |= MIDI_F_READING;
  936                 if ((reason & MIDI_CB_WR) != 0 && (devinfo->flags & MIDI_F_WRITING) == 0)
  937                         /* Start playing. */
  938                         devinfo->flags |= MIDI_F_WRITING;
  939                 break;
  940         case MIDI_CB_STOP:
  941         case MIDI_CB_ABORT:
  942                 if ((reason & MIDI_CB_RD) != 0 && (devinfo->flags & MIDI_F_READING) != 0)
  943                         /* Stop recording. */
  944                         devinfo->flags &= ~MIDI_F_READING;
  945                 if ((reason & MIDI_CB_WR) != 0 && (devinfo->flags & MIDI_F_WRITING) != 0)
  946                         /* Stop Playing. */
  947                         devinfo->flags &= ~MIDI_F_WRITING;
  948                 break;
  949         }
  950 
  951         return (0);
  952 }
  953 
  954 static int
  955 opl_readraw(mididev_info *md, u_char *buf, int len, int *lenr, int nonblock)
  956 {
  957         sc_p scp;
  958         int unit;
  959 
  960         if (md == NULL)
  961                 return (ENXIO);
  962         if (lenr == NULL)
  963                 return (EINVAL);
  964 
  965         unit = md->unit;
  966         scp = md->softc;
  967         if ((md->fflags & FREAD) == 0) {
  968                 MIDI_DEBUG(printf("opl_readraw: unit %d is not for reading.\n", unit));
  969                 return (EIO);
  970         }
  971 
  972         /* NOP. */
  973         *lenr = 0;
  974 
  975         return (0);
  976 }
  977 
  978 static int
  979 opl_writeraw(mididev_info *md, u_char *buf, int len, int *lenw, int nonblock)
  980 {
  981         sc_p scp;
  982         int unit;
  983 
  984         if (md == NULL)
  985                 return (ENXIO);
  986         if (lenw == NULL)
  987                 return (EINVAL);
  988 
  989         unit = md->unit;
  990         scp = md->softc;
  991         if ((md->fflags & FWRITE) == 0) {
  992                 MIDI_DEBUG(printf("opl_writeraw: unit %d is not for writing.\n", unit));
  993                 return (EIO);
  994         }
  995 
  996         /* NOP. */
  997         *lenw = 0;
  998 
  999         return (0);
 1000 }
 1001 
 1002 /* The functions below here are the synthesizer interfaces. */
 1003 
 1004 static int
 1005 opl_killnote(mididev_info *md, int voice, int note, int vel)
 1006 {
 1007         int unit;
 1008         sc_p scp;
 1009         struct physical_voice_info *map;
 1010 
 1011         scp = md->softc;
 1012         unit = md->unit;
 1013 
 1014         MIDI_DEBUG(printf("opl%d: killing a note, voice %d, note %d, vel %d.\n", unit, voice, note, vel));
 1015 
 1016         if (voice < 0 || voice >= md->synth.alloc.max_voice)
 1017                 return (0);
 1018 
 1019         mtx_lock(&md->synth.vc_mtx);
 1020 
 1021         md->synth.alloc.map[voice] = 0;
 1022         mtx_lock(&scp->mtx);
 1023         map = &scp->pv_map[scp->lv_map[voice]];
 1024 
 1025         if (map->voice_mode != VOICE_NONE) {
 1026                 opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte & ~0x20);
 1027 
 1028                 scp->voc[voice].keyon_byte = 0;
 1029                 scp->voc[voice].bender = 0;
 1030                 scp->voc[voice].volume = 64;
 1031                 scp->voc[voice].bender_range = 200;
 1032                 scp->voc[voice].orig_freq = 0;
 1033                 scp->voc[voice].current_freq = 0;
 1034                 scp->voc[voice].mode = 0;
 1035         }
 1036 
 1037         mtx_unlock(&scp->mtx);
 1038         mtx_unlock(&md->synth.vc_mtx);
 1039 
 1040         return (0);
 1041 }
 1042 
 1043 static int
 1044 opl_setinstr(mididev_info *md, int voice, int instr_no)
 1045 {
 1046         int unit;
 1047         sc_p scp;
 1048 
 1049         scp = md->softc;
 1050         unit = md->unit;
 1051 
 1052         MIDI_DEBUG(printf("opl%d: setting an instrument, voice %d, instr_no %d.\n", unit, voice, instr_no));
 1053 
 1054 
 1055         if (voice < 0 || voice >= md->synth.alloc.max_voice || instr_no < 0 || instr_no >= SBFM_MAXINSTR)
 1056                 return (0);
 1057 
 1058         mtx_lock(&scp->mtx);
 1059         scp->act_i[voice] = &scp->i_map[instr_no];
 1060         mtx_unlock(&scp->mtx);
 1061 
 1062         return (0);
 1063 }
 1064 
 1065 static int
 1066 opl_startnote(mididev_info *md, int voice, int note, int volume)
 1067 {
 1068         u_char fpc;
 1069         int unit, block, fnum, freq, voice_mode, voice_shift;
 1070         struct sbi_instrument *instr;
 1071         struct physical_voice_info *map;
 1072         sc_p scp;
 1073 
 1074         scp = md->softc;
 1075         unit = md->unit;
 1076 
 1077         MIDI_DEBUG(printf("opl%d: starting a note, voice %d, note %d, volume %d.\n", unit, voice, note, volume));
 1078 
 1079         if (voice < 0 || voice >= md->synth.alloc.max_voice)
 1080                 return (0);
 1081 
 1082         mtx_lock(&scp->mtx);
 1083         map = &scp->pv_map[scp->lv_map[voice]];
 1084         if (map->voice_mode == VOICE_NONE) {
 1085                 mtx_unlock(&scp->mtx);
 1086                 return (0);
 1087         }
 1088 
 1089         if (note == 255) {
 1090                 /* Change the volume. */
 1091                 opl_setvoicevolume(scp, voice, volume, scp->voc[voice].volume);
 1092                 mtx_unlock(&scp->mtx);
 1093                 return (0);
 1094         }
 1095 
 1096         /* Kill the previous note. */
 1097         opl_command(scp, map->ch, KSL_LEVEL + map->op[1], 0xff); /* Carrier volume */
 1098         opl_command(scp, map->ch, KSL_LEVEL + map->op[0], 0xff); /* Modulator volume */
 1099         if (map->voice_mode == VOICE_4OP) {
 1100                 opl_command(scp, map->ch, KSL_LEVEL + map->op[3], 0xff); /* Carrier volume */
 1101                 opl_command(scp, map->ch, KSL_LEVEL + map->op[2], 0xff); /* Modulator volume */
 1102         }
 1103         opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, 0); /* Note off. */
 1104 
 1105         instr = scp->act_i[voice];
 1106         if (instr == NULL)
 1107                 instr = &scp->i_map[0];
 1108         if (instr->channel < 0) {
 1109                 mtx_unlock(&scp->mtx);
 1110                 printf("opl_startnote: the instrument for voice %d is undefined.\n", voice);
 1111                 return (0);
 1112         }
 1113         if (map->voice_mode == VOICE_2OP && instr->key == OPL3_PATCH) {
 1114                 mtx_unlock(&scp->mtx);
 1115                 printf("opl_startnote: the voice mode %d mismatches the key 0x%x.\n", map->voice_mode, instr->key);
 1116                 return (0);
 1117         }
 1118 
 1119         voice_mode = map->voice_mode;
 1120         if (voice_mode == VOICE_4OP) {
 1121                 if (map->ch == USE_LEFT)
 1122                         voice_shift = 0;
 1123                 else
 1124                         voice_shift = 3;
 1125                 voice_shift += map->voice_num;
 1126                 if (instr->key != OPL3_PATCH) {
 1127                         voice_mode = VOICE_2OP;
 1128                         scp->cmask &= ~(1 << voice_shift);
 1129                 } else
 1130                         scp->cmask |= 1 << voice_shift;
 1131 
 1132                 opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
 1133         }
 1134 
 1135         /* Set the sound characteristics, attack, decay, sustain, release, wave select, feedback, connection. */
 1136         opl_command(scp, map->ch, AM_VIB + map->op[0], instr->operators[0]); /* Sound characteristics. */
 1137         opl_command(scp, map->ch, AM_VIB + map->op[1], instr->operators[1]);
 1138         opl_command(scp, map->ch, ATTACK_DECAY + map->op[0], instr->operators[4]); /* Attack and decay. */
 1139         opl_command(scp, map->ch, ATTACK_DECAY + map->op[1], instr->operators[5]);
 1140         opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[0], instr->operators[6]); /* Sustain and release. */
 1141         opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
 1142         opl_command(scp, map->ch, WAVE_SELECT + map->op[0], instr->operators[8]); /* Wave select. */
 1143         opl_command(scp, map->ch, WAVE_SELECT + map->op[1], instr->operators[9]);
 1144         fpc = instr->operators[10];
 1145         if ((fpc & 0x30) == 0)
 1146                 fpc |= 0x30; /* So that at least one channel is enabled. */
 1147         opl_command(scp, map->ch, FEEDBACK_CONNECTION + map->voice_num, fpc); /* Feedback and connection. */
 1148 
 1149         if (voice_mode == VOICE_4OP) {
 1150                 /* Do not forget the operators 3 and 4. */
 1151                 opl_command(scp, map->ch, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]); /* Sound characteristics. */
 1152                 opl_command(scp, map->ch, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
 1153                 opl_command(scp, map->ch, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]); /* Attack and decay. */
 1154                 opl_command(scp, map->ch, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
 1155                 opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]); /* Sustain and release. */
 1156                 opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
 1157                 opl_command(scp, map->ch, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]); /* Wave select. */
 1158                 opl_command(scp, map->ch, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
 1159                 fpc = instr->operators[OFFS_4OP + 10];
 1160                 if ((fpc & 0x30) == 0)
 1161                         fpc |= 0x30; /* So that at least one channel is enabled. */
 1162                 opl_command(scp, map->ch, FEEDBACK_CONNECTION + map->voice_num + 3, fpc); /* Feedback and connection. */
 1163         }
 1164         scp->voc[voice].mode = voice_mode;
 1165 
 1166         opl_setvoicevolume(scp, voice, volume, scp->voc[voice].volume);
 1167 
 1168         /* Calcurate the frequency. */
 1169         scp->voc[voice].orig_freq = opl_notetofreq(note) / 1000;
 1170         /* Tune for the pitch bend. */
 1171         freq = scp->voc[voice].current_freq = opl_computefinetune(scp->voc[voice].orig_freq, scp->voc[voice].bender, scp->voc[voice].bender_range);
 1172         opl_freqtofnum(freq, &block, &fnum);
 1173 
 1174         /* Now we can play the note. */
 1175         opl_command(scp, map->ch, FNUM_LOW + map->voice_num, fnum & 0xff);
 1176         scp->voc[voice].keyon_byte = 0x20 | ((block & 0x07) << 2) | ((fnum >> 8) & 0x03);
 1177         opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte);
 1178         if (voice_mode == VOICE_4OP)
 1179                 opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num + 3, scp->voc[voice].keyon_byte);
 1180 
 1181         mtx_unlock(&scp->mtx);
 1182 
 1183         return (0);
 1184 }
 1185 
 1186 static int
 1187 opl_reset(mididev_info *md)
 1188 {
 1189         int unit, i;
 1190         sc_p scp;
 1191         struct physical_voice_info *map;
 1192 
 1193         scp = md->softc;
 1194         unit = md->unit;
 1195 
 1196         MIDI_DEBUG(printf("opl%d: resetting.\n", unit));
 1197 
 1198         mtx_lock(&md->synth.vc_mtx);
 1199         mtx_lock(&scp->mtx);
 1200 
 1201         for (i = 0 ; i < MAX_VOICE ; i++)
 1202                 scp->lv_map[i] = i;
 1203 
 1204         for (i = 0 ; i < md->synth.alloc.max_voice ; i++) {
 1205                 opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[0], 0xff);
 1206                 opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[1], 0xff);
 1207                 if (scp->pv_map[scp->lv_map[i]].voice_mode == VOICE_4OP) {
 1208                         opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[2], 0xff);
 1209                         opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[3], 0xff);
 1210                 }
 1211                 /*
 1212                  * opl_killnote(md, i, 0, 64) inline-expanded to avoid
 1213                  * unlocking and relocking mutex unnecessarily.
 1214                  */
 1215                 md->synth.alloc.map[i] = 0;
 1216                 map = &scp->pv_map[scp->lv_map[i]];
 1217 
 1218                 if (map->voice_mode != VOICE_NONE) {
 1219                         opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[i].keyon_byte & ~0x20);
 1220                         
 1221                         scp->voc[i].keyon_byte = 0;
 1222                         scp->voc[i].bender = 0;
 1223                         scp->voc[i].volume = 64;
 1224                         scp->voc[i].bender_range = 200;
 1225                         scp->voc[i].orig_freq = 0;
 1226                         scp->voc[i].current_freq = 0;
 1227                         scp->voc[i].mode = 0;
 1228                 }
 1229         }
 1230 
 1231         if (scp->model >= MODEL_OPL3) {
 1232                 md->synth.alloc.max_voice = 18;
 1233                 for (i = 0 ; i < MAX_VOICE ; i++)
 1234                         scp->pv_map[i].voice_mode = VOICE_2OP;
 1235         }
 1236 
 1237         mtx_unlock(&md->synth.vc_mtx);
 1238         mtx_unlock(&scp->mtx);
 1239 
 1240         return (0);
 1241 }
 1242 
 1243 static int
 1244 opl_hwcontrol(mididev_info *md, u_char *event)
 1245 {
 1246         /* NOP. */
 1247         return (0);
 1248 }
 1249 
 1250 static int
 1251 opl_loadpatch(mididev_info *md, int format, struct uio *buf, int offs, int count, int pmgr_flag)
 1252 {
 1253         int unit;
 1254         struct sbi_instrument ins;
 1255         sc_p scp;
 1256 
 1257         scp = md->softc;
 1258         unit = md->unit;
 1259 
 1260         if (count < sizeof(ins)) {
 1261                 printf("opl_loadpatch: The patch record is too short.\n");
 1262                 return (EINVAL);
 1263         }
 1264         if (uiomove(&((char *)&ins)[offs], sizeof(ins) - offs, buf) != 0)
 1265                 printf("opl_loadpatch: User memory mangled?\n");
 1266         if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
 1267                 printf("opl_loadpatch: Instrument number %d is not valid.\n", ins.channel);
 1268                 return (EINVAL);
 1269         }
 1270         ins.key = format;
 1271 
 1272         opl_storeinstr(scp, ins.channel, &ins);
 1273         return (0);
 1274 }
 1275 
 1276 static int
 1277 opl_panning(mididev_info *md, int chn, int pan)
 1278 {
 1279         /* NOP. */
 1280         return (0);
 1281 }
 1282 
 1283 #define SET_VIBRATO(cell) do { \
 1284         int tmp; \
 1285         tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
 1286         if (press > 110) \
 1287                 tmp |= 0x40;    /* Vibrato on */ \
 1288         opl_command(scp, map->ch, AM_VIB + map->op[cell-1], tmp);} while(0);
 1289 
 1290 static int
 1291 opl_aftertouch(mididev_info *md, int voice, int press)
 1292 {
 1293         int unit, connection;
 1294         struct sbi_instrument *instr;
 1295         struct physical_voice_info *map;
 1296         sc_p scp;
 1297 
 1298         scp = md->softc;
 1299         unit = md->unit;
 1300 
 1301         MIDI_DEBUG(printf("opl%d: setting the aftertouch, voice %d, press %d.\n", unit, voice, press));
 1302 
 1303         if (voice < 0 || voice >= md->synth.alloc.max_voice)
 1304                 return (0);
 1305 
 1306         mtx_lock(&scp->mtx);
 1307 
 1308         map = &scp->pv_map[scp->lv_map[voice]];
 1309         if (map->voice_mode == VOICE_NONE) {
 1310                 mtx_unlock(&scp->mtx);
 1311                 return (0);
 1312         }
 1313 
 1314         /* Adjust the vibrato. */
 1315         instr = scp->act_i[voice];
 1316         if (instr == NULL)
 1317                 instr = &scp->i_map[0];
 1318 
 1319         if (scp->voc[voice].mode == VOICE_4OP) {
 1320                 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 1321                 switch (connection) {
 1322                 case 0:
 1323                         SET_VIBRATO(4);
 1324                         break;
 1325                 case 1:
 1326                         SET_VIBRATO(2);
 1327                         SET_VIBRATO(4);
 1328                         break;
 1329                 case 2:
 1330                         SET_VIBRATO(1);
 1331                         SET_VIBRATO(4);
 1332                         break;
 1333                 case 3:
 1334                         SET_VIBRATO(1);
 1335                         SET_VIBRATO(3);
 1336                         SET_VIBRATO(4);
 1337                         break;
 1338                 }
 1339         } else {
 1340                 SET_VIBRATO(1);
 1341                 if ((instr->operators[10] & 0x01))
 1342                         SET_VIBRATO(2);
 1343         }
 1344 
 1345         mtx_unlock(&scp->mtx);
 1346 
 1347         return (0);
 1348 }
 1349 
 1350 static int
 1351 opl_bendpitch(sc_p scp, int voice, int value)
 1352 {
 1353         int unit, block, fnum, freq;
 1354         struct physical_voice_info *map;
 1355         mididev_info *md;
 1356 
 1357         md = scp->devinfo;
 1358         unit = md->unit;
 1359 
 1360         MIDI_DEBUG(printf("opl%d: setting the pitch bend, voice %d, value %d.\n", unit, voice, value));
 1361 
 1362         mtx_lock(&scp->mtx);
 1363 
 1364         map = &scp->pv_map[scp->lv_map[voice]];
 1365         if (map->voice_mode == 0) {
 1366                 mtx_unlock(&scp->mtx);
 1367                 return (0);
 1368         }
 1369         scp->voc[voice].bender = value;
 1370         if (value == 0 || (scp->voc[voice].keyon_byte & 0x20) == 0) {
 1371                 mtx_unlock(&scp->mtx);
 1372                 return (0);
 1373         }
 1374 
 1375         freq = opl_computefinetune(scp->voc[voice].orig_freq, scp->voc[voice].bender, scp->voc[voice].bender_range);
 1376         scp->voc[voice].current_freq = freq;
 1377 
 1378         opl_freqtofnum(freq, &block, &fnum);
 1379 
 1380         opl_command(scp, map->ch, FNUM_LOW + map->voice_num, fnum & 0xff);
 1381         scp->voc[voice].keyon_byte = 0x20 | ((block & 0x07) << 2) | ((fnum >> 8) & 0x03);
 1382         opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte);
 1383         if (map->voice_mode == VOICE_4OP)
 1384                 opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num + 3, scp->voc[voice].keyon_byte);
 1385 
 1386         mtx_unlock(&scp->mtx);
 1387 
 1388         return (0);
 1389 }
 1390 
 1391 static int
 1392 opl_controller(mididev_info *md, int voice, int ctrlnum, int val)
 1393 {
 1394         int unit;
 1395         sc_p scp;
 1396 
 1397         scp = md->softc;
 1398         unit = md->unit;
 1399 
 1400         MIDI_DEBUG(printf("opl%d: setting the controller, voice %d, ctrlnum %d, val %d.\n", unit, voice, ctrlnum, val));
 1401 
 1402         if (voice < 0 || voice >= md->synth.alloc.max_voice)
 1403                 return (0);
 1404 
 1405         switch (ctrlnum) {
 1406         case CTRL_PITCH_BENDER:
 1407                 opl_bendpitch(scp, voice, val);
 1408                 break;
 1409         case CTRL_PITCH_BENDER_RANGE:
 1410                 mtx_lock(&scp->mtx);
 1411                 scp->voc[voice].bender_range = val;
 1412                 mtx_unlock(&scp->mtx);
 1413                 break;
 1414         case CTRL_MAIN_VOLUME:
 1415                 mtx_lock(&scp->mtx);
 1416                 scp->voc[voice].volume = val / 128;
 1417                 mtx_unlock(&scp->mtx);
 1418                 break;
 1419         }
 1420 
 1421         return (0);
 1422 }
 1423 
 1424 static int
 1425 opl_patchmgr(mididev_info *md, struct patmgr_info *rec)
 1426 {
 1427         return (EINVAL);
 1428 }
 1429 
 1430 static int
 1431 opl_bender(mididev_info *md, int voice, int val)
 1432 {
 1433         sc_p scp;
 1434 
 1435         scp = md->softc;
 1436 
 1437         if (voice < 0 || voice >= md->synth.alloc.max_voice)
 1438                 return (0);
 1439 
 1440         return opl_bendpitch(scp, voice, val - 8192);
 1441 }
 1442 
 1443 static int
 1444 opl_allocvoice(mididev_info *md, int chn, int note, struct voice_alloc_info *alloc)
 1445 {
 1446         int i, p, best, first, avail, best_time, is4op, instr_no;
 1447         struct sbi_instrument *instr;
 1448         sc_p scp;
 1449 
 1450         scp = md->softc;
 1451 
 1452         MIDI_DEBUG(printf("opl%d: allocating a voice, chn %d, note %d.\n", md->unit, chn, note));
 1453 
 1454         best_time = 0x7fffffff;
 1455 
 1456         mtx_lock(&md->synth.vc_mtx);
 1457 
 1458         if (chn < 0 || chn >= 15)
 1459                 instr_no = 0;
 1460         else
 1461                 instr_no = md->synth.chn_info[chn].pgm_num;
 1462 
 1463         mtx_lock(&scp->mtx);
 1464 
 1465         instr = &scp->i_map[instr_no];
 1466         if (instr->channel < 0 || md->synth.alloc.max_voice != 12)
 1467                 is4op = 0;
 1468         else if (md->synth.alloc.max_voice == 12) {
 1469                 if (instr->key == OPL3_PATCH)
 1470                         is4op = 1;
 1471                 else
 1472                         is4op = 0;
 1473         } else
 1474                 is4op = 0;
 1475 
 1476         if (is4op) {
 1477                 first = p = 0;
 1478                 avail = 6;
 1479         } else {
 1480                 if (md->synth.alloc.max_voice == 12)
 1481                         first = p = 6;
 1482                 else
 1483                         first = p = 0;
 1484                 avail = md->synth.alloc.max_voice;
 1485         }
 1486 
 1487         /* Look up a free voice. */
 1488         best = first;
 1489 
 1490         for (i = 0 ; i < avail ; i++) {
 1491                 if (alloc->map[p] == 0)
 1492                         return (p);
 1493         }
 1494         if (alloc->alloc_times[p] < best_time) {
 1495                 best_time = alloc->alloc_times[p];
 1496                 best = p;
 1497         }
 1498         p = (p + 1) % avail;
 1499 
 1500         if (best < 0)
 1501                 best = 0;
 1502         else if (best > md->synth.alloc.max_voice)
 1503                 best -= md->synth.alloc.max_voice;
 1504 
 1505         mtx_unlock(&scp->mtx);
 1506         mtx_unlock(&md->synth.vc_mtx);
 1507 
 1508         return best;
 1509 }
 1510 
 1511 static int
 1512 opl_setupvoice(mididev_info *md, int voice, int chn)
 1513 {
 1514         struct channel_info *info;
 1515         sc_p scp;
 1516 
 1517         scp = md->softc;
 1518 
 1519         MIDI_DEBUG(printf("opl%d: setting up a voice, voice %d, chn %d.\n", md->unit, voice, chn));
 1520 
 1521         mtx_lock(&md->synth.vc_mtx);
 1522 
 1523         info = &md->synth.chn_info[chn];
 1524 
 1525         opl_setinstr(md, voice, info->pgm_num);
 1526         mtx_lock(&scp->mtx);
 1527         scp->voc[voice].bender = info->bender_value;
 1528         scp->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
 1529         mtx_unlock(&scp->mtx);
 1530 
 1531         mtx_lock(&md->synth.vc_mtx);
 1532 
 1533         return (0);
 1534 }
 1535 
 1536 static int
 1537 opl_sendsysex(mididev_info *md, u_char *sysex, int len)
 1538 {
 1539         /* NOP. */
 1540         return (0);
 1541 }
 1542 
 1543 static int
 1544 opl_prefixcmd(mididev_info *md, int status)
 1545 {
 1546         /* NOP. */
 1547         return (0);
 1548 }
 1549 
 1550 static int
 1551 opl_volumemethod(mididev_info *md, int mode)
 1552 {
 1553         /* NOP. */
 1554         return (0);
 1555 }
 1556 
 1557 /*
 1558  * The functions below here are the libraries for the above ones.
 1559  */
 1560 
 1561 /* Writes a command to the OPL chip. */
 1562 static void
 1563 opl_command(sc_p scp, int ch, int addr, u_int val)
 1564 {
 1565         int model;
 1566 
 1567         MIDI_DEBUG(printf("opl%d: sending a command, addr 0x%x, val 0x%x.\n", scp->devinfo->unit, addr, val));
 1568 
 1569         model = scp->model;
 1570 
 1571         /* Write the addr first. */
 1572         bus_space_write_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2, (u_char)(addr & 0xff));
 1573         if (model < MODEL_OPL3)
 1574                 DELAY(10);
 1575         else {
 1576                 bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
 1577                 bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
 1578         }
 1579 
 1580         /* Next write the value. */
 1581         bus_space_write_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2 + 1, (u_char)(val & 0xff));
 1582         if (model < MODEL_OPL3)
 1583                 DELAY(30);
 1584         else {
 1585                 bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
 1586                 bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
 1587         }
 1588 }
 1589 
 1590 /* Reads the status of the OPL chip. */
 1591 static int
 1592 opl_status(sc_p scp)
 1593 {
 1594         MIDI_DEBUG(printf("opl%d: reading the status.\n", scp->devinfo->unit));
 1595 
 1596         return bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), 0);
 1597 }
 1598 
 1599 static void
 1600 opl_enter4opmode(sc_p scp)
 1601 {
 1602         int i;
 1603         mididev_info *devinfo;
 1604         static int v4op[MAX_VOICE] = {
 1605                 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17,
 1606         };
 1607 
 1608         devinfo = scp->devinfo;
 1609 
 1610         MIDI_DEBUG(printf("opl%d: entering 4 OP mode.\n", devinfo->unit));
 1611 
 1612         /* Connect all possible 4 OP voice operators. */
 1613         mtx_lock(&devinfo->synth.vc_mtx);
 1614         mtx_lock(&scp->mtx);
 1615         scp->cmask = 0x3f;
 1616         opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
 1617 
 1618         for (i = 0 ; i < 3 ; i++)
 1619                 scp->pv_map[i].voice_mode = VOICE_4OP;
 1620         for (i = 3 ; i < 6 ; i++)
 1621                 scp->pv_map[i].voice_mode = VOICE_NONE;
 1622         for (i = 9 ; i < 12 ; i++)
 1623                 scp->pv_map[i].voice_mode = VOICE_4OP;
 1624         for (i = 12 ; i < 15 ; i++)
 1625                 scp->pv_map[i].voice_mode = VOICE_NONE;
 1626 
 1627         for (i = 0 ; i < 12 ; i++)
 1628                 scp->lv_map[i] = v4op[i];
 1629         mtx_unlock(&scp->mtx);
 1630         devinfo->synth.alloc.max_voice = 12;
 1631         mtx_unlock(&devinfo->synth.vc_mtx);
 1632 }
 1633 
 1634 static void
 1635 opl_storeinstr(sc_p scp, int instr_no, struct sbi_instrument *instr)
 1636 {
 1637         if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || scp->model < MODEL_OPL3))
 1638                 printf("opl_storeinstr: The patch format field 0x%x is not valid.\n", instr->key);
 1639 
 1640         bcopy(instr, &scp->i_map[instr_no], sizeof(*instr));
 1641 }
 1642 
 1643 static void
 1644 opl_calcvol(u_char *regbyte, int volume, int main_vol)
 1645 {
 1646         int level;
 1647 
 1648         level = (~*regbyte & 0x3f);
 1649 
 1650         if (main_vol > 127)
 1651                 main_vol = 127;
 1652 
 1653         volume = (volume * main_vol) / 127;
 1654 
 1655         if (level > 0)
 1656                 level += opl_volumetable[volume];
 1657 
 1658         RANGE(level, 0, 0x3f);
 1659 
 1660         *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
 1661 }
 1662 
 1663 static void
 1664 opl_setvoicevolume(sc_p scp, int voice, int volume, int main_vol)
 1665 {
 1666         u_char vol1, vol2, vol3, vol4;
 1667         int connection;
 1668         struct sbi_instrument *instr;
 1669         struct physical_voice_info *map;
 1670         mididev_info *devinfo;
 1671 
 1672         devinfo = scp->devinfo;
 1673 
 1674         if (voice < 0 || voice >= devinfo->synth.alloc.max_voice)
 1675                 return;
 1676 
 1677         map = &scp->pv_map[scp->lv_map[voice]];
 1678         instr = scp->act_i[voice];
 1679         if (instr == NULL)
 1680                 instr = &scp->i_map[0];
 1681 
 1682         if (instr->channel < 0)
 1683                 return;
 1684         if (scp->voc[voice].mode == VOICE_NONE)
 1685                 return;
 1686         if (scp->voc[voice].mode == VOICE_2OP) { /* 2 OP mode. */
 1687                 vol1 = instr->operators[2];
 1688                 vol2 = instr->operators[3];
 1689                 if ((instr->operators[10] & 0x01))
 1690                         opl_calcvol(&vol1, volume, main_vol);
 1691                 opl_calcvol(&vol2, volume, main_vol);
 1692                 opl_command(scp, map->ch, KSL_LEVEL + map->op[0], vol1);
 1693                 opl_command(scp, map->ch, KSL_LEVEL + map->op[1], vol2);
 1694         } else { /* 4 OP mode. */
 1695                 vol1 = instr->operators[2];
 1696                 vol2 = instr->operators[3];
 1697                 vol3 = instr->operators[OFFS_4OP + 2];
 1698                 vol4 = instr->operators[OFFS_4OP + 3];
 1699                 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 1700                 switch(connection) {
 1701                 case 0:
 1702                         opl_calcvol(&vol4, volume, main_vol);
 1703                         break;
 1704                 case 1:
 1705                         opl_calcvol(&vol2, volume, main_vol);
 1706                         opl_calcvol(&vol4, volume, main_vol);
 1707                         break;
 1708                 case 2:
 1709                         opl_calcvol(&vol1, volume, main_vol);
 1710                         opl_calcvol(&vol4, volume, main_vol);
 1711                         break;
 1712                 case 3:
 1713                         opl_calcvol(&vol1, volume, main_vol);
 1714                         opl_calcvol(&vol3, volume, main_vol);
 1715                         opl_calcvol(&vol4, volume, main_vol);
 1716                         break;
 1717                 }
 1718                 opl_command(scp, map->ch, KSL_LEVEL + map->op[0], vol1);
 1719                 opl_command(scp, map->ch, KSL_LEVEL + map->op[1], vol2);
 1720                 opl_command(scp, map->ch, KSL_LEVEL + map->op[2], vol3);
 1721                 opl_command(scp, map->ch, KSL_LEVEL + map->op[3], vol4);
 1722         }
 1723 }
 1724 
 1725 static void
 1726 opl_freqtofnum(int freq, int *block, int *fnum)
 1727 {
 1728         int f, octave;
 1729 
 1730         f = freq;
 1731         octave = 5;
 1732 
 1733         if (f == 0)
 1734                 octave = 0;
 1735         else if (f < 261) {
 1736                 while (f < 261) {
 1737                         octave--;
 1738                         f <<= 1;
 1739                 }
 1740         } else if (f > 493) {
 1741                 while (f > 493) {
 1742                         octave++;
 1743                         f >>= 1;
 1744                 }
 1745         }
 1746         if (octave > 7)
 1747                 octave = 7;
 1748 
 1749         *fnum = freq * (1 << (20 - octave)) / 49716;
 1750         *block = octave;
 1751 }
 1752 
 1753 static int notes[] =
 1754 {
 1755         261632,
 1756         277189,
 1757         293671,
 1758         311132,
 1759         329632,
 1760         349232,
 1761         369998,
 1762         391998,
 1763         415306,
 1764         440000,
 1765         466162,
 1766         493880
 1767 };
 1768 
 1769 #define BASE_OCTAVE 5
 1770 
 1771 static int
 1772 opl_notetofreq(int note_num)
 1773 {
 1774         int note, octave, note_freq;
 1775 
 1776         octave = note_num / 12;
 1777         note = note_num % 12;
 1778 
 1779         note_freq = notes[note];
 1780 
 1781         if (octave < BASE_OCTAVE)
 1782                 note_freq >>= (BASE_OCTAVE - octave);
 1783         else if (octave > BASE_OCTAVE)
 1784                 note_freq <<= (octave - BASE_OCTAVE);
 1785 
 1786         return (note_freq);
 1787 }
 1788 
 1789 static u_long
 1790 opl_computefinetune(u_long base_freq, int bend, int range)
 1791 {
 1792         u_long amount;
 1793         int negative, semitones, cents, multiplier;
 1794 
 1795         if (bend == 0 || range == 0 || base_freq == 0)
 1796                 return (base_freq);
 1797 
 1798         multiplier = 1;
 1799 
 1800         if (range > 8192)
 1801                 range = 8192;
 1802 
 1803         bend = bend * range / 8192;
 1804         if (bend == 0)
 1805                 return (base_freq);
 1806 
 1807         if (bend < 0) {
 1808                 negative = 1;
 1809                 bend = -bend;
 1810         }
 1811         else
 1812                 negative = 0;
 1813         if (bend > range)
 1814                 bend = range;
 1815 
 1816         while (bend > 2399) {
 1817                 multiplier *= 4;
 1818                 bend -= 2400;
 1819         }
 1820 
 1821         semitones = bend / 100;
 1822         cents = bend % 100;
 1823 
 1824         amount = (u_long)(semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
 1825 
 1826         if (negative)
 1827                 return (base_freq * 10000) / amount;
 1828         else
 1829                 return (base_freq * amount) / 10000;
 1830 }
 1831 
 1832 /* Allocates resources other than IO ports. */
 1833 static int
 1834 opl_allocres(sc_p scp, device_t dev)
 1835 {
 1836         if (scp->io == NULL) {
 1837                 scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, 0, ~0, 4, RF_ACTIVE);
 1838                 if (scp->io == NULL)
 1839                         return (1);
 1840         }
 1841 
 1842         return (0);
 1843 }
 1844 
 1845 /* Releases resources. */
 1846 static void
 1847 opl_releaseres(sc_p scp, device_t dev)
 1848 {
 1849         if (scp->io != NULL) {
 1850                 bus_release_resource(dev, SYS_RES_IOPORT, scp->io_rid, scp->io);
 1851                 scp->io = NULL;
 1852         }
 1853 }
 1854 
 1855 static device_method_t opl_methods[] = {
 1856         /* Device interface */
 1857         DEVMETHOD(device_probe , opl_probe ),
 1858         DEVMETHOD(device_attach, opl_attach),
 1859 
 1860         { 0, 0 },
 1861 };
 1862 
 1863 static driver_t opl_driver = {
 1864         "midi",
 1865         opl_methods,
 1866         sizeof(struct opl_softc),
 1867 };
 1868 
 1869 DRIVER_MODULE(opl, isa, opl_driver, midi_devclass, 0, 0);
 1870 
 1871 static device_method_t oplsbc_methods[] = {
 1872         /* Device interface */
 1873         DEVMETHOD(device_probe , oplsbc_probe ),
 1874         DEVMETHOD(device_attach, oplsbc_attach),
 1875 
 1876         { 0, 0 },
 1877 };
 1878 
 1879 static driver_t oplsbc_driver = {
 1880         "midi",
 1881         oplsbc_methods,
 1882         sizeof(struct opl_softc),
 1883 };
 1884 
 1885 DRIVER_MODULE(oplsbc, sbc, oplsbc_driver, midi_devclass, 0, 0);

Cache object: 3656387acd8d6a3e6753323efbbfba8f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.