root/drivers/sound/mpu401.c

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

DEFINITIONS

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

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

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