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. copy_to_input
  4. sequencer_midi_input
  5. sequencer_write
  6. seq_queue
  7. extended_event
  8. seq_startplay
  9. sequencer_open
  10. seq_drain_midi_queues
  11. sequencer_release
  12. seq_sync
  13. midi_outc
  14. seq_reset
  15. sequencer_ioctl
  16. sequencer_select
  17. sequencer_timer
  18. note_to_freq
  19. compute_finetune
  20. sequencer_init
  21. sequencer_read
  22. sequencer_write
  23. sequencer_open
  24. sequencer_release
  25. sequencer_ioctl
  26. sequencer_lseek
  27. sequencer_init
  28. sequencer_select

   1 /*
   2  * sound/sequencer.c
   3  *
   4  * The sequencer personality manager.
   5  *
   6  * Copyright by Hannu Savolainen 1993
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions are
  10  * met: 1. Redistributions of source code must retain the above copyright
  11  * notice, this list of conditions and the following disclaimer. 2.
  12  * Redistributions in binary form must reproduce the above copyright notice,
  13  * this list of conditions and the following disclaimer in the documentation
  14  * and/or other materials provided with the distribution.
  15  *
  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26  * SUCH DAMAGE.
  27  *
  28  */
  29 
  30 #define SEQUENCER_C
  31 #include "sound_config.h"
  32 
  33 #ifdef CONFIGURE_SOUNDCARD
  34 
  35 #ifndef EXCLUDE_SEQUENCER
  36 
  37 static int      sequencer_ok = 0;
  38 
  39 DEFINE_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
  40 /* DEFINE_WAIT_QUEUE (midi_sleeper, midi_sleep_flag); */
  41 #define midi_sleeper seq_sleeper
  42 #define midi_sleep_flag seq_sleep_flag
  43 
  44 static int      midi_opened[MAX_MIDI_DEV] =
  45 {0};                            /* 1 if the process has opened MIDI */
  46 static int      midi_written[MAX_MIDI_DEV] =
  47 {0};
  48 
  49 unsigned long   seq_time = 0;   /* Reference point for the timer */
  50 
  51 #include "tuning.h"
  52 
  53 #define EV_SZ   8
  54 #define IEV_SZ  4
  55 static unsigned char *queue = NULL;     /* SEQ_MAX_QUEUE * EV_SZ bytes */
  56 static unsigned char *iqueue = NULL;    /* SEQ_MAX_QUEUE * IEV_SZ bytes */
  57 
  58 static volatile int qhead = 0, qtail = 0, qlen = 0;
  59 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
  60 static volatile int seq_playing = 0;
  61 static int      sequencer_busy = 0;
  62 static int      output_treshold;
  63 static unsigned synth_open_mask;
  64 
  65 static int      seq_queue (unsigned char *note);
  66 static void     seq_startplay (void);
  67 static int      seq_sync (void);
  68 static void     seq_reset (void);
  69 static int      pmgr_present[MAX_SYNTH_DEV] =
  70 {0};
  71 
  72 #if MAX_SYNTH_DEV > 15
  73 #error Too many synthesizer devices
  74 #endif
  75 
  76 int
  77 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   int             c = count, p = 0;
  80   unsigned long   flags;
  81 
  82   dev = dev >> 4;
  83 
  84   if (dev)                      /* Patch manager device */
  85     return pmgr_read (dev - 1, file, buf, count);
  86 
  87   while (c > 3)
  88     {
  89       DISABLE_INTR (flags);
  90       if (!iqlen)
  91         {
  92           if (c != count)       /* Some data has been received */
  93             {
  94               RESTORE_INTR (flags);
  95               return count - c; /* Return what we have */
  96             }
  97 
  98           DO_SLEEP (midi_sleeper, midi_sleep_flag, 0);
  99 
 100           if (!iqlen)
 101             {
 102               RESTORE_INTR (flags);
 103               return count - c;
 104             }
 105         }
 106 
 107       COPY_TO_USER (buf, p, &iqueue[iqhead * IEV_SZ], IEV_SZ);
 108       p += 4;
 109       c -= 4;
 110 
 111       iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
 112       iqlen--;
 113       RESTORE_INTR (flags);
 114     }
 115 
 116   return count - c;
 117 }
 118 
 119 static void
 120 sequencer_midi_output (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122   /* Currently NOP */
 123 }
 124 
 125 static void
 126 copy_to_input (unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128   unsigned long   flags;
 129 
 130   if (iqlen >= (SEQ_MAX_QUEUE - 1))
 131     return;                     /* Overflow */
 132 
 133   DISABLE_INTR (flags);
 134   memcpy (&iqueue[iqtail * IEV_SZ], event, IEV_SZ);
 135   iqlen++;
 136   iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
 137 
 138   if (SOMEONE_WAITING (midi_sleeper, midi_sleep_flag))
 139     {
 140       WAKE_UP (midi_sleeper, midi_sleep_flag);
 141     }
 142   RESTORE_INTR (flags);
 143 }
 144 
 145 static void
 146 sequencer_midi_input (int dev, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148   int             tstamp;
 149   unsigned char   event[4];
 150 
 151   if (data == 0xfe)             /* Active sensing */
 152     return;                     /* Ignore */
 153 
 154   tstamp = GET_TIME () - seq_time;      /* Time since open() */
 155   tstamp = (tstamp << 8) | SEQ_WAIT;
 156 
 157   copy_to_input ((unsigned char *) &tstamp);
 158 
 159   event[0] = SEQ_MIDIPUTC;
 160   event[1] = data;
 161   event[2] = dev;
 162   event[3] = 0;
 163 
 164   copy_to_input (event);
 165 }
 166 
 167 int
 168 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170   unsigned char   event[EV_SZ], ev_code;
 171   int             p = 0, c, ev_size;
 172   int             err;
 173   int             mode = file->mode & O_ACCMODE;
 174 
 175   dev = dev >> 4;
 176 
 177   DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
 178 
 179   if (mode == OPEN_READ)
 180     return RET_ERROR (EIO);
 181 
 182   if (dev)                      /* Patch manager device */
 183     return pmgr_write (dev - 1, file, buf, count);
 184 
 185   c = count;
 186 
 187   while (c >= 4)
 188     {
 189       COPY_FROM_USER (event, buf, p, 4);
 190       ev_code = event[0];
 191 
 192       if (ev_code == SEQ_FULLSIZE)
 193         {
 194           int             err;
 195 
 196           dev = *(unsigned short *) &event[2];
 197           if (dev < 0 || dev >= num_synths)
 198             return RET_ERROR (ENXIO);
 199 
 200           if (!(synth_open_mask & (1 << dev)))
 201             return RET_ERROR (ENXIO);
 202 
 203           err = synth_devs[dev]->load_patch (dev, *(short *) &event[0], buf, p + 4, c, 0);
 204           if (err < 0)
 205             return err;
 206 
 207           return err;
 208         }
 209 
 210       if (ev_code == SEQ_EXTENDED || ev_code == SEQ_PRIVATE)
 211         {
 212 
 213           ev_size = 8;
 214 
 215           if (c < ev_size)
 216             {
 217               if (!seq_playing)
 218                 seq_startplay ();
 219               return count - c;
 220             }
 221 
 222           COPY_FROM_USER (&event[4], buf, p + 4, 4);
 223 
 224         }
 225       else
 226         ev_size = 4;
 227 
 228       if (event[0] == SEQ_MIDIPUTC)
 229         {
 230 
 231           if (!midi_opened[event[2]])
 232             {
 233               int             mode;
 234               int             dev = event[2];
 235 
 236               if (dev >= num_midis)
 237                 {
 238                   printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
 239                   return RET_ERROR (ENXIO);
 240                 }
 241 
 242               mode = file->mode & O_ACCMODE;
 243 
 244               if ((err = midi_devs[dev]->open (dev, mode,
 245                           sequencer_midi_input, sequencer_midi_output)) < 0)
 246                 {
 247                   seq_reset ();
 248                   printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
 249                   return err;
 250                 }
 251 
 252               midi_opened[dev] = 1;
 253             }
 254 
 255         }
 256 
 257       if (!seq_queue (event))
 258         {
 259 
 260           if (!seq_playing)
 261             seq_startplay ();
 262           return count - c;
 263         }
 264 
 265       p += ev_size;
 266       c -= ev_size;
 267     }
 268 
 269   if (!seq_playing)
 270     seq_startplay ();
 271 
 272   return count;
 273 }
 274 
 275 static int
 276 seq_queue (unsigned char *note)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278 
 279   /* Test if there is space in the queue */
 280 
 281   if (qlen >= SEQ_MAX_QUEUE)
 282     if (!seq_playing)
 283       seq_startplay ();         /* Give chance to drain the queue */
 284 
 285   if (qlen >= SEQ_MAX_QUEUE && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 286     {
 287       /* Sleep until there is enough space on the queue */
 288       DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
 289     }
 290 
 291   if (qlen >= SEQ_MAX_QUEUE)
 292     return 0;                   /* To be sure */
 293 
 294   memcpy (&queue[qtail * EV_SZ], note, EV_SZ);
 295 
 296   qtail = (qtail + 1) % SEQ_MAX_QUEUE;
 297   qlen++;
 298 
 299   return 1;
 300 }
 301 
 302 static int
 303 extended_event (unsigned char *q)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305   int             dev = q[2];
 306 
 307   if (dev < 0 || dev >= num_synths)
 308     return RET_ERROR (ENXIO);
 309 
 310   if (!(synth_open_mask & (1 << dev)))
 311     return RET_ERROR (ENXIO);
 312 
 313   switch (q[1])
 314     {
 315     case SEQ_NOTEOFF:
 316       synth_devs[dev]->kill_note (dev, q[3], q[5]);
 317       break;
 318 
 319     case SEQ_NOTEON:
 320       if (q[4] > 127 && q[4] != 255)
 321         return 0;
 322 
 323       synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
 324       break;
 325 
 326     case SEQ_PGMCHANGE:
 327       synth_devs[dev]->set_instr (dev, q[3], q[4]);
 328       break;
 329 
 330     case SEQ_AFTERTOUCH:
 331       synth_devs[dev]->aftertouch (dev, q[3], q[4]);
 332       break;
 333 
 334     case SEQ_BALANCE:
 335       synth_devs[dev]->panning (dev, q[3], (char) q[4]);
 336       break;
 337 
 338     case SEQ_CONTROLLER:
 339       synth_devs[dev]->controller (dev, q[3], q[4], *(short *) &q[5]);
 340       break;
 341 
 342     case SEQ_VOLMODE:
 343       synth_devs[dev]->volume_method (dev, q[3]);
 344       break;
 345 
 346     default:
 347       return RET_ERROR (EINVAL);
 348     }
 349 
 350   return 0;
 351 }
 352 
 353 static void
 354 seq_startplay (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 355 {
 356   int             this_one;
 357   unsigned long  *delay;
 358   unsigned char  *q;
 359 
 360   while (qlen > 0)
 361     {
 362       qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
 363       qlen--;
 364 
 365       q = &queue[this_one * EV_SZ];
 366 
 367       switch (q[0])
 368         {
 369         case SEQ_NOTEOFF:
 370           if (synth_open_mask & (1 << 0))
 371             if (synth_devs[0])
 372               synth_devs[0]->kill_note (0, q[1], q[3]);
 373           break;
 374 
 375         case SEQ_NOTEON:
 376           if (q[4] < 128 || q[4] == 255)
 377             if (synth_open_mask & (1 << 0))
 378               if (synth_devs[0])
 379                 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
 380           break;
 381 
 382         case SEQ_WAIT:
 383           delay = (unsigned long *) q;  /* Bytes 1 to 3 are containing the
 384                                          * delay in GET_TIME() */
 385           *delay = (*delay >> 8) & 0xffffff;
 386 
 387           if (*delay > 0)
 388             {
 389               long            time;
 390 
 391               seq_playing = 1;
 392               time = *delay;
 393 
 394               request_sound_timer (time);
 395 
 396               if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 397                 {
 398                   unsigned long   flags;
 399 
 400                   DISABLE_INTR (flags);
 401                   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 402                     {
 403                       WAKE_UP (seq_sleeper, seq_sleep_flag);
 404                     }
 405                   RESTORE_INTR (flags);
 406                 }
 407               return;           /* Stop here. Timer routine will continue
 408                                  * playing after the delay */
 409             }
 410           break;
 411 
 412         case SEQ_PGMCHANGE:
 413           if (synth_open_mask & (1 << 0))
 414             if (synth_devs[0])
 415               synth_devs[0]->set_instr (0, q[1], q[2]);
 416           break;
 417 
 418         case SEQ_SYNCTIMER:     /* Reset timer */
 419           seq_time = GET_TIME ();
 420           break;
 421 
 422         case SEQ_MIDIPUTC:      /* Put a midi character */
 423           if (midi_opened[q[2]])
 424             {
 425               int             dev;
 426 
 427               dev = q[2];
 428 
 429               if (!midi_devs[dev]->putc (dev, q[1]))
 430                 {
 431                   /*
 432                    * Output FIFO is full. Wait one timer cycle and try again.
 433                    */
 434 
 435                   qlen++;
 436                   qhead = this_one;     /* Restore queue */
 437                   seq_playing = 1;
 438                   request_sound_timer (-1);
 439                   return;
 440                 }
 441               else
 442                 midi_written[dev] = 1;
 443             }
 444           break;
 445 
 446         case SEQ_ECHO:
 447           copy_to_input (q);    /* Echo back to the process */
 448           break;
 449 
 450         case SEQ_PRIVATE:
 451           if (q[1] < num_synths)
 452             synth_devs[q[1]]->hw_control (q[1], q);
 453           break;
 454 
 455         case SEQ_EXTENDED:
 456           extended_event (q);
 457           break;
 458 
 459         default:;
 460         }
 461 
 462     }
 463 
 464   seq_playing = 0;
 465 
 466   if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
 467     {
 468       unsigned long   flags;
 469 
 470       DISABLE_INTR (flags);
 471       if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 472         {
 473           WAKE_UP (seq_sleeper, seq_sleep_flag);
 474         }
 475       RESTORE_INTR (flags);
 476     }
 477 
 478 }
 479 
 480 int
 481 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 482 {
 483   int             retval, mode, i;
 484 
 485   dev = dev >> 4;
 486   mode = file->mode & O_ACCMODE;
 487 
 488   DEB (printk ("sequencer_open(dev=%d)\n", dev));
 489 
 490   if (!sequencer_ok)
 491     {
 492       printk ("Soundcard: Sequencer not initialized\n");
 493       return RET_ERROR (ENXIO);
 494     }
 495 
 496   if (dev)                      /* Patch manager device */
 497     {
 498       int             err;
 499 
 500       dev--;
 501       if (pmgr_present[dev])
 502         return RET_ERROR (EBUSY);
 503       if ((err = pmgr_open (dev)) < 0)
 504         return err;             /* Failed */
 505 
 506       pmgr_present[dev] = 1;
 507       return err;
 508     }
 509 
 510   if (sequencer_busy)
 511     {
 512       printk ("Sequencer busy\n");
 513       return RET_ERROR (EBUSY);
 514     }
 515 
 516   if (!(num_synths + num_midis))
 517     return RET_ERROR (ENXIO);
 518 
 519   synth_open_mask = 0;
 520 
 521   if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
 522     for (i = 0; i < num_synths; i++)    /* Open synth devices */
 523       if (synth_devs[i]->open (i, mode) < 0)
 524         printk ("Sequencer: Warning! Cannot open synth device #%d\n", i);
 525       else
 526         synth_open_mask |= (1 << i);
 527 
 528   seq_time = GET_TIME ();
 529 
 530   for (i = 0; i < num_midis; i++)
 531     {
 532       midi_opened[i] = 0;
 533       midi_written[i] = 0;
 534     }
 535 
 536   if (mode == OPEN_READ || mode == OPEN_READWRITE)
 537     {                           /* Initialize midi input devices */
 538       if (!num_midis)
 539         {
 540           printk ("Sequencer: No Midi devices. Input not possible\n");
 541           return RET_ERROR (ENXIO);
 542         }
 543 
 544       for (i = 0; i < num_midis; i++)
 545         {
 546           if ((retval = midi_devs[i]->open (i, mode,
 547                          sequencer_midi_input, sequencer_midi_output)) >= 0)
 548             midi_opened[i] = 1;
 549         }
 550     }
 551 
 552   sequencer_busy = 1;
 553   RESET_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
 554   RESET_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
 555   output_treshold = SEQ_MAX_QUEUE / 2;
 556 
 557   for (i = 0; i < num_synths; i++)
 558     if (pmgr_present[i])
 559       pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
 560 
 561   return 0;
 562 }
 563 
 564 void
 565 seq_drain_midi_queues (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 566 {
 567   int             i, n;
 568 
 569   /*
 570    * Give the Midi drivers time to drain their output queues
 571    */
 572 
 573   n = 1;
 574 
 575   while (!PROCESS_ABORTING (midi_sleeper, midi_sleep_flag) && n)
 576     {
 577       n = 0;
 578 
 579       for (i = 0; i < num_midis; i++)
 580         if (midi_opened[i] && midi_written[i])
 581           if (midi_devs[i]->buffer_status != NULL)
 582             if (midi_devs[i]->buffer_status (i))
 583               n++;
 584 
 585       /*
 586        * Let's have a delay
 587        */
 588       if (n)
 589         {
 590           DO_SLEEP (seq_sleeper, seq_sleep_flag, HZ / 10);
 591         }
 592     }
 593 }
 594 
 595 void
 596 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
 597 {
 598   int             i;
 599   int             mode = file->mode & O_ACCMODE;
 600 
 601   dev = dev >> 4;
 602 
 603   DEB (printk ("sequencer_release(dev=%d)\n", dev));
 604 
 605   if (dev)                      /* Patch manager device */
 606     {
 607       dev--;
 608       pmgr_release (dev);
 609       pmgr_present[dev] = 0;
 610       return;
 611     }
 612 
 613   /*
 614      * Wait until the queue is empty
 615    */
 616 
 617   while (!PROCESS_ABORTING (seq_sleeper, seq_sleep_flag) && qlen)
 618     {
 619       seq_sync ();
 620     }
 621 
 622   if (mode != OPEN_READ)
 623     seq_drain_midi_queues ();   /* Ensure the output queues are empty */
 624   seq_reset ();
 625   if (mode != OPEN_READ)
 626     seq_drain_midi_queues ();   /* Flush the all notes off messages */
 627 
 628   for (i = 0; i < num_midis; i++)
 629     if (midi_opened[i])
 630       midi_devs[i]->close (i);
 631 
 632   if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
 633     for (i = 0; i < num_synths; i++)
 634       if (synth_open_mask & (1 << i))   /* Actually opened */
 635         if (synth_devs[i])
 636           synth_devs[i]->close (i);
 637 
 638   for (i = 0; i < num_synths; i++)
 639     if (pmgr_present[i])
 640       pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
 641 
 642   sequencer_busy = 0;
 643 }
 644 
 645 static int
 646 seq_sync (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 647 {
 648   if (qlen && !seq_playing && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
 649     seq_startplay ();
 650 
 651   if (qlen && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))   /* Queue not empty */
 652     {
 653       DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
 654     }
 655 
 656   return qlen;
 657 }
 658 
 659 static void
 660 midi_outc (int dev, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 661 {
 662   /*
 663    * NOTE! Calls sleep(). Don't call this from interrupt.
 664    */
 665 
 666   int             n;
 667 
 668   /* This routine sends one byte to the Midi channel. */
 669   /* If the output Fifo is full, it waits until there */
 670   /* is space in the queue */
 671 
 672   n = 300;                      /* Timeout in jiffies */
 673 
 674   while (n && !midi_devs[dev]->putc (dev, data))
 675     {
 676       DO_SLEEP (seq_sleeper, seq_sleep_flag, 4);
 677       n--;
 678     }
 679 }
 680 
 681 static void
 682 seq_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 683 {
 684   /*
 685    * NOTE! Calls sleep(). Don't call this from interrupt.
 686    */
 687 
 688   int             i, chn;
 689 
 690   sound_stop_timer ();
 691 
 692   qlen = qhead = qtail = 0;
 693   iqlen = iqhead = iqtail = 0;
 694 
 695   for (i = 0; i < num_synths; i++)
 696     if (synth_open_mask & (1 << i))
 697       if (synth_devs[i])
 698         synth_devs[i]->reset (i);
 699 
 700   for (i = 0; i < num_midis; i++)
 701     if (midi_written[i])        /* Midi used. Some notes may still be playing */
 702       {
 703         for (chn = 0; chn < 16; chn++)
 704           {
 705             midi_outc (i,
 706                        (unsigned char) (0xb0 + (chn & 0xff)));  /* Channel msg */
 707             midi_outc (i, 0x7b);/* All notes off */
 708             midi_outc (i, 0);   /* Dummy parameter */
 709           }
 710 
 711         midi_devs[i]->close (i);
 712 
 713         midi_written[i] = 0;
 714         midi_opened[i] = 0;
 715       }
 716 
 717   seq_playing = 0;
 718 
 719   if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
 720     printk ("Sequencer Warning: Unexpected sleeping process\n");
 721 
 722 }
 723 
 724 int
 725 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
 726                  unsigned int cmd, unsigned int arg)
 727 {
 728   int             midi_dev, orig_dev;
 729   int             mode = file->mode & O_ACCMODE;
 730 
 731   orig_dev = dev = dev >> 4;
 732 
 733   switch (cmd)
 734     {
 735 
 736     case SNDCTL_SEQ_SYNC:
 737       if (dev)                  /* Patch manager */
 738         return RET_ERROR (EIO);
 739 
 740       if (mode == OPEN_READ)
 741         return 0;
 742       while (qlen && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
 743         seq_sync ();
 744       return 0;
 745       break;
 746 
 747     case SNDCTL_SEQ_RESET:
 748       if (dev)                  /* Patch manager */
 749         return RET_ERROR (EIO);
 750 
 751       seq_reset ();
 752       return 0;
 753       break;
 754 
 755     case SNDCTL_SEQ_TESTMIDI:
 756       if (dev)                  /* Patch manager */
 757         return RET_ERROR (EIO);
 758 
 759       midi_dev = IOCTL_IN (arg);
 760       if (midi_dev >= num_midis)
 761         return RET_ERROR (ENXIO);
 762 
 763       if (!midi_opened[midi_dev])
 764         {
 765           int             err, mode;
 766 
 767           mode = file->mode & O_ACCMODE;
 768           if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
 769                                                 sequencer_midi_input,
 770                                                 sequencer_midi_output)) < 0)
 771             return err;
 772         }
 773 
 774       midi_opened[midi_dev] = 1;
 775 
 776       return 0;
 777       break;
 778 
 779     case SNDCTL_SEQ_GETINCOUNT:
 780       if (dev)                  /* Patch manager */
 781         return RET_ERROR (EIO);
 782 
 783       if (mode == OPEN_WRITE)
 784         return 0;
 785       return IOCTL_OUT (arg, iqlen);
 786       break;
 787 
 788     case SNDCTL_SEQ_GETOUTCOUNT:
 789 
 790       if (mode == OPEN_READ)
 791         return 0;
 792       return IOCTL_OUT (arg, SEQ_MAX_QUEUE - qlen);
 793       break;
 794 
 795     case SNDCTL_SEQ_CTRLRATE:
 796       if (dev)                  /* Patch manager */
 797         return RET_ERROR (EIO);
 798 
 799       /* If *arg == 0, just return the current rate */
 800       return IOCTL_OUT (arg, HZ);
 801       break;
 802 
 803     case SNDCTL_SEQ_RESETSAMPLES:
 804       dev = IOCTL_IN (arg);
 805       if (dev < 0 || dev >= num_synths)
 806         return RET_ERROR (ENXIO);
 807 
 808       if (!(synth_open_mask & (1 << dev)) && !orig_dev)
 809         return RET_ERROR (EBUSY);
 810 
 811       if (!orig_dev && pmgr_present[dev])
 812         pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
 813 
 814       return synth_devs[dev]->ioctl (dev, cmd, arg);
 815       break;
 816 
 817     case SNDCTL_SEQ_NRSYNTHS:
 818       return IOCTL_OUT (arg, num_synths);
 819       break;
 820 
 821     case SNDCTL_SEQ_NRMIDIS:
 822       return IOCTL_OUT (arg, num_midis);
 823       break;
 824 
 825     case SNDCTL_SYNTH_MEMAVL:
 826       {
 827         int             dev = IOCTL_IN (arg);
 828 
 829         if (dev < 0 || dev >= num_synths)
 830           return RET_ERROR (ENXIO);
 831 
 832         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
 833           return RET_ERROR (EBUSY);
 834 
 835         return IOCTL_OUT (arg, synth_devs[dev]->ioctl (dev, cmd, arg));
 836       }
 837       break;
 838 
 839     case SNDCTL_FM_4OP_ENABLE:
 840       {
 841         int             dev = IOCTL_IN (arg);
 842 
 843         if (dev < 0 || dev >= num_synths)
 844           return RET_ERROR (ENXIO);
 845 
 846         if (!(synth_open_mask & (1 << dev)))
 847           return RET_ERROR (ENXIO);
 848 
 849         synth_devs[dev]->ioctl (dev, cmd, arg);
 850         return 0;
 851       }
 852       break;
 853 
 854     case SNDCTL_SYNTH_INFO:
 855       {
 856         struct synth_info inf;
 857         int             dev;
 858 
 859         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
 860         dev = inf.device;
 861 
 862         if (dev < 0 || dev >= num_synths)
 863           return RET_ERROR (ENXIO);
 864 
 865         if (!(synth_open_mask & (1 << dev)) && !orig_dev)
 866           return RET_ERROR (EBUSY);
 867 
 868         return synth_devs[dev]->ioctl (dev, cmd, arg);
 869       }
 870       break;
 871 
 872     case SNDCTL_MIDI_INFO:
 873       {
 874         struct midi_info inf;
 875         int             dev;
 876 
 877         IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
 878         dev = inf.device;
 879 
 880         if (dev < 0 || dev >= num_midis)
 881           return RET_ERROR (ENXIO);
 882 
 883         IOCTL_TO_USER ((char *) arg, 0, (char *) &(midi_devs[dev]->info), sizeof (inf));
 884         return 0;
 885       }
 886       break;
 887 
 888     case SNDCTL_PMGR_IFACE:
 889       {
 890         struct patmgr_info *inf;
 891         int             dev, err;
 892 
 893         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
 894 
 895         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
 896         dev = inf->device;
 897 
 898         if (dev < 0 || dev >= num_synths)
 899           {
 900             KERNEL_FREE (inf);
 901             return RET_ERROR (ENXIO);
 902           }
 903 
 904         if (!synth_devs[dev]->pmgr_interface)
 905           {
 906             KERNEL_FREE (inf);
 907             return RET_ERROR (ENXIO);
 908           }
 909 
 910         if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
 911           {
 912             KERNEL_FREE (inf);
 913             return err;
 914           }
 915 
 916         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
 917         KERNEL_FREE (inf);
 918         return 0;
 919       }
 920       break;
 921 
 922     case SNDCTL_PMGR_ACCESS:
 923       {
 924         struct patmgr_info *inf;
 925         int             dev, err;
 926 
 927         inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
 928 
 929         IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
 930         dev = inf->device;
 931 
 932         if (dev < 0 || dev >= num_synths)
 933           {
 934             KERNEL_FREE (inf);
 935             return RET_ERROR (ENXIO);
 936           }
 937 
 938         if (!pmgr_present[dev])
 939           {
 940             KERNEL_FREE (inf);
 941             return RET_ERROR (ESRCH);
 942           }
 943 
 944         if ((err = pmgr_access (dev, inf)) < 0)
 945           {
 946             KERNEL_FREE (inf);
 947             return err;
 948           }
 949 
 950         IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
 951         KERNEL_FREE (inf);
 952         return 0;
 953       }
 954       break;
 955 
 956     case SNDCTL_SEQ_TRESHOLD:
 957       {
 958         int             tmp = IOCTL_IN (arg);
 959 
 960         if (dev)                /* Patch manager */
 961           return RET_ERROR (EIO);
 962 
 963         if (tmp < 1)
 964           tmp = 1;
 965         if (tmp >= SEQ_MAX_QUEUE)
 966           tmp = SEQ_MAX_QUEUE - 1;
 967         output_treshold = tmp;
 968         return 0;
 969       }
 970       break;
 971 
 972     default:
 973       if (dev)                  /* Patch manager */
 974         return RET_ERROR (EIO);
 975 
 976       if (mode == OPEN_READ)
 977         return RET_ERROR (EIO);
 978 
 979       if (!synth_devs[0])
 980         return RET_ERROR (ENXIO);
 981       if (!(synth_open_mask & (1 << 0)))
 982         return RET_ERROR (ENXIO);
 983       return synth_devs[0]->ioctl (0, cmd, arg);
 984       break;
 985     }
 986 
 987   return RET_ERROR (EINVAL);
 988 }
 989 
 990 #ifdef ALLOW_SELECT
 991 int
 992 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 993 {
 994   dev = dev >> 4;
 995 
 996   switch (sel_type)
 997     {
 998     case SEL_IN:
 999       if (!iqlen)
1000         {
1001           select_wait (&midi_sleeper, wait);
1002           return 0;
1003         }
1004       return 1;
1005 
1006       break;
1007 
1008     case SEL_OUT:
1009       if (qlen >= SEQ_MAX_QUEUE)
1010         {
1011           select_wait (&seq_sleeper, wait);
1012           return 0;
1013         }
1014       return 1;
1015       break;
1016 
1017     case SEL_EX:
1018       return 0;
1019     }
1020 
1021   return 0;
1022 }
1023 
1024 #endif
1025 
1026 void
1027 sequencer_timer (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1028 {
1029   seq_startplay ();
1030 }
1031 
1032 int
1033 note_to_freq (int note_num)
     /* [previous][next][first][last][top][bottom][index][help] */
1034 {
1035 
1036   /*
1037    * This routine converts a midi note to a frequency (multiplied by 1000)
1038    */
1039 
1040   int             note, octave, note_freq;
1041   int             notes[] =
1042   {
1043     261632, 277189, 293671, 311132, 329632, 349232,
1044     369998, 391998, 415306, 440000, 466162, 493880
1045   };                            /* Note freq*1000 for octave 5 */
1046 
1047 #define BASE_OCTAVE     5
1048 
1049   octave = note_num / 12;
1050   note = note_num % 12;
1051 
1052   note_freq = notes[note];
1053 
1054   if (octave < BASE_OCTAVE)
1055     note_freq >>= (BASE_OCTAVE - octave);
1056   else if (octave > BASE_OCTAVE)
1057     note_freq <<= (octave - BASE_OCTAVE);
1058 
1059   /* note_freq >>= 1;    */
1060 
1061   return note_freq;
1062 }
1063 
1064 unsigned long
1065 compute_finetune (unsigned long base_freq, int bend, int range)
     /* [previous][next][first][last][top][bottom][index][help] */
1066 {
1067   unsigned long   amount;
1068   int             negative, semitones, cents, multiplier = 1;
1069 
1070   if (!bend)
1071     return base_freq;
1072   if (!range)
1073     return base_freq;
1074 
1075   if (!base_freq)
1076     return base_freq;
1077 
1078   if (range >= 8192)
1079     range = 8191;
1080 
1081   bend = bend * range / 8192;
1082   if (!bend)
1083     return base_freq;
1084 
1085   negative = bend < 0 ? 1 : 0;
1086 
1087   if (bend < 0)
1088     bend *= -1;
1089   if (bend > range)
1090     bend = range;
1091 
1092   /*
1093      if (bend > 2399)
1094      bend = 2399;
1095    */
1096   while (bend > 2399)
1097     {
1098       multiplier *= 4;
1099       bend -= 2400;
1100     }
1101 
1102   semitones = bend / 100;
1103   cents = bend % 100;
1104 
1105   amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents] / 10000;
1106 
1107   if (negative)
1108     return (base_freq * 10000) / amount;        /* Bend down */
1109   else
1110     return (base_freq * amount) / 10000;        /* Bend up */
1111 }
1112 
1113 
1114 long
1115 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1116 {
1117 
1118   sequencer_ok = 1;
1119   PERMANENT_MALLOC (unsigned char *, queue, SEQ_MAX_QUEUE * EV_SZ, mem_start);
1120   PERMANENT_MALLOC (unsigned char *, iqueue, SEQ_MAX_QUEUE * IEV_SZ, mem_start);
1121 
1122   return mem_start;
1123 }
1124 
1125 #else
1126 /* Stub version */
1127 int
1128 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1129 {
1130   return RET_ERROR (EIO);
1131 }
1132 
1133 int
1134 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1135 {
1136   return RET_ERROR (EIO);
1137 }
1138 
1139 int
1140 sequencer_open (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1141 {
1142   return RET_ERROR (ENXIO);
1143 }
1144 
1145 void
1146 sequencer_release (int dev, struct fileinfo *file)
     /* [previous][next][first][last][top][bottom][index][help] */
1147 {
1148 }
1149 int
1150 sequencer_ioctl (int dev, struct fileinfo *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1151                  unsigned int cmd, unsigned int arg)
1152 {
1153   return RET_ERROR (EIO);
1154 }
1155 
1156 int
1157 sequencer_lseek (int dev, struct fileinfo *file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
1158 {
1159   return RET_ERROR (EIO);
1160 }
1161 
1162 long
1163 sequencer_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1164 {
1165   return mem_start;
1166 }
1167 
1168 int
1169 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1170 {
1171   return RET_ERROR (EIO);
1172 }
1173 
1174 #endif
1175 
1176 #endif

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