home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / sound / mpu401.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-06  |  35.7 KB  |  1,723 lines

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