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. enter_uart_mode
  17. attach_sb16midi
  18. reset_sb16midi
  19. probe_sb16midi
  20. 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, void *dev_id, 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   while (input_avail ())
 129     sb16midi_read ();
 130 
 131   midi_input_intr = input;
 132   sb16midi_opened = mode;
 133 
 134   return 0;
 135 }
 136 
 137 static void
 138 sb16midi_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140   sb16midi_opened = 0;
 141 }
 142 
 143 static int
 144 sb16midi_out (int dev, unsigned char midi_byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146   int             timeout;
 147   unsigned long   flags;
 148 
 149   /*
 150    * Test for input since pending input seems to block the output.
 151    */
 152 
 153   save_flags (flags);
 154   cli ();
 155 
 156   if (input_avail ())
 157     sb16midi_input_loop ();
 158 
 159   restore_flags (flags);
 160 
 161   /*
 162    * Sometimes it takes about 13000 loops before the output becomes ready
 163    * (After reset). Normally it takes just about 10 loops.
 164    */
 165 
 166   for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);    /*
 167                                                                          * Wait
 168                                                                          */
 169 
 170   if (!output_ready ())
 171     {
 172       printk ("MPU-401: Timeout\n");
 173       return 0;
 174     }
 175 
 176   sb16midi_write (midi_byte);
 177   return 1;
 178 }
 179 
 180 static int
 181 sb16midi_start_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183   return 0;
 184 }
 185 
 186 static int
 187 sb16midi_end_read (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 188 {
 189   return 0;
 190 }
 191 
 192 static int
 193 sb16midi_ioctl (int dev, unsigned cmd, caddr_t arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195   return -EINVAL;
 196 }
 197 
 198 static void
 199 sb16midi_kick (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201 }
 202 
 203 static int
 204 sb16midi_buffer_status (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206   return 0;                     /*
 207                                  * No data in buffers
 208                                  */
 209 }
 210 
 211 #define MIDI_SYNTH_NAME "SoundBlaster MPU"
 212 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
 213 #include "midi_synth.h"
 214 
 215 static struct midi_operations sb16midi_operations =
 216 {
 217   {"SoundBlaster MPU", 0, 0, SNDCARD_SB16MIDI},
 218   &std_midi_synth,
 219   {0},
 220   sb16midi_open,
 221   sb16midi_close,
 222   sb16midi_ioctl,
 223   sb16midi_out,
 224   sb16midi_start_read,
 225   sb16midi_end_read,
 226   sb16midi_kick,
 227   NULL,
 228   sb16midi_buffer_status,
 229   NULL
 230 };
 231 
 232 static void
 233 enter_uart_mode (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235   int             ok, timeout;
 236   unsigned long   flags;
 237 
 238   save_flags (flags);
 239   cli ();
 240   for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);
 241 
 242   input_byte = 0;
 243   sb16midi_cmd (UART_MODE_ON);
 244 
 245   ok = 0;
 246   for (timeout = 50000; timeout > 0 && !ok; timeout--)
 247     if (input_byte == MPU_ACK)
 248       ok = 1;
 249     else if (input_avail ())
 250       if (sb16midi_read () == MPU_ACK)
 251         ok = 1;
 252 
 253   restore_flags (flags);
 254 }
 255 
 256 long
 257 attach_sb16midi (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 258 {
 259   sb16midi_base = hw_config->io_base;
 260 
 261   if (!sb16midi_detected)
 262     return mem_start;
 263 
 264   request_region (hw_config->io_base, 4, "SB MIDI");
 265   enter_uart_mode ();
 266 
 267   if (num_midis >= MAX_MIDI_DEV)
 268     {
 269       printk ("Sound: Too many midi devices detected\n");
 270       return mem_start;
 271     }
 272 
 273   conf_printf ("SoundBlaster MPU-401", hw_config);
 274 
 275   std_midi_synth.midi_dev = my_dev = num_midis;
 276   midi_devs[num_midis++] = &sb16midi_operations;
 277   return mem_start;
 278 }
 279 
 280 static int
 281 reset_sb16midi (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283   int             ok, timeout, n;
 284 
 285   /*
 286    * Send the RESET command. Try again if no success at the first time.
 287    */
 288 
 289   ok = 0;
 290 
 291   /*save_flags(flags);cli(); */
 292 
 293   for (n = 0; n < 2 && !ok; n++)
 294     {
 295       for (timeout = 30000; timeout < 0 && !output_ready (); timeout--);        /*
 296                                                                                  * Wait
 297                                                                                  */
 298       input_byte = 0;
 299       sb16midi_cmd (MPU_RESET); /*
 300                                  * Send MPU-401 RESET Command
 301                                  */
 302 
 303       /*
 304        * Wait at least 25 msec. This method is not accurate so let's make the
 305        * loop bit longer. Cannot sleep since this is called during boot.
 306        */
 307 
 308       for (timeout = 50000; timeout > 0 && !ok; timeout--)
 309         if (input_byte == MPU_ACK)      /* Interrupt */
 310           ok = 1;
 311         else if (input_avail ())
 312           if (sb16midi_read () == MPU_ACK)
 313             ok = 1;
 314 
 315     }
 316 
 317   sb16midi_opened = 0;
 318   if (ok)
 319     sb16midi_input_loop ();     /*
 320                                  * Flush input before enabling interrupts
 321                                  */
 322 
 323   /* restore_flags(flags); */
 324 
 325   return ok;
 326 }
 327 
 328 int
 329 probe_sb16midi (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 330 {
 331   int             ok = 0;
 332   extern int      sbc_major;
 333 
 334   extern void     ess_midi_init (struct address_info *hw_config);
 335   extern void     Jazz16_midi_init (struct address_info *hw_config);
 336 
 337   if (check_region (hw_config->io_base, 4))
 338     return 0;
 339 
 340   if (AudioDrive)
 341     ess_midi_init (hw_config);
 342   else if (Jazz16_detected)
 343     Jazz16_midi_init (hw_config);
 344   else if (sbc_major < 4)
 345     return 0;                   /* Not a SB16 */
 346 
 347   sb16midi_base = hw_config->io_base;
 348 
 349   if (sb_get_irq () < 0)
 350     return 0;
 351 
 352   ok = reset_sb16midi ();
 353 
 354   sb16midi_detected = ok;
 355   return ok;
 356 }
 357 
 358 void
 359 unload_sb16midi (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361   release_region (hw_config->io_base, 4);
 362 }
 363 
 364 #endif

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