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, void *dev_id, 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         {
1093           printk ("MPU401: Device didn't respond\n");
1094           return mem_start;
1095         }
1096 
1097       if (!devc->shared_irq)
1098         if (snd_set_irq_handler (devc->irq, mpuintr, "mpu401", devc->osp) < 0)
1099           {
1100             printk ("MPU401: Failed to allocate IRQ%d\n", devc->irq);
1101             return mem_start;
1102           }
1103 
1104       save_flags (flags);
1105       cli ();
1106       mpu401_chk_version (devc);
1107       if (devc->version == 0)
1108         mpu401_chk_version (devc);
1109       restore_flags (flags);
1110     }
1111 
1112   request_region (hw_config->io_base, 2, "mpu401");
1113 
1114   if (devc->version != 0)
1115     if (mpu_cmd (num_midis, 0xC5, 0) >= 0)      /* Set timebase OK */
1116       if (mpu_cmd (num_midis, 0xE0, 120) >= 0)  /* Set tempo OK */
1117         devc->capabilities |= MPU_CAP_INTLG;    /* Supports intelligent mode */
1118 
1119 
1120   mpu401_synth_operations[num_midis] = (struct synth_operations *) (sound_mem_blocks[sound_num_blocks] = kmalloc (sizeof (struct synth_operations), GFP_KERNEL));
1121 
1122   if (sound_num_blocks < 1024)
1123     sound_num_blocks++;;
1124 
1125   if (mpu401_synth_operations[num_midis] == NULL)
1126     {
1127       printk ("mpu401: Can't allocate memory\n");
1128       return mem_start;
1129     }
1130 
1131   if (!(devc->capabilities & MPU_CAP_INTLG))    /* No intelligent mode */
1132     {
1133       memcpy ((char *) mpu401_synth_operations[num_midis],
1134               (char *) &std_midi_synth,
1135               sizeof (struct synth_operations));
1136     }
1137   else
1138     {
1139       memcpy ((char *) mpu401_synth_operations[num_midis],
1140               (char *) &mpu401_synth_proto,
1141               sizeof (struct synth_operations));
1142     }
1143 
1144   memcpy ((char *) &mpu401_midi_operations[num_midis],
1145           (char *) &mpu401_midi_proto,
1146           sizeof (struct midi_operations));
1147 
1148   mpu401_midi_operations[num_midis].converter =
1149     mpu401_synth_operations[num_midis];
1150 
1151   memcpy ((char *) &mpu_synth_info[num_midis],
1152           (char *) &mpu_synth_info_proto,
1153           sizeof (struct synth_info));
1154 
1155   n_mpu_devs++;
1156 
1157   if (devc->version == 0x20 && devc->revision >= 0x07)  /* MusicQuest interface */
1158     {
1159       int             ports = (devc->revision & 0x08) ? 32 : 16;
1160 
1161       devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
1162         MPU_CAP_CLS | MPU_CAP_2PORT;
1163 
1164       revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
1165       sprintf (mpu_synth_info[num_midis].name,
1166                "MQX-%d%c MIDI Interface #%d",
1167                ports,
1168                revision_char,
1169                n_mpu_devs);
1170     }
1171   else
1172     {
1173 
1174       revision_char = devc->revision ? devc->revision + '@' : ' ';
1175       if ((int) devc->revision > ('Z' - '@'))
1176         revision_char = '+';
1177 
1178       devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
1179 
1180       sprintf (mpu_synth_info[num_midis].name,
1181                "MPU-401 %d.%d%c Midi interface #%d",
1182                (int) (devc->version & 0xf0) >> 4,
1183                devc->version & 0x0f,
1184                revision_char,
1185                n_mpu_devs);
1186     }
1187 
1188   strcpy (mpu401_midi_operations[num_midis].info.name,
1189           mpu_synth_info[num_midis].name);
1190 
1191   conf_printf (mpu_synth_info[num_midis].name, hw_config);
1192 
1193   mpu401_synth_operations[num_midis]->midi_dev = devc->devno = num_midis;
1194   mpu401_synth_operations[devc->devno]->info =
1195     &mpu_synth_info[devc->devno];
1196 
1197   if (devc->capabilities & MPU_CAP_INTLG)       /* Intelligent mode */
1198     mpu_timer_init (num_midis);
1199 
1200   irq2dev[devc->irq] = num_midis;
1201   midi_devs[num_midis++] = &mpu401_midi_operations[devc->devno];
1202   return mem_start;
1203 }
1204 
1205 static int
1206 reset_mpu401 (struct mpu_config *devc)
     /* [previous][next][first][last][top][bottom][index][help] */
