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. bend_pitch
  22. opl3_controller
  23. opl3_patchmgr
  24. opl3_bender
  25. opl3_alloc_voice
  26. opl3_setup_voice
  27. opl3_init

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

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