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 capabilites)
  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     if (synth_devs[dev]->alloc_voice)
 487       voice = find_voice (dev, chn, note);
 488 
 489   if (cmd == MIDI_NOTEON && parm == 0)
 490     {
 491       cmd = MIDI_NOTEOFF;
 492       parm = 64;
 493     }
 494 
 495   switch (cmd)
 496     {
 497     case MIDI_NOTEON:
 498       if (note > 127)
 499         return;
 500 
 501       if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
 502         {
 503           voice = alloc_voice (dev, chn, note);
 504         }
 505 
 506       if (voice == -1)
 507         voice = chn;
 508 
 509       if (seq_mode == SEQ_2)
 510         {
 511           synth_devs[dev]->set_instr (dev, voice,
 512                                     synth_devs[dev]->chn_info[chn].pgm_num);
 513         }
 514 
 515       synth_devs[dev]->start_note (dev, voice, note, parm);
 516       break;
 517 
 518     case MIDI_NOTEOFF:
 519       if (voice == -1)
 520         voice = chn;
 521       synth_devs[dev]->kill_note (dev, voice, note, parm);
 522       break;
 523 
 524     case MIDI_KEY_PRESSURE:
 525       /* To be implemented */
 526       break;
 527 
 528     default:;
 529     }
 530 }
 531 
 532 static void
 533 seq_chn_common_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 534 {
 535   unsigned char   dev = event[1];
 536   unsigned char   cmd = event[2];
 537   unsigned char   chn = event[3];
 538   unsigned char   p1 = event[4];
 539 
 540   /* unsigned char   p2 = event[5]; */
 541   unsigned short  w14 = *(short *) &event[6];
 542 
 543   if ((int) dev > max_synthdev)
 544     return;
 545   if (!(synth_open_mask & (1 << dev)))
 546     return;
 547   if (!synth_devs[dev])
 548     return;
 549 
 550   switch (cmd)
 551     {
 552     case MIDI_PGM_CHANGE:
 553       if (seq_mode == SEQ_2)
 554         {
 555           synth_devs[dev]->chn_info[chn].pgm_num = p1;
 556         }
 557       else
 558         synth_devs[dev]->set_instr (dev, chn, p1);
 559       break;
 560 
 561     case MIDI_CTL_CHANGE:
 562       if (p1 == CTRL_MAIN_VOLUME)
 563         {
 564           w14 = (unsigned short) (((int) w14 * 16383) / 100);
 565           p1 = CTL_MAIN_VOLUME;
 566         }
 567       if (p1 == CTRL_EXPRESSION)
 568         {
 569           w14 *= 128;
 570           p1 = CTL_EXPRESSION;
 571         }
 572 
 573       if (seq_mode == SEQ_2)
 574         {
 575           if (chn > 15 || p1 > 127)
 576             break;
 577 
 578           synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0xff;
 579 
 580           if (dev < num_synths)
 581             {
 582               int             val = w14 & 0xff;
 583 
 584               if (p1 < 64)      /* Combine MSB and LSB */
 585                 {
 586                   val = ((synth_devs[dev]->
 587                           chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
 588                     | (synth_devs[dev]->
 589                        chn_info[chn].controllers[p1 | 32] & 0x7f);
 590                   p1 &= ~32;
 591                 }
 592               else
 593                 val = synth_devs[dev]->chn_info[chn].controllers[p1];
 594 
 595               synth_devs[dev]->controller (dev, chn, p1, val);
 596             }
 597           else
 598             synth_devs[dev]->controller (dev, chn, p1, w14);
 599         }
 600       else
 601         synth_devs[dev]->controller (dev, chn, p1, w14);
 602       break;
 603 
 604     case MIDI_PITCH_BEND:
 605       synth_devs[dev]->bender (dev, chn, w14);
 606       break;
 607 
 608     default:;
 609     }
 610 }
 611 
 612 static int
 613 seq_timing_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 614 {
 615   unsigned char   cmd = event[1];
 616   unsigned int    parm = *(int *) &event[4];
 617 
 618   if (seq_mode == SEQ_2)
 619     {
 620       int             ret;
 621 
 622       if ((ret = tmr->event (tmr_no, event)) == TIMER_ARMED)
 623         {
 624           if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 625             {
 626               unsigned long   flags;
 627 
 628               DISABLE_INTR (flags);
 629               if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 630                 {
 631                   WAKE_UP (seq_sleeper, seq_sleep_flag);
 632                 }
 633               RESTORE_INTR (flags);
 634             }
 635         }
 636       return ret;
 637     }
 638 
 639   switch (cmd)
 640     {
 641     case TMR_WAIT_REL:
 642       parm += prev_event_time;
 643 
 644       /*
 645  * NOTE!  No break here. Execution of TMR_WAIT_REL continues in the
 646  * next case (TMR_WAIT_ABS)
 647  */
 648 
 649     case TMR_WAIT_ABS:
 650       if (parm > 0)
 651         {
 652           long            time;
 653 
 654           seq_playing = 1;
 655           time = parm;
 656           prev_event_time = time;
 657 
 658           request_sound_timer (time);
 659 
 660           if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 661             {
 662               unsigned long   flags;
 663 
 664               DISABLE_INTR (flags);
 665               if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 666                 {
 667                   WAKE_UP (seq_sleeper, seq_sleep_flag);
 668                 }
 669               RESTORE_INTR (flags);
 670             }
 671 
 672           return TIMER_ARMED;
 673         }
 674       break;
 675 
 676     case TMR_START:
 677       seq_time = GET_TIME ();
 678       prev_input_time = 0;
 679       prev_event_time = 0;
 680       break;
 681 
 682     case TMR_STOP:
 683       break;
 684 
 685     case TMR_CONTINUE:
 686       break;
 687 
 688     case TMR_TEMPO:
 689       break;
 690 
 691     case TMR_ECHO:
 692       if (seq_mode == SEQ_2)
 693         seq_copy_to_input (event, 8);
 694       else
 695         {
 696           parm = (parm << 8 | SEQ_ECHO);
 697           seq_copy_to_input ((unsigned char *) &parm, 4);
 698         }
 699       break;
 700 
 701     default:;
 702     }
 703 
 704   return TIMER_NOT_ARMED;
 705 }
 706 
 707 static void
 708 seq_local_event (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710   /* unsigned char   cmd = event[1]; */
 711 
 712   printk ("seq_local_event() called. WHY????????\n");
 713 }
 714 
 715 static void
 716 seq_startplay (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 717 {
 718   int             this_one;
 719   unsigned long  *delay;
 720   unsigned char  *q;
 721 
 722   while (qlen > 0)
 723     {
 724 
 725       seq_playing = 1;
 726 
 727       qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
 728       qlen--;
 729 
 730       q = &queue[this_one * EV_SZ];
 731 
 732       switch (q[0])
 733         {
 734         case SEQ_NOTEOFF:
 735           if (synth_open_mask & (1 << 0))
 736             if (synth_devs[0])
 737               synth_devs[0]->kill_note (0, q[1], 255, q[3]);
 738           break;
 739 
 740         case SEQ_NOTEON:
 741           if (q[4] < 128 || q[4] == 255)
 742             if (synth_open_mask & (1 << 0))
 743               if (synth_devs[0])
 744                 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
 745           break;
 746 
 747         case SEQ_WAIT:
 748           delay = (unsigned long *) q;  /*
 749                                          * Bytes 1 to 3 are containing the *
 750                                          * delay in GET_TIME()
 751                                          */
 752           *delay = (*delay >> 8) & 0xffffff;
 753 
 754           if (*delay > 0)
 755             {
 756               long            time;
 757 
 758               seq_playing = 1;
 759               time = *delay;
 760               prev_event_time = time;
 761 
 762               request_sound_timer (time);
 763 
 764               if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 765                 {
 766                   unsigned long   flags;
 767 
 768                   DISABLE_INTR (flags);
 769                   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 770                     {
 771                       WAKE_UP (seq_sleeper, seq_sleep_flag);
 772                     }
 773                   RESTORE_INTR (flags);
 774                 }
 775               /*
 776  * The timer is now active and will reinvoke this function
 777  * after the timer expires. Return to the caller now.
 778  */
 779               return;
 780             }
 781           break;
 782 
 783         case SEQ_PGMCHANGE:
 784           if (synth_open_mask & (1 << 0))
 785             if (synth_devs[0])
 786               synth_devs[0]->set_instr (0, q[1], q[2]);
 787           break;
 788 
 789         case SEQ_SYNCTIMER:     /*
 790                                  * Reset timer
 791                                  */
 792           seq_time = GET_TIME ();
 793           prev_input_time = 0;
 794           prev_event_time = 0;
 795           break;
 796 
 797         case SEQ_MIDIPUTC:      /*
 798                                  * Put a midi character
 799                                  */
 800           if (midi_opened[q[2]])
 801             {
 802               int             dev;
 803 
 804               dev = q[2];
 805 
 806               if (!midi_devs[dev]->putc (dev, q[1]))
 807                 {
 808                   /*
 809                    * Output FIFO is full. Wait one timer cycle and try again.
 810                    */
 811 
 812                   qlen++;
 813                   qhead = this_one;     /*
 814                                          * Restore queue
 815                                          */
 816                   seq_playing = 1;
 817                   request_sound_timer (-1);
 818                   return;
 819                 }
 820               else
 821                 midi_written[dev] = 1;
 822             }
 823           break;
 824 
 825         case SEQ_ECHO:
 826           seq_copy_to_input (q, 4);     /*
 827                                            * Echo back to the process
 828                                          */
 829           break;
 830 
 831         case SEQ_PRIVATE:
 832           if ((int) q[1] < max_synthdev)
 833             synth_devs[q[1]]->hw_control (q[1], q);
 834           break;
 835 
 836         case SEQ_EXTENDED:
 837           extended_event (q);
 838           break;
 839 
 840         case EV_CHN_VOICE:
 841           seq_chn_voice_event (q);
 842           break;
 843 
 844         case EV_CHN_COMMON:
 845           seq_chn_common_event (q);
 846           break;
 847 
 848         case EV_TIMING:
 849           if (seq_timing_event (q) == TIMER_ARMED)
 850             {
 851               return;
 852             }
 853           break;
 854 
 855         case EV_SEQ_LOCAL:
 856           seq_local_event (q);
 857           break;
 858 
 859         default:;
 860         }
 861 
 862     }
 863 
 864   seq_playing = 0;
 865 
 866   if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 867     {
 868       unsigned long   flags;
 869 
 870       DISABLE_INTR (flags);
 871       if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 872         {
 873           WAKE_UP (seq_sleeper, seq_sleep_flag);
 874         }
 875       RESTORE_INTR (flags);
 876     }
 877 
 878 }
 879 
 880 static void
 881 reset_controllers (int dev, unsigned char *controller, int update_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 882 {
 883 #include "midi_ctrl.h"
 884 
 885   int             i;
 886 
 887   for (i = 0; i < 128; i++)
 888     controller[i] = ctrl_def_values[i];
 889 }
 890 
 891 static void
 892 setup_mode2 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 893 {
 894   int             dev;
 895 
 896   max_synthdev = num_synths;
 897 
 898   for (dev = 0; dev < num_midis; dev++)
 899     if (midi_devs[dev]->converter != NULL)
 900       {
 901         synth_devs[max_synthdev++] =
 902           midi_devs[dev]->converter;
 903       }
 904 
 905   for (dev = 0; dev < max_synthdev; dev++)
 906     {
 907       int             chn;
 908 
 909       for (chn = 0; chn < 16; chn++)
 910         {
 911           synth_devs[dev]->chn_info[chn].pgm_num = 0;
 912           reset_controllers (dev,
 913                              synth_devs[dev]->chn_info[chn].controllers,
 914                              0);
 915         }
 916     }
 917 
 918   max_mididev = 0;
 919   seq_mode = SEQ_2;
 920 }
 921 
 922 int
 923 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925   int             retval, mode, i;
 926   int             level, tmp;
 927 
 928   level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
 929 
 930   dev = dev >> 4;
 931   mode = file->mode & O_ACCMODE;
 932 
 933   DEB (printk ("sequencer_open(dev=%d)\n", dev));
 934 
 935   if (!sequencer_ok)
 936     {
 937       printk ("Soundcard: Sequencer not initialized\n");
 938       return RET_ERROR (ENXIO);
 939     }
 940 
 941   if (dev)                      /*
 942                                  * Patch manager device
 943                                  */
 944     {
 945       int             err;
 946 
 947       dev--;
 948 
 949       if (dev >= MAX_SYNTH_DEV)
 950         return RET_ERROR (ENXIO);
 951       if (pmgr_present[dev])
 952         return RET_ERROR (EBUSY);
 953       if ((err = pmgr_open (dev)) < 0)
 954         return err;             /*
 955                                  * Failed
 956                                  */
 957 
 958       pmgr_present[dev] = 1;
 959       return err;
 960     }
 961 
 962   if (sequencer_busy)
 963     {
 964       printk ("Sequencer busy\n");
 965       return RET_ERROR (EBUSY);
 966     }
 967 
 968   max_mididev = num_midis;
 969   max_synthdev = num_synths;
 970   pre_event_timeout = 0;
 971   seq_mode = SEQ_1;
 972 
 973   if (pending_timer != -1)
 974     {
 975       tmr_no = pending_timer;
 976       pending_timer = -1;
 977     }
 978 
 979   if (tmr_no == -1)             /* Not selected yet */
 980     {
 981       int             i, best;
 982 
 983       best = -1;
 984       for (i = 0; i < num_sound_timers; i++)
 985         if (sound_timer_devs[i]->priority > best)
 986           {
 987             tmr_no = i;
 988             best = sound_timer_devs[i]->priority;
 989           }
 990 
 991       if (tmr_no == -1)         /* Should not be */
 992         tmr_no = 0;
 993     }
 994 
 995   tmr = sound_timer_devs[tmr_no];
 996 
 997   if (level == 2)
 998     {
 999       printk ("Using timer #%d\n", tmr_no);
1000       if (tmr == NULL)
1001         {
1002           printk ("sequencer: No timer for level 2\n");
1003           return RET_ERROR (ENXIO);
1004         }
1005       setup_mode2 ();
1006     }
1007 
1008   if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1009     if (!max_mididev)
1010       {
1011         printk ("Sequencer: No Midi devices. Input not possible\n");
1012         return RET_ERROR (ENXIO);
1013       }
1014 
1015   if (!max_synthdev && !max_mididev)
1016     return RET_ERROR (ENXIO);
1017 
1018   synth_open_mask = 0;
1019 
1020   for (i = 0; i < max_mididev; i++)
1021     {
1022       midi_opened[i] = 0;
1023       midi_written[i] = 0;
1024     }
1025 
1026   /*
1027    * if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
1028    */
1029   for (i = 0; i < max_synthdev; i++)    /*
1030                                          * Open synth devices
1031                                          */
1032     if ((tmp = synth_devs[i]->open (i, mode)) < 0)
1033       {
1034         printk ("Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1035         if (synth_devs[i]->midi_dev)
1036           printk ("(Maps to midi dev #%d\n", synth_devs[i]->midi_dev);
1037       }
1038     else
1039       {
1040         synth_open_mask |= (1 << i);
1041         if (synth_devs[i]->midi_dev)    /*
1042                                          * Is a midi interface
1043                                          */
1044           midi_opened[synth_devs[i]->midi_dev] = 1;
1045       }
1046 
1047   seq_time = GET_TIME ();
1048   prev_input_time = 0;
1049   prev_event_time = 0;
1050 
1051   if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1052     {                           /*
1053                                  * Initialize midi input devices
1054                                  */
1055       for (i = 0; i < max_mididev; i++)
1056         if (!midi_opened[i])
1057           {
1058             if ((retval = midi_devs[i]->open (i, mode,
1059                          sequencer_midi_input, sequencer_midi_output)) >= 0)
1060               midi_opened[i] = 1;
1061           }
1062     }
1063 
1064   if (seq_mode == SEQ_2)
1065     {
1066       tmr->open (tmr_no, seq_mode);
1067     }
1068 
1069   sequencer_busy = 1;
1070   RESET_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
1071   RESET_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
1072   output_treshold = SEQ_MAX_QUEUE / 2;
1073 
1074   for (i = 0; i < num_synths; i++)
1075     if (pmgr_present[i])
1076       pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
1077 
1078   return 0;
1079 }
1080 
1081 void
1082 seq_drain_midi_queues (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1083 {
1084   int             i, n;
1085 
1086   /*
1087    * Give the Midi drivers time to drain their output queues
1088    */
1089 
1090   n = 1;
1091 
1092   while (!PROCESS_ABORTING (midi_sleeper, midi_sleep_flag) && n)
1093     {
1094       n = 0;
1095 
1096       for (i = 0; i < max_mididev; i++)
1097         if (midi_opened[i] && midi_written[i])
1098           if (midi_devs[i]->buffer_status != NULL)
1099             if (midi_devs[i]->buffer_status (i))
1100               n++;
1101 
1102       /*
1103        * Let's have a delay
1104        */
1105       if (n)
1106         {
1107           DO_SLEEP (seq_sleeper, seq_sleep_flag, HZ / 10);
1108         }
1109     }
1110 }
1111 
1112 void
1113 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1114 {
1115   int             i;
1116   int             mode = file->mode & O_ACCMODE;
1117 
1118   dev = dev >> 4;
1119 
1120   DEB (printk ("sequencer_release(dev=%d)\n", dev));
1121 
1122   if (dev)                      /*
1123                                  * Patch manager device
1124                                  */
1125     {
1126       dev--;
1127       pmgr_release (dev);
1128       pmgr_present[dev] = 0;
1129       return;
1130     }
1131 
1132   /*
1133    * * Wait until the queue is empty
1134    */
1135 
1136   if (mode != OPEN_READ)
1137     while (!PROCESS_ABORTING (seq_sleeper, seq_sleep_flag) && qlen)
1138       {
1139         seq_sync ();
1140       }
1141 
1142   if (mode != OPEN_READ)
1143     seq_drain_midi_queues ();   /*
1144                                  * Ensure the output queues are empty
1145                                  */
1146   seq_reset ();
1147   if (mode != OPEN_READ)
1148     seq_drain_midi_queues ();   /*
1149                                  * Flush the all notes off messages
1150                                  */
1151 
1152   for (i = 0; i < max_synthdev; i++)
1153     if (synth_open_mask & (1 << i))     /*
1154                                          * Actually opened
1155                                          */
1156       if (synth_devs[i])
1157         {
1158           synth_devs[i]->close (i);
1159 
1160           if (synth_devs[i]->midi_dev)
1161             midi_opened[synth_devs[i]->midi_dev] = 0;
1162         }
1163 
1164   for (i = 0; i < num_synths; i++)
1165     if (pmgr_present[i])
1166       pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
1167 
1168   for (i = 0; i < max_mididev; i++)
1169     if (midi_opened[i])
1170       midi_devs[i]->close (i);
1171 
1172   if (seq_mode == SEQ_2)
1173     tmr->close (tmr_no);
1174 
1175   sequencer_busy = 0;
1176 }
1177 
1178 static int
1179 seq_sync (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181   if (qlen && !seq_playing && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1182     seq_startplay ();
1183 
1184   if (qlen && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))   /*
1185                                                                  * Queue not
1186                                                                  * empty
1187                                                                  */
1188     {
1189       DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
1190     }
1191 
1192   return qlen;
1193 }
1194 
1195 static void
1196 midi_outc (int dev, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
1197 {
1198   /*
1199    * NOTE! Calls sleep(). Don't call this from interrupt.
1200    */
1201 
1202   int             n;
1203 
1204   /*
1205    * This routine sends one byte to the Midi channel.
1206    */
1207   /*
1208    * If the output Fifo is full, it waits until there
1209    */
1210   /*
1211    * is space in the queue
1212    */
1213 
1214   n = 300;                      /*
1215                                  * Timeout in jiffies
1216                                  */
1217 
1218   while (n && !midi_devs[dev]->putc (dev, data))
1219     {
1220       DO_SLEEP (seq_sleeper, seq_sleep_flag, 4);
1221       n--;
1222     }
1223 }
1224 
1225 static void
1226 seq_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1227 {
1228   /*
1229    * NOTE! Calls sleep(). Don't call this from interrupt.
1230    */
1231 
1232   int             i;
1233 
1234   int             chn;
1235 
1236   sound_stop_timer ();
1237   seq_time = GET_TIME ();
1238   prev_input_time = 0;
1239   prev_event_time = 0;
1240 
1241   qlen = qhead = qtail = 0;
1242   iqlen = iqhead = iqtail = 0;
1243 
1244   for (i = 0; i < max_synthdev; i++)
1245     if (synth_open_mask & (1 << i))
1246       if (synth_devs[i])
1247         synth_devs[i]->reset (i);
1248 
1249   if (seq_mode == SEQ_2)
1250     {
1251       for (i = 0; i < max_synthdev; i++)
1252         if (synth_open_mask & (1 << i))
1253           if (synth_devs[i])
1254             for (chn = 0; chn < 16; chn++)
1255               synth_devs[i]->controller (i, chn, 0xfe, 0);      /* All notes off */
1256     }
1257   else
1258     {
1259       for (i = 0; i < max_mididev; i++)
1260         if (midi_written[i])    /*
1261                                  * Midi used. Some notes may still be playing
1262                                  */
1263           {
1264             /*
1265  *    Sending just a ACTIVE SENSING message should be enough to stop all
1266  *      playing notes. Since there are devices not recognizing the
1267  *      active sensing, we have to send some all notes off messages also.
1268  */
1269             midi_outc (i, 0xfe);
1270 
1271             for (chn = 0; chn < 16; chn++)
1272               {
1273                 midi_outc (i,
1274                            (unsigned char) (0xb0 + (chn & 0xff)));      /*
1275                                                                  * Channel
1276                                                                  * msg
1277                                                                  */
1278                 midi_outc (i, 0x7b);    /*
1279                                          * All notes off
1280                                          */
1281                 midi_outc (i, 0);       /*
1282                                  * Dummy parameter
1283                                  */
1284               }
1285 
1286             midi_devs[i]->close (i);
1287 
1288             midi_written[i] = 0;
1289             midi_opened[i] = 0;
1290           }
1291     }
1292 
1293   seq_playing = 0;
1294 
1295   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
1296     printk ("Sequencer Warning: Unexpected sleeping process\n");
1297 
1298 }
1299 
1300 static void
1301 seq_panic (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1302 {
1303   /*
1304  * This routine is called by the application in case the user
1305  * wants to reset the system to the default state.
1306  */
1307 
1308   seq_reset ();
1309 
1310   /*
1311  * Since some of the devices don't recognize the active sensing and
1312  * all notes off messages, we have to shut all notes manually.
1313  *
1314  *      TO BE IMPLEMENTED LATER
1315  */
1316 
1317   /*
1318  * Also return the controllers to their default states
1319  */
1320 }
1321 
1322 int
1323 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1324                  unsigned int cmd, unsigned int arg)
1325 {
1326   int             midi_dev, orig_dev;
1327   int             mode = file->mode & O_ACCMODE;
1328 
1329   orig_dev = dev = dev >> 4;
1330 
1331   switch (cmd)
1332     {
1333     case SNDCTL_TMR_TIMEBASE:
1334     case SNDCTL_TMR_TEMPO:
1335     case SNDCTL_TMR_START:
1336     case SNDCTL_TMR_STOP:
1337     case SNDCTL_TMR_CONTINUE:
1338     case SNDCTL_TMR_METRONOME:
1339     case SNDCTL_TMR_SOURCE:
1340       if (dev)                  /* Patch manager */
1341         return RET_ERROR (EIO);
1342 
1343       if (seq_mode != SEQ_2)
1344         return RET_ERROR (EINVAL);
1345       return tmr->ioctl (tmr_no, cmd, arg);
1346       break;
1347 
1348     case SNDCTL_TMR_SELECT:
1349       if (dev)                  /* Patch manager */
1350         return RET_ERROR (EIO);
1351 
1352       if (seq_mode != SEQ_2)
1353         return RET_ERROR (EINVAL);
1354       pending_timer = IOCTL_IN (arg);
1355 
1356       if (pending_timer < 0 || pending_timer >= num_sound_timers)
1357         {
1358           pending_timer = -1;
1359           return RET_ERROR (EINVAL);
1360         }
1361 
1362       return IOCTL_OUT (arg, pending_timer);
1363       break;
1364 
1365     case SNDCTL_SEQ_PANIC:
1366       seq_panic ();
1367       break;
1368 
1369     case SNDCTL_SEQ_SYNC:
1370       if (dev)                  /*
1371                                  * Patch manager
1372                                  */
1373         return RET_ERROR (EIO);
1374 
1375       if (mode == OPEN_READ)
1376         return 0;
1377       while (qlen && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1378         seq_sync ();
1379       if (qlen)
1380         return RET_ERROR (EINTR);
1381       else
1382         return 0;
1383       break;
1384 
1385     case SNDCTL_SEQ_RESET:
1386       if (dev)                  /*
1387                                  * Patch manager
1388                                  */
1389         return RET_ERROR (EIO);
1390 
1391       seq_reset ();
1392       return 0;
1393       break;
1394 
1395     case SNDCTL_SEQ_TESTMIDI:
1396       if (dev)                  /*
1397                                  * Patch manager
1398                                  */
1399         return RET_ERROR (EIO);
1400 
1401       midi_dev = IOCTL_IN (arg);
1402       if (midi_dev >= max_mididev)
1403         return RET_ERROR (ENXIO);
1404 
1405       if (!midi_opened[midi_dev])
1406         {
1407           int             err, mode;
1408 
1409           mode = file->mode & O_ACCMODE;
1410           if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
1411                                                 sequencer_midi_input,
1412                                                 sequencer_midi_output)) < 0)
1413             return err;
1414         }
1415 
1416       midi_opened[midi_dev] = 1;
1417 
1418       return 0;
1419       break;
1420 
1421     case SNDCTL_SEQ_GETINCOUNT:
1422       if (dev)                  /*
1423                                  * Patch manager
1424                                  */
1425         return RET_ERROR (EIO);
1426 
1427       if (mode == OPEN_WRITE)
1428         return 0;
1429       return IOCTL_OUT (arg, iqlen);
1430       break;
1431 
1432     case SNDCTL_SEQ_GETOUTCOUNT:
1433 
1434       if (mode == OPEN_READ)
1435         return 0;
1436       return IOCTL_OUT (arg, SEQ_MAX_QUEUE - qlen);
1437       break;
1438 
1439     case SNDCTL_SEQ_CTRLRATE:
1440       if (dev)                  /* Patch manager */
1441         return RET_ERROR (EIO);
1442 
1443       /*
1444        * If *arg == 0, just return the current rate
1445        */
1446       if (seq_mode == SEQ_2)
1447         return tmr->ioctl (tmr_no, cmd, arg);
1448 
1449       if (IOCTL_IN (arg) != 0)
1450         return RET_ERROR (EINVAL);
1451 
1452       return IOCTL_OUT (arg, HZ);
1453       break;
1454 
1455     case SNDCTL_SEQ_RESETSAMPLES:
1456       dev = IOCTL_IN (arg);
1457       if (dev < 0 || dev >= num_synths)
1458         return RET_ERROR (ENXIO);
1459 
1460       if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1461         return RET_ERROR (EBUSY);
1462 
1463       if (!orig_dev && pmgr_present[dev])
1464         pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1465 
1466       return synth_devs[dev]->ioctl (dev, cmd, arg);
1467       break;
1468 
1469     case SNDCTL_SEQ_NRSYNTHS:
1470       return IOCTL_OUT (arg, max_synthdev);
1471       break;
1472 
1473     case SNDCTL_SEQ_NRMIDIS:
1474       return IOCTL_OUT (arg, max_mididev);
1475       break;
1476 
1477     case SNDCTL_SYNTH_MEMAVL:
1478       {
1479         int             dev = IOCTL_IN (arg);
1480 
1481         if (dev < 0 || dev >= num_synths)
1482           return RET_ERROR (ENXIO);
1483 
1484         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1485           return RET_ERROR (EBUSY);
1486 
1487         return IOCTL_OUT (arg, synth_devs[dev]->ioctl (dev, cmd, arg));
1488       }
1489       break;
1490 
1491     case SNDCTL_FM_4OP_ENABLE:
1492       {
1493         int             dev = IOCTL_IN (arg);
1494 
1495         if (dev < 0 || dev >= num_synths)
1496           return RET_ERROR (ENXIO);
1497 
1498         if (!(synth_open_mask & (1 << dev)))
1499           return RET_ERROR (ENXIO);
1500 
1501         synth_devs[dev]->ioctl (dev, cmd, arg);
1502         return 0;
1503       }
1504       break;
1505 
1506     case SNDCTL_SYNTH_INFO:
1507       {
1508         struct synth_info inf;
1509         int             dev;
1510 
1511         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1512         dev = inf.device;
1513 
1514         if (dev < 0 || dev >= max_synthdev)
1515           return RET_ERROR (ENXIO);
1516 
1517         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1518           return RET_ERROR (EBUSY);
1519 
1520         return synth_devs[dev]->ioctl (dev, cmd, arg);
1521       }
1522       break;
1523 
1524     case SNDCTL_MIDI_INFO:
1525       {
1526         struct midi_info inf;
1527         int             dev;
1528 
1529         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1530         dev = inf.device;
1531 
1532         if (dev < 0 || dev >= max_mididev)
1533           return RET_ERROR (ENXIO);
1534 
1535         IOCTL_TO_USER ((char *) arg, 0, (char *) &(midi_devs[dev]->info), sizeof (inf));
1536         return 0;
1537       }
1538       break;
1539 
1540     case SNDCTL_PMGR_IFACE:
1541       {
1542         struct patmgr_info *inf;
1543         int             dev, err;
1544 
1545         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1546 
1547         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1548         dev = inf->device;
1549 
1550         if (dev < 0 || dev >= num_synths)
1551           {
1552             KERNEL_FREE (inf);
1553             return RET_ERROR (ENXIO);
1554           }
1555 
1556         if (!synth_devs[dev]->pmgr_interface)
1557           {
1558             KERNEL_FREE (inf);
1559             return RET_ERROR (ENXIO);
1560           }
1561 
1562         if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
1563           {
1564             KERNEL_FREE (inf);
1565             return err;
1566           }
1567 
1568         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1569         KERNEL_FREE (inf);
1570         return 0;
1571       }
1572       break;
1573 
1574     case SNDCTL_PMGR_ACCESS:
1575       {
1576         struct patmgr_info *inf;
1577         int             dev, err;
1578 
1579         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1580 
1581         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1582         dev = inf->device;
1583 
1584         if (dev < 0 || dev >= num_synths)
1585           {
1586             KERNEL_FREE (inf);
1587             return RET_ERROR (ENXIO);
1588           }
1589 
1590         if (!pmgr_present[dev])
1591           {
1592             KERNEL_FREE (inf);
1593             return RET_ERROR (ESRCH);
1594           }
1595 
1596         if ((err = pmgr_access (dev, inf)) < 0)
1597           {
1598             KERNEL_FREE (inf);
1599             return err;
1600           }
1601 
1602         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1603         KERNEL_FREE (inf);
1604         return 0;
1605       }
1606       break;
1607 
1608     case SNDCTL_SEQ_TRESHOLD:
1609       {
1610         int             tmp = IOCTL_IN (arg);
1611 
1612         if (dev)                /*
1613                                  * Patch manager
1614                                  */
1615           return RET_ERROR (EIO);
1616 
1617         if (tmp < 1)
1618           tmp = 1;
1619         if (tmp >= SEQ_MAX_QUEUE)
1620           tmp = SEQ_MAX_QUEUE - 1;
1621         output_treshold = tmp;
1622         return 0;
1623       }
1624       break;
1625 
1626     case SNDCTL_MIDI_PRETIME:
1627       {
1628         int             val = IOCTL_IN (arg);
1629 
1630         if (val < 0)
1631           val = 0;
1632 
1633         val = (HZ * val) / 10;
1634         pre_event_timeout = val;
1635         return IOCTL_OUT (arg, val);
1636       }
1637       break;
1638 
1639     default:
1640       if (dev)                  /*
1641                                  * Patch manager
1642                                  */
1643         return RET_ERROR (EIO);
1644 
1645       if (mode == OPEN_READ)
1646         return RET_ERROR (EIO);
1647 
1648       if (!synth_devs[0])
1649         return RET_ERROR (ENXIO);
1650       if (!(synth_open_mask & (1 << 0)))
1651         return RET_ERROR (ENXIO);
1652       return synth_devs[0]->ioctl (0, cmd, arg);
1653       break;
1654     }
1655 
1656   return RET_ERROR (EINVAL);
1657 }
1658 
1659 #ifdef ALLOW_SELECT
1660 int
1661 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1662 {
1663   unsigned long   flags;
1664 
1665   dev = dev >> 4;
1666 
1667   switch (sel_type)
1668     {
1669     case SEL_IN:
1670       if (!iqlen)
1671         {
1672           DISABLE_INTR (flags);
1673           midi_sleep_flag.mode = WK_SLEEP;
1674           select_wait (&midi_sleeper, wait);
1675           RESTORE_INTR (flags);
1676           return 0;
1677         }
1678       return 1;
1679       break;
1680 
1681     case SEL_OUT:
1682       if (qlen >= SEQ_MAX_QUEUE)
1683         {
1684           DISABLE_INTR (flags);
1685           seq_sleep_flag.mode = WK_SLEEP;
1686           select_wait (&seq_sleeper, wait);
1687           RESTORE_INTR (flags);
1688           return 0;
1689         }
1690       return 1;
1691       break;
1692 
1693     case SEL_EX:
1694       return 0;
1695     }
1696 
1697   return 0;
1698 }
1699 
1700 #endif
1701 
1702 void
1703 sequencer_timer (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1704 {
1705   seq_startplay ();
1706 }
1707 
1708 int
1709 note_to_freq (int note_num)
     /* [previous][next][first][last][top][bottom][index][help] */
1710 {
1711 
1712   /*
1713    * This routine converts a midi note to a frequency (multiplied by 1000)
1714    */
1715 
1716   int             note, octave, note_freq;
1717   int             notes[] =
1718   {
1719     261632, 277189, 293671, 311132, 329632, 349232,
1720     369998, 391998, 415306, 440000, 466162, 493880
1721   };
1722 
1723 #define BASE_OCTAVE     5
1724 
1725   octave = note_num / 12;
1726   note = note_num % 12;
1727 
1728   note_freq = notes[note];
1729 
1730   if (octave < BASE_OCTAVE)
1731     note_freq >>= (BASE_OCTAVE - octave);
1732   else if (octave > BASE_OCTAVE)
1733     note_freq <<= (octave - BASE_OCTAVE);
1734 
1735   /*
1736    * note_freq >>= 1;
1737    */
1738 
1739   return note_freq;
1740 }
1741 
1742 unsigned long
1743 compute_finetune (unsigned long base_freq, int bend, int range)
     /* [previous][next][first][last][top][bottom][index][help] */
1744 {
1745   unsigned long   amount;
1746   int             negative, semitones, cents, multiplier = 1;
1747 
1748   if (!bend)
1749     return base_freq;
1750   if (!range)
1751     return base_freq;
1752 
1753   if (!base_freq)
1754     return base_freq;
1755 
1756   if (range >= 8192)
1757     range = 8191;
1758 
1759   bend = bend * range / 8192;
1760   if (!bend)
1761     return base_freq;
1762 
1763   negative = bend < 0 ? 1 : 0;
1764 
1765   if (bend < 0)
1766     bend *= -1;
1767   if (bend > range)
1768     bend = range;
1769 
1770   /*
1771      if (bend > 2399)
1772      bend = 2399;
1773    */
1774   while (bend > 2399)
1775     {
1776       multiplier *= 4;
1777       bend -= 2400;
1778     }
1779 
1780   semitones = bend / 100;
1781   cents = bend % 100;
1782 
1783   amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
1784     / 10000;
1785 
1786   if (negative)
1787     return (base_freq * 10000) / amount;        /*
1788                                                  * Bend down
1789                                                  */
1790   else
1791     return (base_freq * amount) / 10000;        /*
1792                                                  * Bend up
1793                                                  */
1794 }
1795 
1796 
1797 long
1798 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1799 {
1800 
1801   sequencer_ok = 1;
1802   PERMANENT_MALLOC (unsigned char *, queue, SEQ_MAX_QUEUE * EV_SZ, mem_start);
1803   PERMANENT_MALLOC (unsigned char *, iqueue, SEQ_MAX_QUEUE * IEV_SZ, mem_start);
1804 
1805   return mem_start;
1806 }
1807 
1808 #else
1809 /*
1810  * Stub version
1811  */
1812 int
1813 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1814 {
1815   return RET_ERROR (EIO);
1816 }
1817 
1818 int
1819 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1820 {
1821   return RET_ERROR (EIO);
1822 }
1823 
1824 int
1825 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1826 {
1827   return RET_ERROR (ENXIO);
1828 }
1829 
1830 void
1831 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1832 {
1833 }
1834 int
1835 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1836                  unsigned int cmd, unsigned int arg)
1837 {
1838   return RET_ERROR (EIO);
1839 }
1840 
1841 int
1842 sequencer_lseek (int dev, struct fileinfo *file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
1843 {
1844   return RET_ERROR (EIO);
1845 }
1846 
1847 long
1848 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1849 {
1850   return mem_start;
1851 }
1852 
1853 int
1854 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1855 {
1856   return RET_ERROR (EIO);
1857 }
1858 
1859 #endif
1860 
1861 #endif

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