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

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