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

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