root/drivers/sound/sb16_midi.c

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

DEFINITIONS

This source file includes following definitions.
  1. sb16midi_input_loop
  2. sb16midiintr
  3. sb16midi_open
  4. sb16midi_close
  5. sb16midi_out
  6. sb16midi_command
  7. sb16midi_start_read
  8. sb16midi_end_read
  9. sb16midi_ioctl
  10. sb16midi_kick
  11. sb16midi_buffer_status
  12. attach_sb16midi
  13. reset_sb16midi
  14. probe_sb16midi

   1 /*
   2  * sound/sb16_midi.c
   3  *
   4  * The low level driver for the MPU-401 UART emulation of the SB16.
   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 #include "sound_config.h"
  31 
  32 #ifdef CONFIGURE_SOUNDCARD
  33 
  34 #if !defined(EXCLUDE_SB) && !defined(EXCLUDE_SB16) && !defined(EXCLUDE_MIDI)
  35 
  36 #define DATAPORT   (sb16midi_base)      /* MPU-401 Data I/O Port on IBM */
  37 #define COMDPORT   (sb16midi_base+1)    /* MPU-401 Command Port on IBM */
  38 #define STATPORT   (sb16midi_base+1)    /* MPU-401 Status Port on IBM */
  39 
  40 #define sb16midi_status()               INB(STATPORT)
  41 #define input_avail()           (!(sb16midi_status()&INPUT_AVAIL))
  42 #define output_ready()          (!(sb16midi_status()&OUTPUT_READY))
  43 #define sb16midi_cmd(cmd)               OUTB(cmd, COMDPORT)
  44 #define sb16midi_read()         INB(DATAPORT)
  45 #define sb16midi_write(byte)    OUTB(byte, DATAPORT)
  46 
  47 #define OUTPUT_READY    0x40    /* Mask for Data Read Redy Bit */
  48 #define INPUT_AVAIL     0x80    /* Mask for Data Send Ready Bit */
  49 #define MPU_ACK         0xFE    /* MPU-401 Acknowledge Response */
  50 #define MPU_RESET       0xFF    /* MPU-401 Total Reset Command */
  51 #define UART_MODE_ON    0x3F    /* MPU-401 "Dumb UART Mode" */
  52 
  53 static int      sb16midi_opened = 0;
  54 static int      sb16midi_base = 0x330;
  55 static int      sb16midi_detected = 0;
  56 static int      my_dev;
  57 
  58 static int      reset_sb16midi (void);
  59 static void     (*midi_input_intr) (int dev, unsigned char data);
  60 
  61 extern int      sbc_major;
  62 
  63 static void
  64 sb16midi_input_loop (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66 
  67   while (input_avail ())
  68     {
  69       unsigned char   c = sb16midi_read ();
  70 
  71       if (sb16midi_opened & OPEN_READ)
  72         midi_input_intr (my_dev, c);
  73     }
  74 }
  75 
  76 void
  77 sb16midiintr (int unit)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   if (input_avail ())
  80     sb16midi_input_loop ();
  81 }
  82 
  83 static int
  84 sb16midi_open (int dev, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
  85                void            (*input) (int dev, unsigned char data),
  86                void            (*output) (int dev)
  87 )
  88 {
  89   if (sb16midi_opened)
  90     {
  91       return RET_ERROR (EBUSY);
  92     }
  93 
  94   sb16midi_input_loop ();
  95 
  96   midi_input_intr = input;
  97   sb16midi_opened = mode;
  98 
  99   return 0;
 100 }
 101 
 102 static void
 103 sb16midi_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105   sb16midi_opened = 0;
 106 }
 107 
 108 static int
 109 sb16midi_out (int dev, unsigned char midi_byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111   int             timeout;
 112   unsigned long   flags;
 113 
 114   /*
 115    * Test for input since pending input seems to block the output.
 116    */
 117 
 118   DISABLE_INTR (flags);
 119 
 120   if (input_avail ())
 121     sb16midi_input_loop ();
 122 
 123   RESTORE_INTR (flags);
 124 
 125   /*
 126    * Sometimes it takes about 13000 loops before the output becomes ready
 127    * (After reset). Normally it takes just about 10 loops.
 128    */
 129 
 130   for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);    /* Wait */
 131 
 132   if (!output_ready ())
 133     {
 134       printk ("MPU-401: Timeout\n");
 135       return 0;
 136     }
 137 
 138   sb16midi_write (midi_byte);
 139   return 1;
 140 }
 141 
 142 static int
 143 sb16midi_command (int dev, unsigned char midi_byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145   return 1;
 146 }
 147 
 148 static int
 149 sb16midi_start_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151   return 0;
 152 }
 153 
 154 static int
 155 sb16midi_end_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157   return 0;
 158 }
 159 
 160 static int
 161 sb16midi_ioctl (int dev, unsigned cmd, unsigned arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163   return RET_ERROR (EINVAL);
 164 }
 165 
 166 static void
 167 sb16midi_kick (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169 }
 170 
 171 static int
 172 sb16midi_buffer_status (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174   return 0;                     /* No data in buffers */
 175 }
 176 
 177 static struct midi_operations sb16midi_operations =
 178 {
 179   {"SoundBlaster MPU-401", 0, 0, SNDCARD_SB16MIDI},
 180   sb16midi_open,
 181   sb16midi_close,
 182   sb16midi_ioctl,
 183   sb16midi_out,
 184   sb16midi_start_read,
 185   sb16midi_end_read,
 186   sb16midi_kick,
 187   sb16midi_command,
 188   sb16midi_buffer_status
 189 };
 190 
 191 
 192 long
 193 attach_sb16midi (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195   int             ok, timeout;
 196   unsigned long   flags;
 197 
 198   sb16midi_base = hw_config->io_base;
 199 
 200   if (!sb16midi_detected)
 201     return RET_ERROR (EIO);
 202 
 203   DISABLE_INTR (flags);
 204   for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);    /* Wait */
 205   sb16midi_cmd (UART_MODE_ON);
 206 
 207   ok = 0;
 208   for (timeout = 50000; timeout > 0 && !ok; timeout--)
 209     if (input_avail ())
 210       if (sb16midi_read () == MPU_ACK)
 211         ok = 1;
 212 
 213   RESTORE_INTR (flags);
 214 
 215   printk (" <SoundBlaster MPU-401>");
 216 
 217   my_dev = num_midis;
 218   midi_devs[num_midis++] = &sb16midi_operations;
 219   return mem_start;
 220 }
 221 
 222 static int
 223 reset_sb16midi (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 224 {
 225   unsigned long   flags;
 226   int             ok, timeout, n;
 227 
 228   /*
 229    * Send the RESET command. Try again if no success at the first time.
 230    */
 231 
 232   ok = 0;
 233 
 234   DISABLE_INTR (flags);
 235 
 236   for (n = 0; n < 2 && !ok; n++)
 237     {
 238       for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);        /* Wait */
 239       sb16midi_cmd (MPU_RESET); /* Send MPU-401 RESET Command */
 240 
 241       /*
 242        * Wait at least 25 msec. This method is not accurate so let's make the
 243        * loop bit longer. Cannot sleep since this is called during boot.
 244        */
 245 
 246       for (timeout = 50000; timeout > 0 && !ok; timeout--)
 247         if (input_avail ())
 248           if (sb16midi_read () == MPU_ACK)
 249             ok = 1;
 250 
 251     }
 252 
 253   sb16midi_opened = 0;
 254   if (ok)
 255     sb16midi_input_loop ();     /* Flush input before enabling interrupts */
 256 
 257   RESTORE_INTR (flags);
 258 
 259   return ok;
 260 }
 261 
 262 
 263 int
 264 probe_sb16midi (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266   int             ok = 0;
 267 
 268   sb16midi_base = hw_config->io_base;
 269   if (sbc_major < 4)
 270     return 0;                   /* SB16 not detected */
 271 
 272   if (sb_get_irq () < 0)
 273     return 0;
 274 
 275   ok = reset_sb16midi ();
 276 
 277   sb16midi_detected = ok;
 278   return ok;
 279 }
 280 
 281 #endif
 282 
 283 #endif

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