root/drivers/sound/sb16_midi.c

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

DEFINITIONS

This source file includes following definitions.
  1. sb16midi_status
  2. sb16midi_cmd
  3. sb16midi_read
  4. sb16midi_write
  5. sb16midi_input_loop
  6. sb16midiintr
  7. sbmidiintr
  8. sb16midi_open
  9. sb16midi_close
  10. sb16midi_out
  11. sb16midi_start_read
  12. sb16midi_end_read
  13. sb16midi_ioctl
  14. sb16midi_kick
  15. sb16midi_buffer_status
  16. attach_sb16midi
  17. reset_sb16midi
  18. probe_sb16midi
  19. unload_sb16midi

   1 /*
   2  * sound/sb16_midi.c
   3  *
   4  * The low level driver for the MPU-401 UART emulation of the SB16.
   5  */
   6 /*
   7  * Copyright by Hannu Savolainen 1993-1996
   8  *
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions are
  11  * met: 1. Redistributions of source code must retain the above copyright
  12  * notice, this list of conditions and the following disclaimer. 2.
  13  * Redistributions in binary form must reproduce the above copyright notice,
  14  * this list of conditions and the following disclaimer in the documentation
  15  * and/or other materials provided with the distribution.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 #include <linux/config.h>
  30 
  31 
  32 #include "sound_config.h"
  33 
  34 #if defined(CONFIG_SB) && defined(CONFIG_MIDI)
  35 
  36 #include "sb.h"
  37 
  38 #define DATAPORT   (sb16midi_base)
  39 #define COMDPORT   (sb16midi_base+1)
  40 #define STATPORT   (sb16midi_base+1)
  41 
  42 extern int     *sb_osp;
  43 static int      sb16midi_base = 0;
  44 
  45 static int 
  46 sb16midi_status (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48   return inb (STATPORT);
  49 }
  50 #define input_avail()           (!(sb16midi_status()&INPUT_AVAIL))
  51 #define output_ready()          (!(sb16midi_status()&OUTPUT_READY))
  52 static void 
  53 sb16midi_cmd (unsigned char cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55   outb (cmd, COMDPORT);
  56 }
  57 static int 
  58 sb16midi_read (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60   return inb (DATAPORT);
  61 }
  62 static void 
  63 sb16midi_write (unsigned char byte)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65   outb (byte, DATAPORT);
  66 }
  67 
  68 #define OUTPUT_READY    0x40
  69 #define INPUT_AVAIL     0x80
  70 #define MPU_ACK         0xFE
  71 #define MPU_RESET       0xFF
  72 #define UART_MODE_ON    0x3F
  73 
  74 static int      sb16midi_opened = 0;
  75 static int      sb16midi_detected = 0;
  76 static int      my_dev;
  77 extern int      sbc_base;
  78 
  79 extern int      Jazz16_detected;
  80 extern int      AudioDrive;
  81 
  82 static int      reset_sb16midi (void);
  83 static void     (*midi_input_intr) (int dev, unsigned char data);
  84 static volatile unsigned char input_byte;
  85 
  86 static void
  87 sb16midi_input_loop (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89   while (input_avail ())
  90     {
  91       unsigned char   c = sb16midi_read ();
  92 
  93       if (c == MPU_ACK)
  94         input_byte = c;
  95       else if (sb16midi_opened & OPEN_READ && midi_input_intr)
  96         midi_input_intr (my_dev, c);
  97     }
  98 }
  99 
 100 void
 101 sb16midiintr (int unit)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103   if (sb16midi_base == 0)
 104     return;
 105 
 106   if (input_avail ())
 107     sb16midi_input_loop ();
 108 }
 109 
 110 void
 111 sbmidiintr (int irq, struct pt_regs *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113   if (input_avail ())
 114     sb16midi_input_loop ();
 115 }
 116 
 117 static int
 118 sb16midi_open (int dev, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 119                void            (*input) (int dev, unsigned char data),
 120                void            (*output) (int dev)
 121 )
 122 {
 123   if (sb16midi_opened)
 124     {
 125       return -EBUSY;
 126     }
 127 
 128   sb16midi_input_loop ();
 129 
 130   midi_input_intr = input;
 131   sb16midi_opened = mode;
 132 
 133   return 0;
 134 }
 135 
 136 static void
 137 sb16midi_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139   sb16midi_opened = 0;
 140 }
 141 
 142 static int
 143 sb16midi_out (int dev, unsigned char midi_byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145   int             timeout;
 146   unsigned long   flags;
 147 
 148   /*
 149    * Test for input since pending input seems to block the output.
 150    */
 151 
 152   save_flags (flags);
 153   cli ();
 154 
 155   if (input_avail ())
 156     sb16midi_input_loop ();
 157 
 158   restore_flags (flags);
 159 
 160   /*
 161    * Sometimes it takes about 13000 loops before the output becomes ready
 162    * (After reset). Normally it takes just about 10 loops.
 163    */
 164 
 165   for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);    /*
 166                                                                          * Wait
 167                                                                          */
 168 
 169   if (!output_ready ())
 170     {
 171       printk ("MPU-401: Timeout\n");
 172       return 0;
 173     }
 174 
 175   sb16midi_write (midi_byte);
 176   return 1;
 177 }
 178 
 179 static int
 180 sb16midi_start_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182   return 0;
 183 }
 184 
 185 static int
 186 sb16midi_end_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188   return 0;
 189 }
 190 
 191 static int
 192 sb16midi_ioctl (int dev, unsigned cmd, caddr_t arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 193 {
 194   return -EINVAL;
 195 }
 196 
 197 static void
 198 sb16midi_kick (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200 }
 201 
 202 static int
 203 sb16midi_buffer_status (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 204 {
 205   return 0;                     /*
 206                                  * No data in buffers
 207                                  */
 208 }
 209 
 210 #define MIDI_SYNTH_NAME "SoundBlaster MPU"
 211 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
 212 #include "midi_synth.h"
 213 
 214 static struct midi_operations sb16midi_operations =
 215 {
 216   {"SoundBlaster MPU", 0, 0, SNDCARD_SB16MIDI},
 217   &std_midi_synth,
 218   {0},
 219   sb16midi_open,
 220   sb16midi_close,
 221   sb16midi_ioctl,
 222   sb16midi_out,
 223   sb16midi_start_read,
 224   sb16midi_end_read,
 225   sb16midi_kick,
 226   NULL,
 227   sb16midi_buffer_status,
 228   NULL
 229 };
 230 
 231 long
 232 attach_sb16midi (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234   int             ok, timeout;
 235   unsigned long   flags;
 236 
 237   sb16midi_base = hw_config->io_base;
 238 
 239   if (!sb16midi_detected)
 240     return mem_start;
 241 
 242   request_region (hw_config->io_base, 4, "SB MIDI");
 243 
 244   save_flags (flags);
 245   cli ();
 246   for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);    /*
 247                                                                          * Wait
 248                                                                          */
 249   input_byte = 0;
 250   sb16midi_cmd (UART_MODE_ON);
 251 
 252   ok = 0;
 253   for (timeout = 50000; timeout > 0 && !ok; timeout--)
 254     if (input_byte == MPU_ACK)
 255       ok = 1;
 256     else if (input_avail ())
 257       if (sb16midi_read () == MPU_ACK)
 258         ok = 1;
 259 
 260   restore_flags (flags);
 261 
 262   if (num_midis >= MAX_MIDI_DEV)
 263     {
 264       printk ("Sound: Too many midi devices detected\n");
 265       return mem_start;
 266     }
 267 
 268   conf_printf ("SoundBlaster MPU-401", hw_config);
 269 
 270   std_midi_synth.midi_dev = my_dev = num_midis;
 271   midi_devs[num_midis++] = &sb16midi_operations;
 272   return mem_start;
 273 }
 274 
 275 static int
 276 reset_sb16midi (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278   int             ok, timeout, n;
 279 
 280   /*
 281    * Send the RESET command. Try again if no success at the first time.
 282    */
 283 
 284   if (inb (STATPORT) == 0xff)
 285     return 0;
 286 
 287   ok = 0;
 288 
 289   /*save_flags(flags);cli(); */
 290 
 291   for (n = 0; n < 2 && !ok; n++)
 292     {
 293       for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);        /*
 294                                                                                  * Wait
 295                                                                                  */
 296       input_byte = 0;
 297       sb16midi_cmd (MPU_RESET); /*
 298                                  * Send MPU-401 RESET Command
 299                                  */
 300 
 301       /*
 302        * Wait at least 25 msec. This method is not accurate so let's make the
 303        * loop bit longer. Cannot sleep since this is called during boot.
 304        */
 305 
 306       for (timeout = 50000; timeout > 0 && !ok; timeout--)
 307         if (input_byte == MPU_ACK)      /* Interrupt */
 308           ok = 1;
 309         else if (input_avail ())
 310           if (sb16midi_read () == MPU_ACK)
 311             ok = 1;
 312 
 313     }
 314 
 315   sb16midi_opened = 0;
 316   if (ok)
 317     sb16midi_input_loop ();     /*
 318                                  * Flush input before enabling interrupts
 319                                  */
 320 
 321   /* restore_flags(flags); */
 322 
 323   return ok;
 324 }
 325 
 326 int
 327 probe_sb16midi (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329   int             ok = 0;
 330   extern int      sbc_major;
 331 
 332   extern void     ess_midi_init (struct address_info *hw_config);
 333   extern void     Jazz16_midi_init (struct address_info *hw_config);
 334 
 335   if (check_region (hw_config->io_base, 4))
 336     return 0;
 337 
 338   if (AudioDrive)
 339     ess_midi_init (hw_config);
 340   else if (Jazz16_detected)
 341     Jazz16_midi_init (hw_config);
 342   else if (sbc_major < 4)
 343     return 0;                   /* Not a SB16 */
 344 
 345   sb16midi_base = hw_config->io_base;
 346 
 347   if (sb_get_irq () < 0)
 348     return 0;
 349 
 350   ok = reset_sb16midi ();
 351 
 352   sb16midi_detected = ok;
 353   return ok;
 354 }
 355 
 356 void
 357 unload_sb16midi (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359   release_region (hw_config->io_base, 4);
 360 }
 361 
 362 #endif

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