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

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