1207 {
1208   unsigned long   flags;
1209   int             ok, timeout, n;
1210   int             timeout_limit;
1211 
1212   /*
1213    * Send the RESET command. Try again if no success at the first time.
1214    * (If the device is in the UART mode, it will not ack the reset cmd).
1215    */
1216 
1217   ok = 0;
1218 
1219   timeout_limit = devc->initialized ? 30000 : 100000;
1220   devc->initialized = 1;
1221 
1222   for (n = 0; n < 2 && !ok; n++)
1223     {
1224       for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
1225         ok = output_ready (devc);
1226 
1227       write_command (devc, MPU_RESET);  /*
1228                                            * Send MPU-401 RESET Command
1229                                          */
1230 
1231       /*
1232        * Wait at least 25 msec. This method is not accurate so let's make the
1233        * loop bit longer. Cannot sleep since this is called during boot.
1234        */
1235 
1236       for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
1237         {
1238           save_flags (flags);
1239           cli ();
1240           if (input_avail (devc))
1241             if (read_data (devc) == MPU_ACK)
1242               ok = 1;
1243           restore_flags (flags);
1244         }
1245 
1246     }
1247 
1248   devc->m_state = ST_INIT;
1249   devc->m_ptr = 0;
1250   devc->m_left = 0;
1251   devc->last_status = 0;
1252   devc->uart_mode = 0;
1253 
1254   return ok;
1255 }
1256 
1257 static void
1258 set_uart_mode (int dev, struct mpu_config *devc, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1259 {
1260   if (!arg && (devc->capabilities & MPU_CAP_INTLG))
1261     {
1262       return;
1263     }
1264 
1265   if ((devc->uart_mode == 0) == (arg == 0))
1266     {
1267       return;                   /* Already set */
1268     }
1269 
1270   reset_mpu401 (devc);          /* This exits the uart mode */
1271 
1272   if (arg)
1273     {
1274       if (mpu_cmd (dev, UART_MODE_ON, 0) < 0)
1275         {
1276           printk ("MPU%d: Can't enter UART mode\n", devc->devno);
1277           devc->uart_mode = 0;
1278           return;
1279         }
1280     }
1281   devc->uart_mode = arg;
1282 
1283 }
1284 
1285 int
1286 probe_mpu401 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1287 {
1288   int             ok = 0;
1289   struct mpu_config tmp_devc;
1290 
1291   if (check_region (hw_config->io_base, 2))
1292     {
1293       printk ("\n\nmpu401.c: I/O port %x already in use\n\n",
1294               hw_config->io_base);
1295       return 0;
1296     }
1297 
1298   tmp_devc.base = hw_config->io_base;
1299   tmp_devc.irq = hw_config->irq;
1300   tmp_devc.initialized = 0;
1301   tmp_devc.opened = 0;
1302   tmp_devc.osp = hw_config->osp;
1303 
1304   if (hw_config->always_detect)
1305     return 1;
1306 
1307   if (inb (hw_config->io_base + 1) == 0xff)
1308     {
1309       DDB (printk ("MPU401: Port %x looks dead.\n", hw_config->io_base));
1310       return 0;                 /* Just bus float? */
1311     }
1312 
1313   ok = reset_mpu401 (&tmp_devc);
1314 
1315   if (!ok)
1316     {
1317       DDB (printk ("MPU401: Reset failed on port %x\n", hw_config->io_base));
1318     }
1319 
1320   return ok;
1321 }
1322 
1323 void
1324 unload_mpu401 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326   release_region (hw_config->io_base, 2);
1327   if (hw_config->always_detect == 0 && hw_config->irq > 0)
1328     snd_release_irq (hw_config->irq);
1329 }
1330 
1331 /*****************************************************
1332  *      Timer stuff
1333  ****************************************************/
1334 
1335 #if defined(CONFIG_SEQUENCER)
1336 
1337 static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
1338 static volatile int curr_tempo, curr_timebase, hw_timebase;
1339 static int      max_timebase = 8;       /* 8*24=192 ppqn */
1340 static volatile unsigned long next_event_time;
1341 static volatile unsigned long curr_ticks, curr_clocks;
1342 static unsigned long prev_event_time;
1343 static int      metronome_mode;
1344 
1345 static unsigned long
1346 clocks2ticks (unsigned long clocks)
     /* [previous][next][first][last][top][bottom][index][help] */
