root/drivers/sound/mpu401.c

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

DEFINITIONS

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

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

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