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

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