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

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