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

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