root/drivers/sound/mpu401.c

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

DEFINITIONS

This source file includes following definitions.
  1. mpu_input_scanner
  2. mpu401_input_loop
  3. mpuintr
  4. mpu401_open
  5. mpu401_close
  6. mpu401_out
  7. mpu401_command
  8. mpu_cmd
  9. mpu401_prefix_cmd
  10. mpu401_start_read
  11. mpu401_end_read
  12. mpu401_ioctl
  13. mpu401_kick
  14. mpu401_buffer_status
  15. mpu_synth_ioctl
  16. mpu_synth_open
  17. mpu_synth_close
  18. mpu401_chk_version
  19. attach_mpu401
  20. reset_mpu401
  21. set_uart_mode
  22. probe_mpu401
  23. unload_mpu401
  24. clocks2ticks
  25. set_timebase
  26. tmr_reset
  27. set_timer_mode
  28. stop_metronome
  29. setup_metronome
  30. mpu_start_timer
  31. mpu_timer_open
  32. mpu_timer_close
  33. mpu_timer_event
  34. mpu_timer_get_time
  35. mpu_timer_ioctl
  36. mpu_timer_arm
  37. mpu_timer_interrupt
  38. timer_ext_event
  39. mpu_timer_init

   1 /*
   2  * sound/mpu401.c
   3  *
   4  * The low level driver for Roland MPU-401 compatible Midi cards.
   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  * Modified:
  29  *  Riccardo Facchetti  24 Mar 1995
  30  *  - Added the Audio Excel DSP 16 initialization routine.
  31  */
  32 
  33 #define USE_SEQ_MACROS
  34 #define USE_SIMPLE_MACROS
  35 
  36 #include "sound_config.h"
  37 
  38 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
  39 #include "coproc.h"
  40 
  41 static int      init_sequence[20];      /* NOTE! pos 0 = len, start pos 1. */
  42 
  43 #ifdef CONFIG_SEQUENCER
  44 static int      timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
  45 
  46 #endif
  47 
  48 struct mpu_config
  49   {
  50     int             base;       /*
  51                                  * I/O base
  52                                  */
  53     int             irq;
  54     int             opened;     /*
  55                                  * Open mode
  56                                  */
  57     int             devno;
  58     int             synthno;
  59     int             uart_mode;
  60     int             initialized;
  61     int             mode;
  62 #define MODE_MIDI       1
  63 #define MODE_SYNTH      2
  64     unsigned char   version, revision;
  65     unsigned int    capabilities;
  66 #define MPU_CAP_INTLG   0x10000000
  67 #define MPU_CAP_SYNC    0x00000010
  68 #define MPU_CAP_FSK     0x00000020
  69 #define MPU_CAP_CLS     0x00000040
  70 #define MPU_CAP_SMPTE   0x00000080
  71 #define MPU_CAP_2PORT   0x00000001
  72     int             timer_flag;
  73 
  74 #define MBUF_MAX        10
  75 #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
  76         {printk("MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
  77     int             m_busy;
  78     unsigned char   m_buf[MBUF_MAX];
  79     int             m_ptr;
  80     int             m_state;
  81     int             m_left;
  82     unsigned char   last_status;
  83     void            (*inputintr) (int dev, unsigned char data);
  84     int             shared_irq;
  85     sound_os_info  *osp;
  86   };
  87 
  88 #define DATAPORT(base)   (base)
  89 #define COMDPORT(base)   (base+1)
  90 #define STATPORT(base)   (base+1)
  91 
  92 #define mpu401_status(devc)             inb( STATPORT(devc->base))
  93 #define input_avail(devc)               (!(mpu401_status(devc)&INPUT_AVAIL))
  94 #define output_ready(devc)              (!(mpu401_status(devc)&OUTPUT_READY))
  95 #define write_command(devc, cmd)        outb( cmd,  COMDPORT(devc->base))
  96 #define read_data(devc)         inb( DATAPORT(devc->base))
  97 
  98 #define write_data(devc, byte)  outb( byte,  DATAPORT(devc->base))
  99 
 100 #define OUTPUT_READY    0x40
 101 #define INPUT_AVAIL     0x80
 102 #define MPU_ACK         0xFE
 103 #define MPU_RESET       0xFF
 104 #define UART_MODE_ON    0x3F
 105 
 106 static struct mpu_config dev_conf[MAX_MIDI_DEV] =
 107 {
 108   {0}};
 109 
 110 static int      n_mpu_devs = 0;
 111 static volatile int irq2dev[17] =
 112 {-1, -1, -1, -1, -1, -1, -1, -1,
 113  -1, -1, -1, -1, -1, -1, -1, -1, -1};
 114 
 115 static int      reset_mpu401 (struct mpu_config *devc);
 116 static void     set_uart_mode (int dev, struct mpu_config *devc, int arg);
 117 
 118 static void     mpu_timer_init (int midi_dev);
 119 static void     mpu_timer_interrupt (void);
 120 static void     timer_ext_event (struct mpu_config *devc, int event, int parm);
 121 
 122 static struct synth_info mpu_synth_info_proto =
 123 {"MPU-401 MIDI interface", 0, SYNTH_TYPE_MIDI, MIDI_TYPE_MPU401, 0, 128, 0, 128, SYNTH_CAP_INPUT};
 124 
 125 static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
 126 
 127 /*
 128  * States for the input scanner
 129  */
 130 
 131 #define ST_INIT                 0       /* Ready for timing byte or msg */
 132 #define ST_TIMED                1       /* Leading timing byte rcvd */
 133 #define ST_DATABYTE             2       /* Waiting for (nr_left) data bytes */
 134 
 135 #define ST_SYSMSG               100     /* System message (sysx etc). */
 136 #define ST_SYSEX                101     /* System exclusive msg */
 137 #define ST_MTC                  102     /* Midi Time Code (MTC) qframe msg */
 138 #define ST_SONGSEL              103     /* Song select */
 139 #define ST_SONGPOS              104     /* Song position pointer */
 140 
 141 static unsigned char len_tab[] =        /* # of data bytes following a status
 142                                          */
 143 {
 144   2,                            /* 8x */
 145   2,                            /* 9x */
 146   2,                            /* Ax */
 147   2,                            /* Bx */
 148   1,                            /* Cx */
 149   1,                            /* Dx */
 150   2,                            /* Ex */
 151   0                             /* Fx */
 152 };
 153 
 154 #ifndef CONFIG_SEQUENCER
 155 #define STORE(cmd)
 156 #else
 157 #define STORE(cmd) \
 158 { \
 159   int len; \
 160   unsigned char obuf[8]; \
 161   cmd; \
 162   seq_input_event(obuf, len); \
 163 }
 164 #endif
 165 
 166 #define _seqbuf obuf
 167 #define _seqbufptr 0
 168 #define _SEQ_ADVBUF(x) len=x
 169 
 170 static int
 171 mpu_input_scanner (struct mpu_config *devc, unsigned char midic)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173 
 174   switch (devc->m_state)
 175     {
 176     case ST_INIT:
 177       switch (midic)
 178         {
 179         case 0xf8:
 180           /* Timer overflow */
 181           break;
 182 
 183         case 0xfc:
 184           printk ("<all end>");
 185           break;
 186 
 187         case 0xfd:
 188           if (devc->timer_flag)
 189             mpu_timer_interrupt ();
 190           break;
 191 
 192         case 0xfe:
 193           return MPU_ACK;
 194           break;
 195 
 196         case 0xf0:
 197         case 0xf1:
 198         case 0xf2:
 199         case 0xf3:
 200         case 0xf4:
 201         case 0xf5:
 202         case 0xf6:
 203         case 0xf7:
 204           printk ("<Trk data rq #%d>", midic & 0x0f);
 205           break;
 206 
 207         case 0xf9:
 208           printk ("<conductor rq>");
 209           break;
 210 
 211         case 0xff:
 212           devc->m_state = ST_SYSMSG;
 213           break;
 214 
 215         default:
 216           if (midic <= 0xef)
 217             {
 218               /* printk("mpu time: %d ", midic); */
 219               devc->m_state = ST_TIMED;
 220             }
 221           else
 222             printk ("<MPU: Unknown event %02x> ", midic);
 223         }
 224       break;
 225 
 226     case ST_TIMED:
 227       {
 228         int             msg = ((int) (midic & 0xf0) >> 4);
 229 
 230         devc->m_state = ST_DATABYTE;
 231 
 232         if (msg < 8)            /* Data byte */
 233           {
 234             /* printk("midi msg (running status) "); */
 235             msg = ((int) (devc->last_status & 0xf0) >> 4);
 236             msg -= 8;
 237             devc->m_left = len_tab[msg] - 1;
 238 
 239             devc->m_ptr = 2;
 240             devc->m_buf[0] = devc->last_status;
 241             devc->m_buf[1] = midic;
 242 
 243             if (devc->m_left <= 0)
 244               {
 245                 devc->m_state = ST_INIT;
 246                 do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
 247                 devc->m_ptr = 0;
 248               }
 249           }
 250         else if (msg == 0xf)    /* MPU MARK */
 251           {
 252             devc->m_state = ST_INIT;
 253 
 254             switch (midic)
 255               {
 256               case 0xf8:
 257                 /* printk("NOP "); */
 258                 break;
 259 
 260               case 0xf9:
 261                 /* printk("meas end "); */
 262                 break;
 263 
 264               case 0xfc:
 265                 /* printk("data end "); */
 266                 break;
 267 
 268               default:
 269                 printk ("Unknown MPU mark %02x\n", midic);
 270               }
 271           }
 272         else
 273           {
 274             devc->last_status = midic;
 275             /* printk ("midi msg "); */
 276             msg -= 8;
 277             devc->m_left = len_tab[msg];
 278 
 279             devc->m_ptr = 1;
 280             devc->m_buf[0] = midic;
 281 
 282             if (devc->m_left <= 0)
 283               {
 284                 devc->m_state = ST_INIT;
 285                 do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
 286                 devc->m_ptr = 0;
 287               }
 288           }
 289       }
 290       break;
 291 
 292     case ST_SYSMSG:
 293       switch (midic)
 294         {
 295         case 0xf0:
 296           printk ("<SYX>");
 297           devc->m_state = ST_SYSEX;
 298           break;
 299 
 300         case 0xf1:
 301           devc->m_state = ST_MTC;
 302           break;
 303 
 304         case 0xf2:
 305           devc->m_state = ST_SONGPOS;
 306           devc->m_ptr = 0;
 307           break;
 308 
 309         case 0xf3:
 310           devc->m_state = ST_SONGSEL;
 311           break;
 312 
 313         case 0xf6:
 314           /* printk("tune_request\n"); */
 315           devc->m_state = ST_INIT;
 316 
 317           /*
 318              *    Real time messages
 319            */
 320         case 0xf8:
 321           /* midi clock */
 322           devc->m_state = ST_INIT;
 323           timer_ext_event (devc, TMR_CLOCK, 0);
 324           break;
 325 
 326         case 0xfA:
 327           devc->m_state = ST_INIT;
 328           timer_ext_event (devc, TMR_START, 0);
 329           break;
 330 
 331         case 0xFB:
 332           devc->m_state = ST_INIT;
 333           timer_ext_event (devc, TMR_CONTINUE, 0);
 334           break;
 335 
 336         case 0xFC:
 337           devc->m_state = ST_INIT;
 338           timer_ext_event (devc, TMR_STOP, 0);
 339           break;
 340 
 341         case 0xFE:
 342           /* active sensing */
 343           devc->m_state = ST_INIT;
 344           break;
 345 
 346         case 0xff:
 347           /* printk("midi hard reset"); */
 348           devc->m_state = ST_INIT;
 349           break;
 350 
 351         default:
 352           printk ("unknown MIDI sysmsg %0x\n", midic);
 353           devc->m_state = ST_INIT;
 354         }
 355       break;
 356 
 357     case ST_MTC:
 358       devc->m_state = ST_INIT;
 359       printk ("MTC frame %x02\n", midic);
 360       break;
 361 
 362     case ST_SYSEX:
 363       if (midic == 0xf7)
 364         {
 365           printk ("<EOX>");
 366           devc->m_state = ST_INIT;
 367         }
 368       else
 369         printk ("%02x ", midic);
 370       break;
 371 
 372     case ST_SONGPOS:
 373       BUFTEST (devc);
 374       devc->m_buf[devc->m_ptr++] = midic;
 375       if (devc->m_ptr == 2)
 376         {
 377           devc->m_state = ST_INIT;
 378           devc->m_ptr = 0;
 379           timer_ext_event (devc, TMR_SPP,
 380                            ((devc->m_buf[1] & 0x7f) << 7) |
 381                            (devc->m_buf[0] & 0x7f));
 382         }
 383       break;
 384 
 385     case ST_DATABYTE:
 386       BUFTEST (devc);
 387       devc->m_buf[devc->m_ptr++] = midic;
 388       if ((--devc->m_left) <= 0)
 389         {
 390           devc->m_state = ST_INIT;
 391           do_midi_msg (devc->synthno, devc->m_buf, devc->m_ptr);
 392           devc->m_ptr = 0;
 393         }
 394       break;
 395 
 396     default:
 397       printk ("Bad state %d ", devc->m_state);
 398       devc->m_state = ST_INIT;
 399     }
 400 
 401   return 1;
 402 }
 403 
 404 static void
 405 mpu401_input_loop (struct mpu_config *devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 406 {
 407   unsigned long   flags;
 408   int             busy;
 409   int             n;
 410 
 411   save_flags (flags);
 412   cli ();
 413   busy = devc->m_busy;
 414   devc->m_busy = 1;
 415   restore_flags (flags);
 416 
 417   if (busy)                     /* Already inside the scanner */
 418     return;
 419 
 420   n = 50;
 421 
 422   while (input_avail (devc) && n-- > 0)
 423     {
 424       unsigned char   c = read_data (devc);
 425 
 426       if (devc->mode == MODE_SYNTH)
 427         {
 428           mpu_input_scanner (devc, c);
 429         }
 430       else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
 431         devc->inputintr (devc->devno, c);
 432     }
 433 
 434   devc->m_busy = 0;
 435 }
 436 
 437 void
 438 mpuintr (int irq, struct pt_regs *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440   struct mpu_config *devc;
 441   int             dev;
 442 
 443   sti ();
 444 
 445 /*
 446  * FreeBSD (and some others) pass unit number to the interrupt handler.
 447  * In this case we have to scan the table for first handler.
 448  */
 449 
 450   if (irq < 1 || irq > 15)
 451     {
 452       dev = -1;
 453     }
 454   else
 455     dev = irq2dev[irq];
 456 
 457   if (dev == -1)
 458     {
 459       int             origirq = irq;
 460 
 461       for (irq = 0; irq <= 16; irq++)
 462         if (irq2dev[irq] != -1)
 463           break;
 464       if (irq > 15)
 465         {
 466           printk ("MPU-401: Bogus interrupt #%d?\n", origirq);
 467           return;
 468         }
 469       dev = irq2dev[irq];
 470       devc = &dev_conf[dev];
 471     }
 472   else
 473     devc = &dev_conf[dev];
 474 
 475   if (input_avail (devc))
 476     if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
 477       mpu401_input_loop (devc);
 478     else
 479       {
 480         /* Dummy read (just to acknowledge the interrupt) */
 481         read_data (devc);
 482       }
 483 
 484 }
 485 
 486 static int
 487 mpu401_open (int dev, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 488              void            (*input) (int dev, unsigned char data),
 489              void            (*output) (int dev)
 490 )
 491 {
 492   int             err;
 493   struct mpu_config *devc;
 494 
 495   if (dev < 0 || dev >= num_midis)
 496     return -ENXIO;
 497 
 498   devc = &dev_conf[dev];
 499 
 500   if (devc->opened)
 501     {
 502       printk ("MPU-401: Midi busy\n");
 503       return -EBUSY;
 504     }
 505 
 506   /*
 507      *  Verify that the device is really running.
 508      *  Some devices (such as Ensoniq SoundScape don't
 509      *  work before the on board processor (OBP) is initialized
 510      *  by downloadin it's microcode.
 511    */
 512 
 513   if (!devc->initialized)
 514     {
 515       if (mpu401_status (devc) == 0xff)         /* Bus float */
 516         {
 517           printk ("MPU-401: Device not initialized properly\n");
 518           return -EIO;
 519         }
 520       reset_mpu401 (devc);
 521     }
 522 
 523   irq2dev[devc->irq] = dev;
 524 
 525   if (midi_devs[dev]->coproc)
 526     if ((err = midi_devs[dev]->coproc->
 527          open (midi_devs[dev]->coproc->devc, COPR_MIDI)) < 0)
 528       {
 529         printk ("MPU-401: Can't access coprocessor device\n");
 530 
 531         return err;
 532       }
 533 
 534   set_uart_mode (dev, devc, 1);
 535   devc->mode = MODE_MIDI;
 536   devc->synthno = 0;
 537 
 538   mpu401_input_loop (devc);
 539 
 540   devc->inputintr = input;
 541   devc->opened = mode;
 542 
 543   return 0;
 544 }
 545 
 546 static void
 547 mpu401_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549   struct mpu_config *devc;
 550 
 551   devc = &dev_conf[dev];
 552 
 553   if (devc->uart_mode)
 554     reset_mpu401 (devc);        /*
 555                                  * This disables the UART mode
 556                                  */
 557   devc->mode = 0;
 558 
 559   devc->inputintr = NULL;
 560 
 561   if (midi_devs[dev]->coproc)
 562     midi_devs[dev]->coproc->close (midi_devs[dev]->coproc->devc, COPR_MIDI);
 563   devc->opened = 0;
 564 }
 565 
 566 static int
 567 mpu401_out (int dev, unsigned char midi_byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569   int             timeout;
 570   unsigned long   flags;
 571 
 572   struct mpu_config *devc;
 573 
 574   devc = &dev_conf[dev];
 575 
 576   /*
 577    * Sometimes it takes about 30000 loops before the output becomes ready
 578    * (After reset). Normally it takes just about 10 loops.
 579    */
 580 
 581   for (timeout = 30000; timeout > 0 && !output_ready (devc); timeout--);
 582 
 583   save_flags (flags);
 584   cli ();
 585   if (!output_ready (devc))
 586     {
 587       printk ("MPU-401: Send data timeout\n");
 588       restore_flags (flags);
 589       return 0;
 590     }
 591 
 592   write_data (devc, midi_byte);
 593   restore_flags (flags);
 594   return 1;
 595 }
 596 
 597 static int
 598 mpu401_command (int dev, mpu_command_rec * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 599 {
 600   int             i, timeout, ok;
 601   int             ret = 0;
 602   unsigned long   flags;
 603   struct mpu_config *devc;
 604 
 605   devc = &dev_conf[dev];
 606 
 607   if (devc->uart_mode)          /*
 608                                  * Not possible in UART mode
 609                                  */
 610     {
 611       printk ("MPU-401 commands not possible in the UART mode\n");
 612       return -EINVAL;
 613     }
 614 
 615   /*
 616    * Test for input since pending input seems to block the output.
 617    */
 618   if (input_avail (devc))
 619     mpu401_input_loop (devc);
 620 
 621   /*
 622    * Sometimes it takes about 30000 loops before the output becomes ready
 623    * (After reset). Normally it takes just about 10 loops.
 624    */
 625 
 626   timeout = 30000;
 627 retry:
 628   if (timeout-- <= 0)
 629     {
 630       printk ("MPU-401: Command (0x%x) timeout\n", (int) cmd->cmd);
 631       return -EIO;
 632     }
 633 
 634   save_flags (flags);
 635   cli ();
 636 
 637   if (!output_ready (devc))
 638     {
 639       restore_flags (flags);
 640       goto retry;
 641     }
 642 
 643   write_command (devc, cmd->cmd);
 644   ok = 0;
 645   for (timeout = 50000; timeout > 0 && !ok; timeout--)
 646     if (input_avail (devc))
 647       if (devc->opened && devc->mode == MODE_SYNTH)
 648         {
 649           if (mpu_input_scanner (devc, read_data (devc)) == MPU_ACK)
 650             ok = 1;
 651         }
 652       else
 653         {                       /* Device is not currently open. Use simplier method */
 654           if (read_data (devc) == MPU_ACK)
 655             ok = 1;
 656         }
 657 
 658   if (!ok)
 659     {
 660       restore_flags (flags);
 661       /*       printk ("MPU: No ACK to command (0x%x)\n", (int) cmd->cmd); */
 662       return -EIO;
 663     }
 664 
 665   if (cmd->nr_args)
 666     for (i = 0; i < cmd->nr_args; i++)
 667       {
 668         for (timeout = 3000; timeout > 0 && !output_ready (devc); timeout--);
 669 
 670         if (!mpu401_out (dev, cmd->data[i]))
 671           {
 672             restore_flags (flags);
 673             printk ("MPU: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
 674             return -EIO;
 675           }
 676       }
 677 
 678   ret = 0;
 679   cmd->data[0] = 0;
 680 
 681   if (cmd->nr_returns)
 682     for (i = 0; i < cmd->nr_returns; i++)
 683       {
 684         ok = 0;
 685         for (timeout = 5000; timeout > 0 && !ok; timeout--)
 686           if (input_avail (devc))
 687             {
 688               cmd->data[i] = read_data (devc);
 689               ok = 1;
 690             }
 691 
 692         if (!ok)
 693           {
 694             restore_flags (flags);
 695             /* printk ("MPU: No response(%d) to command (0x%x)\n", i, (int) cmd->cmd);  */
 696             return -EIO;
 697           }
 698       }
 699 
 700   restore_flags (flags);
 701 
 702   return ret;
 703 }
 704 
 705 static int
 706 mpu_cmd (int dev, int cmd, int data)
     /* [previous][next][first][last][top][bottom][index][help] */
 707 {
 708   int             ret;
 709 
 710   static mpu_command_rec rec;
 711 
 712   rec.cmd = cmd & 0xff;
 713   rec.nr_args = ((cmd & 0xf0) == 0xE0);
 714   rec.nr_returns = ((cmd & 0xf0) == 0xA0);
 715   rec.data[0] = data & 0xff;
 716 
 717   if ((ret = mpu401_command (dev, &rec)) < 0)
 718     {
 719       return ret;
 720     }
 721   return (unsigned char) rec.data[0];
 722 }
 723 
 724 static int
 725 mpu401_prefix_cmd (int dev, unsigned char status)
     /* [previous][next][first][last][top][bottom][index][help] */
 726 {
 727   struct mpu_config *devc = &dev_conf[dev];
 728 
 729   if (devc->uart_mode)
 730     return 1;
 731 
 732   if (status < 0xf0)
 733     {
 734       if (mpu_cmd (dev, 0xD0, 0) < 0)
 735         {
 736           return 0;
 737         }
 738 
 739       return 1;
 740     }
 741 
 742   switch (status)
 743     {
 744     case 0xF0:
 745       if (mpu_cmd (dev, 0xDF, 0) < 0)
 746         {
 747           return 0;
 748         }
 749 
 750       return 1;
 751       break;
 752 
 753     default:
 754       return 0;
 755     }
 756 
 757 }
 758 
 759 static int
 760 mpu401_start_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 761 {
 762   return 0;
 763 }
 764 
 765 static int
 766 mpu401_end_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 767 {
 768   return 0;
 769 }
 770 
 771 static int
 772 mpu401_ioctl (int dev, unsigned cmd, ioctl_arg arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 773 {
 774   struct mpu_config *devc;
 775 
 776   devc = &dev_conf[dev];
 777 
 778   switch (cmd)
 779     {
 780     case 1:
 781       memcpy_fromfs ((char *) init_sequence, &(((char *) arg)[0]), sizeof (init_sequence));
 782       return 0;
 783       break;
 784 
 785     case SNDCTL_MIDI_MPUMODE:
 786       if (!(devc->capabilities & MPU_CAP_INTLG))        /* No intelligent mode */
 787         {
 788           printk ("MPU-401: Intelligent mode not supported by the HW\n");
 789           return -EINVAL;
 790         }
 791       set_uart_mode (dev, devc, !get_fs_long ((long *) arg));
 792       return 0;
 793       break;
 794 
 795     case SNDCTL_MIDI_MPUCMD:
 796       {
 797         int             ret;
 798         mpu_command_rec rec;
 799 
 800         memcpy_fromfs ((char *) &rec, &(((char *) arg)[0]), sizeof (rec));
 801 
 802         if ((ret = mpu401_command (dev, &rec)) < 0)
 803           return ret;
 804 
 805         memcpy_tofs ((&((char *) arg)[0]), (char *) &rec, sizeof (rec));
 806         return 0;
 807       }
 808       break;
 809 
 810     default:
 811       return -EINVAL;
 812     }
 813 }
 814 
 815 static void
 816 mpu401_kick (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 817 {
 818 }
 819 
 820 static int
 821 mpu401_buffer_status (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 822 {
 823   return 0;                     /*
 824                                  * No data in buffers
 825                                  */
 826 }
 827 
 828 static int
 829 mpu_synth_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 830                  unsigned int cmd, ioctl_arg arg)
 831 {
 832   int             midi_dev;
 833   struct mpu_config *devc;
 834 
 835   midi_dev = synth_devs[dev]->midi_dev;
 836 
 837   if (midi_dev < 0 || midi_dev > num_midis)
 838     return -ENXIO;
 839 
 840   devc = &dev_conf[midi_dev];
 841 
 842   switch (cmd)
 843     {
 844 
 845     case SNDCTL_SYNTH_INFO:
 846       memcpy_tofs ((&((char *) arg)[0]), &mpu_synth_info[midi_dev], sizeof (struct synth_info));
 847 
 848       return 0;
 849       break;
 850 
 851     case SNDCTL_SYNTH_MEMAVL:
 852       return 0x7fffffff;
 853       break;
 854 
 855     default:
 856       return -EINVAL;
 857     }
 858 }
 859 
 860 static int
 861 mpu_synth_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 862 {
 863   int             midi_dev, err;
 864   struct mpu_config *devc;
 865 
 866   midi_dev = synth_devs[dev]->midi_dev;
 867 
 868   if (midi_dev < 0 || midi_dev > num_midis)
 869     {
 870       return -ENXIO;
 871     }
 872 
 873   devc = &dev_conf[midi_dev];
 874 
 875   /*
 876      *  Verify that the device is really running.
 877      *  Some devices (such as Ensoniq SoundScape don't
 878      *  work before the on board processor (OBP) is initialized
 879      *  by downloadin it's microcode.
 880    */
 881 
 882   if (!devc->initialized)
 883     {
 884       if (mpu401_status (devc) == 0xff)         /* Bus float */
 885         {
 886           printk ("MPU-401: Device not initialized properly\n");
 887           return -EIO;
 888         }
 889       reset_mpu401 (devc);
 890     }
 891 
 892   if (devc->opened)
 893     {
 894       printk ("MPU-401: Midi busy\n");
 895       return -EBUSY;
 896     }
 897 
 898   devc->mode = MODE_SYNTH;
 899   devc->synthno = dev;
 900 
 901   devc->inputintr = NULL;
 902   irq2dev[devc->irq] = midi_dev;
 903 
 904   if (midi_devs[midi_dev]->coproc)
 905     if ((err = midi_devs[midi_dev]->coproc->
 906          open (midi_devs[midi_dev]->coproc->devc, COPR_MIDI)) < 0)
 907       {
 908         printk ("MPU-401: Can't access coprocessor device\n");
 909 
 910         return err;
 911       }
 912 
 913   devc->opened = mode;
 914   reset_mpu401 (devc);
 915 
 916   if (mode & OPEN_READ)
 917     {
 918       mpu_cmd (midi_dev, 0x8B, 0);      /* Enable data in stop mode */
 919       mpu_cmd (midi_dev, 0x34, 0);      /* Return timing bytes in stop mode */
 920       mpu_cmd (midi_dev, 0x87, 0);      /* Enable pitch & controller */
 921     }
 922 
 923   return 0;
 924 }
 925 
 926 static void
 927 mpu_synth_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 928 {
 929   int             midi_dev;
 930   struct mpu_config *devc;
 931 
 932   midi_dev = synth_devs[dev]->midi_dev;
 933 
 934   devc = &dev_conf[midi_dev];
 935   mpu_cmd (midi_dev, 0x15, 0);  /* Stop recording, playback and MIDI */
 936   mpu_cmd (midi_dev, 0x8a, 0);  /* Disable data in stopped mode */
 937 
 938   devc->inputintr = NULL;
 939 
 940   if (midi_devs[midi_dev]->coproc)
 941     midi_devs[midi_dev]->coproc->close (midi_devs[midi_dev]->coproc->devc, COPR_MIDI);
 942   devc->opened = 0;
 943   devc->mode = 0;
 944 }
 945 
 946 #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
 947 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
 948 #include "midi_synth.h"
 949 
 950 static struct synth_operations mpu401_synth_proto =
 951 {
 952   NULL,
 953   0,
 954   SYNTH_TYPE_MIDI,
 955   0,
 956   mpu_synth_open,
 957   mpu_synth_close,
 958   mpu_synth_ioctl,
 959   midi_synth_kill_note,
 960   midi_synth_start_note,
 961   midi_synth_set_instr,
 962   midi_synth_reset,
 963   midi_synth_hw_control,
 964   midi_synth_load_patch,
 965   midi_synth_aftertouch,
 966   midi_synth_controller,
 967   midi_synth_panning,
 968   NULL,
 969   midi_synth_patchmgr,
 970   midi_synth_bender,
 971   NULL,                         /* alloc */
 972   midi_synth_setup_voice,
 973   midi_synth_send_sysex
 974 };
 975 
 976 static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
 977 
 978 static struct midi_operations mpu401_midi_proto =
 979 {
 980   {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
 981   NULL,
 982   {0},
 983   mpu401_open,
 984   mpu401_close,
 985   mpu401_ioctl,
 986   mpu401_out,
 987   mpu401_start_read,
 988   mpu401_end_read,
 989   mpu401_kick,
 990   NULL,
 991   mpu401_buffer_status,
 992   mpu401_prefix_cmd
 993 };
 994 
 995 static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
 996 
 997 static void
 998 mpu401_chk_version (struct mpu_config *devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 999 {
1000   int             tmp;
1001 
1002   devc->version = devc->revision = 0;
1003 
1004   if ((tmp = mpu_cmd (num_midis, 0xAC, 0)) < 0)
1005     return;
1006 
1007   if ((tmp & 0xf0) > 0x20)      /* Why it's larger than 2.x ??? */
1008     return;
1009 
1010   devc->version = tmp;
1011 
1012   if ((tmp = mpu_cmd (num_midis, 0xAD, 0)) < 0)
1013     {
1014       devc->version = 0;
1015       return;
1016     }
1017   devc->revision = tmp;
1018 }
1019 
1020 long
1021 attach_mpu401 (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1022 {
1023   unsigned long   flags;
1024   char            revision_char;
1025 
1026   struct mpu_config *devc;
1027 
1028   if (num_midis >= MAX_MIDI_DEV)
1029     {
1030       printk ("MPU-401: Too many midi devices detected\n");
1031       return mem_start;
1032     }
1033 
1034   devc = &dev_conf[num_midis];
1035 
1036   devc->base = hw_config->io_base;
1037   devc->osp = hw_config->osp;
1038   devc->irq = hw_config->irq;
1039   devc->opened = 0;
1040   devc->uart_mode = 0;
1041   devc->initialized = 0;
1042   devc->version = 0;
1043   devc->revision = 0;
1044   devc->capabilities = 0;
1045   devc->timer_flag = 0;
1046   devc->m_busy = 0;
1047   devc->m_state = ST_INIT;
1048   devc->shared_irq = hw_config->always_detect;
1049   devc->irq = hw_config->irq;
1050 
1051   if (devc->irq < 0)
1052     {
1053       devc->irq *= -1;
1054       devc->shared_irq = 1;
1055     }
1056   irq2dev[devc->irq] = num_midis;
1057 
1058   if (!hw_config->always_detect)
1059     {
1060       /* Verify the hardware again */
1061       if (!reset_mpu401 (devc))
1062         return mem_start;
1063 
1064       if (!devc->shared_irq)
1065         if (snd_set_irq_handler (devc->irq, mpuintr, "mpu401", devc->osp) < 0)
1066           {
1067             return mem_start;
1068           }
1069 
1070       save_flags (flags);
1071       cli ();
1072       mpu401_chk_version (devc);
1073       if (devc->version == 0)
1074         mpu401_chk_version (devc);
1075       restore_flags (flags);
1076     }
1077 
1078   request_region (hw_config->io_base, 2, "mpu401");
1079 
1080   if (devc->version != 0)
1081     if (mpu_cmd (num_midis, 0xC5, 0) >= 0)      /* Set timebase OK */
1082       if (mpu_cmd (num_midis, 0xE0, 120) >= 0)  /* Set tempo OK */
1083         devc->capabilities |= MPU_CAP_INTLG;    /* Supports intelligent mode */
1084 
1085 
1086   mpu401_synth_operations[num_midis] = (struct synth_operations *) (sound_mem_blocks[sound_num_blocks] = kmalloc (sizeof (struct synth_operations), GFP_KERNEL));
1087 
1088   if (sound_num_blocks < 1024)
1089     sound_num_blocks++;;
1090 
1091   if (mpu401_synth_operations[num_midis] == NULL)
1092     {
1093       printk ("mpu401: Can't allocate memory\n");
1094       return mem_start;
1095     }
1096 
1097   if (!(devc->capabilities & MPU_CAP_INTLG))    /* No intelligent mode */
1098     {
1099       memcpy ((char *) mpu401_synth_operations[num_midis],
1100               (char *) &std_midi_synth,
1101               sizeof (struct synth_operations));
1102     }
1103   else
1104     {
1105       memcpy ((char *) mpu401_synth_operations[num_midis],
1106               (char *) &mpu401_synth_proto,
1107               sizeof (struct synth_operations));
1108     }
1109 
1110   memcpy ((char *) &mpu401_midi_operations[num_midis],
1111           (char *) &mpu401_midi_proto,
1112           sizeof (struct midi_operations));
1113 
1114   mpu401_midi_operations[num_midis].converter =
1115     mpu401_synth_operations[num_midis];
1116 
1117   memcpy ((char *) &mpu_synth_info[num_midis],
1118           (char *) &mpu_synth_info_proto,
1119           sizeof (struct synth_info));
1120 
1121   n_mpu_devs++;
1122 
1123   if (devc->version == 0x20 && devc->revision >= 0x07)  /* MusicQuest interface */
1124     {
1125       int             ports = (devc->revision & 0x08) ? 32 : 16;
1126 
1127       devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1128         MPU_CAP_CLS | MPU_CAP_2PORT;
1129 
1130       revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1131       sprintf (mpu_synth_info[num_midis].name,
1132                "MQX-%d%c MIDI Interface #%d",
1133                ports,
1134                revision_char,
1135                n_mpu_devs);
1136     }
1137   else
1138     {
1139 
1140       revision_char = devc->revision ? devc->revision + '@' : ' ';
1141       if ((int) devc->revision > ('Z' - '@'))
1142         revision_char = '+';
1143 
1144       devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1145 
1146       sprintf (mpu_synth_info[num_midis].name,
1147                "MPU-401 %d.%d%c Midi interface #%d",
1148                (int) (devc->version & 0xf0) >> 4,
1149                devc->version & 0x0f,
1150                revision_char,
1151                n_mpu_devs);
1152     }
1153 
1154   strcpy (mpu401_midi_operations[num_midis].info.name,
1155           mpu_synth_info[num_midis].name);
1156 
1157   conf_printf (mpu_synth_info[num_midis].name, hw_config);
1158 
1159   mpu401_synth_operations[num_midis]->midi_dev = devc->devno = num_midis;
1160   mpu401_synth_operations[devc->devno]->info =
1161     &mpu_synth_info[devc->devno];
1162 
1163   if (devc->capabilities & MPU_CAP_INTLG)       /* Intelligent mode */
1164     mpu_timer_init (num_midis);
1165 
1166   irq2dev[devc->irq] = num_midis;
1167   midi_devs[num_midis++] = &mpu401_midi_operations[devc->devno];
1168   return mem_start;
1169 }
1170 
1171 static int
1172 reset_mpu401 (struct mpu_config *devc)
     /* [previous][next][first][last][top][bottom][index][help] */
1173 {
1174   unsigned long   flags;
1175   int             ok, timeout, n;
1176   int             timeout_limit;
1177 
1178   /*
1179    * Send the RESET command. Try again if no success at the first time.
1180    * (If the device is in the UART mode, it will not ack the reset cmd).
1181    */
1182 
1183   ok = 0;
1184 
1185   timeout_limit = devc->initialized ? 30000 : 100000;
1186   devc->initialized = 1;
1187 
1188   for (n = 0; n < 2 && !ok; n++)
1189     {
1190       for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1191         ok = output_ready (devc);
1192 
1193       write_command (devc, MPU_RESET);  /*
1194                                            * Send MPU-401 RESET Command
1195                                          */
1196 
1197       /*
1198        * Wait at least 25 msec. This method is not accurate so let's make the
1199        * loop bit longer. Cannot sleep since this is called during boot.
1200        */
1201 
1202       for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1203         {
1204           save_flags (flags);
1205           cli ();
1206           if (input_avail (devc))
1207             if (read_data (devc) == MPU_ACK)
1208               ok = 1;
1209           restore_flags (flags);
1210         }
1211 
1212     }
1213 
1214   devc->m_state = ST_INIT;
1215   devc->m_ptr = 0;
1216   devc->m_left = 0;
1217   devc->last_status = 0;
1218   devc->uart_mode = 0;
1219 
1220   return ok;
1221 }
1222 
1223 static void
1224 set_uart_mode (int dev, struct mpu_config *devc, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1225 {
1226   if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1227     {
1228       return;
1229     }
1230 
1231   if ((devc->uart_mode == 0) == (arg == 0))
1232     {
1233       return;                   /* Already set */
1234     }
1235 
1236   reset_mpu401 (devc);          /* This exits the uart mode */
1237 
1238   if (arg)
1239     {
1240       if (mpu_cmd (dev, UART_MODE_ON, 0) < 0)
1241         {
1242           printk ("MPU%d: Can't enter UART mode\n", devc->devno);
1243           devc->uart_mode = 0;
1244           return;
1245         }
1246     }
1247   devc->uart_mode = arg;
1248 
1249 }
1250 
1251 int
1252 probe_mpu401 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1253 {
1254   int             ok = 0;
1255   struct mpu_config tmp_devc;
1256 
1257   if (check_region (hw_config->io_base, 2))
1258     {
1259       printk ("\n\nmpu401.c: I/O port %x already in use\n\n",
1260               hw_config->io_base);
1261       return 0;
1262     }
1263 
1264   tmp_devc.base = hw_config->io_base;
1265   tmp_devc.irq = hw_config->irq;
1266   tmp_devc.initialized = 0;
1267   tmp_devc.opened = 0;
1268   tmp_devc.osp = hw_config->osp;
1269 
1270 #if defined(CONFIG_AEDSP16) && defined(AEDSP16_MPU401)
1271   /*
1272      * Initialize Audio Excel DSP 16 to MPU-401, before any operation.
1273    */
1274   InitAEDSP16_MPU401 (hw_config);
1275 #endif
1276 
1277   if (hw_config->always_detect)
1278     return 1;
1279 
1280   if (inb (hw_config->io_base + 1) == 0xff)
1281     {
1282       DDB (printk ("MPU401: Port %x looks dead.\n", hw_config->io_base));
1283       return 0;                 /* Just bus float? */
1284     }
1285 
1286   ok = reset_mpu401 (&tmp_devc);
1287 
1288   if (!ok)
1289     {
1290       DDB (printk ("MPU401: Reset failed on port %x\n", hw_config->io_base));
1291     }
1292 
1293   return ok;
1294 }
1295 
1296 void
1297 unload_mpu401 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1298 {
1299   release_region (hw_config->io_base, 2);
1300   if (hw_config->always_detect == 0 && hw_config->irq > 0)
1301     snd_release_irq (hw_config->irq);
1302 }
1303 
1304 /*****************************************************
1305  *      Timer stuff
1306  ****************************************************/
1307 
1308 #if defined(CONFIG_SEQUENCER)
1309 
1310 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1311 static volatile int curr_tempo, curr_timebase, hw_timebase;
1312 static int      max_timebase = 8;       /* 8*24=192 ppqn */
1313 static volatile unsigned long next_event_time;
1314 static volatile unsigned long curr_ticks, curr_clocks;
1315 static unsigned long prev_event_time;
1316 static int      metronome_mode;
1317 
1318 static unsigned long
1319 clocks2ticks (unsigned long clocks)
     /* [previous][next][first][last][top][bottom][index][help] */
1320 {
1321   /*
1322      * The MPU-401 supports just a limited set of possible timebase values.
1323      * Since the applications require more choices, the driver has to
1324      * program the HW to do it's best and to convert between the HW and
1325      * actual timebases.
1326    */
1327 
1328   return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1329 }
1330 
1331 static void
1332 set_timebase (int midi_dev, int val)
     /* [previous][next][first][last][top][bottom][index][help] */
1333 {
1334   int             hw_val;
1335 
1336   if (val < 48)
1337     val = 48;
1338   if (val > 1000)
1339     val = 1000;
1340 
1341   hw_val = val;
1342   hw_val = (hw_val + 12) / 24;
1343   if (hw_val > max_timebase)
1344     hw_val = max_timebase;
1345 
1346   if (mpu_cmd (midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1347     {
1348       printk ("MPU: Can't set HW timebase to %d\n", hw_val * 24);
1349       return;
1350     }
1351   hw_timebase = hw_val * 24;
1352   curr_timebase = val;
1353 
1354 }
1355 
1356 static void
1357 tmr_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1358 {
1359   unsigned long   flags;
1360 
1361   save_flags (flags);
1362   cli ();
1363   next_event_time = 0xffffffff;
1364   prev_event_time = 0;
1365   curr_ticks = curr_clocks = 0;
1366   restore_flags (flags);
1367 }
1368 
1369 static void
1370 set_timer_mode (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1371 {
1372   if (timer_mode & TMR_MODE_CLS)
1373     mpu_cmd (midi_dev, 0x3c, 0);        /* Use CLS sync */
1374   else if (timer_mode & TMR_MODE_SMPTE)
1375     mpu_cmd (midi_dev, 0x3d, 0);        /* Use SMPTE sync */
1376 
1377   if (timer_mode & TMR_INTERNAL)
1378     {
1379       mpu_cmd (midi_dev, 0x80, 0);      /* Use MIDI sync */
1380     }
1381   else
1382     {
1383       if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1384         {
1385           mpu_cmd (midi_dev, 0x82, 0);  /* Use MIDI sync */
1386           mpu_cmd (midi_dev, 0x91, 0);  /* Enable ext MIDI ctrl */
1387         }
1388       else if (timer_mode & TMR_MODE_FSK)
1389         mpu_cmd (midi_dev, 0x81, 0);    /* Use FSK sync */
1390     }
1391 }
1392 
1393 static void
1394 stop_metronome (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1395 {
1396   mpu_cmd (midi_dev, 0x84, 0);  /* Disable metronome */
1397 }
1398 
1399 static void
1400 setup_metronome (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1401 {
1402   int             numerator, denominator;
1403   int             clks_per_click, num_32nds_per_beat;
1404   int             beats_per_measure;
1405 
1406   numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1407   denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1408   clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1409   num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1410   beats_per_measure = (numerator * 4) >> denominator;
1411 
1412   if (!metronome_mode)
1413     mpu_cmd (midi_dev, 0x84, 0);        /* Disable metronome */
1414   else
1415     {
1416       mpu_cmd (midi_dev, 0xE4, clks_per_click);
1417       mpu_cmd (midi_dev, 0xE6, beats_per_measure);
1418       mpu_cmd (midi_dev, 0x83, 0);      /* Enable metronome without accents */
1419     }
1420 }
1421 
1422 static int
1423 mpu_start_timer (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1424 {
1425   tmr_reset ();
1426   set_timer_mode (midi_dev);
1427 
1428   if (tmr_running)
1429     return TIMER_NOT_ARMED;     /* Already running */
1430 
1431   if (timer_mode & TMR_INTERNAL)
1432     {
1433       mpu_cmd (midi_dev, 0x02, 0);      /* Send MIDI start */
1434       tmr_running = 1;
1435       return TIMER_NOT_ARMED;
1436     }
1437   else
1438     {
1439       mpu_cmd (midi_dev, 0x35, 0);      /* Enable mode messages to PC */
1440       mpu_cmd (midi_dev, 0x38, 0);      /* Enable sys common messages to PC */
1441       mpu_cmd (midi_dev, 0x39, 0);      /* Enable real time messages to PC */
1442       mpu_cmd (midi_dev, 0x97, 0);      /* Enable system exclusive messages to PC */
1443     }
1444 
1445   return TIMER_ARMED;
1446 }
1447 
1448 static int
1449 mpu_timer_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1450 {
1451   int             midi_dev = sound_timer_devs[dev]->devlink;
1452 
1453   if (timer_open)
1454     return -EBUSY;
1455 
1456   tmr_reset ();
1457   curr_tempo = 50;
1458   mpu_cmd (midi_dev, 0xE0, 50);
1459   curr_timebase = hw_timebase = 120;
1460   set_timebase (midi_dev, 120);
1461   timer_open = 1;
1462   metronome_mode = 0;
1463   set_timer_mode (midi_dev);
1464 
1465   mpu_cmd (midi_dev, 0xe7, 0x04);       /* Send all clocks to host */
1466   mpu_cmd (midi_dev, 0x95, 0);  /* Enable clock to host */
1467 
1468   return 0;
1469 }
1470 
1471 static void
1472 mpu_timer_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1473 {
1474   int             midi_dev = sound_timer_devs[dev]->devlink;
1475 
1476   timer_open = tmr_running = 0;
1477   mpu_cmd (midi_dev, 0x15, 0);  /* Stop all */
1478   mpu_cmd (midi_dev, 0x94, 0);  /* Disable clock to host */
1479   mpu_cmd (midi_dev, 0x8c, 0);  /* Disable measure end messages to host */
1480   stop_metronome (midi_dev);
1481 }
1482 
1483 static int
1484 mpu_timer_event (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
1485 {
1486   unsigned char   command = event[1];
1487   unsigned long   parm = *(unsigned int *) &event[4];
1488   int             midi_dev = sound_timer_devs[dev]->devlink;
1489 
1490   switch (command)
1491     {
1492     case TMR_WAIT_REL:
1493       parm += prev_event_time;
1494     case TMR_WAIT_ABS:
1495       if (parm > 0)
1496         {
1497           long            time;
1498 
1499           if (parm <= curr_ticks)       /* It's the time */
1500             return TIMER_NOT_ARMED;
1501 
1502           time = parm;
1503           next_event_time = prev_event_time = time;
1504 
1505           return TIMER_ARMED;
1506         }
1507       break;
1508 
1509     case TMR_START:
1510       if (tmr_running)
1511         break;
1512       return mpu_start_timer (midi_dev);
1513       break;
1514 
1515     case TMR_STOP:
1516       mpu_cmd (midi_dev, 0x01, 0);      /* Send MIDI stop */
1517       stop_metronome (midi_dev);
1518       tmr_running = 0;
1519       break;
1520 
1521     case TMR_CONTINUE:
1522       if (tmr_running)
1523         break;
1524       mpu_cmd (midi_dev, 0x03, 0);      /* Send MIDI continue */
1525       setup_metronome (midi_dev);
1526       tmr_running = 1;
1527       break;
1528 
1529     case TMR_TEMPO:
1530       if (parm)
1531         {
1532           if (parm < 8)
1533             parm = 8;
1534           if (parm > 250)
1535             parm = 250;
1536 
1537           if (mpu_cmd (midi_dev, 0xE0, parm) < 0)
1538             printk ("MPU: Can't set tempo to %d\n", (int) parm);
1539           curr_tempo = parm;
1540         }
1541       break;
1542 
1543     case TMR_ECHO:
1544       seq_copy_to_input (event, 8);
1545       break;
1546 
1547     case TMR_TIMESIG:
1548       if (metronome_mode)       /* Metronome enabled */
1549         {
1550           metronome_mode = parm;
1551           setup_metronome (midi_dev);
1552         }
1553       break;
1554 
1555     default:;
1556     }
1557 
1558   return TIMER_NOT_ARMED;
1559 }
1560 
1561 static unsigned long
1562 mpu_timer_get_time (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1563 {
1564   if (!timer_open)
1565     return 0;
1566 
1567   return curr_ticks;
1568 }
1569 
1570 static int
1571 mpu_timer_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
1572                  unsigned int command, ioctl_arg arg)
1573 {
1574   int             midi_dev = sound_timer_devs[dev]->devlink;
1575 
1576   switch (command)
1577     {
1578     case SNDCTL_TMR_SOURCE:
1579       {
1580         int             parm = (int) get_fs_long ((long *) arg) & timer_caps;
1581 
1582         if (parm != 0)
1583           {
1584             timer_mode = parm;
1585 
1586             if (timer_mode & TMR_MODE_CLS)
1587               mpu_cmd (midi_dev, 0x3c, 0);      /* Use CLS sync */
1588             else if (timer_mode & TMR_MODE_SMPTE)
1589               mpu_cmd (midi_dev, 0x3d, 0);      /* Use SMPTE sync */
1590           }
1591 
1592         return snd_ioctl_return ((int *) arg, timer_mode);
1593       }
1594       break;
1595 
1596     case SNDCTL_TMR_START:
1597       mpu_start_timer (midi_dev);
1598       return 0;
1599       break;
1600 
1601     case SNDCTL_TMR_STOP:
1602       tmr_running = 0;
1603       mpu_cmd (midi_dev, 0x01, 0);      /* Send MIDI stop */
1604       stop_metronome (midi_dev);
1605       return 0;
1606       break;
1607 
1608     case SNDCTL_TMR_CONTINUE:
1609       if (tmr_running)
1610         return 0;
1611       tmr_running = 1;
1612       mpu_cmd (midi_dev, 0x03, 0);      /* Send MIDI continue */
1613       return 0;
1614       break;
1615 
1616     case SNDCTL_TMR_TIMEBASE:
1617       {
1618         int             val = (int) get_fs_long ((long *) arg);
1619 
1620         if (val)
1621           set_timebase (midi_dev, val);
1622 
1623         return snd_ioctl_return ((int *) arg, curr_timebase);
1624       }
1625       break;
1626 
1627     case SNDCTL_TMR_TEMPO:
1628       {
1629         int             val = (int) get_fs_long ((long *) arg);
1630         int             ret;
1631 
1632         if (val)
1633           {
1634             if (val < 8)
1635               val = 8;
1636             if (val > 250)
1637               val = 250;
1638             if ((ret = mpu_cmd (midi_dev, 0xE0, val)) < 0)
1639               {
1640                 printk ("MPU: Can't set tempo to %d\n", (int) val);
1641                 return ret;
1642               }
1643 
1644             curr_tempo = val;
1645           }
1646 
1647         return snd_ioctl_return ((int *) arg, curr_tempo);
1648       }
1649       break;
1650 
1651     case SNDCTL_SEQ_CTRLRATE:
1652       if (get_fs_long ((long *) arg) != 0)      /* Can't change */
1653         return -EINVAL;
1654 
1655       return snd_ioctl_return ((int *) arg, ((curr_tempo * curr_timebase) + 30) / 60);
1656       break;
1657 
1658     case SNDCTL_TMR_METRONOME:
1659       metronome_mode = (int) get_fs_long ((long *) arg);
1660       setup_metronome (midi_dev);
1661       return 0;
1662       break;
1663 
1664     default:;
1665     }
1666 
1667   return -EINVAL;
1668 }
1669 
1670 static void
1671 mpu_timer_arm (int dev, long time)
     /* [previous][next][first][last][top][bottom][index][help] */
1672 {
1673   if (time < 0)
1674     time = curr_ticks + 1;
1675   else if (time <= curr_ticks)  /* It's the time */
1676     return;
1677 
1678   next_event_time = prev_event_time = time;
1679 
1680   return;
1681 }
1682 
1683 static struct sound_timer_operations mpu_timer =
1684 {
1685   {"MPU-401 Timer", 0},
1686   10,                           /* Priority */
1687   0,                            /* Local device link */
1688   mpu_timer_open,
1689   mpu_timer_close,
1690   mpu_timer_event,
1691   mpu_timer_get_time,
1692   mpu_timer_ioctl,
1693   mpu_timer_arm
1694 };
1695 
1696 static void
1697 mpu_timer_interrupt (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1698 {
1699 
1700   if (!timer_open)
1701     return;
1702 
1703   if (!tmr_running)
1704     return;
1705 
1706   curr_clocks++;
1707   curr_ticks = clocks2ticks (curr_clocks);
1708 
1709   if (curr_ticks >= next_event_time)
1710     {
1711       next_event_time = 0xffffffff;
1712       sequencer_timer (0);
1713     }
1714 }
1715 
1716 static void
1717 timer_ext_event (struct mpu_config *devc, int event, int parm)
     /* [previous][next][first][last][top][bottom][index][help] */
1718 {
1719   int             midi_dev = devc->devno;
1720 
1721   if (!devc->timer_flag)
1722     return;
1723 
1724   switch (event)
1725     {
1726     case TMR_CLOCK:
1727       printk ("<MIDI clk>");
1728       break;
1729 
1730     case TMR_START:
1731       printk ("Ext MIDI start\n");
1732       if (!tmr_running)
1733         if (timer_mode & TMR_EXTERNAL)
1734           {
1735             tmr_running = 1;
1736             setup_metronome (midi_dev);
1737             next_event_time = 0;
1738             STORE (SEQ_START_TIMER ());
1739           }
1740       break;
1741 
1742     case TMR_STOP:
1743       printk ("Ext MIDI stop\n");
1744       if (timer_mode & TMR_EXTERNAL)
1745         {
1746           tmr_running = 0;
1747           stop_metronome (midi_dev);
1748           STORE (SEQ_STOP_TIMER ());
1749         }
1750       break;
1751 
1752     case TMR_CONTINUE:
1753       printk ("Ext MIDI continue\n");
1754       if (timer_mode & TMR_EXTERNAL)
1755         {
1756           tmr_running = 1;
1757           setup_metronome (midi_dev);
1758           STORE (SEQ_CONTINUE_TIMER ());
1759         }
1760       break;
1761 
1762     case TMR_SPP:
1763       printk ("Songpos: %d\n", parm);
1764       if (timer_mode & TMR_EXTERNAL)
1765         {
1766           STORE (SEQ_SONGPOS (parm));
1767         }
1768       break;
1769     }
1770 }
1771 
1772 static void
1773 mpu_timer_init (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1774 {
1775   struct mpu_config *devc;
1776   int             n;
1777 
1778   devc = &dev_conf[midi_dev];
1779 
1780   if (timer_initialized)
1781     return;                     /* There is already a similar timer */
1782 
1783   timer_initialized = 1;
1784 
1785   mpu_timer.devlink = midi_dev;
1786   dev_conf[midi_dev].timer_flag = 1;
1787 
1788   if (num_sound_timers >= MAX_TIMER_DEV)
1789     n = 0;                      /* Overwrite the system timer */
1790   else
1791     n = num_sound_timers++;
1792   sound_timer_devs[n] = &mpu_timer;
1793 
1794   if (devc->version < 0x20)     /* Original MPU-401 */
1795     timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1796   else
1797     {
1798       /*
1799          * The version number 2.0 is used (at least) by the
1800          * MusicQuest cards and the Roland Super-MPU.
1801          *
1802          * MusicQuest has given a special meaning to the bits of the
1803          * revision number. The Super-MPU returns 0.
1804        */
1805 
1806       if (devc->revision)
1807         timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1808 
1809       if (devc->revision & 0x02)
1810         timer_caps |= TMR_MODE_CLS;
1811 
1812 
1813       if (devc->revision & 0x40)
1814         max_timebase = 10;      /* Has the 216 and 240 ppqn modes */
1815     }
1816 
1817   timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1818 
1819 }
1820 
1821 #endif
1822 
1823 
1824 
1825 #endif

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