1347 {
1348   /*
1349      * The MPU-401 supports just a limited set of possible timebase values.
1350      * Since the applications require more choices, the driver has to
1351      * program the HW to do it's best and to convert between the HW and
1352      * actual timebases.
1353    */
1354 
1355   return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
1356 }
1357 
1358 static void
1359 set_timebase (int midi_dev, int val)
     /* [previous][next][first][last][top][bottom][index][help] */
1360 {
1361   int             hw_val;
1362 
1363   if (val < 48)
1364     val = 48;
1365   if (val > 1000)
1366     val = 1000;
1367 
1368   hw_val = val;
1369   hw_val = (hw_val + 12) / 24;
1370   if (hw_val > max_timebase)
1371     hw_val = max_timebase;
1372 
1373   if (mpu_cmd (midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
1374     {
1375       printk ("MPU: Can't set HW timebase to %d\n", hw_val * 24);
1376       return;
1377     }
1378   hw_timebase = hw_val * 24;
1379   curr_timebase = val;
1380 
1381 }
1382 
1383 static void
1384 tmr_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1385 {
1386   unsigned long   flags;
1387 
1388   save_flags (flags);
1389   cli ();
1390   next_event_time = (unsigned long) -1;
1391   prev_event_time = 0;
1392   curr_ticks = curr_clocks = 0;
1393   restore_flags (flags);
1394 }
1395 
1396 static void
1397 set_timer_mode (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1398 {
1399   if (timer_mode & TMR_MODE_CLS)
1400     mpu_cmd (midi_dev, 0x3c, 0);        /* Use CLS sync */
1401   else if (timer_mode & TMR_MODE_SMPTE)
1402     mpu_cmd (midi_dev, 0x3d, 0);        /* Use SMPTE sync */
1403 
1404   if (timer_mode & TMR_INTERNAL)
1405     {
1406       mpu_cmd (midi_dev, 0x80, 0);      /* Use MIDI sync */
1407     }
1408   else
1409     {
1410       if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
1411         {
1412           mpu_cmd (midi_dev, 0x82, 0);  /* Use MIDI sync */
1413           mpu_cmd (midi_dev, 0x91, 0);  /* Enable ext MIDI ctrl */
1414         }
1415       else if (timer_mode & TMR_MODE_FSK)
1416         mpu_cmd (midi_dev, 0x81, 0);    /* Use FSK sync */
1417     }
1418 }
1419 
1420 static void
1421 stop_metronome (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1422 {
1423   mpu_cmd (midi_dev, 0x84, 0);  /* Disable metronome */
1424 }
1425 
1426 static void
1427 setup_metronome (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1428 {
1429   int             numerator, denominator;
1430   int             clks_per_click, num_32nds_per_beat;
1431   int             beats_per_measure;
1432 
1433   numerator = ((unsigned) metronome_mode >> 24) & 0xff;
1434   denominator = ((unsigned) metronome_mode >> 16) & 0xff;
1435   clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
1436   num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
1437   beats_per_measure = (numerator * 4) >> denominator;
1438 
1439   if (!metronome_mode)
1440     mpu_cmd (midi_dev, 0x84, 0);        /* Disable metronome */
1441   else
1442     {
1443       mpu_cmd (midi_dev, 0xE4, clks_per_click);
1444       mpu_cmd (midi_dev, 0xE6, beats_per_measure);
1445       mpu_cmd (midi_dev, 0x83, 0);      /* Enable metronome without accents */
1446     }
1447 }
1448 
1449 static int
1450 mpu_start_timer (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1451 {
1452   tmr_reset ();
1453   set_timer_mode (midi_dev);
1454 
1455   if (tmr_running)
1456     return TIMER_NOT_ARMED;     /* Already running */
1457 
1458   if (timer_mode & TMR_INTERNAL)
1459     {
1460       mpu_cmd (midi_dev, 0x02, 0);      /* Send MIDI start */
1461       tmr_running = 1;
1462       return TIMER_NOT_ARMED;
1463     }
1464   else
1465     {
1466       mpu_cmd (midi_dev, 0x35, 0);      /* Enable mode messages to PC */
1467       mpu_cmd (midi_dev, 0x38, 0);      /* Enable sys common messages to PC */
1468       mpu_cmd (midi_dev, 0x39, 0);      /* Enable real time messages to PC */
1469       mpu_cmd (midi_dev, 0x97, 0);      /* Enable system exclusive messages to PC */
1470     }
1471 
1472   return TIMER_ARMED;
1473 }
1474 
1475 static int
1476 mpu_timer_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1477 {
1478   int             midi_dev = sound_timer_devs[dev]->devlink;
1479 
1480   if (timer_open)
1481     return -EBUSY;
1482 
1483   tmr_reset ();
1484   curr_tempo = 50;
1485   mpu_cmd (midi_dev, 0xE0, 50);
1486   curr_timebase = hw_timebase = 120;
1487   set_timebase (midi_dev, 120);
1488   timer_open = 1;
1489   metronome_mode = 0;
1490   set_timer_mode (midi_dev);
1491 
1492   mpu_cmd (midi_dev, 0xe7, 0x04);       /* Send all clocks to host */
1493   mpu_cmd (midi_dev, 0x95, 0);  /* Enable clock to host */
1494 
1495   return 0;
1496 }
1497 
1498 static void
1499 mpu_timer_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1500 {
1501   int             midi_dev = sound_timer_devs[dev]->devlink;
1502 
1503   timer_open = tmr_running = 0;
1504   mpu_cmd (midi_dev, 0x15, 0);  /* Stop all */
1505   mpu_cmd (midi_dev, 0x94, 0);  /* Disable clock to host */
1506   mpu_cmd (midi_dev, 0x8c, 0);  /* Disable measure end messages to host */
1507   stop_metronome (midi_dev);
1508 }
1509 
1510 static int
1511 mpu_timer_event (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
1512 {
1513   unsigned char   command = event[1];
1514   unsigned long   parm = *(unsigned int *) &event[4];
1515   int             midi_dev = sound_timer_devs[dev]->devlink;
1516 
1517   switch (command)
1518     {
1519     case TMR_WAIT_REL:
1520       parm += prev_event_time;
1521     case TMR_WAIT_ABS:
1522       if (parm > 0)
1523         {
1524           long            time;
1525 
1526           if (parm <= curr_ticks)       /* It's the time */
1527             return TIMER_NOT_ARMED;
1528 
1529           time = parm;
1530           next_event_time = prev_event_time = time;
1531 
1532           return TIMER_ARMED;
1533         }
1534       break;
1535 
1536     case TMR_START:
1537       if (tmr_running)
1538         break;
1539       return mpu_start_timer (midi_dev);
1540       break;
1541 
1542     case TMR_STOP:
1543       mpu_cmd (midi_dev, 0x01, 0);      /* Send MIDI stop */
1544       stop_metronome (midi_dev);
1545       tmr_running = 0;
1546       break;
1547 
1548     case TMR_CONTINUE:
1549       if (tmr_running)
1550         break;
1551       mpu_cmd (midi_dev, 0x03, 0);      /* Send MIDI continue */
1552       setup_metronome (midi_dev);
1553       tmr_running = 1;
1554       break;
1555 
1556     case TMR_TEMPO:
1557       if (parm)
1558         {
1559           if (parm < 8)
1560             parm = 8;
1561           if (parm > 250)
1562             parm = 250;
1563 
1564           if (mpu_cmd (midi_dev, 0xE0, parm) < 0)
1565             printk ("MPU: Can't set tempo to %d\n", (int) parm);
1566           curr_tempo = parm;
1567         }
1568       break;
1569 
1570     case TMR_ECHO:
1571       seq_copy_to_input (event, 8);
1572       break;
1573 
1574     case TMR_TIMESIG:
1575       if (metronome_mode)       /* Metronome enabled */
1576         {
1577           metronome_mode = parm;
1578           setup_metronome (midi_dev);
1579         }
1580       break;
1581 
1582     default:;
1583     }
1584 
1585   return TIMER_NOT_ARMED;
1586 }
1587 
1588 static unsigned long
1589 mpu_timer_get_time (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1590 {
1591   if (!timer_open)
1592     return 0;
1593 
1594   return curr_ticks;
1595 }
1596 
1597 static int
1598 mpu_timer_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
1599                  unsigned int command, caddr_t arg)
1600 {
1601   int             midi_dev = sound_timer_devs[dev]->devlink;
1602 
1603   switch (command)
1604     {
1605     case SNDCTL_TMR_SOURCE:
1606       {
1607         int             parm = (int) get_fs_long ((long *) arg) & timer_caps;
1608 
1609         if (parm != 0)
1610           {
1611             timer_mode = parm;
1612 
1613             if (timer_mode & TMR_MODE_CLS)
1614               mpu_cmd (midi_dev, 0x3c, 0);      /* Use CLS sync */
1615             else if (timer_mode & TMR_MODE_SMPTE)
1616               mpu_cmd (midi_dev, 0x3d, 0);      /* Use SMPTE sync */
1617           }
1618 
1619         return snd_ioctl_return ((int *) arg, timer_mode);
1620       }
1621       break;
1622 
1623     case SNDCTL_TMR_START:
1624       mpu_start_timer (midi_dev);
1625       return 0;
1626       break;
1627 
1628     case SNDCTL_TMR_STOP:
1629       tmr_running = 0;
1630       mpu_cmd (midi_dev, 0x01, 0);      /* Send MIDI stop */
1631       stop_metronome (midi_dev);
1632       return 0;
1633       break;
1634 
1635     case SNDCTL_TMR_CONTINUE:
1636       if (tmr_running)
1637         return 0;
1638       tmr_running = 1;
1639       mpu_cmd (midi_dev, 0x03, 0);      /* Send MIDI continue */
1640       return 0;
1641       break;
1642 
1643     case SNDCTL_TMR_TIMEBASE:
1644       {
1645         int             val = (int) get_fs_long ((long *) arg);
1646 
1647         if (val)
1648           set_timebase (midi_dev, val);
1649 
1650         return snd_ioctl_return ((int *) arg, curr_timebase);
1651       }
1652       break;
1653 
1654     case SNDCTL_TMR_TEMPO:
1655       {
1656         int             val = (int) get_fs_long ((long *) arg);
1657         int             ret;
1658 
1659         if (val)
1660           {
1661             if (val < 8)
1662               val = 8;
1663             if (val > 250)
1664               val = 250;
1665             if ((ret = mpu_cmd (midi_dev, 0xE0, val)) < 0)
1666               {
1667                 printk ("MPU: Can't set tempo to %d\n", (int) val);
1668                 return ret;
1669               }
1670 
1671             curr_tempo = val;
1672           }
1673 
1674         return snd_ioctl_return ((int *) arg, curr_tempo);
1675       }
1676       break;
1677 
1678     case SNDCTL_SEQ_CTRLRATE:
1679       if (get_fs_long ((long *) arg) != 0)      /* Can't change */
1680         return -EINVAL;
1681 
1682       return snd_ioctl_return ((int *) arg, ((curr_tempo * curr_timebase) + 30) / 60);
1683       break;
1684 
1685     case SNDCTL_TMR_METRONOME:
1686       metronome_mode = (int) get_fs_long ((long *) arg);
1687       setup_metronome (midi_dev);
1688       return 0;
1689       break;
1690 
1691     default:;
1692     }
1693 
1694   return -EINVAL;
1695 }
1696 
1697 static void
1698 mpu_timer_arm (int dev, long time)
     /* [previous][next][first][last][top][bottom][index][help] */
1699 {
1700   if (time < 0)
1701     time = curr_ticks + 1;
1702   else if (time <= curr_ticks)  /* It's the time */
1703     return;
1704 
1705   next_event_time = prev_event_time = time;
1706 
1707   return;
1708 }
1709 
1710 static struct sound_timer_operations mpu_timer =
1711 {
1712   {"MPU-401 Timer", 0},
1713   10,                           /* Priority */
1714   0,                            /* Local device link */
1715   mpu_timer_open,
1716   mpu_timer_close,
1717   mpu_timer_event,
1718   mpu_timer_get_time,
1719   mpu_timer_ioctl,
1720   mpu_timer_arm
1721 };
1722 
1723 static void
1724 mpu_timer_interrupt (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1725 {
1726 
1727   if (!timer_open)
1728     return;
1729 
1730   if (!tmr_running)
1731     return;
1732 
1733   curr_clocks++;
1734   curr_ticks = clocks2ticks (curr_clocks);
1735 
1736   if (curr_ticks >= next_event_time)
1737     {
1738       next_event_time = (unsigned long) -1;
1739       sequencer_timer (0);
1740     }
1741 }
1742 
1743 static void
1744 timer_ext_event (struct mpu_config *devc, int event, int parm)
     /* [previous][next][first][last][top][bottom][index][help] */
1745 {
1746   int             midi_dev = devc->devno;
1747 
1748   if (!devc->timer_flag)
1749     return;
1750 
1751   switch (event)
1752     {
1753     case TMR_CLOCK:
1754       printk ("<MIDI clk>");
1755       break;
1756 
1757     case TMR_START:
1758       printk ("Ext MIDI start\n");
1759       if (!tmr_running)
1760         if (timer_mode & TMR_EXTERNAL)
1761           {
1762             tmr_running = 1;
1763             setup_metronome (midi_dev);
1764             next_event_time = 0;
1765             STORE (SEQ_START_TIMER ());
1766           }
1767       break;
1768 
1769     case TMR_STOP:
1770       printk ("Ext MIDI stop\n");
1771       if (timer_mode & TMR_EXTERNAL)
1772         {
1773           tmr_running = 0;
1774           stop_metronome (midi_dev);
1775           STORE (SEQ_STOP_TIMER ());
1776         }
1777       break;
1778 
1779     case TMR_CONTINUE:
1780       printk ("Ext MIDI continue\n");
1781       if (timer_mode & TMR_EXTERNAL)
1782         {
1783           tmr_running = 1;
1784           setup_metronome (midi_dev);
1785           STORE (SEQ_CONTINUE_TIMER ());
1786         }
1787       break;
1788 
1789     case TMR_SPP:
1790       printk ("Songpos: %d\n", parm);
1791       if (timer_mode & TMR_EXTERNAL)
1792         {
1793           STORE (SEQ_SONGPOS (parm));
1794         }
1795       break;
1796     }
1797 }
1798 
1799 static void
1800 mpu_timer_init (int midi_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1801 {
1802   struct mpu_config *devc;
1803   int             n;
1804 
1805   devc = &dev_conf[midi_dev];
1806 
1807   if (timer_initialized)
1808     return;                     /* There is already a similar timer */
1809 
1810   timer_initialized = 1;
1811 
1812   mpu_timer.devlink = midi_dev;
1813   dev_conf[midi_dev].timer_flag = 1;
1814 
1815   if (num_sound_timers >= MAX_TIMER_DEV)
1816     n = 0;                      /* Overwrite the system timer */
1817   else
1818     n = num_sound_timers++;
1819   sound_timer_devs[n] = &mpu_timer;
1820 
1821   if (devc->version < 0x20)     /* Original MPU-401 */
1822     timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
1823   else
1824     {
1825       /*
1826          * The version number 2.0 is used (at least) by the
1827          * MusicQuest cards and the Roland Super-MPU.
1828          *
1829          * MusicQuest has given a special meaning to the bits of the
1830          * revision number. The Super-MPU returns 0.
1831        */
1832 
1833       if (devc->revision)
1834         timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
1835 
1836       if (devc->revision & 0x02)
1837         timer_caps |= TMR_MODE_CLS;
1838 
1839 
1840       if (devc->revision & 0x40)
1841         max_timebase = 10;      /* Has the 216 and 240 ppqn modes */
1842     }
1843 
1844   timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
1845 
1846 }
1847 
1848 #endif
1849 
1850 
1851 
1852 #endif

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