root/drivers/sound/sequencer.c

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

DEFINITIONS

This source file includes following definitions.
  1. sequencer_read
  2. sequencer_midi_output
  3. seq_copy_to_input
  4. sequencer_midi_input
  5. seq_input_event
  6. sequencer_write
  7. seq_queue
  8. extended_event
  9. find_voice
  10. alloc_voice
  11. seq_chn_voice_event
  12. seq_chn_common_event
  13. seq_timing_event
  14. seq_local_event
  15. seq_startplay
  16. reset_controllers
  17. setup_mode2
  18. sequencer_open
  19. seq_drain_midi_queues
  20. sequencer_release
  21. seq_sync
  22. midi_outc
  23. seq_reset
  24. seq_panic
  25. sequencer_ioctl
  26. sequencer_select
  27. sequencer_timer
  28. note_to_freq
  29. compute_finetune
  30. sequencer_init
  31. sequencer_read
  32. sequencer_write
  33. sequencer_open
  34. sequencer_release
  35. sequencer_ioctl
  36. sequencer_lseek
  37. sequencer_init
  38. sequencer_select

   1 /*
   2  * sound/sequencer.c
   3  *
   4  * The sequencer personality manager.
   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 #define SEQUENCER_C
  31 #include "sound_config.h"
  32 
  33 #ifdef CONFIGURE_SOUNDCARD
  34 
  35 #ifndef EXCLUDE_SEQUENCER
  36 
  37 static int      sequencer_ok = 0;
  38 static struct sound_timer_operations *tmr;
  39 static int      tmr_no = -1;    /* Currently selected timer */
  40 static int      pending_timer = -1;     /* For timer change operation */
  41 
  42 /*
  43  * Local counts for number of synth and MIDI devices. These are initialized
  44  * by the sequencer_open.
  45  */
  46 static int      max_mididev = 0;
  47 static int      max_synthdev = 0;
  48 
  49 /*
  50  * The seq_mode gives the operating mode of the sequencer:
  51  *      1 = level1 (the default)
  52  *      2 = level2 (extended capabilities)
  53  */
  54 
  55 #define SEQ_1   1
  56 #define SEQ_2   2
  57 static int      seq_mode = SEQ_1;
  58 
  59 DEFINE_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
  60 DEFINE_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
  61 
  62 static int      midi_opened[MAX_MIDI_DEV] =
  63 {0};
  64 static int      midi_written[MAX_MIDI_DEV] =
  65 {0};
  66 
  67 unsigned long   prev_input_time = 0;
  68 int             prev_event_time;
  69 unsigned long   seq_time = 0;
  70 
  71 #include "tuning.h"
  72 
  73 #define EV_SZ   8
  74 #define IEV_SZ  8
  75 static unsigned char *queue = NULL;
  76 static unsigned char *iqueue = NULL;
  77 
  78 static volatile int qhead = 0, qtail = 0, qlen = 0;
  79 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
  80 static volatile int seq_playing = 0;
  81 static int      sequencer_busy = 0;
  82 static int      output_treshold;
  83 static int      pre_event_timeout;
  84 static unsigned synth_open_mask;
  85 
  86 static int      seq_queue (unsigned char *note);
  87 static void     seq_startplay (void);
  88 static int      seq_sync (void);
  89 static void     seq_reset (void);
  90 static int      pmgr_present[MAX_SYNTH_DEV] =
  91 {0};
  92 
  93 #if MAX_SYNTH_DEV > 15
  94 #error Too many synthesizer devices enabled.
  95 #endif
  96 
  97 int
  98 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100   int             c = count, p = 0;
 101   int             ev_len;
 102   unsigned long   flags;
 103 
 104   dev = dev >> 4;
 105 
 106   ev_len = seq_mode == SEQ_1 ? 4 : 8;
 107 
 108   if (dev)                      /*
 109                                  * Patch manager device
 110                                  */
 111     return pmgr_read (dev - 1, file, buf, count);
 112 
 113   DISABLE_INTR (flags);
 114   if (!iqlen)
 115     {
 116       DO_SLEEP (midi_sleeper, midi_sleep_flag, pre_event_timeout);
 117 
 118       if (!iqlen)
 119         {
 120           RESTORE_INTR (flags);
 121           return 0;
 122         }
 123     }
 124 
 125   while (iqlen && c >= ev_len)
 126     {
 127 
 128       COPY_TO_USER (buf, p, &iqueue[iqhead * IEV_SZ], ev_len);
 129       p += ev_len;
 130       c -= ev_len;
 131 
 132       iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
 133       iqlen--;
 134     }
 135   RESTORE_INTR (flags);
 136 
 137   return count - c;
 138 }
 139 
 140 static void
 141 sequencer_midi_output (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143   /*
 144    * Currently NOP
 145    */
 146 }
 147 
 148 void
 149 seq_copy_to_input (unsigned char *event, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151   unsigned long   flags;
 152 
 153   /*
 154  * Verify that the len is valid for the current mode.
 155  */
 156 
 157   if (len != 4 && len != 8)
 158     return;
 159   if ((seq_mode == SEQ_1) != (len == 4))
 160     return;
 161 
 162   if (iqlen >= (SEQ_MAX_QUEUE - 1))
 163     return;                     /* Overflow */
 164 
 165   DISABLE_INTR (flags);
 166   memcpy (&iqueue[iqtail * IEV_SZ], event, len);
 167   iqlen++;
 168   iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
 169 
 170   if (SOMEONE_WAITING (midi_sleeper, midi_sleep_flag))
 171     {
 172       WAKE_UP (midi_sleeper, midi_sleep_flag);
 173     }
 174   RESTORE_INTR (flags);
 175 }
 176 
 177 static void
 178 sequencer_midi_input (int dev, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180   unsigned int    tstamp;
 181   unsigned char   event[4];
 182 
 183   if (data == 0xfe)             /* Ignore active sensing */
 184     return;
 185 
 186   tstamp = GET_TIME () - seq_time;
 187   if (tstamp != prev_input_time)
 188     {
 189       tstamp = (tstamp << 8) | SEQ_WAIT;
 190 
 191       seq_copy_to_input ((unsigned char *) &tstamp, 4);
 192       prev_input_time = tstamp;
 193     }
 194 
 195   event[0] = SEQ_MIDIPUTC;
 196   event[1] = data;
 197   event[2] = dev;
 198   event[3] = 0;
 199 
 200   seq_copy_to_input (event, 4);
 201 }
 202 
 203 void
 204 seq_input_event (unsigned char *event, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206   unsigned long   this_time;
 207 
 208   if (seq_mode == SEQ_2)
 209     this_time = tmr->get_time (tmr_no);
 210   else
 211     this_time = GET_TIME () - seq_time;
 212 
 213   if (this_time != prev_input_time)
 214     {
 215       unsigned char   tmp_event[8];
 216 
 217       tmp_event[0] = EV_TIMING;
 218       tmp_event[1] = TMR_WAIT_ABS;
 219       tmp_event[2] = 0;
 220       tmp_event[3] = 0;
 221       *(unsigned long *) &tmp_event[4] = this_time;
 222 
 223       seq_copy_to_input (tmp_event, 8);
 224       prev_input_time = this_time;
 225     }
 226 
 227   seq_copy_to_input (event, len);
 228 }
 229 
 230 int
 231 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233   unsigned char   event[EV_SZ], ev_code;
 234   int             p = 0, c, ev_size;
 235   int             err;
 236   int             mode = file->mode & O_ACCMODE;
 237 
 238   dev = dev >> 4;
 239 
 240   DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
 241 
 242   if (mode == OPEN_READ)
 243     return RET_ERROR (EIO);
 244 
 245   if (dev)                      /*
 246                                  * Patch manager device
 247                                  */
 248     return pmgr_write (dev - 1, file, buf, count);
 249 
 250   c = count;
 251 
 252   while (c >= 4)
 253     {
 254       COPY_FROM_USER (event, buf, p, 4);
 255       ev_code = event[0];
 256 
 257       if (ev_code == SEQ_FULLSIZE)
 258         {
 259           int             err;
 260 
 261           dev = *(unsigned short *) &event[2];
 262           if (dev < 0 || dev >= max_synthdev)
 263             return RET_ERROR (ENXIO);
 264 
 265           if (!(synth_open_mask & (1 << dev)))
 266             return RET_ERROR (ENXIO);
 267 
 268           err = synth_devs[dev]->load_patch (dev, *(short *) &event[0], buf, p + 4, c, 0);
 269           if (err < 0)
 270             return err;
 271 
 272           return err;
 273         }
 274 
 275       if (ev_code >= 128)
 276         {
 277           if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
 278             {
 279               printk ("Sequencer: Invalid level 2 event %x\n", ev_code);
 280               return RET_ERROR (EINVAL);
 281             }
 282 
 283           ev_size = 8;
 284 
 285           if (c < ev_size)
 286             {
 287               if (!seq_playing)
 288                 seq_startplay ();
 289               return count - c;
 290             }
 291 
 292           COPY_FROM_USER (&event[4], buf, p + 4, 4);
 293 
 294         }
 295       else
 296         {
 297           if (seq_mode == SEQ_2)
 298             {
 299               printk ("Sequencer: 4 byte event in level 2 mode\n");
 300               return RET_ERROR (EINVAL);
 301             }
 302           ev_size = 4;
 303         }
 304 
 305       if (event[0] == SEQ_MIDIPUTC)
 306         {
 307 
 308           if (!midi_opened[event[2]])
 309             {
 310               int             mode;
 311               int             dev = event[2];
 312 
 313               if (dev >= max_mididev)
 314                 {
 315                   printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
 316                   return RET_ERROR (ENXIO);
 317                 }
 318 
 319               mode = file->mode & O_ACCMODE;
 320 
 321               if ((err = midi_devs[dev]->open (dev, mode,
 322                           sequencer_midi_input, sequencer_midi_output)) < 0)
 323                 {
 324                   seq_reset ();
 325                   printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
 326                   return err;
 327                 }
 328 
 329               midi_opened[dev] = 1;
 330             }
 331 
 332         }
 333 
 334       if (!seq_queue (event))
 335         {
 336 
 337           if (!seq_playing)
 338             seq_startplay ();
 339           return count - c;
 340         }
 341 
 342       p += ev_size;
 343       c -= ev_size;
 344     }
 345 
 346   if (!seq_playing)
 347     seq_startplay ();
 348 
 349   return count;
 350 }
 351 
 352 static int
 353 seq_queue (unsigned char *note)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355 
 356   /*
 357    * Test if there is space in the queue
 358    */
 359 
 360   if (qlen >= SEQ_MAX_QUEUE)
 361     if (!seq_playing)
 362       seq_startplay ();         /*
 363                                  * Give chance to drain the queue
 364                                  */
 365 
 366   if (qlen >= SEQ_MAX_QUEUE && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 367     {
 368       /*
 369        * Sleep until there is enough space on the queue
 370        */
 371       DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
 372     }
 373 
 374   if (qlen >= SEQ_MAX_QUEUE)
 375     return 0;                   /*
 376                                  * To be sure
 377                                  */
 378 
 379   memcpy (&queue[qtail * EV_SZ], note, EV_SZ);
 380 
 381   qtail = (qtail + 1) % SEQ_MAX_QUEUE;
 382   qlen++;
 383 
 384   return 1;
 385 }
 386 
 387 static int
 388 extended_event (unsigned char *q)
     /* [previous][next][first][last][top][bottom][index][help] */
 389 {
 390   int             dev = q[2];
 391 
 392   if (dev < 0 || dev >= max_synthdev)
 393     return RET_ERROR (ENXIO);
 394 
 395   if (!(synth_open_mask & (1 << dev)))
 396     return RET_ERROR (ENXIO);
 397 
 398   switch (q[1])
 399     {
 400     case SEQ_NOTEOFF:
 401       synth_devs[dev]->kill_note (dev, q[3], q[4], q[5]);
 402       break;
 403 
 404     case SEQ_NOTEON:
 405       if (q[4] > 127 && q[4] != 255)
 406         return 0;
 407 
 408       synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
 409       break;
 410 
 411     case SEQ_PGMCHANGE:
 412       synth_devs[dev]->set_instr (dev, q[3], q[4]);
 413       break;
 414 
 415     case SEQ_AFTERTOUCH:
 416       synth_devs[dev]->aftertouch (dev, q[3], q[4]);
 417       break;
 418 
 419     case SEQ_BALANCE:
 420       synth_devs[dev]->panning (dev, q[3], (char) q[4]);
 421       break;
 422 
 423     case SEQ_CONTROLLER:
 424       synth_devs[dev]->controller (dev, q[3], q[4], *(short *) &q[5]);
 425       break;
 426 
 427     case SEQ_VOLMODE:
 428       if (synth_devs[dev]->volume_method != NULL)
 429         synth_devs[dev]->volume_method (dev, q[3]);
 430       break;
 431 
 432     default:
 433       return RET_ERROR (EINVAL);
 434     }
 435 
 436   return 0;
 437 }
 438 
 439 static int
 440 find_voice (int dev, int chn, int note)
     /* [previous][next][first][last][top][bottom][index][help] */
 441 {
 442   unsigned short  key;
 443   int             i;
 444 
 445   key = (chn << 8) | (note + 1);
 446 
 447   for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
 448     if (synth_devs[dev]->alloc.map[i] == key)
 449       return i;
 450 
 451   return -1;
 452 }
 453 
 454 static int
 455 alloc_voice (int dev, int chn, int note)
     /* [previous][next][first][last][top][bottom][index][help] */
 456 {
 457   unsigned short  key;
 458   int             voice;
 459 
 460   key = (chn << 8) | (note + 1);
 461 
 462   voice = synth_devs[dev]->alloc_voice (dev, chn, note,
 463                                         &synth_devs[dev]->alloc);
 464   synth_devs[dev]->alloc.map[voice] = key;
 465   return voice;
 466 }
 467 
 468 static void
 469 seq_chn_voice_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 470 {
 471   unsigned char   dev = event[1];
 472   unsigned char   cmd = event[2];
 473   unsigned char   chn = event[3];
 474   unsigned char   note = event[4];
 475   unsigned char   parm = event[5];
 476   int             voice = -1;
 477 
 478   if ((int) dev > max_synthdev)
 479     return;
 480   if (!(synth_open_mask & (1 << dev)))
 481     return;
 482   if (!synth_devs[dev])
 483     return;
 484 
 485   if (seq_mode == SEQ_2)
 486     {
 487       if (synth_devs[dev]->alloc_voice)
 488         voice = find_voice (dev, chn, note);
 489 
 490       if (cmd == MIDI_NOTEON && parm == 0)
 491         {
 492           cmd = MIDI_NOTEOFF;
 493           parm = 64;
 494         }
 495     }
 496 
 497   switch (cmd)
 498     {
 499     case MIDI_NOTEON:
 500       if (note > 127 && note != 255)
 501         return;
 502 
 503       if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
 504         {
 505           voice = alloc_voice (dev, chn, note);
 506         }
 507 
 508       if (voice == -1)
 509         voice = chn;
 510 
 511       if (seq_mode == SEQ_2)
 512         {
 513           synth_devs[dev]->set_instr (dev, voice,
 514                                     synth_devs[dev]->chn_info[chn].pgm_num);
 515         }
 516 
 517       synth_devs[dev]->start_note (dev, voice, note, parm);
 518       break;
 519 
 520     case MIDI_NOTEOFF:
 521       if (voice == -1)
 522         voice = chn;
 523       synth_devs[dev]->kill_note (dev, voice, note, parm);
 524       break;
 525 
 526     case MIDI_KEY_PRESSURE:
 527       /* To be implemented */
 528       break;
 529 
 530     default:;
 531     }
 532 }
 533 
 534 static void
 535 seq_chn_common_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 536 {
 537   unsigned char   dev = event[1];
 538   unsigned char   cmd = event[2];
 539   unsigned char   chn = event[3];
 540   unsigned char   p1 = event[4];
 541 
 542   /* unsigned char   p2 = event[5]; */
 543   unsigned short  w14 = *(short *) &event[6];
 544 
 545   if ((int) dev > max_synthdev)
 546     return;
 547   if (!(synth_open_mask & (1 << dev)))
 548     return;
 549   if (!synth_devs[dev])
 550     return;
 551 
 552   switch (cmd)
 553     {
 554     case MIDI_PGM_CHANGE:
 555       if (seq_mode == SEQ_2)
 556         {
 557           synth_devs[dev]->chn_info[chn].pgm_num = p1;
 558         }
 559       else
 560         synth_devs[dev]->set_instr (dev, chn, p1);
 561       break;
 562 
 563     case MIDI_CTL_CHANGE:
 564       if (p1 == CTRL_MAIN_VOLUME)
 565         {
 566           w14 = (unsigned short) (((int) w14 * 16383) / 100);
 567           p1 = CTL_MAIN_VOLUME;
 568         }
 569       if (p1 == CTRL_EXPRESSION)
 570         {
 571           w14 *= 128;
 572           p1 = CTL_EXPRESSION;
 573         }
 574 
 575       if (seq_mode == SEQ_2)
 576         {
 577           if (chn > 15 || p1 > 127)
 578             break;
 579 
 580           synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0xff;
 581 
 582           if (dev < num_synths)
 583             {
 584               int             val = w14 & 0xff;
 585 
 586               if (p1 < 64)      /* Combine MSB and LSB */
 587                 {
 588                   val = ((synth_devs[dev]->
 589                           chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
 590                     | (synth_devs[dev]->
 591                        chn_info[chn].controllers[p1 | 32] & 0x7f);
 592                   p1 &= ~32;
 593                 }
 594               else
 595                 val = synth_devs[dev]->chn_info[chn].controllers[p1];
 596 
 597               synth_devs[dev]->controller (dev, chn, p1, val);
 598             }
 599           else
 600             synth_devs[dev]->controller (dev, chn, p1, w14);
 601         }
 602       else
 603         synth_devs[dev]->controller (dev, chn, p1, w14);
 604       break;
 605 
 606     case MIDI_PITCH_BEND:
 607       synth_devs[dev]->bender (dev, chn, w14);
 608       break;
 609 
 610     default:;
 611     }
 612 }
 613 
 614 static int
 615 seq_timing_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 616 {
 617   unsigned char   cmd = event[1];
 618   unsigned int    parm = *(int *) &event[4];
 619 
 620   if (seq_mode == SEQ_2)
 621     {
 622       int             ret;
 623 
 624       if ((ret = tmr->event (tmr_no, event)) == TIMER_ARMED)
 625         {
 626           if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 627             {
 628               unsigned long   flags;
 629 
 630               DISABLE_INTR (flags);
 631               if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 632                 {
 633                   WAKE_UP (seq_sleeper, seq_sleep_flag);
 634                 }
 635               RESTORE_INTR (flags);
 636             }
 637         }
 638       return ret;
 639     }
 640 
 641   switch (cmd)
 642     {
 643     case TMR_WAIT_REL:
 644       parm += prev_event_time;
 645 
 646       /*
 647  * NOTE!  No break here. Execution of TMR_WAIT_REL continues in the
 648  * next case (TMR_WAIT_ABS)
 649  */
 650 
 651     case TMR_WAIT_ABS:
 652       if (parm > 0)
 653         {
 654           long            time;
 655 
 656           seq_playing = 1;
 657           time = parm;
 658           prev_event_time = time;
 659 
 660           request_sound_timer (time);
 661 
 662           if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 663             {
 664               unsigned long   flags;
 665 
 666               DISABLE_INTR (flags);
 667               if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 668                 {
 669                   WAKE_UP (seq_sleeper, seq_sleep_flag);
 670                 }
 671               RESTORE_INTR (flags);
 672             }
 673 
 674           return TIMER_ARMED;
 675         }
 676       break;
 677 
 678     case TMR_START:
 679       seq_time = GET_TIME ();
 680       prev_input_time = 0;
 681       prev_event_time = 0;
 682       break;
 683 
 684     case TMR_STOP:
 685       break;
 686 
 687     case TMR_CONTINUE:
 688       break;
 689 
 690     case TMR_TEMPO:
 691       break;
 692 
 693     case TMR_ECHO:
 694       if (seq_mode == SEQ_2)
 695         seq_copy_to_input (event, 8);
 696       else
 697         {
 698           parm = (parm << 8 | SEQ_ECHO);
 699           seq_copy_to_input ((unsigned char *) &parm, 4);
 700         }
 701       break;
 702 
 703     default:;
 704     }
 705 
 706   return TIMER_NOT_ARMED;
 707 }
 708 
 709 static void
 710 seq_local_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 711 {
 712   /* unsigned char   cmd = event[1]; */
 713 
 714   printk ("seq_local_event() called. WHY????????\n");
 715 }
 716 
 717 static void
 718 seq_startplay (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 719 {
 720   int             this_one;
 721   unsigned long  *delay;
 722   unsigned char  *q;
 723 
 724   while (qlen > 0)
 725     {
 726 
 727       seq_playing = 1;
 728 
 729       qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
 730       qlen--;
 731 
 732       q = &queue[this_one * EV_SZ];
 733 
 734       switch (q[0])
 735         {
 736         case SEQ_NOTEOFF:
 737           if (synth_open_mask & (1 << 0))
 738             if (synth_devs[0])
 739               synth_devs[0]->kill_note (0, q[1], 255, q[3]);
 740           break;
 741 
 742         case SEQ_NOTEON:
 743           if (q[4] < 128 || q[4] == 255)
 744             if (synth_open_mask & (1 << 0))
 745               if (synth_devs[0])
 746                 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
 747           break;
 748 
 749         case SEQ_WAIT:
 750           delay = (unsigned long *) q;  /*
 751                                          * Bytes 1 to 3 are containing the *
 752                                          * delay in GET_TIME()
 753                                          */
 754           *delay = (*delay >> 8) & 0xffffff;
 755 
 756           if (*delay > 0)
 757             {
 758               long            time;
 759 
 760               seq_playing = 1;
 761               time = *delay;
 762               prev_event_time = time;
 763 
 764               request_sound_timer (time);
 765 
 766               if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 767                 {
 768                   unsigned long   flags;
 769 
 770                   DISABLE_INTR (flags);
 771                   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 772                     {
 773                       WAKE_UP (seq_sleeper, seq_sleep_flag);
 774                     }
 775                   RESTORE_INTR (flags);
 776                 }
 777               /*
 778  * The timer is now active and will reinvoke this function
 779  * after the timer expires. Return to the caller now.
 780  */
 781               return;
 782             }
 783           break;
 784 
 785         case SEQ_PGMCHANGE:
 786           if (synth_open_mask & (1 << 0))
 787             if (synth_devs[0])
 788               synth_devs[0]->set_instr (0, q[1], q[2]);
 789           break;
 790 
 791         case SEQ_SYNCTIMER:     /*
 792                                  * Reset timer
 793                                  */
 794           seq_time = GET_TIME ();
 795           prev_input_time = 0;
 796           prev_event_time = 0;
 797           break;
 798 
 799         case SEQ_MIDIPUTC:      /*
 800                                  * Put a midi character
 801                                  */
 802           if (midi_opened[q[2]])
 803             {
 804               int             dev;
 805 
 806               dev = q[2];
 807 
 808               if (!midi_devs[dev]->putc (dev, q[1]))
 809                 {
 810                   /*
 811                    * Output FIFO is full. Wait one timer cycle and try again.
 812                    */
 813 
 814                   qlen++;
 815                   qhead = this_one;     /*
 816                                          * Restore queue
 817                                          */
 818                   seq_playing = 1;
 819                   request_sound_timer (-1);
 820                   return;
 821                 }
 822               else
 823                 midi_written[dev] = 1;
 824             }
 825           break;
 826 
 827         case SEQ_ECHO:
 828           seq_copy_to_input (q, 4);     /*
 829                                            * Echo back to the process
 830                                          */
 831           break;
 832 
 833         case SEQ_PRIVATE:
 834           if ((int) q[1] < max_synthdev)
 835             synth_devs[q[1]]->hw_control (q[1], q);
 836           break;
 837 
 838         case SEQ_EXTENDED:
 839           extended_event (q);
 840           break;
 841 
 842         case EV_CHN_VOICE:
 843           seq_chn_voice_event (q);
 844           break;
 845 
 846         case EV_CHN_COMMON:
 847           seq_chn_common_event (q);
 848           break;
 849 
 850         case EV_TIMING:
 851           if (seq_timing_event (q) == TIMER_ARMED)
 852             {
 853               return;
 854             }
 855           break;
 856 
 857         case EV_SEQ_LOCAL:
 858           seq_local_event (q);
 859           break;
 860 
 861         default:;
 862         }
 863 
 864     }
 865 
 866   seq_playing = 0;
 867 
 868   if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 869     {
 870       unsigned long   flags;
 871 
 872       DISABLE_INTR (flags);
 873       if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 874         {
 875           WAKE_UP (seq_sleeper, seq_sleep_flag);
 876         }
 877       RESTORE_INTR (flags);
 878     }
 879 
 880 }
 881 
 882 static void
 883 reset_controllers (int dev, unsigned char *controller, int update_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 884 {
 885 #include "midi_ctrl.h"
 886 
 887   int             i;
 888 
 889   for (i = 0; i < 128; i++)
 890     controller[i] = ctrl_def_values[i];
 891 }
 892 
 893 static void
 894 setup_mode2 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 895 {
 896   int             dev;
 897 
 898   max_synthdev = num_synths;
 899 
 900   for (dev = 0; dev < num_midis; dev++)
 901     if (midi_devs[dev]->converter != NULL)
 902       {
 903         synth_devs[max_synthdev++] =
 904           midi_devs[dev]->converter;
 905       }
 906 
 907   for (dev = 0; dev < max_synthdev; dev++)
 908     {
 909       int             chn;
 910 
 911       for (chn = 0; chn < 16; chn++)
 912         {
 913           synth_devs[dev]->chn_info[chn].pgm_num = 0;
 914           reset_controllers (dev,
 915                              synth_devs[dev]->chn_info[chn].controllers,
 916                              0);
 917         }
 918     }
 919 
 920   max_mididev = 0;
 921   seq_mode = SEQ_2;
 922 }
 923 
 924 int
 925 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 926 {
 927   int             retval, mode, i;
 928   int             level, tmp;
 929 
 930   level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
 931 
 932   dev = dev >> 4;
 933   mode = file->mode & O_ACCMODE;
 934 
 935   DEB (printk ("sequencer_open(dev=%d)\n", dev));
 936 
 937   if (!sequencer_ok)
 938     {
 939       printk ("Soundcard: Sequencer not initialized\n");
 940       return RET_ERROR (ENXIO);
 941     }
 942 
 943   if (dev)                      /*
 944                                  * Patch manager device
 945                                  */
 946     {
 947       int             err;
 948 
 949       dev--;
 950 
 951       if (dev >= MAX_SYNTH_DEV)
 952         return RET_ERROR (ENXIO);
 953       if (pmgr_present[dev])
 954         return RET_ERROR (EBUSY);
 955       if ((err = pmgr_open (dev)) < 0)
 956         return err;             /*
 957                                  * Failed
 958                                  */
 959 
 960       pmgr_present[dev] = 1;
 961       return err;
 962     }
 963 
 964   if (sequencer_busy)
 965     {
 966       printk ("Sequencer busy\n");
 967       return RET_ERROR (EBUSY);
 968     }
 969 
 970   max_mididev = num_midis;
 971   max_synthdev = num_synths;
 972   pre_event_timeout = 0;
 973   seq_mode = SEQ_1;
 974 
 975   if (pending_timer != -1)
 976     {
 977       tmr_no = pending_timer;
 978       pending_timer = -1;
 979     }
 980 
 981   if (tmr_no == -1)             /* Not selected yet */
 982     {
 983       int             i, best;
 984 
 985       best = -1;
 986       for (i = 0; i < num_sound_timers; i++)
 987         if (sound_timer_devs[i]->priority > best)
 988           {
 989             tmr_no = i;
 990             best = sound_timer_devs[i]->priority;
 991           }
 992 
 993       if (tmr_no == -1)         /* Should not be */
 994         tmr_no = 0;
 995     }
 996 
 997   tmr = sound_timer_devs[tmr_no];
 998 
 999   if (level == 2)
1000     {
1001       printk ("Using timer #%d\n", tmr_no);
1002       if (tmr == NULL)
1003         {
1004           printk ("sequencer: No timer for level 2\n");
1005           return RET_ERROR (ENXIO);
1006         }
1007       setup_mode2 ();
1008     }
1009 
1010   if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1011     if (!max_mididev)
1012       {
1013         printk ("Sequencer: No Midi devices. Input not possible\n");
1014         return RET_ERROR (ENXIO);
1015       }
1016 
1017   if (!max_synthdev && !max_mididev)
1018     return RET_ERROR (ENXIO);
1019 
1020   synth_open_mask = 0;
1021 
1022   for (i = 0; i < max_mididev; i++)
1023     {
1024       midi_opened[i] = 0;
1025       midi_written[i] = 0;
1026     }
1027 
1028   /*
1029    * if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
1030    */
1031   for (i = 0; i < max_synthdev; i++)    /*
1032                                          * Open synth devices
1033                                          */
1034     if ((tmp = synth_devs[i]->open (i, mode)) < 0)
1035       {
1036         printk ("Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1037         if (synth_devs[i]->midi_dev)
1038           printk ("(Maps to midi dev #%d\n", synth_devs[i]->midi_dev);
1039       }
1040     else
1041       {
1042         synth_open_mask |= (1 << i);
1043         if (synth_devs[i]->midi_dev)    /*
1044                                          * Is a midi interface
1045                                          */
1046           midi_opened[synth_devs[i]->midi_dev] = 1;
1047       }
1048 
1049   seq_time = GET_TIME ();
1050   prev_input_time = 0;
1051   prev_event_time = 0;
1052 
1053   if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1054     {                           /*
1055                                  * Initialize midi input devices
1056                                  */
1057       for (i = 0; i < max_mididev; i++)
1058         if (!midi_opened[i])
1059           {
1060             if ((retval = midi_devs[i]->open (i, mode,
1061                          sequencer_midi_input, sequencer_midi_output)) >= 0)
1062               midi_opened[i] = 1;
1063           }
1064     }
1065 
1066   if (seq_mode == SEQ_2)
1067     {
1068       tmr->open (tmr_no, seq_mode);
1069     }
1070 
1071   sequencer_busy = 1;
1072   RESET_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
1073   RESET_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
1074   output_treshold = SEQ_MAX_QUEUE / 2;
1075 
1076   for (i = 0; i < num_synths; i++)
1077     if (pmgr_present[i])
1078       pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
1079 
1080   return 0;
1081 }
1082 
1083 void
1084 seq_drain_midi_queues (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1085 {
1086   int             i, n;
1087 
1088   /*
1089    * Give the Midi drivers time to drain their output queues
1090    */
1091 
1092   n = 1;
1093 
1094   while (!PROCESS_ABORTING (midi_sleeper, midi_sleep_flag) && n)
1095     {
1096       n = 0;
1097 
1098       for (i = 0; i < max_mididev; i++)
1099         if (midi_opened[i] && midi_written[i])
1100           if (midi_devs[i]->buffer_status != NULL)
1101             if (midi_devs[i]->buffer_status (i))
1102               n++;
1103 
1104       /*
1105        * Let's have a delay
1106        */
1107       if (n)
1108         {
1109           DO_SLEEP (seq_sleeper, seq_sleep_flag, HZ / 10);
1110         }
1111     }
1112 }
1113 
1114 void
1115 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1116 {
1117   int             i;
1118   int             mode = file->mode & O_ACCMODE;
1119 
1120   dev = dev >> 4;
1121 
1122   DEB (printk ("sequencer_release(dev=%d)\n", dev));
1123 
1124   if (dev)                      /*
1125                                  * Patch manager device
1126                                  */
1127     {
1128       dev--;
1129       pmgr_release (dev);
1130       pmgr_present[dev] = 0;
1131       return;
1132     }
1133 
1134   /*
1135    * * Wait until the queue is empty
1136    */
1137 
1138   if (mode != OPEN_READ)
1139     while (!PROCESS_ABORTING (seq_sleeper, seq_sleep_flag) && qlen)
1140       {
1141         seq_sync ();
1142       }
1143 
1144   if (mode != OPEN_READ)
1145     seq_drain_midi_queues ();   /*
1146                                  * Ensure the output queues are empty
1147                                  */
1148   seq_reset ();
1149   if (mode != OPEN_READ)
1150     seq_drain_midi_queues ();   /*
1151                                  * Flush the all notes off messages
1152                                  */
1153 
1154   for (i = 0; i < max_synthdev; i++)
1155     if (synth_open_mask & (1 << i))     /*
1156                                          * Actually opened
1157                                          */
1158       if (synth_devs[i])
1159         {
1160           synth_devs[i]->close (i);
1161 
1162           if (synth_devs[i]->midi_dev)
1163             midi_opened[synth_devs[i]->midi_dev] = 0;
1164         }
1165 
1166   for (i = 0; i < num_synths; i++)
1167     if (pmgr_present[i])
1168       pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
1169 
1170   for (i = 0; i < max_mididev; i++)
1171     if (midi_opened[i])
1172       midi_devs[i]->close (i);
1173 
1174   if (seq_mode == SEQ_2)
1175     tmr->close (tmr_no);
1176 
1177   sequencer_busy = 0;
1178 }
1179 
1180 static int
1181 seq_sync (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1182 {
1183   if (qlen && !seq_playing && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1184     seq_startplay ();
1185 
1186   if (qlen && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))   /*
1187                                                                  * Queue not
1188                                                                  * empty
1189                                                                  */
1190     {
1191       DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
1192     }
1193 
1194   return qlen;
1195 }
1196 
1197 static void
1198 midi_outc (int dev, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200   /*
1201    * NOTE! Calls sleep(). Don't call this from interrupt.
1202    */
1203 
1204   int             n;
1205 
1206   /*
1207    * This routine sends one byte to the Midi channel.
1208    */
1209   /*
1210    * If the output Fifo is full, it waits until there
1211    */
1212   /*
1213    * is space in the queue
1214    */
1215 
1216   n = 300;                      /*
1217                                  * Timeout in jiffies
1218                                  */
1219 
1220   while (n && !midi_devs[dev]->putc (dev, data))
1221     {
1222       DO_SLEEP (seq_sleeper, seq_sleep_flag, 4);
1223       n--;
1224     }
1225 }
1226 
1227 static void
1228 seq_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1229 {
1230   /*
1231    * NOTE! Calls sleep(). Don't call this from interrupt.
1232    */
1233 
1234   int             i;
1235 
1236   int             chn;
1237 
1238   sound_stop_timer ();
1239   seq_time = GET_TIME ();
1240   prev_input_time = 0;
1241   prev_event_time = 0;
1242 
1243   qlen = qhead = qtail = 0;
1244   iqlen = iqhead = iqtail = 0;
1245 
1246   for (i = 0; i < max_synthdev; i++)
1247     if (synth_open_mask & (1 << i))
1248       if (synth_devs[i])
1249         synth_devs[i]->reset (i);
1250 
1251   if (seq_mode == SEQ_2)
1252     {
1253       for (i = 0; i < max_synthdev; i++)
1254         if (synth_open_mask & (1 << i))
1255           if (synth_devs[i])
1256             for (chn = 0; chn < 16; chn++)
1257               synth_devs[i]->controller (i, chn, 0xfe, 0);      /* All notes off */
1258     }
1259   else
1260     {
1261       for (i = 0; i < max_mididev; i++)
1262         if (midi_written[i])    /*
1263                                  * Midi used. Some notes may still be playing
1264                                  */
1265           {
1266             /*
1267  *    Sending just a ACTIVE SENSING message should be enough to stop all
1268  *      playing notes. Since there are devices not recognizing the
1269  *      active sensing, we have to send some all notes off messages also.
1270  */
1271             midi_outc (i, 0xfe);
1272 
1273             for (chn = 0; chn < 16; chn++)
1274               {
1275                 midi_outc (i,
1276                            (unsigned char) (0xb0 + (chn & 0xff)));      /*
1277                                                                  * Channel
1278                                                                  * msg
1279                                                                  */
1280                 midi_outc (i, 0x7b);    /*
1281                                          * All notes off
1282                                          */
1283                 midi_outc (i, 0);       /*
1284                                  * Dummy parameter
1285                                  */
1286               }
1287 
1288             midi_devs[i]->close (i);
1289 
1290             midi_written[i] = 0;
1291             midi_opened[i] = 0;
1292           }
1293     }
1294 
1295   seq_playing = 0;
1296 
1297   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
1298     printk ("Sequencer Warning: Unexpected sleeping process\n");
1299 
1300 }
1301 
1302 static void
1303 seq_panic (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1304 {
1305   /*
1306  * This routine is called by the application in case the user
1307  * wants to reset the system to the default state.
1308  */
1309 
1310   seq_reset ();
1311 
1312   /*
1313  * Since some of the devices don't recognize the active sensing and
1314  * all notes off messages, we have to shut all notes manually.
1315  *
1316  *      TO BE IMPLEMENTED LATER
1317  */
1318 
1319   /*
1320  * Also return the controllers to their default states
1321  */
1322 }
1323 
1324 int
1325 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1326                  unsigned int cmd, unsigned int arg)
1327 {
1328   int             midi_dev, orig_dev;
1329   int             mode = file->mode & O_ACCMODE;
1330 
1331   orig_dev = dev = dev >> 4;
1332 
1333   switch (cmd)
1334     {
1335     case SNDCTL_TMR_TIMEBASE:
1336     case SNDCTL_TMR_TEMPO:
1337     case SNDCTL_TMR_START:
1338     case SNDCTL_TMR_STOP:
1339     case SNDCTL_TMR_CONTINUE:
1340     case SNDCTL_TMR_METRONOME:
1341     case SNDCTL_TMR_SOURCE:
1342       if (dev)                  /* Patch manager */
1343         return RET_ERROR (EIO);
1344 
1345       if (seq_mode != SEQ_2)
1346         return RET_ERROR (EINVAL);
1347       return tmr->ioctl (tmr_no, cmd, arg);
1348       break;
1349 
1350     case SNDCTL_TMR_SELECT:
1351       if (dev)                  /* Patch manager */
1352         return RET_ERROR (EIO);
1353 
1354       if (seq_mode != SEQ_2)
1355         return RET_ERROR (EINVAL);
1356       pending_timer = IOCTL_IN (arg);
1357 
1358       if (pending_timer < 0 || pending_timer >= num_sound_timers)
1359         {
1360           pending_timer = -1;
1361           return RET_ERROR (EINVAL);
1362         }
1363 
1364       return IOCTL_OUT (arg, pending_timer);
1365       break;
1366 
1367     case SNDCTL_SEQ_PANIC:
1368       seq_panic ();
1369       break;
1370 
1371     case SNDCTL_SEQ_SYNC:
1372       if (dev)                  /*
1373                                  * Patch manager
1374                                  */
1375         return RET_ERROR (EIO);
1376 
1377       if (mode == OPEN_READ)
1378         return 0;
1379       while (qlen && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1380         seq_sync ();
1381       if (qlen)
1382         return RET_ERROR (EINTR);
1383       else
1384         return 0;
1385       break;
1386 
1387     case SNDCTL_SEQ_RESET:
1388       if (dev)                  /*
1389                                  * Patch manager
1390                                  */
1391         return RET_ERROR (EIO);
1392 
1393       seq_reset ();
1394       return 0;
1395       break;
1396 
1397     case SNDCTL_SEQ_TESTMIDI:
1398       if (dev)                  /*
1399                                  * Patch manager
1400                                  */
1401         return RET_ERROR (EIO);
1402 
1403       midi_dev = IOCTL_IN (arg);
1404       if (midi_dev >= max_mididev)
1405         return RET_ERROR (ENXIO);
1406 
1407       if (!midi_opened[midi_dev])
1408         {
1409           int             err, mode;
1410 
1411           mode = file->mode & O_ACCMODE;
1412           if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
1413                                                 sequencer_midi_input,
1414                                                 sequencer_midi_output)) < 0)
1415             return err;
1416         }
1417 
1418       midi_opened[midi_dev] = 1;
1419 
1420       return 0;
1421       break;
1422 
1423     case SNDCTL_SEQ_GETINCOUNT:
1424       if (dev)                  /*
1425                                  * Patch manager
1426                                  */
1427         return RET_ERROR (EIO);
1428 
1429       if (mode == OPEN_WRITE)
1430         return 0;
1431       return IOCTL_OUT (arg, iqlen);
1432       break;
1433 
1434     case SNDCTL_SEQ_GETOUTCOUNT:
1435 
1436       if (mode == OPEN_READ)
1437         return 0;
1438       return IOCTL_OUT (arg, SEQ_MAX_QUEUE - qlen);
1439       break;
1440 
1441     case SNDCTL_SEQ_CTRLRATE:
1442       if (dev)                  /* Patch manager */
1443         return RET_ERROR (EIO);
1444 
1445       /*
1446        * If *arg == 0, just return the current rate
1447        */
1448       if (seq_mode == SEQ_2)
1449         return tmr->ioctl (tmr_no, cmd, arg);
1450 
1451       if (IOCTL_IN (arg) != 0)
1452         return RET_ERROR (EINVAL);
1453 
1454       return IOCTL_OUT (arg, HZ);
1455       break;
1456 
1457     case SNDCTL_SEQ_RESETSAMPLES:
1458       dev = IOCTL_IN (arg);
1459       if (dev < 0 || dev >= num_synths)
1460         return RET_ERROR (ENXIO);
1461 
1462       if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1463         return RET_ERROR (EBUSY);
1464 
1465       if (!orig_dev && pmgr_present[dev])
1466         pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1467 
1468       return synth_devs[dev]->ioctl (dev, cmd, arg);
1469       break;
1470 
1471     case SNDCTL_SEQ_NRSYNTHS:
1472       return IOCTL_OUT (arg, max_synthdev);
1473       break;
1474 
1475     case SNDCTL_SEQ_NRMIDIS:
1476       return IOCTL_OUT (arg, max_mididev);
1477       break;
1478 
1479     case SNDCTL_SYNTH_MEMAVL:
1480       {
1481         int             dev = IOCTL_IN (arg);
1482 
1483         if (dev < 0 || dev >= num_synths)
1484           return RET_ERROR (ENXIO);
1485 
1486         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1487           return RET_ERROR (EBUSY);
1488 
1489         return IOCTL_OUT (arg, synth_devs[dev]->ioctl (dev, cmd, arg));
1490       }
1491       break;
1492 
1493     case SNDCTL_FM_4OP_ENABLE:
1494       {
1495         int             dev = IOCTL_IN (arg);
1496 
1497         if (dev < 0 || dev >= num_synths)
1498           return RET_ERROR (ENXIO);
1499 
1500         if (!(synth_open_mask & (1 << dev)))
1501           return RET_ERROR (ENXIO);
1502 
1503         synth_devs[dev]->ioctl (dev, cmd, arg);
1504         return 0;
1505       }
1506       break;
1507 
1508     case SNDCTL_SYNTH_INFO:
1509       {
1510         struct synth_info inf;
1511         int             dev;
1512 
1513         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1514         dev = inf.device;
1515 
1516         if (dev < 0 || dev >= max_synthdev)
1517           return RET_ERROR (ENXIO);
1518 
1519         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1520           return RET_ERROR (EBUSY);
1521 
1522         return synth_devs[dev]->ioctl (dev, cmd, arg);
1523       }
1524       break;
1525 
1526     case SNDCTL_MIDI_INFO:
1527       {
1528         struct midi_info inf;
1529         int             dev;
1530 
1531         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1532         dev = inf.device;
1533 
1534         if (dev < 0 || dev >= max_mididev)
1535           return RET_ERROR (ENXIO);
1536 
1537         IOCTL_TO_USER ((char *) arg, 0, (char *) &(midi_devs[dev]->info), sizeof (inf));
1538         return 0;
1539       }
1540       break;
1541 
1542     case SNDCTL_PMGR_IFACE:
1543       {
1544         struct patmgr_info *inf;
1545         int             dev, err;
1546 
1547         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1548 
1549         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1550         dev = inf->device;
1551 
1552         if (dev < 0 || dev >= num_synths)
1553           {
1554             KERNEL_FREE (inf);
1555             return RET_ERROR (ENXIO);
1556           }
1557 
1558         if (!synth_devs[dev]->pmgr_interface)
1559           {
1560             KERNEL_FREE (inf);
1561             return RET_ERROR (ENXIO);
1562           }
1563 
1564         if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
1565           {
1566             KERNEL_FREE (inf);
1567             return err;
1568           }
1569 
1570         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1571         KERNEL_FREE (inf);
1572         return 0;
1573       }
1574       break;
1575 
1576     case SNDCTL_PMGR_ACCESS:
1577       {
1578         struct patmgr_info *inf;
1579         int             dev, err;
1580 
1581         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1582 
1583         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1584         dev = inf->device;
1585 
1586         if (dev < 0 || dev >= num_synths)
1587           {
1588             KERNEL_FREE (inf);
1589             return RET_ERROR (ENXIO);
1590           }
1591 
1592         if (!pmgr_present[dev])
1593           {
1594             KERNEL_FREE (inf);
1595             return RET_ERROR (ESRCH);
1596           }
1597 
1598         if ((err = pmgr_access (dev, inf)) < 0)
1599           {
1600             KERNEL_FREE (inf);
1601             return err;
1602           }
1603 
1604         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1605         KERNEL_FREE (inf);
1606         return 0;
1607       }
1608       break;
1609 
1610     case SNDCTL_SEQ_TRESHOLD:
1611       {
1612         int             tmp = IOCTL_IN (arg);
1613 
1614         if (dev)                /*
1615                                  * Patch manager
1616                                  */
1617           return RET_ERROR (EIO);
1618 
1619         if (tmp < 1)
1620           tmp = 1;
1621         if (tmp >= SEQ_MAX_QUEUE)
1622           tmp = SEQ_MAX_QUEUE - 1;
1623         output_treshold = tmp;
1624         return 0;
1625       }
1626       break;
1627 
1628     case SNDCTL_MIDI_PRETIME:
1629       {
1630         int             val = IOCTL_IN (arg);
1631 
1632         if (val < 0)
1633           val = 0;
1634 
1635         val = (HZ * val) / 10;
1636         pre_event_timeout = val;
1637         return IOCTL_OUT (arg, val);
1638       }
1639       break;
1640 
1641     default:
1642       if (dev)                  /*
1643                                  * Patch manager
1644                                  */
1645         return RET_ERROR (EIO);
1646 
1647       if (mode == OPEN_READ)
1648         return RET_ERROR (EIO);
1649 
1650       if (!synth_devs[0])
1651         return RET_ERROR (ENXIO);
1652       if (!(synth_open_mask & (1 << 0)))
1653         return RET_ERROR (ENXIO);
1654       return synth_devs[0]->ioctl (0, cmd, arg);
1655       break;
1656     }
1657 
1658   return RET_ERROR (EINVAL);
1659 }
1660 
1661 #ifdef ALLOW_SELECT
1662 int
1663 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1664 {
1665   unsigned long   flags;
1666 
1667   dev = dev >> 4;
1668 
1669   switch (sel_type)
1670     {
1671     case SEL_IN:
1672       if (!iqlen)
1673         {
1674           DISABLE_INTR (flags);
1675           midi_sleep_flag.mode = WK_SLEEP;
1676           select_wait (&midi_sleeper, wait);
1677           RESTORE_INTR (flags);
1678           return 0;
1679         }
1680       return 1;
1681       break;
1682 
1683     case SEL_OUT:
1684       if (qlen >= SEQ_MAX_QUEUE)
1685         {
1686           DISABLE_INTR (flags);
1687           seq_sleep_flag.mode = WK_SLEEP;
1688           select_wait (&seq_sleeper, wait);
1689           RESTORE_INTR (flags);
1690           return 0;
1691         }
1692       return 1;
1693       break;
1694 
1695     case SEL_EX:
1696       return 0;
1697     }
1698 
1699   return 0;
1700 }
1701 
1702 #endif
1703 
1704 void
1705 sequencer_timer (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1706 {
1707   seq_startplay ();
1708 }
1709 
1710 int
1711 note_to_freq (int note_num)
     /* [previous][next][first][last][top][bottom][index][help] */
1712 {
1713 
1714   /*
1715    * This routine converts a midi note to a frequency (multiplied by 1000)
1716    */
1717 
1718   int             note, octave, note_freq;
1719   int             notes[] =
1720   {
1721     261632, 277189, 293671, 311132, 329632, 349232,
1722     369998, 391998, 415306, 440000, 466162, 493880
1723   };
1724 
1725 #define BASE_OCTAVE     5
1726 
1727   octave = note_num / 12;
1728   note = note_num % 12;
1729 
1730   note_freq = notes[note];
1731 
1732   if (octave < BASE_OCTAVE)
1733     note_freq >>= (BASE_OCTAVE - octave);
1734   else if (octave > BASE_OCTAVE)
1735     note_freq <<= (octave - BASE_OCTAVE);
1736 
1737   /*
1738    * note_freq >>= 1;
1739    */
1740 
1741   return note_freq;
1742 }
1743 
1744 unsigned long
1745 compute_finetune (unsigned long base_freq, int bend, int range)
     /* [previous][next][first][last][top][bottom][index][help] */
1746 {
1747   unsigned long   amount;
1748   int             negative, semitones, cents, multiplier = 1;
1749 
1750   if (!bend)
1751     return base_freq;
1752   if (!range)
1753     return base_freq;
1754 
1755   if (!base_freq)
1756     return base_freq;
1757 
1758   if (range >= 8192)
1759     range = 8191;
1760 
1761   bend = bend * range / 8192;
1762   if (!bend)
1763     return base_freq;
1764 
1765   negative = bend < 0 ? 1 : 0;
1766 
1767   if (bend < 0)
1768     bend *= -1;
1769   if (bend > range)
1770     bend = range;
1771 
1772   /*
1773      if (bend > 2399)
1774      bend = 2399;
1775    */
1776   while (bend > 2399)
1777     {
1778       multiplier *= 4;
1779       bend -= 2400;
1780     }
1781 
1782   semitones = bend / 100;
1783   cents = bend % 100;
1784 
1785   amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
1786     / 10000;
1787 
1788   if (negative)
1789     return (base_freq * 10000) / amount;        /*
1790                                                  * Bend down
1791                                                  */
1792   else
1793     return (base_freq * amount) / 10000;        /*
1794                                                  * Bend up
1795                                                  */
1796 }
1797 
1798 
1799 long
1800 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1801 {
1802 
1803   sequencer_ok = 1;
1804   PERMANENT_MALLOC (unsigned char *, queue, SEQ_MAX_QUEUE * EV_SZ, mem_start);
1805   PERMANENT_MALLOC (unsigned char *, iqueue, SEQ_MAX_QUEUE * IEV_SZ, mem_start);
1806 
1807   return mem_start;
1808 }
1809 
1810 #else
1811 /*
1812  * Stub version
1813  */
1814 int
1815 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1816 {
1817   return RET_ERROR (EIO);
1818 }
1819 
1820 int
1821 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1822 {
1823   return RET_ERROR (EIO);
1824 }
1825 
1826 int
1827 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1828 {
1829   return RET_ERROR (ENXIO);
1830 }
1831 
1832 void
1833 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1834 {
1835 }
1836 int
1837 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1838                  unsigned int cmd, unsigned int arg)
1839 {
1840   return RET_ERROR (EIO);
1841 }
1842 
1843 int
1844 sequencer_lseek (int dev, struct fileinfo *file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
1845 {
1846   return RET_ERROR (EIO);
1847 }
1848 
1849 long
1850 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1851 {
1852   return mem_start;
1853 }
1854 
1855 int
1856 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1857 {
1858   return RET_ERROR (EIO);
1859 }
1860 
1861 #endif
1862 
1863 #endif

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