root/drivers/sound/opl3.c

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

DEFINITIONS

This source file includes following definitions.
  1. enable_opl3_mode
  2. enter_4op_mode
  3. opl3_ioctl
  4. opl3_detect
  5. opl3_kill_note
  6. store_instr
  7. opl3_set_instr
  8. calc_vol
  9. set_voice_volume
  10. opl3_start_note
  11. freq_to_fnum
  12. opl3_command
  13. opl3_reset
  14. opl3_open
  15. opl3_close
  16. opl3_hw_control
  17. opl3_load_patch
  18. opl3_panning
  19. opl3_volume_method
  20. opl3_aftertouch
  21. opl3_controller
  22. opl3_patchmgr
  23. opl3_init

   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 /* Major improvements to the FM handling 30AUG92 by Rob Hooft, */
  31 /* hooft@chem.ruu.nl */
  32 
  33 #include "sound_config.h"
  34 
  35 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
  36 
  37 #include "opl3.h"
  38 
  39 #define MAX_VOICE       18
  40 #define OFFS_4OP        11      /* Definitions for the operators OP3 and OP4
  41                                    * begin here */
  42 
  43 static int      opl3_enabled = 0;
  44 static int      left_address = 0x388, right_address = 0x388, both_address = 0;
  45 
  46 static int      nr_voices = 9;
  47 static int      logical_voices[MAX_VOICE] =
  48 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  49 
  50 struct voice_info
  51   {
  52     unsigned char   keyon_byte;
  53     long            bender;
  54     long            bender_range;
  55     unsigned long   orig_freq;
  56     unsigned long   current_freq;
  57     int             mode;
  58   };
  59 
  60 static struct voice_info voices[MAX_VOICE];
  61 
  62 static struct sbi_instrument *instrmap;
  63 static struct sbi_instrument *active_instrument[MAX_VOICE] =
  64 {NULL};
  65 
  66 static struct synth_info fm_info =
  67 {"AdLib", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
  68 
  69 static int      already_initialized = 0;
  70 
  71 static int      opl3_ok = 0;
  72 static int      opl3_busy = 0;
  73 static int      fm_model = 0;   /* 0=no fm, 1=mono, 2=SB Pro 1, 3=SB Pro 2       */
  74 
  75 static int      store_instr (int instr_no, struct sbi_instrument *instr);
  76 static void     freq_to_fnum (int freq, int *block, int *fnum);
  77 static void     opl3_command (int io_addr, unsigned int addr, unsigned int val);
  78 static int      opl3_kill_note (int dev, int voice, int velocity);
  79 static unsigned char connection_mask = 0x00;
  80 
  81 void
  82 enable_opl3_mode (int left, int right, int both)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84   if (opl3_enabled)
  85     return;
  86 
  87   opl3_enabled = 1;
  88   left_address = left;
  89   right_address = right;
  90   both_address = both;
  91   fm_info.capabilities = SYNTH_CAP_OPL3;
  92   fm_info.synth_subtype = FM_TYPE_OPL3;
  93 }
  94 
  95 static void
  96 enter_4op_mode (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98   int             i;
  99   static int      voices_4op[MAX_VOICE] =
 100   {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
 101 
 102   connection_mask = 0x3f;
 103   opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);       /* Select all 4-OP
 104                                                                          * voices */
 105   for (i = 0; i < 3; i++)
 106     physical_voices[i].voice_mode = 4;
 107   for (i = 3; i < 6; i++)
 108     physical_voices[i].voice_mode = 0;
 109 
 110   for (i = 9; i < 12; i++)
 111     physical_voices[i].voice_mode = 4;
 112   for (i = 12; i < 15; i++)
 113     physical_voices[i].voice_mode = 0;
 114 
 115   for (i = 0; i < 12; i++)
 116     logical_voices[i] = voices_4op[i];
 117   nr_voices = 12;
 118 }
 119 
 120 static int
 121 opl3_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 122             unsigned int cmd, unsigned int arg)
 123 {
 124   switch (cmd)
 125     {
 126 
 127     case SNDCTL_FM_LOAD_INSTR:
 128       {
 129         struct sbi_instrument ins;
 130 
 131         IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
 132 
 133         if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 134           {
 135             printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 136             return RET_ERROR (EINVAL);
 137           }
 138 
 139         pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
 140         return store_instr (ins.channel, &ins);
 141       }
 142       break;
 143 
 144     case SNDCTL_SYNTH_INFO:
 145       fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
 146 
 147       IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
 148       return 0;
 149       break;
 150 
 151     case SNDCTL_SYNTH_MEMAVL:
 152       return 0x7fffffff;
 153       break;
 154 
 155     case SNDCTL_FM_4OP_ENABLE:
 156       if (opl3_enabled)
 157         enter_4op_mode ();
 158       return 0;
 159       break;
 160 
 161     default:
 162       return RET_ERROR (EINVAL);
 163     }
 164 
 165 }
 166 
 167 int
 168 opl3_detect (int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170   /*
 171    * This function returns 1 if the FM chicp is present at the given I/O port
 172    * The detection algorithm plays with the timer built in the FM chip and
 173    * looks for a change in the status register.
 174    *
 175    * Note! The timers of the FM chip are not connected to AdLib (and compatible)
 176    * boards.
 177    *
 178    * Note2! The chip is initialized if detected.
 179    */
 180 
 181   unsigned char   stat1, stat2;
 182   int             i;
 183 
 184   if (already_initialized)
 185     {
 186       return 0;                 /* Do avoid duplicate initializations */
 187     }
 188 
 189   if (opl3_enabled)
 190     ioaddr = left_address;
 191 
 192   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);     /* Reset timers 1 and 2 */
 193   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);     /* Reset the IRQ of FM
 194                                                                  * chicp */
 195 
 196   stat1 = INB (ioaddr);         /* Read status register */
 197 
 198   if ((stat1 & 0xE0) != 0x00)
 199     {
 200       return 0;                 /* Should be 0x00        */
 201     }
 202 
 203   opl3_command (ioaddr, TIMER1_REGISTER, 0xff); /* Set timer 1 to 0xff */
 204   opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
 205                 TIMER2_MASK | TIMER1_START);    /* Unmask and start timer 1 */
 206 
 207   /*
 208    * Now we have to delay at least 80 msec
 209    */
 210 
 211   for (i = 0; i < 50; i++)
 212     tenmicrosec ();             /* To be sure */
 213 
 214   stat2 = INB (ioaddr);         /* Read status after timers have expired */
 215 
 216   /* Stop the timers */
 217 
 218   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);     /* Reset timers 1 and 2 */
 219   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);     /* Reset the IRQ of FM
 220                                                                  * chicp */
 221 
 222   if ((stat2 & 0xE0) != 0xc0)
 223     {
 224       return 0;                 /* There is no YM3812 */
 225     }
 226 
 227   /* There is a FM chicp in this address. Now set some default values. */
 228 
 229   for (i = 0; i < 9; i++)
 230     opl3_command (ioaddr, KEYON_BLOCK + i, 0);  /* Note off */
 231 
 232   opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
 233   opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);     /* Melodic mode. */
 234 
 235   return 1;
 236 }
 237 
 238 static int
 239 opl3_kill_note (int dev, int voice, int velocity)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241   struct physical_voice_info *map;
 242 
 243   if (voice < 0 || voice >= nr_voices)
 244     return 0;
 245 
 246   map = &physical_voices[logical_voices[voice]];
 247 
 248   DEB (printk ("Kill note %d\n", voice));
 249 
 250   if (map->voice_mode == 0)
 251     return 0;
 252 
 253   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
 254 
 255   voices[voice].keyon_byte = 0;
 256   voices[voice].bender = 0;
 257   voices[voice].bender_range = 200;     /* 200 cents = 2 semitones */
 258   voices[voice].orig_freq = 0;
 259   voices[voice].current_freq = 0;
 260   voices[voice].mode = 0;
 261 
 262   return 0;
 263 }
 264 
 265 #define HIHAT                   0
 266 #define CYMBAL                  1
 267 #define TOMTOM                  2
 268 #define SNARE                   3
 269 #define BDRUM                   4
 270 #define UNDEFINED               TOMTOM
 271 #define DEFAULT                 TOMTOM
 272 
 273 static int
 274 store_instr (int instr_no, struct sbi_instrument *instr)
     /* [previous][next][first][last][top][bottom][index][help] */
 275 {
 276 
 277   if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
 278     printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
 279   memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
 280 
 281   return 0;
 282 }
 283 
 284 static int
 285 opl3_set_instr (int dev, int voice, int instr_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 286 {
 287   if (voice < 0 || voice >= nr_voices)
 288     return 0;
 289 
 290   if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
 291     return 0;
 292 
 293   active_instrument[voice] = &instrmap[instr_no];
 294   return 0;
 295 }
 296 
 297 /*
 298  * The next table looks magical, but it certainly is not. Its values have
 299  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
 300  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
 301  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
 302  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
 303  * volume -8 it was implemented as a table because it is only 128 bytes and
 304  * it saves a lot of log() calculations. (RH)
 305  */
 306 char            fm_volume_table[128] =
 307 {-64, -48, -40, -35, -32, -29, -27, -26,        /* 0 -   7 */
 308  -24, -23, -21, -20, -19, -18, -18, -17,        /* 8 -  15 */
 309  -16, -15, -15, -14, -13, -13, -12, -12,        /* 16 -  23 */
 310  -11, -11, -10, -10, -10, -9, -9, -8,   /* 24 -  31 */
 311  -8, -8, -7, -7, -7, -6, -6, -6,/* 32 -  39 */
 312  -5, -5, -5, -5, -4, -4, -4, -4,/* 40 -  47 */
 313  -3, -3, -3, -3, -2, -2, -2, -2,/* 48 -  55 */
 314  -2, -1, -1, -1, -1, 0, 0, 0,   /* 56 -  63 */
 315  0, 0, 0, 1, 1, 1, 1, 1,        /* 64 -  71 */
 316  1, 2, 2, 2, 2, 2, 2, 2,        /* 72 -  79 */
 317  3, 3, 3, 3, 3, 3, 3, 4,        /* 80 -  87 */
 318  4, 4, 4, 4, 4, 4, 4, 5,        /* 88 -  95 */
 319  5, 5, 5, 5, 5, 5, 5, 5,        /* 96 - 103 */
 320  6, 6, 6, 6, 6, 6, 6, 6,        /* 104 - 111 */
 321  6, 7, 7, 7, 7, 7, 7, 7,        /* 112 - 119 */
 322  7, 7, 7, 8, 8, 8, 8, 8};       /* 120 - 127 */
 323 
 324 static void
 325 calc_vol (unsigned char *regbyte, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327   int             level = (~*regbyte & 0x3f);
 328 
 329   if (level)
 330     level += fm_volume_table[volume];
 331 
 332   if (level > 0x3f)
 333     level = 0x3f;
 334   if (level < 0)
 335     level = 0;
 336 
 337   *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
 338 }
 339 
 340 static void
 341 set_voice_volume (int voice, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 342 {
 343   unsigned char   vol1, vol2, vol3, vol4;
 344   struct sbi_instrument *instr;
 345   struct physical_voice_info *map;
 346 
 347   if (voice < 0 || voice >= nr_voices)
 348     return;
 349 
 350   map = &physical_voices[logical_voices[voice]];
 351 
 352   instr = active_instrument[voice];
 353 
 354   if (!instr)
 355     instr = &instrmap[0];
 356 
 357   if (instr->channel < 0)
 358     return;
 359 
 360   if (voices[voice].mode == 0)
 361     return;
 362 
 363   if (voices[voice].mode == 2)
 364     {                           /* 2 OP voice */
 365 
 366       vol1 = instr->operators[2];
 367       vol2 = instr->operators[3];
 368 
 369       if ((instr->operators[10] & 0x01))
 370         {                       /* Additive synthesis    */
 371           calc_vol (&vol1, volume);
 372           calc_vol (&vol2, volume);
 373         }
 374       else
 375         {                       /* FM synthesis */
 376           calc_vol (&vol2, volume);
 377         }
 378 
 379       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1); /* Modulator volume */
 380       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2); /* Carrier volume */
 381     }
 382   else
 383     {                           /* 4 OP voice */
 384       int             connection;
 385 
 386       vol1 = instr->operators[2];
 387       vol2 = instr->operators[3];
 388       vol3 = instr->operators[OFFS_4OP + 2];
 389       vol4 = instr->operators[OFFS_4OP + 3];
 390 
 391       /*
 392        * The connection method for 4 OP voices is defined by the rightmost
 393        * bits at the offsets 10 and 10+OFFS_4OP
 394        */
 395 
 396       connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 397 
 398       switch (connection)
 399         {
 400         case 0:
 401           calc_vol (&vol4, volume);     /* Just the OP 4 is carrier */
 402           break;
 403 
 404         case 1:
 405           calc_vol (&vol2, volume);
 406           calc_vol (&vol4, volume);
 407           break;
 408 
 409         case 2:
 410           calc_vol (&vol1, volume);
 411           calc_vol (&vol4, volume);
 412           break;
 413 
 414         case 3:
 415           calc_vol (&vol1, volume);
 416           calc_vol (&vol3, volume);
 417           calc_vol (&vol4, volume);
 418           break;
 419 
 420         default:/* Why ?? */ ;
 421         }
 422 
 423       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
 424       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
 425       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
 426       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
 427     }
 428 }
 429 
 430 static int
 431 opl3_start_note (int dev, int voice, int note, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 432 {
 433   unsigned char   data, fpc;
 434   int             block, fnum, freq, voice_mode;
 435   struct sbi_instrument *instr;
 436   struct physical_voice_info *map;
 437 
 438   if (voice < 0 || voice >= nr_voices)
 439     return 0;
 440 
 441   map = &physical_voices[logical_voices[voice]];
 442 
 443   if (map->voice_mode == 0)
 444     return 0;
 445 
 446   if (note == 255)              /* Just change the volume */
 447     {
 448       set_voice_volume (voice, volume);
 449       return 0;
 450     }
 451 
 452   /* Kill previous note before playing */
 453   opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);     /* Carrier volume to min */
 454   opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);     /* Modulator volume to */
 455 
 456   if (map->voice_mode == 4)
 457     {
 458       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
 459       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
 460     }
 461 
 462   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);       /* Note off */
 463 
 464   instr = active_instrument[voice];
 465 
 466   if (!instr)
 467     instr = &instrmap[0];
 468 
 469   if (instr->channel < 0)
 470     {
 471       printk (
 472                "OPL3: Initializing voice %d with undefined instrument\n",
 473                voice);
 474       return 0;
 475     }
 476 
 477   if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
 478     return 0;                   /* Cannot play */
 479 
 480   voice_mode = map->voice_mode;
 481 
 482   if (voice_mode == 4)
 483     {
 484       int             voice_shift;
 485 
 486       voice_shift = (map->ioaddr == left_address) ? 0 : 3;
 487       voice_shift += map->voice_num;
 488 
 489       if (instr->key != OPL3_PATCH)     /* Just 2 OP patch */
 490         {
 491           voice_mode = 2;
 492           connection_mask &= ~(1 << voice_shift);
 493         }
 494       else
 495         {
 496           connection_mask |= (1 << voice_shift);
 497         }
 498 
 499       opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 500     }
 501 
 502   /* Set Sound Characteristics */
 503   opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
 504   opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
 505 
 506   /* Set Attack/Decay */
 507   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
 508   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
 509 
 510   /* Set Sustain/Release */
 511   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
 512   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
 513 
 514   /* Set Wave Select */
 515   opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
 516   opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
 517 
 518   /* Set Feedback/Connection */
 519   fpc = instr->operators[10];
 520   if (!(fpc & 0x30))
 521     fpc |= 0x30;                /* Ensure that at least one chn is enabled */
 522   opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
 523                 fpc);
 524 
 525   /*
 526    * If the voice is a 4 OP one, initialize the operators 3 and 4 also
 527    */
 528 
 529   if (voice_mode == 4)
 530     {
 531 
 532       /* Set Sound Characteristics */
 533       opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
 534       opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
 535 
 536       /* Set Attack/Decay */
 537       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
 538       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
 539 
 540       /* Set Sustain/Release */
 541       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
 542       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
 543 
 544       /* Set Wave Select */
 545       opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
 546       opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
 547 
 548       /* Set Feedback/Connection */
 549       fpc = instr->operators[OFFS_4OP + 10];
 550       if (!(fpc & 0x30))
 551         fpc |= 0x30;            /* Ensure that at least one chn is enabled */
 552       opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
 553     }
 554 
 555   voices[voice].mode = voice_mode;
 556 
 557   set_voice_volume (voice, volume);
 558 
 559   freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
 560 
 561   /*
 562    * Since the pitch bender may have been set before playing the note, we
 563    * have to calculate the bending now.
 564    */
 565 
 566   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
 567   voices[voice].current_freq = freq;
 568 
 569   freq_to_fnum (freq, &block, &fnum);
 570 
 571   /* Play note */
 572 
 573   data = fnum & 0xff;           /* Least significant bits of fnumber */
 574   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
 575 
 576   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
 577   voices[voice].keyon_byte = data;
 578   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
 579   if (voice_mode == 4)
 580     opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
 581 
 582   return 0;
 583 }
 584 
 585 static void
 586 freq_to_fnum (int freq, int *block, int *fnum)
     /* [previous][next][first][last][top][bottom][index][help] */
 587 {
 588   int             f, octave;
 589 
 590   /* Converts the note frequency to block and fnum values for the FM chip */
 591   /* First try to compute the block -value (octave) where the note belongs */
 592 
 593   f = freq;
 594 
 595   octave = 5;
 596 
 597   if (f == 0)
 598     octave = 0;
 599   else if (f < 261)
 600     {
 601       while (f < 261)
 602         {
 603           octave--;
 604           f <<= 1;
 605         }
 606     }
 607   else if (f > 493)
 608     {
 609       while (f > 493)
 610         {
 611           octave++;
 612           f >>= 1;
 613         }
 614     }
 615 
 616   if (octave > 7)
 617     octave = 7;
 618 
 619   *fnum = freq * (1 << (20 - octave)) / 49716;
 620   *block = octave;
 621 }
 622 
 623 static void
 624 opl3_command (int io_addr, unsigned int addr, unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
 625 {
 626   int             i;
 627 
 628   /*
 629    * The original 2-OP synth requires a quite long delay after writing to a
 630    * register. The OPL-3 survives with just two INBs
 631    */
 632 
 633   OUTB ((unsigned char) (addr & 0xff), io_addr);        /* Select register       */
 634 
 635   if (!opl3_enabled)
 636     tenmicrosec ();
 637   else
 638     for (i = 0; i < 2; i++)
 639       INB (io_addr);
 640 
 641   OUTB ((unsigned char) (val & 0xff), io_addr + 1);     /* Write to register  */
 642 
 643   if (!opl3_enabled)
 644     {
 645       tenmicrosec ();
 646       tenmicrosec ();
 647       tenmicrosec ();
 648     }
 649   else
 650     for (i = 0; i < 2; i++)
 651       INB (io_addr);
 652 }
 653 
 654 static void
 655 opl3_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 656 {
 657   int             i;
 658 
 659   for (i = 0; i < nr_voices; i++)
 660     {
 661       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 662                 KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);    /* OP1 volume to min */
 663 
 664       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 665                 KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);    /* OP2 volume to min */
 666 
 667       if (physical_voices[logical_voices[i]].voice_mode == 4)   /* 4 OP voice */
 668         {
 669           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 670                 KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);    /* OP3 volume to min */
 671 
 672           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 673                 KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);    /* OP4 volume to min */
 674         }
 675 
 676       opl3_kill_note (dev, i, 64);
 677     }
 678 
 679   if (opl3_enabled)
 680     {
 681       nr_voices = 18;
 682 
 683       for (i = 0; i < 18; i++)
 684         logical_voices[i] = i;
 685 
 686       for (i = 0; i < 18; i++)
 687         physical_voices[i].voice_mode = 2;
 688 
 689     }
 690 
 691 }
 692 
 693 static int
 694 opl3_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 695 {
 696   if (!opl3_ok)
 697     return RET_ERROR (ENXIO);
 698   if (opl3_busy)
 699     return RET_ERROR (EBUSY);
 700   opl3_busy = 1;
 701 
 702   connection_mask = 0x00;       /* Just 2 OP voices */
 703   if (opl3_enabled)
 704     opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 705   return 0;
 706 }
 707 
 708 static void
 709 opl3_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711   opl3_busy = 0;
 712   nr_voices = opl3_enabled ? 18 : 9;
 713   fm_info.nr_drums = 0;
 714   fm_info.perc_mode = 0;
 715 
 716   opl3_reset (dev);
 717 }
 718 
 719 static void
 720 opl3_hw_control (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 721 {
 722 }
 723 
 724 static int
 725 opl3_load_patch (int dev, int format, snd_rw_buf * addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 726                  int offs, int count, int pmgr_flag)
 727 {
 728   struct sbi_instrument ins;
 729 
 730   if (count < sizeof (ins))
 731     {
 732       printk ("FM Error: Patch record too short\n");
 733       return RET_ERROR (EINVAL);
 734     }
 735 
 736   COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
 737 
 738   if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 739     {
 740       printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 741       return RET_ERROR (EINVAL);
 742     }
 743   ins.key = format;
 744 
 745   return store_instr (ins.channel, &ins);
 746 }
 747 
 748 static void
 749 opl3_panning (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 750 {
 751 }
 752 
 753 static void
 754 opl3_volume_method (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 755 {
 756 }
 757 
 758 #define SET_VIBRATO(cell) { \
 759       tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
 760       if (pressure > 110) \
 761         tmp |= 0x40;            /* Vibrato on */ \
 762       opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
 763 
 764 static void
 765 opl3_aftertouch (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 766 {
 767   int             tmp;
 768   struct sbi_instrument *instr;
 769   struct physical_voice_info *map;
 770 
 771   if (voice < 0 || voice >= nr_voices)
 772     return;
 773 
 774   map = &physical_voices[logical_voices[voice]];
 775 
 776   DEB (printk ("Aftertouch %d\n", voice));
 777 
 778   if (map->voice_mode == 0)
 779     return;
 780 
 781   /*
 782    * Adjust the amount of vibrato depending the pressure
 783    */
 784 
 785   instr = active_instrument[voice];
 786 
 787   if (!instr)
 788     instr = &instrmap[0];
 789 
 790   if (voices[voice].mode == 4)
 791     {
 792       int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 793 
 794       switch (connection)
 795         {
 796         case 0:
 797           SET_VIBRATO (4);
 798           break;
 799 
 800         case 1:
 801           SET_VIBRATO (2);
 802           SET_VIBRATO (4);
 803           break;
 804 
 805         case 2:
 806           SET_VIBRATO (1);
 807           SET_VIBRATO (4);
 808           break;
 809 
 810         case 3:
 811           SET_VIBRATO (1);
 812           SET_VIBRATO (3);
 813           SET_VIBRATO (4);
 814           break;
 815 
 816         }
 817       /* Not implemented yet */
 818     }
 819   else
 820     {
 821       SET_VIBRATO (1);
 822 
 823       if ((instr->operators[10] & 0x01))        /* Additive synthesis */
 824         SET_VIBRATO (2);
 825     }
 826 }
 827 
 828 #undef SET_VIBRATO
 829 
 830 static void
 831 opl3_controller (int dev, int voice, int ctrl_num, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 832 {
 833   unsigned char   data;
 834   int             block, fnum, freq;
 835   struct physical_voice_info *map;
 836 
 837   if (voice < 0 || voice >= nr_voices)
 838     return;
 839 
 840   map = &physical_voices[logical_voices[voice]];
 841 
 842   if (map->voice_mode == 0)
 843     return;
 844 
 845   switch (ctrl_num)
 846     {
 847     case CTRL_PITCH_BENDER:
 848       voices[voice].bender = value;
 849       if (!value)
 850         return;
 851       if (!(voices[voice].keyon_byte & 0x20))
 852         return;                 /* Not keyed on */
 853 
 854       freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
 855       voices[voice].current_freq = freq;
 856 
 857       freq_to_fnum (freq, &block, &fnum);
 858 
 859       data = fnum & 0xff;       /* Least significant bits of fnumber */
 860       opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
 861 
 862       data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3); /* KEYON|OCTAVE|MS bits
 863                                                                            * of f-num */
 864       voices[voice].keyon_byte = data;
 865       opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
 866       break;
 867 
 868     case CTRL_PITCH_BENDER_RANGE:
 869       voices[voice].bender_range = value;
 870       break;
 871     }
 872 }
 873 
 874 static int
 875 opl3_patchmgr (int dev, struct patmgr_info *rec)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877   return RET_ERROR (EINVAL);
 878 }
 879 
 880 static struct synth_operations opl3_operations =
 881 {
 882   &fm_info,
 883   SYNTH_TYPE_FM,
 884   FM_TYPE_ADLIB,
 885   opl3_open,
 886   opl3_close,
 887   opl3_ioctl,
 888   opl3_kill_note,
 889   opl3_start_note,
 890   opl3_set_instr,
 891   opl3_reset,
 892   opl3_hw_control,
 893   opl3_load_patch,
 894   opl3_aftertouch,
 895   opl3_controller,
 896   opl3_panning,
 897   opl3_volume_method,
 898   opl3_patchmgr
 899 };
 900 
 901 long
 902 opl3_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
 903 {
 904   int             i;
 905 
 906   PERMANENT_MALLOC (struct sbi_instrument *, instrmap,
 907                     SBFM_MAXINSTR * sizeof (*instrmap), mem_start);
 908 
 909   synth_devs[num_synths++] = &opl3_operations;
 910   fm_model = 0;
 911   opl3_ok = 1;
 912   if (opl3_enabled)
 913     {
 914       printk (" <Yamaha OPL-3 FM>");
 915       fm_model = 2;
 916       nr_voices = 18;
 917       fm_info.nr_drums = 0;
 918       fm_info.capabilities |= SYNTH_CAP_OPL3;
 919 #ifndef SCO
 920       strcpy (fm_info.name, "Yamaha OPL-3");
 921 #endif
 922 
 923       for (i = 0; i < 18; i++)
 924         if (physical_voices[i].ioaddr == USE_LEFT)
 925           physical_voices[i].ioaddr = left_address;
 926         else
 927           physical_voices[i].ioaddr = right_address;
 928 
 929 
 930       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);    /* Enable OPL-3 mode */
 931       opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);   /* Select all 2-OP
 932                                                                          * voices */
 933     }
 934   else
 935     {
 936       printk (" <Yamaha 2-OP FM>");
 937       fm_model = 1;
 938       nr_voices = 9;
 939       fm_info.nr_drums = 0;
 940 
 941       for (i = 0; i < 18; i++)
 942         physical_voices[i].ioaddr = left_address;
 943     };
 944 
 945   already_initialized = 1;
 946   for (i = 0; i < SBFM_MAXINSTR; i++)
 947     instrmap[i].channel = -1;
 948 
 949   return mem_start;
 950 }
 951 
 952 #endif

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