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

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